Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/table/head.c

raw
1 #include "head.h"
2
3 #include "support/util.h"
4
5 static INLINE void initHead(table_head *head) {
6 memset(head, 0, sizeof(*head));
7 head->magicNumber = 0x5f0f3cf5;
8 head->unitsPerEm = 1000;
9 }
10 static INLINE void disposeHead(table_head *head) {
11 // trivial
12 }
13 caryll_standardRefType(table_head, table_iHead, initHead, disposeHead);
14
15 table_head *otfcc_readHead(const otfcc_Packet packet, const otfcc_Options *options) {
16 FOR_TABLE(OTFCC_CHR('h','e','a','d'), table) {
17 font_file_pointer data = table.data;
18 uint32_t length = table.length;
19
20 if (length < 54) {
21 logWarning("table 'head' corrupted.\n");
22 } else {
23 table_head *head;
24 NEW(head);
25 head->version = read_32s(data);
26 head->fontRevision = read_32u(data + 4);
27 head->checkSumAdjustment = read_32u(data + 8);
28 head->magicNumber = read_32u(data + 12);
29 head->flags = read_16u(data + 16);
30 head->unitsPerEm = read_16u(data + 18);
31 head->created = read_64u(data + 20);
32 head->modified = read_64u(data + 28);
33 head->xMin = read_16u(data + 36);
34 head->yMin = read_16u(data + 38);
35 head->xMax = read_16u(data + 40);
36 head->yMax = read_16u(data + 42);
37 head->macStyle = read_16u(data + 44);
38 head->lowestRecPPEM = read_16u(data + 46);
39 head->fontDirectoryHint = read_16u(data + 48);
40 head->indexToLocFormat = read_16u(data + 50);
41 head->glyphDataFormat = read_16u(data + 52);
42 return head;
43 }
44 }
45 return NULL;
46 }
47
48 static const char *headFlagsLabels[] = {"baselineAtY_0",
49 "lsbAtX_0",
50 "instrMayDependOnPointSize",
51 "alwaysUseIntegerSize",
52 "instrMayAlterAdvanceWidth",
53 "designedForVertical",
54 "_reserved1",
55 "designedForComplexScript",
56 "hasMetamorphosisEffects",
57 "containsStrongRTL",
58 "containsIndicRearrangement",
59 "fontIsLossless",
60 "fontIsConverted",
61 "optimizedForCleartype",
62 "lastResortFont",
63 NULL};
64 static const char *macStyleLabels[] = {"bold", "italic", "underline", "outline",
65 "shadow", "condensed", "extended", NULL};
66
67 void otfcc_dumpHead(const table_head *table, json_value *root, const otfcc_Options *options) {
68 if (!table) return;
69 loggedStep("head") {
70 json_value *head = json_object_new(15);
71 json_object_push(head, "version", json_double_new(otfcc_from_fixed(table->version)));
72 json_object_push(head, "fontRevision",
73 json_double_new(otfcc_from_fixed(table->fontRevision)));
74 json_object_push(head, "flags", otfcc_dump_flags(table->flags, headFlagsLabels));
75 json_object_push(head, "unitsPerEm", json_integer_new(table->unitsPerEm));
76 json_object_push(head, "created", json_integer_new(table->created));
77 json_object_push(head, "modified", json_integer_new(table->modified));
78 json_object_push(head, "xMin", json_integer_new(table->xMin));
79 json_object_push(head, "xMax", json_integer_new(table->xMax));
80 json_object_push(head, "yMin", json_integer_new(table->yMin));
81 json_object_push(head, "yMax", json_integer_new(table->yMax));
82 json_object_push(head, "macStyle", otfcc_dump_flags(table->macStyle, macStyleLabels));
83 json_object_push(head, "lowestRecPPEM", json_integer_new(table->lowestRecPPEM));
84 json_object_push(head, "fontDirectoryHint", json_integer_new(table->fontDirectoryHint));
85 json_object_push(head, "indexToLocFormat", json_integer_new(table->indexToLocFormat));
86 json_object_push(head, "glyphDataFormat", json_integer_new(table->glyphDataFormat));
87 json_object_push(root, "head", head);
88 }
89 }
90
91 table_head *otfcc_parseHead(const json_value *root, const otfcc_Options *options) {
92 table_head *head = table_iHead.create();
93 json_value *table = NULL;
94 if ((table = json_obj_get_type(root, "head", json_object))) {
95 loggedStep("head") {
96 head->version = otfcc_to_fixed(json_obj_getnum_fallback(table, "version", 0));
97 head->fontRevision = otfcc_to_fixed(json_obj_getnum_fallback(table, "fontRevision", 0));
98 head->flags = otfcc_parse_flags(json_obj_get(table, "flags"), headFlagsLabels);
99 head->unitsPerEm = json_obj_getnum_fallback(table, "unitsPerEm", 0);
100 head->created = json_obj_getnum_fallback(table, "created", 0);
101 head->modified = json_obj_getnum_fallback(table, "modified", 0);
102 head->xMin = json_obj_getnum_fallback(table, "xMin", 0);
103 head->xMax = json_obj_getnum_fallback(table, "xMax", 0);
104 head->yMin = json_obj_getnum_fallback(table, "yMin", 0);
105 head->yMax = json_obj_getnum_fallback(table, "yMax", 0);
106 head->macStyle = otfcc_parse_flags(json_obj_get(table, "macStyle"), macStyleLabels);
107 head->lowestRecPPEM = json_obj_getnum_fallback(table, "lowestRecPPEM", 0);
108 head->fontDirectoryHint = json_obj_getnum_fallback(table, "fontDirectoryHint", 0);
109 head->indexToLocFormat = json_obj_getnum_fallback(table, "indexToLocFormat", 0);
110 head->glyphDataFormat = json_obj_getnum_fallback(table, "glyphDataFormat", 0);
111 }
112 }
113 return head;
114 }
115
116 caryll_Buffer *otfcc_buildHead(const table_head *head, const otfcc_Options *options) {
117 if (!head) return NULL;
118 caryll_Buffer *buf = bufnew();
119 bufwrite32b(buf, head->version);
120 bufwrite32b(buf, head->fontRevision);
121 bufwrite32b(buf, head->checkSumAdjustment);
122 bufwrite32b(buf, head->magicNumber);
123 bufwrite16b(buf, head->flags);
124 bufwrite16b(buf, head->unitsPerEm);
125 bufwrite64b(buf, head->created);
126 bufwrite64b(buf, head->modified);
127 bufwrite16b(buf, head->xMin);
128 bufwrite16b(buf, head->yMin);
129 bufwrite16b(buf, head->xMax);
130 bufwrite16b(buf, head->yMax);
131 bufwrite16b(buf, head->macStyle);
132 bufwrite16b(buf, head->lowestRecPPEM);
133 bufwrite16b(buf, head->fontDirectoryHint);
134 bufwrite16b(buf, head->indexToLocFormat);
135 bufwrite16b(buf, head->glyphDataFormat);
136 return buf;
137 }