diff options
author | Mike Krus <mike.krus@kdab.com> | 2020-11-30 11:08:00 +0000 |
---|---|---|
committer | Mike Krus <mike.krus@kdab.com> | 2021-01-11 11:14:50 +0000 |
commit | e4a0e92896d65de2bc1ea6786453ae73ecee6f7f (patch) | |
tree | 3c6ad7d3ae77c5ba23121133fad8f1a229a27779 /src/extras | |
parent | 862f47c6b6c6be00cc4de03751f06593f2e2e9c2 (diff) |
Port QText2DEntity to use std::vector
Change-Id: I98febe2ae6500f2eaf44b489283f597c3c20c272
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Diffstat (limited to 'src/extras')
-rw-r--r-- | src/extras/text/distancefieldtextrenderer.cpp | 12 | ||||
-rw-r--r-- | src/extras/text/distancefieldtextrenderer_p.h | 4 | ||||
-rw-r--r-- | src/extras/text/qtext2dentity.cpp | 57 | ||||
-rw-r--r-- | src/extras/text/qtext2dentity_p.h | 2 |
4 files changed, 40 insertions, 35 deletions
diff --git a/src/extras/text/distancefieldtextrenderer.cpp b/src/extras/text/distancefieldtextrenderer.cpp index af4840a36..3cafd93df 100644 --- a/src/extras/text/distancefieldtextrenderer.cpp +++ b/src/extras/text/distancefieldtextrenderer.cpp @@ -129,18 +129,18 @@ DistanceFieldTextRenderer::~DistanceFieldTextRenderer() } void DistanceFieldTextRenderer::setGlyphData(Qt3DRender::QAbstractTexture *glyphTexture, - const QVector<float> &vertexData, - const QVector<quint16> &indexData) + const std::vector<float> &vertexData, + const std::vector<quint16> &indexData) { Q_D(DistanceFieldTextRenderer); - const int vertexCount = vertexData.size() / 5; + const size_t vertexCount = vertexData.size() / 5; d->m_vertexBuffer->setData(QByteArray((char*) vertexData.data(), vertexData.size() * sizeof(float))); d->m_indexBuffer->setData(QByteArray((char*) indexData.data(), indexData.size() * sizeof(quint16))); - d->m_positionAttr->setCount(vertexCount); - d->m_texCoordAttr->setCount(vertexCount); - d->m_indexAttr->setCount(indexData.size()); + d->m_positionAttr->setCount(uint(vertexCount)); + d->m_texCoordAttr->setCount(uint(vertexCount)); + d->m_indexAttr->setCount(uint(indexData.size())); d->m_material->setDistanceFieldTexture(glyphTexture); } diff --git a/src/extras/text/distancefieldtextrenderer_p.h b/src/extras/text/distancefieldtextrenderer_p.h index fdb9a836f..93a91d4e1 100644 --- a/src/extras/text/distancefieldtextrenderer_p.h +++ b/src/extras/text/distancefieldtextrenderer_p.h @@ -75,8 +75,8 @@ public: ~DistanceFieldTextRenderer(); void setGlyphData(Qt3DRender::QAbstractTexture *glyphTexture, - const QVector<float> &vertexData, - const QVector<quint16> &indexData); + const std::vector<float> &vertexData, + const std::vector<quint16> &indexData); void setColor(const QColor &color); diff --git a/src/extras/text/qtext2dentity.cpp b/src/extras/text/qtext2dentity.cpp index 28e0bb770..f9edbe090 100644 --- a/src/extras/text/qtext2dentity.cpp +++ b/src/extras/text/qtext2dentity.cpp @@ -60,7 +60,7 @@ QT_BEGIN_NAMESPACE namespace { -inline constexpr QRectF scaleRectF(const QRectF &rect, float scale) +inline constexpr QRectF scaleRectF(const QRectF &rect, qreal scale) { return QRectF(rect.left() * scale, rect.top() * scale, rect.width() * scale, rect.height() * scale); } @@ -200,7 +200,7 @@ QText2DEntity::~QText2DEntity() { } -float QText2DEntityPrivate::computeActualScale() const +qreal QText2DEntityPrivate::computeActualScale() const { // scale font based on fontScale property and given QFont float scale = 1.0f; @@ -211,8 +211,8 @@ float QText2DEntityPrivate::computeActualScale() const struct RenderData { int vertexCount = 0; - QVector<float> vertex; - QVector<quint16> index; + std::vector<float> vertex; + std::vector<quint16> index; }; void QText2DEntityPrivate::setCurrentGlyphRuns(const QList<QGlyphRun> &runs) @@ -220,7 +220,7 @@ void QText2DEntityPrivate::setCurrentGlyphRuns(const QList<QGlyphRun> &runs) // For each distinct texture, we need a separate DistanceFieldTextRenderer, // for which we need vertex and index data QHash<Qt3DRender::QAbstractTexture*, RenderData> renderData; - const float scale = computeActualScale(); + const qreal scale = computeActualScale(); // process glyph runs for (const QGlyphRun &run : runs) { @@ -232,9 +232,9 @@ void QText2DEntityPrivate::setCurrentGlyphRuns(const QList<QGlyphRun> &runs) const bool doubleGlyphResolution = m_glyphCache->doubleGlyphResolution(run.rawFont()); // faithfully copied from QSGDistanceFieldGlyphNode::updateGeometry() - const float pixelSize = run.rawFont().pixelSize(); - const float fontScale = pixelSize / QT_DISTANCEFIELD_BASEFONTSIZE(doubleGlyphResolution); - const float margin = QT_DISTANCEFIELD_RADIUS(doubleGlyphResolution) / QT_DISTANCEFIELD_SCALE(doubleGlyphResolution) * fontScale; + const qreal pixelSize = run.rawFont().pixelSize(); + const qreal fontScale = pixelSize / QT_DISTANCEFIELD_BASEFONTSIZE(doubleGlyphResolution); + const qreal margin = QT_DISTANCEFIELD_RADIUS(doubleGlyphResolution) / QT_DISTANCEFIELD_SCALE(doubleGlyphResolution) * fontScale; for (int i = 0; i < glyphs.size(); i++) { const QDistanceFieldGlyphCache::Glyph &dfield = m_glyphCache->refGlyph(run.rawFont(), glyphs[i]); @@ -248,15 +248,15 @@ void QText2DEntityPrivate::setCurrentGlyphRuns(const QList<QGlyphRun> &runs) QRectF metrics = scaleRectF(dfield.glyphPathBoundingRect, fontScale); metrics.adjust(-margin, margin, margin, 3*margin); - const float top = 0.0f; - const float left = 0.0f; - const float right = m_width; - const float bottom = m_height; + const qreal top = 0.0; + const qreal left = 0.0; + const qreal right = qreal(m_width); + const qreal bottom = qreal(m_height); - float x1 = left + scale * (pos[i].x() + metrics.left()); - float y2 = bottom - scale * (pos[i].y() - metrics.top()); - float x2 = x1 + scale * metrics.width(); - float y1 = y2 - scale * metrics.height(); + qreal x1 = left + scale * (pos[i].x() + metrics.left()); + qreal y2 = bottom - scale * (pos[i].y() - metrics.top()); + qreal x2 = x1 + scale * metrics.width(); + qreal y1 = y2 - scale * metrics.height(); // only draw glyphs that are at least partly visible if (y2 < top || x1 > right) @@ -267,25 +267,31 @@ void QText2DEntityPrivate::setCurrentGlyphRuns(const QList<QGlyphRun> &runs) // if a glyph is only partly visible within the given rectangle, // cut it in half and adjust tex coords if (y1 < top) { - const float insideRatio = (top - y2) / (y1 - y2); + const auto insideRatio = (top - y2) / (y1 - y2); y1 = top; texCoords.setHeight(texCoords.height() * insideRatio); } // do the same thing horizontally if (x2 > right) { - const float insideRatio = (right - x1) / (x2 - x1); + const auto insideRatio = (right - x1) / (x2 - x1); x2 = right; texCoords.setWidth(texCoords.width() * insideRatio); } - data.vertex << x1 << y1 << i << texCoords.left() << texCoords.bottom(); - data.vertex << x1 << y2 << i << texCoords.left() << texCoords.top(); - data.vertex << x2 << y1 << i << texCoords.right() << texCoords.bottom(); - data.vertex << x2 << y2 << i << texCoords.right() << texCoords.top(); + for (auto v: std::vector<qreal>{x1, y1, qreal(i), texCoords.left(), texCoords.bottom()}) + data.vertex.push_back(float(v)); + for (auto v: std::vector<qreal>{x1, y2, qreal(i), texCoords.left(), texCoords.top()}) + data.vertex.push_back(float(v)); + for (auto v: std::vector<qreal>{x2, y1, qreal(i), texCoords.right(), texCoords.bottom()}) + data.vertex.push_back(float(v)); + for (auto v: std::vector<qreal>{x2, y2, qreal(i), texCoords.right(), texCoords.top()}) + data.vertex.push_back(float(v)); - data.index << data.vertexCount << data.vertexCount+3 << data.vertexCount+1; - data.index << data.vertexCount << data.vertexCount+2 << data.vertexCount+3; + for (int i: std::vector<int>{data.vertexCount, data.vertexCount + 3, data.vertexCount + 1}) + data.index.push_back(quint16(i)); + for (int i: std::vector<int>{data.vertexCount, data.vertexCount + 2, data.vertexCount + 3}) + data.index.push_back(quint16(i)); data.vertexCount += 4; } @@ -313,9 +319,8 @@ void QText2DEntityPrivate::setCurrentGlyphRuns(const QList<QGlyphRun> &runs) // assign vertex data for all textures to the renderers int rendererIdx = 0; - for (auto it = renderData.begin(); it != renderData.end(); ++it) { + for (auto it = renderData.begin(); it != renderData.end(); ++it) m_renderers[rendererIdx++]->setGlyphData(it.key(), it.value().vertex, it.value().index); - } } void QText2DEntityPrivate::clearCurrentGlyphRuns() diff --git a/src/extras/text/qtext2dentity_p.h b/src/extras/text/qtext2dentity_p.h index 99a2e3024..742c3613d 100644 --- a/src/extras/text/qtext2dentity_p.h +++ b/src/extras/text/qtext2dentity_p.h @@ -101,7 +101,7 @@ public: QList<DistanceFieldTextRenderer*> m_renderers; - float computeActualScale() const; + qreal computeActualScale() const; void setCurrentGlyphRuns(const QList<QGlyphRun> &runs); void clearCurrentGlyphRuns(); |