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.

DrawFactory.java 8.5KB

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.Graphics2D;
  17. import java.awt.font.TextLayout;
  18. import java.awt.geom.AffineTransform;
  19. import java.awt.geom.Rectangle2D;
  20. import java.text.AttributedString;
  21. import org.apache.poi.sl.usermodel.Background;
  22. import org.apache.poi.sl.usermodel.ConnectorShape;
  23. import org.apache.poi.sl.usermodel.FreeformShape;
  24. import org.apache.poi.sl.usermodel.GraphicalFrame;
  25. import org.apache.poi.sl.usermodel.GroupShape;
  26. import org.apache.poi.sl.usermodel.MasterSheet;
  27. import org.apache.poi.sl.usermodel.PictureShape;
  28. import org.apache.poi.sl.usermodel.PlaceableShape;
  29. import org.apache.poi.sl.usermodel.Shape;
  30. import org.apache.poi.sl.usermodel.Sheet;
  31. import org.apache.poi.sl.usermodel.Slide;
  32. import org.apache.poi.sl.usermodel.TableShape;
  33. import org.apache.poi.sl.usermodel.TextBox;
  34. import org.apache.poi.sl.usermodel.TextParagraph;
  35. import org.apache.poi.sl.usermodel.TextShape;
  36. public class DrawFactory {
  37. private static final ThreadLocal<DrawFactory> defaultFactory = new ThreadLocal<>();
  38. /**
  39. * Set a custom draw factory for the current thread.
  40. * This is a fallback, for operations where usercode can't set a graphics context.
  41. * Preferably use the rendering hint {@link Drawable#DRAW_FACTORY} to set the factory.
  42. *
  43. * @param factory the custom factory
  44. */
  45. @SuppressWarnings("unused")
  46. public static void setDefaultFactory(DrawFactory factory) {
  47. defaultFactory.set(factory);
  48. }
  49. /**
  50. * Returns the DrawFactory, preferably via a graphics instance.
  51. * If graphics is null, the current thread local is checked or
  52. * if it is not set, a new factory is created.
  53. *
  54. * @param graphics the current graphics context or null
  55. * @return the draw factory
  56. */
  57. public static DrawFactory getInstance(Graphics2D graphics) {
  58. // first try to find the factory over the rendering hint
  59. DrawFactory factory = null;
  60. boolean isHint = false;
  61. if (graphics != null) {
  62. factory = (DrawFactory)graphics.getRenderingHint(Drawable.DRAW_FACTORY);
  63. isHint = (factory != null);
  64. }
  65. // secondly try the thread local default
  66. if (factory == null) {
  67. factory = defaultFactory.get();
  68. }
  69. // and at last, use the default factory
  70. if (factory == null) {
  71. factory = new DrawFactory();
  72. }
  73. if (graphics != null && !isHint) {
  74. graphics.setRenderingHint(Drawable.DRAW_FACTORY, factory);
  75. }
  76. return factory;
  77. }
  78. public Drawable getDrawable(Shape<?,?> shape) {
  79. if (shape instanceof TextBox) {
  80. return getDrawable((TextBox<?,?>)shape);
  81. } else if (shape instanceof FreeformShape) {
  82. return getDrawable((FreeformShape<?,?>)shape);
  83. } else if (shape instanceof TextShape) {
  84. return getDrawable((TextShape<?,?>)shape);
  85. } else if (shape instanceof TableShape) {
  86. return getDrawable((TableShape<?,?>)shape);
  87. } else if (shape instanceof GroupShape) {
  88. return getDrawable((GroupShape<?,?>)shape);
  89. } else if (shape instanceof PictureShape) {
  90. return getDrawable((PictureShape<?,?>)shape);
  91. } else if (shape instanceof GraphicalFrame) {
  92. return getDrawable((GraphicalFrame<?,?>)shape);
  93. } else if (shape instanceof Background) {
  94. return getDrawable((Background<?,?>)shape);
  95. } else if (shape instanceof ConnectorShape) {
  96. return getDrawable((ConnectorShape<?,?>)shape);
  97. } else if (shape instanceof Slide) {
  98. return getDrawable((Slide<?,?>)shape);
  99. } else if (shape instanceof MasterSheet) {
  100. return getDrawable((MasterSheet<?,?>)shape);
  101. } else if (shape instanceof Sheet) {
  102. return getDrawable((Sheet<?,?>)shape);
  103. } else if (shape.getClass().isAnnotationPresent(DrawNotImplemented.class)) {
  104. return new DrawNothing(shape);
  105. }
  106. throw new IllegalArgumentException("Unsupported shape type: "+shape.getClass());
  107. }
  108. public DrawSlide getDrawable(Slide<?,?> sheet) {
  109. return new DrawSlide(sheet);
  110. }
  111. public DrawSheet getDrawable(Sheet<?,?> sheet) {
  112. return new DrawSheet(sheet);
  113. }
  114. public DrawMasterSheet getDrawable(MasterSheet<?,?> sheet) {
  115. return new DrawMasterSheet(sheet);
  116. }
  117. public DrawTextBox getDrawable(TextBox<?,?> shape) {
  118. return new DrawTextBox(shape);
  119. }
  120. public DrawFreeformShape getDrawable(FreeformShape<?,?> shape) {
  121. return new DrawFreeformShape(shape);
  122. }
  123. public DrawConnectorShape getDrawable(ConnectorShape<?,?> shape) {
  124. return new DrawConnectorShape(shape);
  125. }
  126. public DrawTableShape getDrawable(TableShape<?,?> shape) {
  127. return new DrawTableShape(shape);
  128. }
  129. public DrawTextShape getDrawable(TextShape<?,?> shape) {
  130. return new DrawTextShape(shape);
  131. }
  132. public DrawGroupShape getDrawable(GroupShape<?,?> shape) {
  133. return new DrawGroupShape(shape);
  134. }
  135. public DrawPictureShape getDrawable(PictureShape<?,?> shape) {
  136. return new DrawPictureShape(shape);
  137. }
  138. public DrawGraphicalFrame getDrawable(GraphicalFrame<?,?> shape) {
  139. return new DrawGraphicalFrame(shape);
  140. }
  141. public DrawTextParagraph getDrawable(TextParagraph<?,?,?> paragraph) {
  142. return new DrawTextParagraph(paragraph);
  143. }
  144. public DrawBackground getDrawable(Background<?,?> shape) {
  145. return new DrawBackground(shape);
  146. }
  147. @SuppressWarnings("WeakerAccess")
  148. public DrawTextFragment getTextFragment(TextLayout layout, AttributedString str) {
  149. return new DrawTextFragment(layout, str);
  150. }
  151. public DrawPaint getPaint(PlaceableShape<?,?> shape) {
  152. return new DrawPaint(shape);
  153. }
  154. /**
  155. * Convenience method for drawing single shapes.
  156. * For drawing whole slides, use {@link Slide#draw(Graphics2D)}
  157. *
  158. * @param graphics the graphics context to draw to
  159. * @param shape the shape
  160. * @param bounds the bounds within the graphics context to draw to
  161. */
  162. public void drawShape(Graphics2D graphics, Shape<?,?> shape, Rectangle2D bounds) {
  163. Rectangle2D shapeBounds = shape.getAnchor();
  164. if (shapeBounds.isEmpty() || (bounds != null && bounds.isEmpty())) {
  165. return;
  166. }
  167. AffineTransform txg = (AffineTransform)graphics.getRenderingHint(Drawable.GROUP_TRANSFORM);
  168. AffineTransform tx = new AffineTransform();
  169. try {
  170. if (bounds != null) {
  171. double scaleX = bounds.getWidth()/shapeBounds.getWidth();
  172. double scaleY = bounds.getHeight()/shapeBounds.getHeight();
  173. tx.translate(bounds.getCenterX(), bounds.getCenterY());
  174. tx.scale(scaleX, scaleY);
  175. tx.translate(-shapeBounds.getCenterX(), -shapeBounds.getCenterY());
  176. }
  177. graphics.setRenderingHint(Drawable.GROUP_TRANSFORM, tx);
  178. Drawable d = getDrawable(shape);
  179. d.applyTransform(graphics);
  180. d.draw(graphics);
  181. } finally {
  182. graphics.setRenderingHint(Drawable.GROUP_TRANSFORM, txg);
  183. }
  184. }
  185. /**
  186. * Return a FontManager, either registered beforehand or a default implementation
  187. *
  188. * @param graphics the graphics context holding potentially a font manager
  189. * @return the font manager
  190. */
  191. public DrawFontManager getFontManager(Graphics2D graphics) {
  192. DrawFontManager fontHandler = (DrawFontManager)graphics.getRenderingHint(Drawable.FONT_HANDLER);
  193. return (fontHandler != null) ? fontHandler : new DrawFontManagerDefault();
  194. }
  195. }