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

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