- the font libraries are now only initialized when needed.
authorTony Cook <tony@develop=help.com>
Mon, 10 Dec 2007 10:17:24 +0000 (10:17 +0000)
committerTony Cook <tony@develop=help.com>
Mon, 10 Dec 2007 10:17:24 +0000 (10:17 +0000)
   http://rt.cpan.org/Ticket/Display.html?id=28825

Changes
Imager.pm
font.c
freetyp2.c
t/t30t1font.t
t/t35ttfont.t

diff --git a/Changes b/Changes
index 2bb19dbc9f9a3e75faee089c040ae38218fa258d..e7de08abe1cb9d10a3667c7bb21070038be5fb24 100644 (file)
--- a/Changes
+++ b/Changes
@@ -3,6 +3,9 @@ Imager release history.  Older releases can be found in Changes.old
 Imager 0.63 - unreleased
 ===========
 
+ - the font libraries are now only initialized when needed.
+   http://rt.cpan.org/Ticket/Display.html?id=28825
+
 Bug fixes:
 
  - Imager::Matrix2d->translate() now only requires one of the x or y
index ffc3974f97d1e5108141f0ebc96f1edb405585f0..08781af0153cde09d99575580ff7eed243ef5af3 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -173,14 +173,9 @@ BEGIN {
 }
 
 BEGIN {
-  i_init_fonts(); # Initialize font engines
   Imager::Font::__init();
   for(i_list_formats()) { $formats{$_}++; }
 
-  if ($formats{'t1'}) {
-    i_t1_set_aa(1);
-  }
-
   if (!$formats{'t1'} and !$formats{'tt'} 
       && !$formats{'ft2'} && !$formats{'w32'}) {
     $fontstate='no font support';
diff --git a/font.c b/font.c
index 0ab6f912fa7e17933c6413abad1156257546a9a5..706f06d6ee110d0477a6b05a714c47cdda3e0d35 100644 (file)
--- a/font.c
+++ b/font.c
@@ -63,18 +63,10 @@ i_init_fonts(int t1log) {
   mm_log((1,"Initializing fonts\n"));
 
 #ifdef HAVE_LIBT1
-  i_init_t1(t1log);
-#endif
-  
-#ifdef HAVE_LIBTT
-  i_init_tt();
-#endif
-
-#ifdef HAVE_FT2
-  if (!i_ft2_init())
+  if (i_init_t1(t1log))
     return 0;
 #endif
-
+  
   return(1); /* FIXME: Always true - check the return values of the init_t1 and init_tt functions */
 }
 
@@ -104,8 +96,11 @@ i_init_t1(int t1log) {
   int init_flags = IGNORE_CONFIGFILE|IGNORE_FONTDATABASE;
   mm_log((1,"init_t1()\n"));
 
+  i_clear_error();
+
   if (t1_active_fonts) {
     mm_log((1, "Cannot re-initialize T1 - active fonts\n"));
+    i_push_error(0, "Cannot re-initialize T1 - active fonts");
     return 1;
   }
 
@@ -117,6 +112,7 @@ i_init_t1(int t1log) {
     init_flags |= LOGFILE;
   if ((T1_InitLib(init_flags) == NULL)){
     mm_log((1,"Initialization of t1lib failed\n"));
+    i_push_error(0, "T1_InitLib failed");
     return(1);
   }
   T1_SetLogLevel(T1LOG_DEBUG);
@@ -160,6 +156,11 @@ int
 i_t1_new(char *pfb,char *afm) {
   int font_id;
 
+  i_clear_error();
+
+  if (!t1_initialized && i_init_t1(0))
+    return -1;
+
   mm_log((1,"i_t1_new(pfb %s,afm %s)\n",pfb,(afm?afm:"NULL")));
   font_id = T1_AddFont(pfb);
   if (font_id<0) {
@@ -851,6 +852,7 @@ static undef_int i_tt_bbox_inst( TT_Fonthandle *handle, int inst ,const char *tx
 
 /* static globals needed */
 
+static int TT_initialized = 0;
 static TT_Engine    engine;
 static int  LTT_dpi    = 72; /* FIXME: this ought to be a part of the call interface */
 static int  LTT_hinted = 1;  /* FIXME: this too */
@@ -870,14 +872,18 @@ Initializes the freetype font rendering engine
 */
 
 undef_int
-i_init_tt() {
+i_init_tt(void) {
   TT_Error  error;
   TT_Byte palette[] = { 0, 64, 127, 191, 255 };
 
+  i_clear_error();
+
   mm_log((1,"init_tt()\n"));
   error = TT_Init_FreeType( &engine );
   if ( error ){
     mm_log((1,"Initialization of freetype failed, code = 0x%x\n",error));
+    i_tt_push_error(error);
+    i_push_error(0, "Could not initialize freetype 1.x");
     return(1);
   }
 
@@ -885,6 +891,9 @@ i_init_tt() {
   error = TT_Init_Post_Extension( engine );
   if (error) {
     mm_log((1, "Initialization of Post extension failed = 0x%x\n", error));
+    
+    i_tt_push_error(error);
+    i_push_error(0, "Could not initialize FT 1.x POST extension");
     return 1;
   }
 #endif
@@ -892,9 +901,13 @@ i_init_tt() {
   error = TT_Set_Raster_Gray_Palette(engine, palette);
   if (error) {
     mm_log((1, "Initialization of gray levels failed = 0x%x\n", error));
+    i_tt_push_error(error);
+    i_push_error(0, "Could not initialize FT 1.x POST extension");
     return 1;
   }
 
+  TT_initialized = 1;
+
   return(0);
 }
 
@@ -1019,6 +1032,11 @@ i_tt_new(const char *fontname) {
   unsigned short i,n;
   unsigned short platform,encoding;
 
+  if (!TT_initialized && i_init_tt()) {
+    i_push_error(0, "Could not initialize FT1 engine");
+    return NULL;
+  }
+
   i_clear_error();
   
   mm_log((1,"i_tt_new(fontname '%s')\n",fontname));
index d0409970eced396945be4a820cf1bcb3315202ee..b3eccef2ea36a7ee309d8b141df0e86157f8a74a 100644 (file)
@@ -47,6 +47,7 @@ Truetype, Type1 and Windows FNT.
 
 static void ft2_push_message(int code);
 
+static 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) {
index c0ead46e0b34a1150547a3143af4b16d9c709af1..f08ea78fdd343b3f49a8ccd68afe545c5b7fae66 100644 (file)
@@ -34,7 +34,7 @@ SKIP:
 
   print "# has t1\n";
 
-  i_t1_set_aa(1);
+  #i_t1_set_aa(1);
 
   unlink "t1lib.log"; # lose it if it exists
   init(t1log=>0);
@@ -153,6 +153,7 @@ SKIP:
     is(length($exists), 2, "return scalar length");
     ok(ord(substr($exists, 0, 1)), "we have an exclamation mark");
     ok(!ord(substr($exists, 1, 1)), "we have no upper-case A");
+    i_t1_destroy($font_num);
   }
   
   my $font = Imager::Font->new(file=>$exists_font, type=>'t1');
index f628952a486d1597c865f3eb17e5cdbf22e2560a..9e8cf2538f51d4cd9b4c108e7ca187411c28380c 100644 (file)
@@ -24,7 +24,7 @@ SKIP:
     skip('Cannot load test font', 89);
   }
 
-  i_init_fonts();
+  #i_init_fonts();
   #     i_tt_set_aa(1);
   
   my $bgcolor = i_color_new(255,0,0,0);