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.

AbstractPathOrientedRenderer.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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;
  19. import java.awt.Color;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Rectangle2D;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.w3c.dom.Document;
  26. import org.apache.batik.parser.AWTTransformProducer;
  27. import org.apache.xmlgraphics.image.loader.ImageSize;
  28. import org.apache.fop.area.Area;
  29. import org.apache.fop.area.Block;
  30. import org.apache.fop.area.BlockViewport;
  31. import org.apache.fop.area.CTM;
  32. import org.apache.fop.area.NormalFlow;
  33. import org.apache.fop.area.RegionViewport;
  34. import org.apache.fop.area.Trait;
  35. import org.apache.fop.area.inline.ForeignObject;
  36. import org.apache.fop.area.inline.InlineArea;
  37. import org.apache.fop.area.inline.Viewport;
  38. import org.apache.fop.fo.Constants;
  39. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  40. import org.apache.fop.fonts.FontMetrics;
  41. import org.apache.fop.traits.BorderProps;
  42. import org.apache.fop.util.QName;
  43. /**
  44. * Abstract base class for renderers like PDF and PostScript where many painting operations
  45. * follow similar patterns which makes it possible to share some code.
  46. */
  47. public abstract class AbstractPathOrientedRenderer extends PrintRenderer {
  48. /**
  49. * Handle block traits.
  50. * The block could be any sort of block with any positioning
  51. * so this should render the traits such as border and background
  52. * in its position.
  53. *
  54. * @param block the block to render the traits
  55. */
  56. protected void handleBlockTraits(Block block) {
  57. int borderPaddingStart = block.getBorderAndPaddingWidthStart();
  58. int borderPaddingBefore = block.getBorderAndPaddingWidthBefore();
  59. float startx = currentIPPosition / 1000f;
  60. float starty = currentBPPosition / 1000f;
  61. float width = block.getIPD() / 1000f;
  62. float height = block.getBPD() / 1000f;
  63. /* using start-indent now
  64. Integer spaceStart = (Integer) block.getTrait(Trait.SPACE_START);
  65. if (spaceStart != null) {
  66. startx += spaceStart.floatValue() / 1000f;
  67. }*/
  68. startx += block.getStartIndent() / 1000f;
  69. startx -= block.getBorderAndPaddingWidthStart() / 1000f;
  70. width += borderPaddingStart / 1000f;
  71. width += block.getBorderAndPaddingWidthEnd() / 1000f;
  72. height += borderPaddingBefore / 1000f;
  73. height += block.getBorderAndPaddingWidthAfter() / 1000f;
  74. drawBackAndBorders(block, startx, starty,
  75. width, height);
  76. }
  77. /**
  78. * Handle the traits for a region
  79. * This is used to draw the traits for the given page region.
  80. * (See Sect. 6.4.1.2 of XSL-FO spec.)
  81. * @param region the RegionViewport whose region is to be drawn
  82. */
  83. protected void handleRegionTraits(RegionViewport region) {
  84. Rectangle2D viewArea = region.getViewArea();
  85. float startx = (float)(viewArea.getX() / 1000f);
  86. float starty = (float)(viewArea.getY() / 1000f);
  87. float width = (float)(viewArea.getWidth() / 1000f);
  88. float height = (float)(viewArea.getHeight() / 1000f);
  89. if (region.getRegionReference().getRegionClass() == FO_REGION_BODY) {
  90. currentBPPosition = region.getBorderAndPaddingWidthBefore();
  91. currentIPPosition = region.getBorderAndPaddingWidthStart();
  92. }
  93. drawBackAndBorders(region, startx, starty, width, height);
  94. }
  95. /**
  96. * Draw the background and borders.
  97. * This draws the background and border traits for an area given
  98. * the position.
  99. *
  100. * @param area the area to get the traits from
  101. * @param startx the start x position
  102. * @param starty the start y position
  103. * @param width the width of the area
  104. * @param height the height of the area
  105. */
  106. protected void drawBackAndBorders(Area area,
  107. float startx, float starty,
  108. float width, float height) {
  109. // draw background then border
  110. BorderProps bpsBefore = (BorderProps)area.getTrait(Trait.BORDER_BEFORE);
  111. BorderProps bpsAfter = (BorderProps)area.getTrait(Trait.BORDER_AFTER);
  112. BorderProps bpsStart = (BorderProps)area.getTrait(Trait.BORDER_START);
  113. BorderProps bpsEnd = (BorderProps)area.getTrait(Trait.BORDER_END);
  114. Trait.Background back;
  115. back = (Trait.Background)area.getTrait(Trait.BACKGROUND);
  116. if (back != null) {
  117. endTextObject();
  118. //Calculate padding rectangle
  119. float sx = startx;
  120. float sy = starty;
  121. float paddRectWidth = width;
  122. float paddRectHeight = height;
  123. if (bpsStart != null) {
  124. sx += bpsStart.width / 1000f;
  125. paddRectWidth -= bpsStart.width / 1000f;
  126. }
  127. if (bpsBefore != null) {
  128. sy += bpsBefore.width / 1000f;
  129. paddRectHeight -= bpsBefore.width / 1000f;
  130. }
  131. if (bpsEnd != null) {
  132. paddRectWidth -= bpsEnd.width / 1000f;
  133. }
  134. if (bpsAfter != null) {
  135. paddRectHeight -= bpsAfter.width / 1000f;
  136. }
  137. if (back.getColor() != null) {
  138. updateColor(back.getColor(), true);
  139. fillRect(sx, sy, paddRectWidth, paddRectHeight);
  140. }
  141. if (back.getImageInfo() != null) {
  142. ImageSize imageSize = back.getImageInfo().getSize();
  143. saveGraphicsState();
  144. clipRect(sx, sy, paddRectWidth, paddRectHeight);
  145. int horzCount = (int)((paddRectWidth
  146. * 1000 / imageSize.getWidthMpt()) + 1.0f);
  147. int vertCount = (int)((paddRectHeight
  148. * 1000 / imageSize.getHeightMpt()) + 1.0f);
  149. if (back.getRepeat() == EN_NOREPEAT) {
  150. horzCount = 1;
  151. vertCount = 1;
  152. } else if (back.getRepeat() == EN_REPEATX) {
  153. vertCount = 1;
  154. } else if (back.getRepeat() == EN_REPEATY) {
  155. horzCount = 1;
  156. }
  157. //change from points to millipoints
  158. sx *= 1000;
  159. sy *= 1000;
  160. if (horzCount == 1) {
  161. sx += back.getHoriz();
  162. }
  163. if (vertCount == 1) {
  164. sy += back.getVertical();
  165. }
  166. for (int x = 0; x < horzCount; x++) {
  167. for (int y = 0; y < vertCount; y++) {
  168. // place once
  169. Rectangle2D pos;
  170. // Image positions are relative to the currentIP/BP
  171. pos = new Rectangle2D.Float(sx - currentIPPosition
  172. + (x * imageSize.getWidthMpt()),
  173. sy - currentBPPosition
  174. + (y * imageSize.getHeightMpt()),
  175. imageSize.getWidthMpt(),
  176. imageSize.getHeightMpt());
  177. drawImage(back.getURL(), pos);
  178. }
  179. }
  180. restoreGraphicsState();
  181. }
  182. }
  183. Rectangle2D.Float borderRect = new Rectangle2D.Float(startx, starty, width, height);
  184. drawBorders(borderRect, bpsBefore, bpsAfter, bpsStart, bpsEnd);
  185. }
  186. /**
  187. * Draws borders.
  188. * @param borderRect the border rectangle
  189. * @param bpsBefore the border specification on the before side
  190. * @param bpsAfter the border specification on the after side
  191. * @param bpsStart the border specification on the start side
  192. * @param bpsEnd the border specification on the end side
  193. */
  194. protected void drawBorders(Rectangle2D.Float borderRect,
  195. BorderProps bpsBefore, BorderProps bpsAfter, BorderProps bpsStart, BorderProps bpsEnd) {
  196. float startx = borderRect.x;
  197. float starty = borderRect.y;
  198. float width = borderRect.width;
  199. float height = borderRect.height;
  200. boolean[] b = new boolean[] {
  201. (bpsBefore != null), (bpsEnd != null),
  202. (bpsAfter != null), (bpsStart != null)};
  203. if (!b[0] && !b[1] && !b[2] && !b[3]) {
  204. return;
  205. }
  206. float[] bw = new float[] {
  207. (b[0] ? bpsBefore.width / 1000f : 0.0f),
  208. (b[1] ? bpsEnd.width / 1000f : 0.0f),
  209. (b[2] ? bpsAfter.width / 1000f : 0.0f),
  210. (b[3] ? bpsStart.width / 1000f : 0.0f)};
  211. float[] clipw = new float[] {
  212. BorderProps.getClippedWidth(bpsBefore) / 1000f,
  213. BorderProps.getClippedWidth(bpsEnd) / 1000f,
  214. BorderProps.getClippedWidth(bpsAfter) / 1000f,
  215. BorderProps.getClippedWidth(bpsStart) / 1000f};
  216. starty += clipw[0];
  217. height -= clipw[0];
  218. height -= clipw[2];
  219. startx += clipw[3];
  220. width -= clipw[3];
  221. width -= clipw[1];
  222. boolean[] slant = new boolean[] {
  223. (b[3] && b[0]), (b[0] && b[1]), (b[1] && b[2]), (b[2] && b[3])};
  224. if (bpsBefore != null) {
  225. endTextObject();
  226. float sx1 = startx;
  227. float sx2 = (slant[0] ? sx1 + bw[3] - clipw[3] : sx1);
  228. float ex1 = startx + width;
  229. float ex2 = (slant[1] ? ex1 - bw[1] + clipw[1] : ex1);
  230. float outery = starty - clipw[0];
  231. float clipy = outery + clipw[0];
  232. float innery = outery + bw[0];
  233. saveGraphicsState();
  234. moveTo(sx1, clipy);
  235. float sx1a = sx1;
  236. float ex1a = ex1;
  237. if (bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  238. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  239. sx1a -= clipw[3];
  240. }
  241. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  242. ex1a += clipw[1];
  243. }
  244. lineTo(sx1a, outery);
  245. lineTo(ex1a, outery);
  246. }
  247. lineTo(ex1, clipy);
  248. lineTo(ex2, innery);
  249. lineTo(sx2, innery);
  250. closePath();
  251. clip();
  252. drawBorderLine(sx1a, outery, ex1a, innery, true, true,
  253. bpsBefore.style, bpsBefore.color);
  254. restoreGraphicsState();
  255. }
  256. if (bpsEnd != null) {
  257. endTextObject();
  258. float sy1 = starty;
  259. float sy2 = (slant[1] ? sy1 + bw[0] - clipw[0] : sy1);
  260. float ey1 = starty + height;
  261. float ey2 = (slant[2] ? ey1 - bw[2] + clipw[2] : ey1);
  262. float outerx = startx + width + clipw[1];
  263. float clipx = outerx - clipw[1];
  264. float innerx = outerx - bw[1];
  265. saveGraphicsState();
  266. moveTo(clipx, sy1);
  267. float sy1a = sy1;
  268. float ey1a = ey1;
  269. if (bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  270. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  271. sy1a -= clipw[0];
  272. }
  273. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  274. ey1a += clipw[2];
  275. }
  276. lineTo(outerx, sy1a);
  277. lineTo(outerx, ey1a);
  278. }
  279. lineTo(clipx, ey1);
  280. lineTo(innerx, ey2);
  281. lineTo(innerx, sy2);
  282. closePath();
  283. clip();
  284. drawBorderLine(innerx, sy1a, outerx, ey1a, false, false, bpsEnd.style, bpsEnd.color);
  285. restoreGraphicsState();
  286. }
  287. if (bpsAfter != null) {
  288. endTextObject();
  289. float sx1 = startx;
  290. float sx2 = (slant[3] ? sx1 + bw[3] - clipw[3] : sx1);
  291. float ex1 = startx + width;
  292. float ex2 = (slant[2] ? ex1 - bw[1] + clipw[1] : ex1);
  293. float outery = starty + height + clipw[2];
  294. float clipy = outery - clipw[2];
  295. float innery = outery - bw[2];
  296. saveGraphicsState();
  297. moveTo(ex1, clipy);
  298. float sx1a = sx1;
  299. float ex1a = ex1;
  300. if (bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  301. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  302. sx1a -= clipw[3];
  303. }
  304. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  305. ex1a += clipw[1];
  306. }
  307. lineTo(ex1a, outery);
  308. lineTo(sx1a, outery);
  309. }
  310. lineTo(sx1, clipy);
  311. lineTo(sx2, innery);
  312. lineTo(ex2, innery);
  313. closePath();
  314. clip();
  315. drawBorderLine(sx1a, innery, ex1a, outery, true, false, bpsAfter.style, bpsAfter.color);
  316. restoreGraphicsState();
  317. }
  318. if (bpsStart != null) {
  319. endTextObject();
  320. float sy1 = starty;
  321. float sy2 = (slant[0] ? sy1 + bw[0] - clipw[0] : sy1);
  322. float ey1 = sy1 + height;
  323. float ey2 = (slant[3] ? ey1 - bw[2] + clipw[2] : ey1);
  324. float outerx = startx - clipw[3];
  325. float clipx = outerx + clipw[3];
  326. float innerx = outerx + bw[3];
  327. saveGraphicsState();
  328. moveTo(clipx, ey1);
  329. float sy1a = sy1;
  330. float ey1a = ey1;
  331. if (bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  332. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  333. sy1a -= clipw[0];
  334. }
  335. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  336. ey1a += clipw[2];
  337. }
  338. lineTo(outerx, ey1a);
  339. lineTo(outerx, sy1a);
  340. }
  341. lineTo(clipx, sy1);
  342. lineTo(innerx, sy2);
  343. lineTo(innerx, ey2);
  344. closePath();
  345. clip();
  346. drawBorderLine(outerx, sy1a, innerx, ey1a, false, true, bpsStart.style, bpsStart.color);
  347. restoreGraphicsState();
  348. }
  349. }
  350. /**
  351. * Common method to render the background and borders for any inline area.
  352. * The all borders and padding are drawn outside the specified area.
  353. * @param area the inline area for which the background, border and padding is to be
  354. * rendered
  355. */
  356. protected void renderInlineAreaBackAndBorders(InlineArea area) {
  357. float x = currentIPPosition / 1000f;
  358. float y = (currentBPPosition + area.getOffset()) / 1000f;
  359. float width = area.getIPD() / 1000f;
  360. float height = area.getBPD() / 1000f;
  361. float borderPaddingStart = area.getBorderAndPaddingWidthStart() / 1000f;
  362. float borderPaddingBefore = area.getBorderAndPaddingWidthBefore() / 1000f;
  363. float bpwidth = borderPaddingStart
  364. + (area.getBorderAndPaddingWidthEnd() / 1000f);
  365. float bpheight = borderPaddingBefore
  366. + (area.getBorderAndPaddingWidthAfter() / 1000f);
  367. if (height != 0.0f || bpheight != 0.0f && bpwidth != 0.0f) {
  368. drawBackAndBorders(area, x, y - borderPaddingBefore
  369. , width + bpwidth
  370. , height + bpheight);
  371. }
  372. }
  373. private static final QName FOX_TRANSFORM
  374. = new QName(ExtensionElementMapping.URI, "fox:transform");
  375. /** {@inheritDoc} */
  376. protected void renderBlockViewport(BlockViewport bv, List children) {
  377. // clip and position viewport if necessary
  378. // save positions
  379. int saveIP = currentIPPosition;
  380. int saveBP = currentBPPosition;
  381. CTM ctm = bv.getCTM();
  382. int borderPaddingStart = bv.getBorderAndPaddingWidthStart();
  383. int borderPaddingBefore = bv.getBorderAndPaddingWidthBefore();
  384. //This is the content-rect
  385. float width = (float)bv.getIPD() / 1000f;
  386. float height = (float)bv.getBPD() / 1000f;
  387. if (bv.getPositioning() == Block.ABSOLUTE
  388. || bv.getPositioning() == Block.FIXED) {
  389. //For FIXED, we need to break out of the current viewports to the
  390. //one established by the page. We save the state stack for restoration
  391. //after the block-container has been painted. See below.
  392. List breakOutList = null;
  393. if (bv.getPositioning() == Block.FIXED) {
  394. breakOutList = breakOutOfStateStack();
  395. }
  396. AffineTransform positionTransform = new AffineTransform();
  397. positionTransform.translate(bv.getXOffset(), bv.getYOffset());
  398. //"left/"top" (bv.getX/YOffset()) specify the position of the content rectangle
  399. positionTransform.translate(-borderPaddingStart, -borderPaddingBefore);
  400. //Free transformation for the block-container viewport
  401. String transf;
  402. transf = bv.getForeignAttributeValue(FOX_TRANSFORM);
  403. if (transf != null) {
  404. AffineTransform freeTransform = AWTTransformProducer.createAffineTransform(transf);
  405. positionTransform.concatenate(freeTransform);
  406. }
  407. saveGraphicsState();
  408. //Viewport position
  409. concatenateTransformationMatrix(mptToPt(positionTransform));
  410. //Background and borders
  411. float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd()) / 1000f;
  412. float bpheight = (borderPaddingBefore + bv.getBorderAndPaddingWidthAfter()) / 1000f;
  413. drawBackAndBorders(bv, 0, 0, width + bpwidth, height + bpheight);
  414. //Shift to content rectangle after border painting
  415. AffineTransform contentRectTransform = new AffineTransform();
  416. contentRectTransform.translate(borderPaddingStart, borderPaddingBefore);
  417. concatenateTransformationMatrix(mptToPt(contentRectTransform));
  418. //Clipping
  419. if (bv.getClip()) {
  420. clipRect(0f, 0f, width, height);
  421. }
  422. saveGraphicsState();
  423. //Set up coordinate system for content rectangle
  424. AffineTransform contentTransform = ctm.toAffineTransform();
  425. concatenateTransformationMatrix(mptToPt(contentTransform));
  426. currentIPPosition = 0;
  427. currentBPPosition = 0;
  428. renderBlocks(bv, children);
  429. restoreGraphicsState();
  430. restoreGraphicsState();
  431. if (breakOutList != null) {
  432. restoreStateStackAfterBreakOut(breakOutList);
  433. }
  434. currentIPPosition = saveIP;
  435. currentBPPosition = saveBP;
  436. } else {
  437. currentBPPosition += bv.getSpaceBefore();
  438. //borders and background in the old coordinate system
  439. handleBlockTraits(bv);
  440. //Advance to start of content area
  441. currentIPPosition += bv.getStartIndent();
  442. CTM tempctm = new CTM(containingIPPosition, currentBPPosition);
  443. ctm = tempctm.multiply(ctm);
  444. //Now adjust for border/padding
  445. currentBPPosition += borderPaddingBefore;
  446. Rectangle2D clippingRect = null;
  447. if (bv.getClip()) {
  448. clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
  449. bv.getIPD(), bv.getBPD());
  450. }
  451. startVParea(ctm, clippingRect);
  452. currentIPPosition = 0;
  453. currentBPPosition = 0;
  454. renderBlocks(bv, children);
  455. endVParea();
  456. currentIPPosition = saveIP;
  457. currentBPPosition = saveBP;
  458. currentBPPosition += (int)(bv.getAllocBPD());
  459. }
  460. }
  461. /** {@inheritDoc} */
  462. protected void renderReferenceArea(Block block) {
  463. // save position and offset
  464. int saveIP = currentIPPosition;
  465. int saveBP = currentBPPosition;
  466. //Establish a new coordinate system
  467. AffineTransform at = new AffineTransform();
  468. at.translate(currentIPPosition, currentBPPosition);
  469. at.translate(block.getXOffset(), block.getYOffset());
  470. at.translate(0, block.getSpaceBefore());
  471. if (!at.isIdentity()) {
  472. saveGraphicsState();
  473. concatenateTransformationMatrix(mptToPt(at));
  474. }
  475. currentIPPosition = 0;
  476. currentBPPosition = 0;
  477. handleBlockTraits(block);
  478. List children = block.getChildAreas();
  479. if (children != null) {
  480. renderBlocks(block, children);
  481. }
  482. if (!at.isIdentity()) {
  483. restoreGraphicsState();
  484. }
  485. // stacked and relative blocks effect stacking
  486. currentIPPosition = saveIP;
  487. currentBPPosition = saveBP;
  488. }
  489. /** {@inheritDoc} */
  490. protected void renderFlow(NormalFlow flow) {
  491. // save position and offset
  492. int saveIP = currentIPPosition;
  493. int saveBP = currentBPPosition;
  494. //Establish a new coordinate system
  495. AffineTransform at = new AffineTransform();
  496. at.translate(currentIPPosition, currentBPPosition);
  497. if (!at.isIdentity()) {
  498. saveGraphicsState();
  499. concatenateTransformationMatrix(mptToPt(at));
  500. }
  501. currentIPPosition = 0;
  502. currentBPPosition = 0;
  503. super.renderFlow(flow);
  504. if (!at.isIdentity()) {
  505. restoreGraphicsState();
  506. }
  507. // stacked and relative blocks effect stacking
  508. currentIPPosition = saveIP;
  509. currentBPPosition = saveBP;
  510. }
  511. /**
  512. * Concatenates the current transformation matrix with the given one, therefore establishing
  513. * a new coordinate system.
  514. * @param at the transformation matrix to process (coordinates in points)
  515. */
  516. protected abstract void concatenateTransformationMatrix(AffineTransform at);
  517. /**
  518. * Render an inline viewport.
  519. * This renders an inline viewport by clipping if necessary.
  520. * @param viewport the viewport to handle
  521. */
  522. public void renderViewport(Viewport viewport) {
  523. float x = currentIPPosition / 1000f;
  524. float y = (currentBPPosition + viewport.getOffset()) / 1000f;
  525. float width = viewport.getIPD() / 1000f;
  526. float height = viewport.getBPD() / 1000f;
  527. // TODO: Calculate the border rect correctly.
  528. float borderPaddingStart = viewport.getBorderAndPaddingWidthStart() / 1000f;
  529. float borderPaddingBefore = viewport.getBorderAndPaddingWidthBefore() / 1000f;
  530. float bpwidth = borderPaddingStart
  531. + (viewport.getBorderAndPaddingWidthEnd() / 1000f);
  532. float bpheight = borderPaddingBefore
  533. + (viewport.getBorderAndPaddingWidthAfter() / 1000f);
  534. drawBackAndBorders(viewport, x, y, width + bpwidth, height + bpheight);
  535. if (viewport.getClip()) {
  536. saveGraphicsState();
  537. clipRect(x + borderPaddingStart, y + borderPaddingBefore, width, height);
  538. }
  539. super.renderViewport(viewport);
  540. if (viewport.getClip()) {
  541. restoreGraphicsState();
  542. }
  543. }
  544. /**
  545. * Restores the state stack after a break out.
  546. * @param breakOutList the state stack to restore.
  547. */
  548. protected abstract void restoreStateStackAfterBreakOut(List breakOutList);
  549. /**
  550. * Breaks out of the state stack to handle fixed block-containers.
  551. * @return the saved state stack to recreate later
  552. */
  553. protected abstract List breakOutOfStateStack();
  554. /** Saves the graphics state of the rendering engine. */
  555. protected abstract void saveGraphicsState();
  556. /** Restores the last graphics state of the rendering engine. */
  557. protected abstract void restoreGraphicsState();
  558. /** Indicates the beginning of a text object. */
  559. protected abstract void beginTextObject();
  560. /** Indicates the end of a text object. */
  561. protected abstract void endTextObject();
  562. /**
  563. * Paints the text decoration marks.
  564. * @param fm Current typeface
  565. * @param fontsize Current font size
  566. * @param inline inline area to paint the marks for
  567. * @param baseline position of the baseline
  568. * @param startx start IPD
  569. */
  570. protected void renderTextDecoration(FontMetrics fm, int fontsize, InlineArea inline,
  571. int baseline, int startx) {
  572. boolean hasTextDeco = inline.hasUnderline()
  573. || inline.hasOverline()
  574. || inline.hasLineThrough();
  575. if (hasTextDeco) {
  576. endTextObject();
  577. float descender = fm.getDescender(fontsize) / 1000f;
  578. float capHeight = fm.getCapHeight(fontsize) / 1000f;
  579. float halfLineWidth = (descender / -8f) / 2f;
  580. float endx = (startx + inline.getIPD()) / 1000f;
  581. if (inline.hasUnderline()) {
  582. Color ct = (Color) inline.getTrait(Trait.UNDERLINE_COLOR);
  583. float y = baseline - descender / 2f;
  584. drawBorderLine(startx / 1000f, (y - halfLineWidth) / 1000f,
  585. endx, (y + halfLineWidth) / 1000f,
  586. true, true, Constants.EN_SOLID, ct);
  587. }
  588. if (inline.hasOverline()) {
  589. Color ct = (Color) inline.getTrait(Trait.OVERLINE_COLOR);
  590. float y = (float)(baseline - (1.1 * capHeight));
  591. drawBorderLine(startx / 1000f, (y - halfLineWidth) / 1000f,
  592. endx, (y + halfLineWidth) / 1000f,
  593. true, true, Constants.EN_SOLID, ct);
  594. }
  595. if (inline.hasLineThrough()) {
  596. Color ct = (Color) inline.getTrait(Trait.LINETHROUGH_COLOR);
  597. float y = (float)(baseline - (0.45 * capHeight));
  598. drawBorderLine(startx / 1000f, (y - halfLineWidth) / 1000f,
  599. endx, (y + halfLineWidth) / 1000f,
  600. true, true, Constants.EN_SOLID, ct);
  601. }
  602. }
  603. }
  604. /** Clip using the current path. */
  605. protected abstract void clip();
  606. /**
  607. * Clip using a rectangular area.
  608. * @param x the x coordinate (in points)
  609. * @param y the y coordinate (in points)
  610. * @param width the width of the rectangle (in points)
  611. * @param height the height of the rectangle (in points)
  612. */
  613. protected abstract void clipRect(float x, float y, float width, float height);
  614. /**
  615. * Moves the current point to (x, y), omitting any connecting line segment.
  616. * @param x x coordinate
  617. * @param y y coordinate
  618. */
  619. protected abstract void moveTo(float x, float y);
  620. /**
  621. * Appends a straight line segment from the current point to (x, y). The
  622. * new current point is (x, y).
  623. * @param x x coordinate
  624. * @param y y coordinate
  625. */
  626. protected abstract void lineTo(float x, float y);
  627. /**
  628. * Closes the current subpath by appending a straight line segment from
  629. * the current point to the starting point of the subpath.
  630. */
  631. protected abstract void closePath();
  632. /**
  633. * Fill a rectangular area.
  634. * @param x the x coordinate
  635. * @param y the y coordinate
  636. * @param width the width of the rectangle
  637. * @param height the height of the rectangle
  638. */
  639. protected abstract void fillRect(float x, float y, float width, float height);
  640. /**
  641. * Establishes a new foreground or fill color.
  642. * @param col the color to apply (null skips this operation)
  643. * @param fill true to set the fill color, false for the foreground color
  644. */
  645. protected abstract void updateColor(Color col, boolean fill);
  646. /**
  647. * Draw an image at the indicated location.
  648. * @param url the URI/URL of the image
  649. * @param pos the position of the image
  650. * @param foreignAttributes an optional Map with foreign attributes, may be null
  651. */
  652. protected abstract void drawImage(String url, Rectangle2D pos, Map foreignAttributes);
  653. /**
  654. * Draw an image at the indicated location.
  655. * @param url the URI/URL of the image
  656. * @param pos the position of the image
  657. */
  658. protected final void drawImage(String url, Rectangle2D pos) {
  659. drawImage(url, pos, null);
  660. }
  661. /**
  662. * Draw a border segment of an XSL-FO style border.
  663. * @param x1 starting x coordinate
  664. * @param y1 starting y coordinate
  665. * @param x2 ending x coordinate
  666. * @param y2 ending y coordinate
  667. * @param horz true for horizontal border segments, false for vertical border segments
  668. * @param startOrBefore true for border segments on the start or before edge,
  669. * false for end or after.
  670. * @param style the border style (one of Constants.EN_DASHED etc.)
  671. * @param col the color for the border segment
  672. */
  673. protected abstract void drawBorderLine(float x1, float y1, float x2, float y2,
  674. boolean horz, boolean startOrBefore, int style, Color col);
  675. /** {@inheritDoc} */
  676. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  677. endTextObject();
  678. Document doc = fo.getDocument();
  679. String ns = fo.getNameSpace();
  680. renderDocument(doc, ns, pos, fo.getForeignAttributes());
  681. }
  682. }