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.

PSTTFGenerator.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.render.ps.fonts;
  19. import java.io.IOException;
  20. import org.apache.xmlgraphics.ps.PSGenerator;
  21. import org.apache.xmlgraphics.util.io.ASCIIHexOutputStream;
  22. /**
  23. * This is a wrapper for {@link PSGenerator} that contains some members specific for streaming
  24. * TrueType fonts to a PostScript document.
  25. */
  26. public class PSTTFGenerator {
  27. private PSGenerator gen;
  28. private ASCIIHexOutputStream hexOut;
  29. /**
  30. * The buffer is used to store the font file in an array of hex-encoded strings. Strings are
  31. * limited to 65535 characters, string will start with a newline, 2 characters are needed to
  32. * hex-encode each byte.
  33. */
  34. public static final int MAX_BUFFER_SIZE = 32764;
  35. /**
  36. * Creates a new instance wrapping the given generator.
  37. * @param gen the PSGenerator to wrap
  38. */
  39. public PSTTFGenerator(PSGenerator gen) {
  40. this.gen = gen;
  41. hexOut = new ASCIIHexOutputStream(gen.getOutputStream());
  42. }
  43. /**
  44. * Writes the '<' character that starts a string.
  45. */
  46. public void startString() throws IOException {
  47. // We need to reset the streamer so that it starts a new line in the PS document
  48. hexOut = new ASCIIHexOutputStream(gen.getOutputStream());
  49. gen.writeln("<");
  50. }
  51. /**
  52. * Writes the given string to the output.
  53. * @param cmd a string
  54. */
  55. public void write(String cmd) throws IOException {
  56. gen.write(cmd);
  57. }
  58. /**
  59. * Writes the given string to the output, followed by a newline.
  60. * @param cmd a string
  61. */
  62. public void writeln(String cmd) throws IOException {
  63. gen.writeln(cmd);
  64. }
  65. /**
  66. * Writes bytes from the given byte array to the output.
  67. *
  68. * @param byteArray byte[] a byte array
  69. * @param offset the position in the byte array where the streaming must start
  70. * @param length the number of bytes to stream. This MUST be less than
  71. * {@link #MAX_BUFFER_SIZE} - 1 since strings are suffixed by '00' (see Section 4.2 of
  72. * Adobe Technical Note #5012, <em>The Type 42 Font Format Specification</em>.).
  73. */
  74. public void streamBytes(byte[] byteArray, int offset, int length) throws IOException {
  75. if (length > MAX_BUFFER_SIZE) {
  76. throw new UnsupportedOperationException("Attempting to write a string to a PostScript"
  77. + " file that is greater than the buffer size.");
  78. }
  79. hexOut.write(byteArray, offset, length);
  80. }
  81. /**
  82. * Finishes writing a string by appending '00' and '>' to the end.
  83. */
  84. public void endString() throws IOException {
  85. /* Appends a '00' to the end of the string as specified in the spec */
  86. gen.write("00\n> ");
  87. }
  88. }