OSDN Git Service

Revert "always allocate QGlyphLayout data on the heap"
authorIvailo Monev <xakepa10@gmail.com>
Wed, 16 Nov 2022 19:27:57 +0000 (21:27 +0200)
committerIvailo Monev <xakepa10@gmail.com>
Wed, 16 Nov 2022 19:27:57 +0000 (21:27 +0200)
will have to wait until the QTextEngine redo

This reverts commit 4a5239046adeb78397c868074fe48d595fcb8fa4.

src/gui/painting/qpainterpath.cpp
src/gui/text/qfontengine.cpp
src/gui/text/qfontengine_ft.cpp
src/gui/text/qfontmetrics.cpp
src/gui/text/qfontsubset.cpp
src/gui/text/qharfbuzz.cpp
src/gui/text/qtextengine.cpp
src/gui/text/qtextengine_p.h

index 6f6e25d..83e9f9b 100644 (file)
@@ -1092,7 +1092,7 @@ void QPainterPath::addText(const QPointF &point, const QFont &f, const QString &
 
     static const QTextEngine::ShaperFlags shaperflags = 0;
     int nglyphs = text.size();
-    QGlyphLayoutArray glyphs(nglyphs);
+    QVarLengthGlyphLayoutArray glyphs(nglyphs);
     engine->stringToCMap(text.unicode(), nglyphs, &glyphs, &nglyphs, shaperflags);
     engine->addOutlineToPath(point.x(), point.y(), glyphs, this);
 
index 615d767..6143131 100644 (file)
@@ -67,7 +67,7 @@ QFixed QFontEngine::underlinePosition() const
 
 QFixed QFontEngine::xHeight() const
 {
-    QGlyphLayoutArray glyphs(2);
+    QGlyphLayoutArray<2> glyphs;
     int nglyphs = 1;
     QChar x((ushort)'x');
     stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
@@ -78,7 +78,7 @@ QFixed QFontEngine::xHeight() const
 
 QFixed QFontEngine::averageCharWidth() const
 {
-    QGlyphLayoutArray glyphs(2);
+    QGlyphLayoutArray<2> glyphs;
     int nglyphs = 1;
     QChar x((ushort)'x');
     stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
@@ -402,7 +402,7 @@ QFontEngine::Type QFontEngineBox::type() const
 void QFontEngineBox::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nGlyphs,
                                   QPainterPath *path)
 {
-    QGlyphLayoutArray g(nGlyphs);
+    QVarLengthGlyphLayoutArray g(nGlyphs);
 
     for (int i = 0; i < nGlyphs; ++i) {
         g.glyphs[i] = glyphs[i];
index 3e8c47e..a6139eb 100644 (file)
@@ -467,7 +467,7 @@ qreal QFontEngineFT::minRightBearing() const
     if (rbearing == SHRT_MIN) {
         lbearing = rbearing = 0;
         const QChar *ch = reinterpret_cast<const QChar *>(char_table);
-        QGlyphLayoutArray glyphs(char_table_entries);
+        QGlyphLayoutArray<char_table_entries> glyphs;
         int ng = char_table_entries;
         stringToCMap(ch, char_table_entries, &glyphs, &ng, QTextEngine::GlyphIndicesOnly);
         while (--ng) {
index 5800eb8..66574ca 100644 (file)
@@ -445,7 +445,7 @@ int QFontMetrics::width(QChar ch) const
     QFontEngine *engine = d->engineForScript(script);
     Q_ASSERT(engine != 0);
 
-    QGlyphLayoutArray glyphs(2);
+    QGlyphLayoutArray<2> glyphs;
     int nglyphs = 1;
     engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
     return qRound(glyphs.advances_x[0]);
@@ -1035,7 +1035,7 @@ qreal QFontMetricsF::width(QChar ch) const
     QFontEngine *engine = d->engineForScript(script);
     Q_ASSERT(engine != 0);
 
-    QGlyphLayoutArray glyphs(2);
+    QGlyphLayoutArray<2> glyphs;
     int nglyphs = 1;
     engine->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
     return glyphs.advances_x[0].toReal();
index b101f39..144e7c5 100644 (file)
@@ -328,7 +328,7 @@ QVector<int> QFontSubset::getReverseMap() const
     reverseMap.resize(0x10000);
     for (uint i = 0; i < 0x10000; ++i)
         reverseMap[i] = 0;
-    QGlyphLayoutArray glyphs(2);
+    QGlyphLayoutArray<2> glyphs;
     for (uint uc = 0; uc < 0x10000; ++uc) {
         QChar ch(uc);
         int nglyphs = 1;
index b6d3d7f..ef9f5ba 100644 (file)
@@ -65,7 +65,7 @@ static void qHB_GetUnicodeCharProperties(HB_UChar32 ch, HB_CharCategory *categor
 
 static void qHB_GetGlyphAdvances(QFontEngine* fe, const HB_Glyph *glyphs, uint32_t numGlyphs, HB_Fixed *advances, int flags)
 {
-    QGlyphLayoutArray qglyphs(numGlyphs);
+    QVarLengthGlyphLayoutArray qglyphs(numGlyphs);
 
     for (uint32_t i = 0; i < numGlyphs; ++i)
         qglyphs.glyphs[i] = glyphs[i];
index 141047f..7a4a3b1 100644 (file)
@@ -129,7 +129,7 @@ void QTextEngine::shapeTextWithHarfbuzz(int item) const
     bool kerningEnabled = this->font(si).d->kerning;
 
     HB_ShaperItem shaper_item;
-    ::memset(&shaper_item, 0, sizeof(shaper_item));
+    memset(&shaper_item, 0, sizeof(shaper_item));
     shaper_item.string = reinterpret_cast<const HB_UChar16 *>(layoutData->string.constData());
     shaper_item.stringLength = layoutData->string.length();
     shaper_item.item.pos = si.position;
@@ -755,7 +755,7 @@ bool QTextEngine::LayoutData::reallocate(int totalGlyphs)
 
     const int space_preGlyphLayout = space_charAttributes + space_logClusters;
     if (allocated < space_preGlyphLayout)
-        ::memset(memory + allocated, 0, (space_preGlyphLayout - allocated) * QT_POINTER_SIZE);
+        memset(memory + allocated, 0, (space_preGlyphLayout - allocated) * QT_POINTER_SIZE);
 
     glyphLayout.grow(reinterpret_cast<char *>(m), totalGlyphs);
 
@@ -771,10 +771,10 @@ void QGlyphLayout::grow(char *address, int totalGlyphs)
 
     if (numGlyphs) {
         // move the existing data
-        ::memmove(newLayout.attributes, oldLayout.attributes, numGlyphs * sizeof(HB_GlyphAttributes));
-        ::memmove(newLayout.justifications, oldLayout.justifications, numGlyphs * sizeof(QGlyphJustification));
-        ::memmove(newLayout.advances_x, oldLayout.advances_x, numGlyphs * sizeof(QFixed));
-        ::memmove(newLayout.glyphs, oldLayout.glyphs, numGlyphs * sizeof(HB_Glyph));
+        memmove(newLayout.attributes, oldLayout.attributes, numGlyphs * sizeof(HB_GlyphAttributes));
+        memmove(newLayout.justifications, oldLayout.justifications, numGlyphs * sizeof(QGlyphJustification));
+        memmove(newLayout.advances_x, oldLayout.advances_x, numGlyphs * sizeof(QFixed));
+        memmove(newLayout.glyphs, oldLayout.glyphs, numGlyphs * sizeof(HB_Glyph));
     }
 
     // clear the new data
@@ -975,7 +975,7 @@ QString QTextEngine::elidedText(Qt::TextElideMode mode, const QFixed &width, int
 
         QFontEngine *fe = fnt.d->engineForScript(QUnicodeTables::Common);
 
-        QGlyphLayoutArray ellipsisGlyph(2);
+        QGlyphLayoutArray<2> ellipsisGlyph;
         {
             if (fe->canRender(&ellipsisChar, 1)) {
                 int nGlyphs = 1;
@@ -989,7 +989,7 @@ QString QTextEngine::elidedText(Qt::TextElideMode mode, const QFixed &width, int
         } else {
             QString dotDotDot(QLatin1String("..."));
 
-            QGlyphLayoutArray glyphs(3);
+            QGlyphLayoutArray<3> glyphs;
             int nGlyphs = 3;
             if (!fe->stringToCMap(dotDotDot.constData(), 3, &glyphs, &nGlyphs, 0))
                 // should never happen...
index 0bddb53..707707f 100644 (file)
@@ -117,6 +117,7 @@ struct QGlyphJustification
 
 struct QGlyphLayout
 {
+    // init to 0 not needed, done when shaping
     HB_Glyph *glyphs; // 4 bytes per element
     QFixed *advances_x; // 4 bytes per element
     QGlyphJustification *justifications; // 4 bytes per element
@@ -124,11 +125,18 @@ struct QGlyphLayout
 
     int numGlyphs;
 
-    inline QGlyphLayout() : glyphs(nullptr), advances_x(nullptr), justifications(nullptr), attributes(nullptr), numGlyphs(0) {}
+    inline QGlyphLayout() : numGlyphs(0) {}
 
     inline explicit QGlyphLayout(char *address, int totalGlyphs)
     {
-        init(address, totalGlyphs);
+        int offset = totalGlyphs * sizeof(HB_Glyph);
+        glyphs = reinterpret_cast<HB_Glyph *>(address);
+        advances_x = reinterpret_cast<QFixed *>(address + offset);
+        offset += totalGlyphs * sizeof(QFixed);
+        justifications = reinterpret_cast<QGlyphJustification *>(address + offset);
+        offset += totalGlyphs * sizeof(QGlyphJustification);
+        attributes = reinterpret_cast<HB_GlyphAttributes *>(address + offset);
+        numGlyphs = totalGlyphs;
     }
 
     inline QGlyphLayout mid(int position, int n = -1) const {
@@ -151,13 +159,13 @@ struct QGlyphLayout
         if (last == -1)
             last = numGlyphs;
         if (first == 0 && last == numGlyphs) {
-            ::memset(glyphs, 0, QSPACEFORGLYPHS(numGlyphs));
+            memset(glyphs, 0, QSPACEFORGLYPHS(numGlyphs));
         } else {
             const int num = last - first;
-            ::memset(glyphs + first, 0, num * sizeof(HB_Glyph));
-            ::memset(advances_x + first, 0, num * sizeof(QFixed));
-            ::memset(justifications + first, 0, num * sizeof(QGlyphJustification));
-            ::memset(attributes + first, 0, num * sizeof(HB_GlyphAttributes));
+            memset(glyphs + first, 0, num * sizeof(HB_Glyph));
+            memset(advances_x + first, 0, num * sizeof(QFixed));
+            memset(justifications + first, 0, num * sizeof(QGlyphJustification));
+            memset(attributes + first, 0, num * sizeof(HB_GlyphAttributes));
         }
     }
 
@@ -166,43 +174,32 @@ struct QGlyphLayout
     }
 
     void grow(char *address, int totalGlyphs);
-protected:
-    inline void init(char *address, int totalGlyphs)
-    {
-        int offset = totalGlyphs * sizeof(HB_Glyph);
-        glyphs = reinterpret_cast<HB_Glyph *>(address);
-        advances_x = reinterpret_cast<QFixed *>(address + offset);
-        offset += totalGlyphs * sizeof(QFixed);
-        justifications = reinterpret_cast<QGlyphJustification *>(address + offset);
-        offset += totalGlyphs * sizeof(QGlyphJustification);
-        attributes = reinterpret_cast<HB_GlyphAttributes *>(address + offset);
-        numGlyphs = totalGlyphs;
-    }
 };
 
-
-struct QGlyphLayoutArray : public QGlyphLayout
+class QVarLengthGlyphLayoutArray : private QVarLengthArray<void *>, public QGlyphLayout
 {
+private:
+    typedef QVarLengthArray<void *> Array;
 public:
-    QGlyphLayoutArray(const int size)
-        : QGlyphLayout(),
-        buffer(nullptr)
+    QVarLengthGlyphLayoutArray(int totalGlyphs)
+        : Array(QSPACEFORGLYPHS(totalGlyphs) / QT_POINTER_SIZE + 1)
+        , QGlyphLayout(reinterpret_cast<char *>(Array::data()), totalGlyphs)
     {
-        Q_ASSERT(size);
-        buffer = ::malloc(QSPACEFORGLYPHS(size));
-        Q_CHECK_PTR(buffer);
-        ::memset(buffer, 0, sizeof(buffer));
-        init(reinterpret_cast<char *>(buffer), size);
+        memset(Array::data(), 0, Array::size() * QT_POINTER_SIZE);
     }
+};
 
-    ~QGlyphLayoutArray()
+template <int N> struct QGlyphLayoutArray : public QGlyphLayout
+{
+public:
+    QGlyphLayoutArray()
+        : QGlyphLayout(reinterpret_cast<char *>(buffer), N)
     {
-        ::free(buffer);
+        memset(buffer, 0, sizeof(buffer));
     }
 
 private:
-    Q_DISABLE_COPY(QGlyphLayoutArray);
-    void *buffer;
+    void *buffer[QSPACEFORGLYPHS(N) / QT_POINTER_SIZE + 1];
 };
 
 struct QScriptItem;