/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ /* * example * Copyright (C) Hodong Kim 2013 * * example is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * example 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see ."; */ #include "example-page.h" G_DEFINE_TYPE (ExamplePage, example_page, G_TYPE_OBJECT); void example_page_get_size (ExamplePage *page, gdouble *width, gdouble *height) { g_return_if_fail (EXAMPLE_IS_PAGE (page)); *width = 595.0; *height = 842.0; } /* Example 1. Using Pango with Cairo */ /* https://developer.gnome.org/pango/1.34/pango-Cairo-Rendering.html */ #include #include static void draw_text (cairo_t *cr) { #define RADIUS 150 #define N_WORDS 10 #define FONT "Sans Bold 27" PangoLayout *layout; PangoFontDescription *desc; int i; /* Center coordinates on the middle of the region we are drawing */ cairo_translate (cr, RADIUS, RADIUS); /* 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 */ for (i = 0; i < N_WORDS; i++) { int width, height; double angle = (360. * i) / N_WORDS; double red; cairo_save (cr); /* Gradient from red at angle == 60 to blue at angle == 240 */ red = (1 + cos ((angle - 60) * G_PI / 180.)) / 2; cairo_set_source_rgb (cr, red, 0, 1.0 - red); cairo_rotate (cr, angle * G_PI / 180.); /* 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, - ((double)width / PANGO_SCALE) / 2, - RADIUS); pango_cairo_show_layout (cr, layout); cairo_restore (cr); } /* free the layout object */ g_object_unref (layout); } static void draw_sample (cairo_t *cr) { cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); cairo_set_font_size (cr, 20.0); cairo_move_to (cr, 10.0, 135.0); cairo_show_text (cr, "abcdefghijklm"); cairo_move_to (cr, 70.0, 165.0); cairo_text_path (cr, "nopqrstuvwxyz"); cairo_set_source_rgb (cr, 0.5, 0.5, 1); cairo_fill_preserve (cr); cairo_set_source_rgb (cr, 0, 0, 0); cairo_set_line_width (cr, 2.56); cairo_stroke (cr); /* draw helping lines */ cairo_set_source_rgba (cr, 1, 0.2, 0.2, 0.6); cairo_arc (cr, 10.0, 135.0, 5.12, 0, 2*M_PI); cairo_close_path (cr); cairo_arc (cr, 70.0, 165.0, 5.12, 0, 2*M_PI); cairo_fill (cr); } gboolean example_page_render (ExamplePage *page, cairo_t *cr) { g_return_val_if_fail (EXAMPLE_IS_PAGE (page), FALSE); /* draw_text (cr);*/ draw_sample (cr); return TRUE; } static void example_page_init (ExamplePage *example_page) { } static void example_page_finalize (GObject *object) { G_OBJECT_CLASS (example_page_parent_class)->finalize (object); } static void example_page_class_init (ExamplePageClass *klass) { GObjectClass* object_class = G_OBJECT_CLASS (klass); object_class->finalize = example_page_finalize; }