Changed UploadStringAsync to handle UploadString encapsulated exceptions.
[mono.git] / mcs / class / Mono.Cairo / Mono.Cairo / Context.cs
1 //
2 // Mono.Cairo.Context.cs
3 //
4 // Author:
5 //   Duncan Mak (duncan@ximian.com)
6 //   Miguel de Icaza (miguel@novell.com)
7 //   Hisham Mardam Bey (hisham.mardambey@gmail.com)
8 //   Alp Toker (alp@atoker.com)
9 //
10 // (C) Ximian Inc, 2003.
11 // (C) Novell Inc, 2003.
12 //
13 // This is an OO wrapper API for the Cairo API.
14 //
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 //
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 //
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 //
36
37 using System;
38 using System.Runtime.InteropServices;
39 using System.Text;
40 using Cairo;
41
42 namespace Cairo {
43
44         [Obsolete ("Renamed Cairo.Context per suggestion from cairo binding guidelines.")]
45         public class Graphics : Context {
46                 public Graphics (IntPtr state) : base (state) {}
47                 public Graphics (Surface surface) : base (surface) {}
48         }
49
50         public class Context : IDisposable
51         {
52                 IntPtr handle = IntPtr.Zero;
53
54                 static int native_glyph_size, c_compiler_long_size;
55
56                 static Context ()
57                 {
58                         //
59                         // This is used to determine what kind of structure
60                         // we should use to marshal Glyphs, as the public
61                         // definition in Cairo uses `long', which can be
62                         // 32 bits or 64 bits depending on the platform.
63                         //
64                         // We assume that sizeof(long) == sizeof(void*)
65                         // except in the case of Win64 where sizeof(long)
66                         // is 32 bits
67                         //
68                         int ptr_size = Marshal.SizeOf (typeof (IntPtr));
69
70                         PlatformID platform = Environment.OSVersion.Platform;
71                         if (platform == PlatformID.Win32NT ||
72                             platform == PlatformID.Win32S ||
73                             platform == PlatformID.Win32Windows ||
74                             platform == PlatformID.WinCE ||
75                             ptr_size == 4){
76                                 c_compiler_long_size = 4;
77                                 native_glyph_size = Marshal.SizeOf (typeof (NativeGlyph_4byte_longs));
78                         } else {
79                                 c_compiler_long_size = 8;
80                                 native_glyph_size = Marshal.SizeOf (typeof (Glyph));
81                         }
82                 }
83
84                 public Context (Surface surface) : this (NativeMethods.cairo_create (surface.Handle), true)
85                 {
86                 }
87
88
89                 public Context (IntPtr handle, bool owner)
90                 {
91                         this.handle = handle;
92                         if (!owner)
93                                 NativeMethods.cairo_reference (handle);
94                         if (CairoDebug.Enabled)
95                                 CairoDebug.OnAllocated (handle);
96                 }
97
98                 [Obsolete]
99                 public Context (IntPtr state) : this (state, true)
100                 {
101                 }
102
103                 ~Context ()
104                 {
105                         Dispose (false);
106                 }
107
108                 public void Dispose ()
109                 {
110                         Dispose (true);
111                         GC.SuppressFinalize (this);
112                 }
113
114                 protected virtual void Dispose (bool disposing)
115                 {
116                         if (!disposing || CairoDebug.Enabled)
117                                 CairoDebug.OnDisposed<Context> (handle, disposing);
118
119                         if (!disposing|| handle == IntPtr.Zero)
120                                 return;
121
122                         NativeMethods.cairo_destroy (handle);
123                         handle = IntPtr.Zero;
124
125                 }
126
127                 public void Save ()
128                 {
129                         NativeMethods.cairo_save (handle);
130                 }
131
132                 public void Restore ()
133                 {
134                         NativeMethods.cairo_restore (handle);
135                 }
136
137                 public Antialias Antialias {
138                         get { return NativeMethods.cairo_get_antialias (handle); }
139                         set { NativeMethods.cairo_set_antialias (handle, value); }
140                 }
141
142                 public Cairo.Status Status {
143                         get {
144                                 return NativeMethods.cairo_status (handle);
145                         }
146                 }
147
148                 public IntPtr Handle {
149                         get {
150                                 return handle;
151                         }
152                 }
153
154                 public Operator Operator {
155                         set {
156                                 NativeMethods.cairo_set_operator (handle, value);
157                         }
158
159                         get {
160                                 return NativeMethods.cairo_get_operator (handle);
161                         }
162                 }
163
164                 [Obsolete ("Use SetSourceRGBA method")]
165                 public Color Color {
166                         set {
167                                 NativeMethods.cairo_set_source_rgba (handle, value.R, value.G, value.B, value.A);
168                         }
169                 }
170
171                 [Obsolete ("Use SetSourceRGBA method")]
172                 public Cairo.Color ColorRgb {
173                         set {
174                                 Color = new Color (value.R, value.G, value.B);
175                         }
176                 }
177
178                 public double Tolerance {
179                         get {
180                                 return NativeMethods.cairo_get_tolerance (handle);
181                         }
182
183                         set {
184                                 NativeMethods.cairo_set_tolerance (handle, value);
185                         }
186                 }
187
188                 public Cairo.FillRule FillRule {
189                         set {
190                                 NativeMethods.cairo_set_fill_rule (handle, value);
191                         }
192
193                         get {
194                                 return NativeMethods.cairo_get_fill_rule (handle);
195                         }
196                 }
197
198                 public double LineWidth {
199                         set {
200                                 NativeMethods.cairo_set_line_width (handle, value);
201                         }
202
203                         get {
204                                 return NativeMethods.cairo_get_line_width (handle);
205                         }
206                 }
207
208                 public Cairo.LineCap LineCap {
209                         set {
210                                 NativeMethods.cairo_set_line_cap (handle, value);
211                         }
212
213                         get {
214                                 return NativeMethods.cairo_get_line_cap (handle);
215                         }
216                 }
217
218                 public Cairo.LineJoin LineJoin {
219                         set {
220                                 NativeMethods.cairo_set_line_join (handle, value);
221                         }
222
223                         get {
224                                 return NativeMethods.cairo_get_line_join (handle);
225                         }
226                 }
227
228                 public void SetDash (double [] dashes, double offset)
229                 {
230                         NativeMethods.cairo_set_dash (handle, dashes, dashes.Length, offset);
231                 }
232
233                 [Obsolete("Use GetSource/GetSource")]
234                 public Pattern Pattern {
235                         set {
236                                 SetSource (value);
237                         }
238                         get {
239                                 return GetSource ();
240                         }
241                 }
242
243                 //This is obsolete because it wasn't obvious it needed to be disposed
244                 [Obsolete("Use GetSource/GetSource")]
245                 public Pattern Source {
246                         set {
247                                 SetSource (value);
248                         }
249                         get {
250                                 return GetSource ();
251                         }
252                 }
253
254                 public void SetSource (Pattern source)
255                 {
256                         NativeMethods.cairo_set_source (handle, source.Handle);
257                 }
258
259                 public Pattern GetSource ()
260                 {
261                         var ptr = NativeMethods.cairo_get_source (handle);
262                         return Cairo.Pattern.Lookup (ptr, false);
263                 }
264
265                 public double MiterLimit {
266                         set {
267                                 NativeMethods.cairo_set_miter_limit (handle, value);
268                         }
269
270                         get {
271                                 return NativeMethods.cairo_get_miter_limit (handle);
272                         }
273                 }
274
275                 public PointD CurrentPoint {
276                         get {
277                                 double x, y;
278                                 NativeMethods.cairo_get_current_point (handle, out x, out y);
279                                 return new PointD (x, y);
280                         }
281                 }
282
283                 [Obsolete ("Use GetTarget/SetTarget")]
284                 public Cairo.Surface Target {
285                         set {
286                                 if (handle != IntPtr.Zero)
287                                         NativeMethods.cairo_destroy (handle);
288
289                                 handle = NativeMethods.cairo_create (value.Handle);
290                         }
291
292                         get {
293                                 return GetTarget ();
294                         }
295                 }
296
297                 public Surface GetTarget ()
298                 {
299                         return Surface.Lookup (NativeMethods.cairo_get_target (handle), false);
300                 }
301
302                 public void SetTarget (Surface target)
303                 {
304                         if (handle != IntPtr.Zero)
305                                 NativeMethods.cairo_destroy (handle);
306                         handle = NativeMethods.cairo_create (target.Handle);
307                 }
308
309                 [Obsolete("Use GetScaledFont/SetScaledFont")]
310                 public ScaledFont ScaledFont {
311                         set {
312                                 SetScaledFont (value);
313                         }
314
315                         get {
316                                 return GetScaledFont ();
317                         }
318                 }
319
320                 public ScaledFont GetScaledFont ()
321                 {
322                         return new ScaledFont (NativeMethods.cairo_get_scaled_font (handle), false);
323                 }
324
325                 public void SetScaledFont (ScaledFont font)
326                 {
327                         NativeMethods.cairo_set_scaled_font (handle, font.Handle);
328                 }
329
330                 public uint ReferenceCount {
331                         get { return NativeMethods.cairo_get_reference_count (handle); }
332                 }
333
334                 public void SetSourceRGB (double r, double g, double b)
335                 {
336                         NativeMethods.cairo_set_source_rgb (handle, r, g, b);
337                 }
338
339                 public void SetSourceRGBA (double r, double g, double b, double a)
340                 {
341                         NativeMethods.cairo_set_source_rgba (handle, r, g, b, a);
342                 }
343
344                 //[Obsolete ("Use SetSource method (with double parameters)")]
345                 public void SetSourceSurface (Surface source, int x, int y)
346                 {
347                         NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
348                 }
349
350                 public void SetSource (Surface source, double x, double y)
351                 {
352                         NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
353                 }
354
355                 public void SetSource (Surface source)
356                 {
357                         NativeMethods.cairo_set_source_surface (handle, source.Handle, 0, 0);
358                 }
359
360 #region Path methods
361
362                 public void NewPath ()
363                 {
364                         NativeMethods.cairo_new_path (handle);
365                 }
366
367                 public void NewSubPath ()
368                 {
369                         NativeMethods.cairo_new_sub_path (handle);
370                 }
371
372                 public void MoveTo (PointD p)
373                 {
374                         MoveTo (p.X, p.Y);
375                 }
376
377                 public void MoveTo (double x, double y)
378                 {
379                         NativeMethods.cairo_move_to (handle, x, y);
380                 }
381
382                 public void LineTo (PointD p)
383                 {
384                         LineTo (p.X, p.Y);
385                 }
386
387                 public void LineTo (double x, double y)
388                 {
389                         NativeMethods.cairo_line_to (handle, x, y);
390                 }
391
392                 public void CurveTo (PointD p1, PointD p2, PointD p3)
393                 {
394                         CurveTo (p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
395                 }
396
397                 public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3)
398                 {
399                         NativeMethods.cairo_curve_to (handle, x1, y1, x2, y2, x3, y3);
400                 }
401
402                 public void RelMoveTo (Distance d)
403                 {
404                         RelMoveTo (d.Dx, d.Dy);
405                 }
406
407                 public void RelMoveTo (double dx, double dy)
408                 {
409                         NativeMethods.cairo_rel_move_to (handle, dx, dy);
410                 }
411
412                 public void RelLineTo (Distance d)
413                 {
414                         RelLineTo (d.Dx, d.Dy);
415                 }
416
417                 public void RelLineTo (double dx, double dy)
418                 {
419                         NativeMethods.cairo_rel_line_to (handle, dx, dy);
420                 }
421
422                 public void RelCurveTo (Distance d1, Distance d2, Distance d3)
423                 {
424                         RelCurveTo (d1.Dx, d1.Dy, d2.Dx, d2.Dy, d3.Dx, d3.Dy);
425                 }
426
427                 public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
428                 {
429                         NativeMethods.cairo_rel_curve_to (handle, dx1, dy1, dx2, dy2, dx3, dy3);
430                 }
431
432                 public void Arc (double xc, double yc, double radius, double angle1, double angle2)
433                 {
434                         NativeMethods.cairo_arc (handle, xc, yc, radius, angle1, angle2);
435                 }
436
437                 public void ArcNegative (double xc, double yc, double radius, double angle1, double angle2)
438                 {
439                         NativeMethods.cairo_arc_negative (handle, xc, yc, radius, angle1, angle2);
440                 }
441
442                 public void Rectangle (Rectangle rectangle)
443                 {
444                         Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
445                 }
446
447                 public void Rectangle (PointD p, double width, double height)
448                 {
449                         Rectangle (p.X, p.Y, width, height);
450                 }
451
452                 public void Rectangle (double x, double y, double width, double height)
453                 {
454                         NativeMethods.cairo_rectangle (handle, x, y, width, height);
455                 }
456
457                 public void ClosePath ()
458                 {
459                         NativeMethods.cairo_close_path (handle);
460                 }
461
462                 public Path CopyPath ()
463                 {
464                         return new Path (NativeMethods.cairo_copy_path (handle));
465                 }
466
467                 public Path CopyPathFlat ()
468                 {
469                         return new Path (NativeMethods.cairo_copy_path_flat (handle));
470                 }
471
472                 public void AppendPath (Path path)
473                 {
474                         NativeMethods.cairo_append_path (handle, path.Handle);
475                 }
476
477 #endregion
478
479 #region Painting Methods
480                 public void Paint ()
481                 {
482                         NativeMethods.cairo_paint (handle);
483                 }
484
485                 public void PaintWithAlpha (double alpha)
486                 {
487                         NativeMethods.cairo_paint_with_alpha (handle, alpha);
488                 }
489
490                 public void Mask (Pattern pattern)
491                 {
492                         NativeMethods.cairo_mask (handle, pattern.Handle);
493                 }
494
495                 public void MaskSurface (Surface surface, double surface_x, double surface_y)
496                 {
497                         NativeMethods.cairo_mask_surface (handle, surface.Handle, surface_x, surface_y);
498                 }
499
500                 public void Stroke ()
501                 {
502                         NativeMethods.cairo_stroke (handle);
503                 }
504
505                 public void StrokePreserve ()
506                 {
507                         NativeMethods.cairo_stroke_preserve (handle);
508                 }
509
510                 public Rectangle StrokeExtents ()
511                 {
512                         double x1, y1, x2, y2;
513                         NativeMethods.cairo_stroke_extents (handle, out x1, out y1, out x2, out y2);
514                         return new Rectangle (x1, y1, x2 - x1, y2 - y1);
515                 }
516
517                 public void Fill ()
518                 {
519                         NativeMethods.cairo_fill (handle);
520                 }
521
522                 public Rectangle FillExtents ()
523                 {
524                         double x1, y1, x2, y2;
525                         NativeMethods.cairo_fill_extents (handle, out x1, out y1, out x2, out y2);
526                         return new Rectangle (x1, y1, x2 - x1, y2 - y1);
527                 }
528
529                 public void FillPreserve ()
530                 {
531                         NativeMethods.cairo_fill_preserve (handle);
532                 }
533
534 #endregion
535
536                 public void Clip ()
537                 {
538                         NativeMethods.cairo_clip (handle);
539                 }
540
541                 public void ClipPreserve ()
542                 {
543                         NativeMethods.cairo_clip_preserve (handle);
544                 }
545
546                 public void ResetClip ()
547                 {
548                         NativeMethods.cairo_reset_clip (handle);
549                 }
550
551                 public bool InStroke (double x, double y)
552                 {
553                         return NativeMethods.cairo_in_stroke (handle, x, y);
554                 }
555
556                 public bool InFill (double x, double y)
557                 {
558                         return NativeMethods.cairo_in_fill (handle, x, y);
559                 }
560
561                 public Pattern PopGroup ()
562                 {
563                         return Pattern.Lookup (NativeMethods.cairo_pop_group (handle), true);
564                 }
565
566                 public void PopGroupToSource ()
567                 {
568                         NativeMethods.cairo_pop_group_to_source (handle);
569                 }
570
571                 public void PushGroup ()
572                 {
573                         NativeMethods.cairo_push_group (handle);
574                 }
575
576                 public void PushGroup (Content content)
577                 {
578                         NativeMethods.cairo_push_group_with_content (handle, content);
579                 }
580
581                 [Obsolete ("Use GetGroupTarget()")]
582                 public Surface GroupTarget {
583                         get {
584                                 return GetGroupTarget ();
585                         }
586                 }
587
588                 public Surface GetGroupTarget ()
589                 {
590                         IntPtr surface = NativeMethods.cairo_get_group_target (handle);
591                         return Surface.Lookup (surface, false);
592                 }
593
594                 public void Rotate (double angle)
595                 {
596                         NativeMethods.cairo_rotate (handle, angle);
597                 }
598
599                 public void Scale (double sx, double sy)
600                 {
601                         NativeMethods.cairo_scale (handle, sx, sy);
602                 }
603
604                 public void Translate (double tx, double ty)
605                 {
606                         NativeMethods.cairo_translate (handle, tx, ty);
607                 }
608
609                 public void Transform (Matrix m)
610                 {
611                         NativeMethods.cairo_transform (handle, m);
612                 }
613
614                 [Obsolete("Use UserToDevice instead")]
615                 public void TransformPoint (ref double x, ref double y)
616                 {
617                         NativeMethods.cairo_user_to_device (handle, ref x, ref y);
618                 }
619
620                 [Obsolete("Use UserToDeviceDistance instead")]
621                 public void TransformDistance (ref double dx, ref double dy)
622                 {
623                         NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy);
624                 }
625
626                 [Obsolete("Use InverseTransformPoint instead")]
627                 public void InverseTransformPoint (ref double x, ref double y)
628                 {
629                         NativeMethods.cairo_device_to_user (handle, ref x, ref y);
630                 }
631
632                 [Obsolete("Use DeviceToUserDistance instead")]
633                 public void InverseTransformDistance (ref double dx, ref double dy)
634                 {
635                         NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy);
636                 }
637
638                 public void UserToDevice (ref double x, ref double y)
639                 {
640                         NativeMethods.cairo_user_to_device (handle, ref x, ref y);
641                 }
642
643                 public void UserToDeviceDistance (ref double dx, ref double dy)
644                 {
645                         NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy);
646                 }
647
648                 public void DeviceToUser (ref double x, ref double y)
649                 {
650                         NativeMethods.cairo_device_to_user (handle, ref x, ref y);
651                 }
652
653                 public void DeviceToUserDistance (ref double dx, ref double dy)
654                 {
655                         NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy);
656                 }
657
658                 public Matrix Matrix {
659                         set {
660                                 NativeMethods.cairo_set_matrix (handle, value);
661                         }
662
663                         get {
664                                 Matrix m = new Matrix();
665                                 NativeMethods.cairo_get_matrix (handle, m);
666                                 return m;
667                         }
668                 }
669
670                 public void SetFontSize (double scale)
671                 {
672                         NativeMethods.cairo_set_font_size (handle, scale);
673                 }
674
675                 public void IdentityMatrix ()
676                 {
677                         NativeMethods.cairo_identity_matrix (handle);
678                 }
679
680                 [Obsolete ("Use SetFontSize() instead.")]
681                 public void FontSetSize (double scale)
682                 {
683                         SetFontSize (scale);
684                 }
685
686                 [Obsolete ("Use SetFontSize() instead.")]
687                 public double FontSize {
688                         set { SetFontSize (value); }
689                 }
690
691                 public Matrix FontMatrix {
692                         get {
693                                 Matrix m;
694                                 NativeMethods.cairo_get_font_matrix (handle, out m);
695                                 return m;
696                         }
697                         set { NativeMethods.cairo_set_font_matrix (handle, value); }
698                 }
699
700                 public FontOptions FontOptions {
701                         get {
702                                 FontOptions options = new FontOptions ();
703                                 NativeMethods.cairo_get_font_options (handle, options.Handle);
704                                 return options;
705                         }
706                         set { NativeMethods.cairo_set_font_options (handle, value.Handle); }
707                 }
708
709                 [StructLayout(LayoutKind.Sequential)]
710                 internal struct NativeGlyph_4byte_longs {
711                         public int index;
712                         public double x;
713                         public double y;
714
715                         public NativeGlyph_4byte_longs (Glyph source)
716                         {
717                                 index = (int) source.index;
718                                 x = source.x;
719                                 y = source.y;
720                         }
721                 }
722
723                 static internal IntPtr FromGlyphToUnManagedMemory(Glyph [] glyphs)
724                 {
725                         IntPtr dest = Marshal.AllocHGlobal (native_glyph_size * glyphs.Length);
726                         long pos = dest.ToInt64();
727
728                         if (c_compiler_long_size == 8){
729                                 foreach (Glyph g in glyphs){
730                                         Marshal.StructureToPtr (g, (IntPtr)pos, false);
731                                         pos += native_glyph_size;
732                                 }
733                         } else {
734                                 foreach (Glyph g in glyphs){
735                                         NativeGlyph_4byte_longs n = new NativeGlyph_4byte_longs (g);
736
737                                         Marshal.StructureToPtr (n, (IntPtr)pos, false);
738                                         pos += native_glyph_size;
739                                 }
740                         }
741
742                         return dest;
743                 }
744
745                 public void ShowGlyphs (Glyph[] glyphs)
746                 {
747                         IntPtr ptr;
748
749                         ptr = FromGlyphToUnManagedMemory (glyphs);
750
751                         NativeMethods.cairo_show_glyphs (handle, ptr, glyphs.Length);
752
753                         Marshal.FreeHGlobal (ptr);
754                 }
755
756                 [Obsolete("The matrix argument was never used, use ShowGlyphs(Glyphs []) instead")]
757                 public void ShowGlyphs (Matrix matrix, Glyph[] glyphs)
758                 {
759                         ShowGlyphs (glyphs);
760                 }
761
762                 [Obsolete("The matrix argument was never used, use GlyphPath(Glyphs []) instead")]
763                 public void GlyphPath (Matrix matrix, Glyph[] glyphs)
764                 {
765                         GlyphPath (glyphs);
766                 }
767
768                 public void GlyphPath (Glyph[] glyphs)
769                 {
770                         IntPtr ptr;
771
772                         ptr = FromGlyphToUnManagedMemory (glyphs);
773
774                         NativeMethods.cairo_glyph_path (handle, ptr, glyphs.Length);
775
776                         Marshal.FreeHGlobal (ptr);
777
778                 }
779
780                 public FontExtents FontExtents {
781                         get {
782                                 FontExtents f_extents;
783                                 NativeMethods.cairo_font_extents (handle, out f_extents);
784                                 return f_extents;
785                         }
786                 }
787
788                 public void CopyPage ()
789                 {
790                         NativeMethods.cairo_copy_page (handle);
791                 }
792
793                 [Obsolete ("Use SelectFontFace() instead.")]
794                 public void FontFace (string family, FontSlant slant, FontWeight weight)
795                 {
796                         SelectFontFace (family, slant, weight);
797                 }
798
799                 [Obsolete("Use GetFontFace/SetFontFace")]
800                 public FontFace ContextFontFace {
801                         get {
802                                 return GetContextFontFace ();
803                         }
804                         set {
805                                 SetContextFontFace (value);
806                         }
807                 }
808
809                 public FontFace GetContextFontFace ()
810                 {
811                         return Cairo.FontFace.Lookup (NativeMethods.cairo_get_font_face (handle), false);
812                 }
813
814                 public void SetContextFontFace (FontFace value)
815                 {
816                         NativeMethods.cairo_set_font_face (handle, value == null ? IntPtr.Zero : value.Handle);
817                 }
818
819                 public void SelectFontFace (string family, FontSlant slant, FontWeight weight)
820                 {
821                         NativeMethods.cairo_select_font_face (handle, family, slant, weight);
822                 }
823
824                 public void ShowPage ()
825                 {
826                         NativeMethods.cairo_show_page (handle);
827                 }
828
829                 private static byte[] TerminateUtf8(byte[] utf8)
830                 {
831                         if (utf8.Length > 0 && utf8[utf8.Length - 1] == 0)
832                                 return utf8;
833                         var termedArray = new byte[utf8.Length + 1];
834                         Array.Copy(utf8, termedArray, utf8.Length);
835                         termedArray[utf8.Length] = 0;
836                         return termedArray;
837                 }
838
839                 private static byte[] TerminateUtf8(string s)
840                 {
841                         // compute the byte count including the trailing \0
842                         var byteCount = Encoding.UTF8.GetMaxByteCount(s.Length + 1);
843                         var bytes = new byte[byteCount];
844                         Encoding.UTF8.GetBytes(s, 0, s.Length, bytes, 0);
845                         return bytes;
846                 }
847
848                 public void ShowText(string str)
849                 {
850                         NativeMethods.cairo_show_text (handle, TerminateUtf8(str));
851                 }
852
853                 public void ShowText(byte[] utf8)
854                 {
855                         NativeMethods.cairo_show_text (handle, TerminateUtf8(utf8));
856                 }
857
858                 public void TextPath(string str)
859                 {
860                         NativeMethods.cairo_text_path (handle, TerminateUtf8(str));
861                 }
862
863                 public void TextPath(byte[] utf8)
864                 {
865                         NativeMethods.cairo_text_path (handle, TerminateUtf8(utf8));
866                 }
867
868                 public TextExtents TextExtents(string s)
869                 {
870                         TextExtents extents;
871                         NativeMethods.cairo_text_extents (handle, TerminateUtf8(s), out extents);
872                         return extents;
873                 }
874
875                 public TextExtents TextExtents(byte[] utf8)
876                 {
877                         TextExtents extents;
878                         NativeMethods.cairo_text_extents (handle, TerminateUtf8(utf8), out extents);
879                         return extents;
880                 }
881
882                 public TextExtents GlyphExtents (Glyph[] glyphs)
883                 {
884                         IntPtr ptr = FromGlyphToUnManagedMemory (glyphs);
885
886                         TextExtents extents;
887
888                         NativeMethods.cairo_glyph_extents (handle, ptr, glyphs.Length, out extents);
889
890                         Marshal.FreeHGlobal (ptr);
891
892                         return extents;
893                 }
894         }
895 }