aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/terminal/glyphcache.cpp
blob: 72a0fd7b9d17bbc772c5817607e8af79bb17328e (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "glyphcache.h"

#include <QTextLayout>

namespace Terminal::Internal {

size_t qHash(const GlyphCacheKey &key, size_t seed = 0)
{
    return qHash(key.font, seed) ^ qHash(key.text, seed);
}

GlyphCache &GlyphCache::instance()
{
    static GlyphCache cache(5000);
    return cache;
}

const QGlyphRun *GlyphCache::get(const QFont &font, const QString &text)
{
    GlyphCacheKey key{font, text};
    if (auto *run = object(key))
        return run;

    QTextLayout layout;

    layout.setText(text);
    layout.setFont(font);

    layout.beginLayout();
    layout.createLine().setNumColumns(std::numeric_limits<int>::max());
    layout.endLayout();

    if (layout.lineCount() > 0) {
        const auto &line = layout.lineAt(0);
        const auto runs = line.glyphRuns();
        if (!runs.isEmpty()) {
            QGlyphRun *run = new QGlyphRun(layout.lineAt(0).glyphRuns().first());
            insert(key, run);
            return run;
        }
    }
    return nullptr;
}

} // namespace Terminal::Internal