看《windows程序设计》中的一些字体方面的信息,不是很明白,通过查阅得到了一些重要信息。
在字体排版中经常使用EM square,每一种TrueType字体都有一个EM square,而且只有TrueType的字体才有这个EM square。EM square其实就是在实际设计字体时用到的正方形,而这个正方形里面有很多个小方格。一般来说的话,一个EM square里面水平方向,垂直方向都有2048个小方格。我们实际使用的TrueType字体都是基于这个尺寸缩放的。
如果在程序中打印中和在屏幕中直接用GetTextExtentPoint32得到一个字符串的宽度是不一致的,因为不同的分辨率中计算结果四舍五入可能会不同,那可能会导致在打印中的换行和屏幕中的换行位置不同。为了得到绝对精确的字符长度,可以通过创建EM square字号的字体创建原本设计时大小的字体,这时候通过GetCharWidth获得的字符宽度是精确的整数,注意使用GetCharWidth得到缩放字体中字符的大小不精准的,因为字体缩放了,所以用整型保存得到的宽度必然有误差。但用原创字体的设备环境去使用GetCharWidth就可以得到精准的大小。因为字体高度是2048个像素,所以2048个方格没有被缩放,所以得到的宽度是精准的整数。然后用这个宽度,去得到该字体被缩放后字体每个的宽度,用double值保存,用double值计算就可以得到指定字符串的精确宽度。但Windows为什么内部没实现呢?
还由于身边没有打印机,所以不能深入研究。暂且浅显的认识一下吧!
查阅资料:
Font glyphs are designed in relation to a square grid. Each small square in the grid is called a Font Unit, or FUnit for short. For most fonts the grid is ‘standardized’ at 2048 × 2048 FUnits to form the em-square. The em square shrinks or expands with changes
in font size but it’s the small squares in the em square that change size while keeping the actual
number of small squares unchanged. The real nugget of information is that both the vertical and horizontal size of the em square equate to the font size.
One em is the font size of a TrueType font irrespective of whether it’s specified in points or pixels.
The TrueType Manual doesn’t put it as explicitly as that but gives an implicit definition via an equation used for calculating font size in pixels. This equation (Eqn. 1) is in the Manual section “Converting FUnits to pixels” and I’m using it because we
can get two things from it:
- Definition of the em
- How font size in points, or inches, is transformed to font size in pixels. If you don’t like a lot of equations then jump to the
short version
Equation 1 pixels = pointSize × resolution/(72 points per inch × units_per_em)
In Eqn. 1
"pixels" is font size in pixels.
"pointSize" is font size in points.
"resolution" is screen dpi.
"72 points per inch" because a point is 1/72 of an inch.
"units_per_em" is the ratio of the vertical size of a glyph in FUnits to the number of vertical FUnits in the em square. There are (usually) 2048 vertical FUnits in the em square.
Now the meaning of this doesn’t exactly leap out the page so I’ll rearrange the equation, and define the terms in the sidebar.
Equation 2 font size in points glyph size in FUnits pixels = screen dpi × ------------------- × -------------------- 72 points per inch 2048 FUnits
The last two terms in Equation 2 say that the font size in inches is multiplied by the units_per_em ratio. The units_per_em_ratio is the ratio of the vertical size of a glyph to the vertical size of the em square. If the glyph size is the same size as the
em square, i.e. 2048 FUnits, then the units_per_em ratio is unity and the font size would be multiplied by unity. Multiplying the font size by unity just gives, of course, the font size so
the vertical size of the em square corresponds to the font size.
Let’s rearrange Equation 2, but this time with a units_per_em ratio of unity.
Equation 3 font size in points font size in pixels = ------------------- × screen dpi 72 points per inch
This tells us that we get font size in pixels by multiplying font size in inches by the screen dpi. It’s as simple as that.
I’ve shown here how font size in inches is translated into font size in pixels, but screen dpi is not specific to font inches.
Screen dpi is used to translate inches into screen pixels irrespective of whether we are dealing with font inches or just length measurements