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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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.fop.area.Area;
  30. import org.apache.fop.area.Block;
  31. import org.apache.fop.area.BlockViewport;
  32. import org.apache.fop.area.CTM;
  33. import org.apache.fop.area.NormalFlow;
  34. import org.apache.fop.area.RegionReference;
  35. import org.apache.fop.area.RegionViewport;
  36. import org.apache.fop.area.Trait;
  37. import org.apache.fop.area.inline.ForeignObject;
  38. import org.apache.fop.area.inline.InlineArea;
  39. import org.apache.fop.area.inline.Viewport;
  40. import org.apache.fop.fo.Constants;
  41. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  42. import org.apache.fop.fonts.FontMetrics;
  43. import org.apache.fop.traits.BorderProps;
  44. import org.apache.xmlgraphics.util.UnitConv;
  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. /**
  249. * Draws borders.
  250. * @param borderRect the border rectangle
  251. * @param bpsBefore the border specification on the before side
  252. * @param bpsAfter the border specification on the after side
  253. * @param bpsStart the border specification on the start side
  254. * @param bpsEnd the border specification on the end side
  255. */
  256. protected void drawBorders(Rectangle2D.Float borderRect,
  257. BorderProps bpsBefore, BorderProps bpsAfter, BorderProps bpsStart, BorderProps bpsEnd) {
  258. float startx = borderRect.x;
  259. float starty = borderRect.y;
  260. float width = borderRect.width;
  261. float height = borderRect.height;
  262. boolean[] b = new boolean[] {
  263. (bpsBefore != null), (bpsEnd != null),
  264. (bpsAfter != null), (bpsStart != null)};
  265. if (!b[0] && !b[1] && !b[2] && !b[3]) {
  266. return;
  267. }
  268. float[] bw = new float[] {
  269. (b[0] ? bpsBefore.width / 1000f : 0.0f),
  270. (b[1] ? bpsEnd.width / 1000f : 0.0f),
  271. (b[2] ? bpsAfter.width / 1000f : 0.0f),
  272. (b[3] ? bpsStart.width / 1000f : 0.0f)};
  273. float[] clipw = new float[] {
  274. BorderProps.getClippedWidth(bpsBefore) / 1000f,
  275. BorderProps.getClippedWidth(bpsEnd) / 1000f,
  276. BorderProps.getClippedWidth(bpsAfter) / 1000f,
  277. BorderProps.getClippedWidth(bpsStart) / 1000f};
  278. starty += clipw[0];
  279. height -= clipw[0];
  280. height -= clipw[2];
  281. startx += clipw[3];
  282. width -= clipw[3];
  283. width -= clipw[1];
  284. boolean[] slant = new boolean[] {
  285. (b[3] && b[0]), (b[0] && b[1]), (b[1] && b[2]), (b[2] && b[3])};
  286. if (bpsBefore != null) {
  287. endTextObject();
  288. float sx1 = startx;
  289. float sx2 = (slant[0] ? sx1 + bw[3] - clipw[3] : sx1);
  290. float ex1 = startx + width;
  291. float ex2 = (slant[1] ? ex1 - bw[1] + clipw[1] : ex1);
  292. float outery = starty - clipw[0];
  293. float clipy = outery + clipw[0];
  294. float innery = outery + bw[0];
  295. saveGraphicsState();
  296. moveTo(sx1, clipy);
  297. float sx1a = sx1;
  298. float ex1a = ex1;
  299. if (bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  300. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  301. sx1a -= clipw[3];
  302. }
  303. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  304. ex1a += clipw[1];
  305. }
  306. lineTo(sx1a, outery);
  307. lineTo(ex1a, outery);
  308. }
  309. lineTo(ex1, clipy);
  310. lineTo(ex2, innery);
  311. lineTo(sx2, innery);
  312. closePath();
  313. clip();
  314. drawBorderLine(sx1a, outery, ex1a, innery, true, true,
  315. bpsBefore.style, bpsBefore.color);
  316. restoreGraphicsState();
  317. }
  318. if (bpsEnd != null) {
  319. endTextObject();
  320. float sy1 = starty;
  321. float sy2 = (slant[1] ? sy1 + bw[0] - clipw[0] : sy1);
  322. float ey1 = starty + height;
  323. float ey2 = (slant[2] ? ey1 - bw[2] + clipw[2] : ey1);
  324. float outerx = startx + width + clipw[1];
  325. float clipx = outerx - clipw[1];
  326. float innerx = outerx - bw[1];
  327. saveGraphicsState();
  328. moveTo(clipx, sy1);
  329. float sy1a = sy1;
  330. float ey1a = ey1;
  331. if (bpsEnd.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, sy1a);
  339. lineTo(outerx, ey1a);
  340. }
  341. lineTo(clipx, ey1);
  342. lineTo(innerx, ey2);
  343. lineTo(innerx, sy2);
  344. closePath();
  345. clip();
  346. drawBorderLine(innerx, sy1a, outerx, ey1a, false, false, bpsEnd.style, bpsEnd.color);
  347. restoreGraphicsState();
  348. }
  349. if (bpsAfter != null) {
  350. endTextObject();
  351. float sx1 = startx;
  352. float sx2 = (slant[3] ? sx1 + bw[3] - clipw[3] : sx1);
  353. float ex1 = startx + width;
  354. float ex2 = (slant[2] ? ex1 - bw[1] + clipw[1] : ex1);
  355. float outery = starty + height + clipw[2];
  356. float clipy = outery - clipw[2];
  357. float innery = outery - bw[2];
  358. saveGraphicsState();
  359. moveTo(ex1, clipy);
  360. float sx1a = sx1;
  361. float ex1a = ex1;
  362. if (bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  363. if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  364. sx1a -= clipw[3];
  365. }
  366. if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
  367. ex1a += clipw[1];
  368. }
  369. lineTo(ex1a, outery);
  370. lineTo(sx1a, outery);
  371. }
  372. lineTo(sx1, clipy);
  373. lineTo(sx2, innery);
  374. lineTo(ex2, innery);
  375. closePath();
  376. clip();
  377. drawBorderLine(sx1a, innery, ex1a, outery, true, false, bpsAfter.style, bpsAfter.color);
  378. restoreGraphicsState();
  379. }
  380. if (bpsStart != null) {
  381. endTextObject();
  382. float sy1 = starty;
  383. float sy2 = (slant[0] ? sy1 + bw[0] - clipw[0] : sy1);
  384. float ey1 = sy1 + height;
  385. float ey2 = (slant[3] ? ey1 - bw[2] + clipw[2] : ey1);
  386. float outerx = startx - clipw[3];
  387. float clipx = outerx + clipw[3];
  388. float innerx = outerx + bw[3];
  389. saveGraphicsState();
  390. moveTo(clipx, ey1);
  391. float sy1a = sy1;
  392. float ey1a = ey1;
  393. if (bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
  394. if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
  395. sy1a -= clipw[0];
  396. }
  397. if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
  398. ey1a += clipw[2];
  399. }
  400. lineTo(outerx, ey1a);
  401. lineTo(outerx, sy1a);
  402. }
  403. lineTo(clipx, sy1);
  404. lineTo(innerx, sy2);
  405. lineTo(innerx, ey2);
  406. closePath();
  407. clip();
  408. drawBorderLine(outerx, sy1a, innerx, ey1a, false, true, bpsStart.style, bpsStart.color);
  409. restoreGraphicsState();
  410. }
  411. }
  412. /**
  413. * Common method to render the background and borders for any inline area.
  414. * The all borders and padding are drawn outside the specified area.
  415. * @param area the inline area for which the background, border and padding is to be
  416. * rendered
  417. */
  418. protected void renderInlineAreaBackAndBorders(InlineArea area) {
  419. float x = currentIPPosition / 1000f;
  420. float y = (currentBPPosition + area.getOffset()) / 1000f;
  421. float width = area.getIPD() / 1000f;
  422. float height = area.getBPD() / 1000f;
  423. float borderPaddingStart = area.getBorderAndPaddingWidthStart() / 1000f;
  424. float borderPaddingBefore = area.getBorderAndPaddingWidthBefore() / 1000f;
  425. float bpwidth = borderPaddingStart
  426. + (area.getBorderAndPaddingWidthEnd() / 1000f);
  427. float bpheight = borderPaddingBefore
  428. + (area.getBorderAndPaddingWidthAfter() / 1000f);
  429. if (height != 0.0f || bpheight != 0.0f && bpwidth != 0.0f) {
  430. drawBackAndBorders(area, x, y - borderPaddingBefore
  431. , width + bpwidth
  432. , height + bpheight);
  433. }
  434. }
  435. /** Constant for the fox:transform extension attribute */
  436. protected static final QName FOX_TRANSFORM
  437. = new QName(ExtensionElementMapping.URI, "fox:transform");
  438. /** {@inheritDoc} */
  439. protected void renderBlockViewport(BlockViewport bv, List children) {
  440. // clip and position viewport if necessary
  441. // save positions
  442. int saveIP = currentIPPosition;
  443. int saveBP = currentBPPosition;
  444. CTM ctm = bv.getCTM();
  445. int borderPaddingStart = bv.getBorderAndPaddingWidthStart();
  446. int borderPaddingBefore = bv.getBorderAndPaddingWidthBefore();
  447. //This is the content-rect
  448. float width = bv.getIPD() / 1000f;
  449. float height = bv.getBPD() / 1000f;
  450. if (bv.getPositioning() == Block.ABSOLUTE
  451. || bv.getPositioning() == Block.FIXED) {
  452. //For FIXED, we need to break out of the current viewports to the
  453. //one established by the page. We save the state stack for restoration
  454. //after the block-container has been painted. See below.
  455. List breakOutList = null;
  456. if (bv.getPositioning() == Block.FIXED) {
  457. breakOutList = breakOutOfStateStack();
  458. }
  459. AffineTransform positionTransform = new AffineTransform();
  460. positionTransform.translate(bv.getXOffset(), bv.getYOffset());
  461. //"left/"top" (bv.getX/YOffset()) specify the position of the content rectangle
  462. positionTransform.translate(-borderPaddingStart, -borderPaddingBefore);
  463. //Free transformation for the block-container viewport
  464. String transf;
  465. transf = bv.getForeignAttributeValue(FOX_TRANSFORM);
  466. if (transf != null) {
  467. AffineTransform freeTransform = AWTTransformProducer.createAffineTransform(transf);
  468. positionTransform.concatenate(freeTransform);
  469. }
  470. saveGraphicsState();
  471. //Viewport position
  472. concatenateTransformationMatrix(UnitConv.mptToPt(positionTransform));
  473. //Background and borders
  474. float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd()) / 1000f;
  475. float bpheight = (borderPaddingBefore + bv.getBorderAndPaddingWidthAfter()) / 1000f;
  476. drawBackAndBorders(bv, 0, 0, width + bpwidth, height + bpheight);
  477. //Shift to content rectangle after border painting
  478. AffineTransform contentRectTransform = new AffineTransform();
  479. contentRectTransform.translate(borderPaddingStart, borderPaddingBefore);
  480. concatenateTransformationMatrix(UnitConv.mptToPt(contentRectTransform));
  481. //Clipping
  482. if (bv.getClip()) {
  483. clipRect(0f, 0f, width, height);
  484. }
  485. saveGraphicsState();
  486. //Set up coordinate system for content rectangle
  487. AffineTransform contentTransform = ctm.toAffineTransform();
  488. concatenateTransformationMatrix(UnitConv.mptToPt(contentTransform));
  489. currentIPPosition = 0;
  490. currentBPPosition = 0;
  491. renderBlocks(bv, children);
  492. restoreGraphicsState();
  493. restoreGraphicsState();
  494. if (breakOutList != null) {
  495. restoreStateStackAfterBreakOut(breakOutList);
  496. }
  497. currentIPPosition = saveIP;
  498. currentBPPosition = saveBP;
  499. } else {
  500. currentBPPosition += bv.getSpaceBefore();
  501. //borders and background in the old coordinate system
  502. handleBlockTraits(bv);
  503. //Advance to start of content area
  504. currentIPPosition += bv.getStartIndent();
  505. CTM tempctm = new CTM(containingIPPosition, currentBPPosition);
  506. ctm = tempctm.multiply(ctm);
  507. //Now adjust for border/padding
  508. currentBPPosition += borderPaddingBefore;
  509. Rectangle2D clippingRect = null;
  510. if (bv.getClip()) {
  511. clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
  512. bv.getIPD(), bv.getBPD());
  513. }
  514. startVParea(ctm, clippingRect);
  515. currentIPPosition = 0;
  516. currentBPPosition = 0;
  517. renderBlocks(bv, children);
  518. endVParea();
  519. currentIPPosition = saveIP;
  520. currentBPPosition = saveBP;
  521. currentBPPosition += (bv.getAllocBPD());
  522. }
  523. }
  524. /** {@inheritDoc} */
  525. protected void renderReferenceArea(Block block) {
  526. // save position and offset
  527. int saveIP = currentIPPosition;
  528. int saveBP = currentBPPosition;
  529. //Establish a new coordinate system
  530. AffineTransform at = new AffineTransform();
  531. at.translate(currentIPPosition, currentBPPosition);
  532. at.translate(block.getXOffset(), block.getYOffset());
  533. at.translate(0, block.getSpaceBefore());
  534. if (!at.isIdentity()) {
  535. saveGraphicsState();
  536. concatenateTransformationMatrix(UnitConv.mptToPt(at));
  537. }
  538. currentIPPosition = 0;
  539. currentBPPosition = 0;
  540. handleBlockTraits(block);
  541. List children = block.getChildAreas();
  542. if (children != null) {
  543. renderBlocks(block, children);
  544. }
  545. if (!at.isIdentity()) {
  546. restoreGraphicsState();
  547. }
  548. // stacked and relative blocks effect stacking
  549. currentIPPosition = saveIP;
  550. currentBPPosition = saveBP;
  551. }
  552. /** {@inheritDoc} */
  553. protected void renderFlow(NormalFlow flow) {
  554. // save position and offset
  555. int saveIP = currentIPPosition;
  556. int saveBP = currentBPPosition;
  557. //Establish a new coordinate system
  558. AffineTransform at = new AffineTransform();
  559. at.translate(currentIPPosition, currentBPPosition);
  560. if (!at.isIdentity()) {
  561. saveGraphicsState();
  562. concatenateTransformationMatrix(UnitConv.mptToPt(at));
  563. }
  564. currentIPPosition = 0;
  565. currentBPPosition = 0;
  566. super.renderFlow(flow);
  567. if (!at.isIdentity()) {
  568. restoreGraphicsState();
  569. }
  570. // stacked and relative blocks effect stacking
  571. currentIPPosition = saveIP;
  572. currentBPPosition = saveBP;
  573. }
  574. /**
  575. * Concatenates the current transformation matrix with the given one, therefore establishing
  576. * a new coordinate system.
  577. * @param at the transformation matrix to process (coordinates in points)
  578. */
  579. protected abstract void concatenateTransformationMatrix(AffineTransform at);
  580. /**
  581. * Render an inline viewport.
  582. * This renders an inline viewport by clipping if necessary.
  583. * @param viewport the viewport to handle
  584. */
  585. public void renderViewport(Viewport viewport) {
  586. float x = currentIPPosition / 1000f;
  587. float y = (currentBPPosition + viewport.getOffset()) / 1000f;
  588. float width = viewport.getIPD() / 1000f;
  589. float height = viewport.getBPD() / 1000f;
  590. // TODO: Calculate the border rect correctly.
  591. float borderPaddingStart = viewport.getBorderAndPaddingWidthStart() / 1000f;
  592. float borderPaddingBefore = viewport.getBorderAndPaddingWidthBefore() / 1000f;
  593. float bpwidth = borderPaddingStart
  594. + (viewport.getBorderAndPaddingWidthEnd() / 1000f);
  595. float bpheight = borderPaddingBefore
  596. + (viewport.getBorderAndPaddingWidthAfter() / 1000f);
  597. drawBackAndBorders(viewport, x, y, width + bpwidth, height + bpheight);
  598. if (viewport.getClip()) {
  599. saveGraphicsState();
  600. clipRect(x + borderPaddingStart, y + borderPaddingBefore, width, height);
  601. }
  602. super.renderViewport(viewport);
  603. if (viewport.getClip()) {
  604. restoreGraphicsState();
  605. }
  606. }
  607. /**
  608. * Restores the state stack after a break out.
  609. * @param breakOutList the state stack to restore.
  610. */
  611. protected abstract void restoreStateStackAfterBreakOut(List breakOutList);
  612. /**
  613. * Breaks out of the state stack to handle fixed block-containers.
  614. * @return the saved state stack to recreate later
  615. */
  616. protected abstract List breakOutOfStateStack();
  617. /** Saves the graphics state of the rendering engine. */
  618. protected abstract void saveGraphicsState();
  619. /** Restores the last graphics state of the rendering engine. */
  620. protected abstract void restoreGraphicsState();
  621. /** Indicates the beginning of a text object. */
  622. protected abstract void beginTextObject();
  623. /** Indicates the end of a text object. */
  624. protected abstract void endTextObject();
  625. /**
  626. * Paints the text decoration marks.
  627. * @param fm Current typeface
  628. * @param fontsize Current font size
  629. * @param inline inline area to paint the marks for
  630. * @param baseline position of the baseline
  631. * @param startx start IPD
  632. */
  633. protected void renderTextDecoration(FontMetrics fm, int fontsize, InlineArea inline,
  634. int baseline, int startx) {
  635. boolean hasTextDeco = inline.hasUnderline()
  636. || inline.hasOverline()
  637. || inline.hasLineThrough();
  638. if (hasTextDeco) {
  639. endTextObject();
  640. float descender = fm.getDescender(fontsize) / 1000f;
  641. float capHeight = fm.getCapHeight(fontsize) / 1000f;
  642. float halfLineWidth = (descender / -8f) / 2f;
  643. float endx = (startx + inline.getIPD()) / 1000f;
  644. if (inline.hasUnderline()) {
  645. Color ct = (Color) inline.getTrait(Trait.UNDERLINE_COLOR);
  646. float y = baseline - descender / 2f;
  647. drawBorderLine(startx / 1000f, (y - halfLineWidth) / 1000f,
  648. endx, (y + halfLineWidth) / 1000f,
  649. true, true, Constants.EN_SOLID, ct);
  650. }
  651. if (inline.hasOverline()) {
  652. Color ct = (Color) inline.getTrait(Trait.OVERLINE_COLOR);
  653. float y = (float)(baseline - (1.1 * capHeight));
  654. drawBorderLine(startx / 1000f, (y - halfLineWidth) / 1000f,
  655. endx, (y + halfLineWidth) / 1000f,
  656. true, true, Constants.EN_SOLID, ct);
  657. }
  658. if (inline.hasLineThrough()) {
  659. Color ct = (Color) inline.getTrait(Trait.LINETHROUGH_COLOR);
  660. float y = (float)(baseline - (0.45 * capHeight));
  661. drawBorderLine(startx / 1000f, (y - halfLineWidth) / 1000f,
  662. endx, (y + halfLineWidth) / 1000f,
  663. true, true, Constants.EN_SOLID, ct);
  664. }
  665. }
  666. }
  667. /** Clip using the current path. */
  668. protected abstract void clip();
  669. /**
  670. * Clip using a rectangular area.
  671. * @param x the x coordinate (in points)
  672. * @param y the y coordinate (in points)
  673. * @param width the width of the rectangle (in points)
  674. * @param height the height of the rectangle (in points)
  675. */
  676. protected abstract void clipRect(float x, float y, float width, float height);
  677. /**
  678. * Moves the current point to (x, y), omitting any connecting line segment.
  679. * @param x x coordinate
  680. * @param y y coordinate
  681. */
  682. protected abstract void moveTo(float x, float y);
  683. /**
  684. * Appends a straight line segment from the current point to (x, y). The
  685. * new current point is (x, y).
  686. * @param x x coordinate
  687. * @param y y coordinate
  688. */
  689. protected abstract void lineTo(float x, float y);
  690. /**
  691. * Closes the current subpath by appending a straight line segment from
  692. * the current point to the starting point of the subpath.
  693. */
  694. protected abstract void closePath();
  695. /**
  696. * Fill a rectangular area.
  697. * @param x the x coordinate
  698. * @param y the y coordinate
  699. * @param width the width of the rectangle
  700. * @param height the height of the rectangle
  701. */
  702. protected abstract void fillRect(float x, float y, float width, float height);
  703. /**
  704. * Establishes a new foreground or fill color.
  705. * @param col the color to apply (null skips this operation)
  706. * @param fill true to set the fill color, false for the foreground color
  707. */
  708. protected abstract void updateColor(Color col, boolean fill);
  709. /**
  710. * Draw an image at the indicated location.
  711. * @param url the URI/URL of the image
  712. * @param pos the position of the image
  713. * @param foreignAttributes an optional Map with foreign attributes, may be null
  714. */
  715. protected abstract void drawImage(String url, Rectangle2D pos, Map foreignAttributes);
  716. /**
  717. * Draw an image at the indicated location.
  718. * @param url the URI/URL of the image
  719. * @param pos the position of the image
  720. */
  721. protected final void drawImage(String url, Rectangle2D pos) {
  722. drawImage(url, pos, null);
  723. }
  724. /**
  725. * Draw a border segment of an XSL-FO style border.
  726. * @param x1 starting x coordinate
  727. * @param y1 starting y coordinate
  728. * @param x2 ending x coordinate
  729. * @param y2 ending y coordinate
  730. * @param horz true for horizontal border segments, false for vertical border segments
  731. * @param startOrBefore true for border segments on the start or before edge,
  732. * false for end or after.
  733. * @param style the border style (one of Constants.EN_DASHED etc.)
  734. * @param col the color for the border segment
  735. */
  736. protected abstract void drawBorderLine(float x1, float y1, float x2, float y2,
  737. boolean horz, boolean startOrBefore, int style, Color col);
  738. /** {@inheritDoc} */
  739. public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  740. endTextObject();
  741. Document doc = fo.getDocument();
  742. String ns = fo.getNameSpace();
  743. renderDocument(doc, ns, pos, fo.getForeignAttributes());
  744. }
  745. }