📄 step1.html
字号:
<td> <tt>num_glyphs</tt> </td> <td> <p>This variable gives the number of <em>glyphs</em> available in the font face. A glyph is simply a character image. It doesn't necessarily correspond to a <em>character code</em> though.</p> </td> </tr> <tr valign="top"> <td> <tt>flags</tt> </td> <td> <p>A 32-bit integer containing bit flags used to describe some face properties. For example, the flag <tt>FT_FACE_FLAG_SCALABLE</tt> is used to indicate that the face's font format is scalable and that glyph images can be rendered for all character pixel sizes. For more information on face flags, please read the <a href="../reference/ft2-index.html">FreeType 2 API Reference</a>.</p> </td> </tr> <tr valign="top"> <td> <tt>units_per_EM</tt> </td> <td> <p>This field is only valid for scalable formats (it is set to 0 otherwise). It indicates the number of font units covered by the EM.</p> </td> </tr> <tr valign="top"> <td> <tt>num_fixed_sizes</tt> </td> <td> <p>This field gives the number of embedded bitmap strikes in the current face. A <em>strike</em> is simply a series of glyph images for a given character pixel size. For example, a font face could include strikes for pixel sizes 10, 12 and 14. Note that even scalable font formats can have embedded bitmap strikes!</p> </td> </tr> <tr valign="top"> <td> <tt>fixed_sizes</tt> </td> <td> <p>A pointer to an array of <tt>FT_Bitmap_Size</tt> elements. Each <tt>FT_Bitmap_Size</tt> indicates the horizontal and vertical <em>character pixel sizes</em> for each of the strikes that are present in the face.</p> <p><font color="red">Note that, generally speaking, these are <em>not</em> the <em>cell size</em> of the bitmap strikes.</font> </p> </td> </tr> </table> <hr> <h3> 5. Setting the current pixel size </h3> <p>FreeType 2 uses <em>size objects</em> to model all information related to a given character size for a given face. For example, a size object will hold the value of certain metrics like the ascender or text height, expressed in 1/64th of a pixel, for a character size of 12 points.</p> <p>When the <tt>FT_New_Face</tt> function is called (or one of its cousins), it <em>automatically</em> creates a new size object for the returned face. This size object is directly accessible as <tt>face−>size</tt>.</p> <p><em>NOTE: A single face object can deal with one or more size objects at a time; however, this is something that few programmers really need to do. We have thus decided to simplify the API for the most common use (i.e., one size per face) while keeping this feature available through additional functions.</em></p> <p>When a new face object is created, its size object defaults to the character size of 10 pixels (both horizontally and vertically) for scalable formats. For fixed-sizes formats, the size is more or less undefined, which is why you must set it before trying to load a glyph.</p> <p>To do that, simply call <a href="../reference/ft2-base_interface.html#FT_Set_Char_Size"> <tt>FT_Set_Char_Size</tt></a>. Here is an example where the character size is set to 16pt for a 300×300 dpi device:</p> <div class="pre"> error = FT_Set_Char_Size( face, <span class="comment">/* handle to face object */</span> 0, <span class="comment">/* char_width in 1/64th of points */</span> 16*64, <span class="comment">/* char_height in 1/64th of points */</span> 300, <span class="comment">/* horizontal device resolution */</span> 300 ); <span class="comment">/* vertical device resolution */</span> </div> <p>Some notes:</p> <ul> <li> <p>The character widths and heights are specified in 1/64th of points. A point is a <em>physical</em> distance, equaling 1/72th of an inch. Normally, it is not equivalent to a pixel.<p> </li> <li> <p>The horizontal and vertical device resolutions are expressed in <em>dots-per-inch</em>, or <em>dpi</em>. Normal values are 72 or 96 dpi for display devices like the screen. The resolution is used to compute the character pixel size from the character point size.</p> </li> <li> <p>A value of 0 for the character width means ‘same as character height’, a value of 0 for the character height means ‘same as character width’. Otherwise, it is possible to specify different character widths and heights.</p> </li> <li> <p>Using a value of 0 for the horizontal or vertical resolution means 72 dpi, which is the default.</p> </li> <li> <p>The first argument is a handle to a face object, not a size object.</p> </li> </ul> <p>This function computes the character pixel size that corresponds to the character width and height and device resolutions. However, if you want to specify the pixel sizes yourself, you can simply call <a href="../reference/ft2-base_interface.html#FT_Set_Pixel_Sizes"> <tt>FT_Set_Pixel_Sizes</tt></a>, as in</p> <div class="pre"> error = FT_Set_Pixel_Sizes( face, <span class="comment">/* handle to face object */</span> 0, <span class="comment">/* pixel_width */</span> 16 ); <span class="comment">/* pixel_height */</span> </div> <p>This example will set the character pixel sizes to 16×16 pixels. As previously, a value of 0 for one of the dimensions means ‘same as the other’.</p> <p>Note that both functions return an error code. Usually, an error occurs with a fixed-size font format (like FNT or PCF) when trying to set the pixel size to a value that is not listed in the <tt>face->fixed_sizes</tt> array.</p> <hr> <h3> 6. Loading a glyph image </h3> <h4> a. Converting a character code into a glyph index </h4> <p>Usually, an application wants to load a glyph image based on its <em>character code</em>, which is a unique value that defines the character for a given <em>encoding</em>. For example, the character code 65 represents the ‘A’ in ASCII encoding.</p> <p>A face object contains one or more tables, called <em>charmaps</em>, that are used to convert character codes to glyph indices. For example, most TrueType fonts contain two charmaps. One is used to convert Unicode character codes to glyph indices, the other is used to convert Apple Roman encoding into glyph indices. Such fonts can then be used either on Windows (which uses Unicode) and Macintosh (which uses Apple Roman). Note also that a given charmap might not map to all the glyphs present in the font.</p> <p>By default, when a new face object is created, it selects a Unicode charmap. FreeType tries to emulate a Unicode charmap if the font doesn't contain such a charmap, based on glyph names. Note that it is possible that the emulation misses glyphs if glyph names are non-standard. For some fonts, including symbol fonts and (older) fonts for Asian scripts, no Unicode emulation is possible at all.</p> <p>We will describe later how to look for specific charmaps in a face. For now, we will assume that the face contains at least a Unicode charmap that was selected during a call to <tt>FT_New_Face</tt>. To convert a Unicode character code to a font glyph index, we use <tt>FT_Get_Char_Index</tt>, as in</p> <div class="pre"> glyph_index = FT_Get_Char_Index( face, charcode ); </div> <p>This will look the glyph index corresponding to the given <tt>charcode</tt> in the charmap that is currently selected for the face. If no charmap was selected, the function simply returns the charcode.</p> <p>Note that this is one of the rare FreeType functions that do not return an error code. However, when a given character code has no glyph image in the face, the value 0 is returned. By convention, it always correspond to a special glyph image called the <em>missing glyph</em>, which is commonly displayed as a box or a space.</p> <h4> b. Loading a glyph from the face </h4> <p>Once you have a glyph index, you can load the corresponding glyph image. The latter can be stored in various formats within the font file. For fixed-size formats like FNT or PCF, each image is a bitmap. Scalable formats like TrueType or Type 1 use vectorial shapes, named <em>outlines</em> to describe each glyph. Some formats may have even more exotic ways of representing glyphs (e.g., MetaFont — but this format is not supported). Fortunately, FreeType 2 is flexible enough to support any kind of glyph format through a simple API.</p> <p>The glyph image is always stored in a special object called a <em>glyph slot</em>. As its name suggests, a glyph slot is simply a container that is able to hold one glyph image at a time, be it a bitmap, an outline, or something else. Each face object has a single glyph slot object that can be accessed as <tt>face->glyph</tt>. Its fields are explained by the <a href="../reference/ft2-base_interface.html#FT_GlyphSlotRec"> <tt>FT_GlyphSlotRec</tt></a> structure documentation.</p> <p>Loading a glyph image into the slot is performed by calling <a href="../reference/ft2-base_interface.html#FT_Load_Glyph"> <tt>FT_Load_Glyph</tt></a> as in</p> <div class="pre"> error = FT_Load_Glyph( face, <span class="comment">/* handle to face object */</span> glyph_index, <span class="comment">/* glyph index */</span> load_flags ); <span class="comment">/* load flags, see below */</span> </div> <p>The <tt>load_flags</tt> value is a set of bit flags used to indicate some special operations. The default value <tt>FT_LOAD_DEFAULT</tt> is 0.</p> <p>This function will try to load the corresponding glyph image from the face:</p> <ul> <li> <p>If a bitmap is found for the corresponding glyph and pixel size, it will be loaded into the slot. Embedded bitmaps are always favored over native image formats, because we assume that they are higher-quality versions of the same glyph. This can be changed by using the <tt>FT_LOAD_NO_BITMAP</tt> flag.</p> </li> <li> <p>Otherwise, a native image for the glyph will be loaded. It will also be scaled to the current pixel size, as well as hinted for certain formats like TrueType and Type 1.</p> </li> </ul> <p>The field <tt>face−>glyph−>format</tt> describes the format used to store the glyph image in the slot. If it is not <tt>FT_GLYPH_FORMAT_BITMAP</tt>, one can immediately convert it to a bitmap through <a href="../reference/ft2-base_interface.html#FT_Render_Glyph"> <tt>FT_Render_Glyph</tt></a> as in:</p> <div class="pre"> error = FT_Render_Glyph( face->glyph, <span class="comment">/* glyph slot */</span> render_mode ); <span class="comment">/* render mode */</span> </div> <p>The parameter <tt>render_mode</tt> is a set of bit flags used to specify how to render the glyph image. Set it to <tt>FT_RENDER_MODE_NORMAL</tt> to render a high-quality anti-aliased (256 gray levels) bitmap, as this is the default. You can alternatively use <tt>FT_RENDER_MODE_MONO</tt> if you want to generate a 1-bit monochrome bitmap.</p> <p>Once you have a bitmapped glyph image, you can access it directly through <tt>glyph->bitmap</tt> (a simple bitmap descriptor), and position it through <tt>glyph->bitmap_left</tt> and <tt>glyph->bitmap_top</tt>.</p> <p>Note that <tt>bitmap_left</tt> is the horizontal distance from the current pen position to the leftmost border of the glyph bitmap, while <tt>bitmap_top</tt> is the vertical distance from the pen position (on the baseline) to the topmost border of the glyph bitmap. <em>It is positive to indicate an upwards distance</em>.</p> <p>The next section will give more details on the contents of a glyph slot and how to access specific glyph information (including metrics).</p> <h4> c. Using other charmaps </h4> <p>As said before, when a new face object is created, it will look for a Unicode charmap and select it. The currently selected charmap is accessed via <tt>face->charmap</tt>. This field is NULL when no charmap is selected, which typically happens when you create a new <tt>FT_Face</tt> object from a font file that doesn't contain a Unicode charmap (which is rather infrequent today).</p> <p>There are two ways to select a different charmap with FreeType 2. The easiest is when the encoding you need already has a corresponding enumeration defined in <tt>FT_FREETYPE_H</tt>, for example <tt>FT_ENCODING_BIG5</tt>. In this case, you can simply call <a href="../reference/ft2-base_interface.html#FT_Select_CharMap"> <tt>FT_Select_CharMap</tt></a> as in:</p> <div class="pre"> error = FT_Select_CharMap( face, <span class="comment">/* target face object */</span> FT_ENCODING_BIG5 ); <span class="comment">/* encoding */</span> </div> <p>Another way is to manually parse the list of charmaps for the face; this is accessible through the fields <tt>num_charmaps</tt> and <tt>charmaps</tt> (notice the ‘s&rsquo) of the face object. As you could expect, the first is the number of charmaps in the face, while the second is <em>a table of pointers to the charmaps</em> embedded in the face.</p> <p>Each charmap has a few visible fields used to describe it more precisely. Mainly, one will look at <tt>charmap->platform_id</tt> and <tt>charmap->encoding_id</tt> that define a pair of values that can be used to describe the charmap in a rather generic way.</p> <p>Each value pair corresponds to a given encoding. For example, the pair (3,1) corresponds to Unicode. The list is defined in the TrueType specification but you can also use the file <tt>FT_TRUETYPE_IDS_H</tt> which defines several helpful constants to deal with them.</p> <p>To select a specific encoding, you need to find a corresponding value pair in the specification, then look for it in the charmaps list. Don't forget that there are encodings which correspond to several value pairs due to historical reasons. Here some code to do it:</p> <div class="pre"> FT_CharMap found = 0; FT_CharMap charmap; int n; for ( n = 0; n < face->num_charmaps; n++ ) { charmap = face->charmaps[n];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -