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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 org.w3c.dom.Document;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. import org.apache.fop.ResourceEventProducer;
  32. import org.apache.fop.apps.FOPException;
  33. import org.apache.fop.apps.FOUserAgent;
  34. import org.apache.fop.area.Area;
  35. import org.apache.fop.area.BeforeFloat;
  36. import org.apache.fop.area.Block;
  37. import org.apache.fop.area.BlockViewport;
  38. import org.apache.fop.area.BodyRegion;
  39. import org.apache.fop.area.CTM;
  40. import org.apache.fop.area.Footnote;
  41. import org.apache.fop.area.LineArea;
  42. import org.apache.fop.area.MainReference;
  43. import org.apache.fop.area.NormalFlow;
  44. import org.apache.fop.area.OffDocumentItem;
  45. import org.apache.fop.area.Page;
  46. import org.apache.fop.area.PageSequence;
  47. import org.apache.fop.area.PageViewport;
  48. import org.apache.fop.area.RegionReference;
  49. import org.apache.fop.area.RegionViewport;
  50. import org.apache.fop.area.Span;
  51. import org.apache.fop.area.Trait;
  52. import org.apache.fop.area.inline.Container;
  53. import org.apache.fop.area.inline.FilledArea;
  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. /**
  104. *
  105. * @param userAgent the user agent that contains configuration details. This cannot be null.
  106. */
  107. public AbstractRenderer(FOUserAgent userAgent) {
  108. this.userAgent = userAgent;
  109. }
  110. /** {@inheritDoc} */
  111. public FOUserAgent getUserAgent() {
  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. Span span = null;
  361. List spans = mr.getSpans();
  362. int saveBPPos = currentBPPosition;
  363. int saveSpanBPPos = saveBPPos;
  364. int saveIPPos = currentIPPosition;
  365. for (int count = 0; count < spans.size(); count++) {
  366. span = (Span) spans.get(count);
  367. int level = span.getBidiLevel();
  368. if ( level < 0 ) {
  369. level = 0;
  370. }
  371. if ( ( level & 1 ) == 1 ) {
  372. currentIPPosition += span.getIPD();
  373. currentIPPosition += mr.getColumnGap();
  374. }
  375. for (int c = 0; c < span.getColumnCount(); c++) {
  376. NormalFlow flow = span.getNormalFlow(c);
  377. if (flow != null) {
  378. currentBPPosition = saveSpanBPPos;
  379. if ( ( level & 1 ) == 1 ) {
  380. currentIPPosition -= flow.getIPD();
  381. currentIPPosition -= mr.getColumnGap();
  382. }
  383. renderFlow(flow);
  384. if ( ( level & 1 ) == 0 ) {
  385. currentIPPosition += flow.getIPD();
  386. currentIPPosition += mr.getColumnGap();
  387. }
  388. }
  389. }
  390. currentIPPosition = saveIPPos;
  391. currentBPPosition = saveSpanBPPos + span.getHeight();
  392. saveSpanBPPos = currentBPPosition;
  393. }
  394. currentBPPosition = saveBPPos;
  395. }
  396. /**
  397. * Renders a flow reference area.
  398. *
  399. * @param flow The flow reference area
  400. */
  401. protected void renderFlow(NormalFlow flow) {
  402. // the normal flow reference area contains stacked blocks
  403. List blocks = flow.getChildAreas();
  404. if (blocks != null) {
  405. renderBlocks(null, blocks);
  406. }
  407. }
  408. /**
  409. * Handle block traits.
  410. * This method is called when the correct ip and bp posiiton is
  411. * set. This should be overridden to draw border and background
  412. * traits for the block area.
  413. *
  414. * @param block the block area
  415. */
  416. protected void handleBlockTraits(Block block) {
  417. // draw border and background
  418. }
  419. /**
  420. * Renders a block viewport.
  421. *
  422. * @param bv The block viewport
  423. * @param children The children to render within the block viewport
  424. */
  425. protected void renderBlockViewport(BlockViewport bv, List children) {
  426. // clip and position viewport if necessary
  427. if (bv.getPositioning() == Block.ABSOLUTE) {
  428. // save positions
  429. int saveIP = currentIPPosition;
  430. int saveBP = currentBPPosition;
  431. Rectangle clippingRect = null;
  432. if (bv.hasClip()) {
  433. clippingRect = new Rectangle(saveIP, saveBP, bv.getIPD(), bv.getBPD());
  434. }
  435. CTM ctm = bv.getCTM();
  436. currentIPPosition = 0;
  437. currentBPPosition = 0;
  438. startVParea(ctm, clippingRect);
  439. handleBlockTraits(bv);
  440. renderBlocks(bv, children);
  441. endVParea();
  442. // clip if necessary
  443. currentIPPosition = saveIP;
  444. currentBPPosition = saveBP;
  445. } else {
  446. // save position and offset
  447. int saveIP = currentIPPosition;
  448. int saveBP = currentBPPosition;
  449. handleBlockTraits(bv);
  450. renderBlocks(bv, children);
  451. currentIPPosition = saveIP;
  452. currentBPPosition = saveBP + bv.getAllocBPD();
  453. }
  454. }
  455. /**
  456. * Renders a block area that represents a reference area. The reference area establishes
  457. * a new coordinate system.
  458. * @param block the block area
  459. */
  460. protected abstract void renderReferenceArea(Block block);
  461. /**
  462. * Renders a list of block areas.
  463. *
  464. * @param parent the parent block if the parent is a block, otherwise
  465. * a null value.
  466. * @param blocks The block areas
  467. */
  468. protected void renderBlocks(Block parent, List blocks) {
  469. int saveIP = currentIPPosition;
  470. // Calculate the position of the content rectangle.
  471. if (parent != null && !parent.getTraitAsBoolean(Trait.IS_VIEWPORT_AREA)) {
  472. currentBPPosition += parent.getBorderAndPaddingWidthBefore();
  473. }
  474. // the position of the containing block is used for
  475. // absolutely positioned areas
  476. int contBP = currentBPPosition;
  477. int contIP = currentIPPosition;
  478. containingBPPosition = currentBPPosition;
  479. containingIPPosition = currentIPPosition;
  480. for (int count = 0; count < blocks.size(); count++) {
  481. Object obj = blocks.get(count);
  482. if (obj instanceof Block) {
  483. currentIPPosition = contIP;
  484. containingBPPosition = contBP;
  485. containingIPPosition = contIP;
  486. renderBlock((Block) obj);
  487. containingBPPosition = contBP;
  488. containingIPPosition = contIP;
  489. } else if (obj instanceof LineArea) {
  490. // a line area is rendered from the top left position
  491. // of the line, each inline object is offset from there
  492. LineArea line = (LineArea) obj;
  493. if ( parent != null ) {
  494. int level = parent.getBidiLevel();
  495. if ( ( level == -1 ) || ( ( level & 1 ) == 0 ) ) {
  496. currentIPPosition += parent.getStartIndent();
  497. } else {
  498. currentIPPosition += parent.getEndIndent();
  499. }
  500. }
  501. renderLineArea(line);
  502. currentBPPosition += line.getAllocBPD();
  503. }
  504. currentIPPosition = saveIP;
  505. }
  506. }
  507. /**
  508. * Renders a block area.
  509. *
  510. * @param block The block area
  511. */
  512. protected void renderBlock(Block block) {
  513. assert block != null;
  514. List children = block.getChildAreas();
  515. if (block instanceof BlockViewport) {
  516. if (children != null) {
  517. renderBlockViewport((BlockViewport) block, children);
  518. } else {
  519. handleBlockTraits(block);
  520. // simply move position
  521. currentBPPosition += block.getAllocBPD();
  522. }
  523. } else if (block.getTraitAsBoolean(Trait.IS_REFERENCE_AREA)) {
  524. renderReferenceArea(block);
  525. } else {
  526. // save position and offset
  527. int saveIP = currentIPPosition;
  528. int saveBP = currentBPPosition;
  529. currentIPPosition += block.getXOffset();
  530. currentBPPosition += block.getYOffset();
  531. currentBPPosition += block.getSpaceBefore();
  532. handleBlockTraits(block);
  533. if (children != null) {
  534. renderBlocks(block, children);
  535. }
  536. if (block.getPositioning() == Block.ABSOLUTE) {
  537. // absolute blocks do not effect the layout
  538. currentBPPosition = saveBP;
  539. } else {
  540. // stacked and relative blocks effect stacking
  541. currentIPPosition = saveIP;
  542. currentBPPosition = saveBP + block.getAllocBPD();
  543. }
  544. }
  545. }
  546. /**
  547. * Renders a line area. <p>
  548. *
  549. * A line area may have grouped styling for its children such as underline,
  550. * background.</p>
  551. *
  552. * @param line The line area
  553. */
  554. protected void renderLineArea(LineArea line) {
  555. List children = line.getInlineAreas();
  556. int saveBP = currentBPPosition;
  557. currentBPPosition += line.getSpaceBefore();
  558. int bl = line.getBidiLevel();
  559. if ( bl >= 0 ) {
  560. if ( ( bl & 1 ) == 0 ) {
  561. currentIPPosition += line.getStartIndent();
  562. } else {
  563. currentIPPosition += line.getEndIndent();
  564. // if line's content overflows line area, then
  565. // ensure that overflow is drawn (extends)
  566. // outside of left side of line area
  567. int overflow = computeInlinesOverflow ( line );
  568. if ( overflow > 0 ) {
  569. currentIPPosition -= overflow;
  570. }
  571. }
  572. } else {
  573. currentIPPosition += line.getStartIndent();
  574. }
  575. for (int i = 0, l = children.size(); i < l; i++) {
  576. InlineArea inline = (InlineArea) children.get(i);
  577. renderInlineArea(inline);
  578. }
  579. currentBPPosition = saveBP;
  580. }
  581. private int computeInlinesOverflow ( LineArea line ) {
  582. List children = line.getInlineAreas();
  583. int ipdConsumed = 0;
  584. for (int i = 0, l = children.size(); i < l; i++) {
  585. InlineArea inline = (InlineArea) children.get(i);
  586. ipdConsumed += inline.getIPD();
  587. }
  588. return ipdConsumed - line.getIPD();
  589. }
  590. /**
  591. * Render the given InlineArea.
  592. * @param inlineArea inline area text to render
  593. */
  594. protected void renderInlineArea(InlineArea inlineArea) {
  595. if (inlineArea instanceof TextArea) {
  596. renderText((TextArea) inlineArea);
  597. //} else if (inlineArea instanceof Character) {
  598. //renderCharacter((Character) inlineArea);
  599. } else if (inlineArea instanceof WordArea) {
  600. renderWord((WordArea) inlineArea);
  601. } else if (inlineArea instanceof SpaceArea) {
  602. renderSpace((SpaceArea) inlineArea);
  603. } else if (inlineArea instanceof InlineParent) {
  604. renderInlineParent((InlineParent) inlineArea);
  605. } else if (inlineArea instanceof InlineBlockParent) {
  606. renderInlineBlockParent((InlineBlockParent) inlineArea);
  607. } else if (inlineArea instanceof Space) {
  608. renderInlineSpace((Space) inlineArea);
  609. } else if (inlineArea instanceof InlineViewport) {
  610. renderInlineViewport((InlineViewport) inlineArea);
  611. } else if (inlineArea instanceof Leader) {
  612. renderLeader((Leader) inlineArea);
  613. }
  614. }
  615. /**
  616. * Common method to render the background and borders for any inline area.
  617. * The all borders and padding are drawn outside the specified area.
  618. * @param area the inline area for which the background, border and padding is to be
  619. * rendered
  620. */
  621. protected abstract void renderInlineAreaBackAndBorders(InlineArea area);
  622. /**
  623. * Render the given Space.
  624. * @param space the space to render
  625. */
  626. protected void renderInlineSpace(Space space) {
  627. renderInlineAreaBackAndBorders(space);
  628. // an inline space moves the inline progression position
  629. // for the current block by the width or height of the space
  630. // it may also have styling (only on this object) that needs
  631. // handling
  632. currentIPPosition += space.getAllocIPD();
  633. }
  634. /**
  635. * Render the given Leader.
  636. * @param area the leader to render
  637. */
  638. protected void renderLeader(Leader area) {
  639. currentIPPosition += area.getAllocIPD();
  640. }
  641. /**
  642. * Render the given TextArea.
  643. * @param text the text to render
  644. */
  645. protected void renderText(TextArea text) {
  646. List children = text.getChildAreas();
  647. int saveIP = currentIPPosition;
  648. int saveBP = currentBPPosition;
  649. for (int i = 0, l = children.size(); i < l; i++) {
  650. InlineArea inline = (InlineArea) children.get(i);
  651. renderInlineArea(inline);
  652. }
  653. currentIPPosition = saveIP + text.getAllocIPD();
  654. }
  655. /**
  656. * Render the given WordArea.
  657. * @param word the word to render
  658. */
  659. protected void renderWord(WordArea word) {
  660. currentIPPosition += word.getAllocIPD();
  661. }
  662. /**
  663. * Render the given SpaceArea.
  664. * @param space the space to render
  665. */
  666. protected void renderSpace(SpaceArea space) {
  667. currentIPPosition += space.getAllocIPD();
  668. }
  669. /**
  670. * Render the given InlineParent.
  671. * @param ip the inline parent to render
  672. */
  673. protected void renderInlineParent(InlineParent ip) {
  674. int level = ip.getBidiLevel();
  675. List children = ip.getChildAreas();
  676. renderInlineAreaBackAndBorders(ip);
  677. int saveIP = currentIPPosition;
  678. int saveBP = currentBPPosition;
  679. // if inline parent is a filled area (generated by Leader), and if
  680. // it is right-to-left, then adjust starting ip position in order to
  681. // align children to starting (right) edge of filled area
  682. int ipAdjust;
  683. if ( ( ip instanceof FilledArea ) && ( ( level & 1 ) != 0 ) ) {
  684. int ipdChildren = 0;
  685. for (int i = 0, l = children.size(); i < l; i++) {
  686. InlineArea inline = (InlineArea) children.get(i);
  687. ipdChildren += inline.getAllocIPD();
  688. }
  689. ipAdjust = ip.getAllocIPD() - ipdChildren;
  690. } else {
  691. ipAdjust = 0;
  692. }
  693. // perform inline position adjustments
  694. if ( ( level == -1 ) || ( ( level & 1 ) == 0 ) ) {
  695. currentIPPosition += ip.getBorderAndPaddingWidthStart();
  696. } else {
  697. currentIPPosition += ip.getBorderAndPaddingWidthEnd();
  698. if ( ipAdjust > 0 ) {
  699. currentIPPosition += ipAdjust;
  700. }
  701. }
  702. currentBPPosition += ip.getBlockProgressionOffset();
  703. // render children inlines
  704. for (int i = 0, l = children.size(); i < l; i++) {
  705. InlineArea inline = (InlineArea) children.get(i);
  706. renderInlineArea(inline);
  707. }
  708. currentIPPosition = saveIP + ip.getAllocIPD();
  709. currentBPPosition = saveBP;
  710. }
  711. /**
  712. * Render the given InlineBlockParent.
  713. * @param ibp the inline block parent to render
  714. */
  715. protected void renderInlineBlockParent(InlineBlockParent ibp) {
  716. int level = ibp.getBidiLevel();
  717. renderInlineAreaBackAndBorders(ibp);
  718. if ( ( level == -1 ) || ( ( level & 1 ) == 0 ) ) {
  719. currentIPPosition += ibp.getBorderAndPaddingWidthStart();
  720. } else {
  721. currentIPPosition += ibp.getBorderAndPaddingWidthEnd();
  722. }
  723. // For inline content the BP position is updated by the enclosing line area
  724. int saveBP = currentBPPosition;
  725. currentBPPosition += ibp.getBlockProgressionOffset();
  726. renderBlock(ibp.getChildArea());
  727. currentBPPosition = saveBP;
  728. }
  729. /**
  730. * Render the given Viewport.
  731. * @param viewport the viewport to render
  732. */
  733. protected void renderInlineViewport(InlineViewport viewport) {
  734. Area content = viewport.getContent();
  735. int saveBP = currentBPPosition;
  736. currentBPPosition += viewport.getBlockProgressionOffset();
  737. Rectangle2D contpos = viewport.getContentPosition();
  738. if (content instanceof Image) {
  739. renderImage((Image) content, contpos);
  740. } else if (content instanceof Container) {
  741. renderContainer((Container) content);
  742. } else if (content instanceof ForeignObject) {
  743. renderForeignObject((ForeignObject) content, contpos);
  744. } else if (content instanceof InlineBlockParent) {
  745. renderInlineBlockParent((InlineBlockParent) content);
  746. }
  747. currentIPPosition += viewport.getAllocIPD();
  748. currentBPPosition = saveBP;
  749. }
  750. /**
  751. * Renders an image area.
  752. *
  753. * @param image The image
  754. * @param pos The target position of the image
  755. * (todo) Make renderImage() protected
  756. */
  757. public void renderImage(Image image, Rectangle2D pos) {
  758. // Default: do nothing.
  759. // Some renderers (ex. Text) don't support images.
  760. }
  761. /**
  762. * Tells the renderer to render an inline container.
  763. * @param cont The inline container area
  764. */
  765. protected void renderContainer(Container cont) {
  766. int saveIP = currentIPPosition;
  767. int saveBP = currentBPPosition;
  768. List blocks = cont.getBlocks();
  769. renderBlocks(null, blocks);
  770. currentIPPosition = saveIP;
  771. currentBPPosition = saveBP;
  772. }
  773. /**
  774. * Renders a foreign object area.
  775. *
  776. * @param fo The foreign object area
  777. * @param pos The target position of the foreign object
  778. * (todo) Make renderForeignObject() protected
  779. */
  780. protected void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
  781. // Default: do nothing.
  782. // Some renderers (ex. Text) don't support foreign objects.
  783. }
  784. /**
  785. * Render the xml document with the given xml namespace.
  786. * The Render Context is by the handle to render into the current
  787. * rendering target.
  788. * @param ctx rendering context
  789. * @param doc DOM Document containing the source document
  790. * @param namespace Namespace URI of the document
  791. */
  792. public void renderXML(RendererContext ctx, Document doc,
  793. String namespace) {
  794. XMLHandler handler = userAgent.getXMLHandlerRegistry().getXMLHandler(
  795. this, namespace);
  796. if (handler != null) {
  797. try {
  798. XMLHandlerConfigurator configurator
  799. = new XMLHandlerConfigurator(userAgent);
  800. configurator.configure(ctx, namespace);
  801. handler.handleXML(ctx, doc, namespace);
  802. } catch (Exception e) {
  803. // could not handle document
  804. ResourceEventProducer eventProducer
  805. = ResourceEventProducer.Provider.get(
  806. ctx.getUserAgent().getEventBroadcaster());
  807. eventProducer.foreignXMLProcessingError(this, doc, namespace, e);
  808. }
  809. } else {
  810. if (warnedXMLHandlers == null) {
  811. warnedXMLHandlers = new java.util.HashSet();
  812. }
  813. if (!warnedXMLHandlers.contains(namespace)) {
  814. // no handler found for document
  815. warnedXMLHandlers.add(namespace);
  816. ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
  817. ctx.getUserAgent().getEventBroadcaster());
  818. eventProducer.foreignXMLNoHandler(this, doc, namespace);
  819. }
  820. }
  821. }
  822. /**
  823. * Converts a millipoint-based transformation matrix to points.
  824. * @param at a millipoint-based transformation matrix
  825. * @return a point-based transformation matrix
  826. */
  827. protected AffineTransform mptToPt(AffineTransform at) {
  828. double[] matrix = new double[6];
  829. at.getMatrix(matrix);
  830. //Convert to points
  831. matrix[4] = matrix[4] / 1000;
  832. matrix[5] = matrix[5] / 1000;
  833. return new AffineTransform(matrix);
  834. }
  835. /**
  836. * Converts a point-based transformation matrix to millipoints.
  837. * @param at a point-based transformation matrix
  838. * @return a millipoint-based transformation matrix
  839. */
  840. protected AffineTransform ptToMpt(AffineTransform at) {
  841. double[] matrix = new double[6];
  842. at.getMatrix(matrix);
  843. //Convert to millipoints
  844. //Math.round() because things like this can happen: 65.6 * 1000 = 65.599999999999999
  845. //which is bad for testing
  846. matrix[4] = Math.round(matrix[4] * 1000);
  847. matrix[5] = Math.round(matrix[5] * 1000);
  848. return new AffineTransform(matrix);
  849. }
  850. }