Commit 7b275a5af161c71a169dcec65f2f6998c9f2d6d5

Nikhil Ramakrishnan 2019-05-30T23:57:34

[sfnt] Separate WOFF sources and headers. Move WOFF sources and headers to separate files. * include/freetype/internal/wofftypes.h, src/sfnt/sfwoff.c, src/sfnt/sfwoff.h: New files. * include/freetype/internal/fttrace.h: Register `sfwoff.c'. * include/freetype/internal/internal.h: Define FT_INTERNAL_WOFF_TYPES_H. * include/freetype/internal/sfnt.h: Include FT_INTERNAL_WOFF_TYPES_H. * include/freetype/internal/tttypes.h: Move out WOFF structures. * src/sfnt/rules.mk: Add `sfwoff.c'. * src/sfnt/sfnt.c: Include `sfwoff.c'. * src/sfnt/sfobjs.c: Include `sfwoff.h', move out WOFF sources.

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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
diff --git a/ChangeLog b/ChangeLog
index 6513ddd..55efb87 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,27 @@
+2019-05-30  Nikhil Ramakrishnan  <ramakrishnan.nikhil@gmail.com>
+
+	[sfnt] Separate WOFF sources and headers.
+
+	Move WOFF sources and headers to separate files.
+
+	* include/freetype/internal/wofftypes.h, src/sfnt/sfwoff.c,
+	src/sfnt/sfwoff.h: New files.
+
+	* include/freetype/internal/fttrace.h: Register `sfwoff.c'.
+
+	* include/freetype/internal/internal.h: Define
+	FT_INTERNAL_WOFF_TYPES_H.
+
+	* include/freetype/internal/sfnt.h: Include FT_INTERNAL_WOFF_TYPES_H.
+
+	* include/freetype/internal/tttypes.h: Move out WOFF structures.
+
+	* src/sfnt/rules.mk: Add `sfwoff.c'.
+
+	* src/sfnt/sfnt.c: Include `sfwoff.c'.
+
+	* src/sfnt/sfobjs.c: Include `sfwoff.h', move out WOFF sources.
+
 2019-05-30  Werner Lemberg  <wl@gnu.org>
 
 	[base] Fix `make multi'.
diff --git a/include/freetype/internal/fttrace.h b/include/freetype/internal/fttrace.h
index 8089bab..f5f9598 100644
--- a/include/freetype/internal/fttrace.h
+++ b/include/freetype/internal/fttrace.h
@@ -48,6 +48,7 @@ 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( sfwoff )    /* WOFF format handler     (sfwoff.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)   */
diff --git a/include/freetype/internal/internal.h b/include/freetype/internal/internal.h
index 173d8ad..3c8830f 100644
--- a/include/freetype/internal/internal.h
+++ b/include/freetype/internal/internal.h
@@ -40,6 +40,7 @@
 
 #define FT_INTERNAL_TRUETYPE_TYPES_H      <freetype/internal/tttypes.h>
 #define FT_INTERNAL_TYPE1_TYPES_H         <freetype/internal/t1types.h>
+#define FT_INTERNAL_WOFF_TYPES_H          <freetype/internal/wofftypes.h>
 
 #define FT_INTERNAL_POSTSCRIPT_AUX_H      <freetype/internal/psaux.h>
 #define FT_INTERNAL_POSTSCRIPT_HINTS_H    <freetype/internal/pshints.h>
diff --git a/include/freetype/internal/sfnt.h b/include/freetype/internal/sfnt.h
index 225f40d..b19241c 100644
--- a/include/freetype/internal/sfnt.h
+++ b/include/freetype/internal/sfnt.h
@@ -23,6 +23,7 @@
 #include <ft2build.h>
 #include FT_INTERNAL_DRIVER_H
 #include FT_INTERNAL_TRUETYPE_TYPES_H
+#include FT_INTERNAL_WOFF_TYPES_H
 
 
 FT_BEGIN_HEADER
diff --git a/include/freetype/internal/tttypes.h b/include/freetype/internal/tttypes.h
index cb03368..23db240 100644
--- a/include/freetype/internal/tttypes.h
+++ b/include/freetype/internal/tttypes.h
@@ -153,81 +153,6 @@ FT_BEGIN_HEADER
   /**************************************************************************
    *
    * @struct:
-   *   WOFF_HeaderRec
-   *
-   * @description:
-   *   WOFF file format header.
-   *
-   * @fields:
-   *   See
-   *
-   *     https://www.w3.org/TR/WOFF/#WOFFHeader
-   */
-  typedef struct  WOFF_HeaderRec_
-  {
-    FT_ULong   signature;
-    FT_ULong   flavor;
-    FT_ULong   length;
-    FT_UShort  num_tables;
-    FT_UShort  reserved;
-    FT_ULong   totalSfntSize;
-    FT_UShort  majorVersion;
-    FT_UShort  minorVersion;
-    FT_ULong   metaOffset;
-    FT_ULong   metaLength;
-    FT_ULong   metaOrigLength;
-    FT_ULong   privOffset;
-    FT_ULong   privLength;
-
-  } WOFF_HeaderRec, *WOFF_Header;
-
-
-  /**************************************************************************
-   *
-   * @struct:
-   *   WOFF_TableRec
-   *
-   * @description:
-   *   This structure describes a given table of a WOFF font.
-   *
-   * @fields:
-   *   Tag ::
-   *     A four-bytes tag describing the table.
-   *
-   *   Offset ::
-   *     The offset of the table from the start of the WOFF font in its
-   *     resource.
-   *
-   *   CompLength ::
-   *     Compressed table length (in bytes).
-   *
-   *   OrigLength ::
-   *     Uncompressed table length (in bytes).
-   *
-   *   CheckSum ::
-   *     The table checksum.  This value can be ignored.
-   *
-   *   OrigOffset ::
-   *     The uncompressed table file offset.  This value gets computed while
-   *     constructing the (uncompressed) SFNT header.  It is not contained in
-   *     the WOFF file.
-   */
-  typedef struct  WOFF_TableRec_
-  {
-    FT_ULong  Tag;           /* table ID                  */
-    FT_ULong  Offset;        /* table file offset         */
-    FT_ULong  CompLength;    /* compressed table length   */
-    FT_ULong  OrigLength;    /* uncompressed table length */
-    FT_ULong  CheckSum;      /* uncompressed checksum     */
-
-    FT_ULong  OrigOffset;    /* uncompressed table file offset */
-                             /* (not in the WOFF file)         */
-  } WOFF_TableRec, *WOFF_Table;
-
-
-  /**************************************************************************
-   *
-   * @struct:
    *   TT_LongMetricsRec
    *
    * @description:
diff --git a/include/freetype/internal/wofftypes.h b/include/freetype/internal/wofftypes.h
new file mode 100644
index 0000000..ba55bf8
--- /dev/null
+++ b/include/freetype/internal/wofftypes.h
@@ -0,0 +1,112 @@
+/****************************************************************************
+ *
+ * wofftypes.h
+ *
+ *   Basic WOFF/WOFF2 type definitions and interface (specification
+ *   only).
+ *
+ * Copyright (C) 1996-2019 by
+ * David Turner, Robert Wilhelm, and Werner Lemberg.
+ *
+ * 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 WOFFTYPES_H_
+#define WOFFTYPES_H_
+
+
+#include <ft2build.h>
+#include FT_TRUETYPE_TABLES_H
+#include FT_INTERNAL_OBJECTS_H
+
+
+FT_BEGIN_HEADER
+
+
+  /**************************************************************************
+   *
+   * @struct:
+   *   WOFF_HeaderRec
+   *
+   * @description:
+   *   WOFF file format header.
+   *
+   * @fields:
+   *   See
+   *
+   *     https://www.w3.org/TR/WOFF/#WOFFHeader
+   */
+  typedef struct  WOFF_HeaderRec_
+  {
+    FT_ULong   signature;
+    FT_ULong   flavor;
+    FT_ULong   length;
+    FT_UShort  num_tables;
+    FT_UShort  reserved;
+    FT_ULong   totalSfntSize;
+    FT_UShort  majorVersion;
+    FT_UShort  minorVersion;
+    FT_ULong   metaOffset;
+    FT_ULong   metaLength;
+    FT_ULong   metaOrigLength;
+    FT_ULong   privOffset;
+    FT_ULong   privLength;
+
+  } WOFF_HeaderRec, *WOFF_Header;
+
+
+  /**************************************************************************
+   *
+   * @struct:
+   *   WOFF_TableRec
+   *
+   * @description:
+   *   This structure describes a given table of a WOFF font.
+   *
+   * @fields:
+   *   Tag ::
+   *     A four-bytes tag describing the table.
+   *
+   *   Offset ::
+   *     The offset of the table from the start of the WOFF font in its
+   *     resource.
+   *
+   *   CompLength ::
+   *     Compressed table length (in bytes).
+   *
+   *   OrigLength ::
+   *     Uncompressed table length (in bytes).
+   *
+   *   CheckSum ::
+   *     The table checksum.  This value can be ignored.
+   *
+   *   OrigOffset ::
+   *     The uncompressed table file offset.  This value gets computed while
+   *     constructing the (uncompressed) SFNT header.  It is not contained in
+   *     the WOFF file.
+   */
+  typedef struct  WOFF_TableRec_
+  {
+    FT_ULong  Tag;           /* table ID                  */
+    FT_ULong  Offset;        /* table file offset         */
+    FT_ULong  CompLength;    /* compressed table length   */
+    FT_ULong  OrigLength;    /* uncompressed table length */
+    FT_ULong  CheckSum;      /* uncompressed checksum     */
+
+    FT_ULong  OrigOffset;    /* uncompressed table file offset */
+                             /* (not in the WOFF file)         */
+  } WOFF_TableRec, *WOFF_Table;
+
+
+FT_END_HEADER
+
+#endif /* WOFFTYPES_H_ */
+
+
+/* END */
diff --git a/src/sfnt/rules.mk b/src/sfnt/rules.mk
index ff9e7c6..ee3314e 100644
--- a/src/sfnt/rules.mk
+++ b/src/sfnt/rules.mk
@@ -31,6 +31,7 @@ SFNT_COMPILE := $(CC) $(ANSIFLAGS)                             \
 SFNT_DRV_SRC := $(SFNT_DIR)/pngshim.c  \
                 $(SFNT_DIR)/sfdriver.c \
                 $(SFNT_DIR)/sfobjs.c   \
+                $(SFNT_DIR)/sfwoff.c   \
                 $(SFNT_DIR)/ttbdf.c    \
                 $(SFNT_DIR)/ttcmap.c   \
                 $(SFNT_DIR)/ttcolr.c   \
diff --git a/src/sfnt/sfnt.c b/src/sfnt/sfnt.c
index 5a503f3..b4faf34 100644
--- a/src/sfnt/sfnt.c
+++ b/src/sfnt/sfnt.c
@@ -22,6 +22,7 @@
 #include "pngshim.c"
 #include "sfdriver.c"
 #include "sfobjs.c"
+#include "sfwoff.c"
 #include "ttbdf.c"
 #include "ttcmap.c"
 #include "ttcolr.c"
diff --git a/src/sfnt/sfobjs.c b/src/sfnt/sfobjs.c
index 0b43d25..6edf3ae 100644
--- a/src/sfnt/sfobjs.c
+++ b/src/sfnt/sfobjs.c
@@ -21,13 +21,13 @@
 #include "ttload.h"
 #include "ttcmap.h"
 #include "ttkern.h"
+#include "sfwoff.h"
 #include FT_INTERNAL_SFNT_H
 #include FT_INTERNAL_DEBUG_H
 #include FT_TRUETYPE_IDS_H
 #include FT_TRUETYPE_TAGS_H
 #include FT_SERVICE_POSTSCRIPT_CMAPS_H
 #include FT_SFNT_NAMES_H
-#include FT_GZIP_H
 
 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
 #include FT_SERVICE_MULTIPLE_MASTERS_H
@@ -337,403 +337,6 @@
   }
 
 
-#define WRITE_USHORT( p, v )                \
-          do                                \
-          {                                 \
-            *(p)++ = (FT_Byte)( (v) >> 8 ); \
-            *(p)++ = (FT_Byte)( (v) >> 0 ); \
-                                            \
-          } while ( 0 )
-
-#define WRITE_ULONG( p, v )                  \
-          do                                 \
-          {                                  \
-            *(p)++ = (FT_Byte)( (v) >> 24 ); \
-            *(p)++ = (FT_Byte)( (v) >> 16 ); \
-            *(p)++ = (FT_Byte)( (v) >>  8 ); \
-            *(p)++ = (FT_Byte)( (v) >>  0 ); \
-                                             \
-          } while ( 0 )
-
-
-  static void
-  sfnt_stream_close( FT_Stream  stream )
-  {
-    FT_Memory  memory = stream->memory;
-
-
-    FT_FREE( stream->base );
-
-    stream->size  = 0;
-    stream->base  = NULL;
-    stream->close = NULL;
-  }
-
-
-  FT_CALLBACK_DEF( int )
-  compare_offsets( const void*  a,
-                   const void*  b )
-  {
-    WOFF_Table  table1 = *(WOFF_Table*)a;
-    WOFF_Table  table2 = *(WOFF_Table*)b;
-
-    FT_ULong  offset1 = table1->Offset;
-    FT_ULong  offset2 = table2->Offset;
-
-
-    if ( offset1 > offset2 )
-      return 1;
-    else if ( offset1 < offset2 )
-      return -1;
-    else
-      return 0;
-  }
-
-
-  /* Replace `face->root.stream' with a stream containing the extracted */
-  /* SFNT of a WOFF font.                                               */
-
-  static FT_Error
-  woff_open_font( FT_Stream  stream,
-                  TT_Face    face )
-  {
-    FT_Memory       memory = stream->memory;
-    FT_Error        error  = FT_Err_Ok;
-
-    WOFF_HeaderRec  woff;
-    WOFF_Table      tables  = NULL;
-    WOFF_Table*     indices = NULL;
-
-    FT_ULong        woff_offset;
-
-    FT_Byte*        sfnt        = NULL;
-    FT_Stream       sfnt_stream = NULL;
-
-    FT_Byte*        sfnt_header;
-    FT_ULong        sfnt_offset;
-
-    FT_Int          nn;
-    FT_ULong        old_tag = 0;
-
-    static const FT_Frame_Field  woff_header_fields[] =
-    {
-#undef  FT_STRUCTURE
-#define FT_STRUCTURE  WOFF_HeaderRec
-
-      FT_FRAME_START( 44 ),
-        FT_FRAME_ULONG ( signature ),
-        FT_FRAME_ULONG ( flavor ),
-        FT_FRAME_ULONG ( length ),
-        FT_FRAME_USHORT( num_tables ),
-        FT_FRAME_USHORT( reserved ),
-        FT_FRAME_ULONG ( totalSfntSize ),
-        FT_FRAME_USHORT( majorVersion ),
-        FT_FRAME_USHORT( minorVersion ),
-        FT_FRAME_ULONG ( metaOffset ),
-        FT_FRAME_ULONG ( metaLength ),
-        FT_FRAME_ULONG ( metaOrigLength ),
-        FT_FRAME_ULONG ( privOffset ),
-        FT_FRAME_ULONG ( privLength ),
-      FT_FRAME_END
-    };
-
-
-    FT_ASSERT( stream == face->root.stream );
-    FT_ASSERT( FT_STREAM_POS() == 0 );
-
-    if ( FT_STREAM_READ_FIELDS( woff_header_fields, &woff ) )
-      return error;
-
-    /* Make sure we don't recurse back here or hit TTC code. */
-    if ( woff.flavor == TTAG_wOFF || woff.flavor == TTAG_ttcf )
-      return FT_THROW( Invalid_Table );
-
-    /* Miscellaneous checks. */
-    if ( woff.length != stream->size                              ||
-         woff.num_tables == 0                                     ||
-         44 + woff.num_tables * 20UL >= woff.length               ||
-         12 + woff.num_tables * 16UL >= woff.totalSfntSize        ||
-         ( woff.totalSfntSize & 3 ) != 0                          ||
-         ( woff.metaOffset == 0 && ( woff.metaLength != 0     ||
-                                     woff.metaOrigLength != 0 ) ) ||
-         ( woff.metaLength != 0 && woff.metaOrigLength == 0 )     ||
-         ( woff.privOffset == 0 && woff.privLength != 0 )         )
-    {
-      FT_ERROR(( "woff_font_open: invalid WOFF header\n" ));
-      return FT_THROW( Invalid_Table );
-    }
-
-    /* Don't trust `totalSfntSize' before thorough checks. */
-    if ( FT_ALLOC( sfnt, 12 + woff.num_tables * 16UL ) ||
-         FT_NEW( sfnt_stream )                         )
-      goto Exit;
-
-    sfnt_header = sfnt;
-
-    /* Write sfnt header. */
-    {
-      FT_UInt  searchRange, entrySelector, rangeShift, x;
-
-
-      x             = woff.num_tables;
-      entrySelector = 0;
-      while ( x )
-      {
-        x            >>= 1;
-        entrySelector += 1;
-      }
-      entrySelector--;
-
-      searchRange = ( 1 << entrySelector ) * 16;
-      rangeShift  = woff.num_tables * 16 - searchRange;
-
-      WRITE_ULONG ( sfnt_header, woff.flavor );
-      WRITE_USHORT( sfnt_header, woff.num_tables );
-      WRITE_USHORT( sfnt_header, searchRange );
-      WRITE_USHORT( sfnt_header, entrySelector );
-      WRITE_USHORT( sfnt_header, rangeShift );
-    }
-
-    /* While the entries in the sfnt header must be sorted by the */
-    /* tag value, the tables themselves are not.  We thus have to */
-    /* sort them by offset and check that they don't overlap.     */
-
-    if ( FT_NEW_ARRAY( tables, woff.num_tables )  ||
-         FT_NEW_ARRAY( indices, woff.num_tables ) )
-      goto Exit;
-
-    FT_TRACE2(( "\n"
-                "  tag    offset    compLen  origLen  checksum\n"
-                "  -------------------------------------------\n" ));
-
-    if ( FT_FRAME_ENTER( 20L * woff.num_tables ) )
-      goto Exit;
-
-    for ( nn = 0; nn < woff.num_tables; nn++ )
-    {
-      WOFF_Table  table = tables + nn;
-
-      table->Tag        = FT_GET_TAG4();
-      table->Offset     = FT_GET_ULONG();
-      table->CompLength = FT_GET_ULONG();
-      table->OrigLength = FT_GET_ULONG();
-      table->CheckSum   = FT_GET_ULONG();
-
-      FT_TRACE2(( "  %c%c%c%c  %08lx  %08lx  %08lx  %08lx\n",
-                  (FT_Char)( table->Tag >> 24 ),
-                  (FT_Char)( table->Tag >> 16 ),
-                  (FT_Char)( table->Tag >> 8  ),
-                  (FT_Char)( table->Tag       ),
-                  table->Offset,
-                  table->CompLength,
-                  table->OrigLength,
-                  table->CheckSum ));
-
-      if ( table->Tag <= old_tag )
-      {
-        FT_FRAME_EXIT();
-
-        FT_ERROR(( "woff_font_open: table tags are not sorted\n" ));
-        error = FT_THROW( Invalid_Table );
-        goto Exit;
-      }
-
-      old_tag     = table->Tag;
-      indices[nn] = table;
-    }
-
-    FT_FRAME_EXIT();
-
-    /* Sort by offset. */
-
-    ft_qsort( indices,
-              woff.num_tables,
-              sizeof ( WOFF_Table ),
-              compare_offsets );
-
-    /* Check offsets and lengths. */
-
-    woff_offset = 44 + woff.num_tables * 20L;
-    sfnt_offset = 12 + woff.num_tables * 16L;
-
-    for ( nn = 0; nn < woff.num_tables; nn++ )
-    {
-      WOFF_Table  table = indices[nn];
-
-
-      if ( table->Offset != woff_offset                         ||
-           table->CompLength > woff.length                      ||
-           table->Offset > woff.length - table->CompLength      ||
-           table->OrigLength > woff.totalSfntSize               ||
-           sfnt_offset > woff.totalSfntSize - table->OrigLength ||
-           table->CompLength > table->OrigLength                )
-      {
-        FT_ERROR(( "woff_font_open: invalid table offsets\n" ));
-        error = FT_THROW( Invalid_Table );
-        goto Exit;
-      }
-
-      table->OrigOffset = sfnt_offset;
-
-      /* The offsets must be multiples of 4. */
-      woff_offset += ( table->CompLength + 3 ) & ~3U;
-      sfnt_offset += ( table->OrigLength + 3 ) & ~3U;
-    }
-
-    /*
-     * Final checks!
-     *
-     * We don't decode and check the metadata block.
-     * We don't check table checksums either.
-     * But other than those, I think we implement all
-     * `MUST' checks from the spec.
-     */
-
-    if ( woff.metaOffset )
-    {
-      if ( woff.metaOffset != woff_offset                  ||
-           woff.metaOffset + woff.metaLength > woff.length )
-      {
-        FT_ERROR(( "woff_font_open:"
-                   " invalid `metadata' offset or length\n" ));
-        error = FT_THROW( Invalid_Table );
-        goto Exit;
-      }
-
-      /* We have padding only ... */
-      woff_offset += woff.metaLength;
-    }
-
-    if ( woff.privOffset )
-    {
-      /* ... if it isn't the last block. */
-      woff_offset = ( woff_offset + 3 ) & ~3U;
-
-      if ( woff.privOffset != woff_offset                  ||
-           woff.privOffset + woff.privLength > woff.length )
-      {
-        FT_ERROR(( "woff_font_open: invalid `private' offset or length\n" ));
-        error = FT_THROW( Invalid_Table );
-        goto Exit;
-      }
-
-      /* No padding for the last block. */
-      woff_offset += woff.privLength;
-    }
-
-    if ( sfnt_offset != woff.totalSfntSize ||
-         woff_offset != woff.length        )
-    {
-      FT_ERROR(( "woff_font_open: invalid `sfnt' table structure\n" ));
-      error = FT_THROW( Invalid_Table );
-      goto Exit;
-    }
-
-    /* Now use `totalSfntSize'. */
-    if ( FT_REALLOC( sfnt,
-                     12 + woff.num_tables * 16UL,
-                     woff.totalSfntSize ) )
-      goto Exit;
-
-    sfnt_header = sfnt + 12;
-
-    /* Write the tables. */
-
-    for ( nn = 0; nn < woff.num_tables; nn++ )
-    {
-      WOFF_Table  table = tables + nn;
-
-
-      /* Write SFNT table entry. */
-      WRITE_ULONG( sfnt_header, table->Tag );
-      WRITE_ULONG( sfnt_header, table->CheckSum );
-      WRITE_ULONG( sfnt_header, table->OrigOffset );
-      WRITE_ULONG( sfnt_header, table->OrigLength );
-
-      /* Write table data. */
-      if ( FT_STREAM_SEEK( table->Offset )     ||
-           FT_FRAME_ENTER( table->CompLength ) )
-        goto Exit;
-
-      if ( table->CompLength == table->OrigLength )
-      {
-        /* Uncompressed data; just copy. */
-        ft_memcpy( sfnt + table->OrigOffset,
-                   stream->cursor,
-                   table->OrigLength );
-      }
-      else
-      {
-#ifdef FT_CONFIG_OPTION_USE_ZLIB
-
-        /* Uncompress with zlib. */
-        FT_ULong  output_len = table->OrigLength;
-
-
-        error = FT_Gzip_Uncompress( memory,
-                                    sfnt + table->OrigOffset, &output_len,
-                                    stream->cursor, table->CompLength );
-        if ( error )
-          goto Exit;
-        if ( output_len != table->OrigLength )
-        {
-          FT_ERROR(( "woff_font_open: compressed table length mismatch\n" ));
-          error = FT_THROW( Invalid_Table );
-          goto Exit;
-        }
-
-#else /* !FT_CONFIG_OPTION_USE_ZLIB */
-
-        error = FT_THROW( Unimplemented_Feature );
-        goto Exit;
-
-#endif /* !FT_CONFIG_OPTION_USE_ZLIB */
-      }
-
-      FT_FRAME_EXIT();
-
-      /* We don't check whether the padding bytes in the WOFF file are     */
-      /* actually '\0'.  For the output, however, we do set them properly. */
-      sfnt_offset = table->OrigOffset + table->OrigLength;
-      while ( sfnt_offset & 3 )
-      {
-        sfnt[sfnt_offset] = '\0';
-        sfnt_offset++;
-      }
-    }
-
-    /* Ok!  Finally ready.  Swap out stream and return. */
-    FT_Stream_OpenMemory( sfnt_stream, sfnt, woff.totalSfntSize );
-    sfnt_stream->memory = stream->memory;
-    sfnt_stream->close  = sfnt_stream_close;
-
-    FT_Stream_Free(
-      face->root.stream,
-      ( face->root.face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
-
-    face->root.stream = sfnt_stream;
-
-    face->root.face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
-
-  Exit:
-    FT_FREE( tables );
-    FT_FREE( indices );
-
-    if ( error )
-    {
-      FT_FREE( sfnt );
-      FT_Stream_Close( sfnt_stream );
-      FT_FREE( sfnt_stream );
-    }
-
-    return error;
-  }
-
-
-#undef WRITE_USHORT
-#undef WRITE_ULONG
-
-
   /* Fill in face->ttc_header.  If the font is not a TTC, it is */
   /* synthesized into a TTC with one offset table.              */
   static FT_Error
diff --git a/src/sfnt/sfwoff.c b/src/sfnt/sfwoff.c
new file mode 100644
index 0000000..ca4821a
--- /dev/null
+++ b/src/sfnt/sfwoff.c
@@ -0,0 +1,434 @@
+/****************************************************************************
+ *
+ * sfwoff.c
+ *
+ *   WOFF format management (base).
+ *
+ * Copyright (C) 1996-2019 by
+ * David Turner, Robert Wilhelm, and Werner Lemberg.
+ *
+ * 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.
+ *
+ */
+
+
+#include <ft2build.h>
+#include "sfwoff.h"
+#include FT_TRUETYPE_TAGS_H
+#include FT_INTERNAL_DEBUG_H
+#include FT_INTERNAL_STREAM_H
+#include FT_GZIP_H
+
+
+  /**************************************************************************
+   *
+   * 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  sfwoff
+
+
+#define WRITE_USHORT( p, v )                \
+          do                                \
+          {                                 \
+            *(p)++ = (FT_Byte)( (v) >> 8 ); \
+            *(p)++ = (FT_Byte)( (v) >> 0 ); \
+                                            \
+          } while ( 0 )
+
+#define WRITE_ULONG( p, v )                  \
+          do                                 \
+          {                                  \
+            *(p)++ = (FT_Byte)( (v) >> 24 ); \
+            *(p)++ = (FT_Byte)( (v) >> 16 ); \
+            *(p)++ = (FT_Byte)( (v) >>  8 ); \
+            *(p)++ = (FT_Byte)( (v) >>  0 ); \
+                                             \
+          } while ( 0 )
+
+
+  static void
+  sfnt_stream_close( FT_Stream  stream )
+  {
+    FT_Memory  memory = stream->memory;
+
+
+    FT_FREE( stream->base );
+
+    stream->size  = 0;
+    stream->base  = NULL;
+    stream->close = NULL;
+  }
+
+
+  FT_CALLBACK_DEF( int )
+  compare_offsets( const void*  a,
+                   const void*  b )
+  {
+    WOFF_Table  table1 = *(WOFF_Table*)a;
+    WOFF_Table  table2 = *(WOFF_Table*)b;
+
+    FT_ULong  offset1 = table1->Offset;
+    FT_ULong  offset2 = table2->Offset;
+
+
+    if ( offset1 > offset2 )
+      return 1;
+    else if ( offset1 < offset2 )
+      return -1;
+    else
+      return 0;
+  }
+
+
+  /* Replace `face->root.stream' with a stream containing the extracted */
+  /* SFNT of a WOFF font.                                               */
+
+  FT_LOCAL_DEF( FT_Error )
+  woff_open_font( FT_Stream  stream,
+                  TT_Face    face )
+  {
+    FT_Memory       memory = stream->memory;
+    FT_Error        error  = FT_Err_Ok;
+
+    WOFF_HeaderRec  woff;
+    WOFF_Table      tables  = NULL;
+    WOFF_Table*     indices = NULL;
+
+    FT_ULong        woff_offset;
+
+    FT_Byte*        sfnt        = NULL;
+    FT_Stream       sfnt_stream = NULL;
+
+    FT_Byte*        sfnt_header;
+    FT_ULong        sfnt_offset;
+
+    FT_Int          nn;
+    FT_ULong        old_tag = 0;
+
+    static const FT_Frame_Field  woff_header_fields[] =
+    {
+#undef  FT_STRUCTURE
+#define FT_STRUCTURE  WOFF_HeaderRec
+
+      FT_FRAME_START( 44 ),
+        FT_FRAME_ULONG ( signature ),
+        FT_FRAME_ULONG ( flavor ),
+        FT_FRAME_ULONG ( length ),
+        FT_FRAME_USHORT( num_tables ),
+        FT_FRAME_USHORT( reserved ),
+        FT_FRAME_ULONG ( totalSfntSize ),
+        FT_FRAME_USHORT( majorVersion ),
+        FT_FRAME_USHORT( minorVersion ),
+        FT_FRAME_ULONG ( metaOffset ),
+        FT_FRAME_ULONG ( metaLength ),
+        FT_FRAME_ULONG ( metaOrigLength ),
+        FT_FRAME_ULONG ( privOffset ),
+        FT_FRAME_ULONG ( privLength ),
+      FT_FRAME_END
+    };
+
+
+    FT_ASSERT( stream == face->root.stream );
+    FT_ASSERT( FT_STREAM_POS() == 0 );
+
+    if ( FT_STREAM_READ_FIELDS( woff_header_fields, &woff ) )
+      return error;
+
+    /* Make sure we don't recurse back here or hit TTC code. */
+    if ( woff.flavor == TTAG_wOFF || woff.flavor == TTAG_ttcf )
+      return FT_THROW( Invalid_Table );
+
+    /* Miscellaneous checks. */
+    if ( woff.length != stream->size                              ||
+         woff.num_tables == 0                                     ||
+         44 + woff.num_tables * 20UL >= woff.length               ||
+         12 + woff.num_tables * 16UL >= woff.totalSfntSize        ||
+         ( woff.totalSfntSize & 3 ) != 0                          ||
+         ( woff.metaOffset == 0 && ( woff.metaLength != 0     ||
+                                     woff.metaOrigLength != 0 ) ) ||
+         ( woff.metaLength != 0 && woff.metaOrigLength == 0 )     ||
+         ( woff.privOffset == 0 && woff.privLength != 0 )         )
+    {
+      FT_ERROR(( "woff_font_open: invalid WOFF header\n" ));
+      return FT_THROW( Invalid_Table );
+    }
+
+    /* Don't trust `totalSfntSize' before thorough checks. */
+    if ( FT_ALLOC( sfnt, 12 + woff.num_tables * 16UL ) ||
+         FT_NEW( sfnt_stream )                         )
+      goto Exit;
+
+    sfnt_header = sfnt;
+
+    /* Write sfnt header. */
+    {
+      FT_UInt  searchRange, entrySelector, rangeShift, x;
+
+
+      x             = woff.num_tables;
+      entrySelector = 0;
+      while ( x )
+      {
+        x            >>= 1;
+        entrySelector += 1;
+      }
+      entrySelector--;
+
+      searchRange = ( 1 << entrySelector ) * 16;
+      rangeShift  = woff.num_tables * 16 - searchRange;
+
+      WRITE_ULONG ( sfnt_header, woff.flavor );
+      WRITE_USHORT( sfnt_header, woff.num_tables );
+      WRITE_USHORT( sfnt_header, searchRange );
+      WRITE_USHORT( sfnt_header, entrySelector );
+      WRITE_USHORT( sfnt_header, rangeShift );
+    }
+
+    /* While the entries in the sfnt header must be sorted by the */
+    /* tag value, the tables themselves are not.  We thus have to */
+    /* sort them by offset and check that they don't overlap.     */
+
+    if ( FT_NEW_ARRAY( tables, woff.num_tables )  ||
+         FT_NEW_ARRAY( indices, woff.num_tables ) )
+      goto Exit;
+
+    FT_TRACE2(( "\n"
+                "  tag    offset    compLen  origLen  checksum\n"
+                "  -------------------------------------------\n" ));
+
+    if ( FT_FRAME_ENTER( 20L * woff.num_tables ) )
+      goto Exit;
+
+    for ( nn = 0; nn < woff.num_tables; nn++ )
+    {
+      WOFF_Table  table = tables + nn;
+
+      table->Tag        = FT_GET_TAG4();
+      table->Offset     = FT_GET_ULONG();
+      table->CompLength = FT_GET_ULONG();
+      table->OrigLength = FT_GET_ULONG();
+      table->CheckSum   = FT_GET_ULONG();
+
+      FT_TRACE2(( "  %c%c%c%c  %08lx  %08lx  %08lx  %08lx\n",
+                  (FT_Char)( table->Tag >> 24 ),
+                  (FT_Char)( table->Tag >> 16 ),
+                  (FT_Char)( table->Tag >> 8  ),
+                  (FT_Char)( table->Tag       ),
+                  table->Offset,
+                  table->CompLength,
+                  table->OrigLength,
+                  table->CheckSum ));
+
+      if ( table->Tag <= old_tag )
+      {
+        FT_FRAME_EXIT();
+
+        FT_ERROR(( "woff_font_open: table tags are not sorted\n" ));
+        error = FT_THROW( Invalid_Table );
+        goto Exit;
+      }
+
+      old_tag     = table->Tag;
+      indices[nn] = table;
+    }
+
+    FT_FRAME_EXIT();
+
+    /* Sort by offset. */
+
+    ft_qsort( indices,
+              woff.num_tables,
+              sizeof ( WOFF_Table ),
+              compare_offsets );
+
+    /* Check offsets and lengths. */
+
+    woff_offset = 44 + woff.num_tables * 20L;
+    sfnt_offset = 12 + woff.num_tables * 16L;
+
+    for ( nn = 0; nn < woff.num_tables; nn++ )
+    {
+      WOFF_Table  table = indices[nn];
+
+
+      if ( table->Offset != woff_offset                         ||
+           table->CompLength > woff.length                      ||
+           table->Offset > woff.length - table->CompLength      ||
+           table->OrigLength > woff.totalSfntSize               ||
+           sfnt_offset > woff.totalSfntSize - table->OrigLength ||
+           table->CompLength > table->OrigLength                )
+      {
+        FT_ERROR(( "woff_font_open: invalid table offsets\n" ));
+        error = FT_THROW( Invalid_Table );
+        goto Exit;
+      }
+
+      table->OrigOffset = sfnt_offset;
+
+      /* The offsets must be multiples of 4. */
+      woff_offset += ( table->CompLength + 3 ) & ~3U;
+      sfnt_offset += ( table->OrigLength + 3 ) & ~3U;
+    }
+
+    /*
+     * Final checks!
+     *
+     * We don't decode and check the metadata block.
+     * We don't check table checksums either.
+     * But other than those, I think we implement all
+     * `MUST' checks from the spec.
+     */
+
+    if ( woff.metaOffset )
+    {
+      if ( woff.metaOffset != woff_offset                  ||
+           woff.metaOffset + woff.metaLength > woff.length )
+      {
+        FT_ERROR(( "woff_font_open:"
+                   " invalid `metadata' offset or length\n" ));
+        error = FT_THROW( Invalid_Table );
+        goto Exit;
+      }
+
+      /* We have padding only ... */
+      woff_offset += woff.metaLength;
+    }
+
+    if ( woff.privOffset )
+    {
+      /* ... if it isn't the last block. */
+      woff_offset = ( woff_offset + 3 ) & ~3U;
+
+      if ( woff.privOffset != woff_offset                  ||
+           woff.privOffset + woff.privLength > woff.length )
+      {
+        FT_ERROR(( "woff_font_open: invalid `private' offset or length\n" ));
+        error = FT_THROW( Invalid_Table );
+        goto Exit;
+      }
+
+      /* No padding for the last block. */
+      woff_offset += woff.privLength;
+    }
+
+    if ( sfnt_offset != woff.totalSfntSize ||
+         woff_offset != woff.length        )
+    {
+      FT_ERROR(( "woff_font_open: invalid `sfnt' table structure\n" ));
+      error = FT_THROW( Invalid_Table );
+      goto Exit;
+    }
+
+    /* Now use `totalSfntSize'. */
+    if ( FT_REALLOC( sfnt,
+                     12 + woff.num_tables * 16UL,
+                     woff.totalSfntSize ) )
+      goto Exit;
+
+    sfnt_header = sfnt + 12;
+
+    /* Write the tables. */
+
+    for ( nn = 0; nn < woff.num_tables; nn++ )
+    {
+      WOFF_Table  table = tables + nn;
+
+
+      /* Write SFNT table entry. */
+      WRITE_ULONG( sfnt_header, table->Tag );
+      WRITE_ULONG( sfnt_header, table->CheckSum );
+      WRITE_ULONG( sfnt_header, table->OrigOffset );
+      WRITE_ULONG( sfnt_header, table->OrigLength );
+
+      /* Write table data. */
+      if ( FT_STREAM_SEEK( table->Offset )     ||
+           FT_FRAME_ENTER( table->CompLength ) )
+        goto Exit;
+
+      if ( table->CompLength == table->OrigLength )
+      {
+        /* Uncompressed data; just copy. */
+        ft_memcpy( sfnt + table->OrigOffset,
+                   stream->cursor,
+                   table->OrigLength );
+      }
+      else
+      {
+#ifdef FT_CONFIG_OPTION_USE_ZLIB
+
+        /* Uncompress with zlib. */
+        FT_ULong  output_len = table->OrigLength;
+
+
+        error = FT_Gzip_Uncompress( memory,
+                                    sfnt + table->OrigOffset, &output_len,
+                                    stream->cursor, table->CompLength );
+        if ( error )
+          goto Exit;
+        if ( output_len != table->OrigLength )
+        {
+          FT_ERROR(( "woff_font_open: compressed table length mismatch\n" ));
+          error = FT_THROW( Invalid_Table );
+          goto Exit;
+        }
+
+#else /* !FT_CONFIG_OPTION_USE_ZLIB */
+
+        error = FT_THROW( Unimplemented_Feature );
+        goto Exit;
+
+#endif /* !FT_CONFIG_OPTION_USE_ZLIB */
+      }
+
+      FT_FRAME_EXIT();
+
+      /* We don't check whether the padding bytes in the WOFF file are     */
+      /* actually '\0'.  For the output, however, we do set them properly. */
+      sfnt_offset = table->OrigOffset + table->OrigLength;
+      while ( sfnt_offset & 3 )
+      {
+        sfnt[sfnt_offset] = '\0';
+        sfnt_offset++;
+      }
+    }
+
+    /* Ok!  Finally ready.  Swap out stream and return. */
+    FT_Stream_OpenMemory( sfnt_stream, sfnt, woff.totalSfntSize );
+    sfnt_stream->memory = stream->memory;
+    sfnt_stream->close  = sfnt_stream_close;
+
+    FT_Stream_Free(
+      face->root.stream,
+      ( face->root.face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
+
+    face->root.stream = sfnt_stream;
+
+    face->root.face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
+
+  Exit:
+    FT_FREE( tables );
+    FT_FREE( indices );
+
+    if ( error )
+    {
+      FT_FREE( sfnt );
+      FT_Stream_Close( sfnt_stream );
+      FT_FREE( sfnt_stream );
+    }
+
+    return error;
+  }
+
+
+#undef WRITE_USHORT
+#undef WRITE_ULONG
+
+
+/* END */
diff --git a/src/sfnt/sfwoff.h b/src/sfnt/sfwoff.h
new file mode 100644
index 0000000..15495c3
--- /dev/null
+++ b/src/sfnt/sfwoff.h
@@ -0,0 +1,41 @@
+/****************************************************************************
+ *
+ * sfwoff.h
+ *
+ *   WOFFF format management (specification).
+ *
+ * Copyright (C) 1996-2019 by
+ * David Turner, Robert Wilhelm, and Werner Lemberg.
+ *
+ * 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 SFWOFF_H_
+#define SFWOFF_H_
+
+
+#include <ft2build.h>
+#include FT_INTERNAL_SFNT_H
+#include FT_INTERNAL_OBJECTS_H
+
+
+FT_BEGIN_HEADER
+
+
+  FT_LOCAL( FT_Error )
+  woff_open_font( FT_Stream  stream,
+                  TT_Face    face );
+
+
+FT_END_HEADER
+
+#endif /* SFWOFF_H_ */
+
+
+/* END */