Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[Word] text layout: Vertical distance from top of line to baseline?

Status
Not open for further replies.

Bob9876

Technical User
Sep 2, 2012
1
CH
Hello,
Having read a number of very interesting expert postings on the forum here I am hopeful that some of you might point me in the right direction with my little problem.
For a project I'd like to position shapes in MS Word as well aligned with the baseline of the words in a line of text as possible (I added the red lines and red text for illustration purposes to the Word screen shot below.).

160466


Shapes and other objects floating above the main text are always anchored to a paragraph in Word and may be positioned relative to this paragraph (see dialogue box in the screen shot), i.e. relative to the top left corner of the anchoring paragraph. Now I'd like to retrieve or calculate the vertical distance between the anchoring point and the baseline of any line (or at least of the first line) in the paragraph with a Visual Basic for Applications (VBA) macro and place the value into the variable for the vertical offset of the shape position (in suitable units).
[ol][li]Retrieving the vertical distance from top of line to baseline
Ideally Word would expose the vertical distance from the top of a line objectto its baseline, but unfortunately I did not find any reference to a property or method on the web. I did find hints to some hidden properties however ([→] ._GethWnd). Maybe someone of you knows a [']secret['] and helpful property?[/li]
[li]Calculating the vertical distance from top of line to baseline
If Word doesn't tell, maybe it's possible to emulate what Word is doing? Apparently Word uses vertical metrics from tables inside font files to dimension text lines and position the baseline inside the text lines. Using VBA code I managed to read the Textmetric and the OutlineTextMetric structures with WinAPI calls ([→] gdi32). Some information in these structures if obviously essential, but unfortunately I have not found out which exactly. Some trial and error seems to suggest that Word 2010 uses hhea.Ascender (or OS/2.WinAscent?) + hhea.Descender (or OS/2.WinDescent?) + hhea.LineGap to calculate the line height of lines with single line spacing and puts the baseline at hhea.Ascender (or OS/2.WinAscent?) + hhea.LineGap from the top. But there are many more variables influencing vertical positioning: different fonts and/or font sizes in the same line, the presence of superscripts or vertically shifted characters, line spacing (1.5, 2, etc.; points before or after the paragraph; points after of the previous paragraph), the location of the paragraph inside a table cell or in normal text, etc.
So reliably emulating Word's calculations for the vertical placement of a baseline in all possible cases seems a difficult task, involving a lot of testing – except if one knows Word's placement algorithm! Does anybody here know? [/li][/ol]
Once the Word document is complete it will be converted to a PDF. So ultimately the baseline and the shape should be aligned (as well as possible) in the PDF – the alignment on the screen during editing the Word document is less important (WYSIWYG is not always perfect).
Thank you for your comments.
 
Hi Bob,

To do that you would need to query the font's metadata directly. In a given font, a character is simply a rectangular area within which some code that defines a series of bezier curves and lines to draw the character shape. Word doesn't need to know what the baseline is - it only needs the logic for determining the rectangle's size, then applying the character's code to the area thus defined.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top