File: renderer-cairo.gob

package info (click to toggle)
ggobi 2.1.9~20091212-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,340 kB
  • ctags: 5,083
  • sloc: ansic: 57,242; xml: 30,604; cpp: 833; makefile: 355; java: 225; perl: 201; sh: 122; python: 23
file content (274 lines) | stat: -rw-r--r-- 8,980 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
%a{ /* only use cairo when GTK+ 2.8.0 or higher installed */
  #include <gtk/gtk.h>
  #if GTK_CHECK_VERSION(2,8,0)
%}
%{
  #include <math.h>
  /* Takes 1/64 degrees to radians and reverses */
  #define CAIRO_ANGLE(x) ((360 - (x/64.)) * M_PI / 180)
  /* millimeters per inch */
  #define MM_PER_INCH 25.4
%}
%h{
  #include "ggobi-renderer.h"
  #include <cairo/cairo.h>
%}

class GGobi:Renderer:Cairo from GGobi:Renderer
{
  private cairo_t *cairo destroywith cairo_destroy;
  /* This should only be changed for "once off" operations like saving to a 
     file or printing - set to NULL to reset to blank surface */
  property POINTER cairo
  (nick = "cr", blurb = "The cairo context to which this renders", 
   type = cairo_t *, flags = CONSTRUCT_ONLY, export)
   set {
     if (g_value_get_pointer(VAL)) {
       if (selfp->cairo)
         cairo_destroy(selfp->cairo);
       selfp->cairo = cairo_reference(g_value_get_pointer(VAL));
     }
   }
   get {
     g_value_set_pointer(VAL, selfp->cairo);
   };
   
  override (GGobi:Renderer) GdkDrawable *
  create_target(GGobi:Renderer *self, GdkDrawable *parent)
  { // FIXME: eventually we don't want to use the parent's drawable at all
    // -- we should override everything with cairo
    GdkDrawable *target = PARENT_HANDLER(self, parent);
    /* if we do not have a cairo context yet, try for a glitz (pbuffer)
       surface, otherwise fallback to image surface */
    if (!SELF(self)->_priv->cairo)
      SELF(self)->_priv->cairo = self_create_cairo(SELF(self), target);
    return target;
  }
  
  private virtual cairo_t *
  create_cairo(self, GdkDrawable *target)
  {
    return gdk_cairo_create(target);
  }
  
  private void
  line_type(self, GdkGCValues *values)
  {
    cairo_line_cap_t cap = CAIRO_LINE_CAP_ROUND;
    cairo_line_join_t join = CAIRO_LINE_JOIN_ROUND;
    /*static double dashes[8];
    gint i;*/
    
    cairo_set_line_width(selfp->cairo, values->line_width+1);
    
    switch(values->cap_style) {
      case GDK_CAP_ROUND:
        cap = CAIRO_LINE_CAP_ROUND;
      break;
      case GDK_CAP_BUTT:
      case GDK_CAP_NOT_LAST:
        cap = CAIRO_LINE_CAP_BUTT;
      break;
      case GDK_CAP_PROJECTING:
        cap = CAIRO_LINE_CAP_SQUARE;
      break;
    }
    cairo_set_line_cap(selfp->cairo, cap);
    
    switch(values->join_style) {
      case GDK_JOIN_ROUND:
        join = CAIRO_LINE_JOIN_ROUND;
      break;
      case GDK_JOIN_MITER:
        join = CAIRO_LINE_JOIN_MITER;
        //cairo_set_miter_limit(selfp->cairo, gc->lmitre);
      break;
      case GDK_JOIN_BEVEL:
        join = CAIRO_LINE_JOIN_BEVEL;
      break;
    }
    cairo_set_line_join(selfp->cairo, join);
    // FIXME: Don't know how to get the dashes out of the GdkGC
    // Perhaps we could extend GdkGC
    /*
    for(i = 0; i < 8 && gc->lty & 15; i++) {
      dashes[i] = gc->lty & 15;
      gc->lty = gc->lty >> 4;
    }
  
    
    cairo_set_dash(cr, dashes, i, 0);*/
  }
  
  private void
  stroke(self, GdkGC *gc, gboolean filled)
  {
    GdkColor color;
    GdkGCValues values;
    GdkColormap *colors = gdk_gc_get_colormap(gc);
    gdk_gc_get_values(gc, &values);
    gdk_colormap_query_color(colors, values.foreground.pixel, &color);
    //g_debug("color: %d %d %d", color.red, color.green, color.blue);
    gdk_cairo_set_source_color(selfp->cairo, &color);
    if (filled)
      cairo_fill_preserve(selfp->cairo);
    self_line_type(self, &values);
    cairo_stroke(selfp->cairo);
  }
  private void
  polypath(self, guint npoints, GdkPoint *points)
  {
    gint i;
    cairo_move_to(selfp->cairo, points[0].x, points[0].y);
    for (i = 1; i < npoints; i++)
      cairo_line_to(selfp->cairo, points[i].x, points[i].y);
  }
  
  override (Gdk:Drawable) void 
  draw_rectangle (GdkDrawable  *self, GdkGC *gc, gboolean filled,
    gint x, gint y, gint width, gint height)
  {
    //g_debug("drawing rect: %d %d %d %d", x, y, width, height);
    cairo_rectangle(SELF(self)->_priv->cairo, x, y, width, height);
    self_stroke(SELF(self), gc, filled);
  }
  override (Gdk:Drawable) void 
  draw_arc(GdkDrawable  *self, GdkGC *gc, gboolean filled, gint x, gint y, 
    gint width, gint height, gint angle1, gint angle2)
  {
    //cairo_move_to(cr, x+r, y);
    cairo_t *cr = SELF(self)->_priv->cairo;
    cairo_save(cr);
    cairo_translate (cr, x + width / 2., y + height / 2.);
    cairo_scale (cr, (width / 2.), (height / 2.));
    //g_debug("drawing arc from: %f to %f", CAIRO_ANGLE(angle2), CAIRO_ANGLE(angle1));
    cairo_arc_negative (cr, 0., 0., 1., CAIRO_ANGLE(angle1), CAIRO_ANGLE(angle2));
    self_stroke(SELF(self), gc, filled);
    cairo_restore(cr);
  }
  override (Gdk:Drawable) void 
  draw_polygon(GdkDrawable  *self, GdkGC *gc, gboolean filled, 
    GdkPoint *points, gint npoints)
  {
    //g_debug("POLYGON");
    self_polypath(SELF(self), npoints, points);
    cairo_close_path(SELF(self)->_priv->cairo);
    self_stroke(SELF(self), gc, filled);
  }
  override (Gdk:Drawable) void 
  draw_points(GdkDrawable  *self, GdkGC *gc, GdkPoint *points, gint npoints)
  {
    guint i;
    //g_debug("POINTS");
    cairo_t *cr = SELF(self)->_priv->cairo;
    for (i = 0; i < npoints; i++)
      cairo_rectangle(cr, points[i].x - 0.5, points[i].y - 0.5, 1., 1.);
    self_stroke(SELF(self), gc, FALSE);
  }
  override (Gdk:Drawable) void 
  draw_segments(GdkDrawable  *self, GdkGC *gc, GdkSegment *segs, gint nsegs)
  {
    guint i;
    //g_debug("SEGMENTS");
    cairo_t *cr = SELF(self)->_priv->cairo;
    for (i = 0; i < nsegs; i++) {
      cairo_move_to(cr, segs[i].x1, segs[i].y1);
      cairo_line_to(cr, segs[i].x2, segs[i].y2);
    }
    self_stroke(SELF(self), gc, FALSE);
  }
  override (Gdk:Drawable) void 
  draw_lines(GdkDrawable  *self, GdkGC *gc, GdkPoint *points, gint npoints)
  {
    self_polypath(SELF(self), npoints, points);
    self_stroke(SELF(self), gc, FALSE);
  }
  override (Gdk:Drawable) void 
  draw_trapezoids(GdkDrawable *self, GdkGC *gc, GdkTrapezoid *trapezoids, 
    gint n_trapezoids)
  {
    guint i;
    cairo_t *cr = SELF(self)->_priv->cairo;
    for (i = 0; i < n_trapezoids; i++) {
      cairo_move_to(cr, trapezoids[i].x11, trapezoids[i].y1);
      cairo_line_to(cr, trapezoids[i].x21, trapezoids[i].y1);
      cairo_line_to(cr, trapezoids[i].x22, trapezoids[i].y2);
      cairo_line_to(cr, trapezoids[i].x12, trapezoids[i].y2);
      cairo_line_to(cr, trapezoids[i].x11, trapezoids[i].y1);
    }
    self_stroke(SELF(self), gc, FALSE);
  }
  
  /* we do not support the old GDK text stuff (yet) */
  override (Gdk:Drawable) void 
  draw_text(GdkDrawable  *self, GdkFont *font, GdkGC *gc, gint x, gint y, 
    const gchar  *text, gint text_length)
  {
  }
  override (Gdk:Drawable) void 
  draw_text_wc(GdkDrawable *self, GdkFont *font, GdkGC *gc, gint x, gint y, 
    const GdkWChar *text, gint text_length)
  {
  }
  /* here is the modern pango stuff */
  override (Gdk:Drawable) void 
  draw_glyphs(GdkDrawable *self, GdkGC *gc, PangoFont *font, gint x, gint y, 
    PangoGlyphString *glyphs)
  {
    cairo_move_to(SELF(self)->_priv->cairo, x, y);
    pango_cairo_show_glyph_string(SELF(self)->_priv->cairo, font, glyphs);
  }
  override (Gdk:Drawable) void 
  draw_glyphs_transformed(GdkDrawable *self, GdkGC *gc, PangoMatrix *matrix, 
    PangoFont *font, gint x, gint y, PangoGlyphString *glyphs)
  {
    cairo_t *cr = SELF(self)->_priv->cairo;
    cairo_matrix_t cr_matrix = 
    { matrix->xx, matrix->xy, matrix->yx, matrix->yy, matrix->x0, matrix->y0 };
    cairo_save(cr);
    cairo_set_matrix(cr, &cr_matrix);
    GDK_DRAWABLE_GET_CLASS(self)->draw_glyphs(self, gc, font, x, y, glyphs);
    cairo_restore(cr);
  }
  
  override (Gdk:Drawable) void 
  draw_drawable(GdkDrawable  *self, GdkGC *gc, GdkDrawable  *src, gint xsrc, 
    gint ysrc, gint xdest, gint ydest, gint width, gint height)
  {
    cairo_t *cr = SELF(self)->_priv->cairo;
    cairo_surface_t *surf = GDK_DRAWABLE_GET_CLASS(src)->ref_cairo_surface(src);
    cairo_set_source_surface(cr, surf, xdest - xsrc, ydest - ysrc);
    cairo_save(cr);
    cairo_rectangle(cr, xdest, ydest, width, height);
    cairo_clip(cr);
    cairo_paint(cr);
    cairo_restore(cr);
  }
  
  override (Gdk:Drawable) GdkDrawable* 
  get_composite_drawable(GdkDrawable *self, gint x, gint y, gint width, 
    gint height, gint *composite_x_offset, gint *composite_y_offset)
  {
    GdkDrawable *parent, *composite;
    cairo_t *cr;
    g_object_get(G_OBJECT(self), "parent", &parent, NULL);
    composite = gdk_pixmap_new(parent, width, height, -1);
    cr = gdk_cairo_create(composite);
    cairo_set_source_surface(cr, cairo_get_target(SELF(self)->_priv->cairo), 
      -1*x, -1*y);
    cairo_paint(cr);
    cairo_destroy(cr);
    *composite_x_offset = 0;
    *composite_y_offset = 0;
    return composite;
  }
  
  override (Gdk:Drawable) cairo_surface_t *
  ref_cairo_surface(GdkDrawable *self)
  {
    return cairo_surface_reference(cairo_get_target(SELF(self)->_priv->cairo));
  }
}
%a{
  #endif
%}