選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FontPatternExtractor.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. din.skip(4); //checksum
  62. int tidLen = din.readUnsignedShort() - 2;
  63. byte[] tid = new byte[tidLen];
  64. din.readFully(tid);
  65. String filename = new String(tid, "ISO-8859-1");
  66. int asciiCount1 = countUSAsciiCharacters(filename);
  67. String filenameEBCDIC = new String(tid, "Cp1146");
  68. int asciiCount2 = countUSAsciiCharacters(filenameEBCDIC);
  69. println("TID: " + filename + " " + filenameEBCDIC);
  70. if (asciiCount2 > asciiCount1) {
  71. //Haven't found an indicator if the name is encoded in EBCDIC or not
  72. //so we use a trick.
  73. filename = filenameEBCDIC;
  74. }
  75. if (!filename.toLowerCase().endsWith(".pfb")) {
  76. filename = filename + ".pfb";
  77. }
  78. println("Output filename: " + filename);
  79. File out = new File(targetDir, filename);
  80. OutputStream fout = new java.io.FileOutputStream(out);
  81. try {
  82. IOUtils.copyLarge(din, fout);
  83. } finally {
  84. IOUtils.closeQuietly(fout);
  85. }
  86. } finally {
  87. IOUtils.closeQuietly(in);
  88. }
  89. }
  90. private void println(String s) {
  91. printStream.println(s);
  92. }
  93. private void println() {
  94. printStream.println();
  95. }
  96. private int countUSAsciiCharacters(String filename) {
  97. int count = 0;
  98. for (int i = 0, c = filename.length(); i < c; i++) {
  99. if (filename.charAt(i) < 128) {
  100. count++;
  101. }
  102. }
  103. return count;
  104. }
  105. /**
  106. * Main method
  107. * @param args the command-line arguments
  108. */
  109. public static void main(String[] args) {
  110. try {
  111. FontPatternExtractor app = new FontPatternExtractor();
  112. app.println("Font Pattern Extractor");
  113. app.println();
  114. if (args.length > 0) {
  115. String filename = args[0];
  116. File file = new File(filename);
  117. File targetDir = file.getParentFile();
  118. if (args.length > 1) {
  119. targetDir = new File(args[1]);
  120. targetDir.mkdirs();
  121. }
  122. app.extract(file, targetDir);
  123. } else {
  124. app.println("This tool tries to extract the PFB file from an AFP outline font.");
  125. app.println();
  126. app.println("Usage: Java -cp ... " + FontPatternExtractor.class.getName()
  127. + " <afp-font-file> [<target-dir>]");
  128. System.exit(-1);
  129. }
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. System.exit(-1);
  133. }
  134. }
  135. }