summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvipdfm-x/pdffont.c
blob: 5e38733d3c87dab619c34ed1d0b50a21f290f109 (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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.

    Copyright (C) 2002-2020 by Jin-Hwan Cho, Matthias Franz, and Shunsaku Hirata,
    the dvipdfmx project team.
    
    Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include "system.h"
#include "error.h"
#include "mem.h"

#include "dpxconf.h"
#include "dpxfile.h"
#include "dpxutil.h"

#include "pdfobj.h"

#include "agl.h"
#include "pdfencoding.h"
#include "cmap.h"
#include "unicode.h"

#include "type1.h"
#include "type1c.h"
#include "truetype.h"

#include "pkfont.h"

#include "type0.h"
#include "tt_cmap.h"
#include "cidtype0.h"

#include "pdffont.h"

#define MREC_HAS_TOUNICODE(m) ((m) && (m)->opt.tounicode)

void
pdf_font_set_dpi (int font_dpi)
{
  PKFont_set_dpi(font_dpi);
}

void
pdf_font_make_uniqueTag (char *tag)
{
  int    i;
  char   ch;
  static char first = 1;

  if (first) {
    time_t current_time;
    current_time = dpx_util_get_unique_time_if_given();
    if (current_time == INVALID_EPOCH_VALUE)
      current_time = time(NULL);
    srand(current_time);
    first = 0;
  }

  for (i = 0; i < 6; i++) {
    ch = rand() % 26;
    tag[i] = ch + 'A';
  }
  tag[6] = '\0';
}


struct pdf_font
{
  char    *ident;
  int      subtype;

  char    *map_name;

  int      encoding_id; /* encoding or CMap */

  /*
   * If subtype is Type0, it simply points font_id
   * of Type0 font. Type0 and simple font is not
   * unified yet.
   */
  int      font_id;

  /* For simple font */
  uint32_t index;
  char    *fontname;
  char     uniqueID[7];

  /*
   * PDF font resource objects
   */
  pdf_obj *reference;
  pdf_obj *resource;
  pdf_obj *descriptor;

  /*
   * Font format specific data
   */
  char    *usedchars;
  int      flags;

  /* PK font */
  double   point_size;
  double   design_size;
};

static void
pdf_init_font_struct (pdf_font *font)
{
  ASSERT(font);

  font->ident    = NULL;
  font->map_name = NULL;
  font->subtype  = -1;
  font->font_id  = -1; /* Type0 ID */
  font->fontname = NULL;
  memset(font->uniqueID, 0, 7);
  font->index    = 0;

  font->encoding_id = -1;

  font->reference   = NULL;
  font->resource    = NULL;
  font->descriptor  = NULL;

  font->point_size  = 0;
  font->design_size = 0;

  font->usedchars   = NULL;
  font->flags       = 0;

  return;
}

static void
pdf_flush_font (pdf_font *font)
{
  char *fontname, *uniqueTag;

  if (!font) {
    return;
  }

  if (font->resource && font->reference) {
    if (font->subtype != PDF_FONT_FONTTYPE_TYPE3) {
      if (pdf_font_get_flag(font, PDF_FONT_FLAG_NOEMBED)) {
	pdf_add_dict(font->resource,
		     pdf_new_name("BaseFont"), pdf_new_name(font->fontname));
	if (font->descriptor) {
	  pdf_add_dict(font->descriptor,
		       pdf_new_name("FontName"), pdf_new_name(font->fontname));
	}
      } else {
	if (!font->fontname) {
	  ERROR("Undefined in fontname... (%s)", font->ident);
	}
	fontname  = NEW(7+strlen(font->fontname)+1, char);
	uniqueTag = pdf_font_get_uniqueTag(font);
	sprintf(fontname, "%6s+%s", uniqueTag, font->fontname);
	pdf_add_dict(font->resource,
		     pdf_new_name("BaseFont"), pdf_new_name(fontname));
	if (font->descriptor) {
	  pdf_add_dict(font->descriptor,
		       pdf_new_name("FontName"), pdf_new_name(fontname));
	}
	RELEASE(fontname);
      }
      if (font->descriptor) {
	pdf_add_dict(font->resource,
		     pdf_new_name("FontDescriptor"), pdf_ref_obj(font->descriptor));
      }
    }
  }

  if (font->resource)
    pdf_release_obj(font->resource);
  if (font->descriptor)
    pdf_release_obj(font->descriptor);
  if (font->reference)
    pdf_release_obj(font->reference);

  font->reference  = NULL;
  font->resource   = NULL;
  font->descriptor = NULL;

  return;
}

static void
pdf_clean_font_struct (pdf_font *font)
{
  if (font) {
    if (font->ident)
      RELEASE(font->ident);
    if (font->map_name)
      RELEASE(font->map_name);
    if (font->fontname)
      RELEASE(font->fontname);
    if (font->usedchars)
      RELEASE(font->usedchars);

    if (font->reference)
      ERROR("pdf_font>> Object not flushed.");
    if (font->resource)
      ERROR("pdf_font> Object not flushed.");
    if (font->descriptor)
      ERROR("pdf_font>> Object not flushed.");

    font->ident     = NULL;
    font->map_name  = NULL;
    font->fontname  = NULL;
    font->usedchars = NULL;
  }

  return;
}

#define CACHE_ALLOC_SIZE 16u

static struct {
  int       count;
  int       capacity;
  pdf_font *fonts;
} font_cache = {
  0, 0, NULL
};

void
pdf_init_fonts (void)
{
  ASSERT(font_cache.fonts == NULL);  

  agl_init_map();

  CMap_cache_init();
  pdf_init_encodings();

  Type0Font_cache_init();

  font_cache.count    = 0;
  font_cache.capacity = CACHE_ALLOC_SIZE;
  font_cache.fonts    = NEW(font_cache.capacity, pdf_font);
}

#define CHECK_ID(n) do {\
  if ((n) < 0 || (n) >= font_cache.count) {\
    ERROR("Invalid font ID: %d", (n));\
  }\
} while (0)
#define GET_FONT(n)  (&(font_cache.fonts[(n)]))


pdf_obj *
pdf_get_font_reference (int font_id)
{
  pdf_font  *font;

  CHECK_ID(font_id);

  font = GET_FONT(font_id);
  if (font->subtype == PDF_FONT_FONTTYPE_TYPE0) {
    Type0Font *t0font;

    t0font = Type0Font_cache_get(font->font_id);
    return Type0Font_get_resource(t0font);
  } else {
    if (!font->reference) {
      font->reference = pdf_ref_obj(pdf_font_get_resource(font));
    }
  }

  return pdf_link_obj(font->reference);
}

char *
pdf_get_font_usedchars (int font_id)
{
  pdf_font *font;

  CHECK_ID(font_id);

  font = GET_FONT(font_id);
  if (font->subtype == PDF_FONT_FONTTYPE_TYPE0) {
    Type0Font *t0font;

    t0font = Type0Font_cache_get(font->font_id);
    return Type0Font_get_usedchars(t0font);
  } else {
    if (!font->usedchars) {
      font->usedchars = NEW(256, char);
      memset(font->usedchars, 0, 256 * sizeof(char));
    }
    return font->usedchars;
  }
}

int
pdf_get_font_wmode (int font_id)
{
  pdf_font *font;

  CHECK_ID(font_id);

  font = GET_FONT(font_id);
  if (font->subtype == PDF_FONT_FONTTYPE_TYPE0) {
    Type0Font *t0font;

    t0font = Type0Font_cache_get(font->font_id);
    return Type0Font_get_wmode(t0font);
  } else {
    return 0;
  }
}

int
pdf_get_font_subtype (int font_id)
{
  pdf_font *font;

  CHECK_ID(font_id);

  font = GET_FONT(font_id);

  return font->subtype;
}

#if 0
char *
pdf_get_font_fontname (int font_id)
{
  pdf_font *font;

  CHECK_ID(font_id);

  font = GET_FONT(font_id);

  return font->fontname;
}
#endif /* 0 */

int
pdf_get_font_encoding (int font_id)
{
  pdf_font *font;

  CHECK_ID(font_id);

  font = GET_FONT(font_id);

  return font->encoding_id;
}

/* The rule for ToUnicode creation is:
 *
 *  If "tounicode" option is specified in fontmap, use that.
 *  If there is ToUnicode CMap with same name as TFM, use that.
 *  If no "tounicode" option is used and no ToUnicode CMap with
 *  same name as TFM is found, create ToUnicode CMap from glyph
 *  names and AGL file.
 */
static int
try_load_ToUnicode_CMap (pdf_font *font)
{
  pdf_obj     *fontdict;
  pdf_obj     *tounicode;
  const char  *cmap_name = NULL;
  fontmap_rec *mrec; /* Be sure fontmap is still alive here */

  ASSERT(font);

  /* We are using different encoding for Type0 font.
   * This feature is unavailable for them.
   */
  if (font->subtype == PDF_FONT_FONTTYPE_TYPE0)
    return  0;

  ASSERT(font->map_name);

  mrec = pdf_lookup_fontmap_record(font->map_name);
  if (MREC_HAS_TOUNICODE(mrec))
    cmap_name = mrec->opt.tounicode;
  else {
    cmap_name = font->map_name;
  }

  fontdict  = pdf_font_get_resource(font);
  tounicode = pdf_load_ToUnicode_stream(cmap_name);
  if (!tounicode && MREC_HAS_TOUNICODE(mrec))
    WARN("Failed to read ToUnicode mapping \"%s\"...", mrec->opt.tounicode);
  else if (tounicode) {
    if (pdf_obj_typeof(tounicode) != PDF_STREAM)
      ERROR("Object returned by pdf_load_ToUnicode_stream() not stream object! (This must be bug)");
    else if (pdf_stream_length(tounicode) > 0) {
      pdf_add_dict(fontdict,
                   pdf_new_name("ToUnicode"),
                   pdf_ref_obj (tounicode)); /* _FIXME_ */
      if (dpx_conf.verbose_level > 0)
        MESG("pdf_font>> ToUnicode CMap \"%s\" attached to font id=\"%s\".\n",
             cmap_name, font->map_name);
    }
    pdf_release_obj(tounicode);
  }

  return  0;
}

void
pdf_close_fonts (void)
{
  int  font_id;

  for (font_id = 0;
       font_id < font_cache.count; font_id++) {
    pdf_font  *font;

    font = GET_FONT(font_id);

    if (dpx_conf.verbose_level > 0) {
      if (font->subtype != PDF_FONT_FONTTYPE_TYPE0) {
	      MESG("(%s", pdf_font_get_ident(font));
	      if (dpx_conf.verbose_level > 2 &&
	          !pdf_font_get_flag(font, PDF_FONT_FLAG_NOEMBED)) {
	        MESG("[%s+%s]",
	        pdf_font_get_uniqueTag(font),
	        pdf_font_get_fontname(font));
	      } else if (dpx_conf.verbose_level > 1) {
	        MESG("[%s]", pdf_font_get_fontname(font));
	      }
	      if (dpx_conf.verbose_level > 1) {
	        if (pdf_font_get_encoding(font) >= 0) {
	            MESG("[%s]", pdf_encoding_get_name(pdf_font_get_encoding(font)));
	        } else {
	          MESG("[built-in]");
	        }
	      }
      }
    }

    /* Must come before load_xxx */
    try_load_ToUnicode_CMap(font);

    /* Type 0 is handled separately... */
    switch (font->subtype) {
    case PDF_FONT_FONTTYPE_TYPE1:
      if (dpx_conf.verbose_level > 0)
	      MESG("[Type1]");
      if (!pdf_font_get_flag(font, PDF_FONT_FLAG_BASEFONT))
	      pdf_font_load_type1(font);
      break;
    case PDF_FONT_FONTTYPE_TYPE1C:
      if (dpx_conf.verbose_level > 0)
	      MESG("[Type1C]");
      pdf_font_load_type1c(font);
      break;
    case PDF_FONT_FONTTYPE_TRUETYPE:
      if (dpx_conf.verbose_level > 0)
	      MESG("[TrueType]");
      pdf_font_load_truetype(font);
      break;
    case PDF_FONT_FONTTYPE_TYPE3:
      if (dpx_conf.verbose_level > 0)
	      MESG("[Type3/PK]");
      pdf_font_load_pkfont (font);
      break;
    case PDF_FONT_FONTTYPE_TYPE0:
      break;
    default:
      ERROR("Unknown font type: %d", font->subtype);
      break;
    }

    if (font->encoding_id >= 0 && font->subtype != PDF_FONT_FONTTYPE_TYPE0)
      pdf_encoding_add_usedchars(font->encoding_id, font->usedchars);

    if (dpx_conf.verbose_level > 0) {
      if (font->subtype != PDF_FONT_FONTTYPE_TYPE0)
	      MESG(")");
    }
  }

  pdf_encoding_complete();

  for (font_id = 0; font_id < font_cache.count; font_id++) {
    pdf_font *font = GET_FONT(font_id);

    if (font->encoding_id >= 0 && font->subtype != PDF_FONT_FONTTYPE_TYPE0) {
      pdf_obj *enc_obj = pdf_get_encoding_obj(font->encoding_id);
      pdf_obj *tounicode;

      /* Predefined encodings (and those simplified to them) are embedded
	     * as direct objects, but this is purely a matter of taste.
       */
      if (enc_obj)
        pdf_add_dict(font->resource,
		                 pdf_new_name("Encoding"),
		                 PDF_OBJ_NAMETYPE(enc_obj) ? pdf_link_obj(enc_obj) : pdf_ref_obj(enc_obj));
      /* For built-in encoding, each font loader create ToUnicode CMap. */
      if (!pdf_lookup_dict(font->resource, "ToUnicode")) {
        tounicode = pdf_encoding_get_tounicode(font->encoding_id);
        if (tounicode) {
          pdf_add_dict(font->resource,
                       pdf_new_name("ToUnicode"), pdf_ref_obj(tounicode));
        }
      }
    } else if (font->subtype == PDF_FONT_FONTTYPE_TRUETYPE) {
      /* encoding_id < 0 means MacRoman here (but not really)
       * We use MacRoman as "default" encoding. */
      pdf_add_dict(font->resource,
                   pdf_new_name("Encoding"),
                   pdf_new_name("MacRomanEncoding"));
    }

    pdf_flush_font(font);
    pdf_clean_font_struct(font);
  }
  RELEASE(font_cache.fonts);
  font_cache.fonts    = NULL;
  font_cache.count    = 0;
  font_cache.capacity = 0;

  Type0Font_cache_close();

  CMap_cache_close();
  pdf_close_encodings();

  agl_close_map (); /* After encoding */

  return;
}

int
pdf_font_findresource (const char *tex_name,
		                   double font_scale, fontmap_rec *mrec)
{
  int          font_id = -1;
  pdf_font    *font;
  int          encoding_id = -1, cmap_id = -1;
  const char  *fontname;

  /*
   * Get appropriate info from map file. (PK fonts at two different
   * point sizes would be looked up twice unecessarily.)
   */
  fontname = mrec ? mrec->font_name : tex_name;
  /* XeTeX specific...
   * First try loading GID-to-CID mapping from CFF CID-keyed OpenType font.
   * There was a serious bug in xdv support... It was implemented with the wrong
   * assumption that CID always equals to GID. 
   * TODO: There is a possibility that GID-to-CID mapping is not one-to-one.
   * Use internal glyph ordering rather than map GID to CIDs.
   */
  if (mrec && mrec->opt.use_glyph_encoding) {
    int wmode = 0;
    /* Should be always Identity-H or Identity-V for XeTeX output. */
    if (mrec->enc_name) {
      if (!strcmp(mrec->enc_name, "Identity-V"))
        wmode = 1;
      else if (!strcmp(mrec->enc_name, "Identity-H"))
        wmode = 0;
      else {
        WARN("Unexpected encoding specified for xdv: %s", mrec->enc_name);
      }
    /* cmap_id < 0 is returned if ...
     *  Font is not a CFF font
     *  GID to CID mapping is identity mapping
     * 
     * TODO: fontmap record still has Identity CMap assigned but actually different CMap
     * can be attached to the font here. Should we fix mrec->enc_name here?
     */
      cmap_id = otf_try_load_GID_to_CID_map(mrec->font_name, mrec->opt.index, wmode);
    }
  }
  if (cmap_id < 0 && mrec && mrec->enc_name) {
#define MAYBE_CMAP(s) (!strstr((s), ".enc") || strstr((s), ".cmap"))
    if (MAYBE_CMAP(mrec->enc_name)) {
      cmap_id = CMap_cache_find(mrec->enc_name);
      if (cmap_id >= 0) {
	      CMap  *cmap;
	      int    cmap_type, minbytes;

	      cmap      = CMap_cache_get(cmap_id);
	      cmap_type = CMap_get_type (cmap);
	      minbytes  = CMap_get_profile(cmap, CMAP_PROF_TYPE_INBYTES_MIN);
	      /*
	       * Check for output encoding.
	       */
	      if (cmap_type != CMAP_TYPE_IDENTITY    &&
	          cmap_type != CMAP_TYPE_CODE_TO_CID &&
	          cmap_type != CMAP_TYPE_TO_UNICODE) {
	        WARN("Only 16-bit encoding supported for output encoding.");
	      }
	      /*
	       * Turn on map option.
	       */
	      if (minbytes == 2 && mrec->opt.mapc < 0) {
	        if (dpx_conf.verbose_level > 0) {
	          MESG("\n");
	          MESG("pdf_font>> Input encoding \"%s\" requires at least 2 bytes.\n",
		            CMap_get_name(cmap));
	          MESG("pdf_font>> The -m <00> option will be assumed for \"%s\".\n", mrec->font_name);
	        }
          /* FIXME: The following code modifies mrec. */
	        mrec->opt.mapc = 0;
	      }
      } else if (!strcmp(mrec->enc_name, "unicode")) {
	      cmap_id = otf_load_Unicode_CMap(mrec->font_name,
					                              mrec->opt.index, mrec->opt.otl_tags,
					                              ((mrec->opt.flags & FONTMAP_OPT_VERT) ? 1 : 0));
	      if (cmap_id < 0) {
	        cmap_id = t1_load_UnicodeCMap(mrec->font_name, mrec->opt.otl_tags,
					                              ((mrec->opt.flags & FONTMAP_OPT_VERT) ? 1 : 0));
	      }
	      if (cmap_id < 0)
	        ERROR("Failed to read UCS2/UCS4 TrueType cmap...");
      }
    }
    if (cmap_id < 0) {
      encoding_id = pdf_encoding_findresource(mrec->enc_name);
      if (encoding_id < 0)
	      ERROR("Could not find encoding file \"%s\".", mrec->enc_name);
    }
  }
  if (mrec && cmap_id >= 0) {
    /*
     * Composite Font
     */
    int  type0_id, found = 0;

    type0_id = Type0Font_cache_find(mrec->font_name, cmap_id, &mrec->opt);
    if (type0_id < 0) {
      return -1;
    }

    for (font_id = 0;
        font_id < font_cache.count; font_id++) {
      font = GET_FONT(font_id);
      if (font->subtype == PDF_FONT_FONTTYPE_TYPE0 &&
	        font->font_id == type0_id &&
	        font->encoding_id == cmap_id) {
	      found = 1;
	      if (dpx_conf.verbose_level > 0) {
	        MESG("\npdf_font>> Type0 font \"%s\" (cmap_id=%d) found at font_id=%d.\n",
	        mrec->font_name, cmap_id, font_id);
	      }
	      break;
      }
    }

    if (!found) {
      font_id = font_cache.count;
      if (font_cache.count >= font_cache.capacity) {
        font_cache.capacity += CACHE_ALLOC_SIZE;
        font_cache.fonts     = RENEW(font_cache.fonts, font_cache.capacity, pdf_font);
      }
      font    = GET_FONT(font_id);
      pdf_init_font_struct(font);

      font->font_id     = type0_id;
      font->subtype     = PDF_FONT_FONTTYPE_TYPE0;
      font->encoding_id = cmap_id;

      font_cache.count++;

      if (dpx_conf.verbose_level > 0) {
	      MESG("\npdf_font>> Type0 font \"%s\"", fontname);
        MESG(" cmap_id=<%s,%d>", mrec->enc_name, font->encoding_id);
        MESG(" opened at font_id=<%s,%d>.\n", tex_name, font_id);
      }

    }
  } else {
    /*
     * Simple Font - always embed.
     */
    int  found = 0;

    for (font_id = 0;
        font_id < font_cache.count; font_id++) {
      font = GET_FONT(font_id);
      switch (font->subtype) {
      case PDF_FONT_FONTTYPE_TYPE1:
      case PDF_FONT_FONTTYPE_TYPE1C:
      case PDF_FONT_FONTTYPE_TRUETYPE:
	    /* fontname here is font file name.
	     * We must compare both font file name and encoding
	     *
	     * TODO: Embed a font only once if it is used
	     *       with two different encodings
	     */
	      if (!strcmp(fontname, font->ident)   &&
	          encoding_id == font->encoding_id) {
          if (mrec && mrec->opt.index == font->index)
            found = 1;
	      }
	      break;
      case PDF_FONT_FONTTYPE_TYPE3:
	    /* There shouldn't be any encoding specified for PK font.
	     * It must be always font's build-in encoding.
	     *
	     * TODO: a PK font with two encodings makes no sense. Change?
       */
	      if (!strcmp(fontname, font->ident) &&
	        font_scale == font->point_size) {
	        found = 1;
	      }
	      break;
      case PDF_FONT_FONTTYPE_TYPE0:
	      break;
      default:
	      ERROR("Unknown font type: %d", font->subtype);
	      break;
      }

      if (found) {
	      if (dpx_conf.verbose_level > 0) {
	        MESG("\npdf_font>> Simple font \"%s\" (enc_id=%d) found at id=%d.\n",
	            fontname, encoding_id, font_id);
	      }
	      break;
      }
    }


    if (!found) {
      font_id = font_cache.count;
      if (font_cache.count >= font_cache.capacity) {
	      font_cache.capacity += CACHE_ALLOC_SIZE;
	      font_cache.fonts     = RENEW(font_cache.fonts, font_cache.capacity, pdf_font);
      }

      font = GET_FONT(font_id);

      pdf_init_font_struct(font);

      font->point_size  = font_scale;
      font->encoding_id = encoding_id;
      font->ident       = NEW(strlen(fontname) + 1, char);
      strcpy(font->ident, fontname);
      font->map_name    = NEW(strlen(tex_name) + 1, char);
      strcpy(font->map_name, tex_name);
      font->index       = (mrec && mrec->opt.index) ? mrec->opt.index : 0;

      if (pdf_font_open_type1(font) >= 0) {
	      font->subtype = PDF_FONT_FONTTYPE_TYPE1;
      } else if (pdf_font_open_type1c(font) >= 0) {
	      font->subtype = PDF_FONT_FONTTYPE_TYPE1C;
      } else if (pdf_font_open_truetype(font) >= 0) {
	      font->subtype = PDF_FONT_FONTTYPE_TRUETYPE;
      } else if (pdf_font_open_pkfont(font) >= 0) {
	      font->subtype = PDF_FONT_FONTTYPE_TYPE3;
      } else {
	      pdf_clean_font_struct(font);
	      return -1;
      }

      font_cache.count++;

      if (dpx_conf.verbose_level > 0) {
	      MESG("\npdf_font>> Simple font \"%s\"", fontname);
        MESG(" enc_id=<%s,%d>",
             (mrec && mrec->enc_name) ? mrec->enc_name : "builtin", font->encoding_id);
        MESG(" opened at font_id=<%s,%d>.\n", tex_name, font_id);
      }
    }
  }

  return  font_id;
}

int 
pdf_font_is_in_use (pdf_font *font)
{
  ASSERT(font);

  return ((font->reference) ? 1 : 0);
}

uint32_t
pdf_font_get_index (pdf_font *font)
{
  ASSERT(font);

  return font->index;
}

char *
pdf_font_get_ident (pdf_font *font)
{
  ASSERT(font);

  return font->ident;
}

char *
pdf_font_get_mapname (pdf_font *font)
{
  ASSERT(font);

  return font->map_name;
}

char *
pdf_font_get_fontname (pdf_font *font)
{
  ASSERT(font);

  return font->fontname;
}

pdf_obj *
pdf_font_get_resource (pdf_font *font)
{
  ASSERT(font);

  if (!font->resource) {
    font->resource = pdf_new_dict();
    pdf_add_dict(font->resource,
		 pdf_new_name("Type"),      pdf_new_name("Font"));
    switch (font->subtype) {
    case PDF_FONT_FONTTYPE_TYPE1:
    case PDF_FONT_FONTTYPE_TYPE1C:
      pdf_add_dict(font->resource,
		   pdf_new_name("Subtype"), pdf_new_name("Type1"));
      break;
    case PDF_FONT_FONTTYPE_TYPE3:
      pdf_add_dict(font->resource,
		   pdf_new_name("Subtype"), pdf_new_name("Type3"));
      break;
    case PDF_FONT_FONTTYPE_TRUETYPE:
      pdf_add_dict(font->resource,
		   pdf_new_name("Subtype"), pdf_new_name("TrueType"));
      break;
    default:
      break;
    }
  }

  return font->resource;
}

pdf_obj *
pdf_font_get_descriptor (pdf_font *font)
{
  ASSERT(font);

  if (!font->descriptor) {
    font->descriptor = pdf_new_dict();
    pdf_add_dict(font->descriptor,
		 pdf_new_name("Type"), pdf_new_name("FontDescriptor"));
  }

  return font->descriptor;
}

char *
pdf_font_get_usedchars (pdf_font *font)
{
  ASSERT(font);

  return font->usedchars;
}

int
pdf_font_get_encoding (pdf_font *font)
{
  ASSERT(font);

  return font->encoding_id;
}

int
pdf_font_get_flag (pdf_font *font, int mask)
{
  ASSERT(font);

  return ((font->flags & mask) ? 1 : 0);
}

#if 0
int
pdf_font_get_flags (pdf_font *font)
{
  ASSERT(font);

  return font->flags;
}
#endif /* 0 */

double
pdf_font_get_param (pdf_font *font, int param_type)
{
  double param = 0.0;

  ASSERT(font);

  switch (param_type) {
  case PDF_FONT_PARAM_DESIGN_SIZE:
    param = font->design_size;
    break;
  case PDF_FONT_PARAM_POINT_SIZE:
    param = font->point_size;
    break;
  default:
    break;
  }

  return param;
}

char *
pdf_font_get_uniqueTag (pdf_font *font)
{
  ASSERT(font);

  if (font->uniqueID[0] == '\0') {
    pdf_font_make_uniqueTag(font->uniqueID);
  }

  return font->uniqueID;
}

int
pdf_font_set_fontname (pdf_font *font, const char *fontname)
{
  ASSERT(font && fontname);

  if (strlen(fontname) > PDF_NAME_LEN_MAX) {
    ERROR("Unexpected error...");
    return -1;
  }
  if (font->fontname) {
    RELEASE(font->fontname);
  }
  font->fontname = NEW(strlen(fontname)+1, char);
  strcpy(font->fontname, fontname);

  return 0;
}

int
pdf_font_set_subtype (pdf_font *font, int subtype)
{
  ASSERT(font);

  font->subtype = subtype;

  return 0;
}

int
pdf_font_set_flags (pdf_font *font, int flags)
{
  ASSERT(font);

  font->flags |= flags;

  return 0;
}