From 5d009a2b737c01d97be76dc0a2ec64952598f15f Mon Sep 17 00:00:00 2001 From: jules Date: Sun, 10 Jun 2012 14:39:23 +0100 Subject: [PATCH] Workaround for an Apple font bug in 10.6. --- .../juce_graphics/native/juce_mac_Fonts.mm | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/modules/juce_graphics/native/juce_mac_Fonts.mm b/modules/juce_graphics/native/juce_mac_Fonts.mm index f62f915cda..ad80702b76 100644 --- a/modules/juce_graphics/native/juce_mac_Fonts.mm +++ b/modules/juce_graphics/native/juce_mac_Fonts.mm @@ -43,8 +43,38 @@ namespace CoreTextTypeLayout return style; } - static CTFontRef createCTFont (const Font& font, const float fontSize, - const bool applyScaleFactor) + // Workaround for Apple bug in CTFontCreateWithFontDescriptor in Garageband/Logic on 10.6 + #if JUCE_MAC && ((! defined (MAC_OS_X_VERSION_10_7)) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7) + static CTFontRef getFontWithTrait (CTFontRef ctFontRef, CTFontSymbolicTraits trait) + { + CTFontRef newFont = CTFontCreateCopyWithSymbolicTraits (ctFontRef, 0.0f, nullptr, trait, trait); + if (newFont == nullptr) + return ctFontRef; + + CFRelease (ctFontRef); + return newFont; + } + + static CTFontRef useStyleFallbackIfNecessary (CTFontRef ctFontRef, CFStringRef cfFontFamily, + const float fontSize, const Font& font) + { + CFStringRef cfActualFontFamily = (CFStringRef) CTFontCopyAttribute (ctFontRef, kCTFontFamilyNameAttribute); + + if (CFStringCompare (cfFontFamily, cfActualFontFamily, 0) != kCFCompareEqualTo) + { + CFRelease (ctFontRef); + ctFontRef = CTFontCreateWithName (cfFontFamily, fontSize, nullptr); + + if (font.isItalic()) ctFontRef = getFontWithTrait (ctFontRef, kCTFontItalicTrait); + if (font.isBold()) ctFontRef = getFontWithTrait (ctFontRef, kCTFontBoldTrait); + } + + CFRelease (cfActualFontFamily); + return ctFontRef; + } + #endif + + static CTFontRef createCTFont (const Font& font, const float fontSize, const bool applyScaleFactor) { CFStringRef cfFontFamily = FontStyleHelpers::getConcreteFamilyName (font).toCFString(); CFStringRef cfFontStyle = findBestAvailableStyle (font.getTypefaceName(), @@ -58,7 +88,6 @@ namespace CoreTextTypeLayout &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFRelease (cfFontStyle); - CFRelease (cfFontFamily); CTFontDescriptorRef ctFontDescRef = CTFontDescriptorCreateWithAttributes (fontDescAttributes); CFRelease (fontDescAttributes); @@ -66,6 +95,12 @@ namespace CoreTextTypeLayout CTFontRef ctFontRef = CTFontCreateWithFontDescriptor (ctFontDescRef, fontSize, nullptr); CFRelease (ctFontDescRef); + #if JUCE_MAC && ((! defined (MAC_OS_X_VERSION_10_7)) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7) + ctFontRef = useStyleFallbackIfNecessary (ctFontRef, cfFontFamily, fontSize, font); + #endif + + CFRelease (cfFontFamily); + if (applyScaleFactor) { CGFontRef cgFontRef = CTFontCopyGraphicsFont (ctFontRef, nullptr);