Debian Codegraph @

texk/dvipdfm-x/type0.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2021 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 /*
22 * Type0 font support:
23 *
24 * TODO:
25 *
26 * Composite font (multiple descendants) - not supported in PDF
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include <string.h>
34
35 #include "system.h"
36 #include "mem.h"
37 #include "error.h"
38 #include "dpxconf.h"
39 #include "dpxfile.h"
40
41 #include "pdfobj.h"
42 #include "fontmap.h"
43
44 #include "cmap.h"
45 #include "cidtype0.h"
46 #include "cid.h"
47
48 #include "type0.h"
49
50
51 static pdf_obj *pdf_read_ToUnicode_file (const char *cmap_name);
52
53 /* PLEASE FIX THIS */
54 #include "tt_cmap.h"
55
56 /* Try to load ToUnicode CMap from file system first, if not found fallback to
57 * font CMap reverse lookup.
58 * CHANGED: CMap here is not always Unicode to CID mapping. Don't use reverse lookup.
59 */
60 static pdf_obj *
61 try_load_ToUnicode_file (char *cmap_base)
62 {
63 pdf_obj *tounicode;
64 char *cmap_name;
65
66 cmap_name = NEW(strlen(cmap_base)+strlen("-UTF16")+1, char);
67 sprintf(cmap_name, "%s-UTF16", cmap_base);
68 tounicode = pdf_read_ToUnicode_file(cmap_name);
69 if (!tounicode) {
70 sprintf(cmap_name, "%s-UCS2", cmap_base);
71 tounicode = pdf_read_ToUnicode_file(cmap_name);
72 }
73 RELEASE(cmap_name);
74
75 return tounicode;
76 }
77
78 static void
79 Type0Font_attach_ToUnicode_stream (pdf_font *font)
80 {
81 pdf_obj *tounicode;
82 pdf_font *cidfont = pdf_get_font_data(font->type0.descendant);
83 CIDSysInfo *csi;
84 char *fontname;
85
86 /*
87 * ToUnicode CMap:
88 *
89 * ToUnicode CMaps are usually not required for standard character
90 * collections such as Adobe-Japan1. Identity-H is used for UCS
91 * ordering CID-keyed fonts. External resource must be loaded for
92 * others.
93 */
94 ASSERT(cidfont);
95
96 if (CIDFont_is_ACCFont(cidfont)) {
97 /* No need to embed ToUnicode */
98 return;
99 } else if (CIDFont_is_UCSFont(cidfont)) {
100 /*
101 * Old version of dvipdfmx mistakenly used Adobe-Identity as Unicode.
102 */
103 /* ref returned */
104 tounicode = pdf_read_ToUnicode_file("Adobe-Identity-UCS2");
105 if (!tounicode) { /* This should work */
106 tounicode = pdf_new_name("Identity-H");
107 }
108 pdf_add_dict(font->resource, pdf_new_name("ToUnicode"), tounicode);
109 return;
110 }
111
112 tounicode = NULL;
113 csi = &cidfont->cid.csi;
114 if (cidfont->cid.options.embed) {
115 fontname = NEW(strlen(cidfont->fontname)+8, char);
116 sprintf(fontname, "%s+%s", cidfont->uniqueID, cidfont->fontname);
117 } else {
118 fontname = NEW(strlen(cidfont->fontname)+1, char);
119 strcpy(fontname, cidfont->fontname);
120 }
121
122 switch (cidfont->subtype) {
123 case PDF_FONT_FONTTYPE_CIDTYPE2:
124 if (!strcmp(csi->registry, "Adobe") && !strcmp(csi->ordering, "Identity")) {
125 tounicode = otf_create_ToUnicode_stream(cidfont->ident, cidfont->index,
126 fontname, font->usedchars);
127 } else {
128 char *cmap_base = NEW(strlen(csi->registry) + strlen(csi->ordering) + 2, char);
129 sprintf(cmap_base, "%s-%s", csi->registry, csi->ordering);
130 tounicode = try_load_ToUnicode_file(cmap_base);
131 RELEASE(cmap_base);
132 /* In this case glyphs are re-ordered hance otf_create... won't work */
133 }
134 break;
135 default:
136 if (cidfont->flags & CIDFONT_FLAG_TYPE1C) {
137 tounicode = otf_create_ToUnicode_stream(cidfont->ident, cidfont->index,
138 fontname, font->usedchars);
139 } else if (cidfont->flags & CIDFONT_FLAG_TYPE1) {
140 tounicode = CIDFont_type0_t1create_ToUnicode_stream(cidfont->ident, fontname, font->usedchars);
141 } else {
142 tounicode = try_load_ToUnicode_file(cidfont->fontname);
143 if (!tounicode) {
144 tounicode = otf_create_ToUnicode_stream(cidfont->ident, cidfont->index,
145 fontname, font->usedchars);
146 }
147 }
148 }
149 RELEASE(fontname);
150
151 if (tounicode) {
152 pdf_add_dict(font->resource, pdf_new_name("ToUnicode"), tounicode);
153 } else {
154 #if defined(LIBDPX)
155 if (dpx_conf.verbose_level > 0)
156 WARN("Failed to load ToUnicode CMap for font \"%s\"", cidfont->fontname);
157 #else
158 WARN("Failed to load ToUnicode CMap for font \"%s\"", cidfont->filename);
159 #endif /* LIBDPX */
160 }
161
162 return;
163 }
164
165 void
166 pdf_font_load_type0 (pdf_font *font)
167 {
168 if (!font || !font->reference)
169 return;
170
171 /* FIXME: Should move to pdffont.c */
172 if (!pdf_lookup_dict(font->resource, "ToUnicode")) {
173 Type0Font_attach_ToUnicode_stream(font);
174 }
175 }
176
177 int
178 pdf_font_open_type0 (pdf_font *font, int cid_id, int wmode)
179 {
180 pdf_font *cidfont;
181 CIDSysInfo *csi;
182 char *fontname = NULL;
183
184 if (cid_id < 0)
185 return -1;
186
187 cidfont = pdf_get_font_data(cid_id);
188
189 font->type0.wmode = wmode;
190 font->type0.descendant = cid_id;
191
192 /*
193 * PostScript Font name:
194 *
195 * Type0 font's fontname is usually descendant CID-keyed font's font name
196 * appended by -ENCODING.
197 */
198 if (cidfont->cid.options.embed) {
199 fontname = NEW(strlen(cidfont->fontname)+8, char);
200 sprintf(fontname, "%s+%s", cidfont->uniqueID, cidfont->fontname);
201 } else {
202 fontname = NEW(strlen(cidfont->fontname)+1, char);
203 strcpy(fontname, cidfont->fontname);
204 }
205
206 if (dpx_conf.verbose_level > 0) {
207 MESG("(CID:%s)", cidfont->fontname);
208 }
209
210 switch (cidfont->subtype) {
211 case PDF_FONT_FONTTYPE_CIDTYPE0:
212 font->fontname = NEW(strlen(fontname)+strlen("Identity-V")+2, char);
213 sprintf(font->fontname, "%s-%s", fontname, wmode ? "Identity-V" : "Identity-H");
214 font->usedchars = CIDFont_get_usedchars(cidfont);
215 font->flags |= PDF_FONT_FLAG_USEDCHAR_SHARED;
216 if (wmode) {
217 cidfont->cid.need_vmetrics = 1;
218 }
219 break;
220 case PDF_FONT_FONTTYPE_CIDTYPE2:
221 font->fontname = NEW(strlen(fontname)+1, char);
222 strcpy(font->fontname, fontname);
223 /* Adobe-Identity here means use GID as CID directly. No need to use GSUB for finding
224 * vertical glyphs hence separate used_chars for H and V instances are not needed.
225 */
226 csi = &cidfont->cid.csi;
227 if (!csi || (!strcmp(csi->registry, "Adobe") && !strcmp(csi->ordering, "Identity"))) {
228 font->usedchars = CIDFont_get_usedchars(cidfont);
229 font->flags |= PDF_FONT_FLAG_USEDCHAR_SHARED;
230 } else {
231 font->usedchars = wmode ? CIDFont_get_usedchars_v(cidfont) : CIDFont_get_usedchars(cidfont);
232 font->flags |= PDF_FONT_FLAG_USEDCHAR_SHARED;
233 }
234 if (wmode) {
235 cidfont->cid.need_vmetrics = 1;
236 }
237 break;
238 }
239
240 font->resource = pdf_new_dict();
241 pdf_add_dict(font->resource, pdf_new_name ("Type"), pdf_new_name ("Font"));
242 pdf_add_dict(font->resource, pdf_new_name ("Subtype"), pdf_new_name ("Type0"));
243 pdf_add_dict(font->resource,
244 pdf_new_name("BaseFont"), pdf_new_name(font->fontname));
245 pdf_add_dict(font->resource,
246 pdf_new_name("Encoding"), pdf_new_name(wmode ? "Identity-V" : "Identity-H"));
247
248 return 0;
249 }
250
251 /******************************** COMPAT ********************************/
252
253 #ifndef WITHOUT_COMPAT
254
255 #include "cmap_read.h"
256 #include "cmap_write.h"
257 #include "pdfresource.h"
258 #include "pdfencoding.h"
259
260 static pdf_obj *
261 create_dummy_CMap (void)
262 {
263 pdf_obj *stream;
264 char buf[32];
265 int i, n;
266
267 #define CMAP_PART0 "\
268 %!PS-Adobe-3.0 Resource-CMap\n\
269 %%DocumentNeededResources: ProcSet (CIDInit)\n\
270 %%IncludeResource: ProcSet (CIDInit)\n\
271 %%BeginResource: CMap (Adobe-Identity-UCS2)\n\
272 %%Title: (Adobe-Identity-UCS2 Adobe UCS2 0)\n\
273 %%Version: 1.0\n\
274 %%Copyright:\n\
275 %% ---\n\
276 %%EndComments\n\n\
277 "
278 #define CMAP_PART1 "\
279 /CIDInit /ProcSet findresource begin\n\
280 \n\
281 12 dict begin\n\nbegincmap\n\n\
282 /CIDSystemInfo 3 dict dup begin\n\
283 /Registry (Adobe) def\n\
284 /Ordering (UCS2) def\n\
285 /Supplement 0 def\n\
286 end def\n\n\
287 /CMapName /Adobe-Identity-UCS2 def\n\
288 /CMapVersion 1.0 def\n\
289 /CMapType 2 def\n\n\
290 2 begincodespacerange\n\
291 <0000> <FFFF>\n\
292 endcodespacerange\n\
293 "
294 #define CMAP_PART3 "\
295 endcmap\n\n\
296 CMapName currentdict /CMap defineresource pop\n\n\
297 end\nend\n\n\
298 %%EndResource\n\
299 %%EOF\n\
300 "
301
302 stream = pdf_new_stream(STREAM_COMPRESS);
303 pdf_add_stream(stream, CMAP_PART0, strlen(CMAP_PART0));
304 pdf_add_stream(stream, CMAP_PART1, strlen(CMAP_PART1));
305 pdf_add_stream(stream, "\n100 beginbfrange\n", strlen("\n100 beginbfrange\n"));
306 for (i = 0; i < 0x64; i++) {
307 n = sprintf(buf,
308 "<%02X00> <%02XFF> <%02X00>\n", i, i, i);
309 pdf_add_stream(stream, buf, n);
310 }
311 pdf_add_stream(stream, "endbfrange\n\n", strlen("endbfrange\n\n"));
312
313 pdf_add_stream(stream, "\n100 beginbfrange\n", strlen("\n100 beginbfrange\n"));
314 for (i = 0x64; i < 0xc8; i++) {
315 n = sprintf(buf,
316 "<%02X00> <%02XFF> <%02X00>\n", i, i, i);
317 pdf_add_stream(stream, buf, n);
318 }
319 pdf_add_stream(stream, "endbfrange\n\n", strlen("endbfrange\n\n"));
320
321 pdf_add_stream(stream, "\n48 beginbfrange\n", strlen("\n48 beginbfrange\n"));
322 for (i = 0xc8; i <= 0xd7; i++) {
323 n = sprintf(buf,
324 "<%02X00> <%02XFF> <%02X00>\n", i, i, i);
325 pdf_add_stream(stream, buf, n);
326 }
327 for (i = 0xe0; i <= 0xff; i++) {
328 n = sprintf(buf,
329 "<%02X00> <%02XFF> <%02X00>\n", i, i, i);
330 pdf_add_stream(stream, buf, n);
331 }
332 pdf_add_stream(stream, "endbfrange\n\n", strlen("endbfrange\n\n"));
333
334 pdf_add_stream(stream, CMAP_PART3, strlen(CMAP_PART3));
335
336 return stream;
337 }
338
339 static pdf_obj *
340 pdf_read_ToUnicode_file (const char *cmap_name)
341 {
342 pdf_obj *stream;
343 int res_id = -1;
344
345 ASSERT(cmap_name);
346
347 res_id = pdf_findresource("CMap", cmap_name);
348 if (res_id < 0) {
349 if (!strcmp(cmap_name, "Adobe-Identity-UCS2"))
350 stream = create_dummy_CMap();
351 else {
352 stream = pdf_load_ToUnicode_stream(cmap_name);
353 }
354 if (stream) {
355 res_id = pdf_defineresource("CMap",
356 cmap_name,
357 stream, PDF_RES_FLUSH_IMMEDIATE);
358 }
359 }
360
361 return (res_id < 0 ? NULL : pdf_get_resource_reference(res_id));
362 }
363 #endif /* !WITHOUT_COMPAT */