From a869e9c4dee46a67ee64a64c1d668222050e09ad Mon Sep 17 00:00:00 2001 From: Chris Loer Date: Wed, 29 Nov 2017 12:54:58 -0800 Subject: Implement font stack-based heuristics for font loading. In my tests so far, I have yet to get CoreText to render anything differently based on the font weight changes. --- platform/darwin/src/local_glyph_rasterizer.mm | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/platform/darwin/src/local_glyph_rasterizer.mm b/platform/darwin/src/local_glyph_rasterizer.mm index 3d30bdae48..fde0193b86 100644 --- a/platform/darwin/src/local_glyph_rasterizer.mm +++ b/platform/darwin/src/local_glyph_rasterizer.mm @@ -1,5 +1,6 @@ #include #include +#include #include @@ -62,9 +63,14 @@ public: } if (fontHandles.find(fontStack) == fontHandles.end()) { + + NSDictionary* fontTraits = @{ (NSString *)kCTFontSymbolicTrait: [NSNumber numberWithFloat:getFontWeight(fontStack)] }; + NSDictionary *fontAttributes = @{ (NSString *)kCTFontSizeAttribute: [NSNumber numberWithFloat:24.0], - (NSString *)kCTFontFamilyNameAttribute: [[NSString alloc] initWithCString:fontFamily->c_str() encoding:NSUTF8StringEncoding] + (NSString *)kCTFontFamilyNameAttribute: [[NSString alloc] initWithCString:fontFamily->c_str() encoding:NSUTF8StringEncoding], + (NSString *)kCTFontTraitsAttribute: fontTraits + //(NSString *)kCTFontStyleNameAttribute: (getFontWeight(fontStack) > .3) ? @"Bold" : @"Regular" }; CTFontDescriptorRefHandle descriptor(CTFontDescriptorCreateWithAttributes((CFDictionaryRef)fontAttributes)); @@ -79,6 +85,30 @@ public: } private: + float getFontWeight(const FontStack& fontStack) { + // Analog to logic in glyph_manager.js + // From NSFontDescriptor.h (macOS 10.11+) NSFontWeight*: + constexpr float light = -.4; + constexpr float regular = 0.0; + constexpr float medium = .23; + constexpr float bold = .4; + + float fontWeight = regular; + for (auto font : fontStack) { + // Last font in the fontstack "wins" + std::string lowercaseFont = mbgl::platform::lowercase(font); + if (lowercaseFont.find("bold") != std::string::npos) { + fontWeight = bold; + } else if (lowercaseFont.find("medium") != std::string::npos) { + fontWeight = medium; + } else if (lowercaseFont.find("light") != std::string::npos) { + fontWeight = light; + } + } + + return fontWeight; + } + std::unordered_map fontHandles; optional fontFamily; }; -- cgit v1.2.1