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.

DrawPictureShape.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.sl.draw;
  16. import java.awt.Dimension;
  17. import java.awt.Graphics2D;
  18. import java.awt.Insets;
  19. import java.awt.geom.Rectangle2D;
  20. import java.io.IOException;
  21. import org.apache.poi.sl.usermodel.PictureData;
  22. import org.apache.poi.sl.usermodel.PictureShape;
  23. import org.apache.poi.sl.usermodel.RectAlign;
  24. import org.apache.poi.util.POILogFactory;
  25. import org.apache.poi.util.POILogger;
  26. public class DrawPictureShape extends DrawSimpleShape {
  27. private static final POILogger LOG = POILogFactory.getLogger(DrawPictureShape.class);
  28. private static final String[] KNOWN_RENDERER = {
  29. "org.apache.poi.hwmf.draw.HwmfImageRenderer",
  30. "org.apache.poi.hemf.draw.HemfImageRenderer",
  31. "org.apache.poi.xslf.draw.SVGImageRenderer"
  32. };
  33. public DrawPictureShape(PictureShape<?,?> shape) {
  34. super(shape);
  35. }
  36. @Override
  37. public void drawContent(Graphics2D graphics) {
  38. PictureData data = getShape().getPictureData();
  39. if(data == null) return;
  40. Rectangle2D anchor = getAnchor(graphics, getShape());
  41. Insets insets = getShape().getClipping();
  42. try {
  43. ImageRenderer renderer = getImageRenderer(graphics, data.getContentType());
  44. renderer.loadImage(data.getData(), data.getContentType());
  45. renderer.drawImage(graphics, anchor, insets);
  46. } catch (IOException e) {
  47. LOG.log(POILogger.ERROR, "image can't be loaded/rendered.", e);
  48. }
  49. }
  50. /**
  51. * Returns an ImageRenderer for the PictureData
  52. *
  53. * @param graphics the graphics context
  54. * @return the image renderer
  55. */
  56. @SuppressWarnings({"WeakerAccess", "unchecked"})
  57. public static ImageRenderer getImageRenderer(Graphics2D graphics, String contentType) {
  58. final ImageRenderer renderer = (ImageRenderer)graphics.getRenderingHint(Drawable.IMAGE_RENDERER);
  59. if (renderer != null && renderer.canRender(contentType)) {
  60. return renderer;
  61. }
  62. // first try with our default image renderer
  63. final BitmapImageRenderer bir = new BitmapImageRenderer();
  64. if (bir.canRender(contentType)) {
  65. return bir;
  66. }
  67. // then iterate through the scratchpad renderers
  68. //
  69. // this could be nicely implemented via a j.u.ServiceLoader, but OSGi makes things complicated ...
  70. // https://blog.osgi.org/2013/02/javautilserviceloader-in-osgi.html
  71. // ... therefore falling back to classloading attempts
  72. ClassLoader cl = ImageRenderer.class.getClassLoader();
  73. for (String kr : KNOWN_RENDERER) {
  74. final ImageRenderer ir;
  75. try {
  76. ir = ((Class<? extends ImageRenderer>)cl.loadClass(kr)).newInstance();
  77. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
  78. // scratchpad was not on the path, ignore and continue
  79. LOG.log(POILogger.INFO, "Known image renderer '"+kr+" not found/loaded - include poi-scratchpad jar!", e);
  80. continue;
  81. }
  82. if (ir.canRender(contentType)) {
  83. return ir;
  84. }
  85. }
  86. LOG.log(POILogger.WARN, "No suiteable image renderer found for content-type '"+
  87. contentType+"' - include poi-scratchpad jar!");
  88. // falling back to BitmapImageRenderer, at least it gracefully handles invalid images
  89. return bir;
  90. }
  91. @Override
  92. protected PictureShape<?,?> getShape() {
  93. return (PictureShape<?,?>)shape;
  94. }
  95. /**
  96. * Resize this picture to the default size.
  97. *
  98. * For PNG and JPEG resizes the image to 100%,
  99. * for other types, if the size can't be determined it will be 200x200 pixels.
  100. */
  101. public void resize() {
  102. PictureShape<?,?> ps = getShape();
  103. Dimension dim = ps.getPictureData().getImageDimension();
  104. Rectangle2D origRect = ps.getAnchor();
  105. double x = origRect.getX();
  106. double y = origRect.getY();
  107. double w = dim.getWidth();
  108. double h = dim.getHeight();
  109. ps.setAnchor(new Rectangle2D.Double(x, y, w, h));
  110. }
  111. /**
  112. * Fit picture shape into the target rectangle, maintaining the aspect ratio
  113. * and repositioning within the target rectangle with a centered alignment.
  114. *
  115. * @param target The target rectangle
  116. */
  117. public void resize(Rectangle2D target) {
  118. resize(target, RectAlign.CENTER);
  119. }
  120. /**
  121. * Fit picture shape into the target rectangle, maintaining the aspect ratio
  122. * and repositioning within the target rectangle based on the specified
  123. * alignment (gravity).
  124. *
  125. * @param target The target rectangle
  126. * @param align
  127. * The alignment within the target rectangle when resizing.
  128. * A null value corresponds to RectAlign.CENTER
  129. */
  130. public void resize(Rectangle2D target, RectAlign align) {
  131. PictureShape<?,?> ps = getShape();
  132. Dimension dim = ps.getPictureData().getImageDimension();
  133. if (dim.width <= 0 || dim.height <= 0) {
  134. // nothing useful to be done for this case
  135. ps.setAnchor(target);
  136. return;
  137. }
  138. double w = target.getWidth();
  139. double h = target.getHeight();
  140. // scaling
  141. double sx = w / dim.width;
  142. double sy = h / dim.height;
  143. // position adjustments
  144. double dx = 0, dy = 0;
  145. if (sx > sy) {
  146. // use y-scaling for both, reposition x accordingly
  147. w = sy * dim.width;
  148. dx = target.getWidth() - w;
  149. } else if (sy > sx) {
  150. // use x-scaling for both, reposition y accordingly
  151. h = sx * dim.height;
  152. dy = target.getHeight() - h;
  153. } else {
  154. // uniform scaling, can use target values directly
  155. ps.setAnchor(target);
  156. return;
  157. }
  158. // the positioning
  159. double x = target.getX();
  160. double y = target.getY();
  161. switch (align) {
  162. case TOP: // X=balance, Y=ok
  163. x += dx/2;
  164. break;
  165. case TOP_RIGHT: // X=shift, Y=ok
  166. x += dx;
  167. break;
  168. case RIGHT: // X=shift, Y=balance
  169. x += dx;
  170. y += dy/2;
  171. break;
  172. case BOTTOM_RIGHT: // X=shift, Y=shift
  173. x += dx;
  174. y += dy;
  175. break;
  176. case BOTTOM: // X=balance, Y=shift
  177. x += dx/2;
  178. y += dy;
  179. break;
  180. case BOTTOM_LEFT: // X=ok, Y=shift
  181. y += dy;
  182. break;
  183. case LEFT: // X=ok, Y=balance
  184. y += dy/2;
  185. break;
  186. case TOP_LEFT: // X=ok, Y=ok
  187. /* no-op */
  188. break;
  189. default: // CENTER: X=balance, Y=balance
  190. x += dx/2;
  191. y += dy/2;
  192. break;
  193. }
  194. ps.setAnchor(new Rectangle2D.Double(x, y, w, h));
  195. }
  196. }