forked from zelch/TemuTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glyphcache.c
216 lines (167 loc) · 4.84 KB
/
glyphcache.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <gdk/gdkx.h>
#include <fontconfig/fontconfig.h>
/* neenur neenur: */
#define PANGO_ENABLE_ENGINE
#include <pango/pangoxft.h>
#include "glyphcache.h"
TGlyphCache *glyph_cache_new(GtkWidget *widget,
PangoFontDescription *font_desc)
{
TGlyphCache *cache;
cache = g_new(TGlyphCache, 1);
memset(cache, 0, sizeof(*cache));
cache->context = gtk_widget_create_pango_context(widget);
cache->lang = pango_context_get_language(cache->context);
cache->display = gtk_widget_get_display(widget);
g_object_ref (cache->display);
cache->screen = gtk_widget_get_screen(widget);
g_object_ref (cache->screen);
if (font_desc)
glyph_cache_set_font(cache, font_desc);
return cache;
}
void glyph_destroy_gfi(gpointer data)
{
g_slice_free(TGlyphInfo, data);
}
void glyph_cache_destroy(TGlyphCache *cache)
{
if (cache->hash) {
g_hash_table_destroy(cache->hash);
/* font_set can't be unref'd apparently */
}
if (cache->context)
g_object_unref (cache->context);
if (cache->display)
g_object_unref (cache->display);
if (cache->screen)
g_object_unref (cache->screen);
if (cache->font_set)
g_object_unref(cache->font_set);
/* Neither can context or lang */
g_free(cache);
}
void glyph_cache_set_font(TGlyphCache *cache, PangoFontDescription *font_desc)
{
PangoFontMap *font_map;
PangoFontMetrics *metrics;
if (cache->hash) {
g_hash_table_destroy(cache->hash);
g_object_unref(cache->font_set);
}
cache->hash = g_hash_table_new_full(NULL, NULL, NULL, glyph_destroy_gfi);
font_map = pango_xft_get_font_map(
GDK_DISPLAY_XDISPLAY(cache->display),
GDK_SCREEN_XNUMBER(cache->screen)
);
g_object_ref (font_map);
cache->font_set = pango_font_map_load_fontset(
font_map,
cache->context,
font_desc,
cache->lang
);
g_object_unref(font_map);
font_map = NULL;
metrics = pango_fontset_get_metrics(cache->font_set);
pango_font_metrics_ref(metrics);
cache->ascent = pango_font_metrics_get_ascent(metrics);
cache->descent = pango_font_metrics_get_descent(metrics);
cache->width = pango_font_metrics_get_approximate_digit_width(metrics);
cache->height = cache->ascent + cache->descent;
if (cache->width <= 0) {
fprintf (stderr, "Warning: cache->width = %d\n", cache->width);
cache->width = cache->height / 2;
}
pango_font_metrics_unref(metrics);
}
TGlyphInfo *glyph_cache_get_info(TGlyphCache *cache, gunichar glyph) {
TGlyphInfo *gfi;
XftFont *xftfont;
PangoFont *font;
PangoGlyph pglyph;
PangoRectangle ink, logical;
gint ascent, descent;
gint x_offset;
gint nominal_width;
double scale_y, scale_x;
if (!cache->hash)
return NULL;
gfi = g_hash_table_lookup(cache->hash, GINT_TO_POINTER(glyph));
if (gfi)
return gfi;
font = pango_fontset_get_font(cache->font_set, glyph);
pglyph = pango_fc_font_get_glyph(PANGO_FC_FONT(font), glyph);
if (!pglyph) {
fprintf (stderr, "Error: Unable to find glyph %d.\n", glyph);
if (!pglyph)
pglyph = pango_fc_font_get_glyph(PANGO_FC_FONT(font), glyph);
if (!pglyph)
pglyph = PANGO_GET_UNKNOWN_GLYPH (glyph);
if (!pglyph)
return NULL;
}
pango_font_get_glyph_extents(font, pglyph, &ink, &logical);
ascent = PANGO_ASCENT(logical);
descent = PANGO_DESCENT(logical);
xftfont = pango_xft_font_get_font(font);
x_offset = 0;
nominal_width = cache->width;
if (g_unichar_iswide(glyph))
nominal_width *= 2;
scale_x = scale_y = 1.0;
if (logical.width > nominal_width) {
if (logical.width)
scale_x = (double)nominal_width / (double)logical.width;
else
scale_x = 1.0;
}
if (logical.height != cache->height) {
double scale;
if (ascent) {
scale_y = (double)cache->ascent / (double)ascent;
} else {
scale_y = 1.0;
}
if (descent) {
scale = (double)cache->descent / (double)descent;
if (scale < scale_y)
scale_y = scale;
}
}
if (scale_x >= 1.0) {
scale_x = 1.0;
x_offset += (nominal_width - logical.width) / 2;
}
if (scale_x < 1.0 || scale_y != 1.0) {
FcBool scalable;
FcPatternGetBool(xftfont->pattern, FC_SCALABLE, 0, &scalable);
if (!scalable) {
/* Bah. Need better handling of non-scalable fonts */
if (scale_x < 1.0) scale_x = 1.0;
scale_y = 1.0;
} else if (scale_x < 1.0 || scale_y != 1.0) {
FcPattern *pattern;
FcMatrix mat;
pattern = FcPatternDuplicate(xftfont->pattern);
FcMatrixInit (&mat);
FcMatrixScale(&mat, scale_x, scale_y);
FcPatternDel (pattern, FC_MATRIX);
FcPatternAddMatrix(pattern, FC_MATRIX, &mat);
xftfont = XftFontOpenPattern(
GDK_DISPLAY_XDISPLAY(cache->display),
pattern
);
}
ascent = ascent * scale_y;
}
g_object_unref(font);
// gfi = calloc(sizeof(TGlyphInfo), 1);
gfi = g_slice_new(TGlyphInfo);
gfi->font = xftfont;
gfi->x_offset = PANGO_PIXELS(x_offset);
gfi->y_offset = cache->ascent + (cache->ascent - ascent);
gfi->y_offset = PANGO_PIXELS(gfi->y_offset);
g_hash_table_insert(cache->hash, GINT_TO_POINTER(glyph), gfi);
return gfi;
}