1 18 package org.apache.batik.svggen.font; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.RandomAccessFile ; 23 24 import org.apache.batik.svggen.font.table.CmapTable; 25 import org.apache.batik.svggen.font.table.GlyfTable; 26 import org.apache.batik.svggen.font.table.HeadTable; 27 import org.apache.batik.svggen.font.table.HheaTable; 28 import org.apache.batik.svggen.font.table.HmtxTable; 29 import org.apache.batik.svggen.font.table.LocaTable; 30 import org.apache.batik.svggen.font.table.MaxpTable; 31 import org.apache.batik.svggen.font.table.NameTable; 32 import org.apache.batik.svggen.font.table.Os2Table; 33 import org.apache.batik.svggen.font.table.PostTable; 34 import org.apache.batik.svggen.font.table.Table; 35 import org.apache.batik.svggen.font.table.TableDirectory; 36 import org.apache.batik.svggen.font.table.TableFactory; 37 38 43 public class Font { 44 45 private String path; 46 private TableDirectory tableDirectory = null; 49 private Table[] tables; 50 private Os2Table os2; 51 private CmapTable cmap; 52 private GlyfTable glyf; 53 private HeadTable head; 54 private HheaTable hhea; 55 private HmtxTable hmtx; 56 private LocaTable loca; 57 private MaxpTable maxp; 58 private NameTable name; 59 private PostTable post; 60 61 64 public Font() { 65 } 66 67 public Table getTable(int tableType) { 68 for (int i = 0; i < tables.length; i++) { 69 if ((tables[i] != null) && (tables[i].getType() == tableType)) { 70 return tables[i]; 71 } 72 } 73 return null; 74 } 75 76 public Os2Table getOS2Table() { 77 return os2; 78 } 79 80 public CmapTable getCmapTable() { 81 return cmap; 82 } 83 84 public HeadTable getHeadTable() { 85 return head; 86 } 87 88 public HheaTable getHheaTable() { 89 return hhea; 90 } 91 92 public HmtxTable getHmtxTable() { 93 return hmtx; 94 } 95 96 public LocaTable getLocaTable() { 97 return loca; 98 } 99 100 public MaxpTable getMaxpTable() { 101 return maxp; 102 } 103 104 public NameTable getNameTable() { 105 return name; 106 } 107 108 public PostTable getPostTable() { 109 return post; 110 } 111 112 public int getAscent() { 113 return hhea.getAscender(); 114 } 115 116 public int getDescent() { 117 return hhea.getDescender(); 118 } 119 120 public int getNumGlyphs() { 121 return maxp.getNumGlyphs(); 122 } 123 124 public Glyph getGlyph(int i) { 125 return (glyf.getDescription(i) != null) 126 ? new Glyph( 127 glyf.getDescription(i), 128 hmtx.getLeftSideBearing(i), 129 hmtx.getAdvanceWidth(i)) 130 : null; 131 } 132 133 public String getPath() { 134 return path; 135 } 136 137 public TableDirectory getTableDirectory() { 138 return tableDirectory; 139 } 140 141 144 protected void read(String pathName) { 145 path = pathName; 146 File f = new File (pathName); 147 148 if (!f.exists()) { 149 return; 151 } 152 153 try { 154 RandomAccessFile raf = new RandomAccessFile (f, "r"); 155 tableDirectory = new TableDirectory(raf); 156 tables = new Table[tableDirectory.getNumTables()]; 157 158 for (int i = 0; i < tableDirectory.getNumTables(); i++) { 160 tables[i] = TableFactory.create 161 (tableDirectory.getEntry(i), raf); 162 } 163 raf.close(); 164 165 os2 = (Os2Table) getTable(Table.OS_2); 167 cmap = (CmapTable) getTable(Table.cmap); 168 glyf = (GlyfTable) getTable(Table.glyf); 169 head = (HeadTable) getTable(Table.head); 170 hhea = (HheaTable) getTable(Table.hhea); 171 hmtx = (HmtxTable) getTable(Table.hmtx); 172 loca = (LocaTable) getTable(Table.loca); 173 maxp = (MaxpTable) getTable(Table.maxp); 174 name = (NameTable) getTable(Table.name); 175 post = (PostTable) getTable(Table.post); 176 177 hmtx.init(hhea.getNumberOfHMetrics(), 179 maxp.getNumGlyphs() - hhea.getNumberOfHMetrics()); 180 loca.init(maxp.getNumGlyphs(), head.getIndexToLocFormat() == 0); 181 glyf.init(maxp.getNumGlyphs(), loca); 182 } catch (IOException e) { 183 e.printStackTrace(); 184 } 185 } 186 187 public static Font create() { 188 return new Font(); 189 } 190 191 194 public static Font create(String pathName) { 195 Font f = new Font(); 196 f.read(pathName); 197 return f; 198 } 199 } 200 | Popular Tags |