1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.sl.draw;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import org.apache.poi.sl.usermodel.Insets2D;
import org.apache.poi.sl.usermodel.PlaceableShape;
import org.apache.poi.sl.usermodel.ShapeContainer;
import org.apache.poi.sl.usermodel.TextParagraph;
import org.apache.poi.sl.usermodel.TextParagraph.BulletStyle;
import org.apache.poi.sl.usermodel.TextRun;
import org.apache.poi.sl.usermodel.TextShape;
import org.apache.poi.sl.usermodel.TextShape.TextDirection;
public class DrawTextShape extends DrawSimpleShape {
public DrawTextShape(TextShape<?,?> shape) {
super(shape);
}
@Override
public void drawContent(Graphics2D graphics) {
DrawFactory.getInstance(graphics).fixFonts(graphics);
TextShape<?,?> s = getShape();
Rectangle2D anchor = DrawShape.getAnchor(graphics, s);
Insets2D insets = s.getInsets();
double x = anchor.getX() + insets.left;
double y = anchor.getY();
// remember the initial transform
AffineTransform tx = graphics.getTransform();
// Transform of text in flipped shapes is special.
// At this point the flip and rotation transform is already applied
// (see DrawShape#applyTransform ), but we need to restore it to avoid painting "upside down".
// See Bugzilla 54210.
boolean vertFlip = s.getFlipVertical();
boolean horzFlip = s.getFlipHorizontal();
ShapeContainer<?,?> sc = s.getParent();
while (sc instanceof PlaceableShape) {
PlaceableShape<?,?> ps = (PlaceableShape<?,?>)sc;
vertFlip ^= ps.getFlipVertical();
horzFlip ^= ps.getFlipHorizontal();
sc = ps.getParent();
}
// Horizontal flipping applies only to shape outline and not to the text in the shape.
// Applying flip second time restores the original not-flipped transform
if (horzFlip ^ vertFlip) {
final double ax = anchor.getX();
final double ay = anchor.getY();
graphics.translate(ax + anchor.getWidth(), ay);
graphics.scale(-1, 1);
graphics.translate(-ax, -ay);
}
Double textRot = s.getTextRotation();
if (textRot != null && textRot != 0) {
final double cx = anchor.getCenterX();
final double cy = anchor.getCenterY();
graphics.translate(cx, cy);
graphics.rotate(Math.toRadians(textRot));
graphics.translate(-cx, -cy);
}
// first dry-run to calculate the total height of the text
double textHeight;
switch (s.getVerticalAlignment()){
default:
case TOP:
y += insets.top;
break;
case BOTTOM:
textHeight = getTextHeight(graphics);
y += anchor.getHeight() - textHeight - insets.bottom;
break;
case MIDDLE:
textHeight = getTextHeight(graphics);
double delta = anchor.getHeight() - textHeight - insets.top - insets.bottom;
y += insets.top + delta/2;
break;
}
TextDirection textDir = s.getTextDirection();
if (textDir == TextDirection.VERTICAL || textDir == TextDirection.VERTICAL_270) {
final double deg = (textDir == TextDirection.VERTICAL) ? 90 : 270;
final double cx = anchor.getCenterX();
final double cy = anchor.getCenterY();
graphics.translate(cx, cy);
graphics.rotate(Math.toRadians(deg));
graphics.translate(-cx, -cy);
// old top/left edge is now bottom/left or top/right - as we operate on the already
// rotated drawing context, both verticals can be moved in the same direction
final double w = anchor.getWidth();
final double h = anchor.getHeight();
final double dx = (w-h)/2d;
graphics.translate(dx,-dx);
}
drawParagraphs(graphics, x, y);
// restore the transform
graphics.setTransform(tx);
}
/**
* paint the paragraphs starting from top left (x,y)
*
* @return the vertical advance, i.e. the cumulative space occupied by the text
*/
public double drawParagraphs(Graphics2D graphics, double x, double y) {
DrawFactory fact = DrawFactory.getInstance(graphics);
double y0 = y;
//noinspection RedundantCast
@SuppressWarnings("cast")
Iterator<? extends TextParagraph<?,?,? extends TextRun>> paragraphs =
(Iterator<? extends TextParagraph<?,?,? extends TextRun>>) getShape().iterator();
boolean isFirstLine = true;
for (int autoNbrIdx=0; paragraphs.hasNext(); autoNbrIdx++){
TextParagraph<?,?,? extends TextRun> p = paragraphs.next();
DrawTextParagraph dp = fact.getDrawable(p);
BulletStyle bs = p.getBulletStyle();
if (bs == null || bs.getAutoNumberingScheme() == null) {
autoNbrIdx = -1;
} else {
Integer startAt = bs.getAutoNumberingStartAt();
if (startAt == null) startAt = 1;
// TODO: handle reset auto number indexes
if (startAt > autoNbrIdx) autoNbrIdx = startAt;
}
dp.setAutoNumberingIdx(autoNbrIdx);
dp.breakText(graphics);
if (!isFirstLine) {
// the amount of vertical white space before the paragraph
Double spaceBefore = p.getSpaceBefore();
if (spaceBefore == null) spaceBefore = 0d;
if(spaceBefore > 0) {
// positive value means percentage spacing of the height of the first line, e.g.
// the higher the first line, the bigger the space before the paragraph
y += spaceBefore*0.01*dp.getFirstLineHeight();
} else {
// negative value means the absolute spacing in points
y += -spaceBefore;
}
}
isFirstLine = false;
dp.setPosition(x, y);
dp.draw(graphics);
y += dp.getY();
if (paragraphs.hasNext()) {
Double spaceAfter = p.getSpaceAfter();
if (spaceAfter == null) spaceAfter = 0d;
if(spaceAfter > 0) {
// positive value means percentage spacing of the height of the last line, e.g.
// the higher the last line, the bigger the space after the paragraph
y += spaceAfter*0.01*dp.getLastLineHeight();
} else {
// negative value means the absolute spacing in points
y += -spaceAfter;
}
}
}
return y - y0;
}
/**
* Compute the cumulative height occupied by the text
*
* @return the height in points
*/
public double getTextHeight() {
return getTextHeight(null);
}
/**
* Compute the cumulative height occupied by the text
*
* @param oldGraphics the graphics context, which properties are to be copied, may be null
* @return the height in points
*/
protected double getTextHeight(Graphics2D oldGraphics) {
// dry-run in a 1x1 image and return the vertical advance
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
if (oldGraphics != null) {
graphics.addRenderingHints(oldGraphics.getRenderingHints());
graphics.setTransform(oldGraphics.getTransform());
}
DrawFactory.getInstance(graphics).fixFonts(graphics);
return drawParagraphs(graphics, 0, 0);
}
@Override
protected TextShape<?,?> getShape() {
return (TextShape<?,?>)shape;
}
}
|