Debian Codegraph @

texk/dvipdfm-x/tt_post.c

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