You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

font-file.xsl 8.3KB

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