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

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