1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
/* $Header: /home/cvsroot/dvipdfmx/src/type0.c,v 1.24 2008/11/30 21:12:27 matthias Exp $
This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata,
the dvipdfmx project team <dvipdfmx@project.ktug.or.kr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
/*
* Type0 font support:
*
* TODO:
*
* Composite font (multiple descendants) - not supported in PDF
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include "system.h"
#include "mem.h"
#include "error.h"
#include "dpxfile.h"
#include "pdfobj.h"
#include "fontmap.h"
#include "cmap.h"
#include "cid.h"
#include "type0.h"
#define TYPE0FONT_DEBUG_STR "Type0"
#define TYPE0FONT_DEBUG 3
static int __verbose = 0;
static pdf_obj *pdf_read_ToUnicode_file (const char *cmap_name);
void
Type0Font_set_verbose(void)
{
__verbose++;
}
/*
* used_chars:
*
* Single bit is used for each CIDs since used_chars can be reused as a
* stream content of CIDSet by doing so. See, cid.h for add_to_used() and
* is_used().
*/
static char *
new_used_chars2(void)
{
char *used_chars;
used_chars = NEW(8192, char);
memset(used_chars, 0, 8192);
return used_chars;
}
#define FLAG_NONE 0
#define FLAG_USED_CHARS_SHARED (1 << 0)
struct Type0Font {
char *fontname; /* BaseFont */
char *encoding; /* "Identity-H" or "Identity-V" (not ID) */
char *used_chars; /* Used chars (CIDs) */
/*
* Type0 only
*/
CIDFont *descendant; /* Only single descendant is allowed. */
int flags;
int wmode;
/*
* PDF Font Resource
*/
pdf_obj *indirect;
pdf_obj *fontdict;
pdf_obj *descriptor; /* MUST BE NULL */
};
static void
Type0Font_init_font_struct (Type0Font *font)
{
ASSERT(font);
font->fontname = NULL;
font->fontdict = NULL;
font->indirect = NULL;
font->descriptor = NULL;
font->encoding = NULL;
font->used_chars = NULL;
font->descendant = NULL;
font->wmode = -1;
font->flags = FLAG_NONE;
return;
}
static void
Type0Font_clean (Type0Font *font)
{
if (font) {
if (font->fontdict)
ERROR("%s: Object not flushed.", TYPE0FONT_DEBUG_STR);
if (font->indirect)
ERROR("%s: Object not flushed.", TYPE0FONT_DEBUG_STR);
if (font->descriptor)
ERROR("%s: FontDescriptor unexpected for Type0 font.", TYPE0FONT_DEBUG_STR);
if (!(font->flags & FLAG_USED_CHARS_SHARED) && font->used_chars)
RELEASE(font->used_chars);
if (font->encoding)
RELEASE(font->encoding);
if (font->fontname)
RELEASE(font->fontname);
font->fontdict = NULL;
font->indirect = NULL;
font->descriptor = NULL;
font->used_chars = NULL;
font->encoding = NULL;
font->fontname = NULL;
}
}
/* PLEASE FIX THIS */
#include "tt_cmap.h"
static void
add_ToUnicode (Type0Font *font)
{
pdf_obj *tounicode;
CIDFont *cidfont;
CIDSysInfo *csi;
char *cmap_name, *fontname;
/*
* ToUnicode CMap:
*
* ToUnicode CMaps are usually not required for standard character
* collections such as Adobe-Japan1. Identity-H is used for UCS
* ordering CID-keyed fonts. External resource must be loaded for
* others.
*/
cidfont = font->descendant;
if (!cidfont) {
ERROR("%s: No descendant CID-keyed font.", TYPE0FONT_DEBUG_STR);
return;
}
if (CIDFont_is_ACCFont(cidfont)) {
/* No need to embed ToUnicode */
return;
} else if (CIDFont_is_UCSFont(cidfont)) {
/*
* Old version of dvipdfmx mistakenly used Adobe-Identity as Unicode.
*/
tounicode = pdf_read_ToUnicode_file("Adobe-Identity-UCS2");
if (!tounicode) { /* This should work */
tounicode = pdf_new_name("Identity-H");
}
pdf_add_dict(font->fontdict, pdf_new_name("ToUnicode"), tounicode);
return;
}
tounicode = NULL;
csi = CIDFont_get_CIDSysInfo(cidfont);
fontname = CIDFont_get_fontname(cidfont);
if (CIDFont_get_embedding(cidfont)) {
fontname += 7; /* FIXME */
}
if (!strcmp(csi->registry, "Adobe") &&
!strcmp(csi->ordering, "Identity")) {
switch (CIDFont_get_subtype(cidfont)) {
case CIDFONT_TYPE2:
/* PLEASE FIX THIS */
tounicode = otf_create_ToUnicode_stream(CIDFont_get_ident(cidfont),
CIDFont_get_opt_index(cidfont),
Type0Font_get_usedchars(font));
break;
default:
if (CIDFont_get_flag(cidfont, CIDFONT_FLAG_TYPE1C)) { /* FIXME */
tounicode = otf_create_ToUnicode_stream(CIDFont_get_ident(cidfont),
CIDFont_get_opt_index(cidfont),
Type0Font_get_usedchars(font));
} else if (CIDFont_get_flag(cidfont, CIDFONT_FLAG_TYPE1)) { /* FIXME */
/* Font loader will create ToUnicode and set. */
return;
} else {
cmap_name = NEW(strlen(fontname) + 7, char);
sprintf(cmap_name, "%s-UTF16", fontname);
tounicode = pdf_read_ToUnicode_file(cmap_name);
if (!tounicode) {
sprintf(cmap_name, "%s-UCS2", fontname);
tounicode = pdf_read_ToUnicode_file(cmap_name);
}
RELEASE(cmap_name);
}
break;
}
} else {
cmap_name = NEW(strlen(csi->registry)+strlen(csi->ordering)+8, char);
sprintf(cmap_name, "%s-%s-UTF16", csi->registry, csi->ordering);
tounicode = pdf_read_ToUnicode_file(cmap_name);
if (!tounicode) {
sprintf(cmap_name, "%s-%s-UCS2", csi->registry, csi->ordering);
tounicode = pdf_read_ToUnicode_file(cmap_name);
}
RELEASE(cmap_name);
}
if (tounicode) {
pdf_add_dict(font->fontdict,
pdf_new_name("ToUnicode"), tounicode);
} else {
WARN("Failed to load ToUnicode CMap for font \"%s\"", fontname);
}
return;
}
void
Type0Font_set_ToUnicode (Type0Font *font, pdf_obj *cmap_ref)
{
ASSERT(font);
pdf_add_dict(font->fontdict,
pdf_new_name("ToUnicode"), cmap_ref);
}
static void
Type0Font_dofont (Type0Font *font)
{
if (!font || !font->indirect)
return;
if (!pdf_lookup_dict(font->fontdict, "ToUnicode")) { /* FIXME */
add_ToUnicode(font);
}
}
static void
Type0Font_flush (Type0Font *font)
{
if (font) {
if (font->fontdict)
pdf_release_obj(font->fontdict);
font->fontdict = NULL;
if (font->indirect)
pdf_release_obj(font->indirect);
font->indirect = NULL;
if (font->descriptor)
ERROR("%s: FontDescriptor unexpected for Type0 font.", TYPE0FONT_DEBUG_STR);
font->descriptor = NULL;
}
}
int
Type0Font_get_wmode (Type0Font *font)
{
ASSERT(font);
return font->wmode;
}
#if 0
char *
Type0Font_get_encoding (Type0Font *font)
{
ASSERT(font);
return font->encoding;
}
#endif
char *
Type0Font_get_usedchars (Type0Font *font)
{
ASSERT(font);
return font->used_chars;
}
pdf_obj *
Type0Font_get_resource (Type0Font *font)
{
ASSERT(font);
/*
* This looks somewhat strange.
*/
if (!font->indirect) {
pdf_obj *array;
array = pdf_new_array();
pdf_add_array(array, CIDFont_get_resource(font->descendant));
pdf_add_dict(font->fontdict, pdf_new_name("DescendantFonts"), array);
font->indirect = pdf_ref_obj(font->fontdict);
}
return pdf_link_obj(font->indirect);
}
/******************************** CACHE ********************************/
#define CHECK_ID(n) do {\
if ((n) < 0 || (n) >= __cache.count)\
ERROR("%s: Invalid ID %d", TYPE0FONT_DEBUG_STR, (n));\
} while (0)
#define CACHE_ALLOC_SIZE 16u
static struct font_cache {
int count;
int capacity;
Type0Font *fonts;
} __cache = {
0, 0, NULL
};
void
Type0Font_cache_init (void)
{
if (__cache.fonts)
ERROR("%s: Already initialized.", TYPE0FONT_DEBUG_STR);
__cache.count = 0;
__cache.capacity = 0;
__cache.fonts = NULL;
}
Type0Font *
Type0Font_cache_get (int id)
{
CHECK_ID(id);
return &__cache.fonts[id];
}
int
Type0Font_cache_find (const char *map_name, int cmap_id, fontmap_opt *fmap_opt)
{
int font_id = -1;
Type0Font *font;
CIDFont *cidfont;
CMap *cmap;
CIDSysInfo *csi;
char *fontname = NULL;
int cid_id = -1, parent_id = -1, wmode = 0;
int pdf_ver;
pdf_ver = pdf_get_version();
if (!map_name || cmap_id < 0 || pdf_ver < 2)
return -1;
/*
* Encoding is Identity-H or Identity-V according as thier WMode value.
*
* We do not use match against the map_name since fonts (TrueType) covers
* characters across multiple character collection (eg, Adobe-Japan1 and
* Adobe-Japan2) must be splited into multiple CID-keyed fonts.
*/
cmap = CMap_cache_get(cmap_id);
csi = (CMap_is_Identity(cmap)) ? NULL : CMap_get_CIDSysInfo(cmap) ;
cid_id = CIDFont_cache_find(map_name, csi, fmap_opt);
if (cid_id < 0)
return -1;
/*
* The descendant CID-keyed font has already been registerd.
* If CID-keyed font with ID = cid_id is new font, then create new parent
* Type 0 font. Otherwise, there already exists parent Type 0 font and
* then we find him and return his ID. We must check against their WMode.
*/
cidfont = CIDFont_cache_get(cid_id);
wmode = CMap_get_wmode(cmap);
/* Does CID-keyed font already have parent ? */
parent_id = CIDFont_get_parent_id(cidfont, wmode);
if (parent_id >= 0)
return parent_id; /* If so, we don't need new one. */
/*
* CIDFont does not have parent or his parent's WMode does not matched with
* wmode. Create new Type0 font.
*/
if (__cache.count >= __cache.capacity) {
__cache.capacity += CACHE_ALLOC_SIZE;
__cache.fonts = RENEW(__cache.fonts, __cache.capacity, struct Type0Font);
}
font_id = __cache.count;
font = &__cache.fonts[font_id];
Type0Font_init_font_struct(font);
/*
* All CJK double-byte characters are mapped so that resulting
* character codes coincide with CIDs of given character collection.
* So, the Encoding is always Identity-H for horizontal fonts or
* Identity-V for vertical fonts.
*/
if (wmode) {
font->encoding = NEW(strlen("Identity-V")+1, char);
strcpy(font->encoding, "Identity-V");
} else {
font->encoding = NEW(strlen("Identity-H")+1, char);
strcpy(font->encoding, "Identity-H");
}
font->wmode = wmode;
/*
* Now we start font dictionary.
*/
font->fontdict = pdf_new_dict();
pdf_add_dict(font->fontdict, pdf_new_name ("Type"), pdf_new_name ("Font"));
pdf_add_dict(font->fontdict, pdf_new_name ("Subtype"), pdf_new_name ("Type0"));
/*
* Type0 font does not have FontDescriptor because it is not a simple font.
* Instead, DescendantFonts appears here.
*
* Up to PDF version 1.5, Type0 font must have single descendant font which
* is a CID-keyed font. Future PDF spec. will allow multiple desecendant
* fonts.
*/
font->descendant = cidfont;
CIDFont_attach_parent(cidfont, font_id, wmode);
/*
* PostScript Font name:
*
* Type0 font's fontname is usually descendant CID-keyed font's font name
* appended by -ENCODING.
*/
fontname = CIDFont_get_fontname(cidfont);
if (__verbose) {
if (CIDFont_get_embedding(cidfont) && strlen(fontname) > 7)
MESG("(CID:%s)", fontname+7); /* skip XXXXXX+ */
else
MESG("(CID:%s)", fontname);
}
/*
* The difference between CID-keyed font and TrueType font appears here.
*
* Glyph substitution for vertical writing is done in CMap mapping process
* for CID-keyed fonts. But we must rely on OpenType layout table in the
* case of TrueType fonts. So, we must use different used_chars for each
* horizontal and vertical fonts in that case.
*
* In most PDF file, encoding name is not appended to fontname for Type0
* fonts having CIDFontType 2 font as their descendant.
*/
font->used_chars = NULL;
font->flags = FLAG_NONE;
switch (CIDFont_get_subtype(cidfont)) {
case CIDFONT_TYPE0:
font->fontname = NEW(strlen(fontname)+strlen(font->encoding)+2, char);
sprintf(font->fontname, "%s-%s", fontname, font->encoding);
pdf_add_dict(font->fontdict,
pdf_new_name("BaseFont"), pdf_new_name(font->fontname));
/*
* Need used_chars to write W, W2.
*/
if ((parent_id = CIDFont_get_parent_id(cidfont, wmode ? 0 : 1)) < 0)
font->used_chars = new_used_chars2();
else {
/* Don't allocate new one. */
font->used_chars = Type0Font_get_usedchars(Type0Font_cache_get(parent_id));
font->flags |= FLAG_USED_CHARS_SHARED;
}
break;
case CIDFONT_TYPE2:
/*
* TrueType:
*
* Use different used_chars for H and V.
*/
pdf_add_dict(font->fontdict,
pdf_new_name("BaseFont"), pdf_new_name(fontname));
font->used_chars = new_used_chars2();
break;
default:
ERROR("Unrecognized CIDFont Type");
break;
}
pdf_add_dict(font->fontdict,
pdf_new_name("Encoding"), pdf_new_name(font->encoding));
__cache.count++;
return font_id;
}
void
Type0Font_cache_close (void)
{
int font_id;
/*
* This need to be fixed.
*
* CIDFont_cache_close() before Type0Font_release because of used_chars.
* ToUnicode support want descendant CIDFont's CSI and fontname.
*/
if (__cache.fonts) {
for (font_id = 0; font_id < __cache.count; font_id++)
Type0Font_dofont(&__cache.fonts[font_id]);
}
CIDFont_cache_close();
if (__cache.fonts) {
for (font_id = 0; font_id < __cache.count; font_id++) {
Type0Font_flush(&__cache.fonts[font_id]);
Type0Font_clean(&__cache.fonts[font_id]);
}
RELEASE(__cache.fonts);
}
__cache.fonts = NULL;
__cache.count = 0;
__cache.capacity = 0;
}
/************************************************************************/
int
pdf_font_findfont0 (const char *font_name, int cmap_id, fontmap_opt *fmap_opt)
{
return Type0Font_cache_find(font_name, cmap_id, fmap_opt);
}
/******************************** COMPAT ********************************/
#ifndef WITHOUT_COMPAT
#include "cmap_read.h"
#include "cmap_write.h"
#include "pdfresource.h"
#include "pdfencoding.h"
static pdf_obj *
create_dummy_CMap (void)
{
pdf_obj *stream;
char buf[32];
int i, n;
#define CMAP_PART0 "\
%!PS-Adobe-3.0 Resource-CMap\n\
%%DocumentNeededResources: ProcSet (CIDInit)\n\
%%IncludeResource: ProcSet (CIDInit)\n\
%%BeginResource: CMap (Adobe-Identity-UCS2)\n\
%%Title: (Adobe-Identity-UCS2 Adobe UCS2 0)\n\
%%Version: 1.0\n\
%%Copyright:\n\
%% ---\n\
%%EndComments\n\n\
"
#define CMAP_PART1 "\
/CIDInit /ProcSet findresource begin\n\
\n\
12 dict begin\n\nbegincmap\n\n\
/CIDSystemInfo 3 dict dup begin\n\
/Registry (Adobe) def\n\
/Ordering (UCS2) def\n\
/Supplement 0 def\n\
end def\n\n\
/CMapName /Adobe-Identity-UCS2 def\n\
/CMapVersion 1.0 def\n\
/CMapType 2 def\n\n\
2 begincodespacerange\n\
<0000> <FFFF>\n\
endcodespacerange\n\
"
#define CMAP_PART3 "\
endcmap\n\n\
CMapName currentdict /CMap defineresource pop\n\n\
end\nend\n\n\
%%EndResource\n\
%%EOF\n\
"
stream = pdf_new_stream(STREAM_COMPRESS);
pdf_add_stream(stream, CMAP_PART0, strlen(CMAP_PART0));
pdf_add_stream(stream, CMAP_PART1, strlen(CMAP_PART1));
pdf_add_stream(stream, "\n100 beginbfrange\n", strlen("\n100 beginbfrange\n"));
for (i = 0; i < 0x64; i++) {
n = sprintf(buf,
"<%02X00> <%02XFF> <%02X00>\n", i, i, i);
pdf_add_stream(stream, buf, n);
}
pdf_add_stream(stream, "endbfrange\n\n", strlen("endbfrange\n\n"));
pdf_add_stream(stream, "\n100 beginbfrange\n", strlen("\n100 beginbfrange\n"));
for (i = 0x64; i < 0xc8; i++) {
n = sprintf(buf,
"<%02X00> <%02XFF> <%02X00>\n", i, i, i);
pdf_add_stream(stream, buf, n);
}
pdf_add_stream(stream, "endbfrange\n\n", strlen("endbfrange\n\n"));
pdf_add_stream(stream, "\n48 beginbfrange\n", strlen("\n48 beginbfrange\n"));
for (i = 0xc8; i <= 0xd7; i++) {
n = sprintf(buf,
"<%02X00> <%02XFF> <%02X00>\n", i, i, i);
pdf_add_stream(stream, buf, n);
}
for (i = 0xe0; i <= 0xff; i++) {
n = sprintf(buf,
"<%02X00> <%02XFF> <%02X00>\n", i, i, i);
pdf_add_stream(stream, buf, n);
}
pdf_add_stream(stream, "endbfrange\n\n", strlen("endbfrange\n\n"));
pdf_add_stream(stream, CMAP_PART3, strlen(CMAP_PART3));
return stream;
}
static pdf_obj *
pdf_read_ToUnicode_file (const char *cmap_name)
{
pdf_obj *stream;
long res_id = -1;
ASSERT(cmap_name);
res_id = pdf_findresource("CMap", cmap_name);
if (res_id < 0) {
if (!strcmp(cmap_name, "Adobe-Identity-UCS2"))
stream = create_dummy_CMap();
else {
stream = pdf_load_ToUnicode_stream(cmap_name);
}
if (stream) {
res_id = pdf_defineresource("CMap",
cmap_name,
stream, PDF_RES_FLUSH_IMMEDIATE);
}
}
return (res_id < 0 ? NULL : pdf_get_resource_reference(res_id));
}
#endif /* !WITHOUT_COMPAT */
|