2015-06-29 05:46:53 +00:00
|
|
|
/* Pango/Rotated Text
|
2004-11-21 16:24:01 +00:00
|
|
|
*
|
2009-03-27 16:54:10 +00:00
|
|
|
* This demo shows how to use PangoCairo to draw rotated and transformed
|
|
|
|
* text. The right pane shows a rotated GtkLabel widget.
|
2004-11-21 16:24:01 +00:00
|
|
|
*
|
2009-03-27 16:54:10 +00:00
|
|
|
* In both cases, a custom PangoCairo shape renderer is installed to draw
|
|
|
|
* a red heard using cairo drawing operations instead of the Unicode heart
|
|
|
|
* character.
|
2004-11-21 16:24:01 +00:00
|
|
|
*/
|
2009-03-27 16:54:10 +00:00
|
|
|
|
2004-11-21 16:24:01 +00:00
|
|
|
#include <gtk/gtk.h>
|
2009-03-27 16:54:10 +00:00
|
|
|
#include <string.h>
|
2004-11-21 16:24:01 +00:00
|
|
|
|
2009-03-27 16:54:10 +00:00
|
|
|
#define HEART "♥"
|
2019-02-05 09:19:57 +00:00
|
|
|
const char text[] = "I ♥ GTK";
|
2009-03-27 16:54:10 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
fancy_shape_renderer (cairo_t *cr,
|
2010-11-25 03:40:19 +00:00
|
|
|
PangoAttrShape *attr,
|
|
|
|
gboolean do_path,
|
|
|
|
gpointer data)
|
2009-03-27 16:54:10 +00:00
|
|
|
{
|
|
|
|
double x, y;
|
|
|
|
cairo_get_current_point (cr, &x, &y);
|
|
|
|
cairo_translate (cr, x, y);
|
|
|
|
|
|
|
|
cairo_scale (cr,
|
2010-11-25 03:40:19 +00:00
|
|
|
(double) attr->ink_rect.width / PANGO_SCALE,
|
|
|
|
(double) attr->ink_rect.height / PANGO_SCALE);
|
2009-03-27 16:54:10 +00:00
|
|
|
|
2020-03-28 04:04:25 +00:00
|
|
|
if (GPOINTER_TO_UINT (attr->data) == 0x2665) /* U+2665 BLACK HEART SUIT */
|
2009-03-27 16:54:10 +00:00
|
|
|
{
|
2020-03-07 14:01:21 +00:00
|
|
|
cairo_move_to (cr, .5, .0);
|
|
|
|
cairo_line_to (cr, .9, -.4);
|
|
|
|
cairo_curve_to (cr, 1.1, -.8, .5, -.9, .5, -.5);
|
|
|
|
cairo_curve_to (cr, .5, -.9, -.1, -.8, .1, -.4);
|
|
|
|
cairo_close_path (cr);
|
2009-03-27 16:54:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
if (!do_path)
|
|
|
|
{
|
|
|
|
cairo_set_source_rgb (cr, 1., 0., 0.);
|
|
|
|
cairo_fill (cr);
|
|
|
|
}
|
2009-03-27 16:54:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
static PangoAttrList *
|
2009-03-27 16:54:10 +00:00
|
|
|
create_fancy_attr_list_for_layout (PangoLayout *layout)
|
|
|
|
{
|
|
|
|
PangoAttrList *attrs;
|
|
|
|
PangoFontMetrics *metrics;
|
|
|
|
int ascent;
|
|
|
|
PangoRectangle ink_rect, logical_rect;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
/* Get font metrics and prepare fancy shape size */
|
|
|
|
metrics = pango_context_get_metrics (pango_layout_get_context (layout),
|
2010-11-25 03:40:19 +00:00
|
|
|
pango_layout_get_font_description (layout),
|
|
|
|
NULL);
|
2009-03-27 16:54:10 +00:00
|
|
|
ascent = pango_font_metrics_get_ascent (metrics);
|
|
|
|
logical_rect.x = 0;
|
|
|
|
logical_rect.width = ascent;
|
|
|
|
logical_rect.y = -ascent;
|
|
|
|
logical_rect.height = ascent;
|
|
|
|
ink_rect = logical_rect;
|
|
|
|
pango_font_metrics_unref (metrics);
|
|
|
|
|
|
|
|
/* Set fancy shape attributes for all hearts */
|
|
|
|
attrs = pango_attr_list_new ();
|
|
|
|
for (p = text; (p = strstr (p, HEART)); p += strlen (HEART))
|
|
|
|
{
|
|
|
|
PangoAttribute *attr;
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2009-03-27 16:54:10 +00:00
|
|
|
attr = pango_attr_shape_new_with_data (&ink_rect,
|
2010-11-25 03:40:19 +00:00
|
|
|
&logical_rect,
|
|
|
|
GUINT_TO_POINTER (g_utf8_get_char (p)),
|
|
|
|
NULL, NULL);
|
2009-03-27 16:54:10 +00:00
|
|
|
|
|
|
|
attr->start_index = p - text;
|
|
|
|
attr->end_index = attr->start_index + strlen (HEART);
|
|
|
|
|
|
|
|
pango_attr_list_insert (attrs, attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2016-11-18 05:16:38 +00:00
|
|
|
static void
|
|
|
|
rotated_text_draw (GtkDrawingArea *da,
|
|
|
|
cairo_t *cr,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
gpointer data)
|
2004-11-21 16:24:01 +00:00
|
|
|
{
|
|
|
|
#define RADIUS 150
|
2009-03-27 16:54:10 +00:00
|
|
|
#define N_WORDS 5
|
|
|
|
#define FONT "Serif 18"
|
|
|
|
|
2004-11-21 16:24:01 +00:00
|
|
|
PangoContext *context;
|
|
|
|
PangoLayout *layout;
|
|
|
|
PangoFontDescription *desc;
|
|
|
|
|
2009-03-27 16:54:10 +00:00
|
|
|
cairo_pattern_t *pattern;
|
|
|
|
|
|
|
|
PangoAttrList *attrs;
|
|
|
|
|
2004-11-21 16:24:01 +00:00
|
|
|
double device_radius;
|
|
|
|
int i;
|
|
|
|
|
2009-03-27 16:54:10 +00:00
|
|
|
/* Create a cairo context and set up a transformation matrix so that the user
|
|
|
|
* space coordinates for the centered square where we draw are [-RADIUS, RADIUS],
|
|
|
|
* [-RADIUS, RADIUS].
|
|
|
|
* We first center, then change the scale. */
|
2004-11-21 16:24:01 +00:00
|
|
|
device_radius = MIN (width, height) / 2.;
|
2009-03-27 16:54:10 +00:00
|
|
|
cairo_translate (cr,
|
2010-11-25 03:40:19 +00:00
|
|
|
device_radius + (width - 2 * device_radius) / 2,
|
|
|
|
device_radius + (height - 2 * device_radius) / 2);
|
2009-03-27 16:54:10 +00:00
|
|
|
cairo_scale (cr, device_radius / RADIUS, device_radius / RADIUS);
|
|
|
|
|
|
|
|
/* Create and a subtle gradient source and use it. */
|
|
|
|
pattern = cairo_pattern_create_linear (-RADIUS, -RADIUS, RADIUS, RADIUS);
|
|
|
|
cairo_pattern_add_color_stop_rgb (pattern, 0., .5, .0, .0);
|
|
|
|
cairo_pattern_add_color_stop_rgb (pattern, 1., .0, .0, .5);
|
|
|
|
cairo_set_source (cr, pattern);
|
|
|
|
|
|
|
|
/* Create a PangoContext and set up our shape renderer */
|
2016-11-18 05:16:38 +00:00
|
|
|
context = gtk_widget_create_pango_context (GTK_WIDGET (da));
|
2009-03-27 16:54:10 +00:00
|
|
|
pango_cairo_context_set_shape_renderer (context,
|
2010-11-25 03:40:19 +00:00
|
|
|
fancy_shape_renderer,
|
|
|
|
NULL, NULL);
|
2009-03-27 16:54:10 +00:00
|
|
|
|
|
|
|
/* Create a PangoLayout, set the text, font, and attributes */
|
2004-11-21 16:24:01 +00:00
|
|
|
layout = pango_layout_new (context);
|
2009-03-27 16:54:10 +00:00
|
|
|
pango_layout_set_text (layout, text, -1);
|
2004-11-21 16:24:01 +00:00
|
|
|
desc = pango_font_description_from_string (FONT);
|
|
|
|
pango_layout_set_font_description (layout, desc);
|
2009-03-27 16:54:10 +00:00
|
|
|
|
|
|
|
attrs = create_fancy_attr_list_for_layout (layout);
|
|
|
|
pango_layout_set_attributes (layout, attrs);
|
|
|
|
pango_attr_list_unref (attrs);
|
2004-11-21 16:24:01 +00:00
|
|
|
|
|
|
|
/* Draw the layout N_WORDS times in a circle */
|
|
|
|
for (i = 0; i < N_WORDS; i++)
|
|
|
|
{
|
2020-03-07 14:01:21 +00:00
|
|
|
int layout_width, layout_height;
|
2004-11-21 16:24:01 +00:00
|
|
|
|
|
|
|
/* Inform Pango to re-layout the text with the new transformation matrix */
|
2009-03-27 16:54:10 +00:00
|
|
|
pango_cairo_update_layout (cr, layout);
|
2011-09-02 03:55:47 +00:00
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
pango_layout_get_pixel_size (layout, &layout_width, &layout_height);
|
|
|
|
cairo_move_to (cr, - layout_width / 2, - RADIUS * .9);
|
2009-03-27 16:54:10 +00:00
|
|
|
pango_cairo_show_layout (cr, layout);
|
2004-11-21 16:24:01 +00:00
|
|
|
|
2009-03-27 16:54:10 +00:00
|
|
|
/* Rotate for the next turn */
|
|
|
|
cairo_rotate (cr, G_PI*2 / N_WORDS);
|
|
|
|
}
|
2004-11-21 16:24:01 +00:00
|
|
|
|
|
|
|
/* free the objects we created */
|
2009-03-27 16:54:10 +00:00
|
|
|
pango_font_description_free (desc);
|
2004-11-21 16:24:01 +00:00
|
|
|
g_object_unref (layout);
|
|
|
|
g_object_unref (context);
|
2009-03-27 16:54:10 +00:00
|
|
|
cairo_pattern_destroy (pattern);
|
2004-11-21 16:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_rotated_text (GtkWidget *do_widget)
|
|
|
|
{
|
2015-06-28 13:23:28 +00:00
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
2004-11-21 16:24:01 +00:00
|
|
|
if (!window)
|
|
|
|
{
|
2009-03-27 16:54:10 +00:00
|
|
|
GtkWidget *box;
|
|
|
|
GtkWidget *drawing_area;
|
|
|
|
GtkWidget *label;
|
|
|
|
PangoLayout *layout;
|
|
|
|
PangoAttrList *attrs;
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2004-11-21 16:24:01 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Rotated Text");
|
2009-03-27 16:54:10 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 4 * RADIUS, 2 * RADIUS);
|
2015-06-28 13:23:28 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
|
|
|
G_CALLBACK (gtk_widget_destroyed), &window);
|
2004-11-21 16:24:01 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
|
|
|
|
gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
|
2009-03-27 16:54:10 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), box);
|
|
|
|
|
|
|
|
/* Add a drawing area */
|
2004-11-21 16:24:01 +00:00
|
|
|
drawing_area = gtk_drawing_area_new ();
|
2009-03-27 16:54:10 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (box), drawing_area);
|
2014-10-07 04:45:59 +00:00
|
|
|
gtk_style_context_add_class (gtk_widget_get_style_context (drawing_area),
|
|
|
|
GTK_STYLE_CLASS_VIEW);
|
2004-11-21 16:24:01 +00:00
|
|
|
|
2016-11-18 05:16:38 +00:00
|
|
|
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (drawing_area),
|
|
|
|
rotated_text_draw,
|
|
|
|
NULL, NULL);
|
2004-11-21 16:24:01 +00:00
|
|
|
|
2009-03-27 16:54:10 +00:00
|
|
|
/* And a label */
|
|
|
|
label = gtk_label_new (text);
|
|
|
|
gtk_container_add (GTK_CONTAINER (box), label);
|
|
|
|
|
|
|
|
/* Set up fancy stuff on the label */
|
2009-06-17 08:28:03 +00:00
|
|
|
layout = gtk_label_get_layout (GTK_LABEL (label));
|
2009-03-27 16:54:10 +00:00
|
|
|
pango_cairo_context_set_shape_renderer (pango_layout_get_context (layout),
|
2010-11-25 03:40:19 +00:00
|
|
|
fancy_shape_renderer,
|
|
|
|
NULL, NULL);
|
2009-03-27 16:54:10 +00:00
|
|
|
attrs = create_fancy_attr_list_for_layout (layout);
|
2009-06-17 08:28:03 +00:00
|
|
|
gtk_label_set_attributes (GTK_LABEL (label), attrs);
|
2009-03-27 16:54:10 +00:00
|
|
|
pango_attr_list_unref (attrs);
|
2004-11-21 16:24:01 +00:00
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2004-11-21 16:24:01 +00:00
|
|
|
else
|
2015-06-28 13:23:28 +00:00
|
|
|
gtk_widget_destroy (window);
|
2004-11-21 16:24:01 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|