[sfnt] Read `CPAL' version 1 tables. * include/freetype/internal.tttypes.h: Include FT_COLOR_H. (TT_FaceRec): Add `palette' field. * src/sfnt/ttcolr.c: Include FT_COLOR_H. (Cpal): Remove all data covered by the new `palette' field in `TT_FaceRec'. (tt_face_load_colr): Updated. Read `CPAL' version 1 data. (tt_face_load_colr_layers, tt_face_find_color): Updated. * src/sfnt/sfobjs.c (sfnt_done_face): Free glyph color palette data.
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
diff --git a/ChangeLog b/ChangeLog
index 93c1d51..c7ccdfc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2018-06-09 Werner Lemberg <wl@gnu.org>
+
+ [sfnt] Read `CPAL' version 1 tables.
+
+ * include/freetype/internal.tttypes.h: Include FT_COLOR_H.
+ (TT_FaceRec): Add `palette' field.
+
+ * src/sfnt/ttcolr.c: Include FT_COLOR_H.
+ (Cpal): Remove all data covered by the new `palette' field in
+ `TT_FaceRec'.
+ (tt_face_load_colr): Updated.
+ Read `CPAL' version 1 data.
+ (tt_face_load_colr_layers, tt_face_find_color): Updated.
+
+ * src/sfnt/sfobjs.c (sfnt_done_face): Free glyph color palette data.
+
2018-06-07 Alexei Podtelezhnikov <apodtele@gmail.com>
[base] API for Harmony LCD rendering.
diff --git a/include/freetype/internal/tttypes.h b/include/freetype/internal/tttypes.h
index c02d76d..2d5ee0a 100644
--- a/include/freetype/internal/tttypes.h
+++ b/include/freetype/internal/tttypes.h
@@ -24,6 +24,7 @@
#include <ft2build.h>
#include FT_TRUETYPE_TABLES_H
#include FT_INTERNAL_OBJECTS_H
+#include FT_COLOR_H
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
#include FT_MULTIPLE_MASTERS_H
@@ -1657,6 +1658,9 @@ FT_BEGIN_HEADER
/* postscript names table */
TT_Post_NamesRec postscript_names;
+ /* glyph colors */
+ FT_Palette palette; /* since 2.10 */
+
/************************************************************************
*
@@ -1728,7 +1732,6 @@ FT_BEGIN_HEADER
FT_UInt sbit_num_strikes;
FT_UInt* sbit_strike_map;
- void* colr_and_cpal;
FT_Byte* kern_table;
FT_ULong kern_table_size;
FT_UInt num_kern_tables;
@@ -1756,6 +1759,9 @@ FT_BEGIN_HEADER
FT_ULong ebdt_size;
#endif
+ /* since 2.10 */
+ void* colr_and_cpal;
+
} TT_FaceRec;
diff --git a/src/sfnt/sfobjs.c b/src/sfnt/sfobjs.c
index 0c0e020..87749ca 100644
--- a/src/sfnt/sfobjs.c
+++ b/src/sfnt/sfobjs.c
@@ -1808,6 +1808,11 @@
FT_FREE( face->var_postscript_prefix );
#endif
+ /* freeing glyph color palette data */
+ FT_FREE( face->palette.palette_name_ids );
+ FT_FREE( face->palette.palette_types );
+ FT_FREE( face->palette.palette_entry_name_ids );
+
face->sfnt = NULL;
}
diff --git a/src/sfnt/ttcolr.c b/src/sfnt/ttcolr.c
index bfe8a39..ecadcdd 100644
--- a/src/sfnt/ttcolr.c
+++ b/src/sfnt/ttcolr.c
@@ -32,6 +32,7 @@
#include FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_STREAM_H
#include FT_TRUETYPE_TAGS_H
+#include FT_COLOR_H
#ifdef TT_CONFIG_OPTION_COLOR_LAYERS
@@ -68,22 +69,16 @@
} Colr;
+ /* all data from `CPAL' not covered in FT_Palette */
typedef struct Cpal_
{
FT_UShort version; /* Table version number (0 or 1 supported). */
- FT_UShort num_palettes_entries; /* # of entries in each palette. */
- FT_UShort num_palettes; /* # of palettes in the table. */
FT_UShort num_colors; /* Total number of color records, */
/* combined for all palettes. */
FT_Byte* colors; /* RGBA array of colors */
FT_Byte* color_indices; /* Index of each palette's first color record */
/* in the combined color record array. */
- /* version 1 fields */
- FT_ULong* palette_types;
- FT_UShort* palette_labels;
- FT_UShort* palette_entry_labels;
-
} Cpal;
@@ -126,11 +121,13 @@
ColrCpal* cc = NULL;
FT_ULong base_glyph_begin, base_glyph_end, layer_begin, layer_end;
- FT_ULong colors_begin, colors_end;
+ FT_ULong colors_offset;
FT_ULong table_size;
- face->colr_and_cpal = NULL;
+ /*
+ * COLR
+ */
error = face->goto_table( face, TTAG_COLR, stream, &table_size );
if ( error )
@@ -173,6 +170,10 @@
if ( colr.base_glyphs < colr_table || colr.layers < colr_table )
goto InvalidTable;
+ /*
+ * CPAL
+ */
+
error = face->goto_table( face, TTAG_CPAL, stream, &table_size );
if ( error )
goto NoColor;
@@ -186,28 +187,99 @@
p = cpal_table;
FT_ZERO( &cpal );
- cpal.version = FT_NEXT_USHORT( p );
- cpal.num_palettes_entries = FT_NEXT_USHORT( p );
- cpal.num_palettes = FT_NEXT_USHORT( p );
- cpal.num_colors = FT_NEXT_USHORT( p );
+ cpal.version = FT_NEXT_USHORT( p );
+ if ( cpal.version > 1 )
+ goto InvalidTable;
- colors_begin = FT_NEXT_ULONG( p );
+ face->palette.num_palette_entries = FT_NEXT_USHORT( p );
+ face->palette.num_palettes = FT_NEXT_USHORT( p );
- cpal.color_indices = p;
- cpal.colors = (FT_Byte*)cpal_table + colors_begin;
+ cpal.num_colors = FT_NEXT_USHORT( p );
+ colors_offset = FT_NEXT_ULONG( p );
- if ( cpal.version != 0 && cpal.version != 1 )
+ if ( colors_offset >= table_size )
+ goto InvalidTable;
+ if ( cpal.num_colors * COLOR_SIZE > table_size - colors_offset )
goto InvalidTable;
- colors_end = colors_begin + cpal.num_colors * COLOR_SIZE;
+ cpal.color_indices = p;
+ cpal.colors = (FT_Byte*)cpal_table + colors_offset;
- /* Ensure variable length tables lies within the COLR table. */
- /* We wrap around FT_ULong at most once since count is FT_UShort. */
- if ( colors_end < colors_begin || colors_end > table_size )
- goto InvalidTable;
+ if ( cpal.version == 1 )
+ {
+ FT_ULong type_offset, label_offset, entry_label_offset;
+ FT_UShort* array;
+ FT_UShort* limit;
+ FT_UShort* q;
- if ( cpal.colors < cpal_table )
- goto InvalidTable;
+
+ p += face->palette.num_palettes * 2;
+
+ type_offset = FT_NEXT_ULONG( p );
+ label_offset = FT_NEXT_ULONG( p );
+ entry_label_offset = FT_NEXT_ULONG( p );
+
+ if ( type_offset )
+ {
+ if ( type_offset >= table_size )
+ goto InvalidTable;
+ if ( face->palette.num_palettes * 2 > table_size - type_offset )
+ goto InvalidTable;
+
+ if ( FT_QNEW_ARRAY( array, face->palette.num_palettes ) )
+ goto NoColor;
+
+ p = cpal_table + type_offset;
+ q = array;
+ limit = q + face->palette.num_palettes * sizeof ( FT_UShort );
+
+ while ( q < limit )
+ *q++ = FT_NEXT_USHORT( p );
+
+ face->palette.palette_types = array;
+ }
+
+ if ( label_offset )
+ {
+ if ( label_offset >= table_size )
+ goto InvalidTable;
+ if ( face->palette.num_palettes * 2 > table_size - label_offset )
+ goto InvalidTable;
+
+ if ( FT_QNEW_ARRAY( array, face->palette.num_palettes ) )
+ goto NoColor;
+
+ p = cpal_table + label_offset;
+ q = array;
+ limit = q + face->palette.num_palettes * sizeof ( FT_UShort );
+
+ while ( q < limit )
+ *q++ = FT_NEXT_USHORT( p );
+
+ face->palette.palette_name_ids = array;
+ }
+
+ if ( entry_label_offset )
+ {
+ if ( entry_label_offset >= table_size )
+ goto InvalidTable;
+ if ( face->palette.num_palette_entries * 2 >
+ table_size - entry_label_offset )
+ goto InvalidTable;
+
+ if ( FT_QNEW_ARRAY( array, face->palette.num_palette_entries ) )
+ goto NoColor;
+
+ p = cpal_table + entry_label_offset;
+ q = array;
+ limit = q + face->palette.num_palette_entries * sizeof ( FT_UShort );
+
+ while ( q < limit )
+ *q++ = FT_NEXT_USHORT( p );
+
+ face->palette.palette_entry_name_ids = array;
+ }
+ }
if ( FT_NEW( cc ) )
goto NoColor;
@@ -225,10 +297,10 @@
error = FT_THROW( Invalid_File_Format );
NoColor:
- if ( colr_table )
- FT_FRAME_RELEASE( colr_table );
- if ( cpal_table )
- FT_FRAME_RELEASE( cpal_table );
+ FT_FRAME_RELEASE( colr_table );
+ FT_FRAME_RELEASE( cpal_table );
+
+ /* arrays in `face->palette' are freed in `sfnt_face_done' */
return error;
}
@@ -300,7 +372,6 @@
ColrCpal* colr_and_cpal = (ColrCpal *)face->colr_and_cpal;
Colr* colr = &colr_and_cpal->colr;
- Cpal* cpal = &colr_and_cpal->cpal;
BaseGlyphRecord glyph_record;
FT_Glyph_Layer layers;
@@ -335,8 +406,8 @@
FT_UShort palette_index = FT_NEXT_USHORT( layer_record_ptr );
- if ( palette_index != 0xFFFF &&
- palette_index >= cpal->num_palettes_entries )
+ if ( palette_index != 0xFFFF &&
+ palette_index >= face->palette.num_palette_entries )
{
error = FT_THROW( Invalid_File_Format );
goto Error;
@@ -375,7 +446,7 @@
FT_Int color_offset;
- if ( color_index >= cpal->num_palettes_entries )
+ if ( color_index >= face->palette.num_palette_entries )
return 0;
p = cpal->color_indices + palette_index * (int)sizeof ( FT_UShort );