Merge commit '055aee28cedc3' into klp-dev: am: 2051465f2e am: d7cab14288 am: cafd00b375 am: 4cea75b987 am: fb5baf6521 am: fa744d0a95 am: f77113a2f7 am: c10d8e4d3b am: e314d1ffe2 am: 75a1d7329b am: 9037e1d2ba am: 838ce87483 am: fead4c9960 am: a2b598aaa1 am: 4d1264b083
am: 613f720f08

Change-Id: Ia12f79f3d9ebe3d6752680b75d3377eeb8b992b4
diff --git a/README.android b/README.android
index 3f599a0..b634371 100644
--- a/README.android
+++ b/README.android
@@ -19,3 +19,17 @@
 The following option is enabled to support color fonts.
 
   FT_CONFIG_OPTION_USE_PNG
+
+The following commits are cherry-picked from the upstream repository.
+
+  1c6fd994376c182f07cd59558a2f9bdd082b9509
+  Author: Werner Lemberg <wl@gnu.org>
+  [sfnt] Improve FT_LOAD_BITMAP_METRICS_ONLY for `sbix' format.
+
+  565db95b5cc87f8875576afb4282f443a5a10a8e
+  Author: Werner Lemberg <wl@gnu.org>
+  [sfnt] Speed up `sbix' lookup.
+
+  37e193e9357bdccbfb8a4437ddfdc06efd9e140c
+  Author: Werner Lemberg <wl@gnu.org>
+  Introduce a way of quickly retrieving (embedded) bitmap metrics.
diff --git a/include/freetype/freetype.h b/include/freetype/freetype.h
index 10503fd..b19188e 100644
--- a/include/freetype/freetype.h
+++ b/include/freetype/freetype.h
@@ -2790,6 +2790,14 @@
    *
    *     Currently, this flag is only implemented for TrueType fonts.
    *
+   *   FT_LOAD_BITMAP_METRICS_ONLY ::
+   *     This flag is used to request loading of the metrics and bitmap
+   *     image information of a (possibly embedded) bitmap glyph without
+   *     allocating or copying the bitmap image data itself.  No effect if
+   *     the target glyph is not a bitmap image.
+   *
+   *     This flag unsets @FT_LOAD_RENDER.
+   *
    *   FT_LOAD_CROP_BITMAP ::
    *     Ignored.  Deprecated.
    *
@@ -2836,6 +2844,7 @@
   /* Bits 16..19 are used by `FT_LOAD_TARGET_' */
 #define FT_LOAD_COLOR                        ( 1L << 20 )
 #define FT_LOAD_COMPUTE_METRICS              ( 1L << 21 )
+#define FT_LOAD_BITMAP_METRICS_ONLY          ( 1L << 22 )
 
   /* */
 
diff --git a/src/base/ftobjs.c b/src/base/ftobjs.c
index 9006b59..060d78e 100644
--- a/src/base/ftobjs.c
+++ b/src/base/ftobjs.c
@@ -641,6 +641,9 @@
       load_flags &= ~FT_LOAD_RENDER;
     }
 
+    if ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY )
+      load_flags &= ~FT_LOAD_RENDER;
+
     /*
      * Determine whether we need to auto-hint or not.
      * The general rules are:
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_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 0a90111..ba89672 100644
--- a/src/sfnt/ttsbit.c
+++ b/src/sfnt/ttsbit.c
@@ -48,6 +48,7 @@
   {
     FT_Error  error;
     FT_ULong  table_size;
+    FT_ULong  table_start;
 
 
     face->sbit_table       = NULL;
@@ -83,6 +84,8 @@
       goto Exit;
     }
 
+    table_start = FT_STREAM_POS();
+
     switch ( (FT_UInt)face->sbit_table_type )
     {
     case TT_SBIT_TABLE_TYPE_EBLC:
@@ -200,7 +203,14 @@
     face->ebdt_start = 0;
     face->ebdt_size  = 0;
 
-    if ( face->sbit_table_type != TT_SBIT_TABLE_TYPE_NONE )
+    if ( face->sbit_table_type == TT_SBIT_TABLE_TYPE_SBIX )
+    {
+      /* the `sbix' table is self-contained; */
+      /* it has no associated data table     */
+      face->ebdt_start = table_start;
+      face->ebdt_size  = table_size;
+    }
+    else if ( face->sbit_table_type != TT_SBIT_TABLE_TYPE_NONE )
     {
       FT_ULong  ebdt_size;
 
@@ -378,7 +388,6 @@
         FT_UInt         offset;
         FT_UShort       upem, ppem, resolution;
         TT_HoriHeader  *hori;
-        FT_ULong        table_size;
         FT_Pos          ppem_; /* to reduce casts */
 
         FT_Error  error;
@@ -388,15 +397,11 @@
         p      = face->sbit_table + 8 + 4 * strike_index;
         offset = FT_NEXT_ULONG( p );
 
-        error = face->goto_table( face, TTAG_sbix, stream, &table_size );
-        if ( error )
-          return error;
-
-        if ( offset + 4  > table_size )
+        if ( offset + 4 > face->ebdt_size )
           return FT_THROW( Invalid_File_Format );
 
-        if ( FT_STREAM_SEEK( FT_STREAM_POS() + offset ) ||
-             FT_FRAME_ENTER( 4 )                        )
+        if ( FT_STREAM_SEEK( face->ebdt_start + offset ) ||
+             FT_FRAME_ENTER( 4 )                         )
           return error;
 
         ppem       = FT_GET_USHORT();
@@ -525,7 +530,8 @@
 
 
   static FT_Error
-  tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder  decoder )
+  tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder  decoder,
+                                FT_Bool         metrics_only )
   {
     FT_Error    error = FT_Err_Ok;
     FT_UInt     width, height;
@@ -588,6 +594,9 @@
     if ( size == 0 )
       goto Exit;     /* exit successfully! */
 
+    if ( metrics_only )
+      goto Exit;     /* only metrics are requested */
+
     error = ft_glyphslot_alloc_bitmap( decoder->face->root.glyph, size );
     if ( error )
       goto Exit;
@@ -654,7 +663,8 @@
                               FT_UInt         glyph_index,
                               FT_Int          x_pos,
                               FT_Int          y_pos,
-                              FT_UInt         recurse_count );
+                              FT_UInt         recurse_count,
+                              FT_Bool         metrics_only );
 
   typedef FT_Error  (*TT_SBitDecoder_LoadFunc)(
                       TT_SBitDecoder  decoder,
@@ -984,7 +994,9 @@
                                           gindex,
                                           x_pos + dx,
                                           y_pos + dy,
-                                          recurse_count + 1 );
+                                          recurse_count + 1,
+                                          /* request full bitmap image */
+                                          FALSE );
       if ( error )
         break;
     }
@@ -1048,6 +1060,7 @@
                            decoder->stream->memory,
                            p,
                            png_len,
+                           FALSE,
                            FALSE );
 
   Exit:
@@ -1066,7 +1079,8 @@
                                FT_ULong        glyph_size,
                                FT_Int          x_pos,
                                FT_Int          y_pos,
-                               FT_UInt         recurse_count )
+                               FT_UInt         recurse_count,
+                               FT_Bool         metrics_only )
   {
     FT_Error   error;
     FT_Stream  stream = decoder->stream;
@@ -1188,11 +1202,15 @@
 
       if ( !decoder->bitmap_allocated )
       {
-        error = tt_sbit_decoder_alloc_bitmap( decoder );
+        error = tt_sbit_decoder_alloc_bitmap( decoder, metrics_only );
+
         if ( error )
           goto Fail;
       }
 
+      if ( metrics_only )
+        goto Fail; /* this is not an error */
+
       error = loader( decoder, p, p_limit, x_pos, y_pos, recurse_count );
     }
 
@@ -1209,7 +1227,8 @@
                               FT_UInt         glyph_index,
                               FT_Int          x_pos,
                               FT_Int          y_pos,
-                              FT_UInt         recurse_count )
+                              FT_UInt         recurse_count,
+                              FT_Bool         metrics_only )
   {
     FT_Byte*  p          = decoder->eblc_base + decoder->strike_index_array;
     FT_Byte*  p_limit    = decoder->eblc_limit;
@@ -1394,7 +1413,8 @@
                                         image_end,
                                         x_pos,
                                         y_pos,
-                                        recurse_count );
+                                        recurse_count,
+                                        metrics_only );
 
   Failure:
     return FT_THROW( Invalid_Table );
@@ -1413,10 +1433,10 @@
                            FT_UInt              glyph_index,
                            FT_Stream            stream,
                            FT_Bitmap           *map,
-                           TT_SBit_MetricsRec  *metrics )
+                           TT_SBit_MetricsRec  *metrics,
+                           FT_Bool              metrics_only )
   {
-    FT_UInt   sbix_pos, strike_offset, glyph_start, glyph_end;
-    FT_ULong  table_size;
+    FT_UInt   strike_offset, glyph_start, glyph_end;
     FT_Int    originOffsetX, originOffsetY;
     FT_Tag    graphicType;
     FT_Int    recurse_depth = 0;
@@ -1435,21 +1455,18 @@
     p = face->sbit_table + 8 + 4 * strike_index;
     strike_offset = FT_NEXT_ULONG( p );
 
-    error = face->goto_table( face, TTAG_sbix, stream, &table_size );
-    if ( error )
-      return error;
-    sbix_pos = FT_STREAM_POS();
-
   retry:
     if ( glyph_index > (FT_UInt)face->root.num_glyphs )
       return FT_THROW( Invalid_Argument );
 
-    if ( strike_offset >= table_size                          ||
-         table_size - strike_offset < 4 + glyph_index * 4 + 8 )
+    if ( strike_offset >= face->ebdt_size                          ||
+         face->ebdt_size - strike_offset < 4 + glyph_index * 4 + 8 )
       return FT_THROW( Invalid_File_Format );
 
-    if ( FT_STREAM_SEEK( sbix_pos + strike_offset + 4 + glyph_index * 4 ) ||
-         FT_FRAME_ENTER( 8 )                                              )
+    if ( FT_STREAM_SEEK( face->ebdt_start  +
+                         strike_offset + 4 +
+                         glyph_index * 4   ) ||
+         FT_FRAME_ENTER( 8 )                 )
       return error;
 
     glyph_start = FT_GET_ULONG();
@@ -1459,13 +1476,13 @@
 
     if ( glyph_start == glyph_end )
       return FT_THROW( Invalid_Argument );
-    if ( glyph_start > glyph_end                ||
-         glyph_end - glyph_start < 8            ||
-         table_size - strike_offset < glyph_end )
+    if ( glyph_start > glyph_end                     ||
+         glyph_end - glyph_start < 8                 ||
+         face->ebdt_size - strike_offset < glyph_end )
       return FT_THROW( Invalid_File_Format );
 
-    if ( FT_STREAM_SEEK( sbix_pos + strike_offset + glyph_start ) ||
-         FT_FRAME_ENTER( glyph_end - glyph_start )                )
+    if ( FT_STREAM_SEEK( face->ebdt_start + strike_offset + glyph_start ) ||
+         FT_FRAME_ENTER( glyph_end - glyph_start )                        )
       return error;
 
     originOffsetX = FT_GET_SHORT();
@@ -1496,7 +1513,8 @@
                              stream->memory,
                              stream->cursor,
                              glyph_end - glyph_start - 8,
-                             TRUE );
+                             TRUE,
+                             metrics_only );
 #else
       error = FT_THROW( Unimplemented_Feature );
 #endif
@@ -1556,23 +1574,27 @@
         error = tt_sbit_decoder_init( decoder, face, strike_index, metrics );
         if ( !error )
         {
-          error = tt_sbit_decoder_load_image( decoder,
-                                              glyph_index,
-                                              0,
-                                              0,
-                                              0 );
+          error = tt_sbit_decoder_load_image(
+                    decoder,
+                    glyph_index,
+                    0,
+                    0,
+                    0,
+                    ( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) != 0 );
           tt_sbit_decoder_done( decoder );
         }
       }
       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:
@@ -1581,9 +1603,10 @@
     }
 
     /* Flatten color bitmaps if color was not requested. */
-    if ( !error                                &&
-         !( load_flags & FT_LOAD_COLOR )       &&
-         map->pixel_mode == FT_PIXEL_MODE_BGRA )
+    if ( !error                                        &&
+         !( load_flags & FT_LOAD_COLOR )               &&
+         !( load_flags & FT_LOAD_BITMAP_METRICS_ONLY ) &&
+         map->pixel_mode == FT_PIXEL_MODE_BGRA         )
     {
       FT_Bitmap   new_map;
       FT_Library  library = face->root.glyph->library;