Font class, Pattern class, fixes, new methods, and updating Mono.Cairo to the latest...
authorJordi Mas i Hernandez <jordi@mono-cvs.ximian.com>
Sun, 25 Apr 2004 15:31:58 +0000 (15:31 -0000)
committerJordi Mas i Hernandez <jordi@mono-cvs.ximian.com>
Sun, 25 Apr 2004 15:31:58 +0000 (15:31 -0000)
svn path=/trunk/mcs/; revision=25959

mcs/class/Mono.Cairo/ChangeLog
mcs/class/Mono.Cairo/Mono.Cairo.dll.sources
mcs/class/Mono.Cairo/Mono.Cairo/Cairo.cs
mcs/class/Mono.Cairo/Mono.Cairo/Font.cs [new file with mode: 0644]
mcs/class/Mono.Cairo/Mono.Cairo/Graphics.cs
mcs/class/Mono.Cairo/Mono.Cairo/Matrix.cs
mcs/class/Mono.Cairo/Mono.Cairo/Pattern.cs [new file with mode: 0644]
mcs/class/Mono.Cairo/Mono.Cairo/Surface.cs

index e06c8fcab0fd20f4d83686d3edc81489992a28e6..51109da5627bc2b747cb640931551a37fd98f642 100644 (file)
@@ -1,3 +1,12 @@
+2004-04-25  Jordi Mas i Hernandez <jordi@ximian.com>
+       * Mono.Cairo/Pattern.cs: implemented all the new pattern API 
+       introduced in Cairo 0.1.20
+       * Font.cs: created class that encapsulates a font object creation
+       and managment.
+       * Cairo.cs: new dllexports, structs, and enums.
+       * Graphics.cs: added all the font functions, fixed the signature
+       of the methods that have changed, added new methods
+
 2003-11-28  Miguel de Icaza  <miguel@ximian.com>
 
        * Mono.Cairo/Object.cs: 
index 67a9efc7b06c273d0cec8594d8f57e70709ac9b8..efed617f7ee5e1223f5a4fcb065e7ac306900680 100644 (file)
@@ -2,3 +2,6 @@
 ./Mono.Cairo/Matrix.cs
 ./Mono.Cairo/Graphics.cs
 ./Mono.Cairo/Surface.cs
+./Mono.Cairo/Font.cs
+./Mono.Cairo/Pattern.cs
+
index 68e6e4465f60d2f9deb7850b4a9d23dba4f8fe8e..463cad3c37959b583acadf5919a8af55a864df74 100644 (file)
@@ -18,7 +18,8 @@ namespace Cairo {
 
        public class CairoAPI
         {
-                const string CairoImp = "cairo";
+                internal const string CairoImp = "cairo";
+
                 //
                 // Manipulating state objects
                 //
@@ -176,31 +177,56 @@ namespace Cairo {
                 // Font / Text
                 //
                 [DllImport (CairoImp)]
-                public static extern void cairo_select_font (IntPtr cr, string key);
+                public static extern void cairo_select_font (IntPtr cr, string key, FontSlant slant, FontWeight weight);
 
                 [DllImport (CairoImp)]
                 public static extern void cairo_scale_font (IntPtr cr, double scale);
 
                 [DllImport (CairoImp)]
-                public static extern void cairo_transform_font (
-                        IntPtr cr, double a, double b, double c, double d);
+                public static extern void cairo_transform_font (IntPtr cr, IntPtr matrix);
 
                 [DllImport (CairoImp)]
-                public static extern void cairo_text_extents (
-                        IntPtr cr, string utf8,
-                        ref double x, ref double y,
-                        ref double width, ref double height,
-                        ref double dx, ref double dy);
+                public static extern void cairo_show_text (IntPtr cr, string utf8);
 
                 [DllImport (CairoImp)]
-                public static extern void cairo_show_text (IntPtr cr, string utf8);
+                public static extern void cairo_font_set_transform (IntPtr font, IntPtr matrix);
+
+                [DllImport (CairoImp)]
+                public static extern void cairo_font_current_transform (IntPtr font, IntPtr matrix);
+
+                [DllImport (CairoImp)]
+                public static extern void cairo_font_reference (IntPtr font);
+
+                [DllImport (CairoImp)]
+                public static extern void cairo_font_destroy (IntPtr font);
 
+                [DllImport (CairoImp)]
+                public static extern void cairo_current_font_extents (IntPtr source, ref Extents extents);
+
+                [DllImport (CairoImp)]
+                public static extern void cairo_show_glyphs (IntPtr ct, IntPtr glyphs, int num_glyphs);
+
+                [DllImport (CairoImp)]
+                public static extern void cairo_text_path  (IntPtr ct, string utf8);
+
+                [DllImport (CairoImp)]
+                public static extern void cairo_glyph_path (IntPtr ct, IntPtr glyphs, int num_glyphs);
+
+
+                // Cairo's font manipulation platform-specific Unix Fontconfig/Freetype interface
+
+                [DllImport (CairoImp)]
+                public static extern IntPtr cairo_ft_font_create (IntPtr ft_library, IntPtr ft_pattern);
+
+        
                 //
                 // Image
                 //                
                 [DllImport (CairoImp)]
                 public static extern void cairo_show_surface (IntPtr cr, IntPtr surface, int width, int height);
 
+
+
                 //
                 // query
                 //                                
@@ -259,6 +285,11 @@ namespace Cairo {
                 public static extern IntPtr cairo_surface_create_for_image (
                         string data, Cairo.Format format, int width, int height, int stride);
 
+                [DllImport (CairoImp)]
+                public static extern IntPtr cairo_image_surface_create (Cairo.Format format, int width,
+                        int height);
+
+
                 [DllImport (CairoImp)]
                 public static extern IntPtr cairo_surface_create_similar (
                         IntPtr surface, Cairo.Format format, int width, int height);
@@ -345,8 +376,97 @@ namespace Cairo {
                 public static extern Cairo.Status cairo_matrix_transform_point (
                         IntPtr matrix, ref double x, ref double y);
 
+                [DllImport (CairoImp)]
+                public static extern void cairo_set_font (IntPtr ct, IntPtr font);
+
+                [DllImport (CairoImp)]
+                public static extern IntPtr cairo_current_font (IntPtr ct);
+
+                //
+                // Pattern functions
+                //
+                [DllImport (CairoImp)]
+                public static extern IntPtr cairo_pattern_create_for_surface (IntPtr surface);
+
+                [DllImport (CairoImp)]
+                public static extern IntPtr cairo_pattern_create_linear (double x0, double y0,
+                       double x1, double y1);
+
+                [DllImport (CairoImp)]
+                public static extern IntPtr cairo_pattern_create_radial (double cx0, double cy0,
+                        double radius0, double cx1, double cy1, double radius1);
+
+                [DllImport (CairoImp)]
+                public static extern void cairo_pattern_reference (IntPtr pattern);
+
+                [DllImport (CairoImp)]
+                public static extern void cairo_pattern_destroy (IntPtr pattern);
+
+                [DllImport (CairoImp)]
+                public static extern Status cairo_pattern_add_color_stop (IntPtr pattern,
+                       double offset, double red, double green, double blue, double alpha);
+
+                [DllImport (CairoImp)]
+                public static extern Status cairo_pattern_set_matrix (IntPtr pattern, IntPtr matrix);
+
+                [DllImport (CairoImp)]
+                public static extern Status cairo_pattern_get_matrix (IntPtr pattern, out IntPtr matrix);
+
+                [DllImport (CairoImp)]
+                public static extern Status cairo_pattern_set_extend (IntPtr pattern, Extend extend);
+
+                [DllImport (CairoImp)]
+                public static extern Extend cairo_pattern_get_extend (IntPtr pattern);
+
+                [DllImport (CairoImp)]
+                public static extern Status cairo_pattern_set_filter (IntPtr pattern, Filter filter);
+
+                [DllImport (CairoImp)]
+                public static extern Filter cairo_pattern_get_filter (IntPtr pattern);
+
+        }
+
+        //
+        // Freetype interface need it by cairo_ft interface calls
+        //
+        public class FreeType
+        {
+                internal const string FreeTypeImp = "freetype";
+
+                [DllImport (FreeTypeImp)]
+                public static extern int FT_Init_FreeType (out IntPtr library);
+
+                [DllImport (FreeTypeImp)]
+                public static extern int FT_Set_Char_Size (IntPtr face, long width, long height, uint horz_res, uint vert_res);
+        }
+
+        //
+        // Fontconfig interface need it by cairo_ft interface calls
+        //
+        public class FontConfig
+        {
+                internal const string FontConfigImp = "fontconfig";
+
+                public const string FC_FAMILY = "family";
+                public const string FC_STYLE = "style";
+                public const string FC_SLANT = "slant";
+                public const string FC_WEIGHT = "weight";
+
+                [DllImport (FontConfigImp)]
+                public static extern bool FcPatternAddString (IntPtr pattern, string obj, string value);
+
+                [DllImport (FontConfigImp)]
+                public static extern bool FcPatternAddInteger (IntPtr pattern, string obj, int value);
+
+                [DllImport (FontConfigImp)]
+                public static extern IntPtr FcPatternCreate ();
+
+                [DllImport (FontConfigImp)]
+                public static extern bool FcPatternDestroy (IntPtr pattern); 
+                
         }
 
+
         //
         // Enumerations
         //
@@ -427,6 +547,46 @@ namespace Cairo {
                 Good,
                 Best,
                 Nearest,
-                Bilinear
+                Bilinear,
+                Gaussian,
+        }
+
+        public enum FontSlant {
+                Normal  = 0,
+                Italic  = 1,
+                Oblique = 2
+        }
+
+        public enum FontWeight {
+                Normal  = 0,
+                Bold    = 1,
+        }
+
+        public enum Extend {
+                None,
+                Repetat,
+                Reflect,
+        }
+
+       
+        [StructLayout(LayoutKind.Sequential)]
+        public struct Extents
+        {
+                public  double x_bearing;
+                public  double y_bearing;
+                public  double width;
+                public  double height;
+                public  double x_advance;
+                public  double y_advance;
         }
+
+        [StructLayout(LayoutKind.Sequential)]
+        public struct Glyph
+        {
+                public  long index;
+                public  double x;
+                public  double y;
+        }
+
+
 }
diff --git a/mcs/class/Mono.Cairo/Mono.Cairo/Font.cs b/mcs/class/Mono.Cairo/Mono.Cairo/Font.cs
new file mode 100644 (file)
index 0000000..5f8e557
--- /dev/null
@@ -0,0 +1,112 @@
+//                                                   
+// Mono.Cairo.Font.cs
+//
+// Author: Jordi Mas (jordi@ximian.com)
+//
+// (C) Ximian Inc, 2004.
+//
+//
+
+using System;
+using System.Drawing;
+using System.Runtime.InteropServices;
+using Cairo;
+
+namespace Cairo {
+
+        /* Font object encapsulates all the functionality related to font handeling */
+        public class Font
+        {
+                internal IntPtr font = IntPtr.Zero;
+
+                internal IntPtr _create (string family, FontSlant fcslant, FontWeight fcweight)
+                {
+                        IntPtr ft_font = IntPtr.Zero;
+                       IntPtr font = IntPtr.Zero;
+                       IntPtr pattern = IntPtr.Zero;
+                       IntPtr library = IntPtr.Zero;
+                       int error = 0;
+
+                       pattern = FontConfig.FcPatternCreate ();                               
+                       if (pattern == IntPtr.Zero)
+                               return ft_font;
+
+                       FontConfig.FcPatternAddString (pattern, FontConfig.FC_FAMILY, family);
+                       FontConfig.FcPatternAddInteger (pattern, FontConfig.FC_SLANT, (int) fcslant);
+                       FontConfig.FcPatternAddInteger (pattern, FontConfig.FC_WEIGHT, (int) fcweight);
+
+                       error = FreeType.FT_Init_FreeType (out library);                            
+                       if (error != 0) {
+                                FontConfig.FcPatternDestroy (pattern);
+                               return ft_font;
+                       }
+
+                       font = CairoAPI.cairo_ft_font_create (library, pattern);
+                       if (font == IntPtr.Zero)
+                               return ft_font;
+
+                        /*
+                       ft_font = (cairo_ft_font_t *) font;
+
+                       ft_font->owns_ft_library = 1;
+
+                       FT_Set_Char_Size (ft_font->face,
+                                 DOUBLE_TO_26_6 (1.0),
+                                 DOUBLE_TO_26_6 (1.0),
+                                 0, 0);*/
+
+                        FontConfig.FcPatternDestroy (pattern);
+                       return ft_font;
+                }
+
+
+                private Font ()
+                {
+                                     
+                }
+
+                
+                public Font (string family, FontSlant fcslant, FontWeight fcweight)
+                {
+                        font =  _create (family, fcslant, fcweight);
+                }
+                
+
+                internal Font (IntPtr native)
+                {
+                        font = native;
+                }
+
+               
+                public void Destroy ()
+                {
+                        CairoAPI.cairo_font_destroy (font);
+                }
+
+                public void Reference ()
+                {
+                        CairoAPI.cairo_font_reference (font);
+
+                }
+
+                public Cairo.Matrix Transform {
+                        set {
+                                CairoAPI.cairo_font_set_transform (font, value.Pointer);
+                        }
+
+                        get {
+                                IntPtr matrix;
+
+                                CairoAPI.cairo_surface_get_matrix (font, out matrix);
+                                return new Cairo.Matrix (matrix);
+                        }
+                }
+
+                public IntPtr Pointer {
+                        get { return font; }
+                }
+
+
+                              
+        }
+}
index ab209cb1526c1d61cc81e8e71207044021ab1ded..07630c45698f3c43f58e7164588e05e9e0b168e5 100644 (file)
@@ -20,7 +20,7 @@ namespace Cairo {
 
         public class Graphics : IDisposable 
         {
-                IntPtr state;
+                internal IntPtr state = IntPtr.Zero;
 
                 public Graphics ()
                 {
@@ -308,9 +308,9 @@ namespace Cairo {
                        CairoAPI.cairo_set_target_drawable (state, dpy, drawable);
                }
 
-               public void SetPattern (Surface pattern)
+               public void SetPattern (Pattern pattern)
                {
-                       CairoAPI.cairo_set_pattern (state, pattern.Handle);
+                       CairoAPI.cairo_set_pattern (state, pattern.Pointer);
                }
 #endregion
 
@@ -365,5 +365,100 @@ namespace Cairo {
                                 return new Cairo.Matrix (p);
                         }
                 }
+
+                public Font Font {
+                        set {
+                                CairoAPI.cairo_set_font (state, value.Pointer);
+
+                        }
+
+                        get {
+                                IntPtr fnt = IntPtr.Zero;
+
+                                fnt = CairoAPI.cairo_current_font (state);
+
+                                return new Font (fnt);
+                        }
+                }
+
+
+                public void ScaleFont (double scale)
+                {
+                        CairoAPI.cairo_scale_font (state, scale);
+                }
+
+                public void SetText (string str)
+                {
+                        /* Use UTF8 encoding*/
+                        CairoAPI.cairo_show_text (state, str);
+                }
+                
+                public void TransformFont (Matrix matrix)
+                {
+                        CairoAPI.cairo_transform_font (state, matrix.Pointer);
+                }
+
+
+                
+               static internal IntPtr FromGlyphToUnManagedMemory(Glyph [] glyphs)
+               {
+                       int size =  Marshal.SizeOf (glyphs[0]);
+                       IntPtr dest = Marshal.AllocHGlobal (size * glyphs.Length);
+                       int pos = dest.ToInt32();
+
+                       for (int i=0; i < glyphs.Length; i++, pos += size)
+                               Marshal.StructureToPtr (glyphs[i], (IntPtr)pos, false);
+
+                       return dest;
+               }
+
+
+                public void ShowGlyphs (Matrix matrix, Glyph[] glyphs)
+                {
+
+                        IntPtr ptr;
+
+                        ptr = FromGlyphToUnManagedMemory (glyphs);
+                        
+                        CairoAPI.cairo_show_glyphs (state, ptr, glyphs.Length);
+
+                        Marshal.FreeHGlobal (ptr);             
+                     
+                }
+
+                public void TextPath (string str)
+                {
+                        CairoAPI.cairo_text_path  (state, str);
+                }
+
+                public void GlyphPath (Matrix matrix, Glyph[] glyphs)
+                {
+
+                        IntPtr ptr;
+
+                        ptr = FromGlyphToUnManagedMemory (glyphs);
+
+                        CairoAPI.cairo_glyph_path (state, ptr, glyphs.Length);
+
+                        Marshal.FreeHGlobal (ptr);
+
+                }
+
+                public void SelectFont (string key, FontSlant slant, FontWeight weight)
+                {
+                        CairoAPI.cairo_select_font (state, key, slant, weight);
+                }
+
+
+
+                public Extents Extents {
+                        get {
+
+                                Extents extents = new Extents();
+                                CairoAPI.cairo_current_font_extents (state, ref extents);
+                                return extents;
+                        }
+                }
+
         }
 }
index 89e1516a16f6e480354350499173e32707f6e850..1faac0522daa0af98395db4ed90199208bc26286 100644 (file)
@@ -17,7 +17,7 @@ namespace Cairo {
 
         public class Matrix
         {
-                IntPtr matrix;
+                internal IntPtr matrix;
 
                 public Matrix ()
                         : this (Create ())
diff --git a/mcs/class/Mono.Cairo/Mono.Cairo/Pattern.cs b/mcs/class/Mono.Cairo/Mono.Cairo/Pattern.cs
new file mode 100644 (file)
index 0000000..a9748d2
--- /dev/null
@@ -0,0 +1,101 @@
+//                                                   
+// Mono.Cairo.Pattern.cs
+//
+// Author: Jordi Mas (jordi@ximian.com)
+//
+// (C) Ximian Inc, 2004.
+//
+//
+
+using System;
+using System.Runtime.InteropServices;
+using Cairo;
+
+namespace Cairo {
+
+        public class Pattern
+        {
+                IntPtr pattern = IntPtr.Zero;
+
+                private Pattern ()
+                {
+                }
+
+                public Pattern (Surface surface)
+                {
+                        pattern = CairoAPI.cairo_pattern_create_for_surface (surface.Pointer);
+                }
+
+                public Pattern (double x0, double y0, double x1, double y1)
+                {
+                        pattern = CairoAPI.cairo_pattern_create_linear (x0, y0, x1, y1);
+
+                }
+
+                public Pattern (double cx0, double cy0, double radius0,
+                                    double cx1, double cy1, double radius1)
+                {
+                        pattern = CairoAPI.cairo_pattern_create_radial (cx0, cy0, radius0,
+                                cx1, cy1, radius1);
+                }
+
+                public void Reference ()
+                {
+                        CairoAPI.cairo_pattern_reference (pattern);
+                }
+
+                public void Destroy ()
+                {
+                        CairoAPI.cairo_pattern_destroy (pattern);
+                }
+
+                public Status AddColorStop (double offset, double red, double green,
+                        double blue, double alpha)
+                {
+                        return CairoAPI.cairo_pattern_add_color_stop (pattern,
+                                offset, red, green, blue, alpha);                
+                }
+
+                public Matrix Matrix {
+                        set {
+                                CairoAPI.cairo_pattern_set_matrix (pattern, value.Pointer);
+
+                        }
+
+                        get {
+                                IntPtr matrix;
+
+                                CairoAPI.cairo_pattern_get_matrix (pattern, out matrix);
+                          return new Cairo.Matrix (matrix);
+                        }
+                }
+
+                public Extend Extend {
+                        set {
+                                CairoAPI.cairo_pattern_set_extend (pattern, value);
+
+                        }
+
+                        get {
+                                return CairoAPI.cairo_pattern_get_extend (pattern);                        
+                        }
+                }
+
+                public Filter Filter {
+                        set {
+                                CairoAPI.cairo_pattern_set_filter (pattern, value);
+
+                        }
+
+                        get {
+                                return CairoAPI.cairo_pattern_get_filter (pattern);
+                        }
+                }
+
+                public IntPtr Pointer {
+                        get { return pattern; }
+                }
+
+        }
+}
+
index 16660b9b2df1ab7ba17adc9edc0964d41c39fd13..b5af0d0e707106b3742762959e2efb97563aad8b 100644 (file)
@@ -22,7 +22,7 @@ namespace Cairo {
        public class Surface : IDisposable 
         {
                static Hashtable surfaces = new Hashtable ();
-                IntPtr surface;
+                internal IntPtr surface = IntPtr.Zero;
 
                 private Surface (IntPtr ptr, bool owns)
                 {
@@ -44,7 +44,7 @@ namespace Cairo {
                                return (Surface) o;
                        }
                }
-               
+
                 public static Cairo.Surface CreateForImage (
                         string data, Cairo.Format format, int width, int height, int stride)
                 {
@@ -54,6 +54,16 @@ namespace Cairo {
                         return new Cairo.Surface (p, true);
                 }
 
+                public static Cairo.Surface CreateForImage (
+                        Cairo.Format format, int width, int height)
+                {
+                        IntPtr p = CairoAPI.cairo_image_surface_create (
+                                format, width, height);
+
+                        return new Cairo.Surface (p, true);
+                }
+
+
                 public static Cairo.Surface CreateSimilar (
                         Cairo.Surface surface, Cairo.Format format, int width, int height)
                 {
@@ -124,5 +134,12 @@ namespace Cairo {
                                 CairoAPI.cairo_surface_set_filter (surface, value);
                         }
                 }
+
+                public IntPtr Pointer {
+                        get { return surface; }
+                }
+
+
+
         }
 }