From 53abb8267bfa4c261a1aa543f1ad50ed0851bcbf Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 5 Jul 2015 22:37:40 +0200 Subject: QtGui: mark some more types as movable/primitive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These are already held in QVectors. Public API types need to wait until Qt 6, for BC reasons. Even though Q_RELOCATABLE_TYPE deals with most of them, we lack a way to mark a type as primitive, but still isStatic - for QList. Change-Id: I91392b01ae6f94cc847007636e12d4e64c43b2bc Reviewed-by: Jędrzej Nowacki --- src/gui/text/qabstracttextdocumentlayout.h | 2 ++ src/gui/text/qfontdatabase.cpp | 1 + src/gui/text/qfontengine_p.h | 1 + src/gui/text/qstatictext_p.h | 1 + src/gui/text/qtextdocumentfragment_p.h | 8 ++++++++ src/gui/text/qtextformat.cpp | 1 + src/gui/text/qtextformat.h | 1 + src/gui/text/qtexthtmlparser_p.h | 2 ++ src/gui/text/qtextlayout.h | 1 + 9 files changed, 18 insertions(+) (limited to 'src/gui/text') diff --git a/src/gui/text/qabstracttextdocumentlayout.h b/src/gui/text/qabstracttextdocumentlayout.h index e83f4fc0c9..01704fe37b 100644 --- a/src/gui/text/qabstracttextdocumentlayout.h +++ b/src/gui/text/qabstracttextdocumentlayout.h @@ -128,6 +128,8 @@ private: Q_PRIVATE_SLOT(d_func(), int _q_dynamicPageCountSlot()) Q_PRIVATE_SLOT(d_func(), QSizeF _q_dynamicDocumentSizeSlot()) }; +Q_DECLARE_TYPEINFO(QAbstractTextDocumentLayout::Selection, Q_RELOCATABLE_TYPE); +Q_DECLARE_TYPEINFO(QAbstractTextDocumentLayout::PaintContext, Q_RELOCATABLE_TYPE); class Q_GUI_EXPORT QTextObjectInterface { diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index 32cf1b0e83..629a098fb7 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -499,6 +499,7 @@ public: void invalidate(); }; +Q_DECLARE_TYPEINFO(QFontDatabasePrivate::ApplicationFont, Q_MOVABLE_TYPE); void QFontDatabasePrivate::invalidate() { diff --git a/src/gui/text/qfontengine_p.h b/src/gui/text/qfontengine_p.h index f97f91da50..059b3df88e 100644 --- a/src/gui/text/qfontengine_p.h +++ b/src/gui/text/qfontengine_p.h @@ -334,6 +334,7 @@ private: mutable qreal m_minRightBearing; }; +Q_DECLARE_TYPEINFO(QFontEngine::KernPair, Q_PRIMITIVE_TYPE); Q_DECLARE_OPERATORS_FOR_FLAGS(QFontEngine::ShaperFlags) diff --git a/src/gui/text/qstatictext_p.h b/src/gui/text/qstatictext_p.h index d27d5a8725..7fdf46c101 100644 --- a/src/gui/text/qstatictext_p.h +++ b/src/gui/text/qstatictext_p.h @@ -119,6 +119,7 @@ private: // private to avoid abuse // ================ // 43 bytes per item }; +Q_DECLARE_TYPEINFO(QStaticTextItem, Q_MOVABLE_TYPE); class QStaticText; class Q_AUTOTEST_EXPORT QStaticTextPrivate diff --git a/src/gui/text/qtextdocumentfragment_p.h b/src/gui/text/qtextdocumentfragment_p.h index 4cc4f0429f..56dff01149 100644 --- a/src/gui/text/qtextdocumentfragment_p.h +++ b/src/gui/text/qtextdocumentfragment_p.h @@ -148,6 +148,7 @@ private: int listNode; QPointer list; }; + friend class QTypeInfo; QVector lists; int indent; @@ -187,6 +188,7 @@ private: int row; int column; }; + friend class QTypeInfo; friend struct Table; struct Table @@ -200,6 +202,7 @@ private: TableCellIterator currentCell; int lastIndent; }; + friend class QTypeInfo; QVector
tables; struct RowColSpanInfo @@ -207,6 +210,7 @@ private: int row, col; int rowSpan, colSpan; }; + friend class QTypeInfo; enum WhiteSpace { @@ -227,6 +231,10 @@ private: int currentNodeIdx; const QTextHtmlParserNode *currentNode; }; +Q_DECLARE_TYPEINFO(QTextHtmlImporter::List, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QTextHtmlImporter::TableCellIterator, Q_PRIMITIVE_TYPE); +Q_DECLARE_TYPEINFO(QTextHtmlImporter::Table, Q_MOVABLE_TYPE); +Q_DECLARE_TYPEINFO(QTextHtmlImporter::RowColSpanInfo, Q_PRIMITIVE_TYPE); QT_END_NAMESPACE #endif // QT_NO_TEXTHTMLPARSER diff --git a/src/gui/text/qtextformat.cpp b/src/gui/text/qtextformat.cpp index 58049399ef..49ec9ca768 100644 --- a/src/gui/text/qtextformat.cpp +++ b/src/gui/text/qtextformat.cpp @@ -263,6 +263,7 @@ private: friend QDataStream &operator<<(QDataStream &, const QTextFormat &); friend QDataStream &operator>>(QDataStream &, QTextFormat &); }; +Q_DECLARE_TYPEINFO(QTextFormatPrivate::Property, Q_MOVABLE_TYPE); static inline uint hash(const QColor &color) { diff --git a/src/gui/text/qtextformat.h b/src/gui/text/qtextformat.h index 9dd83ab678..bc627521ff 100644 --- a/src/gui/text/qtextformat.h +++ b/src/gui/text/qtextformat.h @@ -116,6 +116,7 @@ private: friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QTextLength &); friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QTextLength &); }; +Q_DECLARE_TYPEINFO(QTextLength, QT_VERSION >= QT_VERSION_CHECK(6,0,0) ? Q_PRIMITIVE_TYPE : Q_RELOCATABLE_TYPE); inline QTextLength::QTextLength(Type atype, qreal avalue) : lengthType(atype), fixedValueOrPercentage(avalue) {} diff --git a/src/gui/text/qtexthtmlparser_p.h b/src/gui/text/qtexthtmlparser_p.h index a8a849ca7f..8e5a90be0b 100644 --- a/src/gui/text/qtexthtmlparser_p.h +++ b/src/gui/text/qtexthtmlparser_p.h @@ -328,12 +328,14 @@ protected: QString url; QCss::StyleSheet sheet; }; + friend class QTypeInfo; QVector externalStyleSheets; QVector inlineStyleSheets; #endif const QTextDocument *resourceProvider; }; +Q_DECLARE_TYPEINFO(QTextHtmlParser::ExternalStyleSheet, Q_MOVABLE_TYPE); QT_END_NAMESPACE diff --git a/src/gui/text/qtextlayout.h b/src/gui/text/qtextlayout.h index d9afb1ed4b..1cbb5ac8d8 100644 --- a/src/gui/text/qtextlayout.h +++ b/src/gui/text/qtextlayout.h @@ -203,6 +203,7 @@ private: QPainter *painter); QTextEngine *d; }; +Q_DECLARE_TYPEINFO(QTextLayout::FormatRange, Q_RELOCATABLE_TYPE); class Q_GUI_EXPORT QTextLine -- cgit v1.2.3 From 600529e07a46d8e20f4302dc988125f3fee36ec4 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 17 Oct 2015 17:48:34 +0200 Subject: QtGui: use printf-style qWarning/qDebug where possible (I) The printf-style version of QDebug expands to a lot less code than the std::ostream-style version. Of course, you pay in type safety (but compilers warn about it these days), you cannot stream complex Qt types and streaming QStrings is awkward, but in many cases you actually improve on readability. But the main reason is that something that's not supposed to be executed under normal operation has no business bloating executable code size. This is not an attempt at converting all qWarnings() to printf-style, only the low-hanging fruit. In this first part, replace qWarning() << "..."; with qWarning("..."); In QTransform shared warning strings. Saves 3KiB in text size on optimized GCC 5.3 AMD64 builds. Change-Id: I142a8020eaab043d78465178192f2c8c6d1cc4f9 Reviewed-by: Friedemann Kleint Reviewed-by: Kai Koehne --- src/gui/text/qfontengine_qpf2.cpp | 2 +- src/gui/text/qfontsubset.cpp | 6 +++--- src/gui/text/qplatformfontdatabase.cpp | 2 +- src/gui/text/qtextdocumentlayout.cpp | 6 +++--- src/gui/text/qtextdocumentwriter.cpp | 4 ++-- src/gui/text/qtextengine.cpp | 2 +- src/gui/text/qtextlayout.cpp | 2 +- src/gui/text/qtextodfwriter.cpp | 2 +- src/gui/text/qzip.cpp | 16 ++++++++-------- 9 files changed, 21 insertions(+), 21 deletions(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qfontengine_qpf2.cpp b/src/gui/text/qfontengine_qpf2.cpp index 4b5d241e60..c3a911fc55 100644 --- a/src/gui/text/qfontengine_qpf2.cpp +++ b/src/gui/text/qfontengine_qpf2.cpp @@ -246,7 +246,7 @@ QFontEngineQPF2::QFontEngineQPF2(const QFontDef &def, const QByteArray &data) if (!verifyHeader(fontData, dataSize)) { #if defined(DEBUG_FONTENGINE) - qDebug() << "verifyHeader failed!"; + qDebug("verifyHeader failed!"); #endif return; } diff --git a/src/gui/text/qfontsubset.cpp b/src/gui/text/qfontsubset.cpp index a9387e5aa0..f591b4333c 100644 --- a/src/gui/text/qfontsubset.cpp +++ b/src/gui/text/qfontsubset.cpp @@ -798,7 +798,7 @@ static void convertPath(const QPainterPath &path, QVector *points, QV base -= 3; } else { // need to split -// qDebug() << " -> splitting"; +// qDebug(" -> splitting"); qint16 a, b, c, d; base[6].x = base[3].x; c = base[1].x; @@ -859,7 +859,7 @@ static void getBounds(const QVector &points, qint16 *xmin, qint16 *xm static int convertToRelative(QVector *points) { // convert points to relative and setup flags -// qDebug() << "relative points:"; +// qDebug("relative points:"); qint16 prev_x = 0; qint16 prev_y = 0; int point_array_size = 0; @@ -980,7 +980,7 @@ static QTtfGlyph generateGlyph(int index, const QPainterPath &path, qreal advanc // qDebug() << "number of contours=" << endPoints.size(); // for (int i = 0; i < points.size(); ++i) // qDebug() << " point[" << i << "] = " << QPoint(points.at(i).x, points.at(i).y) << " flags=" << points.at(i).flags; -// qDebug() << "endPoints:"; +// qDebug("endPoints:"); // for (int i = 0; i < endPoints.size(); ++i) // qDebug() << endPoints.at(i); diff --git a/src/gui/text/qplatformfontdatabase.cpp b/src/gui/text/qplatformfontdatabase.cpp index 017e2f254d..0d7cb204ff 100644 --- a/src/gui/text/qplatformfontdatabase.cpp +++ b/src/gui/text/qplatformfontdatabase.cpp @@ -100,7 +100,7 @@ void QPlatformFontDatabase::registerQPF2Font(const QByteArray &dataArray, void * registerFont(fontName,QString(),QString(),fontWeight,fontStyle,stretch,true,false,pixelSize,false,writingSystems,handle); } } else { - qDebug() << "header verification of QPF2 font failed. maybe it is corrupt?"; + qDebug("header verification of QPF2 font failed. maybe it is corrupt?"); } } diff --git a/src/gui/text/qtextdocumentlayout.cpp b/src/gui/text/qtextdocumentlayout.cpp index ff492b15f6..c26fd08c41 100644 --- a/src/gui/text/qtextdocumentlayout.cpp +++ b/src/gui/text/qtextdocumentlayout.cpp @@ -1598,7 +1598,7 @@ QTextLayoutStruct QTextDocumentLayoutPrivate::layoutCell(QTextTable *t, const QT // floats in other cells we must clear the list here. data(t)->floats.clear(); -// qDebug() << "layoutCell done"; +// qDebug("layoutCell done"); return layoutStruct; } @@ -2030,7 +2030,7 @@ void QTextDocumentLayoutPrivate::positionFloat(QTextFrame *frame, QTextLine *cur // qDebug() << "have line: right=" << right << "left=" << left << "textWidth=" << currentLine->width(); if (right - left < QFixed::fromReal(currentLine->naturalTextWidth()) + fd->size.width) { layoutStruct->pendingFloats.append(frame); -// qDebug() << " adding to pending list"; +// qDebug(" adding to pending list"); return; } } @@ -2543,7 +2543,7 @@ void QTextDocumentLayoutPrivate::layoutFlow(QTextFrame::Iterator it, QTextLayout contentHasAlignment = true; if (it.atEnd()) { - //qDebug() << "layout done!"; + //qDebug("layout done!"); currentLazyLayoutPosition = -1; QCheckPoint cp; cp.y = layoutStruct->y; diff --git a/src/gui/text/qtextdocumentwriter.cpp b/src/gui/text/qtextdocumentwriter.cpp index d672b1187a..a7c58ac2a6 100644 --- a/src/gui/text/qtextdocumentwriter.cpp +++ b/src/gui/text/qtextdocumentwriter.cpp @@ -271,7 +271,7 @@ bool QTextDocumentWriter::write(const QTextDocument *document) #ifndef QT_NO_TEXTHTMLPARSER if (format == "html" || format == "htm") { if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) { - qWarning() << "QTextDocumentWriter::write: the device can not be opened for writing"; + qWarning("QTextDocumentWriter::write: the device can not be opened for writing"); return false; } QTextStream ts(d->device); @@ -285,7 +285,7 @@ bool QTextDocumentWriter::write(const QTextDocument *document) #endif if (format == "txt" || format == "plaintext") { if (!d->device->isWritable() && ! d->device->open(QIODevice::WriteOnly)) { - qWarning() << "QTextDocumentWriter::write: the device can not be opened for writing"; + qWarning("QTextDocumentWriter::write: the device can not be opened for writing"); return false; } QTextStream ts(d->device); diff --git a/src/gui/text/qtextengine.cpp b/src/gui/text/qtextengine.cpp index f5df6fd60b..3ee6177f3c 100644 --- a/src/gui/text/qtextengine.cpp +++ b/src/gui/text/qtextengine.cpp @@ -816,7 +816,7 @@ void QTextEngine::bidiReorder(int numItems, const quint8 *levels, int *visualOrd } #if (BIDI_DEBUG >= 1) -// qDebug() << "visual order is:"; +// qDebug("visual order is:"); // for (i = 0; i < numItems; i++) // qDebug() << visualOrder[i]; #endif diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index 8902d52b28..128966a35a 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -2976,7 +2976,7 @@ int QTextLine::xToCursor(qreal _x, CursorPosition cpos) const } } // right of last item -// qDebug() << "right of last"; +// qDebug("right of last"); int item = visualOrder[nItems-1]+firstItem; QScriptItem &si = eng->layoutData->items[item]; if (!si.num_glyphs) diff --git a/src/gui/text/qtextodfwriter.cpp b/src/gui/text/qtextodfwriter.cpp index 4baa94edd5..bd2a9d5183 100644 --- a/src/gui/text/qtextodfwriter.cpp +++ b/src/gui/text/qtextodfwriter.cpp @@ -769,7 +769,7 @@ bool QTextOdfWriter::writeAll() m_strategy = new QXmlStreamStrategy(m_device); if (!m_device->isWritable() && ! m_device->open(QIODevice::WriteOnly)) { - qWarning() << "QTextOdfWriter::writeAll: the device can not be opened for writing"; + qWarning("QTextOdfWriter::writeAll: the device can not be opened for writing"); return false; } QXmlStreamWriter writer(m_strategy->contentStream); diff --git a/src/gui/text/qzip.cpp b/src/gui/text/qzip.cpp index 4f43f73d41..e42c268493 100644 --- a/src/gui/text/qzip.cpp +++ b/src/gui/text/qzip.cpp @@ -574,7 +574,7 @@ void QZipReaderPrivate::scanFiles() uchar tmp[4]; device->read((char *)tmp, 4); if (readUInt(tmp) != 0x04034b50) { - qWarning() << "QZip: not a zip file!"; + qWarning("QZip: not a zip file!"); return; } @@ -586,7 +586,7 @@ void QZipReaderPrivate::scanFiles() while (start_of_directory == -1) { const int pos = device->size() - int(sizeof(EndOfDirectory)) - i; if (pos < 0 || i > 65535) { - qWarning() << "QZip: EndOfDirectory not found"; + qWarning("QZip: EndOfDirectory not found"); return; } @@ -603,7 +603,7 @@ void QZipReaderPrivate::scanFiles() ZDEBUG("start_of_directory at %d, num_dir_entries=%d", start_of_directory, num_dir_entries); int comment_length = readUShort(eod.comment_length); if (comment_length != i) - qWarning() << "QZip: failed to parse zip file."; + qWarning("QZip: failed to parse zip file."); comment = device->read(qMin(comment_length, i)); @@ -612,30 +612,30 @@ void QZipReaderPrivate::scanFiles() FileHeader header; int read = device->read((char *) &header.h, sizeof(CentralFileHeader)); if (read < (int)sizeof(CentralFileHeader)) { - qWarning() << "QZip: Failed to read complete header, index may be incomplete"; + qWarning("QZip: Failed to read complete header, index may be incomplete"); break; } if (readUInt(header.h.signature) != 0x02014b50) { - qWarning() << "QZip: invalid header signature, index may be incomplete"; + qWarning("QZip: invalid header signature, index may be incomplete"); break; } int l = readUShort(header.h.file_name_length); header.file_name = device->read(l); if (header.file_name.length() != l) { - qWarning() << "QZip: Failed to read filename from zip index, index may be incomplete"; + qWarning("QZip: Failed to read filename from zip index, index may be incomplete"); break; } l = readUShort(header.h.extra_field_length); header.extra_field = device->read(l); if (header.extra_field.length() != l) { - qWarning() << "QZip: Failed to read extra field in zip file, skipping file, index may be incomplete"; + qWarning("QZip: Failed to read extra field in zip file, skipping file, index may be incomplete"); break; } l = readUShort(header.h.file_comment_length); header.file_comment = device->read(l); if (header.file_comment.length() != l) { - qWarning() << "QZip: Failed to read read file comment, index may be incomplete"; + qWarning("QZip: Failed to read read file comment, index may be incomplete"); break; } -- cgit v1.2.3 From 7d2c7ca8fad2d8177599ba3a4851c48d3baf5772 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 18 Feb 2016 12:00:27 +0100 Subject: QFontEngine: use RAII for font_, face_ members Wrap the pairs of (void *ptr, void (*dtor)(void*)) in essentially a std::unique_ptr. This simplifies code and provides the correct implicit destruction, so we can drop the explicit glyph-cache clear()ing in ~QFontEngine(), leaving that job to ~QLinkedList. A subsequent change will turn the QLinkedList into a C array, the clearing of which would otherwise cause excessive code bloat. Since we can't use std::unique_ptr, yet, provide a hand-rolled replacement for now, marking it for replacement with unique_ptr once we can use it. Make that a local type instead of providing a Qt-wide unique_ptr so we don't accidentally lock ourselves into a half-baked std clone we can't get rid of anymore. To prepare unique_ptr use with the same type-erased deleter (function pointer) as now, replace a nullptr destroy_function with a no-op function, so ~unique_ptr doesn't crash when we port to it later. Because QFreetypeFace contains the same construct and shares payloads with QFontEngine, use the Holder there, too. Even saves 150b in text size on optimized GCC 5.3 AMD64 builds. Change-Id: I5ca11a3e6e1ff9e06199124403d96e1b280f3eb2 Reviewed-by: Konstantin Ritt Reviewed-by: Lars Knoll --- src/gui/text/qfontengine.cpp | 25 ++++++------------------ src/gui/text/qfontengine_ft.cpp | 18 +++++++---------- src/gui/text/qfontengine_ft_p.h | 3 +-- src/gui/text/qfontengine_p.h | 43 +++++++++++++++++++++++++++++++++++++---- src/gui/text/qharfbuzzng.cpp | 20 ++++++------------- 5 files changed, 59 insertions(+), 50 deletions(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qfontengine.cpp b/src/gui/text/qfontengine.cpp index 7c0492bb1a..96f7e06a47 100644 --- a/src/gui/text/qfontengine.cpp +++ b/src/gui/text/qfontengine.cpp @@ -246,8 +246,8 @@ Q_AUTOTEST_EXPORT QList QFontEngine_stopCollectingEngines() QFontEngine::QFontEngine(Type type) : m_type(type), ref(0), - font_(0), font_destroy_func(0), - face_(0), face_destroy_func(0), + font_(), + face_(), m_minLeftBearing(kBearingNotInitialized), m_minRightBearing(kBearingNotInitialized) { @@ -269,17 +269,6 @@ QFontEngine::QFontEngine(Type type) QFontEngine::~QFontEngine() { - m_glyphCaches.clear(); - - if (font_ && font_destroy_func) { - font_destroy_func(font_); - font_ = 0; - } - if (face_ && face_destroy_func) { - face_destroy_func(face_); - face_ = 0; - } - #ifdef QT_BUILD_INTERNAL if (enginesCollector) enginesCollector->removeOne(this); @@ -334,10 +323,9 @@ void *QFontEngine::harfbuzzFont() const hbFont->x_scale = (((qint64)hbFont->x_ppem << 6) * 0x10000L + (emSquare >> 1)) / emSquare; hbFont->y_scale = (((qint64)hbFont->y_ppem << 6) * 0x10000L + (emSquare >> 1)) / emSquare; - font_ = (void *)hbFont; - font_destroy_func = free; + font_ = Holder(hbFont, free); } - return font_; + return font_.get(); } void *QFontEngine::harfbuzzFace() const @@ -357,10 +345,9 @@ void *QFontEngine::harfbuzzFace() const Q_CHECK_PTR(hbFace); hbFace->isSymbolFont = symbol; - face_ = (void *)hbFace; - face_destroy_func = hb_freeFace; + face_ = Holder(hbFace, hb_freeFace); } - return face_; + return face_.get(); } bool QFontEngine::supportsScript(QChar::Script script) const diff --git a/src/gui/text/qfontengine_ft.cpp b/src/gui/text/qfontengine_ft.cpp index 779333351f..3207325755 100644 --- a/src/gui/text/qfontengine_ft.cpp +++ b/src/gui/text/qfontengine_ft.cpp @@ -246,9 +246,6 @@ QFreetypeFace *QFreetypeFace::getFace(const QFontEngine::FaceId &face_id, } newFreetype->face = face; - newFreetype->hbFace = 0; - newFreetype->hbFace_destroy_func = 0; - newFreetype->ref.store(1); newFreetype->xsize = 0; newFreetype->ysize = 0; @@ -300,10 +297,7 @@ QFreetypeFace *QFreetypeFace::getFace(const QFontEngine::FaceId &face_id, void QFreetypeFace::cleanup() { - if (hbFace && hbFace_destroy_func) { - hbFace_destroy_func(hbFace); - hbFace = 0; - } + hbFace.reset(); FT_Done_Face(face); face = 0; } @@ -686,6 +680,8 @@ bool QFontEngineFT::init(FaceId faceId, bool antialias, GlyphFormat format, return init(faceId, antialias, format, QFreetypeFace::getFace(faceId, fontData)); } +static void dont_delete(void*) {} + bool QFontEngineFT::init(FaceId faceId, bool antialias, GlyphFormat format, QFreetypeFace *freetypeFace) { @@ -776,13 +772,13 @@ bool QFontEngineFT::init(FaceId faceId, bool antialias, GlyphFormat format, if (!freetype->hbFace) { faceData.user_data = face; faceData.get_font_table = ft_getSfntTable; - freetype->hbFace = harfbuzzFace(); - freetype->hbFace_destroy_func = face_destroy_func; + (void)harfbuzzFace(); // populates face_ + freetype->hbFace = std::move(face_); } else { Q_ASSERT(!face_); - face_ = freetype->hbFace; } - face_destroy_func = 0; // we share the HB face in QFreeTypeFace, so do not let ~QFontEngine() destroy it + // we share the HB face in QFreeTypeFace, so do not let ~QFontEngine() destroy it + face_ = Holder(freetype->hbFace.get(), dont_delete); unlockFace(); diff --git a/src/gui/text/qfontengine_ft_p.h b/src/gui/text/qfontengine_ft_p.h index 1886fc67ba..f9b37ff039 100644 --- a/src/gui/text/qfontengine_ft_p.h +++ b/src/gui/text/qfontengine_ft_p.h @@ -121,8 +121,7 @@ private: QMutex _lock; QByteArray fontData; - void *hbFace; - qt_destroy_func_t hbFace_destroy_func; + QFontEngine::Holder hbFace; }; // If this is exported this breaks compilation of the windows diff --git a/src/gui/text/qfontengine_p.h b/src/gui/text/qfontengine_p.h index 059b3df88e..1ef3a360d4 100644 --- a/src/gui/text/qfontengine_p.h +++ b/src/gui/text/qfontengine_p.h @@ -275,10 +275,45 @@ public: QAtomicInt ref; QFontDef fontDef; - mutable void *font_; - mutable qt_destroy_func_t font_destroy_func; - mutable void *face_; - mutable qt_destroy_func_t face_destroy_func; + class Holder { // replace by std::unique_ptr once available + void *ptr; + qt_destroy_func_t destroy_func; + public: + Holder() : ptr(nullptr), destroy_func(nullptr) {} + explicit Holder(void *p, qt_destroy_func_t d) : ptr(p), destroy_func(d) {} + ~Holder() { if (ptr && destroy_func) destroy_func(ptr); } + Holder(Holder &&other) Q_DECL_NOTHROW + : ptr(other.ptr), + destroy_func(other.destroy_func) + { + other.ptr = nullptr; + other.destroy_func = nullptr; + } + Holder &operator=(Holder &&other) Q_DECL_NOTHROW + { swap(other); return *this; } + + void swap(Holder &other) Q_DECL_NOTHROW + { + qSwap(ptr, other.ptr); + qSwap(destroy_func, other.destroy_func); + } + + void *get() const Q_DECL_NOTHROW { return ptr; } + void *release() Q_DECL_NOTHROW { + void *result = ptr; + ptr = nullptr; + destroy_func = nullptr; + return result; + } + void reset() Q_DECL_NOTHROW { Holder().swap(*this); } + qt_destroy_func_t get_deleter() const Q_DECL_NOTHROW { return destroy_func; } + + bool operator!() const Q_DECL_NOTHROW { return !ptr; } + }; + + mutable Holder font_; // \ NOTE: Declared before m_glyphCaches, so font_, face_ + mutable Holder face_; // / are destroyed _after_ m_glyphCaches is destroyed. + struct FaceData { void *user_data; qt_get_font_table_func_t get_font_table; diff --git a/src/gui/text/qharfbuzzng.cpp b/src/gui/text/qharfbuzzng.cpp index e33b461401..55ef9f0d15 100644 --- a/src/gui/text/qharfbuzzng.cpp +++ b/src/gui/text/qharfbuzzng.cpp @@ -678,14 +678,10 @@ hb_face_t *hb_qt_face_get_for_engine(QFontEngine *fe) { Q_ASSERT(fe && fe->type() != QFontEngine::Multi); - if (Q_UNLIKELY(!fe->face_)) { - fe->face_ = _hb_qt_face_create(fe); - if (Q_UNLIKELY(!fe->face_)) - return NULL; - fe->face_destroy_func = _hb_qt_face_release; - } + if (Q_UNLIKELY(!fe->face_)) + fe->face_ = QFontEngine::Holder(_hb_qt_face_create(fe), _hb_qt_face_release); - return static_cast(fe->face_); + return static_cast(fe->face_.get()); } @@ -728,14 +724,10 @@ hb_font_t *hb_qt_font_get_for_engine(QFontEngine *fe) { Q_ASSERT(fe && fe->type() != QFontEngine::Multi); - if (Q_UNLIKELY(!fe->font_)) { - fe->font_ = _hb_qt_font_create(fe); - if (Q_UNLIKELY(!fe->font_)) - return NULL; - fe->font_destroy_func = _hb_qt_font_release; - } + if (Q_UNLIKELY(!fe->font_)) + fe->font_ = QFontEngine::Holder(_hb_qt_font_create(fe), _hb_qt_font_release); - return static_cast(fe->font_); + return static_cast(fe->font_.get()); } QT_END_NAMESPACE -- cgit v1.2.3 From ec29c76e18f3a30e559e9eb31dfcc20b80b9522c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 5 Mar 2016 11:21:02 +0100 Subject: Fix UB in QFontEngineFT::loadGlyph() Reported by UBSan: src/gui/text/qfontengine_ft.cpp:1079:54: runtime error: null pointer passed as argument 1, which is declared to never be null The default-constructed QScopedArrayPointer is not reset() in every code path. In fact, in the code path leading to this memset, the only reset() call is in the if block right above it, so move the memset into the if block. Change-Id: I1f793c313ca56f3315c6bdd55456cb025cafc089 Reviewed-by: Konstantin Ritt --- src/gui/text/qfontengine_ft.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qfontengine_ft.cpp b/src/gui/text/qfontengine_ft.cpp index 0a0e174343..8fbeff3596 100644 --- a/src/gui/text/qfontengine_ft.cpp +++ b/src/gui/text/qfontengine_ft.cpp @@ -1073,8 +1073,8 @@ QFontEngineFT::Glyph *QFontEngineFT::loadGlyph(QGlyphSet *set, uint glyph, if (glyph_buffer_size < pitch * info.height) { glyph_buffer_size = pitch * info.height; glyph_buffer.reset(new uchar[glyph_buffer_size]); + memset(glyph_buffer.data(), 0, glyph_buffer_size); } - memset(glyph_buffer.data(), 0, glyph_buffer_size); if (slot->format == FT_GLYPH_FORMAT_OUTLINE) { FT_Bitmap bitmap; -- cgit v1.2.3 From 229c51978511e71781f0afcf5051e625cc18901d Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Sun, 21 Feb 2016 19:37:17 +0400 Subject: Increase chances of finding the ellipsis glyph in elided text The glyph for the ellipsis could be absent in the main font, so we should try to find it in a fallback font; otherwise fall back to "...". Change-Id: Ic53060ed42f3c800aba055d2be2a1c7c3cfeec64 Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/gui/text/qtextengine.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qtextengine.cpp b/src/gui/text/qtextengine.cpp index c545240c57..d0c2779a65 100644 --- a/src/gui/text/qtextengine.cpp +++ b/src/gui/text/qtextengine.cpp @@ -2747,8 +2747,7 @@ QString QTextEngine::elidedText(Qt::TextElideMode mode, const QFixed &width, int QFixed ellipsisWidth; QString ellipsisText; { - QFontEngine *fe = fnt.d->engineForScript(QChar::Script_Common); - QFontEngine *engine = fe->type() == QFontEngine::Multi ? static_cast(fe)->engine(0) : fe; + QFontEngine *engine = fnt.d->engineForScript(QChar::Script_Common); QChar ellipsisChar(0x2026); -- cgit v1.2.3 From a3def2869da8ad9c17d44005c0d1fd70a903f855 Mon Sep 17 00:00:00 2001 From: Anton Kudryavtsev Date: Thu, 25 Feb 2016 12:41:35 +0300 Subject: QtGui: de-duplicate calls and cache results Change-Id: Iaf232c31d6780b49dc6a3d0faafb9717f3c36e65 Reviewed-by: Marc Mutz Reviewed-by: Lars Knoll Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/gui/text/qzip.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qzip.cpp b/src/gui/text/qzip.cpp index e42c268493..5178f5a9a8 100644 --- a/src/gui/text/qzip.cpp +++ b/src/gui/text/qzip.cpp @@ -824,14 +824,15 @@ QZipReader::QZipReader(const QString &archive, QIODevice::OpenMode mode) QScopedPointer f(new QFile(archive)); f->open(mode); QZipReader::Status status; - if (f->error() == QFile::NoError) + const QFileDevice::FileError error = f->error(); + if (error == QFile::NoError) status = NoError; else { - if (f->error() == QFile::ReadError) + if (error == QFile::ReadError) status = FileReadError; - else if (f->error() == QFile::OpenError) + else if (error == QFile::OpenError) status = FileOpenError; - else if (f->error() == QFile::PermissionsError) + else if (error == QFile::PermissionsError) status = FilePermissionsError; else status = FileError; -- cgit v1.2.3 From 9739cae4c84218e1a805bbd82b2f40fe20d57b74 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 10 Mar 2016 09:53:36 +0100 Subject: QRawFont: fix UB (misaligned load) in fontTable() Found by UBSan: qrawfont.cpp:618:60: runtime error: load of misaligned address 0x2acee92a5569 for type 'const quint32', which requires 4 byte alignment Fix by using MAKE_TAG(), like everywhere else, instead of a load through a type-punned and misaligned pointer. Change-Id: I52b88ca05a57f7d8c5e5bce953384de49514079b Reviewed-by: Konstantin Ritt Reviewed-by: Lars Knoll --- src/gui/text/qrawfont.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qrawfont.cpp b/src/gui/text/qrawfont.cpp index 0fd5f510c7..59f13581dd 100644 --- a/src/gui/text/qrawfont.cpp +++ b/src/gui/text/qrawfont.cpp @@ -607,8 +607,7 @@ QByteArray QRawFont::fontTable(const char *tagName) const if (!d->isValid()) return QByteArray(); - const quint32 *tagId = reinterpret_cast(tagName); - return d->fontEngine->getSfntTable(qToBigEndian(*tagId)); + return d->fontEngine->getSfntTable(MAKE_TAG(tagName[0], tagName[1], tagName[2], tagName[3])); } /*! -- cgit v1.2.3 From f8f3f0fbd468feaf14adcedbccbf6b434f2e2e49 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Tue, 15 Mar 2016 16:07:11 +0100 Subject: Correct qt_defaultDpi X/Y with just a QCoreApplication Makes the 96DPI attribute check avoid undefined behavior by using QCoreApplication::instance() directly, instead of calling through qApp, which performs an invalid cast to QGuiApplication. Change-Id: Ib86e7d2461b462a2d623f1364414f7d4d2293f22 Reviewed-by: Marc Mutz --- src/gui/text/qfont.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index 2c5a0c74fc..fe68149346 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -131,7 +131,7 @@ extern bool qt_is_gui_used; Q_GUI_EXPORT int qt_defaultDpiX() { - if (qApp->testAttribute(Qt::AA_Use96Dpi)) + if (QCoreApplication::instance()->testAttribute(Qt::AA_Use96Dpi)) return 96; if (!qt_is_gui_used) @@ -146,7 +146,7 @@ Q_GUI_EXPORT int qt_defaultDpiX() Q_GUI_EXPORT int qt_defaultDpiY() { - if (qApp->testAttribute(Qt::AA_Use96Dpi)) + if (QCoreApplication::instance()->testAttribute(Qt::AA_Use96Dpi)) return 96; if (!qt_is_gui_used) -- cgit v1.2.3