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.

FontPatternExtractor.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.afp.apps;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.DataInputStream;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.io.PrintStream;
  26. import org.apache.commons.io.HexDump;
  27. import org.apache.commons.io.IOUtils;
  28. import org.apache.commons.io.output.ByteArrayOutputStream;
  29. import org.apache.fop.afp.parser.MODCAParser;
  30. import org.apache.fop.afp.parser.UnparsedStructuredField;
  31. /**
  32. * This class represents a tool for extracting the Type 1 PFB file from an AFP outline font.
  33. */
  34. public class FontPatternExtractor {
  35. private PrintStream printStream = System.out;
  36. /**
  37. * Extracts the Type1 PFB file from the given AFP outline font.
  38. * @param file the AFP file to read from
  39. * @param targetDir the target directory where the PFB file is to be placed.
  40. * @throws IOException if an I/O error occurs
  41. */
  42. public void extract(File file, File targetDir) throws IOException {
  43. InputStream in = new java.io.FileInputStream(file);
  44. try {
  45. MODCAParser parser = new MODCAParser(in);
  46. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  47. UnparsedStructuredField strucField;
  48. while ((strucField = parser.readNextStructuredField()) != null) {
  49. if (strucField.getSfTypeID() == 0xD3EE89) {
  50. byte[] sfData = strucField.getData();
  51. println(strucField.toString());
  52. HexDump.dump(sfData, 0, printStream, 0);
  53. baout.write(sfData);
  54. }
  55. }
  56. ByteArrayInputStream bin = new ByteArrayInputStream(baout.toByteArray());
  57. IOUtils.closeQuietly(baout);
  58. DataInputStream din = new DataInputStream(bin);
  59. long len = din.readInt() & 0xFFFFFFFFL;
  60. println("Length: " + len);
  61. if (din.skip(4) != 4) { //checksum
  62. throw new IOException("premature EOF when skipping checksum");
  63. }
  64. int tidLen = din.readUnsignedShort() - 2;
  65. byte[] tid = new byte[tidLen];
  66. din.readFully(tid);
  67. String filename = new String(tid, "ISO-8859-1");
  68. int asciiCount1 = countUSAsciiCharacters(filename);
  69. String filenameEBCDIC = new String(tid, "Cp1146");
  70. int asciiCount2 = countUSAsciiCharacters(filenameEBCDIC);
  71. println("TID: " + filename + " " + filenameEBCDIC);
  72. if (asciiCount2 > asciiCount1) {
  73. //Haven't found an indicator if the name is encoded in EBCDIC or not
  74. //so we use a trick.
  75. filename = filenameEBCDIC;
  76. }
  77. if (!filename.toLowerCase().endsWith(".pfb")) {
  78. filename = filename + ".pfb";
  79. }
  80. println("Output filename: " + filename);
  81. File out = new File(targetDir, filename);
  82. OutputStream fout = new java.io.FileOutputStream(out);
  83. try {
  84. IOUtils.copyLarge(din, fout);
  85. } finally {
  86. IOUtils.closeQuietly(fout);
  87. }
  88. } finally {
  89. IOUtils.closeQuietly(in);
  90. }
  91. }
  92. private void println(String s) {
  93. printStream.println(s);
  94. }
  95. private void println() {
  96. printStream.println();
  97. }
  98. private int countUSAsciiCharacters(String filename) {
  99. int count = 0;
  100. for (int i = 0, c = filename.length(); i < c; i++) {
  101. if (filename.charAt(i) < 128) {
  102. count++;
  103. }
  104. }
  105. return count;
  106. }
  107. /**
  108. * Main method
  109. * @param args the command-line arguments
  110. */
  111. public static void main(String[] args) {
  112. try {
  113. FontPatternExtractor app = new FontPatternExtractor();
  114. app.println("Font Pattern Extractor");
  115. app.println();
  116. if (args.length > 0) {
  117. String filename = args[0];
  118. File file = new File(filename);
  119. File targetDir = file.getParentFile();
  120. if (args.length > 1) {
  121. targetDir = new File(args[1]);
  122. targetDir.mkdirs();
  123. }
  124. app.extract(file, targetDir);
  125. } else {
  126. app.println("This tool tries to extract the PFB file from an AFP outline font.");
  127. app.println();
  128. app.println("Usage: Java -cp ... " + FontPatternExtractor.class.getName()
  129. + " <afp-font-file> [<target-dir>]");
  130. System.exit(-1);
  131. }
  132. } catch (Exception e) {
  133. e.printStackTrace();
  134. System.exit(-1);
  135. }
  136. }
  137. }