Provide iterative API to access `COLR' data. This solution doesn't store any data in an `FT_GlyphSlot' object. * include/freetype/freetype.h (FT_LayerIterator): New structure. (FT_Get_Color_Glyph_Layer): New function. * include/freetype/internal/sfnt.h (TT_Get_Colr_Layer_Func): New function type. (SFNT_Interface, FT_DEFINE_SFNT_INTERFACE): Add it. * src/base/ftobjs.c (FT_Get_Color_Glyph_Layer): Implement it. * src/sfnt/ttcolr.c (tt_face_get_colr_layer): New function. * src/sfnt/ttcolr.h: Updated. * src/sfnt/sfdriver.c (sfnt_interface): 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
diff --git a/ChangeLog b/ChangeLog
index b1bec04..5df7fa5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,25 @@
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.
+
+ * include/freetype/freetype.h (FT_LayerIterator): New structure.
+ (FT_Get_Color_Glyph_Layer): New function.
+
+ * include/freetype/internal/sfnt.h (TT_Get_Colr_Layer_Func): New
+ function type.
+ (SFNT_Interface, FT_DEFINE_SFNT_INTERFACE): Add it.
+
+ * src/base/ftobjs.c (FT_Get_Color_Glyph_Layer): Implement it.
+
+ * src/sfnt/ttcolr.c (tt_face_get_colr_layer): New function.
+ * src/sfnt/ttcolr.h: Updated.
+
+ * src/sfnt/sfdriver.c (sfnt_interface): Updated.
+
+2018-06-14 Werner Lemberg <wl@gnu.org>
+
Add glyph index and glyph load flags to glyph slot.
* include/freetype/freetype.h (FT_GlyphSlotRec): Rename unused
diff --git a/include/freetype/freetype.h b/include/freetype/freetype.h
index 35e6ed6..02abd8a 100644
--- a/include/freetype/freetype.h
+++ b/include/freetype/freetype.h
@@ -3101,7 +3101,7 @@ FT_BEGIN_HEADER
* blending of the color glyph layers associated with the glyph index,
* using the same bitmap format as embedded color bitmap images. This
* is mainly for convenience; for full control of color layers use
- * @FT_Get_GlyphLayers and FreeType's color functions like
+ * @FT_Get_Color_Glyph_Layer and FreeType's color functions like
* @FT_Palette_Select instead of setting FT_LOAD_COLOR for rendering
* so that the client application can handle blending by itself.
*
@@ -4264,6 +4264,101 @@ FT_BEGIN_HEADER
FT_Glyph_Layer *alayers );
+ /**********************************************************************
+ *
+ * @struct:
+ * FT_LayerIterator
+ *
+ * @description:
+ * This iterator object is needed for @FT_Get_Color_Glyph_Layer.
+ *
+ * @fields:
+ * num_layers ::
+ * The number of glyph layers for the requested glyph index. Will be
+ * set by @FT_Get_Color_Glyph_Layer.
+ *
+ * layer ::
+ * The current layer. Will be set by @FT_Get_Color_Glyph_Layer.
+ *
+ * p ::
+ * An opaque pointer into `COLR' table data. The caller must set this
+ * to NULL before the first call of @FT_Get_Color_Glyph_Layer.
+ */
+ typedef struct FT_LayerIterator_
+ {
+ FT_UInt num_layers;
+ FT_UInt layer;
+ FT_Byte* p;
+
+ } FT_LayerIterator;
+
+
+ /*************************************************************************
+ *
+ * @func:
+ * FT_Get_Color_Glyph_Layer
+ *
+ * @description:
+ * This is an interface to the `COLR' table in OpenType fonts to
+ * iteratively retrieve the colored glyph layers associated with the
+ * current glyph slot.
+ *
+ * https://docs.microsoft.com/en-us/typography/opentype/spec/colr
+ *
+ * The glyph layer data for a given glyph index, if present, provides an
+ * alternative, multi-colour glyph representation: Instead of rendering
+ * the outline or bitmap with the given glyph index, glyphs with the
+ * indices and colors returned by this function are rendered layer by
+ * layer.
+ *
+ * The returned elements are ordered in the z~direction from bottom to
+ * top; the `n'th element should be rendered with the associated palette
+ * color and blended on top of the already rendered layers (elements 0,
+ * 1, ..., n-1).
+ *
+ * @input:
+ * face ::
+ * A handle to the parent face object.
+ *
+ * base_glyph ::
+ * The glyph index the colored glyph layers are associated with.
+ *
+ * @inout:
+ * iterator ::
+ * An @FT_LayerIterator object. For the first call you should set
+ * `iterator->p' to NULL. For all following calls, simply use the
+ * same object again.
+ *
+ * @output:
+ * acolor_index ::
+ * The color index into the font face's color palette of the current
+ * layer. 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).
+ *
+ * The color palette can be retrieved with @FT_Palette_Select.
+ *
+ * @return:
+ * The glyph index of the current layer. If there are no more layers
+ * (or if there are no layers at all), value~0 gets returned. In case
+ * of an error, value~0 is returned also.
+ *
+ * @note:
+ * 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_UInt )
+ FT_Get_Color_Glyph_Layer( FT_Face face,
+ FT_UInt base_glyph,
+ FT_UInt *acolor_index,
+ FT_LayerIterator* iterator );
+
+
/**************************************************************************
*
* @Enum:
diff --git a/include/freetype/internal/sfnt.h b/include/freetype/internal/sfnt.h
index 731c7fe..2093c28 100644
--- a/include/freetype/internal/sfnt.h
+++ b/include/freetype/internal/sfnt.h
@@ -529,6 +529,46 @@ FT_BEGIN_HEADER
/**************************************************************************
*
* @FuncType:
+ * TT_Get_Colr_Layer_Func
+ *
+ * @Description:
+ * Iteratively get the color layer data of a given glyph index.
+ *
+ * @Input:
+ * face ::
+ * The target face object.
+ *
+ * base_glyph ::
+ * The glyph index the colored glyph layers are associated with.
+ *
+ * @inout:
+ * iterator ::
+ * An @FT_LayerIterator object. For the first call you should set
+ * `iterator->p' to NULL. For all following calls, simply use the
+ * same object again.
+ *
+ * @output:
+ * acolor_index ::
+ * The color index into the font face's color palette of the current
+ * layer. 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).
+ *
+ * @return:
+ * The glyph index of the current layer. If there are no more layers
+ * (or if there are no layers at all), value~0 gets returned. In case
+ * of an error, value~0 is returned also.
+ */
+ typedef FT_UInt
+ (*TT_Get_Colr_Layer_Func)( TT_Face face,
+ FT_UInt base_glyph,
+ FT_UInt *acolor_index,
+ FT_LayerIterator* iterator );
+
+
+ /**************************************************************************
+ *
+ * @FuncType:
* TT_Blend_Colr_Func
*
* @Description:
@@ -769,6 +809,7 @@ FT_BEGIN_HEADER
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;
TT_Get_Metrics_Func get_metrics;
@@ -819,6 +860,7 @@ FT_BEGIN_HEADER
free_colr_, \
set_palette_, \
load_colr_layer_, \
+ get_colr_layer_, \
colr_blend_, \
get_metrics_, \
get_name_, \
@@ -859,6 +901,7 @@ FT_BEGIN_HEADER
free_colr_, \
set_palette_, \
load_colr_layer_, \
+ get_colr_layer_, \
colr_blend_, \
get_metrics_, \
get_name_, \
diff --git a/src/base/ftobjs.c b/src/base/ftobjs.c
index bd3c039..29a1c11 100644
--- a/src/base/ftobjs.c
+++ b/src/base/ftobjs.c
@@ -5490,4 +5490,35 @@
}
+ /* documentation is in freetype.h */
+
+ FT_EXPORT_DEF( FT_UInt )
+ FT_Get_Color_Glyph_Layer( FT_Face face,
+ FT_UInt base_glyph,
+ FT_UInt *acolor_index,
+ FT_LayerIterator* iterator )
+ {
+ TT_Face ttface;
+ SFNT_Service sfnt;
+
+
+ if ( !face ||
+ !acolor_index ||
+ !iterator ||
+ base_glyph >= (FT_UInt)face->num_glyphs )
+ return 0;
+
+ if ( !FT_IS_SFNT( face ) )
+ return 0;
+
+ ttface = (TT_Face)face;
+ sfnt = (SFNT_Service)ttface->sfnt;
+
+ return sfnt->get_colr_layer( ttface,
+ base_glyph,
+ acolor_index,
+ iterator );
+ }
+
+
/* END */
diff --git a/src/sfnt/sfdriver.c b/src/sfnt/sfdriver.c
index 0c3ddc8..e1f4818 100644
--- a/src/sfnt/sfdriver.c
+++ b/src/sfnt/sfdriver.c
@@ -1270,6 +1270,8 @@
/* 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 ),
/* TT_Blend_Colr_Func colr_blend */
diff --git a/src/sfnt/ttcolr.c b/src/sfnt/ttcolr.c
index 66592b6..323f0ca 100644
--- a/src/sfnt/ttcolr.c
+++ b/src/sfnt/ttcolr.c
@@ -275,6 +275,54 @@
}
+ FT_LOCAL_DEF( FT_UInt )
+ tt_face_get_colr_layer( TT_Face face,
+ FT_UInt base_glyph,
+ FT_UInt *acolor_index,
+ FT_LayerIterator* iterator )
+ {
+ Colr* colr = (Colr*)face->colr;
+ BaseGlyphRecord glyph_record;
+ FT_UInt glyph_index;
+
+
+ if ( !iterator->p )
+ {
+ /* first call to function */
+ iterator->layer = 0;
+
+ if ( !find_base_glyph_record( colr->base_glyphs,
+ colr->num_base_glyphs,
+ base_glyph,
+ &glyph_record ) )
+ return 0;
+
+ iterator->p = colr->layers +
+ LAYER_SIZE * glyph_record.first_layer_index;
+
+ if ( glyph_record.num_layers )
+ iterator->num_layers = glyph_record.num_layers;
+ else
+ return 0;
+ }
+
+ if ( iterator->layer >= iterator->num_layers )
+ return 0;
+
+ glyph_index = FT_NEXT_USHORT( iterator->p );
+ *acolor_index = FT_NEXT_USHORT( iterator->p );
+
+ if ( glyph_index >= FT_FACE( face )->num_glyphs ||
+ ( *acolor_index != 0xFFFF &&
+ *acolor_index >= face->palette_data.num_palette_entries ) )
+ return 0;
+
+ iterator->layer++;
+
+ return glyph_index;
+ }
+
+
FT_LOCAL_DEF( FT_Error )
tt_face_colr_blend_layer( TT_Face face,
FT_UInt color_index,
diff --git a/src/sfnt/ttcolr.h b/src/sfnt/ttcolr.h
index a548755..480fd67 100644
--- a/src/sfnt/ttcolr.h
+++ b/src/sfnt/ttcolr.h
@@ -42,6 +42,12 @@ FT_BEGIN_HEADER
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,
+ FT_UInt *acolor_index,
+ FT_LayerIterator* iterator );
+
FT_LOCAL( FT_Error )
tt_face_colr_blend_layer( TT_Face face,
FT_UInt color_index,