Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PSRenderingUtil.java 12KB

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