Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos] Allow specifying multiple fonts or font families for loc…
Browse files Browse the repository at this point in the history
…al 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.
  • Loading branch information
1ec5 committed Mar 1, 2020
1 parent 55d5b97 commit fe73020
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 25 additions & 6 deletions platform/darwin/src/local_glyph_rasterizer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ CTFontDescriptor configuration option (although we'd have to override font size
using CGContextHandle = CFHandle<CGContextRef, CGContextRef, CGContextRelease>;
using CFStringRefHandle = CFHandle<CFStringRef, CFTypeRef, CFRelease>;
using CFAttributedStringRefHandle = CFHandle<CFAttributedStringRef, CFTypeRef, CFRelease>;
using CFMutableArrayRefHandle = CFHandle<CFMutableArrayRef, CFTypeRef, CFRelease>;
using CFDictionaryRefHandle = CFHandle<CFDictionaryRef, CFTypeRef, CFRelease>;
using CTFontRefHandle = CFHandle<CTFontRef, CFTypeRef, CFRelease>;
using CTFontDescriptorRefHandle = CFHandle<CTFontDescriptorRef, CFTypeRef, CFRelease>;
using CTLineRefHandle = CFHandle<CTLineRef, CFTypeRef, CFRelease>;

Expand All @@ -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<NSString *> *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");
Expand Down

0 comments on commit fe73020

Please sign in to comment.