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
qsgopenvgglyphnode.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
10#include <cmath>
11
12QT_BEGIN_NAMESPACE
13
14QSGOpenVGGlyphNode::QSGOpenVGGlyphNode(QSGRenderContext *rc)
15 : m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 0)
16 , m_style(QQuickText::Normal)
17 , m_glyphCache(nullptr)
18{
19 // Set Dummy material to avoid asserts
20 setMaterial((QSGMaterial*)1);
21 setGeometry(&m_geometry);
22 m_fontColorPaint = vgCreatePaint();
23 m_styleColorPaint = vgCreatePaint();
24
25 // Get handle to Glyph Cache
26 m_renderContext = static_cast<QSGOpenVGRenderContext*>(rc);
27}
28
30{
31 if (m_glyphCache)
32 m_glyphCache->release(m_glyphRun.glyphIndexes());
33
34 vgDestroyPaint(m_fontColorPaint);
35 vgDestroyPaint(m_styleColorPaint);
36}
37
38void QSGOpenVGGlyphNode::setGlyphs(const QPointF &position, const QGlyphRun &glyphs)
39{
40 // Obtain glyph cache for font
41 auto oldGlyphCache = m_glyphCache;
42 m_glyphCache = m_renderContext->glyphCache(glyphs.rawFont());
43 if (m_glyphCache != oldGlyphCache) {
44 if (oldGlyphCache)
45 oldGlyphCache->release(m_glyphRun.glyphIndexes());
46 }
47 m_glyphCache->populate(glyphs.glyphIndexes());
48
49 m_position = position;
50 m_glyphRun = glyphs;
51 m_bounding_rect = glyphs.boundingRect().translated(m_position - QPointF(0.0, glyphs.rawFont().ascent()));
52
53 // Recreate ajustments
54 m_xAdjustments.clear();
55 m_yAdjustments.clear();
56
57 for (int i = 1; i < glyphs.positions().count(); ++i) {
58 m_xAdjustments.append(glyphs.positions().at(i).x() - glyphs.positions().at(i-1).x());
59 m_yAdjustments.append(glyphs.positions().at(i).y() - glyphs.positions().at(i-1).y());
60 }
61}
62
63void QSGOpenVGGlyphNode::setColor(const QColor &color)
64{
65 m_color = color;
66 vgSetParameteri(m_fontColorPaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
67 vgSetParameterfv(m_fontColorPaint, VG_PAINT_COLOR, 4, QSGOpenVGHelpers::qColorToVGColor(m_color, opacity()).constData());
68}
69
70void QSGOpenVGGlyphNode::setStyle(QQuickText::TextStyle style)
71{
72 m_style = style;
73}
74
75void QSGOpenVGGlyphNode::setStyleColor(const QColor &color)
76{
77 m_styleColor = color;
78 vgSetParameteri(m_styleColorPaint, VG_PAINT_TYPE, VG_PAINT_TYPE_COLOR);
79 vgSetParameterfv(m_styleColorPaint, VG_PAINT_COLOR, 4, QSGOpenVGHelpers::qColorToVGColor(m_styleColor, opacity()).constData());
80}
81
83{
84 return QPointF();
85}
86
90
92{
93}
94
96{
97 if (m_glyphRun.positions().count() == 0)
98 return;
99
100 // Rendering Style
101 qreal offset = 1.0;
102
103 QOpenVGOffscreenSurface *offscreenSurface = nullptr;
104
105 // Set Transform
106 vgSeti(VG_MATRIX_MODE, VG_MATRIX_GLYPH_USER_TO_SURFACE);
107 if (transform().isAffine()) {
108 vgLoadMatrix(transform().constData());
109 } else {
110 vgLoadIdentity();
111 offscreenSurface = new QOpenVGOffscreenSurface(QSize(std::ceil(m_bounding_rect.width()), std::ceil(m_bounding_rect.height())));
112 offscreenSurface->makeCurrent();
113 }
114
115 // Set Quality
116 vgSeti(VG_RENDERING_QUALITY, VG_RENDERING_QUALITY_BETTER);
117
118
119 switch (m_style) {
120 case QQuickText::Normal: break;
121 case QQuickText::Outline:
122 // Set the correct fill state
123 vgSetPaint(m_styleColorPaint, VG_FILL_PATH);
124 drawGlyphsAtOffset(QPointF(0, offset));
125 drawGlyphsAtOffset(QPointF(0, -offset));
126 drawGlyphsAtOffset(QPointF(offset, 0));
127 drawGlyphsAtOffset(QPointF(-offset, 0));
128 break;
129 case QQuickText::Raised:
130 vgSetPaint(m_styleColorPaint, VG_FILL_PATH);
131 drawGlyphsAtOffset(QPointF(0, offset));
132 break;
133 case QQuickText::Sunken:
134 vgSetPaint(m_styleColorPaint, VG_FILL_PATH);
135 drawGlyphsAtOffset(QPointF(0, -offset));
136 break;
137 }
138
139 // Set the correct fill state
140 vgSetPaint(m_fontColorPaint, VG_FILL_PATH);
141 drawGlyphsAtOffset(QPointF(0.0, 0.0));
142
143 if (!transform().isAffine()) {
144 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
145 vgLoadMatrix(transform().constData());
146 offscreenSurface->doneCurrent();
147 vgDrawImage(offscreenSurface->image());
148 delete offscreenSurface;
149 }
150}
151
152void QSGOpenVGGlyphNode::setOpacity(float opacity)
153{
154 if (QSGOpenVGRenderable::opacity() != opacity) {
156 // Update Colors
157 setColor(m_color);
158 setStyleColor(m_styleColor);
159 }
160}
161
162void QSGOpenVGGlyphNode::drawGlyphsAtOffset(const QPointF &offset)
163{
164 QPointF firstPosition = m_glyphRun.positions()[0] + (m_position - QPointF(0, m_glyphRun.rawFont().ascent()));
165 VGfloat origin[2];
166 origin[0] = firstPosition.x() + offset.x();
167 origin[1] = firstPosition.y() + offset.y();
168 vgSetfv(VG_GLYPH_ORIGIN, 2, origin);
169
170 vgDrawGlyphs(m_glyphCache->font(),
171 m_glyphRun.glyphIndexes().count(),
172 (VGuint*)m_glyphRun.glyphIndexes().constData(),
173 m_xAdjustments.constData(),
174 m_yAdjustments.constData(),
175 VG_FILL_PATH,
176 VG_TRUE);
177}
178
179QT_END_NAMESPACE
void setColor(const QColor &color) override
void setStyle(QQuickText::TextStyle style) override
void setStyleColor(const QColor &color) override
void setPreferredAntialiasingMode(AntialiasingMode) override
QPointF baseLine() const override
void setOpacity(float opacity) override
void setGlyphs(const QPointF &position, const QGlyphRun &glyphs) override
const QOpenVGMatrix & transform() const
virtual void setOpacity(float opacity)