PrivateFontCollection.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // System.Drawing.Text.PrivateFontCollection.cs
  3. //
  4. // (C) 2002 Ximian, Inc. http://www.ximian.com
  5. // Author: Everaldo Canuto [email protected]
  6. // Sanjay Gupta ([email protected])
  7. // Peter Dennis Bartok ([email protected])
  8. //
  9. //
  10. // Copyright (C) 2004 - 2006 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.IO;
  32. using System.Security.Permissions;
  33. using System.Runtime.InteropServices;
  34. namespace System.Drawing.Text {
  35. #if !NET_2_0
  36. [ComVisible(false)]
  37. #endif
  38. public sealed class PrivateFontCollection : FontCollection {
  39. // constructors
  40. public PrivateFontCollection ()
  41. {
  42. Status status = GDIPlus.GdipNewPrivateFontCollection (out nativeFontCollection);
  43. GDIPlus.CheckStatus (status);
  44. }
  45. // methods
  46. public void AddFontFile (string filename)
  47. {
  48. if (filename == null)
  49. throw new ArgumentNullException ("filename");
  50. // this ensure the filename is valid (or throw the correct exception)
  51. string fname = Path.GetFullPath (filename);
  52. if (!File.Exists (fname))
  53. throw new FileNotFoundException ();
  54. // note: MS throw the same exception FileNotFoundException if the file exists but isn't a valid font file
  55. Status status = GDIPlus.GdipPrivateAddFontFile (nativeFontCollection, fname);
  56. GDIPlus.CheckStatus (status);
  57. }
  58. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  59. public void AddMemoryFont (IntPtr memory, int length)
  60. {
  61. // note: MS throw FileNotFoundException if something is bad with the data (except for a null pointer)
  62. Status status = GDIPlus.GdipPrivateAddMemoryFont (nativeFontCollection, memory, length);
  63. GDIPlus.CheckStatus (status);
  64. }
  65. // methods
  66. protected override void Dispose (bool disposing)
  67. {
  68. if (nativeFontCollection!=IntPtr.Zero) {
  69. GDIPlus.GdipDeletePrivateFontCollection (ref nativeFontCollection);
  70. // This must be zeroed out, otherwise our base will also call
  71. // the GDI+ delete method on unix platforms. We're keeping the
  72. // base.Dispose() call in case other cleanup ever gets added there
  73. nativeFontCollection = IntPtr.Zero;
  74. }
  75. base.Dispose (disposing);
  76. }
  77. }
  78. }