Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function for writing marked up text to a GdkPixbuf through pangocairo
/* An example writing text to a GdkPixbuf through pangocairo.
Compile by:
gcc -o write-text-to-pixbuf `pkg-config --cflags --libs gdk-pixbuf-2.0 pangocairo` write-text-to-pixbuf.c
This example is released under the MIT license.
Copyright 2020 Dov Grobgeld <dov.grobgeld@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <cairo.h>
#include <pango/pango.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <pango/pangocairo.h>
static void die(const char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
vfprintf(stderr, fmt, ap);
exit(-1);
}
// Create a cairo context for a GdkPixbuf.
//
// Note:
// - The cairo context references the same underlying data. We therefore
// don't need to convert back from the cairo_t to the GdkPixbuf.
// - cairo and GdkPixbuf have swapped byte order for the R,G,B .
// We therefore ned to swap R and B when setting a cairo color.
cairo_t *pixbuf_to_cairo(GdkPixbuf *pixbuf)
{
cairo_format_t format = (gdk_pixbuf_get_has_alpha (pixbuf)) ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24;
gint width = gdk_pixbuf_get_width (pixbuf);
gint height = gdk_pixbuf_get_height (pixbuf);
gint stride = gdk_pixbuf_get_rowstride(pixbuf);
cairo_surface_t *surface = cairo_image_surface_create_for_data (
gdk_pixbuf_get_pixels(pixbuf),
format,
width,
height,
stride);
g_assert (surface != NULL);
cairo_status_t status = cairo_surface_write_to_png(surface,"s.png");
if (status != CAIRO_STATUS_SUCCESS)
die("Failed writing surface %s!\n", cairo_status_to_string(status));
cairo_t *cr = cairo_create (surface);
return cr;
}
#if 0
// Convert a cairo image surface to a pixbuf. We don't need this as
// the cairo context references the same pixel data!
GdkPixbuf *cairo_to_pixbuf(cairo_t *cr)
{
cairo_surface_t *surface = cairo_get_target(cr);
bool has_alpha = cairo_image_surface_get_format(surface) == CAIRO_FORMAT_ARGB32;
GdkPixbuf *pixbuf =
gdk_pixbuf_new_from_data(cairo_image_surface_get_data(surface),
GDK_COLORSPACE_RGB,
has_alpha,
8, // Bits per sample
cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface),
cairo_image_surface_get_stride(surface),
NULL,
NULL);
return pixbuf;
}
#endif
// A function for annotating GdkPixbuf images with text through pango
//
// fontname - A fontconfig description. E.g. "Sans Bold 15"
// markup - pango markup
// alpha - Alpha transparency A number between 0 and 1
// x_align, y_align - A number between 0 and 1
void write_text_to_pixbuf(GdkPixbuf *pixbuf,
const char *fontname,
PangoAlignment pango_alignment,
const char *colorname,
double alpha,
const char *markup,
int x_pos,
int y_pos,
double x_align,
double y_align)
{
PangoFontDescription *font_descr = pango_font_description_from_string(fontname);
cairo_t *cr = pixbuf_to_cairo(pixbuf);
PangoContext *context = pango_cairo_create_context(cr);
PangoLayout *pango_layout = pango_layout_new(context);
pango_layout_set_font_description(pango_layout, font_descr);
PangoRectangle logical_rect;
pango_layout_set_markup(pango_layout, markup, -1);
pango_layout_set_alignment (pango_layout, pango_alignment);
pango_layout_get_extents(pango_layout,
NULL,
&logical_rect);
cairo_move_to(cr,
x_pos - logical_rect.width * x_align/PANGO_SCALE,
y_pos - logical_rect.height * y_align/PANGO_SCALE);
// Lookup the color by pango
PangoColor color = {0,0,0}; // black by default
if (!pango_color_parse(&color,
colorname))
{
fprintf(stderr, "Failed parsing color: %s!", colorname);
// TBD - add exception handling
}
// Swap blue and red because of differences between GdkPixbuf and
// cairo
cairo_set_source_rgba(cr,
color.blue/0xffff,
color.green/0xffff,
color.red/0xffff,
alpha);
pango_cairo_show_layout(cr, pango_layout);
cairo_surface_destroy(cairo_get_target(cr));
cairo_destroy(cr);
g_object_unref(pango_layout);
g_object_unref(context);
}
int main(int argc, char **argv)
{
int argp = 1;
GdkPixbuf *pixbuf = NULL;
const char *output_filename = "out.png";
if (argp >= argc)
die("Need name of image!\n");
const char* filename = argv[argp++];
pixbuf = gdk_pixbuf_new_from_file(filename,NULL);
g_assert(pixbuf!=NULL);
write_text_to_pixbuf(pixbuf,
"Serif Bold 72",
PANGO_ALIGN_CENTER,
"white",
0.8, // alpha
"Jackie\nis a\ncat",
gdk_pixbuf_get_width(pixbuf)/2,
20,
0.5, // x_align
0 // y_align
);
gdk_pixbuf_save(pixbuf,
output_filename,
"png",
NULL,
NULL);
g_object_unref(pixbuf);
exit(0);
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment