Commit 1c6fd994376c182f07cd59558a2f9bdd082b9509

Werner Lemberg 2016-11-06T12:37:55

[sfnt] Improve FT_LOAD_BITMAP_METRICS_ONLY for `sbix' format. It's unavoidable to call the PNG engine, but to get the metrics it is sufficient to read the PNG image's header only. * src/sfnt/pngshim.c (Load_SBit_Png): Add argument to control the allocation of the glyph slot. * src/sfnt/pngshim.h: Updated. * src/sfnt/ttsbit.c (tt_sbit_decoder_load_png, tt_face_load_sbix_image, tt_face_load_sbit_image): Updated.

diff --git a/ChangeLog b/ChangeLog
index 20c9f3e..e53fc93 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,18 @@
 2016-11-06  Werner Lemberg  <wl@gnu.org>
 
+	[sfnt] Improve FT_LOAD_BITMAP_METRICS_ONLY for `sbix' format.
+
+	It's unavoidable to call the PNG engine, but to get the metrics it
+	is sufficient to read the PNG image's header only.
+
+	* src/sfnt/pngshim.c (Load_SBit_Png): Add argument to control the
+	allocation of the glyph slot.
+	* src/sfnt/pngshim.h: Updated.
+	* src/sfnt/ttsbit.c (tt_sbit_decoder_load_png,
+	tt_face_load_sbix_image, tt_face_load_sbit_image): Updated.
+
+2016-11-06  Werner Lemberg  <wl@gnu.org>
+
 	[sfnt] Speed up `sbix' lookup.
 
 	This also fixes a bug introduced in 2016-10-01 which prevents
diff --git a/src/sfnt/pngshim.c b/src/sfnt/pngshim.c
index 2815759..ff67292 100644
--- a/src/sfnt/pngshim.c
+++ b/src/sfnt/pngshim.c
@@ -184,7 +184,8 @@
                  FT_Memory        memory,
                  FT_Byte*         data,
                  FT_UInt          png_len,
-                 FT_Bool          populate_map_and_metrics )
+                 FT_Bool          populate_map_and_metrics,
+                 FT_Bool          metrics_only )
   {
     FT_Bitmap    *map   = &slot->bitmap;
     FT_Error      error = FT_Err_Ok;
@@ -258,9 +259,6 @@
 
     if ( populate_map_and_metrics )
     {
-      FT_ULong  size;
-
-
       metrics->width  = (FT_UShort)imgWidth;
       metrics->height = (FT_UShort)imgHeight;
 
@@ -276,13 +274,6 @@
         error = FT_THROW( Array_Too_Large );
         goto DestroyExit;
       }
-
-      /* this doesn't overflow: 0x7FFF * 0x7FFF * 4 < 2^32 */
-      size = map->rows * (FT_ULong)map->pitch;
-
-      error = ft_glyphslot_alloc_bitmap( slot, size );
-      if ( error )
-        goto DestroyExit;
     }
 
     /* convert palette/gray image to rgb */
@@ -334,6 +325,9 @@
       goto DestroyExit;
     }
 
+    if ( metrics_only )
+      goto DestroyExit;
+
     switch ( color_type )
     {
     default:
@@ -349,6 +343,17 @@
       break;
     }
 
+    if ( populate_map_and_metrics )
+    {
+      /* this doesn't overflow: 0x7FFF * 0x7FFF * 4 < 2^32 */
+      FT_ULong  size = map->rows * (FT_ULong)map->pitch;
+
+
+      error = ft_glyphslot_alloc_bitmap( slot, size );
+      if ( error )
+        goto DestroyExit;
+    }
+
     if ( FT_NEW_ARRAY( rows, imgHeight ) )
     {
       error = FT_THROW( Out_Of_Memory );
diff --git a/src/sfnt/pngshim.h b/src/sfnt/pngshim.h
index ff05871..0af4bfc 100644
--- a/src/sfnt/pngshim.h
+++ b/src/sfnt/pngshim.h
@@ -38,7 +38,8 @@ FT_BEGIN_HEADER
                  FT_Memory        memory,
                  FT_Byte*         data,
                  FT_UInt          png_len,
-                 FT_Bool          populate_map_and_metrics );
+                 FT_Bool          populate_map_and_metrics,
+                 FT_Bool          metrics_only );
 
 #endif
 
diff --git a/src/sfnt/ttsbit.c b/src/sfnt/ttsbit.c
index de59673..bc51304 100644
--- a/src/sfnt/ttsbit.c
+++ b/src/sfnt/ttsbit.c
@@ -1071,6 +1071,7 @@
                            decoder->stream->memory,
                            p,
                            png_len,
+                           FALSE,
                            FALSE );
 
   Exit:
@@ -1443,7 +1444,8 @@
                            FT_UInt              glyph_index,
                            FT_Stream            stream,
                            FT_Bitmap           *map,
-                           TT_SBit_MetricsRec  *metrics )
+                           TT_SBit_MetricsRec  *metrics,
+                           FT_Bool              metrics_only )
   {
     FT_UInt   strike_offset, glyph_start, glyph_end;
     FT_Int    originOffsetX, originOffsetY;
@@ -1522,7 +1524,8 @@
                              stream->memory,
                              stream->cursor,
                              glyph_end - glyph_start - 8,
-                             TRUE );
+                             TRUE,
+                             metrics_only );
 #else
       error = FT_THROW( Unimplemented_Feature );
 #endif
@@ -1595,12 +1598,14 @@
       break;
 
     case TT_SBIT_TABLE_TYPE_SBIX:
-      error = tt_face_load_sbix_image( face,
-                                       strike_index,
-                                       glyph_index,
-                                       stream,
-                                       map,
-                                       metrics );
+      error = tt_face_load_sbix_image(
+                face,
+                strike_index,
+                glyph_index,
+                stream,
+                map,
+                metrics,
+                ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) != 0 );
       break;
 
     default: