How to print in TTF correctly?

  • Follow


Hello.

TTF fonts are unicode fonts. What format of string should I put to the
function "pisz", to correctly print letters which have code >255?

void pisz(int x, int y, const char *txt, int font_size, int font_nr) {
    fonts[font_nr]->FaceSize(font_size);
    glRasterPos2f(x, y);
    fonts[font_nr]->Render(txt);
}
0
Reply JacekK 12/21/2007 5:59:38 PM

JacekK <jacek.kurzyca@gmail.com> writes:

> Hello.
> 
> TTF fonts are unicode fonts. What format of string should I put to the
> function "pisz", to correctly print letters which have code >255?
> 
> void pisz(int x, int y, const char *txt, int font_size, int font_nr) {
>     fonts[font_nr]->FaceSize(font_size);
>     glRasterPos2f(x, y);
>     fonts[font_nr]->Render(txt);
> }

You know the charset (unicode), which is the charset used by, for example,
the freetype library. You still have to figure which encoding is used to
encode those characters in the string. It's probably in the documentation
of the library you use (which you didn't tell to us).

Once you know the encoding input of pisz(), converting your input to the
given encoding should be fairly simple, you can for example find how UTF-8
strings are encoded on Wikipedia.

Still, you may have further work depending on which type of input you have
(i.e. some more conversion). I, for example, have in my projet to convert
X keysyms to Unicode before assembling them in a UTF-8 string. Early X
distributions didn't seems to have a easy way to convert a keysym to
Unicode (recently something like Xutf8LookupString was add), so i have to
create a function which convert a keysym to Unicode using some homemade
algorithms and lookups table, which is the method used by xterm for example.

-- 
folays
0
Reply folays 12/21/2007 5:27:09 PM


JacekK pisze:
> Hello.
> 
> TTF fonts are unicode fonts. What format of string should I put to the
> function "pisz", to correctly print letters which have code >255?
> 
> void pisz(int x, int y, const char *txt, int font_size, int font_nr) {
>     fonts[font_nr]->FaceSize(font_size);
>     glRasterPos2f(x, y);
>     fonts[font_nr]->Render(txt);
> }

For my purposes I have written a function which returns unicode number
for ISO-8859-2 characters:

unsigned int iso2unicode (char c)
{
  switch (c) {
  case '�':return 0x0105;
  case '�':return 0x0104;
  case '�':return 0x0107;
  case '�':return 0x0106;
  case '�':return 0x0118;
  case '�':return 0x0119;
  case '�':return 0x0141;
  case '�':return 0x0142;
  case '�':return 0x0143;
  case '�':return 0x0144;
  case '�':return 0x00D3;
  case '�':return 0x00F3;
  case '�':return 0x015A;
  case '�':return 0x015B;
  case '�':return 0x0179;
  case '�':return 0x017A;
  case '�':return 0x017B;
  case '�':return 0x017C;
  default:
    return c;
  };
}

Then, the unicode number have to be used for getting an index of the glyph.

below you can find the source which I have made to render a TTF
characters into a simple PixMap (this is a part of my TTF class, so for
sure will not works for you by just copy & paste, but I hope it will
give you a hint ):

PixMap *TTF::drawText (const char *text)
{
  FT_BBox bbox;
  PixMap *pixmap=NULL;
  int error;
  int ginx;
  unsigned int k,i;
  unsigned int offset=0, xoffset=0;
  unsigned char b;
  int width=0, height=0;
  int yMin=0, yMax=0;
  unsigned int znak;

  // 1st pass - oblicz wielko�� pixmapy

  for (i=0; i<strlen (text); i++) {
    znak = iso2unicode (text[i]);
    ginx = FT_Get_Char_Index (face, znak);
    FT_Load_Glyph (face, ginx, 0);
    FT_Get_Glyph (face->glyph, &glyph);
    FT_Render_Glyph (face->glyph, FT_RENDER_MODE_NORMAL);
    slot=face->glyph;

    width+=(slot->metrics.horiAdvance / 64);
    FT_Glyph_Get_CBox (glyph, 0, &bbox);
    yMin = MIN (yMin, bbox.yMin);
    yMax = MAX (yMax, bbox.yMax);
  };

  height = (yMax - yMin ) / 64;
  width = ((width / 4)+1)*4;
  pixmap=new PixMap;
  pixmap->width=width;
  pixmap->height=height;
  pixmap->bpp=4;
  pixmap->pixels  = (unsigned char *)calloc (width*height*4, 1);

  // 2 przebieg - rysujemy

  for (k=0; k<strlen (text); k++) {
    znak = iso2unicode (text[k]);
    ginx = FT_Get_Char_Index (face, znak);

    FT_Load_Glyph (face, ginx, 0);
    FT_Get_Glyph (face->glyph, &glyph);
    FT_Render_Glyph (face->glyph, FT_RENDER_MODE_NORMAL);
    slot=face->glyph;

    FT_Glyph_Get_CBox (glyph, 0, &bbox);

    offset=0;
    for (int i=0; i<slot->bitmap.rows; i++) {
      for (int j=0; j<slot->bitmap.width; j++) {
	b=slot->bitmap.buffer[offset++];
	pixmap->draw (xoffset+j, (yMax-bbox.yMax)/64+i,
		      255,255,255,b);
      };
    };

    xoffset += (slot->metrics.horiAdvance / 64);
  };

  return pixmap;
}
0
Reply Krzysiek 12/22/2007 3:57:37 PM

2 Replies
178 Views

(page loaded in 0.133 seconds)

Similiar Articles:













7/10/2012 9:49:03 PM


Reply: