2 * draw.c - draw functions
4 * Copyright © 2007-2009 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-xcb.h>
32 #include <gdk-pixbuf/gdk-pixbuf.h>
34 #include "globalconf.h"
37 #include "common/xutil.h"
39 /** Convert text from any charset to UTF-8 using iconv.
40 * \param iso The ISO string to convert.
41 * \param len The string size.
42 * \param dest The destination pointer. Memory will be allocated, up to you to
43 * free, like any char *.
44 * \param dlen The destination length, can be NULL.
45 * \return True if conversion was done.
48 draw_iso2utf8(const char *iso
, size_t len
, char **dest
, ssize_t
*dlen
)
50 static iconv_t iso2utf8
= (iconv_t
) -1;
51 static int8_t dont_need_convert
= -1;
53 if(dont_need_convert
== -1)
54 dont_need_convert
= A_STREQ(nl_langinfo(CODESET
), "UTF-8");
56 if(!len
|| dont_need_convert
)
59 if(iso2utf8
== (iconv_t
) -1)
61 iso2utf8
= iconv_open("UTF-8", nl_langinfo(CODESET
));
62 if(iso2utf8
== (iconv_t
) -1)
65 warn("unable to convert text from %s to UTF-8, not available",
66 nl_langinfo(CODESET
));
68 warn("unable to convert text: %s", strerror(errno
));
74 size_t orig_utf8len
, utf8len
;
77 orig_utf8len
= utf8len
= 2 * len
+ 1;
78 utf8
= *dest
= p_new(char, utf8len
);
80 if(iconv(iso2utf8
, (char **) &iso
, &len
, &utf8
, &utf8len
) == (size_t) -1)
82 warn("text conversion failed: %s", strerror(errno
));
88 *dlen
= orig_utf8len
- utf8len
;
93 static cairo_user_data_key_t data_key
;
101 /** Create a surface object from this image data.
102 * \param L The lua stack.
103 * \param width The width of the image.
104 * \param height The height of the image
105 * \param data The image's data in ARGB format, will be copied by this function.
106 * \return Number of items pushed on the lua stack.
109 draw_surface_from_data(int width
, int height
, uint32_t *data
)
111 unsigned long int len
= width
* height
;
113 uint32_t *buffer
= p_new(uint32_t, len
);
114 cairo_surface_t
*surface
;
116 /* Cairo wants premultiplied alpha, meh :( */
117 for(i
= 0; i
< len
; i
++)
119 uint8_t a
= (data
[i
] >> 24) & 0xff;
120 double alpha
= a
/ 255.0;
121 uint8_t r
= ((data
[i
] >> 16) & 0xff) * alpha
;
122 uint8_t g
= ((data
[i
] >> 8) & 0xff) * alpha
;
123 uint8_t b
= ((data
[i
] >> 0) & 0xff) * alpha
;
124 buffer
[i
] = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
128 cairo_image_surface_create_for_data((unsigned char *) buffer
,
133 /* This makes sure that buffer will be freed */
134 cairo_surface_set_user_data(surface
, &data_key
, buffer
, &free_data
);
139 /** Create a surface object from this pixbuf
140 * \param buf The pixbuf
141 * \return Number of items pushed on the lua stack.
143 static cairo_surface_t
*
144 draw_surface_from_pixbuf(GdkPixbuf
*buf
)
146 int width
= gdk_pixbuf_get_width(buf
);
147 int height
= gdk_pixbuf_get_height(buf
);
148 int pix_stride
= gdk_pixbuf_get_rowstride(buf
);
149 guchar
*pixels
= gdk_pixbuf_get_pixels(buf
);
150 int channels
= gdk_pixbuf_get_n_channels(buf
);
151 cairo_surface_t
*surface
;
153 unsigned char *cairo_pixels
;
155 cairo_format_t format
= CAIRO_FORMAT_ARGB32
;
157 format
= CAIRO_FORMAT_RGB24
;
159 surface
= cairo_image_surface_create(format
, width
, height
);
160 cairo_surface_flush(surface
);
161 cairo_stride
= cairo_image_surface_get_stride(surface
);
162 cairo_pixels
= cairo_image_surface_get_data(surface
);
164 for (int y
= 0; y
< height
; y
++)
166 guchar
*row
= pixels
;
167 uint32_t *cairo
= (uint32_t *) cairo_pixels
;
168 for (int x
= 0; x
< width
; x
++) {
174 *cairo
++ = (r
<< 16) | (g
<< 8) | b
;
180 double alpha
= a
/ 255.0;
184 *cairo
++ = (a
<< 24) | (r
<< 16) | (g
<< 8) | b
;
187 pixels
+= pix_stride
;
188 cairo_pixels
+= cairo_stride
;
191 cairo_surface_mark_dirty(surface
);
195 /** Duplicate the specified image surface.
196 * \param surface The surface to copy
197 * \return A pointer to a new cairo image surface.
200 draw_dup_image_surface(cairo_surface_t
*surface
)
202 cairo_surface_t
*res
= cairo_image_surface_create(
203 cairo_image_surface_get_format(surface
),
204 cairo_image_surface_get_width(surface
),
205 cairo_image_surface_get_height(surface
));
207 cairo_t
*cr
= cairo_create(res
);
208 cairo_set_source_surface(cr
, surface
, 0, 0);
209 cairo_set_operator(cr
, CAIRO_OPERATOR_SOURCE
);
216 /** Load the specified path into a cairo surface
218 * \param path file to load
219 * \return A cairo image surface or NULL on error.
222 draw_load_image(lua_State
*L
, const char *path
)
224 GError
*error
= NULL
;
225 cairo_surface_t
*ret
;
226 GdkPixbuf
*buf
= gdk_pixbuf_new_from_file(path
, &error
);
230 lua_pushstring(L
, error
->message
);
237 ret
= draw_surface_from_pixbuf(buf
);
242 xcb_visualtype_t
*draw_find_visual(const xcb_screen_t
*s
, xcb_visualid_t visual
)
244 xcb_depth_iterator_t depth_iter
= xcb_screen_allowed_depths_iterator(s
);
247 for(; depth_iter
.rem
; xcb_depth_next (&depth_iter
))
248 for(xcb_visualtype_iterator_t visual_iter
= xcb_depth_visuals_iterator(depth_iter
.data
);
249 visual_iter
.rem
; xcb_visualtype_next (&visual_iter
))
250 if(visual
== visual_iter
.data
->visual_id
)
251 return visual_iter
.data
;
256 xcb_visualtype_t
*draw_default_visual(const xcb_screen_t
*s
)
258 return draw_find_visual(s
, s
->root_visual
);
261 xcb_visualtype_t
*draw_argb_visual(const xcb_screen_t
*s
)
263 xcb_depth_iterator_t depth_iter
= xcb_screen_allowed_depths_iterator(s
);
266 for(; depth_iter
.rem
; xcb_depth_next (&depth_iter
))
267 if(depth_iter
.data
->depth
== 32)
268 for(xcb_visualtype_iterator_t visual_iter
= xcb_depth_visuals_iterator(depth_iter
.data
);
269 visual_iter
.rem
; xcb_visualtype_next (&visual_iter
))
270 return visual_iter
.data
;
275 uint8_t draw_visual_depth(const xcb_screen_t
*s
, xcb_visualid_t vis
)
277 xcb_depth_iterator_t depth_iter
= xcb_screen_allowed_depths_iterator(s
);
280 for(; depth_iter
.rem
; xcb_depth_next (&depth_iter
))
281 for(xcb_visualtype_iterator_t visual_iter
= xcb_depth_visuals_iterator(depth_iter
.data
);
282 visual_iter
.rem
; xcb_visualtype_next (&visual_iter
))
283 if(vis
== visual_iter
.data
->visual_id
)
284 return depth_iter
.data
->depth
;
286 fatal("Could not find a visual's depth");
289 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80