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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts.apps;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.Iterator;
  22. import java.util.Map;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.Element;
  26. import org.apache.fop.Version;
  27. import org.apache.fop.fonts.type1.PFMFile;
  28. /**
  29. * A tool which reads PFM files from Adobe Type 1 fonts and creates
  30. * XML font metrics file for use in FOP.
  31. */
  32. public class PFMReader extends AbstractFontReader {
  33. /**
  34. * Main constructor.
  35. */
  36. public PFMReader() {
  37. super();
  38. }
  39. private static void displayUsage() {
  40. System.out.println(
  41. "java " + PFMReader.class.getName() + " [options] metricfile.pfm xmlfile.xml");
  42. System.out.println();
  43. System.out.println("where options can be:");
  44. System.out.println("-d Debug mode");
  45. System.out.println("-q Quiet mode");
  46. System.out.println("-fn <fontname>");
  47. System.out.println(" default is to use the fontname in the .pfm file, but");
  48. System.out.println(" you can override that name to make sure that the");
  49. System.out.println(" embedded font is used (if you're embedding fonts)");
  50. System.out.println(" instead of installed fonts when viewing documents ");
  51. System.out.println(" with Acrobat Reader.");
  52. }
  53. /**
  54. * The main method for the PFM reader tool.
  55. *
  56. * @param args Command-line arguments: [options] metricfile.pfm xmlfile.xml
  57. * where options can be:
  58. * -fn <fontname>
  59. * default is to use the fontname in the .pfm file, but you can override
  60. * that name to make sure that the embedded font is used instead of installed
  61. * fonts when viewing documents with Acrobat Reader.
  62. * -cn <classname>
  63. * default is to use the fontname
  64. * -ef <path to the Type1 .pfb fontfile>
  65. * will add the possibility to embed the font. When running fop, fop will look
  66. * for this file to embed it
  67. * -er <path to Type1 fontfile relative to org/apache/fop/render/pdf/fonts>
  68. * you can also include the fontfile in the fop.jar file when building fop.
  69. * You can use both -ef and -er. The file specified in -ef will be searched first,
  70. * then the -er file.
  71. */
  72. public static void main(String[] args) {
  73. String embFile = null;
  74. String embResource = null;
  75. String className = null;
  76. String fontName = null;
  77. Map options = new java.util.HashMap();
  78. String[] arguments = parseArguments(options, args);
  79. determineLogLevel(options);
  80. PFMReader app = new PFMReader();
  81. log.info("PFM Reader for Apache FOP " + Version.getVersion() + "\n");
  82. if (options.get("-ef") != null) {
  83. embFile = (String)options.get("-ef");
  84. }
  85. if (options.get("-er") != null) {
  86. embResource = (String)options.get("-er");
  87. }
  88. if (options.get("-fn") != null) {
  89. fontName = (String)options.get("-fn");
  90. }
  91. if (options.get("-cn") != null) {
  92. className = (String)options.get("-cn");
  93. }
  94. if (arguments.length != 2 || options.get("-h") != null
  95. || options.get("-help") != null || options.get("--help") != null) {
  96. displayUsage();
  97. } else {
  98. try {
  99. log.info("Parsing font...");
  100. PFMFile pfm = app.loadPFM(arguments[0]);
  101. if (pfm != null) {
  102. app.preview(pfm);
  103. Document doc = app.constructFontXML(pfm,
  104. fontName, className, embResource, embFile);
  105. app.writeFontXML(doc, arguments[1]);
  106. }
  107. log.info("XML font metrics file successfullly created.");
  108. } catch (Exception e) {
  109. log.error("Error while building XML font metrics file", e);
  110. System.exit(-1);
  111. }
  112. }
  113. }
  114. /**
  115. * Read a PFM file and returns it as an object.
  116. *
  117. * @param filename The filename of the PFM file.
  118. * @return The PFM as an object.
  119. * @throws IOException In case of an I/O problem
  120. */
  121. public PFMFile loadPFM(String filename) throws IOException {
  122. log.info("Reading " + filename + "...");
  123. log.info("");
  124. InputStream in = new java.io.FileInputStream(filename);
  125. try {
  126. PFMFile pfm = new PFMFile();
  127. pfm.load(in);
  128. return pfm;
  129. } finally {
  130. in.close();
  131. }
  132. }
  133. /**
  134. * Displays a preview of the PFM file on the console.
  135. *
  136. * @param pfm The PFM file to preview.
  137. */
  138. public void preview(PFMFile pfm) {
  139. if (log != null && log.isInfoEnabled()) {
  140. log.info("Font: " + pfm.getWindowsName());
  141. log.info("Name: " + pfm.getPostscriptName());
  142. log.info("CharSet: " + pfm.getCharSetName());
  143. log.info("CapHeight: " + pfm.getCapHeight());
  144. log.info("XHeight: " + pfm.getXHeight());
  145. log.info("LowerCaseAscent: " + pfm.getLowerCaseAscent());
  146. log.info("LowerCaseDescent: " + pfm.getLowerCaseDescent());
  147. log.info("Having widths for " + (pfm.getLastChar() - pfm.getFirstChar())
  148. + " characters (" + pfm.getFirstChar()
  149. + "-" + pfm.getLastChar() + ").");
  150. log.info("for example: Char " + pfm.getFirstChar()
  151. + " has a width of " + pfm.getCharWidth(pfm.getFirstChar()));
  152. log.info("");
  153. }
  154. }
  155. /**
  156. * Generates the font metrics file from the PFM file.
  157. *
  158. * @param pfm The PFM file to generate the font metrics from.
  159. * @param fontName name of the font
  160. * @param className class name for the font
  161. * @param resource path to the font as embedded resource
  162. * @param file path to the font as file
  163. * @return The DOM document representing the font metrics file.
  164. */
  165. public org.w3c.dom.Document constructFontXML(PFMFile pfm,
  166. String fontName, String className, String resource, String file) {
  167. log.info("Creating xml font file...");
  168. log.info("");
  169. Document doc;
  170. try {
  171. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  172. doc = factory.newDocumentBuilder().newDocument();
  173. } catch (javax.xml.parsers.ParserConfigurationException e) {
  174. log.error("Can't create DOM implementation", e);
  175. return null;
  176. }
  177. Element root = doc.createElement("font-metrics");
  178. doc.appendChild(root);
  179. root.setAttribute("type", "TYPE1");
  180. Element el = doc.createElement("font-name");
  181. root.appendChild(el);
  182. el.appendChild(doc.createTextNode(pfm.getPostscriptName()));
  183. // Currently unused.
  184. // String s = pfm.getPostscriptName();
  185. // int pos = s.indexOf("-");
  186. // if (pos >= 0) {
  187. // char[] sb = new char[s.length() - 1];
  188. // s.getChars(0, pos, sb, 0);
  189. // s.getChars(pos + 1, s.length(), sb, pos);
  190. // s = new String(sb);
  191. // }
  192. el = doc.createElement("embed");
  193. root.appendChild(el);
  194. if (file != null) {
  195. el.setAttribute("file", file);
  196. }
  197. if (resource != null) {
  198. el.setAttribute("class", resource);
  199. }
  200. el = doc.createElement("encoding");
  201. root.appendChild(el);
  202. el.appendChild(doc.createTextNode(pfm.getCharSetName() + "Encoding"));
  203. el = doc.createElement("cap-height");
  204. root.appendChild(el);
  205. Integer value = new Integer(pfm.getCapHeight());
  206. el.appendChild(doc.createTextNode(value.toString()));
  207. el = doc.createElement("x-height");
  208. root.appendChild(el);
  209. value = new Integer(pfm.getXHeight());
  210. el.appendChild(doc.createTextNode(value.toString()));
  211. el = doc.createElement("ascender");
  212. root.appendChild(el);
  213. value = new Integer(pfm.getLowerCaseAscent());
  214. el.appendChild(doc.createTextNode(value.toString()));
  215. el = doc.createElement("descender");
  216. root.appendChild(el);
  217. value = new Integer(pfm.getLowerCaseDescent());
  218. el.appendChild(doc.createTextNode(value.toString()));
  219. Element bbox = doc.createElement("bbox");
  220. root.appendChild(bbox);
  221. int[] bb = pfm.getFontBBox();
  222. final String[] names = {"left", "bottom", "right", "top"};
  223. for (int i = 0; i < names.length; i++) {
  224. el = doc.createElement(names[i]);
  225. bbox.appendChild(el);
  226. value = new Integer(bb[i]);
  227. el.appendChild(doc.createTextNode(value.toString()));
  228. }
  229. el = doc.createElement("flags");
  230. root.appendChild(el);
  231. value = new Integer(pfm.getFlags());
  232. el.appendChild(doc.createTextNode(value.toString()));
  233. el = doc.createElement("stemv");
  234. root.appendChild(el);
  235. value = new Integer(pfm.getStemV());
  236. el.appendChild(doc.createTextNode(value.toString()));
  237. el = doc.createElement("italicangle");
  238. root.appendChild(el);
  239. value = new Integer(pfm.getItalicAngle());
  240. el.appendChild(doc.createTextNode(value.toString()));
  241. el = doc.createElement("first-char");
  242. root.appendChild(el);
  243. value = new Integer(pfm.getFirstChar());
  244. el.appendChild(doc.createTextNode(value.toString()));
  245. el = doc.createElement("last-char");
  246. root.appendChild(el);
  247. value = new Integer(pfm.getLastChar());
  248. el.appendChild(doc.createTextNode(value.toString()));
  249. Element widths = doc.createElement("widths");
  250. root.appendChild(widths);
  251. for (short i = pfm.getFirstChar(); i <= pfm.getLastChar(); i++) {
  252. el = doc.createElement("char");
  253. widths.appendChild(el);
  254. el.setAttribute("idx", Integer.toString(i));
  255. el.setAttribute("wdt", Integer.toString(pfm.getCharWidth(i)));
  256. }
  257. // Get kerning
  258. Iterator iter = pfm.getKerning().keySet().iterator();
  259. while (iter.hasNext()) {
  260. Integer kpx1 = (Integer)iter.next();
  261. el = doc.createElement("kerning");
  262. el.setAttribute("kpx1", kpx1.toString());
  263. root.appendChild(el);
  264. Element el2 = null;
  265. Map h2 = (Map) pfm.getKerning().get(kpx1);
  266. Iterator enum2 = h2.entrySet().iterator();
  267. while (enum2.hasNext()) {
  268. Map.Entry entry = (Map.Entry) enum2.next();
  269. Integer kpx2 = (Integer) entry.getKey();
  270. el2 = doc.createElement("pair");
  271. el2.setAttribute("kpx2", kpx2.toString());
  272. Integer val = (Integer) entry.getValue();
  273. el2.setAttribute("kern", val.toString());
  274. el.appendChild(el2);
  275. }
  276. }
  277. return doc;
  278. }
  279. }