選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DrawFactory.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 static org.apache.poi.sl.draw.Drawable.DRAW_FACTORY;
  17. import java.awt.Graphics2D;
  18. import java.awt.font.TextLayout;
  19. import java.text.AttributedString;
  20. import org.apache.poi.sl.usermodel.*;
  21. public class DrawFactory {
  22. protected static ThreadLocal<DrawFactory> defaultFactory = new ThreadLocal<DrawFactory>();
  23. /**
  24. * Set a custom draw factory for the current thread.
  25. * This is a fallback, for operations where usercode can't set a graphics context.
  26. * Preferably use the rendering hint {@link Drawable#DRAW_FACTORY} to set the factory.
  27. *
  28. * @param factory
  29. */
  30. public static void setDefaultFactory(DrawFactory factory) {
  31. defaultFactory.set(factory);
  32. }
  33. public static DrawFactory getInstance(Graphics2D graphics) {
  34. // first try to find the factory over the rendering hint
  35. DrawFactory factory = null;
  36. boolean isHint = false;
  37. if (graphics != null) {
  38. factory = (DrawFactory)graphics.getRenderingHint(DRAW_FACTORY);
  39. isHint = (factory != null);
  40. }
  41. // secondly try the thread local default
  42. if (factory == null) {
  43. factory = defaultFactory.get();
  44. }
  45. // and at last, use the default factory
  46. if (factory == null) {
  47. factory = new DrawFactory();
  48. }
  49. if (graphics != null && !isHint) {
  50. graphics.setRenderingHint(DRAW_FACTORY, factory);
  51. }
  52. return factory;
  53. }
  54. @SuppressWarnings("unchecked")
  55. public Drawable getDrawable(Shape shape) {
  56. if (shape instanceof TextBox) {
  57. return getDrawable((TextBox<? extends TextParagraph<? extends TextRun>>)shape);
  58. } else if (shape instanceof FreeformShape) {
  59. return getDrawable((FreeformShape<? extends TextParagraph<? extends TextRun>>)shape);
  60. } else if (shape instanceof TextShape) {
  61. return getDrawable((TextShape<? extends TextParagraph<? extends TextRun>>)shape);
  62. } else if (shape instanceof GroupShape) {
  63. return getDrawable((GroupShape<? extends Shape>)shape);
  64. } else if (shape instanceof PictureShape) {
  65. return getDrawable((PictureShape)shape);
  66. } else if (shape instanceof Background) {
  67. return getDrawable((Background)shape);
  68. } else if (shape instanceof Slide) {
  69. return getDrawable((Slide<? extends Shape, ? extends SlideShow, ? extends Notes<?,?>>)shape);
  70. } else if (shape instanceof MasterSheet) {
  71. return getDrawable((MasterSheet<? extends Shape, ? extends SlideShow>)shape);
  72. } else if (shape instanceof Sheet) {
  73. return getDrawable((Sheet<? extends Shape, ? extends SlideShow>)shape);
  74. }
  75. throw new IllegalArgumentException("Unsupported shape type: "+shape.getClass());
  76. }
  77. public <T extends Slide<? extends Shape, ? extends SlideShow, ? extends Notes<?,?>>> DrawSlide<T> getDrawable(T sheet) {
  78. return new DrawSlide<T>(sheet);
  79. }
  80. public <T extends Sheet<? extends Shape, ? extends SlideShow>> DrawSheet<T> getDrawable(T sheet) {
  81. return new DrawSheet<T>(sheet);
  82. }
  83. public <T extends MasterSheet<? extends Shape, ? extends SlideShow>> DrawMasterSheet<T> getDrawable(T sheet) {
  84. return new DrawMasterSheet<T>(sheet);
  85. }
  86. public <T extends TextBox<? extends TextParagraph<?>>> DrawTextBox<T> getDrawable(T shape) {
  87. return new DrawTextBox<T>(shape);
  88. }
  89. public <T extends FreeformShape<? extends TextParagraph<? extends TextRun>>> DrawFreeformShape<T> getDrawable(T shape) {
  90. return new DrawFreeformShape<T>(shape);
  91. }
  92. public <T extends TextShape<? extends TextParagraph<? extends TextRun>>> DrawTextShape<T> getDrawable(T shape) {
  93. return new DrawTextShape<T>(shape);
  94. }
  95. public <T extends GroupShape<? extends Shape>> DrawGroupShape<T> getDrawable(T shape) {
  96. return new DrawGroupShape<T>(shape);
  97. }
  98. public <T extends PictureShape> DrawPictureShape<T> getDrawable(T shape) {
  99. return new DrawPictureShape<T>(shape);
  100. }
  101. public <T extends TextRun> DrawTextParagraph<T> getDrawable(TextParagraph<T> paragraph) {
  102. return new DrawTextParagraph<T>(paragraph);
  103. }
  104. public <T extends Background> DrawBackground<T> getDrawable(T shape) {
  105. return new DrawBackground<T>(shape);
  106. }
  107. public DrawTextFragment getTextFragment(TextLayout layout, AttributedString str) {
  108. return new DrawTextFragment(layout, str);
  109. }
  110. public DrawPaint getPaint(PlaceableShape shape) {
  111. return new DrawPaint(shape);
  112. }
  113. }