summaryrefslogtreecommitdiff
path: root/gs/devices/vector/gdevpdtd.c
blob: 395d0531f3852de372e00fb0379f4df4edbf8ab4 (plain)
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
/* Copyright (C) 2001-2012 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 the license contained in the file LICENSE in this distribution.

   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.
*/


/* FontDescriptor implementation for pdfwrite */
#include "math_.h"
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsrect.h"		/* for rect_merge */
#include "gscencs.h"
#include "gxfcopy.h"
#include "gdevpdfx.h"
#include "gdevpdfo.h"		/* for object->written */
#include "gdevpdtb.h"
#include "gdevpdtd.h"
#include "gdevpdtf.h"

/* ================ Types and structures ================ */

/*
 * There are many different flavors of FontDescriptor.  The first division
 * is between FontDescriptors for actual fonts, and character class entries
 * in the FD dictionary of a CID-keyed font.  The latter include metrics and
 * a FontName but nothing else.  We represent these with a different C
 * structure (pdf_sub_font_descriptor_t).
 *
 * Descriptors for actual fonts have three major orthogonal properties:
 *
 *	- Whether they represent a CID-keyed font or a name-keyed
 *	  (non-CID-keyed) font.  We distinguish this by the FontType
 *	  of the saved font (see below).
 *
 *	- Whether the font they represent is embedded.
 *
 *	- Whether the font they represent is a complete font or a subset.
 *	  We always track which glyphs in a font are used: client code
 *	  decides whether to treat the font as a subset when the
 *	  descriptor (and, if embedded, the font) is written.
 *
 * Note that non-embedded fonts in the base set of 14 do not have
 * descriptors, nor do Type 0 or (synthetic bitmap) Type 3 fonts.
 */
/*
 * Start by defining the elements common to font descriptors and sub-font
 * (character class) descriptors.
 */
typedef struct pdf_font_descriptor_values_s {
    /* Required elements */
    int Ascent, CapHeight, Descent, ItalicAngle, StemV;
    gs_int_rect FontBBox;
    gs_string FontName;
    uint Flags;
    /* Optional elements (default to 0) */
    int AvgWidth, Leading, MaxWidth, MissingWidth, StemH, XHeight;
} pdf_font_descriptor_values_t;
typedef struct pdf_font_descriptor_common_s pdf_font_descriptor_common_t;
struct pdf_font_descriptor_common_s {
    pdf_resource_common(pdf_font_descriptor_common_t);
    pdf_font_descriptor_values_t values;
};
/* Flag bits */
/*#define FONT_IS_FIXED_WIDTH (1<<0)*/  /* defined in gxfont.h */
#define FONT_IS_SERIF (1<<1)
#define FONT_IS_SYMBOLIC (1<<2)
#define FONT_IS_SCRIPT (1<<3)
/*
 * There is confusion over the meaning of the 1<<5 bit.  According to the
 * published PDF documentation, in PDF 1.1, it meant "font uses
 * StandardEncoding", and as of PDF 1.2, it means "font uses (a subset of)
 * the Adobe standard Latin character set"; however, Acrobat Reader 3 and 4
 * seem to use the former interpretation, even if the font is embedded and
 * the file is identified as a PDF 1.2 file.  We have to use the former
 * interpretation in order to produce output that Acrobat will handle
 * correctly.
 */
#define FONT_USES_STANDARD_ENCODING (1<<5) /* always used */
#define FONT_IS_ADOBE_ROMAN (1<<5) /* never used */
#define FONT_IS_ITALIC (1<<6)
#define FONT_IS_ALL_CAPS (1<<16)
#define FONT_IS_SMALL_CAPS (1<<17)
#define FONT_IS_FORCE_BOLD (1<<18)

/*
 * Define a (top-level) FontDescriptor.  CID-keyed vs. non-CID-keyed fonts
 * are distinguished by their FontType.
 */
#ifndef pdf_base_font_DEFINED
#  define pdf_base_font_DEFINED
typedef struct pdf_base_font_s pdf_base_font_t;
#endif
struct pdf_font_descriptor_s {
    pdf_font_descriptor_common_t common;
    pdf_base_font_t *base_font;
    font_type FontType;		/* (copied from base_font) */
    bool embed;
    struct cid_ {		/* (CIDFonts only) */
        cos_dict_t *Style;
        char Lang[3];		/* 2 chars + \0 */
        cos_dict_t *FD;		/* value = COS_VALUE_RESOURCE */
    } cid;
};
/*
 * Define a sub-descriptor for a character class (FD dictionary element).
 */
typedef struct pdf_sub_font_descriptor_s {
    pdf_font_descriptor_common_t common;
} pdf_sub_font_descriptor_t;

/*
 * Font descriptors are pseudo-resources, so their GC descriptors
 * must be public.
 */
BASIC_PTRS(pdf_font_descriptor_ptrs) {
    GC_STRING_ELT(pdf_font_descriptor_t, common.values.FontName),
    GC_OBJ_ELT(pdf_font_descriptor_t, base_font),
    GC_OBJ_ELT(pdf_font_descriptor_t, cid.Style),
    GC_OBJ_ELT(pdf_font_descriptor_t, cid.FD)
};
gs_public_st_basic_super(st_pdf_font_descriptor, pdf_font_descriptor_t,
  "pdf_font_descriptor_t", pdf_font_descriptor_ptrs,
  pdf_font_descriptor_data, &st_pdf_resource, 0);

/*
 * Sub-font descriptors are also pseudo-resources.
 */
BASIC_PTRS(pdf_sub_font_descriptor_ptrs) {
    GC_STRING_ELT(pdf_sub_font_descriptor_t, common.values.FontName)
};
gs_public_st_basic_super(st_pdf_sub_font_descriptor,
  pdf_sub_font_descriptor_t, "pdf_sub_font_descriptor_t",
  pdf_sub_font_descriptor_ptrs, pdf_sub_font_descriptor_data,
  &st_pdf_resource, 0);

/* ================ Procedures ================ */

/* ---------------- Private ---------------- */

/* Get the ID of font descriptor metrics. */
static inline long
pdf_font_descriptor_common_id(const pdf_font_descriptor_common_t *pfdc)
{
    return pdf_resource_id((const pdf_resource_t *)pfdc);
}

/* Write the common part of a FontDescriptor, aside from the final >>. */
static int
write_FontDescriptor_common(gx_device_pdf *pdev,
                            const pdf_font_descriptor_common_t *pfd, bool embed)
{
    stream *s;
    int code;
    param_printer_params_t params;
    printer_param_list_t rlist;
    gs_param_list *const plist = (gs_param_list *)&rlist;
    char *base14_name = NULL;

    pdf_open_separate(pdev, pdf_font_descriptor_common_id(pfd), resourceFontDescriptor);
    s = pdev->strm;
    stream_puts(s, "<</Type/FontDescriptor/FontName");
    if (!embed) {
        base14_name = (char *)pdf_find_base14_name(pfd->values.FontName.data, pfd->values.FontName.size);
        if(base14_name)
            pdf_put_name(pdev, (byte *)base14_name, strlen(base14_name));
        else
            pdf_put_name(pdev, pfd->values.FontName.data, pfd->values.FontName.size);
    } else
        pdf_put_name(pdev, pfd->values.FontName.data, pfd->values.FontName.size);

    pdf_write_font_bbox(pdev, &pfd->values.FontBBox);
    params = param_printer_params_default;
    code = s_init_param_printer(&rlist, &params, s);
    if (code >= 0) {
#define DESC_INT(str, memb)\
 {str, gs_param_type_int, offset_of(pdf_font_descriptor_common_t, values.memb)}
        static const gs_param_item_t required_items[] = {
            DESC_INT("Ascent", Ascent),
            DESC_INT("CapHeight", CapHeight),
            DESC_INT("Descent", Descent),
            DESC_INT("ItalicAngle", ItalicAngle),
            DESC_INT("StemV", StemV),
            gs_param_item_end
        };
        static const gs_param_item_t optional_items[] = {
            DESC_INT("AvgWidth", AvgWidth),
            DESC_INT("Leading", Leading),
            DESC_INT("MaxWidth", MaxWidth),
            DESC_INT("MissingWidth", MissingWidth),
            DESC_INT("StemH", StemH),
            DESC_INT("XHeight", XHeight),
            gs_param_item_end
        };
#undef DESC_INT
        int Flags = pfd->values.Flags;
        pdf_font_descriptor_t defaults;

        if (base14_name)
            Flags |=FONT_USES_STANDARD_ENCODING;

        code = param_write_int(plist, "Flags", &Flags);
        if (code < 0)
            return code;
        code = gs_param_write_items(plist, pfd, NULL, required_items);
        if (code < 0)
            return code;
        memset(&defaults, 0, sizeof(defaults));
        code = gs_param_write_items(plist, pfd, &defaults, optional_items);
        if (code < 0)
            return code;
        s_release_param_printer(&rlist);
    }
    return 0;
}

/* ---------------- Public ---------------- */

/*
 * Allocate a FontDescriptor, initializing the FontType and rid from the
 * gs_font.
 */
int
pdf_font_descriptor_alloc(gx_device_pdf *pdev, pdf_font_descriptor_t **ppfd,
                          gs_font_base *font, bool embed)
{
    pdf_font_descriptor_t *pfd;
    pdf_base_font_t *pbfont;
    int code = pdf_base_font_alloc(pdev, &pbfont, font,
                (font->orig_FontMatrix.xx == 0 && font->orig_FontMatrix.xy == 0
                    ? &font->FontMatrix : &font->orig_FontMatrix), false);

    if (code < 0)
        return code;
    code = pdf_alloc_resource(pdev, resourceFontDescriptor,
                              font->id, (pdf_resource_t **)&pfd, -1L);
    if (code < 0) {
        gs_free_object(pdev->pdf_memory, pbfont,
                       "pdf_font_descriptor_alloc(base_font)");
        return code;
    }
    memset(&pfd->common.values, 0,
           sizeof(*pfd) - offset_of(pdf_font_descriptor_t, common.values));
    pfd->base_font = pbfont;
    pfd->FontType = font->FontType;
    pfd->embed = embed;
    *ppfd = pfd;
    return 0;
}

int pdf_font_descriptor_free(gx_device_pdf *pdev, pdf_resource_t *pres)
{
    pdf_font_descriptor_t *pfd = (pdf_font_descriptor_t *)pres;
    pdf_base_font_t *pbfont = pfd->base_font;
    gs_font *copied = (gs_font *)pbfont->copied;

    gs_free_copied_font(copied);
    if (pbfont && pbfont->font_name.size) {
        gs_free_string(pdev->memory, pbfont->font_name.data, pbfont->font_name.size, "Free BaseFont FontName string");
        pbfont->font_name.data = (byte *)0L;
        pbfont->font_name.size = 0;
    }
    if (pbfont)
        gs_free_object(cos_object_memory(pres->object), pbfont, "Free base font from FontDescriptor)");
    if (pres->object) {
        gs_free_object(cos_object_memory(pres->object), pres->object, "free FontDescriptor object");
        pres->object = NULL;
    }
    return 0;
}

/* Get the object ID of a FontDescriptor. */
long
pdf_font_descriptor_id(const pdf_font_descriptor_t *pfd)
{
    return pdf_resource_id((const pdf_resource_t *)pfd);
}

long pdf_set_font_descriptor_usage(gx_device_pdf *pdev, int parent_id, const pdf_font_descriptor_t *pfd)
{
    int id = pdf_resource_id((const pdf_resource_t *)pfd);

    pdf_record_usage_by_parent(pdev, id, parent_id);
    if (pfd->base_font->FontFile) {
        id = pfd->base_font->FontFile->id;
        pdf_record_usage_by_parent(pdev, id, parent_id);
    }
    return 0;
}

/*
 * Get the FontType of a FontDescriptor.
 */
font_type
pdf_font_descriptor_FontType(const pdf_font_descriptor_t *pfd)
{
    return pfd->FontType;
}

/*
 * Get the embedding status of a FontDescriptor.
 */
bool
pdf_font_descriptor_embedding(const pdf_font_descriptor_t *pfd)
{
    return pfd->embed;
}

/*
 * Check for subset font.
 */
bool
pdf_font_descriptor_is_subset(const pdf_font_descriptor_t *pfd)
{
    return pdf_base_font_is_subset(pfd->base_font);
}

/*
 * Return a reference to the FontName of a FontDescriptor, similar to
 * pdf_base_font_name.
 */
gs_string *pdf_font_descriptor_name(pdf_font_descriptor_t *pfd)
{
    return &pfd->common.values.FontName;
}

char *pdf_fontfile_hash(void *pfd)
{
    pdf_font_descriptor_t *fd = (pdf_font_descriptor_t *)pfd;
    cos_dict_t *pcd;

    if (fd->base_font && fd->base_font->FontFile) {
        pcd = (cos_dict_t *)fd->base_font->FontFile;
        if (pcd->stream_md5_valid)
            return ((char *)pcd->stream_hash);
        else
            return 0;
    } else
        return 0;
}

/*
 * Return the (copied, subset or complete) font associated with a FontDescriptor.
 * This procedure probably shouldn't exist....
 */
gs_font_base *
pdf_font_descriptor_font(const pdf_font_descriptor_t *pfd, bool complete)
{
    return pdf_base_font_font(pfd->base_font, complete);
}

/*
 * Drop the copied complete font associated with a FontDescriptor.
 */
void
pdf_font_descriptor_drop_complete_font(const pdf_font_descriptor_t *pfd)
{
    pdf_base_font_drop_complete(pfd->base_font);
}

/*
 * Return a reference to the name of a FontDescriptor's base font, per
 * pdf_base_font_name.
 */
gs_string *pdf_font_descriptor_base_name(const pdf_font_descriptor_t *pfd)
{
    return pdf_base_font_name(pfd->base_font);
}

/*
 * Copy a glyph from a font to the stable copy.  Return 0 if this is a
 * new glyph, 1 if it was already copied.
 */
int
pdf_font_used_glyph(pdf_font_descriptor_t *pfd, gs_glyph glyph,
                    gs_font_base *font)
{
    return pdf_base_font_copy_glyph(pfd->base_font, glyph, font);
}

/* Compute the FontDescriptor metrics for a font. */
int
pdf_compute_font_descriptor(gx_device_pdf *pdev, pdf_font_descriptor_t *pfd)
{
    gs_font_base *bfont = pdf_base_font_font(pfd->base_font, false);
    gs_glyph glyph, notdef;
    int index;
    int wmode = bfont->WMode;
    int members = (GLYPH_INFO_WIDTH0 << wmode) |
        GLYPH_INFO_BBOX | GLYPH_INFO_NUM_PIECES;
    pdf_font_descriptor_values_t desc;
    gs_matrix smat;
    gs_matrix *pmat = NULL;
    int fixed_width = 0;
    int small_descent = 0, small_height = 0;
    bool small_present = false;
    int x_height = 0;
    int cap_height = 0;
    gs_rect bbox_colon, bbox_period, bbox_I;
    bool is_cid = (bfont->FontType == ft_CID_encrypted ||
                   bfont->FontType == ft_CID_TrueType);
    bool have_colon = false, have_period = false, have_I = false;
    int code;

    memset(&bbox_colon, 0, sizeof(bbox_colon)); /* quiet gcc warnings. */
    memset(&bbox_period, 0, sizeof(bbox_period)); /* quiet gcc warnings. */
    memset(&bbox_I, 0, sizeof(bbox_I)); /* quiet gcc warnings. */
    memset(&desc, 0, sizeof(desc));
    if (is_cid && bfont->FontBBox.p.x != bfont->FontBBox.q.x &&
                  bfont->FontBBox.p.y != bfont->FontBBox.q.y) {
        int scale = (bfont->FontType == ft_TrueType || bfont->FontType == ft_CID_TrueType ? 1000 : 1);

        desc.FontBBox.p.x = (int)(bfont->FontBBox.p.x * scale);
        desc.FontBBox.p.y = (int)(bfont->FontBBox.p.y * scale);
        desc.FontBBox.q.x = (int)(bfont->FontBBox.q.x * scale);
        desc.FontBBox.q.y = (int)(bfont->FontBBox.q.y * scale);
        desc.Ascent = desc.FontBBox.q.y;
        members &= ~GLYPH_INFO_BBOX;
    } else {
        desc.FontBBox.p.x = desc.FontBBox.p.y = max_int;
        desc.FontBBox.q.x = desc.FontBBox.q.y = min_int;
    }
    /*
     * Embedded TrueType fonts use a 1000-unit character space, but the
     * font itself uses a 1-unit space.  Compensate for this here.
     */
    switch (bfont->FontType) {
    case ft_TrueType:
    case ft_CID_TrueType:
        gs_make_scaling(1000.0, 1000.0, &smat);
        pmat = &smat;
        /* Type 3 fonts may use a FontMatrix in PDF, so we don't
         * need to deal with non-standard matrices
         */
    case ft_GL2_531:
    case ft_PCL_user_defined:
    case ft_GL2_stick_user_defined:
    case ft_user_defined:
        break;
        /* Other font types may use a non-standard (not 1000x1000) design grid
         * The FontMatrix is used to map to the unit square. However PDF files
         * don't allow FontMatrix entries, all fonts are nominally 1000x1000.
         * If we have a font with a non-standard matrix we must account for that
         * here by scaling the font outline.
         */
    default:
        gs_matrix_scale(&bfont->FontMatrix, 1000.0, 1000.0, &smat);
        pmat = &smat;
        break;
    }

    /*
     * Scan the entire glyph space to compute Ascent, Descent, FontBBox, and
     * the fixed width if any.  For non-symbolic fonts, also note the
     * bounding boxes for Latin letters and a couple of other characters,
     * for computing the remaining descriptor values (CapHeight,
     * ItalicAngle, StemV, XHeight, and flags SERIF, SCRIPT, ITALIC,
     * ALL_CAPS, and SMALL_CAPS).  (The algorithms are pretty crude.)
     */
    notdef = GS_NO_GLYPH;
    for (index = 0;
         (bfont->procs.enumerate_glyph((gs_font *)bfont, &index,
                (is_cid ? GLYPH_SPACE_INDEX : GLYPH_SPACE_NAME), &glyph)) >= 0 &&
             index != 0;
         ) {
        gs_glyph_info_t info;
        gs_const_string gname;
        gs_glyph glyph_known_enc;
        gs_char position=0;

        code = bfont->procs.glyph_info((gs_font *)bfont, glyph, pmat, members, &info);
        if (code == gs_error_VMerror)
            return code;
        if (code < 0) {
            /*
             * Since this function may be indirtectly called from gx_device_finalize,
             * we are unable to propagate error code to the interpreter.
             * Therefore we skip it here hoping that few errors can be
             * recovered by the integration through entire glyph set.
             */
            continue;
        }
        if (members & GLYPH_INFO_BBOX) {
            /* rect_merge(desc.FontBBox, info.bbox); Expanding due to type cast :*/
            if (info.bbox.p.x < desc.FontBBox.p.x) desc.FontBBox.p.x = (int)info.bbox.p.x;
            if (info.bbox.q.x > desc.FontBBox.q.x) desc.FontBBox.q.x = (int)info.bbox.q.x;
            if (info.bbox.p.y < desc.FontBBox.p.y) desc.FontBBox.p.y = (int)info.bbox.p.y;
            if (info.bbox.q.y > desc.FontBBox.q.y) desc.FontBBox.q.y = (int)info.bbox.q.y;
            if (!info.num_pieces)
                desc.Ascent = max(desc.Ascent, (int)info.bbox.q.y);
        }
        if (notdef == GS_NO_GLYPH && gs_font_glyph_is_notdef(bfont, glyph)) {
            notdef = glyph;
            desc.MissingWidth = (int)info.width[wmode].x;
        }
        if (info.width[wmode].y != 0)
            fixed_width = min_int;
        else if (fixed_width == 0)
            fixed_width = (int)info.width[wmode].x;
        else if (info.width[wmode].x != fixed_width)
            fixed_width = min_int;
        if (desc.Flags & FONT_IS_SYMBOLIC)
            continue;		/* skip Roman-only computation */
        if (is_cid)
            continue;
        code = bfont->procs.glyph_name((gs_font *)bfont, glyph, &gname);
        if (code < 0) {
            /* If we fail to get the glyph name, best assume this is a symbolic font */
            desc.Flags |= FONT_IS_SYMBOLIC;
            continue;
        }
        /* See if the glyph name is in any of the known encodings */
        glyph_known_enc = gs_c_name_glyph(gname.data, gname.size);
        if (glyph_known_enc == gs_no_glyph) {
            desc.Flags |= FONT_IS_SYMBOLIC;
            continue;
        }
        /* Finally check if the encoded glyph is in Standard Encoding */
        /* gs_c_decode always fails to find .notdef, its always present so
         * don't worry about it
         */
        if(strncmp(".notdef", (const char *)gname.data, gname.size)) {
            position = gs_c_decode(glyph_known_enc, 0);
            if (position == GS_NO_CHAR) {
                desc.Flags |= FONT_IS_SYMBOLIC;
                continue;
            }
        }
        switch (gname.size) {
        case 5:
            if (!memcmp(gname.data, "colon", 5))
                bbox_colon = info.bbox, have_colon = true;
            continue;
        case 6:
            if (!memcmp(gname.data, "period", 6))
                bbox_period = info.bbox, have_period = true;
            continue;
        case 1:
            break;
        default:
            continue;
        }

        if (gname.data[0] >= 'A' && gname.data[0] <= 'Z') {
            cap_height = max(cap_height, (int)info.bbox.q.y);
            if (gname.data[0] == 'I')
                bbox_I = info.bbox, have_I = true;
        } else if (gname.data[0] >= 'a' && gname.data[0] <= 'z') {
            int y0 = (int)(info.bbox.p.y), y1 = (int)(info.bbox.q.y);

            small_present = true;
            switch (gname.data[0]) {
            case 'b': case 'd': case 'f': case 'h':
            case 'k': case 'l': case 't': /* ascender */
                small_height = max(small_height, y1);
            case 'i':		/* anomalous ascent */
                break;
            case 'j':		/* descender with anomalous ascent */
                small_descent = min(small_descent, y0);
                break;
            case 'g': case 'p': case 'q': case 'y': /* descender */
                small_descent = min(small_descent, y0);
            default:		/* no ascender or descender */
                x_height = max(x_height, y1);
            }
        }
    }
    if (!(desc.Flags & FONT_IS_SYMBOLIC)) {
        desc.Flags |= FONT_IS_ADOBE_ROMAN; /* required if not symbolic */
        desc.XHeight = (int)x_height;
        if (!small_present && (!pdev->PDFA != 0 || bfont->FontType != ft_TrueType))
            desc.Flags |= FONT_IS_ALL_CAPS;
        desc.CapHeight = cap_height;
        /*
         * Look at various glyphs to determine ItalicAngle, StemV,
         * SERIF, SCRIPT, and ITALIC.
         */
        if (have_colon && have_period) {
            /* Calculate the dominant angle. */
            int angle =
                (int)(atan2((bbox_colon.q.y - bbox_colon.p.y) -
                              (bbox_period.q.y - bbox_period.p.y),
                            (bbox_colon.q.x - bbox_colon.p.x) -
                              (bbox_period.q.x - bbox_period.p.x)) *
                      radians_to_degrees) - 90;

            /* Normalize to [-90..90]. */
            while (angle > 90)
                angle -= 180;
            while (angle < -90)
                angle += 180;
            if (angle < -30)
                angle = -30;
            else if (angle > 30)
                angle = 30;
            /*
             * For script or embellished fonts, we can get an angle that is
             * slightly off from zero even for non-italic fonts.
             * Compensate for this now.
             */
            if (angle <= 2 && angle >= -2)
                angle = 0;
            desc.ItalicAngle = angle;
        }
        if (desc.ItalicAngle)
            desc.Flags |= FONT_IS_ITALIC;
        if (have_I) {
            double wdot = bbox_period.q.x - bbox_period.p.x;
            double wcolon = bbox_I.q.x - bbox_I.p.x;
            double wI = bbox_period.q.x - bbox_period.p.x;

            desc.StemV = (int)wdot;
            if (wI > wcolon * 2.5 || wI > (bbox_period.q.y - bbox_period.p.y) * 0.25)
                desc.Flags |= FONT_IS_SERIF;
        }
    }
    if (desc.Ascent == 0)
        desc.Ascent = desc.FontBBox.q.y;
    desc.Descent = desc.FontBBox.p.y;
    if (!(desc.Flags & (FONT_IS_SYMBOLIC | FONT_IS_ALL_CAPS)) &&
        (small_descent > desc.Descent / 3 || desc.XHeight > small_height * 0.9) &&
        (!pdev->PDFA != 0 || bfont->FontType != ft_TrueType)
        )
        desc.Flags |= FONT_IS_SMALL_CAPS;
    if (fixed_width > 0 && (!pdev->PDFA != 0 || bfont->FontType != ft_TrueType)) {
        desc.Flags |= FONT_IS_FIXED_WIDTH;
        desc.AvgWidth = desc.MaxWidth = desc.MissingWidth = fixed_width;
    }
    if (desc.CapHeight == 0)
        desc.CapHeight = desc.Ascent;
    if (desc.StemV == 0)
        desc.StemV = (int)(desc.FontBBox.q.x * 0.15);
    pfd->common.values = desc;
    return 0;
}

/*
 * Finish a FontDescriptor by computing the metric values, and then
 * writing the associated embedded font if any.
 */
int
pdf_finish_FontDescriptor(gx_device_pdf *pdev, pdf_resource_t *pres)
{
    pdf_font_descriptor_t *pfd = (pdf_font_descriptor_t *)pres;
    int code = 0;
    cos_dict_t *pcd = 0;
    if (pfd->common.object->id == -1)
        return 0;
    if (!pfd->common.object->written &&
        (code = pdf_compute_font_descriptor(pdev, pfd)) >= 0 &&
        (!pfd->embed ||
         (code = pdf_write_embedded_font(pdev, pfd->base_font,
                                pfd->FontType,
                                &pfd->common.values.FontBBox,
                                pfd->common.rid, &pcd)) >= 0)
        ) {
        pdf_set_FontFile_object(pfd->base_font, pcd);
    }
    return code;
}

/* Write a FontDescriptor. */
int
pdf_write_FontDescriptor(gx_device_pdf *pdev, pdf_resource_t *pres)
{
    pdf_font_descriptor_t *pfd = (pdf_font_descriptor_t *)pres;
    font_type ftype = pfd->FontType;
    long cidset_id = 0;
    int code = 0;
    stream *s;

    if (pfd->common.object->written)
        return 0;
    if (pfd->common.object->id == -1)
        return 0;

    /* If this is a CIDFont subset, write the CIDSet now. */
    switch (ftype) {
    case ft_CID_encrypted:
    case ft_CID_TrueType:
        if (pdf_do_subset_font(pdev, pfd->base_font, pfd->common.rid)) {
            if (pdev->PDFA < 2) {
                code = pdf_write_CIDSet(pdev, pfd->base_font, &cidset_id);
                if (code < 0)
                    return code;
            }
        }
    default:
        break;
    }

    {
        /*
         * Hack: make all embedded subset TrueType fonts "symbolic" to
         * work around undocumented assumptions in Acrobat Reader.
         */
        pdf_font_descriptor_common_t fd;

        fd = pfd->common;
        if (pfd->embed && pfd->FontType == ft_TrueType /*&& !pdev->PDFA*/ &&
            pdf_do_subset_font(pdev, pfd->base_font, pfd->common.rid)
            )
            fd.values.Flags =
                (fd.values.Flags & ~(FONT_IS_ADOBE_ROMAN)) | FONT_IS_SYMBOLIC;
        code = write_FontDescriptor_common(pdev, &fd, pfd->embed);
    }
    if (code < 0)
        return code;
    s = pdev->strm;
    if (cidset_id != 0)
        pprintld1(s, "/CIDSet %ld 0 R\n", cidset_id);
    else if (pdf_do_subset_font(pdev, pfd->base_font, pfd->common.rid) &&
             (ftype == ft_encrypted || ftype == ft_encrypted2)
             ) {
        stream_puts(s, "/CharSet");
        code = pdf_write_CharSet(pdev, pfd->base_font);
        if (code < 0)
            return code;
    }
    if (pfd->embed && pfd->base_font->FontFile) {
        code = pdf_write_FontFile_entry(pdev, pfd->base_font);
        if (code < 0)
            return code;
    }
    if (pfd->cid.Style) {
        stream_puts(s, "/Style");
        COS_WRITE(pfd->cid.Style, pdev);
    }
    if (pfd->cid.Lang[0]) {
        pprints1(s, "/Lang(%s)", pfd->cid.Lang);
    }
    if (pfd->cid.FD) {
        stream_puts(s, "/FD");
        COS_WRITE(pfd->cid.FD, pdev);
    }
    stream_puts(s, ">>\n");
    pdf_end_separate(pdev, resourceFontDescriptor);
    pfd->common.object->written = true;
    {	const cos_object_t *pco = (const cos_object_t *)pdf_get_FontFile_object(pfd->base_font);
        if (pco != NULL) {
            if (pdev->is_ps2write)
                pprintld1(s, "%%BeginResource: file (PDF FontFile obj_%ld)\n", pco->id);
            code = COS_WRITE_OBJECT(pco, pdev, resourceNone);
            if (code < 0)
                return code;
        }
    }
    return 0;
}

/*
 * Release a FontDescriptor components.
 */
int
pdf_release_FontDescriptor_components(gx_device_pdf *pdev, pdf_resource_t *pres)
{
    pdf_font_descriptor_t *pfd = (pdf_font_descriptor_t *) pres;

    gs_free_object(pdev->pdf_memory, pfd->base_font, "pdf_release_FontDescriptor_components");
    pfd->base_font = NULL;
    /* fixme: underimplemented. */
    return 0;
}

/*
 * Mark a FontDescriptor used in a text.
 */
int
pdf_mark_font_descriptor_used(gx_device_pdf *pdev, pdf_font_descriptor_t *pfd)
{
    if (pfd != NULL && pfd->common.object->id == -1)
        pdf_reserve_object_id(pdev, (pdf_resource_t *)&pfd->common, 0);
    return 0;
}

/*
 * Convert True Type font descriptor into CID font descriptor for PDF/A.
 */
int
pdf_convert_truetype_font_descriptor(gx_device_pdf *pdev, pdf_font_resource_t *pdfont)
{
    pdf_font_descriptor_t *pfd = pdfont->FontDescriptor;
    pdf_base_font_t *pbfont = pfd->base_font;
    gs_font *pfont = (gs_font *)pbfont->copied;
    gs_char ch;
    /* Save the simple font descriptor data because CID font data overlap them. */
    int FirstChar = pdfont->u.simple.FirstChar, LastChar = pdfont->u.simple.LastChar;
    pdf_encoding_element_t *Encoding = pdfont->u.simple.Encoding;
    int length_CIDSet = (pbfont->num_glyphs > LastChar ? (pbfont->num_glyphs + 7) / 8 : ((LastChar + 1) + 7 / 8));
    int length_CIDToGIDMap = (pbfont->num_glyphs > LastChar ? pbfont->num_glyphs * sizeof(ushort) : (LastChar + 1) * sizeof(ushort));

    pfd->FontType = ft_CID_TrueType;
    pdfont->u.simple.Encoding = NULL; /* Drop due to overlapping against a garbager problem. */
    pbfont->CIDSet = gs_alloc_bytes(pdev->pdf_memory, length_CIDSet,
                        "pdf_convert_truetype_font_descriptor");
    if (pbfont->CIDSet == NULL)
        return_error(gs_error_VMerror);
    memset(pbfont->CIDSet, 0, length_CIDSet);
    pdfont->u.cidfont.CIDToGIDMap = (ushort *)gs_alloc_bytes(pdev->pdf_memory,
                        length_CIDToGIDMap, "pdf_convert_truetype_font_descriptor");
    if (pdfont->u.cidfont.CIDToGIDMap == NULL)
        return_error(gs_error_VMerror);
    memset(pdfont->u.cidfont.CIDToGIDMap, 0, length_CIDToGIDMap);
    if(pdev->PDFA == 1) {
        for (ch = FirstChar; ch <= LastChar; ch++) {
            if (Encoding[ch].glyph != GS_NO_GLYPH) {
                gs_glyph glyph = pfont->procs.encode_char(pfont, ch, GLYPH_SPACE_INDEX);

                pbfont->CIDSet[ch / 8] |= 0x80 >> (ch % 8);
                pdfont->u.cidfont.CIDToGIDMap[ch] = glyph - GS_MIN_GLYPH_INDEX;
            }
        }
        /* Set the CIDSet bit for CID 0 (the /.notdef) which must always be present */
        pbfont->CIDSet[0] |= 0x80;
    } else {
        for (ch = 0; ch <= pbfont->num_glyphs; ch++) {
            gs_glyph glyph = pfont->procs.encode_char(pfont, ch, GLYPH_SPACE_INDEX);

            pbfont->CIDSet[ch / 8] |= 0x80 >> (ch % 8);
            pdfont->u.cidfont.CIDToGIDMap[ch] = glyph - GS_MIN_GLYPH_INDEX;
        }
    }
    pbfont->CIDSetLength = length_CIDSet;
    pdfont->u.cidfont.CIDToGIDMapLength = length_CIDToGIDMap / sizeof(ushort);
    pdfont->u.cidfont.Widths2 = NULL;
    pdfont->u.cidfont.used2 = NULL;
    pdfont->u.cidfont.v = NULL;
    return 0;
}

int mark_font_descriptor_symbolic(const pdf_font_resource_t *pdfont)
{
    pdf_font_descriptor_values_t *desc;

    if(!pdfont || !pdfont->FontDescriptor)
        return 0;

    desc = &pdfont->FontDescriptor->common.values;

    if (!(desc->Flags & FONT_IS_SYMBOLIC)) {
        desc->Flags |= FONT_IS_SYMBOLIC;
        desc->Flags &= ~FONT_IS_ADOBE_ROMAN;
    }
    return 1;
}