reuse cairo context stuff

This commit is contained in:
Julien Danjou 2008-01-31 18:18:15 +01:00
parent 395b39de78
commit a28876b5ce
3 changed files with 70 additions and 80 deletions

View File

@ -44,10 +44,20 @@ draw_context_new(Display *disp, int phys_screen, int width, int height, Drawable
d->depth = DefaultDepth(disp, phys_screen); d->depth = DefaultDepth(disp, phys_screen);
d->visual = DefaultVisual(disp, phys_screen); d->visual = DefaultVisual(disp, phys_screen);
d->drawable = dw; d->drawable = dw;
d->surface = cairo_xlib_surface_create(disp, dw, d->visual, width, height);
d->cr = cairo_create(d->surface);
return d; return d;
}; };
void
draw_context_delete(DrawCtx *ctx)
{
cairo_surface_destroy(ctx->surface);
cairo_destroy(ctx->cr);
p_delete(&ctx);
}
/** Draw text into a draw context /** Draw text into a draw context
* \param x x coord * \param x x coord
* \param y y coord * \param y y coord
@ -72,8 +82,6 @@ draw_text(DrawCtx *ctx,
static char buf[256]; static char buf[256];
size_t len, olen; size_t len, olen;
cairo_font_face_t *font_face; cairo_font_face_t *font_face;
cairo_surface_t *surface;
cairo_t *cr;
draw_rectangle(ctx, area, True, bg); draw_rectangle(ctx, area, True, bg);
@ -82,12 +90,10 @@ draw_text(DrawCtx *ctx,
if(!len) if(!len)
return; return;
surface = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
cr = cairo_create(surface);
font_face = cairo_ft_font_face_create_for_pattern(font->pattern); font_face = cairo_ft_font_face_create_for_pattern(font->pattern);
cairo_set_font_face(cr, font_face); cairo_set_font_face(ctx->cr, font_face);
cairo_set_font_size(cr, font->height); cairo_set_font_size(ctx->cr, font->height);
cairo_set_source_rgb(cr, fg.red / 65535.0, fg.green / 65535.0, fg.blue / 65535.0); cairo_set_source_rgb(ctx->cr, fg.red / 65535.0, fg.green / 65535.0, fg.blue / 65535.0);
if(len >= sizeof(buf)) if(len >= sizeof(buf))
len = sizeof(buf) - 1; len = sizeof(buf) - 1;
@ -110,22 +116,21 @@ draw_text(DrawCtx *ctx,
switch(align) switch(align)
{ {
case AlignLeft: case AlignLeft:
cairo_move_to(cr, area.x + padding, area.y + font->ascent + (ctx->height - font->height) / 2); cairo_move_to(ctx->cr, area.x + padding, area.y + font->ascent + (ctx->height - font->height) / 2);
break; break;
case AlignRight: case AlignRight:
cairo_move_to(cr, area.x + (area.width - nw) + padding, cairo_move_to(ctx->cr, area.x + (area.width - nw) + padding,
area.y + font->ascent + (ctx->height - font->height) / 2); area.y + font->ascent + (ctx->height - font->height) / 2);
break; break;
default: default:
cairo_move_to(cr, area.x + ((area.width - nw) / 2) + padding, cairo_move_to(ctx->cr, area.x + ((area.width - nw) / 2) + padding,
area.y + font->ascent + (ctx->height - font->height) / 2); area.y + font->ascent + (ctx->height - font->height) / 2);
break; break;
} }
cairo_show_text(cr, buf);
cairo_show_text(ctx->cr, buf);
cairo_font_face_destroy(font_face); cairo_font_face_destroy(font_face);
cairo_destroy(cr);
cairo_surface_destroy(surface);
} }
/** Draw rectangle /** Draw rectangle
@ -137,27 +142,18 @@ draw_text(DrawCtx *ctx,
void void
draw_rectangle(DrawCtx *ctx, Area geometry, Bool filled, XColor color) draw_rectangle(DrawCtx *ctx, Area geometry, Bool filled, XColor color)
{ {
cairo_surface_t *surface; cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
cairo_t *cr; cairo_set_line_width(ctx->cr, 1.0);
cairo_set_source_rgb(ctx->cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
surface = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
cr = cairo_create (surface);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
cairo_set_line_width(cr, 1.0);
cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
if(filled) if(filled)
{ {
cairo_rectangle(cr, geometry.x, geometry.y, geometry.width, geometry.height); cairo_rectangle(ctx->cr, geometry.x, geometry.y, geometry.width, geometry.height);
cairo_fill(cr); cairo_fill(ctx->cr);
} }
else else
cairo_rectangle(cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1); cairo_rectangle(ctx->cr, geometry.x + 1, geometry.y, geometry.width - 1, geometry.height - 1);
cairo_stroke(cr); cairo_stroke(ctx->cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
} }
/* draw_graph functions */ /* draw_graph functions */
@ -187,6 +183,7 @@ void
draw_graph(cairo_t *cr, int x, int y, int w, int *from, int *to, int cur_index, XColor color) draw_graph(cairo_t *cr, int x, int y, int w, int *from, int *to, int cur_index, XColor color)
{ {
int i; int i;
cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0); cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
i = -1; i = -1;
@ -199,6 +196,7 @@ draw_graph(cairo_t *cr, int x, int y, int w, int *from, int *to, int cur_index,
if (--cur_index < 0) if (--cur_index < 0)
cur_index = w - 1; cur_index = w - 1;
} }
cairo_stroke(cr); cairo_stroke(cr);
} }
void void
@ -240,82 +238,70 @@ draw_graph_line(cairo_t *cr, int x, int y, int w, int *to, int cur_index, XColor
void void
draw_circle(DrawCtx *ctx, int x, int y, int r, Bool filled, XColor color) draw_circle(DrawCtx *ctx, int x, int y, int r, Bool filled, XColor color)
{ {
cairo_surface_t *surface; cairo_set_line_width(ctx->cr, 1.0);
cairo_t *cr; cairo_set_source_rgb(ctx->cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
surface = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
cr = cairo_create (surface);
cairo_set_line_width(cr, 1.0);
cairo_set_source_rgb(cr, color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
if(filled) if(filled)
{ {
cairo_arc (cr, x + r, y + r, r, 0, 2 * M_PI); cairo_arc (ctx->cr, x + r, y + r, r, 0, 2 * M_PI);
cairo_fill(cr); cairo_fill(ctx->cr);
} }
else else
cairo_arc (cr, x + r, y + r, r - 1, 0, 2 * M_PI); cairo_arc (ctx->cr, x + r, y + r, r - 1, 0, 2 * M_PI);
cairo_stroke(cr); cairo_stroke(ctx->cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
} }
void draw_image_from_argb_data(DrawCtx *ctx, int x, int y, int w, int h, void draw_image_from_argb_data(DrawCtx *ctx, int x, int y, int w, int h,
int wanted_h, unsigned char *data) int wanted_h, unsigned char *data)
{ {
double ratio; double ratio;
cairo_surface_t *surface, *source;
cairo_t *cr; cairo_t *cr;
cairo_surface_t *source;
surface = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h, 0); source = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32, w, h, 0);
cr = cairo_create (surface); cr = cairo_create(ctx->surface);
if(wanted_h > 0 && h > 0) if(wanted_h > 0 && h > 0)
{ {
ratio = (double) wanted_h / (double) h; ratio = (double) wanted_h / (double) h;
cairo_scale(cr, ratio, ratio); cairo_scale(cr, ratio, ratio);
cairo_set_source_surface(cr, source, x / ratio, y / ratio); cairo_set_source_surface(cr, source, x / ratio, y / ratio);
} }
else
cairo_set_source_surface(cr, source, x, y);
cairo_paint(cr);
cairo_surface_destroy(source);
}
void
draw_image(DrawCtx *ctx, int x, int y, int wanted_h, const char *filename)
{
double ratio;
int h;
cairo_surface_t *source;
cairo_t *cr;
cairo_status_t cairo_st;
source = cairo_image_surface_create_from_png(filename);
if((cairo_st = cairo_surface_status(source)))
{
warn("failed to draw image %s: %s\n", filename, cairo_status_to_string(cairo_st));
return;
}
cr = cairo_create (ctx->surface);
if(wanted_h > 0 && (h = cairo_image_surface_get_height(source)) > 0)
{
ratio = (double) wanted_h / (double) h;
cairo_scale(cr, ratio, ratio);
cairo_set_source_surface(cr, source, x / ratio, y / ratio);
}
else else
cairo_set_source_surface(cr, source, x, y); cairo_set_source_surface(cr, source, x, y);
cairo_paint(cr); cairo_paint(cr);
cairo_destroy(cr); cairo_destroy(cr);
cairo_surface_destroy(source); cairo_surface_destroy(source);
cairo_surface_destroy(surface);
}
void
draw_image(DrawCtx *ctx, int x, int y, int wanted_h, const char *filename)
{
double ratio;
int h;
cairo_surface_t *surface, *source;
cairo_t *cr;
cairo_status_t cairo_st;
source = cairo_xlib_surface_create(ctx->display, ctx->drawable, ctx->visual, ctx->width, ctx->height);
surface = cairo_image_surface_create_from_png(filename);
if((cairo_st = cairo_surface_status(surface)))
{
warn("failed to draw image %s: %s\n", filename, cairo_status_to_string(cairo_st));
return;
}
cr = cairo_create (source);
if(wanted_h > 0 && (h = cairo_image_surface_get_height(surface)) > 0)
{
ratio = (double) wanted_h / (double) h;
cairo_scale(cr, ratio, ratio);
cairo_set_source_surface(cr, surface, x / ratio, y / ratio);
}
else
cairo_set_source_surface(cr, surface, x, y);
cairo_paint(cr);
cairo_destroy(cr);
cairo_surface_destroy(source);
cairo_surface_destroy(surface);
} }
Area Area

View File

@ -86,9 +86,13 @@ typedef struct
int height; int height;
int phys_screen; int phys_screen;
int depth; int depth;
cairo_t *cr;
cairo_surface_t *surface;
} DrawCtx; } DrawCtx;
DrawCtx *draw_context_new(Display *, int, int, int, Drawable); DrawCtx *draw_context_new(Display *, int, int, int, Drawable);
void draw_context_delete(DrawCtx *);
void draw_text(DrawCtx *, Area, Alignment, int, XftFont *, const char *, XColor fg, XColor bg); void draw_text(DrawCtx *, Area, Alignment, int, XftFont *, const char *, XColor fg, XColor bg);
void draw_rectangle(DrawCtx *, Area, Bool, XColor); void draw_rectangle(DrawCtx *, Area, Bool, XColor);

View File

@ -150,7 +150,7 @@ statusbar_draw(Statusbar *statusbar)
break; break;
} }
p_delete(&ctx); draw_context_delete(ctx);
statusbar_display(statusbar); statusbar_display(statusbar);
} }