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.

AbstractRenderer.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. // Java
  20. import java.awt.Rectangle;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Rectangle2D;
  23. import java.io.IOException;
  24. import java.io.OutputStream;
  25. import java.util.List;
  26. import java.util.Locale;
  27. import java.util.Set;
  28. import java.util.Stack;
  29. import org.w3c.dom.Document;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. import org.apache.fop.ResourceEventProducer;
  33. import org.apache.fop.apps.FOPException;
  34. import org.apache.fop.apps.FOUserAgent;
  35. import org.apache.fop.area.Area;
  36. import org.apache.fop.area.BeforeFloat;
  37. import org.apache.fop.area.Block;
  38. import org.apache.fop.area.BlockViewport;
  39. import org.apache.fop.area.BodyRegion;
  40. import org.apache.fop.area.CTM;
  41. import org.apache.fop.area.Footnote;
  42. import org.apache.fop.area.LineArea;
  43. import org.apache.fop.area.MainReference;
  44. import org.apache.fop.area.NormalFlow;
  45. import org.apache.fop.area.OffDocumentItem;
  46. import org.apache.fop.area.Page;
  47. import org.apache.fop.area.PageSequence;
  48. import org.apache.fop.area.PageViewport;
  49. import org.apache.fop.area.RegionReference;
  50. import org.apache.fop.area.RegionViewport;
  51. import org.apache.fop.area.Span;
  52. import org.apache.fop.area.Trait;
  53. import org.apache.fop.area.inline.Container;
  54. import org.apache.fop.area.inline.FilledArea;
  55. import org.apache.fop.area.inline.ForeignObject;
  56. import org.apache.fop.area.inline.Image;
  57. import org.apache.fop.area.inline.InlineArea;
  58. import org.apache.fop.area.inline.InlineBlockParent;
  59. import org.apache.fop.area.inline.InlineParent;
  60. import org.apache.fop.area.inline.InlineViewport;
  61. import org.apache.fop.area.inline.Leader;
  62. import org.apache.fop.area.inline.Space;
  63. import org.apache.fop.area.inline.SpaceArea;
  64. import org.apache.fop.area.inline.TextArea;
  65. import org.apache.fop.area.inline.WordArea;
  66. import org.apache.fop.fo.Constants;
  67. import org.apache.fop.fonts.FontInfo;
  68. /**
  69. * Abstract base class for all renderers. The Abstract renderer does all the
  70. * top level processing of the area tree and adds some abstract methods to
  71. * handle viewports. This keeps track of the current block and inline position.
  72. */
  73. public abstract class AbstractRenderer
  74. implements Renderer, Constants {
  75. /** logging instance */
  76. protected static final Log log = LogFactory.getLog("org.apache.fop.render");
  77. /**
  78. * user agent
  79. */
  80. protected FOUserAgent userAgent = null;
  81. /**
  82. * block progression position
  83. */
  84. protected int currentBPPosition = 0;
  85. /**
  86. * inline progression position
  87. */
  88. protected int currentIPPosition = 0;
  89. /**
  90. * the block progression position of the containing block used for
  91. * absolutely positioned blocks
  92. */
  93. protected int containingBPPosition = 0;
  94. /**
  95. * the inline progression position of the containing block used for
  96. * absolutely positioned blocks
  97. */
  98. protected int containingIPPosition = 0;
  99. /** the currently active PageViewport */
  100. protected PageViewport currentPageViewport;
  101. /* warned XML handlers */
  102. private Set warnedXMLHandlers;
  103. /* layers stack */
  104. private Stack<String> layers;
  105. /** {@inheritDoc} */
  106. public abstract void setupFontInfo(FontInfo fontInfo) throws FOPException;
  107. /**
  108. *
  109. * @param userAgent the user agent that contains configuration details. This cannot be null.
  110. */
  111. public AbstractRenderer(FOUserAgent userAgent) {
  112. this.userAgent = userAgent;
  113. }
  114. /** {@inheritDoc} */
  115. public FOUserAgent getUserAgent() {
  116. return userAgent;
  117. }
  118. /** {@inheritDoc} */
  119. public void startRenderer(OutputStream outputStream)
  120. throws IOException {
  121. if (userAgent == null) {
  122. throw new IllegalStateException("FOUserAgent has not been set on Renderer");
  123. }
  124. }
  125. /** {@inheritDoc} */
  126. public void stopRenderer()
  127. throws IOException { }
  128. /**
  129. * Check if this renderer supports out of order rendering. If this renderer
  130. * supports out of order rendering then it means that the pages that are
  131. * not ready will be prepared and a future page will be rendered.
  132. *
  133. * @return True if the renderer supports out of order rendering
  134. */
  135. public boolean supportsOutOfOrder() {
  136. return false;
  137. }
  138. /** {@inheritDoc} */
  139. public void setDocumentLocale(Locale locale) {
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public void processOffDocumentItem(OffDocumentItem odi) { }
  145. /** {@inheritDoc} */
  146. public Graphics2DAdapter getGraphics2DAdapter() {
  147. return null;
  148. }
  149. /** {@inheritDoc} */
  150. public ImageAdapter getImageAdapter() {
  151. return null;
  152. }
  153. /** @return the current PageViewport or null, if none is active */
  154. protected PageViewport getCurrentPageViewport() {
  155. return this.currentPageViewport;
  156. }
  157. /** {@inheritDoc} */
  158. public void preparePage(PageViewport page) { }
  159. /**
  160. * Utility method to convert a page sequence title to a string. Some
  161. * renderers may only be able to use a string title. A title is a sequence
  162. * of inline areas that this method attempts to convert to an equivalent
  163. * string.
  164. *
  165. * @param title The Title to convert
  166. * @return An expanded string representing the title
  167. */
  168. protected String convertTitleToString(LineArea title) {
  169. List children = title.getInlineAreas();
  170. String str = convertToString(children);
  171. return str.trim();
  172. }
  173. private String convertToString(List children) {
  174. StringBuffer sb = new StringBuffer();
  175. for (int count = 0; count < children.size(); count++) {
  176. InlineArea inline = (InlineArea) children.get(count);
  177. //if (inline instanceof Character) {
  178. // sb.append(((Character) inline).getChar());
  179. /*} else*/ if (inline instanceof TextArea) {
  180. sb.append(((TextArea) inline).getText());
  181. } else if (inline instanceof InlineParent) {
  182. sb.append(convertToString(
  183. ((InlineParent) inline).getChildAreas()));
  184. } else {
  185. sb.append(" ");
  186. }
  187. }
  188. return sb.toString();
  189. }
  190. /**
  191. * {@inheritDoc}
  192. * @deprecated
  193. */
  194. public void startPageSequence(LineArea seqTitle) {
  195. //do nothing
  196. }
  197. /** {@inheritDoc} */
  198. public void startPageSequence(PageSequence pageSequence) {
  199. // do nothing
  200. }
  201. // normally this would be overriden to create a page in the
  202. // output
  203. /** {@inheritDoc} */
  204. public void renderPage(PageViewport page)
  205. throws IOException, FOPException {
  206. this.currentPageViewport = page;
  207. try {
  208. Page p = page.getPage();
  209. renderPageAreas(p);
  210. } finally {
  211. this.currentPageViewport = null;
  212. }
  213. }
  214. /**
  215. * Renders page areas.
  216. *
  217. * @param page The page whos page areas are to be rendered
  218. */
  219. protected void renderPageAreas(Page page) {
  220. /* Spec does not appear to specify whether fo:region-body should
  221. appear above or below side regions in cases of overlap. FOP
  222. decision is to have fo:region-body on top, hence it is rendered
  223. last here. */
  224. RegionViewport viewport;
  225. viewport = page.getRegionViewport(FO_REGION_BEFORE);
  226. if (viewport != null) {
  227. renderRegionViewport(viewport);
  228. }
  229. viewport = page.getRegionViewport(FO_REGION_START);
  230. if (viewport != null) {
  231. renderRegionViewport(viewport);
  232. }
  233. viewport = page.getRegionViewport(FO_REGION_END);
  234. if (viewport != null) {
  235. renderRegionViewport(viewport);
  236. }
  237. viewport = page.getRegionViewport(FO_REGION_AFTER);
  238. if (viewport != null) {
  239. renderRegionViewport(viewport);
  240. }
  241. viewport = page.getRegionViewport(FO_REGION_BODY);
  242. if (viewport != null) {
  243. renderRegionViewport(viewport);
  244. }
  245. }
  246. /**
  247. * Renders a region viewport. <p>
  248. *
  249. * The region may clip the area and it establishes a position from where
  250. * the region is placed.</p>
  251. *
  252. * @param port The region viewport to be rendered
  253. */
  254. protected void renderRegionViewport(RegionViewport port) {
  255. // The CTM will transform coordinates relative to
  256. // this region-reference area into page coords, so
  257. // set origin for the region to 0,0.
  258. currentBPPosition = 0;
  259. currentIPPosition = 0;
  260. RegionReference regionReference = port.getRegionReference();
  261. handleRegionTraits(port);
  262. // shouldn't the viewport have the CTM
  263. startVParea(regionReference.getCTM(), port.getClipRectangle());
  264. // do after starting viewport area
  265. if (regionReference.getRegionClass() == FO_REGION_BODY) {
  266. renderBodyRegion((BodyRegion) regionReference);
  267. } else {
  268. renderRegion(regionReference);
  269. }
  270. endVParea();
  271. }
  272. /**
  273. * Establishes a new viewport area.
  274. *
  275. * @param ctm the coordinate transformation matrix to use
  276. * @param clippingRect the clipping rectangle if the viewport should be clipping,
  277. * null if no clipping is performed.
  278. */
  279. protected abstract void startVParea(CTM ctm, Rectangle clippingRect);
  280. /**
  281. * Signals exit from a viewport area. Subclasses can restore transformation matrices
  282. * valid before the viewport area was started.
  283. */
  284. protected abstract void endVParea();
  285. /**
  286. * Handle the traits for a region
  287. * This is used to draw the traits for the given page region.
  288. * (See Sect. 6.4.1.2 of XSL-FO spec.)
  289. * @param rv the RegionViewport whose region is to be drawn
  290. */
  291. protected void handleRegionTraits(RegionViewport rv) {
  292. // draw border and background
  293. }
  294. /**
  295. * Renders a region reference area.
  296. *
  297. * @param region The region reference area
  298. */
  299. protected void renderRegion(RegionReference region) {
  300. renderBlocks(null, region.getBlocks());
  301. }
  302. /**
  303. * Renders a body region area.
  304. *
  305. * @param region The body region
  306. */
  307. protected void renderBodyRegion(BodyRegion region) {
  308. BeforeFloat bf = region.getBeforeFloat();
  309. if (bf != null) {
  310. renderBeforeFloat(bf);
  311. }
  312. MainReference mr = region.getMainReference();
  313. if (mr != null) {
  314. renderMainReference(mr);
  315. }
  316. Footnote foot = region.getFootnote();
  317. if (foot != null) {
  318. renderFootnote(foot);
  319. }
  320. }
  321. /**
  322. * Renders a before float area.
  323. *
  324. * @param bf The before float area
  325. */
  326. protected void renderBeforeFloat(BeforeFloat bf) {
  327. List blocks = bf.getChildAreas();
  328. if (blocks != null) {
  329. renderBlocks(null, blocks);
  330. Block sep = bf.getSeparator();
  331. if (sep != null) {
  332. renderBlock(sep);
  333. }
  334. }
  335. }
  336. /**
  337. * Renders a footnote
  338. *
  339. * @param footnote The footnote
  340. */
  341. protected void renderFootnote(Footnote footnote) {
  342. currentBPPosition += footnote.getTop();
  343. List blocks = footnote.getChildAreas();
  344. if (blocks != null) {
  345. Block sep = footnote.getSeparator();
  346. if (sep != null) {
  347. renderBlock(sep);
  348. }
  349. renderBlocks(null, blocks);
  350. }
  351. }
  352. /**
  353. * Renders the main reference area.
  354. * <p>
  355. * The main reference area contains a list of spans that are
  356. * stacked on the page.
  357. * The spans contain a list of normal flow reference areas
  358. * that are positioned into columns.
  359. * </p>
  360. *
  361. * @param mr The main reference area
  362. */
  363. protected void renderMainReference(MainReference mr) {
  364. Span span = null;
  365. List spans = mr.getSpans();
  366. int saveBPPos = currentBPPosition;
  367. int saveSpanBPPos = saveBPPos;
  368. int saveIPPos = currentIPPosition;
  369. for (int count = 0; count < spans.size(); count++) {
  370. span = (Span) spans.get(count);
  371. int level = span.getBidiLevel();
  372. if (level < 0) {
  373. level = 0;
  374. }
  375. if ((level & 1) == 1) {
  376. currentIPPosition += span.getIPD();
  377. currentIPPosition += mr.getColumnGap();
  378. }
  379. for (int c = 0; c < span.getColumnCount(); c++) {
  380. NormalFlow flow = span.getNormalFlow(c);
  381. if (flow != null) {
  382. currentBPPosition = saveSpanBPPos;
  383. if ((level & 1) == 1) {
  384. currentIPPosition -= flow.getIPD();
  385. currentIPPosition -= mr.getColumnGap();
  386. }
  387. renderFlow(flow);
  388. if ((level & 1) == 0) {
  389. currentIPPosition += flow.getIPD();
  390. currentIPPosition += mr.getColumnGap();
  391. }
  392. }
  393. }
  394. currentIPPosition = saveIPPos;
  395. currentBPPosition = saveSpanBPPos + span.getHeight();
  396. saveSpanBPPos = currentBPPosition;
  397. }
  398. currentBPPosition = saveBPPos;
  399. }
  400. /**
  401. * Renders a flow reference area.
  402. *
  403. * @param flow The flow reference area
  404. */
  405. protected void renderFlow(NormalFlow flow) {
  406. // the normal flow reference area contains stacked blocks
  407. List blocks = flow.getChildAreas();
  408. if (blocks != null) {
  409. renderBlocks(null, blocks);
  410. }
  411. }
  412. /**
  413. * Handle block traits.
  414. * This method is called when the correct ip and bp posiiton is
  415. * set. This should be overridden to draw border and background
  416. * traits for the block area.
  417. *
  418. * @param block the block area
  419. */
  420. protected void handleBlockTraits(Block block) {
  421. // draw border and background
  422. }
  423. /**
  424. * Renders a block viewport.
  425. *
  426. * @param bv The block viewport
  427. * @param children The children to render within the block viewport
  428. */
  429. protected void renderBlockViewport(BlockViewport bv, List children) {
  430. boolean inNewLayer = false;
  431. if (maybeStartLayer(bv)) {
  432. inNewLayer = true;
  433. }
  434. // clip and position viewport if necessary
  435. if (bv.getPositioning() == Block.ABSOLUTE) {
  436. // save positions
  437. int saveIP = currentIPPosition;
  438. int saveBP = currentBPPosition;
  439. Rectangle clippingRect = null;
  440. if (bv.hasClip()) {
  441. clippingRect = new Rectangle(saveIP, saveBP, bv.getIPD(), bv.getBPD());
  442. }
  443. CTM ctm = bv.getCTM();
  444. currentIPPosition = 0;
  445. currentBPPosition = 0;
  446. startVParea(ctm, clippingRect);
  447. handleBlockTraits(bv);
  448. renderBlocks(bv, children);
  449. endVParea();
  450. // clip if necessary
  451. currentIPPosition = saveIP;
  452. currentBPPosition = saveBP;
  453. } else {
  454. // save position and offset
  455. int saveIP = currentIPPosition;
  456. int saveBP = currentBPPosition;
  457. handleBlockTraits(bv);
  458. renderBlocks(bv, children);
  459. currentIPPosition = saveIP;
  460. currentBPPosition = saveBP + bv.getAllocBPD();
  461. }
  462. maybeEndLayer(bv, inNewLayer);
  463. }
  464. /**
  465. * Renders a block area that represents a reference area. The reference area establishes
  466. * a new coordinate system.
  467. * @param block the block area
  468. */
  469. protected abstract void renderReferenceArea(Block block);
  470. /**
  471. * Renders a list of block areas.
  472. *
  473. * @param parent the parent block if the parent is a block, otherwise
  474. * a null value.
  475. * @param blocks The block areas
  476. */
  477. protected void renderBlocks(Block parent, List blocks) {
  478. int saveIP = currentIPPosition;
  479. // Calculate the position of the content rectangle.
  480. if (parent != null && !parent.getTraitAsBoolean(Trait.IS_VIEWPORT_AREA)) {
  481. currentBPPosition += parent.getBorderAndPaddingWidthBefore();
  482. }
  483. // the position of the containing block is used for
  484. // absolutely positioned areas
  485. int contBP = currentBPPosition;
  486. int contIP = currentIPPosition;
  487. containingBPPosition = currentBPPosition;
  488. containingIPPosition = currentIPPosition;
  489. for (int count = 0; count < blocks.size(); count++) {
  490. Object obj = blocks.get(count);
  491. if (obj instanceof Block) {
  492. currentIPPosition = contIP;
  493. containingBPPosition = contBP;
  494. containingIPPosition = contIP;
  495. renderBlock((Block) obj);
  496. containingBPPosition = contBP;
  497. containingIPPosition = contIP;
  498. } else if (obj instanceof LineArea) {
  499. // a line area is rendered from the top left position
  500. // of the line, each inline object is offset from there
  501. LineArea line = (LineArea) obj;
  502. if (parent != null) {
  503. int level = parent.getBidiLevel();
  504. if ((level == -1) || ((level & 1) == 0)) {
  505. currentIPPosition += parent.getStartIndent();
  506. } else {
  507. currentIPPosition += parent.getEndIndent();
  508. }
  509. }
  510. renderLineArea(line);
  511. currentBPPosition += line.getAllocBPD();
  512. }
  513. currentIPPosition = saveIP;
  514. }
  515. }
  516. /**
  517. * Renders a block area.
  518. *
  519. * @param block The block area
  520. */
  521. protected void renderBlock(Block block) {
  522. assert block != null;
  523. List children = block.getChildAreas();
  524. boolean inNewLayer = false;
  525. if (maybeStartLayer(block)) {
  526. inNewLayer = true;
  527. }
  528. if (block instanceof BlockViewport) {
  529. if (children != null) {
  530. renderBlockViewport((BlockViewport) block, children);
  531. } else {
  532. handleBlockTraits(block);
  533. // simply move position
  534. currentBPPosition += block.getAllocBPD();
  535. }
  536. } else if (block.getTraitAsBoolean(Trait.IS_REFERENCE_AREA)) {
  537. renderReferenceArea(block);
  538. } else {
  539. // save position and offset
  540. int saveIP = currentIPPosition;
  541. int saveBP = currentBPPosition;
  542. currentIPPosition += block.getXOffset();
  543. currentBPPosition += block.getYOffset();
  544. currentBPPosition += block.getSpaceBefore();
  545. handleBlockTraits(block);
  546. if (children != null) {
  547. renderBlocks(block, children);
  548. }
  549. if (block.getPositioning() == Block.ABSOLUTE) {
  550. // absolute blocks do not effect the layout
  551. currentBPPosition = saveBP;
  552. } else {
  553. // stacked and relative blocks effect stacking
  554. currentIPPosition = saveIP;
  555. currentBPPosition = saveBP + block.getAllocBPD();
  556. }
  557. }
  558. maybeEndLayer(block, inNewLayer);
  559. }
  560. /**
  561. * Establish new optional content group layer.
  562. * @param layer name of layer
  563. */
  564. protected abstract void startLayer(String layer);
  565. /**
  566. * Finish current optional content group layer.
  567. */
  568. protected abstract void endLayer();
  569. protected boolean maybeStartLayer(Area area) {
  570. String layer = (String) area.getTrait(Trait.LAYER);
  571. if (layer != null) {
  572. if (layers == null) {
  573. layers = new Stack<String>();
  574. }
  575. if (layers.empty() || !layers.peek().equals(layer)) {
  576. layers.push(layer);
  577. startLayer(layer);
  578. return true;
  579. }
  580. }
  581. return false;
  582. }
  583. protected void maybeEndLayer(Area area, boolean inNewLayer) {
  584. if (inNewLayer) {
  585. assert layers != null;
  586. assert !layers.empty();
  587. String layer = (String) area.getTrait(Trait.LAYER);
  588. assert layer != null;
  589. assert layers.peek().equals(layer);
  590. endLayer();
  591. layers.pop();
  592. }
  593. }
  594. /**
  595. * Renders a line area. <p>
  596. *
  597. * A line area may have grouped styling for its children such as underline,
  598. * background.</p>
  599. *
  600. * @param line The line area
  601. */
  602. protected void renderLineArea(LineArea line) {
  603. List children = line.getInlineAreas();
  604. int saveBP = currentBPPosition;
  605. currentBPPosition += line.getSpaceBefore();
  606. int bl = line.getBidiLevel();
  607. if (bl >= 0) {
  608. if ((bl & 1) == 0) {
  609. currentIPPosition += line.getStartIndent();
  610. } else {
  611. currentIPPosition += line.getEndIndent();
  612. // if line's content overflows line area, then
  613. // ensure that overflow is drawn (extends)
  614. // outside of left side of line area
  615. int overflow = computeInlinesOverflow(line);
  616. if (overflow > 0) {
  617. currentIPPosition -= overflow;
  618. }
  619. }
  620. } else {
  621. currentIPPosition += line.getStartIndent();
  622. }
  623. for (int i = 0, l = children.size(); i < l; i++) {
  624. InlineArea inline = (InlineArea) children.get(i);
  625. renderInlineArea(inline);
  626. }
  627. currentBPPosition = saveBP;
  628. }
  629. private int computeInlinesOverflow(LineArea line) {
  630. List children = line.getInlineAreas();
  631. int ipdConsumed = 0;
  632. for (int i = 0, l = children.size(); i < l; i++) {
  633. InlineArea inline = (InlineArea) children.get(i);
  634. ipdConsumed += inline.getIPD();
  635. }
  636. return ipdConsumed - line.getIPD();
  637. }
  638. /**
  639. * Render the given InlineArea.
  640. * @param inlineArea inline area text to render
  641. */
  642. protected void renderInlineArea(InlineArea inlineArea) {
  643. if (inlineArea instanceof TextArea) {
  644. renderText((TextArea) inlineArea);
  645. //} else if (inlineArea instanceof Character) {
  646. //renderCharacter((Character) inlineArea);
  647. } else if (inlineArea instanceof WordArea) {
  648. renderWord((WordArea) inlineArea);
  649. } else if (inlineArea instanceof SpaceArea) {
  650. renderSpace((SpaceArea) inlineArea);
  651. } else if (inlineArea instanceof InlineParent) {
  652. renderInlineParent((InlineParent) inlineArea);
  653. } else if (inlineArea instanceof InlineBlockParent) {
  654. renderInlineBlockParent((InlineBlockParent) inlineArea);
  655. } else if (inlineArea instanceof Space) {
  656. renderInlineSpace((Space) inlineArea);
  657. } else if (inlineArea instanceof InlineViewport) {
  658. renderInlineViewport((InlineViewport) inlineArea);
  659. } else if (inlineArea instanceof Leader) {
  660. renderLeader((Leader) inlineArea);
  661. }
  662. }
  663. /**
  664. * Common method to render the background and borders for any inline area.
  665. * The all borders and padding are drawn outside the specified area.
  666. * @param area the inline area for which the background, border and padding is to be
  667. * rendered
  668. */
  669. protected abstract void renderInlineAreaBackAndBorders(InlineArea area);
  670. /**
  671. * Render the given Space.
  672. * @param space the space to render
  673. */
  674. protected void renderInlineSpace(Space space) {
  675. renderInlineAreaBackAndBorders(space);
  676. // an inline space moves the inline progression position
  677. // for the current block by the width or height of the space
  678. // it may also have styling (only on this object) that needs
  679. // handling
  680. currentIPPosition += space.getAllocIPD();
  681. }
  682. /**
  683. * Render the given Leader.
  684. * @param area the leader to render
  685. */
  686. protected void renderLeader(Leader area) {
  687. currentIPPosition += area.getAllocIPD();
  688. }
  689. /**
  690. * Render the given TextArea.
  691. * @param text the text to render
  692. */
  693. protected void renderText(TextArea text) {
  694. List children = text.getChildAreas();
  695. int saveIP = currentIPPosition;
  696. int saveBP = currentBPPosition;
  697. for (int i = 0, l = children.size(); i < l; i++) {
  698. InlineArea inline = (InlineArea) children.get(i);
  699. renderInlineArea(inline);
  700. }
  701. currentIPPosition = saveIP + text.getAllocIPD();
  702. }
  703. /**
  704. * Render the given WordArea.
  705. * @param word the word to render
  706. */
  707. protected void renderWord(WordArea word) {
  708. currentIPPosition += word.getAllocIPD();
  709. }
  710. /**
  711. * Render the given SpaceArea.
  712. * @param space the space to render
  713. */
  714. protected void renderSpace(SpaceArea space) {
  715. currentIPPosition += space.getAllocIPD();
  716. }
  717. /**
  718. * Render the given InlineParent.
  719. * @param ip the inline parent to render
  720. */
  721. protected void renderInlineParent(InlineParent ip) {
  722. boolean inNewLayer = false;
  723. if (maybeStartLayer(ip)) {
  724. inNewLayer = true;
  725. }
  726. int level = ip.getBidiLevel();
  727. List children = ip.getChildAreas();
  728. renderInlineAreaBackAndBorders(ip);
  729. int saveIP = currentIPPosition;
  730. int saveBP = currentBPPosition;
  731. // if inline parent is a filled area (generated by Leader), and if
  732. // it is right-to-left, then adjust starting ip position in order to
  733. // align children to starting (right) edge of filled area
  734. int ipAdjust;
  735. if ((ip instanceof FilledArea) && ((level & 1) != 0)) {
  736. int ipdChildren = 0;
  737. for (int i = 0, l = children.size(); i < l; i++) {
  738. InlineArea inline = (InlineArea) children.get(i);
  739. ipdChildren += inline.getAllocIPD();
  740. }
  741. ipAdjust = ip.getAllocIPD() - ipdChildren;
  742. } else {
  743. ipAdjust = 0;
  744. }
  745. // perform inline position adjustments
  746. if ((level == -1) || ((level & 1) == 0)) {
  747. currentIPPosition += ip.getBorderAndPaddingWidthStart();
  748. } else {
  749. currentIPPosition += ip.getBorderAndPaddingWidthEnd();
  750. if (ipAdjust > 0) {
  751. currentIPPosition += ipAdjust;
  752. }
  753. }
  754. currentBPPosition += ip.getBlockProgressionOffset();
  755. // render children inlines
  756. for (int i = 0, l = children.size(); i < l; i++) {
  757. InlineArea inline = (InlineArea) children.get(i);
  758. renderInlineArea(inline);
  759. }
  760. currentIPPosition = saveIP + ip.getAllocIPD();
  761. currentBPPosition = saveBP;
  762. maybeEndLayer(ip, inNewLayer);
  763. }
  764. /**
  765. * Render the given InlineBlockParent.
  766. * @param ibp the inline block parent to render
  767. */
  768. protected void renderInlineBlockParent(InlineBlockParent ibp) {
  769. int level = ibp.getBidiLevel();
  770. renderInlineAreaBackAndBorders(ibp);
  771. if ((level == -1) || ((level & 1) == 0)) {
  772. currentIPPosition += ibp.getBorderAndPaddingWidthStart();
  773. } else {
  774. currentIPPosition += ibp.getBorderAndPaddingWidthEnd();
  775. }
  776. // For inline content the BP position is updated by the enclosing line area
  777. int saveBP = currentBPPosition;
  778. currentBPPosition += ibp.getBlockProgressionOffset();
  779. renderBlock(ibp.getChildArea());
  780. currentBPPosition = saveBP;
  781. }
  782. /**
  783. * Render the given Viewport.
  784. * @param viewport the viewport to render
  785. */
  786. protected void renderInlineViewport(InlineViewport viewport) {
  787. Area content = viewport.getContent();
  788. int saveBP = currentBPPosition;
  789. currentBPPosition += viewport.getBlockProgressionOffset();
  790. Rectangle2D contpos = viewport.getContentPosition();
  791. if (content instanceof Image) {
  792. renderImage((Image) content, contpos);
  793. } else if (content instanceof Container) {
  794. renderContainer((Container) content);
  795. } else if (content instanceof ForeignObject) {
  796. renderForeignObject((ForeignObject) content, contpos);
  797. } else if (content instanceof InlineBlockParent) {
  798. renderInlineBlockParent((InlineBlockParent) content);
  799. }
  800. currentIPPosition += viewport.getAllocIPD();
  801. currentBPPosition = saveBP;
  802. }
  803. /**
  804. * Renders an image area.
  805. *
  806. * @param image The image
  807. * @param pos The target position of the image
  808. * (todo) Make renderImage() protected
  809. */
  810. public void renderImage(Image image, Rectangle2D pos) {
  811. // Default: do nothing.
  812. // Some renderers (ex. Text) don't support images.
  813. }
  814. /**
  815. * Tells the renderer to render an inline container.
  816. * @param cont The inline container area
  817. */
  818. protected void renderContainer(Container cont) {
  819. int saveIP = currentIPPosition;
  820. int saveBP = currentBPPosition;
  821. List blocks = cont.getBlocks();
  822. renderBlocks(null, blocks);
  823. currentIPPosition = saveIP;
  824. currentBPPosition = saveBP;
  825. }
  826. /**
  827. * Renders a foreign object area.
  828. *
  829. * @param fo The foreign object area
  830. * @param pos The target position of the foreign object
  831. * (todo) Make renderForeignObject() protected
  832. */
  833. protected void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  834. // Default: do nothing.
  835. // Some renderers (ex. Text) don't support foreign objects.
  836. }
  837. /**
  838. * Render the xml document with the given xml namespace.
  839. * The Render Context is by the handle to render into the current
  840. * rendering target.
  841. * @param ctx rendering context
  842. * @param doc DOM Document containing the source document
  843. * @param namespace Namespace URI of the document
  844. */
  845. public void renderXML(RendererContext ctx, Document doc,
  846. String namespace) {
  847. XMLHandler handler = userAgent.getXMLHandlerRegistry().getXMLHandler(
  848. this, namespace);
  849. if (handler != null) {
  850. try {
  851. XMLHandlerConfigurator configurator
  852. = new XMLHandlerConfigurator(userAgent);
  853. configurator.configure(ctx, namespace);
  854. handler.handleXML(ctx, doc, namespace);
  855. } catch (Exception e) {
  856. // could not handle document
  857. ResourceEventProducer eventProducer
  858. = ResourceEventProducer.Provider.get(
  859. ctx.getUserAgent().getEventBroadcaster());
  860. eventProducer.foreignXMLProcessingError(this, doc, namespace, e);
  861. }
  862. } else {
  863. if (warnedXMLHandlers == null) {
  864. warnedXMLHandlers = new java.util.HashSet();
  865. }
  866. if (!warnedXMLHandlers.contains(namespace)) {
  867. // no handler found for document
  868. warnedXMLHandlers.add(namespace);
  869. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  870. ctx.getUserAgent().getEventBroadcaster());
  871. eventProducer.foreignXMLNoHandler(this, doc, namespace);
  872. }
  873. }
  874. }
  875. /**
  876. * Converts a millipoint-based transformation matrix to points.
  877. * @param at a millipoint-based transformation matrix
  878. * @return a point-based transformation matrix
  879. */
  880. protected AffineTransform mptToPt(AffineTransform at) {
  881. double[] matrix = new double[6];
  882. at.getMatrix(matrix);
  883. //Convert to points
  884. matrix[4] = matrix[4] / 1000;
  885. matrix[5] = matrix[5] / 1000;
  886. return new AffineTransform(matrix);
  887. }
  888. /**
  889. * Converts a point-based transformation matrix to millipoints.
  890. * @param at a point-based transformation matrix
  891. * @return a millipoint-based transformation matrix
  892. */
  893. protected AffineTransform ptToMpt(AffineTransform at) {
  894. double[] matrix = new double[6];
  895. at.getMatrix(matrix);
  896. //Convert to millipoints
  897. //Math.round() because things like this can happen: 65.6 * 1000 = 65.599999999999999
  898. //which is bad for testing
  899. matrix[4] = Math.round(matrix[4] * 1000);
  900. matrix[5] = Math.round(matrix[5] * 1000);
  901. return new AffineTransform(matrix);
  902. }
  903. }