MetafileTest.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. //
  2. // Metafile class unit tests
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Drawing;
  30. using System.Drawing.Drawing2D;
  31. using System.Drawing.Imaging;
  32. using System.IO;
  33. using System.Runtime.InteropServices;
  34. using System.Security.Permissions;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Drawing.Imaging {
  37. [TestFixture]
  38. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  39. public class MetafileTest {
  40. public const string Bitmap = "bitmaps/non-inverted.bmp";
  41. public const string WmfPlaceable = "bitmaps/telescope_01.wmf";
  42. public const string Emf = "bitmaps/milkmateya01.emf";
  43. // Get the input directory depending on the runtime
  44. static public string getInFile (string file)
  45. {
  46. string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
  47. if (!File.Exists (sRslt))
  48. sRslt = "Test/System.Drawing/" + file;
  49. return sRslt;
  50. }
  51. [Test]
  52. [ExpectedException (typeof (ArgumentException))]
  53. public void Metafile_Stream_Null ()
  54. {
  55. new Metafile ((Stream)null);
  56. }
  57. [Test]
  58. [ExpectedException (typeof (ArgumentNullException))]
  59. public void Metafile_String_Null ()
  60. {
  61. new Metafile ((string) null);
  62. }
  63. [Test]
  64. [ExpectedException (typeof (ArgumentException))]
  65. public void Metafile_String_Empty ()
  66. {
  67. new Metafile (String.Empty);
  68. }
  69. [Test]
  70. [ExpectedException (typeof (ExternalException))]
  71. public void Metafile_String_FileDoesNotExists ()
  72. {
  73. string filename = getInFile ("telescope_02.wmf");
  74. new Metafile (filename);
  75. }
  76. [Test]
  77. public void Metafile_String ()
  78. {
  79. string filename = getInFile (WmfPlaceable);
  80. Metafile mf = new Metafile (filename);
  81. Metafile clone = (Metafile) mf.Clone ();
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ExternalException))]
  85. public void GetMetafileHeader_Bitmap ()
  86. {
  87. new Metafile (getInFile (Bitmap));
  88. }
  89. static public void Check_MetaHeader_WmfPlaceable (MetaHeader mh)
  90. {
  91. Assert.AreEqual (9, mh.HeaderSize, "HeaderSize");
  92. Assert.AreEqual (98, mh.MaxRecord, "MaxRecord");
  93. Assert.AreEqual (3, mh.NoObjects, "NoObjects");
  94. Assert.AreEqual (0, mh.NoParameters, "NoParameters");
  95. Assert.AreEqual (1737, mh.Size, "Size");
  96. Assert.AreEqual (1, mh.Type, "Type");
  97. Assert.AreEqual (0x300, mh.Version, "Version");
  98. }
  99. public static void Check_MetafileHeader_WmfPlaceable (MetafileHeader header)
  100. {
  101. Assert.AreEqual (MetafileType.WmfPlaceable, header.Type, "Type");
  102. Assert.AreEqual (0x300, header.Version, "Version");
  103. // filesize - 22, which happens to be the size (22) of a PLACEABLEMETAHEADER struct
  104. Assert.AreEqual (3474, header.MetafileSize, "MetafileSize");
  105. Assert.AreEqual (-30, header.Bounds.X, "Bounds.X");
  106. Assert.AreEqual (-40, header.Bounds.Y, "Bounds.Y");
  107. Assert.AreEqual (3096, header.Bounds.Width, "Bounds.Width");
  108. Assert.AreEqual (4127, header.Bounds.Height, "Bounds.Height");
  109. Assert.AreEqual (606, header.DpiX, "DpiX");
  110. Assert.AreEqual (606, header.DpiY, "DpiY");
  111. Assert.AreEqual (0, header.EmfPlusHeaderSize, "EmfPlusHeaderSize");
  112. Assert.AreEqual (0, header.LogicalDpiX, "LogicalDpiX");
  113. Assert.AreEqual (0, header.LogicalDpiY, "LogicalDpiY");
  114. Assert.IsNotNull (header.WmfHeader, "WmfHeader");
  115. Check_MetaHeader_WmfPlaceable (header.WmfHeader);
  116. Assert.IsFalse (header.IsDisplay (), "IsDisplay");
  117. Assert.IsFalse (header.IsEmf (), "IsEmf");
  118. Assert.IsFalse (header.IsEmfOrEmfPlus (), "IsEmfOrEmfPlus");
  119. Assert.IsFalse (header.IsEmfPlus (), "IsEmfPlus");
  120. Assert.IsFalse (header.IsEmfPlusDual (), "IsEmfPlusDual");
  121. Assert.IsFalse (header.IsEmfPlusOnly (), "IsEmfPlusOnly");
  122. Assert.IsTrue (header.IsWmf (), "IsWmf");
  123. Assert.IsTrue (header.IsWmfPlaceable (), "IsWmfPlaceable");
  124. }
  125. [Test]
  126. public void GetMetafileHeader_WmfPlaceable ()
  127. {
  128. using (Metafile mf = new Metafile (getInFile (WmfPlaceable))) {
  129. MetafileHeader header1 = mf.GetMetafileHeader ();
  130. Check_MetafileHeader_WmfPlaceable (header1);
  131. MetafileHeader header2 = mf.GetMetafileHeader ();
  132. Assert.IsFalse (Object.ReferenceEquals (header1, header2), "Same object");
  133. }
  134. }
  135. [Test]
  136. public void GetMetafileHeader_FromFile_WmfPlaceable ()
  137. {
  138. using (Metafile mf = new Metafile (getInFile (WmfPlaceable))) {
  139. MetafileHeader header1 = mf.GetMetafileHeader ();
  140. Check_MetafileHeader_WmfPlaceable (header1);
  141. MetaHeader mh1 = header1.WmfHeader;
  142. Check_MetaHeader_WmfPlaceable (mh1);
  143. MetaHeader mh2 = mf.GetMetafileHeader ().WmfHeader;
  144. Assert.IsFalse (Object.ReferenceEquals (mh1, mh2), "Same object");
  145. }
  146. }
  147. [Test]
  148. public void GetMetafileHeader_FromFileStream_WmfPlaceable ()
  149. {
  150. using (FileStream fs = File.OpenRead (getInFile (WmfPlaceable))) {
  151. using (Metafile mf = new Metafile (fs)) {
  152. MetafileHeader header1 = mf.GetMetafileHeader ();
  153. Check_MetafileHeader_WmfPlaceable (header1);
  154. MetaHeader mh1 = header1.WmfHeader;
  155. Check_MetaHeader_WmfPlaceable (mh1);
  156. MetaHeader mh2 = mf.GetMetafileHeader ().WmfHeader;
  157. Assert.IsFalse (Object.ReferenceEquals (mh1, mh2), "Same object");
  158. }
  159. }
  160. }
  161. [Test]
  162. public void GetMetafileHeader_FromMemoryStream_WmfPlaceable ()
  163. {
  164. MemoryStream ms;
  165. string filename = getInFile (WmfPlaceable);
  166. using (FileStream fs = File.OpenRead (filename)) {
  167. byte[] data = new byte[fs.Length];
  168. fs.Read (data, 0, data.Length);
  169. ms = new MemoryStream (data);
  170. }
  171. using (Metafile mf = new Metafile (ms)) {
  172. MetafileHeader header1 = mf.GetMetafileHeader ();
  173. Check_MetafileHeader_WmfPlaceable (header1);
  174. MetaHeader mh1 = header1.WmfHeader;
  175. Check_MetaHeader_WmfPlaceable (mh1);
  176. MetaHeader mh2 = mf.GetMetafileHeader ().WmfHeader;
  177. Assert.IsFalse (Object.ReferenceEquals (mh1, mh2), "Same object");
  178. }
  179. ms.Close ();
  180. }
  181. public static void Check_MetafileHeader_Emf (MetafileHeader header)
  182. {
  183. Assert.AreEqual (MetafileType.Emf, header.Type, "Type");
  184. Assert.AreEqual (65536, header.Version, "Version");
  185. // extactly the filesize
  186. Assert.AreEqual (20456, header.MetafileSize, "MetafileSize");
  187. Assert.AreEqual (0, header.Bounds.X, "Bounds.X");
  188. Assert.AreEqual (0, header.Bounds.Y, "Bounds.Y");
  189. #if false
  190. Assert.AreEqual (759, header.Bounds.Width, "Bounds.Width");
  191. Assert.AreEqual (1073, header.Bounds.Height, "Bounds.Height");
  192. Assert.AreEqual (96f, header.DpiX, 0.5f, "DpiX");
  193. Assert.AreEqual (96f, header.DpiY, 0.5f, "DpiY");
  194. Assert.AreEqual (6619188, header.EmfPlusHeaderSize, "EmfPlusHeaderSize");
  195. Assert.AreEqual (3670064, header.LogicalDpiX, "LogicalDpiX");
  196. Assert.AreEqual (3670064, header.LogicalDpiY, "LogicalDpiY");
  197. #endif
  198. try {
  199. Assert.IsNotNull (header.WmfHeader, "WmfHeader");
  200. Assert.Fail ("WmfHeader didn't throw an ArgumentException");
  201. }
  202. catch (ArgumentException) {
  203. }
  204. catch (Exception e) {
  205. Assert.Fail ("WmfHeader didn't throw an ArgumentException but: {0}.", e.ToString ());
  206. }
  207. Assert.IsFalse (header.IsDisplay (), "IsDisplay");
  208. Assert.IsTrue (header.IsEmf (), "IsEmf");
  209. Assert.IsTrue (header.IsEmfOrEmfPlus (), "IsEmfOrEmfPlus");
  210. Assert.IsFalse (header.IsEmfPlus (), "IsEmfPlus");
  211. Assert.IsFalse (header.IsEmfPlusDual (), "IsEmfPlusDual");
  212. Assert.IsFalse (header.IsEmfPlusOnly (), "IsEmfPlusOnly");
  213. Assert.IsFalse (header.IsWmf (), "IsWmf");
  214. Assert.IsFalse (header.IsWmfPlaceable (), "IsWmfPlaceable");
  215. }
  216. [Test]
  217. public void GetMetafileHeader_FromFile_Emf ()
  218. {
  219. using (Metafile mf = new Metafile (getInFile (Emf))) {
  220. MetafileHeader header1 = mf.GetMetafileHeader ();
  221. Check_MetafileHeader_Emf (header1);
  222. }
  223. }
  224. [Test]
  225. public void GetMetafileHeader_FromFileStream_Emf ()
  226. {
  227. using (FileStream fs = File.OpenRead (getInFile (Emf))) {
  228. using (Metafile mf = new Metafile (fs)) {
  229. MetafileHeader header1 = mf.GetMetafileHeader ();
  230. Check_MetafileHeader_Emf (header1);
  231. }
  232. }
  233. }
  234. [Test]
  235. public void GetMetafileHeader_FromMemoryStream_Emf ()
  236. {
  237. MemoryStream ms;
  238. string filename = getInFile (Emf);
  239. using (FileStream fs = File.OpenRead (filename)) {
  240. byte[] data = new byte[fs.Length];
  241. fs.Read (data, 0, data.Length);
  242. ms = new MemoryStream (data);
  243. }
  244. using (Metafile mf = new Metafile (ms)) {
  245. MetafileHeader header1 = mf.GetMetafileHeader ();
  246. Check_MetafileHeader_Emf (header1);
  247. }
  248. ms.Close ();
  249. }
  250. [Test]
  251. [ExpectedException (typeof (NullReferenceException))]
  252. public void Static_GetMetafileHeader_Stream_Null ()
  253. {
  254. MetafileHeader header = Metafile.GetMetafileHeader ((Stream)null);
  255. }
  256. [Test]
  257. public void Static_GetMetafileHeader_Stream ()
  258. {
  259. string filename = getInFile (WmfPlaceable);
  260. using (FileStream fs = File.OpenRead (filename)) {
  261. MetafileHeader header = Metafile.GetMetafileHeader (fs);
  262. Check_MetafileHeader_WmfPlaceable (header);
  263. }
  264. }
  265. [Test]
  266. [ExpectedException (typeof (ArgumentNullException))]
  267. public void Static_GetMetafileHeader_Filename_Null ()
  268. {
  269. MetafileHeader header = Metafile.GetMetafileHeader ((string) null);
  270. }
  271. [Test]
  272. public void Static_GetMetafileHeader_Filename ()
  273. {
  274. string filename = getInFile (WmfPlaceable);
  275. MetafileHeader header = Metafile.GetMetafileHeader (filename);
  276. Check_MetafileHeader_WmfPlaceable (header);
  277. }
  278. }
  279. [TestFixture]
  280. public class MetafileFulltrustTest {
  281. private Font test_font;
  282. [TestFixtureSetUp]
  283. public void FixtureSetUp ()
  284. {
  285. try {
  286. test_font = new Font (FontFamily.GenericMonospace, 12);
  287. }
  288. catch (ArgumentException) {
  289. }
  290. }
  291. [Test]
  292. [ExpectedException (typeof (ArgumentException))]
  293. public void Static_GetMetafileHeader_IntPtr_Zero ()
  294. {
  295. Metafile.GetMetafileHeader (IntPtr.Zero);
  296. }
  297. [Test]
  298. [ExpectedException (typeof (ArgumentException))]
  299. public void Static_GetMetafileHeader_IntPtr ()
  300. {
  301. string filename = MetafileTest.getInFile (MetafileTest.WmfPlaceable);
  302. using (Metafile mf = new Metafile (filename)) {
  303. IntPtr hemf = mf.GetHenhmetafile ();
  304. Assert.IsTrue (hemf != IntPtr.Zero, "GetHenhmetafile");
  305. Metafile.GetMetafileHeader (hemf);
  306. }
  307. }
  308. [Test]
  309. [ExpectedException (typeof (ArgumentException))]
  310. public void Metafile_IntPtrBool_Zero ()
  311. {
  312. new Metafile (IntPtr.Zero, false);
  313. }
  314. [Test]
  315. [ExpectedException (typeof (ArgumentException))]
  316. public void Metafile_IntPtrEmfType_Zero ()
  317. {
  318. new Metafile (IntPtr.Zero, EmfType.EmfOnly);
  319. }
  320. private void CheckEmptyHeader (Metafile mf, EmfType type)
  321. {
  322. MetafileHeader mh = mf.GetMetafileHeader ();
  323. Assert.AreEqual (0, mh.Bounds.X, "Bounds.X");
  324. Assert.AreEqual (0, mh.Bounds.Y, "Bounds.Y");
  325. Assert.AreEqual (0, mh.Bounds.Width, "Bounds.Width");
  326. Assert.AreEqual (0, mh.Bounds.Height, "Bounds.Height");
  327. Assert.AreEqual (0, mh.MetafileSize, "MetafileSize");
  328. switch (type) {
  329. case EmfType.EmfOnly:
  330. Assert.AreEqual (MetafileType.Emf, mh.Type, "Type");
  331. break;
  332. case EmfType.EmfPlusDual:
  333. Assert.AreEqual (MetafileType.EmfPlusDual, mh.Type, "Type");
  334. break;
  335. case EmfType.EmfPlusOnly:
  336. Assert.AreEqual (MetafileType.EmfPlusOnly, mh.Type, "Type");
  337. break;
  338. default:
  339. Assert.Fail ("Unknown EmfType '{0}'", type);
  340. break;
  341. }
  342. }
  343. private void Metafile_IntPtrEmfType (EmfType type)
  344. {
  345. using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format32bppArgb)) {
  346. using (Graphics g = Graphics.FromImage (bmp)) {
  347. IntPtr hdc = g.GetHdc ();
  348. try {
  349. Metafile mf = new Metafile (hdc, type);
  350. CheckEmptyHeader (mf, type);
  351. }
  352. finally {
  353. g.ReleaseHdc (hdc);
  354. }
  355. }
  356. }
  357. }
  358. [Test]
  359. [ExpectedException (typeof (ArgumentException))]
  360. public void Metafile_IntPtrEmfType_Invalid ()
  361. {
  362. Metafile_IntPtrEmfType ((EmfType)Int32.MinValue);
  363. }
  364. [Test]
  365. public void Metafile_IntPtrEmfType_EmfOnly ()
  366. {
  367. Metafile_IntPtrEmfType (EmfType.EmfOnly);
  368. }
  369. [Test]
  370. public void Metafile_IntPtrEmfType_EmfPlusDual ()
  371. {
  372. Metafile_IntPtrEmfType (EmfType.EmfPlusDual);
  373. }
  374. [Test]
  375. public void Metafile_IntPtrEmfType_EmfPlusOnly ()
  376. {
  377. Metafile_IntPtrEmfType (EmfType.EmfPlusOnly);
  378. }
  379. [Test]
  380. [ExpectedException (typeof (ArgumentException))]
  381. public void Metafile_IntPtrRectangle_Zero ()
  382. {
  383. new Metafile (IntPtr.Zero, new Rectangle (1, 2, 3, 4));
  384. }
  385. [Test]
  386. public void Metafile_IntPtrRectangle_Empty ()
  387. {
  388. using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format32bppArgb)) {
  389. using (Graphics g = Graphics.FromImage (bmp)) {
  390. IntPtr hdc = g.GetHdc ();
  391. try {
  392. Metafile mf = new Metafile (hdc, new Rectangle ());
  393. CheckEmptyHeader (mf, EmfType.EmfPlusDual);
  394. }
  395. finally {
  396. g.ReleaseHdc (hdc);
  397. }
  398. }
  399. }
  400. }
  401. [Test]
  402. [ExpectedException (typeof (ArgumentException))]
  403. public void Metafile_IntPtrRectangleF_Zero ()
  404. {
  405. new Metafile (IntPtr.Zero, new RectangleF (1, 2, 3, 4));
  406. }
  407. [Test]
  408. public void Metafile_IntPtrRectangleF_Empty ()
  409. {
  410. using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format32bppArgb)) {
  411. using (Graphics g = Graphics.FromImage (bmp)) {
  412. IntPtr hdc = g.GetHdc ();
  413. try {
  414. Metafile mf = new Metafile (hdc, new RectangleF ());
  415. CheckEmptyHeader (mf, EmfType.EmfPlusDual);
  416. }
  417. finally {
  418. g.ReleaseHdc (hdc);
  419. }
  420. }
  421. }
  422. }
  423. private void Metafile_StreamEmfType (Stream stream, EmfType type)
  424. {
  425. using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format32bppArgb)) {
  426. using (Graphics g = Graphics.FromImage (bmp)) {
  427. IntPtr hdc = g.GetHdc ();
  428. try {
  429. Metafile mf = new Metafile (stream, hdc, type);
  430. CheckEmptyHeader (mf, type);
  431. }
  432. finally {
  433. g.ReleaseHdc (hdc);
  434. }
  435. }
  436. }
  437. }
  438. [Test]
  439. [ExpectedException (typeof (NullReferenceException))]
  440. public void Metafile_StreamIntPtrEmfType_Null ()
  441. {
  442. Metafile_StreamEmfType (null, EmfType.EmfOnly);
  443. }
  444. [Test]
  445. public void Metafile_StreamIntPtrEmfType_EmfOnly ()
  446. {
  447. using (MemoryStream ms = new MemoryStream ()) {
  448. Metafile_StreamEmfType (ms, EmfType.EmfOnly);
  449. }
  450. }
  451. [Test]
  452. [ExpectedException (typeof (ArgumentException))]
  453. public void Metafile_StreamIntPtrEmfType_Invalid ()
  454. {
  455. using (MemoryStream ms = new MemoryStream ()) {
  456. Metafile_StreamEmfType (ms, (EmfType)Int32.MinValue);
  457. }
  458. }
  459. private void CreateFilename (EmfType type, bool single)
  460. {
  461. string name = String.Format ("{0}-{1}.emf", type, single ? "Single" : "Multiple");
  462. string filename = Path.Combine (Path.GetTempPath (), name);
  463. Metafile mf;
  464. using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppArgb)) {
  465. using (Graphics g = Graphics.FromImage (bmp)) {
  466. IntPtr hdc = g.GetHdc ();
  467. try {
  468. mf = new Metafile (filename, hdc, type);
  469. Assert.AreEqual (0, new FileInfo (filename).Length, "Empty");
  470. }
  471. finally {
  472. g.ReleaseHdc (hdc);
  473. }
  474. }
  475. long size = 0;
  476. using (Graphics g = Graphics.FromImage (mf)) {
  477. g.FillRectangle (Brushes.BlueViolet, 10, 10, 80, 80);
  478. size = new FileInfo (filename).Length;
  479. Assert.AreEqual (0, size, "Still-Empty");
  480. }
  481. // FIXME / doesn't work on mono yet
  482. // size = new FileInfo (filename).Length;
  483. // Assert.IsTrue (size > 0, "Non-Empty/GraphicsDisposed");
  484. if (!single) {
  485. // can we append stuff ?
  486. using (Graphics g = Graphics.FromImage (mf)) {
  487. g.DrawRectangle (Pens.Azure, 10, 10, 80, 80);
  488. // happily no :)
  489. }
  490. }
  491. mf.Dispose ();
  492. Assert.AreEqual (size, new FileInfo (filename).Length, "Non-Empty/MetafileDisposed");
  493. }
  494. }
  495. [Test]
  496. public void CreateFilename_SingleGraphics_EmfOnly ()
  497. {
  498. CreateFilename (EmfType.EmfOnly, true);
  499. }
  500. [Test]
  501. public void CreateFilename_SingleGraphics_EmfPlusDual ()
  502. {
  503. CreateFilename (EmfType.EmfPlusDual, true);
  504. }
  505. [Test]
  506. public void CreateFilename_SingleGraphics_EmfPlusOnly ()
  507. {
  508. CreateFilename (EmfType.EmfPlusOnly, true);
  509. }
  510. [Test]
  511. [ExpectedException (typeof (OutOfMemoryException))]
  512. public void CreateFilename_MultipleGraphics_EmfOnly ()
  513. {
  514. CreateFilename (EmfType.EmfOnly, false);
  515. }
  516. [Test]
  517. [ExpectedException (typeof (OutOfMemoryException))]
  518. public void CreateFilename_MultipleGraphics_EmfPlusDual ()
  519. {
  520. CreateFilename (EmfType.EmfPlusDual, false);
  521. }
  522. [Test]
  523. [ExpectedException (typeof (OutOfMemoryException))]
  524. public void CreateFilename_MultipleGraphics_EmfPlusOnly ()
  525. {
  526. CreateFilename (EmfType.EmfPlusOnly, false);
  527. }
  528. [Test]
  529. public void Measure ()
  530. {
  531. if (test_font == null)
  532. Assert.Ignore ("No font family could be found.");
  533. Metafile mf;
  534. using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppArgb)) {
  535. using (Graphics g = Graphics.FromImage (bmp)) {
  536. IntPtr hdc = g.GetHdc ();
  537. try {
  538. mf = new Metafile (hdc, EmfType.EmfPlusOnly);
  539. }
  540. finally {
  541. g.ReleaseHdc (hdc);
  542. }
  543. }
  544. using (Graphics g = Graphics.FromImage (mf)) {
  545. string text = "this\nis a test";
  546. CharacterRange[] ranges = new CharacterRange[2];
  547. ranges[0] = new CharacterRange (0, 5);
  548. ranges[1] = new CharacterRange (5, 9);
  549. SizeF size = g.MeasureString (text, test_font);
  550. Assert.IsFalse (size.IsEmpty, "MeasureString");
  551. StringFormat sf = new StringFormat ();
  552. sf.FormatFlags = StringFormatFlags.NoClip;
  553. sf.SetMeasurableCharacterRanges (ranges);
  554. RectangleF rect = new RectangleF (0, 0, size.Width, size.Height);
  555. Region[] region = g.MeasureCharacterRanges (text, test_font, rect, sf);
  556. Assert.AreEqual (2, region.Length, "MeasureCharacterRanges");
  557. }
  558. mf.Dispose ();
  559. }
  560. }
  561. [Test]
  562. public void WorldTransforms ()
  563. {
  564. Metafile mf;
  565. using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppArgb)) {
  566. using (Graphics g = Graphics.FromImage (bmp)) {
  567. IntPtr hdc = g.GetHdc ();
  568. try {
  569. mf = new Metafile (hdc, EmfType.EmfPlusOnly);
  570. }
  571. finally {
  572. g.ReleaseHdc (hdc);
  573. }
  574. }
  575. using (Graphics g = Graphics.FromImage (mf)) {
  576. Assert.IsTrue (g.Transform.IsIdentity, "Initial/IsIdentity");
  577. g.ScaleTransform (2f, 0.5f);
  578. Assert.IsFalse (g.Transform.IsIdentity, "Scale/IsIdentity");
  579. g.RotateTransform (90);
  580. g.TranslateTransform (-2, 2);
  581. Matrix m = g.Transform;
  582. g.MultiplyTransform (m);
  583. // check
  584. float[] elements = g.Transform.Elements;
  585. Assert.AreEqual (-1f, elements[0], 0.00001f, "a0");
  586. Assert.AreEqual (0f, elements[1], 0.00001f, "a1");
  587. Assert.AreEqual (0f, elements[2], 0.00001f, "a2");
  588. Assert.AreEqual (-1f, elements[3], 0.00001f, "a3");
  589. Assert.AreEqual (-2f, elements[4], 0.00001f, "a4");
  590. Assert.AreEqual (-3f, elements[5], 0.00001f, "a5");
  591. g.Transform = m;
  592. elements = g.Transform.Elements;
  593. Assert.AreEqual (0f, elements[0], 0.00001f, "b0");
  594. Assert.AreEqual (0.5f, elements[1], 0.00001f, "b1");
  595. Assert.AreEqual (-2f, elements[2], 0.00001f, "b2");
  596. Assert.AreEqual (0f, elements[3], 0.00001f, "b3");
  597. Assert.AreEqual (-4f, elements[4], 0.00001f, "b4");
  598. Assert.AreEqual (-1f, elements[5], 0.00001f, "b5");
  599. g.ResetTransform ();
  600. Assert.IsTrue (g.Transform.IsIdentity, "Reset/IsIdentity");
  601. }
  602. mf.Dispose ();
  603. }
  604. }
  605. }
  606. }