FontCollection.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // System.Drawing.Text.FontCollection.cs
  3. //
  4. // (C) 2002 Ximian, Inc. http://www.ximian.com
  5. // Author: Everaldo Canuto [email protected]
  6. // Sanjay Gupta ([email protected])
  7. //
  8. using System;
  9. using System.Drawing;
  10. using System.Runtime.InteropServices;
  11. namespace System.Drawing.Text {
  12. public abstract class FontCollection : IDisposable {
  13. internal IntPtr nativeFontCollection = IntPtr.Zero;
  14. internal FontFamily[] families;
  15. internal FontCollection ()
  16. {
  17. }
  18. internal FontCollection ( IntPtr ptr )
  19. {
  20. nativeFontCollection = ptr;
  21. }
  22. // methods
  23. public void Dispose()
  24. {
  25. //Dispose ( true );
  26. if ( families != null ) {
  27. int length = families.Length;
  28. Status status;
  29. for ( int i = 0; i < length; i++){
  30. status = GDIPlus.GdipDeleteFontFamily ( families[i].NativeObject );
  31. if ( status != Status.Ok )
  32. families[i].NativeObject = IntPtr.Zero;
  33. }
  34. }
  35. System.GC.SuppressFinalize ( this );
  36. }
  37. [MonoTODO]
  38. protected virtual void Dispose ( bool disposing )
  39. {
  40. //Nothing for now
  41. }
  42. // properties
  43. public FontFamily[] Families
  44. {
  45. get {
  46. int found;
  47. int returned;
  48. Status status;
  49. status = GDIPlus.GdipGetFontCollectionFamilyCount ( nativeFontCollection, out found );
  50. if ( status != Status.Ok ) {
  51. throw new Exception ( "Error calling GDIPlus.GdipGetFontCollectionFamilyCount: " + status );
  52. }
  53. int nSize = Marshal.SizeOf ( IntPtr.Zero );
  54. IntPtr dest = Marshal.AllocHGlobal ( nSize * found );
  55. status = GDIPlus.GdipGetFontCollectionFamilyList( nativeFontCollection, found, dest, out returned );
  56. if ( status != Status.Ok ) {
  57. throw new Exception ( "Error calling GDIPlus.GdipGetFontCollectionFamilyList: " + status );
  58. }
  59. IntPtr[] ptrAr = new IntPtr [ returned ];
  60. int pos = dest.ToInt32 ();
  61. for ( int i = 0; i < returned ; i++, pos+=nSize )
  62. ptrAr[i] = (IntPtr)Marshal.PtrToStructure ( (IntPtr)pos, typeof(IntPtr) );
  63. Marshal.FreeHGlobal ( dest );
  64. families = new FontFamily [ returned ];
  65. for ( int i = 0; i < returned; i++ )
  66. families[i] = new FontFamily ( ptrAr[i] );
  67. return families;
  68. }
  69. }
  70. ~FontCollection()
  71. {
  72. Dispose ( false );
  73. }
  74. }
  75. }