From 2367d648f45dc6b37fd1ba3816320bcdc06343de Mon Sep 17 00:00:00 2001 From: Tom Poole Date: Thu, 25 Mar 2021 15:45:28 +0000 Subject: [PATCH] Fixed an issue rendering AttributedStrings containing horizontal font scale --- .../native/juce_mac_CoreGraphicsContext.mm | 3 +-- .../juce_graphics/native/juce_mac_Fonts.mm | 22 ++++++++++++++++--- .../juce_win32_DirectWriteTypeLayout.cpp | 10 ++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm b/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm index 4cc4439026..6d6cdf5d00 100644 --- a/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm +++ b/modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm @@ -676,8 +676,7 @@ void CoreGraphicsContext::drawGlyph (int glyphNumber, const AffineTransform& tra bool CoreGraphicsContext::drawTextLayout (const AttributedString& text, const Rectangle& area) { - CoreTextTypeLayout::drawToCGContext (text, area, context.get(), (float) flipHeight); - return true; + return CoreTextTypeLayout::drawToCGContext (text, area, context.get(), (float) flipHeight); } CoreGraphicsContext::SavedState::SavedState() diff --git a/modules/juce_graphics/native/juce_mac_Fonts.mm b/modules/juce_graphics/native/juce_mac_Fonts.mm index cafe3e4546..c54e0144c8 100644 --- a/modules/juce_graphics/native/juce_mac_Fonts.mm +++ b/modules/juce_graphics/native/juce_mac_Fonts.mm @@ -324,9 +324,23 @@ namespace CoreTextTypeLayout return range.getLength(); } - static void drawToCGContext (const AttributedString& text, const Rectangle& area, + static bool areAllFontsDefaultWidth (const AttributedString& text) + { + auto numCharacterAttributes = text.getNumAttributes(); + + for (int i = 0; i < numCharacterAttributes; ++i) + if (text.getAttribute (i).font.getHorizontalScale() != 1.0f) + return false; + + return true; + } + + static bool drawToCGContext (const AttributedString& text, const Rectangle& area, const CGContextRef& context, float flipHeight) { + if (! areAllFontsDefaultWidth (text)) + return false; + auto framesetter = createCTFramesetter (text); // Ugly hack to fix a bug in OS X Sierra where the CTFrame needs to be slightly @@ -377,6 +391,8 @@ namespace CoreTextTypeLayout CGContextRestoreGState (context); CGContextSetTextMatrix (context, textMatrix); + + return true; } static void createLayout (TextLayout& glyphLayout, const AttributedString& text) @@ -817,7 +833,7 @@ static bool canAllTypefacesBeUsedInLayout (const AttributedString& text) for (int i = 0; i < numCharacterAttributes; ++i) { - if (auto tf = dynamic_cast (text.getAttribute(i).font.getTypeface())) + if (auto tf = dynamic_cast (text.getAttribute (i).font.getTypeface())) if (tf->canBeUsedForLayout) continue; @@ -829,7 +845,7 @@ static bool canAllTypefacesBeUsedInLayout (const AttributedString& text) bool TextLayout::createNativeLayout (const AttributedString& text) { - if (canAllTypefacesBeUsedInLayout (text)) + if (canAllTypefacesBeUsedInLayout (text) && CoreTextTypeLayout::areAllFontsDefaultWidth (text)) { CoreTextTypeLayout::createLayout (*this, text); return true; diff --git a/modules/juce_graphics/native/juce_win32_DirectWriteTypeLayout.cpp b/modules/juce_graphics/native/juce_win32_DirectWriteTypeLayout.cpp index 2e9875e323..a8ded0c6d0 100644 --- a/modules/juce_graphics/native/juce_win32_DirectWriteTypeLayout.cpp +++ b/modules/juce_graphics/native/juce_win32_DirectWriteTypeLayout.cpp @@ -419,13 +419,17 @@ namespace DirectWriteTypeLayout } } -static bool canAllTypefacesBeUsedInLayout (const AttributedString& text) +static bool canAllTypefacesAndFontsBeUsedInLayout (const AttributedString& text) { auto numCharacterAttributes = text.getNumAttributes(); for (int i = 0; i < numCharacterAttributes; ++i) - if (dynamic_cast (text.getAttribute(i).font.getTypeface()) == nullptr) + { + const auto& font = text.getAttribute (i).font; + + if (font.getHorizontalScale() != 1.0f || dynamic_cast (font.getTypeface()) == nullptr) return false; + } return true; } @@ -435,7 +439,7 @@ static bool canAllTypefacesBeUsedInLayout (const AttributedString& text) bool TextLayout::createNativeLayout (const AttributedString& text) { #if JUCE_USE_DIRECTWRITE - if (! canAllTypefacesBeUsedInLayout (text)) + if (! canAllTypefacesAndFontsBeUsedInLayout (text)) return false; SharedResourcePointer factories;