1 51 package org.apache.fop.render.pdf.fonts; 52 53 import org.apache.fop.fonts.FontFileReader; 54 import org.apache.fop.fonts.TTFSubSetFile; 55 import org.apache.fop.layout.FontDescriptor; 56 import org.apache.fop.messaging.MessageHandler; 57 import org.apache.fop.pdf.PDFCIDFont; 58 import org.apache.fop.pdf.PDFStream; 59 import org.apache.fop.pdf.PDFTTFStream; 60 import org.apache.fop.pdf.PDFWArray; 61 import org.apache.fop.render.pdf.CIDFont; 62 63 import java.io.InputStream ; 64 import java.io.IOException ; 65 import java.util.Map ; 66 import java.net.URL ; 67 68 71 public class MultiByteFont extends CIDFont implements FontDescriptor { 72 public String fontName = null; 73 public String ttcName = null; 74 public String encoding = "Identity-H"; 75 76 public int capHeight = 0; 77 public int xHeight = 0; 78 public int ascender = 0; 79 public int descender = 0; 80 public int[] fontBBox = { 81 0, 0, 0, 0 82 }; 83 84 public URL embedFileName = null; 85 public String embedResourceName = null; 86 public PDFTTFStream embeddedFont = null; 87 88 public int flags = 4; 89 public int stemV = 0; 90 public int italicAngle = 0; 91 public int missingWidth = 0; 92 public int defaultWidth = 0; 93 public byte cidType = PDFCIDFont.CID_TYPE2; 94 95 public Map kerning = new java.util.HashMap (); 96 public boolean useKerning = true; 97 private String namePrefix = null; private static int uniqueCounter = 1; 99 public PDFWArray warray = new PDFWArray(); 100 public int width[] = null; 101 102 public BFEntry[] bfentries = null; 103 104 105 108 private Map usedGlyphs = new java.util.HashMap (); 109 110 113 private Map usedGlyphsIndex = new java.util.HashMap (); 114 int usedGlyphsCount = 0; 115 116 public MultiByteFont() { 117 usedGlyphs.put(new Integer (0), new Integer (0)); 119 usedGlyphsIndex.put(new Integer (0), new Integer (0)); 120 usedGlyphsCount++; 121 usedGlyphs.put(new Integer (1), new Integer (1)); 122 usedGlyphsIndex.put(new Integer (1), new Integer (1)); 123 usedGlyphsCount++; 124 usedGlyphs.put(new Integer (2), new Integer (2)); 125 usedGlyphsIndex.put(new Integer (2), new Integer (2)); 126 usedGlyphsCount++; 127 128 int cnt = 0; 130 synchronized (this.getClass()) { 131 cnt = uniqueCounter++; 132 } 133 int ctm = (int)(System.currentTimeMillis() & 0xffff); 134 namePrefix = new String (cnt + "E" + Integer.toHexString(ctm)); 135 } 136 137 public final boolean hasKerningInfo() { 138 return (useKerning & kerning.isEmpty()); 139 } 140 141 public final Map getKerningInfo() { 142 if (useKerning) 143 return kerning; 144 else 145 return new java.util.HashMap (); 146 } 147 148 public byte getSubType() { 149 return org.apache.fop.pdf.PDFFont.TYPE0; 150 } 151 152 public String getLang() { 153 return null; 154 } 155 156 public String getPanose() { 157 return null; 158 } 159 160 public int getAvgWidth() { 161 return -1; 162 } 163 164 public int getMinWidth() { 165 return -1; 166 } 167 168 public int getMaxWidth() { 169 return -1; 170 } 171 172 public int getleading() { 173 return -1; 174 } 175 176 public int getStemH() { 177 return 0; 178 } 179 180 public int getMissingWidth() { 181 return missingWidth; 182 } 183 184 public int getDefaultWidth() { 185 return defaultWidth; 186 } 187 188 public String getRegistry() { 189 return "Adobe"; 190 } 191 192 public String getOrdering() { 193 return "UCS"; 194 } 195 196 public int getSupplement() { 197 return 0; 198 } 199 200 public byte getCidType() { 201 return cidType; 202 } 203 204 public String getCidBaseFont() { 205 return isEmbeddable() ? namePrefix + fontName : fontName; 206 } 207 208 public String getCharEncoding() { 209 return "Identity-H"; 210 } 211 212 public PDFWArray getWidths() { 213 if (isEmbeddable()) { 214 warray = new PDFWArray(); 216 int[] tmpWidth = new int[usedGlyphsCount]; 217 218 for (int i = 0; i < usedGlyphsCount; i++) { 219 Integer nw = (Integer )usedGlyphsIndex.get(new Integer (i)); 220 int nwx = (nw == null) ? 0 : nw.intValue(); 221 tmpWidth[i] = width[nwx]; 222 } 223 warray.addEntry(0, tmpWidth); 224 } 225 return warray; 226 } 227 228 public boolean isEmbeddable() { 229 return (embedFileName == null && embedResourceName == null) ? false 230 : true; 231 } 232 233 234 public PDFStream getFontFile(int i) { 235 try { 236 InputStream in = embedFileName.openStream(); 237 FontFileReader reader = new FontFileReader(in); 238 in.close(); 239 TTFSubSetFile subset = new TTFSubSetFile(); 240 241 byte[] subsetFont = subset.readFont(reader, ttcName, usedGlyphs); 242 244 embeddedFont = new PDFTTFStream(i, subsetFont.length); 245 embeddedFont.addFilter("flate"); 246 embeddedFont.addFilter("ascii-85"); 247 embeddedFont.setData(subsetFont, subsetFont.length); 248 } catch (IOException ioe) { 249 MessageHandler.errorln("Failed to embed font [" + i + "] " 250 + fontName + ": " + ioe.getMessage()); 251 return (PDFStream)null; 252 } 253 254 return (PDFStream)embeddedFont; 255 } 256 257 public String encoding() { 258 return encoding; 259 } 260 261 public String fontName() { 262 return isEmbeddable() ? namePrefix + fontName : fontName; 263 } 264 265 public int getAscender() { 266 return ascender; 267 } 268 269 public int getDescender() { 270 return descender; 271 } 272 273 public int getCapHeight() { 274 return capHeight; 275 } 276 277 public int getAscender(int size) { 278 return size * ascender; 279 } 280 281 public int getCapHeight(int size) { 282 return size * capHeight; 283 } 284 285 public int getDescender(int size) { 286 return size * descender; 287 } 288 289 public int getXHeight(int size) { 290 return size * xHeight; 291 } 292 293 public int getFlags() { 294 return flags; 295 } 296 297 public int[] getFontBBox() { 298 return fontBBox; 299 } 300 301 public int getItalicAngle() { 302 return italicAngle; 303 } 304 305 public int getStemV() { 306 return stemV; 307 } 308 309 public int getFirstChar() { 310 return 0; 311 } 312 313 public int getLastChar() { 314 return 255; 315 } 316 317 public int width(int i, int size) { 318 if (isEmbeddable()) { 319 Integer idx = (Integer )usedGlyphsIndex.get(new Integer (i)); 320 return size * width[idx.intValue()]; 321 } else { 322 return size * width[i]; 323 } 324 } 325 326 public int[] getWidths(int size) { 327 int[] arr = new int[width.length]; 328 System.arraycopy(width, 0, arr, 0, width.length - 1); 329 for (int i = 0; i < arr.length; i++) 330 arr[i] *= size; 331 return arr; 332 } 333 334 public Integer reMap(Integer i) { 335 if (isEmbeddable()) { 336 Integer ret = (Integer )usedGlyphsIndex.get(i); 337 if (ret == null) 338 ret = i; 339 return ret; 340 } else { 341 return i; 342 } 343 344 } 345 346 public char mapChar(char c) { 347 int idx = (int)c; 348 int retIdx = 0; 349 350 for (int i = 0; (i < bfentries.length) && retIdx == 0; i++) { 351 if (bfentries[i].unicodeStart <= idx 352 && bfentries[i].unicodeEnd >= idx) { 353 retIdx = bfentries[i].glyphStartIndex + idx 354 - bfentries[i].unicodeStart; 355 } 356 } 357 358 if (isEmbeddable()) { 359 Integer newIdx = (Integer )usedGlyphs.get(new Integer (retIdx)); 362 if (newIdx == null) { 363 usedGlyphs.put(new Integer (retIdx), 364 new Integer (usedGlyphsCount)); 365 usedGlyphsIndex.put(new Integer (usedGlyphsCount), 366 new Integer (retIdx)); 367 retIdx = usedGlyphsCount; 368 usedGlyphsCount++; 370 } else { 371 retIdx = newIdx.intValue(); 372 } 373 } 374 375 return (char)retIdx; 376 } 377 378 } 379 380 381 382 383 384 385 386 387 388 389 | Popular Tags |