aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFAutoShape.java
blob: 20417dd4646d9675f17c53deb7d9b4670b4143ae (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* ====================================================================
   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.hslf.usermodel;

import static org.apache.poi.hslf.usermodel.HSLFFreeformShape.ShapePath.CURVES_CLOSED;
import static org.apache.poi.hslf.usermodel.HSLFFreeformShape.ShapePath.LINES_CLOSED;

import java.awt.geom.Arc2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;

import org.apache.logging.log4j.Logger;
import org.apache.poi.logging.PoiLogManager;
import org.apache.poi.ddf.AbstractEscherOptRecord;
import org.apache.poi.ddf.EscherArrayProperty;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherPropertyTypes;
import org.apache.poi.ddf.EscherSimpleProperty;
import org.apache.poi.sl.draw.geom.AdjustPoint;
import org.apache.poi.sl.draw.geom.ArcToCommand;
import org.apache.poi.sl.draw.geom.ClosePathCommand;
import org.apache.poi.sl.draw.geom.CurveToCommand;
import org.apache.poi.sl.draw.geom.CustomGeometry;
import org.apache.poi.sl.draw.geom.LineToCommand;
import org.apache.poi.sl.draw.geom.MoveToCommand;
import org.apache.poi.sl.draw.geom.Path;
import org.apache.poi.sl.usermodel.AutoShape;
import org.apache.poi.sl.usermodel.ShapeContainer;
import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.util.BitField;
import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.LittleEndian;

/**
 * Represents an AutoShape.<p>
 *
 * AutoShapes are drawing objects with a particular shape that may be customized through smart resizing and adjustments.
 * See {@link ShapeTypes}
 */
public class HSLFAutoShape extends HSLFTextShape implements AutoShape<HSLFShape,HSLFTextParagraph> {
    private static final Logger LOG = PoiLogManager.getLogger(HSLFAutoShape.class);

    static final byte[] SEGMENTINFO_MOVETO   = new byte[]{0x00, 0x40};
    static final byte[] SEGMENTINFO_LINETO   = new byte[]{0x00, (byte)0xAC};
    static final byte[] SEGMENTINFO_ESCAPE   = new byte[]{0x01, 0x00};
    static final byte[] SEGMENTINFO_ESCAPE2  = new byte[]{0x01, 0x20};
    static final byte[] SEGMENTINFO_CUBICTO  = new byte[]{0x00, (byte)0xAD};
    // OpenOffice inserts 0xB3 instead of 0xAD.
    // protected static final byte[] SEGMENTINFO_CUBICTO2 = new byte[]{0x00, (byte)0xB3};
    static final byte[] SEGMENTINFO_CLOSE    = new byte[]{0x01, (byte)0x60};
    static final byte[] SEGMENTINFO_END      = new byte[]{0x00, (byte)0x80};

    private static final BitField PATH_INFO = BitFieldFactory.getInstance(0xE000);
    private static final BitField ESCAPE_INFO = BitFieldFactory.getInstance(0x1F00);

    enum PathInfo {
        lineTo(0),curveTo(1),moveTo(2),close(3),end(4),escape(5),clientEscape(6);
        private final int flag;
        PathInfo(int flag) {
            this.flag = flag;
        }
        public int getFlag() {
            return flag;
        }
        static PathInfo valueOf(int flag) {
            for (PathInfo v : values()) {
                if (v.flag == flag) {
                    return v;
                }
            }
            return null;
        }
    }

    enum EscapeInfo {
        EXTENSION(0x0000),
        ANGLE_ELLIPSE_TO(0x0001),
        ANGLE_ELLIPSE(0x0002),
        ARC_TO(0x0003),
        ARC(0x0004),
        CLOCKWISE_ARC_TO(0x0005),
        CLOCKWISE_ARC(0x0006),
        ELLIPTICAL_QUADRANT_X(0x0007),
        ELLIPTICAL_QUADRANT_Y(0x0008),
        QUADRATIC_BEZIER(0x0009),
        NO_FILL(0X000A),
        NO_LINE(0X000B),
        AUTO_LINE(0X000C),
        AUTO_CURVE(0X000D),
        CORNER_LINE(0X000E),
        CORNER_CURVE(0X000F),
        SMOOTH_LINE(0X0010),
        SMOOTH_CURVE(0X0011),
        SYMMETRIC_LINE(0X0012),
        SYMMETRIC_CURVE(0X0013),
        FREEFORM(0X0014),
        FILL_COLOR(0X0015),
        LINE_COLOR(0X0016);

        private final int flag;
        EscapeInfo(int flag) {
            this.flag = flag;
        }
        public int getFlag() {
            return flag;
        }
        static EscapeInfo valueOf(int flag) {
            for (EscapeInfo v : values()) {
                if (v.flag == flag) {
                    return v;
                }
            }
            return null;
        }
    }

    protected HSLFAutoShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
        super(escherRecord, parent);
    }

    public HSLFAutoShape(ShapeType type, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
        super(null, parent);
        createSpContainer(type, parent instanceof HSLFGroupShape);
    }

    public HSLFAutoShape(ShapeType type){
        this(type, null);
    }

    protected EscherContainerRecord createSpContainer(ShapeType shapeType, boolean isChild){
        EscherContainerRecord ecr = super.createSpContainer(isChild);

        setShapeType(shapeType);

        //set default properties for an autoshape
        setEscherProperty(EscherPropertyTypes.PROTECTION__LOCKAGAINSTGROUPING, 0x40000);
        setEscherProperty(EscherPropertyTypes.FILL__FILLCOLOR, 0x8000004);
        setEscherProperty(EscherPropertyTypes.FILL__FILLCOLOR, 0x8000004);
        setEscherProperty(EscherPropertyTypes.FILL__FILLBACKCOLOR, 0x8000000);
        setEscherProperty(EscherPropertyTypes.FILL__NOFILLHITTEST, 0x100010);
        setEscherProperty(EscherPropertyTypes.LINESTYLE__COLOR, 0x8000001);
        setEscherProperty(EscherPropertyTypes.LINESTYLE__NOLINEDRAWDASH, 0x80008);
        setEscherProperty(EscherPropertyTypes.SHADOWSTYLE__COLOR, 0x8000002);

        return ecr;
    }

    @Override
    protected void setDefaultTextProperties(HSLFTextParagraph _txtrun){
        setVerticalAlignment(VerticalAlignment.MIDDLE);
        setHorizontalCentered(true);
        setWordWrap(false);
    }

    /**
     * Gets adjust value which controls smart resizing of the auto-shape.<p>
     *
     * The adjustment values are given in shape coordinates:
     * the origin is at the top-left, positive-x is to the right, positive-y is down.
     * The region from (0,0) to (S,S) maps to the geometry box of the shape (S=21600 is a constant).
     *
     * @param idx the adjust index in the [0, 9] range
     * @return the adjustment value
     */
    public int getAdjustmentValue(int idx){
        if(idx < 0 || idx > 9) throw new IllegalArgumentException("The index of an adjustment value must be in the [0, 9] range");
        return getEscherProperty(ADJUST_VALUES[idx]);
    }

    /**
     * Sets adjust value which controls smart resizing of the auto-shape.<p>
     *
     * The adjustment values are given in shape coordinates:
     * the origin is at the top-left, positive-x is to the right, positive-y is down.
     * The region from (0,0) to (S,S) maps to the geometry box of the shape (S=21600 is a constant).
     *
     * @param idx the adjust index in the [0, 9] range
     * @param val the adjustment value
     */
    public void setAdjustmentValue(int idx, int val){
        if(idx < 0 || idx > 9) throw new IllegalArgumentException("The index of an adjustment value must be in the [0, 9] range");
        setEscherProperty(ADJUST_VALUES[idx], val);
    }

    @Override
    public CustomGeometry getGeometry() {
        return getGeometry(new Path2D.Double());
    }

    CustomGeometry getGeometry(Path2D path2D) {
        final CustomGeometry cusGeo = new CustomGeometry();

        final AbstractEscherOptRecord opt = getEscherOptRecord();

        EscherArrayProperty verticesProp = getEscherProperty(opt, EscherPropertyTypes.GEOMETRY__VERTICES);
        EscherArrayProperty segmentsProp = getEscherProperty(opt, EscherPropertyTypes.GEOMETRY__SEGMENTINFO);

        // return empty path if either GEOMETRY__VERTICES or GEOMETRY__SEGMENTINFO is missing, see Bugzilla 54188

        //sanity check
        if(verticesProp == null) {
            LOG.atWarn().log("Freeform is missing GEOMETRY__VERTICES ");
            return super.getGeometry();
        }
        if(segmentsProp == null) {
            LOG.atWarn().log("Freeform is missing GEOMETRY__SEGMENTINFO ");
            return super.getGeometry();
        }

        final Iterator<byte[]> vertIter = verticesProp.iterator();
        final Iterator<byte[]> segIter = segmentsProp.iterator();
        final int[] xyPoints = new int[2];
        boolean isClosed = false;

        final Path path = new Path();
        cusGeo.addPath(path);

        while (segIter.hasNext()) {
            byte[] segElem = segIter.next();
            PathInfo pi = getPathInfo(segElem);
            if (pi == null) {
                continue;
            }
            switch (pi) {
                case escape:
                    handleEscapeInfo(path, path2D, segElem, vertIter);
                    break;
                case moveTo:
                    handleMoveTo(vertIter, xyPoints, path, path2D);
                    break;
                case lineTo:
                    handleLineTo(vertIter, xyPoints, path, path2D);
                    break;
                case curveTo:
                    handleCurveTo(vertIter, xyPoints, path, path2D);
                    break;
                case close:
                    if (path2D.getCurrentPoint() != null) {
                        path.addCommand(new ClosePathCommand());
                        path2D.closePath();
                    }
                    isClosed = true;
                    break;
                default:
                    break;
            }
        }

        if (!isClosed) {
            handleClosedShape(opt, path, path2D);
        }

        final Rectangle2D bounds = getBounds(opt, path2D);

        path.setW((int)Math.rint(bounds.getWidth()));
        path.setH((int)Math.rint(bounds.getHeight()));

        return cusGeo;
    }

    private static Rectangle2D getBounds(AbstractEscherOptRecord opt, Path2D path2D) {
        EscherSimpleProperty geoLeft = getEscherProperty(opt, EscherPropertyTypes.GEOMETRY__LEFT);
        EscherSimpleProperty geoRight = getEscherProperty(opt, EscherPropertyTypes.GEOMETRY__RIGHT);
        EscherSimpleProperty geoTop = getEscherProperty(opt, EscherPropertyTypes.GEOMETRY__TOP);
        EscherSimpleProperty geoBottom = getEscherProperty(opt, EscherPropertyTypes.GEOMETRY__BOTTOM);

        if (geoLeft != null && geoRight != null && geoTop != null && geoBottom != null) {
            final Rectangle2D bounds = new Rectangle2D.Double();
            bounds.setFrameFromDiagonal(
                    new Point2D.Double(geoLeft.getPropertyValue(), geoTop.getPropertyValue()),
                    new Point2D.Double(geoRight.getPropertyValue(), geoBottom.getPropertyValue())
            );
            return bounds;
        } else {
            return path2D.getBounds2D();
        }
    }

    private static void handleClosedShape(AbstractEscherOptRecord opt, Path path, Path2D path2D) {
        EscherSimpleProperty shapePath = getEscherProperty(opt, EscherPropertyTypes.GEOMETRY__SHAPEPATH);
        HSLFFreeformShape.ShapePath sp = HSLFFreeformShape.ShapePath.valueOf(shapePath == null ? 1 : shapePath.getPropertyValue());
        if (sp == LINES_CLOSED || sp == CURVES_CLOSED) {
            path.addCommand(new ClosePathCommand());
            path2D.closePath();
        }
    }

    private static void handleMoveTo(Iterator<byte[]> vertIter, int[] xyPoints, Path path, Path2D path2D) {
        if (!vertIter.hasNext()) {
            return;
        }
        final MoveToCommand m = new MoveToCommand();
        m.setPt(fillPoint(vertIter.next(), xyPoints));
        path.addCommand(m);
        path2D.moveTo(xyPoints[0], xyPoints[1]);
    }

    private static void handleLineTo(Iterator<byte[]> vertIter, int[] xyPoints, Path path, Path2D path2D) {
        if (!vertIter.hasNext()) {
            return;
        }
        handleMoveTo0(path, path2D);

        final LineToCommand m = new LineToCommand();
        m.setPt(fillPoint(vertIter.next(), xyPoints));
        path.addCommand(m);
        path2D.lineTo(xyPoints[0], xyPoints[1]);
    }

    private static void handleCurveTo(Iterator<byte[]> vertIter, int[] xyPoints, Path path, Path2D path2D) {
        if (!vertIter.hasNext()) {
            return;
        }
        handleMoveTo0(path, path2D);

        final CurveToCommand m = new CurveToCommand();

        int[] pts = new int[6];
        AdjustPoint[] ap = new AdjustPoint[3];

        for (int i=0; vertIter.hasNext() && i<3; i++) {
            ap[i] = fillPoint(vertIter.next(), xyPoints);
            pts[i*2] = xyPoints[0];
            pts[i*2+1] = xyPoints[1];
        }

        m.setPt1(ap[0]);
        m.setPt2(ap[1]);
        m.setPt3(ap[2]);

        path.addCommand(m);
        path2D.curveTo(pts[0], pts[1], pts[2], pts[3], pts[4], pts[5]);
    }

    /**
     * Sometimes the path2D is not initialized - this initializes it with the 0,0 position
     */
    private static void handleMoveTo0(Path moveLst, Path2D path2D) {
        if (path2D.getCurrentPoint() == null) {
            final MoveToCommand m = new MoveToCommand();

            AdjustPoint pt = new AdjustPoint();
            pt.setX("0");
            pt.setY("0");
            m.setPt(pt);
            moveLst.addCommand(m);
            path2D.moveTo(0, 0);
        }
    }

    private static void handleEscapeInfo(Path pathCT, Path2D path2D, byte[] segElem, Iterator<byte[]> vertIter) {
        EscapeInfo ei = getEscapeInfo(segElem);
        if (ei == null) {
            return;
        }
        switch (ei) {
            case EXTENSION:
                break;
            case ANGLE_ELLIPSE_TO:
                break;
            case ANGLE_ELLIPSE:
                break;
            case ARC_TO: {
                // The first two POINT values specify the bounding rectangle of the ellipse.
                // The second two POINT values specify the radial vectors for the ellipse.
                // The radial vectors are cast from the center of the bounding rectangle.
                // The path starts at the POINT where the first radial vector intersects the
                // bounding rectangle and goes to the POINT where the second radial vector
                // intersects the bounding rectangle. The drawing direction is always counterclockwise.
                // If the path has already been started, a line is drawn from the last POINT to
                // the starting POINT of the arc; otherwise, a new path is started.
                // The number of arc segments drawn equals the number of segments divided by four.

                int[] r1 = new int[2], r2 = new int[2], start = new int[2], end = new int[2];
                fillPoint(vertIter.next(), r1);
                fillPoint(vertIter.next(), r2);
                fillPoint(vertIter.next(), start);
                fillPoint(vertIter.next(), end);

                Arc2D arc2D = new Arc2D.Double();
                Rectangle2D.Double bounds = new Rectangle2D.Double();
                bounds.setFrameFromDiagonal(xy2p(r1), xy2p(r2));
                arc2D.setFrame(bounds);
                arc2D.setAngles(xy2p(start), xy2p(end));
                path2D.append(arc2D, true);


                ArcToCommand arcTo = new ArcToCommand();
                arcTo.setHR(d2s(bounds.getHeight()/2.0));
                arcTo.setWR(d2s(bounds.getWidth()/2.0));

                arcTo.setStAng(d2s(-arc2D.getAngleStart()*60000.));
                arcTo.setSwAng(d2s(-arc2D.getAngleExtent()*60000.));

                pathCT.addCommand(arcTo);

                break;
            }
            case ARC:
                break;
            case CLOCKWISE_ARC_TO:
                break;
            case CLOCKWISE_ARC:
                break;
            case ELLIPTICAL_QUADRANT_X:
                break;
            case ELLIPTICAL_QUADRANT_Y:
                break;
            case QUADRATIC_BEZIER:
                break;
            case NO_FILL:
                break;
            case NO_LINE:
                break;
            case AUTO_LINE:
                break;
            case AUTO_CURVE:
                break;
            case CORNER_LINE:
                break;
            case CORNER_CURVE:
                break;
            case SMOOTH_LINE:
                break;
            case SMOOTH_CURVE:
                break;
            case SYMMETRIC_LINE:
                break;
            case SYMMETRIC_CURVE:
                break;
            case FREEFORM:
                break;
            case FILL_COLOR:
                break;
            case LINE_COLOR:
                break;
            default:
                break;
        }
    }

    private static String d2s(double d) {
        return Integer.toString((int)Math.rint(d));
    }

    private static Point2D xy2p(int[] xyPoints) {
        return new Point2D.Double(xyPoints[0],xyPoints[1]);
    }

    private static PathInfo getPathInfo(byte[] elem) {
        int elemUS = LittleEndian.getUShort(elem, 0);
        int pathInfo = PATH_INFO.getValue(elemUS);
        return PathInfo.valueOf(pathInfo);
    }

    private static EscapeInfo getEscapeInfo(byte[] elem) {
        int elemUS = LittleEndian.getUShort(elem, 0);
        int escInfo = ESCAPE_INFO.getValue(elemUS);
        return EscapeInfo.valueOf(escInfo);
    }


    private static AdjustPoint fillPoint(byte[] xyMaster, int[] xyPoints) {
        if (xyMaster == null || xyPoints == null) {
            LOG.atWarn().log("Master bytes or points not set - ignore point");
            return null;
        }
        if ((xyMaster.length != 4 && xyMaster.length != 8) || xyPoints.length != 2) {
            LOG.atWarn().log("Invalid number of master bytes for a single point - ignore point");
            return null;
        }

        int x, y;
        if (xyMaster.length == 4) {
            x = LittleEndian.getShort(xyMaster, 0);
            y = LittleEndian.getShort(xyMaster, 2);
        } else {
            x = LittleEndian.getInt(xyMaster, 0);
            y = LittleEndian.getInt(xyMaster, 4);
        }

        xyPoints[0] = x;
        xyPoints[1] = y;

        return toPoint(xyPoints);
    }

    private static AdjustPoint toPoint(int[] xyPoints) {
        AdjustPoint pt = new AdjustPoint();
        pt.setX(Integer.toString(xyPoints[0]));
        pt.setY(Integer.toString(xyPoints[1]));
        return pt;
    }
}