KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fonts > truetype > TTFFontLoader


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$ */
19
20 package org.apache.fop.fonts.truetype;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.fop.fonts.BFEntry;
29 import org.apache.fop.fonts.CIDFontType;
30 import org.apache.fop.fonts.CustomFont;
31 import org.apache.fop.fonts.FontLoader;
32 import org.apache.fop.fonts.FontResolver;
33 import org.apache.fop.fonts.MultiByteFont;
34
35 /**
36  * Loads a font into memory directly from the original font file.
37  */

38 public class TTFFontLoader extends FontLoader {
39
40     private String JavaDoc fontFileURI;
41     private TTFFile ttf;
42     private MultiByteFont multiFont;
43     private CustomFont returnFont;
44     private FontResolver resolver;
45     
46     public TTFFontLoader(String JavaDoc fontFileURI, InputStream JavaDoc in, FontResolver resolver)
47                 throws IOException JavaDoc {
48         this.fontFileURI = fontFileURI;
49         this.resolver = resolver;
50
51         this.ttf = new TTFFile();
52         FontFileReader reader = new FontFileReader(in);
53         boolean supported = ttf.readFont(reader, null);
54         if (!supported) {
55             throw new IOException JavaDoc("Could not load TrueType font: " + fontFileURI);
56         }
57         multiFont = new MultiByteFont();
58         multiFont.setResolver(this.resolver);
59         returnFont = multiFont;
60         read();
61     }
62     
63     private void read() throws IOException JavaDoc {
64         returnFont.setFontName(ttf.getFamilyName());
65         //multiFont.setTTCName(ttcName)
66
returnFont.setCapHeight(ttf.getCapHeight());
67         returnFont.setXHeight(ttf.getXHeight());
68         returnFont.setAscender(ttf.getLowerCaseAscent());
69         returnFont.setDescender(ttf.getLowerCaseDescent());
70         returnFont.setFontBBox(ttf.getFontBBox());
71         //returnFont.setFirstChar(ttf.getFirstChar();)
72
returnFont.setFlags(ttf.getFlags());
73         returnFont.setStemV(Integer.parseInt(ttf.getStemV())); //not used for TTF
74
returnFont.setItalicAngle(Integer.parseInt(ttf.getItalicAngle()));
75         returnFont.setMissingWidth(0);
76         multiFont.setCIDType(CIDFontType.CIDTYPE2);
77         int[] wx = ttf.getWidths();
78         multiFont.setWidthArray(wx);
79         List JavaDoc entries = ttf.getCMaps();
80         BFEntry[] bfentries = new BFEntry[entries.size()];
81         int pos = 0;
82         Iterator JavaDoc iter = ttf.getCMaps().listIterator();
83         while (iter.hasNext()) {
84             TTFCmapEntry ce = (TTFCmapEntry)iter.next();
85             bfentries[pos] = new BFEntry(ce.getUnicodeStart(), ce.getUnicodeEnd(),
86                     ce.getGlyphStartIndex());
87             pos++;
88         }
89         multiFont.setBFEntries(bfentries);
90         copyKerning(ttf, true);
91         multiFont.setEmbedFileName(this.fontFileURI);
92         
93     }
94     
95     private void copyKerning(TTFFile ttf, boolean isCid) {
96         
97         // Get kerning
98
Iterator JavaDoc iter;
99         if (isCid) {
100             iter = ttf.getKerning().keySet().iterator();
101         } else {
102             iter = ttf.getAnsiKerning().keySet().iterator();
103         }
104
105         while (iter.hasNext()) {
106             Integer JavaDoc kpx1 = (Integer JavaDoc)iter.next();
107
108             Map JavaDoc h2;
109             if (isCid) {
110                 h2 = (Map JavaDoc)ttf.getKerning().get(kpx1);
111             } else {
112                 h2 = (Map JavaDoc)ttf.getAnsiKerning().get(kpx1);
113             }
114
115             returnFont.putKerningEntry(kpx1, h2);
116         }
117     }
118     
119     
120     /** @see org.apache.fop.fonts.FontLoader#getFont() */
121     public CustomFont getFont() {
122         return this.returnFont;
123     }
124     
125 }
126
Popular Tags