-
Notifications
You must be signed in to change notification settings - Fork 0
/
pango.cpp
83 lines (68 loc) · 2.4 KB
/
pango.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* SPDX-FileCopyrightText: © 2022 Alexandros Theodotou <alex@zrythm.org>
*
* SPDX-License-Identifier: LicenseRef-ZrythmLicense
*/
#include "pango.h"
#include "string.h"
#include <fontconfig/fontconfig.h>
#include <pango/pangoft2.h>
PangoLayout *
z_pango_create_layout_from_description (
GtkWidget * widget,
PangoFontDescription * descr)
{
PangoLayout * layout = NULL;
char * str = pango_font_description_to_string (descr);
g_debug ("font description: %s", str);
#ifdef HAVE_BUNDLED_DSEG
if (string_contains_substr (str, "DSEG"))
{
FcConfig * fc_config = FcConfigCreate ();
/* add fonts/zrythm dir to find DSEG font */
char * fontdir = gZrythm->dir_mgr->get_dir (ZRYTHM_DIR_SYSTEM_FONTSDIR);
FcConfigAppFontAddDir (fc_config, (const unsigned char *) fontdir);
g_free (fontdir);
FcConfigBuildFonts (fc_config);
PangoFontMap * font_map =
pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT);
pango_fc_font_map_set_config (PANGO_FC_FONT_MAP (font_map), fc_config);
PangoContext * context =
pango_font_map_create_context (PANGO_FONT_MAP (font_map));
PangoLayout * pangoLayout = pango_layout_new (context);
FcPattern * pattern = FcPatternCreate ();
FcObjectSet * os = FcObjectSetBuild (FC_FAMILY, NULL);
FcFontSet * fs = FcFontList (fc_config, pattern, os);
FcPatternDestroy (pattern);
FcObjectSetDestroy (os);
for (int i = 0; i < fs->nfont; i++)
{
guchar * font_name = FcNameUnparse (fs->fonts[i]);
PangoFontDescription * desc =
pango_font_description_from_string ((gchar *) font_name);
pango_font_map_load_font (PANGO_FONT_MAP (font_map), context, desc);
pango_font_description_free (desc);
g_debug ("fontname: %s", font_name);
g_free (font_name);
}
layout = pangoLayout;
g_object_unref (context);
g_object_unref (font_map);
FcConfigDestroy (fc_config);
}
else
{
#endif
layout = gtk_widget_create_pango_layout (widget, NULL);
#ifdef HAVE_BUNDLED_DSEG
}
#endif
g_free (str);
g_return_val_if_fail (layout, NULL);
pango_layout_set_font_description (layout, descr);
/* try a small test */
int test_w, test_h;
pango_layout_set_markup (layout, "test", -1);
pango_layout_get_pixel_size (layout, &test_w, &test_h);
return layout;
}