| 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 |
<!-- This file writes the class files for the fonts (Courier.java, |
| 19 |
Helvetica.java etc.). It uses the information in the font |
| 20 |
description files (Courier.xml, Helvetica.xml) to do this. In these |
| 21 |
font description files each character is referenced by its adobe |
| 22 |
glyph name: |
| 23 |
<char name="A" width="667"/> |
| 24 |
To resolve this name and to find the code for this character it looks |
| 25 |
up the adobe name in the file encodings.xml and extracts the appropriate |
| 26 |
code. --> |
| 27 |
<xsl:stylesheet version="2.0" |
| 28 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 29 |
|
| 30 |
<xsl:output method="text"/> |
| 31 |
|
| 32 |
<xsl:param name="encoding" select="/font-metrics/encoding"/> |
| 33 |
<xsl:variable name="glyphs" select="document('encodings.xml')/encoding-set/encoding[@id=$encoding]/glyph"/> |
| 34 |
|
| 35 |
<xsl:template match="font-metrics"> /* |
| 36 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 37 |
* contributor license agreements. See the NOTICE file distributed with |
| 38 |
* this work for additional information regarding copyright ownership. |
| 39 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 40 |
* (the "License"); you may not use this file except in compliance with |
| 41 |
* the License. You may obtain a copy of the License at |
| 42 |
* |
| 43 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 44 |
* |
| 45 |
* Unless required by applicable law or agreed to in writing, software |
| 46 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 47 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 48 |
* See the License for the specific language governing permissions and |
| 49 |
* limitations under the License. |
| 50 |
*/ |
| 51 |
|
| 52 |
package org.apache.fop.fonts.base14; |
| 53 |
|
| 54 |
import java.awt.Rectangle; |
| 55 |
import java.net.URI; |
| 56 |
<xsl:if test="count(kerning) > 0"> |
| 57 |
import java.util.Map; |
| 58 |
</xsl:if> |
| 59 |
import java.util.Set; |
| 60 |
|
| 61 |
import org.apache.fop.fonts.Base14Font; |
| 62 |
import org.apache.fop.fonts.CodePointMapping; |
| 63 |
import org.apache.fop.fonts.FontType; |
| 64 |
import org.apache.fop.fonts.Typeface; |
| 65 |
|
| 66 |
// CSOFF: ConstantNameCheck |
| 67 |
|
| 68 |
public class <xsl:value-of select="class-name"/> extends Base14Font { |
| 69 |
private static final URI fontFileURI; |
| 70 |
private static final String fontName = "<xsl:value-of select="font-name"/>"; |
| 71 |
private static final String fullName = "<xsl:value-of select="full-name"/>"; |
| 72 |
private static final Set familyNames; |
| 73 |
private static final int underlinePosition = <xsl:value-of select="underline-position"/>; |
| 74 |
private static final int underlineThickness = <xsl:value-of select="underline-thickness"/>; |
| 75 |
private static final String encoding = "<xsl:value-of select="$encoding"/>"; |
| 76 |
private static final int capHeight = <xsl:value-of select="cap-height"/>; |
| 77 |
private static final int xHeight = <xsl:value-of select="x-height"/>; |
| 78 |
private static final int ascender = <xsl:value-of select="ascender"/>; |
| 79 |
private static final int descender = <xsl:value-of select="descender"/>; |
| 80 |
private static final int firstChar = <xsl:value-of select="first-char"/>; |
| 81 |
private static final int lastChar = <xsl:value-of select="last-char"/>; |
| 82 |
private static final int[] width; |
| 83 |
private static final Rectangle[] boundingBoxes; |
| 84 |
private final CodePointMapping mapping = |
| 85 |
CodePointMapping.getMapping("<xsl:value-of select="$encoding"/>"); |
| 86 |
<xsl:if test="count(kerning) > 0"> |
| 87 |
private static final Map kerning; |
| 88 |
</xsl:if> |
| 89 |
|
| 90 |
private boolean enableKerning; |
| 91 |
|
| 92 |
static { |
| 93 |
URI uri = null; |
| 94 |
try { |
| 95 |
uri = new URI("base14:" + fontName.toLowerCase()); |
| 96 |
} catch (java.net.URISyntaxException e) { |
| 97 |
throw new RuntimeException(e); |
| 98 |
} |
| 99 |
fontFileURI = uri; |
| 100 |
width = new int[256]; |
| 101 |
boundingBoxes = new Rectangle[256]; |
| 102 |
<xsl:apply-templates select="char-metrics"/> |
| 103 |
familyNames = new java.util.HashSet(); |
| 104 |
familyNames.add("<xsl:value-of select="family-name"/>"); |
| 105 |
<xsl:if test="count(kerning) > 0"> |
| 106 |
kerning = new java.util.HashMap(); |
| 107 |
Integer first; |
| 108 |
Integer second; |
| 109 |
Map pairs; |
| 110 |
<xsl:apply-templates select="kerning"/> |
| 111 |
</xsl:if> |
| 112 |
} |
| 113 |
|
| 114 |
public <xsl:value-of select="class-name"/>() { |
| 115 |
this(false); |
| 116 |
} |
| 117 |
|
| 118 |
public <xsl:value-of select="class-name"/>(boolean enableKerning) { |
| 119 |
this.enableKerning = enableKerning; |
| 120 |
} |
| 121 |
|
| 122 |
public String getEncodingName() { |
| 123 |
return encoding; |
| 124 |
} |
| 125 |
|
| 126 |
public URI getFontURI() { |
| 127 |
return fontFileURI; |
| 128 |
} |
| 129 |
|
| 130 |
public String getFontName() { |
| 131 |
return fontName; |
| 132 |
} |
| 133 |
|
| 134 |
public String getEmbedFontName() { |
| 135 |
return getFontName(); |
| 136 |
} |
| 137 |
|
| 138 |
public String getFullName() { |
| 139 |
return fullName; |
| 140 |
} |
| 141 |
|
| 142 |
public Set getFamilyNames() { |
| 143 |
return familyNames; |
| 144 |
} |
| 145 |
|
| 146 |
public FontType getFontType() { |
| 147 |
return FontType.TYPE1; |
| 148 |
} |
| 149 |
|
| 150 |
public int getAscender(int size) { |
| 151 |
return size * ascender; |
| 152 |
} |
| 153 |
|
| 154 |
public int getCapHeight(int size) { |
| 155 |
return size * capHeight; |
| 156 |
} |
| 157 |
|
| 158 |
public int getDescender(int size) { |
| 159 |
return size * descender; |
| 160 |
} |
| 161 |
|
| 162 |
public int getXHeight(int size) { |
| 163 |
return size * xHeight; |
| 164 |
} |
| 165 |
|
| 166 |
public int getUnderlinePosition(int size) { |
| 167 |
return size * underlinePosition; |
| 168 |
} |
| 169 |
|
| 170 |
public int getUnderlineThickness(int size) { |
| 171 |
return size * underlineThickness; |
| 172 |
} |
| 173 |
|
| 174 |
public int getFirstChar() { |
| 175 |
return firstChar; |
| 176 |
} |
| 177 |
|
| 178 |
public int getLastChar() { |
| 179 |
return lastChar; |
| 180 |
} |
| 181 |
|
| 182 |
public int getWidth(int i, int size) { |
| 183 |
return size * width[i]; |
| 184 |
} |
| 185 |
|
| 186 |
public Rectangle getBoundingBox(int glyphIndex, int size) { |
| 187 |
Rectangle bbox = boundingBoxes[glyphIndex]; |
| 188 |
return new Rectangle(bbox.x * size, bbox.y * size, bbox.width * size, bbox.height * size); |
| 189 |
} |
| 190 |
|
| 191 |
public int[] getWidths() { |
| 192 |
int[] arr = new int[getLastChar() - getFirstChar() + 1]; |
| 193 |
System.arraycopy(width, getFirstChar(), arr, 0, getLastChar() - getFirstChar() + 1); |
| 194 |
return arr; |
| 195 |
} |
| 196 |
<xsl:choose> |
| 197 |
<xsl:when test="count(kerning) > 0"> |
| 198 |
public boolean hasKerningInfo() { |
| 199 |
return enableKerning; |
| 200 |
} |
| 201 |
|
| 202 |
public java.util.Map getKerningInfo() { |
| 203 |
return kerning; |
| 204 |
} |
| 205 |
</xsl:when> |
| 206 |
<xsl:otherwise> |
| 207 |
public boolean hasKerningInfo() { |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
public java.util.Map getKerningInfo() { |
| 212 |
return java.util.Collections.EMPTY_MAP; |
| 213 |
} |
| 214 |
</xsl:otherwise> |
| 215 |
</xsl:choose> |
| 216 |
public char mapChar(char c) { |
| 217 |
notifyMapOperation(); |
| 218 |
char d = mapping.mapChar(c); |
| 219 |
if (d != 0) { |
| 220 |
return d; |
| 221 |
} else { |
| 222 |
this.warnMissingGlyph(c); |
| 223 |
return Typeface.NOT_FOUND; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
public boolean hasChar(char c) { |
| 228 |
return (mapping.mapChar(c) > 0); |
| 229 |
} |
| 230 |
|
| 231 |
} |
| 232 |
</xsl:template> |
| 233 |
|
| 234 |
<xsl:template match="char-metrics/char"><xsl:variable name="char-name" select="@name"/><xsl:variable name="char-num" select="$glyphs[@name = $char-name]/@codepoint"/><xsl:if test="$char-num!=''"> width[0x<xsl:value-of select="$char-num"/>] = <xsl:value-of select="@width"/>; |
| 235 |
boundingBoxes[0x<xsl:value-of select="$char-num"/>] = new Rectangle(<xsl:value-of select="@llx"/>, <xsl:value-of select="@lly"/>, <xsl:value-of select="@urx - @llx"/>, <xsl:value-of select="@ury - @lly"/>);</xsl:if></xsl:template> |
| 236 |
|
| 237 |
<xsl:template match="kerning"> |
| 238 |
first = <xsl:value-of select="@kpx1"/>; |
| 239 |
pairs = (Map)kerning.get(first); |
| 240 |
if (pairs == null) { |
| 241 |
pairs = new java.util.HashMap(); |
| 242 |
kerning.put(first, pairs); |
| 243 |
} |
| 244 |
<xsl:apply-templates select="pair"/></xsl:template> |
| 245 |
|
| 246 |
<xsl:template match="pair"> |
| 247 |
second = <xsl:value-of select="@kpx2"/>; |
| 248 |
pairs.put(second, <xsl:value-of select="@kern"/>); |
| 249 |
</xsl:template> |
| 250 |
|
| 251 |
</xsl:stylesheet> |
| 252 |
|