Commit a44e20879cefea41663bb36ff4af908cc4146fb8

Werner Lemberg 2018-06-14T11:32:47

[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.

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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
diff --git a/ChangeLog b/ChangeLog
index eb751ab..d42fc2f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2018-06-14  Werner Lemberg  <wl@gnu.org>
+
+	[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  <wl@gnu.org>
 
 	[sfnt] Separate `CPAL' and `COLR' table handling.
diff --git a/include/freetype/internal/fttrace.h b/include/freetype/internal/fttrace.h
index 7cf4556..d7c17d3 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( raster )    /* monochrome rasterizer   (ftraster.c) */
-FT_TRACE_DEF( smooth )    /* anti-aliasing raster    (ftgrays.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( 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( gxvmodule )
+FT_TRACE_DEF( gxvmort )
+FT_TRACE_DEF( gxvmorx )
+FT_TRACE_DEF( gxvlcar )
 FT_TRACE_DEF( gxvopbd )
-FT_TRACE_DEF( gxvtrak )
 FT_TRACE_DEF( gxvprop )
-FT_TRACE_DEF( gxvlcar )
+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 5e8e789..635aa60 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 f6e062b..ff7d7c7 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_DRV_SRC := $(SFNT_DIR)/pngshim.c  \
+                $(SFNT_DIR)/sfdriver.c \
+                $(SFNT_DIR)/sfobjs.c   \
+                $(SFNT_DIR)/ttbdf.c    \
                 $(SFNT_DIR)/ttcmap.c   \
-                $(SFNT_DIR)/ttsbit.c   \
                 $(SFNT_DIR)/ttcolr.c   \
-                $(SFNT_DIR)/ttpost.c   \
+                $(SFNT_DIR)/ttcpal.c   \
                 $(SFNT_DIR)/ttkern.c   \
-                $(SFNT_DIR)/ttbdf.c    \
-                $(SFNT_DIR)/sfobjs.c   \
-                $(SFNT_DIR)/sfdriver.c \
-                $(SFNT_DIR)/pngshim.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 ff1305e..0c3ddc8 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 7ac5483..9b4e3c0 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 a087ee2..66592b6 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 <shaozhang@fb.com>.
+ * Originally written by Shao Yu Zhang <shaozhang@fb.com>.
  *
  * 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 )
@@ -328,23 +155,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 )
   {
     FT_Stream  stream = face->root.stream;
@@ -466,42 +276,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,
                             FT_GlyphSlot  dstSlot,
diff --git a/src/sfnt/ttcolr.h b/src/sfnt/ttcolr.h
index 258f0f6..a548755 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 <shaozhang@fb.com>.
+ * Originally written by Shao Yu Zhang <shaozhang@fb.com>.
  *
  * This file is part of the FreeType project, and may only be used,
  * modified, and distributed under the terms of the FreeType project
@@ -30,17 +30,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 );
 
   FT_LOCAL( FT_Error )
@@ -50,10 +43,6 @@ FT_BEGIN_HEADER
                             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,
                             FT_GlyphSlot  dstSlot,
diff --git a/src/sfnt/ttcpal.c b/src/sfnt/ttcpal.c
new file mode 100644
index 0000000..54c5f0f
--- /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 <shaozhang@fb.com>.
+ *
+ * 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 <ft2build.h>
+#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 0000000..424ef35
--- /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 <shaozhang@fb.com>.
+ *
+ * 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 <ft2build.h>
+#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 */