KickJava   Java API By Example, From Geeks To Geeks.

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


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: MultiByteFont.java 474387 2006-11-13 16:28:13Z jeremias $ */
19
20 package org.apache.fop.fonts;
21
22 //Java
23
import java.util.Map JavaDoc;
24
25 /**
26  * Generic MultiByte (CID) font
27  */

28 public class MultiByteFont extends CIDFont {
29
30     private static int uniqueCounter = 1;
31
32
33     private String JavaDoc ttcName = null;
34     private String JavaDoc encoding = "Identity-H";
35
36     private int defaultWidth = 0;
37     private CIDFontType cidType = CIDFontType.CIDTYPE2;
38
39     private String JavaDoc namePrefix = null; // Quasi unique prefix
40

41     private BFEntry[] bfentries = null;
42
43     /**
44      * Default constructor
45      */

46     public MultiByteFont() {
47         // Make sure that the 3 first glyphs are included
48
usedGlyphs.put(new Integer JavaDoc(0), new Integer JavaDoc(0));
49         usedGlyphsIndex.put(new Integer JavaDoc(0), new Integer JavaDoc(0));
50         usedGlyphsCount++;
51         usedGlyphs.put(new Integer JavaDoc(1), new Integer JavaDoc(1));
52         usedGlyphsIndex.put(new Integer JavaDoc(1), new Integer JavaDoc(1));
53         usedGlyphsCount++;
54         usedGlyphs.put(new Integer JavaDoc(2), new Integer JavaDoc(2));
55         usedGlyphsIndex.put(new Integer JavaDoc(2), new Integer JavaDoc(2));
56         usedGlyphsCount++;
57
58         // Create a quasiunique prefix for fontname
59
int cnt = 0;
60         synchronized (this.getClass()) {
61             cnt = uniqueCounter++;
62         }
63         int ctm = (int)(System.currentTimeMillis() & 0xffff);
64         namePrefix = new String JavaDoc(cnt + "E" + Integer.toHexString(ctm));
65
66         setFontType(FontType.TYPE0);
67     }
68
69     /**
70      * @see org.apache.fop.fonts.CIDFont#getDefaultWidth()
71      */

72     public int getDefaultWidth() {
73         return defaultWidth;
74     }
75
76     /**
77      * @see org.apache.fop.fonts.CIDFont#getRegistry()
78      */

79     public String JavaDoc getRegistry() {
80         return "Adobe";
81     }
82
83     /**
84      * @see org.apache.fop.fonts.CIDFont#getOrdering()
85      */

86     public String JavaDoc getOrdering() {
87         return "UCS";
88     }
89
90     /**
91      * @see org.apache.fop.fonts.CIDFont#getSupplement()
92      */

93     public int getSupplement() {
94         return 0;
95     }
96
97     /**
98      * @see org.apache.fop.fonts.CIDFont#getCIDType()
99      */

100     public CIDFontType getCIDType() {
101         return cidType;
102     }
103
104     /**
105      * Sets the CIDType.
106      * @param cidType The cidType to set
107      */

108     public void setCIDType(CIDFontType cidType) {
109         this.cidType = cidType;
110     }
111
112     /**
113      * @see org.apache.fop.fonts.CIDFont#getCidBaseFont()
114      */

115     public String JavaDoc getCidBaseFont() {
116         if (isEmbeddable()) {
117             return namePrefix + super.getFontName();
118         } else {
119             return super.getFontName();
120         }
121     }
122
123     /**
124      * @see org.apache.fop.fonts.FontDescriptor#isEmbeddable()
125      */

126     public boolean isEmbeddable() {
127         return !(getEmbedFileName() == null && embedResourceName == null);
128     }
129
130     /**
131      * @see org.apache.fop.fonts.Typeface#getEncoding()
132      */

133     public String JavaDoc getEncoding() {
134         return encoding;
135     }
136
137     /**
138      * @see org.apache.fop.fonts.FontMetrics#getFontName()
139      */

140     public String JavaDoc getFontName() {
141         if (isEmbeddable()) {
142             return namePrefix + super.getFontName();
143         } else {
144             return super.getFontName();
145         }
146     }
147
148     /**
149      * @see org.apache.fop.fonts.FontMetrics#getWidth(int, int)
150      */

151     public int getWidth(int i, int size) {
152         if (isEmbeddable()) {
153             Integer JavaDoc idx = (Integer JavaDoc)usedGlyphsIndex.get(new Integer JavaDoc(i));
154             return size * width[idx.intValue()];
155         } else {
156             return size * width[i];
157         }
158     }
159
160     /**
161      * @see org.apache.fop.fonts.FontMetrics#getWidths()
162      */

163     public int[] getWidths() {
164         int[] arr = new int[width.length];
165         System.arraycopy(width, 0, arr, 0, width.length - 1);
166         /*
167         for (int i = 0; i < arr.length; i++)
168             arr[i] *= size;
169         */

170         return arr;
171     }
172
173     /**
174      * Remaps a codepoint based.
175      * @param i codepoint to remap
176      * @return new codepoint
177      */

178 /* unused
179     public Integer reMap(Integer i) {
180         if (isEmbeddable()) {
181             Integer ret = (Integer)usedGlyphsIndex.get(i);
182             if (ret == null) {
183                 ret = i;
184             }
185             return ret;
186         } else {
187             return i;
188         }
189
190     }
191 */

192
193     private int findGlyphIndex(char c) {
194         int idx = (int)c;
195         int retIdx = 0;
196
197         for (int i = 0; (i < bfentries.length) && retIdx == 0; i++) {
198             if (bfentries[i].getUnicodeStart() <= idx
199                     && bfentries[i].getUnicodeEnd() >= idx) {
200
201                 retIdx = bfentries[i].getGlyphStartIndex()
202                     + idx
203                     - bfentries[i].getUnicodeStart();
204             }
205         }
206         return retIdx;
207     }
208
209     /**
210      * @see org.apache.fop.fonts.Typeface#mapChar(char)
211      */

212     public char mapChar(char c) {
213         int retIdx = findGlyphIndex(c);
214
215         if (isEmbeddable()) {
216             // Reencode to a new subset font or get
217
// the reencoded value
218
Integer JavaDoc newIdx = (Integer JavaDoc)usedGlyphs.get(new Integer JavaDoc(retIdx));
219             if (newIdx == null) {
220                 usedGlyphs.put(new Integer JavaDoc(retIdx),
221                                new Integer JavaDoc(usedGlyphsCount));
222                 usedGlyphsIndex.put(new Integer JavaDoc(usedGlyphsCount),
223                                     new Integer JavaDoc(retIdx));
224                 usedCharsIndex.put(new Integer JavaDoc(usedGlyphsCount),
225                                     new Integer JavaDoc((int) c));
226                 retIdx = usedGlyphsCount;
227                 usedGlyphsCount++;
228             } else {
229                 retIdx = newIdx.intValue();
230             }
231         }
232
233         return (char)retIdx;
234     }
235
236     /**
237      * @see org.apache.fop.fonts.Typeface#hasChar(char)
238      */

239     public boolean hasChar(char c) {
240         return (findGlyphIndex(c) > 0);
241     }
242
243
244     /**
245      * Sets the bfentries.
246      * @param bfentries The bfentries to set
247      */

248     public void setBFEntries(BFEntry[] bfentries) {
249         this.bfentries = bfentries;
250     }
251
252     /**
253      * Sets the defaultWidth.
254      * @param defaultWidth The defaultWidth to set
255      */

256     public void setDefaultWidth(int defaultWidth) {
257         this.defaultWidth = defaultWidth;
258     }
259
260     /**
261      * Returns the TrueType Collection Name.
262      * @return the TrueType Collection Name
263      */

264     public String JavaDoc getTTCName() {
265         return ttcName;
266     }
267
268     /**
269      * Sets the the TrueType Collection Name.
270      * @param ttcName the TrueType Collection Name
271      */

272     public void setTTCName(String JavaDoc ttcName) {
273         this.ttcName = ttcName;
274     }
275
276     /**
277      * Adds a new CID width entry to the font.
278      * @param cidWidthIndex index
279      * @param wds array of widths
280      */

281     /*
282     public void addCIDWidthEntry(int cidWidthIndex, int[] wds) {
283         this.warray.addEntry(cidWidthIndex, wds);
284     }*/

285
286
287     /**
288      * Sets the width array.
289      * @param wds array of widths.
290      */

291     public void setWidthArray(int[] wds) {
292         this.width = wds;
293     }
294
295     /**
296      * Returns a Map of used Glyphs.
297      * @return Map Map of used Glyphs
298      */

299     public Map JavaDoc getUsedGlyphs() {
300         return usedGlyphs;
301     }
302
303     /** The invalid Unicode character, suitable as a return value in methods
304      * that need to return an invalid character. */

305     public static final char INVALID_UNICODE_CHAR = 0xFFFF;
306
307     /** @see org.apache.fop.fonts.CIDFont#getCharsUsed() */
308     public char[] getCharsUsed() {
309         if (!isEmbeddable()) {
310             return null;
311         }
312         char[] charArray = new char[usedGlyphsCount];
313         for (int i = 0; i < usedGlyphsCount; i++) {
314             Integer JavaDoc mapValue = (Integer JavaDoc)usedCharsIndex.get(new Integer JavaDoc(i));
315             if (mapValue != null) {
316                 char arrayItem = (char) mapValue.intValue();
317                 charArray[i] = arrayItem;
318             }
319             else {
320                 charArray[i] = INVALID_UNICODE_CHAR;
321             }
322         }
323         return charArray;
324     }
325 }
326
327
Popular Tags