+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:
./Mono.Cairo/Matrix.cs
./Mono.Cairo/Graphics.cs
./Mono.Cairo/Surface.cs
+./Mono.Cairo/Font.cs
+./Mono.Cairo/Pattern.cs
+
public class CairoAPI
{
- const string CairoImp = "cairo";
+ internal const string CairoImp = "cairo";
+
//
// Manipulating state objects
//
// 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
//
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);
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
//
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;
+ }
+
+
}
--- /dev/null
+//
+// 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; }
+ }
+
+
+
+ }
+}
public class Graphics : IDisposable
{
- IntPtr state;
+ internal IntPtr state = IntPtr.Zero;
public Graphics ()
{
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
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;
+ }
+ }
+
}
}
public class Matrix
{
- IntPtr matrix;
+ internal IntPtr matrix;
public Matrix ()
: this (Create ())
--- /dev/null
+//
+// 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; }
+ }
+
+ }
+}
+
public class Surface : IDisposable
{
static Hashtable surfaces = new Hashtable ();
- IntPtr surface;
+ internal IntPtr surface = IntPtr.Zero;
private Surface (IntPtr ptr, bool owns)
{
return (Surface) o;
}
}
-
+
public static Cairo.Surface CreateForImage (
string data, Cairo.Format format, int width, int height, int stride)
{
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)
{
CairoAPI.cairo_surface_set_filter (surface, value);
}
}
+
+ public IntPtr Pointer {
+ get { return surface; }
+ }
+
+
+
}
}