[cairo-commit] cairo-5c/examples draw.5c, 1.2, 1.3 graph.5c, 1.2, 1.3 grid.5c, NONE, 1.1 rottext.5c, 1.4, 1.5

Keith Packard commit at pdx.freedesktop.org
Thu Dec 23 14:39:42 PST 2004


Committed by: keithp

Update of /cvs/cairo/cairo-5c/examples
In directory gabe:/tmp/cvs-serv25485/examples

Modified Files:
	draw.5c graph.5c rottext.5c 
Added Files:
	grid.5c 
Log Message:
2004-12-23  Keith Packard  <keithp at keithp.com>

	* Makefile.am:
	* cairo-5c.h:
	* cairo.5c:
	* cairo.c: (cairo_5c_get), (cairo_5c_mark), (cairo_5c_free),
	(cairo_foreign_mark), (cairo_foreign_free), (do_Cairo_create),
	(cairo_5c_dirty), (cairo_5c_enable), (cairo_5c_disable),
	(do_Cairo_set_target_surface), (do_Cairo_current_target_surface),
	(do_Cairo_destroy), (do_Cairo_copy), (do_Cairo_status),
	(do_Cairo_status_string), (do_Cairo_enable), (do_Cairo_disable):
	* draw.c: (do_Cairo_new_path), (do_Cairo_move_to),
	(do_Cairo_line_to), (do_Cairo_curve_to), (do_Cairo_arc),
	(do_Cairo_arc_negative), (do_Cairo_rel_move_to),
	(do_Cairo_rel_line_to), (do_Cairo_rel_curve_to),
	(do_Cairo_rectangle), (do_Cairo_close_path), (do_Cairo_fill),
	(do_Cairo_stroke), (do_Cairo_in_stroke), (do_Cairo_in_fill),
	(do_Cairo_stroke_extents), (do_Cairo_fill_extents),
	(do_Cairo_current_path_list), (do_Cairo_current_path_flat_list):
	* event.c: (do_Cairo_Surface_open_event):
	* examples/draw.5c:
	* examples/graph.5c:
	* examples/grid.5c:
	* examples/rottext.5c:
	* gstate.c: (do_Cairo_save), (do_Cairo_restore),
	(do_Cairo_set_operator), (do_Cairo_set_rgb_color),
	(do_Cairo_set_alpha), (do_Cairo_set_tolerance),
	(do_Cairo_set_fill_rule), (do_Cairo_set_line_width),
	(do_Cairo_set_line_cap), (do_Cairo_set_line_join),
	(do_Cairo_set_dash), (do_Cairo_set_miter_limit),
	(do_Cairo_identity_matrix), (do_Cairo_default_matrix),
	(do_Cairo_translate), (do_Cairo_scale), (do_Cairo_rotate),
	(do_Cairo_current_matrix), (do_Cairo_concat_matrix),
	(do_Cairo_set_matrix), (do_Cairo_transform_point),
	(do_Cairo_transform_distance), (do_Cairo_inverse_transform_point),
	(do_Cairo_inverse_transform_distance), (do_Cairo_init_clip),
	(do_Cairo_clip), (do_Cairo_current_operator),
	(do_Cairo_current_rgb_color), (do_Cairo_current_alpha),
	(do_Cairo_current_tolerance), (do_Cairo_current_point),
	(do_Cairo_current_fill_rule), (do_Cairo_current_line_width),
	(do_Cairo_current_line_cap), (do_Cairo_current_line_join),
	(do_Cairo_current_miter_limit):
	* gtk.c: (configure_event), (expose_event), (delete_drawing_area),
	(delete_event), (motion_notify_event), (button_press_event),
	(button_release_event), (gtk_repaint), (gtk_repaint_timeout),
	(gtk_global_mark), (gtk_global_free), (create_gtk_global),
	(gtk_tool_mark), (gtk_tool_free), (cairo_5c_tool_create),
	(cairo_5c_tool_destroy), (cairo_5c_tool_mark),
	(cairo_5c_tool_dirty), (cairo_5c_tool_disable),
	(cairo_5c_tool_enable), (cairo_5c_tool_display):
	* init.c: (init_types), (nickle_init):
	* pattern.c: (mark_cairo_pattern), (make_pattern_value),
	(do_Cairo_set_pattern), (do_Cairo_current_pattern),
	(do_Cairo_Pattern_create_for_surface):
	* surface.c: (create_cairo_window), (cairo_5c_surface_get),
	(cairo_5c_surface_mark), (cairo_5c_surface_free),
	(cairo_surface_foreign_mark), (cairo_surface_foreign_free),
	(do_Cairo_Surface_create_window), (do_Cairo_Surface_create_png),
	(do_Cairo_Surface_create_ps), (do_Cairo_Surface_create_similar),
	(do_Cairo_Surface_destroy), (do_Cairo_Surface_width),
	(do_Cairo_Surface_height):
	* text.c: (do_Cairo_select_font), (do_Cairo_set_font),
	(do_Cairo_scale_font), (do_Cairo_transform_font),
	(do_Cairo_current_font_extents), (do_Cairo_show_text),
	(do_Cairo_text_path), (do_Cairo_text_extents):
	Split cairo_t and cairo_surface_t functions apart, permitting
	more complex programs to be written that use more than a single
	window.  Switch allocations around to mostly use the nickle memory
	allocator now.  Requires updated nickle bits that expose this
	change in the foreign object API.


Index: draw.5c
===================================================================
RCS file: /cvs/cairo/cairo-5c/examples/draw.5c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- draw.5c	18 Dec 2004 08:16:28 -0000	1.2
+++ draw.5c	23 Dec 2004 22:39:40 -0000	1.3
@@ -105,6 +105,12 @@
 	    if (button == 1)
 		down = false;
 	    break;
+	case "delete":
+	    exit (0);
+	    break;
+	default:
+	    printf ("unknown event %s\n", name);
+	    break;
 	}
     }
 }

Index: graph.5c
===================================================================
RCS file: /cvs/cairo/cairo-5c/examples/graph.5c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- graph.5c	17 Dec 2004 02:09:46 -0000	1.2
+++ graph.5c	23 Dec 2004 22:39:40 -0000	1.3
@@ -38,17 +38,124 @@
 namespace Graph {
     import Cairo;
 
-    typedef struct {
-	foreign	cr;
-    } graph;
+    public typedef enum { linear, log } scale_kind_t;
+    
+    public typedef struct {
+	scale_kind_t	kind;
+	real		low, high;
+	real		len;
+    } scale_t;
 
-    graph new () {
-	graph	g;
+    real scale (scale_t scale, real value)
+    {
+	if (scale.kind == scale_kind_t.log)
+	{
+	    value = log(value);
+	    scale.low = log (scale.low);
+	    scale.high = log (scale.high);
+	}
+        return scale.len * (value - scale.low) / (scale.high - scale.low);
+    }
 
-	g.cr = new ();
-    	return g;
+    public typedef struct {
+	&scale_t    scale;
+	real	    major;
+	real	    minor;
+    } axis_t;
+    
+    public typedef struct {
+	cairo_t	cr;
+	axis_t	left;
+	axis_t	bottom;
+    } graph_t;
+
+    void axis_draw (cairo_t cr, axis_t axis)
+    {
+	move_to (cr, scale (axis.scale,axis.scale.low), 0);
+	line_to (cr, scale (axis.scale,axis.scale.high), 0);
+	for (real minor = axis.scale.low; 
+	     minor <= axis.scale.high; 
+	     minor += axis.minor)
+	{
+	    if ((minor - axis.scale.low) % axis.major != 0)
+	    {
+		move_to (cr, scale (axis.scale, minor), 0);
+		rel_line_to (cr, 0, 5);
+	    }
+	}
+	for (real major = axis.scale.low; major <= axis.scale.high; major += axis.major)
+	{
+	    move_to (cr, scale (axis.scale, major), 0);
+	    rel_line_to (cr, 0, 10);
+	}
+	stroke (cr);
+        font_extents_t  f = current_font_extents (cr);
+	printf ("font extents: %v\n", f);
+	for (real major = axis.scale.low; major <= axis.scale.high; major += axis.major)
+	{
+	    string	    text = sprintf ("%g", major);
+	    text_extents_t  e = text_extents (cr, text);
+	    
+	    move_to (cr, scale (axis.scale, major) - e.x_advance / 2, 
+		     abs(f.height) * 1.2);
+	    show_text (cr, text);
+	}
     }
 
+    public void axes_draw (cairo_t cr, graph_t g)
+    {
+	save (cr);
+	axis_draw (cr, g.bottom);
+	restore (cr);
+	save (cr);
+	transform_font (cr, (matrix_t) { { 1, 0 }, { 0, -1 }, { 0, 0 } });
+	set_matrix (cr, Matrix::multiply ((matrix_t) { { 0, 1 }, 
+	    { 1, 0 }, { 0, 0 } }, current_matrix (cr)));
+				  
+	axis_draw (cr, g.left);
+	restore (cr);
+    }
+        
+    public graph_t new (cairo_t cr) {
+	set_font (cr, "sans-12");
+	transform_font (cr, (matrix_t) { { 1, 0 }, { 0, -1 }, { 0, 0 } });
+	translate (cr, 0, height(cr));
+	Cairo::scale (cr, 1, -1);
+	
+	set_line_cap (cr, line_cap_t.SQUARE);
+	translate (cr, width(cr) * .1, height(cr) * .1);
+	return (graph_t) {
+	    cr = cr,
+	    left = { 
+		scale = &(scale_t) {
+		    kind = scale_kind_t.linear,
+		    len = height(cr) * .8,
+		    low = 0,
+		    high = 10
+		},
+		major = 1,
+		minor = 0.25
+	    },
+	    bottom = {
+		scale = &(scale_t) {
+		    kind = scale_kind_t.linear,
+		    len = width(cr) * .8,
+		    low = 0,
+		    high = 10
+		},
+		major = 1,
+		minor = 0.25
+	    } 
+	};
+    }
+    public void test ()
+    {
+	cairo_t cr = Cairo::new (500, 500);
+	graph_t gr = new (cr);
+	axes_draw (cr, gr);
+    }
     
 }
 
+Graph::test ();
+for (;;) sleep (1000);

--- NEW FILE: grid.5c ---
autoimport Cairo;

typedef struct {
    real    min, max, space;
} grid_t;

void grid (cairo_t cr, grid_t h, grid_t v)
{
    for (real x = h.min; x <= h.max; x += h.space)
    {
	move_to (cr, x, v.min);
	line_to (cr, x, v.max);
    }
    for (real y = v.min; y <= v.max; y += v.space)
    {
	move_to (cr, h.min, y);
	line_to (cr, h.max, y);
    }
    stroke (cr);
}

void dots (cairo_t cr, grid_t h, grid_t v, real size)
{
    for (real x = h.min; x <= h.max; x += h.space)
    {
	for (real y = v.min; y <= v.max; y += v.space)
	{
	    move_to (cr, x - size, y);
	    line_to (cr, x + size, y);
	    move_to (cr, x, y - size);
	    line_to (cr, x, y + size);
	}
    }
    stroke (cr);
}

void
grid_png (string filename, int width, int height, real major, real x_minor, y_minor)
{
    cairo_t cr = new_png (filename, width, height);
    set_line_width (cr, 2);
    grid (cr, 
	  (grid_t) { min = 0, max = width, space = major },
	  (grid_t) { min = 0, max = height, space = major });
    set_line_width (cr, 1);
    dots (cr, 
	  (grid_t) { min = 0.5 + x_minor//2, max = width + 0.5, space = x_minor },
	  (grid_t) { min = 0.5 + y_minor//2, max = height + 0.5, space = y_minor },
	  major / 20);
}

grid_png ("grid.png", 1000, 1000, 90, 18, 30);
Debug::collect ();

Index: rottext.5c
===================================================================
RCS file: /cvs/cairo/cairo-5c/examples/rottext.5c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- rottext.5c	18 Dec 2004 08:16:28 -0000	1.4
+++ rottext.5c	23 Dec 2004 22:39:40 -0000	1.5
@@ -38,8 +38,7 @@
 
 void rottext () {
     cairo_t	cr = new ();
-    select_font (cr, "sans-serif", font_slant_t.NORMAL, font_weight_t.NORMAL);
-    scale_font (cr, 20);
+    set_font (cr, "sans-20");
     set_rgb_color (cr, 0, 0, 0);
     
     for (real a = 0; a < 2 * pi; a += pi / 10) {
@@ -52,5 +51,8 @@
     }
 }
 
-rottext ();
-sleep (20000);
+if (dim (argv) > 0)
+{
+    rottext ();
+    sleep (20000);
+}




More information about the cairo-commit mailing list