1 17 18 19 20 package org.apache.fop.fonts.truetype; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.fop.fonts.BFEntry; 29 import org.apache.fop.fonts.CIDFontType; 30 import org.apache.fop.fonts.CustomFont; 31 import org.apache.fop.fonts.FontLoader; 32 import org.apache.fop.fonts.FontResolver; 33 import org.apache.fop.fonts.MultiByteFont; 34 35 38 public class TTFFontLoader extends FontLoader { 39 40 private String fontFileURI; 41 private TTFFile ttf; 42 private MultiByteFont multiFont; 43 private CustomFont returnFont; 44 private FontResolver resolver; 45 46 public TTFFontLoader(String fontFileURI, InputStream in, FontResolver resolver) 47 throws IOException { 48 this.fontFileURI = fontFileURI; 49 this.resolver = resolver; 50 51 this.ttf = new TTFFile(); 52 FontFileReader reader = new FontFileReader(in); 53 boolean supported = ttf.readFont(reader, null); 54 if (!supported) { 55 throw new IOException ("Could not load TrueType font: " + fontFileURI); 56 } 57 multiFont = new MultiByteFont(); 58 multiFont.setResolver(this.resolver); 59 returnFont = multiFont; 60 read(); 61 } 62 63 private void read() throws IOException { 64 returnFont.setFontName(ttf.getFamilyName()); 65 returnFont.setCapHeight(ttf.getCapHeight()); 67 returnFont.setXHeight(ttf.getXHeight()); 68 returnFont.setAscender(ttf.getLowerCaseAscent()); 69 returnFont.setDescender(ttf.getLowerCaseDescent()); 70 returnFont.setFontBBox(ttf.getFontBBox()); 71 returnFont.setFlags(ttf.getFlags()); 73 returnFont.setStemV(Integer.parseInt(ttf.getStemV())); returnFont.setItalicAngle(Integer.parseInt(ttf.getItalicAngle())); 75 returnFont.setMissingWidth(0); 76 multiFont.setCIDType(CIDFontType.CIDTYPE2); 77 int[] wx = ttf.getWidths(); 78 multiFont.setWidthArray(wx); 79 List entries = ttf.getCMaps(); 80 BFEntry[] bfentries = new BFEntry[entries.size()]; 81 int pos = 0; 82 Iterator iter = ttf.getCMaps().listIterator(); 83 while (iter.hasNext()) { 84 TTFCmapEntry ce = (TTFCmapEntry)iter.next(); 85 bfentries[pos] = new BFEntry(ce.getUnicodeStart(), ce.getUnicodeEnd(), 86 ce.getGlyphStartIndex()); 87 pos++; 88 } 89 multiFont.setBFEntries(bfentries); 90 copyKerning(ttf, true); 91 multiFont.setEmbedFileName(this.fontFileURI); 92 93 } 94 95 private void copyKerning(TTFFile ttf, boolean isCid) { 96 97 Iterator iter; 99 if (isCid) { 100 iter = ttf.getKerning().keySet().iterator(); 101 } else { 102 iter = ttf.getAnsiKerning().keySet().iterator(); 103 } 104 105 while (iter.hasNext()) { 106 Integer kpx1 = (Integer )iter.next(); 107 108 Map h2; 109 if (isCid) { 110 h2 = (Map )ttf.getKerning().get(kpx1); 111 } else { 112 h2 = (Map )ttf.getAnsiKerning().get(kpx1); 113 } 114 115 returnFont.putKerningEntry(kpx1, h2); 116 } 117 } 118 119 120 121 public CustomFont getFont() { 122 return this.returnFont; 123 } 124 125 } 126 | Popular Tags |