summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2022-06-12 10:04:16 -0400
committerMatthias Clasen <mclasen@redhat.com>2022-06-22 13:54:02 -0400
commit4924c6452f6c0a23d253ffc9375cb695fc26c598 (patch)
treefab613465da74dcbc6c21edfc5d4027f077c26ab /examples
parent7d21242bb8aefe601d12bf94d41b88975ffacbba (diff)
downloadpango-4924c6452f6c0a23d253ffc9375cb695fc26c598.tar.gz
docs: Add an example for user fonts
Diffstat (limited to 'examples')
-rw-r--r--examples/cairoshape.c91
1 files changed, 40 insertions, 51 deletions
diff --git a/examples/cairoshape.c b/examples/cairoshape.c
index d3a90bd8..2fdb9c15 100644
--- a/examples/cairoshape.c
+++ b/examples/cairoshape.c
@@ -1,8 +1,5 @@
-/* Example code to show how to use pangocairo to render arbitrary shapes
- * inside a text layout, positioned by Pango. This has become possibly
- * using the following API added in Pango 1.18:
- *
- * pango_cairo_context_set_shape_renderer ()
+/* Example code to show how to use user fonts to render arbitrary content
+ * inside a text layout, positioned by Pango.
*
* This examples uses a small parser to convert shapes in the format of
* SVG paths to cairo instructions. You can typically extract these from
@@ -12,22 +9,18 @@
* text and automatically adds PangoAttribtues to the layout to replace
* each of the with a rendering of the GNOME Foot logo.
*
- *
* Written by Behdad Esfahbod, 2007
+ * Updated for user fonts by Matthias Clasen, 2022
*
* Permission to use, copy, modify, distribute, and sell this example
* for any purpose is hereby granted without fee.
* It is provided "as is" without express or implied warranty.
*/
-
#include <stdio.h>
#include <string.h>
#include <pango/pangocairo.h>
-#include <pango/pangofc-fontmap.h>
-
-static PangoFontMap *fontmap;
#define BULLET "•"
#define HEART "♥"
@@ -56,15 +49,13 @@ static MiniSvg GnomeFootLogo = {
};
static void
-mini_svg_render (MiniSvg *shape,
- cairo_t *cr,
- gboolean do_path)
+mini_svg_render (MiniSvg *shape, cairo_t *cr)
{
double x, y;
- const char *p;
char op[2];
int len;
double x1, y1, x2, y2, x3, y3;
+ const char *p;
cairo_get_current_point (cr, &x, &y);
cairo_translate (cr, x, y);
@@ -92,28 +83,28 @@ mini_svg_render (MiniSvg *shape,
break;
}
- if (!do_path)
- cairo_fill (cr);
+ cairo_fill (cr);
}
static PangoLayout *
get_layout (cairo_t *cr)
{
- PangoContext *context;
PangoLayout *layout;
PangoAttrList *attrs;
+ PangoFontDescription *font_desc;
const char *p;
/* Create a PangoLayout, set the font and text */
- context = pango_font_map_create_context (fontmap);
- layout = pango_layout_new (context);
- g_object_unref (context);
-
+ layout = pango_cairo_create_layout (cr);
pango_layout_set_text (layout, text, -1);
+ font_desc = pango_font_description_from_string ("Cantarell 12");
+ pango_layout_set_font_description (layout, font_desc);
+ pango_font_description_free (font_desc);
+
attrs = pango_attr_list_new ();
- PangoFontDescription *font_desc = pango_font_description_from_string ("Bullets 12");
+ font_desc = pango_font_description_from_string ("Bullets 12");
for (p = text; (p = strstr (p, BULLET)); p += strlen (BULLET))
{
@@ -133,11 +124,6 @@ get_layout (cairo_t *cr)
attr->start_index = p - text;
attr->end_index = attr->start_index + strlen (HEART);
pango_attr_list_insert (attrs, attr);
-
- attr = pango_attr_fallback_new (0);
- attr->start_index = p - text;
- attr->end_index = attr->start_index + strlen (HEART);
- pango_attr_list_insert (attrs, attr);
}
pango_font_description_free (font_desc);
@@ -145,30 +131,28 @@ get_layout (cairo_t *cr)
pango_layout_set_attributes (layout, attrs);
pango_attr_list_unref (attrs);
-
return layout;
}
static void
-draw_text (cairo_t *cr, int *width, int *height)
+measure_text (cairo_t *cr, int *width, int *height)
{
PangoLayout *layout = get_layout (cr);
PangoLines *lines = pango_layout_get_lines (layout);
+ PangoRectangle ext;
- pango_layout_write_to_file (layout, "out.layout");
+ pango_lines_get_extents (lines, NULL, &ext);
+ pango_extents_to_pixels (&ext, NULL);
- /* Adds a fixed 10-pixel margin on the sides. */
+ *width = ext.width + 20;
+ *height = ext.height + 20;
+}
- if (width || height)
- {
- PangoRectangle ext;
- pango_lines_get_extents (lines, NULL, &ext);
- pango_extents_to_pixels (&ext, NULL);
- if (width)
- *width = ext.width + 20;
- if (height)
- *height = ext.height + 20;
- }
+static void
+draw_text (cairo_t *cr)
+{
+ PangoLayout *layout = get_layout (cr);
+ PangoLines *lines = pango_layout_get_lines (layout);
cairo_move_to (cr, 10, 10);
pango_cairo_show_lines (cr, lines);
@@ -182,8 +166,7 @@ glyph_cb (PangoUserFace *face,
hb_codepoint_t *glyph,
gpointer data)
{
- if (unicode == 0x2022 || /* bullet */
- unicode == 0x2665) /* heart */
+ if (unicode == 0x2022 || unicode == 0x2665)
{
*glyph = unicode;
return TRUE;
@@ -211,7 +194,6 @@ glyph_info_cb (PangoUserFace *face,
*h_advance = size;
*v_advance = size;
-
*is_color = glyph == 0x2665;
return TRUE;
@@ -253,7 +235,9 @@ render_cb (PangoUserFace *face,
cairo_move_to (cr, 0, -1);
cairo_scale (cr, 1. / shape->width, 1. / shape->height);
- mini_svg_render (shape, cr, FALSE);
+ mini_svg_render (shape, cr);
+
+ return TRUE;
}
else if (glyph == 0x2665)
{
@@ -265,26 +249,32 @@ render_cb (PangoUserFace *face,
cairo_curve_to (cr, .5, -.9, -.1, -.8, .1, -.4);
cairo_close_path (cr);
cairo_fill (cr);
+
+ return TRUE;
}
- return TRUE;
+ return FALSE;
}
static void
-setup_fontmap (PangoFontMap *fontmap)
+setup_fontmap (void)
{
+ PangoFontMap *fontmap = pango_font_map_get_default ();
PangoFontDescription *desc;
PangoUserFace *face;
desc = pango_font_description_new ();
pango_font_description_set_family (desc, "Bullets");
+ /* Create our fancy user font, "Bullets Black" */
face = pango_user_face_new (font_info_cb,
glyph_cb,
glyph_info_cb,
NULL,
render_cb,
NULL, NULL, "Black", desc);
+
+ /* And add it to the default fontmap */
pango_font_map_add_face (fontmap, PANGO_FONT_FACE (face));
pango_font_description_free (desc);
@@ -307,12 +297,11 @@ main (int argc, char **argv)
filename = argv[1];
- fontmap = PANGO_FONT_MAP (pango_fc_font_map_new ());
- setup_fontmap (PANGO_FONT_MAP (fontmap));
+ setup_fontmap ();
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
cr = cairo_create (surface);
- draw_text (cr, &width, &height);
+ measure_text (cr, &width, &height);
cairo_destroy (cr);
cairo_surface_destroy (surface);
@@ -323,7 +312,7 @@ main (int argc, char **argv)
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
cairo_paint (cr);
cairo_set_source_rgb (cr, 0.0, 0.0, 0.5);
- draw_text (cr, NULL, NULL);
+ draw_text (cr);
cairo_destroy (cr);
/* Write out the surface as PNG */