Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsgopenvgfontglyphcache.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
6#include <private/qfontengine_p.h>
7#include <private/qrawfont_p.h>
8
10
11QSGOpenVGFontGlyphCacheManager::QSGOpenVGFontGlyphCacheManager()
12{
13
14}
15
20
22{
23 return m_caches.value(font, nullptr);
24}
25
27{
28 m_caches.insert(font, cache);
29}
30
32 : m_manager(manager)
33{
34 m_referenceFont = font;
35 QRawFontPrivate *fontD = QRawFontPrivate::get(font);
36 m_glyphCount = fontD->fontEngine->glyphCount();
37 m_font = vgCreateFont(0);
38}
39
41{
42 if (m_font != VG_INVALID_HANDLE)
43 vgDestroyFont(m_font);
44}
45
46void QSGOpenVGFontGlyphCache::populate(const QVector<quint32> &glyphs)
47{
48 QSet<quint32> referencedGlyphs;
49 QSet<quint32> newGlyphs;
50 int count = glyphs.count();
51 for (int i = 0; i < count; ++i) {
52 quint32 glyphIndex = glyphs.at(i);
53 if ((int) glyphIndex >= glyphCount()) {
54 qWarning("Warning: glyph is not available with index %d", glyphIndex);
55 continue;
56 }
57
58 referencedGlyphs.insert(glyphIndex);
59
60
61 if (!m_glyphReferences.contains(glyphIndex)) {
62 newGlyphs.insert(glyphIndex);
63 }
64 }
65
66 referenceGlyphs(referencedGlyphs);
67 if (!newGlyphs.isEmpty())
68 requestGlyphs(newGlyphs);
69}
70
71void QSGOpenVGFontGlyphCache::release(const QVector<quint32> &glyphs)
72{
73 QSet<quint32> unusedGlyphs;
74 int count = glyphs.count();
75 for (int i = 0; i < count; ++i) {
76 quint32 glyphIndex = glyphs.at(i);
77 unusedGlyphs.insert(glyphIndex);
78 }
79 releaseGlyphs(unusedGlyphs);
80}
81
82void QSGOpenVGFontGlyphCache::requestGlyphs(const QSet<quint32> &glyphs)
83{
84 VGfloat origin[2];
85 VGfloat escapement[2];
86 QRawFont rawFont = m_referenceFont;
87
88 for (auto glyph : glyphs) {
89 // Calculate the path for the glyph and cache it.
90 QPainterPath path = rawFont.pathForGlyph(glyph);
91 VGPath vgPath;
92 if (!path.isEmpty()) {
93 vgPath = QSGOpenVGHelpers::qPainterPathToVGPath(path);
94 } else {
95 // Probably a "space" character with no visible outline.
96 vgPath = VG_INVALID_HANDLE;
97 }
98 origin[0] = 0;
99 origin[1] = 0;
100 escapement[0] = 0;
101 escapement[1] = 0;
102 vgSetGlyphToPath(m_font, glyph, vgPath, VG_FALSE, origin, escapement);
103 vgDestroyPath(vgPath); // Reduce reference count.
104 }
105
106}
107
108void QSGOpenVGFontGlyphCache::referenceGlyphs(const QSet<quint32> &glyphs)
109{
110 for (auto glyph : glyphs) {
111 if (m_glyphReferences.contains(glyph))
112 m_glyphReferences[glyph] += 1;
113 else
114 m_glyphReferences.insert(glyph, 1);
115 }
116}
117
118void QSGOpenVGFontGlyphCache::releaseGlyphs(const QSet<quint32> &glyphs)
119{
120 for (auto glyph : glyphs) {
121 int references = m_glyphReferences[glyph] -= 1;
122 if (references == 0) {
123 vgClearGlyph(m_font, glyph);
124 m_glyphReferences.remove(glyph);
125 }
126 }
127}
128
129QT_END_NAMESPACE
QSGOpenVGFontGlyphCache * cache(const QRawFont &font)
void insertCache(const QRawFont &font, QSGOpenVGFontGlyphCache *cache)
void populate(const QVector< quint32 > &glyphs)
void release(const QVector< quint32 > &glyphs)
QSGOpenVGFontGlyphCache(QSGOpenVGFontGlyphCacheManager *manager, const QRawFont &font)