Debian Codegraph @

texk/xdvipsk/otflib/tt_post.c

raw
1 /* This is xdvipsk, an eXtended version of dvips(k) by Tomas Rokicki.
2
3 Copyright (C) 2016 by VTeX Ltd (www.vtex.lt),
4 the xdvipsk project team - Sigitas Tolusis and Arunas Povilaitis.
5
6 Program original code copyright (C) 2007-2014 by Jin-Hwan Cho and
7 Shunsaku Hirata, the dvipdfmx project team.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 */
23
24 #include <config.h>
25
26 #include "system.h"
27 #include "error.h"
28 #include "mem.h"
29
30 #include "dpxfile.h"
31
32 #include "sfnt.h"
33
34 #include "tt_post.h"
35
36 static const char *macglyphorder[258];
37
38 /* offset from begenning of the post table */
39 #define NAME_STR_OFFSET 32
40
41 static int
42 read_v2_post_names (struct tt_post_table *post, sfnt *sfont)
43 {
44 USHORT i, idx, *indices, maxidx;
45 int len;
46
47 post->numberOfGlyphs = sfnt_get_ushort(sfont);
48
49 indices = NEW(post->numberOfGlyphs, USHORT);
50 maxidx = 257;
51 for (i = 0;
52 i < post->numberOfGlyphs; i++) {
53 idx = sfnt_get_ushort(sfont);
54 if (idx >= 258) {
55 if (idx > maxidx)
56 maxidx = idx;
57 if (idx > 32767) {
58 /* Although this is strictly speaking out of spec, it seems to work
59 and there are real-life fonts that use it.
60 We show a warning only once, instead of thousands of times */
61 static char warning_issued = 0;
62 if (!warning_issued) {
63 WARN("TrueType post table name index %u > 32767", idx);
64 warning_issued = 1;
65 }
66 /* In a real-life large font, (x)dvipdfmx crashes if we use
67 nonvanishing idx in the case of idx > 32767.
68 If we set idx = 0, (x)dvipdfmx works fine for the font and
69 created pdf seems fine. The post table may not be important
70 in such a case */
71 idx = 0;
72 }
73 }
74 indices[i] = idx;
75 }
76
77 post->count = maxidx - 257;
78 if (post->count < 1) {
79 post->names = NULL;
80 } else {
81 post->names = NEW(post->count, char *);
82 for (i = 0; i < post->count; i++) { /* read Pascal strings */
83 len = sfnt_get_byte(sfont);
84 if (len > 0) {
85 post->names[i] = NEW(len + 1, char);
86 sfnt_read(post->names[i], len, sfont);
87 post->names[i][len] = 0;
88 } else {
89 post->names[i] = NULL;
90 }
91 }
92 }
93
94 post->glyphNamePtr = NEW(post->numberOfGlyphs, const char *);
95 for (i = 0; i < post->numberOfGlyphs; i++) {
96 idx = indices[i];
97 if (idx < 258) {
98 post->glyphNamePtr[i] = macglyphorder[idx];
99 } else if (idx - 258 < post->count) {
100 post->glyphNamePtr[i] = post->names[idx - 258];
101 } else {
102 WARN("Invalid glyph name index number: %u (>= %u)",
103 idx, post->count + 258);
104 RELEASE(indices);
105 return -1;
106 }
107 }
108 RELEASE(indices);
109
110 return 0;
111 }
112
113 struct tt_post_table *
114 tt_read_post_table (sfnt *sfont)
115 {
116 struct tt_post_table *post;
117
118 /* offset = */ sfnt_locate_table(sfont, "post");
119
120 post = NEW(1, struct tt_post_table);
121
122 post->Version = sfnt_get_ulong(sfont); /* Fixed */
123 post->italicAngle = sfnt_get_ulong(sfont); /* Fixed */
124 post->underlinePosition = sfnt_get_short(sfont); /* FWord */
125 post->underlineThickness = sfnt_get_short(sfont); /* FWord */
126 post->isFixedPitch = sfnt_get_ulong(sfont);
127 post->minMemType42 = sfnt_get_ulong(sfont);
128 post->maxMemType42 = sfnt_get_ulong(sfont);
129 post->minMemType1 = sfnt_get_ulong(sfont);
130 post->maxMemType1 = sfnt_get_ulong(sfont);
131
132 post->numberOfGlyphs = 0;
133 post->glyphNamePtr = NULL;
134 post->count = 0;
135 post->names = NULL;
136
137 if (post->Version == 0x00010000UL) {
138 post->numberOfGlyphs = 258; /* wrong */
139 post->glyphNamePtr = macglyphorder;
140 } else if (post->Version == 0x00028000UL) {
141 WARN("TrueType 'post' version 2.5 found (deprecated)");
142 } else if (post->Version == 0x00020000UL) {
143 if (read_v2_post_names(post, sfont) < 0) {
144 WARN("Invalid version 2.0 'post' table");
145 tt_release_post_table(post);
146 post = NULL;
147 }
148 } else if (post->Version == 0x00030000UL || /* no glyph names provided */
149 post->Version == 0x00040000UL) { /* Apple format for printer-based fonts */
150 /* don't bother constructing char names, not sure if they'll ever be needed */
151 } else { /* some broken font files have 0x00000000UL and perhaps other values */
152 WARN("Unknown 'post' version: %08X, assuming version 3.0", post->Version);
153 }
154
155 return post;
156 }
157
158 USHORT
159 tt_lookup_post_table (struct tt_post_table *post, const char *glyphname)
160 {
161 USHORT gid;
162
163 ASSERT(post && glyphname);
164
165 for (gid = 0; gid < post->count; gid++) {
166 if (post->glyphNamePtr[gid] &&
167 !strcmp(glyphname, post->glyphNamePtr[gid])) {
168 return gid;
169 }
170 }
171
172 return 0;
173 }
174
175 char*
176 tt_get_glyphname (struct tt_post_table *post, USHORT gid)
177 {
178 if (gid < post->count)
179 return xstrdup(post->glyphNamePtr[gid]);
180 return NULL;
181 }
182
183 void
184 tt_release_post_table (struct tt_post_table *post)
185 {
186 USHORT i;
187
188 ASSERT(post);
189
190 if (post->glyphNamePtr && post->Version != 0x00010000UL)
191 RELEASE((void *)post->glyphNamePtr);
192 if (post->names) {
193 for (i = 0; i < post->count; i++) {
194 if (post->names[i])
195 RELEASE(post->names[i]);
196 }
197 RELEASE(post->names);
198 }
199 post->count = 0;
200 post->glyphNamePtr = NULL;
201 post->names = NULL;
202
203 RELEASE(post);
204
205 return;
206 }
207
208 /* Macintosh glyph order - from apple's TTRefMan */
209 static const char *
210 macglyphorder[258] = {
211 /* 0x0000 */
212 ".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl",
213 "numbersign", "dollar", "percent", "ampersand", "quotesingle",
214 "parenleft", "parenright", "asterisk", "plus", "comma",
215 /* 0x0010 */
216 "hyphen", "period", "slash", "zero", "one", "two", "three", "four",
217 "five", "six", "seven", "eight", "nine", "colon", "semicolon", "less",
218 /* 0x0020 */
219 "equal", "greater", "question", "at", "A", "B", "C", "D",
220 "E", "F", "G", "H", "I", "J", "K", "L",
221 /* 0x0030 */
222 "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
223 "Y", "Z", "bracketleft", "backslash",
224 /* 0x0040 */
225 "bracketright", "asciicircum", "underscore", "grave",
226 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
227 /* 0x0050 */
228 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
229 "y", "z", "braceleft", "bar",
230 /* 0x0060 */
231 "braceright", "asciitilde", "Adieresis", "Aring", "Ccedilla",
232 "Eacute", "Ntilde", "Odieresis", "Udieresis", "aacute", "agrave",
233 "acircumflex", "adieresis", "atilde", "aring", "ccedilla",
234 /* 0x0070 */
235 "eacute", "egrave", "ecircumflex", "edieresis", "iacute", "igrave",
236 "icircumflex", "idieresis", "ntilde", "oacute", "ograve", "ocircumflex",
237 "odieresis", "otilde", "uacute", "ugrave",
238 /* 0x0080 */
239 "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling",
240 "section", "bullet", "paragraph", "germandbls", "registered",
241 "copyright", "trademark", "acute", "dieresis", "notequal",
242 /* 0x0090 */
243 "AE", "Oslash", "infinity", "plusminus", "lessequal", "greaterequal",
244 "yen", "mu", "partialdiff", "summation", "product", "pi", "integral",
245 "ordfeminine", "ordmasculine", "Omega",
246 /* 0x00a0 */
247 "ae", "oslash", "questiondown", "exclamdown", "logicalnot", "radical",
248 "florin", "approxequal", "Delta", "guillemotleft", "guillemotright",
249 "ellipsis", "nonbreakingspace", "Agrave", "Atilde", "Otilde",
250 /* 0x00b0 */
251 "OE", "oe", "endash", "emdash", "quotedblleft", "quotedblright",
252 "quoteleft", "quoteright", "divide", "lozenge", "ydieresis",
253 "Ydieresis", "fraction", "currency", "guilsinglleft", "guilsinglright",
254 /* 0x00c0 */
255 "fi", "fl", "daggerdbl", "periodcentered", "quotesinglbase",
256 "quotedblbase", "perthousand", "Acircumflex", "Ecircumflex", "Aacute",
257 "Edieresis", "Egrave", "Iacute", "Icircumflex", "Idieresis", "Igrave",
258 /* 0x00d0 */
259 "Oacute", "Ocircumflex", "apple", "Ograve", "Uacute", "Ucircumflex",
260 "Ugrave", "dotlessi", "circumflex", "tilde", "macron", "breve",
261 "dotaccent", "ring", "cedilla", "hungarumlaut",
262 /* 0x00e0 */
263 "ogonek", "caron", "Lslash", "lslash", "Scaron", "scaron", "Zcaron",
264 "zcaron", "brokenbar", "Eth", "eth", "Yacute", "yacute", "Thorn",
265 "thorn", "minus",
266 /* 0x00f0 */
267 "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf",
268 "onequarter", "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent",
269 "Scedilla", "scedilla", "Cacute", "cacute", "Ccaron",
270 /* 0x0100 */
271 "ccaron", "dcroat"
272 };