Merge pull request #1218 from AndreyAkinshin/master
[mono.git] / mcs / class / System.Drawing / System.Drawing.Imaging / MetafileHeader.cs
1 //
2 // System.Drawing.Imaging.MetafileHeader.cs
3 //
4 // Author: Everaldo Canuto
5 // eMail: everaldo.canuto@bol.com.br
6 // Dennis Hayes (dennish@raytek.com)
7 //
8 // (C) 2002 Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Drawing.Drawing2D;
32 using System.Runtime.InteropServices;
33
34 namespace System.Drawing.Imaging {
35
36         [StructLayout(LayoutKind.Sequential, Pack=2)]
37         struct EnhMetafileHeader {
38                 public int              type;
39                 public int              size;
40                 public Rectangle        bounds;
41                 public Rectangle        frame;
42                 public int              signature;
43                 public int              version;
44                 public int              bytes;
45                 public int              records;
46                 public short            handles;
47                 public short            reserved;
48                 public int              description;
49                 public int              off_description;
50                 public int              palette_entires;
51                 public Size             device;
52                 public Size             millimeters;
53         }
54
55         // hack: keep public type as Sequential while making it possible to get the required union
56         [StructLayout(LayoutKind.Explicit)]
57         struct MonoMetafileHeader {
58                 [FieldOffset (0)]
59                 public MetafileType     type;
60                 [FieldOffset (4)]
61                 public int              size;
62                 [FieldOffset (8)]
63                 public int              version;
64                 [FieldOffset (12)]
65                 public int              emf_plus_flags;
66                 [FieldOffset (16)]
67                 public float            dpi_x;
68                 [FieldOffset (20)]
69                 public float            dpi_y;
70                 [FieldOffset (24)]
71                 public int              x;
72                 [FieldOffset (28)]
73                 public int              y;
74                 [FieldOffset (32)]
75                 public int              width;
76                 [FieldOffset (36)]
77                 public int              height;
78                 [FieldOffset (40)]
79                 public WmfMetaHeader    wmf_header;
80                 [FieldOffset (40)]
81                 public EnhMetafileHeader emf_header;
82                 [FieldOffset (128)]
83                 public int              emfplus_header_size;
84                 [FieldOffset (132)]
85                 public int              logical_dpi_x;
86                 [FieldOffset (136)]
87                 public int              logical_dpi_y;
88         }
89
90         [MonoTODO ("Metafiles, both WMF and EMF formats, aren't supported.")]
91         [StructLayout(LayoutKind.Sequential)]
92         public sealed class MetafileHeader {
93
94                 private MonoMetafileHeader header;
95                 
96                 //constructor
97
98                 internal MetafileHeader (IntPtr henhmetafile)
99                 {
100                         Marshal.PtrToStructure (henhmetafile, this);
101                 }
102
103                 // methods
104
105                 [MonoTODO ("always returns false")]
106                 public bool IsDisplay ()
107                 {
108                         return false;
109                 }
110
111                 public bool IsEmf ()
112                 {
113                         return (Type == MetafileType.Emf);
114                 }
115
116                 public bool IsEmfOrEmfPlus ()
117                 {
118                         return (Type >= MetafileType.Emf);
119                 }
120
121                 public bool IsEmfPlus ()
122                 {
123                         return (Type >= MetafileType.EmfPlusOnly);
124                 }
125
126                 public bool IsEmfPlusDual ()
127                 {
128                         return (Type == MetafileType.EmfPlusDual);
129                 }
130
131                 public bool IsEmfPlusOnly ()
132                 {
133                         return (Type == MetafileType.EmfPlusOnly);
134                 }
135
136                 public bool IsWmf ()
137                 {
138                         return (Type <= MetafileType.WmfPlaceable);
139                 }
140
141                 public bool IsWmfPlaceable ()
142                 {
143                         return (Type == MetafileType.WmfPlaceable);
144                 }
145
146                 // properties
147
148                 public Rectangle Bounds {
149                         get { return new Rectangle (header.x, header.y, header.width, header.height); }
150                 }
151
152                 public float DpiX {
153                         get { return header.dpi_x; }
154                 }
155                 
156                 public float DpiY {
157                         get { return header.dpi_y; }
158                 }
159                 
160                 public int EmfPlusHeaderSize {
161                         get { return header.emfplus_header_size; }
162                 }
163
164                 public int LogicalDpiX {
165                         get { return header.logical_dpi_x; }
166                 }
167                 
168                 public int LogicalDpiY {
169                         get { return header.logical_dpi_y; }
170                 }
171
172                 public int MetafileSize {
173                         get { return header.size; }
174                 }
175
176                 public MetafileType Type {
177                         get { return header.type; }
178                 }
179
180                 public int Version {
181                         get { return header.version; }
182                 }
183
184                 // note: this always returns a new instance (where we can change
185                 // properties even if they don't seems to affect anything)
186                 public MetaHeader WmfHeader {
187                         get {
188                                 if (IsWmf ())
189                                          return new MetaHeader (header.wmf_header);
190                                 throw new ArgumentException ("WmfHeader only available on WMF files.");
191                         }
192                 }
193         }
194 }