1 /*
2  * $Id$
3  *
4  * Copyright 2001, 2002 Paulo Soares
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */
49 
50 package com.lowagie.text.pdf;
51 
52 import java.io.IOException;
53 import java.util.Arrays;
54 import java.util.Comparator;
55 import java.util.HashMap;
56 import com.lowagie.text.error_messages.MessageLocalization;
57 
58 import com.lowagie.text.DocumentException;
59 import com.lowagie.text.Utilities;
60 
61 /** Represents a True Type font with Unicode encoding. All the character
62  * in the font can be used directly by using the encoding Identity-H or
63  * Identity-V. This is the only way to represent some character sets such
64  * as Thai.
65  * @author  Paulo Soares (psoares@consiste.pt)
66  */
67 class TrueTypeFontUnicode extends TrueTypeFont implements Comparator{
68 
69     /**
70      * <CODE>true</CODE> if the encoding is vertical.
71      */
72     boolean vertical = false;
73 
74     /**
75      * Creates a new TrueType font addressed by Unicode characters. The font
76      * will always be embedded.
77      * @param ttFile the location of the font on file. The file must end in '.ttf'.
78      * The modifiers after the name are ignored.
79      * @param enc the encoding to be applied to this font
80      * @param emb true if the font is to be embedded in the PDF
81      * @param ttfAfm the font as a <CODE>byte</CODE> array
82      * @throws DocumentException the font is invalid
83      * @throws IOException the font file could not be read
84      */
TrueTypeFontUnicode(String ttFile, String enc, boolean emb, byte ttfAfm[], boolean forceRead)85     TrueTypeFontUnicode(String ttFile, String enc, boolean emb, byte ttfAfm[], boolean forceRead) throws DocumentException, IOException {
86         String nameBase = getBaseName(ttFile);
87         String ttcName = getTTCName(nameBase);
88         if (nameBase.length() < ttFile.length()) {
89             style = ttFile.substring(nameBase.length());
90         }
91         encoding = enc;
92         embedded = emb;
93         fileName = ttcName;
94         ttcIndex = "";
95         if (ttcName.length() < nameBase.length())
96             ttcIndex = nameBase.substring(ttcName.length() + 1);
97         fontType = FONT_TYPE_TTUNI;
98         if ((fileName.toLowerCase().endsWith(".ttf") || fileName.toLowerCase().endsWith(".otf") || fileName.toLowerCase().endsWith(".ttc")) && ((enc.equals(IDENTITY_H) || enc.equals(IDENTITY_V)) && emb)) {
99             process(ttfAfm, forceRead);
100             if (os_2.fsType == 2)
101                 throw new DocumentException(MessageLocalization.getComposedMessage("1.cannot.be.embedded.due.to.licensing.restrictions", fileName + style));
102             // Sivan
103             if ((cmap31 == null && !fontSpecific) || (cmap10 == null && fontSpecific))
104                 directTextToByte=true;
105                 //throw new DocumentException(MessageLocalization.getComposedMessage("1.2.does.not.contain.an.usable.cmap", fileName, style));
106             if (fontSpecific) {
107                 fontSpecific = false;
108                 String tempEncoding = encoding;
109                 encoding = "";
110                 createEncoding();
111                 encoding = tempEncoding;
112                 fontSpecific = true;
113             }
114         }
115         else
116             throw new DocumentException(MessageLocalization.getComposedMessage("1.2.is.not.a.ttf.font.file", fileName, style));
117         vertical = enc.endsWith("V");
118     }
119 
120     /**
121      * Gets the width of a <CODE>char</CODE> in normalized 1000 units.
122      * @param char1 the unicode <CODE>char</CODE> to get the width of
123      * @return the width in normalized 1000 units
124      */
getWidth(int char1)125     public int getWidth(int char1) {
126         if (vertical)
127             return 1000;
128         if (fontSpecific) {
129             if ((char1 & 0xff00) == 0 || (char1 & 0xff00) == 0xf000)
130                 return getRawWidth(char1 & 0xff, null);
131             else
132                 return 0;
133         }
134         else {
135             return getRawWidth(char1, encoding);
136         }
137     }
138 
139     /**
140      * Gets the width of a <CODE>String</CODE> in normalized 1000 units.
141      * @param text the <CODE>String</CODE> to get the width of
142      * @return the width in normalized 1000 units
143      */
getWidth(String text)144     public int getWidth(String text) {
145         if (vertical)
146             return text.length() * 1000;
147         int total = 0;
148         if (fontSpecific) {
149             char cc[] = text.toCharArray();
150             int len = cc.length;
151             for (int k = 0; k < len; ++k) {
152                 char c = cc[k];
153                 if ((c & 0xff00) == 0 || (c & 0xff00) == 0xf000)
154                     total += getRawWidth(c & 0xff, null);
155             }
156         }
157         else {
158             int len = text.length();
159             for (int k = 0; k < len; ++k) {
160                 if (Utilities.isSurrogatePair(text, k)) {
161                     total += getRawWidth(Utilities.convertToUtf32(text, k), encoding);
162                     ++k;
163                 }
164                 else
165                     total += getRawWidth(text.charAt(k), encoding);
166             }
167         }
168         return total;
169     }
170 
171     /** Creates a ToUnicode CMap to allow copy and paste from Acrobat.
172      * @param metrics metrics[0] contains the glyph index and metrics[2]
173      * contains the Unicode code
174      * @return the stream representing this CMap or <CODE>null</CODE>
175      */
getToUnicode(Object metrics[])176     private PdfStream getToUnicode(Object metrics[]) {
177         if (metrics.length == 0)
178             return null;
179         StringBuffer buf = new StringBuffer(
180         "/CIDInit /ProcSet findresource begin\n" +
181         "12 dict begin\n" +
182         "begincmap\n" +
183         "/CIDSystemInfo\n" +
184         "<< /Registry (TTX+0)\n" +
185         "/Ordering (T42UV)\n" +
186         "/Supplement 0\n" +
187         ">> def\n" +
188         "/CMapName /TTX+0 def\n" +
189         "/CMapType 2 def\n" +
190         "1 begincodespacerange\n" +
191         "<0000><FFFF>\n" +
192         "endcodespacerange\n");
193         int size = 0;
194         for (int k = 0; k < metrics.length; ++k) {
195             if (size == 0) {
196                 if (k != 0) {
197                     buf.append("endbfrange\n");
198                 }
199                 size = Math.min(100, metrics.length - k);
200                 buf.append(size).append(" beginbfrange\n");
201             }
202             --size;
203             int metric[] = (int[])metrics[k];
204             String fromTo = toHex(metric[0]);
205             buf.append(fromTo).append(fromTo).append(toHex(metric[2])).append('\n');
206         }
207         buf.append(
208         "endbfrange\n" +
209         "endcmap\n" +
210         "CMapName currentdict /CMap defineresource pop\n" +
211         "end end\n");
212         String s = buf.toString();
213         PdfStream stream = new PdfStream(PdfEncodings.convertToBytes(s, null));
214         stream.flateCompress(compressionLevel);
215         return stream;
216     }
217 
toHex4(int n)218     private static String toHex4(int n) {
219         String s = "0000" + Integer.toHexString(n);
220         return s.substring(s.length() - 4);
221     }
222 
223     /** Gets an hex string in the format "&lt;HHHH&gt;".
224      * @param n the number
225      * @return the hex string
226      */
toHex(int n)227     static String toHex(int n) {
228         if (n < 0x10000)
229             return "<" + toHex4(n) + ">";
230         n -= 0x10000;
231         int high = (n / 0x400) + 0xd800;
232         int low = (n % 0x400) + 0xdc00;
233         return "[<" + toHex4(high) + toHex4(low) + ">]";
234     }
235 
236     /** Generates the CIDFontTyte2 dictionary.
237      * @param fontDescriptor the indirect reference to the font descriptor
238      * @param subsetPrefix the subset prefix
239      * @param metrics the horizontal width metrics
240      * @return a stream
241      */
getCIDFontType2(PdfIndirectReference fontDescriptor, String subsetPrefix, Object metrics[])242     private PdfDictionary getCIDFontType2(PdfIndirectReference fontDescriptor, String subsetPrefix, Object metrics[]) {
243         PdfDictionary dic = new PdfDictionary(PdfName.FONT);
244         // sivan; cff
245         if (cff) {
246 			dic.put(PdfName.SUBTYPE, PdfName.CIDFONTTYPE0);
247             dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName+"-"+encoding));
248         }
249 		else {
250 			dic.put(PdfName.SUBTYPE, PdfName.CIDFONTTYPE2);
251             dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName));
252         }
253         dic.put(PdfName.FONTDESCRIPTOR, fontDescriptor);
254         if (!cff)
255           dic.put(PdfName.CIDTOGIDMAP,PdfName.IDENTITY);
256         PdfDictionary cdic = new PdfDictionary();
257         cdic.put(PdfName.REGISTRY, new PdfString("Adobe"));
258         cdic.put(PdfName.ORDERING, new PdfString("Identity"));
259         cdic.put(PdfName.SUPPLEMENT, new PdfNumber(0));
260         dic.put(PdfName.CIDSYSTEMINFO, cdic);
261         if (!vertical) {
262             dic.put(PdfName.DW, new PdfNumber(1000));
263             StringBuffer buf = new StringBuffer("[");
264             int lastNumber = -10;
265             boolean firstTime = true;
266             for (int k = 0; k < metrics.length; ++k) {
267                 int metric[] = (int[])metrics[k];
268                 if (metric[1] == 1000)
269                     continue;
270                 int m = metric[0];
271                 if (m == lastNumber + 1) {
272                     buf.append(' ').append(metric[1]);
273                 }
274                 else {
275                     if (!firstTime) {
276                         buf.append(']');
277                     }
278                     firstTime = false;
279                     buf.append(m).append('[').append(metric[1]);
280                 }
281                 lastNumber = m;
282             }
283             if (buf.length() > 1) {
284                 buf.append("]]");
285                 dic.put(PdfName.W, new PdfLiteral(buf.toString()));
286             }
287         }
288         return dic;
289     }
290 
291     /** Generates the font dictionary.
292      * @param descendant the descendant dictionary
293      * @param subsetPrefix the subset prefix
294      * @param toUnicode the ToUnicode stream
295      * @return the stream
296      */
getFontBaseType(PdfIndirectReference descendant, String subsetPrefix, PdfIndirectReference toUnicode)297     private PdfDictionary getFontBaseType(PdfIndirectReference descendant, String subsetPrefix, PdfIndirectReference toUnicode) {
298         PdfDictionary dic = new PdfDictionary(PdfName.FONT);
299 
300         dic.put(PdfName.SUBTYPE, PdfName.TYPE0);
301         // The PDF Reference manual advises to add -encoding to CID font names
302 		if (cff)
303 		  dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName+"-"+encoding));
304 		  //dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName));
305 		else
306 		  dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName));
307 		  //dic.put(PdfName.BASEFONT, new PdfName(fontName));
308         dic.put(PdfName.ENCODING, new PdfName(encoding));
309         dic.put(PdfName.DESCENDANTFONTS, new PdfArray(descendant));
310         if (toUnicode != null)
311             dic.put(PdfName.TOUNICODE, toUnicode);
312         return dic;
313     }
314 
315     /** The method used to sort the metrics array.
316      * @param o1 the first element
317      * @param o2 the second element
318      * @return the comparison
319      */
compare(Object o1, Object o2)320     public int compare(Object o1, Object o2) {
321         int m1 = ((int[])o1)[0];
322         int m2 = ((int[])o2)[0];
323         if (m1 < m2)
324             return -1;
325         if (m1 == m2)
326             return 0;
327         return 1;
328     }
329 
330     private static final byte[] rotbits = {(byte)0x80,(byte)0x40,(byte)0x20,(byte)0x10,(byte)0x08,(byte)0x04,(byte)0x02,(byte)0x01};
331 
332     /** Outputs to the writer the font dictionaries and streams.
333      * @param writer the writer for this document
334      * @param ref the font indirect reference
335      * @param params several parameters that depend on the font type
336      * @throws IOException on error
337      * @throws DocumentException error in generating the object
338      */
writeFont(PdfWriter writer, PdfIndirectReference ref, Object params[])339     void writeFont(PdfWriter writer, PdfIndirectReference ref, Object params[]) throws DocumentException, IOException {
340         HashMap longTag = (HashMap)params[0];
341         addRangeUni(longTag, true, subset);
342         Object metrics[] = longTag.values().toArray();
343         Arrays.sort(metrics, this);
344         PdfIndirectReference ind_font = null;
345         PdfObject pobj = null;
346         PdfIndirectObject obj = null;
347         PdfIndirectReference cidset = null;
348         if (writer.getPDFXConformance() == PdfWriter.PDFA1A || writer.getPDFXConformance() == PdfWriter.PDFA1B) {
349             PdfStream stream;
350             if (metrics.length == 0) {
351                 stream = new PdfStream(new byte[]{(byte)0x80});
352             }
353             else {
354                 int top = ((int[])metrics[metrics.length - 1])[0];
355                 byte[] bt = new byte[top / 8 + 1];
356                 for (int k = 0; k < metrics.length; ++k) {
357                     int v = ((int[])metrics[k])[0];
358                     bt[v / 8] |= rotbits[v % 8];
359                 }
360                 stream = new PdfStream(bt);
361                 stream.flateCompress(compressionLevel);
362             }
363             cidset = writer.addToBody(stream).getIndirectReference();
364         }
365         // sivan: cff
366         if (cff) {
367 			byte b[] = readCffFont();
368             if (subset || subsetRanges != null) {
369                 CFFFontSubset cff = new CFFFontSubset(new RandomAccessFileOrArray(b),longTag);
370                 b = cff.Process(cff.getNames()[0]);
371             }
372 			pobj = new StreamFont(b, "CIDFontType0C", compressionLevel);
373 			obj = writer.addToBody(pobj);
374 			ind_font = obj.getIndirectReference();
375         } else {
376             byte[] b;
377             if (subset || directoryOffset != 0) {
378                 TrueTypeFontSubSet sb = new TrueTypeFontSubSet(fileName, new RandomAccessFileOrArray(rf), longTag, directoryOffset, false, false);
379                 b = sb.process();
380             }
381             else {
382                 b = getFullFont();
383             }
384             int lengths[] = new int[]{b.length};
385             pobj = new StreamFont(b, lengths, compressionLevel);
386             obj = writer.addToBody(pobj);
387             ind_font = obj.getIndirectReference();
388         }
389         String subsetPrefix = "";
390         if (subset)
391             subsetPrefix = createSubsetPrefix();
392         PdfDictionary dic = getFontDescriptor(ind_font, subsetPrefix, cidset);
393         obj = writer.addToBody(dic);
394         ind_font = obj.getIndirectReference();
395 
396         pobj = getCIDFontType2(ind_font, subsetPrefix, metrics);
397         obj = writer.addToBody(pobj);
398         ind_font = obj.getIndirectReference();
399 
400         pobj = getToUnicode(metrics);
401         PdfIndirectReference toUnicodeRef = null;
402 
403         if (pobj != null) {
404             obj = writer.addToBody(pobj);
405             toUnicodeRef = obj.getIndirectReference();
406         }
407 
408         pobj = getFontBaseType(ind_font, subsetPrefix, toUnicodeRef);
409         writer.addToBody(pobj, ref);
410     }
411 
412     /**
413      * Returns a PdfStream object with the full font program.
414      * @return	a PdfStream with the font program
415      * @since	2.1.3
416      */
getFullFontStream()417     public PdfStream getFullFontStream() throws IOException, DocumentException {
418     	if (cff) {
419 			return new StreamFont(readCffFont(), "CIDFontType0C", compressionLevel);
420         }
421     	return super.getFullFontStream();
422     }
423 
424     /** A forbidden operation. Will throw a null pointer exception.
425      * @param text the text
426      * @return always <CODE>null</CODE>
427      */
convertToBytes(String text)428     byte[] convertToBytes(String text) {
429         return null;
430     }
431 
convertToBytes(int char1)432     byte[] convertToBytes(int char1) {
433         return null;
434     }
435 
436     /** Gets the glyph index and metrics for a character.
437      * @param c the character
438      * @return an <CODE>int</CODE> array with {glyph index, width}
439      */
getMetricsTT(int c)440     public int[] getMetricsTT(int c) {
441         if (cmapExt != null)
442             return (int[])cmapExt.get(new Integer(c));
443         HashMap map = null;
444         if (fontSpecific)
445             map = cmap10;
446         else
447             map = cmap31;
448         if (map == null)
449             return null;
450         if (fontSpecific) {
451             if ((c & 0xffffff00) == 0 || (c & 0xffffff00) == 0xf000)
452                 return (int[])map.get(new Integer(c & 0xff));
453             else
454                 return null;
455         }
456         else
457             return (int[])map.get(new Integer(c));
458     }
459 
460     /**
461      * Checks if a character exists in this font.
462      * @param c the character to check
463      * @return <CODE>true</CODE> if the character has a glyph,
464      * <CODE>false</CODE> otherwise
465      */
charExists(int c)466     public boolean charExists(int c) {
467         return getMetricsTT(c) != null;
468     }
469 
470     /**
471      * Sets the character advance.
472      * @param c the character
473      * @param advance the character advance normalized to 1000 units
474      * @return <CODE>true</CODE> if the advance was set,
475      * <CODE>false</CODE> otherwise
476      */
setCharAdvance(int c, int advance)477     public boolean setCharAdvance(int c, int advance) {
478         int[] m = getMetricsTT(c);
479         if (m == null)
480             return false;
481         m[1] = advance;
482         return true;
483     }
484 
getCharBBox(int c)485     public int[] getCharBBox(int c) {
486         if (bboxes == null)
487             return null;
488         int[] m = getMetricsTT(c);
489         if (m == null)
490             return null;
491         return bboxes[m[0]];
492     }
493 }
494