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.

t1font-file.xsl 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. <!--
  17. This files writes the class files for the Adobe Type 1 fonts.
  18. It uses the information in the font description files (Courier.xml, Helvetica.xml) to this
  19. In these font description files each character is referenced by its adobe name:
  20. <char name="A" width="667"/>
  21. To resolve this name and to find the code for this character it looks up the adobe name in the
  22. file charlist.xml and extracts the WinAnsi code.
  23. -->
  24. <xsl:stylesheet version="1.0"
  25. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  26. xmlns:lxslt="http://xml.apache.org/xslt"
  27. xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"
  28. extension-element-prefixes="redirect">
  29. <xsl:output method="text" />
  30. <xsl:template match="font-metrics">
  31. <xsl:variable name="class-name" select="class-name"/>
  32. <!--<redirect:write select="concat('org/apache/fop/render/pdf/fonts/', $class-name, '.java')">-->
  33. package org.apache.fop.render.pdf.fonts;
  34. import org.apache.fop.render.pdf.Font;
  35. import org.apache.fop.layout.FontDescriptor;
  36. import org.apache.fop.fonts.Glyphs;
  37. import org.apache.fop.pdf.PDFStream;
  38. import org.apache.fop.pdf.PDFT1Stream;
  39. import java.io.InputStream;
  40. import java.io.FileInputStream;
  41. import java.io.BufferedInputStream;
  42. import java.util.HashMap;
  43. public class <xsl:value-of select="class-name"/> extends Font implements FontDescriptor {
  44. private final static String fontName = "<xsl:value-of select="font-name"/>";
  45. private final static String encoding = "<xsl:value-of select="encoding"/>";
  46. private final static int capHeight = <xsl:value-of select="cap-height"/>;
  47. private final static int xHeight = <xsl:value-of select="x-height"/>;
  48. private final static int ascender = <xsl:value-of select="ascender"/>;
  49. private final static int descender = <xsl:value-of select="descender"/>;
  50. private final static int[] fontBBox = {
  51. <xsl:value-of select="bbox/left"/>,
  52. <xsl:value-of select="bbox/bottom"/>,
  53. <xsl:value-of select="bbox/right"/>,
  54. <xsl:value-of select="bbox/top"/>
  55. };
  56. private final static String embedFileName = <xsl:value-of select="embedFile"/>;
  57. private final static String embedResourceName = <xsl:value-of select="embedResource"/>;
  58. private static PDFT1Stream embeddedFont=null;
  59. private final static int flags = <xsl:value-of select="flags"/>;
  60. private final static int stemV = <xsl:value-of select="stemv"/>;
  61. private final static int italicAngle = <xsl:value-of select="italicangle"/>;
  62. private final static int firstChar = <xsl:value-of select="first-char"/>;
  63. private final static int lastChar = <xsl:value-of select="last-char"/>;
  64. private final static int[] width;
  65. private final static HashMap kerning=new HashMap();
  66. static {
  67. width = new int[256];
  68. <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"/>;
  69. </xsl:if></xsl:for-each>
  70. HashMap tmptable;
  71. <xsl:for-each select="kerning">
  72. <xsl:variable name="kpx1-name" select="@kpx1"/>
  73. tmptable=new HashMap();<xsl:for-each select="pair"><xsl:variable name="kpx2-name" select="@kpx2"/><xsl:variable name="kern-name" select="@kern"/>
  74. tmptable.put(Glyphs.glyphToString("<xsl:value-of select="$kpx2-name"/>"), new Integer(<xsl:value-of select="$kern-name"/>));</xsl:for-each>
  75. kerning.put(Glyphs.glyphToString("<xsl:value-of select="$kpx1-name"/>"), tmptable);
  76. </xsl:for-each>
  77. }
  78. public final boolean hasKerningInfo() {return kerning.isEmpty();}
  79. public final java.util.HashMap getKerningInfo() {return kerning;}
  80. public byte getSubType() {return org.apache.fop.pdf.PDFFont.TYPE1;}
  81. public boolean isEmbeddable() {
  82. return (embedFileName==null &amp;&amp; embedResourceName==null) ? false : true;
  83. }
  84. public PDFStream getFontFile(int i) {
  85. InputStream instream=null;
  86. // Get file first
  87. if (embedFileName!=null)
  88. try {
  89. instream=new FileInputStream(embedFileName);
  90. } catch (Exception e) {
  91. System.out.println("Failed to embed fontfile: "+embedFileName);
  92. }
  93. // Get resource
  94. if (instream==null &amp;&amp; embedResourceName!=null)
  95. try {
  96. instream=new BufferedInputStream(this.getClass().getResourceAsStream(embedResourceName));
  97. } catch (Exception e) {
  98. System.out.println("Failed to embed fontresource: "+embedResourceName);
  99. }
  100. if (instream==null)
  101. return (PDFStream)null;
  102. // Read fontdata
  103. byte[] file = new byte[128000];
  104. int fsize = 0;
  105. try {
  106. int l = instream.read(file, 0, 128000);
  107. fsize += l;
  108. if (l==128000) {
  109. // More to read - needs to extend
  110. byte[] tmpbuf;
  111. while (l &gt; 0) {
  112. tmpbuf = new byte[file.length + 64000];
  113. System.arraycopy(file, 0, tmpbuf, 0, file.length);
  114. l=instream.read(tmpbuf, file.length, 64000);
  115. fsize += l;
  116. file = tmpbuf;
  117. if (l &lt; 64000) // whole file read. No need to loop again
  118. l=0;
  119. }
  120. }
  121. embeddedFont=new PDFT1Stream(i, fsize);
  122. embeddedFont.addFilter("flate");
  123. embeddedFont.addFilter("ascii-85");
  124. embeddedFont.setData(file, fsize);
  125. instream.close();
  126. } catch (Exception e) {}
  127. return (PDFStream) embeddedFont;
  128. }
  129. public String encoding() {
  130. return encoding;
  131. }
  132. public String fontName() {
  133. return fontName;
  134. }
  135. public int getAscender() {
  136. return ascender;
  137. }
  138. public int getCapHeight() {
  139. return capHeight;
  140. }
  141. public int getDescender() {
  142. return descender;
  143. }
  144. public int getAscender(int size) {
  145. return size * ascender;
  146. }
  147. public int getCapHeight(int size) {
  148. return size * capHeight;
  149. }
  150. public int getDescender(int size) {
  151. return size * descender;
  152. }
  153. public int getXHeight(int size) {
  154. return size * xHeight;
  155. }
  156. public int getFlags() {
  157. return flags;
  158. }
  159. public int[] getFontBBox() {
  160. return fontBBox;
  161. }
  162. public int getItalicAngle() {
  163. return italicAngle;
  164. }
  165. public int getStemV() {
  166. return stemV;
  167. }
  168. public int getFirstChar() {
  169. return firstChar;
  170. }
  171. public int getLastChar() {
  172. return lastChar;
  173. }
  174. public int width(int i, int size) {
  175. return size * width[i];
  176. }
  177. public int[] getWidths(int size) {
  178. int[] arr = new int[getLastChar()-getFirstChar()+1];
  179. System.arraycopy(width, getFirstChar(), arr, 0, getLastChar()-getFirstChar()+1);
  180. for( int i = 0; i &lt; arr.length; i++) arr[i] *= size;
  181. return arr;
  182. }
  183. }
  184. <!--</redirect:write>-->
  185. </xsl:template>
  186. </xsl:stylesheet>