Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DrawTextShape.java 9.0KB

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