+ PFONTGDI FontGDI;
+ FT_Face face;
+ FT_GlyphSlot glyph;
+ INT error, n, glyph_index, i, previous;
+ LONG TotalWidth = 0, MaxHeight = 0;
+ FT_CharMap charmap, found = NULL;
+ BOOL use_kerning;
+
+ GetFontObjectsFromTextObj(TextObj, NULL, NULL, &FontGDI);
+ face = FontGDI->face;
+ if (NULL != Fit)
+ {
+ *Fit = 0;
+ }
+
+ if (face->charmap == NULL)
+ {
+ DPRINT("WARNING: No charmap selected!\n");
+ DPRINT("This font face has %d charmaps\n", face->num_charmaps);
+
+ for (n = 0; n < face->num_charmaps; n++)
+ {
+ charmap = face->charmaps[n];
+ DPRINT("found charmap encoding: %u\n", charmap->encoding);
+ if (charmap->encoding != 0)
+ {
+ found = charmap;
+ break;
+ }
+ }
+
+ if (! found)
+ {
+ DPRINT1("WARNING: Could not find desired charmap!\n");
+ }
+
+ error = FT_Set_Charmap(face, found);
+ if (error)
+ {
+ DPRINT1("WARNING: Could not set the charmap!\n");
+ }
+ }
+
+ error = FT_Set_Pixel_Sizes(face,
+ /* FIXME should set character height if neg */
+ (TextObj->logfont.lfHeight < 0 ?
+ - TextObj->logfont.lfHeight :
+ TextObj->logfont.lfHeight),
+ TextObj->logfont.lfWidth);
+ if (error)
+ {
+ DPRINT1("Error in setting pixel sizes: %u\n", error);
+ }
+
+ use_kerning = FT_HAS_KERNING(face);
+ previous = 0;
+
+ for (i = 0; i < Count; i++)
+ {
+ glyph_index = FT_Get_Char_Index(face, *String);
+ error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
+ if (error)
+ {
+ DPRINT1("WARNING: Failed to load and render glyph! [index: %u]\n", glyph_index);
+ }
+ glyph = face->glyph;
+
+ /* retrieve kerning distance */
+ if (use_kerning && previous && glyph_index)
+ {
+ FT_Vector delta;
+ FT_Get_Kerning(face, previous, glyph_index, 0, &delta);
+ TotalWidth += delta.x >> 6;
+ }
+
+ TotalWidth += glyph->advance.x >> 6;
+ if (glyph->format == ft_glyph_format_outline)
+ {
+ error = FT_Render_Glyph(glyph, ft_render_mode_mono);
+ if (error)
+ {
+ DPRINT1("WARNING: Failed to render glyph!\n");
+ }
+
+ if (0 != glyph->bitmap.rows && MaxHeight < (glyph->bitmap.rows - 1))
+ {
+ MaxHeight = glyph->bitmap.rows - 1;
+ }
+ }
+
+ if (TotalWidth <= MaxExtent && NULL != Fit)
+ {
+ *Fit = i + 1;
+ }
+ if (NULL != Dx)
+ {
+ Dx[i] = TotalWidth;
+ }
+
+ previous = glyph_index;
+ String++;
+ }
+
+ Size->cx = TotalWidth;
+ Size->cy = MaxHeight;
+
+ return TRUE;