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

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