From fe73020b4a922781438a4f52d3d149b0347cec88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Sun, 1 Mar 2020 03:32:40 -0800 Subject: [PATCH] [ios, macos] Allow specifying multiple fonts or font families for local font rendering mbgl::Renderer and mbgl::MapSnapshotter can now contain a list of font family names, font display names, and font PostScript names, each name separated by a newline. --- CHANGELOG.md | 4 +++ platform/darwin/src/local_glyph_rasterizer.mm | 31 +++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0c5d2c01fb..48cca4b8bbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master +- [ios, macos] Allow specifying multiple fonts or font families for local font rendering ([#16253](https://github.com/mapbox/mapbox-gl-native/pull/16253)) + + The `localFontFamily` parameter of `mbgl::Renderer::Renderer()` and `mbgl::MapSnapshotter::MapSnapshotter()` can now contain a list of font family names, font display names, and font PostScript names, each name separated by a newline. + ## maps-v1.3.0 (2020.02-relvanillashake) ### 🐞 Bug fixes diff --git a/platform/darwin/src/local_glyph_rasterizer.mm b/platform/darwin/src/local_glyph_rasterizer.mm index 14cee5063e5..62d4943fb8e 100644 --- a/platform/darwin/src/local_glyph_rasterizer.mm +++ b/platform/darwin/src/local_glyph_rasterizer.mm @@ -45,7 +45,9 @@ CTFontDescriptor configuration option (although we'd have to override font size using CGContextHandle = CFHandle; using CFStringRefHandle = CFHandle; using CFAttributedStringRefHandle = CFHandle; +using CFMutableArrayRefHandle = CFHandle; using CFDictionaryRefHandle = CFHandle; +using CTFontRefHandle = CFHandle; using CTFontDescriptorRefHandle = CFHandle; using CTLineRefHandle = CFHandle; @@ -62,19 +64,36 @@ CTFontDescriptor configuration option (although we'd have to override font size } } - CTFontRef getFont() { if (!fontFamily) { return NULL; } if (!fontHandle) { - NSDictionary *fontAttributes = @{ - (NSString *)kCTFontSizeAttribute: [NSNumber numberWithFloat:24.0], - (NSString *)kCTFontFamilyNameAttribute: [[NSString alloc] initWithCString:fontFamily->c_str() encoding:NSUTF8StringEncoding] - }; + NSArray *fontFamilyNames = [@(fontFamily->c_str()) componentsSeparatedByString:@"\n"]; + CFMutableArrayRefHandle fontDescriptors(CFArrayCreateMutable(kCFAllocatorDefault, fontFamilyNames.count, &kCFTypeArrayCallBacks)); + for (NSString *name in fontFamilyNames) { + NSDictionary *fontAttributes = @{ + (NSString *)kCTFontSizeAttribute: @(24.0), + (NSString *)kCTFontNameAttribute: name, + (NSString *)kCTFontDisplayNameAttribute: name, + (NSString *)kCTFontFamilyNameAttribute: name, + }; + + CTFontDescriptorRefHandle descriptor(CTFontDescriptorCreateWithAttributes((CFDictionaryRef)fontAttributes)); + CFArrayAppendValue(*fontDescriptors, *descriptor); + } + + CFStringRef keys[] = { kCTFontSizeAttribute, kCTFontCascadeListAttribute }; + CFTypeRef values[] = { (__bridge CFNumberRef)@(24.0), *fontDescriptors }; - CTFontDescriptorRefHandle descriptor(CTFontDescriptorCreateWithAttributes((CFDictionaryRef)fontAttributes)); + CFDictionaryRefHandle attributes( + CFDictionaryCreate(kCFAllocatorDefault, (const void**)&keys, + (const void**)&values, sizeof(keys) / sizeof(keys[0]), + &kCFTypeDictionaryKeyCallBacks, + &kCFTypeDictionaryValueCallBacks)); + + CTFontDescriptorRefHandle descriptor(CTFontDescriptorCreateWithAttributes(*attributes)); fontHandle = CTFontCreateWithFontDescriptor(*descriptor, 0.0, NULL); if (!fontHandle) { throw std::runtime_error("CTFontCreateWithFontDescriptor failed");