ttext::ttext() : #if PANGO_VERSION_CHECK(1,22,0) context_(pango_font_map_create_context(pango_cairo_font_map_get_default())), #else context_(pango_cairo_font_map_create_context(( reinterpret_cast<PangoCairoFontMap*>(pango_cairo_font_map_get_default())))), #endif layout_(pango_layout_new(context_)), rect_(), surface_(), #ifdef SDL_GPU texture_(), #endif text_(), markedup_text_(false), link_aware_(false), link_color_(), font_class_(font::FONT_SANS_SERIF), font_size_(14), font_style_(STYLE_NORMAL), foreground_color_(0xFFFFFFFF), // solid white maximum_width_(-1), characters_per_line_(0), maximum_height_(-1), ellipse_mode_(PANGO_ELLIPSIZE_END), alignment_(PANGO_ALIGN_LEFT), maximum_length_(std::string::npos), calculation_dirty_(true), length_(0), surface_dirty_(true), surface_buffer_(nullptr) { // With 72 dpi the sizes are the same as with SDL_TTF so hardcoded. pango_cairo_context_set_resolution(context_, 72.0); pango_layout_set_ellipsize(layout_, ellipse_mode_); pango_layout_set_alignment(layout_, alignment_); pango_layout_set_wrap(layout_, PANGO_WRAP_WORD_CHAR); /* * Set the pango spacing a bit bigger since the default is deemed to small * http://www.wesnoth.org/forum/viewtopic.php?p=358832#p358832 */ pango_layout_set_spacing(layout_, 2 * PANGO_SCALE); cairo_font_options_t *fo = cairo_font_options_create(); cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL); cairo_font_options_set_hint_metrics(fo, CAIRO_HINT_METRICS_ON); #ifdef _WIN32 // Cairo on Windows (at least the latest available version from gtk.org // as of 2014-02-22, version 1.10.2) has issues with ClearType resulting // in glitchy anti-aliasing with CAIRO_ANTIALIAS_SUBPIXEL or // CAIRO_ANTIALIAS_DEFAULT, but not CAIRO_ANTIALIAS_GRAY, so we use that // as a workaround until the Windows package is updated to use a newer // version of Cairo (see Wesnoth bug #21648). cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_GRAY); #endif pango_cairo_context_set_font_options(context_, fo); cairo_font_options_destroy(fo); }
void TextAsset::applyLeading(cairo_t* cairoContext, PangoLayout* layout, PangoFontDescription* font) const { float lineHeight; charHeight(cairoContext, font, 'a', &lineHeight); const float lineSpacing = (_leadingMultiplier - 1.0) * lineHeight; pango_layout_set_spacing(layout, pango_units_from_double(lineSpacing)); }
/* creates a commonly used layout object from the given context for use in get_page_count and * draw_page */ static PangoLayout *setup_pango_layout(GtkPrintContext *context, PangoFontDescription *desc) { PangoLayout *layout; layout = gtk_print_context_create_pango_layout(context); pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); pango_layout_set_spacing(layout, 0); pango_layout_set_attributes(layout, NULL); pango_layout_set_font_description(layout, desc); return layout; }
ttext::ttext() : #if PANGO_VERSION_CHECK(1,22,0) context_(pango_font_map_create_context(pango_cairo_font_map_get_default())), #else context_(pango_cairo_font_map_create_context(( reinterpret_cast<PangoCairoFontMap*>(pango_cairo_font_map_get_default())))), #endif layout_(pango_layout_new(context_)), rect_(), surface_(), text_(), markedup_text_(false), link_aware_(false), link_color_(), font_class_(font::FONT_SANS_SERIF), font_size_(14), font_style_(STYLE_NORMAL), foreground_color_(0xFFFFFFFF), // solid white maximum_width_(-1), characters_per_line_(0), maximum_height_(-1), ellipse_mode_(PANGO_ELLIPSIZE_END), alignment_(PANGO_ALIGN_LEFT), maximum_length_(std::string::npos), calculation_dirty_(true), length_(0), surface_dirty_(true), surface_buffer_(nullptr) { // With 72 dpi the sizes are the same as with SDL_TTF so hardcoded. pango_cairo_context_set_resolution(context_, 72.0); pango_layout_set_ellipsize(layout_, ellipse_mode_); pango_layout_set_alignment(layout_, alignment_); pango_layout_set_wrap(layout_, PANGO_WRAP_WORD_CHAR); /* * Set the pango spacing a bit bigger since the default is deemed to small * http://www.wesnoth.org/forum/viewtopic.php?p=358832#p358832 */ pango_layout_set_spacing(layout_, 4 * PANGO_SCALE); cairo_font_options_t *fo = cairo_font_options_create(); cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL); cairo_font_options_set_hint_metrics(fo, CAIRO_HINT_METRICS_ON); cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_DEFAULT); pango_cairo_context_set_font_options(context_, fo); cairo_font_options_destroy(fo); }
static PangoLayout * make_new_page (PangoContext * context, PangoFontDescription * desc, gdouble height, gdouble width) { PangoLayout *layout; layout = pango_layout_new (context); pango_layout_set_justify (layout, TRUE); pango_layout_set_spacing (layout, 1.5 * PANGO_SCALE); pango_layout_set_width (layout, pango_units_from_double(width - SIDE_MARGIN)); pango_layout_set_height (layout, pango_units_from_double(height - EDGE_MARGIN)); pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR); pango_layout_set_font_description (layout, desc); return layout; }
static PangoLayout * text_layout_new( PangoContext *context, const char *text, const char *font, int width, int spacing, VipsAlign align, int dpi ) { PangoLayout *layout; PangoFontDescription *font_description; PangoAlignment palign; layout = pango_layout_new( context ); pango_layout_set_markup( layout, text, -1 ); font_description = pango_font_description_from_string( font ); pango_layout_set_font_description( layout, font_description ); pango_font_description_free( font_description ); if( width > 0 ) pango_layout_set_width( layout, width * PANGO_SCALE ); if( spacing > 0 ) pango_layout_set_spacing( layout, spacing * PANGO_SCALE ); switch( align ) { case VIPS_ALIGN_LOW: palign = PANGO_ALIGN_LEFT; break; case VIPS_ALIGN_CENTRE: palign = PANGO_ALIGN_CENTER; break; case VIPS_ALIGN_HIGH: palign = PANGO_ALIGN_RIGHT; break; default: palign = PANGO_ALIGN_LEFT; break; } pango_layout_set_alignment( layout, palign ); return( layout ); }
static void schgui_cairo_drafter_draw_text(SchGUICairoDrafter *drafter, const struct _SchText *text) { if (text != NULL) { int visible; sch_text_get_visible(text, &visible); if (visible) { SchGUICairoDrafterPrivate *privat = SCHGUI_CAIRO_DRAFTER_GET_PRIVATE(drafter); if (privat->cairo != NULL) { PangoLayout *layout; SchMultiline *multiline = sch_text_get_multiline(text); int point_size = sch_text_get_size(text); float height; int alignment; cairo_font_options_t *options; PangoContext *context; int baseline; PangoLayoutIter *iter; int index; int show; SchGUIDrawingCfgColor color; int enabled; sch_text_get_color(text, &index); enabled = schgui_drawing_cfg_get_color(privat->config, index, &color); if (enabled) { if (0) /* show ink rect */ { GeomBounds bounds; int success; success = schgui_cairo_drafter_text_bounds(drafter, text, &bounds); if (success) { cairo_set_source_rgb(privat->cairo, 1.0, 0, 0); cairo_move_to(privat->cairo, bounds.min_x, bounds.min_y); cairo_line_to(privat->cairo, bounds.max_x, bounds.min_y); cairo_stroke(privat->cairo); cairo_set_source_rgb(privat->cairo, 0.75, 0, 0); cairo_move_to(privat->cairo, bounds.max_x, bounds.min_y); cairo_line_to(privat->cairo, bounds.max_x, bounds.max_y); cairo_line_to(privat->cairo, bounds.min_x, bounds.max_y); cairo_line_to(privat->cairo, bounds.min_x, bounds.min_y); //cairo_close_path(privat->cairo); cairo_stroke(privat->cairo); cairo_set_source_rgb(privat->cairo, 0, 0, 0); } cairo_set_source_rgb(privat->cairo, 0, 0, 1.0); cairo_new_sub_path(privat->cairo); cairo_arc(privat->cairo, sch_text_get_x(text), sch_text_get_y(text), 10, 0, 2 * M_PI); cairo_stroke(privat->cairo); cairo_set_source_rgb(privat->cairo, 0, 0, 0); } cairo_save(privat->cairo); height = 1000 * point_size / 72; layout = pango_cairo_create_layout(privat->cairo); pango_cairo_context_set_resolution(pango_layout_get_context(layout), 936); // context = pango_layout_get_context(layout); // options = cairo_font_options_create (); // cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF); // cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_MEDIUM); // pango_cairo_context_set_font_options (context, options); // cairo_font_options_destroy (options); cairo_set_source_rgb(privat->cairo, color.red, color.green, color.blue); pango_font_description_set_size(privat->desc, point_size * PANGO_SCALE ); pango_layout_set_spacing(layout, 40000); pango_layout_set_font_description(layout, privat->desc); sch_text_get_show(text, &show); pango_layout_set_markup(layout, sch_multiline_peek_markup(multiline, show), -1); PangoFontMetrics *metrics = pango_context_get_metrics( pango_layout_get_context(layout), privat->desc, NULL ); cairo_move_to(privat->cairo, sch_text_get_x(text), sch_text_get_y(text)); cairo_rotate(privat->cairo, M_PI * sch_text_get_angle(text) / 180); cairo_scale(privat->cairo, 1, -1); baseline = pango_layout_get_baseline(layout); alignment = sch_text_get_alignment(text); #if 1 switch (alignment) { case 2: case 5: case 8: /* upper */ //cairo_rel_move_to(privat->cairo, 0, -pango_font_metrics_get_ascent(metrics)/(privat->zoom * PANGO_SCALE)); //cairo_rel_move_to(privat->cairo, 0, height); break; case 1: case 4: case 7: /* center */ cairo_rel_move_to(privat->cairo, 0, -pango_font_metrics_get_ascent(metrics)/(privat->zoom * PANGO_SCALE)); cairo_rel_move_to(privat->cairo, 0, height); cairo_rel_move_to(privat->cairo, 0, -pango_font_metrics_get_ascent(metrics) * sch_multiline_lines(multiline)/(2 * privat->zoom * PANGO_SCALE)); cairo_rel_move_to(privat->cairo, 0, -pango_font_metrics_get_descent(metrics) * (sch_multiline_lines(multiline) - 1)/(2 * privat->zoom * PANGO_SCALE)); break; case 0: case 3: case 6: default: /* lower */ //cairo_rel_move_to(privat->cairo, 0, -pango_font_metrics_get_ascent(metrics) * sch_multiline_lines(multiline)/(privat->zoom * PANGO_SCALE)); //cairo_rel_move_to(privat->cairo, 0, -pango_font_metrics_get_descent(metrics) * (sch_multiline_lines(multiline)-1)/(privat->zoom * PANGO_SCALE)); //cairo_rel_move_to(privat->cairo, 0, -pango_layout_get_spacing(layout) * (sch_multiline_lines(multiline)-1)/ PANGO_SCALE); iter = pango_layout_get_iter(layout); while (!pango_layout_iter_at_last_line(iter)) { pango_layout_iter_next_line(iter); } cairo_rel_move_to(privat->cairo, 0, -pango_layout_iter_get_baseline(iter) / PANGO_SCALE); pango_layout_iter_free(iter); } #endif //g_debug("Ascent: %d", pango_font_metrics_get_ascent(metrics)); //g_debug("Descent: %d", pango_font_metrics_get_descent(metrics)); //g_debug("Spacing: %d", pango_layout_get_spacing(layout)); //g_debug("Font size: %d", pango_font_description_get_size(privat->desc)); //g_debug("Baseline %d", pango_layout_get_baseline(layout)); pango_font_metrics_unref(metrics); pango_cairo_show_layout(privat->cairo, layout); cairo_restore(privat->cairo); g_object_unref(layout); } } } } }
static void schgui_cairo_text_draw(SchGUICairoDrawItem *item, cairo_t *cairo) { if (cairo != NULL) { SchGUICairoTextPrivate *privat = SCHGUI_CAIRO_TEXT_GET_PRIVATE(item); if (privat != NULL) { PangoLayout *layout = pango_cairo_create_layout(cairo); if (layout != NULL) { PangoLayoutIter *iter; int width; cairo_save(cairo); cairo_set_source_rgba(cairo, privat->red, privat->green, privat->blue, privat->alpha); pango_cairo_context_set_resolution(pango_layout_get_context(layout), 936); pango_layout_set_spacing(layout, 40000); pango_layout_set_font_description(layout, privat->font_desc); pango_layout_set_markup(layout, privat->markup, -1); cairo_move_to(cairo, privat->x, privat->y); cairo_rotate(cairo, privat->angle); cairo_scale(cairo, 1, -1); switch (privat->alignment) { case 2: case 5: case 8: /* upper */ break; case 1: case 4: case 7: /* center */ iter = pango_layout_get_iter(layout); while (!pango_layout_iter_at_last_line(iter)) { pango_layout_iter_next_line(iter); } cairo_rel_move_to(cairo, 0, -pango_layout_iter_get_baseline(iter) / PANGO_SCALE / 2); pango_layout_iter_free(iter); break; case 0: case 3: case 6: default: /* lower */ iter = pango_layout_get_iter(layout); while (!pango_layout_iter_at_last_line(iter)) { pango_layout_iter_next_line(iter); } cairo_rel_move_to(cairo, 0, -pango_layout_iter_get_baseline(iter) / PANGO_SCALE); pango_layout_iter_free(iter); } switch (privat->alignment) { case 3: case 4: case 5: /* center */ pango_layout_get_size(layout, &width, NULL); cairo_rel_move_to(cairo, -width / PANGO_SCALE / 2, 0); break; case 6: case 7: case 8: pango_layout_get_size(layout, &width, NULL); cairo_rel_move_to(cairo, -width / PANGO_SCALE, 0); /* right */ break; case 0: case 1: case 2: default: /* left */ ; } pango_cairo_show_layout(cairo, layout); cairo_restore(cairo); g_object_unref(layout); } } } }
static VALUE rg_set_spacing(VALUE self, VALUE spacing) { pango_layout_set_spacing(_SELF(self), NUM2INT(spacing)); return self; }
static void draw_text_bubble (cairo_t *cr, GtkWidget *widget, gdouble pointx, gdouble pointy) { static const double corner_radius = 9.0; static const double margin_top = 12.0; static const double margin_bottom = 12.0; static const double margin_left = 24.0; static const double margin_right = 24.0; CcTimezoneMapPrivate *priv = CC_TIMEZONE_MAP (widget)->priv; GtkAllocation alloc; PangoLayout *layout; PangoRectangle text_rect; double x; double y; double width; double height; if (!priv->bubble_text) return; gtk_widget_get_allocation (widget, &alloc); layout = gtk_widget_create_pango_layout (widget, NULL); /* Layout the text */ pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER); pango_layout_set_spacing (layout, 3); pango_layout_set_markup (layout, priv->bubble_text, -1); pango_layout_get_pixel_extents (layout, NULL, &text_rect); /* Calculate the bubble size based on the text layout size */ width = text_rect.width + margin_left + margin_right; height = text_rect.height + margin_top + margin_bottom; if (pointx < alloc.width / 2) x = pointx + 25; else x = pointx - width - 25; y = pointy - height / 2; /* Make sure it fits in the visible area */ x = CLAMP (x, 0, alloc.width - width); y = CLAMP (y, 0, alloc.height - height); cairo_save (cr); cairo_translate (cr, x, y); /* Draw the bubble */ cairo_new_sub_path (cr); cairo_arc (cr, width - corner_radius, corner_radius, corner_radius, radians (-90), radians (0)); cairo_arc (cr, width - corner_radius, height - corner_radius, corner_radius, radians (0), radians (90)); cairo_arc (cr, corner_radius, height - corner_radius, corner_radius, radians (90), radians (180)); cairo_arc (cr, corner_radius, corner_radius, corner_radius, radians (180), radians (270)); cairo_close_path (cr); cairo_set_source_rgba (cr, 0.2, 0.2, 0.2, 0.7); cairo_fill (cr); /* And finally draw the text */ cairo_set_source_rgb (cr, 1, 1, 1); cairo_move_to (cr, margin_left, margin_top); pango_cairo_show_layout (cr, layout); g_object_unref (layout); cairo_restore (cr); }
SetIconText(GtkStatusIcon *tray_icon, const char *text, const char *color) { // build background from image GdkPixbuf* special_icon = gdk_pixbuf_new_from_file("message-mail-new.png", NULL); // GError **error); GdkPixbuf *dest = gdk_pixbuf_copy(special_icon); int w=gdk_pixbuf_get_width(special_icon); int h=gdk_pixbuf_get_height(special_icon); // prepare colors/alpha GdkColormap* cmap=gdk_screen_get_system_colormap(gdk_screen_get_default()); int screen_depth=24; GdkVisual* visual = gdk_colormap_get_visual(cmap); screen_depth = visual->depth; GdkColor fore = { 0, 0, 0, 0 }; GdkColor alpha = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF}; gdk_color_parse(color, &fore); if(fore.red==alpha.red && fore.green==alpha.green && fore.blue==alpha.blue) { alpha.red=0; // make sure alpha is different from fore } gdk_colormap_alloc_color (cmap, &fore, TRUE, TRUE); gdk_colormap_alloc_color (cmap, &alpha, TRUE, TRUE); // build pixmap with rectangle GdkPixmap *pm = gdk_pixmap_new (NULL, w, h, screen_depth); cairo_t *cr = gdk_cairo_create(pm); gdk_cairo_set_source_color(cr, &alpha); /* void gdk_cairo_set_source_color (cairo_t *cr, */ /* const GdkColor *color); */ cairo_rectangle(cr, 0, 0, w, h); /* void cairo_rectangle (cairo_t *cr, */ /* double x, */ /* double y, */ /* double width, */ /* double height); */ cairo_set_source_rgb(cr, 1, 1, 1); cairo_fill(cr); // build text GtkWidget *scratch = gtk_window_new(GTK_WINDOW_TOPLEVEL); PangoLayout *layout = gtk_widget_create_pango_layout(scratch, NULL); gtk_widget_destroy(scratch); PangoFontDescription *fnt = pango_font_description_from_string("Sans 18"); pango_font_description_set_weight (fnt,PANGO_WEIGHT_SEMIBOLD); pango_layout_set_spacing (layout,0); pango_layout_set_font_description (layout, fnt); pango_layout_set_text (layout, (gchar *)text,-1); int tw=0; int th=0; int sz; int border=4; pango_layout_get_pixel_size(layout, &tw, &th); while( (tw>w - border || th > h - border)) //fit text to the icon by decreasing font size { sz=pango_font_description_get_size (fnt); if(sz<MIN_FONT_SIZE) { sz=MIN_FONT_SIZE; break; } sz-=PANGO_SCALE; pango_font_description_set_size (fnt,sz); pango_layout_set_font_description (layout, fnt); pango_layout_get_pixel_size(layout, &tw, &th); } pango_font_description_free (fnt); // center text int px, py; px=(w-tw)/2; py=(h-th)/2; // draw text on pixmap gdk_cairo_set_source_color(cr, &fore); cairo_move_to (cr, px, py); pango_cairo_show_layout (cr, layout); cairo_destroy(cr); g_object_unref (layout); GdkPixbuf *buf = gdk_pixbuf_get_from_drawable (NULL, pm, NULL, 0, 0, 0, 0, w, h); g_object_unref (pm); GdkPixbuf *alpha_buf = gdk_pixbuf_add_alpha(buf, TRUE, (guchar)alpha.red, (guchar)alpha.green, (guchar)alpha.blue); g_object_unref (buf); //merge the rendered text on top gdk_pixbuf_composite(alpha_buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255); g_object_unref(alpha_buf); /* gdk_pixbuf_composite(buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255); */ /* g_object_unref(buf); */ gtk_status_icon_set_from_pixbuf(GTK_STATUS_ICON(tray_icon), GDK_PIXBUF(dest)); }