static void text_entry_get_cursor_rectangle(struct text_entry *entry, struct rectangle *rectangle) { struct rectangle allocation; PangoRectangle extents; PangoRectangle cursor_pos; widget_get_allocation(entry->widget, &allocation); if (entry->preedit.text && entry->preedit.cursor < 0) { rectangle->x = 0; rectangle->y = 0; rectangle->width = 0; rectangle->height = 0; return; } pango_layout_get_extents(entry->layout, &extents, NULL); pango_layout_get_cursor_pos(entry->layout, entry->cursor + entry->preedit.cursor, &cursor_pos, NULL); rectangle->x = allocation.x + (allocation.height / 2) + PANGO_PIXELS(cursor_pos.x); rectangle->y = allocation.y + 10 + PANGO_PIXELS(cursor_pos.y); rectangle->width = PANGO_PIXELS(cursor_pos.width); rectangle->height = PANGO_PIXELS(cursor_pos.height); }
/* Given a paragraph and offset in that paragraph, find the * bounding rectangle for the character at the offset. */ void char_bounds (Paragraph *para, int index, int width, PangoRectangle *rect) { GList *para_list; int height = 0; para_list = paragraphs; while (para_list) { Paragraph *cur_para = para_list->data; if (cur_para == para) { PangoRectangle pos; pango_layout_index_to_pos (cur_para->layout, index, &pos); rect->x = PANGO_PIXELS (MIN (pos.x, pos.x + pos.width)); rect->width = PANGO_PIXELS (ABS (pos.width)); rect->y = height + PANGO_PIXELS (pos.y); rect->height = PANGO_PIXELS (pos.height); } height += cur_para->height; para_list = para_list->next; } }
int unicodeGetXRanges(char *utf8, int utf8Length, int *resultPtr, int resultLength) { int w, h, offsetX, offsetY; int count, ch, i, j; PangoRectangle rect; count = unicodeLength(utf8, utf8Length); if (resultLength < (2 * count)) return -1; if (cachedLayout == NULL) { cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_A8, 1, 1); cairo_t *cr = cairo_create(surface); cachedLayout = pango_cairo_create_layout(cr); } computeLayout(cachedLayout, utf8, utf8Length, &w, &h, &offsetX, &offsetY, NULL); i = j = 0; while ((i < utf8Length) && (j < (resultLength - 1))) { pango_layout_index_to_pos(cachedLayout, i, &rect); ch = utf8[i]; if ((ch & 0xE0) == 0xC0) i += 2; else if ((ch & 0xF0) == 0xE0) i += 3; else if ((ch & 0xF8) == 0xF0) i += 4; else i += 1; resultPtr[j] = PANGO_PIXELS(rect.x); resultPtr[j + 1] = PANGO_PIXELS(rect.x + rect.width); j += 2; } return count; }
wxFontProperties::wxFontProperties(wxFont* font): m_ascent(0), m_descent(0), m_lineGap(0), m_lineSpacing(0), m_xHeight(0) { PangoContext* context = gdk_pango_context_get_for_screen( gdk_screen_get_default() ); PangoLayout* layout = pango_layout_new(context); // and use it if it's valid if ( font && font->Ok() ) { pango_layout_set_font_description ( layout, font->GetNativeFontInfo()->description ); } PangoFontMetrics* metrics = pango_context_get_metrics (context, font->GetNativeFontInfo()->description, NULL); int height = font->GetPixelSize().GetHeight(); m_ascent = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics)); m_descent = PANGO_PIXELS(pango_font_metrics_get_descent(metrics)); int h; const char* x = "x"; pango_layout_set_text( layout, x, strlen(x) ); pango_layout_get_pixel_size( layout, NULL, &h ); m_xHeight = h; m_lineGap = (m_ascent + m_descent) / 4; // FIXME: How can we calculate this via Pango? m_lineSpacing = m_ascent + m_descent; pango_font_metrics_unref(metrics); }
/* Get a clip region to draw only part of a layout. index_ranges * contains alternating range starts/stops. The region is the * region which contains the given ranges, i.e. if you draw with the * region as clip, only the given ranges are drawn. */ static cairo_region_t* layout_iter_get_line_clip_region (PangoLayoutIter *iter, gint x_origin, gint y_origin, const gint *index_ranges, gint n_ranges) { PangoLayoutLine *line; cairo_region_t *clip_region; PangoRectangle logical_rect; gint baseline; gint i; line = pango_layout_iter_get_line_readonly (iter); clip_region = cairo_region_create (); pango_layout_iter_get_line_extents (iter, NULL, &logical_rect); baseline = pango_layout_iter_get_baseline (iter); i = 0; while (i < n_ranges) { gint *pixel_ranges = NULL; gint n_pixel_ranges = 0; gint j; /* Note that get_x_ranges returns layout coordinates */ if (index_ranges[i*2+1] >= line->start_index && index_ranges[i*2] < line->start_index + line->length) pango_layout_line_get_x_ranges (line, index_ranges[i*2], index_ranges[i*2+1], &pixel_ranges, &n_pixel_ranges); for (j = 0; j < n_pixel_ranges; j++) { GdkRectangle rect; int x_off, y_off; x_off = PANGO_PIXELS (pixel_ranges[2*j] - logical_rect.x); y_off = PANGO_PIXELS (baseline - logical_rect.y); rect.x = x_origin + x_off; rect.y = y_origin - y_off; rect.width = PANGO_PIXELS (pixel_ranges[2*j + 1] - logical_rect.x) - x_off; rect.height = PANGO_PIXELS (baseline - logical_rect.y + logical_rect.height) - y_off; cairo_region_union_rectangle (clip_region, &rect); } g_free (pixel_ranges); ++i; } return clip_region; }
HRESULT ScriptXtoCPRightToLeft( /*__in*/ int iX, /*__in*/ int cChars, /*__in*/ int cGlyphs, /*__in*/ const WORD *pwLogClust, /*__in*/ const SCRIPT_VISATTR *psva, /*__in*/ const int *piAdvance, /*__in*/ const SCRIPT_ANALYSIS *psa, /*__out*/ int *piCP, /*__out*/ int *piTrailing ) { // TODO: this implementation doesn't use pwLogClust // which means it doesn't handle scripts whose clusters are not single Glyphs int totalRunWidth = 0; int pos = 0; int index = 0; // is iX isn't before run if (iX < 0) { *piCP = cChars; *piTrailing = 0; return S_OK; } for (index = 0; index < cGlyphs; ++index) totalRunWidth += piAdvance[index]; totalRunWidth = PANGO_PIXELS(totalRunWidth); // is iX after run if (iX >= totalRunWidth) { *piCP = -1; *piTrailing = 1; return S_OK; } // loop until pos in run is greater than or equal to iX for (index = cGlyphs - 1; index >= 0 && pos < iX; --index) pos += PANGO_PIXELS(piAdvance[index]); // trailing or leading edge? if (pos - iX > PANGO_PIXELS(piAdvance[index]/2)) *piTrailing = 0; else *piTrailing = 1; *piCP = index + 1; Assert(*piCP >= 0); return S_OK; }
void show_text(GtkWidget *window, const char *text) { GtkWidget *dialog, *content_area, *view, *sw; GtkDialogFlags flags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT; dialog = gtk_dialog_new_with_buttons(PACKAGE_NAME " Help", GTK_WINDOW(window), flags, "_Close", GTK_RESPONSE_CANCEL, NULL); content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL); sw = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_IN); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_container_add_with_properties(GTK_CONTAINER(content_area), sw, "expand", TRUE, "fill", TRUE, NULL); view = gtk_text_view_new(); gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD); gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(view)), text, -1); gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE); gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE); /* Use font metrics to set the size of the text view. */ PangoContext *context = gtk_widget_get_pango_context(view); PangoFontMetrics *metrics = pango_context_get_metrics(context, NULL, NULL); gint char_width = pango_font_metrics_get_approximate_char_width(metrics); gint ascent = pango_font_metrics_get_ascent(metrics); gint descent = pango_font_metrics_get_descent(metrics); gint height = PANGO_PIXELS(ascent + descent); gint width = PANGO_PIXELS(char_width); gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view), 2 * width); gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view), 2 * width); gtk_window_set_default_size(GTK_WINDOW(dialog), 70 * width, 20 * height); gtk_container_add(GTK_CONTAINER(sw), view); gtk_widget_show_all(dialog); gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); }
gboolean mw_tooltip_timeout(GtkWidget *tv) { GtkAllocation allocation; int scr_w,scr_h, w, h, x, y; char *tooltiptext = NULL; tooltiptext = get_tooltip_text(); tipwindow = gtk_window_new(GTK_WINDOW_POPUP); gtk_widget_set_parent(tipwindow, tv); gtk_widget_set_app_paintable(tipwindow, TRUE); gtk_window_set_resizable(GTK_WINDOW(tipwindow), FALSE); gtk_widget_set_name(tipwindow, "gtk-tooltips"); g_signal_connect(G_OBJECT(tipwindow), "expose_event", G_CALLBACK(mw_paint_tip), NULL); gtk_widget_ensure_style (tipwindow); layout = gtk_widget_create_pango_layout (tipwindow, NULL); pango_layout_set_wrap(layout, PANGO_WRAP_WORD); pango_layout_set_width(layout, 300000); pango_layout_set_markup(layout, tooltiptext, strlen(tooltiptext)); scr_w = gdk_screen_width(); scr_h = gdk_screen_height(); pango_layout_get_size (layout, &w, &h); w = PANGO_PIXELS(w) + 8; h = PANGO_PIXELS(h) + 8; gdk_window_get_pointer(NULL, &x, &y, NULL); if (!gtk_widget_get_has_window (mw.vbox)) { gtk_widget_get_allocation (mw.vbox, &allocation); y += allocation.y; } x -= ((w >> 1) + 4); if ((x + w) > scr_w) x -= (x + w) - scr_w; else if (x < 0) x = 0; if ((y + h + 4) > scr_h) y = y - h; else y = y + 6; /* g_object_unref(layout); */ g_free(tooltiptext); gtk_widget_set_size_request(tipwindow, w, h); gtk_window_move(GTK_WINDOW(tipwindow), x, y); gtk_widget_show(tipwindow); return FALSE; }
/* subfunction of gtk_plot_cairo_draw_string(). */ static gint drawstring(GtkPlotPC *pc, gint angle, gint dx, gint dy, GtkPSFont *psfont, gint height, const gchar *text) { cairo_t *cairo = GTK_PLOT_CAIRO(pc)->cairo; PangoLayout *layout = GTK_PLOT_CAIRO(pc)->layout; PangoFontDescription *font; PangoRectangle rect; PangoFontMap *map; gint ret_value; gint dpi_cairo, dpi_screen; GdkScreen *screen = gdk_screen_get_default(); if(!text || strlen(text) == 0) return 0; cairo_save(cairo); map = pango_cairo_font_map_get_default(); dpi_cairo = pango_cairo_font_map_get_resolution(PANGO_CAIRO_FONT_MAP(map)); dpi_screen = gdk_screen_get_resolution(screen); height *= (double)dpi_screen/(double)dpi_cairo; font = gtk_psfont_get_font_description(psfont, height); pango_layout_set_font_description(GTK_PLOT_CAIRO(pc)->layout, font); pango_layout_set_text(GTK_PLOT_CAIRO(pc)->layout, text, strlen(text)); pango_layout_get_extents(GTK_PLOT_CAIRO(pc)->layout, NULL, &rect); if (psfont->i18n_latinfamily && psfont->vertical) { /* vertical-writing CJK postscript fonts. */ return rect.height; } else { /* horizontal writing */ if(angle == 90) // cairo_translate(cairo, dx, dy-PANGO_PIXELS(rect.width)); cairo_translate(cairo, dx, dy); else if(angle == 270) cairo_translate(cairo, dx+PANGO_PIXELS(rect.height), dy); else if(angle == 180) cairo_translate(cairo, dx-PANGO_PIXELS(rect.width), dy); else cairo_translate(cairo, dx, dy); } cairo_rotate(cairo, -angle * G_PI / 180); pango_cairo_update_layout(cairo, layout); pango_cairo_show_layout(cairo, layout); cairo_restore(cairo); pango_font_description_free(font); ret_value = (angle == 0 || angle == 180) ? rect.width : rect.height; return PANGO_PIXELS(rect.width); }
// Notice we don't check here the font. It is supposed to be OK before the call. void wxTextMeasure::DoGetTextExtent(const wxString& string, wxCoord *width, wxCoord *height, wxCoord *descent, wxCoord *externalLeading) { if ( !m_context ) { if ( width ) *width = 0; if ( height ) *height = 0; return; } // Set layout's text const wxCharBuffer dataUTF8 = wxGTK_CONV_FONT(string, GetFont()); if ( !dataUTF8 ) { // hardly ideal, but what else can we do if conversion failed? wxLogLastError(wxT("GetTextExtent")); return; } pango_layout_set_text(m_layout, dataUTF8, -1); if ( m_dc ) { // in device units pango_layout_get_pixel_size(m_layout, width, height); } else // win { // the logical rect bounds the ink rect PangoRectangle rect; pango_layout_get_extents(m_layout, NULL, &rect); *width = PANGO_PIXELS(rect.width); *height = PANGO_PIXELS(rect.height); } if (descent) { PangoLayoutIter *iter = pango_layout_get_iter(m_layout); int baseline = pango_layout_iter_get_baseline(iter); pango_layout_iter_free(iter); *descent = *height - PANGO_PIXELS(baseline); } if (externalLeading) { // No support for MSW-like "external leading" in Pango. *externalLeading = 0; } }
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkFontPeer_getFontMetrics (JNIEnv *env, jobject java_font, jdoubleArray java_metrics) { struct peerfont *pfont = NULL; jdouble *native_metrics = NULL; PangoFontMetrics *pango_metrics; gdk_threads_enter(); pfont = (struct peerfont *) NSA_GET_FONT_PTR (env, java_font); g_assert (pfont != NULL); pango_metrics = pango_context_get_metrics (pfont->ctx, pfont->desc, gtk_get_default_language ()); native_metrics = (*env)->GetDoubleArrayElements (env, java_metrics, NULL); g_assert (native_metrics != NULL); native_metrics[FONT_METRICS_ASCENT] = PANGO_PIXELS (pango_font_metrics_get_ascent (pango_metrics)); native_metrics[FONT_METRICS_MAX_ASCENT] = native_metrics[FONT_METRICS_ASCENT]; native_metrics[FONT_METRICS_DESCENT] = PANGO_PIXELS (pango_font_metrics_get_descent (pango_metrics)); if (native_metrics[FONT_METRICS_DESCENT] < 0) native_metrics[FONT_METRICS_DESCENT] = - native_metrics[FONT_METRICS_DESCENT]; native_metrics[FONT_METRICS_MAX_DESCENT] = native_metrics[FONT_METRICS_DESCENT]; native_metrics[FONT_METRICS_MAX_ADVANCE] = PANGO_PIXELS (pango_font_metrics_get_approximate_char_width (pango_metrics)); (*env)->ReleaseDoubleArrayElements (env, java_metrics, native_metrics, 0); pango_font_metrics_unref (pango_metrics); gdk_threads_leave(); }
static void gnm_notebook_button_size_allocate (GtkWidget *widget, GtkAllocation *allocation) { GnmNotebookButton *nbb = GNM_NOTEBOOK_BUTTON (widget); gnm_notebook_button_ensure_layout (nbb); nbb->x_offset = (allocation->width - PANGO_PIXELS (nbb->logical.width)) / 2; nbb->x_offset_active = (allocation->width - PANGO_PIXELS (nbb->logical_active.width)) / 2; GTK_WIDGET_CLASS(gnm_notebook_button_parent_class) ->size_allocate (widget, allocation); }
static gboolean util_split_font_string(const gchar *font_name, gchar **name, gint *size) { PangoFontDescription *desc; PangoFontMask mask; gboolean retval = FALSE; if (font_name == NULL) { return FALSE; } mask = (PangoFontMask) (PANGO_FONT_MASK_FAMILY | PANGO_FONT_MASK_SIZE); desc = pango_font_description_from_string(font_name); if (!desc) { return FALSE; } if ((pango_font_description_get_set_fields(desc) & mask) == mask) { *size = PANGO_PIXELS(pango_font_description_get_size (desc)); *name = g_strdup(pango_font_description_get_family (desc)); retval = TRUE; } pango_font_description_free(desc); return retval; }
/** * Find the position in a string where an x coordinate falls. * * \param[in] fstyle style for this text * \param[in] string UTF-8 string to measure * \param[in] length length of string, in bytes * \param[in] x coordinate to search for * \param[out] char_offset updated to offset in string of actual_x, [0..length] * \param[out] actual_x updated to x coordinate of character closest to x * \return NSERROR_OK and char_offset and actual_x updated or appropriate * error code on faliure */ static nserror nsfont_position_in_string(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x) { int index; PangoFontDescription *desc; PangoRectangle pos; nsfont_pango_check(); desc = nsfont_style_to_description(fstyle); pango_layout_set_font_description(nsfont_pango_layout, desc); pango_font_description_free(desc); pango_layout_set_text(nsfont_pango_layout, string, length); if (pango_layout_xy_to_index(nsfont_pango_layout, x * PANGO_SCALE, 0, &index, 0) == FALSE) { index = length; } pango_layout_index_to_pos(nsfont_pango_layout, index, &pos); *char_offset = index; *actual_x = PANGO_PIXELS(pos.x); return NSERROR_OK; }
void gtk_glwidget_create_font (GtkWidget *widget) { PangoFontDescription *font_desc; PangoFont *font; PangoFontMetrics *font_metrics; font_list_base = qglGenLists (256); font_desc = pango_font_description_from_string (font_string); font = gdk_gl_font_use_pango_font (font_desc, 0, 256, font_list_base); if(font != NULL) { font_metrics = pango_font_get_metrics (font, NULL); font_height = pango_font_metrics_get_ascent (font_metrics) + pango_font_metrics_get_descent (font_metrics); font_height = PANGO_PIXELS (font_height); pango_font_metrics_unref (font_metrics); } pango_font_description_free (font_desc); }
gboolean myScreenUpdateFontHeight (ScreenInfo *screen_info) { PangoFontDescription *desc; PangoContext *context; PangoFontMetrics *metrics; GtkWidget *widget; g_return_val_if_fail (screen_info != NULL, FALSE); TRACE ("entering myScreenUpdateFontHeight"); widget = myScreenGetGtkWidget (screen_info); context = getUIPangoContext (widget); desc = getUIPangoFontDesc (widget); if (desc && context) { metrics = pango_context_get_metrics (context, desc, NULL); screen_info->font_height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)); pango_font_metrics_unref (metrics); return TRUE; } return FALSE; }
static void gtk_anim_label_size_allocate(GtkWidget * widget, GtkAllocation * allocation ) { PangoRectangle prect; g_return_if_fail(widget != NULL); g_return_if_fail(GTK_IS_ANIM_LABEL(widget)); g_return_if_fail(allocation != NULL); widget->allocation = *allocation; (*GTK_WIDGET_CLASS(parent_class)->size_allocate) (widget, allocation); if (GTK_WIDGET_REALIZED(widget)) { gdk_window_move_resize(widget->window, allocation->x, allocation->y, allocation->width, allocation->height); if (((GTK_ANIM_LABEL(widget)->animate) || (GTK_ANIM_LABEL(widget)->auto_animate)) && (GTK_ANIM_LABEL(widget)->layout)) { pango_layout_get_extents(GTK_ANIM_LABEL(widget)->layout, NULL, &prect); if (PANGO_PIXELS(prect.width) < widget->allocation.width) { GTK_ANIM_LABEL(widget)->pos_x = 0; gtk_anim_label_animate(GTK_ANIM_LABEL(widget), FALSE); } else if ((GTK_ANIM_LABEL(widget)->auto_animate) && (!GTK_ANIM_LABEL(widget)->animate)) { GTK_ANIM_LABEL(widget)->pos_x = 0; gtk_anim_label_animate(GTK_ANIM_LABEL(widget), TRUE); } } } }
/* 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 int ol_scroll_window_get_font_height (OlScrollWindow *scroll) { ol_assert_ret (OL_IS_SCROLL_WINDOW (scroll), 0); OlScrollWindowPrivate *priv = OL_SCROLL_WINDOW_GET_PRIVATE (scroll); PangoContext *pango_context = gdk_pango_context_get (); PangoLayout *pango_layout = pango_layout_new (pango_context); PangoFontDescription *font_desc = pango_font_description_from_string (priv->font_name); pango_layout_set_font_description (pango_layout, font_desc); PangoFontMetrics *metrics = pango_context_get_metrics (pango_context, pango_layout_get_font_description (pango_layout), /* font desc */ NULL); /* languague */ int height = 0; int ascent, descent; ascent = pango_font_metrics_get_ascent (metrics); descent = pango_font_metrics_get_descent (metrics); pango_font_metrics_unref (metrics); height += PANGO_PIXELS (ascent + descent); pango_font_description_free (font_desc); g_object_unref (pango_layout); g_object_unref (pango_context); return height; }
/* This function is based on reverse_engineer_stepper_box * (and gtk2 sources) except it is for getting spin button * size instead. It is not always right, and only returns * a (hopefully more accurate) arrow box, not the entire * button box, as the button box is passed correctly * to paint_box and so only paint_arrow needs this. */ void reverse_engineer_spin_button (GtkWidget *widget, GtkArrowType arrow_type, gint *x, gint *y, gint *width, gint *height) { #ifdef GTK2 gint size = pango_font_description_get_size (widget->style->font_desc); gint realheight, realwidth; realwidth = MIN(PANGO_PIXELS (size), 30); realwidth -= realwidth % 2; /* force even */ realwidth -= 2 * xthickness(widget->style); realheight = ((widget->requisition.height) - 2 * ythickness(widget->style)) / 2; realheight -= 1; realwidth += 1; *x += ((*width - realwidth) / 2); *y += ((*height - realheight) / 2) + (arrow_type==GTK_ARROW_DOWN?1:-1); *width = realwidth; *height = realheight; #endif }
/* * 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 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; }
/* Taken from the Gtk+ file gtkfilechooserdefault.c * Copyright (C) 2003, Red Hat, Inc. * * Changed by File-Roller authors * * Guesses a size based upon font sizes */ static int get_font_size (GtkWidget *widget) { GtkStyleContext *context; GtkStateFlags state; int font_size; GdkScreen *screen; double resolution; PangoFontDescription *font; context = gtk_widget_get_style_context (widget); state = gtk_widget_get_state_flags (widget); screen = gtk_widget_get_screen (widget); if (screen) { resolution = gdk_screen_get_resolution (screen); if (resolution < 0.0) /* will be -1 if the resolution is not defined in the GdkScreen */ resolution = 96.0; } else resolution = 96.0; /* wheeee */ gtk_style_context_get (context, state, "font", &font, NULL); font_size = pango_font_description_get_size (font); font_size = PANGO_PIXELS (font_size) * resolution / 72.0; return font_size; }
gui2::tpoint ttext::get_column_line(const gui2::tpoint& position) const { recalculate(); // Get the index of the character. int index, trailing; pango_layout_xy_to_index(layout_, position.x * PANGO_SCALE, position.y * PANGO_SCALE, &index, &trailing); // Extract the line and the offset in pixels in that line. int line, offset; pango_layout_index_to_line_x(layout_, index, trailing, &line, &offset); offset = PANGO_PIXELS(offset); // Now convert this offset to a column, this way is a bit hacky but haven't // found a better solution yet. /** * @todo There's still a bug left. When you select a text which is in the * ellipses on the right side the text gets reformatted with ellipses on * the left and the selected character is not the one under the cursor. * Other widget toolkits don't show ellipses and have no indication more * text is available. Haven't found what the best thing to do would be. * Until that time leave it as is. */ for(size_t i = 0; ; ++i) { const int pos = get_cursor_position(i, line).x; if(pos == offset) { return gui2::tpoint(i, line); } } }
static void _draw_page_number (GtkPrintContext *context, gint page_nr, GwPageInfo *page, GwPrintData *data) { //Declarations PangoLayout *layout; char *text; PangoFontDescription *desc; int width; int height; cairo_t *cr; gdouble drawable_width; //Initializations text = g_strdup_printf (gettext("Page %d/%d"), page_nr + 1, g_list_length (data->pages)); layout = gtk_print_context_create_pango_layout (context); desc = pango_font_description_from_string ("sans 8"); cr = gtk_print_context_get_cairo_context (context); drawable_width = gtk_print_context_get_width (context); //Draw if (text != NULL && layout != NULL && desc != NULL) { pango_font_description_set_weight (desc, PANGO_WEIGHT_BOLD); pango_layout_set_alignment (layout, PANGO_ALIGN_RIGHT); pango_layout_set_font_description (layout, desc); pango_layout_set_markup (layout, text, -1); pango_layout_get_size (layout, &width, &height); cairo_move_to (cr, (int) drawable_width - PANGO_PIXELS (width), 0); pango_cairo_show_layout (cr, layout); } //Cleanup if (text != NULL) g_free (text); if (layout != NULL) g_object_unref (layout); if (desc != NULL) pango_font_description_free (desc); }
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 gint anim_label_timeout_callback(gpointer user_data ) { GtkAnimLabel *anim_label = NULL; GtkWidget *widget = NULL; PangoRectangle prect; g_return_val_if_fail(user_data != NULL, FALSE); g_return_val_if_fail(GTK_IS_ANIM_LABEL(user_data), FALSE); anim_label = GTK_ANIM_LABEL(user_data); if (anim_label->animate == FALSE) return FALSE; if (anim_label->delay_sec > 0) { gint sec; gulong msec; sec = (gint) g_timer_elapsed(anim_label->timer, &msec); if (sec >= anim_label->delay_sec) g_timer_stop(anim_label->timer); else return TRUE; } widget = GTK_WIDGET(anim_label); pango_layout_get_extents(anim_label->layout, NULL, &prect); /* stop animate if whole label can be drawn in window */ if (PANGO_PIXELS(prect.width) < widget->allocation.width) return FALSE; anim_label->pos_x = anim_label->pos_x - 1; if ((anim_label->pos_x + (PANGO_PIXELS(prect.width))) < 0) anim_label->pos_x = widget->allocation.width - 1; gdk_draw_drawable(widget->window, widget->style->bg_gc[widget->state], anim_label->pixmap, 0, 0, anim_label->pos_x, 0, PANGO_PIXELS(prect.width) + 5, PANGO_PIXELS(prect.height)); return TRUE; }
void GetTextExtent( const wxFont& font, const wxString& str, wxCoord *width, wxCoord *height, wxCoord *descent, wxCoord *externalLeading ) { if ( width ) *width = 0; if ( height ) *height = 0; if ( descent ) *descent = 0; if ( externalLeading ) *externalLeading = 0; if (str.empty()) return; PangoContext* context = gdk_pango_context_get_for_screen( gdk_screen_get_default() ); PangoLayout* m_layout = pango_layout_new(context); // and use it if it's valid if ( font != wxNullFont ) { pango_layout_set_font_description ( m_layout, font.GetNativeFontInfo()->description ); } // Set layout's text const wxCharBuffer dataUTF8 = wxConvUTF8.cWX2MB(str); if ( !dataUTF8 ) { // hardly ideal, but what else can we do if conversion failed? return; } pango_layout_set_text( m_layout, dataUTF8, strlen(dataUTF8) ); if (descent) { int h; pango_layout_get_pixel_size( m_layout, width, &h ); PangoLayoutIter *iter = pango_layout_get_iter(m_layout); int baseline = pango_layout_iter_get_baseline(iter); pango_layout_iter_free(iter); *descent = h - PANGO_PIXELS(baseline); if (height) *height = (wxCoord) h; } else { pango_layout_get_pixel_size( m_layout, width, height ); } // Reset old font description //if (font != wxNullFont) // pango_layout_set_font_description( m_layout, m_fontdesc ); }
static void setup_font_sample(GtkWidget* darea, Antialiasing antialiasing, Hinting hinting) { const char *str = "<span font=\"18\" style=\"normal\">abcfgop AO </span>" "<span font=\"20\" style=\"italic\">abcfgop</span>"; PangoContext *context; PangoLayout *layout; PangoFontDescription *fd; PangoRectangle extents; cairo_surface_t *surface; cairo_t *cr; int width, height; context = gtk_widget_get_pango_context (darea); set_fontoptions (context, antialiasing, hinting); layout = pango_layout_new (context); fd = pango_font_description_from_string ("Serif"); pango_layout_set_font_description (layout, fd); pango_font_description_free (fd); pango_layout_set_markup (layout, str, -1); pango_layout_get_extents (layout, NULL, &extents); width = PANGO_PIXELS(extents.width) + 4; height = PANGO_PIXELS(extents.height) + 2; surface = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height); cr = cairo_create (surface); cairo_move_to (cr, 2, 1); pango_cairo_show_layout (cr, layout); g_object_unref (layout); cairo_destroy (cr); g_object_set_data_full(G_OBJECT(darea), "sample-surface", surface, (GDestroyNotify) cairo_surface_destroy); gtk_widget_set_size_request (GTK_WIDGET(darea), width + 2, height + 2); #if GTK_CHECK_VERSION (3, 0, 0) g_signal_connect(darea, "draw", G_CALLBACK(sample_draw), NULL); #else g_signal_connect(darea, "expose_event", G_CALLBACK(sample_expose), NULL); #endif }
/* force to wrap truncated label by setting explicit size request * see N#27000 and G#329646 */ static void force_to_wrap_truncated (HildonBanner *banner) { PangoLayout *layout; int lines; int width; int height = -1; PangoRectangle logical; GtkRequisition requisition; HildonBannerPrivate *priv = HILDON_BANNER_GET_PRIVATE (banner); g_return_if_fail (priv); width = priv->is_timed ? HILDON_BANNER_LABEL_MAX_TIMED : HILDON_BANNER_LABEL_MAX_PROGRESS; /* Force the label to compute its layout using the maximum * available width rather than its default one. */ gtk_widget_set_size_request (priv->label, width, height); gtk_widget_size_request (priv->label, &requisition); layout = gtk_label_get_layout (GTK_LABEL (priv->label)); pango_layout_get_extents (layout, NULL, &logical); /* Now get the actual width needed by the pango layout */ width = PANGO_PIXELS (logical.width); /* If the layout has now been wrapped and exceeds 3 lines, we truncate * the rest of the label according to spec. */ lines = pango_layout_get_line_count (layout); if (pango_layout_is_wrapped (layout) && lines > 3) { /* This calculation assumes that the same font is used * throughout the banner -- this is usually the case on maemo * * FIXME: Pango >= 1.20 has pango_layout_set_height(). */ height = (PANGO_PIXELS (logical.height) * 3) / lines + 1; } /* Set the final width/height */ gtk_widget_set_size_request (priv->label, width, height); }