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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * True Type 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. * Constructor - initialises the PSGenerator in this wrapper class.
  37. * @param gen PSGenerator
  38. */
  39. public PSTTFGenerator(PSGenerator gen) {
  40. this.gen = gen;
  41. hexOut = new ASCIIHexOutputStream(gen.getOutputStream());
  42. }
  43. /**
  44. * Begins writing a string by writing '<' to the begin.
  45. * @throws IOException file write exception.
  46. */
  47. public void startString() throws IOException {
  48. // We need to reset the streamer so that it starts a new line in the PS document
  49. hexOut = new ASCIIHexOutputStream(gen.getOutputStream());
  50. gen.writeln("<");
  51. }
  52. /**
  53. * Streams a string to a PostScript document (wraps PSGenerator.write(String)).
  54. * @param cmd String
  55. * @throws IOException file write exception
  56. */
  57. public void write(String cmd) throws IOException {
  58. gen.write(cmd);
  59. }
  60. /**
  61. * Streams a string followed by a new line char to a PostScript document (wraps
  62. * PSGenerator.writeln(String)).
  63. * @param cmd String
  64. * @throws IOException file write exception
  65. */
  66. public void writeln(String cmd) throws IOException {
  67. gen.writeln(cmd);
  68. }
  69. /**
  70. * Streams the bytes.
  71. * @param byteArray byte[] the byte array to stream to file.
  72. * @param offset int the starting position in the byte array to stream to file.
  73. * @param length the number of bytes to stream to file. This MUST be less than
  74. * MAX_BUFFER_SIZE - 1 since strings are suffixed by '00' (as in spec).
  75. * @throws IOException file write exception
  76. */
  77. public void streamBytes(byte[] byteArray, int offset, int length) throws IOException {
  78. if (length > MAX_BUFFER_SIZE) {
  79. throw new UnsupportedOperationException("Attempting to write a string to a PostScript"
  80. + " file that is greater than the buffer size.");
  81. }
  82. hexOut.write(byteArray, offset, length);
  83. }
  84. /**
  85. * Finishes writing a string by appending '00' and '>' to the end.
  86. * @throws IOException file write exception
  87. */
  88. public void endString() throws IOException {
  89. /* Appends a '00' to the end of the string as specified in the spec */
  90. gen.write("00\n> ");
  91. }
  92. }