int FontHeightWithContextReal(cairo_t* c, PangoFontDescription* fontDesc, int dpi) { int height; PangoContext* pc = pango_cairo_create_context(c); pango_cairo_context_set_resolution(pc, dpi); PangoLayout *layout = pango_layout_new(pc); pango_layout_set_text(layout, "Aygä¸", -1); pango_layout_set_font_description(layout, fontDesc); pango_layout_get_pixel_size(layout, NULL, &height); g_object_unref(layout); g_object_unref(pc); return height; }
static PyObject * pango_CreateContext(PyObject *self, PyObject *args) { PycairoContext *context; cairo_t *ctx; PangoContext *pcctx; if (!PyArg_ParseTuple(args, "O", &context)) { return NULL; } ctx = context->ctx; pcctx = pango_cairo_create_context(ctx); return Py_BuildValue("O", PyCObject_FromVoidPtr((void *) pcctx, (void *) g_object_unref)); }
void buffer_to_pdf (Ebook * ebook) { GtkTextBuffer * buffer; GtkTextIter start, end; Pqueue queue; gchar * size, *editor_font; /* A4 initial */ queue.ebook = ebook; queue.pos = 0; queue.page_count = 1; queue.width = 8.3 * POINTS; queue.height = 11.7 * POINTS; queue.page_height = 40.0; g_return_if_fail (ebook); g_return_if_fail (ebook->filename); size = gconf_client_get_string (ebook->client, ebook->paper_size.key, NULL); buffer = GTK_TEXT_BUFFER(gtk_builder_get_object (ebook->builder, "textbuffer1")); queue.progressbar = GTK_PROGRESS_BAR(gtk_builder_get_object (ebook->builder, "progressbar")); queue.statusbar = GTK_STATUSBAR(gtk_builder_get_object (ebook->builder, "statusbar")); gtk_text_buffer_get_bounds (buffer, &start, &end); queue.text = g_strdup(gtk_text_buffer_get_text (buffer, &start, &end, TRUE)); editor_font = gconf_client_get_string(ebook->client, ebook->editor_font.key, NULL); if (0 == g_strcmp0 (size, "A5")) { queue.width = a5_width * POINTS; queue.height = a5_height * POINTS; } if (0 == g_strcmp0 (size, "B5")) { queue.width = b5_width * POINTS; queue.height = b5_height * POINTS; } queue.surface = cairo_pdf_surface_create (ebook->filename, queue.width, queue.height); queue.cr = cairo_create (queue.surface); queue.context = pango_cairo_create_context (queue.cr); /* pango_cairo_create_layout is wasteful with a lot of text. */ queue.desc = pango_font_description_from_string (editor_font); queue.layout = make_new_page (queue.context, queue.desc, queue.height, queue.width); pango_layout_set_text (queue.layout, queue.text, -1); cairo_move_to (queue.cr, SIDE_MARGIN / 2, EDGE_MARGIN / 2); gtk_progress_bar_set_fraction (queue.progressbar, 0.0); queue.lines_per_page = pango_layout_get_line_count (queue.layout); queue.iter = pango_layout_get_iter (queue.layout); create_pages (&queue); }
void StringSizeStrict(const char* str, const char* font, int fontSize, int dpi, int* w, int* h) { if (!str || str[0] == 0) { if (w) *w = 0; if (h) *h = 0; return; } if (!fcitx_utf8_check_string(str)) { if (w) *w = 0; if (h) *h = 0; return; } cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 10, 10); cairo_t *c = cairo_create(surface); SetFontContext(c, font, fontSize, dpi); #ifdef _ENABLE_PANGO PangoRectangle rect; PangoContext* pc = pango_cairo_create_context(c); pango_cairo_context_set_resolution(pc, dpi); PangoLayout *layout = pango_layout_new(pc); pango_layout_set_text(layout, str, -1); pango_layout_set_font_description(layout, fontDesc); pango_layout_get_pixel_extents(layout, &rect, NULL); if (w) *w = rect.width; if (h) *h = rect.height; g_object_unref(layout); #else cairo_text_extents_t extents; cairo_text_extents(c, str, &extents); if (w) *w = extents.width; if (h) *h = extents.height; #endif ResetFontContext(); cairo_destroy(c); cairo_surface_destroy(surface); }
// Create and return a new lithograph, returns NULL on failure. simplet_lithograph_t * simplet_lithograph_new(cairo_t *ctx){ simplet_lithograph_t *litho; if(!(litho = malloc(sizeof(*litho)))) return NULL; memset(litho, 0, sizeof(*litho)); if(!(litho->placements = simplet_list_new(litho))){ free(litho); return NULL; } litho->ctx = ctx; litho->pango_ctx = pango_cairo_create_context(ctx); cairo_reference(ctx); simplet_retain((simplet_retainable_t *)litho); return litho; }
void OutputStringWithContextReal(cairo_t * c, PangoFontDescription* desc, int dpi, const char *str, int x, int y) { if (!str || str[0] == 0) return; if (!fcitx_utf8_check_string(str)) return; cairo_save(c); PangoContext *pc = pango_cairo_create_context(c); pango_cairo_context_set_resolution(pc, dpi); PangoLayout *layout = pango_layout_new(pc); pango_layout_set_text(layout, str, -1); pango_layout_set_font_description(layout, desc); cairo_move_to(c, x, y); pango_cairo_show_layout(c, layout); cairo_restore(c); g_object_unref(layout); g_object_unref(pc); }
void update_debug_tree(void) { if (!debug.render_tree) { return; } int width = 640, height = 480; for (int i = 0; i < root->outputs->length; ++i) { struct sway_output *output = root->outputs->items[i]; if (output->width > width) { width = output->width; } if (output->height > height) { height = output->height; } } cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); cairo_t *cairo = cairo_create(surface); PangoContext *pango = pango_cairo_create_context(cairo); struct sway_seat *seat = input_manager_current_seat(); struct sway_node *focus = seat_get_focus(seat); cairo_set_source_u32(cairo, 0x000000FF); draw_node(cairo, &root->node, focus, 0, 0); cairo_surface_flush(surface); struct wlr_renderer *renderer = wlr_backend_get_renderer(server.backend); if (root->debug_tree) { wlr_texture_destroy(root->debug_tree); } unsigned char *data = cairo_image_surface_get_data(surface); int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width); struct wlr_texture *texture = wlr_texture_from_pixels(renderer, WL_SHM_FORMAT_ARGB8888, stride, width, height, data); root->debug_tree = texture; cairo_surface_destroy(surface); g_object_unref(pango); cairo_destroy(cairo); }
void StringSizeWithContextReal(cairo_t * c, PangoFontDescription* fontDesc, int dpi, const char *str, int* w, int* h) { if (!str || str[0] == 0) { if (w) *w = 0; if (h) *h = 0; return; } if (!fcitx_utf8_check_string(str)) { if (w) *w = 0; if (h) *h = 0; return; } PangoContext *pc = pango_cairo_create_context(c); pango_cairo_context_set_resolution(pc, dpi); PangoLayout *layout = pango_layout_new(pc); pango_layout_set_text(layout, str, -1); pango_layout_set_font_description(layout, fontDesc); pango_layout_get_pixel_size(layout, w, h); g_object_unref(layout); g_object_unref(pc); }
static void text_to_glyphs (cairo_t *cr, const gchar *text, cairo_glyph_t **glyphs, int *num_glyphs) { PangoAttribute *fallback_attr; PangoAttrList *attr_list; PangoContext *context; PangoDirection base_dir; GList *items; GList *visual_items; FT_Face ft_face; hb_font_t *hb_font; gdouble x = 0, y = 0; gint i; gdouble x_scale, y_scale; *num_glyphs = 0; *glyphs = NULL; base_dir = pango_find_base_dir (text, -1); cairo_scaled_font_t *cr_font = cairo_get_scaled_font (cr); ft_face = cairo_ft_scaled_font_lock_face (cr_font); hb_font = hb_ft_font_create (ft_face, NULL); cairo_surface_t *target = cairo_get_target (cr); cairo_surface_get_device_scale (target, &x_scale, &y_scale); /* We abuse pango itemazation to split text into script and direction * runs, since we use our fonts directly no through pango, we don't * bother changing the default font, but we disable font fallback as * pango will split runs at font change */ context = pango_cairo_create_context (cr); attr_list = pango_attr_list_new (); fallback_attr = pango_attr_fallback_new (FALSE); pango_attr_list_insert (attr_list, fallback_attr); items = pango_itemize_with_base_dir (context, base_dir, text, 0, strlen (text), attr_list, NULL); g_object_unref (context); pango_attr_list_unref (attr_list); /* reorder the items in the visual order */ visual_items = pango_reorder_items (items); while (visual_items) { PangoItem *item; PangoAnalysis analysis; hb_buffer_t *hb_buffer; hb_glyph_info_t *hb_glyphs; hb_glyph_position_t *hb_positions; gint n; item = visual_items->data; analysis = item->analysis; hb_buffer = hb_buffer_create (); hb_buffer_add_utf8 (hb_buffer, text, -1, item->offset, item->length); hb_buffer_set_script (hb_buffer, hb_glib_script_to_script (analysis.script)); hb_buffer_set_language (hb_buffer, hb_language_from_string (pango_language_to_string (analysis.language), -1)); hb_buffer_set_direction (hb_buffer, analysis.level % 2 ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); hb_shape (hb_font, hb_buffer, NULL, 0); n = hb_buffer_get_length (hb_buffer); hb_glyphs = hb_buffer_get_glyph_infos (hb_buffer, NULL); hb_positions = hb_buffer_get_glyph_positions (hb_buffer, NULL); *glyphs = g_renew (cairo_glyph_t, *glyphs, *num_glyphs + n); for (i = 0; i < n; i++) { (*glyphs)[*num_glyphs + i].index = hb_glyphs[i].codepoint; (*glyphs)[*num_glyphs + i].x = x + (hb_positions[i].x_offset / (64. * x_scale)); (*glyphs)[*num_glyphs + i].y = y - (hb_positions[i].y_offset / (64. * y_scale)); x += (hb_positions[i].x_advance / (64. * x_scale)); y -= (hb_positions[i].y_advance / (64. * y_scale)); } *num_glyphs += n; hb_buffer_destroy (hb_buffer); visual_items = visual_items->next; } g_list_free_full (visual_items, (GDestroyNotify) pango_item_free); g_list_free_full (items, (GDestroyNotify) pango_item_free); hb_font_destroy (hb_font); cairo_ft_scaled_font_unlock_face (cr_font); }
context_t::context_t(cairo_t* cr) : pango_context( pango_cairo_create_context(cr) ) { if(!pango_context) throw error("Unable to create pango context"); }