]> git.imager.perl.org - imager.git/blobdiff - freetyp2.c
0.73 release
[imager.git] / freetyp2.c
index 4d30c5c06a7e289b6845376b34c9a2bdc86b2457..d2e3c0695d6fad151ab334b963dffe7a91fa0416 100644 (file)
@@ -47,6 +47,7 @@ Truetype, Type1 and Windows FNT.
 
 static void ft2_push_message(int code);
 
+static int ft2_initialized = 0;
 static FT_Library library;
 
 /*
@@ -69,6 +70,9 @@ i_ft2_init(void) {
     i_push_error(0, "Initializing Freetype2");
     return 0;
   }
+
+  ft2_initialized = 1;
+
   return 1;
 }
 
@@ -132,6 +136,9 @@ i_ft2_new(const char *name, int index) {
 
   mm_log((1, "i_ft2_new(name %p, index %d)\n", name, index));
 
+  if (!ft2_initialized && !i_ft2_init())
+    return NULL;
+
   i_clear_error();
   error = FT_New_Face(library, name, index, &face);
   if (error) {
@@ -327,7 +334,7 @@ i_ft2_bbox(FT2_Fonthandle *handle, double cheight, double cwidth,
   FT_Glyph_Metrics *gm;
   int start = 0;
   int loadFlags = FT_LOAD_DEFAULT;
-  int rightb;
+  int rightb = 0;
 
   mm_log((1, "i_ft2_bbox(handle %p, cheight %f, cwidth %f, text %p, len %d, bbox %p)\n",
          handle, cheight, cwidth, text, len, bbox));
@@ -642,9 +649,8 @@ i_ft2_text(FT2_Fonthandle *handle, i_img *im, int tx, int ty, const i_color *cl,
   unsigned char map[256];
   char last_mode = ft_pixel_mode_none; 
   int last_grays = -1;
-  int ch;
-  i_color pel;
   int loadFlags = FT_LOAD_DEFAULT;
+  i_render render;
 
   mm_log((1, "i_ft2_text(handle %p, im %p, tx %d, ty %d, cl %p, cheight %f, cwidth %f, text %p, len %d, align %d, aa %d)\n",
          handle, im, tx, ty, cl, cheight, cwidth, text, align, aa));
@@ -663,6 +669,9 @@ i_ft2_text(FT2_Fonthandle *handle, i_img *im, int tx, int ty, const i_color *cl,
   if (!i_ft2_bbox(handle, cheight, cwidth, text, len, bbox, utf8))
     return 0;
 
+  if (aa)
+    i_render_init(&render, im, bbox[BBOX_POS_WIDTH] - bbox[BBOX_NEG_WIDTH]);
+
   if (!align) {
     /* this may need adjustment */
     tx -= bbox[0] * handle->matrix[0] + bbox[5] * handle->matrix[1] + handle->matrix[2];
@@ -688,6 +697,8 @@ i_ft2_text(FT2_Fonthandle *handle, i_img *im, int tx, int ty, const i_color *cl,
       ft2_push_message(error);
       i_push_errorf(0, "loading glyph for character \\x%02x (glyph 0x%04X)", 
                     c, index);
+      if (aa)
+        i_render_done(&render);
       return 0;
     }
     slot = handle->face->glyph;
@@ -698,6 +709,8 @@ i_ft2_text(FT2_Fonthandle *handle, i_img *im, int tx, int ty, const i_color *cl,
       if (error) {
        ft2_push_message(error);
        i_push_errorf(0, "rendering glyph 0x%04X (character \\x%02X)");
+      if (aa)
+        i_render_done(&render);
        return 0;
       }
       if (slot->bitmap.pixel_mode == ft_pixel_mode_mono) {
@@ -728,20 +741,16 @@ i_ft2_text(FT2_Fonthandle *handle, i_img *im, int tx, int ty, const i_color *cl,
          last_mode = slot->bitmap.pixel_mode;
          last_grays = slot->bitmap.num_grays;
        }
-       
+
        bmp = slot->bitmap.buffer;
        for (y = 0; y < slot->bitmap.rows; ++y) {
-         for (x = 0; x < slot->bitmap.width; ++x) {
-           int value = map[bmp[x]];
-           if (value) {
-             i_gpix(im, tx+x+slot->bitmap_left, ty+y-slot->bitmap_top, &pel);
-             for (ch = 0; ch < im->channels; ++ch) {
-               pel.channel[ch] = 
-                 ((255-value)*pel.channel[ch] + value * cl->channel[ch]) / 255;
-             }
-             i_ppix(im, tx+x+slot->bitmap_left, ty+y-slot->bitmap_top, &pel);
-           }
-         }
+          if (last_mode == ft_pixel_mode_grays &&
+              last_grays != 255) {
+            for (x = 0; x < slot->bitmap.width; ++x) 
+              bmp[x] = map[bmp[x]];
+          }
+          i_render_color(&render, tx + slot->bitmap_left, ty-slot->bitmap_top+y,
+                         slot->bitmap.width, bmp, cl);
          bmp += slot->bitmap.pitch;
        }
       }
@@ -751,6 +760,9 @@ i_ft2_text(FT2_Fonthandle *handle, i_img *im, int tx, int ty, const i_color *cl,
     ty -= slot->advance.y / 64;
   }
 
+  if (aa)
+    i_render_done(&render);
+
   return 1;
 }