看《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
<a href="screendpi.html#shortversion">short version</a>
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.
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.
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