您的位置:首页 > 其它

freetype的一些基本概念 -- EM

2010-03-02 20:41 134 查看
使用freetype正确显示字体也有一段时间了,但是其中有些关于字体的基本概念一直困扰着自己,今天找点空闲加班找了一些资料,基本上算是有些眉目了,也许自己的理解是正确的,^_^

通过查找truetype手册,对EM的概念有些更深一步的了解,摘抄来共享之。

em square
1 em square的概念

1)“em square”在truetype手册中的描述

Truetype手册 “DIGITIZING LETTERFORM DESIGNS”章节中有如下的描述,网址为: http://developer.apple.com/textfonts/TTRefMan/RM01/Chap1.html The points that make up a glyph outline specification are located on a grid whose units are indivisible and of a size defined by the creator of the font. These units are termed font-units.

The grid is a two-dimensional coordinate system whose x -axis describes movement in a horizontal direction and whose y -axis describes movement in a vertical direction. The grid origin has the coordinates (0,0). The grid: sizegrid is not an infinite plane. Each point must be within the range -16384 through +16383 font-units.

In creating the glyph outlines, the font creator makes use of an imaginary square that derives from the old typographic concept of the em square. This square can be thought of an a tablet on which the characters are drawn, although it is permissible for characters to extend beyond the tablet or em square. It is the size of the font-unit relative to the size of the em square that determines the granularity of the grid. In other words, the resolution of the grid is a function of the number of font-units per em.

The greater the number of font-units per em, the greater subtlety of design that can be represented in the font file. In determining what is a suitable grid granularity, the font designer should take into account the maximum resolution of the potential output device and allow for possible loss of precision in data conversion. Outline scaling will be fastest if the units per em is a power of two. For example, the Apple core fonts are designed on a grid with 2048 units per em.

FIGURE 8 shows two different grids. The grid on the left cannot represent the subtlety of original design for the letter A. The grid on the right, with four times the number of units per em, comes closer, though a finer granularity is needed to faithfully render the original design.

图片地址: http://developer.apple.com/textfonts/TTRefMan/RM01/fig08.gif



从这里我们可以大致可以做如下猜测:
em square ―― 如上图所示,em square有很多小方块组成的大方块区域。在这个大方块区域中,字体设计者通过将不同的交叉点连接起来,来描绘一个字的轮廓。每个小方块就是一个font-units。由于字的轮廓是由一系列交叉点来描述的,所以如果小方框的数量越多,则我们可以更精确地描绘一个字体的轮廓,精度越高。因此一个em square包含由多少个小方块(也就是多少个font-units)可以用来描述这个精度,由此有了units_per_em的概念。
units_per_em ―― 基于上面的描述,我们可以猜测,units_per_em就是表示em square方块的一个边包含有多少个小方块。
从上图可以看到,左图的units_per_em为9,分辨率很低,灰色是字体设计者原本想设计出来的字型,但是因为小方块太少,无法精确用交叉点来描述,所以最终黑色部分的“A”字无法正确表达字体设计者的本意。

2)Converting FUnits to pixels

同样来自truetype手册中的相关信息,位于手册“The Font Engine”部分。网址: http://developer.apple.com/textfonts/TTRefMan/RM02/Chap2.html
How the truetype font engine works
Rasterizing a glyph outline is a multi-step process that proceeds as follows:

Step 1: The master outline description of the glyph is scaled to the appropriate size.
Step 2: The scaled outline is grid-fitted according to its associated instructions.
Step 3: The grid-fitted outline is scan converted to produce a bitmap image suitable for raster display.
Figure 1 illustrates this process.

Figure 1 The work of the font engine

http://developer.apple.com/textfonts/TTRefMan/RM02/fig2-1-1.gif

master outline



1-scaled outline

http://developer.apple.com/textfonts/TTRefMan/RM02/fig2-1-2.gif



2-grid-fitted outline

http://developer.apple.com/textfonts/TTRefMan/RM02/fig2-1-3.gif



3-raster image

http://developer.apple.com/textfonts/TTRefMan/RM02/fig2-1-4.gif



Scaling the master outline
A TrueType font stores a master outline description for each glyph. When an application requests a particular glyph at a specific size for a specific device, the font engine will create the necessary bitmap.

The first step in this process is to scale the master outline to the desired size. Scaling an outline is the work of that portion of the font engine known as the scaler. When the master outline is scaled, the points that make up the glyph outline are changed from device independent em units to device dependent 26.6 fixed point numbers representing locations in a pixel grid. In the text that follows, master outline points shown as grid coordinates will have a bar over the coordinate numbers as in (x~,y~).

A scaled outline point can occupy any position expressible as a sixty-fourth of a pixel (i.e as a 26.6 fixed point number). Scaled coordinates which are still in their original outline positions (that is, have not been grid-fitted by instructions) will be shown with a tilde over the coordinates as in (x~, y~). Point coordinates that are expressed to the nearest sixty fourth of a pixel will be shown with a colon separating the whole number portion of the coordinate from the fractional portion. One and one half pixels will therefore be written as 1:32 in this notation.

Pixel centers are always at the intersection of two half-grid lines. The point (2:32, 7:32) occupies a pixel center while the point (3:0, 4:0) does not.

Converting FUnits to pixels
The scaler converts values in the master coordinate system to values in the pixel coordinate system by multiplying them by a scale. This scale is:

pointSize * resolution / (72 points per inch * units_per_em).




where pointSize is the size at which the glyph is to be displayed, resolution is the resolution of the output device and units per em is the resolution of the grid of which the master outline was originally defined. The 72 in the denominator represents the number of points per inch.

For example, assume that a glyph feature is 550 FUnits in length and defined on a master grid with 2048 units per em. The following calculation reveals that its size on a 72 dpi screen at 18 points, is 4.83 pixels.

550 * [(18 * 72 )/ (72 * 2048 )] = 4.83
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: