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 34KB

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