1/****************************************************************************
2**
3** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D 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 "qtext2dentity.h"
41#include "qtext2dentity_p.h"
42#include "qtext2dmaterial_p.h"
43
44#include <QtGui/qtextlayout.h>
45#include <QtGui/qglyphrun.h>
46#include <QtGui/private/qdistancefield_p.h>
47#include <QtGui/private/qtextureglyphcache_p.h>
48#include <QtGui/private/qfont_p.h>
49#include <QtGui/private/qdistancefield_p.h>
50
51#include <Qt3DRender/qmaterial.h>
52#include <Qt3DRender/qbuffer.h>
53#include <Qt3DRender/qattribute.h>
54#include <Qt3DRender/qgeometry.h>
55#include <Qt3DRender/qgeometryrenderer.h>
56
57#include <Qt3DCore/private/qscene_p.h>
58
59QT_BEGIN_NAMESPACE
60
61namespace {
62
63inline Q_DECL_CONSTEXPR QRectF scaleRectF(const QRectF &rect, float scale)
64{
65 return QRectF(rect.left() * scale, rect.top() * scale, rect.width() * scale, rect.height() * scale);
66}
67
68} // anonymous
69
70namespace Qt3DExtras {
71
72/*!
73 * \qmltype Text2DEntity
74 * \instantiates Qt3DExtras::QText2DEntity
75 * \inqmlmodule Qt3D.Extras
76 * \brief Text2DEntity allows creation of a 2D text in 3D space.
77 *
78 * The Text2DEntity renders text as triangles in the XY plane. The geometry will be fitted
79 * in the rectangle of specified width and height. If the resulting geometry is wider than
80 * the specified width, the remainder will be rendered on the new line.
81 *
82 * The entity can be positionned in the scene by adding a transform component.
83 *
84 * Text2DEntity will create geometry based on the shape of the glyphs and a solid
85 * material using the specified color.
86 *
87 */
88
89/*!
90 * \qmlproperty QString Text2DEntity::text
91 *
92 * Holds the text used for the mesh.
93 */
94
95/*!
96 * \qmlproperty QFont Text2DEntity::font
97 *
98 * Holds the font of the text.
99 */
100
101/*!
102 * \qmlproperty QColor Text2DEntity::color
103 *
104 * Holds the color of the text.
105 */
106
107/*!
108 * \qmlproperty float Text2DEntity::width
109 *
110 * Holds the width of the text's bounding rectangle.
111 */
112
113/*!
114 * \qmlproperty float Text2DEntity::height
115 *
116 * Holds the height of the text's bounding rectangle.
117 */
118
119
120/*!
121 * \class Qt3DExtras::QText2DEntity
122 * \inheaderfile Qt3DExtras/QText2DEntity
123 * \inmodule Qt3DExtras
124 *
125 * \brief QText2DEntity allows creation of a 2D text in 3D space.
126 *
127 * The QText2DEntity renders text as triangles in the XY plane. The geometry will be fitted
128 * in the rectangle of specified width and height. If the resulting geometry is wider than
129 * the specified width, the remainder will be rendered on the new line.
130 *
131 * The entity can be positionned in the scene by adding a transform component.
132 *
133 * QText2DEntity will create geometry based on the shape of the glyphs and a solid
134 * material using the specified color.
135 *
136 */
137
138QHash<Qt3DCore::QScene *, QText2DEntityPrivate::CacheEntry> QText2DEntityPrivate::m_glyphCacheInstances;
139
140QText2DEntityPrivate::QText2DEntityPrivate()
141 : m_glyphCache(nullptr)
142 , m_font(QLatin1String("Times"), 10)
143 , m_scaledFont(QLatin1String("Times"), 10)
144 , m_color(QColor(255, 255, 255, 255))
145 , m_width(0.0f)
146 , m_height(0.0f)
147{
148}
149
150QText2DEntityPrivate::~QText2DEntityPrivate()
151{
152}
153
154void QText2DEntityPrivate::setScene(Qt3DCore::QScene *scene)
155{
156 if (scene == m_scene)
157 return;
158
159 // Unref old glyph cache if it exists
160 if (m_scene != nullptr) {
161 // Ensure we don't keep reference to glyphs
162 // if we are changing the cache
163 if (m_glyphCache != nullptr)
164 clearCurrentGlyphRuns();
165
166 m_glyphCache = nullptr;
167
168 QText2DEntityPrivate::CacheEntry &entry = QText2DEntityPrivate::m_glyphCacheInstances[m_scene];
169 --entry.count;
170 if (entry.count == 0 && entry.glyphCache != nullptr) {
171
172 delete entry.glyphCache;
173 entry.glyphCache = nullptr;
174 }
175 }
176
177 QEntityPrivate::setScene(scene);
178
179 // Ref new glyph cache is scene is valid
180 if (scene != nullptr) {
181 QText2DEntityPrivate::CacheEntry &entry = QText2DEntityPrivate::m_glyphCacheInstances[scene];
182 if (entry.glyphCache == nullptr) {
183 entry.glyphCache = new QDistanceFieldGlyphCache();
184 entry.glyphCache->setRootNode(scene->rootNode());
185 }
186 m_glyphCache = entry.glyphCache;
187 ++entry.count;
188 // Update to populate glyphCache if needed
189 update();
190 }
191}
192
193QText2DEntity::QText2DEntity(QNode *parent)
194 : Qt3DCore::QEntity(*new QText2DEntityPrivate(), parent)
195{
196}
197
198/*! \internal */
199QText2DEntity::~QText2DEntity()
200{
201}
202
203float QText2DEntityPrivate::computeActualScale() const
204{
205 // scale font based on fontScale property and given QFont
206 float scale = 1.0f;
207 if (m_font.pointSizeF() > 0)
208 scale *= m_font.pointSizeF() / m_scaledFont.pointSizeF();
209 return scale;
210}
211
212struct RenderData {
213 int vertexCount = 0;
214 QVector<float> vertex;
215 QVector<quint16> index;
216};
217
218void QText2DEntityPrivate::setCurrentGlyphRuns(const QVector<QGlyphRun> &runs)
219{
220 if (runs.isEmpty())
221 return;
222
223 // For each distinct texture, we need a separate DistanceFieldTextRenderer,
224 // for which we need vertex and index data
225 QHash<Qt3DRender::QAbstractTexture*, RenderData> renderData;
226 const float scale = computeActualScale();
227
228 // process glyph runs
229 for (const QGlyphRun &run : runs) {
230 const QVector<quint32> glyphs = run.glyphIndexes();
231 const QVector<QPointF> pos = run.positions();
232
233 Q_ASSERT(glyphs.size() == pos.size());
234
235 const bool doubleGlyphResolution = m_glyphCache->doubleGlyphResolution(run.rawFont());
236
237 // faithfully copied from QSGDistanceFieldGlyphNode::updateGeometry()
238 const float pixelSize = run.rawFont().pixelSize();
239 const float fontScale = pixelSize / QT_DISTANCEFIELD_BASEFONTSIZE(doubleGlyphResolution);
240 const float margin = QT_DISTANCEFIELD_RADIUS(doubleGlyphResolution) / QT_DISTANCEFIELD_SCALE(doubleGlyphResolution) * fontScale;
241
242 for (int i = 0; i < glyphs.size(); i++) {
243 const QDistanceFieldGlyphCache::Glyph &dfield = m_glyphCache->refGlyph(run.rawFont(), glyphs[i]);
244
245 if (!dfield.texture)
246 continue;
247
248 RenderData &data = renderData[dfield.texture];
249
250 // faithfully copied from QSGDistanceFieldGlyphNode::updateGeometry()
251 QRectF metrics = scaleRectF(dfield.glyphPathBoundingRect, fontScale);
252 metrics.adjust(-margin, margin, margin, 3*margin);
253
254 const float top = 0.0f;
255 const float left = 0.0f;
256 const float right = m_width;
257 const float bottom = m_height;
258
259 float x1 = left + scale * (pos[i].x() + metrics.left());
260 float y2 = bottom - scale * (pos[i].y() - metrics.top());
261 float x2 = x1 + scale * metrics.width();
262 float y1 = y2 - scale * metrics.height();
263
264 // only draw glyphs that are at least partly visible
265 if (y2 < top || x1 > right)
266 continue;
267
268 QRectF texCoords = dfield.texCoords;
269
270 // if a glyph is only partly visible within the given rectangle,
271 // cut it in half and adjust tex coords
272 if (y1 < top) {
273 const float insideRatio = (top - y2) / (y1 - y2);
274 y1 = top;
275 texCoords.setHeight(texCoords.height() * insideRatio);
276 }
277
278 // do the same thing horizontally
279 if (x2 > right) {
280 const float insideRatio = (right - x1) / (x2 - x1);
281 x2 = right;
282 texCoords.setWidth(texCoords.width() * insideRatio);
283 }
284
285 data.vertex << x1 << y1 << i << texCoords.left() << texCoords.bottom();
286 data.vertex << x1 << y2 << i << texCoords.left() << texCoords.top();
287 data.vertex << x2 << y1 << i << texCoords.right() << texCoords.bottom();
288 data.vertex << x2 << y2 << i << texCoords.right() << texCoords.top();
289
290 data.index << data.vertexCount << data.vertexCount+3 << data.vertexCount+1;
291 data.index << data.vertexCount << data.vertexCount+2 << data.vertexCount+3;
292
293 data.vertexCount += 4;
294 }
295 }
296
297 // make sure we have the correct number of DistanceFieldTextRenderers
298 // TODO: we might keep one renderer at all times, so we won't delete and
299 // re-allocate one every time the text changes from an empty to a non-empty string
300 // and vice-versa
301 while (m_renderers.size() > renderData.size())
302 delete m_renderers.takeLast();
303
304 while (m_renderers.size() < renderData.size()) {
305 DistanceFieldTextRenderer *renderer = new DistanceFieldTextRenderer(q_func());
306 renderer->setColor(m_color);
307 m_renderers << renderer;
308 }
309
310 Q_ASSERT(m_renderers.size() == renderData.size());
311
312 // assign vertex data for all textures to the renderers
313 int rendererIdx = 0;
314 for (auto it = renderData.begin(); it != renderData.end(); ++it) {
315 m_renderers[rendererIdx++]->setGlyphData(it.key(), it.value().vertex, it.value().index);
316 }
317
318 // de-ref all glyphs for previous QGlyphRuns
319 for (int i = 0; i < m_currentGlyphRuns.size(); i++)
320 m_glyphCache->derefGlyphs(m_currentGlyphRuns[i]);
321 m_currentGlyphRuns = runs;
322}
323
324void QText2DEntityPrivate::clearCurrentGlyphRuns()
325{
326 for (int i = 0; i < m_currentGlyphRuns.size(); i++)
327 m_glyphCache->derefGlyphs(m_currentGlyphRuns[i]);
328 m_currentGlyphRuns.clear();
329}
330
331void QText2DEntityPrivate::update()
332{
333 if (m_glyphCache == nullptr)
334 return;
335
336 QVector<QGlyphRun> glyphRuns;
337
338 // collect all GlyphRuns generated by the QTextLayout
339 if ((m_width > 0.0f || m_height > 0.0f) && !m_text.isEmpty()) {
340 QTextLayout layout(m_text, m_scaledFont);
341 const float lineWidth = m_width / computeActualScale();
342 float height = 0;
343 layout.beginLayout();
344
345 while (true) {
346 QTextLine line = layout.createLine();
347 if (!line.isValid())
348 break;
349
350 // position current line
351 line.setLineWidth(lineWidth);
352 line.setPosition(QPointF(0, height));
353 height += line.height();
354
355 // add glyph runs created by line
356 const QList<QGlyphRun> runs = line.glyphRuns();
357 for (const QGlyphRun &run : runs)
358 glyphRuns << run;
359 }
360
361 layout.endLayout();
362 }
363
364 setCurrentGlyphRuns(glyphRuns);
365}
366
367/*!
368 \property QText2DEntity::font
369
370 Holds the font for the text item that is displayed
371 in the Qt Quick scene.
372*/
373QFont QText2DEntity::font() const
374{
375 Q_D(const QText2DEntity);
376 return d->m_font;
377}
378
379void QText2DEntity::setFont(const QFont &font)
380{
381 Q_D(QText2DEntity);
382 if (d->m_font != font) {
383 // ignore the point size of the font, just make it a default value.
384 // still we want to make sure that font() returns the same value
385 // that was passed to setFont(), so we store it nevertheless
386 d->m_font = font;
387 d->m_scaledFont = font;
388 d->m_scaledFont.setPointSize(10);
389
390 emit fontChanged(font);
391
392 if (!d->m_text.isEmpty())
393 d->update();
394 }
395}
396
397/*!
398 \property QText2DEntity::color
399
400 Holds the color for the text item that is displayed in the Qt
401 Quick scene.
402*/
403QColor QText2DEntity::color() const
404{
405 Q_D(const QText2DEntity);
406 return d->m_color;
407}
408
409void QText2DEntity::setColor(const QColor &color)
410{
411 Q_D(QText2DEntity);
412 if (d->m_color != color) {
413 d->m_color = color;
414
415 emit colorChanged(color);
416
417 for (DistanceFieldTextRenderer *renderer : qAsConst(d->m_renderers))
418 renderer->setColor(color);
419 }
420}
421
422/*!
423 \property QText2DEntity::text
424
425 Holds the text that is displayed in the Qt Quick scene.
426*/
427QString QText2DEntity::text() const
428{
429 Q_D(const QText2DEntity);
430 return d->m_text;
431}
432
433void QText2DEntity::setText(const QString &text)
434{
435 Q_D(QText2DEntity);
436 if (d->m_text != text) {
437 d->m_text = text;
438 emit textChanged(text);
439
440 d->update();
441 }
442}
443
444/*!
445 \property QText2DEntity::width
446
447 Returns the width of the text item that is displayed in the
448 Qt Quick scene.
449*/
450float QText2DEntity::width() const
451{
452 Q_D(const QText2DEntity);
453 return d->m_width;
454}
455
456/*!
457 \property QText2DEntity::height
458
459 Returns the height of the text item that is displayed in the
460 Qt Quick scene.
461*/
462float QText2DEntity::height() const
463{
464 Q_D(const QText2DEntity);
465 return d->m_height;
466}
467
468void QText2DEntity::setWidth(float width)
469{
470 Q_D(QText2DEntity);
471 if (width != d->m_width) {
472 d->m_width = width;
473 emit widthChanged(width);
474 d->update();
475 }
476}
477
478void QText2DEntity::setHeight(float height)
479{
480 Q_D(QText2DEntity);
481 if (height != d->m_height) {
482 d->m_height = height;
483 emit heightChanged(height);
484 d->update();
485 }
486}
487
488} // namespace Qt3DExtras
489
490QT_END_NAMESPACE
491