Anonymous avatar Anonymous committed 70d0ffc

Fixed calculating drawn text size on FT fonts with pixels under the baseline.

Comments (0)

Files changed (5)

examples/freetype/sdlfont.py

 
     screen = video.set_mode (640, 480)
     screen.fill (pygame2.Color (200, 200, 200))
-    w, h, _ = font.render("Hello", pygame2.Color(100, 100, 100), None, screen, 100, 100, ptsize=24)
+    w, h, _ = font.render("Hello", pygame2.Color(100, 100, 100), None, screen, 100, 100, ptsize=48)
 
-    w, h, _ = font.render("Hello qjky", pygame2.Color(100, 100, 100), None, screen, 100, 200, ptsize=48)
+    w, h, _ = font.render("Hello qjky", pygame2.Color(100, 100, 100), None, screen, 100, 200, ptsize=48, vertical = True)
 
 #    w, g, buf = font.render("Hello World, Jay",
 #            pygame2.Color(100, 200, 32),

src/freetype/ft_metrics.c

 
     if (render->vertical)
     {
-        *w = text->max_w;
+        *w = PGFT_TRUNC(text->size.x);
         *h = PGFT_TRUNC(extent.y);
     }
     else
     {
         *w = PGFT_TRUNC(extent.x);
-        *h = text->max_h;
+        *h = PGFT_TRUNC(text->size.y);
     }
 
     return 0;

src/freetype/ft_render.c

 
     _PGFT_GetTextSize_INTERNAL(ft, font, font_size, render, font_text, &width, &height);
 
-#if 0
+#if 1
     fprintf(stderr, "Drawing @ (%d, %d, %d, %d)\n", x, y, width, height);
 
     SDL_Rect fill = {x, y, width, height};
     int x = surface->x_offset;
     int y = surface->y_offset;
 
+    if (render->vertical)
+        x += PGFT_TRUNC(PGFT_ROUND(text->size.x / 2));
+    else
+    {
+        y += PGFT_TRUNC(text->size.y);
+        y -= PGFT_TRUNC(text->baseline_offset.y);
+    }
+
+
     /* TODO: return if drawing outside surface */
 
 

src/freetype/ft_text.c

     int         swapped = 0;
     int         string_length = 0;
 
-    int         maxw = 0, maxh = 0;
-
     FT_UInt16 * buffer = NULL;
     FT_UInt16 * orig_buffer;
     FT_UInt16 * ch;
 
     FT_Pos      prev_rsb_delta = 0;
+    FT_Fixed    baseline;
 
     FontText  * ftext = NULL;
     FontGlyph * glyph = NULL;
 
     FT_Face     face;
 
+    FTC_ScalerRec scale;
+    FT_Size fontsize;
+
     /* compute proper load flags */
     load_flags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
 
     for (ch = buffer; *ch; ++ch)
         string_length++;
 
-
     /* create the text struct */
     ftext = malloc(sizeof(FontText));
     ftext->length = string_length;
     ftext->glyphs = calloc((size_t)string_length, sizeof(FontGlyph));
-
+    ftext->size.x = ftext->size.y = 0;
+    ftext->baseline_offset.x = ftext->baseline_offset.y = 0;
 
     /* fill it with the glyphs */
     glyph = &(ftext->glyphs[0]);
             glyph->vadvance.x = 0;
             glyph->vadvance.y = -metrics->vertAdvance;
 
-            /* TODO: Fix for vert drawing */
-            glyph->bearing = metrics->horiBearingY;
+            baseline = metrics->height - metrics->horiBearingY;
 
-            if (metrics->width > maxw)
-                maxw = metrics->width;
+            if (baseline > ftext->baseline_offset.y)
+                ftext->baseline_offset.y = baseline;
 
-            if (metrics->height > maxh)
-                maxh = metrics->height;
+            if (metrics->width > ftext->size.x)
+                ftext->size.x = metrics->width;
+
+            if (metrics->height > ftext->size.y)
+                ftext->size.y = metrics->height;
 
             if (prev_rsb_delta - face->glyph->lsb_delta >= 32)
                 glyph->delta = -1 << 6;
 
     }
 
-    ftext->max_w = PGFT_TRUNC(maxw);
-    ftext->max_h = PGFT_TRUNC(maxh);
-
     if (must_free)
         free(orig_buffer);
 

src/freetype/ft_wrap.h

     FT_Glyph    image;    
 
     FT_Pos      delta;    
-    FT_Fixed    bearing;
     FT_Vector   vvector;  
     FT_Vector   vadvance; 
 
     int length;
     FT_UInt32 _hash;
 
-    int max_w;
-    int max_h;
+    FT_Vector size;
+    FT_Vector baseline_offset;
 
 } FontText;
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.