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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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="1.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="native-encoding" select="/font-metrics/encoding"/>
  30. <xsl:variable name="glyphs" select="document('encodings.xml')/encoding-set/encoding[@id=$encoding]/glyph"/>
  31. <xsl:template match="font-metrics">
  32. package org.apache.fop.fonts.base14;
  33. <xsl:if test="count(kerning) &gt; 0">
  34. import java.util.Map;
  35. </xsl:if>
  36. import org.apache.fop.fonts.FontType;
  37. import org.apache.fop.fonts.Typeface;
  38. import org.apache.fop.fonts.CodePointMapping;
  39. public class <xsl:value-of select="class-name"/> extends Typeface {
  40. private final static String fontName = "<xsl:value-of select="font-name"/>";
  41. private final static String encoding = <xsl:choose><xsl:when test="$encoding != $native-encoding">"<xsl:value-of select="$encoding"/>"</xsl:when><xsl:otherwise>null</xsl:otherwise></xsl:choose>;
  42. private final static int capHeight = <xsl:value-of select="cap-height"/>;
  43. private final static int xHeight = <xsl:value-of select="x-height"/>;
  44. private final static int ascender = <xsl:value-of select="ascender"/>;
  45. private final static int descender = <xsl:value-of select="descender"/>;
  46. private final static int firstChar = <xsl:value-of select="first-char"/>;
  47. private final static int lastChar = <xsl:value-of select="last-char"/>;
  48. private final static int[] width;
  49. private final CodePointMapping mapping =
  50. CodePointMapping.getMapping("<xsl:value-of select="$encoding"/>");
  51. <xsl:if test="count(kerning) &gt; 0">
  52. private final static Map kerning;
  53. </xsl:if>
  54. private boolean enableKerning = false;
  55. static {
  56. width = new int[256];
  57. <xsl:apply-templates select="widths"/>
  58. <xsl:if test="count(kerning) &gt; 0">
  59. kerning = new java.util.HashMap();
  60. Integer first, second;
  61. Map pairs;
  62. <xsl:apply-templates select="kerning"/>
  63. </xsl:if>
  64. }
  65. public <xsl:value-of select="class-name"/>() {
  66. this(false);
  67. }
  68. public <xsl:value-of select="class-name"/>(boolean enableKerning) {
  69. this.enableKerning = enableKerning;
  70. }
  71. public String getEncoding() {
  72. return encoding;
  73. }
  74. public String getFontName() {
  75. return fontName;
  76. }
  77. public FontType getFontType() {
  78. return FontType.TYPE1;
  79. }
  80. public int getAscender(int size) {
  81. return size * ascender;
  82. }
  83. public int getCapHeight(int size) {
  84. return size * capHeight;
  85. }
  86. public int getDescender(int size) {
  87. return size * descender;
  88. }
  89. public int getXHeight(int size) {
  90. return size * xHeight;
  91. }
  92. public int getFirstChar() {
  93. return firstChar;
  94. }
  95. public int getLastChar() {
  96. return lastChar;
  97. }
  98. public int getWidth(int i,int size) {
  99. return size * width[i];
  100. }
  101. public int[] getWidths() {
  102. int[] arr = new int[getLastChar() - getFirstChar() + 1];
  103. System.arraycopy(width, getFirstChar(), arr, 0, getLastChar() - getFirstChar() + 1);
  104. return arr;
  105. }
  106. <xsl:choose>
  107. <xsl:when test="count(kerning) &gt; 0">
  108. public boolean hasKerningInfo() {
  109. return enableKerning;
  110. }
  111. public java.util.Map getKerningInfo() {
  112. return kerning;
  113. }
  114. </xsl:when>
  115. <xsl:otherwise>
  116. public boolean hasKerningInfo() {
  117. return false;
  118. }
  119. public java.util.Map getKerningInfo() {
  120. return java.util.Collections.EMPTY_MAP;
  121. }
  122. </xsl:otherwise>
  123. </xsl:choose>
  124. public char mapChar(char c) {
  125. char d = mapping.mapChar(c);
  126. if(d != 0) {
  127. return d;
  128. } else {
  129. return '#';
  130. }
  131. }
  132. public boolean hasChar(char c) {
  133. return (mapping.mapChar(c) > 0);
  134. }
  135. }
  136. </xsl:template>
  137. <xsl:template match="widths/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"/>;</xsl:if></xsl:template>
  138. <xsl:template match="kerning">
  139. first = new Integer(<xsl:value-of select="@kpx1"/>);
  140. pairs = (Map)kerning.get(first);
  141. if (pairs == null) {
  142. pairs = new java.util.HashMap();
  143. kerning.put(first, pairs);
  144. }
  145. <xsl:apply-templates select="pair"/>
  146. </xsl:template>
  147. <xsl:template match="pair">
  148. second = new Integer(<xsl:value-of select="@kpx2"/>);
  149. pairs.put(second, new Integer(<xsl:value-of select="@kern"/>));
  150. </xsl:template>
  151. </xsl:stylesheet>