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.

SVGImageRenderer.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xslf.draw;
  16. import java.awt.Dimension;
  17. import java.awt.Graphics2D;
  18. import java.awt.Insets;
  19. import java.awt.RenderingHints;
  20. import java.awt.geom.Rectangle2D;
  21. import java.awt.image.BufferedImage;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.lang.ref.WeakReference;
  26. import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
  27. import org.apache.batik.bridge.BridgeContext;
  28. import org.apache.batik.bridge.DocumentLoader;
  29. import org.apache.batik.bridge.GVTBuilder;
  30. import org.apache.batik.bridge.UserAgent;
  31. import org.apache.batik.bridge.UserAgentAdapter;
  32. import org.apache.batik.ext.awt.RenderingHintsKeyExt;
  33. import org.apache.batik.ext.awt.image.renderable.ClipRable8Bit;
  34. import org.apache.batik.gvt.GraphicsNode;
  35. import org.apache.batik.util.XMLResourceDescriptor;
  36. import org.apache.poi.sl.draw.ImageRenderer;
  37. import org.apache.poi.sl.usermodel.PictureData;
  38. import org.w3c.dom.Document;
  39. public class SVGImageRenderer implements ImageRenderer {
  40. private final GVTBuilder builder = new GVTBuilder();
  41. private final BridgeContext context;
  42. private final SAXSVGDocumentFactory svgFact;
  43. private GraphicsNode svgRoot;
  44. private double alpha = 1.0;
  45. public SVGImageRenderer() {
  46. String parser = XMLResourceDescriptor.getXMLParserClassName();
  47. // TOOO: tell the batik guys to use secure parsing feature
  48. svgFact = new SAXSVGDocumentFactory(parser);
  49. UserAgent agent = new UserAgentAdapter();
  50. DocumentLoader loader = new DocumentLoader(agent);
  51. context = new BridgeContext(agent, loader);
  52. context.setDynamic(true);
  53. }
  54. @Override
  55. public void loadImage(InputStream data, String contentType) throws IOException {
  56. Document document = svgFact.createDocument("", data);
  57. svgRoot = builder.build(context, document);
  58. }
  59. @Override
  60. public void loadImage(byte[] data, String contentType) throws IOException {
  61. loadImage(new ByteArrayInputStream(data), contentType);
  62. }
  63. @Override
  64. public Dimension getDimension() {
  65. Rectangle2D r = svgRoot.getPrimitiveBounds();
  66. return new Dimension((int)Math.ceil(r.getWidth()), (int)Math.ceil(r.getHeight()));
  67. }
  68. @Override
  69. public void setAlpha(double alpha) {
  70. this.alpha = alpha;
  71. }
  72. @Override
  73. public BufferedImage getImage() {
  74. return getImage(getDimension());
  75. }
  76. @Override
  77. public BufferedImage getImage(Dimension dim) {
  78. BufferedImage bi = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
  79. Graphics2D g2d = (Graphics2D) bi.getGraphics();
  80. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  81. g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  82. g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  83. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  84. g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
  85. g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  86. g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, new WeakReference(bi));
  87. Dimension dimSVG = getDimension();
  88. double scaleX = dim.getWidth() / dimSVG.getWidth();
  89. double scaleY = dim.getHeight() / dimSVG.getHeight();
  90. g2d.scale(scaleX, scaleY);
  91. svgRoot.paint(g2d);
  92. g2d.dispose();
  93. return bi;
  94. }
  95. @Override
  96. public boolean drawImage(Graphics2D graphics, Rectangle2D anchor) {
  97. return drawImage(graphics, anchor, null);
  98. }
  99. @Override
  100. public boolean drawImage(Graphics2D graphics, Rectangle2D anchor, Insets clip) {
  101. if (clip == null) {
  102. svgRoot.setClip(null);
  103. } else {
  104. Rectangle2D clippedRect = new Rectangle2D.Double(
  105. anchor.getX()+clip.left,
  106. anchor.getY()+clip.top,
  107. anchor.getWidth()-(clip.left+clip.right),
  108. anchor.getHeight()-(clip.top+clip.bottom)
  109. );
  110. svgRoot.setClip(new ClipRable8Bit(null, clippedRect));
  111. }
  112. svgRoot.paint(graphics);
  113. return true;
  114. }
  115. @Override
  116. public boolean canRender(String contentType) {
  117. return PictureData.PictureType.SVG.contentType.equalsIgnoreCase(contentType);
  118. }
  119. }