2007-10-03 17:26:14 +02:00
|
|
|
/*
|
2007-09-12 14:29:51 +02:00
|
|
|
* draw.c - draw functions
|
2007-10-03 17:26:14 +02:00
|
|
|
*
|
2009-07-10 16:26:49 +02:00
|
|
|
* Copyright © 2007-2009 Julien Danjou <julien@danjou.info>
|
2007-10-03 17:26:14 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2007-09-12 14:29:51 +02:00
|
|
|
*/
|
2007-09-05 20:15:00 +02:00
|
|
|
|
2008-03-21 16:50:17 +01:00
|
|
|
#include <cairo-xcb.h>
|
|
|
|
|
2008-06-03 11:16:25 +02:00
|
|
|
#include "config.h"
|
2008-02-04 13:28:20 +01:00
|
|
|
|
|
|
|
#include <langinfo.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
#include <errno.h>
|
2008-04-25 15:50:36 +02:00
|
|
|
#include <ctype.h>
|
2007-10-11 17:06:55 +02:00
|
|
|
#include <math.h>
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2009-09-07 17:22:32 +02:00
|
|
|
#include "globalconf.h"
|
2009-07-10 16:26:49 +02:00
|
|
|
#include "screen.h"
|
2008-09-11 14:11:13 +02:00
|
|
|
|
2009-04-17 16:52:25 +02:00
|
|
|
#include "common/xutil.h"
|
2008-09-11 14:11:13 +02:00
|
|
|
|
2008-07-01 14:44:19 +02:00
|
|
|
/** Convert text from any charset to UTF-8 using iconv.
|
|
|
|
* \param iso The ISO string to convert.
|
|
|
|
* \param len The string size.
|
2008-12-03 14:37:35 +01:00
|
|
|
* \param dest The destination pointer. Memory will be allocated, up to you to
|
|
|
|
* free, like any char *.
|
|
|
|
* \param dlen The destination length, can be NULL.
|
|
|
|
* \return True if conversion was done.
|
2008-02-27 09:32:45 +01:00
|
|
|
*/
|
2008-12-03 14:37:35 +01:00
|
|
|
bool
|
|
|
|
draw_iso2utf8(const char *iso, size_t len, char **dest, ssize_t *dlen)
|
2008-02-04 13:28:20 +01:00
|
|
|
{
|
2008-09-16 19:51:14 +02:00
|
|
|
static iconv_t iso2utf8 = (iconv_t) -1;
|
2008-11-12 15:16:01 +01:00
|
|
|
static int8_t dont_need_convert = -1;
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2008-11-12 15:16:01 +01:00
|
|
|
if(dont_need_convert == -1)
|
|
|
|
dont_need_convert = !a_strcmp(nl_langinfo(CODESET), "UTF-8");
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2008-11-12 15:16:01 +01:00
|
|
|
if(!len || dont_need_convert)
|
2008-12-03 14:37:35 +01:00
|
|
|
return false;
|
2008-02-04 13:28:20 +01:00
|
|
|
|
|
|
|
if(iso2utf8 == (iconv_t) -1)
|
|
|
|
{
|
2008-07-09 08:48:51 +02:00
|
|
|
iso2utf8 = iconv_open("UTF-8", nl_langinfo(CODESET));
|
|
|
|
if(iso2utf8 == (iconv_t) -1)
|
|
|
|
{
|
|
|
|
if(errno == EINVAL)
|
|
|
|
warn("unable to convert text from %s to UTF-8, not available",
|
|
|
|
nl_langinfo(CODESET));
|
|
|
|
else
|
|
|
|
warn("unable to convert text: %s", strerror(errno));
|
|
|
|
|
2008-12-03 14:37:35 +01:00
|
|
|
return false;
|
2008-07-09 08:48:51 +02:00
|
|
|
}
|
2008-02-04 13:28:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-03 14:37:35 +01:00
|
|
|
size_t orig_utf8len, utf8len;
|
|
|
|
char *utf8;
|
|
|
|
|
|
|
|
orig_utf8len = utf8len = 2 * len + 1;
|
|
|
|
utf8 = *dest = p_new(char, utf8len);
|
2008-02-04 13:28:20 +01:00
|
|
|
|
2008-04-25 15:50:36 +02:00
|
|
|
if(iconv(iso2utf8, (char **) &iso, &len, &utf8, &utf8len) == (size_t) -1)
|
2008-02-04 13:28:20 +01:00
|
|
|
{
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("text conversion failed: %s", strerror(errno));
|
2008-12-03 14:37:35 +01:00
|
|
|
p_delete(dest);
|
2009-06-15 11:24:56 +02:00
|
|
|
return false;
|
2008-02-04 13:28:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-03 14:37:35 +01:00
|
|
|
if(dlen)
|
|
|
|
*dlen = orig_utf8len - utf8len;
|
|
|
|
|
|
|
|
return true;
|
2008-02-04 13:28:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-03 15:23:10 +01:00
|
|
|
/** Initialize a draw_text_context_t with text data.
|
|
|
|
* \param data The draw text context to init.
|
|
|
|
* \param str The text string to render.
|
|
|
|
* \param slen The text string length.
|
|
|
|
* \return True if everything is ok, false otherwise.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
draw_text_context_init(draw_text_context_t *data, const char *str, ssize_t slen)
|
2008-04-26 19:02:19 +02:00
|
|
|
{
|
2008-08-18 14:29:50 +02:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2009-06-15 11:24:56 +02:00
|
|
|
if(!str)
|
|
|
|
return false;
|
|
|
|
|
2008-12-03 15:23:10 +01:00
|
|
|
if(!pango_parse_markup(str, slen, 0, &data->attr_list, &data->text, NULL, &error))
|
2008-08-18 14:32:05 +02:00
|
|
|
{
|
|
|
|
warn("cannot parse pango markup: %s", error ? error->message : "unknown error");
|
|
|
|
if(error)
|
|
|
|
g_error_free(error);
|
2008-12-03 15:23:10 +01:00
|
|
|
return false;
|
2008-08-18 14:32:05 +02:00
|
|
|
}
|
2008-04-25 15:50:36 +02:00
|
|
|
|
2008-09-02 14:03:27 +02:00
|
|
|
data->len = a_strlen(data->text);
|
2008-04-26 19:02:19 +02:00
|
|
|
|
2008-12-03 15:23:10 +01:00
|
|
|
return true;
|
2008-04-25 15:50:36 +02:00
|
|
|
}
|
|
|
|
|
2008-09-20 19:31:38 +02:00
|
|
|
/** Initialize a new draw context.
|
|
|
|
* \param d The draw context to initialize.
|
|
|
|
* \param phys_screen Physical screen id.
|
|
|
|
* \param width Width.
|
|
|
|
* \param height Height.
|
|
|
|
* \param px Pixmap object to store.
|
|
|
|
* \param fg Foreground color.
|
|
|
|
* \param bg Background color.
|
|
|
|
*/
|
|
|
|
void
|
2010-08-16 13:47:40 +02:00
|
|
|
draw_context_init(draw_context_t *d,
|
2008-09-20 19:31:38 +02:00
|
|
|
int width, int height, xcb_pixmap_t px,
|
|
|
|
const xcolor_t *fg, const xcolor_t *bg)
|
|
|
|
{
|
|
|
|
d->width = width;
|
|
|
|
d->height = height;
|
|
|
|
d->pixmap = px;
|
2009-07-10 16:26:49 +02:00
|
|
|
d->surface = cairo_xcb_surface_create(globalconf.connection,
|
2010-08-16 13:59:51 +02:00
|
|
|
px, globalconf.visual,
|
2009-07-10 16:26:49 +02:00
|
|
|
width, height);
|
2008-09-20 19:31:38 +02:00
|
|
|
d->cr = cairo_create(d->surface);
|
|
|
|
d->layout = pango_cairo_create_layout(d->cr);
|
|
|
|
d->fg = *fg;
|
|
|
|
d->bg = *bg;
|
|
|
|
};
|
|
|
|
|
2008-04-29 11:51:59 +02:00
|
|
|
/** Draw text into a draw context.
|
|
|
|
* \param ctx Draw context to draw to.
|
2008-12-03 15:25:44 +01:00
|
|
|
* \param data Draw text context data.
|
2009-07-27 06:20:00 +02:00
|
|
|
* \param ellip Ellipsize mode.
|
2008-11-06 17:04:23 +01:00
|
|
|
* \param wrap Wrap mode.
|
2008-12-02 17:29:19 +01:00
|
|
|
* \param align Text alignment.
|
2009-08-25 17:35:57 +02:00
|
|
|
* \param valign Vertical text alignment.
|
2008-04-29 11:51:59 +02:00
|
|
|
* \param area Area to draw to.
|
2007-12-30 15:11:37 +01:00
|
|
|
*/
|
2007-09-15 15:16:53 +02:00
|
|
|
void
|
2008-12-03 15:25:44 +01:00
|
|
|
draw_text(draw_context_t *ctx, draw_text_context_t *data,
|
2008-12-03 15:23:10 +01:00
|
|
|
PangoEllipsizeMode ellip, PangoWrapMode wrap,
|
2009-08-27 15:38:17 +02:00
|
|
|
alignment_t align, alignment_t valign, area_t area)
|
2007-09-05 20:15:00 +02:00
|
|
|
{
|
2008-12-03 15:23:10 +01:00
|
|
|
pango_layout_set_text(ctx->layout, data->text, data->len);
|
2008-04-28 14:53:32 +02:00
|
|
|
pango_layout_set_width(ctx->layout,
|
2009-08-25 20:24:38 +02:00
|
|
|
pango_units_from_double(area.width));
|
|
|
|
pango_layout_set_height(ctx->layout, pango_units_from_double(area.height));
|
2008-11-06 17:04:23 +01:00
|
|
|
pango_layout_set_ellipsize(ctx->layout, ellip);
|
|
|
|
pango_layout_set_wrap(ctx->layout, wrap);
|
2008-12-03 15:23:10 +01:00
|
|
|
pango_layout_set_attributes(ctx->layout, data->attr_list);
|
2008-12-03 15:25:44 +01:00
|
|
|
pango_layout_set_font_description(ctx->layout, globalconf.font->desc);
|
2008-03-07 17:40:40 +01:00
|
|
|
|
2009-08-27 15:38:17 +02:00
|
|
|
PangoRectangle ext;
|
|
|
|
pango_layout_get_pixel_extents(ctx->layout, NULL, &ext);
|
|
|
|
|
|
|
|
switch(align)
|
|
|
|
{
|
|
|
|
case AlignCenter:
|
|
|
|
area.x += (area.width - ext.width) / 2;
|
|
|
|
break;
|
|
|
|
case AlignRight:
|
|
|
|
area.x += area.width - ext.width;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2008-01-31 18:18:15 +01:00
|
|
|
|
2009-08-25 17:35:57 +02:00
|
|
|
switch(valign)
|
|
|
|
{
|
|
|
|
case AlignCenter:
|
2009-08-27 15:38:17 +02:00
|
|
|
area.y += (area.height - ext.height) / 2;
|
2009-08-25 17:35:57 +02:00
|
|
|
break;
|
|
|
|
case AlignBottom:
|
2009-08-27 15:38:17 +02:00
|
|
|
area.y += area.height - ext.height;
|
2009-08-25 17:35:57 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-25 20:24:38 +02:00
|
|
|
cairo_move_to(ctx->cr, area.x, area.y);
|
2008-04-28 16:16:02 +02:00
|
|
|
|
2008-05-30 13:00:52 +02:00
|
|
|
cairo_set_source_rgba(ctx->cr,
|
2008-06-02 12:18:17 +02:00
|
|
|
ctx->fg.red / 65535.0,
|
|
|
|
ctx->fg.green / 65535.0,
|
|
|
|
ctx->fg.blue / 65535.0,
|
|
|
|
ctx->fg.alpha / 65535.0);
|
2008-03-17 12:38:49 +01:00
|
|
|
pango_cairo_update_layout(ctx->cr, ctx->layout);
|
|
|
|
pango_cairo_show_layout(ctx->cr, ctx->layout);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
|
|
|
|
2008-04-17 17:30:10 +02:00
|
|
|
/** Draw rectangle inside the coordinates
|
2008-01-12 23:38:31 +01:00
|
|
|
* \param ctx Draw context
|
|
|
|
* \param geometry geometry
|
2008-03-21 18:02:40 +01:00
|
|
|
* \param line_width line width
|
2008-02-27 09:32:45 +01:00
|
|
|
* \param filled fill rectangle?
|
2008-01-12 23:38:31 +01:00
|
|
|
* \param color color to use
|
|
|
|
*/
|
2007-09-15 15:16:53 +02:00
|
|
|
void
|
Streamline xcolor_t.
Do not have a ->name char * field, but a char[32] instead. This isn't a
big problem, the longest color in /etc/X11/rgb.txt is 23 chars long, and
if it becomes a problem one day, one could just strip the name and
generate an hexadecimal representation on the fly instead. But allocating
the name is asking for a lot of trouble.
Since we do not allocate anything anymore, just don't allocate anything
anymore at all, it avoids the mess of xcolor_copy/_wipe and fixes a lot of
sleeping bugs (p_dup were used e.g., which is wrong).
Pass xcolor_t *, xcolor_t becomes too big to be passed by value. Add
consts at some places.
xcolor_new allocates nothing, hence is renamed xcolor_init, has xcolor_t
as a first argument (OO-style, this is self), and doesn't touch the
structure at all if it returns false, which allow us to skip a lot of
intermediates values.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Julien Danjou <julien@danjou.info>
2008-07-02 10:14:42 +02:00
|
|
|
draw_rectangle(draw_context_t *ctx, area_t geometry,
|
2009-04-17 18:40:43 +02:00
|
|
|
float line_width, bool filled, const color_t *color)
|
2007-09-05 20:15:00 +02:00
|
|
|
{
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_set_antialias(ctx->cr, CAIRO_ANTIALIAS_NONE);
|
2008-03-21 18:02:40 +01:00
|
|
|
cairo_set_line_width(ctx->cr, line_width);
|
2008-04-13 02:09:10 +02:00
|
|
|
cairo_set_miter_limit(ctx->cr, 10.0);
|
2008-03-26 03:18:21 +01:00
|
|
|
cairo_set_line_join(ctx->cr, CAIRO_LINE_JOIN_MITER);
|
2008-05-30 13:00:52 +02:00
|
|
|
cairo_set_source_rgba(ctx->cr,
|
2009-04-17 18:40:43 +02:00
|
|
|
color->red / 255.0,
|
|
|
|
color->green / 255.0,
|
|
|
|
color->blue / 255.0,
|
|
|
|
color->alpha / 255.0);
|
2007-09-05 20:15:00 +02:00
|
|
|
if(filled)
|
|
|
|
{
|
2008-03-21 11:26:56 +01:00
|
|
|
cairo_rectangle(ctx->cr, geometry.x, geometry.y,
|
|
|
|
geometry.width, geometry.height);
|
2008-01-31 18:18:15 +01:00
|
|
|
cairo_fill(ctx->cr);
|
2007-09-05 20:15:00 +02:00
|
|
|
}
|
2007-09-20 20:11:33 +02:00
|
|
|
else
|
2008-02-04 11:16:30 +01:00
|
|
|
{
|
2008-04-17 17:30:10 +02:00
|
|
|
cairo_rectangle(ctx->cr, geometry.x + line_width / 2.0, geometry.y + line_width / 2.0,
|
|
|
|
geometry.width - line_width, geometry.height - line_width);
|
2008-02-04 11:16:30 +01:00
|
|
|
cairo_stroke(ctx->cr);
|
|
|
|
}
|
|
|
|
}
|
2008-03-21 11:26:56 +01:00
|
|
|
|
2010-08-20 12:02:31 +02:00
|
|
|
/** Draw an image to a draw context.
|
|
|
|
* \param ctx Draw context to draw to.
|
|
|
|
* \param x X coordinate.
|
|
|
|
* \param y Y coordinate.
|
|
|
|
* \param ratio The ratio to apply to the image.
|
|
|
|
* \param source The image to draw.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
draw_surface(draw_context_t *ctx, int x, int y,
|
|
|
|
double ratio, cairo_surface_t *source)
|
|
|
|
{
|
|
|
|
cairo_t *cr = cairo_create(ctx->surface);
|
|
|
|
cairo_scale(cr, ratio, ratio);
|
|
|
|
cairo_set_source_surface(cr, source, x / ratio, y / ratio);
|
|
|
|
cairo_paint(cr);
|
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
}
|
|
|
|
|
2008-06-04 16:13:41 +02:00
|
|
|
/** Rotate a pixmap.
|
2008-05-30 17:53:10 +02:00
|
|
|
* \param ctx Draw context to draw with.
|
|
|
|
* \param src Drawable to draw from.
|
|
|
|
* \param dest Drawable to draw to.
|
|
|
|
* \param src_w Drawable width.
|
|
|
|
* \param src_h Drawable height.
|
|
|
|
* \param dest_w Drawable width.
|
|
|
|
* \param dest_h Drawable height.
|
|
|
|
* \param angle angle to rotate.
|
|
|
|
* \param tx Translate to this x coordinate.
|
|
|
|
* \param ty Translate to this y coordinate.
|
2008-02-27 09:32:45 +01:00
|
|
|
*/
|
2008-03-20 09:17:34 +01:00
|
|
|
void
|
2008-05-30 17:53:10 +02:00
|
|
|
draw_rotate(draw_context_t *ctx,
|
2008-06-04 16:13:41 +02:00
|
|
|
xcb_pixmap_t src, xcb_pixmap_t dest,
|
2008-05-30 17:53:10 +02:00
|
|
|
int src_w, int src_h,
|
|
|
|
int dest_w, int dest_h,
|
2008-03-20 09:17:34 +01:00
|
|
|
double angle, int tx, int ty)
|
2007-11-11 18:59:11 +01:00
|
|
|
{
|
|
|
|
cairo_surface_t *surface, *source;
|
|
|
|
cairo_t *cr;
|
2007-11-11 22:27:00 +01:00
|
|
|
|
2008-09-11 14:11:13 +02:00
|
|
|
surface = cairo_xcb_surface_create(globalconf.connection, dest,
|
2010-08-16 13:59:51 +02:00
|
|
|
globalconf.visual,
|
2009-07-10 16:26:49 +02:00
|
|
|
dest_w, dest_h);
|
2008-09-11 14:11:13 +02:00
|
|
|
source = cairo_xcb_surface_create(globalconf.connection, src,
|
2010-08-16 13:59:51 +02:00
|
|
|
globalconf.visual,
|
2009-07-10 16:26:49 +02:00
|
|
|
src_w, src_h);
|
2007-11-11 18:59:11 +01:00
|
|
|
cr = cairo_create (surface);
|
2007-11-11 19:49:42 +01:00
|
|
|
|
|
|
|
cairo_translate(cr, tx, ty);
|
2007-11-11 21:13:37 +01:00
|
|
|
cairo_rotate(cr, angle);
|
2007-11-11 18:59:11 +01:00
|
|
|
|
|
|
|
cairo_set_source_surface(cr, source, 0.0, 0.0);
|
2007-11-11 19:49:42 +01:00
|
|
|
cairo_paint(cr);
|
2007-11-11 18:59:11 +01:00
|
|
|
|
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(source);
|
2007-11-11 22:38:29 +01:00
|
|
|
cairo_surface_destroy(surface);
|
2007-11-11 18:59:11 +01:00
|
|
|
}
|
|
|
|
|
2008-06-08 10:58:48 +02:00
|
|
|
/** Return the width and height of a text in pixel.
|
2008-12-03 15:23:10 +01:00
|
|
|
* \param data The draw context text data.
|
2008-06-08 10:58:48 +02:00
|
|
|
* \return Text height and width.
|
2008-02-27 09:32:45 +01:00
|
|
|
*/
|
2008-04-23 17:44:09 +02:00
|
|
|
area_t
|
2008-12-03 15:25:44 +01:00
|
|
|
draw_text_extents(draw_text_context_t *data)
|
2007-10-01 19:22:57 +02:00
|
|
|
{
|
2007-10-16 01:20:03 +02:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
2008-03-17 12:38:49 +01:00
|
|
|
PangoLayout *layout;
|
|
|
|
PangoRectangle ext;
|
2008-06-22 22:09:48 +02:00
|
|
|
area_t geom = { 0, 0, 0, 0 };
|
2008-06-23 13:09:42 +02:00
|
|
|
|
2008-12-03 15:23:10 +01:00
|
|
|
if(data->len <= 0)
|
2008-09-02 14:03:27 +02:00
|
|
|
return geom;
|
2008-04-25 15:50:36 +02:00
|
|
|
|
2008-11-04 17:50:10 +01:00
|
|
|
surface = cairo_xcb_surface_create(globalconf.connection,
|
|
|
|
globalconf.default_screen,
|
2010-08-16 13:59:51 +02:00
|
|
|
globalconf.visual,
|
2010-08-16 14:10:58 +02:00
|
|
|
globalconf.screen->width_in_pixels,
|
|
|
|
globalconf.screen->height_in_pixels);
|
2008-04-25 15:50:36 +02:00
|
|
|
|
2007-10-16 01:20:03 +02:00
|
|
|
cr = cairo_create(surface);
|
2008-03-17 12:38:49 +01:00
|
|
|
layout = pango_cairo_create_layout(cr);
|
2008-12-03 15:23:10 +01:00
|
|
|
pango_layout_set_text(layout, data->text, data->len);
|
|
|
|
pango_layout_set_attributes(layout, data->attr_list);
|
2008-12-03 15:25:44 +01:00
|
|
|
pango_layout_set_font_description(layout, globalconf.font->desc);
|
2008-03-17 12:38:49 +01:00
|
|
|
pango_layout_get_pixel_extents(layout, NULL, &ext);
|
|
|
|
g_object_unref(layout);
|
2007-10-16 01:20:03 +02:00
|
|
|
cairo_destroy(cr);
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
|
2008-04-23 17:44:09 +02:00
|
|
|
geom.width = ext.width;
|
2008-12-02 11:50:39 +01:00
|
|
|
geom.height = ext.height;
|
2008-04-23 17:44:09 +02:00
|
|
|
|
|
|
|
return geom;
|
2007-09-06 22:09:00 +02:00
|
|
|
}
|
2007-10-15 13:40:52 +02:00
|
|
|
|
2008-04-11 11:18:23 +02:00
|
|
|
/** Transform a string to a alignment_t type.
|
2009-03-06 13:43:03 +01:00
|
|
|
* Recognized string are flex, fixed, left, center, middle or right.
|
2009-08-30 07:26:19 +02:00
|
|
|
* \param align A string with align text.
|
2008-06-23 14:01:33 +02:00
|
|
|
* \return An alignment_t type.
|
2008-02-27 09:32:45 +01:00
|
|
|
*/
|
2008-04-11 11:18:23 +02:00
|
|
|
alignment_t
|
2010-09-02 19:04:48 +02:00
|
|
|
draw_align_fromstr(const char *align)
|
2008-01-03 16:02:32 +01:00
|
|
|
{
|
2010-09-02 19:04:48 +02:00
|
|
|
if(a_strcmp(align, "center") == 0)
|
|
|
|
return AlignCenter;
|
|
|
|
if(a_strcmp(align, "right") == 0)
|
|
|
|
return AlignRight;
|
|
|
|
if(a_strcmp(align, "top") == 0)
|
|
|
|
return AlignTop;
|
|
|
|
if(a_strcmp(align, "bottom") == 0)
|
|
|
|
return AlignBottom;
|
|
|
|
if(a_strcmp(align, "middle") == 0)
|
|
|
|
return AlignMiddle;
|
|
|
|
return AlignLeft;
|
2008-01-03 16:02:32 +01:00
|
|
|
}
|
|
|
|
|
2008-07-01 16:29:50 +02:00
|
|
|
/** Transform an alignment to a string.
|
|
|
|
* \param a The alignment.
|
|
|
|
* \return A string which must not be freed.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
draw_align_tostr(alignment_t a)
|
|
|
|
{
|
|
|
|
switch(a)
|
|
|
|
{
|
|
|
|
case AlignLeft: return "left";
|
|
|
|
case AlignCenter: return "center";
|
|
|
|
case AlignRight: return "right";
|
2008-11-04 15:48:18 +01:00
|
|
|
case AlignBottom: return "bottom";
|
|
|
|
case AlignTop: return "top";
|
2009-03-06 13:43:03 +01:00
|
|
|
case AlignMiddle: return "middle";
|
2008-07-01 16:29:50 +02:00
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-20 11:56:23 +02:00
|
|
|
static cairo_user_data_key_t data_key;
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
free_data(void *data)
|
|
|
|
{
|
|
|
|
p_delete(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create a surface object on the lua stack from this image data.
|
|
|
|
* \param L The lua stack.
|
|
|
|
* \param width The width of the image.
|
|
|
|
* \param height The height of the image
|
|
|
|
* \param data The image's data in ARGB format, will be copied by this function.
|
|
|
|
* \return Number of items pushed on the lua stack.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_surface_from_data(lua_State *L, int width, int height, uint32_t *data)
|
|
|
|
{
|
|
|
|
unsigned long int len = width * height;
|
|
|
|
unsigned char *buffer = p_dup(data, len);
|
|
|
|
cairo_surface_t *surface =
|
|
|
|
cairo_image_surface_create_for_data(buffer,
|
|
|
|
CAIRO_FORMAT_ARGB32,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
width*4);
|
|
|
|
/* This makes sure that buffer will be freed */
|
|
|
|
cairo_surface_set_user_data(surface, &data_key, buffer, &free_data);
|
|
|
|
|
|
|
|
/* This will increase the reference count of the surface */
|
|
|
|
int ret = oocairo_surface_push(globalconf.L, surface);
|
|
|
|
/* So we have to drop our own reference */
|
|
|
|
cairo_surface_destroy(surface);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Duplicate the specified image surface.
|
|
|
|
* \param surface The surface to copy
|
|
|
|
* \return A pointer to a new cairo image surface.
|
|
|
|
*/
|
|
|
|
cairo_surface_t *
|
|
|
|
draw_dup_image_surface(cairo_surface_t *surface)
|
|
|
|
{
|
|
|
|
cairo_surface_t *res = cairo_image_surface_create(
|
|
|
|
cairo_image_surface_get_format(surface),
|
|
|
|
cairo_image_surface_get_width(surface),
|
|
|
|
cairo_image_surface_get_height(surface));
|
|
|
|
|
|
|
|
cairo_t *cr = cairo_create(res);
|
|
|
|
cairo_set_source_surface(cr, surface, 0, 0);
|
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_paint(cr);
|
|
|
|
cairo_destroy(cr);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:24:15 +01:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|