4

Hello there! I don't know as much about fonts as I could, and I was wondering if some of the people at Tex, who know so much about Typography, could give me a hand.

Basically, I have some questions on how to calculate the line spacing between lines in a Docx file, and I think my problem may be from parsing fonts. First let me explain the whole dilemma.

My requirement is to exactly match Word, not necessarily the OOXML spec, in the spacing between lines in a simple paragraph.

In order to try to do this, I have built a tool to analyze the differences between my layout and Word's layout. To do so it does the following:

  1. First it generates a (or many) docx files.
  2. Next it creates PDFs from the docx files. It uses Word to render the docx to PDF, and my program to render the docx to PDF. "word.pdf", and "me.pdf"
  3. Then it analyzes the resulting PDFs for differences in layout.

So, my tool would say:

  1. Create a document "template.docx" with 1000 "a" characters in a single run of text with the same properties.
  2. Make a "word.pdf" and "me.pdf" from this docx
  3. Calculate info from the PDFs, in particular, calculating the line spacing in terms of the calculated leading between a line's ascent and the previous line's descent (our "Ascent + Descent" are almost identical, so all that differs is the whitespace between lines). I often think of it as the line's whitespace...

This tool showed me that the leading varies greatly from font to font.

To depict this, I used the tool to make thousands of these comparisons, in particular generating for:

  1. For each font in system
  2. For "a", "y", and a mix of letters and spaces.
  3. For different font sizes.
  4. For different line spacing types (Single, One and a half, and Double)

I was hoping to find groupings, such as "this type of font has 1.3 times my calculation of leading".

I was able to conclude far less than I had hoped, and was wondering if you could help me further with the issue of calculating line spacing. I'm providing you with a file that is best downloaded and opened in Excel while using the filters in the header row. Note that its not totally complete, there are missing entries, but I doubt they will be a problem for anyone, and I'm going to regenerate it soon but its pretty slow, so I'm finishing up some changes to it first.

Here is a comparison of the layout of our software, vs the layout of Word's for every font installed on my system, etc.

I'm not positive, but I believe the issue could be one of the following:

  1. Word is using a different process than we are to calculate the "leading" of a font. We don't parse the font files ourselves, instead rely on libraries to get font sizing information, and perhaps in the "world of font files" I am missing something, and word is parsing the fonts directly and differently.
  2. Word has some sort of lookup table that handles groups of fonts, or an algorithm, that scales a font's leading up or down based on some criteria I am unaware of.
  3. Word is using an additional criteria besides leading, ascent, and descent, to determine line spacing.

What do you think? Do you think this could be an issue in parsing font files or something related to fonts? Am I missing a key piece of understanding? Thanks so much!

Please feel free to email me: nathanb at windward dot net

Thank you so much for your time!

  • Welcome to TeX - LaTeX! To be on topic here, you should reword the question to ask how this is done in TeX/LaTeX. Otherwise you could ask on the fonts & typography, or perhaps graphic design, groups. – Andrew Swann Aug 29 '16 at 16:57
3

Almost always in tex the baseline spacing is set by the document class independent of the font.

So for example in the default article class, \normalsize is 10pt font on a 12pt baseline, as a result of the marked line of code (from size10.clo)

\renewcommand\normalsize{%
   \@setfontsize\normalsize\@xpt\@xiipt%   <<<< here 10pt on 12pt baseline
   \abovedisplayskip 10\p@ \@plus2\p@ \@minus5\p@
   \abovedisplayshortskip \z@ \@plus3\p@
   \belowdisplayshortskip 6\p@ \@plus3\p@ \@minus3\p@
   \belowdisplayskip \abovedisplayskip
   \let\@listi\@listI}

This setting is not normally changed, even if you change the default font from computer modern to some other font family.

  • Interesting, this is not how Docx works. However, not selecting as the answer because it doesn't really answer my question. Thanks for trying though, I understand that this question is perhaps off-topic slightly. – Nathan Bellowe Aug 29 '16 at 17:15
  • Interesting, I didn't know that is how Tex worked! – Nathan Bellowe Aug 29 '16 at 17:23
  • 1
    @NathanBellowe I don't mind if you accept or not:-) But it answers the question in the title quite exactly, the shown code is exactly how the default latex line spacing is calculated. It may be that the answer isn't helpful for your real use case, sorry about that, I can only tell it as it is:-) – David Carlisle Aug 29 '16 at 20:06
  • I suppose thats a valid viewpoint too. I'm going to give it a day, but then I'll likely add you as the accepted answer – Nathan Bellowe Aug 30 '16 at 7:37
3

I don't know docx and certainly have no wish to know anything about Word but I do know a little about LaTeX and also traditional typesetting with lead type.

A piece of lead type (a sort) is basically a rectangular bar just under an inch long with a height corresponding to the height of the font and a width large enough to accomodate the width of the letter. For, say, a 12pt font the height will be 12pt. The particular character is a raised portion on the face of the sort fitting inside the height/width area. The height and width of the printed character are, then, less than the that of the lead type itself. In some fonts the area used for the character are much smaller than that available are while in others the character will be much closer to the rectangular boundary.

For printing the sorts are arranged in a line, then the next line is assembled, and so on. If the typesetter does nothing then the line spacing is that specified by the font designer (no extra spacing between the lines of the sorts), otherwise the typesetter can put thin strips of lead between each line to space them farther apart.

I believe that it is the same with digital fonts. The font designer will create a font with a specific leading. The typesetting program may then, if it wishes, expand or contract the natural space between the lines of text.

  • the situation is a bit different with xetex/luatex and opentype fonts but with (pdf)tex and tfm font metric based setting, the metrics do not specify a default line spacing for a font. – David Carlisle Aug 29 '16 at 20:03
3

Background

I was dealing with Chinese typesetting these days, and came across what I believed the answer to your question (and somewhat related to mine as well).

Some of the referenced websites in this answer are written in Chinese, but I think you can read some of the graphics (written in English) and numerals inside.

Apparently, you and I are not the only ones who were curious about how Word does its math. I had peers who write in LaTeX but are forced to obey the ridiculous university rules, and those rules are ironically based on Word. Regarding this matter, I have to agree with Alan Munn’s comments in this answer. But anyway …

How does Microsoft Word calculate single line spacing?

First of all, I’ll assume you understand that one point in Word equals 1bp (big point) in TeX, which is 1/72 of an inch.

On my Windows OS, I used the SimSun font (which is in your spreadsheet) with single line spacing and 0pt before and after paragraph. By using various font sizes, actually measuring the text height, counting the number of lines in a page, recording the font size, and finally converting between units, I was able to deduce that

Under a single line spacing setting, the leading of the SimSun font appeared to be about 1.3 multiples of the font size.

As I found out later, my observation was supported by this answer to “How does Office Word calculate single line spacing based on the info in the ‘OS/2’ table? (Title translated)” Let me explain:

  1. Word has access to the font metrics given by a so-called “table OS/2”, in which three values are needed for this answer: units/em, winAscent and winDescent.
    • For the SimSun font, the virtual em-square has width units/em = 256, with winAscent = 220 and winDescent = 36. This information can partly be found here on GitHub (search “FONT METRICS” within the webpage).
  2. To compute the single line height, Word will add extra paddings above the winAscent and below the winDescent, with each being the rounding units of 0.15 * (winAscent + winDescent).
    • For the SimSun font, they are 38 units each.
  3. Then, the line height is set as paddingabove + winAscent + winDescent + paddingbelow.
    • For the SimSun font, this turns out to be 332 units.
  4. Leading happens to equal line height in length. Divide leading by the em-size to obtain the “basic ratio”.
    • For the SimSun font, 332/256 = 1.296875, Aha! The 1.3-conjecture confirmed!
  5. If you are on Mac OS X, font metrics is read from “table hhea”.
  6. There is another value LineGap. I only tested the fonts with TypoLineGap 0 and/or LineGap = 0.

If you have installed Source Han Serif, which is open-sourced, you can reproduce the calculations above:

  • For Source Han Serif, units/em = TypoAscender + |TypoDescender| = 1000, with winAscent = 1151 and winDescent = 286, and no line gap. Font metrics found on this webpage.
  • Extra padding are 216 units each.
  • line height turns out to be 1869 units.
  • The basic leading/fontsize ratio is 1869/1000 = 1.869.
  • Use a letter paper docx, set top margin = 1in and bottom margin = 1.27in, use Source Han Serif at 14 points. Then the text height = (11 - 1 - 1.27)in = 8.73in = 628.56 points, the leading = (14 * 1.869) points = 26.166 points, and please let me know if you counted 628.56/26.166 = about 24.0 lines in the first page.

How does Microsoft Word calculate <factor> line spacing?

This is easy, multiply the single line height by the <factor>, and you get the new leading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.