1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qsgdistancefieldglyphnode_p.h"
41#include "qsgdistancefieldglyphnode_p_p.h"
42#include <QtQuick/private/qsgcontext_p.h>
43
44QT_BEGIN_NAMESPACE
45
46QSGDistanceFieldGlyphNode::QSGDistanceFieldGlyphNode(QSGRenderContext *context)
47 : m_glyphNodeType(RootGlyphNode)
48 , m_context(context)
49 , m_material(nullptr)
50 , m_glyph_cache(nullptr)
51 , m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 0)
52 , m_style(QQuickText::Normal)
53 , m_antialiasingMode(GrayAntialiasing)
54 , m_texture(nullptr)
55 , m_dirtyGeometry(false)
56 , m_dirtyMaterial(false)
57{
58 m_geometry.setDrawingMode(GL_TRIANGLES);
59 setGeometry(&m_geometry);
60 setFlag(UsePreprocess);
61#ifdef QSG_RUNTIME_DESCRIPTION
62 qsgnode_set_description(this, QLatin1String("glyphs"));
63#endif
64}
65
66QSGDistanceFieldGlyphNode::~QSGDistanceFieldGlyphNode()
67{
68 delete m_material;
69
70 if (m_glyphNodeType == SubGlyphNode)
71 return;
72
73 if (m_glyph_cache) {
74 m_glyph_cache->release(m_glyphs.glyphIndexes());
75 m_glyph_cache->unregisterGlyphNode(this);
76 m_glyph_cache->unregisterOwnerElement(ownerElement());
77 }
78}
79
80void QSGDistanceFieldGlyphNode::setColor(const QColor &color)
81{
82 m_color = color;
83 if (m_material != nullptr) {
84 m_material->setColor(color);
85 markDirty(DirtyMaterial);
86 } else {
87 m_dirtyMaterial = true;
88 }
89}
90
91void QSGDistanceFieldGlyphNode::setPreferredAntialiasingMode(AntialiasingMode mode)
92{
93 if (mode == m_antialiasingMode)
94 return;
95 m_antialiasingMode = mode;
96 m_dirtyMaterial = true;
97}
98
99void QSGDistanceFieldGlyphNode::setGlyphs(const QPointF &position, const QGlyphRun &glyphs)
100{
101 QRawFont font = glyphs.rawFont();
102 m_originalPosition = position;
103 m_position = QPointF(position.x(), position.y() - font.ascent());
104 m_glyphs = glyphs;
105
106 m_dirtyGeometry = true;
107 m_dirtyMaterial = true;
108
109 QSGDistanceFieldGlyphCache *oldCache = m_glyph_cache;
110 m_glyph_cache = m_context->distanceFieldGlyphCache(m_glyphs.rawFont());
111
112 if (m_glyphNodeType == SubGlyphNode)
113 return;
114
115 if (m_glyph_cache != oldCache) {
116 Q_ASSERT(ownerElement() != nullptr);
117 if (oldCache) {
118 oldCache->unregisterGlyphNode(this);
119 oldCache->unregisterOwnerElement(ownerElement());
120 }
121 m_glyph_cache->registerGlyphNode(this);
122 m_glyph_cache->registerOwnerElement(ownerElement());
123 }
124 m_glyph_cache->populate(glyphs.glyphIndexes());
125
126 const QVector<quint32> glyphIndexes = m_glyphs.glyphIndexes();
127 for (int i = 0; i < glyphIndexes.count(); ++i)
128 m_allGlyphIndexesLookup.insert(glyphIndexes.at(i));
129}
130
131void QSGDistanceFieldGlyphNode::setStyle(QQuickText::TextStyle style)
132{
133 if (m_style == style)
134 return;
135 m_style = style;
136 m_dirtyMaterial = true;
137}
138
139void QSGDistanceFieldGlyphNode::setStyleColor(const QColor &color)
140{
141 if (m_styleColor == color)
142 return;
143 m_styleColor = color;
144 m_dirtyMaterial = true;
145}
146
147void QSGDistanceFieldGlyphNode::update()
148{
149 if (m_dirtyMaterial)
150 updateMaterial();
151}
152
153void QSGDistanceFieldGlyphNode::preprocess()
154{
155 Q_ASSERT(m_glyph_cache);
156
157 m_glyph_cache->processPendingGlyphs();
158 m_glyph_cache->update();
159
160 if (m_dirtyGeometry)
161 updateGeometry();
162}
163
164void QSGDistanceFieldGlyphNode::invalidateGlyphs(const QVector<quint32> &glyphs)
165{
166 if (m_dirtyGeometry)
167 return;
168
169 for (int i = 0; i < glyphs.count(); ++i) {
170 if (m_allGlyphIndexesLookup.contains(glyphs.at(i))) {
171 m_dirtyGeometry = true;
172 return;
173 }
174 }
175}
176
177void QSGDistanceFieldGlyphNode::updateGeometry()
178{
179 Q_ASSERT(m_glyph_cache);
180
181 // Remove previously created sub glyph nodes
182 // We assume all the children are sub glyph nodes
183 QSGNode *subnode = firstChild();
184 QSGNode *nextNode = nullptr;
185 while (subnode) {
186 nextNode = subnode->nextSibling();
187 delete subnode;
188 subnode = nextNode;
189 }
190
191 QSGGeometry *g = geometry();
192
193 Q_ASSERT(g->indexType() == GL_UNSIGNED_SHORT);
194
195 QHash<const QSGDistanceFieldGlyphCache::Texture *, GlyphInfo> glyphsInOtherTextures;
196
197 const QVector<quint32> indexes = m_glyphs.glyphIndexes();
198 const QVector<QPointF> positions = m_glyphs.positions();
199 qreal fontPixelSize = m_glyphs.rawFont().pixelSize();
200
201 // The template parameters here are assuming that most strings are short, 64
202 // characters or less.
203 QVarLengthArray<QSGGeometry::TexturedPoint2D, 256> vp;
204 vp.reserve(indexes.size() * 4);
205 QVarLengthArray<ushort, 384> ip;
206 ip.reserve(indexes.size() * 6);
207
208 qreal maxTexMargin = m_glyph_cache->distanceFieldRadius();
209 qreal fontScale = m_glyph_cache->fontScale(fontPixelSize);
210 qreal margin = 2;
211 qreal texMargin = margin / fontScale;
212 if (texMargin > maxTexMargin) {
213 texMargin = maxTexMargin;
214 margin = maxTexMargin * fontScale;
215 }
216
217 for (int i = 0; i < indexes.size(); ++i) {
218 const int glyphIndex = indexes.at(i);
219 QSGDistanceFieldGlyphCache::TexCoord c = m_glyph_cache->glyphTexCoord(glyphIndex);
220
221 if (c.isNull())
222 continue;
223
224 const QPointF position = positions.at(i);
225
226 const QSGDistanceFieldGlyphCache::Texture *texture = m_glyph_cache->glyphTexture(glyphIndex);
227 if ((!texture->rhiBased && texture->textureId && !m_texture)
228 || (texture->rhiBased && texture->texture && !m_texture))
229 {
230 m_texture = texture;
231 }
232
233 // As we use UNSIGNED_SHORT indexing in the geometry, we overload the
234 // "glyphsInOtherTextures" concept as overflow for if there are more
235 // than 65535 vertices to render which would otherwise exceed the
236 // maximum index size. (leave 0xFFFF unused in order not to clash with
237 // primitive restart) This will cause sub-nodes to be recursively
238 // created to handle any number of glyphs.
239 if (m_texture != texture || vp.size() >= 65535) {
240 if (texture->textureId) {
241 GlyphInfo &glyphInfo = glyphsInOtherTextures[texture];
242 glyphInfo.indexes.append(glyphIndex);
243 glyphInfo.positions.append(position);
244 }
245 continue;
246 }
247
248 QSGDistanceFieldGlyphCache::Metrics metrics = m_glyph_cache->glyphMetrics(glyphIndex, fontPixelSize);
249
250 if (!metrics.isNull() && !c.isNull()) {
251 metrics.width += margin * 2;
252 metrics.height += margin * 2;
253 metrics.baselineX -= margin;
254 metrics.baselineY += margin;
255 c.xMargin -= texMargin;
256 c.yMargin -= texMargin;
257 c.width += texMargin * 2;
258 c.height += texMargin * 2;
259 }
260
261 qreal x = position.x() + metrics.baselineX + m_position.x();
262 qreal y = position.y() - metrics.baselineY + m_position.y();
263
264 m_boundingRect |= QRectF(x, y, metrics.width, metrics.height);
265
266 float cx1 = x;
267 float cx2 = x + metrics.width;
268 float cy1 = y;
269 float cy2 = y + metrics.height;
270
271 float tx1 = c.x + c.xMargin;
272 float tx2 = tx1 + c.width;
273 float ty1 = c.y + c.yMargin;
274 float ty2 = ty1 + c.height;
275
276 if (m_baseLine.isNull())
277 m_baseLine = position;
278
279 int o = vp.size();
280
281 QSGGeometry::TexturedPoint2D v1;
282 v1.set(cx1, cy1, tx1, ty1);
283 QSGGeometry::TexturedPoint2D v2;
284 v2.set(cx2, cy1, tx2, ty1);
285 QSGGeometry::TexturedPoint2D v3;
286 v3.set(cx1, cy2, tx1, ty2);
287 QSGGeometry::TexturedPoint2D v4;
288 v4.set(cx2, cy2, tx2, ty2);
289 vp.append(v1);
290 vp.append(v2);
291 vp.append(v3);
292 vp.append(v4);
293
294 ip.append(o + 0);
295 ip.append(o + 2);
296 ip.append(o + 3);
297 ip.append(o + 3);
298 ip.append(o + 1);
299 ip.append(o + 0);
300 }
301
302 QHash<const QSGDistanceFieldGlyphCache::Texture *, GlyphInfo>::const_iterator ite = glyphsInOtherTextures.constBegin();
303 while (ite != glyphsInOtherTextures.constEnd()) {
304 QGlyphRun subNodeGlyphRun(m_glyphs);
305 subNodeGlyphRun.setGlyphIndexes(ite->indexes);
306 subNodeGlyphRun.setPositions(ite->positions);
307
308 QSGDistanceFieldGlyphNode *subNode = new QSGDistanceFieldGlyphNode(m_context);
309 subNode->setGlyphNodeType(SubGlyphNode);
310 subNode->setColor(m_color);
311 subNode->setStyle(m_style);
312 subNode->setStyleColor(m_styleColor);
313 subNode->setPreferredAntialiasingMode(m_antialiasingMode);
314 subNode->setGlyphs(m_originalPosition, subNodeGlyphRun);
315 subNode->update();
316 subNode->updateGeometry(); // we have to explicitly call this now as preprocess won't be called before it's rendered
317 appendChildNode(subNode);
318
319 ++ite;
320 }
321
322 g->allocate(vp.size(), ip.size());
323 memcpy(g->vertexDataAsTexturedPoint2D(), vp.constData(), vp.size() * sizeof(QSGGeometry::TexturedPoint2D));
324 memcpy(g->indexDataAsUShort(), ip.constData(), ip.size() * sizeof(quint16));
325
326 setBoundingRect(m_boundingRect);
327 markDirty(DirtyGeometry);
328 m_dirtyGeometry = false;
329
330 m_material->setTexture(m_texture);
331}
332
333void QSGDistanceFieldGlyphNode::updateMaterial()
334{
335 delete m_material;
336
337 if (m_style == QQuickText::Normal) {
338 switch (m_antialiasingMode) {
339 case HighQualitySubPixelAntialiasing:
340 m_material = new QSGHiQSubPixelDistanceFieldTextMaterial;
341 break;
342 case LowQualitySubPixelAntialiasing:
343 m_material = new QSGLoQSubPixelDistanceFieldTextMaterial;
344 break;
345 case GrayAntialiasing:
346 default:
347 m_material = new QSGDistanceFieldTextMaterial;
348 break;
349 }
350 } else {
351 QSGDistanceFieldStyledTextMaterial *material;
352 if (m_style == QQuickText::Outline) {
353 material = new QSGDistanceFieldOutlineTextMaterial;
354 } else {
355 QSGDistanceFieldShiftedStyleTextMaterial *sMaterial = new QSGDistanceFieldShiftedStyleTextMaterial;
356 if (m_style == QQuickText::Raised)
357 sMaterial->setShift(QPointF(0.0, 1.0));
358 else
359 sMaterial->setShift(QPointF(0.0, -1.0));
360 material = sMaterial;
361 }
362 material->setStyleColor(m_styleColor);
363 m_material = material;
364 }
365
366 m_material->setGlyphCache(m_glyph_cache);
367 if (m_glyph_cache)
368 m_material->setFontScale(m_glyph_cache->fontScale(m_glyphs.rawFont().pixelSize()));
369 m_material->setColor(m_color);
370 setMaterial(m_material);
371 m_dirtyMaterial = false;
372}
373
374QT_END_NAMESPACE
375