Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
glpangofont.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include <glcomp/glpangofont.h>
12#include <stdlib.h>
13
14static PangoLayout *get_pango_layout(char *markup_text,
15 char *fontdescription, int fontsize,
16 double *width, double *height)
17{
18 PangoFontMap *fontmap;
19 PangoLayout *layout;
20 int pango_width, pango_height;
21 char *text;
22 PangoAttrList *attr_list;
23 fontmap = pango_cairo_font_map_get_default();
24
25 if (!pango_parse_markup
26 (markup_text, -1, '\0', &attr_list, &text, NULL, NULL))
27 return NULL;
28 PangoContext *const context = pango_font_map_create_context(fontmap);
29 layout = pango_layout_new(context);
30 g_object_unref(context);
31 pango_layout_set_text(layout, text, -1);
32 free(text);
33 PangoFontDescription *const desc =
34 pango_font_description_from_string(fontdescription);
35 pango_font_description_set_size(desc, (int)(fontsize * PANGO_SCALE));
36 pango_layout_set_font_description(layout, desc);
37 pango_layout_set_attributes(layout, attr_list);
38 pango_attr_list_unref(attr_list);
39 pango_font_description_free(desc);
40 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
41
42 pango_layout_get_size(layout, &pango_width, &pango_height);
43
44 *width = (double) pango_width / PANGO_SCALE;
45 *height = (double) pango_height / PANGO_SCALE;
46
47 return layout;
48}
49
50unsigned char *glCompCreatePangoTexture(char *fontdescription, int fontsize,
51 char *txt, cairo_surface_t **surface,
52 int *w, int *h) {
53 PangoLayout *layout;
54 double width, height;
55 *surface = NULL;
56
57 layout =
58 get_pango_layout(txt, fontdescription, fontsize, &width, &height);
59 if (layout == NULL) {
60 return NULL;
61 }
62 //create the right size canvas for character set
63 *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, (int)width,
64 (int) height);
65
66 cairo_t *cr = cairo_create(*surface);
67 //set pen color to white
68 cairo_set_source_rgba(cr, 1, 1, 1, 1);
69 //draw the text
70 pango_cairo_show_layout(cr, layout);
71
72
73
74 *w = (int) width;
75 *h = (int) height;
76 g_object_unref(layout);
77 cairo_destroy(cr);
78
79 return cairo_image_surface_get_data(*surface);
80}
unsigned char * glCompCreatePangoTexture(char *fontdescription, int fontsize, char *txt, cairo_surface_t **surface, int *w, int *h)
Definition glpangofont.c:50
static PangoLayout * get_pango_layout(char *markup_text, char *fontdescription, int fontsize, double *width, double *height)
Definition glpangofont.c:14
void free(void *)
node NULL
Definition grammar.y:163
static int layout(graph_t *g, layout_info *infop)
Definition layout.c:811