1 51 package org.apache.fop.render.pdf.fonts; 52 53 import java.io.InputStream ; 55 import java.io.BufferedInputStream ; 56 import java.util.Map ; 57 import java.net.URL ; 58 59 import org.apache.fop.messaging.MessageHandler; 61 import org.apache.fop.layout.FontDescriptor; 62 import org.apache.fop.pdf.PDFStream; 63 import org.apache.fop.pdf.PDFT1Stream; 64 import org.apache.fop.pdf.PDFTTFStream; 65 import org.apache.fop.render.pdf.CodePointMapping; 66 import org.apache.fop.render.pdf.Font; 67 import org.apache.fop.fonts.type1.PFBParser; 68 import org.apache.fop.fonts.type1.PFBData; 69 import org.apache.fop.tools.IOUtil; 70 71 72 75 public class SingleByteFont extends Font implements FontDescriptor { 76 public String fontName = null; 77 public String encoding = "WinAnsiEncoding"; 78 private final CodePointMapping mapping 79 = CodePointMapping.getMapping("WinAnsiEncoding"); 80 81 public int capHeight = 0; 82 public int xHeight = 0; 83 public int ascender = 0; 84 public int descender = 0; 85 public int[] fontBBox = {0, 0, 0, 0}; 86 87 public URL embedFileName = null; 88 public String embedResourceName = null; 89 public PDFStream embeddedFont = null; 90 91 public int firstChar = 0; 92 public int lastChar = 255; 93 public int flags = 4; 94 public int stemV = 0; 95 public int italicAngle = 0; 96 public int missingWidth = 0; 97 98 public Map kerning = new java.util.HashMap (); 99 public boolean useKerning = true; 100 101 public int width[] = null; 102 public byte subType = 0; 103 104 public final boolean hasKerningInfo() { 105 return (useKerning & kerning.isEmpty()); 106 } 107 108 public final Map getKerningInfo() { 109 if (useKerning) { 110 return kerning; 111 } else { 112 return new java.util.HashMap (); 113 } 114 } 115 116 public byte getSubType() { 117 return subType; 118 } 119 120 public int getAvgWidth() { 121 return -1; 122 } 123 124 public int getMinWidth() { 125 return -1; 126 } 127 128 public int getMaxWidth() { 129 return -1; 130 } 131 132 public int getleading() { 133 return -1; 134 } 135 136 public int getStemH() { 137 return 0; 138 } 139 140 public int getMissingWidth() { 141 return missingWidth; 142 } 143 144 public String getCharEncoding() { 145 return encoding; 146 } 147 148 public boolean isEmbeddable() { 149 return (embedFileName == null && embedResourceName == null) ? false 150 : true; 151 } 152 153 154 public PDFStream getFontFile(int i) { 155 InputStream instream = null; 156 157 if (embedFileName != null) try { 159 instream = embedFileName.openStream(); 160 } catch (Exception e) { 161 MessageHandler.error("Failed to embed fontfile: " 162 + embedFileName); 163 } 164 165 if (instream == null && embedResourceName != null) try { 167 instream = 168 new BufferedInputStream (this.getClass().getResourceAsStream(embedResourceName)); 169 } catch (Exception e) { 170 MessageHandler.error("Failed to embed fontresource: " 171 + embedResourceName); 172 } 173 174 if (instream == null) { 175 return (PDFStream)null; 176 } 177 178 try { 180 if (subType == org.apache.fop.pdf.PDFFont.TYPE1) { 181 PFBParser parser = new PFBParser(); 182 PFBData pfb = parser.parsePFB(instream); 183 embeddedFont = new PDFT1Stream(i); 184 ((PDFT1Stream)embeddedFont).setData(pfb); 185 } else { 186 byte[] file = IOUtil.toByteArray(instream, 128000); 187 embeddedFont = new PDFTTFStream(i, file.length); 188 ((PDFTTFStream)embeddedFont).setData(file, file.length); 189 } 190 191 embeddedFont.addFilter("flate"); 192 embeddedFont.addFilter("ascii-85"); 193 instream.close(); 194 } catch (Exception e) { 195 MessageHandler.error("Failed to read font data for embedded font: "+e.getMessage()); 196 } 197 198 return (PDFStream)embeddedFont; 199 } 200 201 public String encoding() { 202 return encoding; 203 } 204 205 public String fontName() { 206 return fontName; 207 } 208 209 public int getAscender() { 210 return ascender; 211 } 212 213 public int getDescender() { 214 return descender; 215 } 216 217 public int getCapHeight() { 218 return capHeight; 219 } 220 221 public int getAscender(int size) { 222 return size * ascender; 223 } 224 225 public int getCapHeight(int size) { 226 return size * capHeight; 227 } 228 229 public int getDescender(int size) { 230 return size * descender; 231 } 232 233 public int getXHeight(int size) { 234 return size * xHeight; 235 } 236 237 public int getFlags() { 238 return flags; 239 } 240 241 public int[] getFontBBox() { 242 return fontBBox; 243 } 244 245 public int getItalicAngle() { 246 return italicAngle; 247 } 248 249 public int getStemV() { 250 return stemV; 251 } 252 253 public int getFirstChar() { 254 return 0; 255 } 257 258 public int getLastChar() { 259 return lastChar; 260 } 261 262 public int width(int i, int size) { 263 return size * width[i]; 264 } 265 266 public int[] getWidths(int size) { 267 int[] arr = new int[width.length]; 268 System.arraycopy(width, 0, arr, 0, width.length - 1); 269 for (int i = 0; i < arr.length; i++) { 270 arr[i] *= size; 271 } 272 return arr; 273 } 274 275 public char mapChar(char c) { 276 char d = mapping.mapChar(c); 277 if(d != 0) { 278 return d; 279 } else { 280 return '#'; 281 } 282 } 283 284 } 285 286 | Popular Tags |