⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 step1.html

📁 Free type 2.1.0 Documents
💻 HTML
📖 第 1 页 / 共 3 页
字号:
    if ( charmap->platform_id == my_platform_id &&
         charmap->encoding_id == my_encoding_id )
    {
      found = charmap;
      break;
    }
  }

  if ( !found ) { ... }

  <span class="comment">/* now, select the charmap for the face object */</span>
  error = FT_Set_CharMap( face, found );
  if ( error ) { ... }
      </div>

      <p>Once a charmap has been selected, either through
      <tt>FT_Select_CharMap</tt> or <tt>FT_Set_CharMap</tt>, it is used by
      all subsequent calls to <tt>FT_Get_Char_Index</tt>.</p>

      <h4>
        d.&nbsp;Glyph transformations
      </h4>

      <p>It is possible to specify an affine transformation to be applied to
      glyph images when they are loaded.  Of course, this will only work for
      scalable (vectorial) font formats.</p>

      <p>To do that, simply call <a
      href="../reference/ft2-base_interface.html#FT_Set_Transform">
      <tt>FT_Set_Transform</tt></a>, as in:</p>

      <div class="pre">
  error = FT_Set_Transform(
            face,       <span class="comment">/* target face object    */</span>
            &amp;matrix,    <span class="comment">/* pointer to 2x2 matrix */</span>
            &amp;delta );   <span class="comment">/* pointer to 2d vector  */</span>
      </div>

      <p>This function will set the current transform for a given face
      object.  Its second parameter is a pointer to a simple <a
      href="../reference/ft2-basic_types.html#FT_Matrix">
      <tt>FT_Matrix</tt></a> structure that describes a 2&times;2 affine
      matrix.  The third parameter is a pointer to a <a
      href="../reference/ft2-basic_types.html#FT_Vector">
      <tt>FT_Vector</tt></a> structure that describes a simple
      two-dimensional vector that is used to translate the glyph image
      <em>after</em> the 2&times;2 transformation.</p>

      <p>Note that the matrix pointer can be set to NULL, in which case the
      identity transform will be used.  Coefficients of the matrix are
      otherwise in 16.16 fixed float units.</p>

      <p>The vector pointer can also be set to NULL (in which case a delta
      of (0,0) will be used).  The vector coordinates are expressed in
      1/64th of a pixel (also known as 26.6 fixed floats).</p>

      <p><font color="red">NOTE: The transformation is applied to every
      glyph that is loaded through <tt>FT_Load_Glyph</tt> and is
      <em>completely independent of any hinting process</em>.  This means
      that you won't get the same results if you load a glyph at the size of
      24&nbsp;pixels, or a glyph at the size at 12&nbsp;pixels scaled
      by&nbsp;2 through a transform, because the hints will have been
      computed differently (except you have disabled hints).</font></p>

      <p>If you ever need to use a non-orthogonal transformation with
      optimal hints, you first have to decompose your transformation into a
      scaling part and a rotation/shearing part.  Use the scaling part to
      compute a new character pixel size, then the other one to call
      <tt>FT_Set_Transform</tt>.  This is explained in details in a later
      section of this tutorial.</p>

      <p>Note also that loading a glyph bitmap with a non-identity transform
      will produce an error.</p>

    <hr>

    <h3>
      7.&nbsp;Simple text rendering
    </h3>

    <p>We will now present a very simple example used to render a string of
    8-bit Latin-1 text, assuming a face that contains a Unicode charmap</p>

    <p>The idea is to create a loop that will, on each iteration, load one
    glyph image, convert it to an anti-aliased bitmap, draw it on the target
    surface, then increment the current pen position.</p>

      <h4>
        a.&nbsp;Basic code
      </h4>

      <p>The following code performs our simple text rendering with the
      functions previously described.</p>

      <div class="pre">
  FT_GlyphSlot  slot = face-&gt;glyph;  <span class="comment">/* a small shortcut */</span>
  int           pen_x, pen_y, n;


  ... initialize library ...
  ... create face object ...
  ... set character size ...

  pen_x = 300;
  pen_y = 200;

  for ( n = 0; n &lt; num_chars; n++ )
  {
    FT_UInt  glyph_index;


    <span class="comment">/* retrieve glyph index from character code */</span>
    glyph_index = FT_Get_Char_Index( face, text[n] );

    <span class="comment">/* load glyph image into the slot (erase previous one) */</span>
    error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
    if ( error )
      continue;  <span class="comment">/* ignore errors */</span>

    <span class="comment">/* convert to an anti-aliased bitmap */</span>
    error = FT_Render_Glyph( face-&gt;glyph, ft_render_mode_normal );
    if ( error )
      continue;

    <span class="comment">/* now, draw to our target surface */</span>
    my_draw_bitmap( &amp;slot-&gt;bitmap,
                    pen_x + slot-&gt;bitmap_left,
                    pen_y - slot-&gt;bitmap_top );

    <span class="comment">/* increment pen position */</span>
    pen_x += slot-&gt;advance.x >> 6;
    pen_y += slot-&gt;advance.y >> 6; <span class="comment">/* not useful for now */</span>
  }
      </div>

      <p>This code needs a few explanations:</p>

      <ul>
        <li>
          <p>We define a handle named <tt>slot</tt> that points to the
          face object's glyph slot.  (The type <tt>FT_GlyphSlot</tt> is
          a pointer).  That is a convenience to avoid using
          <tt>face-&gt;glyph-&gt;XXX</tt> every time.</p>
        </li>

        <li>
          <p>We increment the pen position with the vector
          <tt>slot-&gt;advance</tt>, which correspond to the glyph's
          <em>advance width</em> (also known as its <em>escapement</em>).
          The advance vector is expressed in 1/64th of pixels, and is
          truncated to integer pixels on each iteration.</p>
        </li>

        <li>
          <p>The function <tt>my_draw_bitmap</tt> is not part of FreeType
          but must be provided by the application to draw the bitmap to the
          target surface.  In this example, it takes a pointer to a
          FT_Bitmap descriptor and the position of its top-left corner as
          arguments.</p>
        </li>

        <li>
          <p>The value of <tt>slot-&gt;bitmap_top</tt> is positive for an
          <em>upwards</em> vertical distance.  Assuming that the coordinates
          taken by <tt>my_draw_bitmap</tt> use the opposite convention
          (increasing&nbsp;Y corresponds to downwards scanlines), we
          substract it from <tt>pen_y</tt>, instead of adding to it.</p>
        </li>
      </ul>

      <h4>
        b.&nbsp;Refined code
      </h4>

      <p>The following code is a refined version of the example above.  It
      uses features and functions of FreeType&nbsp;2 that have not yet been
      introduced, and which are explained below:</p>

      <div class="pre">
  FT_GlyphSlot  slot = face-&gt;glyph;  <span class="comment">/* a small shortcut */</span>
  FT_UInt       glyph_index;
  int           pen_x, pen_y, n;


  ... initialize library ...
  ... create face object ...
  ... set character size ...

  pen_x = 300;
  pen_y = 200;

  for ( n = 0; n &lt; num_chars; n++ )
  {
    <span class="comment">/* load glyph image into the slot (erase previous one) */</span>
    error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
    if ( error )
      continue;  <span class="comment">/* ignore errors */</span>

    <span class="comment">/* now, draw to our target surface */</span>
    my_draw_bitmap( &amp;slot-&gt;bitmap,
                    pen_x + slot-&gt;bitmap_left,
                    pen_y - slot-&gt;bitmap_top );

    <span class="comment">/* increment pen position */</span>
    pen_x += slot-&gt;advance.x >> 6;
  }
      </div>

      <p>We have reduced the size of our code, but it does exactly the
      same thing:</p>

      <ul>
        <li>
          <p>We use the function <tt>FT_Load_Char</tt> instead of
          <tt>FT_Load_Glyph</tt>.  As you probably imagine, it is equivalent
          to calling <tt>FT_Get_Char_Index</tt> then
          <tt>FT_Get_Load_Glyph</tt>.</p>
        </li>

        <li>
          <p>We do not use <tt>FT_LOAD_DEFAULT</tt> for the loading mode,
          but the bit flag <tt>FT_LOAD_RENDER</tt>.  It indicates that the
          glyph image must be immediately converted to an anti-aliased
          bitmap.  This is of course a shortcut that avoids calling
          <tt>FT_Render_Glyph</tt> explicitly but is strictly
          equivalent.</p>

          <p>Note that you can also specify that you want a monochrome
          bitmap instead by using the addition <tt>FT_LOAD_MONOCHROME</tt>
          load flag.</p>
        </li>
      </ul>

    <h4>
      c.&nbsp;More advanced rendering
    </h4>

    <p>Let us try to render transformed text now (for example through a
    rotation).  We can do this using <tt>FT_Set_Transform</tt>.  Here is how
    to do it:</p>

    <div class="pre">
  FT_GlyphSlot  slot;
  FT_Matrix     matrix;              <span class="comment">/* transformation matrix */</span>
  FT_UInt       glyph_index;
  FT_Vector     pen;                 <span class="comment">/* untransformed origin */</span>
  int           n;


  ... initialize library ...
  ... create face object ...
  ... set character size ...

  slot = face-&gt;glyph;                <span class="comment">/* a small shortcut */</span>

  <span class="comment">/* set up matrix */</span>
  matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
  matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
  matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
  matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );

  <span class="comment">/* the pen position in 26.6 cartesian space coordinates */</span>
  <span class="comment">/* start at (300,200)                                   */</span>
  pen.x = 300 * 64;
  pen.y = ( my_target_height - 200 ) * 64;

  for ( n = 0; n &lt; num_chars; n++ )
  {
    <span class="comment">/* set transformation */</span>
    FT_Set_Transform( face, &amp;matrix, &amp;pen );

    <span class="comment">/* load glyph image into the slot (erase previous one) */</span>
    error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
    if ( error )
      continue;  <span class="comment">/* ignore errors */</span>

    <span class="comment">/* now, draw to our target surface (convert position) */</span>
    my_draw_bitmap( &amp;slot-&gt;bitmap,
                    slot-&gt;bitmap_left,
                    my_target_height - slot-&gt;bitmap_top );

    <span class="comment">/* increment pen position */</span>
    pen.x += slot-&gt;advance.x;
    pen.y += slot-&gt;advance.y;
  }
    </div>

    <p>Some remarks:</p>

    <ul>
      <li>
        <p>We now use a vector of type <tt>FT_Vector</tt> to store the pen
        position, with coordinates expressed as 1/64th of pixels, hence a
        multiplication.  The position is expressed in cartesian space.</p>
      </li>

      <li>
        <p>Glyph images are always loaded, transformed, and described in the
        cartesian coordinate system in FreeType (which means that
        increasing&nbsp;Y corresponds to upper scanlines), unlike the system
        typically used for bitmaps (where the topmost scanline has
        coordinate&nbsp;0).  We must thus convert between the two systems
        when we define the pen position, and when we compute the topleft
        position of the bitmap.</p>
      </li>

      <li>
        <p>We set the transformation on each glyph to indicate the rotation
        matrix as well as a delta that will move the transformed image to
        the current pen position (in cartesian space, not bitmap space).</p>

        <p>As a consequence, the values of <tt>bitmap_left</tt> and
        <tt>bitmap_top</tt> correspond to the bitmap origin in target space
        pixels.  We thus don't add <tt>pen.x</tt> or <tt>pen.y</tt> to their
        values when calling <tt>my_draw_bitmap</tt>.</p>
      </li>

      <li>
        <p>The advance width is always returned transformed, which is why it
        can be directly added to the current pen position.  Note that it is
        <em>not</em> rounded this time.</p>
      </li>
    </ul>

    <p>A complete source code example can be found
    <a href="example1.c">here</a>.</p>

    <p>It is important to note that, while this example is a bit more
    complex than the previous one, it is strictly equivalent for the case
    where the transform is the identity.  Hence it can be used as a
    replacement (but a more powerful one).</p>

    <p>It has however a few shortcomings that we will explain, and solve, in
    the next part of this tutorial.</p>

    <hr>

    <h3>
      Conclusion
    </h3>

    <p>In this first section, you have learned the basics of
    FreeType&nbsp;2, as well as sufficient knowledge how to render rotated
    text.</p>

    <p>The next section will dive into more details of the API in order
    to let you access glyph metrics and images directly, as well as
    how to deal with scaling, hinting, kerning, etc.</p>

    <p>The third section will discuss issues like modules, caching and a few
    other advanced topics like how to use multiple size objects with a
    single face.  [This part hasn't been written yet.]</p>

</td></tr>
</table>
</center>

<h3 align=center>
  <a href="step2.html">FreeType&nbsp;2 Tutorial Step&nbsp;2</a>
</h3>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -