1 // Copyright 2007, FreeHEP.
2 package org.freehep.graphicsio.emf.gdi;
3
4 import org.freehep.graphicsio.emf.EMFTag;
5 import org.freehep.graphicsio.emf.EMFRenderer;
6 import org.freehep.graphicsio.emf.EMFConstants;
7
8 import java.awt.geom.GeneralPath;
9 import java.awt.geom.AffineTransform;
10 import java.awt.geom.Area;
11 import java.awt.Shape;
12
13 /**
14 * base class for all tags that change the
15 * clipping area of the {@link org.freehep.graphicsio.emf.EMFRenderer}
16 *
17 * @author Steffen Greiffenberg
18 * @version $Id$
19 */
20 public abstract class AbstractClipPath extends EMFTag {
21
22 private int mode;
23
24 protected AbstractClipPath(int id, int version, int mode) {
25 super(id, version);
26 this.mode = mode;
27 }
28
29 public String toString() {
30 return super.toString() + "\n mode: " + mode;
31 }
32
33 public int getMode() {
34 return mode;
35 }
36
37 /**
38 * displays the tag using the renderer
39 *
40 * @param renderer EMFRenderer storing the drawing session data
41 * @param shape shape to use as clipping area
42 */
43 public void render(EMFRenderer renderer, Shape shape) {
44 if (shape != null) {
45 // The new clipping region includes the intersection
46 // (overlapping areas) of the current clipping region and the current shape.
47 if (mode == EMFConstants.RGN_AND) {
48 renderer.clip(shape);
49 }
50 // The new clipping region is the current shape
51 else if (mode == EMFConstants.RGN_COPY) {
52 // rest the clip ...
53 AffineTransform at = renderer.getTransform();
54 // temporarly switch to the base transformation to
55 // aplly the base clipping area
56 renderer.resetTransformation();
57 // set the clip
58 renderer.setClip(renderer.getInitialClip());
59 renderer.setTransform(at);
60 renderer.clip(shape);
61 }
62 // The new clipping region includes the areas of the
63 // current clipping region with those of the current shape excluded.
64 else if (mode == EMFConstants.RGN_DIFF) {
65 Shape clip = renderer.getClip();
66 if (clip != null) {
67 Area a = new Area(shape);
68 a.subtract(new Area(clip));
69 renderer.setClip(a);
70 } else {
71 renderer.setClip(shape);
72 }
73 }
74 // The new clipping region includes the union (combined areas)
75 // of the current clipping region and the current shape.
76 else if(mode == EMFConstants.RGN_OR) {
77 GeneralPath path = new GeneralPath(shape);
78 Shape clip = renderer.getClip();
79 if (clip != null) {
80 path.append(clip, false);
81 }
82 renderer.setClip(path);
83 }
84 // The new clipping region includes the union of the current
85 // clipping region and the current shape but without the overlapping areas.
86 else if(mode == EMFConstants.RGN_XOR) {
87 Shape clip = renderer.getClip();
88 if (clip != null) {
89 Area a = new Area(shape);
90 a.exclusiveOr(new Area(clip));
91 renderer.setClip(a);
92 } else {
93 renderer.setClip(shape);
94 }
95 }
96 }
97
98 // delete the current shape
99 renderer.setPath(null);
100 }
101 }