-
Notifications
You must be signed in to change notification settings - Fork 628
/
Copy pathBaseFont.java
1960 lines (1839 loc) · 71.3 KB
/
BaseFont.java
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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: BaseFont.java 4065 2009-09-16 23:09:11Z psoares33 $
*
* Copyright 2000-2006 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library 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 Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* https://github.com/LibrePDF/OpenPDF
*/
package com.lowagie.text.pdf;
import com.lowagie.text.DocumentException;
import com.lowagie.text.error_messages.MessageLocalization;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
/**
* Base class for the several font types supported
*
* @author Paulo Soares ([email protected])
*/
public abstract class BaseFont {
/** This is a possible value of a base 14 type 1 font */
public static final String COURIER = "Courier";
/** This is a possible value of a base 14 type 1 font */
public static final String COURIER_BOLD = "Courier-Bold";
/** This is a possible value of a base 14 type 1 font */
public static final String COURIER_OBLIQUE = "Courier-Oblique";
/** This is a possible value of a base 14 type 1 font */
public static final String COURIER_BOLDOBLIQUE = "Courier-BoldOblique";
/** This is a possible value of a base 14 type 1 font */
public static final String HELVETICA = "Helvetica";
/** This is a possible value of a base 14 type 1 font */
public static final String HELVETICA_BOLD = "Helvetica-Bold";
/** This is a possible value of a base 14 type 1 font */
public static final String HELVETICA_OBLIQUE = "Helvetica-Oblique";
/** This is a possible value of a base 14 type 1 font */
public static final String HELVETICA_BOLDOBLIQUE = "Helvetica-BoldOblique";
/** This is a possible value of a base 14 type 1 font */
public static final String SYMBOL = "Symbol";
/** This is a possible value of a base 14 type 1 font */
public static final String TIMES_ROMAN = "Times-Roman";
/** This is a possible value of a base 14 type 1 font */
public static final String TIMES_BOLD = "Times-Bold";
/** This is a possible value of a base 14 type 1 font */
public static final String TIMES_ITALIC = "Times-Italic";
/** This is a possible value of a base 14 type 1 font */
public static final String TIMES_BOLDITALIC = "Times-BoldItalic";
/** This is a possible value of a base 14 type 1 font */
public static final String ZAPFDINGBATS = "ZapfDingbats";
/**
* The maximum height above the baseline reached by glyphs in this font,
* excluding the height of glyphs for accented characters.
*/
public static final int ASCENT = 1;
/**
* The y coordinate of the top of flat capital letters, measured from the
* baseline.
*/
public static final int CAPHEIGHT = 2;
/**
* The maximum depth below the baseline reached by glyphs in this font. The
* value is a negative number.
*/
public static final int DESCENT = 3;
/**
* The angle, expressed in degrees counterclockwise from the vertical, of
* the dominant vertical strokes of the font. The value is negative for
* fonts that slope to the right, as almost all italic fonts do.
*/
public static final int ITALICANGLE = 4;
/**
* The lower left x glyph coordinate.
*/
public static final int BBOXLLX = 5;
/**
* The lower left y glyph coordinate.
*/
public static final int BBOXLLY = 6;
/**
* The upper right x glyph coordinate.
*/
public static final int BBOXURX = 7;
/**
* The upper right y glyph coordinate.
*/
public static final int BBOXURY = 8;
/** java.awt.Font property */
public static final int AWT_ASCENT = 9;
/** java.awt.Font property */
public static final int AWT_DESCENT = 10;
/** java.awt.Font property */
public static final int AWT_LEADING = 11;
/** java.awt.Font property */
public static final int AWT_MAXADVANCE = 12;
/**
* The underline position. Usually a negative value.
*/
public static final int UNDERLINE_POSITION = 13;
/**
* The underline thickness.
*/
public static final int UNDERLINE_THICKNESS = 14;
/**
* The strikethrough position.
*/
public static final int STRIKETHROUGH_POSITION = 15;
/**
* The strikethrough thickness.
*/
public static final int STRIKETHROUGH_THICKNESS = 16;
/**
* The recommended vertical size for subscripts for this font.
*/
public static final int SUBSCRIPT_SIZE = 17;
/**
* The recommended vertical offset from the baseline for subscripts for this
* font. Usually a negative value.
*/
public static final int SUBSCRIPT_OFFSET = 18;
/**
* The recommended vertical size for superscripts for this font.
*/
public static final int SUPERSCRIPT_SIZE = 19;
/**
* The recommended vertical offset from the baseline for superscripts for
* this font.
*/
public static final int SUPERSCRIPT_OFFSET = 20;
/**
* The font is Type 1.
*/
public static final int FONT_TYPE_T1 = 0;
/**
* The font is True Type with a standard encoding.
*/
public static final int FONT_TYPE_TT = 1;
/**
* The font is CJK.
*/
public static final int FONT_TYPE_CJK = 2;
/**
* The font is True Type with a Unicode encoding.
*/
public static final int FONT_TYPE_TTUNI = 3;
/**
* A font already inside the document.
*/
public static final int FONT_TYPE_DOCUMENT = 4;
/**
* A Type3 font.
*/
public static final int FONT_TYPE_T3 = 5;
/**
* The Unicode encoding with horizontal writing.
*/
public static final String IDENTITY_H = "Identity-H";
/**
* The Unicode encoding with vertical writing.
*/
public static final String IDENTITY_V = "Identity-V";
/** A possible encoding. */
public static final String CP1250 = "Cp1250";
/** A possible encoding. */
public static final String CP1252 = "Cp1252";
/** A possible encoding. */
public static final String CP1257 = "Cp1257";
/** A possible encoding. */
public static final String WINANSI = "Cp1252";
/** A possible encoding. */
public static final String MACROMAN = "MacRoman";
public static final int[] CHAR_RANGE_LATIN = { 0, 0x17f, 0x2000, 0x206f,
0x20a0, 0x20cf, 0xfb00, 0xfb06 };
public static final int[] CHAR_RANGE_ARABIC = { 0, 0x7f, 0x0600, 0x067f,
0x20a0, 0x20cf, 0xfb50, 0xfbff, 0xfe70, 0xfeff };
public static final int[] CHAR_RANGE_HEBREW = { 0, 0x7f, 0x0590, 0x05ff,
0x20a0, 0x20cf, 0xfb1d, 0xfb4f };
public static final int[] CHAR_RANGE_CYRILLIC = { 0, 0x7f, 0x0400, 0x052f,
0x2000, 0x206f, 0x20a0, 0x20cf };
/** if the font has to be embedded */
public static final boolean EMBEDDED = true;
/** if the font doesn't have to be embedded */
public static final boolean NOT_EMBEDDED = false;
/** if the font has to be cached */
public static final boolean CACHED = true;
/** if the font doesn't have to be cached */
public static final boolean NOT_CACHED = false;
/** The path to the font resources. */
public static final String RESOURCE_PATH = "com/lowagie/text/pdf/fonts/";
/** The fake CID code that represents a newline. */
public static final char CID_NEWLINE = '\u7fff';
protected ArrayList<int[]> subsetRanges;
/**
* The font type.
*/
int fontType;
/** a not defined character in a custom PDF encoding */
public static final String notdef = ".notdef";
/**
* table of characters widths for this encoding
*/
protected int[] widths = new int[256];
/**
* encoding names
*/
protected String[] differences = new String[256];
/**
* same as differences but with the unicode codes
*/
protected char[] unicodeDifferences = new char[256];
protected int[][] charBBoxes = new int[256][];
/** encoding used with this font */
protected String encoding;
/** true if the font is to be embedded in the PDF */
protected boolean embedded;
/**
* The compression level for the font stream.
*
* @since 2.1.3
*/
protected int compressionLevel = PdfStream.DEFAULT_COMPRESSION;
/**
* true if the font must use its built in encoding. In that case the
* <CODE>encoding</CODE> is only used to map a char to the position inside
* the font, not to the expected char name.
*/
protected boolean fontSpecific = true;
/** cache for the fonts already used. */
protected static ConcurrentHashMap<String, BaseFont> fontCache = new ConcurrentHashMap<>(
500, 0.85f, 64);
/** list of the 14 built in fonts. */
protected static final HashMap<String, PdfName> BuiltinFonts14 = new HashMap<>();
/**
* Forces the output of the width array. Only matters for the 14 built-in
* fonts.
*/
protected boolean forceWidthsOutput = false;
/**
* Converts <CODE>char</CODE> directly to <CODE>byte</CODE> by casting.
*/
protected boolean directTextToByte = false;
/**
* Indicates if all the glyphs and widths for that particular encoding
* should be included in the document.
*/
protected boolean subset = true;
protected boolean fastWinansi = false;
/**
* Custom encodings use this map to key the Unicode character to the single
* byte code.
*/
protected IntHashtable specialMap;
static {
BuiltinFonts14.put(COURIER, PdfName.COURIER);
BuiltinFonts14.put(COURIER_BOLD, PdfName.COURIER_BOLD);
BuiltinFonts14.put(COURIER_BOLDOBLIQUE, PdfName.COURIER_BOLDOBLIQUE);
BuiltinFonts14.put(COURIER_OBLIQUE, PdfName.COURIER_OBLIQUE);
BuiltinFonts14.put(HELVETICA, PdfName.HELVETICA);
BuiltinFonts14.put(HELVETICA_BOLD, PdfName.HELVETICA_BOLD);
BuiltinFonts14
.put(HELVETICA_BOLDOBLIQUE, PdfName.HELVETICA_BOLDOBLIQUE);
BuiltinFonts14.put(HELVETICA_OBLIQUE, PdfName.HELVETICA_OBLIQUE);
BuiltinFonts14.put(SYMBOL, PdfName.SYMBOL);
BuiltinFonts14.put(TIMES_ROMAN, PdfName.TIMES_ROMAN);
BuiltinFonts14.put(TIMES_BOLD, PdfName.TIMES_BOLD);
BuiltinFonts14.put(TIMES_BOLDITALIC, PdfName.TIMES_BOLDITALIC);
BuiltinFonts14.put(TIMES_ITALIC, PdfName.TIMES_ITALIC);
BuiltinFonts14.put(ZAPFDINGBATS, PdfName.ZAPFDINGBATS);
}
/**
* Generates the PDF stream with the Type1 and Truetype fonts returning a
* PdfStream.
*/
static class StreamFont extends PdfStream {
/**
* Generates the PDF stream with the Type1 and Truetype fonts returning
* a PdfStream.
*
* @param contents
* the content of the stream
* @param lengths
* an array of int that describes the several lengths of each
* part of the font
* @param compressionLevel
* the compression level of the Stream
* @throws DocumentException
* error in the stream compression
* @since 2.1.3 (replaces the constructor without param
* compressionLevel)
*/
public StreamFont(byte[] contents, int[] lengths, int compressionLevel)
throws DocumentException {
try {
bytes = contents;
put(PdfName.LENGTH, new PdfNumber(bytes.length));
for (int k = 0; k < lengths.length; ++k) {
put(new PdfName("Length" + (k + 1)), new PdfNumber(
lengths[k]));
}
flateCompress(compressionLevel);
} catch (Exception e) {
throw new DocumentException(e);
}
}
/**
* Generates the PDF stream for a font.
*
* @param contents
* the content of a stream
* @param subType
* the subtype of the font.
* @param compressionLevel
* the compression level of the Stream
* @throws DocumentException
* error in the stream compression
* @since 2.1.3 (replaces the constructor without param
* compressionLevel)
*/
public StreamFont(byte[] contents, String subType, int compressionLevel)
throws DocumentException {
try {
bytes = contents;
put(PdfName.LENGTH, new PdfNumber(bytes.length));
if (subType != null) {
put(PdfName.SUBTYPE, new PdfName(subType));
}
flateCompress(compressionLevel);
} catch (Exception e) {
throw new DocumentException(e);
}
}
}
/**
* Creates new BaseFont
*/
protected BaseFont() {
}
/**
* Creates a new font. This will always be the default Helvetica font (not
* embedded). This method is introduced because Helvetica is used in many
* examples.
*
* @return a BaseFont object (Helvetica, Winansi, not embedded)
* @throws IOException
* This shouldn't occur ever
* @throws DocumentException
* This shouldn't occur ever
* @since 2.1.1
*/
public static BaseFont createFont() throws DocumentException, IOException {
return createFont(BaseFont.HELVETICA, BaseFont.WINANSI,
BaseFont.NOT_EMBEDDED);
}
/**
* Creates a new font. This font can be one of the 14 built in types, a
* Type1 font referred to by an AFM or PFM file, a TrueType font (simple or
* collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts
* and CJK fonts can have an optional style modifier appended to the name.
* These modifiers are: Bold, Italic and BoldItalic. An example would be
* "STSong-Light,Bold". Note that this modifiers do not work if the font is
* embedded. Fonts in TrueType collections are addressed by index such as
* "msgothic.ttc,1". This would get the second font (indexes start at 0), in
* this case "MS PGothic".
* <P>
* The fonts are cached and if they already exist they are extracted from
* the cache, not parsed again.
* <P>
* Besides the common encodings described by name, custom encodings can also
* be made. These encodings will only work for the single byte fonts Type1
* and TrueType. The encoding string starts with a '#' followed by "simple"
* or "full". If "simple" there is a decimal for the first character
* position and then a list of hex values representing the Unicode codes
* that compose that encoding.<br>
* The "simple" encoding is recommended for TrueType fonts as the "full"
* encoding risks not matching the character with the right glyph if not
* done with care.<br>
* The "full" encoding is specially aimed at Type1 fonts where the glyphs
* have to be described by non standard names like the Tex math fonts. Each
* group of three elements compose a code position: the one byte code order
* in decimal or as 'x' (x cannot be the space), the name and the Unicode
* character used to access the glyph. The space must be assigned to
* character position 32 otherwise text justification will not work.
* <P>
* Example for a "simple" encoding that includes the Unicode character
* space, A, B and ecyrillic:
*
* <PRE>
* "# simple 32 0020 0041 0042 0454"
* </PRE>
* <P>
* Example for a "full" encoding for a Type1 Tex font:
*
* <PRE>
* "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
* </PRE>
* <P>
* This method calls:<br>
*
* <PRE>
* createFont(name, encoding, embedded, true, null, null);
* </PRE>
*
* @param name
* the name of the font or its location on file
* @param encoding
* the encoding to be applied to this font
* @param embedded
* true if the font is to be embedded in the PDF
* @return returns a new font. This font may come from the cache
* @throws DocumentException
* the font is invalid
* @throws IOException
* the font file could not be read
*/
public static BaseFont createFont(String name, String encoding,
boolean embedded) throws DocumentException, IOException {
return createFont(name, encoding, embedded, true, null, null, false);
}
/**
* Creates a new font. This font can be one of the 14 built in types, a
* Type1 font referred to by an AFM or PFM file, a TrueType font (simple or
* collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts
* and CJK fonts can have an optional style modifier appended to the name.
* These modifiers are: Bold, Italic and BoldItalic. An example would be
* "STSong-Light,Bold". Note that this modifiers do not work if the font is
* embedded. Fonts in TrueType collections are addressed by index such as
* "msgothic.ttc,1". This would get the second font (indexes start at 0), in
* this case "MS PGothic".
* <P>
* The fonts are cached and if they already exist they are extracted from
* the cache, not parsed again.
* <P>
* Besides the common encodings described by name, custom encodings can also
* be made. These encodings will only work for the single byte fonts Type1
* and TrueType. The encoding string starts with a '#' followed by "simple"
* or "full". If "simple" there is a decimal for the first character
* position and then a list of hex values representing the Unicode codes
* that compose that encoding.<br>
* The "simple" encoding is recommended for TrueType fonts as the "full"
* encoding risks not matching the character with the right glyph if not
* done with care.<br>
* The "full" encoding is specially aimed at Type1 fonts where the glyphs
* have to be described by non standard names like the Tex math fonts. Each
* group of three elements compose a code position: the one byte code order
* in decimal or as 'x' (x cannot be the space), the name and the Unicode
* character used to access the glyph. The space must be assigned to
* character position 32 otherwise text justification will not work.
* <P>
* Example for a "simple" encoding that includes the Unicode character
* space, A, B and ecyrillic:
*
* <PRE>
* "# simple 32 0020 0041 0042 0454"
* </PRE>
* <P>
* Example for a "full" encoding for a Type1 Tex font:
*
* <PRE>
* "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
* </PRE>
* <P>
* This method calls:<br>
*
* <PRE>
* createFont(name, encoding, embedded, true, null, null);
* </PRE>
*
* @param name
* the name of the font or its location on file
* @param encoding
* the encoding to be applied to this font
* @param embedded
* true if the font is to be embedded in the PDF
* @param forceRead
* in some cases (TrueTypeFont, Type1Font), the full font file
* will be read and kept in memory if forceRead is true
* @return returns a new font. This font may come from the cache
* @throws DocumentException
* the font is invalid
* @throws IOException
* the font file could not be read
* @since 2.1.5
*/
public static BaseFont createFont(String name, String encoding,
boolean embedded, boolean forceRead) throws DocumentException,
IOException {
return createFont(name, encoding, embedded, true, null, null, forceRead);
}
/**
* Creates a new font. This font can be one of the 14 built in types, a
* Type1 font referred to by an AFM or PFM file, a TrueType font (simple or
* collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts
* and CJK fonts can have an optional style modifier appended to the name.
* These modifiers are: Bold, Italic and BoldItalic. An example would be
* "STSong-Light,Bold". Note that this modifiers do not work if the font is
* embedded. Fonts in TrueType collections are addressed by index such as
* "msgothic.ttc,1". This would get the second font (indexes start at 0), in
* this case "MS PGothic".
* <P>
* The fonts may or may not be cached depending on the flag
* <CODE>cached</CODE>. If the <CODE>byte</CODE> arrays are present the font
* will be read from them instead of the name. A name is still required to
* identify the font type.
* <P>
* Besides the common encodings described by name, custom encodings can also
* be made. These encodings will only work for the single byte fonts Type1
* and TrueType. The encoding string starts with a '#' followed by "simple"
* or "full". If "simple" there is a decimal for the first character
* position and then a list of hex values representing the Unicode codes
* that compose that encoding.<br>
* The "simple" encoding is recommended for TrueType fonts as the "full"
* encoding risks not matching the character with the right glyph if not
* done with care.<br>
* The "full" encoding is specially aimed at Type1 fonts where the glyphs
* have to be described by non standard names like the Tex math fonts. Each
* group of three elements compose a code position: the one byte code order
* in decimal or as 'x' (x cannot be the space), the name and the Unicode
* character used to access the glyph. The space must be assigned to
* character position 32 otherwise text justification will not work.
* <P>
* Example for a "simple" encoding that includes the Unicode character
* space, A, B and ecyrillic:
*
* <PRE>
* "# simple 32 0020 0041 0042 0454"
* </PRE>
* <P>
* Example for a "full" encoding for a Type1 Tex font:
*
* <PRE>
* "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
* </PRE>
*
* @param name
* the name of the font or its location on file
* @param encoding
* the encoding to be applied to this font
* @param embedded
* true if the font is to be embedded in the PDF
* @param cached
* true if the font comes from the cache or is added to the cache
* if new, false if the font is always created new
* @param ttfAfm
* the true type font or the afm in a byte array
* @param pfb
* the pfb in a byte array
* @return returns a new font. This font may come from the cache but only if
* cached is true, otherwise it will always be created new
* @throws DocumentException
* the font is invalid
* @throws IOException
* the font file could not be read
* @since iText 0.80
*/
public static BaseFont createFont(String name, String encoding,
boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb)
throws DocumentException, IOException {
return createFont(name, encoding, embedded, cached, ttfAfm, pfb, false);
}
/**
* Creates a new font. This font can be one of the 14 built in types, a
* Type1 font referred to by an AFM or PFM file, a TrueType font (simple or
* collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts
* and CJK fonts can have an optional style modifier appended to the name.
* These modifiers are: Bold, Italic and BoldItalic. An example would be
* "STSong-Light,Bold". Note that this modifiers do not work if the font is
* embedded. Fonts in TrueType collections are addressed by index such as
* "msgothic.ttc,1". This would get the second font (indexes start at 0), in
* this case "MS PGothic".
* <P>
* The fonts may or may not be cached depending on the flag
* <CODE>cached</CODE>. If the <CODE>byte</CODE> arrays are present the font
* will be read from them instead of the name. A name is still required to
* identify the font type.
* <P>
* Besides the common encodings described by name, custom encodings can also
* be made. These encodings will only work for the single byte fonts Type1
* and TrueType. The encoding string starts with a '#' followed by "simple"
* or "full". If "simple" there is a decimal for the first character
* position and then a list of hex values representing the Unicode codes
* that compose that encoding.<br>
* The "simple" encoding is recommended for TrueType fonts as the "full"
* encoding risks not matching the character with the right glyph if not
* done with care.<br>
* The "full" encoding is specially aimed at Type1 fonts where the glyphs
* have to be described by non standard names like the Tex math fonts. Each
* group of three elements compose a code position: the one byte code order
* in decimal or as 'x' (x cannot be the space), the name and the Unicode
* character used to access the glyph. The space must be assigned to
* character position 32 otherwise text justification will not work.
* <P>
* Example for a "simple" encoding that includes the Unicode character
* space, A, B and ecyrillic:
*
* <PRE>
* "# simple 32 0020 0041 0042 0454"
* </PRE>
* <P>
* Example for a "full" encoding for a Type1 Tex font:
*
* <PRE>
* "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
* </PRE>
*
* @param name
* the name of the font or its location on file
* @param encoding
* the encoding to be applied to this font
* @param embedded
* true if the font is to be embedded in the PDF
* @param cached
* true if the font comes from the cache or is added to the cache
* if new, false if the font is always created new
* @param ttfAfm
* the true type font or the afm in a byte array
* @param pfb
* the pfb in a byte array
* @param noThrow
* if true will not throw an exception if the font is not
* recognized and will return null, if false will throw an
* exception if the font is not recognized. Note that even if
* true an exception may be thrown in some circumstances. This
* parameter is useful for FontFactory that may have to check
* many invalid font names before finding the right one
* @return returns a new font. This font may come from the cache but only if
* cached is true, otherwise it will always be created new
* @throws DocumentException
* the font is invalid
* @throws IOException
* the font file could not be read
* @since 2.0.3
*/
public static BaseFont createFont(String name, String encoding,
boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb,
boolean noThrow) throws DocumentException, IOException {
return createFont(name, encoding, embedded, cached, ttfAfm, pfb, false,
false);
}
/**
* Creates a new font. This font can be one of the 14 built in types, a
* Type1 font referred to by an AFM or PFM file, a TrueType font (simple or
* collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts
* and CJK fonts can have an optional style modifier appended to the name.
* These modifiers are: Bold, Italic and BoldItalic. An example would be
* "STSong-Light,Bold". Note that this modifiers do not work if the font is
* embedded. Fonts in TrueType collections are addressed by index such as
* "msgothic.ttc,1". This would get the second font (indexes start at 0), in
* this case "MS PGothic".
* <P>
* The fonts may or may not be cached depending on the flag
* <CODE>cached</CODE>. If the <CODE>byte</CODE> arrays are present the font
* will be read from them instead of the name. A name is still required to
* identify the font type.
* <P>
* Besides the common encodings described by name, custom encodings can also
* be made. These encodings will only work for the single byte fonts Type1
* and TrueType. The encoding string starts with a '#' followed by "simple"
* or "full". If "simple" there is a decimal for the first character
* position and then a list of hex values representing the Unicode codes
* that compose that encoding.<br>
* The "simple" encoding is recommended for TrueType fonts as the "full"
* encoding risks not matching the character with the right glyph if not
* done with care.<br>
* The "full" encoding is specially aimed at Type1 fonts where the glyphs
* have to be described by non standard names like the Tex math fonts. Each
* group of three elements compose a code position: the one byte code order
* in decimal or as 'x' (x cannot be the space), the name and the Unicode
* character used to access the glyph. The space must be assigned to
* character position 32 otherwise text justification will not work.
* <P>
* Example for a "simple" encoding that includes the Unicode character
* space, A, B and ecyrillic:
*
* <PRE>
* "# simple 32 0020 0041 0042 0454"
* </PRE>
* <P>
* Example for a "full" encoding for a Type1 Tex font:
*
* <PRE>
* "# full 'A' nottriangeqlleft 0041 'B' dividemultiply 0042 32 space 0020"
* </PRE>
*
* @param name
* the name of the font or its location on file
* @param encoding
* the encoding to be applied to this font
* @param embedded
* true if the font is to be embedded in the PDF
* @param cached
* true if the font comes from the cache or is added to the cache
* if new, false if the font is always created new
* @param ttfAfm
* the true type font or the afm in a byte array
* @param pfb
* the pfb in a byte array
* @param noThrow
* if true will not throw an exception if the font is not
* recognized and will return null, if false will throw an
* exception if the font is not recognized. Note that even if
* true an exception may be thrown in some circumstances. This
* parameter is useful for FontFactory that may have to check
* many invalid font names before finding the right one
* @param forceRead
* in some cases (TrueTypeFont, Type1Font), the full font file
* will be read and kept in memory if forceRead is true
* @return returns a new font. This font may come from the cache but only if
* cached is true, otherwise it will always be created new
* @throws DocumentException
* the font is invalid
* @throws IOException
* the font file could not be read
* @since 2.1.5
*/
public static BaseFont createFont(String name, String encoding,
boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb,
boolean noThrow, boolean forceRead) throws DocumentException,
IOException {
String nameBase = getBaseName(name);
encoding = normalizeEncoding(encoding);
boolean isBuiltinFonts14 = BuiltinFonts14.containsKey(name);
boolean isCJKFont = !isBuiltinFonts14 && CJKFont.isCJKFont(
nameBase, encoding);
if (isBuiltinFonts14 || isCJKFont) {
embedded = false;
} else if (encoding.equals(IDENTITY_H) || encoding.equals(IDENTITY_V)) {
embedded = true;
}
BaseFont fontFound = null;
BaseFont fontBuilt = null;
String key = name + "\n" + encoding + "\n" + embedded;
if (cached) {
fontFound = fontCache.get(key);
if (fontFound != null) {
return fontFound;
}
}
if (isBuiltinFonts14 || name.toLowerCase().endsWith(".afm")
|| name.toLowerCase().endsWith(".pfm")) {
fontBuilt = new Type1Font(name, encoding, embedded, ttfAfm, pfb,
forceRead);
fontBuilt.fastWinansi = encoding.equals(CP1252);
} else if (nameBase.toLowerCase().endsWith(".ttf")
|| nameBase.toLowerCase().endsWith(".otf")
|| nameBase.toLowerCase().indexOf(".ttc,") > 0) {
if (encoding.equals(IDENTITY_H) || encoding.equals(IDENTITY_V)) {
fontBuilt = new TrueTypeFontUnicode(name, encoding, embedded,
ttfAfm, forceRead);
LayoutProcessor.loadFont(fontBuilt, name);
} else {
fontBuilt = new TrueTypeFont(name, encoding, embedded, ttfAfm,
false, forceRead);
fontBuilt.fastWinansi = encoding.equals(CP1252);
}
} else if (isCJKFont) {
fontBuilt = new CJKFont(name, encoding, embedded);
} else if (noThrow) {
return null;
} else {
throw new DocumentException(MessageLocalization.getComposedMessage(
"font.1.with.2.is.not.recognized", name, encoding));
}
if (cached) {
fontCache.putIfAbsent(key, fontBuilt);
return fontCache.get(key);
}
return fontBuilt;
}
/**
* Creates a font based on an existing document font. The created font font
* may not behave as expected, depending on the encoding or subset.
*
* @param fontRef
* the reference to the document font
* @return the font
*/
public static BaseFont createFont(PRIndirectReference fontRef) {
return new DocumentFont(fontRef);
}
/**
* Gets the name without the modifiers Bold, Italic or BoldItalic.
*
* @param name
* the full name of the font
* @return the name without the modifiers Bold, Italic or BoldItalic
*/
protected static String getBaseName(String name) {
if (name.endsWith(",Bold")) {
return name.substring(0, name.length() - 5);
} else if (name.endsWith(",Italic")) {
return name.substring(0, name.length() - 7);
} else if (name.endsWith(",BoldItalic")) {
return name.substring(0, name.length() - 11);
} else {
return name;
}
}
/**
* Normalize the encoding names. "winansi" is changed to "Cp1252" and
* "macroman" is changed to "MacRoman".
*
* @param enc
* the encoding to be normalized
* @return the normalized encoding
*/
protected static String normalizeEncoding(String enc) {
if (enc.equals("winansi") || enc.equals("")) {
return CP1252;
} else if (enc.equals("macroman")) {
return MACROMAN;
} else {
return enc;
}
}
/**
* Creates the <CODE>widths</CODE> and the <CODE>differences</CODE> arrays
*/
protected void createEncoding() {
if (encoding.startsWith("#")) {
specialMap = new IntHashtable();
StringTokenizer tok = new StringTokenizer(encoding.substring(1),
" ,\t\n\r\f");
if (tok.nextToken().equals("full")) {
while (tok.hasMoreTokens()) {
String order = tok.nextToken();
String name = tok.nextToken();
char uni = (char) Integer.parseInt(tok.nextToken(), 16);
int orderK;
if (order.startsWith("'")) {
orderK = order.charAt(1);
} else {
orderK = Integer.parseInt(order);
}
orderK %= 256;
specialMap.put(uni, orderK);
differences[orderK] = name;
unicodeDifferences[orderK] = uni;
widths[orderK] = getRawWidth(uni, name);
charBBoxes[orderK] = getRawCharBBox(uni, name);
}
} else {
int k = 0;
if (tok.hasMoreTokens()) {
k = Integer.parseInt(tok.nextToken());
}
while (tok.hasMoreTokens() && k < 256) {
String hex = tok.nextToken();
int uni = Integer.parseInt(hex, 16) % 0x10000;
String name = GlyphList.unicodeToName(uni);
if (name != null) {
specialMap.put(uni, k);
differences[k] = name;
unicodeDifferences[k] = (char) uni;
widths[k] = getRawWidth(uni, name);
charBBoxes[k] = getRawCharBBox(uni, name);
++k;
}
}
}
for (int k = 0; k < 256; ++k) {
if (differences[k] == null) {
differences[k] = notdef;
}
}
} else if (fontSpecific) {
for (int k = 0; k < 256; ++k) {
widths[k] = getRawWidth(k, null);
charBBoxes[k] = getRawCharBBox(k, null);
}
} else {
String s;
String name;
char c;
byte[] b = new byte[1];
for (int k = 0; k < 256; ++k) {
b[0] = (byte) k;
s = PdfEncodings.convertToString(b, encoding);
if (s.length() > 0) {
c = s.charAt(0);
} else {
c = '?';
}
name = GlyphList.unicodeToName(c);
if (name == null) {
name = notdef;
}
differences[k] = name;
unicodeDifferences[k] = c;
widths[k] = getRawWidth(c, name);
charBBoxes[k] = getRawCharBBox(c, name);
}
}
}
/**
* Gets the width from the font according to the Unicode char <CODE>c</CODE>
* or the <CODE>name</CODE>. If the <CODE>name</CODE> is null it's a
* symbolic font.
*
* @param c