~ubuntu-branches/ubuntu/jaunty/texlive-bin/jaunty-security

« back to all changes in this revision

Viewing changes to build/TeX/texk/dvipdfmx/src/cidtype2.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2008-06-26 23:14:59 UTC
  • mfrom: (2.1.30 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080626231459-y02rjsrgtafu83yr
Tags: 2007.dfsg.2-3
add missing source roadmap.fig of roadmap.eps in fontinst documentation
(Closes: #482915) (urgency medium due to RC bug)
(new patch add-missing-fontinst-source)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  $Header: /home/cvsroot/dvipdfmx/src/cidtype2.c,v 1.33 2005/07/17 09:53:38 hirata Exp $
2
 
    
3
 
    This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
4
 
 
5
 
    Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata,
6
 
    the dvipdfmx project team <dvipdfmx@project.ktug.or.kr>
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
 
/*
24
 
 * TrueType glyf table is sorted by CID and no CIDToGIDMap is used here.
25
 
 * GhostScript can't handle CIDToGIDMap correctly.
26
 
 */
27
 
 
28
 
#if HAVE_CONFIG_H
29
 
#include "config.h"
30
 
#endif
31
 
 
32
 
#include "system.h"
33
 
#include "numbers.h"
34
 
#include "mem.h"
35
 
#include "error.h"
36
 
#include "dpxfile.h"
37
 
 
38
 
#include "pdfobj.h"
39
 
/* pseudo unique tag */
40
 
#include "pdffont.h"
41
 
 
42
 
#ifndef PDF_NAME_LEN_MAX
43
 
#  define PDF_NAME_LEN_MAX 255
44
 
#endif
45
 
 
46
 
/* TrueType */
47
 
#include "sfnt.h"
48
 
#include "tt_aux.h"
49
 
#include "tt_glyf.h"
50
 
#include "tt_cmap.h"
51
 
#include "tt_table.h"
52
 
 
53
 
#include "tt_gsub.h"
54
 
 
55
 
/* CID font */
56
 
#include "cmap.h"
57
 
#include "type0.h"
58
 
#include "cid.h"
59
 
#include "cid_p.h"
60
 
#include "cidtype2.h"
61
 
 
62
 
static int verbose   = 0;
63
 
static int opt_flags = 0;
64
 
 
65
 
void
66
 
CIDFont_type2_set_verbose (void)
67
 
{
68
 
  verbose++;
69
 
}
70
 
 
71
 
void
72
 
CIDFont_type2_set_flags (long flags)
73
 
{
74
 
  opt_flags = flags;
75
 
}
76
 
 
77
 
/*
78
 
 * PDF viewer applications use following tables (CIDFontType 2)
79
 
 *
80
 
 *  head, hhea, loca, maxp, glyf, hmtx, fpgm, cvt_, prep
81
 
 *
82
 
 *                                         - from PDF Ref. v.1.3, 2nd ed.
83
 
 *
84
 
 * The fpgm, cvt_, and prep tables appears only when TrueType instructions
85
 
 * requires them. Those tables must be preserved if they exist.
86
 
 * We use must_exist flag to indicate `preserve it if present'
87
 
 * and to make sure not to cause an error when it does not exist.
88
 
 *
89
 
 * post and name table must exist in ordinary TrueType font file,
90
 
 * but when a TrueType font is converted to CIDFontType 2 font, those tables
91
 
 * are no longer required.
92
 
 *
93
 
 * The OS/2 table (required for TrueType font for Windows and OS/2) contains
94
 
 * liscencing information, but PDF viewers seems not using them.
95
 
 *
96
 
 * The 'name' table added. See comments in ttf.c.
97
 
 */
98
 
 
99
 
static struct
100
 
{
101
 
  const char *name;
102
 
  int         must_exist;
103
 
} required_table[] = {
104
 
  {"OS/2", 1}, {"head", 1}, {"hhea", 1}, {"loca", 1}, {"maxp", 1},
105
 
  {"name", 1}, {"glyf", 1}, {"hmtx", 1}, {"fpgm", 0}, {"cvt ", 0},
106
 
  {"prep", 0}, {NULL, 0}
107
 
};
108
 
 
109
 
static void
110
 
validate_name (char *fontname, int len)
111
 
{
112
 
  int    i, count;
113
 
  char  *p;
114
 
  static const char *badstrlist[] = {
115
 
    "-WIN-RKSJ-H",
116
 
    "-WINP-RKSJ-H",
117
 
    "-WING-RKSJ-H",
118
 
    "-90pv-RKSJ-H",
119
 
    NULL
120
 
  };
121
 
 
122
 
  for (count = 0, i = 0; i < len; i++) {
123
 
    if (fontname[i] == 0) {
124
 
      memmove(fontname + i, fontname + i + 1, len - i);
125
 
      count++;
126
 
      len--;
127
 
    }
128
 
  }
129
 
  if (count > 0) {
130
 
    WARN("Removed %d null character(s) from fontname --> %s",
131
 
         count, fontname);
132
 
  }
133
 
  fontname[len] = '\0';
134
 
 
135
 
  /* For some fonts that have bad PS name. ad hoc. remove me. */
136
 
  for (i = 0; badstrlist[i] != NULL; i++) {
137
 
    p = strstr(fontname, badstrlist[i]);
138
 
    if (p && p > fontname) {
139
 
      WARN("Removed string \"%s\" from fontname \"%s\".",
140
 
           badstrlist[i], fontname);
141
 
      p[0] = '\0';
142
 
      len  = (int) (p - fontname);
143
 
      break;
144
 
    }
145
 
  }
146
 
 
147
 
  if (len < 1) {
148
 
    ERROR("No valid character found in fontname string.");
149
 
  }
150
 
}
151
 
 
152
 
/*
153
 
 * We will follow the convension for finding ToUnicode CMap described in PDF
154
 
 * Reference 4th ed., page 432. The name of "ToCode" (not limited to Unicode
155
 
 * here) CMap is obtained by concatenating the registry, ordering, and the
156
 
 * name of encoding.
157
 
 *
158
 
 * UCSms-UCS4, UCSms-UCS2, UCS4 added...
159
 
 */
160
 
 
161
 
#define WIN_UCS_INDEX_MAX   1
162
 
#define KNOWN_ENCODINGS_MAX 8
163
 
static struct
164
 
{
165
 
  unsigned short  platform;
166
 
  unsigned short  encoding;
167
 
  const char     *pdfnames[5];
168
 
} known_encodings[] = {
169
 
  {TT_WIN, TT_WIN_UCS4,     {"UCSms-UCS4", "UCSms-UCS2", "UCS4", "UCS2", NULL}},
170
 
  {TT_WIN, TT_WIN_UNICODE,  {"UCSms-UCS4", "UCSms-UCS2", "UCS4", "UCS2", NULL}},
171
 
  {TT_WIN, TT_WIN_SJIS,     {"90ms-RKSJ", NULL}},
172
 
  {TT_WIN, TT_WIN_RPC,      {"GBK-EUC",   NULL}},
173
 
  {TT_WIN, TT_WIN_BIG5,     {"ETen-B5",   NULL}},
174
 
  {TT_WIN, TT_WIN_WANSUNG,  {"KSCms-UHC", NULL}},
175
 
  {TT_MAC, TT_MAC_JAPANESE, {"90pv-RKSJ", NULL}},
176
 
  {TT_MAC, TT_MAC_TRADITIONAL_CHINESE, {"B5pc",     NULL}},
177
 
  {TT_MAC, TT_MAC_SIMPLIFIED_CHINESE,  {"GBpc-EUC", NULL}},
178
 
  {TT_MAC, TT_MAC_KOREAN,   {"KSCpc-EUC", NULL}},
179
 
  {0, 0, {NULL}}
180
 
};
181
 
 
182
 
static CMap *
183
 
find_tocode_cmap (const char *reg, const char *ord, int select)
184
 
{
185
 
  int   cmap_id = -1, i;
186
 
  char *cmap_name;
187
 
  char *append;
188
 
 
189
 
  if (!reg || !ord ||
190
 
      select < 0 || select > KNOWN_ENCODINGS_MAX)
191
 
    ERROR("Character set unknown.");
192
 
 
193
 
  if (!strcmp(ord, "UCS") &&
194
 
      select <= WIN_UCS_INDEX_MAX)
195
 
    return NULL;
196
 
 
197
 
  for (i = 0; cmap_id < 0 && i < 5; i++) {
198
 
    append = (char *) known_encodings[select].pdfnames[i];
199
 
    if (!append)
200
 
      break;
201
 
    cmap_name = NEW(strlen(reg) + strlen(ord) + strlen(append) + 3, char);
202
 
    sprintf(cmap_name, "%s-%s-%s", reg, ord, append);
203
 
    cmap_id = CMap_cache_find(cmap_name);
204
 
    RELEASE(cmap_name);
205
 
  }
206
 
  if (cmap_id < 0) {
207
 
    WARN("Could not find CID-to-Code mapping for \"%s-%s\".", reg, ord);
208
 
    WARN("I tried to load (one of) the following file(s):");
209
 
    for (i = 0; i < 5; i++) {
210
 
      append = (char *) known_encodings[select].pdfnames[i];
211
 
      if (!append)
212
 
        break;
213
 
      MESG(" %s-%s-%s", reg, ord, append);
214
 
    }
215
 
    WARN("Please check if this file exists.");
216
 
    ERROR("Cannot continue...");
217
 
  }
218
 
 
219
 
  return CMap_cache_get(cmap_id);
220
 
}
221
 
 
222
 
 
223
 
/*
224
 
 * CIDFont glyph metrics:
225
 
 * Mostly same as add_CID[HV]Metrics in cidtype0.c.
226
 
 */
227
 
#define PDFUNIT(v) ((double) (ROUND(1000.0*(v)/(g->emsize), 1)))
228
 
 
229
 
static void
230
 
add_TTCIDHMetrics (pdf_obj *fontdict, struct tt_glyphs *g,
231
 
                   char *used_chars, unsigned char *cidtogidmap, unsigned short last_cid)
232
 
{
233
 
  long cid, start = 0, prev = 0;
234
 
  pdf_obj *w_array, *an_array = NULL;
235
 
  double   dw;
236
 
  int      empty = 1;
237
 
 
238
 
  w_array = pdf_new_array();
239
 
  if (g->dw != 0 && g->dw <= g->emsize) {
240
 
    dw = PDFUNIT(g->dw);
241
 
  } else {
242
 
    dw = PDFUNIT(g->gd[0].advw);
243
 
  }
244
 
  for (cid = 0; cid <= last_cid; cid++) {
245
 
    USHORT idx, gid;
246
 
    double width;
247
 
 
248
 
    if (!is_used_char2(used_chars, cid))
249
 
      continue;
250
 
    gid = (cidtogidmap) ? ((cidtogidmap[2*cid] << 8)|cidtogidmap[2*cid+1]) : cid;
251
 
    idx = tt_get_index(g, gid);
252
 
    if (cid != 0 && idx == 0)
253
 
      continue;
254
 
    width = PDFUNIT((g->gd)[idx].advw);
255
 
    if (width == dw) {
256
 
      if (an_array) {
257
 
        pdf_add_array(w_array, pdf_new_number(start));
258
 
        pdf_add_array(w_array, an_array);
259
 
        an_array = NULL;
260
 
        empty = 0;
261
 
      }
262
 
    } else {
263
 
      if (cid != prev + 1) {
264
 
        if (an_array) {
265
 
          pdf_add_array(w_array, pdf_new_number(start));
266
 
          pdf_add_array(w_array, an_array);
267
 
          an_array = NULL;
268
 
          empty = 0;
269
 
        }
270
 
      }
271
 
      if (an_array == NULL) {
272
 
        an_array = pdf_new_array();
273
 
        start = cid;
274
 
      }
275
 
      pdf_add_array(an_array, pdf_new_number(width));
276
 
      prev = cid;
277
 
    }
278
 
  }
279
 
 
280
 
  if (an_array) {
281
 
    pdf_add_array(w_array, pdf_new_number(start));
282
 
    pdf_add_array(w_array, an_array);
283
 
    empty = 0;
284
 
  }
285
 
 
286
 
  pdf_add_dict(fontdict,
287
 
               pdf_new_name("DW"),
288
 
               pdf_new_number(dw));
289
 
  if (!empty) {
290
 
    pdf_add_dict(fontdict,
291
 
                 pdf_new_name("W"),
292
 
                 pdf_ref_obj(w_array));
293
 
  }
294
 
  pdf_release_obj(w_array);
295
 
 
296
 
  return;
297
 
}
298
 
 
299
 
static void
300
 
add_TTCIDVMetrics (pdf_obj *fontdict, struct tt_glyphs *g,
301
 
                   char *used_chars, unsigned char *cidtogidmap, unsigned short last_cid)
302
 
{
303
 
  pdf_obj *w2_array, *an_array = NULL;
304
 
  long cid, prev, start;
305
 
  double defaultVertOriginY, defaultAdvanceHeight;
306
 
  int    empty = 1;
307
 
 
308
 
  defaultVertOriginY   = PDFUNIT(g->default_advh - g->default_tsb);
309
 
  defaultAdvanceHeight = PDFUNIT(g->default_advh);
310
 
 
311
 
  w2_array = pdf_new_array();
312
 
  start = prev = 0;
313
 
  for (cid = 0; cid <= last_cid; cid++) {
314
 
    USHORT idx, gid;
315
 
    double vertOriginX, vertOriginY, advanceHeight;
316
 
 
317
 
    if (!is_used_char2(used_chars, cid))
318
 
      continue;
319
 
    gid = (cidtogidmap) ? ((cidtogidmap[2*cid] << 8)|cidtogidmap[2*cid+1]) : cid;
320
 
    idx = tt_get_index(g, cid);
321
 
    if (cid != 0 && idx == 0)
322
 
      continue;
323
 
    advanceHeight = PDFUNIT(g->gd[idx].advh);
324
 
    vertOriginX   = PDFUNIT(0.5*(g->gd[idx].advw));
325
 
    vertOriginY   = PDFUNIT(g->gd[idx].tsb + g->gd[idx].ury);
326
 
#if 0
327
 
    /*
328
 
     * c [w1_1y v_1x v_1y w1_2y v_2x v_2y ...]
329
 
     * Not working... Why?
330
 
     * Acrobat Reader:
331
 
     *  Wrong rendering, interpretation of position vector is wrong.
332
 
     * Xpdf and gs: ignores W2?
333
 
     */
334
 
    if (vertOriginY == defaultVertOriginY &&
335
 
        advanceHeight == defaultAdvanceHeight) {
336
 
      if (an_array) {
337
 
        pdf_add_array(w2_array, pdf_new_number(start));
338
 
        pdf_add_array(w2_array, an_array);
339
 
        an_array = NULL;
340
 
        empty = 0;
341
 
      }
342
 
    } else {
343
 
      if (cid != prev + 1 && an_array) {
344
 
        pdf_add_array(w2_array, pdf_new_number(start));
345
 
        pdf_add_array(w2_array, an_array);
346
 
        an_array = NULL;
347
 
        empty = 0;
348
 
      }
349
 
      if (an_array == NULL) {
350
 
        an_array = pdf_new_array();
351
 
        start = cid;
352
 
      }
353
 
      pdf_add_array(an_array, pdf_new_number(-advanceHeight));
354
 
      pdf_add_array(an_array, pdf_new_number(vertOriginX));
355
 
      pdf_add_array(an_array, pdf_new_number(vertOriginY));
356
 
      prev = cid;
357
 
    }
358
 
#else
359
 
    /*
360
 
     * c_first c_last w1_y v_x v_y
361
 
     * This form may hit Acrobat's implementation limit of array element size,
362
 
     * 8192. AFPL GhostScript 8.11 stops with rangecheck error with this.
363
 
     * Maybe GS's bug?
364
 
     */
365
 
    if (vertOriginY != defaultVertOriginY ||
366
 
        advanceHeight != defaultAdvanceHeight) {
367
 
      pdf_add_array(w2_array, pdf_new_number(cid));
368
 
      pdf_add_array(w2_array, pdf_new_number(cid));
369
 
      pdf_add_array(w2_array, pdf_new_number(-advanceHeight));
370
 
      pdf_add_array(w2_array, pdf_new_number(vertOriginX));
371
 
      pdf_add_array(w2_array, pdf_new_number(vertOriginY));
372
 
      empty = 0;
373
 
    }
374
 
#endif
375
 
  }
376
 
 
377
 
#if 0
378
 
  if (an_array) {
379
 
    pdf_add_array(w2_array, pdf_new_number(start));
380
 
    pdf_add_array(w2_array, an_array);
381
 
    empty = 0;
382
 
  }
383
 
#endif
384
 
 
385
 
  if (defaultVertOriginY != 880 || defaultAdvanceHeight != 1000) {
386
 
    an_array = pdf_new_array();
387
 
    pdf_add_array(an_array, pdf_new_number(defaultVertOriginY));
388
 
    pdf_add_array(an_array, pdf_new_number(-defaultAdvanceHeight));
389
 
    pdf_add_dict(fontdict, pdf_new_name ("DW2"), an_array);
390
 
  }
391
 
  if (!empty) {
392
 
    pdf_add_dict(fontdict,
393
 
                 pdf_new_name("W2"),
394
 
                 pdf_ref_obj(w2_array));
395
 
  }
396
 
  pdf_release_obj(w2_array);
397
 
 
398
 
  return;
399
 
}
400
 
 
401
 
/*
402
 
 * The following routine fixes few problems caused by vendor specific
403
 
 * Unicode mappings.
404
 
 */
405
 
 
406
 
#define FIX_CJK_UNIOCDE_SYMBOLS 1
407
 
 
408
 
static unsigned short
409
 
fix_CJK_symbols (unsigned short code)
410
 
{
411
 
  unsigned short alt_code;
412
 
  static struct
413
 
  {
414
 
    unsigned short alt1;
415
 
    unsigned short alt2;
416
 
  } CJK_Uni_symbols[] = {
417
 
    /*
418
 
     * Microsoft/Apple Unicode mapping difference:
419
 
     * They are taken from SJIS-Unicode mapping difference but nearly
420
 
     * same thing might be applied to Chinese (e.g., Big5) too.
421
 
     */
422
 
    {0x2014, 0x2015}, /* EM DASH <-> HORIZONTAL BAR */
423
 
    {0x2016, 0x2225}, /* DOUBLE VERTICAL LINE <-> PARALLEL TO */
424
 
    {0x203E, 0xFFE3}, /* OVERLINE <-> FULLWIDTH MACRON */
425
 
    {0x2026, 0x22EF}, /* HORIZONTAL ELLIPSIS <-> MIDLINE HORIZONTAL ELLIPSIS */
426
 
    {0x2212, 0xFF0D}, /* MINUS SIGN <-> FULLWIDTH HYPHEN-MINUS */
427
 
    {0x301C, 0xFF5E}, /* WAVE DASH <-> FULLWIDTH TILDE */
428
 
    {0xFFE0, 0x00A2}, /* FULLWIDTH CENT SIGN <-> CENT SIGN */
429
 
    {0xFFE1, 0x00A3}, /* FULLWIDTH POUND SIGN <-> POUND SIGN */
430
 
    {0xFFE2, 0x00AC}, /* FULLWIDTH NOT SIGN <-> NOT SIGN */
431
 
    {0xFFFF, 0xFFFF}, /* EOD */
432
 
  };
433
 
#define NUM_CJK_SYMBOLS (sizeof(CJK_Uni_symbols)/sizeof(CJK_Uni_symbols[0]))
434
 
  int i;
435
 
 
436
 
  alt_code = code;
437
 
  for (i = 0; i < NUM_CJK_SYMBOLS; i++) {
438
 
    if (CJK_Uni_symbols[i].alt1 == code) {
439
 
      alt_code = CJK_Uni_symbols[i].alt2;
440
 
      break;
441
 
    } else if (CJK_Uni_symbols[i].alt2 == code) {
442
 
      alt_code = CJK_Uni_symbols[i].alt1;
443
 
      break;
444
 
    }
445
 
  }
446
 
 
447
 
  return alt_code;
448
 
}
449
 
 
450
 
static long
451
 
cid_to_code (CMap *cmap, CID cid)
452
 
{
453
 
  unsigned char  inbuf[2], outbuf[32];
454
 
  long           inbytesleft = 2, outbytesleft = 32;
455
 
  unsigned char *p, *q;
456
 
 
457
 
  if (!cmap)
458
 
    return cid;
459
 
 
460
 
  inbuf[0] = (cid >> 8) & 0xff;
461
 
  inbuf[1] = cid & 0xff;
462
 
  p = inbuf; q = outbuf;
463
 
 
464
 
  CMap_decode_char(cmap, (const unsigned char **) &p, &inbytesleft, &q, &outbytesleft);
465
 
 
466
 
  if (inbytesleft != 0)
467
 
    return 0;
468
 
  else if (outbytesleft == 31)
469
 
    return (long) outbuf[0];
470
 
  else if (outbytesleft == 30)
471
 
    return (long) (outbuf[0] << 8|outbuf[1]);
472
 
  else if (outbytesleft == 28)
473
 
    return (long) (outbuf[0] << 24|outbuf[1] << 16|outbuf[2] << 8|outbuf[3]);
474
 
 
475
 
  return 0;
476
 
}
477
 
 
478
 
/* #define NO_GHOSTSCRIPT_BUG 1 */
479
 
 
480
 
void
481
 
CIDFont_type2_dofont (CIDFont *font)
482
 
{
483
 
  pdf_obj *fontfile;
484
 
  sfnt    *sfont;
485
 
  char    *h_used_chars, *v_used_chars, *used_chars;
486
 
  struct tt_glyphs *glyphs;
487
 
  CMap    *cmap = NULL;
488
 
  tt_cmap *ttcmap = NULL;
489
 
  unsigned long offset = 0;
490
 
  CID      cid, last_cid;
491
 
  unsigned char *cidtogidmap;
492
 
  USHORT   num_glyphs;
493
 
  int      i, glyph_ordering = 0, unicode_cmap = 0;
494
 
  FILE    *fp;
495
 
 
496
 
  if (!font->indirect)
497
 
    return;
498
 
 
499
 
  pdf_add_dict(font->fontdict, 
500
 
               pdf_new_name("FontDescriptor"), pdf_ref_obj(font->descriptor));
501
 
 
502
 
  if (CIDFont_is_BaseFont(font))
503
 
    return;
504
 
 
505
 
  /*
506
 
   * CIDSystemInfo comes here since Supplement can be increased.
507
 
   */
508
 
  {
509
 
    pdf_obj *tmp;
510
 
 
511
 
    tmp = pdf_new_dict ();
512
 
    pdf_add_dict(tmp,
513
 
                 pdf_new_name("Registry"),
514
 
                 pdf_new_string(font->csi->registry, strlen(font->csi->registry)));
515
 
    pdf_add_dict(tmp,
516
 
                 pdf_new_name("Ordering"),
517
 
                 pdf_new_string(font->csi->ordering, strlen(font->csi->ordering)));
518
 
    pdf_add_dict(tmp,
519
 
                 pdf_new_name("Supplement"),
520
 
                 pdf_new_number(font->csi->supplement));
521
 
    pdf_add_dict(font->fontdict, pdf_new_name("CIDSystemInfo"), tmp);
522
 
  }
523
 
 
524
 
  /* Quick exit for non-embedded & fixed-pitch font. */
525
 
  if (!CIDFont_get_embedding(font) &&
526
 
      (opt_flags & CIDFONT_FORCE_FIXEDPITCH)) {
527
 
    pdf_add_dict(font->fontdict,
528
 
                 pdf_new_name("DW"), pdf_new_number(1000.0));
529
 
    return;
530
 
  }
531
 
 
532
 
  fp = DPXFOPEN(font->ident, DPX_RES_TYPE_TTFONT);
533
 
  if (!fp)
534
 
    ERROR("Could not open TTF file: %s", font->ident);
535
 
 
536
 
  sfont = sfnt_open(fp);
537
 
  if (!sfont) {
538
 
    ERROR("Could not open TTF file: %s", font->ident);
539
 
  }
540
 
 
541
 
  switch (sfont->type) {
542
 
  case SFNT_TYPE_TTC:
543
 
    offset = ttc_read_offset(sfont, font->options->index);
544
 
    if (offset == 0)
545
 
      ERROR("Invalid TTC index in %s.", font->ident);
546
 
    break;
547
 
  case SFNT_TYPE_TRUETYPE:
548
 
    if (font->options->index > 0)
549
 
      ERROR("Found TrueType font file while expecting TTC file (%s).", font->ident);
550
 
    offset = 0;
551
 
    break;
552
 
  default:
553
 
    ERROR("Not a TrueType/TTC font (%s)?", font->ident);
554
 
    break;
555
 
  }
556
 
 
557
 
  if (sfnt_read_table_directory(sfont, offset) < 0)
558
 
    ERROR("Could not read TrueType table directory (%s).", font->ident);
559
 
 
560
 
  /*
561
 
   * Adobe-Identity means font's internal glyph ordering here.
562
 
   */
563
 
  if (!strcmp(font->csi->registry, "Adobe") &&
564
 
      !strcmp(font->csi->ordering, "Identity")) {
565
 
    glyph_ordering = 1;
566
 
  } else {
567
 
    glyph_ordering = 0;
568
 
  }
569
 
 
570
 
  /*
571
 
   * Select TrueType cmap table, find ToCode CMap for each TrueType encodings.
572
 
   */
573
 
  if (glyph_ordering) {
574
 
    ttcmap = NULL;
575
 
    cmap   = NULL;
576
 
  } else {
577
 
    /*
578
 
     * This part contains a bug. It may choose SJIS encoding TrueType cmap
579
 
     * table for Adobe-GB1.
580
 
     */
581
 
    for (i = 0; i <= KNOWN_ENCODINGS_MAX; i++) {
582
 
      ttcmap = tt_cmap_read(sfont,
583
 
                            known_encodings[i].platform,
584
 
                            known_encodings[i].encoding);
585
 
      if (ttcmap)
586
 
        break;
587
 
    }
588
 
    if (!ttcmap) {
589
 
      WARN("No usable TrueType cmap table found for font \"%s\".", font->ident);
590
 
      WARN("CID character collection for this font is set to \"%s-%s\"",
591
 
           font->csi->registry, font->csi->ordering);
592
 
      ERROR("Cannot continue without this...");
593
 
    } else if (i <= WIN_UCS_INDEX_MAX) {
594
 
      unicode_cmap = 1;
595
 
    } else {
596
 
      unicode_cmap = 0;
597
 
    }
598
 
 
599
 
    /*
600
 
     * NULL is returned if CMap is Identity CMap.
601
 
     */
602
 
    cmap = find_tocode_cmap(font->csi->registry,
603
 
                            font->csi->ordering, i);
604
 
  }
605
 
 
606
 
  glyphs = tt_build_init();
607
 
 
608
 
  last_cid   = 0;
609
 
  num_glyphs = 1; /* .notdef */
610
 
  used_chars = h_used_chars = v_used_chars = NULL;
611
 
  {
612
 
    Type0Font *parent;
613
 
    int        parent_id, c;
614
 
 
615
 
    if ((parent_id = CIDFont_get_parent_id(font, 0)) >= 0) {
616
 
      parent = Type0Font_cache_get(parent_id);
617
 
      h_used_chars = Type0Font_get_usedchars(parent);
618
 
    }
619
 
    if ((parent_id = CIDFont_get_parent_id(font, 1)) >= 0) {
620
 
      parent = Type0Font_cache_get(parent_id);
621
 
      v_used_chars = Type0Font_get_usedchars(parent);
622
 
    }
623
 
    if (!h_used_chars && !v_used_chars)
624
 
      ERROR("Unexpected error.");
625
 
 
626
 
    /*
627
 
     * Quick check of max CID.
628
 
     */
629
 
    c = 0;
630
 
    for (i = 8191; i >= 0; i--) {
631
 
      if (h_used_chars && h_used_chars[i] != 0) {
632
 
        last_cid = i * 8 + 7;
633
 
        c = h_used_chars[i];
634
 
        break;
635
 
      }
636
 
      if (v_used_chars && v_used_chars[i] != 0) {
637
 
        last_cid = i * 8 + 7;
638
 
        c = v_used_chars[i];
639
 
        break;
640
 
      }
641
 
    }
642
 
    if (last_cid > 0) {
643
 
      for (i = 0; i < 8; i++) {
644
 
        if ((c >> i) & 1)
645
 
          break;
646
 
        last_cid--;
647
 
      }
648
 
    }
649
 
    if (last_cid >= 0xFFFFu) {
650
 
      ERROR("CID count > 65535");
651
 
    }
652
 
  }
653
 
 
654
 
#ifndef NO_GHOSTSCRIPT_BUG
655
 
  cidtogidmap = NULL;
656
 
#else
657
 
  cidtogidmap = NEW((last_cid + 1) * 2, unsigned char);
658
 
  memset(cidtogidmap, 0, (last_cid + 1) * 2);
659
 
#endif /* !NO_GHOSTSCRIPT_BUG */
660
 
 
661
 
  /*
662
 
   * Map CIDs to GIDs.
663
 
   * Horizontal and vertical used_chars are merged.
664
 
   */
665
 
 
666
 
  /*
667
 
   * Horizontal
668
 
   */
669
 
  if (h_used_chars) {
670
 
    used_chars = h_used_chars;
671
 
    for (cid = 1; cid <= last_cid; cid++) {
672
 
      long           code;
673
 
      unsigned short gid;
674
 
 
675
 
      if (!is_used_char2(h_used_chars, cid))
676
 
        continue;
677
 
 
678
 
      if (glyph_ordering) {
679
 
        gid  = cid;
680
 
        code = cid;
681
 
      } else {
682
 
        code = cid_to_code(cmap, cid);
683
 
        gid  = tt_cmap_lookup(ttcmap, code);
684
 
#ifdef FIX_CJK_UNIOCDE_SYMBOLS
685
 
        if (gid == 0 && unicode_cmap) {
686
 
          long alt_code;
687
 
 
688
 
          alt_code = fix_CJK_symbols(code);
689
 
          if (alt_code != code) {
690
 
            gid = tt_cmap_lookup(ttcmap, alt_code);
691
 
            if (gid != 0) {
692
 
              WARN("Unicode char U+%04x replaced with U+%04x.",
693
 
                   code, alt_code);
694
 
            }
695
 
          }
696
 
        }
697
 
#endif /* FIX_CJK_UNIOCDE_SYMBOLS */
698
 
      }
699
 
 
700
 
      if (gid == 0) {
701
 
        WARN("Glyph missing in font. (CID=%u, code=0x%04x)", cid, code);
702
 
      }
703
 
 
704
 
      /* TODO: duplicated glyph */
705
 
#ifndef NO_GHOSTSCRIPT_BUG
706
 
      gid = tt_add_glyph(glyphs, gid, cid);
707
 
#else
708
 
      gid = tt_add_glyph(glyphs, gid, num_glyphs);
709
 
      cidtogidmap[2*cid  ] = gid >> 8;
710
 
      cidtogidmap[2*cid+1] = gid & 0xff;
711
 
#endif /* !NO_GHOSTSCRIPT_BUG */
712
 
 
713
 
      num_glyphs++;
714
 
    }
715
 
  }
716
 
 
717
 
  /*
718
 
   * Vertical
719
 
   */
720
 
  if (v_used_chars) {
721
 
    otl_gsub *gsub_list = NULL;
722
 
 
723
 
    /*
724
 
     * Require `vrt2' or `vert'.
725
 
     */
726
 
    if (glyph_ordering) {
727
 
      gsub_list = NULL;
728
 
    } else {
729
 
      gsub_list = otl_gsub_new();
730
 
      if (otl_gsub_add_feat(gsub_list,
731
 
                            "*", "*", "vrt2", sfont) < 0) {
732
 
        if (otl_gsub_add_feat(gsub_list,
733
 
                              "*", "*", "vert", sfont) < 0) {
734
 
          WARN("GSUB feature vrt2/vert not found.");
735
 
          otl_gsub_release(gsub_list);
736
 
          gsub_list = NULL;
737
 
        } else {
738
 
          otl_gsub_select(gsub_list, "*", "*", "vert");
739
 
        }
740
 
      } else {
741
 
        otl_gsub_select(gsub_list, "*", "*", "vrt2");
742
 
      }
743
 
    }
744
 
 
745
 
    for (cid = 1; cid <= last_cid; cid++) {
746
 
      long           code;
747
 
      unsigned short gid;
748
 
 
749
 
      if (!is_used_char2(v_used_chars, cid))
750
 
        continue;
751
 
 
752
 
      /* There may be conflict of horizontal and vertical glyphs
753
 
       * when font is used with /UCS. However, we simply ignore
754
 
       * that...
755
 
       */
756
 
      if (h_used_chars && is_used_char2(h_used_chars, cid)) {
757
 
        continue;
758
 
      }
759
 
 
760
 
      if (glyph_ordering) {
761
 
        gid  = cid;
762
 
        code = cid;
763
 
      } else {
764
 
        code = cid_to_code(cmap, cid);
765
 
        gid  = tt_cmap_lookup(ttcmap, code);
766
 
#ifdef FIX_CJK_UNIOCDE_SYMBOLS
767
 
        if (gid == 0 && unicode_cmap) {
768
 
          long alt_code;
769
 
 
770
 
          alt_code = fix_CJK_symbols(code);
771
 
          if (alt_code != code) {
772
 
            gid = tt_cmap_lookup(ttcmap, alt_code);
773
 
            if (gid != 0) {
774
 
              WARN("Unicode char U+%04x replaced with U+%04x.",
775
 
                   code, alt_code);
776
 
            }
777
 
          }
778
 
        }
779
 
#endif /* FIX_CJK_UNIOCDE_SYMBOLS */
780
 
      }
781
 
      if (gid == 0) {
782
 
        WARN("Glyph missing in font. (CID=%u, code=0x%04x)", cid, code);
783
 
      } else if (gsub_list) {
784
 
        otl_gsub_apply(gsub_list, &gid);
785
 
      }
786
 
 
787
 
#ifndef NO_GHOSTSCRIPT_BUG
788
 
      gid = tt_add_glyph(glyphs, gid, cid);
789
 
#else
790
 
      gid = tt_add_glyph(glyphs, gid, num_glyphs);
791
 
      cidtogidmap[2*cid  ] = gid >> 8;
792
 
      cidtogidmap[2*cid+1] = gid & 0xff;
793
 
#endif /* !NO_GHOSTSCRIPT_BUG */
794
 
 
795
 
      if (used_chars) /* merge vertical used_chars to horizontal */
796
 
        add_to_used_chars2(used_chars, cid);
797
 
 
798
 
      num_glyphs++;
799
 
    }
800
 
 
801
 
    if (gsub_list)
802
 
      otl_gsub_release(gsub_list);
803
 
 
804
 
    if (!used_chars) /* We have no horizontal. */
805
 
      used_chars = v_used_chars;
806
 
  }
807
 
 
808
 
  if (!used_chars)
809
 
    ERROR("Unexpected error.");
810
 
 
811
 
  tt_cmap_release(ttcmap);
812
 
 
813
 
  if (CIDFont_get_embedding(font)) {
814
 
    if (tt_build_tables(sfont, glyphs) < 0)
815
 
      ERROR("Could not created FontFile stream.");
816
 
    if (verbose > 1)
817
 
      MESG("[%u glyphs (Max CID: %u)]", glyphs->num_glyphs, last_cid);
818
 
  } else {
819
 
    if (tt_get_metrics(sfont, glyphs) < 0)
820
 
      ERROR("Reading glyph metrics failed...");
821
 
  }
822
 
 
823
 
  /*
824
 
   * DW, W, DW2, and W2
825
 
   */
826
 
  if (opt_flags & CIDFONT_FORCE_FIXEDPITCH) {
827
 
    pdf_add_dict(font->fontdict,
828
 
                 pdf_new_name("DW"), pdf_new_number(1000.0));
829
 
  } else {
830
 
    add_TTCIDHMetrics(font->fontdict, glyphs, used_chars, cidtogidmap, last_cid);
831
 
    if (v_used_chars)
832
 
      add_TTCIDVMetrics(font->fontdict, glyphs, used_chars, cidtogidmap, last_cid);
833
 
  }
834
 
 
835
 
  tt_build_finish(glyphs);
836
 
 
837
 
  /* Finish here if not embedded. */
838
 
  if (!CIDFont_get_embedding(font)) {
839
 
    if (cidtogidmap)
840
 
      RELEASE(cidtogidmap);
841
 
    sfnt_close(sfont);
842
 
    DPXFCLOSE(fp);
843
 
 
844
 
    return;
845
 
  }
846
 
 
847
 
  /* Create font file */
848
 
  for (i = 0; required_table[i].name; i++) {
849
 
    if (sfnt_require_table(sfont,
850
 
                           required_table[i].name,
851
 
                           required_table[i].must_exist) < 0) {
852
 
      ERROR("Some required TrueType table does not exist.");
853
 
    }
854
 
  }
855
 
 
856
 
  /*
857
 
   * FontFile2
858
 
   */
859
 
  fontfile = sfnt_create_FontFile_stream(sfont);
860
 
 
861
 
  sfnt_close(sfont);
862
 
  DPXFCLOSE(fp);
863
 
 
864
 
  if (!fontfile)
865
 
    ERROR("Could not created FontFile stream for \"%s\".", font->ident);
866
 
 
867
 
  if (verbose > 1) {
868
 
    MESG("[%ld bytes]", pdf_stream_length(fontfile));
869
 
  }
870
 
 
871
 
  pdf_add_dict(font->descriptor,
872
 
               pdf_new_name("FontFile2"),
873
 
               pdf_ref_obj (fontfile));
874
 
  pdf_release_obj(fontfile);
875
 
 
876
 
  /*
877
 
   * CIDSet
878
 
   */
879
 
  {
880
 
    pdf_obj *cidset;
881
 
 
882
 
    cidset = pdf_new_stream(STREAM_COMPRESS);
883
 
    pdf_add_stream(cidset, used_chars, last_cid/8 + 1);
884
 
    pdf_add_dict(font->descriptor,
885
 
                 pdf_new_name("CIDSet"),
886
 
                 pdf_ref_obj(cidset));
887
 
    pdf_release_obj(cidset);
888
 
  }
889
 
 
890
 
  /*
891
 
   * CIDToGIDMap
892
 
   */
893
 
  if (cidtogidmap) {
894
 
    pdf_obj *c2gmstream;
895
 
 
896
 
    c2gmstream = pdf_new_stream(STREAM_COMPRESS);
897
 
    pdf_add_stream(c2gmstream, cidtogidmap, (last_cid + 1) * 2);
898
 
    pdf_add_dict(font->fontdict,
899
 
                 pdf_new_name("CIDToGIDMap"),
900
 
                 pdf_ref_obj (c2gmstream));
901
 
    pdf_release_obj(c2gmstream);
902
 
    RELEASE(cidtogidmap);
903
 
  }
904
 
  
905
 
  return;
906
 
}
907
 
 
908
 
int
909
 
CIDFont_type2_open (CIDFont *font, const char *name,
910
 
                    CIDSysInfo *cmap_csi, cid_opt *opt)
911
 
{
912
 
  char    *fontname;
913
 
  sfnt    *sfont;
914
 
  unsigned long offset = 0;
915
 
  FILE    *fp;
916
 
 
917
 
  ASSERT(font && opt);
918
 
 
919
 
  fp = DPXFOPEN(name, DPX_RES_TYPE_TTFONT);
920
 
  if (!fp)
921
 
    return -1;
922
 
 
923
 
  sfont = sfnt_open(fp);
924
 
  if (!sfont) {
925
 
    DPXFCLOSE(fp);
926
 
    return -1;
927
 
  }
928
 
 
929
 
  switch (sfont->type) {
930
 
  case SFNT_TYPE_TTC:
931
 
    offset = ttc_read_offset(sfont, opt->index);
932
 
    break;
933
 
  case SFNT_TYPE_TRUETYPE:
934
 
    if (opt->index > 0) {
935
 
      ERROR("Invalid TTC index (not TTC font): %s", name);
936
 
    } else {
937
 
      offset = 0;
938
 
    }
939
 
    break;
940
 
  default:
941
 
    sfnt_close(sfont);
942
 
    DPXFCLOSE(fp);
943
 
    return -1;
944
 
    break;
945
 
  }
946
 
 
947
 
  if (sfnt_read_table_directory(sfont, offset) < 0) {
948
 
    ERROR("Reading TrueType table directory failed.");
949
 
  }
950
 
 
951
 
  {
952
 
    char *shortname;
953
 
    long  namelen;
954
 
 
955
 
    /* MAC-ROMAN-EN-POSTSCRIPT or WIN-UNICODE-EN(US)-POSTSCRIPT */
956
 
    shortname = NEW(PDF_NAME_LEN_MAX, char);
957
 
    namelen   = tt_get_ps_fontname(sfont, shortname, PDF_NAME_LEN_MAX);
958
 
    if (namelen == 0) {
959
 
      memset(shortname, 0, PDF_NAME_LEN_MAX);
960
 
      strncpy(shortname, name, PDF_NAME_LEN_MAX);
961
 
      namelen = strlen(shortname);
962
 
    }
963
 
    validate_name(shortname, namelen); /* for SJIS, UTF-16, ... string */
964
 
    /*
965
 
     * Strlen works, after validate_named string.
966
 
     * Mangled name requires more 7 bytes.
967
 
     * Style requires more 11 bytes.
968
 
     */
969
 
    fontname = NEW(strlen(shortname)+19, char);
970
 
    strcpy(fontname, shortname);
971
 
    RELEASE(shortname);
972
 
  }
973
 
 
974
 
  if (opt->embed && opt->style != FONT_STYLE_NONE) {
975
 
    WARN("Embedding disabled due to style option for %s.", name);
976
 
    opt->embed = 0;
977
 
  }
978
 
  switch (opt->style) {
979
 
  case FONT_STYLE_BOLD:
980
 
    strcat(fontname, ",Bold");
981
 
    break;
982
 
  case FONT_STYLE_ITALIC:
983
 
    strcat(fontname, ",Italic");
984
 
    break;
985
 
  case FONT_STYLE_BOLDITALIC:
986
 
    strcat(fontname, ",BoldItalic");
987
 
    break;
988
 
  }
989
 
  /*
990
 
   * CIDSystemInfo is determined from CMap or from map record option.
991
 
   */
992
 
  font->fontname = fontname;
993
 
  font->subtype  = CIDFONT_TYPE2;
994
 
  font->csi      = NEW(1, CIDSysInfo);
995
 
  if (opt->csi) {
996
 
    if (cmap_csi) {
997
 
      if (strcmp(opt->csi->registry, cmap_csi->registry) ||
998
 
          strcmp(opt->csi->ordering, cmap_csi->ordering)) {
999
 
        WARN("CID character collection mismatched:\n");
1000
 
        MESG("\tFont: %s-%s-%d\n",
1001
 
             opt->csi->registry, opt->csi->ordering, opt->csi->supplement);
1002
 
        MESG("\tCMap: %s-%s-%d\n",
1003
 
             cmap_csi->registry, cmap_csi->ordering, cmap_csi->supplement);
1004
 
        ERROR("Incompatible CMap specified for this font.");
1005
 
      }
1006
 
      if (opt->csi->supplement < cmap_csi->supplement) {
1007
 
        WARN("Supplmement value in CIDSystemInfo increased.");
1008
 
        WARN("Some characters may not shown.");
1009
 
        opt->csi->supplement = cmap_csi->supplement;
1010
 
      }
1011
 
    }
1012
 
    font->csi->registry   = NEW(strlen(opt->csi->registry)+1, char);
1013
 
    strcpy(font->csi->registry, opt->csi->registry);
1014
 
    font->csi->ordering   = NEW(strlen(opt->csi->ordering)+1, char);
1015
 
    strcpy(font->csi->ordering, opt->csi->ordering);
1016
 
    font->csi->supplement = opt->csi->supplement;
1017
 
  } else if (cmap_csi) {
1018
 
    font->csi->registry   = NEW(strlen(cmap_csi->registry)+1, char);
1019
 
    strcpy(font->csi->registry, cmap_csi->registry);
1020
 
    font->csi->ordering   = NEW(strlen(cmap_csi->ordering)+1, char);
1021
 
    strcpy(font->csi->ordering, cmap_csi->ordering);
1022
 
    font->csi->supplement = cmap_csi->supplement;
1023
 
  } else { /* This means font's internal glyph ordering. */
1024
 
    font->csi->registry   = NEW(strlen("Adobe")+1, char);
1025
 
    strcpy(font->csi->registry, "Adobe");
1026
 
    font->csi->ordering   = NEW(strlen("Identity")+1, char);
1027
 
    strcpy(font->csi->ordering, "Identity");
1028
 
    font->csi->supplement = 0;
1029
 
  }
1030
 
 
1031
 
  font->fontdict = pdf_new_dict();
1032
 
  pdf_add_dict(font->fontdict,
1033
 
               pdf_new_name("Type"),
1034
 
               pdf_new_name("Font"));
1035
 
  pdf_add_dict(font->fontdict,
1036
 
               pdf_new_name("Subtype"),
1037
 
               pdf_new_name("CIDFontType2"));
1038
 
 
1039
 
  font->descriptor = tt_get_fontdesc(sfont, &(opt->embed), 0);
1040
 
  if (!font->descriptor) {
1041
 
    ERROR("Could not obtain neccesary font info.");
1042
 
  }
1043
 
 
1044
 
  if (opt->embed) {
1045
 
    memmove(fontname + 7, fontname, strlen(fontname) + 1);
1046
 
    pdf_font_make_uniqueTag(fontname);
1047
 
    fontname[6] = '+';
1048
 
  }
1049
 
 
1050
 
  pdf_add_dict(font->descriptor,
1051
 
               pdf_new_name("FontName"),
1052
 
               pdf_new_name(fontname));
1053
 
  pdf_add_dict(font->fontdict, 
1054
 
               pdf_new_name("BaseFont"),
1055
 
               pdf_new_name(fontname));
1056
 
 
1057
 
  sfnt_close(sfont);
1058
 
  DPXFCLOSE(fp);
1059
 
 
1060
 
  /*
1061
 
   * Don't write fontdict here.
1062
 
   * /Supplement in /CIDSystemInfo may change.
1063
 
   */
1064
 
 
1065
 
  return 0;
1066
 
}
1067
 
 
1068
 
void
1069
 
CIDFont_type2_release (CIDFont *font)
1070
 
{
1071
 
  return;
1072
 
}