summaryrefslogtreecommitdiff
path: root/src/fonts-pangocairo.cc
blob: 91ed62f3a91a592e9ac20db698befabf30efb6e8 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
 * Copyright (C) 2003,2008 Red Hat, Inc.
 * Copyright © 2019, 2020 Christian Persch
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "fonts-pangocairo.hh"

#include "cairo-glue.hh"
#include "debug.h"
#include "vtedefines.hh"

/* Have a space between letters to make sure ligatures aren't used when caching the glyphs: bug 793391. */
#define VTE_DRAW_SINGLE_WIDE_CHARACTERS	\
					"  ! \" # $ % & ' ( ) * + , - . / " \
					"0 1 2 3 4 5 6 7 8 9 " \
					": ; < = > ? @ " \
					"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z " \
					"[ \\ ] ^ _ ` " \
					"a b c d e f g h i j k l m n o p q r s t u v w x y z " \
					"{ | } ~ " \
					""

static inline bool
_vte_double_equal(double a,
                  double b)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
        return a == b;
#pragma GCC diagnostic pop
}

#define FONT_CACHE_TIMEOUT (30) /* seconds */

namespace vte {
namespace view {

static GHashTable* s_font_info_for_context{nullptr};

FontInfo::UnistrInfo*
FontInfo::find_unistr_info(vteunistr c)
{
	if (G_LIKELY (c < G_N_ELEMENTS(m_ascii_unistr_info)))
		return &m_ascii_unistr_info[c];

	if (G_UNLIKELY (m_other_unistr_info == nullptr))
		m_other_unistr_info = g_hash_table_new_full(nullptr, nullptr, nullptr, (GDestroyNotify)unistr_info_destroy);

	auto uinfo = reinterpret_cast<UnistrInfo*>(g_hash_table_lookup(m_other_unistr_info, GINT_TO_POINTER(c)));
	if (G_LIKELY (uinfo))
		return uinfo;

	uinfo = new UnistrInfo{};
	g_hash_table_insert(m_other_unistr_info, GINT_TO_POINTER (c), uinfo);
	return uinfo;
}

void
FontInfo::cache_ascii()
{
	PangoLayoutLine *line;
	PangoGlyphItemIter iter;
	PangoGlyphItem *glyph_item;
	PangoGlyphString *glyph_string;
	PangoFont *pango_font;
	cairo_scaled_font_t *scaled_font;
	const char *text;
	gboolean more;
	PangoLanguage *language;
	gboolean latin_uses_default_language;

	/* We have m_layout holding most ASCII characters.  We want to
	 * cache as much info as we can about the ASCII letters so we don't
	 * have to look them up again later */

	/* Don't cache if unknown glyphs found in layout */
	if (pango_layout_get_unknown_glyphs_count(m_layout.get()) != 0)
		return;

	language = pango_context_get_language(pango_layout_get_context(m_layout.get()));
	if (language == nullptr)
		language = pango_language_get_default ();
	latin_uses_default_language = pango_language_includes_script (language, PANGO_SCRIPT_LATIN);

	text = pango_layout_get_text(m_layout.get());

	line = pango_layout_get_line_readonly(m_layout.get(), 0);

	/* Don't cache if more than one font used for the line */
	if (G_UNLIKELY (!line || !line->runs || line->runs->next))
		return;

	glyph_item = (PangoGlyphItem *)line->runs->data;
	glyph_string = glyph_item->glyphs;
	pango_font = glyph_item->item->analysis.font;
	if (!pango_font)
		return;
	scaled_font = pango_cairo_font_get_scaled_font ((PangoCairoFont *) pango_font);
	if (!scaled_font)
		return;

	for (more = pango_glyph_item_iter_init_start (&iter, glyph_item, text);
	     more;
	     more = pango_glyph_item_iter_next_cluster (&iter))
	{
		PangoGlyphGeometry *geometry;
		PangoGlyph glyph;
		vteunistr c;

		/* Only cache simple clusters */
		if (iter.start_char +1 != iter.end_char  ||
		    iter.start_index+1 != iter.end_index ||
		    iter.start_glyph+1 != iter.end_glyph)
			continue;

		c = text[iter.start_index];
		glyph = glyph_string->glyphs[iter.start_glyph].glyph;
		geometry = &glyph_string->glyphs[iter.start_glyph].geometry;

		/* If not using the default locale language, only cache non-common
		 * characters as common characters get their font from their neighbors
		 * and we don't want to force Latin on them. */
		if (!latin_uses_default_language &&
                    g_unichar_get_script (c) <= G_UNICODE_SCRIPT_INHERITED)
			continue;

		/* Only cache simple glyphs */
		if (!(glyph <= 0xFFFF) || (geometry->x_offset | geometry->y_offset) != 0)
			continue;

		auto uinfo = find_unistr_info(c);
		if (G_UNLIKELY (uinfo->coverage() != UnistrInfo::Coverage::UNKNOWN))
			continue;

		auto ufi = &uinfo->m_ufi;

		uinfo->width = PANGO_PIXELS_CEIL (geometry->width);
		uinfo->has_unknown_chars = false;

		uinfo->set_coverage(UnistrInfo::Coverage::USE_CAIRO_GLYPH);

		ufi->using_cairo_glyph.scaled_font = cairo_scaled_font_reference (scaled_font);
		ufi->using_cairo_glyph.glyph_index = glyph;

#ifdef VTE_DEBUG
		m_coverage_count[0]++;
		m_coverage_count[(unsigned)uinfo->coverage()]++;
#endif
	}

#ifdef VTE_DEBUG
	_vte_debug_print (VTE_DEBUG_PANGOCAIRO,
			  "vtepangocairo: %p cached %d ASCII letters\n",
			  (void*)this, m_coverage_count[0]);
#endif
}

void
FontInfo::measure_font()
{
	PangoRectangle logical;

        /* Measure U+0021..U+007E individually instead of all together and then
         * averaging. For monospace fonts, the results should be the same, but
         * if the user (by design, or trough mis-configuration) uses a proportional
         * font, the latter method will greatly underestimate the required width,
         * leading to unreadable, overlapping characters.
         * https://gitlab.gnome.org/GNOME/vte/issues/138
         */
        auto max_width = 1;
        auto max_height = 1;
        for (char c = 0x21; c < 0x7f; ++c) {
                pango_layout_set_text(m_layout.get(), &c, 1);
                pango_layout_get_extents(m_layout.get(), nullptr, &logical);
                max_width = std::max(max_width, PANGO_PIXELS_CEIL(logical.width));
                max_height = std::max(max_height, PANGO_PIXELS_CEIL(logical.height));
        }

        /* Use the sample text to get the baseline */
	pango_layout_set_text(m_layout.get(), VTE_DRAW_SINGLE_WIDE_CHARACTERS, -1);
	pango_layout_get_extents(m_layout.get(), nullptr, &logical);
	/* We don't do CEIL for width since we are averaging;
	 * rounding is more accurate */
	m_ascent = PANGO_PIXELS_CEIL(pango_layout_get_baseline(m_layout.get()));

        m_height = max_height;
        m_width = max_width;

	/* Now that we shaped the entire ASCII character string, cache glyph
	 * info for them */
	cache_ascii();
}

FontInfo::FontInfo(vte::glib::RefPtr<PangoContext> context)
{
	_vte_debug_print (VTE_DEBUG_PANGOCAIRO,
			  "vtepangocairo: %p allocating FontInfo\n",
			  (void*)this);

	m_layout = vte::glib::take_ref(pango_layout_new(context.get()));

	auto tabs = pango_tab_array_new_with_positions(1, FALSE, PANGO_TAB_LEFT, 1);
	pango_layout_set_tabs(m_layout.get(), tabs);
	pango_tab_array_free(tabs);

        // FIXME!!!
	m_string = g_string_sized_new(VTE_UTF8_BPC+1);

        measure_font();

#if PANGO_VERSION_CHECK(1, 44, 0)
        /* Try using the font's metrics; see issue#163. */
        if (auto metrics = vte::take_freeable
            (pango_context_get_metrics(context.get(),
                                       nullptr /* use font from context */,
                                       nullptr /* use language from context */))) {
		/* Use provided metrics if possible */
		auto const ascent = PANGO_PIXELS_CEIL(pango_font_metrics_get_ascent(metrics.get()));
		auto const height = PANGO_PIXELS_CEIL(pango_font_metrics_get_height(metrics.get()));
#if 0
                /* Note that we cannot use the font's width, since doing so
                 * regresses issue#138 (non-monospaced font).
                 * FIXME: Make sure the font is monospace before we get
                 * here, and then use the font's width too.
                 */
		auto const width = PANGO_PIXELS_CEIL(pango_font_metrics_get_approximate_char_width(metrics.get()));
#endif /* 0 */

                /* Sometimes, the metrics return a lower height than the one we measured
                 * in measure_font(), causing cut-off at the bottom of the last line, see
                 * https://gitlab.gnome.org/GNOME/gnome-terminal/-/issues/340 . Therefore
                 * we only use the metrics when its height is at least that which we measured.
                 */
                if (ascent > 0 && height >= m_height) {
                        _vte_debug_print(VTE_DEBUG_PANGOCAIRO, "Using pango metrics\n");

                        m_ascent = ascent;
                        m_height = height;
#if 0
                        m_width = width;
#endif
                } else if (ascent >= 0 && height > 0) {
                        _vte_debug_print(VTE_DEBUG_PANGOCAIRO, "Disregarding pango metrics due to incorrect height (%d < %d)\n",
                                         height, m_height);
                } else {
                        _vte_debug_print(VTE_DEBUG_PANGOCAIRO, "Not using pango metrics due to not providing height or ascent\n");
                }
	}
#endif /* pango >= 1.44 */

	_vte_debug_print (VTE_DEBUG_PANGOCAIRO | VTE_DEBUG_MISC,
			  "vtepangocairo: %p font metrics = %dx%d (%d)\n",
			  (void*)this, m_width, m_height, m_ascent);

	g_hash_table_insert(s_font_info_for_context,
                            pango_layout_get_context(m_layout.get()),
                            this);

}

FontInfo::~FontInfo()
{
	g_hash_table_remove(s_font_info_for_context,
                            pango_layout_get_context(m_layout.get()));

#ifdef VTE_DEBUG
	_vte_debug_print (VTE_DEBUG_PANGOCAIRO,
			  "vtepangocairo: %p freeing font_info.  coverages %d = %d + %d + %d\n",
			  (void*)this,
			  m_coverage_count[0],
			  m_coverage_count[1],
			  m_coverage_count[2],
			  m_coverage_count[3]);
#endif

	g_string_free(m_string, true);

	if (m_other_unistr_info) {
		g_hash_table_destroy(m_other_unistr_info);
	}
}

static GQuark
fontconfig_timestamp_quark (void)
{
	static GQuark quark;

	if (G_UNLIKELY (!quark))
		quark = g_quark_from_static_string ("vte-fontconfig-timestamp");

	return quark;
}

static void
vte_pango_context_set_fontconfig_timestamp (PangoContext *context,
					    guint         fontconfig_timestamp)
{
	g_object_set_qdata ((GObject *) context,
			    fontconfig_timestamp_quark (),
			    GUINT_TO_POINTER (fontconfig_timestamp));
}

static guint
vte_pango_context_get_fontconfig_timestamp (PangoContext *context)
{
	return GPOINTER_TO_UINT (g_object_get_qdata ((GObject *) context,
						     fontconfig_timestamp_quark ()));
}

static guint
context_hash (PangoContext *context)
{
	return pango_units_from_double (pango_cairo_context_get_resolution (context))
	     ^ pango_font_description_hash (pango_context_get_font_description (context))
	     ^ cairo_font_options_hash (pango_cairo_context_get_font_options (context))
	     ^ GPOINTER_TO_UINT (pango_context_get_language (context))
	     ^ vte_pango_context_get_fontconfig_timestamp (context);
}

static gboolean
context_equal (PangoContext *a,
	       PangoContext *b)
{
	return _vte_double_equal(pango_cairo_context_get_resolution(a), pango_cairo_context_get_resolution (b))
	    && pango_font_description_equal (pango_context_get_font_description (a), pango_context_get_font_description (b))
	    && cairo_font_options_equal (pango_cairo_context_get_font_options (a), pango_cairo_context_get_font_options (b))
	    && pango_context_get_language (a) == pango_context_get_language (b)
	    && vte_pango_context_get_fontconfig_timestamp (a) == vte_pango_context_get_fontconfig_timestamp (b);
}

// FIXMEchpe return vte::base::RefPtr<FontInfo>
FontInfo*
FontInfo::create_for_context(vte::glib::RefPtr<PangoContext> context,
                             PangoFontDescription const* desc,
                             PangoLanguage* language,
                             cairo_font_options_t const* font_options,
                             guint fontconfig_timestamp)
{
	if (!PANGO_IS_CAIRO_FONT_MAP(pango_context_get_font_map(context.get()))) {
		/* Ouch, Gtk+ switched over to some drawing system?
		 * Lets just create one from the default font map.
		 */
		context = vte::glib::take_ref(pango_font_map_create_context(pango_cairo_font_map_get_default()));
	}

	vte_pango_context_set_fontconfig_timestamp(context.get(), fontconfig_timestamp);

	pango_context_set_base_dir(context.get(), PANGO_DIRECTION_LTR);

	if (desc)
		pango_context_set_font_description(context.get(), desc);

        if (language != nullptr &&
            language != pango_context_get_language(context.get()))
                pango_context_set_language(context.get(), language);

        {
                // Make sure our contexts have a font_options set.  We use
                // this invariant in our context hash and equal functions.
                auto builtin_font_options = vte::take_freeable(cairo_font_options_create());

#if VTE_GTK == 4
                // On gtk4, we need to ensure Pango and cairo are configured to quantize
                // and hint font metrics.  Terminal cells in vte have integer pixel sizes.
                // If Pango is configured to do sub-pixel glyph advances, a small fractional
                // part might get rounded up to a whole pixel; so the character spacing will
                // appear too wide. Setting the cairo hint metrics option ensures that there
                // are integer numbers of pixels both above and below the baseline.
                // See issue#2573.
                cairo_font_options_set_hint_metrics(builtin_font_options.get(),
                                                    CAIRO_HINT_METRICS_ON);
#endif /* VTE_GTK == 4 */

                // Allow using the API to override the built-in hint metrics setting.
                if (!font_options)
                        font_options = builtin_font_options.get();

                if (auto const ctx_font_options = pango_cairo_context_get_font_options(context.get())) {
                        auto const merged_font_options =
                                vte::take_freeable(cairo_font_options_copy(ctx_font_options));
                        cairo_font_options_merge(merged_font_options.get(),
                                                 font_options);
                        pango_cairo_context_set_font_options(context.get(),
                                                             merged_font_options.get());
                } else {
                        pango_cairo_context_set_font_options(context.get(), font_options);
                }

#if VTE_GTK == 4
                // If hinting font metrics, also make sure to round glyph positions
                // to integers.  See issue#2573.
                if (auto const ctx_font_options = pango_cairo_context_get_font_options(context.get());
                    ctx_font_options &&
                    cairo_version() >= CAIRO_VERSION_ENCODE(1, 17, 4)) {
                        auto const hint_metrics = cairo_font_options_get_hint_metrics(ctx_font_options);
                        pango_context_set_round_glyph_positions(context.get(),
                                                                hint_metrics == CAIRO_HINT_METRICS_ON);
                } else {
                        pango_context_set_round_glyph_positions(context.get(), false);
                }
#endif /* VTE_GTK == 4 */
        }

	if (G_UNLIKELY(s_font_info_for_context == nullptr))
		s_font_info_for_context = g_hash_table_new((GHashFunc) context_hash, (GEqualFunc) context_equal);

	auto info = reinterpret_cast<FontInfo*>(g_hash_table_lookup(s_font_info_for_context, context.get()));
	if (G_LIKELY(info)) {
		_vte_debug_print (VTE_DEBUG_PANGOCAIRO,
				  "vtepangocairo: %p found FontInfo in cache\n",
				  info);
		info = info->ref();
	} else {
                info = new FontInfo{std::move(context)};
	}

	return info;
}

#if VTE_GTK == 3
FontInfo*
FontInfo::create_for_screen(GdkScreen* screen,
                            PangoFontDescription const* desc,
                            PangoLanguage* language,
                            cairo_font_options_t const* font_options)
{
	auto settings = gtk_settings_get_for_screen(screen);
	auto fontconfig_timestamp = guint{};
	g_object_get (settings, "gtk-fontconfig-timestamp", &fontconfig_timestamp, nullptr);
	return create_for_context(vte::glib::take_ref(gdk_pango_context_get_for_screen(screen)),
                                  desc, language, font_options, fontconfig_timestamp);
}
#endif /* VTE_GTK */

FontInfo*
FontInfo::create_for_widget(GtkWidget* widget,
                            PangoFontDescription const* desc,
                            cairo_font_options_t const* font_options)
{
#if VTE_GTK == 3
	auto screen = gtk_widget_get_screen(widget);
	return create_for_screen(screen, desc, nullptr, font_options);
#elif VTE_GTK == 4
        auto display = gtk_widget_get_display(widget);
        auto settings = gtk_settings_get_for_display(display);
        auto fontconfig_timestamp = guint{};
        g_object_get (settings, "gtk-fontconfig-timestamp", &fontconfig_timestamp, nullptr);
        return create_for_context(vte::glib::take_ref(gtk_widget_create_pango_context(widget)),
                                  desc, nullptr, font_options, fontconfig_timestamp);
        // FIXMEgtk4: this uses a per-widget context, while the gtk3 code uses a per-screen
        // one. That means there may be a lot less sharing and a lot more FontInfo's around?
#endif
}

FontInfo::UnistrInfo*
FontInfo::get_unistr_info(vteunistr c)
{
	PangoRectangle logical;
	PangoLayoutLine *line;

	auto uinfo = find_unistr_info(c);
	if (G_LIKELY (uinfo->coverage() != UnistrInfo::Coverage::UNKNOWN))
		return uinfo;

	auto ufi = &uinfo->m_ufi;

	g_string_set_size(m_string, 0);
	_vte_unistr_append_to_string(c, m_string);
	pango_layout_set_text(m_layout.get(), m_string->str, m_string->len);
	pango_layout_get_extents(m_layout.get(), NULL, &logical);

	uinfo->width = PANGO_PIXELS_CEIL (logical.width);

	line = pango_layout_get_line_readonly(m_layout.get(), 0);

	uinfo->has_unknown_chars = pango_layout_get_unknown_glyphs_count(m_layout.get()) != 0;
	/* we use PangoLayoutRun rendering unless there is exactly one run in the line. */
	if (G_UNLIKELY (!line || !line->runs || line->runs->next))
	{
		uinfo->set_coverage(UnistrInfo::Coverage::USE_PANGO_LAYOUT_LINE);

		ufi->using_pango_layout_line.line = pango_layout_line_ref (line);
		/* we hold a manual reference on layout.  pango currently
		 * doesn't work if line->layout is NULL.  ugh! */
		pango_layout_set_text(m_layout.get(), "", -1); /* make layout disassociate from the line */
		ufi->using_pango_layout_line.line->layout = (PangoLayout *)g_object_ref(m_layout.get());

	} else {
		PangoGlyphItem *glyph_item = (PangoGlyphItem *)line->runs->data;
		PangoFont *pango_font = glyph_item->item->analysis.font;
		PangoGlyphString *glyph_string = glyph_item->glyphs;

		/* we use fast cairo path if glyph string has only one real
		 * glyph and at origin */
		if (!uinfo->has_unknown_chars &&
		    glyph_string->num_glyphs == 1 && glyph_string->glyphs[0].glyph <= 0xFFFF &&
		    (glyph_string->glyphs[0].geometry.x_offset |
		     glyph_string->glyphs[0].geometry.y_offset) == 0)
		{
			cairo_scaled_font_t *scaled_font = pango_cairo_font_get_scaled_font ((PangoCairoFont *) pango_font);

			if (scaled_font) {
				uinfo->set_coverage(UnistrInfo::Coverage::USE_CAIRO_GLYPH);

				ufi->using_cairo_glyph.scaled_font = cairo_scaled_font_reference (scaled_font);
				ufi->using_cairo_glyph.glyph_index = glyph_string->glyphs[0].glyph;
			}
		}

		/* use pango fast path otherwise */
		if (G_UNLIKELY (uinfo->coverage() == UnistrInfo::Coverage::UNKNOWN)) {
			uinfo->set_coverage(UnistrInfo::Coverage::USE_PANGO_GLYPH_STRING);

			ufi->using_pango_glyph_string.font = pango_font ? (PangoFont *)g_object_ref (pango_font) : NULL;
			ufi->using_pango_glyph_string.glyph_string = pango_glyph_string_copy (glyph_string);
		}
	}

	/* release internal layout resources */
	pango_layout_set_text(m_layout.get(), "", -1);

#ifdef VTE_DEBUG
	m_coverage_count[0]++;
	m_coverage_count[uinfo->m_coverage]++;
#endif

	return uinfo;
}

} // namespace view
} // namespace vte