Commit 9b31c44620b30d3b2e6f6c0aa1cf54409cbd1087

Werner Lemberg 2018-06-14T21:30:43

Replace `FT_Get_GlyphLayers' with `FT_Get_Color_Glyph_Layer'. This avoids any additional allocation of COLR related structures in a glyph slot. * include/freetype/freetype.h (FT_Glyph_Layer, FT_Glyph_LayerRec, FT_Get_GlyphLayers): Removed. * include/freetype/internal/ftobjs.h (FT_Colr_InternalRec): Removed. (FT_Slot_InternalRec): Remove `color_layers'. * include/freetype/internal/sfnt.h (TT_Load_Colr_Layer_Func): Removed. (SFNT_Interface, FT_DEFINE_SFNT_INTERFACE): Remove `load_colr_layer'. * src/base/ftobjs.c (ft_glyph_slot_done): Updated. (FT_Render_Glyph_Internal): Use `FT_Get_Color_Glyph_Layer'. (FT_Get_GlyphLayers): Removed. * src/sfnt/sfdriver.c (sfnt_interface): Updated. * src/sfnt/ttcolr.c (tt_face_load_colr_layers): Removed. * src/sfnt/ttcolr.h: Updated. * src/truetype/ttgload.c (TT_Load_Glyph): Updated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
diff --git a/ChangeLog b/ChangeLog
index 5df7fa5..d1559ac 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,34 @@
 2018-06-14  Werner Lemberg  <wl@gnu.org>
 
+	Replace `FT_Get_GlyphLayers' with `FT_Get_Color_Glyph_Layer'.
+
+	This avoids any additional allocation of COLR related structures in
+	a glyph slot.
+
+	* include/freetype/freetype.h (FT_Glyph_Layer, FT_Glyph_LayerRec,
+	FT_Get_GlyphLayers): Removed.
+
+	* include/freetype/internal/ftobjs.h (FT_Colr_InternalRec): Removed.
+	(FT_Slot_InternalRec): Remove `color_layers'.
+
+	* include/freetype/internal/sfnt.h (TT_Load_Colr_Layer_Func):
+	Removed.
+	(SFNT_Interface, FT_DEFINE_SFNT_INTERFACE): Remove
+	`load_colr_layer'.
+
+	* src/base/ftobjs.c (ft_glyph_slot_done): Updated.
+	(FT_Render_Glyph_Internal): Use `FT_Get_Color_Glyph_Layer'.
+	(FT_Get_GlyphLayers): Removed.
+
+	* src/sfnt/sfdriver.c (sfnt_interface): Updated.
+
+	* src/sfnt/ttcolr.c (tt_face_load_colr_layers): Removed.
+	* src/sfnt/ttcolr.h: Updated.
+
+	* src/truetype/ttgload.c (TT_Load_Glyph): Updated.
+
+2018-06-14  Werner Lemberg  <wl@gnu.org>
+
 	Provide iterative API to access `COLR' data.
 
 	This solution doesn't store any data in an `FT_GlyphSlot' object.
diff --git a/include/freetype/freetype.h b/include/freetype/freetype.h
index 02abd8a..546e5d8 100644
--- a/include/freetype/freetype.h
+++ b/include/freetype/freetype.h
@@ -4171,101 +4171,6 @@ FT_BEGIN_HEADER
 
   /**********************************************************************
    *
-   * @type:
-   *    FT_Glyph_Layer
-   *
-   * @description:
-   *    A handle to an @FT_Glyph_LayerRec structure to model a given
-   *    colored glyph layer.
-   */
-  typedef struct FT_Glyph_LayerRec_*  FT_Glyph_Layer;
-
-
-  /**********************************************************************
-   *
-   * @struct:
-   *   FT_Glyph_LayerRec
-   *
-   * @description:
-   *   This structure models a given colored glyph layer as defined in the
-   *   OpenType `COLR' table.  It is used by @FT_Get_GlyphLayers.
-   *
-   * @fields:
-   *   glyph_index ::
-   *     The glyph index of the current glyph layer.
-   *
-   *   color_index ::
-   *     The color index into the font face's color palette, which can be
-   *     retrieved with @FT_Palette_Select.  The value 0xFFFF is special; it
-   *     doesn't reference a palette entry but indicates that the text
-   *     foreground color should be used instead (to be set up by the
-   *     application outside of FreeType).
-   */
-  typedef struct  FT_Glyph_LayerRec_
-  {
-    FT_UShort  glyph_index;
-    FT_UShort  color_index;
-
-  } FT_Glyph_LayerRec;
-
-
-  /*************************************************************************
-   *
-   * @func:
-   *   FT_Get_GlyphLayers
-   *
-   * @description:
-   *   This is an interface to the `COLR' table in OpenType fonts to
-   *   retrieve the colored glyph layers array associated with the current
-   *   glyph slot.
-   *
-   *     https://docs.microsoft.com/en-us/typography/opentype/spec/colr
-   *
-   *   The glyph layer data for a given glyph slot, if present, provides an
-   *   alternative, multi-colour glyph representation: Instead of rendering
-   *   the outline or bitmap in the glyph slot, glyphs with the indices and
-   *   colors returned in the @FT_Glyph_Layer array are rendered layer by
-   *   layer.
-   *
-   * @input:
-   *   glyph ::
-   *     The source glyph slot.
-   *
-   * @output:
-   *   anum_layers ::
-   *     The number of colored glyph layers for `glyph'.
-   *
-   *   alayers ::
-   *     An @FT_Glyph_Layer array with `anum_layers' elements.  NULL if there
-   *     aren't glyph layers.
-   *
-   *     The elements are ordered in the z~direction from bottom to top; an
-   *     element `n' should be rendered with the associated palette color
-   *     and blended on top of the already rendered layers (elements 0, 1,
-   *     ..., n-1).
-   *
-   * @return:
-   *   FreeType error code.  0~means success.
-   *
-   * @note:
-   *   The data in `alayers' is owned and managed by the glyph slot.
-   *
-   *   This function is necessary if you want to handle glyph layers by
-   *   yourself.  In particular, functions that operate with @FT_GlyphRec
-   *   objects (like @FT_Get_Glyph or @FT_Glyph_To_Bitmap) don't have access
-   *   to this information.
-   *
-   *   @FT_Render_Glyph, however, handles colored glyph layers
-   *   automatically if the @FT_LOAD_COLOR flag is passed to it.
-   */
-  FT_EXPORT( FT_Error )
-  FT_Get_GlyphLayers( FT_GlyphSlot     glyph,
-                      FT_UShort       *anum_layers,
-                      FT_Glyph_Layer  *alayers );
-
-
-  /**********************************************************************
-   *
    * @struct:
    *   FT_LayerIterator
    *
diff --git a/include/freetype/internal/ftobjs.h b/include/freetype/internal/ftobjs.h
index 9e92a31..54f973a 100644
--- a/include/freetype/internal/ftobjs.h
+++ b/include/freetype/internal/ftobjs.h
@@ -384,15 +384,6 @@ FT_BEGIN_HEADER
   } FT_Face_InternalRec;
 
 
-  typedef struct  FT_Colr_InternalRec_
-  {
-    FT_Glyph_Layer  layers;
-    FT_UShort       num_layers;
-    FT_Int32        load_flags;
-
-  } FT_Colr_InternalRec, *FT_Colr_Internal;
-
-
   /**************************************************************************
    *
    * @Struct:
@@ -432,9 +423,6 @@ FT_BEGIN_HEADER
    *   glyph_hints ::
    *     Format-specific glyph hints management.
    *
-   *   color_layers ::
-   *     Data from (SFNT) COLR/CPAL tables.
-   *
    *   load_flags ::
    *     The load flags passed as an argument to @FT_Load_Glyph while
    *     initializing the glyph slot.
@@ -451,8 +439,6 @@ FT_BEGIN_HEADER
     FT_Vector       glyph_delta;
     void*           glyph_hints;
 
-    FT_Colr_Internal  color_layers;
-
     FT_Int32        load_flags;
 
   } FT_GlyphSlot_InternalRec;
diff --git a/include/freetype/internal/sfnt.h b/include/freetype/internal/sfnt.h
index 2093c28..bcc2665 100644
--- a/include/freetype/internal/sfnt.h
+++ b/include/freetype/internal/sfnt.h
@@ -495,40 +495,6 @@ FT_BEGIN_HEADER
   /**************************************************************************
    *
    * @FuncType:
-   *   TT_Load_Colr_Layer_Func
-   *
-   * @Description:
-   *   Load the color layer data given a glyph index.
-   *
-   * @Input:
-   *   face ::
-   *     The target face object.
-   *
-   *   idx ::
-   *     The glyph index.
-   *
-   * @Output:
-   *   layers ::
-   *     The layer info with color index and glyph index.
-   *     Deallocate with `FT_FREE'.
-   *
-   *   num_layers ::
-   *     Number of layers.
-   *
-   * @Return:
-   *   FreeType error code.  0 means success.  Returns an error if no
-   *   color layer information exists for `idx'.
-   */
-  typedef FT_Error
-  (*TT_Load_Colr_Layer_Func)( TT_Face          face,
-                              FT_UInt          idx,
-                              FT_Glyph_Layer  *layers,
-                              FT_UShort*       num_layers );
-
-
-  /**************************************************************************
-   *
-   * @FuncType:
    *   TT_Get_Colr_Layer_Func
    *
    * @Description:
@@ -808,7 +774,6 @@ FT_BEGIN_HEADER
     TT_Free_Table_Func           free_cpal;
     TT_Free_Table_Func           free_colr;
     TT_Set_Palette_Func          set_palette;
-    TT_Load_Colr_Layer_Func      load_colr_layer;
     TT_Get_Colr_Layer_Func       get_colr_layer;
     TT_Blend_Colr_Func           colr_blend;
 
@@ -859,7 +824,6 @@ FT_BEGIN_HEADER
           free_cpal_,                    \
           free_colr_,                    \
           set_palette_,                  \
-          load_colr_layer_,              \
           get_colr_layer_,               \
           colr_blend_,                   \
           get_metrics_,                  \
@@ -900,7 +864,6 @@ FT_BEGIN_HEADER
     free_cpal_,                          \
     free_colr_,                          \
     set_palette_,                        \
-    load_colr_layer_,                    \
     get_colr_layer_,                     \
     colr_blend_,                         \
     get_metrics_,                        \
diff --git a/src/base/ftobjs.c b/src/base/ftobjs.c
index 29a1c11..e733f95 100644
--- a/src/base/ftobjs.c
+++ b/src/base/ftobjs.c
@@ -532,16 +532,6 @@
     /* free bitmap buffer if needed */
     ft_glyphslot_free_bitmap( slot );
 
-    /* free glyph color layers if needed */
-    if ( slot->internal->color_layers )
-    {
-      FT_Colr_InternalRec*  color_layers = slot->internal->color_layers;
-
-
-      FT_FREE( color_layers->layers );
-      FT_FREE( slot->internal->color_layers );
-    }
-
     /* slot->internal might be NULL in out-of-memory situations */
     if ( slot->internal )
     {
@@ -4513,69 +4503,85 @@
                             FT_Render_Mode  render_mode )
   {
     FT_Error     error = FT_Err_Ok;
+    FT_Face      face  = slot->face;
     FT_Renderer  renderer;
 
 
-    /* if it is already a bitmap, no need to do anything */
     switch ( slot->format )
     {
     case FT_GLYPH_FORMAT_BITMAP:   /* already a bitmap, don't do anything */
       break;
 
     default:
-      if ( slot->internal->color_layers )
+      if ( slot->internal->load_flags & FT_LOAD_COLOR )
       {
-        FT_Face  face = slot->face;
+        FT_LayerIterator  iterator;
 
+        FT_UInt  base_glyph = slot->glyph_index;
 
-        error = FT_New_GlyphSlot( face, NULL );
-        if ( !error )
+        FT_UInt  glyph_index;
+        FT_UInt  color_index;
+
+
+        /* check whether we have colored glyph layers */
+        iterator.p  = NULL;
+        glyph_index = FT_Get_Color_Glyph_Layer( face,
+                                                base_glyph,
+                                                &color_index,
+                                                &iterator );
+        if ( glyph_index )
         {
-          TT_Face       ttface = (TT_Face)face;
-          SFNT_Service  sfnt   = (SFNT_Service)ttface->sfnt;
+          error = FT_New_GlyphSlot( face, NULL );
+          if ( !error )
+          {
+            TT_Face       ttface = (TT_Face)face;
+            SFNT_Service  sfnt   = (SFNT_Service)ttface->sfnt;
 
-          FT_Glyph_Layer  glyph_layers =
-                            slot->internal->color_layers->layers;
 
-          FT_Int  idx;
+            do
+            {
+              FT_Int32  load_flags = slot->internal->load_flags;
 
 
-          for ( idx = 0;
-                idx < slot->internal->color_layers->num_layers;
-                idx++ )
-          {
-            FT_Int32  load_flags;
+              /* disable the `FT_LOAD_COLOR' flag to avoid recursion */
+              /* right here in this function                         */
+              load_flags &= ~FT_LOAD_COLOR;
 
+              /* render into the new `face->glyph' glyph slot */
+              load_flags |=  FT_LOAD_RENDER;
 
-            load_flags  = slot->internal->color_layers->load_flags
-                          & ~FT_LOAD_COLOR;
-            load_flags |= FT_LOAD_RENDER;
+              error = FT_Load_Glyph( face, glyph_index, load_flags );
+              if ( error )
+                break;
 
-            error = FT_Load_Glyph( face,
-                                   glyph_layers[idx].glyph_index,
-                                   load_flags );
-            if ( error )
-              break;
+              /* blend new `face->glyph' into old `slot'; */
+              /* at the first call, `slot' is still empty */
+              error = sfnt->colr_blend( ttface,
+                                        color_index,
+                                        slot,
+                                        face->glyph );
+              if ( error )
+                break;
 
-            error = sfnt->colr_blend( ttface,
-                                      glyph_layers[idx].color_index,
-                                      slot,
-                                      face->glyph );
-            if ( error )
-              break;
+            } while ( ( glyph_index =
+                          FT_Get_Color_Glyph_Layer( face,
+                                                    base_glyph,
+                                                    &color_index,
+                                                    &iterator ) ) != 0 );
+
+            if ( !error )
+              slot->format = FT_GLYPH_FORMAT_BITMAP;
+
+            /* this call also restores `slot' as the glyph slot */
+            FT_Done_GlyphSlot( face->glyph );
           }
 
           if ( !error )
-            slot->format = FT_GLYPH_FORMAT_BITMAP;
+            return error;
 
-          FT_Done_GlyphSlot( face->glyph );
+          /* Failed to do the colored layer.  Draw outline instead. */
+          slot->format = FT_GLYPH_FORMAT_OUTLINE;
         }
-
-        if ( !error )
-          return error;
-
-        /* Failed to do the colored layer.  Draw outline instead. */
-        slot->format = FT_GLYPH_FORMAT_OUTLINE;
       }
 
       {
@@ -5467,31 +5473,6 @@
 
   /* documentation is in freetype.h */
 
-  FT_EXPORT_DEF( FT_Error )
-  FT_Get_GlyphLayers( FT_GlyphSlot     glyph,
-                      FT_UShort       *anum_layers,
-                      FT_Glyph_Layer  *alayers )
-  {
-    if ( !glyph )
-      return FT_THROW( Invalid_Argument );
-
-    if ( glyph->internal->color_layers )
-    {
-      *anum_layers = glyph->internal->color_layers->num_layers;
-      *alayers     = glyph->internal->color_layers->layers;
-    }
-    else
-    {
-      *anum_layers = 0;
-      *alayers     = NULL;
-    }
-
-    return FT_Err_Ok;
-  }
-
-
-  /* documentation is in freetype.h */
-
   FT_EXPORT_DEF( FT_UInt )
   FT_Get_Color_Glyph_Layer( FT_Face            face,
                             FT_UInt            base_glyph,
diff --git a/src/sfnt/sfdriver.c b/src/sfnt/sfdriver.c
index e1f4818..cd2d809 100644
--- a/src/sfnt/sfdriver.c
+++ b/src/sfnt/sfdriver.c
@@ -1268,8 +1268,6 @@
                             /* TT_Free_Table_Func      free_colr       */
     PUT_COLOR_LAYERS( tt_face_palette_set ),
                             /* TT_Set_Palette_Func     set_palette     */
-    PUT_COLOR_LAYERS( tt_face_load_colr_layers ),
-                            /* TT_Load_Colr_Layer_Func load_colr_layer */
     PUT_COLOR_LAYERS( tt_face_get_colr_layer ),
                             /* TT_Get_Colr_Layer_Func  get_colr_layer  */
     PUT_COLOR_LAYERS( tt_face_colr_blend_layer ),
diff --git a/src/sfnt/ttcolr.c b/src/sfnt/ttcolr.c
index 323f0ca..7e44d42 100644
--- a/src/sfnt/ttcolr.c
+++ b/src/sfnt/ttcolr.c
@@ -207,74 +207,6 @@
   }
 
 
-  FT_LOCAL_DEF( FT_Error )
-  tt_face_load_colr_layers( TT_Face          face,
-                            FT_UInt          glyph_id,
-                            FT_Glyph_Layer  *ret_layers,
-                            FT_UShort*       ret_num_layers )
-  {
-    FT_Error   error;
-    FT_Memory  memory = face->root.memory;
-
-    Colr*  colr = (Colr*)face->colr;
-
-    BaseGlyphRecord  glyph_record;
-    FT_Glyph_Layer   layers = NULL;
-    int              layer_idx;
-    FT_Byte*         layer_record_ptr;
-
-
-    if ( !ret_layers || !ret_num_layers )
-      return FT_THROW( Invalid_Argument );
-
-    if ( !find_base_glyph_record( colr->base_glyphs,
-                                  colr->num_base_glyphs,
-                                  glyph_id,
-                                  &glyph_record ) )
-    {
-      *ret_layers     = NULL;
-      *ret_num_layers = 0;
-
-      return FT_Err_Ok;
-    }
-
-    /* Load all colors for the glyphs; this would be stored in the slot. */
-    layer_record_ptr = colr->layers +
-                       glyph_record.first_layer_index * LAYER_SIZE;
-
-    if ( FT_NEW_ARRAY( layers, glyph_record.num_layers ) )
-      goto Error;
-
-    for ( layer_idx = 0; layer_idx < glyph_record.num_layers; layer_idx++ )
-    {
-      FT_UShort  gid                 = FT_NEXT_USHORT( layer_record_ptr );
-      FT_UShort  palette_entry_index = FT_NEXT_USHORT( layer_record_ptr );
-
-
-      if ( palette_entry_index != 0xFFFF                                 &&
-           palette_entry_index >= face->palette_data.num_palette_entries )
-      {
-        error = FT_THROW( Invalid_Table );
-        goto Error;
-      }
-
-      layers[layer_idx].color_index = palette_entry_index;
-      layers[layer_idx].glyph_index = gid;
-    }
-
-    *ret_layers     = layers;
-    *ret_num_layers = glyph_record.num_layers;
-
-    return FT_Err_Ok;
-
-  Error:
-    if ( layers )
-      FT_FREE( layers );
-
-    return error;
-  }
-
-
   FT_LOCAL_DEF( FT_UInt )
   tt_face_get_colr_layer( TT_Face            face,
                           FT_UInt            base_glyph,
diff --git a/src/sfnt/ttcolr.h b/src/sfnt/ttcolr.h
index 480fd67..983ab83 100644
--- a/src/sfnt/ttcolr.h
+++ b/src/sfnt/ttcolr.h
@@ -36,12 +36,6 @@ FT_BEGIN_HEADER
   FT_LOCAL( void )
   tt_face_free_colr( TT_Face  face );
 
-  FT_LOCAL( FT_Error )
-  tt_face_load_colr_layers( TT_Face          face,
-                            FT_UInt          glyph_id,
-                            FT_Glyph_Layer  *ret_layers,
-                            FT_UShort*       ret_num_layers );
-
   FT_LOCAL( FT_UInt )
   tt_face_get_colr_layer( TT_Face            face,
                           FT_UInt            base_glyph,
diff --git a/src/truetype/ttgload.c b/src/truetype/ttgload.c
index c5e96e6..f0f6a06 100644
--- a/src/truetype/ttgload.c
+++ b/src/truetype/ttgload.c
@@ -2904,39 +2904,6 @@
          size->metrics->y_ppem < 24         )
       glyph->outline.flags |= FT_OUTLINE_HIGH_PRECISION;
 
-    /* The outline based algorithm took care of metrics. */
-    /* Read additional color info if requested.          */
-    if ( ( load_flags & FT_LOAD_COLOR ) &&
-         ( (TT_Face)glyph->face )->colr )
-    {
-      TT_Face       face   = (TT_Face)glyph->face;
-      FT_Memory     memory = face->root.memory;
-      SFNT_Service  sfnt   = (SFNT_Service)face->sfnt;
-
-      FT_Glyph_Layer  glyph_layers;
-      FT_UShort       num_glyph_layers;
-
-
-      error = sfnt->load_colr_layer( face,
-                                     glyph_index,
-                                     &glyph_layers,
-                                     &num_glyph_layers );
-      if ( error )
-        return error;
-
-      if ( !glyph->internal->color_layers )
-      {
-        if ( FT_NEW( glyph->internal->color_layers ) )
-          return error;
-      }
-
-      FT_FREE( glyph->internal->color_layers->layers );
-
-      glyph->internal->color_layers->layers     = glyph_layers;
-      glyph->internal->color_layers->num_layers = num_glyph_layers;
-      glyph->internal->color_layers->load_flags = load_flags;
-    }
-
   Exit:
 #ifdef FT_DEBUG_LEVEL_TRACE
     if ( error )