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

PSPainter.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Paint;
  22. import java.awt.Point;
  23. import java.awt.Rectangle;
  24. import java.awt.geom.AffineTransform;
  25. import java.io.IOException;
  26. import java.util.Map;
  27. import org.w3c.dom.Document;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import org.apache.xmlgraphics.image.loader.ImageException;
  31. import org.apache.xmlgraphics.image.loader.ImageInfo;
  32. import org.apache.xmlgraphics.image.loader.ImageProcessingHints;
  33. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  34. import org.apache.xmlgraphics.ps.PSGenerator;
  35. import org.apache.xmlgraphics.ps.PSResource;
  36. import org.apache.fop.apps.FOUserAgent;
  37. import org.apache.fop.fonts.Font;
  38. import org.apache.fop.fonts.FontInfo;
  39. import org.apache.fop.fonts.FontTriplet;
  40. import org.apache.fop.fonts.LazyFont;
  41. import org.apache.fop.fonts.SingleByteFont;
  42. import org.apache.fop.fonts.Typeface;
  43. import org.apache.fop.render.RenderingContext;
  44. import org.apache.fop.render.intermediate.AbstractIFPainter;
  45. import org.apache.fop.render.intermediate.IFException;
  46. import org.apache.fop.render.intermediate.IFState;
  47. import org.apache.fop.traits.BorderProps;
  48. import org.apache.fop.traits.RuleStyle;
  49. import org.apache.fop.util.CharUtilities;
  50. /**
  51. * IFPainter implementation that produces PostScript.
  52. */
  53. public class PSPainter extends AbstractIFPainter {
  54. /** logging instance */
  55. private static Log log = LogFactory.getLog(PSPainter.class);
  56. private PSDocumentHandler documentHandler;
  57. private boolean inTextMode = false;
  58. /**
  59. * Default constructor.
  60. * @param documentHandler the parent document handler
  61. */
  62. public PSPainter(PSDocumentHandler documentHandler) {
  63. super();
  64. this.documentHandler = documentHandler;
  65. this.state = IFState.create();
  66. }
  67. /** {@inheritDoc} */
  68. protected FOUserAgent getUserAgent() {
  69. return this.documentHandler.getUserAgent();
  70. }
  71. PSRenderingUtil getPSUtil() {
  72. return this.documentHandler.psUtil;
  73. }
  74. FontInfo getFontInfo() {
  75. return this.documentHandler.getFontInfo();
  76. }
  77. private PSGenerator getGenerator() {
  78. return this.documentHandler.gen;
  79. }
  80. /** {@inheritDoc} */
  81. public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
  82. throws IFException {
  83. try {
  84. PSGenerator generator = getGenerator();
  85. saveGraphicsState();
  86. generator.concatMatrix(toPoints(transform));
  87. } catch (IOException ioe) {
  88. throw new IFException("I/O error in startViewport()", ioe);
  89. }
  90. if (clipRect != null) {
  91. clipRect(clipRect);
  92. }
  93. }
  94. /** {@inheritDoc} */
  95. public void endViewport() throws IFException {
  96. try {
  97. restoreGraphicsState();
  98. } catch (IOException ioe) {
  99. throw new IFException("I/O error in endViewport()", ioe);
  100. }
  101. }
  102. /** {@inheritDoc} */
  103. public void startGroup(AffineTransform transform) throws IFException {
  104. try {
  105. PSGenerator generator = getGenerator();
  106. saveGraphicsState();
  107. generator.concatMatrix(toPoints(transform));
  108. } catch (IOException ioe) {
  109. throw new IFException("I/O error in startGroup()", ioe);
  110. }
  111. }
  112. /** {@inheritDoc} */
  113. public void endGroup() throws IFException {
  114. try {
  115. restoreGraphicsState();
  116. } catch (IOException ioe) {
  117. throw new IFException("I/O error in endGroup()", ioe);
  118. }
  119. }
  120. /** {@inheritDoc} */
  121. protected Map createDefaultImageProcessingHints(ImageSessionContext sessionContext) {
  122. Map hints = super.createDefaultImageProcessingHints(sessionContext);
  123. //PostScript doesn't support alpha channels
  124. hints.put(ImageProcessingHints.TRANSPARENCY_INTENT,
  125. ImageProcessingHints.TRANSPARENCY_INTENT_IGNORE);
  126. //TODO We might want to support image masks in the future.
  127. return hints;
  128. }
  129. /** {@inheritDoc} */
  130. protected RenderingContext createRenderingContext() {
  131. PSRenderingContext psContext = new PSRenderingContext(
  132. getUserAgent(), getGenerator(), getFontInfo());
  133. return psContext;
  134. }
  135. /** {@inheritDoc} */
  136. protected void drawImageUsingImageHandler(ImageInfo info, Rectangle rect)
  137. throws ImageException, IOException {
  138. if (!getPSUtil().isOptimizeResources()
  139. || PSImageUtils.isImageInlined(info,
  140. (PSRenderingContext)createRenderingContext())) {
  141. super.drawImageUsingImageHandler(info, rect);
  142. } else {
  143. if (log.isDebugEnabled()) {
  144. log.debug("Image " + info + " is embedded as a form later");
  145. }
  146. //Don't load image at this time, just put a form placeholder in the stream
  147. PSResource form = documentHandler.getFormForImage(info.getOriginalURI());
  148. PSImageUtils.drawForm(form, info, rect, getGenerator());
  149. }
  150. }
  151. /** {@inheritDoc} */
  152. public void drawImage(String uri, Rectangle rect, Map foreignAttributes) throws IFException {
  153. try {
  154. endTextObject();
  155. } catch (IOException ioe) {
  156. throw new IFException("I/O error in drawImage()", ioe);
  157. }
  158. drawImageUsingURI(uri, rect);
  159. }
  160. /** {@inheritDoc} */
  161. public void drawImage(Document doc, Rectangle rect, Map foreignAttributes) throws IFException {
  162. try {
  163. endTextObject();
  164. } catch (IOException ioe) {
  165. throw new IFException("I/O error in drawImage()", ioe);
  166. }
  167. drawImageUsingDocument(doc, rect);
  168. }
  169. /** {@inheritDoc} */
  170. public void clipRect(Rectangle rect) throws IFException {
  171. try {
  172. PSGenerator generator = getGenerator();
  173. endTextObject();
  174. generator.defineRect(rect.x / 1000.0, rect.y / 1000.0,
  175. rect.width / 1000.0, rect.height / 1000.0);
  176. generator.writeln("clip newpath");
  177. } catch (IOException ioe) {
  178. throw new IFException("I/O error in clipRect()", ioe);
  179. }
  180. }
  181. /** {@inheritDoc} */
  182. public void fillRect(Rectangle rect, Paint fill) throws IFException {
  183. if (fill == null) {
  184. return;
  185. }
  186. if (rect.width != 0 && rect.height != 0) {
  187. try {
  188. endTextObject();
  189. PSGenerator generator = getGenerator();
  190. if (fill != null) {
  191. if (fill instanceof Color) {
  192. generator.useColor((Color)fill);
  193. } else {
  194. throw new UnsupportedOperationException("Non-Color paints NYI");
  195. }
  196. }
  197. generator.defineRect(rect.x / 1000.0, rect.y / 1000.0,
  198. rect.width / 1000.0, rect.height / 1000.0);
  199. generator.writeln("fill");
  200. } catch (IOException ioe) {
  201. throw new IFException("I/O error in fillRect()", ioe);
  202. }
  203. }
  204. }
  205. /** {@inheritDoc} */
  206. public void drawBorderRect(Rectangle rect, BorderProps before, BorderProps after,
  207. BorderProps start, BorderProps end) throws IFException {
  208. if (before != null || after != null || start != null || end != null) {
  209. try {
  210. //TODO Implement me
  211. endTextObject();
  212. //this.borderPainter.drawBorders(rect, before, after, start, end);
  213. } catch (IOException ioe) {
  214. throw new IFException("I/O error in drawBorderRect()", ioe);
  215. }
  216. }
  217. }
  218. /** {@inheritDoc} */
  219. public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
  220. throws IFException {
  221. try {
  222. //TODO Implement me
  223. endTextObject();
  224. //this.borderPainter.drawLine(start, end, width, color, style);
  225. } catch (IOException ioe) {
  226. throw new IFException("I/O error in drawLine()", ioe);
  227. }
  228. }
  229. private Typeface getTypeface(String fontName) {
  230. if (fontName == null) {
  231. throw new NullPointerException("fontName must not be null");
  232. }
  233. Typeface tf = (Typeface)getFontInfo().getFonts().get(fontName);
  234. if (tf instanceof LazyFont) {
  235. tf = ((LazyFont)tf).getRealFont();
  236. }
  237. return tf;
  238. }
  239. /**
  240. * Saves the graphics state of the rendering engine.
  241. * @throws IOException if an I/O error occurs
  242. */
  243. protected void saveGraphicsState() throws IOException {
  244. endTextObject();
  245. getGenerator().saveGraphicsState();
  246. }
  247. /**
  248. * Restores the last graphics state of the rendering engine.
  249. * @throws IOException if an I/O error occurs
  250. */
  251. protected void restoreGraphicsState() throws IOException {
  252. endTextObject();
  253. getGenerator().restoreGraphicsState();
  254. }
  255. /**
  256. * Indicates the beginning of a text object.
  257. * @throws IOException if an I/O error occurs
  258. */
  259. protected void beginTextObject() throws IOException {
  260. if (!inTextMode) {
  261. PSGenerator generator = getGenerator();
  262. generator.saveGraphicsState();
  263. generator.writeln("BT");
  264. inTextMode = true;
  265. }
  266. }
  267. /**
  268. * Indicates the end of a text object.
  269. * @throws IOException if an I/O error occurs
  270. */
  271. protected void endTextObject() throws IOException {
  272. if (inTextMode) {
  273. inTextMode = false;
  274. PSGenerator generator = getGenerator();
  275. generator.writeln("ET");
  276. generator.restoreGraphicsState();
  277. }
  278. }
  279. /** {@inheritDoc} */
  280. public void drawText(int x, int y, int[] dx, int[] dy, String text) throws IFException {
  281. try {
  282. //Note: dy is currently ignored
  283. PSGenerator generator = getGenerator();
  284. generator.useColor(state.getTextColor());
  285. beginTextObject();
  286. FontTriplet triplet = new FontTriplet(
  287. state.getFontFamily(), state.getFontStyle(), state.getFontWeight());
  288. //TODO Ignored: state.getFontVariant()
  289. //TODO Opportunity for font caching if font state is more heavily used
  290. String fontKey = getFontInfo().getInternalFontKey(triplet);
  291. int sizeMillipoints = state.getFontSize();
  292. float fontSize = sizeMillipoints / 1000f;
  293. // This assumes that *all* CIDFonts use a /ToUnicode mapping
  294. Typeface tf = getTypeface(fontKey);
  295. SingleByteFont singleByteFont = null;
  296. if (tf instanceof SingleByteFont) {
  297. singleByteFont = (SingleByteFont)tf;
  298. }
  299. Font font = getFontInfo().getFontInstance(triplet, sizeMillipoints);
  300. //String fontName = font.getFontName();
  301. PSResource res = this.documentHandler.getPSResourceForFontKey(fontKey);
  302. generator.useFont("/" + res.getName(), fontSize);
  303. generator.getResourceTracker().notifyResourceUsageOnPage(res);
  304. //textutil.updateTf(fontKey, fontSize, tf.isMultiByte());
  305. generator.writeln("1 0 0 -1 " + generator.formatDouble(x / 1000.0)
  306. + " " + generator.formatDouble(y / 1000.0) + " Tm");
  307. //textutil.writeTextMatrix(new AffineTransform(1, 0, 0, -1, x / 1000f, y / 1000f));
  308. int textLen = text.length();
  309. if (singleByteFont != null && singleByteFont.hasAdditionalEncodings()) {
  310. //Analyze string and split up in order to paint in different sub-fonts/encodings
  311. int start = 0;
  312. int currentEncoding = -1;
  313. for (int i = 0; i < textLen; i++) {
  314. char c = text.charAt(i);
  315. char mapped = tf.mapChar(c);
  316. int encoding = mapped / 256;
  317. if (currentEncoding != encoding) {
  318. if (i > 0) {
  319. writeText(text, start, i - start, dx, dy, font, tf);
  320. }
  321. if (encoding == 0) {
  322. useFont(fontKey, sizeMillipoints);
  323. } else {
  324. useFont(fontKey + "_" + Integer.toString(encoding), sizeMillipoints);
  325. }
  326. currentEncoding = encoding;
  327. start = i;
  328. }
  329. }
  330. writeText(text, start, textLen - start, dx, dy, font, tf);
  331. } else {
  332. //Simple single-font painting
  333. useFont(fontKey, sizeMillipoints);
  334. writeText(text, 0, textLen, dx, dy, font, tf);
  335. }
  336. } catch (IOException ioe) {
  337. throw new IFException("I/O error in drawText()", ioe);
  338. }
  339. }
  340. private void writeText(String text, int start, int len, int[] dx, int[] dy,
  341. Font font, Typeface tf) throws IOException {
  342. PSGenerator generator = getGenerator();
  343. int end = start + len;
  344. int initialSize = len;
  345. initialSize += initialSize / 2;
  346. StringBuffer sb = new StringBuffer(initialSize);
  347. sb.append("(");
  348. int[] offsets = new int[len];
  349. int dxl = (dx != null ? dx.length : 0);
  350. for (int i = start; i < end; i++) {
  351. char orgChar = text.charAt(i);
  352. char ch;
  353. int cw;
  354. if (CharUtilities.isFixedWidthSpace(orgChar)) {
  355. //Fixed width space are rendered as spaces so copy/paste works in a reader
  356. ch = font.mapChar(CharUtilities.SPACE);
  357. //int spaceDiff = font.getCharWidth(ch) - font.getCharWidth(orgChar);
  358. //glyphAdjust = -(spaceDiff);
  359. } else {
  360. ch = font.mapChar(orgChar);
  361. //cw = tf.getWidth(ch, font.getFontSize()) / 1000;
  362. }
  363. cw = font.getCharWidth(orgChar);
  364. int glyphAdjust = 0;
  365. if (dx != null && i < dxl - 1) {
  366. glyphAdjust += dx[i + 1];
  367. }
  368. offsets[i - start] = cw + glyphAdjust;
  369. char codepoint = (char)(ch % 256);
  370. PSGenerator.escapeChar(codepoint, sb);
  371. }
  372. sb.append(")" + PSGenerator.LF + "[");
  373. for (int i = 0; i < len; i++) {
  374. if (i > 0) {
  375. if (i % 8 == 0) {
  376. sb.append(PSGenerator.LF);
  377. } else {
  378. sb.append(" ");
  379. }
  380. }
  381. sb.append(generator.formatDouble(offsets[i] / 1000f));
  382. }
  383. sb.append("]" + PSGenerator.LF + "xshow");
  384. generator.writeln(sb.toString());
  385. }
  386. private void useFont(String key, int size) throws IOException {
  387. PSResource res = this.documentHandler.getPSResourceForFontKey(key);
  388. PSGenerator generator = getGenerator();
  389. generator.useFont("/" + res.getName(), size / 1000f);
  390. generator.getResourceTracker().notifyResourceUsageOnPage(res);
  391. }
  392. }