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.8KB

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