Commit c8a24209d314c23632deaccd328e107888fab13a

Werner Lemberg 2023-04-27T11:40:46

[truetype] Fix PostScript name handling for variation fonts. A variation font's PostScript name of a named instance is usually different from the PostScript name of an unnamed instance. However, if a change between a named instance and an unnamed instance with exactly the same design axis values happened, it was possible that the PostScript name wasn't correctly updated. This commit reorganizes the code to handle this issue within the top-level API functions, using a new service to trigger recomputation of the PostScript name. * include/freetype/internal/services/svmm.h (FT_Construct_PS_Name_Func): New typedef. (FT_Service_MultiMasters): New field `construct_ps_name`. (FT_DEFINE_SERVICE_MULTIMASTERSREC): Updated. * src/base/ftmm.c (FT_Set_Var_Design_Coordinates, FT_Set_MM_Blend_Coordinates, FT_Set_Var_Blend_Coordinates): Call `mm->construct_ps_name` to handle `postscript_name`. (FT_Set_Named_Instance): Call `mm->construct_ps_name` to handle `postscript_name`. Use shortcut. * src/cff/cffdrivr.c (cff_construct_ps_name): New function. (cff_service_multi_masters): Updated. * src/truetype/ttgxvar.c (tt_set_mm_blend): Don't handle `postscript_name`. (TT_Set_MM_Blend): Simplify. (TT_Set_Named_Instance): Return -1 if axis values haven't changed. Don't set `face_index`. (tt_construct_ps_name): New function. * src/truetype/ttgxvar.h: Updated. * src/truetype/ttdriver.c (tt_service_gx_multi_masters): Updated. * src/type1/t1driver.c (t1_service_multi_masters): Updated. * src/type1/t1load.c (T1_Set_MM_Blend): Simplify.

diff --git a/include/freetype/internal/services/svmm.h b/include/freetype/internal/services/svmm.h
index 59910f2..982bb47 100644
--- a/include/freetype/internal/services/svmm.h
+++ b/include/freetype/internal/services/svmm.h
@@ -102,6 +102,9 @@ FT_BEGIN_HEADER
                                   FT_UInt*   len,
                                   FT_Fixed*  weight_vector );
 
+  typedef void
+  (*FT_Construct_PS_Name_Func)( FT_Face  face );
+
   typedef FT_Error
   (*FT_Var_Load_Delta_Set_Idx_Map_Func)( FT_Face            face,
                                          FT_ULong           offset,
@@ -144,6 +147,7 @@ FT_BEGIN_HEADER
     FT_Get_MM_WeightVector_Func           get_mm_weightvector;
 
     /* for internal use; only needed for code sharing between modules */
+    FT_Construct_PS_Name_Func             construct_ps_name;
     FT_Var_Load_Delta_Set_Idx_Map_Func    load_delta_set_idx_map;
     FT_Var_Load_Item_Var_Store_Func       load_item_var_store;
     FT_Var_Get_Item_Delta_Func            get_item_delta;
@@ -166,6 +170,8 @@ FT_BEGIN_HEADER
                                            get_default_named_instance_, \
                                            set_mm_weightvector_,        \
                                            get_mm_weightvector_,        \
+                                                                        \
+                                           construct_ps_name_,          \
                                            load_delta_set_idx_map_,     \
                                            load_item_var_store_,        \
                                            get_item_delta_,             \
@@ -186,6 +192,8 @@ FT_BEGIN_HEADER
     get_default_named_instance_,                                        \
     set_mm_weightvector_,                                               \
     get_mm_weightvector_,                                               \
+                                                                        \
+    construct_ps_name_,                                                 \
     load_delta_set_idx_map_,                                            \
     load_item_var_store_,                                               \
     get_item_delta_,                                                    \
diff --git a/src/base/ftmm.c b/src/base/ftmm.c
index c061431..9e2dd7e 100644
--- a/src/base/ftmm.c
+++ b/src/base/ftmm.c
@@ -301,10 +301,26 @@
 
       if ( !error || error == -1 )
       {
+        FT_Bool  is_variation_old = FT_IS_VARIATION( face );
+
+
         if ( num_coords )
           face->face_flags |= FT_FACE_FLAG_VARIATION;
         else
           face->face_flags &= ~FT_FACE_FLAG_VARIATION;
+
+        if ( service_mm->construct_ps_name )
+        {
+          if ( error == -1 )
+          {
+            /* The PS name of a named instance and a non-named instance */
+            /* usually differs, even if the axis values are identical.  */
+            if ( is_variation_old != FT_IS_VARIATION( face ) )
+              service_mm->construct_ps_name( face );
+          }
+          else
+            service_mm->construct_ps_name( face );
+        }
       }
 
       /* internal error code -1 means `no change'; we can exit immediately */
@@ -385,10 +401,26 @@
 
       if ( !error || error == -1 )
       {
+        FT_Bool  is_variation_old = FT_IS_VARIATION( face );
+
+
         if ( num_coords )
           face->face_flags |= FT_FACE_FLAG_VARIATION;
         else
           face->face_flags &= ~FT_FACE_FLAG_VARIATION;
+
+        if ( service_mm->construct_ps_name )
+        {
+          if ( error == -1 )
+          {
+            /* The PS name of a named instance and a non-named instance */
+            /* usually differs, even if the axis values are identical.  */
+            if ( is_variation_old != FT_IS_VARIATION( face ) )
+              service_mm->construct_ps_name( face );
+          }
+          else
+            service_mm->construct_ps_name( face );
+        }
       }
 
       /* internal error code -1 means `no change'; we can exit immediately */
@@ -444,10 +476,26 @@
 
       if ( !error || error == -1 )
       {
+        FT_Bool  is_variation_old = FT_IS_VARIATION( face );
+
+
         if ( num_coords )
           face->face_flags |= FT_FACE_FLAG_VARIATION;
         else
           face->face_flags &= ~FT_FACE_FLAG_VARIATION;
+
+        if ( service_mm->construct_ps_name )
+        {
+          if ( error == -1 )
+          {
+            /* The PS name of a named instance and a non-named instance */
+            /* usually differs, even if the axis values are identical.  */
+            if ( is_variation_old != FT_IS_VARIATION( face ) )
+              service_mm->construct_ps_name( face );
+          }
+          else
+            service_mm->construct_ps_name( face );
+        }
       }
 
       /* internal error code -1 means `no change'; we can exit immediately */
@@ -577,6 +625,33 @@
       error = FT_ERR( Invalid_Argument );
       if ( service_mm->set_named_instance )
         error = service_mm->set_named_instance( face, instance_index );
+
+      if ( !error || error == -1 )
+      {
+        FT_Bool  is_variation_old = FT_IS_VARIATION( face );
+
+
+        face->face_flags &= ~FT_FACE_FLAG_VARIATION;
+        face->face_index  = ( instance_index << 16 )        |
+                            ( face->face_index & 0xFFFFL );
+
+        if ( service_mm->construct_ps_name )
+        {
+          if ( error == -1 )
+          {
+            /* The PS name of a named instance and a non-named instance */
+            /* usually differs, even if the axis values are identical.  */
+            if ( is_variation_old != FT_IS_VARIATION( face ) )
+              service_mm->construct_ps_name( face );
+          }
+          else
+            service_mm->construct_ps_name( face );
+        }
+      }
+
+      /* internal error code -1 means `no change'; we can exit immediately */
+      if ( error == -1 )
+        return FT_Err_Ok;
     }
 
     if ( !error )
@@ -594,13 +669,6 @@
       face->autohint.data = NULL;
     }
 
-    if ( !error )
-    {
-      face->face_index  = ( instance_index << 16 )        |
-                          ( face->face_index & 0xFFFFL );
-      face->face_flags &= ~FT_FACE_FLAG_VARIATION;
-    }
-
     return error;
   }
 
diff --git a/src/cff/cffdrivr.c b/src/cff/cffdrivr.c
index 29f2a2b..6d96e4a 100644
--- a/src/cff/cffdrivr.c
+++ b/src/cff/cffdrivr.c
@@ -896,6 +896,16 @@
   }
 
 
+  static void
+  cff_construct_ps_name( CFF_Face  face )
+  {
+    FT_Service_MultiMasters  mm = (FT_Service_MultiMasters)face->mm;
+
+
+    mm->construct_ps_name( FT_FACE( face ) );
+  }
+
+
   static FT_Error
   cff_get_mm_var( CFF_Face     face,
                   FT_MM_Var*  *master )
@@ -1039,6 +1049,10 @@
     (FT_Get_MM_WeightVector_Func)
                             cff_get_mm_weightvector,
                                                 /* get_mm_weightvector        */
+
+    (FT_Construct_PS_Name_Func)
+                            cff_construct_ps_name,
+                                                /* construct_ps_name          */
     (FT_Var_Load_Delta_Set_Idx_Map_Func)
                             cff_load_delta_set_index_mapping,
                                                 /* load_delta_set_idx_map     */
diff --git a/src/truetype/ttdriver.c b/src/truetype/ttdriver.c
index 54ad932..8bde30f 100644
--- a/src/truetype/ttdriver.c
+++ b/src/truetype/ttdriver.c
@@ -533,6 +533,9 @@
                             NULL,                  /* set_mm_weightvector        */
     (FT_Get_MM_WeightVector_Func)
                             NULL,                  /* get_mm_weightvector        */
+
+    (FT_Construct_PS_Name_Func)
+                            tt_construct_ps_name,  /* construct_ps_name          */
     (FT_Var_Load_Delta_Set_Idx_Map_Func)
                             tt_var_load_delta_set_index_mapping,
                                                    /* load_delta_set_idx_map     */
diff --git a/src/truetype/ttgxvar.c b/src/truetype/ttgxvar.c
index 4448362..aec43c8 100644
--- a/src/truetype/ttgxvar.c
+++ b/src/truetype/ttgxvar.c
@@ -2936,9 +2936,6 @@
       }
     }
 
-    /* enforce recomputation of the PostScript name; */
-    FT_FREE( face->postscript_name );
-
   Exit:
     return error;
   }
@@ -2978,14 +2975,7 @@
                    FT_UInt    num_coords,
                    FT_Fixed*  coords )
   {
-    FT_Error  error;
-
-
-    error = tt_set_mm_blend( face, num_coords, coords, 1 );
-    if ( error )
-      return error;
-
-    return FT_Err_Ok;
+    return tt_set_mm_blend( face, num_coords, coords, 1 );
   }
 
 
@@ -3305,7 +3295,8 @@
    *     Value 0 indicates to not use an instance.
    *
    * @Return:
-   *   FreeType error code.  0~means success.
+   *   FreeType error code.  0~means success, -1 means success and unchanged
+   *   axis values.
    */
   FT_LOCAL_DEF( FT_Error )
   TT_Set_Named_Instance( TT_Face  face,
@@ -3361,20 +3352,10 @@
       error = TT_Set_Var_Design( face,
                                  mmvar->num_axis,
                                  named_style->coords );
-      if ( error )
-      {
-        /* internal error code -1 means `no change' */
-        if ( error == -1 )
-          error = FT_Err_Ok;
-        goto Exit;
-      }
     }
     else
       error = TT_Set_Var_Design( face, 0, NULL );
 
-    face->root.face_index = ( instance_index << 16 )             |
-                            ( face->root.face_index & 0xFFFFL );
-
   Exit:
     return error;
   }
@@ -3419,6 +3400,19 @@
   }
 
 
+  /* This function triggers (lazy) recomputation of the `postscript_name` */
+  /* field in `TT_Face`.                                                  */
+
+  FT_LOCAL_DEF( void )
+  tt_construct_ps_name( TT_Face  face )
+  {
+    FT_Memory  memory = face->root.memory;
+
+
+    FT_FREE( face->postscript_name );
+  }
+
+
   /*************************************************************************/
   /*************************************************************************/
   /*****                                                               *****/
diff --git a/src/truetype/ttgxvar.h b/src/truetype/ttgxvar.h
index 0c096f7..4de772c 100644
--- a/src/truetype/ttgxvar.h
+++ b/src/truetype/ttgxvar.h
@@ -378,6 +378,9 @@ FT_BEGIN_HEADER
   TT_Get_Default_Named_Instance( TT_Face   face,
                                  FT_UInt  *instance_index );
 
+  FT_LOCAL( void )
+  tt_construct_ps_name( TT_Face  face );
+
   FT_LOCAL( FT_Error )
   tt_face_vary_cvt( TT_Face    face,
                     FT_Stream  stream );
@@ -401,7 +404,6 @@ FT_BEGIN_HEADER
   FT_LOCAL( void )
   tt_apply_mvar( TT_Face  face );
 
-
   FT_LOCAL( FT_Error )
   tt_var_load_item_variation_store( TT_Face          face,
                                     FT_ULong         offset,
diff --git a/src/type1/t1driver.c b/src/type1/t1driver.c
index 4239d86..4abcef1 100644
--- a/src/type1/t1driver.c
+++ b/src/type1/t1driver.c
@@ -136,6 +136,9 @@
                             T1_Set_MM_WeightVector, /* set_mm_weightvector        */
     (FT_Get_MM_WeightVector_Func)
                             T1_Get_MM_WeightVector, /* get_mm_weightvector        */
+
+    (FT_Construct_PS_Name_Func)
+                            NULL,                   /* construct_ps_name          */
     (FT_Var_Load_Delta_Set_Idx_Map_Func)
                             NULL,                   /* load_delta_set_idx_map     */
     (FT_Var_Load_Item_Var_Store_Func)
diff --git a/src/type1/t1load.c b/src/type1/t1load.c
index a3b2810..ee52bf2 100644
--- a/src/type1/t1load.c
+++ b/src/type1/t1load.c
@@ -442,14 +442,7 @@
                    FT_UInt    num_coords,
                    FT_Fixed*  coords )
   {
-    FT_Error  error;
-
-
-    error = t1_set_mm_blend( face, num_coords, coords );
-    if ( error )
-      return error;
-
-    return FT_Err_Ok;
+    return t1_set_mm_blend( face, num_coords, coords );
   }