Skip to content

Instantly share code, notes, and snippets.

@qichunren
Created January 4, 2021 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qichunren/641c4fa7e77a2ab9579f960706a557e4 to your computer and use it in GitHub Desktop.
Save qichunren/641c4fa7e77a2ab9579f960706a557e4 to your computer and use it in GitHub Desktop.
Cairo demo
#include <cairo.h>
#include <stdio.h>
// sudo apt-get install libpango1.0-dev
// gcc cairo-demo.c $(pkg-config --cflags --libs pangocairo)
int main()
{
cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 200);
cairo_t *cr = cairo_create(surface);
cairo_surface_destroy(surface);
/* Fill everything with white */
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
/* Draw some text */
cairo_select_font_face(cr, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 32);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_move_to(cr, 0, 23);
cairo_show_text(cr, "ABC");
cairo_glyph_t glyph;
cairo_show_glyphs (cr, &glyph, 1);
cairo_text_extents_t extents;
cairo_text_extents(cr, "A", &extents);
printf("x_bearing:%f, y_bearing:%f\n", extents.x_bearing, extents.y_bearing);
printf("width:%f, height:%f\n", extents.width, extents.height);
printf("x_advance:%f, y_advance:%f\n", extents.x_advance, extents.y_advance);
cairo_surface_write_to_png(cairo_get_target(cr), "out.png");
cairo_destroy(cr);
return 0;
}
#include <cairo.h>
#include <stdio.h>
int main()
{
cairo_surface_t *surface = cairo_image_surface_create_from_png("/home/qichunren/code/pis/ntpis-broadcast/misc/test.png");
cairo_t *cr = cairo_create(surface);
unsigned char * data_buff = cairo_image_surface_get_data(surface);
int width = cairo_image_surface_get_width(surface);
int height = cairo_image_surface_get_height(surface);
int stride = cairo_image_surface_get_stride(surface);
int data_size = stride * height;
printf("width/height: %d/%d,stride:%d, data_size: %d\n", width, height, stride, data_size);
for(int i = 0; i < data_size;i++)
{
if(i%4 ==0)
{
printf("\n");
printf("%d: %02X ", i, data_buff[i]);
}
else
{
printf("%02X ", data_buff[i]);
}
}
printf("\n");
cairo_surface_destroy(surface);
// cairo_surface_write_to_png(cairo_get_target(cr), "out.png");
cairo_destroy(cr);
return 0;
}
#include <math.h>
#include <pango/pangocairo.h>
#define FONT "Sans Bold 27"
// sudo apt-get install libpango1.0-dev
// gcc cairo-demo.c $(pkg-config --cflags --libs pangocairo) -lm
static cairo_t*
create_layout_context ()
{
cairo_surface_t *temp_surface;
cairo_t *context;
temp_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
context = cairo_create (temp_surface);
cairo_surface_destroy (temp_surface);
return context;
}
static void
get_text_size (PangoLayout *layout,
unsigned int *width,
unsigned int *height)
{
pango_layout_get_pixel_size(layout, width, height);
}
static void
draw_text (cairo_t *cr, const char * text, int width, int height)
{
PangoLayout *layout;
PangoFontDescription *desc;
/* Create a PangoLayout, set the font and text */
layout = pango_cairo_create_layout (cr);
pango_layout_set_text (layout, text, -1);
desc = pango_font_description_from_string (FONT);
pango_layout_set_font_description(layout, desc);
pango_font_description_free (desc);
/* Draw the layout N_WORDS times in a circle */
cairo_save (cr);
/* Gradient from red at angle == 60 to blue at angle == 240 */
cairo_set_source_rgb (cr, 0, 0, 0);
/* Inform Pango to re-layout the text with the new transformation */
pango_cairo_update_layout (cr, layout);
// pango_layout_get_size (layout, &width, &height);
cairo_move_to (cr, 0, 0);
pango_cairo_show_layout (cr, layout);
cairo_restore (cr);
/* free the layout object */
g_object_unref (layout);
}
int main (int argc, char **argv)
{
const char * display_text = "临时停车";
cairo_t *cr;
cairo_t *temp_cr;
char *filename;
cairo_status_t status;
cairo_surface_t *surface;
cairo_surface_t *temp_surface;
filename = "/home/qichunren/code/pis/ntpis-broadcast/misc/aaa.png";
PangoLayout *layout;
temp_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0);
temp_cr = cairo_create(temp_surface);
cairo_surface_destroy(temp_surface);
/* Create a PangoLayout, set the font and text */
layout = pango_cairo_create_layout(temp_cr);
pango_layout_set_text(layout, display_text, -1);
PangoFontDescription * desc = pango_font_description_from_string(FONT);
pango_layout_set_font_description(layout, desc);
pango_font_description_free(desc);
unsigned int text_width;
unsigned int text_height;
pango_layout_get_pixel_size(layout, &text_width, &text_height);
cairo_destroy(temp_cr);
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, text_width, text_height);
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_paint(cr);
draw_text(cr, display_text, text_width, text_height);
cairo_destroy(cr);
status = cairo_surface_write_to_png(surface, filename);
cairo_surface_destroy (surface);
if (status != CAIRO_STATUS_SUCCESS)
{
g_printerr ("Could not save png to '%s'\n", filename);
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment