View | Details | Raw Unified | Return to bug 53176
Collapse All | Expand All

(-)src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFShape.java (-3 / +34 lines)
Lines 141-151 Link Here
141
        double rotation = getRotation();
141
        double rotation = getRotation();
142
        if (rotation != 0.) {
142
        if (rotation != 0.) {
143
            // PowerPoint rotates shapes relative to the geometric center
143
            // PowerPoint rotates shapes relative to the geometric center
144
            double centerX = anchor.getX() + anchor.getWidth() / 2;
144
            double centerX = anchor.getCenterX();
145
            double centerY = anchor.getY() + anchor.getHeight() / 2;
145
            double centerY = anchor.getCenterY();
146
146
147
            // normalize rotation
148
            rotation = (360.+(rotation%360.))%360.;
149
            int quadrant = (((int)rotation+45)/90)%4;
150
            double scaleX = 1.0, scaleY = 1.0;
151
152
            // scale to bounding box (bug #53176)
153
            if (quadrant == 1 || quadrant == 3) {
154
                // In quadrant 1 and 3, which is basically a shape in a more or less portrait orientation (45°-135° and 225°-315°),
155
                // we need to first rotate the shape by a multiple of 90° and then resize the bounding box  
156
                // to its original bbox. After that we can rotate the shape to the exact rotation amount.
157
                // It's strange that you'll need to rotate the shape back and forth again, but you can
158
                // think of it, as if you paint the shape on a canvas. First you rotate the canvas, which might
159
                // be already (differently) scaled, so you can paint the shape in its default orientation
160
                // and later on, turn it around again to compare it with its original size ...
161
                AffineTransform txg = new AffineTransform(); // graphics coordinate space
162
                AffineTransform txs = new AffineTransform(tx); // shape coordinate space
163
                txg.translate(centerX, centerY);
164
                txg.rotate(Math.toRadians(quadrant*90));
165
                txg.translate(-centerX, -centerY);
166
                txs.translate(centerX, centerY);
167
                txs.rotate(Math.toRadians(-quadrant*90));
168
                txs.translate(-centerX, -centerY);
169
                txg.concatenate(txs);
170
                Rectangle2D anchor2 = txg.createTransformedShape(getAnchor()).getBounds2D();
171
                scaleX = anchor.getWidth() == 0. ? 1.0 : anchor.getWidth() / anchor2.getWidth();
172
                scaleY = anchor.getHeight() == 0. ? 1.0 : anchor.getHeight() / anchor2.getHeight();
173
            }
174
175
            // transformation is applied reversed ...
147
            graphics.translate(centerX, centerY);
176
            graphics.translate(centerX, centerY);
148
            graphics.rotate(Math.toRadians(rotation));
177
            graphics.rotate(Math.toRadians(rotation-(double)(quadrant*90)));
178
            graphics.scale(scaleX, scaleY);
179
            graphics.rotate(Math.toRadians(quadrant*90));
149
            graphics.translate(-centerX, -centerY);
180
            graphics.translate(-centerX, -centerY);
150
        }
181
        }
151
182

Return to bug 53176