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.

PFMReader.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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.fonts.apps;
  41. import java.io.*;
  42. import org.w3c.dom.*;
  43. import org.apache.xerces.dom.*;
  44. import org.apache.xml.serialize.*;
  45. import org.apache.xalan.xslt.*;
  46. import org.apache.fop.fonts.*;
  47. import java.util.Hashtable;
  48. import java.util.Vector;
  49. import java.util.Enumeration;
  50. /**
  51. * A tool which reads PFM files from Adobe Type 1 fonts and creates
  52. * XML font metrics file for use in FOP.
  53. *
  54. * @author jeremias.maerki@outline.ch
  55. */
  56. public class PFMReader {
  57. private boolean invokedStandalone = false;
  58. public PFMReader() {
  59. }
  60. /**
  61. * Parse commandline arguments. put options in the Hashtable and return
  62. * arguments in the String array
  63. * the arguments: -fn Perpetua,Bold -cn PerpetuaBold per.ttf Perpetua.xml
  64. * returns a String[] with the per.ttf and Perpetua.xml. The hash
  65. * will have the (key, value) pairs: (-fn, Perpetua) and (-cn, PerpetuaBold)
  66. */
  67. private static String[] parseArguments(Hashtable options, String[] args) {
  68. Vector arguments=new Vector();
  69. for (int i=0; i < args.length; i++) {
  70. if (args[i].startsWith("-")) {
  71. if ((i+1) < args.length && !args[i+1].startsWith("-")) {
  72. options.put(args[i], args[i+1]);
  73. i++;
  74. } else {
  75. options.put(args[i], "");
  76. }
  77. } else {
  78. arguments.addElement(args[i]);
  79. }
  80. }
  81. String[] argStrings=new String[arguments.size()];
  82. arguments.copyInto(argStrings);
  83. return argStrings;
  84. }
  85. private final static void displayUsage() {
  86. System.out.println(" java org.apache.fop.fonts.apps.PFMReader [options] metricfile.pfm xmlfile.xml\n");
  87. System.out.println(" where options can be:\n");
  88. System.out.println(" -fn <fontname>\n");
  89. System.out.println(" default is to use the fontname in the .ttf file, but\n"+
  90. " you can override that name to make sure that the\n");
  91. System.out.println(" embedded font is used (if you're embedding fonts)\n");
  92. System.out.println(" instead of installed fonts when viewing documents with Acrobat Reader.\n");
  93. }
  94. /**
  95. * The main method for the PFM reader tool.
  96. *
  97. * @param args Command-line arguments: [options] metricfile.pfm xmlfile.xml
  98. * where options can be:
  99. * -fn <fontname>
  100. * default is to use the fontname in the .pfm file, but you can override
  101. * that name to make sure that the embedded font is used instead of installed
  102. * fonts when viewing documents with Acrobat Reader.
  103. * -cn <classname>
  104. * default is to use the fontname
  105. * -ef <path to the Type1 .pfb fontfile>
  106. * will add the possibility to embed the font. When running fop, fop will look
  107. * for this file to embed it
  108. * -er <path to Type1 fontfile relative to org/apache/fop/render/pdf/fonts>
  109. * you can also include the fontfile in the fop.jar file when building fop.
  110. * You can use both -ef and -er. The file specified in -ef will be searched first,
  111. * then the -er file.
  112. */
  113. public static void main(String[] args) {
  114. String embFile=null;
  115. String embResource=null;
  116. String className=null;
  117. String fontName=null;
  118. Hashtable options=new Hashtable();
  119. String[] arguments=parseArguments(options, args);
  120. PFMReader app = new PFMReader();
  121. app.invokedStandalone = true;
  122. System.out.println("PFM Reader v1.1");
  123. System.out.println();
  124. if (options.get("-ef") != null)
  125. embFile=(String)options.get("-ef");
  126. if (options.get("-er") != null)
  127. embResource=(String)options.get("-er");
  128. if (options.get("-fn") != null)
  129. fontName=(String)options.get("-fn");
  130. if (options.get("-cn") != null)
  131. className=(String)options.get("-cn");
  132. if (arguments.length != 2 ||
  133. options.get("-h") != null ||
  134. options.get("-help") != null ||
  135. options.get("--help") != null)
  136. displayUsage();
  137. else {
  138. PFMFile pfm = app.loadPFM(arguments[0]);
  139. if (pfm != null) {
  140. app.preview(pfm);
  141. org.w3c.dom.Document doc = app.constructFontXML(pfm,
  142. fontName,
  143. className,
  144. embResource,
  145. embFile);
  146. app.writeFontXML(doc, arguments[1]);
  147. }
  148. }
  149. }
  150. /**
  151. * Read a PFM file and returns it as an object.
  152. *
  153. * @param filename The filename of the PFM file.
  154. * @return The PFM as an object.
  155. */
  156. public PFMFile loadPFM(String filename) {
  157. try {
  158. System.out.println("Reading " + filename + "...");
  159. System.out.println();
  160. FileInputStream in = new FileInputStream(filename);
  161. PFMFile pfm = new PFMFile();
  162. pfm.load(in);
  163. return pfm;
  164. } catch (Exception e) {
  165. e.printStackTrace();
  166. return null;
  167. }
  168. }
  169. /**
  170. * Displays a preview of the PFM file on the console.
  171. *
  172. * @param pfm The PFM file to preview.
  173. */
  174. public void preview(PFMFile pfm) {
  175. PrintStream out = System.out;
  176. out.print("Font: ");
  177. out.println(pfm.getWindowsName());
  178. out.print("Name: ");
  179. out.println(pfm.getPostscriptName());
  180. out.print("CharSet: ");
  181. out.println(pfm.getCharSetName());
  182. out.print("CapHeight: ");
  183. out.println(pfm.getCapHeight());
  184. out.print("XHeight: ");
  185. out.println(pfm.getXHeight());
  186. out.print("LowerCaseAscent: ");
  187. out.println(pfm.getLowerCaseAscent());
  188. out.print("LowerCaseDescent: ");
  189. out.println(pfm.getLowerCaseDescent());
  190. out.print("Having widths for ");
  191. out.print(pfm.getLastChar()-pfm.getFirstChar());
  192. out.print(" characters (");
  193. out.print(pfm.getFirstChar());
  194. out.print("-");
  195. out.print(pfm.getLastChar());
  196. out.println(").");
  197. out.print("for example: Char ");
  198. out.print(pfm.getFirstChar());
  199. out.print(" has a width of ");
  200. out.println(pfm.getCharWidth(pfm.getFirstChar()));
  201. out.println();
  202. }
  203. /**
  204. * Writes the generated DOM Document to a file.
  205. *
  206. * @param doc The DOM Document to save.
  207. * @param target The target filename for the XML file.
  208. */
  209. public void writeFontXML(org.w3c.dom.Document doc, String target) {
  210. System.out.println("Writing xml font file " + target + "...");
  211. System.out.println();
  212. try {
  213. OutputFormat format = new OutputFormat(doc); //Serialize DOM
  214. FileWriter out = new FileWriter(target); //Writer will be a String
  215. XMLSerializer serial = new XMLSerializer(out, format);
  216. serial.asDOMSerializer(); // As a DOM Serializer
  217. serial.serialize(doc.getDocumentElement());
  218. out.close();
  219. } catch (Exception e) {
  220. e.printStackTrace();
  221. }
  222. }
  223. /**
  224. * Generates the font metrics file from the PFM file.
  225. *
  226. * @param pfm The PFM file to generate the font metrics from.
  227. * @return The DOM document representing the font metrics file.
  228. */
  229. public org.w3c.dom.Document constructFontXML(PFMFile pfm, String fontName,
  230. String className, String resource,
  231. String file) {
  232. System.out.println("Creating xml font file...");
  233. System.out.println();
  234. Document doc = new DocumentImpl();
  235. Element root = doc.createElement("font-metrics");
  236. doc.appendChild(root);
  237. root.setAttribute("type", "TYPE1");
  238. Element el = doc.createElement("font-name");
  239. root.appendChild(el);
  240. el.appendChild(doc.createTextNode(pfm.getPostscriptName()));
  241. String s = pfm.getPostscriptName();
  242. int pos = s.indexOf("-");
  243. if (pos >= 0) {
  244. char sb[] = new char[s.length() - 1];
  245. s.getChars(0, pos, sb, 0);
  246. s.getChars(pos + 1, s.length(), sb, pos);
  247. s = new String(sb);
  248. }
  249. el = doc.createElement("embed");
  250. root.appendChild(el);
  251. if (file!=null)
  252. el.setAttribute("file", file);
  253. if (resource!=null)
  254. el.setAttribute("class", resource);
  255. el = doc.createElement("encoding");
  256. root.appendChild(el);
  257. el.appendChild(doc.createTextNode(pfm.getCharSetName()+"Encoding"));
  258. el = doc.createElement("cap-height");
  259. root.appendChild(el);
  260. Integer value = new Integer(pfm.getCapHeight());
  261. el.appendChild(doc.createTextNode(value.toString()));
  262. el = doc.createElement("x-height");
  263. root.appendChild(el);
  264. value = new Integer(pfm.getXHeight());
  265. el.appendChild(doc.createTextNode(value.toString()));
  266. el = doc.createElement("ascender");
  267. root.appendChild(el);
  268. value = new Integer(pfm.getLowerCaseAscent());
  269. el.appendChild(doc.createTextNode(value.toString()));
  270. el = doc.createElement("descender");
  271. root.appendChild(el);
  272. value = new Integer(-pfm.getLowerCaseDescent());
  273. el.appendChild(doc.createTextNode(value.toString()));
  274. Element bbox = doc.createElement("bbox");
  275. root.appendChild(bbox);
  276. int[] bb = pfm.getFontBBox();
  277. String[] names = {"left","bottom","right","top"};
  278. for (int i=0; i<4; i++) {
  279. el = doc.createElement(names[i]);
  280. bbox.appendChild(el);
  281. value = new Integer(bb[i]);
  282. el.appendChild(doc.createTextNode(value.toString()));
  283. }
  284. el = doc.createElement("flags");
  285. root.appendChild(el);
  286. value = new Integer(pfm.getFlags());
  287. el.appendChild(doc.createTextNode(value.toString()));
  288. el = doc.createElement("stemv");
  289. root.appendChild(el);
  290. value = new Integer(pfm.getStemV());
  291. el.appendChild(doc.createTextNode(value.toString()));
  292. el = doc.createElement("italicangle");
  293. root.appendChild(el);
  294. value = new Integer(pfm.getItalicAngle());
  295. el.appendChild(doc.createTextNode(value.toString()));
  296. el = doc.createElement("first-char");
  297. root.appendChild(el);
  298. value = new Integer(pfm.getFirstChar());
  299. el.appendChild(doc.createTextNode(value.toString()));
  300. el = doc.createElement("last-char");
  301. root.appendChild(el);
  302. value = new Integer(pfm.getLastChar());
  303. el.appendChild(doc.createTextNode(value.toString()));
  304. Element widths = doc.createElement("widths");
  305. root.appendChild(widths);
  306. for (short i = pfm.getFirstChar(); i < pfm.getLastChar(); i++) {
  307. el = doc.createElement("char");
  308. widths.appendChild(el);
  309. el.setAttribute("idx", Integer.toString(i));
  310. el.setAttribute("wdt", new Integer(pfm.getCharWidth(i)).toString());
  311. }
  312. // Get kerning
  313. for (Enumeration enum=pfm.getKerning().keys(); enum.hasMoreElements();) {
  314. String kpx1=(String)enum.nextElement();
  315. el=doc.createElement("kerning");
  316. el.setAttribute("kpx1", kpx1);
  317. root.appendChild(el);
  318. Element el2=null;
  319. Hashtable h2=(Hashtable)pfm.getKerning().get(kpx1);
  320. for (Enumeration enum2=h2.keys(); enum2.hasMoreElements(); ) {
  321. Integer kpx2=(Integer)enum2.nextElement();
  322. el2=doc.createElement("pair");
  323. el2.setAttribute("kpx2", kpx2.toString());
  324. Integer val=(Integer)h2.get(kpx2);
  325. el2.setAttribute("kern", val.toString());
  326. el.appendChild(el2);
  327. }
  328. }
  329. return doc;
  330. }
  331. private String escapeString(String str) {
  332. StringBuffer esc=new StringBuffer();
  333. for (int i=0; i < str.length(); i++) {
  334. if (str.charAt(i)=='\\')
  335. esc.append("\\\\");
  336. else
  337. esc.append(str.charAt(i));
  338. }
  339. return esc.toString();
  340. }
  341. }