Commits

Anonymous committed 43eaa4c

Fixed metrics and layouting of styled fonts.

  • Participants
  • Parent commits 1439c9a
  • Branches pgreloaded

Comments (0)

Files changed (6)

File examples/freetype/sdlfont.py

     fontdir = os.path.dirname (os.path.abspath (__file__))
     font = freetype.Font (os.path.join (fontdir, "sans.ttf"))
 
-    screen = video.set_mode (800, 600)
+    screen = video.set_mode (300, 200)
     screen.fill (colors["grey_light"])
 
     font.render("Hello World", colors["red"], None, screen, 32, 32, ptsize=64, style=ftconstants.STYLE_BOLD)
     font.render("or BLAND!", pygame2.Color(0, 0xCC, 28, 128), None, screen, 258, 237, ptsize=64)
     font.render("I \u2665 Unicode", pygame2.Color(0, 0xCC, 0xDD), None, screen, 298, 320, ptsize=64)
     font.render("\u2665", colors["grey_light"], colors["red"], screen, 480, 32, ptsize=148)
-    font.render("...yes, this is a SDL surface", pygame2.Color(0, 0, 0), None, screen, 380, 380, ptsize=24, style=ftconstants.STYLE_BOLD)
+    font.render("...yes, this is a SDL surface", pygame2.Color(0, 0, 0), None, screen, 380, 380, ptsize=24,
+            style=ftconstants.STYLE_BOLD)
 
 
     screen.flip ()
             "src/freetype/ft_render.c",
             "src/freetype/ft_metrics.c",
             "src/freetype/ft_text.c",
-            "src/freetype/ft_style.c"
         ],
         instheaders = ["src/freetype/pgfreetype.h"],
         docfile = "freetypebase.xml",

File src/freetype/ft_render.c

 #   include "surface.h"
 #endif
 
-#define BOLD_FACTOR     0.08
-#define SLANT_FACTOR    0.22
-
 typedef void (* FontRenderPtr)(int, int, FontSurface *, FT_Bitmap *, PyColor *);
 
 /* blitters */
 
 void __render_glyph_ByteArray(int x, int y, FontSurface *surface, FT_Bitmap *bitmap, PyColor *color);
 
+
+#define SLANT_FACTOR    0.22
+FT_Matrix PGFT_SlantMatrix = 
+{
+    (1 << 16),  (FT_Fixed)(SLANT_FACTOR * (1 << 16)),
+    0,          (1 << 16) 
+};
+
+FT_Fixed PGFT_GetBoldStrength(FT_Face face)
+{
+    const float bold_factor = 0.06;
+    FT_Fixed bold_str;
+
+    bold_str = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale);
+    bold_str = (FT_Fixed)(bold_str * bold_factor);
+
+    return bold_str;
+}
+
 void 
 PGFT_BuildRenderMode(FontRenderMode *mode, int style, int vertical, int antialias, int rotation)
 {
     FT_Face     face;
     FT_Error    error;
 
-    FT_Fixed    bold_str;
-    FT_Matrix   shear_matrix;
+    FT_Fixed    bold_str = 0;
 
     int         x = (surface->x_offset << 6);
     int         y = (surface->y_offset << 6);
     /******************************************************
      * Prepare data for glyph transformations
      ******************************************************/
-
-    if (render->style & FT_STYLE_ITALIC)
-    {
-        shear_matrix.xx = (1<< 16);
-        shear_matrix.xy = (FT_Fixed)(SLANT_FACTOR * (1 << 16));
-        shear_matrix.yx = 0;
-        shear_matrix.yy = (1 << 16);
-    }
-
     if (render->style & FT_STYLE_BOLD)
     {
-        bold_str = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale);
-        bold_str = (FT_Fixed)(bold_str * BOLD_FACTOR);
+        bold_str = PGFT_GetBoldStrength(face);
     }
 
     /******************************************************
 
         if (image->format == FT_GLYPH_FORMAT_OUTLINE)
         {
+            FT_OutlineGlyph outline;
+            outline = (FT_OutlineGlyph)image;
+
             if (render->style & FT_STYLE_BOLD)
             {
-                FT_OutlineGlyph outline;
-                outline = (FT_OutlineGlyph)image;
                 FT_Outline_Embolden(&(outline->outline), bold_str);
             }
 
             if (render->style & FT_STYLE_ITALIC)
             {
-                FT_OutlineGlyph outline;
-                outline = (FT_OutlineGlyph)image;
-                FT_Outline_Transform(&(outline->outline), &shear_matrix);
+                FT_Outline_Transform(&(outline->outline), &PGFT_SlantMatrix);
             }
 
             if (render->vertical)

File src/freetype/ft_style.c

-/*
-  pygame - Python Game Library
-  Copyright (C) 2009 Vicent Marti
-
-  This library is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Library General Public
-  License as published by the Free Software Foundation; either
-  version 2 of the License, or (at your option) any later version.
-
-  This library is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  Library General Public License for more details.
-
-  You should have received a copy of the GNU Library General Public
-  License along with this library; if not, write to the Free
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-
-*/
-
-#define PYGAME_FREETYPE_INTERNAL
-
-#include "ft_mod.h"
-#include "ft_wrap.h"
-#include "pgfreetype.h"
-#include "pgtypes.h"
-#include "freetypebase_doc.h"
-
-#include FT_MODULE_H
-#include FT_OUTLINE_H 
- 
-
-#define BOLD_FACTOR     0.08
-#define SLANT_FACTOR    0.22
-
-int
-PGFT_Style_MakeBold(FT_Face face)
-{
-    FT_GlyphSlot    slot = face->glyph;
-    FT_Pos          xstr, ystr;
-    FT_Error        error;
-
-    xstr = FT_MulFix(face->units_per_EM,
-            face->size->metrics.y_scale);
-    xstr = (FT_Fixed)(xstr * BOLD_FACTOR);
-    ystr = xstr;
-
-    if (slot->format == FT_GLYPH_FORMAT_OUTLINE)
-    {
-        FT_Outline_Embolden(&slot->outline, xstr);
-        xstr = xstr * 2;
-        ystr = xstr;
-    }
-    else if (slot->format == FT_GLYPH_FORMAT_BITMAP)
-    {
-        FT_Library    library = slot->library;
-
-        xstr &= ~63;
-        ystr &= ~63;
-
-        error = FT_GlyphSlot_Own_Bitmap(slot);
-        if (error)
-            return error;
-
-        error = FT_Bitmap_Embolden(library, &slot->bitmap, xstr, ystr);
-        if (error)
-            return error;
-    }
-
-    if (slot->advance.x)
-        slot->advance.x += xstr;
-
-    if (slot->advance.y)
-        slot->advance.y += ystr;
-
-    slot->metrics.width        += xstr;
-    slot->metrics.height       += ystr;
-    slot->metrics.horiBearingY += ystr;
-    slot->metrics.horiAdvance  += xstr;
-    slot->metrics.vertBearingX -= xstr / 2;
-    slot->metrics.vertBearingY += ystr;
-    slot->metrics.vertAdvance  += ystr;
-    return 0;
-}
-
-

File src/freetype/ft_text.c

 
     FT_Pos      prev_rsb_delta = 0;
     FT_Fixed    baseline;
+    FT_Fixed    bold_str = 0;
 
     FontText  * ftext = NULL;
     FontGlyph * glyph = NULL;
     /* fill it with the glyphs */
     glyph = &(ftext->glyphs[0]);
 
+    if (render->style & FT_STYLE_BOLD)
+        bold_str = PGFT_GetBoldStrength(face);
+
     for (ch = buffer; *ch; ++ch, ++glyph)
     {
         FT_UInt16 c = *ch;
         {
             FT_Glyph_Metrics *metrics = &face->glyph->metrics;
 
-
             /* note that in vertical layout, y-positive goes downwards */
-            glyph->vvector.x  = metrics->vertBearingX - metrics->horiBearingX;
-            glyph->vvector.y  = -metrics->vertBearingY - metrics->horiBearingY;
+            glyph->vvector.x  = (metrics->vertBearingX - bold_str / 2) - metrics->horiBearingX;
+            glyph->vvector.y  = -(metrics->vertBearingY + bold_str) - (metrics->horiBearingY + bold_str);
 
             glyph->vadvance.x = 0;
-            glyph->vadvance.y = -metrics->vertAdvance;
+            glyph->vadvance.y = -(metrics->vertAdvance + bold_str);
 
             baseline = metrics->height - metrics->horiBearingY;
 
             if (baseline > ftext->baseline_offset.y)
                 ftext->baseline_offset.y = baseline;
 
-            if (metrics->width > ftext->glyph_size.x)
-                ftext->glyph_size.x = metrics->width;
+            if (metrics->width + bold_str > ftext->glyph_size.x)
+                ftext->glyph_size.x = metrics->width + bold_str;
 
-            if (metrics->height > ftext->glyph_size.y)
-                ftext->glyph_size.y = metrics->height;
+            if (metrics->height + bold_str > ftext->glyph_size.y)
+                ftext->glyph_size.y = metrics->height + bold_str;
 
             if (prev_rsb_delta - face->glyph->lsb_delta >= 32)
                 glyph->delta = -1 << 6;
     FT_Vector*  prev_advance = NULL;
     FT_Vector   extent       = {0, 0};
     FT_Int      i;
+    FT_Fixed    bold_str    = 0;
 
     face = _PGFT_GetFaceSized(ft, font, pt_size);
 
     if (!face)
         return -1;
 
+    if (render->style & FT_STYLE_BOLD)
+        bold_str = PGFT_GetBoldStrength(face);
+
     if (!render->vertical && render->kerning_degree)
     {
         FT_Fixed  ptsize;
         else
         {
             advances[i] = glyph->image->advance;
+
+            /* Convert to 26.6 */
             advances[i].x >>= 10;
             advances[i].y >>= 10;
 
+            /* Apply BOLD transformation */
+            if (advances[i].x)
+                advances[i].x += bold_str;
+
+            if (advances[i].y)
+                advances[i].y += bold_str;
+
             if (prev_advance)
             {
                 prev_advance->x += track_kern;

File src/freetype/ft_wrap.h

 void        PGFT_BuildRenderMode(FontRenderMode *mode, int style, int vertical, 
                 int antialias, int rotation);
 
+FT_Fixed    PGFT_GetBoldStrength(FT_Face face);
+
 int         _PGFT_Render_INTERNAL(FreeTypeInstance *ft, PyFreeTypeFont *font, 
                 FontText *text, int font_size, PyColor *fg_color, 
                 FontSurface *surface, FontRenderMode *render);