[Mono.Cairo] Add mechanism to debug missing Dispose calls
[mono.git] / mcs / class / Mono.Cairo / Mono.Cairo / ScaledFont.cs
1 //
2 // Mono.Cairo.ScaledFont.cs
3 //
4 // (c) 2008 Jordi Mas i Hernandez (jordimash@gmail.com)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 // 
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 // 
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25
26 using System;
27 using System.Runtime.InteropServices;
28
29 namespace Cairo {
30    
31         public class ScaledFont : IDisposable
32         {
33                 protected IntPtr handle = IntPtr.Zero;
34
35                 internal ScaledFont (IntPtr handle)
36                 {
37                         this.handle = handle;
38                         if (CairoDebug.Enabled)
39                                 CairoDebug.OnAllocated (handle);
40                 }
41
42                 public ScaledFont (FontFace fontFace, Matrix matrix, Matrix ctm, FontOptions options)
43                         : this (NativeMethods.cairo_scaled_font_create (fontFace.Handle, matrix, ctm, options.Handle))
44                 {
45                 }
46
47                 ~ScaledFont ()
48                 {
49                         Dispose (false);
50                 }
51
52                 public IntPtr Handle {
53                         get {
54                                 return handle;
55                         }
56                 }
57
58                 public FontExtents FontExtents {
59                         get {
60                                 FontExtents extents;
61                                 NativeMethods.cairo_scaled_font_extents (handle, out extents);
62                                 return extents;
63                         }
64                 }
65
66                 public Matrix FontMatrix {
67                         get {
68                                 Matrix m;
69                                 NativeMethods.cairo_scaled_font_get_font_matrix (handle, out m);
70                                 return m;
71                         }
72                 }
73
74                 public FontType FontType {
75                         get {
76                                 return NativeMethods.cairo_scaled_font_get_type (handle);
77                         }
78                 }
79
80                 public TextExtents GlyphExtents (Glyph[] glyphs)
81                 {
82                         IntPtr ptr = Context.FromGlyphToUnManagedMemory (glyphs);
83                         TextExtents extents;
84
85                         NativeMethods.cairo_scaled_font_glyph_extents (handle, ptr, glyphs.Length, out extents);
86
87                         Marshal.FreeHGlobal (ptr);
88                         return extents;
89                 }
90         
91                 public Status Status
92                 {
93                         get { return NativeMethods.cairo_scaled_font_status (handle); }
94                 }
95
96                 public void Dispose ()
97                 {
98                         Dispose (true);
99                         GC.SuppressFinalize (this);
100                 }
101
102                 protected virtual void Dispose (bool disposing)
103                 {
104                         if (!disposing || CairoDebug.Enabled)
105                                 CairoDebug.OnDisposed<ScaledFont> (handle, disposing);
106
107                         if (!disposing|| handle == IntPtr.Zero)
108                                 return;
109
110                         NativeMethods.cairo_scaled_font_destroy (handle);
111                         handle = IntPtr.Zero;
112                 }
113                 
114                 protected void Reference ()
115                 {
116                         NativeMethods.cairo_scaled_font_reference (handle);
117                 }
118         }
119 }
120