You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PCLRenderer.java 58KB

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