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.

FontReader.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*-- $Id$ --
  2. ============================================================================
  3. The Apache Software License, Version 1.1
  4. ============================================================================
  5. Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modifica-
  7. tion, are permitted provided that the following conditions are met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. 3. The end-user documentation included with the redistribution, if any, must
  14. include the following acknowledgment: "This product includes software
  15. developed by the Apache Software Foundation (http://www.apache.org/)."
  16. Alternately, this acknowledgment may appear in the software itself, if
  17. and wherever such third-party acknowledgments normally appear.
  18. 4. The names "Fop" and "Apache Software Foundation" must not be used to
  19. endorse or promote products derived from this software without prior
  20. written permission. For written permission, please contact
  21. apache@apache.org.
  22. 5. Products derived from this software may not be called "Apache", nor may
  23. "Apache" appear in their name, without prior written permission of the
  24. Apache Software Foundation.
  25. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  26. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  29. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  30. DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  32. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. This software consists of voluntary contributions made by many individuals
  36. on behalf of the Apache Software Foundation and was originally created by
  37. James Tauber <jtauber@jtauber.com>. For more information on the Apache
  38. Software Foundation, please see <http://www.apache.org/>.
  39. */
  40. package org.apache.fop.render.pdf;
  41. import org.apache.fop.render.pdf.fonts.*;
  42. import org.xml.sax.helpers.DefaultHandler;
  43. import org.xml.sax.XMLReader;
  44. import org.xml.sax.SAXException;
  45. import org.xml.sax.InputSource;
  46. import org.xml.sax.Locator;
  47. import org.xml.sax.Attributes;
  48. import java.io.IOException;
  49. import java.util.Enumeration;
  50. import java.util.Vector;
  51. import java.util.Hashtable;
  52. import org.apache.fop.pdf.PDFWArray;
  53. import org.apache.fop.pdf.PDFCIDFont;
  54. import org.apache.fop.configuration.ConfigurationReader;
  55. /**
  56. * Class for reading a metric.xml file and creating a font object.
  57. * Typical usage:
  58. <pre>
  59. FontReader reader = new FontReader(<path til metrics.xml>);
  60. reader.setFontEmbedPath(<path to a .ttf or .pfb file or null to diable embedding>);
  61. reader.useKerning(true);
  62. Font f = reader.getFont();
  63. </pre>
  64. */
  65. public class FontReader extends DefaultHandler {
  66. private Locator locator = null;
  67. private boolean isCID = false;
  68. private MultiByteFont multiFont = null;
  69. private SingleByteFont singleFont = null;
  70. private Font returnFont = null;
  71. //private SingleByteFont singleFont = null;
  72. private String text = null;
  73. private Vector cidWidths = null;
  74. private int cidWidthIndex = 0;
  75. private Hashtable currentKerning = null;
  76. private Vector bfranges = null;
  77. private void createFont(String path) throws IOException {
  78. XMLReader parser = ConfigurationReader.createParser();
  79. if (parser == null)
  80. throw new IOException("Unable to create SAX parser");
  81. try {
  82. parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
  83. false);
  84. } catch (SAXException e) {
  85. throw new IOException ("You need a SAX parser which supports " +
  86. "SAX version 2");
  87. }
  88. parser.setContentHandler(this);
  89. try {
  90. parser.parse(path);
  91. } catch (SAXException e) {
  92. throw new IOException(e.getMessage());
  93. }
  94. }
  95. /**
  96. * Sets the path to embed a font. a null value disables font embedding
  97. */
  98. public void setFontEmbedPath(String path) {
  99. if (isCID)
  100. multiFont.embedFileName=path;
  101. else
  102. singleFont.embedFileName=path;
  103. }
  104. /**
  105. * Enable/disable use of kerning for the font
  106. */
  107. public void useKerning(boolean kern) {
  108. if (isCID)
  109. multiFont.useKerning = true;
  110. else
  111. singleFont.useKerning = true;
  112. }
  113. /**
  114. * Get the generated font object
  115. */
  116. public Font getFont() {
  117. return returnFont;
  118. }
  119. /**
  120. * Construct a FontReader object from a path to a metric.xml file
  121. * and read metric data
  122. */
  123. public FontReader(String path) throws IOException {
  124. createFont(path);
  125. }
  126. public void startDocument() {
  127. }
  128. public void setDocumentLocator(Locator locator) {
  129. this.locator = locator;
  130. }
  131. public void startElement(String uri, String localName,
  132. String qName, Attributes attributes) {
  133. if (localName.equals("font-metrics")) {
  134. if ("TYPE0".equals(attributes.getValue("type"))) {
  135. multiFont = new MultiByteFont();
  136. returnFont = multiFont;
  137. isCID = true;
  138. } else if ("TRUETYPE".equals(attributes.getValue("type"))) {
  139. singleFont = new SingleByteFont();
  140. singleFont.subType = org.apache.fop.pdf.PDFFont.TRUETYPE;
  141. returnFont = singleFont;
  142. isCID = false;
  143. } else {
  144. singleFont = new SingleByteFont();
  145. singleFont.subType = org.apache.fop.pdf.PDFFont.TYPE1;
  146. returnFont = singleFont;
  147. isCID = false;
  148. }
  149. } else if ("embed".equals(localName)) {
  150. if (isCID) {
  151. // This *is* annoying... should create a common
  152. // interface for sing/multibytefonts...
  153. multiFont.embedFileName = attributes.getValue("file");
  154. multiFont.embedResourceName = attributes.getValue("class");
  155. } else {
  156. singleFont.embedFileName = attributes.getValue("file");
  157. singleFont.embedResourceName = attributes.getValue("class");
  158. }
  159. } else if ("cid-widths".equals(localName)) {
  160. cidWidthIndex = getInt(attributes.getValue("start-index"));
  161. cidWidths = new Vector();
  162. } else if ("kerning".equals(localName)) {
  163. currentKerning = new Hashtable();
  164. if (isCID)
  165. multiFont.kerning.put(new Integer(attributes.getValue("kpx1")),
  166. currentKerning);
  167. else
  168. singleFont.kerning.put(new Integer(attributes.getValue("kpx1")),
  169. currentKerning);
  170. } else if ("bfranges".equals(localName)) {
  171. bfranges = new Vector();
  172. } else if ("bf".equals(localName)) {
  173. BFEntry entry = new BFEntry();
  174. entry.unicodeStart = getInt(attributes.getValue("us"));
  175. entry.unicodeEnd = getInt(attributes.getValue("ue"));
  176. entry.glyphStartIndex = getInt(attributes.getValue("gi"));
  177. bfranges.addElement(entry);
  178. } else if ("wx".equals(localName)) {
  179. cidWidths.addElement(new Integer(attributes.getValue("w")));
  180. } else if ("widths".equals(localName)) {
  181. singleFont.width = new int[256];
  182. } else if ("char".equals(localName)) {
  183. try {
  184. singleFont.width[Integer.parseInt(attributes.getValue("idx"))] =
  185. Integer.parseInt(attributes.getValue("wdt"));
  186. } catch (NumberFormatException ne) {
  187. System.out.println("Malformed width in metric file: " +
  188. ne.getMessage());
  189. }
  190. } else if ("pair".equals(localName)) {
  191. currentKerning.put(new Integer(attributes.getValue("kpx2")),
  192. new Integer(attributes.getValue("kern")));
  193. }
  194. }
  195. private int getInt(String str) {
  196. int ret = 0;
  197. try {
  198. ret = Integer.parseInt(str);
  199. } catch (Exception e) {}
  200. return ret;
  201. }
  202. public void endElement(String uri, String localName, String qName) {
  203. if ("font-name".equals(localName))
  204. if (isCID)
  205. multiFont.fontName = text;
  206. else
  207. singleFont.fontName = text;
  208. else if ("cap-height".equals(localName))
  209. if (isCID)
  210. multiFont.capHeight = getInt(text);
  211. else
  212. singleFont.capHeight = getInt(text);
  213. else if ("x-height".equals(localName))
  214. if (isCID)
  215. multiFont.xHeight = getInt(text);
  216. else
  217. singleFont.xHeight = getInt(text);
  218. else if ("ascender".equals(localName))
  219. if (isCID)
  220. multiFont.ascender = getInt(text);
  221. else
  222. singleFont.ascender = getInt(text);
  223. else if ("descender".equals(localName))
  224. if (isCID)
  225. multiFont.descender = getInt(text);
  226. else
  227. singleFont.descender = getInt(text);
  228. else if ("left".equals(localName))
  229. if (isCID)
  230. multiFont.fontBBox[0] = getInt(text);
  231. else
  232. singleFont.fontBBox[0] = getInt(text);
  233. else if ("bottom".equals(localName))
  234. if (isCID)
  235. multiFont.fontBBox[1] = getInt(text);
  236. else
  237. singleFont.fontBBox[1] = getInt(text);
  238. else if ("right".equals(localName))
  239. if (isCID)
  240. multiFont.fontBBox[2] = getInt(text);
  241. else
  242. singleFont.fontBBox[2] = getInt(text);
  243. else if ("first-char".equals(localName))
  244. singleFont.firstChar = getInt(text);
  245. else if ("last-char".equals(localName))
  246. singleFont.lastChar = getInt(text);
  247. else if ("top".equals(localName))
  248. if (isCID)
  249. multiFont.fontBBox[3] = getInt(text);
  250. else
  251. singleFont.fontBBox[3] = getInt(text);
  252. else if ("flags".equals(localName))
  253. if (isCID)
  254. multiFont.flags = getInt(text);
  255. else
  256. singleFont.flags = getInt(text);
  257. else if ("stemv".equals(localName))
  258. if (isCID)
  259. multiFont.stemV = getInt(text);
  260. else
  261. singleFont.stemV = getInt(text);
  262. else if ("italic-angle".equals(localName))
  263. if (isCID)
  264. multiFont.italicAngle = getInt(text);
  265. else
  266. singleFont.italicAngle = getInt(text);
  267. else if ("missing-width".equals(localName))
  268. if (isCID)
  269. multiFont.missingWidth = getInt(text);
  270. else
  271. singleFont.missingWidth = getInt(text);
  272. else if ("cid-type".equals(localName)) {
  273. if ("CIDFontType2".equals(text))
  274. multiFont.cidType = PDFCIDFont.CID_TYPE2;
  275. } else if ("default-width".equals(localName)) {
  276. multiFont.defaultWidth = getInt(text);
  277. } else if ("cid-widths".equals(localName)) {
  278. int[] wds = new int[cidWidths.size()];
  279. int j = 0;
  280. for (Enumeration e = cidWidths.elements(); e.hasMoreElements();) {
  281. Integer i = (Integer)e.nextElement();
  282. wds[j++] = i.intValue();
  283. }
  284. multiFont.warray.addEntry(cidWidthIndex, wds);
  285. multiFont.width = wds;
  286. } else if ("bfranges".equals(localName)) {
  287. BFEntry[] entries = new BFEntry[bfranges.size()];
  288. bfranges.copyInto(entries);
  289. multiFont.bfentries = entries;
  290. }
  291. }
  292. public void characters(char[] ch, int start, int length) {
  293. char c[] = new char[length];
  294. System.arraycopy(ch, start, c, 0, length);
  295. text = new String(c);
  296. }
  297. }