1 /* sfnt format font support for GNU Emacs.
2
3 Copyright (C) 2023 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #ifndef _SFNT_H_
22 #define _SFNT_H_
23
24 #include <stdint.h>
25 #include <stddef.h>
26 #include <setjmp.h>
27
28 #include <sys/types.h>
29
30
31
32 /* Container structure and enumerator definitions. */
33
34 /* The sfnt container format is organized into different tables, such
35 as ``cmap'' or ``glyf''. Each of these tables has a specific
36 format and use. These are all the tables known to Emacs. */
37
38 enum sfnt_table
39 {
40 SFNT_TABLE_CMAP,
41 SFNT_TABLE_GLYF,
42 SFNT_TABLE_HEAD,
43 SFNT_TABLE_HHEA,
44 SFNT_TABLE_HMTX,
45 SFNT_TABLE_LOCA,
46 SFNT_TABLE_MAXP,
47 SFNT_TABLE_NAME,
48 SFNT_TABLE_META,
49 SFNT_TABLE_CVT ,
50 SFNT_TABLE_FPGM,
51 SFNT_TABLE_PREP,
52 SFNT_TABLE_FVAR,
53 SFNT_TABLE_GVAR,
54 SFNT_TABLE_CVAR,
55 SFNT_TABLE_AVAR,
56 };
57
58 #define SFNT_ENDOF(type, field, type1) \
59 ((size_t) offsetof (type, field) + sizeof (type1))
60
61 /* Each of these structures must be aligned so that no compiler will
62 ever generate padding bytes on platforms where the alignment
63 requirements for uint32_t and uint16_t are no larger than 4 and 2
64 bytes respectively.
65
66 Pointer types are assumed to impose an alignmnent requirement no
67 less than that of uint32_t.
68
69 If a table has more than one kind of variable-length subtable array
70 at the end, make sure to pad subsequent subtables
71 appropriately. */
72
73 struct sfnt_offset_subtable
74 {
75 /* The scaler type. */
76 uint32_t scaler_type;
77
78 /* The number of tables. */
79 uint16_t num_tables;
80
81 /* (Maximum power of 2 <= numTables) * 16. */
82 uint16_t search_range;
83
84 /* log2 (maximum power of 2 <= numTables) */
85 uint16_t entry_selector;
86
87 /* numTables * 16 - searchRange. */
88 uint16_t range_shift;
89
90 /* Variable length data. */
91 struct sfnt_table_directory *subtables;
92 };
93
94 /* The table directory. Follows the offset subtable, with one for
95 each table. */
96
97 struct sfnt_table_directory
98 {
99 /* 4-byte identifier for each table. See sfnt_table_names. */
100 uint32_t tag;
101
102 /* Table checksum. */
103 uint32_t checksum;
104
105 /* Offset from the start of the file. */
106 uint32_t offset;
107
108 /* Length of the table in bytes, not subject to padding. */
109 uint32_t length;
110 };
111
112 enum sfnt_scaler_type
113 {
114 SFNT_SCALER_TRUE = 0x74727565,
115 SFNT_SCALER_VER1 = 0x00010000,
116 SFNT_SCALER_TYP1 = 0x74797031,
117 SFNT_SCALER_OTTO = 0x4F54544F,
118 };
119
120 typedef int32_t sfnt_fixed;
121 typedef int16_t sfnt_fword;
122 typedef uint16_t sfnt_ufword;
123
124 #define sfnt_coerce_fixed(fixed) ((sfnt_fixed) (fixed) / 65535.0)
125
126 typedef unsigned int sfnt_glyph;
127 typedef unsigned int sfnt_char;
128
129 struct sfnt_head_table
130 {
131 /* The version. This is a 16.16 fixed point number. */
132 sfnt_fixed version;
133
134 /* The revision. */
135 sfnt_fixed revision;
136
137 /* Checksum adjustment. */
138 uint32_t checksum_adjustment;
139
140 /* Magic number, should be 0x5F0F3CF5. */
141 uint32_t magic;
142
143 /* Flags for the font. */
144 uint16_t flags;
145
146 /* Units per em. */
147 uint16_t units_per_em;
148
149 /* Time of creation. */
150 uint32_t created_high, created_low;
151
152 /* Time of modification. */
153 uint32_t modified_high, modified_low;
154
155 /* Minimum bounds. */
156 sfnt_fword xmin, ymin, xmax, ymax;
157
158 /* Mac specific stuff. */
159 uint16_t mac_style;
160
161 /* Smallest readable size in pixels. */
162 uint16_t lowest_rec_ppem;
163
164 /* Font direction hint. */
165 int16_t font_direction_hint;
166
167 /* Index to loc format. 0 for short offsets, 1 for long. */
168 int16_t index_to_loc_format;
169
170 /* Unused. */
171 int16_t glyph_data_format;
172 };
173
174 struct sfnt_hhea_table
175 {
176 /* The version. This is a 16.16 fixed point number. */
177 sfnt_fixed version;
178
179 /* The maximum ascent and descent values for this font. */
180 sfnt_fword ascent, descent;
181
182 /* The typographic line gap. */
183 sfnt_fword line_gap;
184
185 /* The maximum advance width. */
186 sfnt_ufword advance_width_max;
187
188 /* The minimum bearings on either side. */
189 sfnt_fword min_left_side_bearing, min_right_side_bearing;
190
191 /* The maximum extent. */
192 sfnt_fword x_max_extent;
193
194 /* Caret slope. */
195 int16_t caret_slope_rise, caret_slope_run;
196
197 /* Caret offset for non slanted fonts. */
198 sfnt_fword caret_offset;
199
200 /* Reserved values. */
201 int16_t reserved1, reserved2, reserved3, reserved4;
202
203 /* Should always be zero. */
204 int16_t metric_data_format;
205
206 /* Number of advanced widths in metrics table. */
207 uint16_t num_of_long_hor_metrics;
208 };
209
210 struct sfnt_cmap_table
211 {
212 /* Should be zero. */
213 uint16_t version;
214
215 /* Number of subtables. */
216 uint16_t num_subtables;
217 };
218
219 enum sfnt_platform_id
220 {
221 SFNT_PLATFORM_UNICODE = 0,
222 SFNT_PLATFORM_MACINTOSH = 1,
223 SFNT_PLATFORM_RESERVED = 2,
224 SFNT_PLATFORM_MICROSOFT = 3,
225 };
226
227 enum sfnt_unicode_platform_specific_id
228 {
229 SFNT_UNICODE_1_0 = 0,
230 SFNT_UNICODE_1_1 = 1,
231 SFNT_UNICODE_ISO_10646_1993 = 2,
232 SFNT_UNICODE_2_0_BMP = 3,
233 SFNT_UNICODE_2_0 = 4,
234 SFNT_UNICODE_VARIATION_SEQUENCES = 5,
235 SFNT_UNICODE_LAST_RESORT = 6,
236 };
237
238 enum sfnt_macintosh_platform_specific_id
239 {
240 SFNT_MACINTOSH_ROMAN = 0,
241 SFNT_MACINTOSH_JAPANESE = 1,
242 SFNT_MACINTOSH_TRADITIONAL_CHINESE = 2,
243 SFNT_MACINTOSH_KOREAN = 3,
244 SFNT_MACINTOSH_ARABIC = 4,
245 SFNT_MACINTOSH_HEBREW = 5,
246 SFNT_MACINTOSH_GREEK = 6,
247 SFNT_MACINTOSH_RUSSIAN = 7,
248 SFNT_MACINTOSH_RSYMBOL = 8,
249 SFNT_MACINTOSH_DEVANGARI = 9,
250 SFNT_MACINTOSH_GURMUKHI = 10,
251 SFNT_MACINTOSH_GUJARATI = 11,
252 SFNT_MACINTOSH_ORIYA = 12,
253 SFNT_MACINTOSH_BENGALI = 13,
254 SFNT_MACINTOSH_TAMIL = 14,
255 SFNT_MACINTOSH_TELUGU = 15,
256 SFNT_MACINTOSH_KANNADA = 16,
257 SFNT_MACINTOSH_MALAYALAM = 17,
258 SFNT_MACINTOSH_SINHALESE = 18,
259 SFNT_MACINTOSH_BURMESE = 19,
260 SFNT_MACINTOSH_KHMER = 20,
261 SFNT_MACINTOSH_THAI = 21,
262 SFNT_MACINTOSH_LAOTIAN = 22,
263 SFNT_MACINTOSH_GEORGIAN = 23,
264 SFNT_MACINTOSH_ARMENIAN = 24,
265 SFNT_MACINTOSH_SIMPLIFIED_CHINESE = 25,
266 SFNT_MACINTOSH_TIBETIAN = 26,
267 SFNT_MACINTOSH_MONGOLIAN = 27,
268 SFNT_MACINTOSH_GEEZ = 28,
269 SFNT_MACINTOSH_SLAVIC = 29,
270 SFNT_MACINTOSH_VIETNAMESE = 30,
271 SFNT_MACINTOSH_SINDHI = 31,
272 SFNT_MACINTOSH_UNINTERPRETED = 32,
273 };
274
275 enum sfnt_microsoft_platform_specific_id
276 {
277 SFNT_MICROSOFT_SYMBOL = 0,
278 SFNT_MICROSOFT_UNICODE_BMP = 1,
279 SFNT_MICROSOFT_SHIFT_JIS = 2,
280 SFNT_MICROSOFT_PRC = 3,
281 SFNT_MICROSOFT_BIG_FIVE = 4,
282 SFNT_MICROSOFT_WANSUNG = 5,
283 SFNT_MICROSOFT_JOHAB = 6,
284 SFNT_MICROSOFT_UNICODE_UCS_4 = 10,
285 };
286
287 struct sfnt_cmap_encoding_subtable
288 {
289 /* The platform ID. */
290 uint16_t platform_id;
291
292 /* Platform specific ID. */
293 uint16_t platform_specific_id;
294
295 /* Mapping table offset. */
296 uint32_t offset;
297 };
298
299 struct sfnt_cmap_encoding_subtable_data
300 {
301 /* Format and possibly the length in bytes. */
302 uint16_t format, length;
303 };
304
305 struct sfnt_cmap_format_0
306 {
307 /* Format, set to 0. */
308 uint16_t format;
309
310 /* Length in bytes. Should be 262. */
311 uint16_t length;
312
313 /* Language code. */
314 uint16_t language;
315
316 /* Character code to glyph index map. */
317 uint8_t glyph_index_array[256];
318 };
319
320 struct sfnt_cmap_format_2_subheader
321 {
322 uint16_t first_code;
323 uint16_t entry_count;
324 int16_t id_delta;
325 uint16_t id_range_offset;
326 };
327
328 struct sfnt_cmap_format_2
329 {
330 /* Format, set to 2. */
331 uint16_t format;
332
333 /* Length in bytes. */
334 uint16_t length;
335
336 /* Language code. */
337 uint16_t language;
338
339 /* Array mapping high bytes to subheaders. */
340 uint16_t sub_header_keys[256];
341
342 /* Variable length data. */
343 struct sfnt_cmap_format_2_subheader *subheaders;
344 uint16_t *glyph_index_array;
345 uint16_t num_glyphs;
346 };
347
348 struct sfnt_cmap_format_4
349 {
350 /* Format, set to 4. */
351 uint16_t format;
352
353 /* Length in bytes. */
354 uint16_t length;
355
356 /* Language code. */
357 uint16_t language;
358
359 /* 2 * seg_count. */
360 uint16_t seg_count_x2;
361
362 /* 2 * (2**FLOOR(log2(segCount))) */
363 uint16_t search_range;
364
365 /* log2(searchRange/2) */
366 uint16_t entry_selector;
367
368 /* Variable-length data. */
369 uint16_t *end_code;
370 uint16_t *reserved_pad;
371 uint16_t *start_code;
372 int16_t *id_delta;
373 int16_t *id_range_offset;
374 uint16_t *glyph_index_array;
375
376 /* The number of elements in glyph_index_array. */
377 size_t glyph_index_size;
378 };
379
380 struct sfnt_cmap_format_6
381 {
382 /* Format, set to 6. */
383 uint16_t format;
384
385 /* Length in bytes. */
386 uint16_t length;
387
388 /* Language code. */
389 uint16_t language;
390
391 /* First character code in subrange. */
392 uint16_t first_code;
393
394 /* Number of character codes. */
395 uint16_t entry_count;
396
397 /* Variable-length data. */
398 uint16_t *glyph_index_array;
399 };
400
401 struct sfnt_cmap_format_8_or_12_group
402 {
403 uint32_t start_char_code;
404 uint32_t end_char_code;
405 uint32_t start_glyph_code;
406 };
407
408 struct sfnt_cmap_format_8
409 {
410 /* Format, set to 8. */
411 uint16_t format;
412
413 /* Reserved. */
414 uint16_t reserved;
415
416 /* Length in bytes. */
417 uint32_t length;
418
419 /* Language code. */
420 uint32_t language;
421
422 /* Tightly packed array of bits (8K bytes total) indicating whether
423 the particular 16-bit (index) value is the start of a 32-bit
424 character code. */
425 uint8_t is32[65536];
426
427 /* Number of groups. */
428 uint32_t num_groups;
429
430 /* Variable length data. */
431 struct sfnt_cmap_format_8_or_12_group *groups;
432 };
433
434 /* cmap formats 10, 13 unsupported. */
435
436 struct sfnt_cmap_format_12
437 {
438 /* Format, set to 12. */
439 uint16_t format;
440
441 /* Reserved. */
442 uint16_t reserved;
443
444 /* Length in bytes. */
445 uint32_t length;
446
447 /* Language code. */
448 uint32_t language;
449
450 /* Number of groups. */
451 uint32_t num_groups;
452
453 /* Variable length data. */
454 struct sfnt_cmap_format_8_or_12_group *groups;
455 };
456
457 struct sfnt_cmap_format_14
458 {
459 /* Format, set to 14. */
460 uint16_t format;
461
462 /* The length of the table in bytes. */
463 uint32_t length;
464
465 /* Number of variation selector records. */
466 uint16_t num_var_selector_records;
467
468 /* The offset of this table in the font file. */
469 off_t offset;
470
471 /* Variable length data. */
472 struct sfnt_variation_selector_record *records;
473 };
474
475 struct sfnt_variation_selector_record
476 {
477 /* 24-bit unsigned variation selector. */
478 unsigned int var_selector;
479
480 /* Offset to default UVS table. */
481 uint32_t default_uvs_offset;
482
483 /* Offset to non-default UVS table. */
484 uint32_t nondefault_uvs_offset;
485 };
486
487 struct sfnt_maxp_table
488 {
489 /* Table version. */
490 sfnt_fixed version;
491
492 /* The number of glyphs in this font - 1. Set at version 0.5 or
493 later. */
494 uint16_t num_glyphs;
495
496 /* These fields are only set in version 1.0 or later. Maximum
497 points in a non-composite glyph. */
498 uint16_t max_points;
499
500 /* Maximum contours in a non-composite glyph. */
501 uint16_t max_contours;
502
503 /* Maximum points in a composite glyph. */
504 uint16_t max_composite_points;
505
506 /* Maximum contours in a composite glyph. */
507 uint16_t max_composite_contours;
508
509 /* 1 if instructions do not use the twilight zone (Z0), or 2 if
510 instructions do use Z0; should be set to 2 in most cases. */
511 uint16_t max_zones;
512
513 /* Maximum points used in Z0. */
514 uint16_t max_twilight_points;
515
516 /* Number of Storage Area locations. */
517 uint16_t max_storage;
518
519 /* Number of FDEFs, equal to the highest function number + 1. */
520 uint16_t max_function_defs;
521
522 /* Number of IDEFs. */
523 uint16_t max_instruction_defs;
524
525 /* Maximum stack depth across Font Program ('fpgm' table), CVT
526 Program ('prep' table) and all glyph instructions (in the 'glyf'
527 table). */
528 uint16_t max_stack_elements;
529
530 /* Maximum byte count for glyph instructions. */
531 uint16_t max_size_of_instructions;
532
533 /* Maximum number of components referenced at ``top level'' for any
534 composite glyph. */
535 uint16_t max_component_elements;
536
537 /* Maximum levels of recursion; 1 for simple components. */
538 uint16_t max_component_depth;
539 };
540
541 struct sfnt_loca_table_short
542 {
543 /* Offsets to glyph data divided by two. */
544 uint16_t *offsets;
545
546 /* Size of the offsets list. */
547 size_t num_offsets;
548 };
549
550 struct sfnt_loca_table_long
551 {
552 /* Offsets to glyph data. */
553 uint32_t *offsets;
554
555 /* Size of the offsets list. */
556 size_t num_offsets;
557 };
558
559 struct sfnt_glyf_table
560 {
561 /* Size of the glyph data. */
562 size_t size;
563
564 /* Pointer to possibly unaligned glyph data. */
565 unsigned char *glyphs;
566
567 /* Pointer to the start of the mapping.
568 Only initialized if this table was mmapped. */
569 unsigned char *start;
570 };
571
572 struct sfnt_simple_glyph
573 {
574 /* The total number of points in this glyph. */
575 size_t number_of_points;
576
577 /* Array containing the last points of each contour. */
578 uint16_t *restrict end_pts_of_contours;
579
580 /* Total number of bytes needed for instructions. */
581 uint16_t instruction_length;
582
583 /* Instruction data. */
584 uint8_t *restrict instructions;
585
586 /* Array of flags. */
587 uint8_t *restrict flags;
588
589 /* Array of X coordinates. */
590 int16_t *restrict x_coordinates;
591
592 /* Array of Y coordinates. */
593 int16_t *restrict y_coordinates;
594
595 /* Pointer to the end of that array. */
596 int16_t *restrict y_coordinates_end;
597 };
598
599 struct sfnt_compound_glyph_component
600 {
601 /* Compound glyph flags. */
602 uint16_t flags;
603
604 /* Component glyph index. */
605 uint16_t glyph_index;
606
607 /* X-offset for component or point number; type depends on bits 0
608 and 1 in component flags. */
609 union {
610 uint8_t a;
611 int8_t b;
612 uint16_t c;
613 int16_t d;
614 } argument1;
615
616 /* Y-offset for component or point number; type depends on bits 0
617 and 1 in component flags. */
618 union {
619 uint8_t a;
620 int8_t b;
621 uint16_t c;
622 int16_t d;
623 } argument2;
624
625 /* Various scale formats. */
626 union {
627 uint16_t scale;
628 struct {
629 uint16_t xscale;
630 uint16_t yscale;
631 } a;
632 struct {
633 uint16_t xscale;
634 uint16_t scale01;
635 uint16_t scale10;
636 uint16_t yscale;
637 } b;
638 } u;
639 };
640
641 struct sfnt_compound_glyph
642 {
643 /* Pointer to array of components. */
644 struct sfnt_compound_glyph_component *components;
645
646 /* Number of elements in that array. */
647 size_t num_components;
648
649 /* Instruction data. */
650 uint8_t *instructions;
651
652 /* Length of instructions. */
653 uint16_t instruction_length;
654 };
655
656 struct sfnt_glyph
657 {
658 /* Number of contours in this glyph. */
659 int16_t number_of_contours;
660
661 /* Coordinate bounds. */
662 sfnt_fword xmin, ymin, xmax, ymax;
663
664 /* Distortion applied to the right side phantom point. */
665 sfnt_fword advance_distortion;
666
667 /* Distortion applied to the origin point. */
668 sfnt_fword origin_distortion;
669
670 /* Either a simple glyph or a compound glyph, depending on which is
671 set. */
672 struct sfnt_simple_glyph *simple;
673 struct sfnt_compound_glyph *compound;
674 };
675
676
677
678 /* Glyph outline decomposition. */
679
680 struct sfnt_point
681 {
682 /* X and Y in em space. */
683 sfnt_fixed x, y;
684 };
685
686 typedef void (*sfnt_move_to_proc) (struct sfnt_point, void *);
687 typedef void (*sfnt_line_to_proc) (struct sfnt_point, void *);
688 typedef void (*sfnt_curve_to_proc) (struct sfnt_point,
689 struct sfnt_point,
690 void *);
691
692 typedef struct sfnt_glyph *(*sfnt_get_glyph_proc) (sfnt_glyph, void *,
693 bool *);
694 typedef void (*sfnt_free_glyph_proc) (struct sfnt_glyph *, void *);
695
696
697
698 /* Decomposed glyph outline. */
699
700 struct sfnt_glyph_outline_command
701 {
702 /* Flags for this outline command. */
703 int flags;
704
705 /* X and Y position of this command. */
706 sfnt_fixed x, y;
707 };
708
709 /* Structure describing a single recorded outline in fixed pixel
710 space. */
711
712 struct sfnt_glyph_outline
713 {
714 /* Array of outlines elements. */
715 struct sfnt_glyph_outline_command *outline;
716
717 /* Size of the outline data, and how much is full. */
718 size_t outline_size, outline_used;
719
720 /* Rectangle defining bounds of the outline. Namely, the minimum
721 and maximum X and Y positions. */
722 sfnt_fixed xmin, ymin, xmax, ymax;
723
724 /* The origin point of the outline on the X axis. Value defaults to
725 0. */
726 sfnt_fixed origin;
727
728 /* Reference count. Initially zero. */
729 short refcount;
730 };
731
732 enum sfnt_glyph_outline_flags
733 {
734 SFNT_GLYPH_OUTLINE_LINETO = (1 << 1),
735 };
736
737
738
739 /* Glyph rasterization. */
740
741 struct sfnt_raster
742 {
743 /* Pointer to coverage data. */
744 unsigned char *cells;
745
746 /* Basic dimensions of the raster. */
747 unsigned short width, height;
748
749 /* Integer offset to apply to positions in the raster so that they
750 start from the origin point of the glyph. */
751 short offx, offy;
752
753 /* The raster stride. */
754 unsigned short stride;
755
756 /* Reference count. Initially zero. */
757 unsigned short refcount;
758 };
759
760 struct sfnt_edge
761 {
762 /* Next edge in this chain. */
763 struct sfnt_edge *next;
764
765 /* Winding direction. 1 if clockwise, -1 if counterclockwise. */
766 int winding;
767
768 /* X position, top and bottom of edges. */
769 sfnt_fixed x, top, bottom;
770
771 /* Amount to move X by upon each change of Y. */
772 sfnt_fixed step_x;
773 };
774
775
776
777 /* Polygon rasterization constants. */
778
779 enum
780 {
781 SFNT_POLY_SHIFT = 3,
782 SFNT_POLY_SAMPLE = (1 << SFNT_POLY_SHIFT),
783 SFNT_POLY_MASK = (SFNT_POLY_SAMPLE - 1),
784 SFNT_POLY_STEP = (0x10000 >> SFNT_POLY_SHIFT),
785 SFNT_POLY_START = (SFNT_POLY_STEP >> 1),
786 };
787
788
789
790 /* Glyph metrics computation. */
791
792 struct sfnt_long_hor_metric
793 {
794 uint16_t advance_width;
795 int16_t left_side_bearing;
796 };
797
798 struct sfnt_hmtx_table
799 {
800 /* Array of horizontal metrics for each glyph. */
801 struct sfnt_long_hor_metric *h_metrics;
802
803 /* Lbearing for remaining glyphs. */
804 int16_t *left_side_bearing;
805 };
806
807 /* Structure describing the metrics of a single glyph. The fields
808 mean the same as in XCharStruct, except they are 16.16 fixed point
809 values, and are missing significant information. */
810
811 struct sfnt_glyph_metrics
812 {
813 /* Distance between origin and left edge of raster. Positive
814 changes move rightwards.
815
816 If sfnt_lookup_glyph_metrics is given a pixel size of -1,
817 this is actually a sign extended fword. */
818 sfnt_fixed lbearing;
819
820 /* Advance to next glyph's origin.
821
822 If sfnt_lookup_glyph_metrics is given a pixel size of -1, this is
823 actually a sign extended fword. */
824 sfnt_fixed advance;
825 };
826
827
828
829 /* Font style parsing. */
830
831 struct sfnt_name_record
832 {
833 /* Platform identifier code. */
834 uint16_t platform_id;
835
836 /* Platform specific ID. */
837 uint16_t platform_specific_id;
838
839 /* Language identifier. */
840 uint16_t language_id;
841
842 /* Name identifier. */
843 uint16_t name_id;
844
845 /* String length in bytes. */
846 uint16_t length;
847
848 /* Offset from start of storage area. */
849 uint16_t offset;
850 };
851
852 struct sfnt_name_table
853 {
854 /* Format selector of name table. */
855 uint16_t format;
856
857 /* Number of name records. */
858 uint16_t count;
859
860 /* Offset to start of string data. */
861 uint16_t string_offset;
862
863 /* Variable length data. */
864 struct sfnt_name_record *name_records;
865
866 /* Start of string data. */
867 unsigned char *data;
868 };
869
870 /* Name identifier codes. These are Apple's codes, not
871 Microsoft's. */
872
873 enum sfnt_name_identifier_code
874 {
875 SFNT_NAME_COPYRIGHT_NOTICE = 0,
876 SFNT_NAME_FONT_FAMILY = 1,
877 SFNT_NAME_FONT_SUBFAMILY = 2,
878 SFNT_NAME_UNIQUE_SUBFAMILY_IDENTIFICATION = 3,
879 SFNT_NAME_FULL_NAME = 4,
880 SFNT_NAME_NAME_TABLE_VERSION = 5,
881 SFNT_NAME_POSTSCRIPT_NAME = 6,
882 SFNT_NAME_TRADEMARK_NOTICE = 7,
883 SFNT_NAME_MANUFACTURER_NAME = 8,
884 SFNT_NAME_DESIGNER = 9,
885 SFNT_NAME_DESCRIPTION = 10,
886 SFNT_NAME_FONT_VENDOR_URL = 11,
887 SFNT_NAME_FONT_DESIGNER_URL = 12,
888 SFNT_NAME_LICENSE_DESCRIPTION = 13,
889 SFNT_NAME_LICENSE_INFORMATION_URL = 14,
890 SFNT_NAME_PREFERRED_FAMILY = 16,
891 SFNT_NAME_PREFERRED_SUBFAMILY = 17,
892 SFNT_NAME_COMPATIBLE_FULL = 18,
893 SFNT_NAME_SAMPLE_TEXT = 19,
894 SFNT_NAME_VARIATIONS_POSTSCRIPT_NAME_PREFIX = 25,
895 };
896
897 struct sfnt_meta_data_map
898 {
899 /* Identifier for the tag. */
900 uint32_t tag;
901
902 /* Offset from start of table to data. */
903 uint32_t data_offset;
904
905 /* Length of the data. */
906 uint32_t data_length;
907 };
908
909 struct sfnt_meta_table
910 {
911 /* Version of the table. Currently set to 1. */
912 uint32_t version;
913
914 /* Flags. Currently 0. */
915 uint32_t flags;
916
917 /* Offset from start of table to beginning of variable length
918 data. */
919 uint32_t data_offset;
920
921 /* Number of data maps in the table. */
922 uint32_t num_data_maps;
923
924 /* Beginning of variable length data. */
925 struct sfnt_meta_data_map *data_maps;
926
927 /* The whole table contents. */
928 unsigned char *data;
929 };
930
931 enum sfnt_meta_data_tag
932 {
933 SFNT_META_DATA_TAG_DLNG = 0x646c6e67,
934 SFNT_META_DATA_TAG_SLNG = 0x736c6e67,
935 };
936
937
938
939 /* TrueType collection format support. */
940
941 struct sfnt_ttc_header
942 {
943 /* TrueType collection ID tag. */
944 uint32_t ttctag;
945
946 /* Version of the TTC header. */
947 uint32_t version;
948
949 /* Number of fonts in the TTC header. */
950 uint32_t num_fonts;
951
952 /* Array of offsets to the offset table for each font in the
953 file. */
954 uint32_t *offset_table;
955
956 /* Tag indicating that a DSIG table exists, or 0. Fields from here
957 on are only set on version 2.0 headers or later. */
958 uint32_t ul_dsig_tag;
959
960 /* Length in bytes of the signature table, or 0 if there is no
961 signature. */
962 uint32_t ul_dsig_length;
963
964 /* Offset in bytes of the dsig table from the beginning of the TTC
965 file. */
966 uint32_t ul_dsig_offset;
967 };
968
969 enum sfnt_ttc_tag
970 {
971 SFNT_TTC_TTCF = 0x74746366,
972 SFNT_TTC_DSIG = 0x44534947,
973 };
974
975
976
977 /* Unicode Variation Sequence (UVS) support. */
978
979 struct sfnt_default_uvs_table
980 {
981 /* Number of ranges that follow. */
982 uint32_t num_unicode_value_ranges;
983
984 /* Variable length data. */
985 struct sfnt_unicode_value_range *ranges;
986 };
987
988 struct sfnt_unicode_value_range
989 {
990 /* First value in this range. */
991 unsigned int start_unicode_value;
992
993 /* Number of additional values in this range. */
994 unsigned char additional_count;
995 };
996
997 struct sfnt_nondefault_uvs_table
998 {
999 /* Number of UVS mappings which follow. */
1000 uint32_t num_uvs_mappings;
1001
1002 /* Variable length data. */
1003 struct sfnt_uvs_mapping *mappings;
1004 };
1005
1006 struct sfnt_uvs_mapping
1007 {
1008 /* Base character value. */
1009 unsigned int unicode_value;
1010
1011 /* Glyph ID of the base character value. */
1012 uint16_t base_character_value;
1013 };
1014
1015 struct sfnt_mapped_variation_selector_record
1016 {
1017 /* The variation selector. */
1018 unsigned int selector;
1019
1020 /* Its default UVS table. */
1021 struct sfnt_default_uvs_table *default_uvs;
1022
1023 /* Its nondefault UVS table. */
1024 struct sfnt_nondefault_uvs_table *nondefault_uvs;
1025 };
1026
1027 /* Structure describing a single offset to load into a variation
1028 selection context. */
1029
1030 struct sfnt_table_offset_rec
1031 {
1032 /* The offset from the start of the font file. */
1033 off_t offset;
1034
1035 /* Whether or not the offset points to a non-default UVS table. */
1036 bool is_nondefault_table;
1037
1038 /* Pointer to the UVS table. */
1039 void *table;
1040 };
1041
1042 struct sfnt_uvs_context
1043 {
1044 /* Number of records and tables. */
1045 size_t num_records, nmemb;
1046
1047 /* Array of UVS tables. */
1048 struct sfnt_table_offset_rec *tables;
1049
1050 /* Array of variation selector records mapped to
1051 their corresponding tables. */
1052 struct sfnt_mapped_variation_selector_record *records;
1053 };
1054
1055
1056
1057 #if defined HAVE_MMAP && !defined TEST
1058
1059 /* Memory mapping support. */
1060
1061 struct sfnt_mapped_table
1062 {
1063 /* Pointer to table data. */
1064 void *data;
1065
1066 /* Pointer to table mapping. */
1067 void *mapping;
1068
1069 /* Size of mapped data and size of mapping. */
1070 size_t length, size;
1071 };
1072
1073 #endif /* HAVE_MMAP && !TEST */
1074
1075
1076
1077 /* Glyph variation support. */
1078
1079 /* 2.14 fixed point type used to represent versors of unit
1080 vectors. */
1081 typedef int16_t sfnt_f2dot14;
1082
1083 /* Forward declaration used only for the distortable font stuff. */
1084 struct sfnt_cvt_table;
1085
1086 struct sfnt_variation_axis
1087 {
1088 /* The axis tag. */
1089 uint32_t axis_tag;
1090
1091 /* The minimum style coordinate for the axis. */
1092 sfnt_fixed min_value;
1093
1094 /* The default style coordinate for the axis. */
1095 sfnt_fixed default_value;
1096
1097 /* The maximum style coordinate for the axis. */
1098 sfnt_fixed max_value;
1099
1100 /* Set to zero. */
1101 uint16_t flags;
1102
1103 /* Identifier under which this axis's name will be found in the
1104 `name' table. */
1105 uint16_t name_id;
1106 };
1107
1108 struct sfnt_instance
1109 {
1110 /* The instance name ID. */
1111 uint16_t name_id;
1112
1113 /* Flags. */
1114 uint16_t flags;
1115
1116 /* Optional PostScript name. */
1117 uint16_t ps_name_id;
1118
1119 /* Coordinates of each defined instance. */
1120 sfnt_fixed *coords;
1121 };
1122
1123 struct sfnt_fvar_table
1124 {
1125 /* Major version; should be 1. */
1126 uint16_t major_version;
1127
1128 /* Minor version; should be 0. */
1129 uint16_t minor_version;
1130
1131 /* Offset in bytes from the beginning of the table to the beginning
1132 of the first axis data. */
1133 uint16_t offset_to_data;
1134
1135 /* Reserved field; always 2. */
1136 uint16_t count_size_pairs;
1137
1138 /* Number of style axes in this font. */
1139 uint16_t axis_count;
1140
1141 /* The number of bytes in each variation axis record. Currently 20
1142 bytes. */
1143 uint16_t axis_size;
1144
1145 /* The number of named instances for the font found in the
1146 instance array. */
1147 uint16_t instance_count;
1148
1149 /* The size of each instance record. */
1150 uint16_t instance_size;
1151
1152 /* Variable length data. */
1153 struct sfnt_variation_axis *axis;
1154 struct sfnt_instance *instance;
1155 };
1156
1157 struct sfnt_short_frac_correspondence
1158 {
1159 /* Value in normalized user space. */
1160 sfnt_f2dot14 from_coord;
1161
1162 /* Value in normalized axis space. */
1163 sfnt_f2dot14 to_coord;
1164 };
1165
1166 struct sfnt_short_frac_segment
1167 {
1168 /* The number of pairs for this axis. */
1169 uint16_t pair_count;
1170
1171 /* Variable length data. */
1172 struct sfnt_short_frac_correspondence *correspondence;
1173 };
1174
1175 struct sfnt_avar_table
1176 {
1177 /* The version of the table. Should be 1.0. */
1178 sfnt_fixed version;
1179
1180 /* Number of variation axes defined in this table.
1181 XXX: why is this signed? */
1182 int32_t axis_count;
1183
1184 /* Variable length data. */
1185 struct sfnt_short_frac_segment *segments;
1186 };
1187
1188 struct sfnt_tuple_variation
1189 {
1190 /* Tuple point numbers. */
1191 uint16_t *points;
1192
1193 /* Deltas. */
1194 sfnt_fword *deltas;
1195
1196 /* Tuple coordinates. One for each axis specified in the [gaf]var
1197 tables. */
1198 sfnt_f2dot14 *coordinates;
1199
1200 /* Intermediate start and end coordinates. */
1201 sfnt_f2dot14 *restrict intermediate_start;
1202
1203 /* Intermediate start and end coordinates. */
1204 sfnt_f2dot14 *restrict intermediate_end;
1205
1206 /* The number of points and deltas present.
1207
1208 UINT16_MAX and POINTS set to NULL means there are deltas for each
1209 CVT entry. */
1210 uint16_t num_points;
1211 };
1212
1213 struct sfnt_cvar_table
1214 {
1215 /* The version of this CVT variations table. */
1216 sfnt_fixed version;
1217
1218 /* Flags. */
1219 uint16_t tuple_count;
1220
1221 /* Offset from the beginning of the table to the tuple data. */
1222 uint16_t data_offset;
1223
1224 /* Variable length data. */
1225 struct sfnt_tuple_variation *variation;
1226 };
1227
1228 struct sfnt_gvar_table
1229 {
1230 /* Version of the glyph variations table. */
1231 uint16_t version;
1232
1233 /* Reserved, currently 0. */
1234 uint16_t reserved;
1235
1236 /* The number of style axes for this font. This must be the same
1237 number as axisCount in the 'fvar' table. */
1238 uint16_t axis_count;
1239
1240 /* The number of shared coordinates. */
1241 uint16_t shared_coord_count;
1242
1243 /* Byte offset from the beginning of this table to the list of
1244 shared style coordinates. */
1245 uint32_t offset_to_coord;
1246
1247 /* The number of glyphs in this font; this should match the number
1248 of the glyphs store elsewhere in the font. */
1249 uint16_t glyph_count;
1250
1251 /* Bit-field that gives the format of the offset array that
1252 follows. If the flag is 0, the type is uint16. If the flag is 1,
1253 the type is unit 32. */
1254 uint16_t flags;
1255
1256 /* Byte offset from the beginning of this table to the first glyph
1257 glyphVariationData. */
1258 uint32_t offset_to_data;
1259
1260 /* Number of bytes in the glyph variation data. */
1261 size_t data_size;
1262
1263 /* Byte offsets from the beginning of the glyphVariationData array
1264 to the glyphVariationData for each glyph in the font. The format
1265 of this field is set by the flags field. */
1266 union {
1267 uint16_t *offset_word;
1268 uint32_t *offset_long;
1269 } u;
1270
1271 /* Other variable length data. */
1272 sfnt_f2dot14 *global_coords;
1273 unsigned char *glyph_variation_data;
1274 };
1275
1276 /* Structure repesenting a set of axis coordinates and their
1277 normalized equivalents.
1278
1279 To use this structure, call
1280
1281 sfnt_init_blend (&blend, fvar, gvar)
1282
1283 on a `struct sfnt_blend *', with an appropriate fvar and gvar
1284 table.
1285
1286 Then, fill in blend.coords with the un-normalized coordinates,
1287 and call
1288
1289 sfnt_normalize_blend (&blend)
1290
1291 finally, call sfnt_vary_simple_glyph and related functions. */
1292
1293 struct sfnt_blend
1294 {
1295 /* The fvar table. This determines the number of elements in each
1296 of the arrays below. */
1297 struct sfnt_fvar_table *fvar;
1298
1299 /* The gvar table. This provides the glyph variation data. */
1300 struct sfnt_gvar_table *gvar;
1301
1302 /* The avar table. This provides adjustments to normalized axis
1303 values, and may be NULL. */
1304 struct sfnt_avar_table *avar;
1305
1306 /* The cvar table. This provides adjustments to CVT values, and may
1307 be NULL. */
1308 struct sfnt_cvar_table *cvar;
1309
1310 /* Un-normalized coordinates. */
1311 sfnt_fixed *coords;
1312
1313 /* Normalized coordinates. */
1314 sfnt_fixed *norm_coords;
1315 };
1316
1317 struct sfnt_metrics_distortion
1318 {
1319 /* Distortion applied to the origin point. */
1320 sfnt_fword origin;
1321
1322 /* Distortion applied to the advance point. */
1323 sfnt_fword advance;
1324 };
1325
1326
1327
1328 #define SFNT_CEIL_FIXED(fixed) (((fixed) + 0177777) & 037777600000)
1329 #define SFNT_FLOOR_FIXED(fixed) ((fixed) & 037777600000)
1330
1331
1332
1333 /* Function declarations. Keep these sorted by the order in which
1334 they appear in sfnt.c. Keep each line no longer than 80
1335 columns. */
1336
1337 #ifndef TEST
1338
1339 extern struct sfnt_offset_subtable *sfnt_read_table_directory (int);
1340
1341 #define PROTOTYPE \
1342 int, struct sfnt_offset_subtable *, \
1343 struct sfnt_cmap_encoding_subtable **, \
1344 struct sfnt_cmap_encoding_subtable_data ***
1345 extern struct sfnt_cmap_table *sfnt_read_cmap_table (PROTOTYPE);
1346 #undef PROTOTYPE
1347
1348 extern sfnt_glyph sfnt_lookup_glyph (sfnt_char,
1349 struct sfnt_cmap_encoding_subtable_data *);
1350
1351 #define PROTOTYPE int, struct sfnt_offset_subtable *
1352 extern struct sfnt_head_table *sfnt_read_head_table (PROTOTYPE);
1353 extern struct sfnt_hhea_table *sfnt_read_hhea_table (PROTOTYPE);
1354 extern struct sfnt_loca_table_short *sfnt_read_loca_table_short (PROTOTYPE);
1355 extern struct sfnt_loca_table_long *sfnt_read_loca_table_long (PROTOTYPE);
1356 extern struct sfnt_maxp_table *sfnt_read_maxp_table (PROTOTYPE);
1357 extern struct sfnt_glyf_table *sfnt_read_glyf_table (PROTOTYPE);
1358
1359 #ifdef HAVE_MMAP
1360 extern struct sfnt_glyf_table *sfnt_map_glyf_table (PROTOTYPE);
1361 extern int sfnt_unmap_glyf_table (struct sfnt_glyf_table *);
1362 #endif /* HAVE_MMAP */
1363 #undef PROTOTYPE
1364
1365 extern struct sfnt_glyph *sfnt_read_glyph (sfnt_glyph, struct sfnt_glyf_table *,
1366 struct sfnt_loca_table_short *,
1367 struct sfnt_loca_table_long *);
1368 extern void sfnt_free_glyph (struct sfnt_glyph *);
1369
1370 #define PROTOTYPE \
1371 struct sfnt_glyph *, \
1372 sfnt_fixed, \
1373 struct sfnt_glyph_metrics *, \
1374 sfnt_get_glyph_proc, \
1375 sfnt_free_glyph_proc, \
1376 void *
1377 extern struct sfnt_glyph_outline *sfnt_build_glyph_outline (PROTOTYPE);
1378 #undef PROTOTYPE
1379
1380 extern void sfnt_prepare_raster (struct sfnt_raster *,
1381 struct sfnt_glyph_outline *);
1382
1383 #define PROTOTYPE struct sfnt_glyph_outline *
1384 extern struct sfnt_raster *sfnt_raster_glyph_outline (PROTOTYPE);
1385 #undef PROTOTYPE
1386
1387 #define PROTOTYPE \
1388 int, \
1389 struct sfnt_offset_subtable *, \
1390 struct sfnt_hhea_table *, \
1391 struct sfnt_maxp_table *
1392 extern struct sfnt_hmtx_table *sfnt_read_hmtx_table (PROTOTYPE);
1393 #undef PROTOTYPE
1394
1395 extern int sfnt_lookup_glyph_metrics (sfnt_glyph, int,
1396 struct sfnt_glyph_metrics *,
1397 struct sfnt_hmtx_table *,
1398 struct sfnt_hhea_table *,
1399 struct sfnt_head_table *,
1400 struct sfnt_maxp_table *);
1401
1402 extern void sfnt_scale_metrics (struct sfnt_glyph_metrics *,
1403 sfnt_fixed);
1404 extern sfnt_fixed sfnt_get_scale (struct sfnt_head_table *, int);
1405
1406 #define PROTOTYPE int, struct sfnt_offset_subtable *
1407 extern struct sfnt_name_table *sfnt_read_name_table (PROTOTYPE);
1408 #undef PROTOTYPE
1409
1410 extern unsigned char *sfnt_find_name (struct sfnt_name_table *,
1411 enum sfnt_name_identifier_code,
1412 struct sfnt_name_record *);
1413
1414 #define PROTOTYPE int, struct sfnt_offset_subtable *
1415 extern struct sfnt_meta_table *sfnt_read_meta_table (PROTOTYPE);
1416 #undef PROTOTYPE
1417
1418 extern char *sfnt_find_metadata (struct sfnt_meta_table *,
1419 enum sfnt_meta_data_tag,
1420 struct sfnt_meta_data_map *);
1421
1422 extern struct sfnt_ttc_header *sfnt_read_ttc_header (int);
1423
1424
1425
1426 #define PROTOTYPE struct sfnt_cmap_format_14 *, int
1427
1428 extern struct sfnt_uvs_context *sfnt_create_uvs_context (PROTOTYPE);
1429
1430 #undef PROTOTYPE
1431
1432 extern void sfnt_free_uvs_context (struct sfnt_uvs_context *);
1433
1434 #define PROTOTYPE struct sfnt_nondefault_uvs_table *, sfnt_char
1435
1436 extern sfnt_glyph sfnt_variation_glyph_for_char (PROTOTYPE);
1437
1438 #undef PROTOTYPE
1439
1440
1441
1442 #ifdef HAVE_MMAP
1443
1444 extern int sfnt_map_table (int, struct sfnt_offset_subtable *,
1445 uint32_t, struct sfnt_mapped_table *);
1446 extern int sfnt_unmap_table (struct sfnt_mapped_table *);
1447
1448 #endif /* HAVE_MMAP */
1449
1450
1451
1452 extern void *sfnt_read_table (int, struct sfnt_offset_subtable *,
1453 uint32_t, size_t *);
1454
1455
1456
1457 #define PROTOTYPE int, struct sfnt_offset_subtable *
1458
1459 extern struct sfnt_fvar_table *sfnt_read_fvar_table (PROTOTYPE);
1460 extern struct sfnt_gvar_table *sfnt_read_gvar_table (PROTOTYPE);
1461 extern struct sfnt_avar_table *sfnt_read_avar_table (PROTOTYPE);
1462
1463 #undef PROTOTYPE
1464
1465 #define PROTOTYPE \
1466 int, \
1467 struct sfnt_offset_subtable *, \
1468 struct sfnt_fvar_table *, \
1469 struct sfnt_cvt_table *
1470
1471 extern struct sfnt_cvar_table *sfnt_read_cvar_table (PROTOTYPE);
1472
1473 #undef PROTOTYPE
1474
1475
1476
1477 extern void sfnt_init_blend (struct sfnt_blend *,
1478 struct sfnt_fvar_table *,
1479 struct sfnt_gvar_table *,
1480 struct sfnt_avar_table *,
1481 struct sfnt_cvar_table *);
1482 extern void sfnt_free_blend (struct sfnt_blend *);
1483 extern void sfnt_normalize_blend (struct sfnt_blend *);
1484
1485
1486
1487 extern int sfnt_vary_simple_glyph (struct sfnt_blend *, sfnt_glyph,
1488 struct sfnt_glyph *,
1489 struct sfnt_metrics_distortion *);
1490 extern int sfnt_vary_compound_glyph (struct sfnt_blend *, sfnt_glyph,
1491 struct sfnt_glyph *,
1492 struct sfnt_metrics_distortion *);
1493
1494 #endif /* TEST */
1495
1496
1497
1498 /* TrueType hinting support. */
1499
1500 /* Structure definitions for tables used by the TrueType
1501 interpreter. */
1502
1503 struct sfnt_cvt_table
1504 {
1505 /* Number of elements in the control value table. */
1506 size_t num_elements;
1507
1508 /* Pointer to elements in the control value table. */
1509 sfnt_fword *values;
1510 };
1511
1512 struct sfnt_fpgm_table
1513 {
1514 /* Number of instructions in the font program table. */
1515 size_t num_instructions;
1516
1517 /* Pointer to elements in the font program table. */
1518 unsigned char *instructions;
1519 };
1520
1521 struct sfnt_prep_table
1522 {
1523 /* Number of instructions in the control value program (pre-program)
1524 table. */
1525 size_t num_instructions;
1526
1527 /* Pointer to elements in the preprogram table. */
1528 unsigned char *instructions;
1529 };
1530
1531
1532
1533 /* Fixed point types used by the TrueType interpreter. */
1534
1535 /* 26.6 fixed point type used within the interpreter. */
1536 typedef int32_t sfnt_f26dot6;
1537
1538 /* 18.14 fixed point type used to calculate rounding details. */
1539 typedef int32_t sfnt_f18dot14;
1540
1541
1542
1543 /* Interpreter execution environment. */
1544
1545 struct sfnt_unit_vector
1546 {
1547 /* X and Y versors of the 2d unit vector. */
1548 sfnt_f2dot14 x, y;
1549 };
1550
1551 struct sfnt_interpreter_definition
1552 {
1553 /* The opcode of this instruction or function. */
1554 uint16_t opcode;
1555
1556 /* The number of instructions. */
1557 uint16_t instruction_count;
1558
1559 /* Pointer to instructions belonging to the definition. This
1560 pointer points directly into the control value or font program.
1561 Make sure both programs are kept around as long as the
1562 interpreter continues to exist. */
1563 unsigned char *instructions;
1564 };
1565
1566 /* This structure represents a ``struct sfnt_glyph'' that has been
1567 scaled to a given pixel size.
1568
1569 It can either contain a simple glyph, or a decomposed compound
1570 glyph; instructions are interpreted for both simple glyphs, simple
1571 glyph components inside a compound glyph, and compound glyphs as a
1572 whole.
1573
1574 In addition to the glyph data itself, it also records various
1575 information for the instruction interpretation process:
1576
1577 - ``current'' point coordinates, which have been modified
1578 by the instructing process.
1579
1580 - two phantom points at the origin and the advance of the
1581 glyph. */
1582
1583 struct sfnt_interpreter_zone
1584 {
1585 /* The number of points in this zone, including the two phantom
1586 points at the end. */
1587 size_t num_points;
1588
1589 /* The number of contours in this zone. */
1590 size_t num_contours;
1591
1592 /* The end points of each contour. */
1593 size_t *contour_end_points;
1594
1595 /* Pointer to the X axis point data. */
1596 sfnt_f26dot6 *restrict x_points;
1597
1598 /* Pointer to the X axis current point data. */
1599 sfnt_f26dot6 *restrict x_current;
1600
1601 /* Pointer to the Y axis point data. */
1602 sfnt_f26dot6 *restrict y_points;
1603
1604 /* Pointer to the Y axis current point data. */
1605 sfnt_f26dot6 *restrict y_current;
1606
1607 /* Pointer to the flags associated with this data. */
1608 unsigned char *flags;
1609 };
1610
1611 enum
1612 {
1613 /* Bits 1 stands for X_SHORT_VECTOR on disk and in the tables, but
1614 this representation is not useful in memory. Inside an
1615 instructed glyph, this bit is repurposed to mean that the
1616 corresponding point is a phantom point. */
1617 SFNT_POINT_PHANTOM = (1 << 1),
1618 /* Bits 7 and 6 of a glyph point's flags is reserved. This scaler
1619 uses it to mean that the point has been touched in one axis or
1620 another. */
1621 SFNT_POINT_TOUCHED_X = (1 << 7),
1622 SFNT_POINT_TOUCHED_Y = (1 << 6),
1623 SFNT_POINT_TOUCHED_BOTH = (SFNT_POINT_TOUCHED_X
1624 | SFNT_POINT_TOUCHED_Y),
1625 };
1626
1627 /* This is needed because `round' below needs an interpreter
1628 argument. */
1629 struct sfnt_interpreter;
1630
1631 struct sfnt_graphics_state
1632 {
1633 /* Pointer to the function used for rounding. This function is
1634 asymmetric, so -0.5 rounds up to 0, not -1. It is up to the
1635 caller to handle negative values.
1636
1637 Value is undefined unless sfnt_validate_gs has been called, and
1638 the second argument may be used to provide detailed rounding
1639 information (``super rounding state''.) */
1640 sfnt_f26dot6 (*round) (sfnt_f26dot6, struct sfnt_interpreter *);
1641
1642 /* Pointer to the function used to project euclidean vectors onto
1643 the projection vector. Value is the magnitude of the projected
1644 vector. */
1645 sfnt_f26dot6 (*project) (sfnt_f26dot6, sfnt_f26dot6,
1646 struct sfnt_interpreter *);
1647
1648 /* Pointer to the function used to project euclidean vectors onto
1649 the dual projection vector. Value is the magnitude of the
1650 projected vector. */
1651 sfnt_f26dot6 (*dual_project) (sfnt_f26dot6, sfnt_f26dot6,
1652 struct sfnt_interpreter *);
1653
1654 /* Pointer to the function used to move specified points
1655 along the freedom vector by a distance specified in terms
1656 of the projection vector. */
1657 void (*move) (sfnt_f26dot6 *restrict,
1658 sfnt_f26dot6 *restrict, size_t,
1659 struct sfnt_interpreter *,
1660 sfnt_f26dot6, unsigned char *);
1661
1662 /* Dot product between the freedom and the projection vectors. */
1663 sfnt_f2dot14 vector_dot_product;
1664
1665 /* Controls whether the sign of control value table entries will be
1666 changed to match the sign of the actual distance measurement with
1667 which it is compared. Setting auto flip to TRUE makes it
1668 possible to control distances measured with or against the
1669 projection vector with a single control value table entry. When
1670 auto flip is set to FALSE, distances must be measured with the
1671 projection vector. */
1672 bool auto_flip;
1673
1674 /* Limits the regularizing effects of control value table entries to
1675 cases where the difference between the table value and the
1676 measurement taken from the original outline is sufficiently
1677 small. */
1678 sfnt_f26dot6 cvt_cut_in;
1679
1680 /* Establishes the base value used to calculate the range of point
1681 sizes to which a given DELTAC[] or DELTAP[] instruction will
1682 apply. The formulas given below are used to calculate the range
1683 of the various DELTA instructions.
1684
1685 DELTAC1 DELTAP1 (delta_base) through (delta_base + 15)
1686 DELTAC2 DELTAP2 (delta_base + 16) through (delta_base + 31)
1687 DELTAC3 DELTAP3 (delta_base + 32) through (delta_base + 47)
1688
1689 Please keep this documentation in sync with the TrueType
1690 reference manual. */
1691 unsigned short delta_base;
1692
1693 /* Determines the range of movement and smallest magnitude of
1694 movement (the step) in a DELTAC[] or DELTAP[] instruction.
1695 Changing the value of the delta shift makes it possible to trade
1696 off fine control of point movement for range of movement. A low
1697 delta shift favors range of movement over fine control. A high
1698 delta shift favors fine control over range of movement. The step
1699 has the value 1/2 to the power delta shift. The range of
1700 movement is calculated by taking the number of steps allowed (16)
1701 and multiplying it by the step.
1702
1703 The legal range for delta shift is zero through six. Negative
1704 values are illegal. */
1705 unsigned short delta_shift;
1706
1707 /* A second projection vector set to a line defined by the original
1708 outline location of two points. The dual projection vector is
1709 used when it is necessary to measure distances from the scaled
1710 outline before any instructions were executed. */
1711 struct sfnt_unit_vector dual_projection_vector;
1712
1713 /* A unit vector that establishes an axis along which points can
1714 move. */
1715 struct sfnt_unit_vector freedom_vector;
1716
1717 /* Makes it possible to turn off instructions under some
1718 circumstances. When flag 1 is set, changes to the graphics state
1719 made in the control value program will be ignored. When flag is
1720 1, grid fitting instructions will be ignored. */
1721 unsigned char instruct_control;
1722
1723 /* Makes it possible to repeat certain instructions a designated
1724 number of times. The default value of one assures that unless
1725 the value of loop is altered, these instructions will execute one
1726 time. */
1727 unsigned short loop;
1728
1729 /* Establishes the smallest possible value to which a distance will
1730 be rounded. */
1731 sfnt_f26dot6 minimum_distance;
1732
1733 /* A unit vector whose direction establishes an axis along which
1734 distances are measured. */
1735 struct sfnt_unit_vector projection_vector;
1736
1737 /* Determines the manner in which values are rounded. Can be set to
1738 a number of predefined states or to a customized state with the
1739 SROUND or S45ROUND instructions. */
1740 int round_state;
1741
1742 /* Reference points. These reference point numbers, which together
1743 with a zone designation, specify a point in either the glyph zone
1744 or the twilight zone. */
1745 uint16_t rp0, rp1, rp2;
1746
1747 /* Flags which determine whether the interpreter will activate
1748 dropout control for the current glyph. */
1749 int scan_control;
1750
1751 /* The distance difference below which the interpreter will replace
1752 a CVT distance or an actual distance in favor of the single width
1753 value. */
1754 sfnt_f26dot6 sw_cut_in;
1755
1756 /* The value used in place of the control value table distance or
1757 the actual distance value when the difference between that
1758 distance and the single width value is less than the single width
1759 cut-in. */
1760 sfnt_f26dot6 single_width_value;
1761
1762 /* Zone pointers, which reference a zone. */
1763 int zp0, zp1, zp2;
1764 };
1765
1766 struct sfnt_interpreter
1767 {
1768 /* The number of elements in the stack. */
1769 uint16_t max_stack_elements;
1770
1771 /* The number of instructions in INSTRUCTIONS. */
1772 uint16_t num_instructions;
1773
1774 /* Size of the storage area. */
1775 uint16_t storage_size;
1776
1777 /* Size of the function definition area. */
1778 uint16_t function_defs_size;
1779
1780 /* Size of the instruction definition area. */
1781 uint16_t instruction_defs_size;
1782
1783 /* Size of the twilight zone. */
1784 uint16_t twilight_zone_size;
1785
1786 /* The instruction pointer. This points to the instruction
1787 currently being executed. */
1788 int IP;
1789
1790 /* The current scale. */
1791 sfnt_fixed scale;
1792
1793 /* The current ppem and point size. */
1794 int ppem, point_size;
1795
1796 /* The execution stack. This has at most max_stack_elements
1797 elements. */
1798 uint32_t *stack;
1799
1800 /* Pointer past the top of the stack. */
1801 uint32_t *SP;
1802
1803 /* The size of the control value table. */
1804 size_t cvt_size;
1805
1806 /* Pointer to instructions currently being executed. */
1807 unsigned char *restrict instructions;
1808
1809 /* The twilight zone. May not be NULL. */
1810 sfnt_f26dot6 *restrict twilight_x, *restrict twilight_y;
1811
1812 /* The original X positions of points in the twilight zone. */
1813 sfnt_f26dot6 *restrict twilight_original_x;
1814
1815 /* The original Y positions of points in the twilight zone.
1816
1817 Apple does not directly say whether or not points in the twilight
1818 zone can have their original positions changed. But this is
1819 implied by ``create points in the twilight zone''. */
1820 sfnt_f26dot6 *restrict twilight_original_y;
1821
1822 /* The scaled outlines being manipulated. May be NULL. */
1823 struct sfnt_interpreter_zone *glyph_zone;
1824
1825 /* The glyph advance width. Value is undefined unless GLYPH_ZONE is
1826 set. */
1827 sfnt_f26dot6 advance_width;
1828
1829 /* The storage area. */
1830 uint32_t *storage;
1831
1832 /* Control value table values. */
1833 sfnt_f26dot6 *cvt;
1834
1835 /* Function definitions. */
1836 struct sfnt_interpreter_definition *function_defs;
1837
1838 /* Instruction definitions. */
1839 struct sfnt_interpreter_definition *instruction_defs;
1840
1841 /* Interpreter registers. */
1842 struct sfnt_graphics_state state;
1843
1844 /* Detailed rounding state used when state.round_state indicates
1845 that fine grained rounding should be used.
1846
1847 PERIOD says how often a round value occurs, for numbers
1848 increasing from PHASE to infinity.
1849
1850 THRESHOLD says when to round a value between two increasing
1851 periods towards the larger period. */
1852 sfnt_f26dot6 period, phase, threshold;
1853
1854 /* The depth of any ongoing calls. */
1855 int call_depth;
1856
1857 /* Jump buffer for traps. */
1858 jmp_buf trap;
1859
1860 /* What was the trap. */
1861 const char *trap_reason;
1862
1863 /* Number of variation axes provided by this distortable font. */
1864 int n_axis;
1865
1866 /* Normalized axis coordinates set for this distortable font. */
1867 sfnt_fixed *norm_coords;
1868
1869 #ifdef TEST
1870 /* If non-NULL, function called before each instruction is
1871 executed. */
1872 void (*run_hook) (struct sfnt_interpreter *);
1873
1874 /* If non-NULL, function called before each stack element is
1875 pushed. */
1876 void (*push_hook) (struct sfnt_interpreter *, uint32_t);
1877
1878 /* If non-NULL, function called before each stack element is
1879 popped. */
1880 void (*pop_hook) (struct sfnt_interpreter *, uint32_t);
1881 #endif
1882 };
1883
1884
1885
1886 /* Glyph hinting. */
1887
1888 /* Structure describing a single scaled and fitted outline. */
1889
1890 struct sfnt_instructed_outline
1891 {
1892 /* The number of points in this contour, including the two phantom
1893 points at the end. */
1894 size_t num_points;
1895
1896 /* The number of contours in this outline. */
1897 size_t num_contours;
1898
1899 /* The end points of each contour. */
1900 size_t *contour_end_points;
1901
1902 /* The points of each contour, with two additional phantom points at
1903 the end. */
1904 sfnt_f26dot6 *restrict x_points, *restrict y_points;
1905
1906 /* The flags of each point. */
1907 unsigned char *flags;
1908 };
1909
1910
1911
1912 /* Functions used to read tables used by the TrueType interpreter. */
1913
1914 #ifndef TEST
1915
1916 #define PROTOTYPE int, struct sfnt_offset_subtable *
1917
1918 extern struct sfnt_cvt_table *sfnt_read_cvt_table (PROTOTYPE);
1919 extern struct sfnt_fpgm_table *sfnt_read_fpgm_table (PROTOTYPE);
1920 extern struct sfnt_prep_table *sfnt_read_prep_table (PROTOTYPE);
1921
1922 #undef PROTOTYPE
1923
1924 #define PROTOTYPE \
1925 struct sfnt_maxp_table *, \
1926 struct sfnt_cvt_table *, \
1927 struct sfnt_head_table *, \
1928 struct sfnt_fvar_table *, \
1929 int, int
1930
1931 extern struct sfnt_interpreter *sfnt_make_interpreter (PROTOTYPE);
1932
1933 #undef PROTOTYPE
1934
1935 #define PROTOTYPE \
1936 struct sfnt_interpreter *, \
1937 struct sfnt_fpgm_table *
1938
1939 extern const char *sfnt_interpret_font_program (PROTOTYPE);
1940
1941 #undef PROTOTYPE
1942
1943 #define PROTOTYPE \
1944 struct sfnt_interpreter *, \
1945 struct sfnt_prep_table *, \
1946 struct sfnt_graphics_state *
1947
1948 extern const char *sfnt_interpret_control_value_program (PROTOTYPE);
1949
1950 #undef PROTOTYPE
1951
1952 #define PROTOTYPE struct sfnt_instructed_outline *
1953
1954 extern struct sfnt_glyph_outline *sfnt_build_instructed_outline (PROTOTYPE);
1955
1956 #undef PROTOTYPE
1957
1958 #define PROTOTYPE \
1959 struct sfnt_glyph *, \
1960 struct sfnt_interpreter *, \
1961 struct sfnt_glyph_metrics *, \
1962 struct sfnt_instructed_outline **
1963
1964 extern const char *sfnt_interpret_simple_glyph (PROTOTYPE);
1965
1966 #undef PROTOTYPE
1967
1968 #define PROTOTYPE \
1969 struct sfnt_glyph *, \
1970 struct sfnt_interpreter *, \
1971 struct sfnt_graphics_state *, \
1972 sfnt_get_glyph_proc, \
1973 sfnt_free_glyph_proc, \
1974 struct sfnt_hmtx_table *, \
1975 struct sfnt_hhea_table *, \
1976 struct sfnt_maxp_table *, \
1977 struct sfnt_glyph_metrics *, \
1978 void *, \
1979 struct sfnt_instructed_outline **
1980
1981 extern const char *sfnt_interpret_compound_glyph (PROTOTYPE);
1982
1983 #undef PROTOTYPE
1984
1985
1986
1987 extern void sfnt_vary_interpreter (struct sfnt_interpreter *,
1988 struct sfnt_blend *);
1989
1990 #endif /* TEST */
1991
1992
1993
1994 #endif /* _SFNT_H_ */