/* Handle a size allocation by re-laying-out each paragraph to * the new width, setting the new size for the layout and * then queing a redraw */ void size_allocate (GtkWidget *layout, GtkAllocation *allocation) { GList *tmp_list; guint height = 0; PangoDirection base_dir = pango_context_get_base_dir (context); tmp_list = paragraphs; while (tmp_list) { Paragraph *para = tmp_list->data; PangoRectangle logical_rect; tmp_list = tmp_list->next; pango_layout_set_alignment (para->layout, base_dir == PANGO_DIRECTION_LTR ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT); pango_layout_set_width (para->layout, layout->allocation.width * PANGO_SCALE); pango_layout_get_extents (para->layout, NULL, &logical_rect); para->height = PANGO_PIXELS (logical_rect.height); height += para->height; } gtk_layout_set_size (GTK_LAYOUT (layout), allocation->width, height); if (GTK_LAYOUT (layout)->yoffset + allocation->height > height) gtk_adjustment_set_value (GTK_LAYOUT (layout)->vadjustment, (float)(height - allocation->height)); }
static gboolean on_entry_draw (GtkWidget *widget, cairo_t *cr, gpointer user_data) { g_debug (G_STRLOC ": %s", G_STRFUNC); GtkStyleContext *style_context; PangoContext *pango_context; PangoLayout *layout; const char *text; gint cursor_index; gint x, y; style_context = gtk_widget_get_style_context (widget); pango_context = gtk_widget_get_pango_context (widget); layout = gtk_entry_get_layout (GTK_ENTRY (widget)); text = pango_layout_get_text (layout); gtk_entry_get_layout_offsets (GTK_ENTRY (widget), &x, &y); cursor_index = g_utf8_offset_to_pointer (text, gtk_editable_get_position (GTK_EDITABLE (widget))) - text; gtk_render_insertion_cursor (style_context, cr, x, y, layout, cursor_index, pango_context_get_base_dir (pango_context)); return FALSE; }
unsigned Gosu::pango::textWidth(const std::wstring& text, const std::wstring& fontFace, unsigned fontHeight, unsigned fontFlags) { g_type_init(); int dpi_x = 100, dpi_y = 100; context = pango_ft2_get_context(dpi_x, dpi_y); pango_context_set_language(context, pango_language_from_string ("en_US")); PangoDirection init_dir = PANGO_DIRECTION_LTR; pango_context_set_base_dir(context, init_dir); // static PangoFontDescription *font_description; font_description = pango_font_description_new(); pango_font_description_set_family(font_description, g_strdup(narrow(fontFace).c_str())); pango_font_description_set_style(font_description, (fontFlags & ffItalic) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL); pango_font_description_set_variant(font_description, PANGO_VARIANT_NORMAL); pango_font_description_set_weight(font_description, (fontFlags & ffBold) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL); pango_font_description_set_stretch(font_description, PANGO_STRETCH_NORMAL); int init_scale = int(fontHeight/2.0 + 0.5); pango_font_description_set_size(font_description, init_scale * PANGO_SCALE); pango_context_set_font_description(context, font_description); layout = pango_layout_new(context); if(fontFlags & ffUnderline) { // PangoAttribute *attr; attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE); attr->start_index = 0; attr->end_index = text.length(); // PangoAttrList* attrList; attrList = pango_attr_list_new(); pango_attr_list_insert(attrList, attr); pango_layout_set_attributes(layout, attrList); pango_attr_list_unref(attrList); } // IMPR: Catch errors? (Last NULL-Pointer) gchar* utf8Str = g_ucs4_to_utf8((gunichar*)text.c_str(), text.length(), NULL, NULL, NULL); pango_layout_set_text(layout, utf8Str, -1); g_free(utf8Str); PangoDirection base_dir = pango_context_get_base_dir(context); pango_layout_set_alignment(layout, base_dir == PANGO_DIRECTION_LTR ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT); pango_layout_set_width(layout, -1); PangoRectangle logical_rect; pango_layout_get_pixel_extents(layout, NULL, &logical_rect); height = logical_rect.height; width = logical_rect.width; return width; }