summaryrefslogtreecommitdiffstats
path: root/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2017-04-05 13:35:11 +0200
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2017-04-11 14:30:26 +0000
commitf9226217d1b653e7786f8b920f6b36bfd4bd0512 (patch)
tree5b1c09aff9cb4b6a97d862c6807f0b01c24a0896 /src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm
parent31273f079eb1431a3f51bef5c390a1c15cbe382c (diff)
macOS: Prevent leaking font data when creating QRawFont from QByteArrays
CTFontCreateWithGraphicsFont has a bug causing it to never release the graphics font, which cascades down to the data provider never being released and releaseFontData never being called. Instead of relying on a callback to release the byte array font data, we attach the font data to the engine's lifetime. Note that we are still leaking the CoreFoundation types. Change-Id: I6eda4212638ccc9439b90e004222272d204c707a Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
Diffstat (limited to 'src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm')
-rw-r--r--src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm
index c8ae342f16..49a6049c4b 100644
--- a/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm
+++ b/src/platformsupport/fontdatabases/mac/qfontengine_coretext.mm
@@ -177,6 +177,43 @@ CGAffineTransform qt_transform_from_fontdef(const QFontDef &fontDef)
return transform;
}
+// Keeps font data alive until engine is disposed
+class QCoreTextRawFontEngine : public QCoreTextFontEngine
+{
+public:
+ QCoreTextRawFontEngine(CGFontRef font, const QFontDef &def, const QByteArray &fontData)
+ : QCoreTextFontEngine(font, def)
+ , m_fontData(fontData)
+ {}
+ QByteArray m_fontData;
+};
+
+QCoreTextFontEngine *QCoreTextFontEngine::create(const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)
+{
+ Q_UNUSED(hintingPreference);
+
+ QCFType<CFDataRef> fontDataReference = fontData.toRawCFData();
+ QCFType<CGDataProviderRef> dataProvider = CGDataProviderCreateWithCFData(fontDataReference);
+
+ // Note: CTFontCreateWithGraphicsFont (which we call from the QCoreTextFontEngine
+ // constructor) has a bug causing it to retain the CGFontRef but never release it.
+ // The result is that we are leaking the CGFont, CGDataProvider, and CGData, but
+ // as the CGData is created from the raw QByteArray data, which we deref in the
+ // subclass above during destruction, we're at least not leaking the font data,
+ // (unless CoreText copies it internally). http://stackoverflow.com/questions/40805382/
+ QCFType<CGFontRef> cgFont = CGFontCreateWithDataProvider(dataProvider);
+
+ if (!cgFont) {
+ qWarning("QCoreTextFontEngine::create: CGFontCreateWithDataProvider failed");
+ return nullptr;
+ }
+
+ QFontDef def;
+ def.pixelSize = pixelSize;
+ def.pointSize = pixelSize * 72.0 / qt_defaultDpi();
+ return new QCoreTextRawFontEngine(cgFont, def, fontData);
+}
+
QCoreTextFontEngine::QCoreTextFontEngine(CTFontRef font, const QFontDef &def)
: QFontEngine(Mac)
{