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

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