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

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