Debian Codegraph @

fop-core/src/main/codegen/fonts/t1font-file.xsl

raw
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 <!-- $Id$ -->
18 <!--
19 This files writes the class files for the Adobe Type 1 fonts.
20 It uses the information in the font description files (Courier.xml, Helvetica.xml) to this
21 In these font description files each character is referenced by its adobe name:
22 <char name="A" width="667"/>
23 To resolve this name and to find the code for this character it looks up the adobe name in the
24 file charlist.xml and extracts the WinAnsi code.
25 -->
26
27 <xsl:stylesheet version="1.0"
28 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
29 xmlns:lxslt="http://xml.apache.org/xslt"
30 xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"
31 extension-element-prefixes="redirect">
32 <xsl:output method="text" />
33
34 <xsl:template match="font-metrics">
35 <xsl:variable name="class-name" select="class-name"/>
36 <!--<redirect:write select="concat('org/apache/fop/render/pdf/fonts/', $class-name, '.java')">-->
37 package org.apache.fop.render.pdf.fonts;
38
39 import org.apache.fop.render.pdf.Font;
40 import org.apache.fop.layout.FontDescriptor;
41 import org.apache.fop.fonts.Glyphs;
42 import org.apache.fop.pdf.PDFStream;
43 import org.apache.fop.pdf.PDFT1Stream;
44 import java.io.InputStream;
45 import java.io.FileInputStream;
46 import java.io.BufferedInputStream;
47 import java.util.HashMap;
48
49 public class <xsl:value-of select="class-name"/> extends Font implements FontDescriptor {
50 private final static String fontName = "<xsl:value-of select="font-name"/>";
51 private final static String encoding = "<xsl:value-of select="encoding"/>";
52 private final static int capHeight = <xsl:value-of select="cap-height"/>;
53 private final static int xHeight = <xsl:value-of select="x-height"/>;
54 private final static int ascender = <xsl:value-of select="ascender"/>;
55 private final static int descender = <xsl:value-of select="descender"/>;
56 private final static int[] fontBBox = {
57 <xsl:value-of select="bbox/left"/>,
58 <xsl:value-of select="bbox/bottom"/>,
59 <xsl:value-of select="bbox/right"/>,
60 <xsl:value-of select="bbox/top"/>
61 };
62 private final static String embedFileName = <xsl:value-of select="embedFile"/>;
63 private final static String embedResourceName = <xsl:value-of select="embedResource"/>;
64 private static PDFT1Stream embeddedFont=null;
65 private final static int flags = <xsl:value-of select="flags"/>;
66 private final static int stemV = <xsl:value-of select="stemv"/>;
67 private final static int italicAngle = <xsl:value-of select="italicangle"/>;
68 private final static int firstChar = <xsl:value-of select="first-char"/>;
69 private final static int lastChar = <xsl:value-of select="last-char"/>;
70 private final static int[] width;
71 private final static HashMap kerning=new HashMap();
72
73 static {
74 width = new int[256];
75 <xsl:for-each select="widths/char"><xsl:variable name="char-name" select="@name"/><xsl:variable name="char-num" select="document('charlist.xml')/font-mappings/map[@adobe-name=$char-name]/@win-ansi"/><xsl:if test="$char-num!='-1'"> width[<xsl:value-of select="$char-num"/>] = <xsl:value-of select="@width"/>;
76 </xsl:if></xsl:for-each>
77
78 HashMap tmptable;
79 <xsl:for-each select="kerning">
80 <xsl:variable name="kpx1-name" select="@kpx1"/>
81 tmptable=new HashMap();<xsl:for-each select="pair"><xsl:variable name="kpx2-name" select="@kpx2"/><xsl:variable name="kern-name" select="@kern"/>
82 tmptable.put(Glyphs.glyphToString("<xsl:value-of select="$kpx2-name"/>"), new Integer(<xsl:value-of select="$kern-name"/>));</xsl:for-each>
83 kerning.put(Glyphs.glyphToString("<xsl:value-of select="$kpx1-name"/>"), tmptable);
84 </xsl:for-each>
85 }
86
87 public final boolean hasKerningInfo() {return kerning.isEmpty();}
88 public final java.util.HashMap getKerningInfo() {return kerning;}
89 public byte getSubType() {return org.apache.fop.pdf.PDFFont.TYPE1;}
90
91 public boolean isEmbeddable() {
92 return (embedFileName==null &amp;&amp; embedResourceName==null) ? false : true;
93 }
94
95 public PDFStream getFontFile(int i) {
96 InputStream instream=null;
97
98 // Get file first
99 if (embedFileName!=null)
100 try {
101 instream=new FileInputStream(embedFileName);
102 } catch (Exception e) {
103 System.out.println("Failed to embed fontfile: "+embedFileName);
104 }
105
106 // Get resource
107 if (instream==null &amp;&amp; embedResourceName!=null)
108 try {
109 instream=new BufferedInputStream(this.getClass().getResourceAsStream(embedResourceName));
110 } catch (Exception e) {
111 System.out.println("Failed to embed fontresource: "+embedResourceName);
112 }
113
114 if (instream==null)
115 return (PDFStream)null;
116
117 // Read fontdata
118 byte[] file = new byte[128000];
119 int fsize = 0;
120
121 try {
122 int l = instream.read(file, 0, 128000);
123 fsize += l;
124
125 if (l==128000) {
126 // More to read - needs to extend
127 byte[] tmpbuf;
128
129 while (l &gt; 0) {
130 tmpbuf = new byte[file.length + 64000];
131 System.arraycopy(file, 0, tmpbuf, 0, file.length);
132 l=instream.read(tmpbuf, file.length, 64000);
133 fsize += l;
134 file = tmpbuf;
135
136 if (l &lt; 64000) // whole file read. No need to loop again
137 l=0;
138 }
139 }
140
141 embeddedFont=new PDFT1Stream(i, fsize);
142 embeddedFont.addFilter("flate");
143 embeddedFont.addFilter("ascii-85");
144 embeddedFont.setData(file, fsize);
145 instream.close();
146 } catch (Exception e) {}
147
148 return (PDFStream) embeddedFont;
149 }
150
151 public String encoding() {
152 return encoding;
153 }
154
155 public String fontName() {
156 return fontName;
157 }
158
159 public int getAscender() {
160 return ascender;
161 }
162
163 public int getCapHeight() {
164 return capHeight;
165 }
166
167 public int getDescender() {
168 return descender;
169 }
170
171 public int getAscender(int size) {
172 return size * ascender;
173 }
174
175 public int getCapHeight(int size) {
176 return size * capHeight;
177 }
178
179 public int getDescender(int size) {
180 return size * descender;
181 }
182
183 public int getXHeight(int size) {
184 return size * xHeight;
185 }
186
187 public int getFlags() {
188 return flags;
189 }
190
191 public int[] getFontBBox() {
192 return fontBBox;
193 }
194
195 public int getItalicAngle() {
196 return italicAngle;
197 }
198
199 public int getStemV() {
200 return stemV;
201 }
202
203 public int getFirstChar() {
204 return firstChar;
205 }
206
207 public int getLastChar() {
208 return lastChar;
209 }
210
211 public int width(int i, int size) {
212 return size * width[i];
213 }
214
215 public int[] getWidths(int size) {
216 int[] arr = new int[getLastChar()-getFirstChar()+1];
217 System.arraycopy(width, getFirstChar(), arr, 0, getLastChar()-getFirstChar()+1);
218 for( int i = 0; i &lt; arr.length; i++) arr[i] *= size;
219 return arr;
220 }
221 }
222 <!--</redirect:write>-->
223 </xsl:template>
224 </xsl:stylesheet>
225