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

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