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

PCLRenderer.java 58KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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.pcl;
  19. //Java
  20. import java.awt.BasicStroke;
  21. import java.awt.Color;
  22. import java.awt.Dimension;
  23. import java.awt.Graphics2D;
  24. import java.awt.Rectangle;
  25. import java.awt.RenderingHints;
  26. import java.awt.color.ColorSpace;
  27. import java.awt.geom.AffineTransform;
  28. import java.awt.geom.GeneralPath;
  29. import java.awt.geom.Line2D;
  30. import java.awt.geom.Point2D;
  31. import java.awt.geom.Rectangle2D;
  32. import java.awt.image.BufferedImage;
  33. import java.awt.image.ColorModel;
  34. import java.awt.image.ComponentColorModel;
  35. import java.awt.image.DataBuffer;
  36. import java.awt.image.DataBufferByte;
  37. import java.awt.image.PixelInterleavedSampleModel;
  38. import java.awt.image.Raster;
  39. import java.awt.image.RenderedImage;
  40. import java.awt.image.SampleModel;
  41. import java.awt.image.WritableRaster;
  42. import java.io.IOException;
  43. import java.io.OutputStream;
  44. import java.util.List;
  45. import java.util.Map;
  46. import java.util.Stack;
  47. import org.w3c.dom.Document;
  48. import org.apache.xmlgraphics.java2d.GraphicContext;
  49. // FOP
  50. import org.apache.commons.logging.Log;
  51. import org.apache.commons.logging.LogFactory;
  52. import org.apache.fop.apps.FOPException;
  53. import org.apache.fop.apps.MimeConstants;
  54. import org.apache.fop.area.Area;
  55. import org.apache.fop.area.Block;
  56. import org.apache.fop.area.BlockViewport;
  57. import org.apache.fop.area.CTM;
  58. import org.apache.fop.area.PageViewport;
  59. import org.apache.fop.area.Trait;
  60. import org.apache.fop.area.inline.AbstractTextArea;
  61. import org.apache.fop.area.inline.ForeignObject;
  62. import org.apache.fop.area.inline.Image;
  63. import org.apache.fop.area.inline.InlineArea;
  64. import org.apache.fop.area.inline.SpaceArea;
  65. import org.apache.fop.area.inline.TextArea;
  66. import org.apache.fop.area.inline.Viewport;
  67. import org.apache.fop.area.inline.WordArea;
  68. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  69. import org.apache.fop.fonts.Font;
  70. import org.apache.fop.fonts.FontInfo;
  71. import org.apache.fop.fonts.FontMetrics;
  72. import org.apache.fop.image.EPSImage;
  73. import org.apache.fop.image.FopImage;
  74. import org.apache.fop.image.ImageFactory;
  75. import org.apache.fop.image.XMLImage;
  76. import org.apache.fop.render.Graphics2DAdapter;
  77. import org.apache.fop.render.Graphics2DImagePainter;
  78. import org.apache.fop.render.PrintRenderer;
  79. import org.apache.fop.render.RendererContext;
  80. import org.apache.fop.render.RendererContextConstants;
  81. import org.apache.fop.render.java2d.FontMetricsMapper;
  82. import org.apache.fop.render.java2d.FontSetup;
  83. import org.apache.fop.render.java2d.Java2DRenderer;
  84. import org.apache.fop.render.pcl.extensions.PCLElementMapping;
  85. import org.apache.fop.traits.BorderProps;
  86. import org.apache.fop.util.QName;
  87. import org.apache.fop.util.UnitConv;
  88. /**
  89. * Renderer for the PCL 5 printer language. It also uses HP GL/2 for certain graphic elements.
  90. */
  91. public class PCLRenderer extends PrintRenderer {
  92. /** logging instance */
  93. private static Log log = LogFactory.getLog(PCLRenderer.class);
  94. /** The MIME type for PCL */
  95. public static final String MIME_TYPE = MimeConstants.MIME_PCL_ALT;
  96. private static final QName CONV_MODE
  97. = new QName(ExtensionElementMapping.URI, null, "conversion-mode");
  98. private static final QName SRC_TRANSPARENCY
  99. = new QName(ExtensionElementMapping.URI, null, "source-transparency");
  100. /** The OutputStream to write the PCL stream to */
  101. protected OutputStream out;
  102. /** The PCL generator */
  103. protected PCLGenerator gen;
  104. private boolean ioTrouble = false;
  105. private Stack graphicContextStack = new Stack();
  106. private GraphicContext graphicContext = new GraphicContext();
  107. private PCLPageDefinition currentPageDefinition;
  108. private int currentPrintDirection = 0;
  109. private GeneralPath currentPath = null;
  110. private java.awt.Color currentFillColor = null;
  111. /**
  112. * Controls whether appearance is more important than speed. False can cause some FO feature
  113. * to be ignored (like the advanced borders).
  114. */
  115. private boolean qualityBeforeSpeed = false;
  116. /**
  117. * Controls whether all text should be painted as text. This is a fallback setting in case
  118. * the mixture of native and bitmapped text does not provide the necessary quality.
  119. */
  120. private boolean allTextAsBitmaps = false;
  121. /**
  122. * Create the PCL renderer
  123. */
  124. public PCLRenderer() {
  125. }
  126. public void setQualityBeforeSpeed(boolean qualityBeforeSpeed) {
  127. this.qualityBeforeSpeed = qualityBeforeSpeed;
  128. }
  129. /**
  130. * @see org.apache.fop.render.Renderer#setupFontInfo(org.apache.fop.fonts.FontInfo)
  131. */
  132. public void setupFontInfo(FontInfo inFontInfo) {
  133. //Don't call super.setupFontInfo() here!
  134. //The PCLRenderer uses the Java2D FontSetup which needs a special font setup
  135. //create a temp Image to test font metrics on
  136. fontInfo = inFontInfo;
  137. BufferedImage fontImage = new BufferedImage(100, 100,
  138. BufferedImage.TYPE_INT_RGB);
  139. Graphics2D g = fontImage.createGraphics();
  140. //The next line is important to get accurate font metrics!
  141. g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
  142. RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  143. FontSetup.setup(fontInfo, g);
  144. }
  145. /**
  146. * Central exception handler for I/O exceptions.
  147. * @param ioe IOException to handle
  148. */
  149. protected void handleIOTrouble(IOException ioe) {
  150. if (!ioTrouble) {
  151. log.error("Error while writing to target file", ioe);
  152. ioTrouble = true;
  153. }
  154. }
  155. /** @see org.apache.fop.render.Renderer#getGraphics2DAdapter() */
  156. public Graphics2DAdapter getGraphics2DAdapter() {
  157. return new PCLGraphics2DAdapter();
  158. }
  159. /** @return the GraphicContext used to track coordinate system transformations */
  160. public GraphicContext getGraphicContext() {
  161. return this.graphicContext;
  162. }
  163. /** @return the target resolution */
  164. protected int getResolution() {
  165. int resolution = (int)Math.round(userAgent.getTargetResolution());
  166. if (resolution <= 300) {
  167. return 300;
  168. } else {
  169. return 600;
  170. }
  171. }
  172. /**
  173. * Sets the current font (NOTE: Hard-coded font mappings ATM!)
  174. * @param name the font name (internal F* names for now)
  175. * @param size the font size
  176. * @param text the text to be rendered (used to determine if there are non-printable chars)
  177. * @return true if the font can be mapped to PCL
  178. * @throws IOException if an I/O problem occurs
  179. */
  180. public boolean setFont(String name, float size, String text) throws IOException {
  181. byte[] encoded = text.getBytes("ISO-8859-1");
  182. for (int i = 0, c = encoded.length; i < c; i++) {
  183. if (encoded[i] == 0x3F && text.charAt(i) != '?') {
  184. return false;
  185. }
  186. }
  187. int fontcode = 0;
  188. if (name.length() > 1 && name.charAt(0) == 'F') {
  189. try {
  190. fontcode = Integer.parseInt(name.substring(1));
  191. } catch (Exception e) {
  192. log.error(e);
  193. }
  194. }
  195. //Note "(ON" selects ISO 8859-1 symbol set as used by PCLGenerator
  196. String formattedSize = gen.formatDouble2(size / 1000);
  197. switch (fontcode) {
  198. case 1: // F1 = Helvetica
  199. // gen.writeCommand("(8U");
  200. // gen.writeCommand("(s1p" + formattedSize + "v0s0b24580T");
  201. // Arial is more common among PCL5 printers than Helvetica - so use Arial
  202. gen.writeCommand("(0N");
  203. gen.writeCommand("(s1p" + formattedSize + "v0s0b16602T");
  204. break;
  205. case 2: // F2 = Helvetica Oblique
  206. gen.writeCommand("(0N");
  207. gen.writeCommand("(s1p" + formattedSize + "v1s0b16602T");
  208. break;
  209. case 3: // F3 = Helvetica Bold
  210. gen.writeCommand("(0N");
  211. gen.writeCommand("(s1p" + formattedSize + "v0s3b16602T");
  212. break;
  213. case 4: // F4 = Helvetica Bold Oblique
  214. gen.writeCommand("(0N");
  215. gen.writeCommand("(s1p" + formattedSize + "v1s3b16602T");
  216. break;
  217. case 5: // F5 = Times Roman
  218. // gen.writeCommand("(8U");
  219. // gen.writeCommand("(s1p" + formattedSize + "v0s0b25093T");
  220. // Times New is more common among PCL5 printers than Times - so use Times New
  221. gen.writeCommand("(0N");
  222. gen.writeCommand("(s1p" + formattedSize + "v0s0b16901T");
  223. break;
  224. case 6: // F6 = Times Italic
  225. gen.writeCommand("(0N");
  226. gen.writeCommand("(s1p" + formattedSize + "v1s0b16901T");
  227. break;
  228. case 7: // F7 = Times Bold
  229. gen.writeCommand("(0N");
  230. gen.writeCommand("(s1p" + formattedSize + "v0s3b16901T");
  231. break;
  232. case 8: // F8 = Times Bold Italic
  233. gen.writeCommand("(0N");
  234. gen.writeCommand("(s1p" + formattedSize + "v1s3b16901T");
  235. break;
  236. case 9: // F9 = Courier
  237. gen.writeCommand("(0N");
  238. gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
  239. + "h0s0b4099T");
  240. break;
  241. case 10: // F10 = Courier Oblique
  242. gen.writeCommand("(0N");
  243. gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
  244. + "h1s0b4099T");
  245. break;
  246. case 11: // F11 = Courier Bold
  247. gen.writeCommand("(0N");
  248. gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
  249. + "h0s3b4099T");
  250. break;
  251. case 12: // F12 = Courier Bold Oblique
  252. gen.writeCommand("(0N");
  253. gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
  254. + "h1s3b4099T");
  255. break;
  256. case 13: // F13 = Symbol
  257. return false;
  258. //gen.writeCommand("(19M");
  259. //gen.writeCommand("(s1p" + formattedSize + "v0s0b16686T");
  260. // ECMA Latin 1 Symbol Set in Times Roman???
  261. // gen.writeCommand("(9U");
  262. // gen.writeCommand("(s1p" + formattedSize + "v0s0b25093T");
  263. //break;
  264. case 14: // F14 = Zapf Dingbats
  265. return false;
  266. //gen.writeCommand("(14L");
  267. //gen.writeCommand("(s1p" + formattedSize + "v0s0b45101T");
  268. //break;
  269. default:
  270. //gen.writeCommand("(0N");
  271. //gen.writeCommand("(s" + formattedSize + "V");
  272. return false;
  273. }
  274. return true;
  275. }
  276. /** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
  277. public void startRenderer(OutputStream outputStream) throws IOException {
  278. log.debug("Rendering areas to PCL...");
  279. this.out = outputStream;
  280. this.gen = new PCLGenerator(out, getResolution());
  281. gen.universalEndOfLanguage();
  282. gen.writeText("@PJL COMMENT Produced by " + userAgent.getProducer() + "\n");
  283. if (userAgent.getTitle() != null) {
  284. gen.writeText("@PJL JOB NAME = \"" + userAgent.getTitle() + "\"\n");
  285. }
  286. gen.writeText("@PJL SET RESOLUTION = " + getResolution() + "\n");
  287. gen.writeText("@PJL ENTER LANGUAGE = PCL\n");
  288. gen.resetPrinter();
  289. gen.setUnitOfMeasure(getResolution());
  290. gen.setRasterGraphicsResolution(getResolution());
  291. }
  292. /** @see org.apache.fop.render.Renderer#stopRenderer() */
  293. public void stopRenderer() throws IOException {
  294. gen.separateJobs();
  295. gen.resetPrinter();
  296. gen.universalEndOfLanguage();
  297. }
  298. /** @see org.apache.fop.render.AbstractRenderer */
  299. public String getMimeType() {
  300. return MIME_TYPE;
  301. }
  302. /**
  303. * @see org.apache.fop.render.AbstractRenderer#renderPage(org.apache.fop.area.PageViewport)
  304. */
  305. public void renderPage(PageViewport page) throws IOException, FOPException {
  306. saveGraphicsState();
  307. //Paper source
  308. String paperSource = page.getForeignAttributeValue(
  309. new QName(PCLElementMapping.NAMESPACE, null, "paper-source"));
  310. if (paperSource != null) {
  311. gen.selectPaperSource(Integer.parseInt(paperSource));
  312. }
  313. //Page size
  314. final long pagewidth = Math.round(page.getViewArea().getWidth());
  315. final long pageheight = Math.round(page.getViewArea().getHeight());
  316. selectPageFormat(pagewidth, pageheight);
  317. super.renderPage(page);
  318. //Eject page
  319. gen.formFeed();
  320. restoreGraphicsState();
  321. }
  322. private void selectPageFormat(long pagewidth, long pageheight) throws IOException {
  323. this.currentPageDefinition = PCLPageDefinition.getPageDefinition(
  324. pagewidth, pageheight, 1000);
  325. if (this.currentPageDefinition == null) {
  326. this.currentPageDefinition = PCLPageDefinition.getDefaultPageDefinition();
  327. log.warn("Paper type could not be determined. Falling back to: "
  328. + this.currentPageDefinition.getName());
  329. }
  330. log.debug("page size: " + currentPageDefinition.getPhysicalPageSize());
  331. log.debug("logical page: " + currentPageDefinition.getLogicalPageRect());
  332. if (this.currentPageDefinition.isLandscapeFormat()) {
  333. gen.writeCommand("&l1O"); //Orientation
  334. } else {
  335. gen.writeCommand("&l0O"); //Orientation
  336. }
  337. gen.selectPageSize(this.currentPageDefinition.getSelector());
  338. gen.clearHorizontalMargins();
  339. gen.setTopMargin(0);
  340. }
  341. /** Saves the current graphics state on the stack. */
  342. protected void saveGraphicsState() {
  343. graphicContextStack.push(graphicContext);
  344. graphicContext = (GraphicContext)graphicContext.clone();
  345. }
  346. /** Restores the last graphics state from the stack. */
  347. protected void restoreGraphicsState() {
  348. graphicContext = (GraphicContext)graphicContextStack.pop();
  349. }
  350. /**
  351. * Clip an area. write a clipping operation given coordinates in the current
  352. * transform. Coordinates are in points.
  353. *
  354. * @param x the x coordinate
  355. * @param y the y coordinate
  356. * @param width the width of the area
  357. * @param height the height of the area
  358. */
  359. protected void clipRect(float x, float y, float width, float height) {
  360. //PCL cannot clip (only HP GL/2 can)
  361. }
  362. private Point2D transformedPoint(float x, float y) {
  363. return transformedPoint(Math.round(x), Math.round(y));
  364. }
  365. private Point2D transformedPoint(int x, int y) {
  366. AffineTransform at = graphicContext.getTransform();
  367. if (log.isTraceEnabled()) {
  368. log.trace("Current transform: " + at);
  369. }
  370. Point2D.Float orgPoint = new Point2D.Float(x, y);
  371. Point2D.Float transPoint = new Point2D.Float();
  372. at.transform(orgPoint, transPoint);
  373. //At this point we have the absolute position in FOP's coordinate system
  374. //Now get PCL coordinates taking the current print direction and the logical page
  375. //into account.
  376. Dimension pageSize = currentPageDefinition.getPhysicalPageSize();
  377. Rectangle logRect = currentPageDefinition.getLogicalPageRect();
  378. switch (currentPrintDirection) {
  379. case 0:
  380. transPoint.x -= logRect.x;
  381. transPoint.y -= logRect.y;
  382. break;
  383. case 90:
  384. float ty = transPoint.x;
  385. transPoint.x = pageSize.height - transPoint.y;
  386. transPoint.y = ty;
  387. transPoint.x -= logRect.y;
  388. transPoint.y -= logRect.x;
  389. break;
  390. case 180:
  391. transPoint.x = pageSize.width - transPoint.x;
  392. transPoint.y = pageSize.height - transPoint.y;
  393. transPoint.x -= pageSize.width - logRect.x - logRect.width;
  394. transPoint.y -= pageSize.height - logRect.y - logRect.height;
  395. //The next line is odd and is probably necessary due to the default value of the
  396. //Text Length command: "1/2 inch less than maximum text length"
  397. //I wonder why this isn't necessary for the 90 degree rotation. *shrug*
  398. transPoint.y -= UnitConv.in2mpt(0.5);
  399. break;
  400. case 270:
  401. float tx = transPoint.y;
  402. transPoint.y = pageSize.width - transPoint.x;
  403. transPoint.x = tx;
  404. transPoint.x -= pageSize.height - logRect.y - logRect.height;
  405. transPoint.y -= pageSize.width - logRect.x - logRect.width;
  406. break;
  407. default:
  408. throw new IllegalStateException("Illegal print direction: " + currentPrintDirection);
  409. }
  410. return transPoint;
  411. }
  412. private void changePrintDirection() {
  413. AffineTransform at = graphicContext.getTransform();
  414. int newDir;
  415. try {
  416. if (at.getScaleX() == 0 && at.getScaleY() == 0
  417. && at.getShearX() == 1 && at.getShearY() == -1) {
  418. newDir = 90;
  419. } else if (at.getScaleX() == -1 && at.getScaleY() == -1
  420. && at.getShearX() == 0 && at.getShearY() == 0) {
  421. newDir = 180;
  422. } else if (at.getScaleX() == 0 && at.getScaleY() == 0
  423. && at.getShearX() == -1 && at.getShearY() == 1) {
  424. newDir = 270;
  425. } else {
  426. newDir = 0;
  427. }
  428. if (newDir != this.currentPrintDirection) {
  429. this.currentPrintDirection = newDir;
  430. gen.changePrintDirection(this.currentPrintDirection);
  431. }
  432. } catch (IOException ioe) {
  433. handleIOTrouble(ioe);
  434. }
  435. }
  436. /**
  437. * @see org.apache.fop.render.AbstractRenderer#startVParea(CTM, Rectangle2D)
  438. */
  439. protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
  440. saveGraphicsState();
  441. AffineTransform at = new AffineTransform(ctm.toArray());
  442. graphicContext.transform(at);
  443. changePrintDirection();
  444. if (log.isDebugEnabled()) {
  445. log.debug("startVPArea: " + at + " --> " + graphicContext.getTransform());
  446. }
  447. }
  448. /**
  449. * @see org.apache.fop.render.AbstractRenderer#endVParea()
  450. */
  451. protected void endVParea() {
  452. restoreGraphicsState();
  453. changePrintDirection();
  454. if (log.isDebugEnabled()) {
  455. log.debug("endVPArea() --> " + graphicContext.getTransform());
  456. }
  457. }
  458. /**
  459. * Handle block traits.
  460. * The block could be any sort of block with any positioning
  461. * so this should render the traits such as border and background
  462. * in its position.
  463. *
  464. * @param block the block to render the traits
  465. */
  466. protected void handleBlockTraits(Block block) {
  467. int borderPaddingStart = block.getBorderAndPaddingWidthStart();
  468. int borderPaddingBefore = block.getBorderAndPaddingWidthBefore();
  469. float startx = currentIPPosition / 1000f;
  470. float starty = currentBPPosition / 1000f;
  471. float width = block.getIPD() / 1000f;
  472. float height = block.getBPD() / 1000f;
  473. startx += block.getStartIndent() / 1000f;
  474. startx -= block.getBorderAndPaddingWidthStart() / 1000f;
  475. width += borderPaddingStart / 1000f;
  476. width += block.getBorderAndPaddingWidthEnd() / 1000f;
  477. height += borderPaddingBefore / 1000f;
  478. height += block.getBorderAndPaddingWidthAfter() / 1000f;
  479. drawBackAndBorders(block, startx, starty, width, height);
  480. }
  481. /**
  482. * @see org.apache.fop.render.AbstractRenderer#renderText(TextArea)
  483. */
  484. protected void renderText(final TextArea text) {
  485. renderInlineAreaBackAndBorders(text);
  486. String fontname = getInternalFontNameForArea(text);
  487. final int fontsize = text.getTraitAsInteger(Trait.FONT_SIZE);
  488. //Determine position
  489. int saveIP = currentIPPosition;
  490. final int rx = currentIPPosition + text.getBorderAndPaddingWidthStart();
  491. int bl = currentBPPosition + text.getOffset() + text.getBaselineOffset();
  492. try {
  493. final Color col = (Color)text.getTrait(Trait.COLOR);
  494. boolean pclFont = allTextAsBitmaps
  495. ? false
  496. : setFont(fontname, fontsize, text.getText());
  497. if (pclFont) {
  498. //this.currentFill = col;
  499. if (col != null) {
  500. //useColor(ct);
  501. gen.setTransparencyMode(true, false);
  502. gen.selectGrayscale(col);
  503. }
  504. saveGraphicsState();
  505. graphicContext.translate(rx, bl);
  506. setCursorPos(0, 0);
  507. gen.setTransparencyMode(true, true);
  508. if (text.hasUnderline()) {
  509. gen.writeCommand("&d0D");
  510. }
  511. super.renderText(text); //Updates IPD and renders words and spaces
  512. if (text.hasUnderline()) {
  513. gen.writeCommand("&d@");
  514. }
  515. restoreGraphicsState();
  516. } else {
  517. //Use Java2D to paint different fonts via bitmap
  518. final Font font = getFontFromArea(text);
  519. final int baseline = text.getBaselineOffset();
  520. //for cursive fonts, so the text isn't clipped
  521. int extraWidth = font.getFontSize() / 3;
  522. final FontMetricsMapper mapper = (FontMetricsMapper)fontInfo.getMetricsFor(
  523. font.getFontName());
  524. int maxAscent = mapper.getMaxAscent(font.getFontSize()) / 1000;
  525. final int additionalBPD = maxAscent - baseline;
  526. Graphics2DAdapter g2a = getGraphics2DAdapter();
  527. final Rectangle paintRect = new Rectangle(
  528. rx, currentBPPosition + text.getOffset() - additionalBPD,
  529. text.getIPD() + extraWidth, text.getBPD() + additionalBPD);
  530. RendererContext rc = createRendererContext(paintRect.x, paintRect.y,
  531. paintRect.width, paintRect.height, null);
  532. Map atts = new java.util.HashMap();
  533. atts.put(CONV_MODE, "bitmap");
  534. atts.put(SRC_TRANSPARENCY, "true");
  535. rc.setProperty(RendererContextConstants.FOREIGN_ATTRIBUTES, atts);
  536. Graphics2DImagePainter painter = new Graphics2DImagePainter() {
  537. public void paint(Graphics2D g2d, Rectangle2D area) {
  538. g2d.setFont(mapper.getFont(font.getFontSize()));
  539. g2d.translate(0, baseline + additionalBPD);
  540. g2d.scale(1000, 1000);
  541. g2d.setColor(col);
  542. Java2DRenderer.renderText(text, g2d, font);
  543. renderTextDecoration(g2d, mapper, fontsize, text, 0, 0);
  544. }
  545. public Dimension getImageSize() {
  546. return paintRect.getSize();
  547. }
  548. };
  549. g2a.paintImage(painter, rc,
  550. paintRect.x, paintRect.y, paintRect.width, paintRect.height);
  551. currentIPPosition = saveIP + text.getAllocIPD();
  552. }
  553. } catch (IOException ioe) {
  554. handleIOTrouble(ioe);
  555. }
  556. }
  557. /**
  558. * Paints the text decoration marks.
  559. * @param g2d Graphics2D instance to paint to
  560. * @param fm Current typeface
  561. * @param fontsize Current font size
  562. * @param inline inline area to paint the marks for
  563. * @param baseline position of the baseline
  564. * @param startx start IPD
  565. */
  566. private static void renderTextDecoration(Graphics2D g2d,
  567. FontMetrics fm, int fontsize, InlineArea inline,
  568. int baseline, int startx) {
  569. boolean hasTextDeco = inline.hasUnderline()
  570. || inline.hasOverline()
  571. || inline.hasLineThrough();
  572. if (hasTextDeco) {
  573. float descender = fm.getDescender(fontsize) / 1000f;
  574. float capHeight = fm.getCapHeight(fontsize) / 1000f;
  575. float lineWidth = (descender / -4f) / 1000f;
  576. float endx = (startx + inline.getIPD()) / 1000f;
  577. if (inline.hasUnderline()) {
  578. Color ct = (Color) inline.getTrait(Trait.UNDERLINE_COLOR);
  579. g2d.setColor(ct);
  580. float y = baseline - descender / 2f;
  581. g2d.setStroke(new BasicStroke(lineWidth));
  582. g2d.draw(new Line2D.Float(startx / 1000f, y / 1000f,
  583. endx, y / 1000f));
  584. }
  585. if (inline.hasOverline()) {
  586. Color ct = (Color) inline.getTrait(Trait.OVERLINE_COLOR);
  587. g2d.setColor(ct);
  588. float y = (float)(baseline - (1.1 * capHeight));
  589. g2d.setStroke(new BasicStroke(lineWidth));
  590. g2d.draw(new Line2D.Float(startx / 1000f, y / 1000f,
  591. endx, y / 1000f));
  592. }
  593. if (inline.hasLineThrough()) {
  594. Color ct = (Color) inline.getTrait(Trait.LINETHROUGH_COLOR);
  595. g2d.setColor(ct);
  596. float y = (float)(baseline - (0.45 * capHeight));
  597. g2d.setStroke(new BasicStroke(lineWidth));
  598. g2d.draw(new Line2D.Float(startx / 1000f, y / 1000f,
  599. endx, y / 1000f));
  600. }
  601. }
  602. }
  603. /**
  604. * Sets the current cursor position. The coordinates are transformed to the absolute position
  605. * on the logical PCL page and then passed on to the PCLGenerator.
  606. * @param x the x coordinate (in millipoints)
  607. * @param y the y coordinate (in millipoints)
  608. */
  609. void setCursorPos(float x, float y) {
  610. try {
  611. Point2D transPoint = transformedPoint(x, y);
  612. gen.setCursorPos(transPoint.getX(), transPoint.getY());
  613. } catch (IOException ioe) {
  614. handleIOTrouble(ioe);
  615. }
  616. }
  617. /**
  618. * @see org.apache.fop.render.AbstractPathOrientedRenderer#clip()
  619. */
  620. protected void clip() {
  621. if (currentPath == null) {
  622. throw new IllegalStateException("No current path available!");
  623. }
  624. //TODO Find a good way to do clipping. PCL itself cannot clip.
  625. currentPath = null;
  626. }
  627. /**
  628. * @see org.apache.fop.render.AbstractPathOrientedRenderer#closePath()
  629. */
  630. protected void closePath() {
  631. currentPath.closePath();
  632. }
  633. /**
  634. * @see org.apache.fop.render.AbstractPathOrientedRenderer#lineTo(float, float)
  635. */
  636. protected void lineTo(float x, float y) {
  637. if (currentPath == null) {
  638. currentPath = new GeneralPath();
  639. }
  640. currentPath.lineTo(x, y);
  641. }
  642. /**
  643. * @see org.apache.fop.render.AbstractPathOrientedRenderer#moveTo(float, float)
  644. */
  645. protected void moveTo(float x, float y) {
  646. if (currentPath == null) {
  647. currentPath = new GeneralPath();
  648. }
  649. currentPath.moveTo(x, y);
  650. }
  651. /**
  652. * Fill a rectangular area.
  653. * @param x the x coordinate (in pt)
  654. * @param y the y coordinate (in pt)
  655. * @param width the width of the rectangle
  656. * @param height the height of the rectangle
  657. */
  658. protected void fillRect(float x, float y, float width, float height) {
  659. try {
  660. setCursorPos(x * 1000, y * 1000);
  661. gen.fillRect((int)(width * 1000), (int)(height * 1000),
  662. this.currentFillColor);
  663. } catch (IOException ioe) {
  664. handleIOTrouble(ioe);
  665. }
  666. }
  667. /**
  668. * Sets the new current fill color.
  669. * @param color the color
  670. */
  671. protected void updateFillColor(java.awt.Color color) {
  672. this.currentFillColor = color;
  673. }
  674. /**
  675. * @see org.apache.fop.render.AbstractRenderer#renderWord(org.apache.fop.area.inline.WordArea)
  676. */
  677. protected void renderWord(WordArea word) {
  678. //Font font = getFontFromArea(word.getParentArea());
  679. String s = word.getWord();
  680. try {
  681. gen.writeText(s);
  682. } catch (IOException ioe) {
  683. handleIOTrouble(ioe);
  684. }
  685. super.renderWord(word);
  686. }
  687. /**
  688. * @see org.apache.fop.render.AbstractRenderer#renderSpace(org.apache.fop.area.inline.SpaceArea)
  689. */
  690. protected void renderSpace(SpaceArea space) {
  691. AbstractTextArea textArea = (AbstractTextArea)space.getParentArea();
  692. String s = space.getSpace();
  693. char sp = s.charAt(0);
  694. Font font = getFontFromArea(textArea);
  695. int tws = (space.isAdjustable()
  696. ? textArea.getTextWordSpaceAdjust()
  697. + 2 * textArea.getTextLetterSpaceAdjust()
  698. : 0);
  699. double dx = (font.getCharWidth(sp) + tws) / 100f;
  700. try {
  701. gen.writeCommand("&a+" + gen.formatDouble2(dx) + "H");
  702. } catch (IOException ioe) {
  703. handleIOTrouble(ioe);
  704. }
  705. super.renderSpace(space);
  706. }
  707. /**
  708. * Render an inline viewport.
  709. * This renders an inline viewport by clipping if necessary.
  710. * @param viewport the viewport to handle
  711. * @todo Copied from AbstractPathOrientedRenderer
  712. */
  713. public void renderViewport(Viewport viewport) {
  714. float x = currentIPPosition / 1000f;
  715. float y = (currentBPPosition + viewport.getOffset()) / 1000f;
  716. float width = viewport.getIPD() / 1000f;
  717. float height = viewport.getBPD() / 1000f;
  718. // TODO: Calculate the border rect correctly.
  719. float borderPaddingStart = viewport.getBorderAndPaddingWidthStart() / 1000f;
  720. float borderPaddingBefore = viewport.getBorderAndPaddingWidthBefore() / 1000f;
  721. float bpwidth = borderPaddingStart
  722. + (viewport.getBorderAndPaddingWidthEnd() / 1000f);
  723. float bpheight = borderPaddingBefore
  724. + (viewport.getBorderAndPaddingWidthAfter() / 1000f);
  725. drawBackAndBorders(viewport, x, y, width + bpwidth, height + bpheight);
  726. if (viewport.getClip()) {
  727. saveGraphicsState();
  728. clipRect(x + borderPaddingStart, y + borderPaddingBefore, width, height);
  729. }
  730. super.renderViewport(viewport);
  731. if (viewport.getClip()) {
  732. restoreGraphicsState();
  733. }
  734. }
  735. /**
  736. * @see org.apache.fop.render.AbstractRenderer#renderBlockViewport(BlockViewport, List)
  737. */
  738. protected void renderBlockViewport(BlockViewport bv, List children) {
  739. // clip and position viewport if necessary
  740. // save positions
  741. int saveIP = currentIPPosition;
  742. int saveBP = currentBPPosition;
  743. //String saveFontName = currentFontName;
  744. CTM ctm = bv.getCTM();
  745. int borderPaddingStart = bv.getBorderAndPaddingWidthStart();
  746. int borderPaddingBefore = bv.getBorderAndPaddingWidthBefore();
  747. float x, y;
  748. x = (float)(bv.getXOffset() + containingIPPosition) / 1000f;
  749. y = (float)(bv.getYOffset() + containingBPPosition) / 1000f;
  750. //This is the content-rect
  751. float width = (float)bv.getIPD() / 1000f;
  752. float height = (float)bv.getBPD() / 1000f;
  753. if (bv.getPositioning() == Block.ABSOLUTE
  754. || bv.getPositioning() == Block.FIXED) {
  755. currentIPPosition = bv.getXOffset();
  756. currentBPPosition = bv.getYOffset();
  757. //For FIXED, we need to break out of the current viewports to the
  758. //one established by the page. We save the state stack for restoration
  759. //after the block-container has been painted. See below.
  760. List breakOutList = null;
  761. if (bv.getPositioning() == Block.FIXED) {
  762. breakOutList = breakOutOfStateStack();
  763. }
  764. CTM tempctm = new CTM(containingIPPosition, containingBPPosition);
  765. ctm = tempctm.multiply(ctm);
  766. //Adjust for spaces (from margin or indirectly by start-indent etc.
  767. x += bv.getSpaceStart() / 1000f;
  768. currentIPPosition += bv.getSpaceStart();
  769. y += bv.getSpaceBefore() / 1000f;
  770. currentBPPosition += bv.getSpaceBefore();
  771. float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd()) / 1000f;
  772. float bpheight = (borderPaddingBefore + bv.getBorderAndPaddingWidthAfter()) / 1000f;
  773. drawBackAndBorders(bv, x, y, width + bpwidth, height + bpheight);
  774. //Now adjust for border/padding
  775. currentIPPosition += borderPaddingStart;
  776. currentBPPosition += borderPaddingBefore;
  777. Rectangle2D clippingRect = null;
  778. if (bv.getClip()) {
  779. clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
  780. bv.getIPD(), bv.getBPD());
  781. }
  782. startVParea(ctm, clippingRect);
  783. currentIPPosition = 0;
  784. currentBPPosition = 0;
  785. renderBlocks(bv, children);
  786. endVParea();
  787. if (breakOutList != null) {
  788. restoreStateStackAfterBreakOut(breakOutList);
  789. }
  790. currentIPPosition = saveIP;
  791. currentBPPosition = saveBP;
  792. } else {
  793. currentBPPosition += bv.getSpaceBefore();
  794. //borders and background in the old coordinate system
  795. handleBlockTraits(bv);
  796. //Advance to start of content area
  797. currentIPPosition += bv.getStartIndent();
  798. CTM tempctm = new CTM(containingIPPosition, currentBPPosition);
  799. ctm = tempctm.multiply(ctm);
  800. //Now adjust for border/padding
  801. currentBPPosition += borderPaddingBefore;
  802. Rectangle2D clippingRect = null;
  803. if (bv.getClip()) {
  804. clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
  805. bv.getIPD(), bv.getBPD());
  806. }
  807. startVParea(ctm, clippingRect);
  808. currentIPPosition = 0;
  809. currentBPPosition = 0;
  810. renderBlocks(bv, children);
  811. endVParea();
  812. currentIPPosition = saveIP;
  813. currentBPPosition = saveBP;
  814. currentBPPosition += (int)(bv.getAllocBPD());
  815. }
  816. //currentFontName = saveFontName;
  817. }
  818. private List breakOutOfStateStack() {
  819. log.debug("Block.FIXED --> break out");
  820. List breakOutList = new java.util.ArrayList();
  821. while (!this.graphicContextStack.empty()) {
  822. breakOutList.add(0, this.graphicContext);
  823. restoreGraphicsState();
  824. }
  825. return breakOutList;
  826. }
  827. private void restoreStateStackAfterBreakOut(List breakOutList) {
  828. log.debug("Block.FIXED --> restoring context after break-out");
  829. for (int i = 0, c = breakOutList.size(); i < c; i++) {
  830. saveGraphicsState();
  831. this.graphicContext = (GraphicContext)breakOutList.get(i);
  832. }
  833. }
  834. /**
  835. * @see org.apache.fop.render.AbstractRenderer#renderImage(Image, Rectangle2D)
  836. */
  837. public void renderImage(Image image, Rectangle2D pos) {
  838. drawImage(image.getURL(), pos, image.getForeignAttributes());
  839. }
  840. /**
  841. * Draw an image at the indicated location.
  842. * @param url the URI/URL of the image
  843. * @param pos the position of the image
  844. * @param foreignAttributes an optional Map with foreign attributes, may be null
  845. */
  846. protected void drawImage(String url, Rectangle2D pos, Map foreignAttributes) {
  847. url = ImageFactory.getURL(url);
  848. ImageFactory fact = userAgent.getFactory().getImageFactory();
  849. FopImage fopimage = fact.getImage(url, userAgent);
  850. if (fopimage == null) {
  851. return;
  852. }
  853. if (!fopimage.load(FopImage.DIMENSIONS)) {
  854. return;
  855. }
  856. String mime = fopimage.getMimeType();
  857. if ("text/xml".equals(mime)) {
  858. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  859. return;
  860. }
  861. Document doc = ((XMLImage) fopimage).getDocument();
  862. String ns = ((XMLImage) fopimage).getNameSpace();
  863. renderDocument(doc, ns, pos, foreignAttributes);
  864. } else if ("image/svg+xml".equals(mime)) {
  865. if (!fopimage.load(FopImage.ORIGINAL_DATA)) {
  866. return;
  867. }
  868. Document doc = ((XMLImage) fopimage).getDocument();
  869. String ns = ((XMLImage) fopimage).getNameSpace();
  870. renderDocument(doc, ns, pos, foreignAttributes);
  871. } else if (fopimage instanceof EPSImage) {
  872. log.warn("EPS images are not supported by this renderer");
  873. } else {
  874. if (!fopimage.load(FopImage.BITMAP)) {
  875. log.error("Bitmap image could not be processed: " + fopimage);
  876. return;
  877. }
  878. byte[] imgmap = fopimage.getBitmaps();
  879. ColorModel cm = new ComponentColorModel(
  880. ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB),
  881. new int[] {8, 8, 8},
  882. false, false,
  883. ColorModel.OPAQUE, DataBuffer.TYPE_BYTE);
  884. int imgw = fopimage.getWidth();
  885. int imgh = fopimage.getHeight();
  886. SampleModel sampleModel = new PixelInterleavedSampleModel(
  887. DataBuffer.TYPE_BYTE, imgw, imgh, 3, imgw * 3, new int[] {0, 1, 2});
  888. DataBuffer dbuf = new DataBufferByte(imgmap, imgw * imgh * 3);
  889. WritableRaster raster = Raster.createWritableRaster(sampleModel,
  890. dbuf, null);
  891. // Combine the color model and raster into a buffered image
  892. RenderedImage img = new BufferedImage(cm, raster, false, null);
  893. try {
  894. setCursorPos(this.currentIPPosition + (int)pos.getX(),
  895. this.currentBPPosition + (int)pos.getY());
  896. gen.paintBitmap(img,
  897. new Dimension((int)pos.getWidth(), (int)pos.getHeight()),
  898. false);
  899. } catch (IOException ioe) {
  900. handleIOTrouble(ioe);
  901. }
  902. }
  903. }
  904. /**
  905. * @see org.apache.fop.render.AbstractRenderer#renderForeignObject(ForeignObject, Rectangle2D)
  906. */
  907. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  908. Document doc = fo.getDocument();
  909. String ns = fo.getNameSpace();
  910. renderDocument(doc, ns, pos, fo.getForeignAttributes());
  911. }
  912. /**
  913. * Common method to render the background and borders for any inline area.
  914. * The all borders and padding are drawn outside the specified area.
  915. * @param area the inline area for which the background, border and padding is to be
  916. * rendered
  917. * @todo Copied from AbstractPathOrientedRenderer
  918. */
  919. protected void renderInlineAreaBackAndBorders(InlineArea area) {
  920. float x = currentIPPosition / 1000f;
  921. float y = (currentBPPosition + area.getOffset()) / 1000f;
  922. float width = area.getIPD() / 1000f;
  923. float height = area.getBPD() / 1000f;
  924. float borderPaddingStart = area.getBorderAndPaddingWidthStart() / 1000f;
  925. float borderPaddingBefore = area.getBorderAndPaddingWidthBefore() / 1000f;
  926. float bpwidth = borderPaddingStart
  927. + (area.getBorderAndPaddingWidthEnd() / 1000f);
  928. float bpheight = borderPaddingBefore
  929. + (area.getBorderAndPaddingWidthAfter() / 1000f);
  930. if (height != 0.0f || bpheight != 0.0f && bpwidth != 0.0f) {
  931. drawBackAndBorders(area, x, y - borderPaddingBefore
  932. , width + bpwidth
  933. , height + bpheight);
  934. }
  935. }
  936. /**
  937. * Draw the background and borders. This draws the background and border
  938. * traits for an area given the position.
  939. *
  940. * @param area the area whose traits are used
  941. * @param startx the start x position
  942. * @param starty the start y position
  943. * @param width the width of the area
  944. * @param height the height of the area
  945. */
  946. protected void drawBackAndBorders(Area area, float startx, float starty,
  947. float width, float height) {
  948. BorderProps bpsBefore = (BorderProps) area.getTrait(Trait.BORDER_BEFORE);
  949. BorderProps bpsAfter = (BorderProps) area.getTrait(Trait.BORDER_AFTER);
  950. BorderProps bpsStart = (BorderProps) area.getTrait(Trait.BORDER_START);
  951. BorderProps bpsEnd = (BorderProps) area.getTrait(Trait.BORDER_END);
  952. // draw background
  953. Trait.Background back;
  954. back = (Trait.Background) area.getTrait(Trait.BACKGROUND);
  955. if (back != null) {
  956. // Calculate padding rectangle
  957. float sx = startx;
  958. float sy = starty;
  959. float paddRectWidth = width;
  960. float paddRectHeight = height;
  961. if (bpsStart != null) {
  962. sx += bpsStart.width / 1000f;
  963. paddRectWidth -= bpsStart.width / 1000f;
  964. }
  965. if (bpsBefore != null) {
  966. sy += bpsBefore.width / 1000f;
  967. paddRectHeight -= bpsBefore.width / 1000f;
  968. }
  969. if (bpsEnd != null) {
  970. paddRectWidth -= bpsEnd.width / 1000f;
  971. }
  972. if (bpsAfter != null) {
  973. paddRectHeight -= bpsAfter.width / 1000f;
  974. }
  975. if (back.getColor() != null) {
  976. updateFillColor(back.getColor());
  977. fillRect(sx, sy, paddRectWidth, paddRectHeight);
  978. }
  979. // background image
  980. if (back.getFopImage() != null) {
  981. FopImage fopimage = back.getFopImage();
  982. if (fopimage != null && fopimage.load(FopImage.DIMENSIONS)) {
  983. saveGraphicsState();
  984. clipRect(sx, sy, paddRectWidth, paddRectHeight);
  985. int horzCount = (int) ((paddRectWidth * 1000 / fopimage
  986. .getIntrinsicWidth()) + 1.0f);
  987. int vertCount = (int) ((paddRectHeight * 1000 / fopimage
  988. .getIntrinsicHeight()) + 1.0f);
  989. if (back.getRepeat() == EN_NOREPEAT) {
  990. horzCount = 1;
  991. vertCount = 1;
  992. } else if (back.getRepeat() == EN_REPEATX) {
  993. vertCount = 1;
  994. } else if (back.getRepeat() == EN_REPEATY) {
  995. horzCount = 1;
  996. }
  997. // change from points to millipoints
  998. sx *= 1000;
  999. sy *= 1000;
  1000. if (horzCount == 1) {
  1001. sx += back.getHoriz();
  1002. }
  1003. if (vertCount == 1) {
  1004. sy += back.getVertical();
  1005. }
  1006. for (int x = 0; x < horzCount; x++) {
  1007. for (int y = 0; y < vertCount; y++) {
  1008. // place once
  1009. Rectangle2D pos;
  1010. // Image positions are relative to the currentIP/BP
  1011. pos = new Rectangle2D.Float(
  1012. sx - currentIPPosition
  1013. + (x * fopimage.getIntrinsicWidth()),
  1014. sy - currentBPPosition
  1015. + (y * fopimage.getIntrinsicHeight()),
  1016. fopimage.getIntrinsicWidth(),
  1017. fopimage.getIntrinsicHeight());
  1018. drawImage(back.getURL(), pos, null);
  1019. }
  1020. }
  1021. restoreGraphicsState();
  1022. } else {
  1023. log.warn(
  1024. "Can't find background image: " + back.getURL());
  1025. }
  1026. }
  1027. }
  1028. Rectangle2D.Float borderRect = new Rectangle2D.Float(startx, starty, width, height);
  1029. drawBorders(borderRect, bpsBefore, bpsAfter, bpsStart, bpsEnd);
  1030. }
  1031. /**
  1032. * Draws borders.
  1033. * @param borderRect the border rectangle
  1034. * @param bpsBefore the border specification on the before side
  1035. * @param bpsAfter the border specification on the after side
  1036. * @param bpsStart the border specification on the start side
  1037. * @param bpsEnd the border specification on the end side
  1038. */
  1039. protected void drawBorders(Rectangle2D.Float borderRect,
  1040. final BorderProps bpsBefore, final BorderProps bpsAfter,
  1041. final BorderProps bpsStart, final BorderProps bpsEnd) {
  1042. if (bpsBefore == null && bpsAfter == null && bpsStart == null && bpsEnd == null) {
  1043. return; //no borders to paint
  1044. }
  1045. if (qualityBeforeSpeed) {
  1046. drawQualityBorders(borderRect, bpsBefore, bpsAfter, bpsStart, bpsEnd);
  1047. } else {
  1048. drawFastBorders(borderRect, bpsBefore, bpsAfter, bpsStart, bpsEnd);
  1049. }
  1050. }
  1051. /**
  1052. * Draws borders. Borders are drawn as shaded rectangles with no clipping.
  1053. * @param borderRect the border rectangle
  1054. * @param bpsBefore the border specification on the before side
  1055. * @param bpsAfter the border specification on the after side
  1056. * @param bpsStart the border specification on the start side
  1057. * @param bpsEnd the border specification on the end side
  1058. */
  1059. protected void drawFastBorders(Rectangle2D.Float borderRect,
  1060. final BorderProps bpsBefore, final BorderProps bpsAfter,
  1061. final BorderProps bpsStart, final BorderProps bpsEnd) {
  1062. float startx = borderRect.x;
  1063. float starty = borderRect.y;
  1064. float width = borderRect.width;
  1065. float height = borderRect.height;
  1066. if (bpsBefore != null) {
  1067. float borderWidth = bpsBefore.width / 1000f;
  1068. updateFillColor(bpsBefore.color);
  1069. fillRect(startx, starty, width, borderWidth);
  1070. }
  1071. if (bpsAfter != null) {
  1072. float borderWidth = bpsAfter.width / 1000f;
  1073. updateFillColor(bpsAfter.color);
  1074. fillRect(startx, (starty + height - borderWidth),
  1075. width, borderWidth);
  1076. }
  1077. if (bpsStart != null) {
  1078. float borderWidth = bpsStart.width / 1000f;
  1079. updateFillColor(bpsStart.color);
  1080. fillRect(startx, starty, borderWidth, height);
  1081. }
  1082. if (bpsEnd != null) {
  1083. float borderWidth = bpsEnd.width / 1000f;
  1084. updateFillColor(bpsEnd.color);
  1085. fillRect((startx + width - borderWidth), starty, borderWidth, height);
  1086. }
  1087. }
  1088. /**
  1089. * Draws borders. Borders are drawn in-memory and painted as a bitmap.
  1090. * @param borderRect the border rectangle
  1091. * @param bpsBefore the border specification on the before side
  1092. * @param bpsAfter the border specification on the after side
  1093. * @param bpsStart the border specification on the start side
  1094. * @param bpsEnd the border specification on the end side
  1095. */
  1096. protected void drawQualityBorders(Rectangle2D.Float borderRect,
  1097. final BorderProps bpsBefore, final BorderProps bpsAfter,
  1098. final BorderProps bpsStart, final BorderProps bpsEnd) {
  1099. Graphics2DAdapter g2a = getGraphics2DAdapter();
  1100. final Rectangle.Float effBorderRect = new Rectangle2D.Float(
  1101. borderRect.x - (currentIPPosition / 1000f),
  1102. borderRect.y - (currentBPPosition / 1000f),
  1103. borderRect.width,
  1104. borderRect.height);
  1105. final Rectangle paintRect = new Rectangle(
  1106. (int)Math.round(borderRect.x * 1000f),
  1107. (int)Math.round(borderRect.y * 1000f),
  1108. (int)Math.floor(borderRect.width * 1000f) + 1,
  1109. (int)Math.floor(borderRect.height * 1000f) + 1);
  1110. //Add one pixel wide safety margin around the paint area
  1111. int pixelWidth = (int)Math.round(UnitConv.in2mpt(1) / userAgent.getTargetResolution());
  1112. final int xoffset = (int)Math.round(-effBorderRect.x * 1000f) + pixelWidth;
  1113. final int yoffset = pixelWidth;
  1114. paintRect.x += xoffset;
  1115. paintRect.y += yoffset;
  1116. paintRect.width += 2 * pixelWidth;
  1117. paintRect.height += 2 * pixelWidth;
  1118. RendererContext rc = createRendererContext(paintRect.x, paintRect.y,
  1119. paintRect.width, paintRect.height, null);
  1120. Map atts = new java.util.HashMap();
  1121. atts.put(CONV_MODE, "bitmap");
  1122. atts.put(SRC_TRANSPARENCY, "true");
  1123. rc.setProperty(RendererContextConstants.FOREIGN_ATTRIBUTES, atts);
  1124. Graphics2DImagePainter painter = new Graphics2DImagePainter() {
  1125. public void paint(Graphics2D g2d, Rectangle2D area) {
  1126. g2d.translate(xoffset, yoffset);
  1127. g2d.scale(1000, 1000);
  1128. float startx = effBorderRect.x;
  1129. float starty = effBorderRect.y;
  1130. float width = effBorderRect.width;
  1131. float height = effBorderRect.height;
  1132. boolean[] b = new boolean[] {
  1133. (bpsBefore != null), (bpsEnd != null),
  1134. (bpsAfter != null), (bpsStart != null)};
  1135. if (!b[0] && !b[1] && !b[2] && !b[3]) {
  1136. return;
  1137. }
  1138. float[] bw = new float[] {
  1139. (b[0] ? bpsBefore.width / 1000f : 0.0f),
  1140. (b[1] ? bpsEnd.width / 1000f : 0.0f),
  1141. (b[2] ? bpsAfter.width / 1000f : 0.0f),
  1142. (b[3] ? bpsStart.width / 1000f : 0.0f)};
  1143. float[] clipw = new float[] {
  1144. BorderProps.getClippedWidth(bpsBefore) / 1000f,
  1145. BorderProps.getClippedWidth(bpsEnd) / 1000f,
  1146. BorderProps.getClippedWidth(bpsAfter) / 1000f,
  1147. BorderProps.getClippedWidth(bpsStart) / 1000f};
  1148. starty += clipw[0];
  1149. height -= clipw[0];
  1150. height -= clipw[2];
  1151. startx += clipw[3];
  1152. width -= clipw[3];
  1153. width -= clipw[1];
  1154. boolean[] slant = new boolean[] {
  1155. (b[3] && b[0]), (b[0] && b[1]), (b[1] && b[2]), (b[2] && b[3])};
  1156. if (bpsBefore != null) {
  1157. //endTextObject();
  1158. float sx1 = startx;
  1159. float sx2 = (slant[0] ? sx1 + bw[3] - clipw[3] : sx1);
  1160. float ex1 = startx + width;
  1161. float ex2 = (slant[1] ? ex1 - bw[1] + clipw[1] : ex1);
  1162. float outery = starty - clipw[0];
  1163. float clipy = outery + clipw[0];
  1164. float innery = outery + bw[0];
  1165. //saveGraphicsState();
  1166. Graphics2D g = (Graphics2D)g2d.create();
  1167. moveTo(sx1, clipy);
  1168. float sx1a = sx1;
  1169. float ex1a = ex1;
  1170. if (bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  1171. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  1172. sx1a -= clipw[3];
  1173. }
  1174. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  1175. ex1a += clipw[1];
  1176. }
  1177. lineTo(sx1a, outery);
  1178. lineTo(ex1a, outery);
  1179. }
  1180. lineTo(ex1, clipy);
  1181. lineTo(ex2, innery);
  1182. lineTo(sx2, innery);
  1183. closePath();
  1184. //clip();
  1185. g.clip(currentPath);
  1186. currentPath = null;
  1187. Rectangle2D.Float lineRect = new Rectangle2D.Float(
  1188. sx1a, outery, ex1a - sx1a, innery - outery);
  1189. Java2DRenderer.drawBorderLine(lineRect, true, true,
  1190. bpsBefore.style, bpsBefore.color, g);
  1191. //restoreGraphicsState();
  1192. }
  1193. if (bpsEnd != null) {
  1194. //endTextObject();
  1195. float sy1 = starty;
  1196. float sy2 = (slant[1] ? sy1 + bw[0] - clipw[0] : sy1);
  1197. float ey1 = starty + height;
  1198. float ey2 = (slant[2] ? ey1 - bw[2] + clipw[2] : ey1);
  1199. float outerx = startx + width + clipw[1];
  1200. float clipx = outerx - clipw[1];
  1201. float innerx = outerx - bw[1];
  1202. //saveGraphicsState();
  1203. Graphics2D g = (Graphics2D)g2d.create();
  1204. moveTo(clipx, sy1);
  1205. float sy1a = sy1;
  1206. float ey1a = ey1;
  1207. if (bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  1208. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  1209. sy1a -= clipw[0];
  1210. }
  1211. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  1212. ey1a += clipw[2];
  1213. }
  1214. lineTo(outerx, sy1a);
  1215. lineTo(outerx, ey1a);
  1216. }
  1217. lineTo(clipx, ey1);
  1218. lineTo(innerx, ey2);
  1219. lineTo(innerx, sy2);
  1220. closePath();
  1221. //clip();
  1222. g.setClip(currentPath);
  1223. currentPath = null;
  1224. Rectangle2D.Float lineRect = new Rectangle2D.Float(
  1225. innerx, sy1a, outerx - innerx, ey1a - sy1a);
  1226. Java2DRenderer.drawBorderLine(lineRect, false, false,
  1227. bpsEnd.style, bpsEnd.color, g);
  1228. //restoreGraphicsState();
  1229. }
  1230. if (bpsAfter != null) {
  1231. //endTextObject();
  1232. float sx1 = startx;
  1233. float sx2 = (slant[3] ? sx1 + bw[3] - clipw[3] : sx1);
  1234. float ex1 = startx + width;
  1235. float ex2 = (slant[2] ? ex1 - bw[1] + clipw[1] : ex1);
  1236. float outery = starty + height + clipw[2];
  1237. float clipy = outery - clipw[2];
  1238. float innery = outery - bw[2];
  1239. //saveGraphicsState();
  1240. Graphics2D g = (Graphics2D)g2d.create();
  1241. moveTo(ex1, clipy);
  1242. float sx1a = sx1;
  1243. float ex1a = ex1;
  1244. if (bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  1245. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  1246. sx1a -= clipw[3];
  1247. }
  1248. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  1249. ex1a += clipw[1];
  1250. }
  1251. lineTo(ex1a, outery);
  1252. lineTo(sx1a, outery);
  1253. }
  1254. lineTo(sx1, clipy);
  1255. lineTo(sx2, innery);
  1256. lineTo(ex2, innery);
  1257. closePath();
  1258. //clip();
  1259. g.setClip(currentPath);
  1260. currentPath = null;
  1261. Rectangle2D.Float lineRect = new Rectangle2D.Float(
  1262. sx1a, innery, ex1a - sx1a, outery - innery);
  1263. Java2DRenderer.drawBorderLine(lineRect, true, false,
  1264. bpsAfter.style, bpsAfter.color, g);
  1265. //restoreGraphicsState();
  1266. }
  1267. if (bpsStart != null) {
  1268. //endTextObject();
  1269. float sy1 = starty;
  1270. float sy2 = (slant[0] ? sy1 + bw[0] - clipw[0] : sy1);
  1271. float ey1 = sy1 + height;
  1272. float ey2 = (slant[3] ? ey1 - bw[2] + clipw[2] : ey1);
  1273. float outerx = startx - clipw[3];
  1274. float clipx = outerx + clipw[3];
  1275. float innerx = outerx + bw[3];
  1276. //saveGraphicsState();
  1277. Graphics2D g = (Graphics2D)g2d.create();
  1278. moveTo(clipx, ey1);
  1279. float sy1a = sy1;
  1280. float ey1a = ey1;
  1281. if (bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  1282. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  1283. sy1a -= clipw[0];
  1284. }
  1285. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  1286. ey1a += clipw[2];
  1287. }
  1288. lineTo(outerx, ey1a);
  1289. lineTo(outerx, sy1a);
  1290. }
  1291. lineTo(clipx, sy1);
  1292. lineTo(innerx, sy2);
  1293. lineTo(innerx, ey2);
  1294. closePath();
  1295. //clip();
  1296. g.setClip(currentPath);
  1297. currentPath = null;
  1298. Rectangle2D.Float lineRect = new Rectangle2D.Float(
  1299. outerx, sy1a, innerx - outerx, ey1a - sy1a);
  1300. Java2DRenderer.drawBorderLine(lineRect, false, false,
  1301. bpsStart.style, bpsStart.color, g);
  1302. //restoreGraphicsState();
  1303. }
  1304. }
  1305. public Dimension getImageSize() {
  1306. return paintRect.getSize();
  1307. }
  1308. };
  1309. try {
  1310. g2a.paintImage(painter, rc,
  1311. paintRect.x - xoffset, paintRect.y, paintRect.width, paintRect.height);
  1312. } catch (IOException ioe) {
  1313. handleIOTrouble(ioe);
  1314. }
  1315. }
  1316. public void setAllTextAsBitmaps(boolean allTextAsBitmaps) {
  1317. this.allTextAsBitmaps = allTextAsBitmaps;
  1318. }
  1319. }