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

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