dotnet OpenXML talk about PPT text line spacing and line height calculation formula

Posted by qrt123 on Tue, 14 Dec 2021 07:23:39 +0100

In the Office PPT, the line height of each rendered line will be calculated according to the line spacing and font size of the stored document. According to the two versions of Office 2016 and M365, combined with QQ screenshot measurement, this paper will get the corresponding relationship formula through magic calculation and funny ratio algorithm. It may be the same rule for Word, but this article only talks about ppt

Before we begin, let's unify the concept. Line spacing can be divided into two different routes: multiple line spacing and fixed line spacing. Multiple line spacing is the focus of this paper, which refers to the distance of each line of text in the text box according to a certain multiple, such as 1.5 line spacing. Fixed line spacing refers to the line spacing of a fixed height

Row height, the height value of a row. Taking horizontal text as the default example, it refers to the height value of the selection range seen when selecting text, as shown in the following figure

Line spacing = top margin + bottom margin

Changing the line spacing does not affect the text height of the text, but only the values of the upper and lower margins. The row height also changes due to the change of row spacing

I use QQ screenshot tool to measure the impact of different fonts and font sizes on the line height of Chinese characters, and calculate the travel distance line height calculation formula through the tease ratio algorithm. There is no authoritative document found in the following formula, but there is only a small error in the value calculated by using the following formula according to the measured value

The formula for calculating the text line height of PPT in Office is

PPTPixelLineSpacing = (a * PPTFL * OriginLineSpacing + b) * FontSize

Among them, a and b of PPT line spacing calculation are linear functions, and PPTFL means PPT Font Line Spacing, which is the value of all (perhaps most) font line spacing in PPT. The above OriginLineSpacing is the line spacing multiple set on the interface, such as 1 line spacing and 1.5 line spacing

You can combine a and PPTFL into PPTFL, and then use a instead. At this time, a and b are constants, and the values are as follows

a = 1.2018
b = 0.0034

It should be noted that although each font has its own LineSpacing value, which can be obtained through the following code in WPF, this process is ignored in PPT and the same fixed constant value is adopted for most fonts (unclear rules)

            var fontFamily = new System.Windows.Media.FontFamily("Song typeface");
            var fontFamilyLineSpacing = fontFamily.LineSpacing;

For example, at 1.5 times the line spacing, the corresponding OriginLineSpacing is 1.5. Using 9 font, the substitution formula is calculated as follows

PPTPixelLineSpacing = (a * PPTFL * OriginLineSpacing + b) * FontSize
                    = (1.2018 * OriginLineSpacing + 0.0034) * FontSize
                    = (1.2018 * 1.5 + 0.0034) * 9 Pound
                    = (1.2018 * 1.5 + 0.0034) * 12 Pixel
                    = 21.6732 Pixel

Through actual measurement, 21.65 pixels are obtained, which is about equal to the calculated value

If you are concerned about the calculation method, please see below

The relationship with WPF is as follows

In WPF, the height calculation formula is as follows:

ENPixelLineSpacing = LineSpaceHelper.CalcRenderLineSpace(maxFontSizeRunProperty, lineSpace) + FontSize * FontFamilyLineSpacing

And in linespacehelper Calcrenderlinespace is calculated as follows

        private static double CalcRenderLineSpace(double lineSpace, double fontSize, double fontFamilyLineSpacing)
        {
            return fontSize * fontFamilyLineSpacing * (lineSpace - 1) / 10;
        }

So the actual line spacing pixels are as follows

ENPixelLineSpacing = FontSize * FontFamilyLineSpacing * (LineSpace - 1) / 10 + FontSize * FontFamilyLineSpacing

Assuming that ENPixelLineSpacing and PPT are the same, there are

PPTPixelLineSpacing = ENPixelLineSpacing
(a * OriginLineSpacing + b) * FontSize = FontSize * FontFamilyLineSpacing * (LineSpace - 1) / 10 + FontSize * FontFamilyLineSpacing

Both sides divided by FontSize font size

(a * OriginLineSpacing + b) = FontFamilyLineSpacing * (LineSpace - 1) / 10 + FontFamilyLineSpacing

Perform mathematical calculations

                        (a * OriginLineSpacing + b) = FontFamilyLineSpacing * (LineSpace - 1) / 10 + FontFamilyLineSpacing
(a * OriginLineSpacing + b) - FontFamilyLineSpacing = FontFamilyLineSpacing * (LineSpace - 1) / 10
((a * OriginLineSpacing + b) - FontFamilyLineSpacing) * 10 = FontFamilyLineSpacing * (LineSpace - 1)
(((a * OriginLineSpacing + b) - FontFamilyLineSpacing) * 10) / FontFamilyLineSpacing = (LineSpace - 1)
(((a * OriginLineSpacing + b) - FontFamilyLineSpacing) * 10) / FontFamilyLineSpacing + 1 = LineSpace

The values of constants a and b are as follows

a = 1.2018;
b = 0.0034;
PPTFontLineSpacing = a;

The formula constants in PPT can be calculated through the accurate value of WPF rendering size. After modifying the font size and line spacing, you can see that the line height is linearly modified through measurement. The linear function y = ax + b is used to control the font size, modify the line spacing multiple, and control the line spacing multiple to control the line height. Measure the rendering size respectively, establish quadratic linear algebra, and calculate the specific value according to the above formula

The following are specific experiments:

For the test under fixed 1.5 times line spacing, the following results are written in the following format

Enlarged rendering value = actual rendering value = > font size = > font size pixel value

173 / 4 pixel = 43.5 pixel = > 18 pound font size = 24 Pixel font size = > 43.5 / 24 = 1.8125 font size pixel value

229 / 4 pixel = 57.2 pixel = > 24 pound font size = 32 Pixel font size = > 57.2 / 32 = 1.7875 font size pixel value

289 / 4 pixel = 72.25 pixel = > 30 pound font size = 40 Pixel font size = > 72.25 / 40 = 1.80625 font size pixel value

Similarly, calculate the value of single line spacing = 1.204166667

Assuming that the default line spacing is a multiple of 1.2018, calculate according to the following test formula

(1.2018 x OriginLineSpacing + 0.0034) * FontSize 

Bring in 1.5 line spacing + 24 Pixel font size, and calculate as follows:

1.5 Double line spacing + 24 Pixel => (1.2018 * 1.5 + 0.0034) * 24 Pixel = 43.3464 Pixel

The measurement results are similar

LineSpacing values for different fonts are as follows:

  • Microsoft YaHei 1.31982421875
  • Tahoma 1.140625

Because different fonts have no effect on the line height in the PPT, the above are tested by Microsoft YaHei

See more Office uses the OpenXML SDK to parse document blog directories

This article will be updated frequently. Please read the original text: https://blog.lindexi.com/post/dotnet-OpenXML-%E8%81%8A%E8%81%8A-PPT-%E6%96%87%E6%9C%AC%E8%A1%8C%E8%B7%9D%E8%A1%8C%E9%AB%98%E8%AE%A1%E7%AE%97%E5%85%AC%E5%BC%8F.html In order to avoid the misleading of old wrong knowledge and have a better reading experience.