KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > afp > modca > MapCodedFont


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: MapCodedFont.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.modca;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.util.ArrayList JavaDoc;
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 /**
36  * The Map Coded Font structured field maps a unique coded font resource local
37  * ID, which may be embedded one or more times within an object's data and
38  * descriptor, to the identifier of a coded font resource object. Additionally,
39  * the Map Coded Font structured field specifies a set of resource attributes
40  * for the coded font.
41  *
42  * @author <a HREF="mailto:pete@townsend.uk.com">Pete Townsend </a>
43  */

44 public class MapCodedFont extends AbstractAFPObject {
45
46     /**
47      * The collection of map coded fonts (maximum of 254)
48      */

49     private ArrayList JavaDoc _fontlist = null;
50
51     /**
52      * Constructor for the MapCodedFont
53      */

54     public MapCodedFont() {
55
56         _fontlist = new ArrayList JavaDoc();
57
58     }
59
60     /**
61      * Accessor method to write the AFP datastream for the Map Coded Font
62      * @param os The stream to write to
63      * @throws java.io.IOException
64      */

65     public void writeDataStream(OutputStream JavaDoc os)
66         throws IOException JavaDoc {
67
68
69         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
70         baos.write(0x5A);
71         baos.write(new byte[] { 0x00, 0x00 });
72
73         // Field identifier for a MapCodedFont
74
baos.write(new byte[] { (byte) 0xD3, (byte) 0xAB, (byte) 0x8A });
75
76         // Reserved
77
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             // Start of repeating groups (occurs 1 to 254)
84
baos.write(0x00);
85
86             if (fd._scale == 0) {
87                 // Raster Font
88
baos.write(0x22); // Length of 34
89
} else {
90                 // Outline Font
91
baos.write(0x3A); // Length of 58
92
}
93
94             // Font Character Set Name Reference
95
baos.write(0x0C);
96             baos.write(0x02);
97             baos.write((byte) 0x86);
98             baos.write(0x00);
99             baos.write(fd._characterset);
100
101             // Font Code Page Name Reference
102
baos.write(0x0C);
103             baos.write(0x02);
104             baos.write((byte) 0x85);
105             baos.write(0x00);
106             baos.write(fd._codepage);
107
108             // Character Rotation
109
baos.write(0x04);
110             baos.write(0x26);
111             baos.write(fd._orientation);
112             baos.write(0x00);
113
114             // Resource Local Identifier
115
baos.write(0x04);
116             baos.write(0x24);
117             baos.write(0x05);
118             baos.write(fd._fontReferenceKey);
119
120             if (fd._scale != 0) {
121                 // Outline Font (triplet '1F')
122
baos.write(0x14);
123                 baos.write(0x1F);
124                 baos.write(0x00);
125                 baos.write(0x00);
126
127                 baos.write(BinaryUtils.convert(fd._scale, 2)); // Height
128
baos.write(new byte[] { 0x00, 0x00 }); // Width
129

130                 baos.write(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131                     0x00, 0x00, 0x00, 0x00, 0x00 });
132
133                 baos.write(0x60);
134
135                 // Outline Font (triplet '5D')
136
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         // Set the total record length
146
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     /**
155      * Add a font definition on the the map coded font object.
156      *
157      * @param fontReference
158      * the font number used as the resource identifier
159      * @param font
160      * the font
161      * @param size
162      * the size of the font
163      * @param orientation
164      * the orientation of the font
165      */

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 JavaDoc 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 JavaDoc("The character set "
205                         + new String JavaDoc(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 JavaDoc("The code page "
215                         + new String JavaDoc(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                 // There are approximately 72 points to 1 inch or 20 1440ths per point.
227

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 JavaDoc("The code page "
235                         + new String JavaDoc(fd._codepage,
236                         AFPConstants.EBCIDIC_ENCODING)
237                         + " must have a fixed length of 8 characters.");
238                 }
239
240             } else {
241                 String JavaDoc 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 an exception if the size is exceeded
250
throw new MaximumSizeExceededException();
251
252             } else {
253
254                 _fontlist.add(fd);
255
256             }
257
258         } catch (UnsupportedEncodingException JavaDoc ex) {
259
260             throw new FontRuntimeException("Failed to create font "
261                 + " due to a UnsupportedEncodingException", ex);
262
263         }
264
265     }
266
267     /**
268      * Private utility class used as a container for font attributes
269      */

270     private class FontDefinition {
271
272         /**
273          * The code page of the font
274          */

275         private byte[] _codepage;
276
277         /**
278          * The character set of the font
279          */

280         private byte[] _characterset;
281
282         /**
283          * The font reference key
284          */

285         private byte _fontReferenceKey;
286
287         /**
288          * The orientation of the font
289          */

290         private byte _orientation;
291
292         /**
293          * The scale (only specified for outline fonts)
294          */

295         private int _scale = 0;
296
297     }
298
299 }
Popular Tags