001/* 002 * $Id: TrueTypeFontUnicode.java 4784 2011-03-15 08:33:00Z blowagie $ 003 * 004 * This file is part of the iText (R) project. 005 * Copyright (c) 1998-2011 1T3XT BVBA 006 * Authors: Bruno Lowagie, Paulo Soares, et al. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Affero General Public License version 3 010 * as published by the Free Software Foundation with the addition of the 011 * following permission added to Section 15 as permitted in Section 7(a): 012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 014 * 015 * This program is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 017 * or FITNESS FOR A PARTICULAR PURPOSE. 018 * See the GNU Affero General Public License for more details. 019 * You should have received a copy of the GNU Affero General Public License 020 * along with this program; if not, see http://www.gnu.org/licenses or write to 021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 022 * Boston, MA, 02110-1301 USA, or download the license from the following URL: 023 * http://itextpdf.com/terms-of-use/ 024 * 025 * The interactive user interfaces in modified source and object code versions 026 * of this program must display Appropriate Legal Notices, as required under 027 * Section 5 of the GNU Affero General Public License. 028 * 029 * In accordance with Section 7(b) of the GNU Affero General Public License, 030 * a covered work must retain the producer line in every PDF that is created 031 * or manipulated using iText. 032 * 033 * You can be released from the requirements of the license by purchasing 034 * a commercial license. Buying such a license is mandatory as soon as you 035 * develop commercial activities involving the iText software without 036 * disclosing the source code of your own applications. 037 * These activities include: offering paid services to customers as an ASP, 038 * serving PDFs on the fly in a web application, shipping iText with a closed 039 * source product. 040 * 041 * For more information, please contact iText Software Corp. at this 042 * address: sales@itextpdf.com 043 */ 044package com.itextpdf.text.pdf; 045 046import java.io.IOException; 047import java.util.Arrays; 048import java.util.Comparator; 049import java.util.HashMap; 050import java.util.HashSet; 051 052import com.itextpdf.text.DocumentException; 053import com.itextpdf.text.Utilities; 054import com.itextpdf.text.error_messages.MessageLocalization; 055 056/** Represents a True Type font with Unicode encoding. All the character 057 * in the font can be used directly by using the encoding Identity-H or 058 * Identity-V. This is the only way to represent some character sets such 059 * as Thai. 060 * @author Paulo Soares 061 */ 062class TrueTypeFontUnicode extends TrueTypeFont implements Comparator<int[]>{ 063 064 /** 065 * <CODE>true</CODE> if the encoding is vertical. 066 */ 067 boolean vertical = false; 068 069 /** 070 * Creates a new TrueType font addressed by Unicode characters. The font 071 * will always be embedded. 072 * @param ttFile the location of the font on file. The file must end in '.ttf'. 073 * The modifiers after the name are ignored. 074 * @param enc the encoding to be applied to this font 075 * @param emb true if the font is to be embedded in the PDF 076 * @param ttfAfm the font as a <CODE>byte</CODE> array 077 * @throws DocumentException the font is invalid 078 * @throws IOException the font file could not be read 079 */ 080 TrueTypeFontUnicode(String ttFile, String enc, boolean emb, byte ttfAfm[], boolean forceRead) throws DocumentException, IOException { 081 String nameBase = getBaseName(ttFile); 082 String ttcName = getTTCName(nameBase); 083 if (nameBase.length() < ttFile.length()) { 084 style = ttFile.substring(nameBase.length()); 085 } 086 encoding = enc; 087 embedded = emb; 088 fileName = ttcName; 089 ttcIndex = ""; 090 if (ttcName.length() < nameBase.length()) 091 ttcIndex = nameBase.substring(ttcName.length() + 1); 092 fontType = FONT_TYPE_TTUNI; 093 if ((fileName.toLowerCase().endsWith(".ttf") || fileName.toLowerCase().endsWith(".otf") || fileName.toLowerCase().endsWith(".ttc")) && (enc.equals(IDENTITY_H) || enc.equals(IDENTITY_V)) && emb) { 094 process(ttfAfm, forceRead); 095 if (os_2.fsType == 2) 096 throw new DocumentException(MessageLocalization.getComposedMessage("1.cannot.be.embedded.due.to.licensing.restrictions", fileName + style)); 097 // Sivan 098 if (cmap31 == null && !fontSpecific || cmap10 == null && fontSpecific) 099 directTextToByte=true; 100 //throw new DocumentException(MessageLocalization.getComposedMessage("1.2.does.not.contain.an.usable.cmap", fileName, style)); 101 if (fontSpecific) { 102 fontSpecific = false; 103 String tempEncoding = encoding; 104 encoding = ""; 105 createEncoding(); 106 encoding = tempEncoding; 107 fontSpecific = true; 108 } 109 } 110 else 111 throw new DocumentException(MessageLocalization.getComposedMessage("1.2.is.not.a.ttf.font.file", fileName, style)); 112 vertical = enc.endsWith("V"); 113 } 114 115 /** 116 * Gets the width of a <CODE>char</CODE> in normalized 1000 units. 117 * @param char1 the unicode <CODE>char</CODE> to get the width of 118 * @return the width in normalized 1000 units 119 */ 120 @Override 121 public int getWidth(int char1) { 122 if (vertical) 123 return 1000; 124 if (fontSpecific) { 125 if ((char1 & 0xff00) == 0 || (char1 & 0xff00) == 0xf000) 126 return getRawWidth(char1 & 0xff, null); 127 else 128 return 0; 129 } 130 else { 131 return getRawWidth(char1, encoding); 132 } 133 } 134 135 /** 136 * Gets the width of a <CODE>String</CODE> in normalized 1000 units. 137 * @param text the <CODE>String</CODE> to get the width of 138 * @return the width in normalized 1000 units 139 */ 140 @Override 141 public int getWidth(String text) { 142 if (vertical) 143 return text.length() * 1000; 144 int total = 0; 145 if (fontSpecific) { 146 char cc[] = text.toCharArray(); 147 int len = cc.length; 148 for (int k = 0; k < len; ++k) { 149 char c = cc[k]; 150 if ((c & 0xff00) == 0 || (c & 0xff00) == 0xf000) 151 total += getRawWidth(c & 0xff, null); 152 } 153 } 154 else { 155 int len = text.length(); 156 for (int k = 0; k < len; ++k) { 157 if (Utilities.isSurrogatePair(text, k)) { 158 total += getRawWidth(Utilities.convertToUtf32(text, k), encoding); 159 ++k; 160 } 161 else 162 total += getRawWidth(text.charAt(k), encoding); 163 } 164 } 165 return total; 166 } 167 168 /** Creates a ToUnicode CMap to allow copy and paste from Acrobat. 169 * @param metrics metrics[0] contains the glyph index and metrics[2] 170 * contains the Unicode code 171 * @return the stream representing this CMap or <CODE>null</CODE> 172 */ 173 private PdfStream getToUnicode(Object metrics[]) { 174 if (metrics.length == 0) 175 return null; 176 StringBuffer buf = new StringBuffer( 177 "/CIDInit /ProcSet findresource begin\n" + 178 "12 dict begin\n" + 179 "begincmap\n" + 180 "/CIDSystemInfo\n" + 181 "<< /Registry (TTX+0)\n" + 182 "/Ordering (T42UV)\n" + 183 "/Supplement 0\n" + 184 ">> def\n" + 185 "/CMapName /TTX+0 def\n" + 186 "/CMapType 2 def\n" + 187 "1 begincodespacerange\n" + 188 "<0000><FFFF>\n" + 189 "endcodespacerange\n"); 190 int size = 0; 191 for (int k = 0; k < metrics.length; ++k) { 192 if (size == 0) { 193 if (k != 0) { 194 buf.append("endbfrange\n"); 195 } 196 size = Math.min(100, metrics.length - k); 197 buf.append(size).append(" beginbfrange\n"); 198 } 199 --size; 200 int metric[] = (int[])metrics[k]; 201 String fromTo = toHex(metric[0]); 202 buf.append(fromTo).append(fromTo).append(toHex(metric[2])).append('\n'); 203 } 204 buf.append( 205 "endbfrange\n" + 206 "endcmap\n" + 207 "CMapName currentdict /CMap defineresource pop\n" + 208 "end end\n"); 209 String s = buf.toString(); 210 PdfStream stream = new PdfStream(PdfEncodings.convertToBytes(s, null)); 211 stream.flateCompress(compressionLevel); 212 return stream; 213 } 214 215 private static String toHex4(int n) { 216 String s = "0000" + Integer.toHexString(n); 217 return s.substring(s.length() - 4); 218 } 219 220 /** Gets an hex string in the format "<HHHH>". 221 * @param n the number 222 * @return the hex string 223 */ 224 static String toHex(int n) { 225 if (n < 0x10000) 226 return "<" + toHex4(n) + ">"; 227 n -= 0x10000; 228 int high = n / 0x400 + 0xd800; 229 int low = n % 0x400 + 0xdc00; 230 return "[<" + toHex4(high) + toHex4(low) + ">]"; 231 } 232 233 /** Generates the CIDFontTyte2 dictionary. 234 * @param fontDescriptor the indirect reference to the font descriptor 235 * @param subsetPrefix the subset prefix 236 * @param metrics the horizontal width metrics 237 * @return a stream 238 */ 239 private PdfDictionary getCIDFontType2(PdfIndirectReference fontDescriptor, String subsetPrefix, Object metrics[]) { 240 PdfDictionary dic = new PdfDictionary(PdfName.FONT); 241 // sivan; cff 242 if (cff) { 243 dic.put(PdfName.SUBTYPE, PdfName.CIDFONTTYPE0); 244 dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName+"-"+encoding)); 245 } 246 else { 247 dic.put(PdfName.SUBTYPE, PdfName.CIDFONTTYPE2); 248 dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName)); 249 } 250 dic.put(PdfName.FONTDESCRIPTOR, fontDescriptor); 251 if (!cff) 252 dic.put(PdfName.CIDTOGIDMAP,PdfName.IDENTITY); 253 PdfDictionary cdic = new PdfDictionary(); 254 cdic.put(PdfName.REGISTRY, new PdfString("Adobe")); 255 cdic.put(PdfName.ORDERING, new PdfString("Identity")); 256 cdic.put(PdfName.SUPPLEMENT, new PdfNumber(0)); 257 dic.put(PdfName.CIDSYSTEMINFO, cdic); 258 if (!vertical) { 259 dic.put(PdfName.DW, new PdfNumber(1000)); 260 StringBuffer buf = new StringBuffer("["); 261 int lastNumber = -10; 262 boolean firstTime = true; 263 for (int k = 0; k < metrics.length; ++k) { 264 int metric[] = (int[])metrics[k]; 265 if (metric[1] == 1000) 266 continue; 267 int m = metric[0]; 268 if (m == lastNumber + 1) { 269 buf.append(' ').append(metric[1]); 270 } 271 else { 272 if (!firstTime) { 273 buf.append(']'); 274 } 275 firstTime = false; 276 buf.append(m).append('[').append(metric[1]); 277 } 278 lastNumber = m; 279 } 280 if (buf.length() > 1) { 281 buf.append("]]"); 282 dic.put(PdfName.W, new PdfLiteral(buf.toString())); 283 } 284 } 285 return dic; 286 } 287 288 /** Generates the font dictionary. 289 * @param descendant the descendant dictionary 290 * @param subsetPrefix the subset prefix 291 * @param toUnicode the ToUnicode stream 292 * @return the stream 293 */ 294 private PdfDictionary getFontBaseType(PdfIndirectReference descendant, String subsetPrefix, PdfIndirectReference toUnicode) { 295 PdfDictionary dic = new PdfDictionary(PdfName.FONT); 296 297 dic.put(PdfName.SUBTYPE, PdfName.TYPE0); 298 // The PDF Reference manual advises to add -encoding to CID font names 299 if (cff) 300 dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName+"-"+encoding)); 301 //dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName)); 302 else 303 dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName)); 304 //dic.put(PdfName.BASEFONT, new PdfName(fontName)); 305 dic.put(PdfName.ENCODING, new PdfName(encoding)); 306 dic.put(PdfName.DESCENDANTFONTS, new PdfArray(descendant)); 307 if (toUnicode != null) 308 dic.put(PdfName.TOUNICODE, toUnicode); 309 return dic; 310 } 311 312 /** The method used to sort the metrics array. 313 * @param o1 the first element 314 * @param o2 the second element 315 * @return the comparison 316 */ 317 public int compare(int[] o1, int[] o2) { 318 int m1 = o1[0]; 319 int m2 = o2[0]; 320 if (m1 < m2) 321 return -1; 322 if (m1 == m2) 323 return 0; 324 return 1; 325 } 326 327 private static final byte[] rotbits = {(byte)0x80,(byte)0x40,(byte)0x20,(byte)0x10,(byte)0x08,(byte)0x04,(byte)0x02,(byte)0x01}; 328 329 /** Outputs to the writer the font dictionaries and streams. 330 * @param writer the writer for this document 331 * @param ref the font indirect reference 332 * @param params several parameters that depend on the font type 333 * @throws IOException on error 334 * @throws DocumentException error in generating the object 335 */ 336 @SuppressWarnings("unchecked") 337 @Override 338 void writeFont(PdfWriter writer, PdfIndirectReference ref, Object params[]) throws DocumentException, IOException { 339 HashMap<Integer, int[]> longTag = (HashMap<Integer, int[]>)params[0]; 340 addRangeUni(longTag, true, subset); 341 int metrics[][] = longTag.values().toArray(new int[0][]); 342 Arrays.sort(metrics, this); 343 PdfIndirectReference ind_font = null; 344 PdfObject pobj = null; 345 PdfIndirectObject obj = null; 346 PdfIndirectReference cidset = null; 347 if (writer.getPDFXConformance() == PdfWriter.PDFA1A || writer.getPDFXConformance() == PdfWriter.PDFA1B) { 348 PdfStream stream; 349 if (metrics.length == 0) { 350 stream = new PdfStream(new byte[]{(byte)0x80}); 351 } 352 else { 353 int top = metrics[metrics.length - 1][0]; 354 byte[] bt = new byte[top / 8 + 1]; 355 for (int k = 0; k < metrics.length; ++k) { 356 int v = metrics[k][0]; 357 bt[v / 8] |= rotbits[v % 8]; 358 } 359 stream = new PdfStream(bt); 360 stream.flateCompress(compressionLevel); 361 } 362 cidset = writer.addToBody(stream).getIndirectReference(); 363 } 364 // sivan: cff 365 if (cff) { 366 byte b[] = readCffFont(); 367 if (subset || subsetRanges != null) { 368 CFFFontSubset cff = new CFFFontSubset(new RandomAccessFileOrArray(b),longTag); 369 b = cff.Process(cff.getNames()[0]); 370 } 371 pobj = new StreamFont(b, "CIDFontType0C", compressionLevel); 372 obj = writer.addToBody(pobj); 373 ind_font = obj.getIndirectReference(); 374 } else { 375 byte[] b; 376 if (subset || directoryOffset != 0) { 377 TrueTypeFontSubSet sb = new TrueTypeFontSubSet(fileName, new RandomAccessFileOrArray(rf), new HashSet<Integer>(longTag.keySet()), directoryOffset, false, false); 378 b = sb.process(); 379 } 380 else { 381 b = getFullFont(); 382 } 383 int lengths[] = new int[]{b.length}; 384 pobj = new StreamFont(b, lengths, compressionLevel); 385 obj = writer.addToBody(pobj); 386 ind_font = obj.getIndirectReference(); 387 } 388 String subsetPrefix = ""; 389 if (subset) 390 subsetPrefix = createSubsetPrefix(); 391 PdfDictionary dic = getFontDescriptor(ind_font, subsetPrefix, cidset); 392 obj = writer.addToBody(dic); 393 ind_font = obj.getIndirectReference(); 394 395 pobj = getCIDFontType2(ind_font, subsetPrefix, metrics); 396 obj = writer.addToBody(pobj); 397 ind_font = obj.getIndirectReference(); 398 399 pobj = getToUnicode(metrics); 400 PdfIndirectReference toUnicodeRef = null; 401 402 if (pobj != null) { 403 obj = writer.addToBody(pobj); 404 toUnicodeRef = obj.getIndirectReference(); 405 } 406 407 pobj = getFontBaseType(ind_font, subsetPrefix, toUnicodeRef); 408 writer.addToBody(pobj, ref); 409 } 410 411 /** 412 * Returns a PdfStream object with the full font program. 413 * @return a PdfStream with the font program 414 * @since 2.1.3 415 */ 416 @Override 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 */ 428 @Override 429 byte[] convertToBytes(String text) { 430 return null; 431 } 432 433 @Override 434 byte[] convertToBytes(int char1) { 435 return null; 436 } 437 438 /** Gets the glyph index and metrics for a character. 439 * @param c the character 440 * @return an <CODE>int</CODE> array with {glyph index, width} 441 */ 442 @Override 443 public int[] getMetricsTT(int c) { 444 if (cmapExt != null) 445 return cmapExt.get(Integer.valueOf(c)); 446 HashMap<Integer, int[]> map = null; 447 if (fontSpecific) 448 map = cmap10; 449 else 450 map = cmap31; 451 if (map == null) 452 return null; 453 if (fontSpecific) { 454 if ((c & 0xffffff00) == 0 || (c & 0xffffff00) == 0xf000) 455 return map.get(Integer.valueOf(c & 0xff)); 456 else 457 return null; 458 } 459 else 460 return map.get(Integer.valueOf(c)); 461 } 462 463 /** 464 * Checks if a character exists in this font. 465 * @param c the character to check 466 * @return <CODE>true</CODE> if the character has a glyph, 467 * <CODE>false</CODE> otherwise 468 */ 469 @Override 470 public boolean charExists(int c) { 471 return getMetricsTT(c) != null; 472 } 473 474 /** 475 * Sets the character advance. 476 * @param c the character 477 * @param advance the character advance normalized to 1000 units 478 * @return <CODE>true</CODE> if the advance was set, 479 * <CODE>false</CODE> otherwise 480 */ 481 @Override 482 public boolean setCharAdvance(int c, int advance) { 483 int[] m = getMetricsTT(c); 484 if (m == null) 485 return false; 486 m[1] = advance; 487 return true; 488 } 489 490 @Override 491 public int[] getCharBBox(int c) { 492 if (bboxes == null) 493 return null; 494 int[] m = getMetricsTT(c); 495 if (m == null) 496 return null; 497 return bboxes[m[0]]; 498 } 499}