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.

DrawTextShape.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.geom.AffineTransform;
  18. import java.awt.geom.Rectangle2D;
  19. import java.awt.image.BufferedImage;
  20. import java.util.Iterator;
  21. import org.apache.poi.sl.usermodel.Insets2D;
  22. import org.apache.poi.sl.usermodel.PlaceableShape;
  23. import org.apache.poi.sl.usermodel.ShapeContainer;
  24. import org.apache.poi.sl.usermodel.TextParagraph;
  25. import org.apache.poi.sl.usermodel.TextParagraph.BulletStyle;
  26. import org.apache.poi.sl.usermodel.TextRun;
  27. import org.apache.poi.sl.usermodel.TextShape;
  28. import org.apache.poi.sl.usermodel.TextShape.TextDirection;
  29. public class DrawTextShape extends DrawSimpleShape {
  30. public DrawTextShape(TextShape<?,?> shape) {
  31. super(shape);
  32. }
  33. @Override
  34. public void drawContent(Graphics2D graphics) {
  35. TextShape<?,?> s = getShape();
  36. Rectangle2D anchor = DrawShape.getAnchor(graphics, s);
  37. if(anchor == null) {
  38. return;
  39. }
  40. Insets2D insets = s.getInsets();
  41. double x = anchor.getX() + insets.left;
  42. double y = anchor.getY();
  43. // remember the initial transform
  44. AffineTransform tx = graphics.getTransform();
  45. // Transform of text in flipped shapes is special.
  46. // At this point the flip and rotation transform is already applied
  47. // (see DrawShape#applyTransform ), but we need to restore it to avoid painting "upside down".
  48. // See Bugzilla 54210.
  49. boolean vertFlip = s.getFlipVertical();
  50. boolean horzFlip = s.getFlipHorizontal();
  51. ShapeContainer<?,?> sc = s.getParent();
  52. while (sc instanceof PlaceableShape) {
  53. PlaceableShape<?,?> ps = (PlaceableShape<?,?>)sc;
  54. vertFlip ^= ps.getFlipVertical();
  55. horzFlip ^= ps.getFlipHorizontal();
  56. sc = ps.getParent();
  57. }
  58. // Horizontal flipping applies only to shape outline and not to the text in the shape.
  59. // Applying flip second time restores the original not-flipped transform
  60. if (horzFlip ^ vertFlip) {
  61. final double ax = anchor.getX();
  62. final double ay = anchor.getY();
  63. graphics.translate(ax + anchor.getWidth(), ay);
  64. graphics.scale(-1, 1);
  65. graphics.translate(-ax, -ay);
  66. }
  67. Double textRot = s.getTextRotation();
  68. if (textRot != null && textRot != 0) {
  69. final double cx = anchor.getCenterX();
  70. final double cy = anchor.getCenterY();
  71. graphics.translate(cx, cy);
  72. graphics.rotate(Math.toRadians(textRot));
  73. graphics.translate(-cx, -cy);
  74. }
  75. // first dry-run to calculate the total height of the text
  76. double textHeight;
  77. switch (s.getVerticalAlignment()){
  78. default:
  79. case TOP:
  80. y += insets.top;
  81. break;
  82. case BOTTOM:
  83. textHeight = getTextHeight(graphics);
  84. y += anchor.getHeight() - textHeight - insets.bottom;
  85. break;
  86. case MIDDLE:
  87. textHeight = getTextHeight(graphics);
  88. double delta = anchor.getHeight() - textHeight - insets.top - insets.bottom;
  89. y += insets.top + delta/2;
  90. break;
  91. }
  92. TextDirection textDir = s.getTextDirection();
  93. if (textDir == TextDirection.VERTICAL || textDir == TextDirection.VERTICAL_270) {
  94. final double deg = (textDir == TextDirection.VERTICAL) ? 90 : 270;
  95. final double cx = anchor.getCenterX();
  96. final double cy = anchor.getCenterY();
  97. graphics.translate(cx, cy);
  98. graphics.rotate(Math.toRadians(deg));
  99. graphics.translate(-cx, -cy);
  100. // old top/left edge is now bottom/left or top/right - as we operate on the already
  101. // rotated drawing context, both verticals can be moved in the same direction
  102. final double w = anchor.getWidth();
  103. final double h = anchor.getHeight();
  104. final double dx = (w-h)/2d;
  105. graphics.translate(dx,-dx);
  106. }
  107. drawParagraphs(graphics, x, y);
  108. // restore the transform
  109. graphics.setTransform(tx);
  110. }
  111. /**
  112. * paint the paragraphs starting from top left (x,y)
  113. *
  114. * @return the vertical advance, i.e. the cumulative space occupied by the text
  115. */
  116. public double drawParagraphs(Graphics2D graphics, double x, double y) {
  117. DrawFactory fact = DrawFactory.getInstance(graphics);
  118. double y0 = y;
  119. Iterator<? extends TextParagraph<?,?,? extends TextRun>> paragraphs = getShape().iterator();
  120. boolean isFirstLine = true;
  121. for (int autoNbrIdx=0; paragraphs.hasNext(); autoNbrIdx++){
  122. TextParagraph<?,?,? extends TextRun> p = paragraphs.next();
  123. DrawTextParagraph dp = fact.getDrawable(p);
  124. BulletStyle bs = p.getBulletStyle();
  125. if (bs == null || bs.getAutoNumberingScheme() == null) {
  126. autoNbrIdx = -1;
  127. } else {
  128. Integer startAt = bs.getAutoNumberingStartAt();
  129. if (startAt == null) startAt = 1;
  130. // TODO: handle reset auto number indexes
  131. if (startAt > autoNbrIdx) autoNbrIdx = startAt;
  132. }
  133. dp.setAutoNumberingIdx(autoNbrIdx);
  134. dp.breakText(graphics);
  135. if (isFirstLine) {
  136. y += dp.getFirstLineLeading();
  137. } else {
  138. // the amount of vertical white space before the paragraph
  139. Double spaceBefore = p.getSpaceBefore();
  140. if (spaceBefore == null) spaceBefore = 0d;
  141. if(spaceBefore > 0) {
  142. // positive value means percentage spacing of the height of the first line, e.g.
  143. // the higher the first line, the bigger the space before the paragraph
  144. y += spaceBefore*0.01*dp.getFirstLineHeight();
  145. } else {
  146. // negative value means the absolute spacing in points
  147. y += -spaceBefore;
  148. }
  149. }
  150. isFirstLine = false;
  151. dp.setPosition(x, y);
  152. dp.draw(graphics);
  153. y += dp.getY();
  154. if (paragraphs.hasNext()) {
  155. Double spaceAfter = p.getSpaceAfter();
  156. if (spaceAfter == null) spaceAfter = 0d;
  157. if(spaceAfter > 0) {
  158. // positive value means percentage spacing of the height of the last line, e.g.
  159. // the higher the last line, the bigger the space after the paragraph
  160. y += spaceAfter*0.01*dp.getLastLineHeight();
  161. } else {
  162. // negative value means the absolute spacing in points
  163. y += -spaceAfter;
  164. }
  165. }
  166. }
  167. return y - y0;
  168. }
  169. /**
  170. * Compute the cumulative height occupied by the text
  171. *
  172. * @return the height in points
  173. */
  174. public double getTextHeight() {
  175. return getTextHeight(null);
  176. }
  177. /**
  178. * Compute the cumulative height occupied by the text
  179. *
  180. * @param oldGraphics the graphics context, which properties are to be copied, may be null
  181. * @return the height in points
  182. */
  183. public double getTextHeight(Graphics2D oldGraphics) {
  184. // dry-run in a 1x1 image and return the vertical advance
  185. BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  186. Graphics2D graphics = img.createGraphics();
  187. if (oldGraphics != null) {
  188. graphics.addRenderingHints(oldGraphics.getRenderingHints());
  189. graphics.setTransform(oldGraphics.getTransform());
  190. }
  191. return drawParagraphs(graphics, 0, 0);
  192. }
  193. @Override
  194. protected TextShape<?,? extends TextParagraph<?,?,? extends TextRun>> getShape() {
  195. return (TextShape<?,? extends TextParagraph<?,?,? extends TextRun>>)shape;
  196. }
  197. }