1 // Copyright 2002, FreeHEP.
2 package org.freehep.graphicsio.emf.gdi;
3
4 import java.io.IOException;
5 import java.awt.geom.AffineTransform;
6
7 import org.freehep.graphicsio.emf.EMFConstants;
8 import org.freehep.graphicsio.emf.EMFInputStream;
9 import org.freehep.graphicsio.emf.EMFOutputStream;
10 import org.freehep.graphicsio.emf.EMFTag;
11 import org.freehep.graphicsio.emf.EMFRenderer;
12
13 /**
14 * SetMapMode TAG.
15 *
16 * @author Mark Donszelmann
17 * @version $Id: SetMapMode.java 10367 2007-01-22 19:26:48Z duns $
18 */
19 public class SetMapMode extends EMFTag implements EMFConstants {
20
21 private int mode;
22
23 public SetMapMode() {
24 super(17, 1);
25 }
26
27 public SetMapMode(int mode) {
28 this();
29 this.mode = mode;
30 }
31
32 public EMFTag read(int tagID, EMFInputStream emf, int len)
33 throws IOException {
34
35 return new SetMapMode(emf.readDWORD());
36 }
37
38 public void write(int tagID, EMFOutputStream emf) throws IOException {
39 emf.writeDWORD(mode);
40 }
41
42 public String toString() {
43 return super.toString() + "\n mode: " + mode;
44 }
45
46 /**
47 * displays the tag using the renderer
48 *
49 * @param renderer EMFRenderer storing the drawing session data
50 */
51 public void render(EMFRenderer renderer) {
52 // MM_ANISOTROPIC Logical units are mapped to arbitrary
53 // units with arbitrarily scaled axes. Use the SetWindowExtEx
54 // and SetViewportExtEx functions to specify the units,
55 // orientation, and scaling.
56 if (mode == EMFConstants.MM_ANISOTROPIC) {
57 renderer.setMapModeIsotropic(false);
58 }
59
60 // MM_HIENGLISH Each logical unit is mapped to 0.001 inch.
61 // Positive x is to the right; positive y is up.
62 else if (mode == EMFConstants.MM_HIENGLISH) {
63 // TODO not sure
64 double scale = 0.001 * 25.4;
65 renderer.setMapModeTransform(
66 AffineTransform.getScaleInstance(scale, scale));
67 }
68
69 // MM_HIMETRIC Each logical unit is mapped to 0.01 millimeter.
70 // Positive x is to the right; positive y is up.
71 else if (mode == EMFConstants.MM_HIMETRIC) {
72 // TODO not sure
73 double scale = 0.01;
74 renderer.setMapModeTransform(
75 AffineTransform.getScaleInstance(scale, scale));
76 }
77
78 // MM_ISOTROPIC Logical units are mapped to arbitrary units
79 // with equally scaled axes; that is, one unit along the x-axis
80 // is equal to one unit along the y-axis. Use the SetWindowExtEx
81 // and SetViewportExtEx functions to specify the units and the
82 // orientation of the axes. Graphics device interface (GDI) makes
83 // adjustments as necessary to ensure the x and y units remain
84 // the same size (When the window extent is set, the viewport will
85 // be adjusted to keep the units isotropic).
86 else if (mode == EMFConstants.MM_ISOTROPIC) {
87 renderer.setMapModeIsotropic(true);
88 renderer.fixViewportSize();
89 }
90
91 // MM_LOENGLISH Each logical unit is mapped to 0.01 inch.
92 // Positive x is to the right; positive y is up.
93 else if (mode == EMFConstants.MM_LOENGLISH) {
94 // TODO not sure
95 double scale = 0.01 * 25.4;
96 renderer.setMapModeTransform(
97 AffineTransform.getScaleInstance(scale, scale));
98 }
99
100 // MM_LOMETRIC Each logical unit is mapped to 0.1 millimeter.
101 // Positive x is to the right; positive y is up.
102 else if (mode == EMFConstants.MM_LOMETRIC) {
103 // TODO not sure
104 double scale = 0.1;
105 renderer.setMapModeTransform(
106 AffineTransform.getScaleInstance(scale, scale));
107 }
108
109 // MM_TEXT Each logical unit is mapped to one device pixel. Positive
110 // x is to the right; positive y is down.
111 else if (mode == EMFConstants.MM_TEXT) {
112 renderer.setMapModeTransform(
113 AffineTransform.getScaleInstance(1, -1));
114 }
115
116 // MM_TWIPS Each logical unit is mapped to one twentieth of a
117 // printer's point (1/1440 inch, also called a twip). Positive x
118 // is to the right; positive y is up.
119 else if (mode == EMFConstants.MM_TWIPS) {
120 renderer.setMapModeTransform(
121 AffineTransform.getScaleInstance(
122 EMFRenderer.TWIP_SCALE, EMFRenderer.TWIP_SCALE));
123 }
124 }
125 }