From 858686d88c8161c7d232c209735b9ca45d712ff5 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 8 Jan 2014 14:02:32 +0000 Subject: [PATCH] Workaround for CoreText layout failure with in-memory typefaces. --- .../juce_graphics/native/juce_mac_Fonts.mm | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/modules/juce_graphics/native/juce_mac_Fonts.mm b/modules/juce_graphics/native/juce_mac_Fonts.mm index d54d7d6f41..073fe117fc 100644 --- a/modules/juce_graphics/native/juce_mac_Fonts.mm +++ b/modules/juce_graphics/native/juce_mac_Fonts.mm @@ -486,7 +486,7 @@ public: font.getTypefaceStyle()), fontRef (nullptr), ctFontRef (nullptr), - fontHeightToPointsFactor (1.0f), + fontHeightToPointsFactor (1.0f), isMemoryFont (false), renderingTransform (CGAffineTransformIdentity), attributedStringAtts (nullptr), ascent (0.0f), @@ -505,7 +505,7 @@ public: : Typeface (String(), String()), fontRef (nullptr), ctFontRef (nullptr), - fontHeightToPointsFactor (1.0f), + fontHeightToPointsFactor (1.0f), isMemoryFont (true), renderingTransform (CGAffineTransformIdentity), attributedStringAtts (nullptr), ascent (0.0f), @@ -672,6 +672,8 @@ public: float fontHeightToPointsFactor; CGAffineTransform renderingTransform; + bool isMemoryFont; + private: CFDictionaryRef attributedStringAtts; float ascent, unitsToHeightScaleFactor; @@ -701,13 +703,12 @@ private: CTFontRef getCTFontFromTypeface (const Font& f) { - if (OSXTypeface* tf = dynamic_cast (f.getTypeface())) + if (OSXTypeface* tf = dynamic_cast (f.getTypeface())) return tf->ctFontRef; return 0; } - StringArray Font::findAllTypefaceNames() { StringArray names; @@ -912,7 +913,7 @@ public: #if SUPPORT_ONLY_10_4_FONTS HeapBlock advances (length); - [nsFont getAdvancements: advances forGlyphs: reinterpret_cast (glyphs.getData()) count: length]; + [nsFont getAdvancements: advances forGlyphs: reinterpret_cast (glyphs.getData()) count: length]; for (int i = 0; i < length; ++i) x += advances[i].width; @@ -940,7 +941,7 @@ public: return x * unitsToHeightScaleFactor; } - void getGlyphPositions (const String& text, Array & resultGlyphs, Array & xOffsets) override + void getGlyphPositions (const String& text, Array& resultGlyphs, Array& xOffsets) override { xOffsets.add (0); @@ -1152,8 +1153,8 @@ private: if (rangeOffset == 0) return delta + c; - else - return getValue16 (glyphIndexes, 2 * ((rangeOffset / 2) + (c - start) - (segCount - i))); + + return getValue16 (glyphIndexes, 2 * ((rangeOffset / 2) + (c - start) - (segCount - i))); } } @@ -1271,13 +1272,33 @@ Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font) return Typeface::createSystemTypefaceFor (newFont); } +#if JUCE_CORETEXT_AVAILABLE +static bool containsNoMemoryTypefaces (const AttributedString& text) +{ + const int numCharacterAttributes = text.getNumAttributes(); + + for (int i = 0; i < numCharacterAttributes; ++i) + if (const Font* const f = text.getAttribute (i)->getFont()) + if (OSXTypeface* tf = dynamic_cast (f->getTypeface())) + if (tf->isMemoryFont) + return false; + + return true; +} +#endif + bool TextLayout::createNativeLayout (const AttributedString& text) { #if JUCE_CORETEXT_AVAILABLE - CoreTextTypeLayout::createLayout (*this, text); - return true; - #else + // Seems to be an unfathomable bug in CoreText which prevents the layout working with + // typefaces that were loaded from memory, so have to fallback if we hit any of those.. + if (containsNoMemoryTypefaces (text)) + { + CoreTextTypeLayout::createLayout (*this, text); + return true; + } + #endif + (void) text; return false; - #endif }