diff options
Diffstat (limited to 'src/extras/text')
-rw-r--r-- | src/extras/text/areaallocator.cpp | 13 | ||||
-rw-r--r-- | src/extras/text/qdistancefieldglyphcache.cpp | 26 | ||||
-rw-r--r-- | src/extras/text/qtext2dentity.cpp | 48 | ||||
-rw-r--r-- | src/extras/text/qtext2dentity.h | 4 | ||||
-rw-r--r-- | src/extras/text/qtext2dentity_p.h | 1 | ||||
-rw-r--r-- | src/extras/text/qtext2dmaterial.cpp | 2 | ||||
-rw-r--r-- | src/extras/text/qtextureatlas.cpp | 3 | ||||
-rw-r--r-- | src/extras/text/text.pri | 21 |
8 files changed, 72 insertions, 46 deletions
diff --git a/src/extras/text/areaallocator.cpp b/src/extras/text/areaallocator.cpp index 4a7404d97..b2aedfd88 100644 --- a/src/extras/text/areaallocator.cpp +++ b/src/extras/text/areaallocator.cpp @@ -15,16 +15,13 @@ QT_BEGIN_NAMESPACE namespace Qt3DExtras { -namespace +enum SplitType { - enum SplitType - { - VerticalSplit, - HorizontalSplit - }; + VerticalSplit, + HorizontalSplit +}; - static const int maxMargin = 2; -} +static const int maxMargin = 2; struct AreaAllocatorNode { diff --git a/src/extras/text/qdistancefieldglyphcache.cpp b/src/extras/text/qdistancefieldglyphcache.cpp index dd6640354..ae55a07c7 100644 --- a/src/extras/text/qdistancefieldglyphcache.cpp +++ b/src/extras/text/qdistancefieldglyphcache.cpp @@ -19,10 +19,11 @@ QT_BEGIN_NAMESPACE #define DEFAULT_IMAGE_PADDING 1 -using namespace Qt3DCore; namespace Qt3DExtras { +using namespace Qt3DCore; + // ref-count glyphs and keep track of where they are stored class StoredGlyph { public: @@ -125,6 +126,7 @@ DistanceFieldFont::DistanceFieldFont(const QRawFont &font, bool doubleRes, Qt3DC , m_doubleGlyphResolution(doubleRes) , m_parentNode(parent) { + Q_ASSERT(m_parentNode); } DistanceFieldFont::~DistanceFieldFont() @@ -161,13 +163,14 @@ StoredGlyph DistanceFieldFont::refGlyph(quint32 glyph) // scenarios const int size = m_doubleGlyphResolution ? 512 : 256; - QTextureAtlas *atlas = new QTextureAtlas(m_parentNode); + QTextureAtlas *atlas = new QTextureAtlas(); atlas->setWidth(size); atlas->setHeight(size); atlas->setFormat(Qt3DRender::QAbstractTexture::R8_UNorm); atlas->setPixelFormat(QOpenGLTexture::Red); atlas->setMinificationFilter(Qt3DRender::QAbstractTexture::Linear); atlas->setMagnificationFilter(Qt3DRender::QAbstractTexture::Linear); + atlas->setParent(m_parentNode); m_atlasses << atlas; if (!storedGlyph.addToTextureAtlas(atlas)) @@ -200,7 +203,12 @@ void DistanceFieldFont::derefGlyph(quint32 glyph) Q_ASSERT(m_atlasses.contains(atlas)); m_atlasses.removeAll(atlas); - delete atlas; + + // This function might have been called as a result of destroying + // the scene root which traverses the entire scene tree. Calling + // delete on the atlas here could lead to dangling pointers in the + // least of children being traversed for destruction. + atlas->deleteLater(); } m_glyphs.erase(it); @@ -251,7 +259,8 @@ DistanceFieldFont* QDistanceFieldGlyphCache::getOrCreateDistanceFieldFont(const // create new font cache // we set the parent node to nullptr, since the parent node of QTextureAtlasses // will be set when we pass them to QText2DMaterial later - DistanceFieldFont *dff = new DistanceFieldFont(actualFont, useDoubleRes, nullptr); + Q_ASSERT(m_rootNode); + DistanceFieldFont *dff = new DistanceFieldFont(actualFont, useDoubleRes, m_rootNode); m_fonts.insert(key, dff); return dff; } @@ -288,11 +297,10 @@ QDistanceFieldGlyphCache::Glyph refAndGetGlyph(DistanceFieldFont *dff, quint32 g if (dff) { const auto entry = dff->refGlyph(glyph); - if (entry.atlas()) { - ret.glyphPathBoundingRect = entry.glyphPathBoundingRect(); - ret.texCoords = entry.texCoords(); - ret.texture = entry.atlas(); - } + Q_ASSERT(entry.atlas()); + ret.glyphPathBoundingRect = entry.glyphPathBoundingRect(); + ret.texCoords = entry.texCoords(); + ret.texture = entry.atlas(); } return ret; diff --git a/src/extras/text/qtext2dentity.cpp b/src/extras/text/qtext2dentity.cpp index b0c53cb24..8b096205f 100644 --- a/src/extras/text/qtext2dentity.cpp +++ b/src/extras/text/qtext2dentity.cpp @@ -80,6 +80,12 @@ namespace Qt3DExtras { * Holds the height of the text's bounding rectangle. */ + /*! + * \qmlproperty Qt::Alignment Text2DEntity::alignment + * + * Holds the alignment of the text. + */ + /*! * \class Qt3DExtras::QText2DEntity @@ -108,6 +114,7 @@ QText2DEntityPrivate::QText2DEntityPrivate() , m_color(QColor(255, 255, 255, 255)) , m_width(0.0f) , m_height(0.0f) + , m_alignment(Qt::AlignTop | Qt::AlignLeft) { } @@ -274,8 +281,9 @@ void QText2DEntityPrivate::setCurrentGlyphRuns(const QList<QGlyphRun> &runs) delete m_renderers.takeLast(); while (m_renderers.size() < renderData.size()) { - DistanceFieldTextRenderer *renderer = new DistanceFieldTextRenderer(q_func()); + DistanceFieldTextRenderer *renderer = new DistanceFieldTextRenderer(); renderer->setColor(m_color); + renderer->setParent(q_func()); m_renderers << renderer; } @@ -304,6 +312,11 @@ void QText2DEntityPrivate::updateGlyphs() // collect all GlyphRuns generated by the QTextLayout if ((m_width > 0.0f || m_height > 0.0f) && !m_text.isEmpty()) { QTextLayout layout(m_text, m_scaledFont); + + QTextOption textOption = layout.textOption(); + textOption.setAlignment(m_alignment); + layout.setTextOption(textOption); + const float lineWidth = m_width / computeActualScale(); float height = 0; layout.beginLayout(); @@ -331,7 +344,7 @@ void QText2DEntityPrivate::updateGlyphs() } /*! - \property QText2DEntity::font + \property Qt3DExtras::QText2DEntity::font Holds the font for the text item that is displayed in the Qt Quick scene. @@ -361,7 +374,7 @@ void QText2DEntity::setFont(const QFont &font) } /*! - \property QText2DEntity::color + \property Qt3DExtras::QText2DEntity::color Holds the color for the text item that is displayed in the Qt Quick scene. @@ -386,7 +399,7 @@ void QText2DEntity::setColor(const QColor &color) } /*! - \property QText2DEntity::text + \property Qt3DExtras::QText2DEntity::text Holds the text that is displayed in the Qt Quick scene. */ @@ -408,7 +421,7 @@ void QText2DEntity::setText(const QString &text) } /*! - \property QText2DEntity::width + \property Qt3DExtras::QText2DEntity::width Returns the width of the text item that is displayed in the Qt Quick scene. @@ -420,7 +433,7 @@ float QText2DEntity::width() const } /*! - \property QText2DEntity::height + \property Qt3DExtras::QText2DEntity::height Returns the height of the text item that is displayed in the Qt Quick scene. @@ -451,6 +464,29 @@ void QText2DEntity::setHeight(float height) } } +/*! + \property Qt3DExtras::QText2DEntity::alignment + + Returns the alignment of the text item that is displayed in the + Qt Quick scene. + + The default alignment is top-left-aligned. +*/ +Qt::Alignment QText2DEntity::alignment() const +{ + Q_D(const QText2DEntity); + return d->m_alignment; +} + +void QText2DEntity::setAlignment(Qt::Alignment alignment) +{ + Q_D(QText2DEntity); + if (alignment != d->m_alignment) { + d->m_alignment = alignment; + d->updateGlyphs(); + } +} + } // namespace Qt3DExtras QT_END_NAMESPACE diff --git a/src/extras/text/qtext2dentity.h b/src/extras/text/qtext2dentity.h index 8fa00ae4b..7fc74dc77 100644 --- a/src/extras/text/qtext2dentity.h +++ b/src/extras/text/qtext2dentity.h @@ -24,6 +24,7 @@ class Q_3DEXTRASSHARED_EXPORT QText2DEntity : public Qt3DCore::QEntity Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(float width READ width WRITE setWidth NOTIFY widthChanged) Q_PROPERTY(float height READ height WRITE setHeight NOTIFY heightChanged) + Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment) public: explicit QText2DEntity(Qt3DCore::QNode *parent = nullptr); @@ -44,6 +45,9 @@ public: void setWidth(float width); void setHeight(float height); + Qt::Alignment alignment() const; + void setAlignment(Qt::Alignment alignment); + Q_SIGNALS: void fontChanged(const QFont &font); void colorChanged(const QColor &color); diff --git a/src/extras/text/qtext2dentity_p.h b/src/extras/text/qtext2dentity_p.h index b1de2dffa..25e8df3a3 100644 --- a/src/extras/text/qtext2dentity_p.h +++ b/src/extras/text/qtext2dentity_p.h @@ -62,6 +62,7 @@ public: QString m_text; float m_width; float m_height; + Qt::Alignment m_alignment; QList<DistanceFieldTextRenderer*> m_renderers; diff --git a/src/extras/text/qtext2dmaterial.cpp b/src/extras/text/qtext2dmaterial.cpp index bbb594667..57ef9227d 100644 --- a/src/extras/text/qtext2dmaterial.cpp +++ b/src/extras/text/qtext2dmaterial.cpp @@ -28,7 +28,7 @@ QText2DMaterialPrivate::QText2DMaterialPrivate() , m_effect(new Qt3DRender::QEffect()) , m_distanceFieldTexture(nullptr) , m_textureParameter(new Qt3DRender::QParameter(QStringLiteral("distanceFieldTexture"), QVariant(0))) - , m_textureSizeParameter(new Qt3DRender::QParameter(QStringLiteral("textureSize"), QVariant(256.f))) + , m_textureSizeParameter(new Qt3DRender::QParameter(QStringLiteral("textureWidth"), QVariant(256.f))) , m_colorParameter(new Qt3DRender::QParameter(QStringLiteral("color"), QVariant(QColor(255, 255, 255, 255)))) , m_gl3Technique(new Qt3DRender::QTechnique()) , m_gl2Technique(new Qt3DRender::QTechnique()) diff --git a/src/extras/text/qtextureatlas.cpp b/src/extras/text/qtextureatlas.cpp index 85ed57d97..951f0cf31 100644 --- a/src/extras/text/qtextureatlas.cpp +++ b/src/extras/text/qtextureatlas.cpp @@ -8,10 +8,11 @@ QT_BEGIN_NAMESPACE -using namespace Qt3DCore; namespace Qt3DExtras { +using namespace Qt3DCore; + QTextureAtlasData::QTextureAtlasData(int w, int h, QImage::Format fmt) : m_image(w, h, fmt) { diff --git a/src/extras/text/text.pri b/src/extras/text/text.pri deleted file mode 100644 index 1d2cb793c..000000000 --- a/src/extras/text/text.pri +++ /dev/null @@ -1,21 +0,0 @@ -HEADERS += \ - $$PWD/distancefieldtextrenderer_p.h \ - $$PWD/distancefieldtextrenderer_p_p.h \ - $$PWD/areaallocator_p.h \ - $$PWD/qdistancefieldglyphcache_p.h \ - $$PWD/qtextureatlas_p_p.h \ - $$PWD/qtextureatlas_p.h \ - $$PWD/qtext2dentity_p.h \ - $$PWD/qtext2dentity.h \ - $$PWD/qtext2dmaterial_p_p.h \ - $$PWD/qtext2dmaterial_p.h - -SOURCES += \ - $$PWD/qtextureatlas.cpp \ - $$PWD/qdistancefieldglyphcache.cpp \ - $$PWD/distancefieldtextrenderer.cpp \ - $$PWD/areaallocator.cpp \ - $$PWD/qtext2dentity.cpp \ - $$PWD/qtext2dmaterial.cpp - -INCLUDEPATH += $$PWD |