699c079cf2df242e03356611603f8cdabcca94fa
[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 Source")]
234                 public Pattern Pattern {
235                         set {
236                                 Source = value;
237                         }
238
239                         get {
240                                 return Source;
241                         }
242                 }
243
244                 public Pattern Source {
245                         set {
246                                 NativeMethods.cairo_set_source (handle, value.Handle);
247                         }
248
249                         get {
250                                 var ptr = NativeMethods.cairo_get_source (handle);
251                                 return Cairo.Pattern.Lookup (ptr, false);
252                         }
253                 }
254
255                 public double MiterLimit {
256                         set {
257                                 NativeMethods.cairo_set_miter_limit (handle, value);
258                         }
259
260                         get {
261                                 return NativeMethods.cairo_get_miter_limit (handle);
262                         }
263                 }
264
265                 public PointD CurrentPoint {
266                         get {
267                                 double x, y;
268                                 NativeMethods.cairo_get_current_point (handle, out x, out y);
269                                 return new PointD (x, y);
270                         }
271                 }
272
273                 public Cairo.Surface Target {
274                         set {
275                                 if (handle != IntPtr.Zero)
276                                         NativeMethods.cairo_destroy (handle);
277
278                                 handle = NativeMethods.cairo_create (value.Handle);
279                         }
280
281                         get {
282                                 return Surface.Lookup (NativeMethods.cairo_get_target (handle), false);
283                         }
284                 }
285
286                 public ScaledFont ScaledFont {
287                         set {
288                                 NativeMethods.cairo_set_scaled_font (handle, value.Handle);
289                         }
290
291                         get {
292                                 return new ScaledFont (NativeMethods.cairo_get_scaled_font (handle), false);
293                         }
294                 }
295
296                 public uint ReferenceCount {
297                         get { return NativeMethods.cairo_get_reference_count (handle); }
298                 }
299
300                 public void SetSourceRGB (double r, double g, double b)
301                 {
302                         NativeMethods.cairo_set_source_rgb (handle, r, g, b);
303                 }
304
305                 public void SetSourceRGBA (double r, double g, double b, double a)
306                 {
307                         NativeMethods.cairo_set_source_rgba (handle, r, g, b, a);
308                 }
309
310                 //[Obsolete ("Use SetSource method (with double parameters)")]
311                 public void SetSourceSurface (Surface source, int x, int y)
312                 {
313                         NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
314                 }
315
316                 public void SetSource (Surface source, double x, double y)
317                 {
318                         NativeMethods.cairo_set_source_surface (handle, source.Handle, x, y);
319                 }
320
321                 public void SetSource (Surface source)
322                 {
323                         NativeMethods.cairo_set_source_surface (handle, source.Handle, 0, 0);
324                 }
325
326 #region Path methods
327
328                 public void NewPath ()
329                 {
330                         NativeMethods.cairo_new_path (handle);
331                 }
332
333                 public void NewSubPath ()
334                 {
335                         NativeMethods.cairo_new_sub_path (handle);
336                 }
337
338                 public void MoveTo (PointD p)
339                 {
340                         MoveTo (p.X, p.Y);
341                 }
342
343                 public void MoveTo (double x, double y)
344                 {
345                         NativeMethods.cairo_move_to (handle, x, y);
346                 }
347
348                 public void LineTo (PointD p)
349                 {
350                         LineTo (p.X, p.Y);
351                 }
352
353                 public void LineTo (double x, double y)
354                 {
355                         NativeMethods.cairo_line_to (handle, x, y);
356                 }
357
358                 public void CurveTo (PointD p1, PointD p2, PointD p3)
359                 {
360                         CurveTo (p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
361                 }
362
363                 public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3)
364                 {
365                         NativeMethods.cairo_curve_to (handle, x1, y1, x2, y2, x3, y3);
366                 }
367
368                 public void RelMoveTo (Distance d)
369                 {
370                         RelMoveTo (d.Dx, d.Dy);
371                 }
372
373                 public void RelMoveTo (double dx, double dy)
374                 {
375                         NativeMethods.cairo_rel_move_to (handle, dx, dy);
376                 }
377
378                 public void RelLineTo (Distance d)
379                 {
380                         RelLineTo (d.Dx, d.Dy);
381                 }
382
383                 public void RelLineTo (double dx, double dy)
384                 {
385                         NativeMethods.cairo_rel_line_to (handle, dx, dy);
386                 }
387
388                 public void RelCurveTo (Distance d1, Distance d2, Distance d3)
389                 {
390                         RelCurveTo (d1.Dx, d1.Dy, d2.Dx, d2.Dy, d3.Dx, d3.Dy);
391                 }
392
393                 public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
394                 {
395                         NativeMethods.cairo_rel_curve_to (handle, dx1, dy1, dx2, dy2, dx3, dy3);
396                 }
397
398                 public void Arc (double xc, double yc, double radius, double angle1, double angle2)
399                 {
400                         NativeMethods.cairo_arc (handle, xc, yc, radius, angle1, angle2);
401                 }
402
403                 public void ArcNegative (double xc, double yc, double radius, double angle1, double angle2)
404                 {
405                         NativeMethods.cairo_arc_negative (handle, xc, yc, radius, angle1, angle2);
406                 }
407
408                 public void Rectangle (Rectangle rectangle)
409                 {
410                         Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
411                 }
412
413                 public void Rectangle (PointD p, double width, double height)
414                 {
415                         Rectangle (p.X, p.Y, width, height);
416                 }
417
418                 public void Rectangle (double x, double y, double width, double height)
419                 {
420                         NativeMethods.cairo_rectangle (handle, x, y, width, height);
421                 }
422
423                 public void ClosePath ()
424                 {
425                         NativeMethods.cairo_close_path (handle);
426                 }
427
428                 public Path CopyPath ()
429                 {
430                         return new Path (NativeMethods.cairo_copy_path (handle));
431                 }
432
433                 public Path CopyPathFlat ()
434                 {
435                         return new Path (NativeMethods.cairo_copy_path_flat (handle));
436                 }
437
438                 public void AppendPath (Path path)
439                 {
440                         NativeMethods.cairo_append_path (handle, path.Handle);
441                 }
442
443 #endregion
444
445 #region Painting Methods
446                 public void Paint ()
447                 {
448                         NativeMethods.cairo_paint (handle);
449                 }
450
451                 public void PaintWithAlpha (double alpha)
452                 {
453                         NativeMethods.cairo_paint_with_alpha (handle, alpha);
454                 }
455
456                 public void Mask (Pattern pattern)
457                 {
458                         NativeMethods.cairo_mask (handle, pattern.Handle);
459                 }
460
461                 public void MaskSurface (Surface surface, double surface_x, double surface_y)
462                 {
463                         NativeMethods.cairo_mask_surface (handle, surface.Handle, surface_x, surface_y);
464                 }
465
466                 public void Stroke ()
467                 {
468                         NativeMethods.cairo_stroke (handle);
469                 }
470
471                 public void StrokePreserve ()
472                 {
473                         NativeMethods.cairo_stroke_preserve (handle);
474                 }
475
476                 public Rectangle StrokeExtents ()
477                 {
478                         double x1, y1, x2, y2;
479                         NativeMethods.cairo_stroke_extents (handle, out x1, out y1, out x2, out y2);
480                         return new Rectangle (x1, y1, x2 - x1, y2 - y1);
481                 }
482
483                 public void Fill ()
484                 {
485                         NativeMethods.cairo_fill (handle);
486                 }
487
488                 public Rectangle FillExtents ()
489                 {
490                         double x1, y1, x2, y2;
491                         NativeMethods.cairo_fill_extents (handle, out x1, out y1, out x2, out y2);
492                         return new Rectangle (x1, y1, x2 - x1, y2 - y1);
493                 }
494
495                 public void FillPreserve ()
496                 {
497                         NativeMethods.cairo_fill_preserve (handle);
498                 }
499
500 #endregion
501
502                 public void Clip ()
503                 {
504                         NativeMethods.cairo_clip (handle);
505                 }
506
507                 public void ClipPreserve ()
508                 {
509                         NativeMethods.cairo_clip_preserve (handle);
510                 }
511
512                 public void ResetClip ()
513                 {
514                         NativeMethods.cairo_reset_clip (handle);
515                 }
516
517                 public bool InStroke (double x, double y)
518                 {
519                         return NativeMethods.cairo_in_stroke (handle, x, y);
520                 }
521
522                 public bool InFill (double x, double y)
523                 {
524                         return NativeMethods.cairo_in_fill (handle, x, y);
525                 }
526
527                 public Pattern PopGroup ()
528                 {
529                         return Pattern.Lookup (NativeMethods.cairo_pop_group (handle), true);
530                 }
531
532                 public void PopGroupToSource ()
533                 {
534                         NativeMethods.cairo_pop_group_to_source (handle);
535                 }
536
537                 public void PushGroup ()
538                 {
539                         NativeMethods.cairo_push_group (handle);
540                 }
541
542                 public void PushGroup (Content content)
543                 {
544                         NativeMethods.cairo_push_group_with_content (handle, content);
545                 }
546
547                 public Surface GroupTarget {
548                         get {
549                                 IntPtr surface = NativeMethods.cairo_get_group_target (handle);
550                                 return Surface.Lookup (surface, false);
551                         }
552                 }
553
554                 public void Rotate (double angle)
555                 {
556                         NativeMethods.cairo_rotate (handle, angle);
557                 }
558
559                 public void Scale (double sx, double sy)
560                 {
561                         NativeMethods.cairo_scale (handle, sx, sy);
562                 }
563
564                 public void Translate (double tx, double ty)
565                 {
566                         NativeMethods.cairo_translate (handle, tx, ty);
567                 }
568
569                 public void Transform (Matrix m)
570                 {
571                         NativeMethods.cairo_transform (handle, m);
572                 }
573
574                 [Obsolete("Use UserToDevice instead")]
575                 public void TransformPoint (ref double x, ref double y)
576                 {
577                         NativeMethods.cairo_user_to_device (handle, ref x, ref y);
578                 }
579
580                 [Obsolete("Use UserToDeviceDistance instead")]
581                 public void TransformDistance (ref double dx, ref double dy)
582                 {
583                         NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy);
584                 }
585
586                 [Obsolete("Use InverseTransformPoint instead")]
587                 public void InverseTransformPoint (ref double x, ref double y)
588                 {
589                         NativeMethods.cairo_device_to_user (handle, ref x, ref y);
590                 }
591
592                 [Obsolete("Use DeviceToUserDistance instead")]
593                 public void InverseTransformDistance (ref double dx, ref double dy)
594                 {
595                         NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy);
596                 }
597
598                 public void UserToDevice (ref double x, ref double y)
599                 {
600                         NativeMethods.cairo_user_to_device (handle, ref x, ref y);
601                 }
602
603                 public void UserToDeviceDistance (ref double dx, ref double dy)
604                 {
605                         NativeMethods.cairo_user_to_device_distance (handle, ref dx, ref dy);
606                 }
607
608                 public void DeviceToUser (ref double x, ref double y)
609                 {
610                         NativeMethods.cairo_device_to_user (handle, ref x, ref y);
611                 }
612
613                 public void DeviceToUserDistance (ref double dx, ref double dy)
614                 {
615                         NativeMethods.cairo_device_to_user_distance (handle, ref dx, ref dy);
616                 }
617
618                 public Matrix Matrix {
619                         set {
620                                 NativeMethods.cairo_set_matrix (handle, value);
621                         }
622
623                         get {
624                                 Matrix m = new Matrix();
625                                 NativeMethods.cairo_get_matrix (handle, m);
626                                 return m;
627                         }
628                 }
629
630                 public void SetFontSize (double scale)
631                 {
632                         NativeMethods.cairo_set_font_size (handle, scale);
633                 }
634
635                 public void IdentityMatrix ()
636                 {
637                         NativeMethods.cairo_identity_matrix (handle);
638                 }
639
640                 [Obsolete ("Use SetFontSize() instead.")]
641                 public void FontSetSize (double scale)
642                 {
643                         SetFontSize (scale);
644                 }
645
646                 [Obsolete ("Use SetFontSize() instead.")]
647                 public double FontSize {
648                         set { SetFontSize (value); }
649                 }
650
651                 public Matrix FontMatrix {
652                         get {
653                                 Matrix m;
654                                 NativeMethods.cairo_get_font_matrix (handle, out m);
655                                 return m;
656                         }
657                         set { NativeMethods.cairo_set_font_matrix (handle, value); }
658                 }
659
660                 public FontOptions FontOptions {
661                         get {
662                                 FontOptions options = new FontOptions ();
663                                 NativeMethods.cairo_get_font_options (handle, options.Handle);
664                                 return options;
665                         }
666                         set { NativeMethods.cairo_set_font_options (handle, value.Handle); }
667                 }
668
669                 [StructLayout(LayoutKind.Sequential)]
670                 internal struct NativeGlyph_4byte_longs {
671                         public int index;
672                         public double x;
673                         public double y;
674
675                         public NativeGlyph_4byte_longs (Glyph source)
676                         {
677                                 index = (int) source.index;
678                                 x = source.x;
679                                 y = source.y;
680                         }
681                 }
682
683                 static internal IntPtr FromGlyphToUnManagedMemory(Glyph [] glyphs)
684                 {
685                         IntPtr dest = Marshal.AllocHGlobal (native_glyph_size * glyphs.Length);
686                         long pos = dest.ToInt64();
687
688                         if (c_compiler_long_size == 8){
689                                 foreach (Glyph g in glyphs){
690                                         Marshal.StructureToPtr (g, (IntPtr)pos, false);
691                                         pos += native_glyph_size;
692                                 }
693                         } else {
694                                 foreach (Glyph g in glyphs){
695                                         NativeGlyph_4byte_longs n = new NativeGlyph_4byte_longs (g);
696
697                                         Marshal.StructureToPtr (n, (IntPtr)pos, false);
698                                         pos += native_glyph_size;
699                                 }
700                         }
701
702                         return dest;
703                 }
704
705                 public void ShowGlyphs (Glyph[] glyphs)
706                 {
707                         IntPtr ptr;
708
709                         ptr = FromGlyphToUnManagedMemory (glyphs);
710
711                         NativeMethods.cairo_show_glyphs (handle, ptr, glyphs.Length);
712
713                         Marshal.FreeHGlobal (ptr);
714                 }
715
716                 [Obsolete("The matrix argument was never used, use ShowGlyphs(Glyphs []) instead")]
717                 public void ShowGlyphs (Matrix matrix, Glyph[] glyphs)
718                 {
719                         ShowGlyphs (glyphs);
720                 }
721
722                 [Obsolete("The matrix argument was never used, use GlyphPath(Glyphs []) instead")]
723                 public void GlyphPath (Matrix matrix, Glyph[] glyphs)
724                 {
725                         GlyphPath (glyphs);
726                 }
727
728                 public void GlyphPath (Glyph[] glyphs)
729                 {
730                         IntPtr ptr;
731
732                         ptr = FromGlyphToUnManagedMemory (glyphs);
733
734                         NativeMethods.cairo_glyph_path (handle, ptr, glyphs.Length);
735
736                         Marshal.FreeHGlobal (ptr);
737
738                 }
739
740                 public FontExtents FontExtents {
741                         get {
742                                 FontExtents f_extents;
743                                 NativeMethods.cairo_font_extents (handle, out f_extents);
744                                 return f_extents;
745                         }
746                 }
747
748                 public void CopyPage ()
749                 {
750                         NativeMethods.cairo_copy_page (handle);
751                 }
752
753                 [Obsolete ("Use SelectFontFace() instead.")]
754                 public void FontFace (string family, FontSlant slant, FontWeight weight)
755                 {
756                         SelectFontFace (family, slant, weight);
757                 }
758
759                 public FontFace ContextFontFace {
760                         get {
761                                 return Cairo.FontFace.Lookup (NativeMethods.cairo_get_font_face (handle), false);
762                         }
763
764                         set {
765                                 NativeMethods.cairo_set_font_face (handle, value == null ? IntPtr.Zero : value.Handle);
766                         }
767                 }
768
769                 public void SelectFontFace (string family, FontSlant slant, FontWeight weight)
770                 {
771                         NativeMethods.cairo_select_font_face (handle, family, slant, weight);
772                 }
773
774                 public void ShowPage ()
775                 {
776                         NativeMethods.cairo_show_page (handle);
777                 }
778
779                 private static byte[] TerminateUtf8(byte[] utf8)
780                 {
781                         if (utf8.Length > 0 && utf8[utf8.Length - 1] == 0)
782                                 return utf8;
783                         var termedArray = new byte[utf8.Length + 1];
784                         Array.Copy(utf8, termedArray, utf8.Length);
785                         termedArray[utf8.Length] = 0;
786                         return termedArray;
787                 }
788
789                 private static byte[] TerminateUtf8(string s)
790                 {
791                         // compute the byte count including the trailing \0
792                         var byteCount = Encoding.UTF8.GetMaxByteCount(s.Length + 1);
793                         var bytes = new byte[byteCount];
794                         Encoding.UTF8.GetBytes(s, 0, s.Length, bytes, 0);
795                         return bytes;
796                 }
797
798                 public void ShowText(string str)
799                 {
800                         NativeMethods.cairo_show_text (handle, TerminateUtf8(str));
801                 }
802
803                 public void ShowText(byte[] utf8)
804                 {
805                         NativeMethods.cairo_show_text (handle, TerminateUtf8(utf8));
806                 }
807
808                 public void TextPath(string str)
809                 {
810                         NativeMethods.cairo_text_path (handle, TerminateUtf8(str));
811                 }
812
813                 public void TextPath(byte[] utf8)
814                 {
815                         NativeMethods.cairo_text_path (handle, TerminateUtf8(utf8));
816                 }
817
818                 public TextExtents TextExtents(string s)
819                 {
820                         TextExtents extents;
821                         NativeMethods.cairo_text_extents (handle, TerminateUtf8(s), out extents);
822                         return extents;
823                 }
824
825                 public TextExtents TextExtents(byte[] utf8)
826                 {
827                         TextExtents extents;
828                         NativeMethods.cairo_text_extents (handle, TerminateUtf8(utf8), out extents);
829                         return extents;
830                 }
831
832                 public TextExtents GlyphExtents (Glyph[] glyphs)
833                 {
834                         IntPtr ptr = FromGlyphToUnManagedMemory (glyphs);
835
836                         TextExtents extents;
837
838                         NativeMethods.cairo_glyph_extents (handle, ptr, glyphs.Length, out extents);
839
840                         Marshal.FreeHGlobal (ptr);
841
842                         return extents;
843                 }
844         }
845 }