freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ft] Fw: [ft-devel] fitting a text


From: Werner LEMBERG
Subject: [ft] Fw: [ft-devel] fitting a text
Date: Sat, 06 Jun 2015 05:38:52 +0200 (CEST)

[Originally sent to the wrong list.]

Please help this guy.


    Werner
--- Begin Message --- Subject: [ft-devel] fitting a text Date: Fri, 5 Jun 2015 11:24:12 +0200 User-agent: Mutt/1.5.23 (2014-03-12)
Hi,

I'm trying to draw text to a bitmap. It should fit the height of the
bitmap and the width can be extended whenever required.
Getting the width right works fine but the height is a problem: a, b, c
fit nicely but g, q and _ fall below the bottom. How can I get this to
work?
My code is:

        int target_height = 50; // height of bitmap

        FT_Library library;
        FT_Face face;
          (void)FT_Init_FreeType(&library);
          (void)FT_New_Face(library, filename.c_str(), 0, &face);
  
          (void)FT_Set_Char_Size(face, target_height * 64, 0, 100, 0); /* set 
character size */
        FT_GlyphSlot slot = face->glyph;
  
          w = 0;
          h = target_height;

          // calculate resulting width
          for(int n = 0; n < text.size(); n++)
          {
                 if (FT_Load_Char(face, text.at(n), FT_LOAD_RENDER))
                          continue;
 
                 w += slot->advance.x / 64;
          }
 
          // draw text to bitmap
          image = new uint8_t[w * h];
          memset(image, 0x00, w * h);
  
          double x = 0.0;
          for(int n = 0; n < text.size(); n++)
          {
                if (FT_Load_Char(face, text.at(n), FT_LOAD_RENDER))
                          continue;
  
                draw_bitmap(w, h, &slot->bitmap, slot->bitmap_left + int(x / 
64.0), target_height - slot->bitmap_top - slot->advance.y);

                x += slot->advance.x;
          }
  
          FT_Done_Face(face);
          FT_Done_FreeType(library);


draw bitmap:
    // x_offset is the 'x' from the code above that gets the slot->advance.x 
added to it for every character
    // x,y here just iterate from 0 to slot->bitmap.width and .height

    int y_offset = target_height - slot->bitmap_top - slot->advance.y;

    image[(y + y_offset) * image_width + x + x_offset] = slot->bitmap.buffer[y 
* slot->bitmap.width + x];


Folkert van Heusden

_______________________________________________
Freetype-devel mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/freetype-devel

--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]