1 17 18 19 20 package org.apache.fop.render.afp.modca; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.UnsupportedEncodingException ; 26 import java.util.ArrayList ; 27 28 import org.apache.fop.render.afp.exceptions.FontRuntimeException; 29 import org.apache.fop.render.afp.fonts.AFPFont; 30 import org.apache.fop.render.afp.fonts.CharacterSet; 31 import org.apache.fop.render.afp.fonts.OutlineFont; 32 import org.apache.fop.render.afp.fonts.RasterFont; 33 import org.apache.fop.render.afp.tools.BinaryUtils; 34 35 44 public class MapCodedFont extends AbstractAFPObject { 45 46 49 private ArrayList _fontlist = null; 50 51 54 public MapCodedFont() { 55 56 _fontlist = new ArrayList (); 57 58 } 59 60 65 public void writeDataStream(OutputStream os) 66 throws IOException { 67 68 69 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 70 baos.write(0x5A); 71 baos.write(new byte[] { 0x00, 0x00 }); 72 73 baos.write(new byte[] { (byte) 0xD3, (byte) 0xAB, (byte) 0x8A }); 75 76 baos.write(new byte[] { 0x00, 0x00, 0x00 }); 78 79 for (int i = 0; i < _fontlist.size(); i++) { 80 81 FontDefinition fd = (FontDefinition) _fontlist.get(i); 82 83 baos.write(0x00); 85 86 if (fd._scale == 0) { 87 baos.write(0x22); } else { 90 baos.write(0x3A); } 93 94 baos.write(0x0C); 96 baos.write(0x02); 97 baos.write((byte) 0x86); 98 baos.write(0x00); 99 baos.write(fd._characterset); 100 101 baos.write(0x0C); 103 baos.write(0x02); 104 baos.write((byte) 0x85); 105 baos.write(0x00); 106 baos.write(fd._codepage); 107 108 baos.write(0x04); 110 baos.write(0x26); 111 baos.write(fd._orientation); 112 baos.write(0x00); 113 114 baos.write(0x04); 116 baos.write(0x24); 117 baos.write(0x05); 118 baos.write(fd._fontReferenceKey); 119 120 if (fd._scale != 0) { 121 baos.write(0x14); 123 baos.write(0x1F); 124 baos.write(0x00); 125 baos.write(0x00); 126 127 baos.write(BinaryUtils.convert(fd._scale, 2)); baos.write(new byte[] { 0x00, 0x00 }); 130 baos.write(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 131 0x00, 0x00, 0x00, 0x00, 0x00 }); 132 133 baos.write(0x60); 134 135 baos.write(0x04); 137 baos.write(0x5D); 138 baos.write(BinaryUtils.convert(fd._scale, 2)); 139 } 140 141 } 142 143 byte[] data = baos.toByteArray(); 144 145 byte[] rl1 = BinaryUtils.convert(data.length - 1, 2); 147 data[1] = rl1[0]; 148 data[2] = rl1[1]; 149 150 os.write(data); 151 152 } 153 154 166 public void addFont(byte fontReference, AFPFont font, int size, int orientation) 167 throws MaximumSizeExceededException { 168 169 FontDefinition fd = new FontDefinition(); 170 171 fd._fontReferenceKey = fontReference; 172 173 switch (orientation) { 174 case 90: 175 fd._orientation = 0x2D; 176 break; 177 case 180: 178 fd._orientation = 0x5A; 179 break; 180 case 270: 181 fd._orientation = (byte) 0x87; 182 break; 183 default: 184 fd._orientation = 0x00; 185 break; 186 } 187 188 try { 189 190 if (font instanceof RasterFont) { 191 192 RasterFont raster = (RasterFont) font; 193 CharacterSet cs = raster.getCharacterSet(size); 194 if (cs == null) { 195 String msg = "Character set not found for font " 196 + font.getFontName() + " with point size " + size; 197 log.error(msg); 198 throw new FontRuntimeException(msg); 199 } 200 201 fd._characterset = cs.getNameBytes(); 202 203 if (fd._characterset.length != 8) { 204 throw new IllegalArgumentException ("The character set " 205 + new String (fd._characterset, 206 AFPConstants.EBCIDIC_ENCODING) 207 + " must have a fixed length of 8 characters."); 208 } 209 210 fd._codepage = cs.getCodePage().getBytes( 211 AFPConstants.EBCIDIC_ENCODING); 212 213 if (fd._codepage.length != 8) { 214 throw new IllegalArgumentException ("The code page " 215 + new String (fd._codepage, 216 AFPConstants.EBCIDIC_ENCODING) 217 + " must have a fixed length of 8 characters."); 218 } 219 220 } else if (font instanceof OutlineFont) { 221 222 OutlineFont outline = (OutlineFont) font; 223 CharacterSet cs = outline.getCharacterSet(); 224 fd._characterset = cs.getNameBytes(); 225 226 228 fd._scale = ((size / 1000) * 20); 229 230 fd._codepage = cs.getCodePage().getBytes( 231 AFPConstants.EBCIDIC_ENCODING); 232 233 if (fd._codepage.length != 8) { 234 throw new IllegalArgumentException ("The code page " 235 + new String (fd._codepage, 236 AFPConstants.EBCIDIC_ENCODING) 237 + " must have a fixed length of 8 characters."); 238 } 239 240 } else { 241 String msg = "Font of type " + font.getClass().getName() 242 + " not recognized."; 243 log.error(msg); 244 throw new FontRuntimeException(msg); 245 } 246 247 if (_fontlist.size() > 253) { 248 249 throw new MaximumSizeExceededException(); 251 252 } else { 253 254 _fontlist.add(fd); 255 256 } 257 258 } catch (UnsupportedEncodingException ex) { 259 260 throw new FontRuntimeException("Failed to create font " 261 + " due to a UnsupportedEncodingException", ex); 262 263 } 264 265 } 266 267 270 private class FontDefinition { 271 272 275 private byte[] _codepage; 276 277 280 private byte[] _characterset; 281 282 285 private byte _fontReferenceKey; 286 287 290 private byte _orientation; 291 292 295 private int _scale = 0; 296 297 } 298 299 } | Popular Tags |