1 17 18 19 20 package org.apache.fop.render.afp.fonts; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.fop.render.afp.exceptions.FontRuntimeException; 29 30 37 public class RasterFont extends AFPFont { 38 39 40 protected static final Log log = LogFactory.getLog("org.apache.fop.render.afp.fonts"); 41 42 private HashMap _characterSets = new HashMap (); 43 44 private CharacterSet _characterSet = null; 45 46 53 public RasterFont(String name) { 54 super(name); 55 } 56 57 public void addCharacterSet(int size, CharacterSet characterSet) { 58 59 _characterSets.put(String.valueOf(size), characterSet); 60 61 _characterSet = characterSet; 62 63 } 64 65 71 public CharacterSet getCharacterSet(int size) { 72 73 String pointsize = String.valueOf(size / 1000); 74 CharacterSet csm = (CharacterSet) _characterSets.get(pointsize); 75 if (csm == null) { 76 csm = (CharacterSet) _characterSets.get(size + "mpt"); 77 } 78 if (csm == null) { 79 int distance = Integer.MAX_VALUE; 81 for (Iterator it = _characterSets.entrySet().iterator(); it.hasNext(); ) { 82 Map.Entry me = (Map.Entry )it.next(); 83 String key = (String )me.getKey(); 84 if (!key.endsWith("mpt")) { 85 int mpt = Integer.parseInt(key) * 1000; 86 if (Math.abs(size - mpt) < distance) { 87 distance = Math.abs(size - mpt); 88 pointsize = (String )me.getKey(); 89 csm = (CharacterSet)me.getValue(); 90 } 91 } 92 } 93 if (csm != null) { 94 _characterSets.put(size + "mpt", csm); 95 String msg = "No " + (size / 1000) + "pt font " + _name 96 + " found, substituted with " + pointsize + "pt font"; 97 log.warn(msg); 98 } 99 } 100 if (csm == null) { 101 String msg = "No font found for font " + _name 102 + " with point size " + pointsize; 103 log.error(msg); 104 throw new FontRuntimeException(msg); 105 } 106 return csm; 107 108 } 109 110 113 public int getFirstChar() { 114 115 Iterator i = _characterSets.values().iterator(); 116 if (i.hasNext()) { 117 CharacterSet csm = (CharacterSet) i.next(); 118 return csm.getFirstChar(); 119 } else { 120 String msg = "getFirstChar() - No character set found for font:" + _name; 121 log.error(msg); 122 throw new FontRuntimeException(msg); 123 } 124 125 } 126 127 130 public int getLastChar() { 131 132 Iterator i = _characterSets.values().iterator(); 133 if (i.hasNext()) { 134 CharacterSet csm = (CharacterSet) i.next(); 135 return csm.getLastChar(); 136 } else { 137 String msg = "getLastChar() - No character set found for font:" + _name; 138 log.error(msg); 139 throw new FontRuntimeException(msg); 140 } 141 142 } 143 144 151 public int getAscender(int size) { 152 153 return getCharacterSet(size).getAscender(); 154 155 } 156 157 162 public int getCapHeight(int size) { 163 164 return getCharacterSet(size).getCapHeight(); 165 166 } 167 168 175 public int getDescender(int size) { 176 177 return getCharacterSet(size).getDescender(); 178 179 } 180 181 186 public int getXHeight(int size) { 187 188 return getCharacterSet(size).getXHeight(); 189 190 } 191 192 195 public int getWidth(int character, int size) { 196 197 return getCharacterSet(size).width(character); 198 199 } 200 201 209 public int[] getWidths(int size) { 210 211 return getCharacterSet(size).getWidths(); 212 213 } 214 215 221 public int[] getWidths() { 222 223 return getWidths(1000); 224 225 } 226 227 232 public char mapChar(char c) { 233 return _characterSet.mapChar(c); 234 } 235 236 240 public String getEncoding() { 241 return _characterSet.getEncoding(); 242 } 243 244 } | Popular Tags |