Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PSRenderingUtil.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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;
  19. import java.io.IOException;
  20. import java.io.LineNumberReader;
  21. import java.util.Collection;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import org.apache.xmlgraphics.ps.PSGenerator;
  25. import org.apache.fop.apps.FOUserAgent;
  26. import org.apache.fop.render.ps.extensions.PSCommentAfter;
  27. import org.apache.fop.render.ps.extensions.PSCommentBefore;
  28. import org.apache.fop.render.ps.extensions.PSExtensionAttachment;
  29. import org.apache.fop.render.ps.extensions.PSSetupCode;
  30. import static org.apache.fop.render.ps.PSRendererConfigurationOption.AUTO_ROTATE_LANDSCAPE;
  31. import static org.apache.fop.render.ps.PSRendererConfigurationOption.LANGUAGE_LEVEL;
  32. import static org.apache.fop.render.ps.PSRendererConfigurationOption.OPTIMIZE_RESOURCES;
  33. /**
  34. * Utility class which enables all sorts of features that are not directly connected to the
  35. * normal rendering process.
  36. */
  37. public class PSRenderingUtil {
  38. private FOUserAgent userAgent;
  39. /** Whether or not the safe set page device macro will be used or not */
  40. private boolean safeSetPageDevice = false;
  41. /**
  42. * Whether or not PostScript Document Structuring Conventions (DSC) compliant output are
  43. * enforced.
  44. */
  45. private boolean dscCompliant = true;
  46. private boolean autoRotateLandscape = false;
  47. private int languageLevel = PSGenerator.DEFAULT_LANGUAGE_LEVEL;
  48. /** Determines whether the PS file is generated in two passes to minimize file size */
  49. private boolean optimizeResources = false;
  50. /**
  51. * Determines whether the generated PostScript code is optimized for minimum file size
  52. * of best quality.
  53. */
  54. private PSRenderingMode renderingMode = PSRenderingMode.QUALITY;
  55. PSRenderingUtil(FOUserAgent userAgent) {
  56. this.userAgent = userAgent;
  57. //PSRendererConfig confi = userAgent.getRendererConfig(rendererConfiguration, configCreator)
  58. initialize();
  59. }
  60. private void initialize() {
  61. Object obj;
  62. obj = userAgent.getRendererOptions().get(AUTO_ROTATE_LANDSCAPE.getName());
  63. if (obj != null) {
  64. setAutoRotateLandscape(booleanValueOf(obj));
  65. }
  66. obj = userAgent.getRendererOptions().get(LANGUAGE_LEVEL.getName());
  67. if (obj != null) {
  68. setLanguageLevel(intValueOf(obj));
  69. }
  70. obj = userAgent.getRendererOptions().get(OPTIMIZE_RESOURCES.getName());
  71. if (obj != null) {
  72. setOptimizeResources(booleanValueOf(obj));
  73. }
  74. }
  75. private boolean booleanValueOf(Object obj) {
  76. if (obj instanceof Boolean) {
  77. return ((Boolean)obj).booleanValue();
  78. } else if (obj instanceof String) {
  79. return Boolean.valueOf((String)obj).booleanValue();
  80. } else {
  81. throw new IllegalArgumentException("Boolean or \"true\" or \"false\" expected.");
  82. }
  83. }
  84. private int intValueOf(Object obj) {
  85. if (obj instanceof Integer) {
  86. return ((Integer)obj).intValue();
  87. } else if (obj instanceof String) {
  88. return Integer.parseInt((String)obj);
  89. } else {
  90. throw new IllegalArgumentException("Integer or String with a number expected.");
  91. }
  92. }
  93. /**
  94. * Formats and writes a List of PSSetupCode instances to the output stream.
  95. * @param gen the PS generator
  96. * @param setupCodeList a List of PSSetupCode instances
  97. * @param type the type of code section
  98. * @throws IOException if an I/O error occurs.
  99. */
  100. public static void writeSetupCodeList(PSGenerator gen, List setupCodeList, String type)
  101. throws IOException {
  102. if (setupCodeList != null) {
  103. Iterator i = setupCodeList.iterator();
  104. while (i.hasNext()) {
  105. PSSetupCode setupCode = (PSSetupCode)i.next();
  106. gen.commentln("%FOPBegin" + type + ": ("
  107. + (setupCode.getName() != null ? setupCode.getName() : "")
  108. + ")");
  109. LineNumberReader reader = new LineNumberReader(
  110. new java.io.StringReader(setupCode.getContent()));
  111. String line;
  112. while ((line = reader.readLine()) != null) {
  113. line = line.trim();
  114. if (line.length() > 0) {
  115. gen.writeln(line.trim());
  116. }
  117. }
  118. gen.commentln("%FOPEnd" + type);
  119. i.remove();
  120. }
  121. }
  122. }
  123. /**
  124. * Formats and writes a Collection of PSExtensionAttachment instances to
  125. * the output stream. The instances are removed from the collection when they
  126. * have been written.
  127. *
  128. * @param gen the PS generator
  129. * @param attachmentCollection
  130. * a Collection of PSExtensionAttachment instances
  131. * @throws IOException if an I/O error occurs.
  132. */
  133. public static void writeEnclosedExtensionAttachments(PSGenerator gen,
  134. Collection attachmentCollection) throws IOException {
  135. Iterator iter = attachmentCollection.iterator();
  136. while (iter.hasNext()) {
  137. PSExtensionAttachment attachment = (PSExtensionAttachment)iter.next();
  138. if (attachment != null) {
  139. writeEnclosedExtensionAttachment(gen, attachment);
  140. }
  141. iter.remove();
  142. }
  143. }
  144. /**
  145. * Formats and writes a PSExtensionAttachment to the output stream.
  146. *
  147. * @param gen the PS generator
  148. * @param attachment an PSExtensionAttachment instance
  149. * @throws IOException if an I/O error occurs.
  150. */
  151. public static void writeEnclosedExtensionAttachment(PSGenerator gen,
  152. PSExtensionAttachment attachment) throws IOException {
  153. if (attachment instanceof PSCommentBefore) {
  154. gen.commentln("%" + attachment.getContent());
  155. } else if (attachment instanceof PSCommentAfter) {
  156. gen.commentln("%" + attachment.getContent());
  157. } else {
  158. String info = "";
  159. if (attachment instanceof PSSetupCode) {
  160. PSSetupCode setupCodeAttach = (PSSetupCode)attachment;
  161. String name = setupCodeAttach.getName();
  162. if (name != null) {
  163. info += ": (" + name + ")";
  164. }
  165. }
  166. String type = attachment.getType();
  167. gen.commentln("%FOPBegin" + type + info);
  168. LineNumberReader reader = new LineNumberReader(
  169. new java.io.StringReader(attachment.getContent()));
  170. String line;
  171. while ((line = reader.readLine()) != null) {
  172. line = line.trim();
  173. if (line.length() > 0) {
  174. gen.writeln(line);
  175. }
  176. }
  177. gen.commentln("%FOPEnd" + type);
  178. }
  179. }
  180. /**
  181. * Sets whether or not PostScript Document Structuring Conventions (DSC) compliance are
  182. * enforced.
  183. * <p>
  184. * It can cause problems (unwanted PostScript subsystem initgraphics/erasepage calls)
  185. * on some printers when the pagedevice is set. If this causes problems on a
  186. * particular implementation then use this setting with a 'false' value to try and
  187. * minimize the number of setpagedevice calls in the PostScript document output.
  188. * <p>
  189. * Set this value to false if you experience unwanted blank pages in your
  190. * PostScript output.
  191. * @param value boolean value (default is true)
  192. */
  193. public void setSafeSetPageDevice(boolean value) {
  194. this.safeSetPageDevice = value;
  195. }
  196. /**
  197. * Indicates whether the "safe setpagedevice" mode is active.
  198. * See {@link #setSafeSetPageDevice(boolean)} for more information.
  199. * @return true if active
  200. */
  201. public boolean isSafeSetPageDevice() {
  202. return this.safeSetPageDevice;
  203. }
  204. /**
  205. * Sets whether or not the safe set page device macro should be used
  206. * (as opposed to directly invoking setpagedevice) when setting the
  207. * PostScript page device.
  208. * <p>
  209. * This option is a useful option when you want to guard against the possibility
  210. * of invalid/unsupported PostScript key/values being placed in the page device.
  211. * <p>
  212. * @param value setting to false and the renderer will make a
  213. * standard "setpagedevice" call, setting to true will make a safe set page
  214. * device macro call (default is false).
  215. */
  216. public void setDSCComplianceEnabled(boolean value) {
  217. this.dscCompliant = value;
  218. }
  219. /** @return true if DSC complicance is enabled */
  220. public boolean isDSCComplianceEnabled() {
  221. return this.dscCompliant;
  222. }
  223. /**
  224. * Controls whether landscape pages should be rotated.
  225. * @param value true to enable the rotation
  226. */
  227. public void setAutoRotateLandscape(boolean value) {
  228. this.autoRotateLandscape = value;
  229. }
  230. /**
  231. * Indicates whether landscape pages are rotated.
  232. * @return true if landscape pages are to be rotated
  233. */
  234. public boolean isAutoRotateLandscape() {
  235. return autoRotateLandscape;
  236. }
  237. /**
  238. * Sets the PostScript language level.
  239. * @param level the PostScript language level (Only 2 and 3 are currently supported)
  240. */
  241. public void setLanguageLevel(int level) {
  242. if (level == 2 || level == 3) {
  243. this.languageLevel = level;
  244. } else {
  245. throw new IllegalArgumentException("Only language levels 2 or 3 are allowed/supported");
  246. }
  247. }
  248. /**
  249. * Indicates the selected PostScript language level.
  250. * @return the PostScript language level
  251. */
  252. public int getLanguageLevel() {
  253. return languageLevel;
  254. }
  255. /**
  256. * Controls whether PostScript resources are optimized in a second pass over the document.
  257. * Enable this to obtain smaller PostScript files.
  258. * @param value true to enable resource optimization
  259. */
  260. public void setOptimizeResources(boolean value) {
  261. this.optimizeResources = value;
  262. }
  263. /**
  264. * Indicates whether PostScript resources are optimized in a second pass over the document.
  265. * @return true if resource optimization is enabled
  266. */
  267. public boolean isOptimizeResources() {
  268. return optimizeResources;
  269. }
  270. /**
  271. * Sets the rendering mode.
  272. * @param renderingMode the rendering mode
  273. */
  274. public void setRenderingMode(PSRenderingMode renderingMode) {
  275. this.renderingMode = renderingMode;
  276. }
  277. /**
  278. * Returns the rendering mode.
  279. * @return the rendering mode
  280. */
  281. public PSRenderingMode getRenderingMode() {
  282. return this.renderingMode;
  283. }
  284. }