| 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 "config.h" |
| 12 |
|
| 13 |
#include <glcomp/glpangofont.h> |
| 14 |
#include <stdlib.h> |
| 15 |
|
| 16 |
static PangoLayout *get_pango_layout(const char *markup_text, |
| 17 |
char *fontdescription, int fontsize, |
| 18 |
double *width, double *height) |
| 19 |
{ |
| 20 |
PangoFontMap *fontmap; |
| 21 |
PangoLayout *layout; |
| 22 |
int pango_width, pango_height; |
| 23 |
char *text; |
| 24 |
PangoAttrList *attr_list; |
| 25 |
fontmap = pango_cairo_font_map_get_default(); |
| 26 |
|
| 27 |
if (!pango_parse_markup |
| 28 |
(markup_text, -1, '\0', &attr_list, &text, NULL, NULL)) |
| 29 |
return NULL; |
| 30 |
PangoContext *const context = pango_font_map_create_context(fontmap); |
| 31 |
layout = pango_layout_new(context); |
| 32 |
g_object_unref(context); |
| 33 |
pango_layout_set_text(layout, text, -1); |
| 34 |
free(text); |
| 35 |
PangoFontDescription *const desc = |
| 36 |
pango_font_description_from_string(fontdescription); |
| 37 |
pango_font_description_set_size(desc, (int)(fontsize * PANGO_SCALE)); |
| 38 |
pango_layout_set_font_description(layout, desc); |
| 39 |
pango_layout_set_attributes(layout, attr_list); |
| 40 |
pango_attr_list_unref(attr_list); |
| 41 |
pango_font_description_free(desc); |
| 42 |
pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); |
| 43 |
|
| 44 |
pango_layout_get_size(layout, &pango_width, &pango_height); |
| 45 |
|
| 46 |
*width = (double) pango_width / PANGO_SCALE; |
| 47 |
*height = (double) pango_height / PANGO_SCALE; |
| 48 |
|
| 49 |
return layout; |
| 50 |
} |
| 51 |
|
| 52 |
unsigned char *glCompCreatePangoTexture(char *fontdescription, int fontsize, |
| 53 |
const char *txt, |
| 54 |
cairo_surface_t **surface, int *w, |
| 55 |
int *h) { |
| 56 |
PangoLayout *layout; |
| 57 |
double width, height; |
| 58 |
*surface = NULL; |
| 59 |
|
| 60 |
layout = |
| 61 |
get_pango_layout(txt, fontdescription, fontsize, &width, &height); |
| 62 |
if (layout == NULL) { |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
//create the right size canvas for character set |
| 66 |
*surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, (int)width, |
| 67 |
(int) height); |
| 68 |
|
| 69 |
cairo_t *cr = cairo_create(*surface); |
| 70 |
//set pen color to white |
| 71 |
cairo_set_source_rgba(cr, 1, 1, 1, 1); |
| 72 |
//draw the text |
| 73 |
pango_cairo_show_layout(cr, layout); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
*w = (int) width; |
| 78 |
*h = (int) height; |
| 79 |
g_object_unref(layout); |
| 80 |
cairo_destroy(cr); |
| 81 |
|
| 82 |
return cairo_image_surface_get_data(*surface); |
| 83 |
} |