aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfDrawProperties.java
blob: ebb3a0234a2f8f35640a7d68e692379c983aa07f (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
/* ====================================================================
   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.hwmf.draw;

import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.List;

import org.apache.poi.hwmf.draw.HwmfGraphics.BufferedImageRenderer;
import org.apache.poi.hwmf.record.HwmfBinaryRasterOp;
import org.apache.poi.hwmf.record.HwmfBrushStyle;
import org.apache.poi.hwmf.record.HwmfColorRef;
import org.apache.poi.hwmf.record.HwmfFill.WmfSetPolyfillMode.HwmfPolyfillMode;
import org.apache.poi.hwmf.record.HwmfFont;
import org.apache.poi.hwmf.record.HwmfHatchStyle;
import org.apache.poi.hwmf.record.HwmfMapMode;
import org.apache.poi.hwmf.record.HwmfMisc.WmfSetBkMode.HwmfBkMode;
import org.apache.poi.hwmf.record.HwmfPalette.PaletteEntry;
import org.apache.poi.hwmf.record.HwmfPenStyle;
import org.apache.poi.hwmf.record.HwmfTernaryRasterOp;
import org.apache.poi.hwmf.record.HwmfText.HwmfTextAlignment;
import org.apache.poi.hwmf.record.HwmfText.HwmfTextVerticalAlignment;
import org.apache.poi.sl.draw.ImageRenderer;

public class HwmfDrawProperties {
    private final Rectangle2D window;
    private Rectangle2D viewport;
    private final Point2D location;
    private HwmfMapMode mapMode;
    private HwmfColorRef backgroundColor;
    private HwmfBrushStyle brushStyle;
    private HwmfColorRef brushColor;
    private HwmfHatchStyle brushHatch;
    private ImageRenderer brushBitmap;
    private final AffineTransform brushTransform = new AffineTransform();
    private double penWidth;
    private HwmfPenStyle penStyle;
    private HwmfColorRef penColor;
    private double penMiterLimit;
    private HwmfBkMode bkMode;
    private HwmfPolyfillMode polyfillMode;
    private Shape region;
    private List<PaletteEntry> palette;
    private int paletteOffset;
    private HwmfFont font;
    private HwmfColorRef textColor;
    private HwmfTextAlignment textAlignLatin;
    private HwmfTextVerticalAlignment textVAlignLatin;
    private HwmfTextAlignment textAlignAsian;
    private HwmfTextVerticalAlignment textVAlignAsian;
    private HwmfBinaryRasterOp rasterOp2;
    private HwmfTernaryRasterOp rasterOp3;
    protected Shape clip;
    protected final AffineTransform transform = new AffineTransform();

    public HwmfDrawProperties() {
        window = new Rectangle2D.Double(0, 0, 1, 1);
        viewport = null;
        location = new Point2D.Double(0,0);
        mapMode = HwmfMapMode.MM_ANISOTROPIC;
        backgroundColor = new HwmfColorRef(Color.BLACK);
        brushStyle = HwmfBrushStyle.BS_SOLID;
        brushColor = new HwmfColorRef(Color.BLACK);
        brushHatch = HwmfHatchStyle.HS_HORIZONTAL;
        penWidth = 1;
        penStyle = HwmfPenStyle.valueOf(0);
        penColor = new HwmfColorRef(Color.BLACK);
        penMiterLimit = 10;
        bkMode = HwmfBkMode.OPAQUE;
        polyfillMode = HwmfPolyfillMode.WINDING;
        textColor = new HwmfColorRef(Color.BLACK);
        textAlignLatin = HwmfTextAlignment.LEFT;
        textVAlignLatin = HwmfTextVerticalAlignment.TOP;
        textAlignAsian = HwmfTextAlignment.RIGHT;
        textVAlignAsian = HwmfTextVerticalAlignment.TOP;
        rasterOp2 = HwmfBinaryRasterOp.R2_COPYPEN;
        rasterOp3 = null; // default: PATCOPY?
        clip = null;
        font = new HwmfFont();
        font.initDefaults();
    }

    public HwmfDrawProperties(HwmfDrawProperties other) {
        this.window = (other.window == null) ? null : (Rectangle2D)other.window.clone();
        this.viewport = (other.viewport == null) ? null : (Rectangle2D)other.viewport.clone();
        this.location = (Point2D)other.location.clone();
        this.mapMode = other.mapMode;
        this.backgroundColor = (other.backgroundColor == null) ? null : other.backgroundColor.copy();
        this.brushStyle = other.brushStyle;
        this.brushColor = other.brushColor.copy();
        this.brushHatch = other.brushHatch;
        this.brushBitmap = other.brushBitmap;
        this.brushTransform.setTransform(other.brushTransform);
        this.penWidth = other.penWidth;
        this.penStyle = (other.penStyle == null) ? null : other.penStyle.copy();
        this.penColor = (other.penColor == null) ? null : other.penColor.copy();
        this.penMiterLimit = other.penMiterLimit;
        this.bkMode = other.bkMode;
        this.polyfillMode = other.polyfillMode;
        if (other.region instanceof Rectangle2D) {
            this.region = other.region.getBounds2D();
        } else if (other.region instanceof Area) {
            this.region = new Area(other.region);
        }
        this.palette = other.palette;
        this.paletteOffset = other.paletteOffset;
        this.font = other.font;
        this.textColor = (other.textColor == null) ? null : other.textColor.copy();
        this.textAlignLatin = other.textAlignLatin;
        this.textVAlignLatin = other.textVAlignLatin;
        this.textAlignAsian = other.textAlignAsian;
        this.textVAlignAsian = other.textVAlignAsian;
        this.rasterOp2 = other.rasterOp2;
        this.rasterOp3 = other.rasterOp3;
        this.transform.setTransform(other.transform);
        this.clip = other.clip;
    }

    public void setViewportExt(double width, double height) {
        if (viewport == null) {
            viewport = (Rectangle2D)window.clone();
        }
        double x = viewport.getX();
        double y = viewport.getY();
        double w = (width != 0) ? width : viewport.getWidth();
        double h = (height != 0) ? height : viewport.getHeight();
        viewport.setRect(x, y, w, h);
    }

    public void setViewportOrg(double x, double y) {
        if (viewport == null) {
            viewport = (Rectangle2D)window.clone();
        }
        double w = viewport.getWidth();
        double h = viewport.getHeight();
        viewport.setRect(x, y, w, h);
    }

    public Rectangle2D getViewport() {
        return (viewport == null) ? null : (Rectangle2D)viewport.clone();
    }

    public void setWindowExt(double width, double height) {
        double x = window.getX();
        double y = window.getY();
        double w = (width != 0) ? width : window.getWidth();
        double h = (height != 0) ? height : window.getHeight();
        window.setRect(x, y, w, h);
    }

    public void setWindowOrg(double x, double y) {
        double w = window.getWidth();
        double h = window.getHeight();
        window.setRect(x, y, w, h);
    }

    public Rectangle2D getWindow() {
        return (Rectangle2D)window.clone();
    }

    public void setLocation(double x, double y) {
        location.setLocation(x, y);
    }

    public void setLocation(Point2D point) {
        location.setLocation(point);
    }

    public Point2D getLocation() {
        return (Point2D)location.clone();
    }

    public void setMapMode(HwmfMapMode mapMode) {
        this.mapMode = mapMode;
    }

    public HwmfMapMode getMapMode() {
        return mapMode;
    }

    public HwmfBrushStyle getBrushStyle() {
        return brushStyle;
    }

    public void setBrushStyle(HwmfBrushStyle brushStyle) {
        this.brushStyle = brushStyle;
    }

    public HwmfHatchStyle getBrushHatch() {
        return brushHatch;
    }

    public void setBrushHatch(HwmfHatchStyle brushHatch) {
        this.brushHatch = brushHatch;
    }

    public HwmfColorRef getBrushColor() {
        return brushColor;
    }

    public void setBrushColor(HwmfColorRef brushColor) {
        this.brushColor = brushColor;
    }

    public HwmfBkMode getBkMode() {
        return bkMode;
    }

    public void setBkMode(HwmfBkMode bkMode) {
        this.bkMode = bkMode;
    }

    public HwmfPenStyle getPenStyle() {
        return penStyle;
    }

    public void setPenStyle(HwmfPenStyle penStyle) {
        this.penStyle = penStyle;
    }

    public HwmfColorRef getPenColor() {
        return penColor;
    }

    public void setPenColor(HwmfColorRef penColor) {
        this.penColor = penColor;
    }

    public double getPenWidth() {
        return penWidth;
    }

    public void setPenWidth(double penWidth) {
        this.penWidth = penWidth;
    }

    public double getPenMiterLimit() {
        return penMiterLimit;
    }

    public void setPenMiterLimit(double penMiterLimit) {
        this.penMiterLimit = penMiterLimit;
    }

    public HwmfColorRef getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(HwmfColorRef backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public HwmfPolyfillMode getPolyfillMode() {
        return polyfillMode;
    }

    public void setPolyfillMode(HwmfPolyfillMode polyfillMode) {
        this.polyfillMode = polyfillMode;
    }

    public ImageRenderer getBrushBitmap() {
        return brushBitmap;
    }

    public void setBrushBitmap(ImageRenderer brushBitmap) {
        this.brushBitmap = brushBitmap;
    }

    public void setBrushBitmap(BufferedImage brushBitmap) {
        this.brushBitmap = (brushBitmap == null) ? null : new BufferedImageRenderer(brushBitmap);
    }

    /**
     * Gets the last stored region
     *
     * @return the last stored region
     */
    public Shape getRegion() {
        return region;
    }

    /**
     * Sets a region for further usage
     *
     * @param region a region object which is usually a rectangle
     */
    public void setRegion(Shape region) {
        this.region = region;
    }

    /**
     * Returns the current palette.
     * Callers may modify the palette.
     *
     * @return the current palette or null, if it hasn't been set
     */
    public List<PaletteEntry> getPalette() {
        return palette;
    }

    /**
     * Sets the current palette.
     * It's the callers duty to set a modifiable copy of the palette.
     */
    public void setPalette(List<PaletteEntry> palette) {
        this.palette = palette;
    }

    public int getPaletteOffset() {
        return paletteOffset;
    }

    public void setPaletteOffset(int paletteOffset) {
        this.paletteOffset = paletteOffset;
    }

    public HwmfColorRef getTextColor() {
        return textColor;
    }

    public void setTextColor(HwmfColorRef textColor) {
        this.textColor = textColor;
    }

    public HwmfFont getFont() {
        return font;
    }

    public void setFont(HwmfFont font) {
        this.font = font;
    }

    public HwmfTextAlignment getTextAlignLatin() {
        return textAlignLatin;
    }

    public void setTextAlignLatin(HwmfTextAlignment textAlignLatin) {
        this.textAlignLatin = textAlignLatin;
    }

    public HwmfTextVerticalAlignment getTextVAlignLatin() {
        return textVAlignLatin;
    }

    public void setTextVAlignLatin(HwmfTextVerticalAlignment textVAlignLatin) {
        this.textVAlignLatin = textVAlignLatin;
    }

    public HwmfTextAlignment getTextAlignAsian() {
        return textAlignAsian;
    }

    public void setTextAlignAsian(HwmfTextAlignment textAlignAsian) {
        this.textAlignAsian = textAlignAsian;
    }

    public HwmfTextVerticalAlignment getTextVAlignAsian() {
        return textVAlignAsian;
    }

    public void setTextVAlignAsian(HwmfTextVerticalAlignment textVAlignAsian) {
        this.textVAlignAsian = textVAlignAsian;
    }

    /**
     * @return the current active winding rule ({@link Path2D#WIND_EVEN_ODD} or {@link Path2D#WIND_NON_ZERO})
     */
    public int getWindingRule() {
        return getPolyfillMode().awtFlag;
    }

    public HwmfTernaryRasterOp getRasterOp3() {
        return rasterOp3;
    }

    public void setRasterOp3(HwmfTernaryRasterOp rasterOp3) {
        this.rasterOp3 = rasterOp3;
    }

    public AffineTransform getTransform() {
        return transform;
    }

    public void setTransform(AffineTransform transform) {
        this.transform.setTransform(transform);
    }

    public Shape getClip() {
        return clip;
    }

    public void setClip(Shape clip) {
        this.clip = clip;
    }

    public AffineTransform getBrushTransform() {
        return brushTransform;
    }

    public void setBrushTransform(AffineTransform brushTransform) {
        if (brushTransform == null) {
            this.brushTransform.setToIdentity();
        } else {
            this.brushTransform.setTransform(brushTransform);
        }
    }
    public HwmfBinaryRasterOp getRasterOp2() {
        return rasterOp2;
    }

    public void setRasterOp2(HwmfBinaryRasterOp rasterOp2) {
        this.rasterOp2 = rasterOp2;
    }
}