diff options
Diffstat (limited to 'src/plugins/terminal/glyphcache.cpp')
-rw-r--r-- | src/plugins/terminal/glyphcache.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/plugins/terminal/glyphcache.cpp b/src/plugins/terminal/glyphcache.cpp new file mode 100644 index 00000000000..72a0fd7b9d1 --- /dev/null +++ b/src/plugins/terminal/glyphcache.cpp @@ -0,0 +1,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 |