/* We don't support variable width fonts (yet) */ static gint get_font_width(GtkPrintContext *context, PangoFontDescription *desc) { PangoContext *pc; PangoFontMetrics *metrics; gint width; pc = gtk_print_context_create_pango_context(context); if (!utils_font_desc_check_monospace(pc, desc)) dialogs_show_msgbox_with_secondary(GTK_MESSAGE_WARNING, _("The editor font is not a monospaced font!"), _("Text will be wrongly spaced.")); metrics = pango_context_get_metrics(pc, desc, pango_context_get_language(pc)); /** TODO is this the best result we can get? */ /* digit and char width are mostly equal for monospace fonts, char width might be * for dual width characters(e.g. Japanese) so use digit width to get sure we get the width * for one character */ width = pango_font_metrics_get_approximate_digit_width(metrics) / PANGO_SCALE; pango_font_metrics_unref(metrics); g_object_unref(pc); return width; }
static gint matewnck_selector_get_width (GtkWidget *widget, const char *text) { GtkStyle *style; PangoContext *context; PangoFontMetrics *metrics; gint char_width; PangoLayout *layout; PangoRectangle natural; gint max_width; gint screen_width; gint width; gtk_widget_ensure_style (widget); style = gtk_widget_get_style (widget); context = gtk_widget_get_pango_context (widget); metrics = pango_context_get_metrics (context, style->font_desc, pango_context_get_language (context)); char_width = pango_font_metrics_get_approximate_char_width (metrics); pango_font_metrics_unref (metrics); max_width = PANGO_PIXELS (SELECTOR_MAX_WIDTH * char_width); layout = gtk_widget_create_pango_layout (widget, text); pango_layout_get_pixel_extents (layout, NULL, &natural); g_object_unref (G_OBJECT (layout)); screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget)); width = MIN (natural.width, max_width); width = MIN (width, 3 * (screen_width / 4)); return width; }
static void tab_label_style_set_cb (GtkWidget *hbox, GtkStyle *previous_style, gpointer user_data) { PangoFontMetrics *metrics; PangoContext *context; GtkWidget *button; int char_width, h, w; context = gtk_widget_get_pango_context (hbox); metrics = pango_context_get_metrics (context, hbox->style->font_desc, pango_context_get_language (context)); char_width = pango_font_metrics_get_approximate_digit_width (metrics); pango_font_metrics_unref (metrics); gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (hbox), GTK_ICON_SIZE_MENU, &w, &h); gtk_widget_set_size_request (hbox, TAB_WIDTH_N_CHARS * PANGO_PIXELS(char_width) + 2 * w, -1); button = g_object_get_data (G_OBJECT (hbox), "close-button"); gtk_widget_set_size_request (button, w + 2, h + 2); }
static bool pango_load (Lisp_Font *f) { PangoLanguage *language; PangoFontDescription *fontdesc; PangoFont *font; PangoFontMetrics *metrics; if (pango_context) { language = pango_context_get_language (pango_context); } else { char *langname, *p; #ifdef HAVE_PANGO_XFT pango_context = pango_xft_get_context (dpy, screen_num); #endif langname = g_strdup (setlocale (LC_CTYPE, NULL)); p = strchr (langname, '.'); if (p) *p = 0; p = strchr (langname, '@'); if (p) *p = 0; language = pango_language_from_string (langname); pango_context_set_language (pango_context, language); g_free (langname); } fontdesc = pango_font_description_from_string (rep_STR (f->name)); if (!pango_font_description_get_family (fontdesc)) pango_font_description_set_family (fontdesc, "Sans"); if (pango_font_description_get_size (fontdesc) <= 0) pango_font_description_set_size (fontdesc, 12 * PANGO_SCALE); pango_context_set_font_description (pango_context, fontdesc); font = pango_context_load_font (pango_context, fontdesc); if (!font) { pango_font_description_free(fontdesc); return FALSE; } metrics = pango_font_get_metrics (font, language); f->ascent = metrics->ascent / PANGO_SCALE; f->descent = metrics->descent / PANGO_SCALE; pango_font_metrics_unref (metrics); f->font = fontdesc; /* We save the font description, not the font itself! That's because it seems we can't recover it perfectly later, and the layout routines want a description */ return TRUE; }
/* * frame_update_titlebar_font * * Returns: void * Description: updates the titlebar font from the pango context, should * be called whenever the gtk style or font has changed */ void frame_update_titlebar_font (decor_frame_t *frame) { const PangoFontDescription *font_desc; PangoFontMetrics *metrics; PangoLanguage *lang; frame = gwd_decor_frame_ref (frame); font_desc = get_titlebar_font (frame); if (!font_desc) { GtkStyle *default_style; default_style = gtk_widget_get_default_style (); font_desc = default_style->font_desc; } pango_context_set_font_description (frame->pango_context, font_desc); lang = pango_context_get_language (frame->pango_context); metrics = pango_context_get_metrics (frame->pango_context, font_desc, lang); frame->text_height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)); gwd_decor_frame_unref (frame); pango_font_metrics_unref (metrics); }
static PangoFontMetrics * create_metrics_for_context (PangoFont *font, PangoContext *context) { PangoCairoWin32Font *cwfont = PANGO_CAIRO_WIN32_FONT (font); PangoFontMetrics *metrics; PangoFontDescription *font_desc; PangoLayout *layout; PangoRectangle extents; PangoLanguage *language = pango_context_get_language (context); const char *sample_str = pango_language_get_sample_string (language); cairo_scaled_font_t *scaled_font; cairo_font_extents_t font_extents; double height; metrics = pango_font_metrics_new (); scaled_font = pango_cairo_win32_font_get_scaled_font (PANGO_CAIRO_FONT (cwfont)); cairo_scaled_font_extents (scaled_font, &font_extents); cairo_win32_scaled_font_done_font (scaled_font); metrics->ascent = font_extents.ascent * PANGO_SCALE; metrics->descent = font_extents.descent * PANGO_SCALE; /* FIXME: Should get the real settings for these from the TrueType * font file. */ height = metrics->ascent + metrics->descent; metrics->underline_thickness = height / 14; metrics->underline_position = - metrics->underline_thickness; metrics->strikethrough_thickness = metrics->underline_thickness; metrics->strikethrough_position = height / 4; pango_quantize_line_geometry (&metrics->underline_thickness, &metrics->underline_position); pango_quantize_line_geometry (&metrics->strikethrough_thickness, &metrics->strikethrough_position); layout = pango_layout_new (context); font_desc = pango_font_describe_with_absolute_size (font); pango_layout_set_font_description (layout, font_desc); pango_layout_set_text (layout, sample_str, -1); pango_layout_get_extents (layout, NULL, &extents); metrics->approximate_char_width = extents.width / g_utf8_strlen (sample_str, -1); pango_layout_set_text (layout, "0123456789", -1); metrics->approximate_digit_width = max_glyph_width (layout); pango_font_description_free (font_desc); g_object_unref (layout); return metrics; }
int draw_get_listview_rowheight (drawctx_t *ctx) { PangoFontDescription *font_desc = ctx->font_style->font_desc; PangoFontMetrics *metrics = pango_context_get_metrics (ctx->pangoctx, font_desc, pango_context_get_language (ctx->pangoctx)); int row_height = (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)); pango_font_metrics_unref (metrics); return PANGO_PIXELS(row_height)+6; }
static void dma_data_view_data_size_request (DmaDataView *view, GtkRequisition *requisition) { PangoFontMetrics *metrics; PangoContext *context; context = gtk_widget_get_pango_context (view->data); metrics = pango_context_get_metrics (context, gtk_widget_get_style (view->data)->font_desc, pango_context_get_language (context)); requisition->height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)); requisition->width = (pango_font_metrics_get_approximate_char_width (metrics) + PANGO_SCALE - 1) / PANGO_SCALE; pango_font_metrics_unref (metrics); }
/* * frame_update_titlebar_font * * Returns: void * Description: updates the titlebar font from the pango context, should * be called whenever the gtk style or font has changed */ void frame_update_titlebar_font (decor_frame_t *frame) { const PangoFontDescription *font_desc; PangoFontDescription *free_font_desc; PangoFontMetrics *metrics; PangoLanguage *lang; free_font_desc = NULL; frame = gwd_decor_frame_ref (frame); font_desc = get_titlebar_font (frame); if (!font_desc) { GtkCssProvider *provider = gtk_css_provider_get_default (); GtkStyleContext *context = gtk_style_context_new (); GtkWidgetPath *path = gtk_widget_path_new (); gtk_widget_path_prepend_type (path, GTK_TYPE_WIDGET); gtk_style_context_set_path (context, path); gtk_widget_path_free (path); gtk_style_context_add_provider (context, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_FALLBACK); gtk_style_context_get (context, GTK_STATE_FLAG_NORMAL, "font", &free_font_desc, NULL); font_desc = (const PangoFontDescription *) free_font_desc; } pango_context_set_font_description (frame->pango_context, font_desc); lang = pango_context_get_language (frame->pango_context); metrics = pango_context_get_metrics (frame->pango_context, font_desc, lang); frame->text_height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)); gwd_decor_frame_unref (frame); pango_font_metrics_unref (metrics); if (free_font_desc) pango_font_description_free (free_font_desc); }
static PangoFontMetrics * pango_cairo_core_text_font_create_base_metrics_for_context (PangoCairoFont *font, PangoContext *context) { PangoCoreTextFont *cfont = (PangoCoreTextFont *) font; PangoFontMetrics *metrics; PangoFontDescription *font_desc; PangoLayout *layout; PangoRectangle extents; PangoLanguage *language = pango_context_get_language (context); const char *sample_str = pango_language_get_sample_string (language); CTFontRef ctfont; metrics = pango_font_metrics_new (); ctfont = pango_core_text_font_get_ctfont (cfont); metrics->ascent = CTFontGetAscent (ctfont) * PANGO_SCALE; metrics->descent = CTFontGetDescent (ctfont) * PANGO_SCALE; metrics->underline_position = CTFontGetUnderlinePosition (ctfont) * PANGO_SCALE; metrics->underline_thickness = CTFontGetUnderlineThickness (ctfont) * PANGO_SCALE; metrics->strikethrough_position = metrics->ascent / 3; metrics->strikethrough_thickness = CTFontGetUnderlineThickness (ctfont) * PANGO_SCALE; layout = pango_layout_new (context); font_desc = pango_font_describe_with_absolute_size ((PangoFont *) font); pango_layout_set_font_description (layout, font_desc); pango_layout_set_text (layout, sample_str, -1); pango_layout_get_extents (layout, NULL, &extents); metrics->approximate_char_width = extents.width / pango_utf8_strwidth (sample_str); pango_layout_set_text (layout, "0123456789", -1); metrics->approximate_digit_width = max_glyph_width (layout); pango_font_description_free (font_desc); g_object_unref (layout); return metrics; }
void gglk_text_init_tags(GglkText *tb) { PangoContext *context; PangoFontMetrics *metrics; PangoFontDescription *font_desc = NULL; GtkTextTag *tag; tb->buffer = gtk_text_view_get_buffer(&tb->view); gtk_text_buffer_get_end_iter(tb->buffer, &tb->iter); tb->startedit=gtk_text_buffer_create_mark(tb->buffer,NULL,&tb->iter,TRUE); tb->endedit =gtk_text_buffer_create_mark(tb->buffer,NULL,&tb->iter,FALSE); tb->scrollmark=gtk_text_buffer_create_mark(tb->buffer,NULL,&tb->iter,TRUE); tb->endmark =gtk_text_buffer_create_mark(tb->buffer,NULL,&tb->iter,FALSE); tb->hypermark=gtk_text_buffer_create_mark(tb->buffer,NULL,&tb->iter,TRUE); tag = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(tb->buffer), "Normal"); if(!tag) return; gglk_text_set_style(tb, tag); /* Measure default font size */ g_object_get(G_OBJECT(tag), "font-desc", &font_desc, NULL); font_desc = pango_font_description_copy(font_desc); pango_font_description_merge(font_desc, GTK_WIDGET(tb)->style->font_desc,FALSE); context = gtk_widget_get_pango_context(GTK_WIDGET(tb)); metrics = pango_context_get_metrics(context, font_desc, pango_context_get_language(context)); pango_font_description_free(font_desc); tb->xunits = PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width( metrics)); tb->yunits = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics) + pango_font_metrics_get_descent(metrics)); pango_font_metrics_unref(metrics); }
static void get_widget_char_size (GtkWidget *widget, gint *width, gint *height) { PangoFontMetrics *metrics; PangoContext *context; PangoFontDescription *font_desc; font_desc = pango_font_description_from_string ("Monospace 10"); context = gtk_widget_get_pango_context (widget); metrics = pango_context_get_metrics (context, /*widget->style->font_desc,*/ font_desc, pango_context_get_language (context)); *height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)); *width = (pango_font_metrics_get_approximate_char_width (metrics) + PANGO_SCALE - 1) / PANGO_SCALE; pango_font_metrics_unref (metrics); pango_font_description_free (font_desc); }
static void cdgetfontdim(cdCtxCanvas *ctxcanvas, int *max_width, int *height, int *ascent, int *descent) { PangoFontMetrics* metrics; int charwidth, charheight, charascent, chardescent; if(!ctxcanvas->fontdesc) return; metrics = pango_context_get_metrics(ctxcanvas->fontcontext, ctxcanvas->fontdesc, pango_context_get_language(ctxcanvas->fontcontext)); charascent = pango_font_metrics_get_ascent(metrics); chardescent = pango_font_metrics_get_descent(metrics); charheight = charascent + chardescent; charwidth = pango_font_metrics_get_approximate_char_width(metrics); if (max_width) *max_width = (((charwidth) + PANGO_SCALE/2) / PANGO_SCALE); if (height) *height = (((charheight) + PANGO_SCALE/2) / PANGO_SCALE); if (ascent) *ascent = (((charascent) + PANGO_SCALE/2) / PANGO_SCALE); if (descent) *descent = (((chardescent) + PANGO_SCALE/2) / PANGO_SCALE); pango_font_metrics_unref(metrics); }
static int get_font_height_for_widget (GtkWidget *widget) { PangoFontMetrics *metrics; PangoContext *context; int ascent; int descent; int height; gtk_widget_ensure_style (widget); context = gtk_widget_get_pango_context (widget); metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context)); ascent = pango_font_metrics_get_ascent (metrics); descent = pango_font_metrics_get_descent (metrics); height = PANGO_PIXELS (ascent + descent); pango_font_metrics_unref (metrics); return height; }
TGlyphCache *glyph_cache_new(GtkWidget *widget, PangoFontDescription *font_desc) { TGlyphCache *cache; cache = g_new(TGlyphCache, 1); memset(cache, 0, sizeof(*cache)); cache->context = gtk_widget_create_pango_context(widget); cache->lang = pango_context_get_language(cache->context); cache->display = gtk_widget_get_display(widget); g_object_ref (cache->display); cache->screen = gtk_widget_get_screen(widget); g_object_ref (cache->screen); if (font_desc) glyph_cache_set_font(cache, font_desc); return cache; }
static gint wnck_selector_get_width (GtkWidget *widget, const char *text) { GtkStyleContext *style_context; GtkStateFlags state; PangoContext *context; PangoFontMetrics *metrics; PangoFontDescription *description; gint char_width; PangoLayout *layout; PangoRectangle natural; gint max_width; gint screen_width; gint width; state = gtk_widget_get_state_flags (widget); style_context = gtk_widget_get_style_context (widget); gtk_style_context_get (style_context, state, GTK_STYLE_PROPERTY_FONT, &description, NULL); context = gtk_widget_get_pango_context (widget); metrics = pango_context_get_metrics (context, description, pango_context_get_language (context)); char_width = pango_font_metrics_get_approximate_char_width (metrics); pango_font_metrics_unref (metrics); max_width = PANGO_PIXELS (SELECTOR_MAX_WIDTH * char_width); layout = gtk_widget_create_pango_layout (widget, text); pango_layout_get_pixel_extents (layout, NULL, &natural); g_object_unref (G_OBJECT (layout)); screen_width = gdk_screen_get_width (gtk_widget_get_screen (widget)); width = MIN (natural.width, max_width); width = MIN (width, 3 * (screen_width / 4)); return width; }
static void bytes_view_ensure_layout(BytesView *bv) { if (bv->context == NULL) { bv->context = gtk_widget_get_pango_context(GTK_WIDGET(bv)); g_object_ref(bv->context); { PangoLanguage *lang; PangoFontMetrics *metrics; /* vte and xchat does it this way */ lang = pango_context_get_language(bv->context); metrics = pango_context_get_metrics(bv->context, bv->font, lang); bv->font_ascent = pango_font_metrics_get_ascent(metrics) / PANGO_SCALE; bv->font_descent = pango_font_metrics_get_descent(metrics) / PANGO_SCALE; pango_font_metrics_unref(metrics); bv->fontsize = bv->font_ascent + bv->font_descent; } g_assert(bv->context); bytes_view_adjustment_set(bv); } }
static IgtkFont* gtkFindFont(const char *standardfont) { PangoFontMetrics* metrics; PangoFontDescription* fontdesc; int i, is_underline = 0, is_strikeout = 0, count = iupArrayCount(gtk_fonts); IgtkFont* fonts = (IgtkFont*)iupArrayGetData(gtk_fonts); /* Check if the standardfont already exists in cache */ for (i = 0; i < count; i++) { if (iupStrEqualNoCase(standardfont, fonts[i].standardfont)) return &fonts[i]; } /* not found, create a new one */ { int size; int is_bold = 0, is_italic = 0; char typeface[1024]; char *new_standardfont = NULL; const char* mapped_name; if (!iupFontParsePango(standardfont, typeface, &size, &is_bold, &is_italic, &is_underline, &is_strikeout)) return NULL; mapped_name = iupFontGetPangoName(typeface); if (mapped_name) strcpy(typeface, mapped_name); if (is_underline || is_strikeout || size<0) new_standardfont = iupStrDup(standardfont); if (is_underline) { char* under = strstr(standardfont, "Underline"); int len = strlen(standardfont); int len1 = (size_t)(under-standardfont); memcpy(new_standardfont, standardfont, len1); memcpy(new_standardfont+len1, under+9+1, len-len1-9+1); /* strlen("Underline") */ standardfont = new_standardfont; } if (is_strikeout) { char* strike = strstr(standardfont, "Strikeout"); int len = strlen(standardfont); int len1 = (size_t)(strike-standardfont); memcpy(new_standardfont, standardfont, len1); memcpy(new_standardfont+len1, strike+9+1, len-len1-9+1); /* strlen("Strikeout") */ standardfont = new_standardfont; } if (size<0) { double res; int len1, len2; char *sz, size_str[10]; sprintf(size_str, "%d", size); sz = strstr(standardfont, size_str); len1 = (size_t)(sz-standardfont); res = ((double)gdk_screen_get_width(gdk_screen_get_default()) / (double)gdk_screen_get_width_mm(gdk_screen_get_default())); /* pixels/mm */ /* 1 point = 1/72 inch 1 inch = 25.4 mm */ /* pixel = ((point/72)*25.4)*pixel/mm */ size = (int)((-size/res)*2.83464567 + 0.5); /* from pixels to points */ len2 = sprintf(size_str, "%d", size); memcpy(new_standardfont, standardfont, len1); memcpy(new_standardfont+len1, size_str, len2+1); } fontdesc = pango_font_description_from_string(standardfont); if (new_standardfont) free(new_standardfont); } if (!fontdesc) return NULL; /* create room in the array */ fonts = (IgtkFont*)iupArrayInc(gtk_fonts); strcpy(fonts[i].standardfont, standardfont); fonts[i].fontdesc = fontdesc; fonts[i].strikethrough = pango_attr_strikethrough_new(is_strikeout? TRUE: FALSE); fonts[i].underline = pango_attr_underline_new(is_underline? PANGO_UNDERLINE_SINGLE: PANGO_UNDERLINE_NONE); fonts[i].layout = pango_layout_new(gtk_fonts_context); metrics = pango_context_get_metrics(gtk_fonts_context, fontdesc, pango_context_get_language(gtk_fonts_context)); fonts[i].charheight = pango_font_metrics_get_ascent(metrics) + pango_font_metrics_get_descent(metrics); fonts[i].charheight = IUPGTK_PANGOUNITS2PIXELS(fonts[i].charheight); fonts[i].charwidth = pango_font_metrics_get_approximate_char_width(metrics); fonts[i].charwidth = IUPGTK_PANGOUNITS2PIXELS(fonts[i].charwidth); pango_font_metrics_unref(metrics); gtkFontUpdate(&(fonts[i])); return &fonts[i]; }
static void thunar_text_renderer_set_widget (ThunarTextRenderer *text_renderer, GtkWidget *widget) { PangoFontMetrics *metrics; PangoContext *context; gint focus_padding; gint focus_line_width; if (G_LIKELY (widget == text_renderer->widget)) return; /* disconnect from the previously set widget */ if (G_UNLIKELY (text_renderer->widget != NULL)) { g_signal_handlers_disconnect_by_func (G_OBJECT (text_renderer->widget), thunar_text_renderer_invalidate, text_renderer); g_object_unref (G_OBJECT (text_renderer->layout)); g_object_unref (G_OBJECT (text_renderer->widget)); } /* activate the new widget */ text_renderer->widget = widget; /* connect to the new widget */ if (G_LIKELY (widget != NULL)) { /* take a reference on the widget */ g_object_ref (G_OBJECT (widget)); /* we need to recalculate the metrics when a new style (and thereby a new font) is set */ g_signal_connect_swapped (G_OBJECT (text_renderer->widget), "destroy", G_CALLBACK (thunar_text_renderer_invalidate), text_renderer); g_signal_connect_swapped (G_OBJECT (text_renderer->widget), "style-set", G_CALLBACK (thunar_text_renderer_invalidate), text_renderer); /* allocate a new pango layout for this widget */ context = gtk_widget_get_pango_context (widget); text_renderer->layout = pango_layout_new (context); /* disable automatic text direction, but use the direction specified by Gtk+ */ pango_layout_set_auto_dir (text_renderer->layout, FALSE); /* we don't want to interpret line separators in file names */ pango_layout_set_single_paragraph_mode (text_renderer->layout, TRUE); /* calculate the average character dimensions */ metrics = pango_context_get_metrics (context, widget->style->font_desc, pango_context_get_language (context)); text_renderer->char_width = PANGO_PIXELS (pango_font_metrics_get_approximate_char_width (metrics)); text_renderer->char_height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)); pango_font_metrics_unref (metrics); /* tell the cell renderer about the fixed height if we're not wrapping text */ if (G_LIKELY (text_renderer->wrap_width < 0)) gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (text_renderer), -1, text_renderer->char_height); /* determine the focus-padding and focus-line-width style properties from the widget */ gtk_widget_style_get (widget, "focus-padding", &focus_padding, "focus-line-width", &focus_line_width, NULL); text_renderer->focus_width = focus_padding + focus_line_width; } else { text_renderer->layout = NULL; text_renderer->char_width = 0; text_renderer->char_height = 0; } }
static void gtk_plot_gdk_draw_string (GtkPlotPC *pc, gint tx, gint ty, gint angle, const GdkColor *fg, const GdkColor *bg, gboolean transparent, gint border, gint border_space, gint border_width, gint shadow_width, const gchar *font_name, gint font_height, GtkJustification just, const gchar *text) { GdkGC *gc; GList *family = NULL; gint x0, y0; gint old_width, old_height; gboolean bold, italic; gint fontsize; gint ascent, descent; gint numf; gint width, height; gint x, y; gint i; PangoFontDescription *font = NULL, *latin_font = NULL; GtkPSFont *psfont, *base_psfont, *latin_psfont; gchar subs[2], insert_char; const gchar *aux = text; const gchar *lastchar = text; const gchar *wtext = text; const gchar *xaux = text; gchar new_text[strlen(text)+1]; gchar num[4]; PangoRectangle rect; PangoFontMetrics *metrics = NULL; PangoLayout *layout = NULL; gint real_x, real_y, real_width, real_height; GdkColor real_fg = *fg; GdkColor real_bg = *bg; PangoMatrix matrix = PANGO_MATRIX_INIT; PangoContext *context = GTK_PLOT_GDK(pc)->context; GdkDrawable *drawable = GTK_PLOT_GDK(pc)->drawable; gint sign_x = 1, sign_y = 0; if(!GTK_PLOT_GDK(pc)->drawable) return; if(!GTK_PLOT_GDK(pc)->gc) return; if(!text || strlen(text) == 0) return; gc = GTK_PLOT_GDK(pc)->gc; layout = GTK_PLOT_GDK(pc)->layout; if(!gc) return; gtk_plot_text_get_size(text, angle, font_name, font_height, &width, &height, &ascent, &descent); if(height == 0 || width == 0) return; old_width = width; old_height = height; if(angle == 90 || angle == 270) { old_width = height; old_height = width; } switch(angle){ case 90: sign_x = 0; sign_y = -1; break; case 180: sign_x = -1; sign_y = 0; break; case 270: sign_x = 0; sign_y = 1; break; case 0: default: sign_x = 1; sign_y = 0; break; } switch(just){ case GTK_JUSTIFY_LEFT: switch(angle){ case 0: ty -= ascent; break; case 90: ty -= height; tx -= ascent; break; case 180: tx -= width; ty -= descent; break; case 270: tx -= descent; break; } break; case GTK_JUSTIFY_RIGHT: switch(angle){ case 0: tx -= width; ty -= ascent; break; case 90: tx -= ascent; ty += height; break; case 180: tx += width; ty -= descent; break; case 270: tx -= descent; break; } break; case GTK_JUSTIFY_CENTER: default: switch(angle){ case 0: tx -= width / 2.; ty -= ascent; break; case 90: tx -= ascent; ty += height / 2.; break; case 180: tx += width / 2.; ty -= descent; break; case 270: tx -= descent; ty -= height / 2.; break; } } real_x = tx; real_y = ty; real_width = width; real_height = height; pango_matrix_rotate (&matrix, angle); pango_context_set_matrix (context, &matrix); pango_layout_context_changed (layout); if(!transparent){ gdk_gc_set_foreground(gc, &real_bg); gdk_draw_rectangle(drawable, gc, TRUE, tx, ty, old_width, old_height); } /* TEST */ /* gdk_draw_rectangle(text_pixmap, gc, FALSE, 0, 0, old_width-1, old_height-1); */ gtk_psfont_get_families(&family, &numf); base_psfont = psfont = gtk_psfont_get_by_name(font_name); font = gtk_psfont_get_font_description(psfont, font_height); italic = psfont->italic; bold = psfont->bold; fontsize = font_height; x0 = x = 0; y0 = y = 0; if (psfont->i18n_latinfamily) { latin_psfont = gtk_psfont_get_by_family(psfont->i18n_latinfamily, italic, bold); if(latin_font) pango_font_description_free(latin_font); latin_font = gtk_psfont_get_font_description(latin_psfont, fontsize); } else { latin_psfont = NULL; latin_font = NULL; } gdk_gc_set_foreground(gc, &real_fg); aux = text; while(aux && *aux != '\0' && *aux != '\n'){ if(*aux == '\\'){ aux = g_utf8_next_char(aux); switch(*aux){ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '9': psfont = gtk_psfont_get_by_family((gchar *)g_list_nth_data(family, *aux-'0'), italic, bold); pango_font_description_free(font); font = gtk_psfont_get_font_description(psfont, fontsize); aux = g_utf8_next_char(aux); break; case '8': case 'g': psfont = gtk_psfont_get_by_family("Symbol", italic, bold); pango_font_description_free(font); font = gtk_psfont_get_font_description(psfont, fontsize); aux = g_utf8_next_char(aux); break; case 'B': bold = TRUE; psfont = gtk_psfont_get_by_family(psfont->family, italic, bold); pango_font_description_free(font); font = gtk_psfont_get_font_description(psfont, fontsize); if(latin_font){ latin_font = NULL; } if (psfont->i18n_latinfamily) { latin_psfont = gtk_psfont_get_by_family(psfont->i18n_latinfamily, italic, bold); if(latin_font) pango_font_description_free(latin_font); latin_font = gtk_psfont_get_font_description(latin_psfont, fontsize); } aux = g_utf8_next_char(aux); break; case 'x': xaux = aux + 1; for (i=0; i<3; i++){ if (xaux[i] >= '0' && xaux[i] <= '9') num[i] = xaux[i]; else break; } if (i < 3){ aux = g_utf8_next_char(aux); break; } num[3] = '\0'; insert_char = (gchar)atoi(num); subs[0] = insert_char; subs[1] = '\0'; pango_layout_set_font_description(layout, font); pango_layout_set_text(layout, subs, 1); pango_layout_get_extents(layout, NULL, &rect); x += sign_x*PANGO_PIXELS(rect.width); y += sign_y*PANGO_PIXELS(rect.width); aux += 4; lastchar = aux - 1; break; case 'i': italic = TRUE; psfont = gtk_psfont_get_by_family(psfont->family, italic, bold); pango_font_description_free(font); font = gtk_psfont_get_font_description(psfont, fontsize); if (psfont->i18n_latinfamily) { latin_psfont = gtk_psfont_get_by_family(psfont->i18n_latinfamily, italic, bold); if(latin_font) pango_font_description_free(latin_font); latin_font = gtk_psfont_get_font_description(latin_psfont, fontsize); } aux = g_utf8_next_char(aux); break; case 'S': case '^': fontsize = (int)((gdouble)fontsize * 0.6 + 0.5); pango_font_description_free(font); font = gtk_psfont_get_font_description(psfont, fontsize); if(metrics) pango_font_metrics_unref(metrics); metrics = pango_context_get_metrics(pango_layout_get_context(layout), font, pango_context_get_language(pango_layout_get_context(layout))); if (psfont->i18n_latinfamily) { latin_font = gtk_psfont_get_font_description(latin_psfont, fontsize); } if(angle == 180) y = y0 + fontsize; else if(angle == 270) x = x0 + sign_y*fontsize; aux = g_utf8_next_char(aux); break; case 's': case '_': fontsize = (int)((gdouble)fontsize * 0.6 + 0.5); pango_font_description_free(font); font = gtk_psfont_get_font_description(psfont, fontsize); if(metrics) pango_font_metrics_unref(metrics); metrics = pango_context_get_metrics(pango_layout_get_context(layout), font, pango_context_get_language(pango_layout_get_context(layout))); if(angle == 0) y = y0 + fontsize; else if(angle == 90) x = x0 - sign_y*fontsize; if (psfont->i18n_latinfamily) { latin_font = gtk_psfont_get_font_description(latin_psfont, fontsize); } aux = g_utf8_next_char(aux); break; case '+': fontsize += 3; y -= sign_x*3; x += sign_y*3; pango_font_description_free(font); font = gtk_psfont_get_font_description(psfont, fontsize); if (psfont->i18n_latinfamily) { latin_font = gtk_psfont_get_font_description(latin_psfont, fontsize); } aux = g_utf8_next_char(aux); break; case '-': fontsize -= 3; y += sign_x*3; x -= sign_y*3; pango_font_description_free(font); font = gtk_psfont_get_font_description(psfont, fontsize); if (psfont->i18n_latinfamily) { latin_font = gtk_psfont_get_font_description(latin_psfont, fontsize); } aux = g_utf8_next_char(aux); break; case 'N': psfont = base_psfont; fontsize = font_height; pango_font_description_free(font); font = gtk_psfont_get_font_description(psfont, fontsize); if(angle == 0 || angle == 180) y = y0; else x = x0; italic = psfont->italic; bold = psfont->bold; aux = g_utf8_next_char(aux); break; case 'b': if (lastchar) { const gchar *aux2 = lastchar; gint i = g_utf8_prev_char(lastchar) != --aux2 ? 2 : 1; pango_layout_set_text(layout, lastchar, i); pango_layout_get_extents(layout, NULL, &rect); x -= sign_x*PANGO_PIXELS(rect.width); y -= sign_y*PANGO_PIXELS(rect.width); if (lastchar == wtext) lastchar = NULL; else lastchar = g_utf8_prev_char(lastchar); } else { pango_layout_set_text(layout, "X", 1); pango_layout_get_extents(layout, NULL, &rect); x -= sign_x*PANGO_PIXELS(rect.width); y -= sign_y*PANGO_PIXELS(rect.width); } aux = g_utf8_next_char(aux); break; default: if(aux && *aux != '\0' && *aux !='\n'){ gint new_width = 0; new_width = drawstring(pc, drawable, gc, angle, tx+x, ty+y, psfont, fontsize, aux); x += sign_x * new_width; y += sign_y * new_width; lastchar = aux; aux = g_utf8_next_char(aux); } break; } } else { gint new_len = 0; gint new_width = 0; lastchar = aux; while(aux && *aux != '\0' && *aux !='\n' && *aux != '\\'){ xaux = aux; new_len += g_utf8_next_char(aux) != ++xaux ? 2 : 1; aux = g_utf8_next_char(aux); } xaux = lastchar; for(i = 0; i < new_len; i++) new_text[i] = *xaux++; new_text[new_len] = '\0'; new_width = drawstring(pc, drawable, gc, angle, tx+x, ty+y, psfont, fontsize, new_text); x += sign_x * new_width; y += sign_y * new_width; lastchar = aux; } } pango_font_description_free(font); if(latin_font) pango_font_description_free(latin_font); if(metrics) pango_font_metrics_unref(metrics); /* border */ gdk_gc_set_foreground(gc, &real_fg); gtk_plot_pc_set_dash(pc, 0, NULL, 0); gtk_plot_pc_set_lineattr(pc, border_width, 0, 0, 0); switch(border){ case GTK_PLOT_BORDER_SHADOW: gtk_plot_pc_draw_rectangle(pc, TRUE, tx - border_space + shadow_width, ty + height + border_space, width + 2 * border_space, shadow_width); gtk_plot_pc_draw_rectangle(pc, TRUE, tx + width + border_space, ty - border_space + shadow_width, shadow_width, height + 2 * border_space); case GTK_PLOT_BORDER_LINE: gtk_plot_pc_draw_rectangle(pc, FALSE, tx - border_space, ty - border_space, width + 2*border_space, height + 2*border_space); case GTK_PLOT_BORDER_NONE: default: break; } return; }
/** * gail_misc_get_default_attributes: * @attrib_set: The #AtkAttributeSet to add the attribute to * @layout: The PangoLayout from which the attributes will be obtained * @widget: The GtkWidget for which the default attributes are required. * * Adds the default attributes to the specified attribute set. * * Returns: A pointer to the #AtkAttributeSet. **/ AtkAttributeSet* gail_misc_get_default_attributes (AtkAttributeSet *attrib_set, PangoLayout *layout, GtkWidget *widget) { PangoContext *context; GtkStyleContext *style_context; gint int_value; PangoWrapMode mode; GdkRGBA color; gchar *value; attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_DIRECTION, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_DIRECTION, gtk_widget_get_direction (widget)))); context = pango_layout_get_context (layout); if (context) { PangoLanguage* language; PangoFontDescription* font; language = pango_context_get_language (context); if (language) { attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_LANGUAGE, g_strdup (pango_language_to_string (language))); } font = pango_context_get_font_description (context); if (font) { attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_STYLE, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_STYLE, pango_font_description_get_style (font)))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_VARIANT, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_VARIANT, pango_font_description_get_variant (font)))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_STRETCH, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_STRETCH, pango_font_description_get_stretch (font)))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_FAMILY_NAME, g_strdup (pango_font_description_get_family (font))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_WEIGHT, g_strdup_printf ("%d", pango_font_description_get_weight (font))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_SIZE, g_strdup_printf ("%i", pango_font_description_get_size (font) / PANGO_SCALE)); } } if (pango_layout_get_justify (layout)) { int_value = 3; } else { PangoAlignment align; align = pango_layout_get_alignment (layout); if (align == PANGO_ALIGN_LEFT) int_value = 0; else if (align == PANGO_ALIGN_CENTER) int_value = 2; else /* if (align == PANGO_ALIGN_RIGHT) */ int_value = 1; } attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_JUSTIFICATION, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_JUSTIFICATION, int_value))); mode = pango_layout_get_wrap (layout); if (mode == PANGO_WRAP_WORD) int_value = 2; else /* if (mode == PANGO_WRAP_CHAR) */ int_value = 1; attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_WRAP_MODE, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_WRAP_MODE, int_value))); style_context = gtk_widget_get_style_context (widget); gtk_style_context_get_background_color (style_context, 0, &color); value = g_strdup_printf ("%u,%u,%u", (guint) ceil (color.red * 65536 - color.red), (guint) ceil (color.green * 65536 - color.green), (guint) ceil (color.blue * 65536 - color.blue)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_BG_COLOR, value); gtk_style_context_get_color (style_context, 0, &color); value = g_strdup_printf ("%u,%u,%u", (guint) ceil (color.red * 65536 - color.red), (guint) ceil (color.green * 65536 - color.green), (guint) ceil (color.blue * 65536 - color.blue)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_FG_COLOR, value); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_FG_STIPPLE, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_FG_STIPPLE, 0))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_BG_STIPPLE, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_BG_STIPPLE, 0))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_STRIKETHROUGH, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_STRIKETHROUGH, 0))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_UNDERLINE, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_UNDERLINE, 0))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_RISE, g_strdup_printf ("%i", 0)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_SCALE, g_strdup_printf ("%g", 1.0)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_BG_FULL_HEIGHT, g_strdup_printf ("%i", 0)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_PIXELS_INSIDE_WRAP, g_strdup_printf ("%i", 0)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_PIXELS_BELOW_LINES, g_strdup_printf ("%i", 0)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_PIXELS_ABOVE_LINES, g_strdup_printf ("%i", 0)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_EDITABLE, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_EDITABLE, 0))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_INVISIBLE, g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_INVISIBLE, 0))); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_INDENT, g_strdup_printf ("%i", 0)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_RIGHT_MARGIN, g_strdup_printf ("%i", 0)); attrib_set = gail_misc_add_attribute (attrib_set, ATK_TEXT_ATTR_LEFT_MARGIN, g_strdup_printf ("%i", 0)); return attrib_set; }
static void cdtext(cdCtxCanvas *ctxcanvas, int x, int y, const char *s, int len) { PangoFontMetrics* metrics; int w, h, desc, dir = -1; int ox = x, oy = y; pango_layout_set_text(ctxcanvas->fontlayout, sStrConvertToUTF8(ctxcanvas, s, len), -1); pango_layout_get_pixel_size(ctxcanvas->fontlayout, &w, &h); metrics = pango_context_get_metrics(ctxcanvas->fontcontext, ctxcanvas->fontdesc, pango_context_get_language(ctxcanvas->fontcontext)); desc = (((pango_font_metrics_get_descent(metrics)) + PANGO_SCALE/2) / PANGO_SCALE); switch (ctxcanvas->canvas->text_alignment) { case CD_BASE_RIGHT: case CD_NORTH_EAST: case CD_EAST: case CD_SOUTH_EAST: x = x - w; break; case CD_BASE_CENTER: case CD_CENTER: case CD_NORTH: case CD_SOUTH: x = x - w/2; break; case CD_BASE_LEFT: case CD_NORTH_WEST: case CD_WEST: case CD_SOUTH_WEST: x = x; break; } if (ctxcanvas->canvas->invert_yaxis) dir = 1; switch (ctxcanvas->canvas->text_alignment) { case CD_BASE_LEFT: case CD_BASE_CENTER: case CD_BASE_RIGHT: y = y - (dir*h - desc); break; case CD_SOUTH_EAST: case CD_SOUTH_WEST: case CD_SOUTH: y = y - (dir*h); break; case CD_NORTH_EAST: case CD_NORTH: case CD_NORTH_WEST: y = y; break; case CD_CENTER: case CD_EAST: case CD_WEST: y = y - (dir*(h/2)); break; } if(!ctxcanvas->canvas->use_matrix) { ctxcanvas->fontmatrix.xx = 1; ctxcanvas->fontmatrix.xy = 0; ctxcanvas->fontmatrix.yx = 0; ctxcanvas->fontmatrix.yy = 1; ctxcanvas->fontmatrix.x0 = 0; ctxcanvas->fontmatrix.y0 = 0; } if (ctxcanvas->canvas->use_matrix || ctxcanvas->canvas->text_orientation) { PangoRectangle rect; double angle = ctxcanvas->canvas->text_orientation; if (ctxcanvas->canvas->text_orientation) pango_matrix_rotate(&ctxcanvas->fontmatrix, angle); pango_context_set_matrix (ctxcanvas->fontcontext, &ctxcanvas->fontmatrix); pango_layout_context_changed (ctxcanvas->fontlayout); pango_layout_get_pixel_extents(ctxcanvas->fontlayout, NULL, &rect); #if PANGO_VERSION_CHECK(1,16,0) pango_matrix_transform_pixel_rectangle(&ctxcanvas->fontmatrix, &rect); #endif if (ctxcanvas->canvas->text_orientation) { double cos_angle = cos(angle*CD_DEG2RAD); double sin_angle = sin(angle*CD_DEG2RAD); cdRotatePoint(ctxcanvas->canvas, x, y, ox, oy, &x, &y, sin_angle, cos_angle); } if (ctxcanvas->canvas->use_matrix) cdMatrixTransformPoint(ctxcanvas->xmatrix, x, y, &x, &y); /* Defines the new position (X,Y), considering the Pango rectangle transformed */ x += (int)rect.x; y += (int)rect.y; } cdgdkCheckSolidStyle(ctxcanvas, 1); if (ctxcanvas->canvas->new_region) { GdkRegion *rgn; gint *idx; gint range; pango_layout_line_get_x_ranges(pango_layout_get_line(ctxcanvas->fontlayout, 0), 0, len, &idx, &range); /* TODO: this is only the bounding box of the layout, not the text itself, must transform the text into a polygon. */ rgn = gdk_pango_layout_get_clip_region(ctxcanvas->fontlayout, x, y, idx, range); sCombineRegion(ctxcanvas, rgn); } else gdk_draw_layout(ctxcanvas->wnd, ctxcanvas->gc, x, y, ctxcanvas->fontlayout); pango_context_set_matrix(ctxcanvas->fontcontext, NULL); cdgdkCheckSolidStyle(ctxcanvas, 0); pango_font_metrics_unref(metrics); }
static IgtkFont* gtkFindFont(const char *standardfont) { PangoFontMetrics* metrics; PangoFontDescription* fontdesc; int i, is_underline = 0, is_strikeout = 0, count = iupArrayCount(gtk_fonts); IgtkFont* fonts = (IgtkFont*)iupArrayGetData(gtk_fonts); /* Check if the standardfont already exists in cache */ for (i = 0; i < count; i++) { if (iupStrEqualNoCase(standardfont, fonts[i].standardfont)) return &fonts[i]; } /* not found, create a new one */ { int size = 0, is_pango = 0; int is_bold = 0, is_italic = 0; char typeface[1024]; const char* mapped_name; /* parse the old Windows format first */ if (!iupFontParseWin(standardfont, typeface, &size, &is_bold, &is_italic, &is_underline, &is_strikeout)) { if (!iupFontParseX(standardfont, typeface, &size, &is_bold, &is_italic, &is_underline, &is_strikeout)) { if (!iupFontParsePango(standardfont, typeface, &size, &is_bold, &is_italic, &is_underline, &is_strikeout)) return NULL; else is_pango = 1; } } mapped_name = iupFontGetPangoName(typeface); if (mapped_name) strcpy(typeface, mapped_name); if (is_pango && !is_underline && !is_strikeout && size>0) fontdesc = pango_font_description_from_string(standardfont); else { char new_standardfont[200]; if (size<0) { double res = ((double)gdk_screen_get_width(gdk_screen_get_default()) / (double)gdk_screen_get_width_mm(gdk_screen_get_default())); /* pixels/mm */ /* 1 point = 1/72 inch 1 inch = 25.4 mm */ /* pixel = ((point/72)*25.4)*pixel/mm */ size = (int)((-size/res)*2.83464567 + 0.5); /* from pixels to points */ } sprintf(new_standardfont, "%s, %s%s%d", typeface, is_bold?"Bold ":"", is_italic?"Italic ":"", size); fontdesc = pango_font_description_from_string(new_standardfont); } } if (!fontdesc) return NULL; /* create room in the array */ fonts = (IgtkFont*)iupArrayInc(gtk_fonts); strcpy(fonts[i].standardfont, standardfont); fonts[i].fontdesc = fontdesc; fonts[i].strikethrough = pango_attr_strikethrough_new(is_strikeout? TRUE: FALSE); fonts[i].underline = pango_attr_underline_new(is_underline? PANGO_UNDERLINE_SINGLE: PANGO_UNDERLINE_NONE); fonts[i].layout = pango_layout_new(gtk_fonts_context); metrics = pango_context_get_metrics(gtk_fonts_context, fontdesc, pango_context_get_language(gtk_fonts_context)); fonts[i].charheight = pango_font_metrics_get_ascent(metrics) + pango_font_metrics_get_descent(metrics); fonts[i].charheight = IUPGTK_PANGOUNITS2PIXELS(fonts[i].charheight); fonts[i].charwidth = pango_font_metrics_get_approximate_char_width(metrics); fonts[i].charwidth = IUPGTK_PANGOUNITS2PIXELS(fonts[i].charwidth); pango_font_metrics_unref(metrics); gtkFontUpdate(&(fonts[i])); return &fonts[i]; }
static void gd_two_lines_renderer_get_preferred_width (GtkCellRenderer *cell, GtkWidget *widget, gint *minimum_size, gint *natural_size) { PangoContext *context; PangoFontMetrics *metrics; PangoFontDescription *font_desc; GtkStyleContext *style_context; gint nat_width, min_width; gint xpad, char_width, wrap_width, text_width; gint width_chars, ellipsize_chars; g_object_get (cell, "xpad", &xpad, "width-chars", &width_chars, "wrap-width", &wrap_width, NULL); style_context = gtk_widget_get_style_context (widget); gtk_cell_renderer_get_padding (cell, &xpad, NULL); gd_two_lines_renderer_get_size (cell, widget, NULL, NULL, &text_width, NULL, NULL, NULL, NULL, NULL); /* Fetch the average size of a character */ context = gtk_widget_get_pango_context (widget); gtk_style_context_save (style_context); gtk_style_context_set_state (style_context, 0); gtk_style_context_get (style_context, gtk_style_context_get_state (style_context), "font", &font_desc, NULL); gtk_style_context_restore (style_context); metrics = pango_context_get_metrics (context, font_desc, pango_context_get_language (context)); char_width = pango_font_metrics_get_approximate_char_width (metrics); pango_font_metrics_unref (metrics); pango_font_description_free (font_desc); /* enforce minimum width for ellipsized labels at ~3 chars */ ellipsize_chars = 3; /* If no width-chars set, minimum for wrapping text will be the wrap-width */ if (wrap_width > -1) min_width = xpad * 2 + MIN (text_width, wrap_width); else min_width = xpad * 2 + MIN (text_width, (PANGO_PIXELS (char_width) * MAX (width_chars, ellipsize_chars))); if (width_chars > 0) nat_width = xpad * 2 + MAX ((PANGO_PIXELS (char_width) * width_chars), text_width); else nat_width = xpad * 2 + text_width; nat_width = MAX (nat_width, min_width); if (minimum_size) *minimum_size = min_width; if (natural_size) *natural_size = nat_width; }