trying to use kerning but not working cuz i cant find a font and these widths arent...
authorDana Jansens <danakj@orodu.net>
Thu, 29 May 2003 01:06:22 +0000 (01:06 +0000)
committerDana Jansens <danakj@orodu.net>
Thu, 29 May 2003 01:06:22 +0000 (01:06 +0000)
glft/font.c
glft/font.h
glft/render.c

index 6ebab3670751dd5a4a33c0cf499bf8e446911bbe..f93da67c87b2f97ab64e778f7b07b6965442e947 100644 (file)
@@ -174,9 +174,14 @@ struct GlftFont *GlftFontOpen(const char *name)
         GlftDebug("failed to open FT face\n");
         goto openfail0;
     }
+    assert(FT_IS_SCALABLE(font->face));
+    if (!FT_IS_SCALABLE(font->face)) {
+        GlftDebug("got a non-scalable face");
+        goto openfail1;
+    }
     if (FT_Set_Char_Size(font->face, 0, font->ftcharsize, 0, 0)) {
         GlftDebug("failed to set char size on FT face\n");
-        goto openfail0;
+        goto openfail1;
     }
 
     if (!FcPatternGetCharSet(match, FC_CHARSET, 0, &font->chars) !=
@@ -184,7 +189,7 @@ struct GlftFont *GlftFontOpen(const char *name)
         font->chars = FcFreeTypeCharSet(font->face, FcConfigGetBlanks(NULL));
     if (!font->chars) {
         GlftDebug("failed to get a valid CharSet\n");
-        goto openfail0;
+        goto openfail1;
     }
 
     if (font->char_width)
@@ -196,10 +201,14 @@ struct GlftFont *GlftFontOpen(const char *name)
     if (font->minspace) font->height = font->ascent + font->descent;
     else                font->height = font->face->size->metrics.height >> 6;
 
+    font->kerning = FT_HAS_KERNING(font->face);
+
     font->glyph_map = g_hash_table_new(g_int_hash, g_int_equal);
 
     return font;
 
+openfail1:
+    FT_Done_Face(font->face);
 openfail0:
     FcPatternDestroy(match);
     free(font);
@@ -239,8 +248,6 @@ struct GlftGlyph *GlftFontGlyph(struct GlftFont *font, const char *c)
         }
     }
     if (!g) {
-        assert(font->face->face_flags & FT_FACE_FLAG_SCALABLE);
-
         g = malloc(sizeof(struct GlftGlyph));
         g->w = w;
         g->dlist = glGenLists(1);
@@ -250,14 +257,33 @@ struct GlftGlyph *GlftFontGlyph(struct GlftFont *font, const char *c)
         if (!(font->spacing == FC_PROPORTIONAL)) {
             g->width = font->max_advance_width;
         } else {
-            /*g->width = TRUNC(ROUND(font->face->glyph->advance.x));*/
-            g->width = font->face->glyph->metrics.width >> 6;
-            g_message("Width: %c = %d", *c, g->width);
+            g->width = font->face->glyph->advance.x;
+            g->width = TRUNC(ROUND(font->face->glyph->metrics.width));/* >> 6;*/
         }
-        g->height = -(font->face->glyph->metrics.height >> 6);
+        g->height = TRUNC(ROUND(font->face->glyph->metrics.height));/* >> 6;*/
+        g_message("%c = Width: %d Height: %d", *c, g->width, g->height);
 
         g_hash_table_insert(font->glyph_map, &g->w, g);
     }
 
     return g;
 }
+
+int GlftFontAdvance(struct GlftFont *font,
+                    struct GlftGlyph *left,
+                    struct GlftGlyph *right)
+{
+    FT_Vector v;
+    int k = left->width;
+
+/*    font->kerning = 0;*/
+    g_message("HAS KERNING: %d", font->kerning);
+
+    if (font->kerning && right) {
+        FT_Get_Kerning(font->face, left->glyph, right->glyph,
+                       FT_KERNING_UNFITTED, &v);
+/*x_scale  = pixel_size_x / EM_size*/
+        k += v.x >> 6;
+    }
+    return k;
+}
index f428a9e8ec5302a13daab003c8e292f65cc0127f..f96b2e1a951df7cf65ba79c04b09a12921b2d012 100644 (file)
@@ -31,6 +31,8 @@ struct GlftFont {
 
     GHashTable *glyph_map;
 
+    int kerning : 1;
+
     /* public shit */
     int ascent;
     int descent;
@@ -43,6 +45,8 @@ struct GlftGlyph {
     FcChar32 w;
     /* OpenGL display list for the character */
     unsigned int dlist;
+    /* The FT_Face glyph */
+    FT_UInt glyph;
 
     int width;
     int height;
@@ -52,4 +56,8 @@ struct GlftGlyph {
  for it */
 struct GlftGlyph *GlftFontGlyph(struct GlftFont *font, const char *c);
 
+int GlftFontAdvance(struct GlftFont *font,
+                    struct GlftGlyph *left,
+                    struct GlftGlyph *right);
+
 #endif
index 2e9c5712f22531902c1d43b793e04e3b32da3733..d0465d0c070b15602662826f9ac3057f023629b5 100644 (file)
@@ -85,7 +85,7 @@ void GlftRenderString(struct GlftFont *font, const char *str, int bytes,
                       int x, int y)
 {
     const char *c;
-    struct GlftGlyph *g;
+    struct GlftGlyph *g, *n;
 
     if (!g_utf8_validate(str, bytes, NULL)) {
         GlftDebug("Invalid UTF-8 in string\n");
@@ -97,9 +97,10 @@ void GlftRenderString(struct GlftFont *font, const char *str, int bytes,
     c = str;
     while (c - str < bytes) {
         g = GlftFontGlyph(font, c);
+        n = (c - str < bytes - 1) ? GlftFontGlyph(font, c+1) : 0;
         if (g) {
             glCallList(g->dlist);
-            glTranslatef(g->width, 0.0, 0.0);
+            if (n) glTranslatef(GlftFontAdvance(font, g, n), 0.0, 0.0);
         } else
             glTranslatef(font->max_advance_width, 0.0, 0.0);
         c = g_utf8_next_char(c);
@@ -115,7 +116,7 @@ void GlftMeasureString(struct GlftFont *font,
                        int *h)
 {
     const char *c;
-    struct GlftGlyph *g;
+    struct GlftGlyph *g, *n;
 
     if (!g_utf8_validate(str, bytes, NULL)) {
         GlftDebug("Invalid UTF-8 in string\n");
@@ -128,8 +129,9 @@ void GlftMeasureString(struct GlftFont *font,
     c = str;
     while (c - str < bytes) {
         g = GlftFontGlyph(font, c);
+        n = (c - str < bytes - 1) ? GlftFontGlyph(font, c+1) : 0;
         if (g) {
-            *w += g->width;
+            *w += GlftFontAdvance(font, g, n);
             *h = MAX(g->height, *h);
         } else {
             *w += font->max_advance_width;