diff options
author | Chris Loer <chris.loer@gmail.com> | 2017-11-29 12:54:58 -0800 |
---|---|---|
committer | Chris Loer <chris.loer@gmail.com> | 2017-11-29 13:51:19 -0800 |
commit | a869e9c4dee46a67ee64a64c1d668222050e09ad (patch) | |
tree | dab394389e16e8616d1fb5d2dec56acd3802e223 | |
parent | bb9e480c4f4f146943e4e3a56a062e4c289bb5d3 (diff) | |
download | qtlocation-mapboxgl-a869e9c4dee46a67ee64a64c1d668222050e09ad.tar.gz |
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.
-rw-r--r-- | platform/darwin/src/local_glyph_rasterizer.mm | 32 |
1 files changed, 31 insertions, 1 deletions
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 <mbgl/text/local_glyph_rasterizer.hpp> #include <mbgl/util/i18n.hpp> +#include <mbgl/util/platform.hpp> #include <unordered_map> @@ -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<FontStack, CTFontRef, FontStackHash> fontHandles; optional<std::string> fontFamily; }; |