rename a bunch of draw functions
[awesome.git] / draw.c
blob4869c2a1510ebe616cfaf271746d1beb793d98dc
1 /*
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.
22 #include <cairo.h>
23 #include <cairo-ft.h>
24 #include <cairo-xlib.h>
25 #include <math.h>
26 #include "layout.h"
27 #include "util.h"
28 #include "draw.h"
30 DrawCtx *
31 draw_get_context(Display *display, int phys_screen, int width, int height)
33 DrawCtx *d;
34 d = p_new(DrawCtx, 1);
35 d->phys_screen = phys_screen;
36 d->display = display;
37 d->width = width;
38 d->height = height;
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);
44 return d;
47 void
48 draw_free_context(DrawCtx *ctx)
50 XFreePixmap(ctx->display, ctx->drawable);
51 p_delete(&ctx);
54 void
55 draw_text(DrawCtx *ctx, int x, int y, int w, int h, XftFont *font, const char *text, XColor fg, XColor bg)
57 int nw = 0;
58 static char buf[256];
59 size_t len, olen;
60 cairo_font_face_t *font_face;
61 cairo_surface_t *surface;
62 cairo_t *cr;
64 draw_rectangle(ctx, x, y, w, h, True, bg);
65 if(!a_strlen(text))
66 return;
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);
79 buf[len] = 0;
80 while(len && (nw = textwidth(ctx, font, buf)) > w)
81 buf[--len] = 0;
82 if(nw > w)
83 return; /* too long */
84 if(len < olen)
86 if(len > 1)
87 buf[len - 1] = '.';
88 if(len > 2)
89 buf[len - 2] = '.';
90 if(len > 3)
91 buf[len - 3] = '.';
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);
98 cairo_destroy(cr);
99 cairo_surface_destroy(surface);
102 void
103 draw_rectangle(DrawCtx *ctx, int x, int y, int w, int h, Bool filled, XColor color)
105 cairo_surface_t *surface;
106 cairo_t *cr;
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);
114 if(filled)
116 cairo_rectangle(cr, x, y, w + 1, h + 1);
117 cairo_fill(cr);
119 else
120 cairo_rectangle(cr, x + 1, y, w, h);
122 cairo_stroke(cr);
124 cairo_destroy(cr);
125 cairo_surface_destroy(surface);
128 void
129 draw_circle(DrawCtx *ctx, int x, int y, int r, Bool filled, XColor color)
131 cairo_surface_t *surface;
132 cairo_t *cr;
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);
138 if(filled)
140 cairo_arc (cr, x + r, y + r, r, 0, 2 * M_PI);
141 cairo_fill(cr);
143 else
144 cairo_arc (cr, x + r, y + r, r - 1, 0, 2 * M_PI);
146 cairo_stroke(cr);
148 cairo_destroy(cr);
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)
155 double ratio;
156 cairo_surface_t *surface, *source;
157 cairo_t *cr;
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);
168 else
169 cairo_set_source_surface(cr, source, x, y);
170 cairo_paint(cr);
172 cairo_destroy(cr);
173 cairo_surface_destroy(source);
174 cairo_surface_destroy(surface);
177 void
178 draw_image(DrawCtx *ctx, int x, int y, const char *filename)
180 cairo_surface_t *surface, *source;
181 cairo_t *cr;
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);
187 cairo_paint(cr);
189 cairo_destroy(cr);
190 cairo_surface_destroy(source);
191 cairo_surface_destroy(surface);
195 draw_get_image_width(const char *filename)
197 int width;
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);
205 return width;
209 Drawable
210 draw_rotate(DrawCtx *ctx, int screen, double angle, int tx, int ty)
212 cairo_surface_t *surface, *source;
213 cairo_t *cr;
214 Drawable newdrawable;
216 newdrawable = XCreatePixmap(ctx->display,
217 RootWindow(ctx->display, screen),
218 ctx->height, ctx->width,
219 ctx->depth);
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);
228 cairo_paint(cr);
230 cairo_destroy(cr);
231 cairo_surface_destroy(source);
232 cairo_surface_destroy(surface);
234 return newdrawable;
237 unsigned short
238 textwidth_primitive(Display *display, XftFont *font, char *text)
240 cairo_surface_t *surface;
241 cairo_t *cr;
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);
254 cairo_destroy(cr);
255 cairo_surface_destroy(surface);
256 cairo_font_face_destroy(font_face);
258 return MAX(te.x_advance, te.width) + font->height;
261 unsigned short
262 textwidth(DrawCtx *ctx, XftFont *font, char *text)
264 if (!text)
265 return 0;
266 return textwidth_primitive(ctx->display, font, text);
269 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80