From a44e20879cefea41663bb36ff4af908cc4146fb8 Mon Sep 17 00:00:00 2001 From: Werner Lemberg Date: Thu, 14 Jun 2018 11:32:47 +0200 Subject: [PATCH] [sfnt] Move `CPAL' stuff into separate files. * src/sfnt/sfdriver.c: Include `ttcpal.h'. * src/sfnt/sfnt.c: Include `ttcpal.c'. * src/sfnt/ttcolr.c, src/sfnt/ttcolr.h: Move CPAL stuff to ... * src/sfnt/ttcpal.c, src/sfnt/ttcpal.c: ... these new files. * src/sfnt/Jamfile (_sources), src/sfnt/rules.mk (SFNT_DRV_SRC): Updated. * include/freetype/internal/fttrace.h: Add support for `colr' and `cpal'. Sort entries. --- ChangeLog | 17 ++ include/freetype/internal/fttrace.h | 52 ++--- src/sfnt/Jamfile | 3 +- src/sfnt/rules.mk | 21 +- src/sfnt/sfdriver.c | 1 + src/sfnt/sfnt.c | 1 + src/sfnt/ttcolr.c | 234 +---------------------- src/sfnt/ttcolr.h | 17 +- src/sfnt/ttcpal.c | 287 ++++++++++++++++++++++++++++ src/sfnt/ttcpal.h | 49 +++++ 10 files changed, 402 insertions(+), 280 deletions(-) create mode 100644 src/sfnt/ttcpal.c create mode 100644 src/sfnt/ttcpal.h diff --git a/ChangeLog b/ChangeLog index eb751ab83..d42fc2fe4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2018-06-14 Werner Lemberg + + [sfnt] Move `CPAL' stuff into separate files. + + * src/sfnt/sfdriver.c: Include `ttcpal.h'. + * src/sfnt/sfnt.c: Include `ttcpal.c'. + + * src/sfnt/ttcolr.c, src/sfnt/ttcolr.h: Move CPAL stuff to ... + * src/sfnt/ttcpal.c, src/sfnt/ttcpal.c: ... these new files. + + * src/sfnt/Jamfile (_sources), src/sfnt/rules.mk (SFNT_DRV_SRC): + Updated. + + * include/freetype/internal/fttrace.h: Add support for `colr' and + `cpal'. + Sort entries. + 2018-06-13 Werner Lemberg [sfnt] Separate `CPAL' and `COLR' table handling. diff --git a/include/freetype/internal/fttrace.h b/include/freetype/internal/fttrace.h index 7cf455658..d7c17d37c 100644 --- a/include/freetype/internal/fttrace.h +++ b/include/freetype/internal/fttrace.h @@ -23,23 +23,23 @@ FT_TRACE_DEF( any ) /* base components */ FT_TRACE_DEF( calc ) /* calculations (ftcalc.c) */ +FT_TRACE_DEF( gloader ) /* glyph loader (ftgloadr.c) */ +FT_TRACE_DEF( glyph ) /* glyph management (ftglyph.c) */ FT_TRACE_DEF( memory ) /* memory manager (ftobjs.c) */ -FT_TRACE_DEF( stream ) /* stream manager (ftstream.c) */ +FT_TRACE_DEF( init ) /* initialization (ftinit.c) */ FT_TRACE_DEF( io ) /* i/o interface (ftsystem.c) */ FT_TRACE_DEF( list ) /* list management (ftlist.c) */ -FT_TRACE_DEF( init ) /* initialization (ftinit.c) */ FT_TRACE_DEF( objs ) /* base objects (ftobjs.c) */ FT_TRACE_DEF( outline ) /* outline management (ftoutln.c) */ -FT_TRACE_DEF( glyph ) /* glyph management (ftglyph.c) */ -FT_TRACE_DEF( gloader ) /* glyph loader (ftgloadr.c) */ +FT_TRACE_DEF( stream ) /* stream manager (ftstream.c) */ +FT_TRACE_DEF( bitmap ) /* bitmap checksum (ftobjs.c) */ +FT_TRACE_DEF( mm ) /* MM interface (ftmm.c) */ +FT_TRACE_DEF( psprops ) /* PS driver properties (ftpsprop.c) */ +FT_TRACE_DEF( raccess ) /* resource fork accessor (ftrfork.c) */ FT_TRACE_DEF( raster ) /* monochrome rasterizer (ftraster.c) */ FT_TRACE_DEF( smooth ) /* anti-aliasing raster (ftgrays.c) */ -FT_TRACE_DEF( mm ) /* MM interface (ftmm.c) */ -FT_TRACE_DEF( raccess ) /* resource fork accessor (ftrfork.c) */ FT_TRACE_DEF( synth ) /* bold/slant synthesizer (ftsynth.c) */ -FT_TRACE_DEF( bitmap ) /* bitmap checksum (ftobjs.c) */ -FT_TRACE_DEF( psprops ) /* PS driver properties (ftpsprop.c) */ /* Cache sub-system */ FT_TRACE_DEF( cache ) /* cache sub-system (ftcache.c, etc.) */ @@ -47,21 +47,23 @@ FT_TRACE_DEF( cache ) /* cache sub-system (ftcache.c, etc.) */ /* SFNT driver components */ FT_TRACE_DEF( sfdriver ) /* SFNT font driver (sfdriver.c) */ FT_TRACE_DEF( sfobjs ) /* SFNT object handler (sfobjs.c) */ +FT_TRACE_DEF( ttbdf ) /* TrueType embedded BDF (ttbdf.c) */ FT_TRACE_DEF( ttcmap ) /* charmap handler (ttcmap.c) */ +FT_TRACE_DEF( ttcolr ) /* glyph layer table (ttcolr.c) */ +FT_TRACE_DEF( ttcpal ) /* color palette table (ttcpal.c) */ FT_TRACE_DEF( ttkern ) /* kerning handler (ttkern.c) */ FT_TRACE_DEF( ttload ) /* basic TrueType tables (ttload.c) */ FT_TRACE_DEF( ttmtx ) /* metrics-related tables (ttmtx.c) */ FT_TRACE_DEF( ttpost ) /* PS table processing (ttpost.c) */ FT_TRACE_DEF( ttsbit ) /* TrueType sbit handling (ttsbit.c) */ -FT_TRACE_DEF( ttbdf ) /* TrueType embedded BDF (ttbdf.c) */ /* TrueType driver components */ FT_TRACE_DEF( ttdriver ) /* TT font driver (ttdriver.c) */ FT_TRACE_DEF( ttgload ) /* TT glyph loader (ttgload.c) */ +FT_TRACE_DEF( ttgxvar ) /* TrueType GX var handler (ttgxvar.c) */ FT_TRACE_DEF( ttinterp ) /* bytecode interpreter (ttinterp.c) */ FT_TRACE_DEF( ttobjs ) /* TT objects manager (ttobjs.c) */ FT_TRACE_DEF( ttpload ) /* TT data/program loader (ttpload.c) */ -FT_TRACE_DEF( ttgxvar ) /* TrueType GX var handler (ttgxvar.c) */ /* Type 1 driver components */ FT_TRACE_DEF( t1afm ) @@ -72,14 +74,14 @@ FT_TRACE_DEF( t1objs ) FT_TRACE_DEF( t1parse ) /* PostScript helper module `psaux' */ -FT_TRACE_DEF( t1decode ) FT_TRACE_DEF( cffdecode ) -FT_TRACE_DEF( psobjs ) FT_TRACE_DEF( psconv ) +FT_TRACE_DEF( psobjs ) +FT_TRACE_DEF( t1decode ) /* PostScript hinting module `pshinter' */ -FT_TRACE_DEF( pshrec ) FT_TRACE_DEF( pshalgo ) +FT_TRACE_DEF( pshrec ) /* Type 2 driver components */ FT_TRACE_DEF( cffdriver ) @@ -117,7 +119,6 @@ FT_TRACE_DEF( bdflib ) FT_TRACE_DEF( pfr ) /* OpenType validation components */ -FT_TRACE_DEF( otvmodule ) FT_TRACE_DEF( otvcommon ) FT_TRACE_DEF( otvbase ) FT_TRACE_DEF( otvgdef ) @@ -125,29 +126,30 @@ FT_TRACE_DEF( otvgpos ) FT_TRACE_DEF( otvgsub ) FT_TRACE_DEF( otvjstf ) FT_TRACE_DEF( otvmath ) +FT_TRACE_DEF( otvmodule ) /* TrueTypeGX/AAT validation components */ -FT_TRACE_DEF( gxvmodule ) +FT_TRACE_DEF( gxvbsln ) FT_TRACE_DEF( gxvcommon ) FT_TRACE_DEF( gxvfeat ) -FT_TRACE_DEF( gxvmort ) -FT_TRACE_DEF( gxvmorx ) -FT_TRACE_DEF( gxvbsln ) FT_TRACE_DEF( gxvjust ) FT_TRACE_DEF( gxvkern ) -FT_TRACE_DEF( gxvopbd ) -FT_TRACE_DEF( gxvtrak ) -FT_TRACE_DEF( gxvprop ) +FT_TRACE_DEF( gxvmodule ) +FT_TRACE_DEF( gxvmort ) +FT_TRACE_DEF( gxvmorx ) FT_TRACE_DEF( gxvlcar ) +FT_TRACE_DEF( gxvopbd ) +FT_TRACE_DEF( gxvprop ) +FT_TRACE_DEF( gxvtrak ) /* autofit components */ -FT_TRACE_DEF( afmodule ) -FT_TRACE_DEF( afhints ) FT_TRACE_DEF( afcjk ) +FT_TRACE_DEF( afglobal ) +FT_TRACE_DEF( afhints ) +FT_TRACE_DEF( afmodule ) FT_TRACE_DEF( aflatin ) FT_TRACE_DEF( aflatin2 ) -FT_TRACE_DEF( afwarp ) FT_TRACE_DEF( afshaper ) -FT_TRACE_DEF( afglobal ) +FT_TRACE_DEF( afwarp ) /* END */ diff --git a/src/sfnt/Jamfile b/src/sfnt/Jamfile index 5e8e78902..635aa6081 100644 --- a/src/sfnt/Jamfile +++ b/src/sfnt/Jamfile @@ -22,12 +22,13 @@ SubDir FT2_TOP $(FT2_SRC_DIR) sfnt ; sfobjs ttbdf ttcmap + ttcolr + ttcpal ttkern ttload ttmtx ttpost ttsbit - ttcolr ; } else diff --git a/src/sfnt/rules.mk b/src/sfnt/rules.mk index f6e062b07..ff7d7c789 100644 --- a/src/sfnt/rules.mk +++ b/src/sfnt/rules.mk @@ -28,17 +28,18 @@ SFNT_COMPILE := $(CC) $(ANSIFLAGS) \ # SFNT driver sources (i.e., C files) # -SFNT_DRV_SRC := $(SFNT_DIR)/ttload.c \ - $(SFNT_DIR)/ttmtx.c \ - $(SFNT_DIR)/ttcmap.c \ - $(SFNT_DIR)/ttsbit.c \ - $(SFNT_DIR)/ttcolr.c \ - $(SFNT_DIR)/ttpost.c \ - $(SFNT_DIR)/ttkern.c \ - $(SFNT_DIR)/ttbdf.c \ - $(SFNT_DIR)/sfobjs.c \ +SFNT_DRV_SRC := $(SFNT_DIR)/pngshim.c \ $(SFNT_DIR)/sfdriver.c \ - $(SFNT_DIR)/pngshim.c + $(SFNT_DIR)/sfobjs.c \ + $(SFNT_DIR)/ttbdf.c \ + $(SFNT_DIR)/ttcmap.c \ + $(SFNT_DIR)/ttcolr.c \ + $(SFNT_DIR)/ttcpal.c \ + $(SFNT_DIR)/ttkern.c \ + $(SFNT_DIR)/ttload.c \ + $(SFNT_DIR)/ttmtx.c \ + $(SFNT_DIR)/ttpost.c \ + $(SFNT_DIR)/ttsbit.c # SFNT driver headers # diff --git a/src/sfnt/sfdriver.c b/src/sfnt/sfdriver.c index ff1305e5a..0c3ddc89f 100644 --- a/src/sfnt/sfdriver.c +++ b/src/sfnt/sfdriver.c @@ -34,6 +34,7 @@ #ifdef TT_CONFIG_OPTION_COLOR_LAYERS #include "ttcolr.h" +#include "ttcpal.h" #endif #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES diff --git a/src/sfnt/sfnt.c b/src/sfnt/sfnt.c index 7ac5483cb..9b4e3c0c6 100644 --- a/src/sfnt/sfnt.c +++ b/src/sfnt/sfnt.c @@ -25,6 +25,7 @@ #include "ttbdf.c" #include "ttcmap.c" #include "ttcolr.c" +#include "ttcpal.c" #include "ttkern.c" #include "ttload.c" diff --git a/src/sfnt/ttcolr.c b/src/sfnt/ttcolr.c index a087ee2a9..66592b626 100644 --- a/src/sfnt/ttcolr.c +++ b/src/sfnt/ttcolr.c @@ -2,12 +2,12 @@ * * ttcolr.c * - * TrueType and OpenType color outline support. + * TrueType and OpenType colored glyph layer support (body). * * Copyright 2018 by * David Turner, Robert Wilhelm, and Werner Lemberg. * - * Written by Shao Yu Zhang . + * Originally written by Shao Yu Zhang . * * This file is part of the FreeType project, and may only be used, * modified, and distributed under the terms of the FreeType project @@ -20,10 +20,9 @@ /************************************************************************** * - * `COLR' and `CPAL' table specification: + * `COLR' table specification: * * https://www.microsoft.com/typography/otspec/colr.htm - * https://www.microsoft.com/typography/otspec/cpal.htm * */ @@ -44,8 +43,6 @@ #define BASE_GLYPH_SIZE 6 #define LAYER_SIZE 4 #define COLR_HEADER_SIZE 14 -#define CPAL_V0_HEADER_BASE_SIZE 12 -#define COLOR_SIZE 4 typedef struct BaseGlyphRecord_ @@ -57,22 +54,6 @@ } BaseGlyphRecord; - /* all data from `CPAL' not covered in FT_Palette_Data */ - typedef struct Cpal_ - { - FT_UShort version; /* Table version number (0 or 1 supported). */ - 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. */ - - /* The memory which backs up the `CPAL' table. */ - void* table; - - } Cpal; - - typedef struct Colr_ { FT_UShort version; @@ -95,161 +76,7 @@ * messages during execution. */ #undef FT_COMPONENT -#define FT_COMPONENT trace_ttcolrcpal - - - FT_LOCAL_DEF( FT_Error ) - tt_face_load_cpal( TT_Face face, - FT_Stream stream ) - { - FT_Error error; - FT_Memory memory = face->root.memory; - - FT_Byte* table = NULL; - FT_Byte* p = NULL; - - Cpal* cpal = NULL; - - FT_ULong colors_offset; - FT_ULong table_size; - - - error = face->goto_table( face, TTAG_CPAL, stream, &table_size ); - if ( error ) - goto NoCpal; - - if ( table_size < CPAL_V0_HEADER_BASE_SIZE ) - goto InvalidTable; - - if ( FT_FRAME_EXTRACT( table_size, table ) ) - goto NoCpal; - - p = table; - - if ( FT_NEW( cpal ) ) - goto NoCpal; - - cpal->version = FT_NEXT_USHORT( p ); - if ( cpal->version > 1 ) - goto InvalidTable; - - face->palette_data.num_palette_entries = FT_NEXT_USHORT( p ); - face->palette_data.num_palettes = FT_NEXT_USHORT( p ); - - cpal->num_colors = FT_NEXT_USHORT( p ); - colors_offset = FT_NEXT_ULONG( p ); - - if ( colors_offset >= table_size ) - goto InvalidTable; - if ( cpal->num_colors * COLOR_SIZE > table_size - colors_offset ) - goto InvalidTable; - - cpal->color_indices = p; - cpal->colors = (FT_Byte*)( table + colors_offset ); - - if ( cpal->version == 1 ) - { - FT_ULong type_offset, label_offset, entry_label_offset; - FT_UShort* array = NULL; - FT_UShort* limit; - FT_UShort* q; - - - p += face->palette_data.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_data.num_palettes * 2 > - table_size - type_offset ) - goto InvalidTable; - - if ( FT_QNEW_ARRAY( array, face->palette_data.num_palettes ) ) - goto NoCpal; - - p = table + type_offset; - q = array; - limit = q + face->palette_data.num_palettes; - - while ( q < limit ) - *q++ = FT_NEXT_USHORT( p ); - - face->palette_data.palette_types = array; - } - - if ( label_offset ) - { - if ( label_offset >= table_size ) - goto InvalidTable; - if ( face->palette_data.num_palettes * 2 > - table_size - label_offset ) - goto InvalidTable; - - if ( FT_QNEW_ARRAY( array, face->palette_data.num_palettes ) ) - goto NoCpal; - - p = table + label_offset; - q = array; - limit = q + face->palette_data.num_palettes; - - while ( q < limit ) - *q++ = FT_NEXT_USHORT( p ); - - face->palette_data.palette_name_ids = array; - } - - if ( entry_label_offset ) - { - if ( entry_label_offset >= table_size ) - goto InvalidTable; - if ( face->palette_data.num_palette_entries * 2 > - table_size - entry_label_offset ) - goto InvalidTable; - - if ( FT_QNEW_ARRAY( array, face->palette_data.num_palette_entries ) ) - goto NoCpal; - - p = table + entry_label_offset; - q = array; - limit = q + face->palette_data.num_palette_entries; - - while ( q < limit ) - *q++ = FT_NEXT_USHORT( p ); - - face->palette_data.palette_entry_name_ids = array; - } - } - - cpal->table = table; - - face->cpal = cpal; - - /* set up default palette */ - if ( FT_NEW_ARRAY( face->palette, - face->palette_data.num_palette_entries ) ) - goto NoCpal; - - tt_face_palette_set( face, 0 ); - - return FT_Err_Ok; - - InvalidTable: - error = FT_THROW( Invalid_Table ); - - NoCpal: - FT_FRAME_RELEASE( table ); - FT_FREE( cpal ); - - /* arrays in `face->palette_data' and `face->palette' */ - /* are freed in `sfnt_done_face' */ - - return error; - } +#define FT_COMPONENT trace_ttcolr FT_LOCAL_DEF( FT_Error ) @@ -327,23 +154,6 @@ } - FT_LOCAL_DEF( void ) - tt_face_free_cpal( TT_Face face ) - { - FT_Stream stream = face->root.stream; - FT_Memory memory = face->root.memory; - - Cpal* cpal = (Cpal*)face->cpal; - - - if ( cpal ) - { - FT_FRAME_RELEASE( cpal->table ); - FT_FREE( cpal ); - } - } - - FT_LOCAL_DEF( void ) tt_face_free_colr( TT_Face face ) { @@ -465,42 +275,6 @@ } - FT_LOCAL_DEF( FT_Error ) - tt_face_palette_set( TT_Face face, - FT_UInt palette_index ) - { - Cpal* cpal = (Cpal*)face->cpal; - - FT_Byte* offset; - FT_Byte* p; - - FT_Color* q; - FT_Color* limit; - - - if ( palette_index >= face->palette_data.num_palettes ) - return FT_THROW( Invalid_Argument ); - - offset = cpal->color_indices + 2 * palette_index; - p = cpal->colors + COLOR_SIZE * FT_PEEK_USHORT( offset ); - - q = face->palette; - limit = q + face->palette_data.num_palette_entries; - - while ( q < limit ) - { - q->blue = FT_NEXT_BYTE( p ); - q->green = FT_NEXT_BYTE( p ); - q->red = FT_NEXT_BYTE( p ); - q->alpha = FT_NEXT_BYTE( p ); - - q++; - } - - return FT_Err_Ok; - } - - 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 258f0f66e..a5487556c 100644 --- a/src/sfnt/ttcolr.h +++ b/src/sfnt/ttcolr.h @@ -1,13 +1,13 @@ /**************************************************************************** * - * ttsbit.h + * ttcolr.h * - * TrueType and OpenType color outline support (specification). + * TrueType and OpenType colored glyph layer support (specification). * * Copyright 2018 by * David Turner, Robert Wilhelm, and Werner Lemberg. * - * Written by Shao Yu Zhang . + * Originally written by Shao Yu Zhang . * * This file is part of the FreeType project, and may only be used, * modified, and distributed under the terms of the FreeType project @@ -29,17 +29,10 @@ FT_BEGIN_HEADER - FT_LOCAL( FT_Error ) - tt_face_load_cpal( TT_Face face, - FT_Stream stream ); - FT_LOCAL( FT_Error ) tt_face_load_colr( TT_Face face, FT_Stream stream ); - FT_LOCAL( void ) - tt_face_free_cpal( TT_Face face ); - FT_LOCAL( void ) tt_face_free_colr( TT_Face face ); @@ -49,10 +42,6 @@ FT_BEGIN_HEADER FT_Glyph_Layer *ret_layers, FT_UShort* ret_num_layers ); - FT_LOCAL( FT_Error ) - tt_face_palette_set( TT_Face face, - FT_UInt palette_index ); - FT_LOCAL( FT_Error ) tt_face_colr_blend_layer( TT_Face face, FT_UInt color_index, diff --git a/src/sfnt/ttcpal.c b/src/sfnt/ttcpal.c new file mode 100644 index 000000000..54c5f0fa2 --- /dev/null +++ b/src/sfnt/ttcpal.c @@ -0,0 +1,287 @@ +/**************************************************************************** + * + * ttcpal.c + * + * TrueType and OpenType color palette support (body). + * + * Copyright 2018 by + * David Turner, Robert Wilhelm, and Werner Lemberg. + * + * Originally written by Shao Yu Zhang . + * + * This file is part of the FreeType project, and may only be used, + * modified, and distributed under the terms of the FreeType project + * license, LICENSE.TXT. By continuing to use, modify, or distribute + * this file you indicate that you have read the license and + * understand and accept it fully. + * + */ + + + /************************************************************************** + * + * `CPAL' table specification: + * + * https://www.microsoft.com/typography/otspec/cpal.htm + * + */ + + +#include +#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 + +#include "ttcpal.h" + + + /* NOTE: These are the table sizes calculated through the specs. */ +#define CPAL_V0_HEADER_BASE_SIZE 12 +#define COLOR_SIZE 4 + + + /* all data from `CPAL' not covered in FT_Palette_Data */ + typedef struct Cpal_ + { + FT_UShort version; /* Table version number (0 or 1 supported). */ + 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. */ + + /* The memory which backs up the `CPAL' table. */ + void* table; + + } Cpal; + + + /************************************************************************** + * + * The macro FT_COMPONENT is used in trace mode. It is an implicit + * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log + * messages during execution. + */ +#undef FT_COMPONENT +#define FT_COMPONENT trace_ttcpal + + + FT_LOCAL_DEF( FT_Error ) + tt_face_load_cpal( TT_Face face, + FT_Stream stream ) + { + FT_Error error; + FT_Memory memory = face->root.memory; + + FT_Byte* table = NULL; + FT_Byte* p = NULL; + + Cpal* cpal = NULL; + + FT_ULong colors_offset; + FT_ULong table_size; + + + error = face->goto_table( face, TTAG_CPAL, stream, &table_size ); + if ( error ) + goto NoCpal; + + if ( table_size < CPAL_V0_HEADER_BASE_SIZE ) + goto InvalidTable; + + if ( FT_FRAME_EXTRACT( table_size, table ) ) + goto NoCpal; + + p = table; + + if ( FT_NEW( cpal ) ) + goto NoCpal; + + cpal->version = FT_NEXT_USHORT( p ); + if ( cpal->version > 1 ) + goto InvalidTable; + + face->palette_data.num_palette_entries = FT_NEXT_USHORT( p ); + face->palette_data.num_palettes = FT_NEXT_USHORT( p ); + + cpal->num_colors = FT_NEXT_USHORT( p ); + colors_offset = FT_NEXT_ULONG( p ); + + if ( colors_offset >= table_size ) + goto InvalidTable; + if ( cpal->num_colors * COLOR_SIZE > table_size - colors_offset ) + goto InvalidTable; + + cpal->color_indices = p; + cpal->colors = (FT_Byte*)( table + colors_offset ); + + if ( cpal->version == 1 ) + { + FT_ULong type_offset, label_offset, entry_label_offset; + FT_UShort* array = NULL; + FT_UShort* limit; + FT_UShort* q; + + + p += face->palette_data.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_data.num_palettes * 2 > + table_size - type_offset ) + goto InvalidTable; + + if ( FT_QNEW_ARRAY( array, face->palette_data.num_palettes ) ) + goto NoCpal; + + p = table + type_offset; + q = array; + limit = q + face->palette_data.num_palettes; + + while ( q < limit ) + *q++ = FT_NEXT_USHORT( p ); + + face->palette_data.palette_types = array; + } + + if ( label_offset ) + { + if ( label_offset >= table_size ) + goto InvalidTable; + if ( face->palette_data.num_palettes * 2 > + table_size - label_offset ) + goto InvalidTable; + + if ( FT_QNEW_ARRAY( array, face->palette_data.num_palettes ) ) + goto NoCpal; + + p = table + label_offset; + q = array; + limit = q + face->palette_data.num_palettes; + + while ( q < limit ) + *q++ = FT_NEXT_USHORT( p ); + + face->palette_data.palette_name_ids = array; + } + + if ( entry_label_offset ) + { + if ( entry_label_offset >= table_size ) + goto InvalidTable; + if ( face->palette_data.num_palette_entries * 2 > + table_size - entry_label_offset ) + goto InvalidTable; + + if ( FT_QNEW_ARRAY( array, face->palette_data.num_palette_entries ) ) + goto NoCpal; + + p = table + entry_label_offset; + q = array; + limit = q + face->palette_data.num_palette_entries; + + while ( q < limit ) + *q++ = FT_NEXT_USHORT( p ); + + face->palette_data.palette_entry_name_ids = array; + } + } + + cpal->table = table; + + face->cpal = cpal; + + /* set up default palette */ + if ( FT_NEW_ARRAY( face->palette, + face->palette_data.num_palette_entries ) ) + goto NoCpal; + + tt_face_palette_set( face, 0 ); + + return FT_Err_Ok; + + InvalidTable: + error = FT_THROW( Invalid_Table ); + + NoCpal: + FT_FRAME_RELEASE( table ); + FT_FREE( cpal ); + + /* arrays in `face->palette_data' and `face->palette' */ + /* are freed in `sfnt_done_face' */ + + return error; + } + + + FT_LOCAL_DEF( void ) + tt_face_free_cpal( TT_Face face ) + { + FT_Stream stream = face->root.stream; + FT_Memory memory = face->root.memory; + + Cpal* cpal = (Cpal*)face->cpal; + + + if ( cpal ) + { + FT_FRAME_RELEASE( cpal->table ); + FT_FREE( cpal ); + } + } + + + FT_LOCAL_DEF( FT_Error ) + tt_face_palette_set( TT_Face face, + FT_UInt palette_index ) + { + Cpal* cpal = (Cpal*)face->cpal; + + FT_Byte* offset; + FT_Byte* p; + + FT_Color* q; + FT_Color* limit; + + + if ( palette_index >= face->palette_data.num_palettes ) + return FT_THROW( Invalid_Argument ); + + offset = cpal->color_indices + 2 * palette_index; + p = cpal->colors + COLOR_SIZE * FT_PEEK_USHORT( offset ); + + q = face->palette; + limit = q + face->palette_data.num_palette_entries; + + while ( q < limit ) + { + q->blue = FT_NEXT_BYTE( p ); + q->green = FT_NEXT_BYTE( p ); + q->red = FT_NEXT_BYTE( p ); + q->alpha = FT_NEXT_BYTE( p ); + + q++; + } + + return FT_Err_Ok; + } + + +#else /* !TT_CONFIG_OPTION_COLOR_LAYERS */ + + /* ANSI C doesn't like empty source files */ + typedef int _tt_cpal_dummy; + +#endif /* !TT_CONFIG_OPTION_COLOR_LAYERS */ + +/* EOF */ diff --git a/src/sfnt/ttcpal.h b/src/sfnt/ttcpal.h new file mode 100644 index 000000000..424ef35d2 --- /dev/null +++ b/src/sfnt/ttcpal.h @@ -0,0 +1,49 @@ +/**************************************************************************** + * + * ttcpal.h + * + * TrueType and OpenType color palette support (specification). + * + * Copyright 2018 by + * David Turner, Robert Wilhelm, and Werner Lemberg. + * + * Originally written by Shao Yu Zhang . + * + * This file is part of the FreeType project, and may only be used, + * modified, and distributed under the terms of the FreeType project + * license, LICENSE.TXT. By continuing to use, modify, or distribute + * this file you indicate that you have read the license and + * understand and accept it fully. + * + */ + + +#ifndef __TTCPAL_H__ +#define __TTCPAL_H__ + + +#include +#include "ttload.h" + + +FT_BEGIN_HEADER + + + FT_LOCAL( FT_Error ) + tt_face_load_cpal( TT_Face face, + FT_Stream stream ); + + FT_LOCAL( void ) + tt_face_free_cpal( TT_Face face ); + + FT_LOCAL( FT_Error ) + tt_face_palette_set( TT_Face face, + FT_UInt palette_index ); + + +FT_END_HEADER + + +#endif /* __TTCPAL_H__ */ + +/* END */