2 * draw.c - draw functions
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <cairo-xlib.h>
31 draw_get_context(Display
*display
, int phys_screen
, int width
, int height
)
34 d
= p_new(DrawCtx
, 1);
35 d
->phys_screen
= phys_screen
;
39 d
->depth
= DefaultDepth(display
, phys_screen
);
40 d
->visual
= DefaultVisual(display
, phys_screen
);
41 d
->drawable
= XCreatePixmap(display
,
42 RootWindow(display
, phys_screen
),
43 width
, height
, d
->depth
);
48 draw_free_context(DrawCtx
*ctx
)
50 XFreePixmap(ctx
->display
, ctx
->drawable
);
55 draw_text(DrawCtx
*ctx
, int x
, int y
, int w
, int h
, XftFont
*font
, const char *text
, XColor fg
, XColor bg
)
60 cairo_font_face_t
*font_face
;
61 cairo_surface_t
*surface
;
64 draw_rectangle(ctx
, x
, y
, w
, h
, True
, bg
);
68 surface
= cairo_xlib_surface_create(ctx
->display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
69 cr
= cairo_create(surface
);
70 font_face
= cairo_ft_font_face_create_for_pattern(font
->pattern
);
71 cairo_set_font_face(cr
, font_face
);
72 cairo_set_font_size(cr
, font
->height
);
73 cairo_set_source_rgb(cr
, fg
.red
/ 65535.0, fg
.green
/ 65535.0, fg
.blue
/ 65535.0);
75 olen
= len
= a_strlen(text
);
76 if(len
>= sizeof(buf
))
77 len
= sizeof(buf
) - 1;
78 memcpy(buf
, text
, len
);
80 while(len
&& (nw
= textwidth(ctx
, font
, buf
)) > w
)
83 return; /* too long */
94 cairo_move_to(cr
, x
+ font
->height
/ 2, y
+ font
->ascent
+ (ctx
->height
- font
->height
) / 2);
95 cairo_show_text(cr
, buf
);
97 cairo_font_face_destroy(font_face
);
99 cairo_surface_destroy(surface
);
103 draw_rectangle(DrawCtx
*ctx
, int x
, int y
, int w
, int h
, Bool filled
, XColor color
)
105 cairo_surface_t
*surface
;
108 surface
= cairo_xlib_surface_create(ctx
->display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
109 cr
= cairo_create (surface
);
111 cairo_set_antialias(cr
, CAIRO_ANTIALIAS_NONE
);
112 cairo_set_line_width(cr
, 1.0);
113 cairo_set_source_rgb(cr
, color
.red
/ 65535.0, color
.green
/ 65535.0, color
.blue
/ 65535.0);
116 cairo_rectangle(cr
, x
, y
, w
+ 1, h
+ 1);
120 cairo_rectangle(cr
, x
+ 1, y
, w
, h
);
125 cairo_surface_destroy(surface
);
129 draw_circle(DrawCtx
*ctx
, int x
, int y
, int r
, Bool filled
, XColor color
)
131 cairo_surface_t
*surface
;
134 surface
= cairo_xlib_surface_create(ctx
->display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
135 cr
= cairo_create (surface
);
136 cairo_set_line_width(cr
, 1.0);
137 cairo_set_source_rgb(cr
, color
.red
/ 65535.0, color
.green
/ 65535.0, color
.blue
/ 65535.0);
140 cairo_arc (cr
, x
+ r
, y
+ r
, r
, 0, 2 * M_PI
);
144 cairo_arc (cr
, x
+ r
, y
+ r
, r
- 1, 0, 2 * M_PI
);
149 cairo_surface_destroy(surface
);
152 void draw_image_from_argb_data(DrawCtx
*ctx
, int x
, int y
, int w
, int h
,
153 int wanted_w
, unsigned char *data
)
156 cairo_surface_t
*surface
, *source
;
159 surface
= cairo_xlib_surface_create(ctx
->display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
160 source
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_ARGB32
, w
, h
, 0);
161 cr
= cairo_create (surface
);
162 if(wanted_w
> 0 && w
> 0)
164 ratio
= (double) wanted_w
/ (double) w
;
165 cairo_scale(cr
, ratio
, ratio
);
166 cairo_set_source_surface(cr
, source
, x
/ ratio
, y
/ ratio
);
169 cairo_set_source_surface(cr
, source
, x
, y
);
173 cairo_surface_destroy(source
);
174 cairo_surface_destroy(surface
);
178 draw_image(DrawCtx
*ctx
, int x
, int y
, const char *filename
)
180 cairo_surface_t
*surface
, *source
;
183 source
= cairo_xlib_surface_create(ctx
->display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
184 surface
= cairo_image_surface_create_from_png(filename
);
185 cr
= cairo_create (source
);
186 cairo_set_source_surface(cr
, surface
, x
, y
);
190 cairo_surface_destroy(source
);
191 cairo_surface_destroy(surface
);
195 draw_get_image_width(const char *filename
)
198 cairo_surface_t
*surface
;
200 surface
= cairo_image_surface_create_from_png(filename
);
201 cairo_image_surface_get_width(surface
);
202 width
= cairo_image_surface_get_width(surface
);
203 cairo_surface_destroy(surface
);
210 draw_rotate(DrawCtx
*ctx
, int screen
, double angle
, int tx
, int ty
)
212 cairo_surface_t
*surface
, *source
;
214 Drawable newdrawable
;
216 newdrawable
= XCreatePixmap(ctx
->display
,
217 RootWindow(ctx
->display
, screen
),
218 ctx
->height
, ctx
->width
,
220 surface
= cairo_xlib_surface_create(ctx
->display
, newdrawable
, ctx
->visual
, ctx
->height
, ctx
->width
);
221 source
= cairo_xlib_surface_create(ctx
->display
, ctx
->drawable
, ctx
->visual
, ctx
->width
, ctx
->height
);
222 cr
= cairo_create (surface
);
224 cairo_translate(cr
, tx
, ty
);
225 cairo_rotate(cr
, angle
);
227 cairo_set_source_surface(cr
, source
, 0.0, 0.0);
231 cairo_surface_destroy(source
);
232 cairo_surface_destroy(surface
);
238 textwidth_primitive(Display
*display
, XftFont
*font
, char *text
)
240 cairo_surface_t
*surface
;
242 cairo_font_face_t
*font_face
;
243 cairo_text_extents_t te
;
245 surface
= cairo_xlib_surface_create(display
, DefaultScreen(display
),
246 DefaultVisual(display
, DefaultScreen(display
)),
247 DisplayWidth(display
, DefaultScreen(display
)),
248 DisplayHeight(display
, DefaultScreen(display
)));
249 cr
= cairo_create(surface
);
250 font_face
= cairo_ft_font_face_create_for_pattern(font
->pattern
);
251 cairo_set_font_face(cr
, font_face
);
252 cairo_set_font_size(cr
, font
->height
);
253 cairo_text_extents(cr
, text
, &te
);
255 cairo_surface_destroy(surface
);
256 cairo_font_face_destroy(font_face
);
258 return MAX(te
.x_advance
, te
.width
) + font
->height
;
262 textwidth(DrawCtx
*ctx
, XftFont
*font
, char *text
)
266 return textwidth_primitive(ctx
->display
, font
, text
);
269 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80