MetafileHeader.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // System.Drawing.Imaging.MetafileHeader.cs
  3. //
  4. // Author: Everaldo Canuto
  5. // eMail: [email protected]
  6. // Dennis Hayes ([email protected])
  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. using System.Drawing.Drawing2D;
  31. using System.Runtime.InteropServices;
  32. namespace System.Drawing.Imaging {
  33. [StructLayout(LayoutKind.Sequential, Pack=2)]
  34. struct EnhMetafileHeader {
  35. public int type;
  36. public int size;
  37. public Rectangle bounds;
  38. public Rectangle frame;
  39. public int signature;
  40. public int version;
  41. public int bytes;
  42. public int records;
  43. public short handles;
  44. public short reserved;
  45. public int description;
  46. public int off_description;
  47. public int palette_entires;
  48. public Size device;
  49. public Size millimeters;
  50. }
  51. // hack: keep public type as Sequential while making it possible to get the required union
  52. [StructLayout(LayoutKind.Explicit)]
  53. struct MonoMetafileHeader {
  54. [FieldOffset (0)]
  55. public MetafileType type;
  56. [FieldOffset (4)]
  57. public int size;
  58. [FieldOffset (8)]
  59. public int version;
  60. [FieldOffset (12)]
  61. public int emf_plus_flags;
  62. [FieldOffset (16)]
  63. public float dpi_x;
  64. [FieldOffset (20)]
  65. public float dpi_y;
  66. [FieldOffset (24)]
  67. public int x;
  68. [FieldOffset (28)]
  69. public int y;
  70. [FieldOffset (32)]
  71. public int width;
  72. [FieldOffset (36)]
  73. public int height;
  74. [FieldOffset (40)]
  75. public WmfMetaHeader wmf_header;
  76. [FieldOffset (40)]
  77. public EnhMetafileHeader emf_header;
  78. [FieldOffset (128)]
  79. public int emfplus_header_size;
  80. [FieldOffset (132)]
  81. public int logical_dpi_x;
  82. [FieldOffset (136)]
  83. public int logical_dpi_y;
  84. }
  85. [MonoTODO ("Metafiles, both WMF and EMF formats, aren't supported.")]
  86. #if !TARGET_JVM
  87. [StructLayout(LayoutKind.Sequential)]
  88. #endif
  89. public sealed class MetafileHeader {
  90. private MonoMetafileHeader header;
  91. //constructor
  92. internal MetafileHeader (IntPtr henhmetafile)
  93. {
  94. Marshal.PtrToStructure (henhmetafile, this);
  95. }
  96. // methods
  97. [MonoTODO ("always returns false")]
  98. public bool IsDisplay ()
  99. {
  100. return false;
  101. }
  102. public bool IsEmf ()
  103. {
  104. return (Type == MetafileType.Emf);
  105. }
  106. public bool IsEmfOrEmfPlus ()
  107. {
  108. return (Type >= MetafileType.Emf);
  109. }
  110. public bool IsEmfPlus ()
  111. {
  112. return (Type >= MetafileType.EmfPlusOnly);
  113. }
  114. public bool IsEmfPlusDual ()
  115. {
  116. return (Type == MetafileType.EmfPlusDual);
  117. }
  118. public bool IsEmfPlusOnly ()
  119. {
  120. return (Type == MetafileType.EmfPlusOnly);
  121. }
  122. public bool IsWmf ()
  123. {
  124. return (Type <= MetafileType.WmfPlaceable);
  125. }
  126. public bool IsWmfPlaceable ()
  127. {
  128. return (Type == MetafileType.WmfPlaceable);
  129. }
  130. // properties
  131. public Rectangle Bounds {
  132. get { return new Rectangle (header.x, header.y, header.width, header.height); }
  133. }
  134. public float DpiX {
  135. get { return header.dpi_x; }
  136. }
  137. public float DpiY {
  138. get { return header.dpi_y; }
  139. }
  140. public int EmfPlusHeaderSize {
  141. get { return header.emfplus_header_size; }
  142. }
  143. public int LogicalDpiX {
  144. get { return header.logical_dpi_x; }
  145. }
  146. public int LogicalDpiY {
  147. get { return header.logical_dpi_y; }
  148. }
  149. public int MetafileSize {
  150. get { return header.size; }
  151. }
  152. public MetafileType Type {
  153. get { return header.type; }
  154. }
  155. public int Version {
  156. get { return header.version; }
  157. }
  158. // note: this always returns a new instance (where we can change
  159. // properties even if they don't seems to affect anything)
  160. public MetaHeader WmfHeader {
  161. get {
  162. if (IsWmfPlaceable ())
  163. return new MetaHeader (header.wmf_header);
  164. throw new ArgumentException ("WmfHeader only available on WMF files.");
  165. }
  166. }
  167. }
  168. }