Index: qtbase-everywhere-src-5.13.1/src/gui/text/qfontengine.cpp
===================================================================
--- qtbase-everywhere-src-5.13.1.orig/src/gui/text/qfontengine.cpp
+++ qtbase-everywhere-src-5.13.1/src/gui/text/qfontengine.cpp
@@ -1762,11 +1762,11 @@ QImage QFontEngineBox::alphaMapForGlyph(
 // ------------------------------------------------------------------
 
 uchar QFontEngineMulti::highByte(glyph_t glyph)
-{ return glyph >> 24; }
+{ return glyph >> 22; }
 
 // strip high byte from glyph
 static inline glyph_t stripped(glyph_t glyph)
-{ return glyph & 0x00ffffff; }
+{ return glyph & 0x003fffff; }
 
 QFontEngineMulti::QFontEngineMulti(QFontEngine *engine, int script, const QStringList &fallbackFamilies)
     : QFontEngine(Multi),
@@ -1825,6 +1825,18 @@ void QFontEngineMulti::setFallbackFamili
     } else {
         m_engines.resize(m_fallbackFamilies.size() + 1);
     }
+    qDebug() << "----- fallbackFamilies:";
+    for (int i = 0; i < m_fallbackFamilies.size(); ++i) {
+        qDebug() << i << ") " << m_fallbackFamilies.at(i);
+    }
+    qDebug() << "----- engines:";
+
+    for (int i = 0; i < m_engines.size(); ++i) {
+        QFontEngine *engine = m_engines.at(i);
+        if (engine)
+            qDebug() << i << ") " << engine->fontDef.family << " : " << engine->fontDef.families << " ; " << engine->fontDef.fallBackFamilies;
+    }
+    qDebug() << "------------------";
 
     m_fallbackFamiliesQueried = true;
 }
@@ -1834,7 +1846,9 @@ void QFontEngineMulti::ensureEngineAt(in
     if (!m_fallbackFamiliesQueried)
         ensureFallbackFamiliesQueried();
     Q_ASSERT(at < m_engines.size());
+    qDebug() << "ensureEngineAt" << at;
     if (!m_engines.at(at)) {
+        qDebug() << "   -- loadingEngineAt" << at;
         QFontEngine *engine = loadEngine(at);
         if (!engine)
             engine = new QFontEngineBox(fontDef.pixelSize);
@@ -1868,6 +1882,10 @@ QFontEngine *QFontEngineMulti::loadEngin
 
 glyph_t QFontEngineMulti::glyphIndex(uint ucs4) const
 {
+    bool ok;
+    int maxEngines = qEnvironmentVariableIntValue("QT_MAX_ENGINES", &ok);
+    if (!ok)
+        maxEngines = 1024;
     glyph_t glyph = engine(0)->glyphIndex(ucs4);
     if (glyph == 0
             && ucs4 != QChar::LineSeparator
@@ -1876,7 +1894,7 @@ glyph_t QFontEngineMulti::glyphIndex(uin
             && ucs4 != QChar::ParagraphSeparator) {
         if (!m_fallbackFamiliesQueried)
             const_cast<QFontEngineMulti *>(this)->ensureFallbackFamiliesQueried();
-        for (int x = 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
+        for (int x = 1, n = qMin(m_engines.size(), maxEngines); x < n; ++x) {
             QFontEngine *engine = m_engines.at(x);
             if (!engine) {
                 if (!shouldLoadFontEngineForCharacter(x, ucs4))
@@ -1891,7 +1909,7 @@ glyph_t QFontEngineMulti::glyphIndex(uin
             glyph = engine->glyphIndex(ucs4);
             if (glyph != 0) {
                 // set the high byte to indicate which engine the glyph came from
-                glyph |= (x << 24);
+                glyph |= (x << 22);
                 break;
             }
         }
@@ -1909,6 +1927,7 @@ bool QFontEngineMulti::stringToCMap(cons
 
     int glyph_pos = 0;
     QStringIterator it(str, str + len);
+    qDebug() << "stringToCMap";
 
     int lastFallback = -1;
     while (it.hasNext()) {
@@ -1918,6 +1937,7 @@ bool QFontEngineMulti::stringToCMap(cons
         // ZWJ or ZWNJ, we should also try applying the same fallback font to that, in order
         // to get the correct shaping rules applied.
         if (lastFallback >= 0 && (ucs4 == QChar(0x200d) || ucs4 == QChar(0x200c))) {
+            qDebug() << "lastFallback logic";
             QFontEngine *engine = m_engines.at(lastFallback);
             glyph_t glyph = engine->glyphIndex(ucs4);
             if (glyph != 0) {
@@ -1928,13 +1948,17 @@ bool QFontEngineMulti::stringToCMap(cons
                 }
 
                 // set the high byte to indicate which engine the glyph came from
-                glyphs->glyphs[glyph_pos] |= (lastFallback << 24);
+                glyphs->glyphs[glyph_pos] |= (lastFallback << 22);
             } else {
                 lastFallback = -1;
             }
         } else {
             lastFallback = -1;
         }
+        bool ok;
+        int maxEngines = qEnvironmentVariableIntValue("QT_MAX_ENGINES", &ok);
+        if (!ok)
+            maxEngines = 1024;
 
         if (glyphs->glyphs[glyph_pos] == 0
                 && ucs4 != QChar::LineSeparator
@@ -1943,11 +1967,13 @@ bool QFontEngineMulti::stringToCMap(cons
                 && ucs4 != QChar::ParagraphSeparator) {
             if (!m_fallbackFamiliesQueried)
                 const_cast<QFontEngineMulti *>(this)->ensureFallbackFamiliesQueried();
-            for (int x = 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
+            for (int x = 1, n = qMin(m_engines.size(), maxEngines); x < n; ++x) {
                 QFontEngine *engine = m_engines.at(x);
                 if (!engine) {
+                    qDebug() << "not engine at " << x;
                     if (!shouldLoadFontEngineForCharacter(x, ucs4))
                         continue;
+                    qDebug() << "should load engine at " << x << " for character " << ucs4;
                     const_cast<QFontEngineMulti *>(this)->ensureEngineAt(x);
                     engine = m_engines.at(x);
                     if (!engine)
@@ -1968,7 +1994,7 @@ bool QFontEngineMulti::stringToCMap(cons
                     lastFallback = x;
 
                     // set the high byte to indicate which engine the glyph came from
-                    glyphs->glyphs[glyph_pos] |= (x << 24);
+                    glyphs->glyphs[glyph_pos] |= (x << 22);
                     break;
                 }
             }
@@ -2022,7 +2048,7 @@ glyph_metrics_t QFontEngineMulti::boundi
         overall.yoff += gm.yoff;
 
         // reset the high byte for all glyphs
-        const int hi = which << 24;
+        const int hi = which << 22;
         for (i = start; i < end; ++i)
             glyphs.glyphs[i] = hi | glyphs.glyphs[i];
 
@@ -2047,7 +2073,7 @@ glyph_metrics_t QFontEngineMulti::boundi
     overall.yoff += gm.yoff;
 
     // reset the high byte for all glyphs
-    const int hi = which << 24;
+    const int hi = which << 22;
     for (i = start; i < end; ++i)
         glyphs.glyphs[i] = hi | glyphs.glyphs[i];
 
@@ -2089,7 +2115,7 @@ void QFontEngineMulti::addOutlineToPath(
             glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
         engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
         // reset the high byte for all glyphs and update x and y
-        const int hi = which << 24;
+        const int hi = which << 22;
         for (i = start; i < end; ++i)
             glyphs.glyphs[i] = hi | glyphs.glyphs[i];
 
@@ -2115,7 +2141,7 @@ void QFontEngineMulti::addOutlineToPath(
     engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
 
     // reset the high byte for all glyphs
-    const int hi = which << 24;
+    const int hi = which << 22;
     for (i = start; i < end; ++i)
         glyphs.glyphs[i] = hi | glyphs.glyphs[i];
 }
@@ -2141,7 +2167,7 @@ void QFontEngineMulti::recalcAdvances(QG
         engine(which)->recalcAdvances(&offs, flags);
 
         // reset the high byte for all glyphs and update x and y
-        const int hi = which << 24;
+        const int hi = which << 22;
         for (i = start; i < end; ++i)
             glyphs->glyphs[i] = hi | glyphs->glyphs[i];
 
@@ -2158,7 +2184,7 @@ void QFontEngineMulti::recalcAdvances(QG
     engine(which)->recalcAdvances(&offs, flags);
 
     // reset the high byte for all glyphs
-    const int hi = which << 24;
+    const int hi = which << 22;
     for (i = start; i < end; ++i)
         glyphs->glyphs[i] = hi | glyphs->glyphs[i];
 }
@@ -2184,7 +2210,7 @@ void QFontEngineMulti::doKerning(QGlyphL
         engine(which)->doKerning(&offs, flags);
 
         // reset the high byte for all glyphs and update x and y
-        const int hi = which << 24;
+        const int hi = which << 22;
         for (i = start; i < end; ++i)
             glyphs->glyphs[i] = hi | glyphs->glyphs[i];
 
@@ -2201,7 +2227,7 @@ void QFontEngineMulti::doKerning(QGlyphL
     engine(which)->doKerning(&offs, flags);
 
     // reset the high byte for all glyphs
-    const int hi = which << 24;
+    const int hi = which << 22;
     for (i = start; i < end; ++i)
         glyphs->glyphs[i] = hi | glyphs->glyphs[i];
 }
Index: qtbase-everywhere-src-5.13.1/src/gui/text/qfontdatabase.cpp
===================================================================
--- qtbase-everywhere-src-5.13.1.orig/src/gui/text/qfontdatabase.cpp
+++ qtbase-everywhere-src-5.13.1/src/gui/text/qfontdatabase.cpp
@@ -837,6 +837,7 @@ QStringList QPlatformFontDatabase::fallb
             }
         }
     }
+    qDebug()<< "fallbacksForFamily(" << family << "): " << preferredFallbacks << " ... " << otherFallbacks;
 
     return preferredFallbacks + otherFallbacks;
 }
@@ -871,6 +872,7 @@ static QStringList fallbacksForFamily(co
             --i;
         }
     }
+    qDebug()<< "fallbacksForFamily(" << family << "): " << retList;
 
     db->fallbacksCache.insert(cacheKey, new QStringList(retList));
 
Index: qtbase-everywhere-src-5.13.1/src/gui/text/qtextengine.cpp
===================================================================
--- qtbase-everywhere-src-5.13.1.orig/src/gui/text/qtextengine.cpp
+++ qtbase-everywhere-src-5.13.1/src/gui/text/qtextengine.cpp
@@ -1455,7 +1455,7 @@ void QTextEngine::shapeText(int item) co
     if (fontEngine->type() == QFontEngine::Multi) {
         uint lastEngine = ~0u;
         for (int i = 0, glyph_pos = 0; i < itemLength; ++i, ++glyph_pos) {
-            const uint engineIdx = initialGlyphs.glyphs[glyph_pos] >> 24;
+            const uint engineIdx = initialGlyphs.glyphs[glyph_pos] >> 22;
             if (lastEngine != engineIdx) {
                 itemBoundaries.append(i);
                 itemBoundaries.append(glyph_pos);
@@ -1749,7 +1749,7 @@ int QTextEngine::shapeTextWithHarfbuzzNG
 
         if (Q_UNLIKELY(engineIdx != 0)) {
             for (quint32 i = 0; i < num_glyphs; ++i)
-                g.glyphs[i] |= (engineIdx << 24);
+                g.glyphs[i] |= (engineIdx << 22);
         }
 
 #ifdef Q_OS_DARWIN
@@ -1870,7 +1870,7 @@ int QTextEngine::shapeTextWithHarfbuzz(c
 
             if (engineIdx != 0 && shaper_item.glyphIndicesPresent) {
                 for (quint32 i = 0; i < shaper_item.initialGlyphCount; ++i)
-                    shaper_item.glyphs[i] &= 0x00ffffff;
+                    shaper_item.glyphs[i] &= 0x003fffff;
             }
 
             shaper_item.log_clusters = logClusters(&si) + shaper_item.item.pos - entire_shaper_item.item.pos;
@@ -1900,7 +1900,7 @@ int QTextEngine::shapeTextWithHarfbuzz(c
 
         if (engineIdx != 0) {
             for (quint32 i = 0; i < shaper_item.num_glyphs; ++i)
-                g.glyphs[i] |= (engineIdx << 24);
+                g.glyphs[i] |= (engineIdx << 22);
         }
 
         glyph_pos += shaper_item.num_glyphs;
Index: qtbase-everywhere-src-5.13.1/src/gui/text/qfont.cpp
===================================================================
--- qtbase-everywhere-src-5.13.1.orig/src/gui/text/qfont.cpp
+++ qtbase-everywhere-src-5.13.1/src/gui/text/qfont.cpp
@@ -67,7 +67,7 @@
 #include <QtCore/QMutexLocker>
 #include <QtCore/QMutex>
 
-// #define QFONTCACHE_DEBUG
+#define QFONTCACHE_DEBUG
 #ifdef QFONTCACHE_DEBUG
 #  define FC_DEBUG qDebug
 #else
Index: qtbase-everywhere-src-5.13.1/src/platformsupport/fontdatabases/fontconfig/qfontconfigdatabase.cpp
===================================================================
--- qtbase-everywhere-src-5.13.1.orig/src/platformsupport/fontdatabases/fontconfig/qfontconfigdatabase.cpp
+++ qtbase-everywhere-src-5.13.1/src/platformsupport/fontdatabases/fontconfig/qfontconfigdatabase.cpp
@@ -784,7 +784,7 @@ QStringList QFontconfigDatabase::fallbac
         }
         FcFontSetDestroy(fontSet);
     }
-//    qDebug() << "fallbackFamilies for:" << family << style << styleHint << script << fallbackFamilies;
+    qDebug() << "fallbackFamilies for:" << family << style << styleHint << script << fallbackFamilies;
 
     return fallbackFamilies;
 }
Index: qtbase-everywhere-src-5.13.1/src/gui/painting/qpainter.cpp
===================================================================
--- qtbase-everywhere-src-5.13.1.orig/src/gui/painting/qpainter.cpp
+++ qtbase-everywhere-src-5.13.1/src/gui/painting/qpainter.cpp
@@ -6488,7 +6488,7 @@ void QPainterPrivate::drawTextItem(const
         QFontEngineMulti *multi = static_cast<QFontEngineMulti *>(ti.fontEngine);
 
         const QGlyphLayout &glyphs = ti.glyphs;
-        int which = glyphs.glyphs[0] >> 24;
+        int which = glyphs.glyphs[0] >> 22;
 
         qreal x = p.x();
         qreal y = p.y();
@@ -6500,7 +6500,7 @@ void QPainterPrivate::drawTextItem(const
         int start = 0;
         int end, i;
         for (end = 0; end < ti.glyphs.numGlyphs; ++end) {
-            const int e = glyphs.glyphs[end] >> 24;
+            const int e = glyphs.glyphs[end] >> 22;
             if (e == which)
                 continue;
 
@@ -6510,7 +6510,7 @@ void QPainterPrivate::drawTextItem(const
             ti2.width = 0;
             // set the high byte to zero and calc the width
             for (i = start; i < end; ++i) {
-                glyphs.glyphs[i] = glyphs.glyphs[i] & 0xffffff;
+                glyphs.glyphs[i] = glyphs.glyphs[i] & 0x003fffff;
                 ti2.width += ti.glyphs.effectiveAdvance(i);
             }
 
@@ -6528,7 +6528,7 @@ void QPainterPrivate::drawTextItem(const
                 x += ti2.width.toReal();
 
             // reset the high byte for all glyphs and advance to the next sub-string
-            const int hi = which << 24;
+            const int hi = which << 22;
             for (i = start; i < end; ++i) {
                 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
             }
@@ -6543,7 +6543,7 @@ void QPainterPrivate::drawTextItem(const
         ti2.width = 0;
         // set the high byte to zero and calc the width
         for (i = start; i < end; ++i) {
-            glyphs.glyphs[i] = glyphs.glyphs[i] & 0xffffff;
+            glyphs.glyphs[i] = glyphs.glyphs[i] & 0x003fffff;
             ti2.width += ti.glyphs.effectiveAdvance(i);
         }
 
@@ -6558,7 +6558,7 @@ void QPainterPrivate::drawTextItem(const
                                ti2.flags, ti2.width.toReal(), ti2.charFormat);
 
         // reset the high byte for all glyphs
-        const int hi = which << 24;
+        const int hi = which << 22;
         for (i = start; i < end; ++i)
             glyphs.glyphs[i] = hi | glyphs.glyphs[i];
 
