freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[freetype2] parthw-wip 11f4cad 35/35: [gf] Preliminary tracing and error


From: Parth Wazurkar
Subject: [freetype2] parthw-wip 11f4cad 35/35: [gf] Preliminary tracing and error handling support and minor fixes.
Date: Wed, 4 Jul 2018 14:09:33 -0400 (EDT)

branch: parthw-wip
commit 11f4cadddd0a28efbcb2bb6556408b60b9060867
Author: Parth Wazurkar <address@hidden>
Commit: Parth Wazurkar <address@hidden>

    [gf] Preliminary tracing and error handling support and minor fixes.
---
 include/freetype/internal/fttrace.h |   1 +
 src/gf/gfdrivr.c                    | 113 +++++++++++++++++++++++++++---------
 src/gf/gfdrivr.h                    |   2 +-
 src/gf/gflib.c                      |  88 +++++++++++++++-------------
 4 files changed, 135 insertions(+), 69 deletions(-)

diff --git a/include/freetype/internal/fttrace.h 
b/include/freetype/internal/fttrace.h
index f59a06e..30a9609 100644
--- a/include/freetype/internal/fttrace.h
+++ b/include/freetype/internal/fttrace.h
@@ -118,6 +118,7 @@ FT_TRACE_DEF( bdflib )
 
   /* GF font components */
 FT_TRACE_DEF( gfdriver )
+FT_TRACE_DEF( gflib )
 
   /* PFR font component */
 FT_TRACE_DEF( pfr )
diff --git a/src/gf/gfdrivr.c b/src/gf/gfdrivr.c
index 05129bb..0c8717d 100644
--- a/src/gf/gfdrivr.c
+++ b/src/gf/gfdrivr.c
@@ -143,7 +143,6 @@
     memory = FT_FACE_MEMORY( face );
 
     gf_free_font( gfface, memory );
-    /* FT_FREE(  ); */
   }
 
 
@@ -158,19 +157,48 @@
     FT_Error    error;
     FT_Memory   memory = FT_FACE_MEMORY( face );
     GF_Glyph    go;
-    int i,count;
+    FT_UInt16   i,count;
 
     FT_UNUSED( num_params );
     FT_UNUSED( params );
     go=NULL;
+
     FT_TRACE2(( "GF driver\n" ));
 
     /* load font */
     error = gf_load_font( stream, memory, &go );
-    if ( error )
+
+    if ( FT_ERR_EQ( error, Unknown_File_Format ) )
+    {
+      FT_TRACE2(( "  not a GF file\n" ));
+      goto Fail;
+    }
+    else if ( error )
       goto Exit;
 
     face->gf_glyph = go ;
+
+    /* sanity check */
+    if ( !face->gf_glyph->bm_table )
+    {
+      FT_TRACE2(( "bitmaps not allocated\n" ));
+      error = FT_THROW( Invalid_File_Format );
+      goto Fail;
+    }
+
+    /* GF cannot have multiple faces in a single font file.
+     * XXX: non-zero face_index is already invalid argument, but
+     *      Type1, Type42 driver has a convention to return
+     *      an invalid argument error when the font could be
+     *      opened by the specified driver.
+     */
+    if ( face_index > 0 && ( face_index & 0xFFFF ) > 0 )
+    {
+      FT_ERROR(( "GF_Face_Init: invalid face index\n" ));
+      GF_Face_Done( gfface );
+      return FT_THROW( Invalid_Argument );
+    }
+
     /* we now need to fill the root FT_Face fields */
     /* with relevant information                   */
 
@@ -182,7 +210,7 @@
      * XXX: TO-DO: gfface->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
      * XXX: I have to check for this.
      */
-     printf("Hi I am here2\n");
+
     gfface->family_name     = NULL;
     count=0;
     for (i = 0; i < 256; i++)
@@ -192,42 +220,58 @@
     }
     gfface->num_glyphs      = (FT_Long)count;printf("count is %d", count);
 
-printf("Hi I am here3\n");
+    FT_TRACE4(( "  number of glyphs: allocated %d\n",gfface->num_glyphs));
+
+    if ( gfface->num_glyphs <= 0 )
+    {
+      FT_ERROR(( "GF_Face_Init: glyphs not allocated\n" ));
+      error = FT_THROW( Invalid_File_Format );
+      goto Fail;
+    }
+
+    gfface->num_fixed_sizes = 1;
     if ( FT_NEW_ARRAY( gfface->available_sizes, 1 ) )
       goto Exit;
-printf("Hi I am here4\n");
+
     {
       FT_Bitmap_Size*  bsize = gfface->available_sizes;
+      FT_UShort        x_res, y_res;
 
       bsize->width  = (FT_Short) face->gf_glyph->font_bbx_w ;
       bsize->height = (FT_Short) face->gf_glyph->font_bbx_h ;
-      bsize->size   = (FT_Short) face->gf_glyph->ds         ; /* Preliminary 
to be checked for 26.6 fractional points*/
+      bsize->size   = (FT_Pos)   face->gf_glyph->ds         ; /* Preliminary 
to be checked for 26.6 fractional points*/
 
       /*x_res =  ;  To be Checked for x_resolution and y_resolution
       y_res =  ;*/
 
-      bsize->y_ppem = face->gf_glyph->font_bbx_yoff ;
-      bsize->x_ppem = face->gf_glyph->font_bbx_xoff ;
+      bsize->y_ppem = (FT_Pos)face->gf_glyph->font_bbx_yoff ;
+      bsize->x_ppem = (FT_Pos)face->gf_glyph->font_bbx_xoff ;
     }
 printf("Hi I am here5\n");
-      /* Charmaps */
-
-      {
-        FT_CharMapRec  charmap;
+    /* Charmaps */
 
+    {
+      FT_CharMapRec  charmap;
 
-        charmap.encoding    = FT_ENCODING_NONE;
-        /* initial platform/encoding should indicate unset status? */
-        charmap.platform_id = TT_PLATFORM_APPLE_UNICODE;  /*Preliminary */
-        charmap.encoding_id = TT_APPLE_ID_DEFAULT;
-        charmap.face        = FT_FACE( face );
+      charmap.encoding    = FT_ENCODING_NONE;
+      /* initial platform/encoding should indicate unset status? */
+      charmap.platform_id = TT_PLATFORM_APPLE_UNICODE;  /*Preliminary */
+      charmap.encoding_id = TT_APPLE_ID_DEFAULT;
+      charmap.face        = FT_FACE( face );
 
-        error = FT_CMap_New( &gf_cmap_class, NULL, &charmap, NULL );
+      error = FT_CMap_New( &gf_cmap_class, NULL, &charmap, NULL );
 
-        if ( error )
-          goto Fail;
+      if ( error )
+        goto Fail;
 printf("Hi I am here completed GF_Face_Init1\n");
-      }
+    }
+
+    if ( go->code_max < go->code_min )
+    {
+      FT_TRACE2(( "invalid number of glyphs\n" ));
+      error = FT_THROW( Invalid_File_Format );
+      goto Fail;
+    }
 printf("Hi I am here6\n");
   Fail:
     GF_Face_Done( gfface );
@@ -305,20 +349,20 @@ printf("Hi I am here6\n");
     FT_Error     error  = FT_Err_Ok;
     FT_Bitmap*   bitmap = &slot->bitmap;
     GF_BitmapRec bm ;
-    GF_Glyph    go;
-
-    go = gf->gf_glyph;
+    GF_Glyph     go;
 
     FT_UNUSED( load_flags );
 
-
     if ( !face )
     {
       error = FT_THROW( Invalid_Face_Handle );
       goto Exit;
     }
 
-    if ( glyph_index >= (FT_UInt)face->num_glyphs )
+    go = gf->gf_glyph;
+
+    if ( !go                                         ||
+         glyph_index >= (FT_UInt)( face->num_glyphs ) )
     {
       error = FT_THROW( Invalid_Argument );
       goto Exit;
@@ -331,16 +375,31 @@ printf("Hi I am here6\n");
 
     if ((glyph_index < go->code_min) || (go->code_max < glyph_index))
     {
+      FT_TRACE2(( "invalid glyph index\n" ));
       error = FT_THROW( Invalid_Argument );
       goto Exit;
     }
 
+    if ( !go->bm_table )
+    {
+      FT_TRACE2(( "invalid bitmap table\n" ));
+      error = FT_THROW( Invalid_File_Format );
+      goto Exit;
+    }
+
     /* slot, bitmap => freetype, glyph => gflib */
     bm = gf->gf_glyph->bm_table[glyph_index];
 
     bitmap->rows  = bm.mv_y   ; /* Prelimiary */
     bitmap->width = bm.mv_x   ; /* Prelimiary */
+
     bitmap->pitch = bm.raster ; /* Prelimiary */
+    if ( !bm.raster )
+    {
+      FT_TRACE2(( "invalid bitmap width\n" ));
+      error = FT_THROW( Invalid_File_Format );
+      goto Exit;
+    }
 
     /* note: we don't allocate a new array to hold the bitmap; */
     /*       we can simply point to it                         */
diff --git a/src/gf/gfdrivr.h b/src/gf/gfdrivr.h
index f32b3b0..da8f97d 100644
--- a/src/gf/gfdrivr.h
+++ b/src/gf/gfdrivr.h
@@ -43,7 +43,7 @@ FT_BEGIN_HEADER
   {
     FT_UInt         code_min, code_max;
     GF_Bitmap       bm_table;
-    double          ds, hppp, vppp;
+    FT_UInt         ds, hppp, vppp;
     FT_UInt         font_bbx_w, font_bbx_h;
     FT_UInt         font_bbx_xoff, font_bbx_yoff;
 
diff --git a/src/gf/gflib.c b/src/gf/gflib.c
index 95d9fad..8729290 100644
--- a/src/gf/gflib.c
+++ b/src/gf/gflib.c
@@ -121,7 +121,7 @@ unsigned char   bit_table[] = {
     unsigned char  *ptr;
     FT_Error       error  = FT_Err_Ok;
 
-    switch (READ_UINT1( stream ))
+    switch (d= READ_UINT1( stream ))
     {
     case GF_BOC:
       if ( FT_STREAM_SKIP( 4 ) )
@@ -133,7 +133,6 @@ unsigned char   bit_table[] = {
       min_n = READ_INT4( stream );
       max_n = READ_INT4( stream );
       break;
-
     case GF_BOC1:
       if ( FT_STREAM_SKIP( 1 ) )
         return -1;
@@ -144,7 +143,6 @@ unsigned char   bit_table[] = {
       min_m = max_m - del_m;
       min_n = max_n - del_n;
       break;
-
     default:
       return -1;
     }
@@ -155,6 +153,7 @@ unsigned char   bit_table[] = {
     h = max_n - min_n + 1;
     if ((w < 0) || (h < 0))
     {
+      FT_ERROR(( "gf_read_glyph: invalid w and h values\n" ));
       error = FT_THROW( Invalid_File_Format );
       return -1;
     }
@@ -277,7 +276,7 @@ unsigned char   bit_table[] = {
     INT4            min_m, max_m, min_n, max_n;
     INT4            w;
     UINT4           code;
-    double          dx, dy;
+    long            dx, dy;
     long            ptr_post, ptr_p, ptr, optr;
     int             bc, ec, nchars, i;
     FT_Error        error  = FT_Err_Ok;
@@ -289,70 +288,74 @@ unsigned char   bit_table[] = {
     /* seek to post_post instr. */
     /* fseek(fp, -1, SEEK_END); */
     if( FT_STREAM_SEEK( stream->size - 1 ) )
-      goto ErrExit;
-    if( FT_STREAM_SEEK( stream->size - 1 ) )
-      goto ErrExit;
+      goto Exit;
 
     while ( READ_UINT1( stream ) == 223)
     {
       if( FT_STREAM_SEEK( stream->pos -2 ) )
-        goto ErrExit;
+        goto Exit;
       /* fseek(fp, -2, SEEK_CUR); */
     }
 
-
     if( FT_STREAM_SEEK( stream->pos -1 ) )
-        goto ErrExit;
+        goto Exit;
     d= READ_UINT1( stream );
 
     if (d != GF_ID)
     {
+      FT_ERROR(( "gf_load_font: missing GF_ID(131) field\n" ));
       error = FT_THROW( Invalid_File_Format );
-      goto ErrExit;
+      goto Exit;
     }
+    FT_TRACE2(( "gf_load_font: GF_ID(131) found " ));
 
     /* fseek(fp, -6, SEEK_CUR); */
     if(FT_STREAM_SEEK( stream->pos -6 ))
-      goto ErrExit;
+      goto Exit;
 
     /* check if the code is post_post */
     if (READ_UINT1( stream ) != GF_POST_POST)
     {
+      FT_ERROR(( "gf_load_font: missing GF_POST_POST(249) field\n" ));
       error = FT_THROW( Invalid_File_Format );
-      goto ErrExit;
+      goto Exit;
     }
+    FT_TRACE2(( "gf_load_font: GF_POST_POST(249) found\n" ));
 
     /* read pointer to post instr. */
     if(FT_READ_ULONG( ptr_post ))
-      goto ErrExit;
+      goto Exit;
 
     if (ptr_post == -1)
     {
+      FT_ERROR(( "gf_load_font: invalid postamble pointer\n" ));
       error = FT_THROW( Invalid_File_Format );
-      goto ErrExit;
+      goto Exit;
     }
 
     /* goto post instr. and read it */
     /* fseek(fp, ptr_post, SEEK_SET); */
     if(FT_STREAM_SEEK( ptr_post ))
-      goto ErrExit;
+      goto Exit;
 
     if (READ_UINT1( stream ) != GF_POST)
     {
+      FT_ERROR(( "gf_load_font: missing GF_POST(248) field\n" ));
       error = FT_THROW( Invalid_File_Format );
-      goto ErrExit;
+      goto Exit;
     }
+    FT_TRACE2(( "gf_load_font: GF Postamble found\n" ));
 
     if(FT_READ_ULONG( ptr_p ))
-      goto ErrExit;
+      goto Exit;
     if(FT_READ_ULONG( ds ))
-      goto ErrExit;
+      goto Exit;
     if(FT_READ_ULONG( check_sum ))
-      goto ErrExit;
+      goto Exit;
     if(FT_READ_ULONG( hppp ))
-      goto ErrExit;
+      goto Exit;
     if(FT_READ_ULONG( vppp ))
-      goto ErrExit;
+      goto Exit;
     min_m     = READ_INT4( stream );
     max_m     = READ_INT4( stream );
     min_n     = READ_INT4( stream );
@@ -386,7 +389,7 @@ unsigned char   bit_table[] = {
         else
         {
           error = FT_THROW( Invalid_File_Format );
-          goto ErrExit;
+          goto Exit;
         }
         if (code < bc)
           bc = code;
@@ -401,18 +404,20 @@ unsigned char   bit_table[] = {
     nchars = ec - bc + 1;
     /*go= malloc(sizeof(GF_GlyphRec));*/
     if( FT_ALLOC(go, sizeof(GF_GlyphRec)) )
-      goto ErrExit;
+      goto Exit;
 
     /*go->bm_table = (GF_Bitmap)malloc(nchars* sizeof(GF_BitmapRec));*/
     if( FT_ALLOC_MULT(go->bm_table, sizeof(GF_BitmapRec), nchars) )
-      goto ErrExit;
+      goto Exit;
+
+    FT_TRACE2(( "gf_load_font: Allocated bitmap table\n" ));
 
     for (i = 0; i < nchars; i++)
       go->bm_table[i].bitmap = NULL;
 
-    go->ds   = (double)ds/(1<<20);
-    go->hppp = (double)hppp/(1<<16);
-    go->vppp = (double)vppp/(1<<16);
+    go->ds   = (FT_UInt)ds/(1<<20);
+    go->hppp = (FT_UInt)hppp/(1<<16);
+    go->vppp = (FT_UInt)vppp/(1<<16);
     go->font_bbx_w = max_m - min_m;
     go->font_bbx_h = max_n - min_n;
     go->font_bbx_xoff = min_m;
@@ -446,41 +451,38 @@ unsigned char   bit_table[] = {
         ptr  = READ_INT4( stream );
         break;
       default:
+        FT_ERROR(( "gf_load_font: missing character locators in postamble\n" 
));
         error = FT_THROW( Invalid_File_Format );
-        goto ErrExit;
+        goto Exit;
       }
 
       /* optr = ft_ftell(fp); */
       optr = stream->pos;
       /* ft_fseek(fp, ptr, SEEK_SET); */
       if( FT_STREAM_SEEK( ptr ) )
-        goto ErrExit;
+        goto Exit;
 
       bm = &go->bm_table[code - bc];
       if (gf_read_glyph( stream, bm, memory ) < 0)
-        goto ErrExit;
+        goto Exit;
 
       bm->mv_x = dx;
       bm->mv_y = dy;
       /* ft_fseek(fp, optr, SEEK_SET); */
       if(FT_STREAM_SEEK( optr ))
-        goto ErrExit;
+        goto Exit;
     }
     *goptr          = go;
   return error;
 
-               ErrExit:
+               Exit:
       printf("*ERROR\n");
       if (go != NULL)
       {
-        if (go->bm_table != NULL)
-        {
-          for (i = 0; i < nchars; i++){}
-            /* FT_FREE(go->bm_table[i].bitmap); */
-        }
-        /* FT_FREE(go->bm_table); */
+        FT_FREE(go->bm_table);
+        FT_FREE(go);
       }
-      /* FT_FREE(go); */
+      return error;
   }
 
 
@@ -490,7 +492,11 @@ unsigned char   bit_table[] = {
     GF_Face    gf = (GF_Face)gfface;
     GF_Glyph   go;
     go = gf->gf_glyph;
-
+    if (go != NULL)
+    {
+      FT_FREE(go->bm_table);
+      FT_FREE(go);
+    }
   }
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]