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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. package org.apache.fop.fonts.base14;
  32. import java.awt.Rectangle;
  33. import java.net.URI;
  34. <xsl:if test="count(kerning) &gt; 0">
  35. import java.util.Map;
  36. </xsl:if>
  37. import java.util.Set;
  38. import org.apache.fop.fonts.FontType;
  39. import org.apache.fop.fonts.Base14Font;
  40. import org.apache.fop.fonts.CodePointMapping;
  41. import org.apache.fop.fonts.Typeface;
  42. public class <xsl:value-of select="class-name"/> extends Base14Font {
  43. private final static URI fontFileURI;
  44. private final static String fontName = "<xsl:value-of select="font-name"/>";
  45. private final static String fullName = "<xsl:value-of select="full-name"/>";
  46. private final static Set familyNames;
  47. private final static int underlinePosition = <xsl:value-of select="underline-position"/>;
  48. private final static int underlineThickness = <xsl:value-of select="underline-thickness"/>;
  49. private final static String encoding = "<xsl:value-of select="$encoding"/>";
  50. private final static int capHeight = <xsl:value-of select="cap-height"/>;
  51. private final static int xHeight = <xsl:value-of select="x-height"/>;
  52. private final static int ascender = <xsl:value-of select="ascender"/>;
  53. private final static int descender = <xsl:value-of select="descender"/>;
  54. private final static int firstChar = <xsl:value-of select="first-char"/>;
  55. private final static int lastChar = <xsl:value-of select="last-char"/>;
  56. private final static int[] width;
  57. private final static Rectangle[] boundingBoxes;
  58. private final CodePointMapping mapping =
  59. CodePointMapping.getMapping("<xsl:value-of select="$encoding"/>");
  60. <xsl:if test="count(kerning) &gt; 0">
  61. private final static Map kerning;
  62. </xsl:if>
  63. private boolean enableKerning = false;
  64. static {
  65. URI uri = null;
  66. try {
  67. uri = new URI("base14:" + fontName.toLowerCase());
  68. } catch (java.net.URISyntaxException e) {
  69. }
  70. fontFileURI = uri;
  71. width = new int[256];
  72. boundingBoxes = new Rectangle[256];
  73. <xsl:apply-templates select="char-metrics"/>
  74. <xsl:if test="count(kerning) &gt; 0">
  75. kerning = new java.util.HashMap();
  76. Integer first, second;
  77. Map pairs;
  78. <xsl:apply-templates select="kerning"/>
  79. </xsl:if>
  80. familyNames = new java.util.HashSet();
  81. familyNames.add("<xsl:value-of select="family-name"/>");
  82. }
  83. public <xsl:value-of select="class-name"/>() {
  84. this(false);
  85. }
  86. public <xsl:value-of select="class-name"/>(boolean enableKerning) {
  87. this.enableKerning = enableKerning;
  88. }
  89. public String getEncodingName() {
  90. return encoding;
  91. }
  92. public URI getFontURI() {
  93. return fontFileURI;
  94. }
  95. public String getFontName() {
  96. return fontName;
  97. }
  98. public String getEmbedFontName() {
  99. return getFontName();
  100. }
  101. public String getFullName() {
  102. return fullName;
  103. }
  104. public Set getFamilyNames() {
  105. return familyNames;
  106. }
  107. public FontType getFontType() {
  108. return FontType.TYPE1;
  109. }
  110. public int getAscender(int size) {
  111. return size * ascender;
  112. }
  113. public int getCapHeight(int size) {
  114. return size * capHeight;
  115. }
  116. public int getDescender(int size) {
  117. return size * descender;
  118. }
  119. public int getXHeight(int size) {
  120. return size * xHeight;
  121. }
  122. public int getUnderlinePosition(int size) {
  123. return size * underlinePosition;
  124. }
  125. public int getUnderlineThickness(int size) {
  126. return size * underlineThickness;
  127. }
  128. public int getFirstChar() {
  129. return firstChar;
  130. }
  131. public int getLastChar() {
  132. return lastChar;
  133. }
  134. public int getWidth(int i,int size) {
  135. return size * width[i];
  136. }
  137. public Rectangle getBoundingBox(int glyphIndex, int size) {
  138. Rectangle bbox = boundingBoxes[glyphIndex];
  139. return new Rectangle(bbox.x * size, bbox.y * size, bbox.width * size, bbox.height * size);
  140. }
  141. public int[] getWidths() {
  142. int[] arr = new int[getLastChar() - getFirstChar() + 1];
  143. System.arraycopy(width, getFirstChar(), arr, 0, getLastChar() - getFirstChar() + 1);
  144. return arr;
  145. }
  146. <xsl:choose>
  147. <xsl:when test="count(kerning) &gt; 0">
  148. public boolean hasKerningInfo() {
  149. return enableKerning;
  150. }
  151. public java.util.Map getKerningInfo() {
  152. return kerning;
  153. }
  154. </xsl:when>
  155. <xsl:otherwise>
  156. public boolean hasKerningInfo() {
  157. return false;
  158. }
  159. public java.util.Map getKerningInfo() {
  160. return java.util.Collections.EMPTY_MAP;
  161. }
  162. </xsl:otherwise>
  163. </xsl:choose>
  164. public char mapChar(char c) {
  165. notifyMapOperation();
  166. char d = mapping.mapChar(c);
  167. if (d != 0) {
  168. return d;
  169. } else {
  170. this.warnMissingGlyph(c);
  171. return Typeface.NOT_FOUND;
  172. }
  173. }
  174. public boolean hasChar(char c) {
  175. return (mapping.mapChar(c) > 0);
  176. }
  177. }
  178. </xsl:template>
  179. <xsl:template match="char-metrics/char">
  180. <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"/>;
  181. 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>
  182. <xsl:template match="kerning">
  183. first = new Integer(<xsl:value-of select="@kpx1"/>);
  184. pairs = (Map)kerning.get(first);
  185. if (pairs == null) {
  186. pairs = new java.util.HashMap();
  187. kerning.put(first, pairs);
  188. }
  189. <xsl:apply-templates select="pair"/>
  190. </xsl:template>
  191. <xsl:template match="pair">
  192. second = new Integer(<xsl:value-of select="@kpx2"/>);
  193. pairs.put(second, new Integer(<xsl:value-of select="@kern"/>));
  194. </xsl:template>
  195. </xsl:stylesheet>