Debian Codegraph @

fop-core/src/main/codegen/fonts/ttffontfile.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.PDFTTFStream;
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
63 private final static String embedFileName = <xsl:value-of select="embedFile"/>;
64 private final static String embedResourceName = <xsl:value-of select="embedResource"/>;
65 private static PDFTTFStream embeddedFont=null;
66 private final static int flags = <xsl:value-of select="flags"/>;
67 private final static int stemV = <xsl:value-of select="stemv"/>;
68 private final static int italicAngle = <xsl:value-of select="italicangle"/>;
69 private final static int firstChar = <xsl:value-of select="first-char"/>;
70 private final static int lastChar = <xsl:value-of select="last-char"/>;
71 private final static int[] width;
72 private final static HashMap kerning=new HashMap();
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"/>;</xsl:if>-->
76 <xsl:for-each select="widths/char"><xsl:variable name="char-name" select="@name"/> width[<xsl:value-of select="$char-name"/>] = <xsl:value-of select="@width"/>;
77 </xsl:for-each>
78
79 HashMap tmptable;
80 <xsl:for-each select="kerning">
81 <xsl:variable name="kpx1-name" select="@kpx1"/>
82 tmptable=new HashMap();<xsl:for-each select="pair"><xsl:variable name="kpx2-name" select="@kpx2"/><xsl:variable name="kern-name" select="@kern"/>
83 tmptable.put(Glyphs.glyphToString("<xsl:value-of select="$kpx2-name"/>"), new Integer(<xsl:value-of select="$kern-name"/>));</xsl:for-each>
84 kerning.put(Glyphs.glyphToString("<xsl:value-of select="$kpx1-name"/>"), tmptable);
85 </xsl:for-each>
86 }
87
88 public final boolean hasKerningInfo() {return kerning.isEmpty();}
89 public final java.util.HashMap getKerningInfo() {return kerning;}
90
91 public byte getSubType() {return org.apache.fop.pdf.PDFFont.TRUETYPE;}
92 public boolean isEmbeddable() {
93 return (embedFileName==null &amp;&amp; embedResourceName==null) ? false : true;
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 PDFTTFStream(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 public String encoding() {
151 return encoding;
152 }
153
154 public String fontName() {
155 return fontName;
156 }
157
158 public int getAscender() {return ascender;}
159 public int getDescender() {return descender;}
160 public int getCapHeight() {return capHeight;}
161
162 public int getAscender(int size) {
163 return size * ascender;
164 }
165
166 public int getCapHeight(int size) {
167 return size * capHeight;
168 }
169
170 public int getDescender(int size) {
171 return size * descender;
172 }
173
174 public int getXHeight(int size) {
175 return size * xHeight;
176 }
177
178 public int getFlags() {
179 return flags;
180 }
181
182 public int[] getFontBBox() {
183 return fontBBox;
184 }
185
186 public int getItalicAngle() {
187 return italicAngle;
188 }
189
190 public int getStemV() {
191 return stemV;
192 }
193
194 public int getFirstChar() {
195 return firstChar;
196 }
197
198 public int getLastChar() {
199 return lastChar;
200 }
201
202 public int width(int i, int size) {
203 return size * width[i];
204 }
205
206 public int[] getWidths(int size) {
207 int[] arr = new int[getLastChar()-getFirstChar()+1];
208 System.arraycopy(width, getFirstChar(), arr, 0, getLastChar()-getFirstChar()+1);
209 for( int i = 0; i &lt; arr.length; i++) arr[i] *= size;
210 return arr;
211 }
212 }
213 <!--</redirect:write>-->
214 </xsl:template>
215 </xsl:stylesheet>
216
217