aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi/sl
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2019-12-08 23:29:50 +0000
committerAndreas Beeker <kiwiwings@apache.org>2019-12-08 23:29:50 +0000
commit0f29ae8e4dbf1a19093d08a4da15cdfe83948d8c (patch)
tree0636b89feaaa34a285d82aa9d501f7c2b8995e58 /src/java/org/apache/poi/sl
parent82156bc63cd8a9e6b58e97859c1b9b86d2b98bf2 (diff)
downloadpoi-0f29ae8e4dbf1a19093d08a4da15cdfe83948d8c.tar.gz
poi-0f29ae8e4dbf1a19093d08a4da15cdfe83948d8c.zip
Sonar fixes - type: bugs / severity: major
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871064 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/sl')
-rw-r--r--src/java/org/apache/poi/sl/draw/DrawFactory.java32
-rw-r--r--src/java/org/apache/poi/sl/draw/DrawShape.java94
2 files changed, 61 insertions, 65 deletions
diff --git a/src/java/org/apache/poi/sl/draw/DrawFactory.java b/src/java/org/apache/poi/sl/draw/DrawFactory.java
index 6a032d6c6e..6b63274455 100644
--- a/src/java/org/apache/poi/sl/draw/DrawFactory.java
+++ b/src/java/org/apache/poi/sl/draw/DrawFactory.java
@@ -47,17 +47,21 @@ public class DrawFactory {
* This is a fallback, for operations where usercode can't set a graphics context.
* Preferably use the rendering hint {@link Drawable#DRAW_FACTORY} to set the factory.
*
- * @param factory the custom factory
+ * @param factory the custom factory or {@code null} to reset/remove the default factory
*/
@SuppressWarnings("unused")
public static void setDefaultFactory(DrawFactory factory) {
- defaultFactory.set(factory);
+ if (factory == null) {
+ defaultFactory.remove();
+ } else {
+ defaultFactory.set(factory);
+ }
}
/**
* Returns the DrawFactory, preferably via a graphics instance.
* If graphics is null, the current thread local is checked or
- * if it is not set, a new factory is created.
+ * if it is not set, a new factory is created.
*
* @param graphics the current graphics context or null
* @return the draw factory
@@ -112,7 +116,7 @@ public class DrawFactory {
} else if (shape.getClass().isAnnotationPresent(DrawNotImplemented.class)) {
return new DrawNothing(shape);
}
-
+
throw new IllegalArgumentException("Unsupported shape type: "+shape.getClass());
}
@@ -139,11 +143,11 @@ public class DrawFactory {
public DrawConnectorShape getDrawable(ConnectorShape<?,?> shape) {
return new DrawConnectorShape(shape);
}
-
+
public DrawTableShape getDrawable(TableShape<?,?> shape) {
return new DrawTableShape(shape);
}
-
+
public DrawTextShape getDrawable(TextShape<?,?> shape) {
return new DrawTextShape(shape);
}
@@ -151,15 +155,15 @@ public class DrawFactory {
public DrawGroupShape getDrawable(GroupShape<?,?> shape) {
return new DrawGroupShape(shape);
}
-
+
public DrawPictureShape getDrawable(PictureShape<?,?> shape) {
return new DrawPictureShape(shape);
}
-
+
public DrawGraphicalFrame getDrawable(GraphicalFrame<?,?> shape) {
return new DrawGraphicalFrame(shape);
}
-
+
public DrawTextParagraph getDrawable(TextParagraph<?,?,?> paragraph) {
return new DrawTextParagraph(paragraph);
}
@@ -167,12 +171,12 @@ public class DrawFactory {
public DrawBackground getDrawable(Background<?,?> shape) {
return new DrawBackground(shape);
}
-
+
@SuppressWarnings("WeakerAccess")
public DrawTextFragment getTextFragment(TextLayout layout, AttributedString str) {
return new DrawTextFragment(layout, str);
}
-
+
public DrawPaint getPaint(PlaceableShape<?,?> shape) {
return new DrawPaint(shape);
}
@@ -183,7 +187,7 @@ public class DrawFactory {
*
* @param graphics the graphics context to draw to
* @param shape the shape
- * @param bounds the bounds within the graphics context to draw to
+ * @param bounds the bounds within the graphics context to draw to
*/
public void drawShape(Graphics2D graphics, Shape<?,?> shape, Rectangle2D bounds) {
Rectangle2D shapeBounds = shape.getAnchor();
@@ -202,7 +206,7 @@ public class DrawFactory {
tx.translate(-shapeBounds.getCenterX(), -shapeBounds.getCenterY());
}
graphics.setRenderingHint(Drawable.GROUP_TRANSFORM, tx);
-
+
Drawable d = getDrawable(shape);
d.applyTransform(graphics);
d.draw(graphics);
@@ -210,7 +214,7 @@ public class DrawFactory {
graphics.setRenderingHint(Drawable.GROUP_TRANSFORM, txg);
}
}
-
+
/**
* Return a FontManager, either registered beforehand or a default implementation
diff --git a/src/java/org/apache/poi/sl/draw/DrawShape.java b/src/java/org/apache/poi/sl/draw/DrawShape.java
index 67162ac4a8..28ac58b011 100644
--- a/src/java/org/apache/poi/sl/draw/DrawShape.java
+++ b/src/java/org/apache/poi/sl/draw/DrawShape.java
@@ -60,56 +60,52 @@ public class DrawShape implements Drawable {
return;
}
- final PlaceableShape<?,?> ps = (PlaceableShape<?,?>)shape;
- final boolean isHSLF = isHSLF(shape);
+ final Rectangle2D anchor = getAnchor(graphics, (PlaceableShape<?,?>)shape);
+ if (anchor == null) {
+ return;
+ }
- final Rectangle2D anchor = getAnchor(graphics, ps);
-
- char[] cmds = isHSLF ? new char[]{'h', 'v', 'r'} : new char[]{'r', 'h', 'v'};
- for (char ch : cmds) {
- switch (ch) {
- case 'h':
- //flip horizontal
- if (ps.getFlipHorizontal()) {
- graphics.translate(anchor.getX() + anchor.getWidth(), anchor.getY());
- graphics.scale(-1, 1);
- graphics.translate(-anchor.getX(), -anchor.getY());
- }
- break;
- case 'v':
- //flip vertical
- if (ps.getFlipVertical()) {
- graphics.translate(anchor.getX(), anchor.getY() + anchor.getHeight());
- graphics.scale(1, -1);
- graphics.translate(-anchor.getX(), -anchor.getY());
- }
- break;
- case 'r':
- // rotation
- double rotation = ps.getRotation();
- if (rotation != 0.) {
- // PowerPoint rotates shapes relative to the geometric center
- double centerX = anchor.getCenterX();
- double centerY = anchor.getCenterY();
-
-
- // transformation is applied reversed ...
- graphics.translate(centerX, centerY);
- graphics.rotate(Math.toRadians(rotation));
- graphics.translate(-centerX, -centerY);
- }
- break;
- default:
- throw new RuntimeException("unexpected transform code " + ch);
- }
+ if (isHSLF(shape)) {
+ flipHorizontal(graphics, anchor);
+ flipVertical(graphics, anchor);
+ rotate(graphics, anchor);
+ } else {
+ rotate(graphics, anchor);
+ flipHorizontal(graphics, anchor);
+ flipVertical(graphics, anchor);
}
}
- private static double safeScale(double dim1, double dim2) {
- if (dim1 == 0.) {
- return 1;
+ private void flipHorizontal(Graphics2D graphics, Rectangle2D anchor) {
+ assert(shape instanceof PlaceableShape && anchor != null);
+ if (((PlaceableShape<?,?>)shape).getFlipHorizontal()) {
+ graphics.translate(anchor.getX() + anchor.getWidth(), anchor.getY());
+ graphics.scale(-1, 1);
+ graphics.translate(-anchor.getX(), -anchor.getY());
}
- return (dim2 == 0.) ? 1 : dim1/dim2;
+ }
+
+ private void flipVertical(Graphics2D graphics, Rectangle2D anchor) {
+ assert(shape instanceof PlaceableShape && anchor != null);
+ if (((PlaceableShape<?,?>)shape).getFlipVertical()) {
+ graphics.translate(anchor.getX(), anchor.getY() + anchor.getHeight());
+ graphics.scale(1, -1);
+ graphics.translate(-anchor.getX(), -anchor.getY());
+ }
+ }
+
+ private void rotate(Graphics2D graphics, Rectangle2D anchor) {
+ assert(shape instanceof PlaceableShape && anchor != null);
+ double rotation = ((PlaceableShape<?,?>)shape).getRotation();
+ if (rotation != 0.) {
+ // PowerPoint rotates shapes relative to the geometric center
+ graphics.rotate(Math.toRadians(rotation), anchor.getCenterX(), anchor.getCenterY());
+ }
+
+ }
+
+ private static double safeScale(double dim1, double dim2) {
+ return (dim1 == 0. || dim2 == 0.) ? 1 : dim1/dim2;
}
@Override
@@ -160,15 +156,11 @@ public class DrawShape implements Drawable {
// this handling is only based on try and error ... not sure why h/xslf is handled differently.
if (!isHSLF) {
- txs2.translate(centerX, centerY);
- txs2.quadrantRotate(1);
- txs2.translate(-centerX, -centerY);
+ txs2.quadrantRotate(1, centerX, centerY);
txs2.concatenate(tx);
}
- txs2.translate(centerX, centerY);
- txs2.quadrantRotate(3);
- txs2.translate(-centerX, -centerY);
+ txs2.quadrantRotate(3, centerX, centerY);
if (isHSLF) {
txs2.concatenate(tx);