File: gdevpdtc.c

package info (click to toggle)
ghostscript 8.71~dfsg2-9+squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 79,896 kB
  • ctags: 80,654
  • sloc: ansic: 501,432; sh: 25,689; python: 4,853; cpp: 3,633; perl: 3,597; tcl: 1,480; makefile: 1,187; lisp: 407; asm: 284; xml: 263; awk: 66; csh: 17; yacc: 15
file content (865 lines) | stat: -rw-r--r-- 28,376 bytes parent folder | download | duplicates (2)
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/* Copyright (C) 2001-2006 Artifex Software, Inc.
   All Rights Reserved.
  
   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id: gdevpdtc.c 10603 2010-01-12 09:49:58Z masaki $ */
/* Composite and CID-based text processing for pdfwrite. */
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gxfcmap.h"
#include "gxfont.h"
#include "gxfont0.h"
#include "gxfont0c.h"
#include "gzpath.h"
#include "gxchar.h"
#include "gdevpsf.h"
#include "gdevpdfx.h"
#include "gdevpdtx.h"
#include "gdevpdtd.h"
#include "gdevpdtf.h"
#include "gdevpdts.h"
#include "gdevpdtt.h"

/* ---------------- Non-CMap-based composite font ---------------- */

/*
 * Process a text string in a composite font with FMapType != 9 (CMap).
 */
int
process_composite_text(gs_text_enum_t *pte, void *vbuf, uint bsize)
{
    byte *const buf = vbuf;
    pdf_text_enum_t *const penum = (pdf_text_enum_t *)pte;
    int code = 0;
    gs_string str;
    pdf_text_process_state_t text_state;
    pdf_text_enum_t curr, prev, out;
    gs_point total_width;
    const gs_matrix *psmat = 0;
    gs_font *prev_font = 0;
    gs_char chr, char_code = 0x0badf00d, space_char = GS_NO_CHAR;
    int buf_index = 0;
    bool return_width = (penum->text.operation & TEXT_RETURN_WIDTH);

    str.data = buf;
    if (return_width) {
	code = gx_path_current_point(penum->path, &penum->origin);
	if (code < 0)
	    return code;
    }
    if (pte->text.operation &
	(TEXT_FROM_ANY - (TEXT_FROM_STRING | TEXT_FROM_BYTES))
	)
	return_error(gs_error_rangecheck);
    if (pte->text.operation & TEXT_INTERVENE) {
	/* Not implemented. (PostScript doesn't even allow this case.) */
	return_error(gs_error_rangecheck);
    }
    total_width.x = total_width.y = 0;
    curr = *penum;
    prev = curr;
    out = curr;
    out.current_font = 0;
    /* Scan runs of characters in the same leaf font. */
    for ( ; ; ) {
	int font_code;
	gs_font *new_font = 0;

	gs_text_enum_copy_dynamic((gs_text_enum_t *)&out,
				  (gs_text_enum_t *)&curr, false);
	for (;;) {
	    gs_glyph glyph;

	    gs_text_enum_copy_dynamic((gs_text_enum_t *)&prev,
				      (gs_text_enum_t *)&curr, false);
	    font_code = pte->orig_font->procs.next_char_glyph
		((gs_text_enum_t *)&curr, &chr, &glyph);
	    /*
	     * We check for a font change by comparing the current
	     * font, rather than testing the return code, because
	     * it makes the control structure a little simpler.
	     */
	    switch (font_code) {
	    case 0:		/* no font change */
	    case 1:		/* font change */
		curr.returned.current_char = chr;
		char_code = gx_current_char((gs_text_enum_t *)&curr);
		new_font = curr.fstack.items[curr.fstack.depth].font;
		if (new_font != prev_font)
		    break;
		if (chr != (byte)chr)	/* probably can't happen */
		    return_error(gs_error_rangecheck);
		if (buf_index >= bsize)
		    return_error(gs_error_unregistered); /* Must not happen. */
		buf[buf_index] = (byte)chr;
		buf_index++;
		prev_font = new_font;
		psmat = &curr.fstack.items[curr.fstack.depth - 1].font->FontMatrix;
		if ((pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH) &&
			pte->text.space.s_char == char_code)
		    space_char = chr;
		continue;
	    case 2:		/* end of string */
		break;
	    default:	/* error */
		return font_code;
	    }
	    break;
	}
	str.size = buf_index;
	if (buf_index) {
	    /* buf_index == 0 is only possible the very first time. */
	    /*
	     * The FontMatrix of leaf descendant fonts is not updated
	     * by scalefont.  Compute the effective FontMatrix now.
	     */
	    gs_matrix fmat;

	    /* set up the base font : */
	    out.fstack.depth = 0;
	    out.fstack.items[out.fstack.depth].font = out.current_font = prev_font;
	    pte->current_font = prev_font;

	    /* Provide the decoded space character : */
	    out.text.space.s_char = space_char;

	    gs_matrix_multiply(&prev_font->FontMatrix, psmat, &fmat);
	    out.index = 0; /* Note : we don't reset out.xy_index here. */
	    code = pdf_process_string_aux(&out, &str, NULL, &fmat, &text_state);
	    if (code < 0)
		return code;
	    curr.xy_index = out.xy_index; /* pdf_encode_process_string advanced it. */
	    if (out.index < str.size) {
		gs_glyph glyph;

		/* Advance *pte exactly for out.index chars, 
		   because above we stored bytes into buf. */
		while (out.index--)
		    pte->orig_font->procs.next_char_glyph(pte, &chr, &glyph);
		font_code = 2; /* force exiting the loop */
	    } else {
		/* advance *pte past the current substring */
		gs_text_enum_copy_dynamic(pte, (gs_text_enum_t *)&prev, true);
	    }
	    pte->xy_index = out.xy_index;
	    if (return_width) {
		pte->returned.total_width.x = total_width.x +=
		    out.returned.total_width.x;
		pte->returned.total_width.y = total_width.y +=
		    out.returned.total_width.y;
	    }
	    pdf_text_release_cgp(penum);
	}
	if (font_code == 2)
	    break;
	buf[0] = (byte)chr;
	buf_index = 1;
	space_char = ((pte->text.operation & TEXT_ADD_TO_SPACE_WIDTH) &&
	              pte->text.space.s_char == char_code ? chr : ~0);
	psmat = &curr.fstack.items[curr.fstack.depth - 1].font->FontMatrix;
	prev_font = new_font;
    }
    if (!return_width)
	return 0;
    return pdf_shift_text_currentpoint(penum, &total_width);
}

/* ---------------- CMap-based composite font ---------------- */

/*
 * Process a text string in a composite font with FMapType == 9 (CMap).
 */
static const char *const standard_cmap_names[] = {
    /* The following were added in PDF 1.5. */

    "UniGB-UTF16-H", "UniGB-UTF16-V",


    "GBKp-EUC-H", "GBKp-EUC-V", 
    "HKscs-B5-H", "HKscs-B5-V",
    "UniCNS-UTF16-H", "UniCNS-UTF16-V",
    "UniJIS-UTF16-H", "UniJIS-UTF16-V",
    "UniKS-UTF16-H", "UniKS-UTF16-V",
#define END_PDF15_CMAP_NAMES_INDEX 12
    /* The following were added in PDF 1.4. */
    "GBKp-EUC-H", "GBKp-EUC-V",
    "GBK2K-H", "GBK2K-V",
    "HKscs-B5-H", "HKscs-B5-V",
#define END_PDF14_CMAP_NAMES_INDEX 18
    /* The following were added in PDF 1.3. */

    "GBpc-EUC-V",
    "GBK-EUC-H", "GBK-EUC-V",
    "UniGB-UCS2-H", "UniGB-UCS2-V",

    "ETenms-B5-H", "ETenms-B5-V",

    "UniCNS-UCS2-H", "UniCNS-UCS2-V",

    "90msp-RKSJ-H", "90msp-RKSJ-V",
    "EUC-H", "EUC-V",
    "UniJIS-UCS2-H", "UniJIS-UCS2-V",
    "UniJIS-UCS2-HW-H", "UniJIS-UCS2-HW-V",

    "KSCms-UHC-HW-H", "KSCms-UHC-HW-V",
    "UniKS-UCS2-H", "UniKS-UCS2-V",

#define END_PDF13_CMAP_NAMES_INDEX 39
    /* The following were added in PDF 1.2. */

    "GB-EUC-H", "GB-EUC-V",
    "GBpc-EUC-H"
    
    "B5pc-H", "B5pc-V",
    "ETen-B5-H", "ETen-B5-V",
    "CNS-EUC-H", "CNS-EUC-V",

    "83pv-RKSJ-H",
    "90ms-RKSJ-H", "90ms-RKSJ-V",
    "90pv-RKSJ-H",
    "Add-RKSJ-H", "Add-RKSJ-V",
    "Ext-RKSJ-H", "Ext-RKSJ-V",
    "H", "V",

    "KSC-EUC-H", "KSC-EUC-V",
    "KSCms-UHC-H", "KSCms-UHC-V",
    "KSCpc-EUC-H",

    "Identity-H", "Identity-V",

    0
};

static int
attach_cmap_resource(gx_device_pdf *pdev, pdf_font_resource_t *pdfont, 
		const gs_cmap_t *pcmap, int font_index_only)
{
    const char *const *pcmn =
	standard_cmap_names +
	(pdev->CompatibilityLevel < 1.3 ? END_PDF13_CMAP_NAMES_INDEX : 
	 pdev->CompatibilityLevel < 1.4 ? END_PDF14_CMAP_NAMES_INDEX : 
	 pdev->CompatibilityLevel < 1.5 ? END_PDF15_CMAP_NAMES_INDEX : 0);
    bool is_identity = false;
    pdf_resource_t *pcmres = 0;	/* CMap */
    int code;

    /* Make sure cmap names is properly initialised. Silences Coverity warning */
    if (!pcmn)
	return_error(gs_error_unknownerror);

    /*
     * If the CMap isn't standard, write it out if necessary.
     */
    for (; *pcmn != 0; ++pcmn)
	if (pcmap->CMapName.size == strlen(*pcmn) &&
	    !memcmp(*pcmn, pcmap->CMapName.data, pcmap->CMapName.size))
	    break;
    if (*pcmn == 0) {
	/* 
	 * PScript5.dll Version 5.2 creates identity CMaps with
	 * instandard name. Check this specially here
	 * and later replace with a standard name.
	 * This is a temporary fix for SF bug #615994 "CMAP is corrupt".
	 */
	is_identity = gs_cmap_is_identity(pcmap, font_index_only);
    }
    if (*pcmn == 0 && !is_identity) {		/* not standard */
	pcmres = pdf_find_resource_by_gs_id(pdev, resourceCMap, pcmap->id + font_index_only);
	if (pcmres == 0) {
	    /* Create and write the CMap object. */
	    code = pdf_cmap_alloc(pdev, pcmap, &pcmres, font_index_only);
	    if (code < 0)
		return code;
	}
    }
    if (pcmap->from_Unicode) {
	gs_cmap_ranges_enum_t renum;

	gs_cmap_ranges_enum_init(pcmap, &renum);
	if (gs_cmap_enum_next_range(&renum) == 0 && renum.range.size == 2 &&
	    gs_cmap_enum_next_range(&renum) == 1) {
	    /*
	     * Exactly one code space range, of size 2.  Add an identity
	     * ToUnicode CMap.
	     */
	    if (!pdev->Identity_ToUnicode_CMaps[pcmap->WMode]) {
		/* Create and write an identity ToUnicode CMap now. */
		gs_cmap_t *pidcmap;

		code = gs_cmap_create_char_identity(&pidcmap, 2, pcmap->WMode,
						    pdev->memory);
		if (code < 0)
		    return code;
		pidcmap->CMapType = 2;	/* per PDF Reference */
		pidcmap->ToUnicode = true;
		code = pdf_cmap_alloc(pdev, pidcmap,
				&pdev->Identity_ToUnicode_CMaps[pcmap->WMode], -1);
		if (code < 0)
		    return code;
	    }
	    pdfont->res_ToUnicode = pdev->Identity_ToUnicode_CMaps[pcmap->WMode];
	}
    }
    if (pcmres || is_identity) {
	uint size = pcmap->CMapName.size;
	byte *chars = gs_alloc_string(pdev->pdf_memory, size,
				      "pdf_font_resource_t(CMapName)");

	if (chars == 0)
	    return_error(gs_error_VMerror);
	memcpy(chars, pcmap->CMapName.data, size);
	if (is_identity)
	    strcpy(pdfont->u.type0.Encoding_name, 
		    (pcmap->WMode ? "/Identity-V" : "/Identity-H"));
	else
	    sprintf(pdfont->u.type0.Encoding_name, "%ld 0 R",
		    pdf_resource_id(pcmres));
	pdfont->u.type0.CMapName.data = chars;
	pdfont->u.type0.CMapName.size = size;
    } else {
	if (!*pcmn)
	    /* Should not be possible, if *pcmn is NULL then either 
	     * is_identity is true or we create pcmres.
	     */ 
	    return_error(gs_error_invalidfont);

	sprintf(pdfont->u.type0.Encoding_name, "/%s", *pcmn);
	pdfont->u.type0.CMapName.data = (const byte *)*pcmn;
	pdfont->u.type0.CMapName.size = strlen(*pcmn);
	pdfont->u.type0.cmap_is_standard = true;
    }
    pdfont->u.type0.WMode = pcmap->WMode;
    return 0;
}

/* Record widths and CID => GID mappings. */
static int
scan_cmap_text(pdf_text_enum_t *pte, void *vbuf)
{
    gx_device_pdf *pdev = (gx_device_pdf *)pte->dev;
    /* gs_font_type0 *const font = (gs_font_type0 *)pte->current_font;*/ /* Type 0, fmap_CMap */
    gs_font_type0 *const font = (gs_font_type0 *)pte->orig_font; /* Type 0, fmap_CMap */
    /* Not sure. Changed for CDevProc callout. Was pte->current_font */
    gs_text_enum_t scan = *(gs_text_enum_t *)pte;
    int wmode = font->WMode, code, rcode = 0;
    pdf_font_resource_t *pdsubf0 = NULL;
    gs_font *subfont0 = NULL, *saved_subfont = NULL;
    uint index = scan.index, xy_index = scan.xy_index, start_index = index;
    uint font_index0 = 0x7badf00d;
    bool done = false;
    pdf_char_glyph_pairs_t p;
    gs_glyph *type1_glyphs = (gs_glyph *)vbuf;
    int num_type1_glyphs = 0;

    p.num_all_chars = 1;
    p.num_unused_chars = 1;
    p.unused_offset = 0;
    pte->returned.total_width.x = pte->returned.total_width.y = 0;;
    for (;;) {
	uint break_index, break_xy_index;
	uint font_index = 0x7badf00d;
	gs_const_string str;
	pdf_text_process_state_t text_state;
	pdf_font_resource_t *pdsubf;
	gs_font *subfont = NULL;
	gs_point wxy;
	bool font_change = 0;

	code = gx_path_current_point(pte->path, &pte->origin);
	if (code < 0)
	    return code;
        do {
	    gs_char chr;
	    gs_glyph glyph;
	    pdf_font_descriptor_t *pfd;
	    byte *glyph_usage;
	    double *real_widths, *w, *v, *w0;
	    int char_cache_size, width_cache_size;
	    gs_char cid;

	    break_index = scan.index;
	    break_xy_index = scan.xy_index;
	    code = font->procs.next_char_glyph(&scan, &chr, &glyph);
	    if (code == 2) {		/* end of string */
		done = true;
		break;
	    }
	    if (code < 0)
	    	return code;
	    subfont = scan.fstack.items[scan.fstack.depth].font;
	    font_index = scan.fstack.items[scan.fstack.depth - 1].index;
	    scan.xy_index++;
	    if (glyph == GS_NO_GLYPH)
		glyph = GS_MIN_CID_GLYPH;
	    cid = glyph - GS_MIN_CID_GLYPH;
	    switch (subfont->FontType) {
		case ft_encrypted:
		case ft_encrypted2:{
		    if (glyph == GS_MIN_CID_GLYPH) {
			glyph = subfont->procs.encode_char(subfont, chr, GLYPH_SPACE_NAME);
    		    }
		    type1_glyphs[num_type1_glyphs] = glyph;
		    num_type1_glyphs++;
		    break;
		}
		case ft_CID_encrypted:
		case ft_CID_TrueType: {
		    p.s[0].glyph = glyph;
		    p.s[0].chr = cid;
		    code = pdf_obtain_cidfont_resource(pdev, subfont, &pdsubf, &p);
		    if (code < 0)
			return code;
		    break;
		}
		case ft_user_defined: {
		    gs_string str1;

		    str1.data = NULL;
		    str1.size = 0;
		    pte->current_font = subfont;
		    code = pdf_obtain_font_resource(pte, &str1, &pdsubf);
		    if (code < 0)
			return code;
		    cid = pdf_find_glyph(pdsubf, glyph);
		    if (cid == GS_NO_CHAR) {
			code = pdf_make_font3_resource(pdev, subfont, &pdsubf);
			if (code < 0) 		    
			    return code;
			code = pdf_attach_font_resource(pdev, subfont, pdsubf);
			if (code < 0) 		    
			    return code;
			cid = 0;
		    }
		    break;
		}
		default:
		    /* An unsupported case, fall back to default implementation. */
		    return_error(gs_error_rangecheck);
	    }
	    code = pdf_attached_font_resource(pdev, (gs_font *)subfont, &pdsubf, 
				       &glyph_usage, &real_widths, &char_cache_size, &width_cache_size);
	    if (code < 0)
		return code;
	    if (break_index > start_index && pdev->charproc_just_accumulated)
		break;
	    if (subfont->FontType == ft_user_defined &&
		(break_index > start_index || !pdev->charproc_just_accumulated) &&
		!(pdsubf->u.simple.s.type3.cached[cid >> 3] & (0x80 >> (cid & 7)))) {
		pte->current_font = subfont;
		return gs_error_undefined;
	    }
	    if (subfont->FontType == ft_encrypted || subfont->FontType == ft_encrypted2) {
		font_change = (subfont != subfont0 && subfont0 != NULL);
		if (font_change) {
		    saved_subfont = subfont;
		    subfont = subfont0;
		    num_type1_glyphs--;
		}
    	    } else
		font_change = (pdsubf != pdsubf0 && pdsubf0 != NULL);
	    if (!font_change) {
		pdsubf0 = pdsubf;
		font_index0 = font_index;
		subfont0 = subfont;
	    }
	    if (subfont->FontType != ft_encrypted && subfont->FontType != ft_encrypted2) {
		pfd = pdsubf->FontDescriptor;
		code = pdf_resize_resource_arrays(pdev, pdsubf, cid + 1);
		if (code < 0)
		    return code;
		if (subfont->FontType == ft_CID_encrypted || subfont->FontType == ft_CID_TrueType) {
		    if (cid >=width_cache_size) {
			/* fixme: we add the CID=0 glyph as CID=cid glyph to the output font.    
		           Really it must not add and leave the CID undefined. */
			cid = 0; /* notdef. */
		    }
		} 
		if (cid >= char_cache_size || cid >= width_cache_size)
		    return_error(gs_error_unregistered); /* Must not happen */
		if (pdsubf->FontType == ft_user_defined  || pdsubf->FontType == ft_encrypted || 
				pdsubf->FontType == ft_encrypted2) {
		    code += 0; /* A good place for a breakpoint. */
		} else {
		    pdf_font_resource_t *pdfont;
		    bool notdef_subst = false;

		    code = pdf_obtain_cidfont_widths_arrays(pdev, pdsubf, wmode, &w, &w0, &v);
		    if (code < 0)
			return code;
		    code = pdf_obtain_parent_type0_font_resource(pdev, pdsubf, font_index,
			&font->data.CMap->CMapName, &pdfont);
		    if (code < 0)
			return code;
		    if (pdf_is_CID_font(subfont)) {
			if (subfont->procs.decode_glyph((gs_font *)subfont, glyph) != GS_NO_CHAR) {
			    /* Since PScript5.dll creates GlyphNames2Unicode with character codes
			       instead CIDs, and with the WinCharSetFFFF-H2 CMap
		               character codes appears different than CIDs (Bug 687954),
		               pass the character code intead the CID. */
			    code = pdf_add_ToUnicode(pdev, subfont, pdfont, 
				chr + GS_MIN_CID_GLYPH, chr, NULL);
			} else {
			    /* If we interpret a PDF document, ToUnicode 
			       CMap may be attached to the Type 0 font. */
			    code = pdf_add_ToUnicode(pdev, pte->orig_font, pdfont, 
				chr + GS_MIN_CID_GLYPH, chr, NULL);
			}
		    } else
			code = pdf_add_ToUnicode(pdev, subfont, pdfont, glyph, cid, NULL);
		    if (code < 0)
			return code;
		    /*  We can't check pdsubf->used[cid >> 3] here,
			because it mixed data for different values of WMode. 
			Perhaps pdf_font_used_glyph returns fast with reused glyphs.
		     */
		    code = pdf_font_used_glyph(pfd, glyph, (gs_font_base *)subfont);
		    if (code == gs_error_rangecheck) {
			if (!(pdsubf->used[cid >> 3] & (0x80 >> (cid & 7)))) {
			    char buf[gs_font_name_max + 1];
			    int l = min(sizeof(buf) - 1, subfont->font_name.size);
    
			    memcpy(buf, subfont->font_name.chars, l);
			    buf[l] = 0;
			    eprintf3("Missing glyph CID=%d, glyph=%04x in the font %s . The output PDF may fail with some viewers.\n", 
				    (int)cid, (unsigned int)(glyph - GS_MIN_CID_GLYPH), buf);
			    pdsubf->used[cid >> 3] |= 0x80 >> (cid & 7);
			}
			cid = 0, code = 1;  /* undefined glyph. */
			notdef_subst = true;
			/* If this is the first use of CID=0, get its width */
			if (pdsubf->Widths[cid] == 0) {
			    pdf_glyph_widths_t widths;

			    code = pdf_glyph_widths(pdsubf, wmode, glyph, (gs_font *)subfont, &widths,
				pte->cdevproc_callout ? pte->cdevproc_result : NULL);
			}
		    } else if (code < 0)
			return code;
		    if ((code == 0 /* just copied */ || pdsubf->Widths[cid] == 0) && !notdef_subst) {
			pdf_glyph_widths_t widths;

		    code = pdf_glyph_widths(pdsubf, wmode, glyph, (gs_font *)subfont, &widths,
			pte->cdevproc_callout ? pte->cdevproc_result : NULL);
		    if (code < 0)
			return code;
		    if (code == TEXT_PROCESS_CDEVPROC) {
			pte->returned.current_glyph = glyph;
			pte->current_font = subfont;
			rcode = TEXT_PROCESS_CDEVPROC;
			break;
		    }
		    if (code == 0) { /* OK to cache */
			if (cid > pdsubf->count)
			    return_error(gs_error_unregistered); /* Must not happen. */
			w[cid] = widths.Width.w;
			if (v != NULL) {
			    v[cid * 2 + 0] = widths.Width.v.x;
			    v[cid * 2 + 1] = widths.Width.v.y;
			}
			real_widths[cid] = widths.real_width.w;
		    }
		    if (wmode) {
			/* Since AR5 use W or DW to compute the x-coordinate of
			   v-vector, comupte and store the glyph width for WMode 0. */
			/* fixme : skip computing real_width here. */
			code = pdf_glyph_widths(pdsubf, 0, glyph, (gs_font *)subfont, &widths,
			    pte->cdevproc_callout ? pte->cdevproc_result : NULL);
			if (code < 0)
			    return code;
			w0[cid] = widths.Width.w;
		    }
		    if (pdsubf->u.cidfont.CIDToGIDMap != 0) {
			gs_font_cid2 *subfont2 = (gs_font_cid2 *)subfont;
    
			pdsubf->u.cidfont.CIDToGIDMap[cid] =
			subfont2->cidata.CIDMap_proc(subfont2, glyph);
		    }
		}
		if (wmode)
		    pdsubf->u.cidfont.used2[cid >> 3] |= 0x80 >> (cid & 7);
		}
		pdsubf->used[cid >> 3] |= 0x80 >> (cid & 7);
	    }
	    if (pte->cdevproc_callout) {
		/* Only handle a single character because its width is stored 
	          into pte->cdevproc_result, and process_text_modify_width neds it. 
                  fixme: next time take from w, v, real_widths. */
		break_index = scan.index;
		break_xy_index = scan.xy_index;
		break;
	    }
	} while (!font_change);
	if (break_index > index) {
	    pdf_font_resource_t *pdfont;
	    gs_matrix m3;
	    int xy_index_step = (!(pte->text.operation & TEXT_REPLACE_WIDTHS) ? 0 :
				 pte->text.x_widths == pte->text.y_widths ? 2 : 1);
	    gs_text_params_t save_text;

	    if (!subfont && num_type1_glyphs != 0)
 		subfont = subfont0;
	    if (subfont && (subfont->FontType == ft_encrypted || subfont->FontType == ft_encrypted2)) {
		int save_op = pte->text.operation;
		gs_font *save_font = pte->current_font;
		const gs_glyph *save_data = pte->text.data.glyphs;

		pte->current_font = subfont;
		pte->text.operation |= TEXT_FROM_GLYPHS;
		pte->text.data.glyphs = type1_glyphs;
		str.data = ((const byte *)vbuf) + ((pte->text.size - pte->index) * sizeof(gs_glyph)); 
		str.size = num_type1_glyphs;
		code = pdf_obtain_font_resource_unencoded(pte, (const gs_string *)&str, &pdsubf0, 
		    type1_glyphs);
		if (code < 0) {
		    /* Replace the modified values, fall back to default implementation
		     * (type 3 bitmap image font)
		     */
		    pte->current_font = save_font;
		    pte->text.operation |= save_op;
		    pte->text.data.glyphs = save_data;
		    return(code);
		}
		memcpy((void *)scan.text.data.bytes, (void *)str.data, str.size);
		str.data = scan.text.data.bytes;
		pdsubf = pdsubf0;
		pte->text.operation = save_op;
	    }
	    pte->current_font = subfont0;
	    if (!subfont0 || !pdsubf0)
		/* This should be impossible */
		return_error(gs_error_invalidfont);

	    code = gs_matrix_multiply(&subfont0->FontMatrix, &font->FontMatrix, &m3); 
	    /* We thought that it should be gs_matrix_multiply(&font->FontMatrix, &subfont0->FontMatrix, &m3); */
	    if (code < 0) 
		return code;
	    if (pdsubf0->FontType == ft_user_defined  || pdsubf->FontType == ft_encrypted || 
		pdsubf->FontType == ft_encrypted2)
		    pdfont = pdsubf0;
	    else {
		code = pdf_obtain_parent_type0_font_resource(pdev, pdsubf0, font_index0, 
			    &font->data.CMap->CMapName, &pdfont);
		if (code < 0)
		    return code;
		if (!pdfont->u.type0.Encoding_name[0]) {
		    /*
		    * If pdfont->u.type0.Encoding_name is set, 
		    * a CMap resource is already attached.
		    * See attach_cmap_resource.
		    */
		    code = attach_cmap_resource(pdev, pdfont, font->data.CMap, font_index0);
		    if (code < 0)
			return code;
		}
	    }
	    pdf_set_text_wmode(pdev, font->WMode);
	    code = pdf_update_text_state(&text_state, (pdf_text_enum_t *)pte, pdfont, &m3);
	    if (code < 0)
		return code;
	    /* process_text_modify_width breaks text parameters. 
	       We would like to improve it someday. 
	       Now save them locally and restore after the call. */
	    save_text = pte->text;
	    if (subfont && (subfont->FontType != ft_encrypted && 
		subfont->FontType != ft_encrypted2)) {
    		/* If we are a type 1 descendant, we already sorted this out above */
		str.data = scan.text.data.bytes + index;
		str.size = break_index - index;
	    }
	    if (pte->text.operation & TEXT_REPLACE_WIDTHS) {
		if (pte->text.x_widths != NULL)
		    pte->text.x_widths += xy_index * xy_index_step;
		if (pte->text.y_widths != NULL)
		    pte->text.y_widths += xy_index * xy_index_step;
	    }
	    pte->xy_index = 0;
	    if (subfont && (subfont->FontType == ft_encrypted || 
		subfont->FontType == ft_encrypted2)) {
		gs_font *f = pte->orig_font;

		adjust_first_last_char(pdfont, (byte *)str.data, str.size);

		/* Make sure we use the descendant font, not the original type 0 ! */
		pte->orig_font = subfont;
		code = process_text_modify_width((pdf_text_enum_t *)pte, 
		    (gs_font *)subfont, &text_state, &str, &wxy, type1_glyphs, false);
		if (code < 0)
		    return(code);
    		if(font_change) {
		    type1_glyphs[0] = type1_glyphs[num_type1_glyphs];
		    num_type1_glyphs = 1;
		    subfont = saved_subfont;
		    font_change = 0;
		} else {
		    num_type1_glyphs = 0;
		}
		pte->orig_font = f;
	    } else {
		code = process_text_modify_width((pdf_text_enum_t *)pte, (gs_font *)font,
		    &text_state, &str, &wxy, NULL, true);
	    }
	    if (pte->text.operation & TEXT_REPLACE_WIDTHS) {
		if (pte->text.x_widths != NULL)
		    pte->text.x_widths -= xy_index * xy_index_step;
		if (pte->text.y_widths != NULL)
		    pte->text.y_widths -= xy_index * xy_index_step;
	    }
	    pte->text = save_text;
	    pte->cdevproc_callout = false;
	    if (code < 0) {
		pte->index = index;
		pte->xy_index = xy_index;
		return code;
	    }
	    pte->index = break_index;
	    pte->xy_index = break_xy_index;
	    code = pdf_shift_text_currentpoint(pte, &wxy);
	    if (code < 0)
		return code;
	} 
	pdf_text_release_cgp(pte);
	index = break_index;
	xy_index = break_xy_index;
	if (done || rcode != 0)
	    break;
	pdsubf0 = pdsubf;
	font_index0 = font_index;
	subfont0 = subfont;
    }
    pte->index = index;
    pte->xy_index = xy_index;
    return rcode;
}

int
process_cmap_text(gs_text_enum_t *penum, void *vbuf, uint bsize)
{
    int code;
    pdf_text_enum_t *pte = (pdf_text_enum_t *)penum;

    if (pte->text.operation &
	(TEXT_FROM_ANY - (TEXT_FROM_STRING | TEXT_FROM_BYTES))
	)
	return_error(gs_error_rangecheck);
    if (pte->text.operation & TEXT_INTERVENE) {
	/* Not implemented.  (PostScript doesn't allow TEXT_INTERVENE.) */
	return_error(gs_error_rangecheck);
    }
    code = scan_cmap_text(pte, vbuf);
    if (code == TEXT_PROCESS_CDEVPROC)
	pte->cdevproc_callout = true;
    else
	pte->cdevproc_callout = false;
    return code;
}

/* ---------------- CIDFont ---------------- */

/*
 * Process a text string in a CIDFont.  (Only glyphshow is supported.)
 */
int
process_cid_text(gs_text_enum_t *pte, void *vbuf, uint bsize)
{
    pdf_text_enum_t *penum = (pdf_text_enum_t *)pte;
    uint operation = pte->text.operation;
    gs_text_enum_t save;
    gs_font *scaled_font = pte->current_font; /* CIDFont */
    gs_font *font;		/* unscaled font (CIDFont) */
    const gs_glyph *glyphs;
    gs_matrix scale_matrix;
    pdf_font_resource_t *pdsubf; /* CIDFont */
    gs_font_type0 *font0 = NULL;
    uint size;
    int code;

    if (operation & TEXT_FROM_GLYPHS) {
	glyphs = pte->text.data.glyphs;
	size = pte->text.size - pte->index;
    } else if (operation & TEXT_FROM_SINGLE_GLYPH) {
	glyphs = &pte->text.data.d_glyph;
	size = 1;
    } else if (operation & TEXT_FROM_STRING) {
	glyphs = &pte->outer_CID;
	size = 1;
    } else
	return_error(gs_error_rangecheck);

    /*
     * PDF doesn't support glyphshow directly: we need to create a Type 0
     * font with an Identity CMap.  Make sure all the glyph numbers fit
     * into 16 bits.  (Eventually we should support wider glyphs too,
     * but this would require a different CMap.)
     */
    if (bsize < size * 2)
	return_error(gs_error_unregistered); /* Must not happen. */
    {
	int i;
	byte *pchars = vbuf;

	for (i = 0; i < size; ++i) {
	    ulong gnum = glyphs[i] - GS_MIN_CID_GLYPH;

	    if (gnum & ~0xffffL)
		return_error(gs_error_rangecheck);
	    *pchars++ = (byte)(gnum >> 8);  
	    *pchars++ = (byte)gnum;
	}
    }

    /* Find the original (unscaled) version of this font. */

    for (font = scaled_font; font->base != font; )
	font = font->base;
    /* Compute the scaling matrix. */
    gs_matrix_invert(&font->FontMatrix, &scale_matrix);
    gs_matrix_multiply(&scale_matrix, &scaled_font->FontMatrix, &scale_matrix);

    /* Find or create the CIDFont resource. */

    code = pdf_obtain_font_resource(penum, NULL, &pdsubf);
    if (code < 0)
	return code;

    /* Create the CMap and Type 0 font if they don't exist already. */

    if (pdsubf->u.cidfont.glyphshow_font_id != 0)
 	font0 = (gs_font_type0 *)gs_find_font_by_id(font->dir, 
 		    pdsubf->u.cidfont.glyphshow_font_id, &scaled_font->FontMatrix);
    if (font0 == NULL) {
  	code = gs_font_type0_from_cidfont(&font0, font, font->WMode,
 					  &scale_matrix, font->memory);
	if (code < 0)
	    return code;
 	pdsubf->u.cidfont.glyphshow_font_id = font0->id;
    }

    /* Now handle the glyphshow as a show in the Type 0 font. */

    save = *pte;
    pte->current_font = pte->orig_font = (gs_font *)font0;
    /* Patch the operation temporarily for init_fstack. */
    pte->text.operation = (operation & ~TEXT_FROM_ANY) | TEXT_FROM_BYTES;
    /* Patch the data for process_cmap_text. */
    pte->text.data.bytes = vbuf;
    pte->text.size = size * 2;
    pte->index = 0;
    gs_type0_init_fstack(pte, pte->current_font);
    code = process_cmap_text(pte, vbuf, bsize);
    pte->current_font = scaled_font;
    pte->orig_font = save.orig_font;
    pte->text = save.text;
    pte->index = save.index + pte->index / 2;
    pte->fstack = save.fstack;
    return code;
}