OSDN Git Service

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