2015-07-09 22:46:29 +00:00
|
|
|
/* ====================================================================
|
|
|
|
|
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.
|
|
|
|
|
==================================================================== */
|
|
|
|
|
|
2015-02-21 10:56:03 +00:00
|
|
|
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;
|
2015-07-09 22:46:29 +00:00
|
|
|
import java.util.*;
|
2015-02-21 10:56:03 +00:00
|
|
|
|
|
|
|
|
import org.apache.poi.sl.usermodel.*;
|
2015-07-08 00:09:34 +00:00
|
|
|
import org.apache.poi.sl.usermodel.TextParagraph.BulletStyle;
|
2015-07-09 22:46:29 +00:00
|
|
|
import org.apache.poi.util.JvmBugs;
|
2015-02-21 10:56:03 +00:00
|
|
|
|
2015-08-24 23:15:14 +00:00
|
|
|
public class DrawTextShape extends DrawSimpleShape {
|
2015-02-21 10:56:03 +00:00
|
|
|
|
2015-08-24 23:15:14 +00:00
|
|
|
public DrawTextShape(TextShape<?,?> shape) {
|
2015-02-21 10:56:03 +00:00
|
|
|
super(shape);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void drawContent(Graphics2D graphics) {
|
2015-07-24 21:47:55 +00:00
|
|
|
fixFonts(graphics);
|
|
|
|
|
|
2015-11-14 02:44:07 +00:00
|
|
|
TextShape<?,?> s = getShape();
|
|
|
|
|
|
|
|
|
|
Rectangle2D anchor = DrawShape.getAnchor(graphics, s);
|
|
|
|
|
Insets2D insets = s.getInsets();
|
2015-02-21 10:56:03 +00:00
|
|
|
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
|
2015-03-19 23:44:23 +00:00
|
|
|
// (see DrawShape#applyTransform ), but we need to restore it to avoid painting "upside down".
|
2015-02-21 10:56:03 +00:00
|
|
|
// See Bugzilla 54210.
|
|
|
|
|
|
2015-11-14 02:44:07 +00:00
|
|
|
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();
|
2016-02-15 08:33:12 +00:00
|
|
|
}
|
2015-11-14 02:44:07 +00:00
|
|
|
|
2015-02-21 10:56:03 +00:00
|
|
|
// 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
|
2015-11-14 02:44:07 +00:00
|
|
|
if (horzFlip ^ vertFlip) {
|
2015-02-21 10:56:03 +00:00
|
|
|
graphics.translate(anchor.getX() + anchor.getWidth(), anchor.getY());
|
|
|
|
|
graphics.scale(-1, 1);
|
2015-11-14 02:44:07 +00:00
|
|
|
graphics.translate(-anchor.getX(), -anchor.getY());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Double textRot = s.getTextRotation();
|
|
|
|
|
if (textRot != null) {
|
|
|
|
|
graphics.translate(anchor.getCenterX(), anchor.getCenterY());
|
|
|
|
|
graphics.rotate(Math.toRadians(textRot));
|
|
|
|
|
graphics.translate(-anchor.getCenterX(), -anchor.getCenterY());
|
2015-02-21 10:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// first dry-run to calculate the total height of the text
|
2015-11-14 02:44:07 +00:00
|
|
|
double textHeight = s.getTextHeight();
|
2015-02-21 10:56:03 +00:00
|
|
|
|
2015-11-14 02:44:07 +00:00
|
|
|
switch (s.getVerticalAlignment()){
|
|
|
|
|
default:
|
2015-02-21 10:56:03 +00:00
|
|
|
case TOP:
|
|
|
|
|
y += insets.top;
|
|
|
|
|
break;
|
|
|
|
|
case BOTTOM:
|
|
|
|
|
y += anchor.getHeight() - textHeight - insets.bottom;
|
|
|
|
|
break;
|
|
|
|
|
case MIDDLE:
|
|
|
|
|
double delta = anchor.getHeight() - textHeight - insets.top - insets.bottom;
|
|
|
|
|
y += insets.top + delta/2;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2016-02-15 08:33:12 +00:00
|
|
|
//noinspection RedundantCast
|
|
|
|
|
Iterator<? extends TextParagraph<?,?,? extends TextRun>> paragraphs = (Iterator<? extends TextParagraph<?, ?, ? extends TextRun>>) getShape().iterator();
|
|
|
|
|
|
2015-02-21 10:56:03 +00:00
|
|
|
boolean isFirstLine = true;
|
2015-07-08 00:09:34 +00:00
|
|
|
for (int autoNbrIdx=0; paragraphs.hasNext(); autoNbrIdx++){
|
2015-08-24 23:15:14 +00:00
|
|
|
TextParagraph<?,?,? extends TextRun> p = paragraphs.next();
|
|
|
|
|
DrawTextParagraph dp = fact.getDrawable(p);
|
2015-07-08 00:09:34 +00:00
|
|
|
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);
|
2015-02-21 10:56:03 +00:00
|
|
|
dp.breakText(graphics);
|
|
|
|
|
|
|
|
|
|
if (!isFirstLine) {
|
|
|
|
|
// the amount of vertical white space before the paragraph
|
2015-06-17 22:21:13 +00:00
|
|
|
Double spaceBefore = p.getSpaceBefore();
|
|
|
|
|
if (spaceBefore == null) spaceBefore = 0d;
|
2015-02-21 10:56:03 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-03 23:42:42 +00:00
|
|
|
isFirstLine = false;
|
2015-02-21 10:56:03 +00:00
|
|
|
|
|
|
|
|
dp.setPosition(x, y);
|
|
|
|
|
dp.draw(graphics);
|
|
|
|
|
y += dp.getY();
|
|
|
|
|
|
|
|
|
|
if (paragraphs.hasNext()) {
|
2015-06-17 22:21:13 +00:00
|
|
|
Double spaceAfter = p.getSpaceAfter();
|
|
|
|
|
if (spaceAfter == null) spaceAfter = 0d;
|
2015-02-21 10:56:03 +00:00
|
|
|
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
|
|
|
|
|
*/
|
2015-03-07 23:35:40 +00:00
|
|
|
public double getTextHeight(){
|
2015-02-21 10:56:03 +00:00
|
|
|
// 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();
|
2015-07-09 22:46:29 +00:00
|
|
|
fixFonts(graphics);
|
2015-02-21 10:56:03 +00:00
|
|
|
return drawParagraphs(graphics, 0, 0);
|
|
|
|
|
}
|
2015-07-09 22:46:29 +00:00
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private static void fixFonts(Graphics2D graphics) {
|
|
|
|
|
if (!JvmBugs.hasLineBreakMeasurerBug()) return;
|
|
|
|
|
Map<String,String> fontMap = (Map<String,String>)graphics.getRenderingHint(Drawable.FONT_MAP);
|
2015-07-24 21:47:55 +00:00
|
|
|
if (fontMap == null) {
|
|
|
|
|
fontMap = new HashMap<String,String>();
|
|
|
|
|
graphics.setRenderingHint(Drawable.FONT_MAP, fontMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!fontMap.containsKey("Calibri")) fontMap.put("Calibri", "Lucida Sans");
|
|
|
|
|
if (!fontMap.containsKey("Cambria")) fontMap.put("Cambria", "Lucida Bright");
|
2015-07-09 22:46:29 +00:00
|
|
|
}
|
2015-08-24 23:15:14 +00:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected TextShape<?,?> getShape() {
|
|
|
|
|
return (TextShape<?,?>)shape;
|
|
|
|
|
}
|
2015-02-21 10:56:03 +00:00
|
|
|
}
|