KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fonts > CustomFont


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: CustomFont.java 474387 2006-11-13 16:28:13Z jeremias $ */
19
20 package org.apache.fop.fonts;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Map JavaDoc;
24 import javax.xml.transform.Source JavaDoc;
25
26 /**
27  * Abstract base class for custom fonts loaded from files, for example.
28  */

29 public abstract class CustomFont extends Typeface
30             implements FontDescriptor, MutableFont {
31
32     private String JavaDoc fontName = null;
33     private String JavaDoc embedFileName = null;
34     protected String JavaDoc embedResourceName = null;
35     private FontResolver resolver = null;
36     
37     private int capHeight = 0;
38     private int xHeight = 0;
39     private int ascender = 0;
40     private int descender = 0;
41     private int[] fontBBox = {0, 0, 0, 0};
42     private int flags = 4;
43     private int stemV = 0;
44     private int italicAngle = 0;
45     private int missingWidth = 0;
46     private FontType fontType = FontType.TYPE1;
47     private int firstChar = 0;
48     private int lastChar = 255;
49
50     private Map JavaDoc kerning;
51
52     private boolean useKerning = true;
53
54
55     /**
56      * @see org.apache.fop.fonts.FontMetrics#getFontName()
57      */

58     public String JavaDoc getFontName() {
59         return fontName;
60     }
61
62     /**
63      * Returns an URI representing an embeddable font file. The URI will often
64      * be a filename or an URL.
65      * @return URI to an embeddable font file or null if not available.
66      */

67     public String JavaDoc getEmbedFileName() {
68         return embedFileName;
69     }
70
71     /**
72      * Returns a Source representing an embeddable font file.
73      * @return Source for an embeddable font file
74      * @throws IOException if embedFileName is not null but Source is not found
75      */

76     public Source JavaDoc getEmbedFileSource() throws IOException JavaDoc {
77         Source JavaDoc result = null;
78         if (resolver != null && embedFileName != null) {
79             result = resolver.resolve(embedFileName);
80             if(result == null) throw new IOException JavaDoc("Unable to resolve Source '" + embedFileName + "' for embedded font");
81         }
82         return result;
83     }
84
85     /**
86      * Returns the lookup name to an embeddable font file available as a
87      * resource.
88      * (todo) Remove this method, this should be done using a resource: URI.
89      * @return the lookup name
90      */

91     public String JavaDoc getEmbedResourceName() {
92         return embedResourceName;
93     }
94
95     /**
96      * @see org.apache.fop.fonts.FontDescriptor#getAscender()
97      */

98     public int getAscender() {
99         return ascender;
100     }
101
102     /**
103      * @see org.apache.fop.fonts.FontDescriptor#getDescender()
104      */

105     public int getDescender() {
106         return descender;
107     }
108
109     /**
110      * @see org.apache.fop.fonts.FontDescriptor#getCapHeight()
111      */

112     public int getCapHeight() {
113         return capHeight;
114     }
115
116     /**
117      * @see org.apache.fop.fonts.FontMetrics#getAscender(int)
118      */

119     public int getAscender(int size) {
120         return size * ascender;
121     }
122
123     /**
124      * @see org.apache.fop.fonts.FontMetrics#getDescender(int)
125      */

126     public int getDescender(int size) {
127         return size * descender;
128     }
129
130     /**
131      * @see org.apache.fop.fonts.FontMetrics#getCapHeight(int)
132      */

133     public int getCapHeight(int size) {
134         return size * capHeight;
135     }
136
137     /**
138      * @see org.apache.fop.fonts.FontMetrics#getXHeight(int)
139      */

140     public int getXHeight(int size) {
141         return size * xHeight;
142     }
143
144     /**
145      * @see org.apache.fop.fonts.FontDescriptor#getFontBBox()
146      */

147     public int[] getFontBBox() {
148         return fontBBox;
149     }
150
151     /**
152      * @see org.apache.fop.fonts.FontDescriptor#getFlags()
153      */

154     public int getFlags() {
155         return flags;
156     }
157
158     /**
159      * @see org.apache.fop.fonts.FontDescriptor#getStemV()
160      */

161     public int getStemV() {
162         return stemV;
163     }
164
165     /**
166      * @see org.apache.fop.fonts.FontDescriptor#getItalicAngle()
167      */

168     public int getItalicAngle() {
169         return italicAngle;
170     }
171
172     /**
173      * Returns the width to be used when no width is available.
174      * @return a character width
175      */

176     public int getMissingWidth() {
177         return missingWidth;
178     }
179
180     /**
181      * @see org.apache.fop.fonts.FontDescriptor#getFontType()
182      */

183     public FontType getFontType() {
184         return fontType;
185     }
186
187     /**
188      * Returns the index of the first character defined in this font.
189      * @return the index of the first character
190      */

191     public int getFirstChar() {
192         return 0;
193         // return firstChar;
194
/**(todo) Why is this hardcoded??? This code was in SingleByteFont.java */
195     }
196
197     /**
198      * Returns the index of the last character defined in this font.
199      * @return the index of the last character
200      */

201     public int getLastChar() {
202         return lastChar;
203     }
204
205     /**
206      * Used to determine if kerning is enabled.
207      * @return True if kerning is enabled.
208      */

209     public boolean isKerningEnabled() {
210         return useKerning;
211     }
212
213     /**
214      * @see org.apache.fop.fonts.FontMetrics#hasKerningInfo()
215      */

216     public final boolean hasKerningInfo() {
217         return (isKerningEnabled() && (kerning != null) && !kerning.isEmpty());
218     }
219
220     /**
221      * @see org.apache.fop.fonts.FontMetrics#getKerningInfo()
222      */

223     public final Map JavaDoc getKerningInfo() {
224         if (hasKerningInfo()) {
225             return kerning;
226         } else {
227             return java.util.Collections.EMPTY_MAP;
228         }
229     }
230
231
232     /* ---- MutableFont interface ---- */
233
234     /**
235      * @see org.apache.fop.fonts.MutableFont#setFontName(String)
236      */

237     public void setFontName(String JavaDoc name) {
238         this.fontName = name;
239     }
240
241     /**
242      * @see org.apache.fop.fonts.MutableFont#setEmbedFileName(String)
243      */

244     public void setEmbedFileName(String JavaDoc path) {
245         this.embedFileName = path;
246     }
247
248     /**
249      * @see org.apache.fop.fonts.MutableFont#setEmbedResourceName(String)
250      */

251     public void setEmbedResourceName(String JavaDoc name) {
252         this.embedResourceName = name;
253     }
254
255     /**
256      * @see org.apache.fop.fonts.MutableFont#setCapHeight(int)
257      */

258     public void setCapHeight(int capHeight) {
259         this.capHeight = capHeight;
260     }
261
262     /**
263      * Returns the XHeight value of the font.
264      * @param xHeight the XHeight value
265      */

266     public void setXHeight(int xHeight) {
267         this.xHeight = xHeight;
268     }
269
270     /**
271      * @see org.apache.fop.fonts.MutableFont#setAscender(int)
272      */

273     public void setAscender(int ascender) {
274         this.ascender = ascender;
275     }
276
277     /**
278      * @see org.apache.fop.fonts.MutableFont#setDescender(int)
279      */

280     public void setDescender(int descender) {
281         this.descender = descender;
282     }
283
284     /**
285      * @see org.apache.fop.fonts.MutableFont#setFontBBox(int[])
286      */

287     public void setFontBBox(int[] bbox) {
288         this.fontBBox = bbox;
289     }
290
291     /**
292      * @see org.apache.fop.fonts.MutableFont#setFlags(int)
293      */

294     public void setFlags(int flags) {
295         this.flags = flags;
296     }
297
298     /**
299      * @see org.apache.fop.fonts.MutableFont#setStemV(int)
300      */

301     public void setStemV(int stemV) {
302         this.stemV = stemV;
303     }
304
305     /**
306      * @see org.apache.fop.fonts.MutableFont#setItalicAngle(int)
307      */

308     public void setItalicAngle(int italicAngle) {
309         this.italicAngle = italicAngle;
310     }
311
312     /**
313      * @see org.apache.fop.fonts.MutableFont#setMissingWidth(int)
314      */

315     public void setMissingWidth(int width) {
316         this.missingWidth = width;
317     }
318
319     /**
320      * @see org.apache.fop.fonts.MutableFont#setFontType(FontType)
321      */

322     public void setFontType(FontType fontType) {
323         this.fontType = fontType;
324     }
325
326     /**
327      * @see org.apache.fop.fonts.MutableFont#setFirstChar(int)
328      */

329     public void setFirstChar(int index) {
330         this.firstChar = index;
331     }
332
333     /**
334      * @see org.apache.fop.fonts.MutableFont#setLastChar(int)
335      */

336     public void setLastChar(int index) {
337         this.lastChar = index;
338     }
339
340     /**
341      * @see org.apache.fop.fonts.MutableFont#setKerningEnabled(boolean)
342      */

343     public void setKerningEnabled(boolean enabled) {
344         this.useKerning = enabled;
345     }
346
347     /**
348      * Sets the font resolver. Needed for URI resolution.
349      * @param resolver the font resolver
350      */

351     public void setResolver(FontResolver resolver) {
352         this.resolver = resolver;
353     }
354
355     /**
356      * @see org.apache.fop.fonts.MutableFont#putKerningEntry(Integer, Map)
357      */

358     public void putKerningEntry(Integer JavaDoc key, Map JavaDoc value) {
359         if (kerning == null) {
360             kerning = new java.util.HashMap JavaDoc();
361         }
362         this.kerning.put(key, value);
363     }
364
365 }
366
Popular Tags