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.

AWTRenderer.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. package org.apache.fop.render.awt;
  2. /*
  3. originally contributed by
  4. Juergen Verwohlt: Juergen.Verwohlt@jCatalog.com,
  5. Rainer Steinkuhle: Rainer.Steinkuhle@jCatalog.com,
  6. Stanislav Gorkhover: Stanislav.Gorkhover@jCatalog.com
  7. */
  8. import org.apache.fop.layout.*;
  9. import org.apache.fop.layout.inline.*;
  10. import org.apache.fop.messaging.MessageHandler;
  11. import org.apache.fop.datatypes.*;
  12. import org.apache.fop.image.*;
  13. import org.apache.fop.svg.*;
  14. import org.apache.fop.render.pdf.*;
  15. import org.apache.fop.viewer.*;
  16. import org.apache.fop.apps.*;
  17. import org.w3c.dom.svg.*;
  18. import org.w3c.dom.Document;
  19. import org.apache.batik.bridge.*;
  20. import org.apache.batik.swing.svg.*;
  21. import org.apache.batik.swing.gvt.*;
  22. import org.apache.batik.gvt.*;
  23. import org.apache.batik.gvt.renderer.*;
  24. import org.apache.batik.gvt.filter.*;
  25. import org.apache.batik.gvt.event.*;
  26. import java.awt.*;
  27. import java.awt.Image;
  28. import java.awt.image.*;
  29. import java.awt.geom.*;
  30. import java.awt.font.*;
  31. import java.util.*;
  32. import java.net.URL;
  33. import java.net.MalformedURLException;
  34. import java.io.*;
  35. import java.beans.*;
  36. import javax.swing.*;
  37. import java.awt.print.*;
  38. import java.awt.image.BufferedImage;
  39. import java.text.*;
  40. import org.apache.fop.render.Renderer;
  41. public class AWTRenderer implements Renderer, Printable, Pageable {
  42. protected int pageWidth = 0;
  43. protected int pageHeight = 0;
  44. protected double scaleFactor = 100.0;
  45. protected int pageNumber = 0;
  46. protected AreaTree tree;
  47. protected ProgressListener progressListener = null;
  48. protected Translator res = null;
  49. protected Hashtable fontNames = new Hashtable();
  50. protected Hashtable fontStyles = new Hashtable();
  51. protected Color saveColor = null;
  52. /**
  53. * Image Object and Graphics Object. The Graphics Object is the Graphics
  54. * object that is contained withing the Image Object.
  55. */
  56. private BufferedImage pageImage = null;
  57. private Graphics2D graphics = null;
  58. /**
  59. * The current (internal) font name
  60. */
  61. protected String currentFontName;
  62. /**
  63. * The parent component, used to set up the font.
  64. * This is needed as FontSetup needs a live AWT component
  65. * in order to generate valid font measures.
  66. */
  67. protected Component parent;
  68. /**
  69. * The current font size in millipoints
  70. */
  71. protected int currentFontSize;
  72. /**
  73. * The current colour's red, green and blue component
  74. */
  75. protected float currentRed = 0;
  76. protected float currentGreen = 0;
  77. protected float currentBlue = 0;
  78. /**
  79. * The current vertical position in millipoints from bottom
  80. */
  81. protected int currentYPosition = 0;
  82. /**
  83. * The current horizontal position in millipoints from left
  84. */
  85. protected int currentXPosition = 0;
  86. /**
  87. * The horizontal position of the current area container
  88. */
  89. private int currentAreaContainerXPosition = 0;
  90. /** options */
  91. protected Hashtable options;
  92. /** set up renderer options */
  93. public void setOptions(Hashtable options) {
  94. this.options = options;
  95. }
  96. public AWTRenderer(Translator aRes) {
  97. res = aRes;
  98. }
  99. /**
  100. * Sets parent component which is used to set up the font.
  101. * This is needed as FontSetup needs a live AWT component
  102. * in order to generate valid font measures.
  103. * @param parent the live AWT component reference
  104. */
  105. public void setComponent(Component parent) {
  106. this.parent = parent;
  107. }
  108. public int getPageNumber() {
  109. return pageNumber;
  110. }
  111. public void setPageNumber(int aValue) {
  112. pageNumber = aValue;
  113. }
  114. public void setScaleFactor(double newScaleFactor) {
  115. scaleFactor = newScaleFactor;
  116. }
  117. public double getScaleFactor() {
  118. return scaleFactor;
  119. }
  120. public BufferedImage getLastRenderedPage() {
  121. return pageImage;
  122. }
  123. /**
  124. * add a line to the current stream
  125. *
  126. * @param x1 the start x location in millipoints
  127. * @param y1 the start y location in millipoints
  128. * @param x2 the end x location in millipoints
  129. * @param y2 the end y location in millipoints
  130. * @param th the thickness in millipoints
  131. * @param r the red component
  132. * @param g the green component
  133. * @param b the blue component
  134. */
  135. protected void addLine(int x1, int y1, int x2, int y2, int th,
  136. float r, float g, float b) {
  137. graphics.setColor(new Color (r, g, b));
  138. // graphics.setColor(Color.red);
  139. graphics.drawLine((int)(x1 / 1000f),
  140. pageHeight - (int)(y1 / 1000f), (int)(x2 / 1000f),
  141. pageHeight - (int)(y2 / 1000f));
  142. }
  143. /**
  144. * draw a filled rectangle
  145. *
  146. * @param x the x position of left edge in millipoints
  147. * @param y the y position of top edge in millipoints
  148. * @param w the width in millipoints
  149. * @param h the height in millipoints
  150. * @param r the red component
  151. * @param g the green component
  152. * @param b the blue component
  153. */
  154. protected void addRect(int x, int y, int w, int h, float r,
  155. float g, float b) {
  156. graphics.setColor(new Color (r, g, b));
  157. // graphics.setColor(Color.green);
  158. graphics.drawRect((int)(x / 1000f),
  159. pageHeight - (int)(y / 1000f), (int)(w / 1000f),
  160. -(int)(h / 1000f));
  161. }
  162. /**
  163. * draw a filled rectangle
  164. *
  165. * @param x the x position of left edge in millipoints
  166. * @param y the y position of top edge in millipoints
  167. * @param w the width in millipoints
  168. * @param h the height in millipoints
  169. * @param r the red component of edges
  170. * @param g the green component of edges
  171. * @param b the blue component of edges
  172. * @param fr the red component of the fill
  173. * @param fg the green component of the fill
  174. * @param fb the blue component of the fill
  175. */
  176. protected void addRect(int x, int y, int w, int h, float r,
  177. float g, float b, float fr, float fg, float fb) {
  178. graphics.setColor(new Color (r, g, b));
  179. graphics.fill3DRect((int)(x / 1000f),
  180. pageHeight - (int)(y / 1000f), (int)(w / 1000f),
  181. -(int)(h / 1000f), true);
  182. }
  183. /**
  184. * To configure before print.
  185. *
  186. * Choose pages
  187. * Zoom factor
  188. * Page format / Landscape or Portrait
  189. **/
  190. public void transform(Graphics2D g2d, double zoomPercent,
  191. double angle) {
  192. AffineTransform at = g2d.getTransform();
  193. at.rotate(angle);
  194. at.scale(zoomPercent / 100.0, zoomPercent / 100.0);
  195. g2d.setTransform(at);
  196. }
  197. protected void drawFrame() {
  198. int width = pageWidth;
  199. int height = pageHeight;
  200. graphics.setColor(Color.white);
  201. graphics.fillRect(0, 0, width, height);
  202. graphics.setColor(Color.black);
  203. graphics.drawRect(-1, -1, width + 2, height + 2);
  204. graphics.drawLine(width + 2, 0, width + 2, height + 2);
  205. graphics.drawLine(width + 3, 1, width + 3, height + 3);
  206. graphics.drawLine(0, height + 2, width + 2, height + 2);
  207. graphics.drawLine(1, height + 3, width + 3, height + 3);
  208. }
  209. /**
  210. * Retrieve the number of pages in this document.
  211. *
  212. * @return the number of pages
  213. */
  214. public int getPageCount() {
  215. if (tree == null) {
  216. return 0;
  217. }
  218. return tree.getPages().size();
  219. }
  220. public void render(int aPageNumber) {
  221. if (tree != null) {
  222. try {
  223. render(tree, aPageNumber);
  224. } catch (IOException e) {
  225. e.printStackTrace();
  226. // This exception can't occur because we are not dealing with
  227. // any files.
  228. }
  229. }
  230. }
  231. public void render(AreaTree areaTree,
  232. OutputStream stream) throws IOException {
  233. tree = areaTree;
  234. render(areaTree, 0);
  235. }
  236. public void render(AreaTree areaTree,
  237. int aPageNumber) throws IOException {
  238. tree = areaTree;
  239. Page page = (Page) areaTree.getPages().elementAt(aPageNumber);
  240. pageWidth = (int)((float) page.getWidth() / 1000f);
  241. pageHeight = (int)((float) page.getHeight() / 1000f);
  242. pageImage = new BufferedImage(
  243. (int)((pageWidth * (int) scaleFactor) / 100),
  244. (int)((pageHeight * (int) scaleFactor) / 100),
  245. BufferedImage.TYPE_INT_RGB);
  246. graphics = pageImage.createGraphics();
  247. transform(graphics, scaleFactor, 0);
  248. drawFrame();
  249. renderPage(page);
  250. }
  251. public void renderPage(Page page) {
  252. BodyAreaContainer body;
  253. AreaContainer before, after;
  254. body = page.getBody();
  255. before = page.getBefore();
  256. after = page.getAfter();
  257. this.currentFontName = "";
  258. this.currentFontSize = 0;
  259. renderBodyAreaContainer(body);
  260. if (before != null) {
  261. renderAreaContainer(before);
  262. }
  263. if (after != null) {
  264. renderAreaContainer(after);
  265. }
  266. // SG: Wollen wir Links abbilden?
  267. /*
  268. if (page.hasLinks()) {
  269. ....
  270. }
  271. */
  272. }
  273. public void renderAreaContainer(AreaContainer area) {
  274. int saveY = this.currentYPosition;
  275. int saveX = this.currentAreaContainerXPosition;
  276. if (area.getPosition() ==
  277. org.apache.fop.fo.properties.Position.ABSOLUTE) {
  278. // Y position is computed assuming positive Y axis, adjust
  279. //for negative postscript one
  280. this.currentYPosition =
  281. area.getYPosition() - 2 * area.getPaddingTop() -
  282. 2 * area.getBorderTopWidth();
  283. this.currentAreaContainerXPosition = area.getXPosition();
  284. } else if (area.getPosition() ==
  285. org.apache.fop.fo.properties.Position.RELATIVE) {
  286. this.currentYPosition -= area.getYPosition();
  287. this.currentAreaContainerXPosition += area.getXPosition();
  288. } else if (area.getPosition() ==
  289. org.apache.fop.fo.properties.Position.STATIC) {
  290. this.currentYPosition -=
  291. area.getPaddingTop() + area.getBorderTopWidth();
  292. this.currentAreaContainerXPosition +=
  293. area.getPaddingLeft() + area.getBorderLeftWidth();
  294. }
  295. doFrame(area);
  296. Enumeration e = area.getChildren().elements();
  297. while (e.hasMoreElements()) {
  298. org.apache.fop.layout.Box b =
  299. (org.apache.fop.layout.Box) e.nextElement();
  300. b.render(this);
  301. }
  302. if (area.getPosition() !=
  303. org.apache.fop.fo.properties.Position.STATIC) {
  304. this.currentYPosition = saveY;
  305. this.currentAreaContainerXPosition = saveX;
  306. } else {
  307. this.currentYPosition -= area.getHeight();
  308. }
  309. }
  310. // empty for now
  311. public void renderBodyAreaContainer(BodyAreaContainer area) {
  312. renderAreaContainer(area.getBeforeFloatReferenceArea());
  313. renderAreaContainer(area.getFootnoteReferenceArea());
  314. // main reference area
  315. Enumeration e =
  316. area.getMainReferenceArea().getChildren().elements();
  317. while (e.hasMoreElements()) {
  318. org.apache.fop.layout.Box b =
  319. (org.apache.fop.layout.Box) e.nextElement();
  320. b.render(this); // span areas
  321. }
  322. }
  323. // empty for now
  324. public void renderSpanArea(SpanArea area) {
  325. Enumeration e = area.getChildren().elements();
  326. while (e.hasMoreElements()) {
  327. org.apache.fop.layout.Box b =
  328. (org.apache.fop.layout.Box) e.nextElement();
  329. b.render(this); // column areas
  330. }
  331. }
  332. private void doFrame(org.apache.fop.layout.Area area) {
  333. int w, h;
  334. int rx = this.currentAreaContainerXPosition;
  335. w = area.getContentWidth();
  336. if (area instanceof BlockArea) {
  337. rx += ((BlockArea) area).getStartIndent();
  338. }
  339. h = area.getContentHeight();
  340. int ry = this.currentYPosition;
  341. ColorType bg = area.getBackgroundColor();
  342. rx = rx - area.getPaddingLeft();
  343. ry = ry + area.getPaddingTop();
  344. w = w + area.getPaddingLeft() + area.getPaddingRight();
  345. h = h + area.getPaddingTop() + area.getPaddingBottom();
  346. // I'm not sure I should have to check for bg being null
  347. // but I do
  348. if ((bg != null) && (bg.alpha() == 0)) {
  349. this.addRect(rx, ry, w, -h, bg.red(), bg.green(),
  350. bg.blue(), bg.red(), bg.green(), bg.blue());
  351. }
  352. rx = rx - area.getBorderLeftWidth();
  353. ry = ry + area.getBorderTopWidth();
  354. w = w + area.getBorderLeftWidth() + area.getBorderRightWidth();
  355. h = h + area.getBorderTopWidth() + area.getBorderBottomWidth();
  356. BorderAndPadding bp = area.getBorderAndPadding();
  357. ColorType borderColor;
  358. if (area.getBorderTopWidth() != 0) {
  359. borderColor = bp.getBorderColor(BorderAndPadding.TOP);
  360. addLine(rx, ry, rx + w, ry, area.getBorderTopWidth(),
  361. borderColor.red(), borderColor.green(),
  362. borderColor.blue());
  363. }
  364. if (area.getBorderLeftWidth() != 0) {
  365. borderColor = bp.getBorderColor(BorderAndPadding.LEFT);
  366. addLine(rx, ry, rx, ry - h, area.getBorderLeftWidth(),
  367. borderColor.red(), borderColor.green(),
  368. borderColor.blue());
  369. }
  370. if (area.getBorderRightWidth() != 0) {
  371. borderColor = bp.getBorderColor(BorderAndPadding.RIGHT);
  372. addLine(rx + w, ry, rx + w, ry - h,
  373. area.getBorderRightWidth(), borderColor.red(),
  374. borderColor.green(), borderColor.blue());
  375. }
  376. if (area.getBorderBottomWidth() != 0) {
  377. borderColor = bp.getBorderColor(BorderAndPadding.BOTTOM);
  378. addLine(rx, ry - h, rx + w, ry - h,
  379. area.getBorderBottomWidth(), borderColor.red(),
  380. borderColor.green(), borderColor.blue());
  381. }
  382. }
  383. protected Rectangle2D getBounds(org.apache.fop.layout.Area a) {
  384. return new Rectangle2D.Double(currentAreaContainerXPosition,
  385. currentYPosition, a.getAllocationWidth(), a.getHeight());
  386. }
  387. public void renderBlockArea(BlockArea area) {
  388. doFrame(area);
  389. Enumeration e = area.getChildren().elements();
  390. while (e.hasMoreElements()) {
  391. org.apache.fop.layout.Box b =
  392. (org.apache.fop.layout.Box) e.nextElement();
  393. b.render(this);
  394. }
  395. }
  396. public void setupFontInfo(FontInfo fontInfo) {
  397. FontSetup.setup(fontInfo, parent);
  398. }
  399. public void renderDisplaySpace(DisplaySpace space) {
  400. int d = space.getSize();
  401. this.currentYPosition -= d;
  402. }
  403. public void renderImageArea(ImageArea area) {
  404. int x = currentAreaContainerXPosition + area.getXOffset();
  405. int y = currentYPosition;
  406. int w = area.getContentWidth();
  407. int h = area.getHeight();
  408. FopImage img = area.getImage();
  409. if (img == null) {
  410. MessageHandler.logln("Error while loading image : area.getImage() is null");
  411. graphics.drawRect(x / 1000, pageHeight - y / 1000,
  412. w / 1000, h / 1000);
  413. java.awt.Font f = graphics.getFont();
  414. java.awt.Font smallFont =
  415. new java.awt.Font(f.getFontName(), f.getStyle(), 8);
  416. graphics.setFont(smallFont);
  417. graphics.drawString("area.getImage() is null", x / 1000,
  418. pageHeight - y / 1000);
  419. graphics.setFont(f);
  420. } else {
  421. if (img instanceof SVGImage) {
  422. try {
  423. SVGDocument svg = ((SVGImage) img).getSVGDocument();
  424. renderSVGDocument(svg, (int) x, (int) y);
  425. } catch (FopImageException e) {
  426. }
  427. } else {
  428. String urlString = img.getURL();
  429. try {
  430. URL url = new URL(urlString);
  431. ImageIcon icon = new ImageIcon(url);
  432. Image image = icon.getImage();
  433. graphics.drawImage(image, x / 1000,
  434. pageHeight - y / 1000, w / 1000, h / 1000,
  435. null);
  436. } catch (MalformedURLException mue) {
  437. // cannot normally occur because, if URL is wrong, constructing FopImage
  438. // will already have failed earlier on
  439. }
  440. }
  441. }
  442. currentYPosition -= h;
  443. }
  444. public void renderWordArea(WordArea area) {
  445. char ch;
  446. StringBuffer pdf = new StringBuffer();
  447. String name = area.getFontState().getFontName();
  448. int size = area.getFontState().getFontSize();
  449. boolean underlined = area.getUnderlined();
  450. float red = area.getRed();
  451. float green = area.getGreen();
  452. float blue = area.getBlue();
  453. FontMetricsMapper mapper;
  454. try {
  455. mapper = (FontMetricsMapper)
  456. area.getFontState().getFontInfo().getMetricsFor(name);
  457. } catch (FOPException iox) {
  458. mapper = new FontMetricsMapper("MonoSpaced",
  459. java.awt.Font.PLAIN, parent);
  460. }
  461. if ((!name.equals(this.currentFontName)) ||
  462. (size != this.currentFontSize)) {
  463. this.currentFontName = name;
  464. this.currentFontSize = size;
  465. }
  466. if ((red != this.currentRed) || (green != this.currentGreen) ||
  467. (blue != this.currentBlue)) {
  468. this.currentRed = red;
  469. this.currentGreen = green;
  470. this.currentBlue = blue;
  471. }
  472. int rx = this.currentXPosition;
  473. int bl = this.currentYPosition;
  474. String s = area.getText();
  475. Color oldColor = graphics.getColor();
  476. java.awt.Font oldFont = graphics.getFont();
  477. java.awt.Font f = mapper.getFont(size);
  478. if (saveColor != null) {
  479. if (saveColor.getRed() != red ||
  480. saveColor.getGreen() != green ||
  481. saveColor.getBlue() != blue) {
  482. saveColor = new Color(red, green, blue);
  483. }
  484. } else {
  485. saveColor = new Color(red, green, blue);
  486. }
  487. graphics.setColor(saveColor);
  488. FontRenderContext newContext = graphics.getFontRenderContext();
  489. AttributedString ats = new AttributedString(s);
  490. ats.addAttribute(TextAttribute.FONT, f);
  491. if (underlined) {
  492. ats.addAttribute(TextAttribute.UNDERLINE,
  493. TextAttribute.UNDERLINE_ON);
  494. }
  495. AttributedCharacterIterator iter = ats.getIterator();
  496. graphics.drawString(iter, rx / 1000f,
  497. (int)(pageHeight - bl / 1000f));
  498. graphics.setColor(oldColor);
  499. this.currentXPosition += area.getContentWidth();
  500. }
  501. public void renderInlineSpace(InlineSpace space) {
  502. this.currentXPosition += space.getSize();
  503. }
  504. public void renderLineArea(LineArea area) {
  505. int rx = this.currentAreaContainerXPosition + area.getStartIndent();
  506. int ry = this.currentYPosition;
  507. int w = area.getContentWidth();
  508. int h = area.getHeight();
  509. this.currentYPosition -= area.getPlacementOffset();
  510. this.currentXPosition = rx;
  511. int bl = this.currentYPosition;
  512. Enumeration e = area.getChildren().elements();
  513. while (e.hasMoreElements()) {
  514. org.apache.fop.layout.Box b =
  515. (org.apache.fop.layout.Box) e.nextElement();
  516. if (b instanceof InlineArea) {
  517. InlineArea ia = (InlineArea) b;
  518. this.currentYPosition = ry - ia.getYOffset();
  519. } else {
  520. this.currentYPosition = ry - area.getPlacementOffset();
  521. }
  522. b.render(this);
  523. }
  524. this.currentYPosition = ry - h;
  525. }
  526. /**
  527. * render leader area into AWT
  528. *
  529. * @param area area to render
  530. */
  531. public void renderLeaderArea(LeaderArea area) {
  532. int rx = this.currentXPosition;
  533. int ry = this.currentYPosition;
  534. int w = area.getLeaderLength();
  535. int h = area.getHeight();
  536. int th = area.getRuleThickness();
  537. int st = area.getRuleStyle(); //not used at the moment
  538. float r = area.getRed();
  539. float g = area.getGreen();
  540. float b = area.getBlue();
  541. Color oldColor = graphics.getColor();
  542. graphics.setColor(new Color(r, g, b));
  543. graphics.fillRect((int)(rx / 1000f),
  544. (int)(pageHeight - ry / 1000f), (int)(w / 1000f),
  545. (int)(th / 1000f));
  546. graphics.setColor(oldColor);
  547. this.currentXPosition += area.getContentWidth();
  548. }
  549. public void renderSVGArea(SVGArea area) {
  550. int x = this.currentXPosition;
  551. int y = this.currentYPosition;
  552. int w = area.getContentWidth();
  553. int h = area.getHeight();
  554. Document doc = area.getSVGDocument();
  555. renderSVGDocument(doc, x, y);
  556. this.currentXPosition += area.getContentWidth();
  557. }
  558. protected void renderSVGDocument(Document doc, int x, int y) {
  559. UserAgent userAgent = new MUserAgent(new AffineTransform());
  560. GVTBuilder builder = new GVTBuilder();
  561. GraphicsNodeRenderContext rc = getRenderContext();
  562. BridgeContext ctx = new BridgeContext(userAgent, rc);
  563. GraphicsNode root;
  564. graphics.translate(x / 1000f, pageHeight - y / 1000f);
  565. graphics.setRenderingHints(rc.getRenderingHints());
  566. try {
  567. root = builder.build(ctx, doc);
  568. root.paint(graphics, rc);
  569. } catch (Exception e) {
  570. e.printStackTrace();
  571. }
  572. graphics.translate(-x / 1000f, y / 1000f - pageHeight);
  573. }
  574. public GraphicsNodeRenderContext getRenderContext() {
  575. GraphicsNodeRenderContext nodeRenderContext = null;
  576. if (nodeRenderContext == null) {
  577. RenderingHints hints = new RenderingHints(null);
  578. hints.put(RenderingHints.KEY_ANTIALIASING,
  579. RenderingHints.VALUE_ANTIALIAS_ON);
  580. hints.put(RenderingHints.KEY_INTERPOLATION,
  581. RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  582. FontRenderContext fontRenderContext =
  583. new FontRenderContext(new AffineTransform(), true,
  584. true);
  585. TextPainter textPainter = new StrokingTextPainter();
  586. GraphicsNodeRableFactory gnrFactory =
  587. new ConcreteGraphicsNodeRableFactory();
  588. nodeRenderContext = new GraphicsNodeRenderContext(
  589. new AffineTransform(), null, hints,
  590. fontRenderContext, textPainter, gnrFactory);
  591. }
  592. return nodeRenderContext;
  593. }
  594. public void setProducer(String producer) {
  595. // defined in Renderer Interface
  596. }
  597. public int print(Graphics g, PageFormat pageFormat,
  598. int pageIndex) throws PrinterException {
  599. if (pageIndex >= tree.getPages().size())
  600. return NO_SUCH_PAGE;
  601. Graphics2D oldGraphics = graphics;
  602. int oldPageNumber = pageNumber;
  603. graphics = (Graphics2D) g;
  604. Page aPage = (Page) tree.getPages().elementAt(pageIndex);
  605. renderPage(aPage);
  606. graphics = oldGraphics;
  607. return PAGE_EXISTS;
  608. }
  609. public int getNumberOfPages() {
  610. return tree.getPages().size();
  611. }
  612. public PageFormat getPageFormat(int pageIndex)
  613. throws IndexOutOfBoundsException {
  614. if (pageIndex >= tree.getPages().size())
  615. return null;
  616. Page page = (Page) tree.getPages().elementAt(pageIndex);
  617. PageFormat pageFormat = new PageFormat();
  618. Paper paper = new Paper();
  619. paper.setImageableArea(0, 0, page.getWidth() / 1000d,
  620. page.getHeight() / 1000d);
  621. paper.setSize(page.getWidth() / 1000d, page.getHeight() / 1000d);
  622. pageFormat.setPaper(paper);
  623. return pageFormat;
  624. }
  625. public Printable getPrintable(int pageIndex)
  626. throws IndexOutOfBoundsException {
  627. return this;
  628. }
  629. public void setProgressListener(ProgressListener l) {
  630. progressListener = l;
  631. }
  632. public static Color colorType2Color(ColorType ct) {
  633. if (ct == null) {
  634. return null;
  635. }
  636. return new Color(ct.red(), ct.green(), ct.blue());
  637. }
  638. /**
  639. * Draws an image.
  640. * TODO: protect other image formats (JIMI)
  641. */
  642. /* public void renderImage(String href, float x, float y, float width,
  643. float height, Vector transform) {
  644. // What is with transformations?
  645. try {
  646. URL url = new URL(href);
  647. ImageIcon imageIcon = new ImageIcon(url);
  648. AffineTransform fullTransform = new AffineTransform();
  649. AffineTransform aTransform;
  650. transform = (transform == null) ? new Vector() : transform;
  651. for (int i = 0; i < transform.size(); i++) {
  652. org.w3c.dom.svg.SVGTransform t =
  653. (org.w3c.dom.svg.SVGTransform)
  654. transform.elementAt(i);
  655. SVGMatrix matrix = t.getMatrix();
  656. aTransform = new AffineTransform(matrix.getA(),
  657. matrix.getB(), matrix.getC(), matrix.getD(),
  658. matrix.getE(), matrix.getF());
  659. fullTransform.concatenate(aTransform);
  660. }
  661. BufferedImage bi = new BufferedImage((int) width, (int) height,
  662. BufferedImage.TYPE_INT_RGB);
  663. Graphics2D g2d = bi.createGraphics();
  664. BufferedImageOp bop = new AffineTransformOp(fullTransform,
  665. AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
  666. g2d.drawImage(imageIcon.getImage(), 0, 0, (int) width,
  667. (int) height, imageIcon.getImageObserver());
  668. graphics.drawImage(bi, bop, (int) x, (int) y);
  669. } catch (Exception ex) {
  670. MessageHandler.errorln("AWTRenderer: renderImage(): " +
  671. ex.getMessage());
  672. }
  673. }*/
  674. public void renderForeignObjectArea(ForeignObjectArea area) {
  675. area.getObject().render(this);
  676. }
  677. protected class MUserAgent implements UserAgent {
  678. AffineTransform currentTransform = null;
  679. /**
  680. * Creates a new SVGUserAgent.
  681. */
  682. protected MUserAgent(AffineTransform at) {
  683. currentTransform = at;
  684. }
  685. /**
  686. * Displays an error message.
  687. */
  688. public void displayError(String message) {
  689. System.err.println(message);
  690. }
  691. /**
  692. * Displays an error resulting from the specified Exception.
  693. */
  694. public void displayError(Exception ex) {
  695. ex.printStackTrace(System.err);
  696. }
  697. /**
  698. * Displays a message in the User Agent interface.
  699. * The given message is typically displayed in a status bar.
  700. */
  701. public void displayMessage(String message) {
  702. System.out.println(message);
  703. }
  704. /**
  705. * Returns a customized the pixel to mm factor.
  706. */
  707. public float getPixelToMM() {
  708. return 0.264583333333333333333f; // 72 dpi
  709. }
  710. /**
  711. * Returns the language settings.
  712. */
  713. public String getLanguages() {
  714. return "en";//userLanguages;
  715. }
  716. /**
  717. * Returns the user stylesheet uri.
  718. * @return null if no user style sheet was specified.
  719. */
  720. public String getUserStyleSheetURI() {
  721. return null;//userStyleSheetURI;
  722. }
  723. /**
  724. * Returns the class name of the XML parser.
  725. */
  726. public String getXMLParserClassName() {
  727. String parserClassName = System.getProperty("org.xml.sax.parser");
  728. if (parserClassName == null) {
  729. parserClassName = "org.apache.xerces.parsers.SAXParser";
  730. }
  731. return parserClassName;//application.getXMLParserClassName();
  732. }
  733. /**
  734. * Opens a link in a new component.
  735. * @param doc The current document.
  736. * @param uri The document URI.
  737. */
  738. public void openLink(SVGAElement elt) {
  739. //application.openLink(uri);
  740. }
  741. public Point getClientAreaLocationOnScreen() {
  742. return new Point(0, 0);
  743. }
  744. public void setSVGCursor(java.awt.Cursor cursor) {
  745. }
  746. public AffineTransform getTransform() {
  747. return currentTransform;
  748. }
  749. public Dimension2D getViewportSize() {
  750. return new Dimension(100, 100);
  751. }
  752. public EventDispatcher getEventDispatcher() {
  753. return null;
  754. }
  755. public boolean supportExtension(String str) {
  756. return false;
  757. }
  758. public boolean hasFeature(String str) {
  759. return false;
  760. }
  761. public void registerExtension(BridgeExtension be) {
  762. }
  763. }
  764. }