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.

CMapBuilder.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.pdf;
  19. import java.io.IOException;
  20. import java.io.Writer;
  21. /** A cmap builder. */
  22. public class CMapBuilder {
  23. /** name */
  24. protected String name;
  25. /** writer */
  26. protected Writer writer;
  27. /**
  28. * Construct cmap builder.
  29. * @param writer a writer
  30. * @param name a name
  31. */
  32. public CMapBuilder(Writer writer, String name) {
  33. this.writer = writer;
  34. this.name = name;
  35. }
  36. /**
  37. * Writes the CMap to a Writer.
  38. * @throws IOException if an I/O error occurs
  39. */
  40. public void writeCMap() throws IOException {
  41. writePreStream();
  42. writeStreamComments();
  43. writeCIDInit();
  44. writeCIDSystemInfo();
  45. writeVersion("1");
  46. writeType("1");
  47. writeName(name);
  48. writeCodeSpaceRange();
  49. writeCIDRange();
  50. writeBFEntries();
  51. writeWrapUp();
  52. writeStreamAfterComments();
  53. writeUseCMap();
  54. }
  55. /** @throws IOException if i/o exception */
  56. protected void writePreStream() throws IOException {
  57. // writer.write("/Type /CMap\n");
  58. // writer.write(sysInfo.toPDFString());
  59. // writer.write("/CMapName /" + name + EOL);
  60. }
  61. /** @throws IOException if i/o exception */
  62. protected void writeStreamComments() throws IOException {
  63. writer.write("%!PS-Adobe-3.0 Resource-CMap\n");
  64. writer.write("%%DocumentNeededResources: ProcSet (CIDInit)\n");
  65. writer.write("%%IncludeResource: ProcSet (CIDInit)\n");
  66. writer.write("%%BeginResource: CMap (" + name + ")\n");
  67. writer.write("%%EndComments\n");
  68. }
  69. /** @throws IOException if i/o exception */
  70. protected void writeCIDInit() throws IOException {
  71. writer.write("/CIDInit /ProcSet findresource begin\n");
  72. writer.write("12 dict begin\n");
  73. writer.write("begincmap\n");
  74. }
  75. /**
  76. * @param registry string
  77. * @param ordering string
  78. * @param supplement string
  79. * @throws IOException if i/o exception
  80. */
  81. protected void writeCIDSystemInfo(String registry, String ordering, int supplement)
  82. throws IOException {
  83. writer.write("/CIDSystemInfo 3 dict dup begin\n");
  84. writer.write(" /Registry (");
  85. writer.write(registry);
  86. writer.write(") def\n");
  87. writer.write(" /Ordering (");
  88. writer.write(ordering);
  89. writer.write(") def\n");
  90. writer.write(" /Supplement ");
  91. writer.write(Integer.toString(supplement));
  92. writer.write(" def\n");
  93. writer.write("end def\n");
  94. }
  95. /** @throws IOException if i/o exception */
  96. protected void writeCIDSystemInfo() throws IOException {
  97. writeCIDSystemInfo("Adobe", "Identity", 0);
  98. }
  99. /**
  100. * @param version a version
  101. * @throws IOException if i/o exception
  102. */
  103. protected void writeVersion(String version) throws IOException {
  104. writer.write("/CMapVersion ");
  105. writer.write(version);
  106. writer.write(" def\n");
  107. }
  108. /**
  109. * @param type a type
  110. * @throws IOException if i/o exception
  111. */
  112. protected void writeType(String type) throws IOException {
  113. writer.write("/CMapType ");
  114. writer.write(type);
  115. writer.write(" def\n");
  116. }
  117. /**
  118. * @param name a name
  119. * @throws IOException if i/o exception
  120. */
  121. protected void writeName(String name) throws IOException {
  122. writer.write("/CMapName /");
  123. writer.write(name);
  124. writer.write(" def\n");
  125. }
  126. /** @throws IOException if i/o exception */
  127. protected void writeCodeSpaceRange() throws IOException {
  128. writeCodeSpaceRange(false);
  129. }
  130. /**
  131. * @param singleByte true if single byte range
  132. * @throws IOException if i/o exception
  133. */
  134. protected void writeCodeSpaceRange(boolean singleByte) throws IOException {
  135. writer.write("1 begincodespacerange\n");
  136. if (singleByte) {
  137. writer.write("<00> <FF>\n");
  138. } else {
  139. writer.write("<0000> <FFFF>\n");
  140. }
  141. writer.write("endcodespacerange\n");
  142. }
  143. /** @throws IOException if i/o exception */
  144. protected void writeCIDRange() throws IOException {
  145. writer.write("1 begincidrange\n");
  146. writer.write("<0000> <FFFF> 0\n");
  147. writer.write("endcidrange\n");
  148. }
  149. /** @throws IOException if i/o exception */
  150. protected void writeBFEntries() throws IOException {
  151. // writer.write("1 beginbfrange\n");
  152. // writer.write("<0020> <0100> <0000>\n");
  153. // writer.write("endbfrange\n");
  154. }
  155. /** @throws IOException if i/o exception */
  156. protected void writeWrapUp() throws IOException {
  157. writer.write("endcmap\n");
  158. writer.write("CMapName currentdict /CMap defineresource pop\n");
  159. writer.write("end\n");
  160. writer.write("end\n");
  161. }
  162. /** @throws IOException if i/o exception */
  163. protected void writeStreamAfterComments() throws IOException {
  164. writer.write("%%EndResource\n");
  165. writer.write("%%EOF\n");
  166. }
  167. /** does nothing */
  168. protected void writeUseCMap() {
  169. /*
  170. * writer.write(" /Type /CMap");
  171. * writer.write("/CMapName /" + name + EOL);
  172. * writer.write("/WMode " + wMode + EOL);
  173. * if (base != null) {
  174. * writer.write("/UseCMap ");
  175. * if (base instanceof String) {
  176. * writer.write("/"+base);
  177. * } else {// base instanceof PDFStream
  178. * writer.write(((PDFStream)base).referencePDF());
  179. * }
  180. * }
  181. */
  182. }
  183. }