summaryrefslogtreecommitdiff
path: root/platform/darwin
diff options
context:
space:
mode:
Diffstat (limited to 'platform/darwin')
-rw-r--r--platform/darwin/src/MGLRendererConfiguration.h23
-rw-r--r--platform/darwin/src/MGLRendererConfiguration.mm51
-rw-r--r--platform/darwin/test/MGLRendererConfigurationTests.mm105
3 files changed, 166 insertions, 13 deletions
diff --git a/platform/darwin/src/MGLRendererConfiguration.h b/platform/darwin/src/MGLRendererConfiguration.h
index ee5aaef174..bfe88b7209 100644
--- a/platform/darwin/src/MGLRendererConfiguration.h
+++ b/platform/darwin/src/MGLRendererConfiguration.h
@@ -23,14 +23,21 @@ MGL_EXPORT
/** The cache dir to use. */
@property (nonatomic, readonly) mbgl::optional<std::string> cacheDir;
-/** The name of the font family to use for client-side text rendering.
-
- Currently only used for CJK glyphs. Changing this at run time is not currently
- supported. Enable client-side rendering of CJK glyphs by setting
- `MGLIdeographicFontFamilyName` in your containing app's Info.plist to a value
- which will be available at run time. Default font for local ideograph font family
- is "PingFang". */
-@property (nonatomic, readonly) std::string localFontFamilyName;
+/** The name of the font family to use for client-side text rendering of CJK ideographs.
+
+ Set MGLIdeographicFontFamilyName in your containing application's Info.plist to
+ font family name(s) that will be available at run time, such as “PingFang TC”
+ or “Marker Felt”. This plist key accepts:
+
+ - A string value of a single font family name.
+
+ - An array of font family names. Fonts will be used in the defined order,
+ eventually falling back to default system font if none are available.
+
+ - A boolean value NO to disable client-side rendering of CJK glyphs —
+ remote fonts specified in your style will be used instead.
+ */
+@property (nonatomic, readonly) mbgl::optional<std::string> localFontFamilyName;
/**
A Boolean value indicating whether symbol layers may enable per-source symbol
diff --git a/platform/darwin/src/MGLRendererConfiguration.mm b/platform/darwin/src/MGLRendererConfiguration.mm
index d616e93eb2..54bdcaa691 100644
--- a/platform/darwin/src/MGLRendererConfiguration.mm
+++ b/platform/darwin/src/MGLRendererConfiguration.mm
@@ -9,6 +9,7 @@
#endif
static NSString * const MGLCollisionBehaviorPre4_0Key = @"MGLCollisionBehaviorPre4_0";
+static NSString * const MGLIdeographicFontFamilyNameKey = @"MGLIdeographicFontFamilyName";
@interface MGLRendererConfiguration ()
@property (nonatomic, readwrite) BOOL perSourceCollisions;
@@ -69,10 +70,54 @@ static NSString * const MGLCollisionBehaviorPre4_0Key = @"MGLCollisionBehaviorPr
return mbgl::optional<std::string>();
}
-- (std::string)localFontFamilyName {
- NSString *fontFamilyName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"MGLIdeographicFontFamilyName"];
+- (mbgl::optional<std::string>)localFontFamilyName {
+ return [self _localFontFamilyNameWithPropertyDictionary:[[NSBundle mainBundle] infoDictionary]];
+}
- return fontFamilyName ? std::string([fontFamilyName UTF8String]) : std::string("PingFang");
+- (mbgl::optional<std::string>)_localFontFamilyNameWithPropertyDictionary:(nonnull NSDictionary *)properties {
+
+ std::string systemFontFamilyName;
+#if TARGET_OS_IPHONE
+ systemFontFamilyName = std::string([[UIFont systemFontOfSize:0 weight:UIFontWeightRegular].familyName UTF8String]);
+#else
+ systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]);
+#endif
+
+ id fontFamilyName = properties[MGLIdeographicFontFamilyNameKey];
+
+ if([fontFamilyName isKindOfClass:[NSNumber class]] && ![fontFamilyName boolValue])
+ {
+ return mbgl::optional<std::string>();
+ }
+ else if([fontFamilyName isKindOfClass:[NSString class]])
+ {
+ BOOL isValidFont = NO;
+#if TARGET_OS_IPHONE
+ if([[UIFont familyNames] containsObject:fontFamilyName]){
+ isValidFont = YES;
+ }
+#else
+ if([[[NSFontManager sharedFontManager] availableFontFamilies] containsObject:fontFamilyName]){
+ isValidFont = YES;
+ }
+#endif
+ return (fontFamilyName && isValidFont) ? std::string([fontFamilyName UTF8String]) : systemFontFamilyName;
+ }
+ // Ability to specify an array of fonts for fallbacks for `localIdeographicFontFamily`
+ else if ([fontFamilyName isKindOfClass:[NSArray class]]){
+ for(NSString *name in fontFamilyName){
+#if TARGET_OS_IPHONE
+ if([[UIFont familyNames] containsObject:name]){
+ return std::string([name UTF8String]);
+ }
+#else
+ if([[[NSFontManager sharedFontManager] availableFontFamilies] containsObject:name]){
+ return std::string([name UTF8String]);
+ }
+#endif
+ }
+ }
+ return systemFontFamilyName;
}
@end
diff --git a/platform/darwin/test/MGLRendererConfigurationTests.mm b/platform/darwin/test/MGLRendererConfigurationTests.mm
index a0c630ebb5..6483aa3587 100644
--- a/platform/darwin/test/MGLRendererConfigurationTests.mm
+++ b/platform/darwin/test/MGLRendererConfigurationTests.mm
@@ -6,6 +6,7 @@ static NSString * const MGLRendererConfigurationTests_collisionBehaviorKey = @"M
@interface MGLRendererConfiguration (Tests)
- (instancetype)initWithPropertyDictionary:(nonnull NSDictionary*)bundle;
+- (mbgl::optional<std::string>)_localFontFamilyNameWithPropertyDictionary:(nonnull NSDictionary *)properties;
@end
@@ -77,14 +78,12 @@ static NSString * const MGLRendererConfigurationTests_collisionBehaviorKey = @"M
}
- (void)testOverridingMGLCollisionBehaviorPre40 {
-
// Dictionary = NO, NSUserDefaults = YES
{
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:MGLRendererConfigurationTests_collisionBehaviorKey];
MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] initWithPropertyDictionary:@{MGLRendererConfigurationTests_collisionBehaviorKey:@(NO)}];
XCTAssert(config.perSourceCollisions);
}
-
// Dictionary = YES, NSUserDefaults = NO
{
[[NSUserDefaults standardUserDefaults] setObject:@(NO) forKey:MGLRendererConfigurationTests_collisionBehaviorKey];
@@ -93,4 +92,106 @@ static NSString * const MGLRendererConfigurationTests_collisionBehaviorKey = @"M
}
}
+- (void)testDefaultLocalFontFamilyName {
+
+ MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] init];
+ std::string localFontFamilyName = config.localFontFamilyName.value();
+
+ std::string systemFontFamilyName;
+#if TARGET_OS_IPHONE
+ systemFontFamilyName = std::string([[UIFont systemFontOfSize:0 weight:UIFontWeightRegular].familyName UTF8String]);
+#else
+ systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]);
+#endif
+
+ XCTAssertEqual(localFontFamilyName, systemFontFamilyName, @"Default local font family name should match default system font");
+}
+
+- (void)testSettingMGLIdeographicFontFamilyNameWithPlistValue {
+
+ MGLRendererConfiguration *config = [[MGLRendererConfiguration alloc] init];
+ NSDictionary *dic;
+
+ // `MGLIdeographicFontFamilyName` set to bool value `YES`
+ {
+ dic = @{@"MGLIdeographicFontFamilyName": @(YES)};
+ std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value();
+
+ std::string systemFontFamilyName;
+#if TARGET_OS_IPHONE
+ systemFontFamilyName = std::string([[UIFont systemFontOfSize:0 weight:UIFontWeightRegular].familyName UTF8String]);
+#else
+ systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]);
+#endif
+ XCTAssertEqual(localFontFamilyName, systemFontFamilyName, @"Local font family name should match default system font name when setting `YES`");
+ }
+
+ // `MGLIdeographicFontFamilyName` set to bool value `NO`
+ {
+ dic = @{@"MGLIdeographicFontFamilyName": @(NO)};
+ mbgl::optional<std::string> localFontFamilyName = [config _localFontFamilyNameWithPropertyDictionary:dic];
+ XCTAssertFalse(localFontFamilyName.has_value(), @"Client rendering font should use remote font when setting `NO`");
+ }
+
+ // `MGLIdeographicFontFamilyName` set to a valid font string value
+ {
+ dic = @{@"MGLIdeographicFontFamilyName": @"PingFang TC"};
+ std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value();
+ std::string targetFontFamilyName = std::string([@"PingFang TC" UTF8String]);
+ XCTAssertEqual(localFontFamilyName, targetFontFamilyName, @"Local font family name should match a custom valid font name");
+ }
+
+ // `MGLIdeographicFontFamilyName` set to an invalid font string value
+ {
+ dic = @{@"MGLIdeographicFontFamilyName": @"test font"};
+ std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value();
+
+ std::string systemFontFamilyName;
+#if TARGET_OS_IPHONE
+ systemFontFamilyName = std::string([[UIFont systemFontOfSize:0 weight:UIFontWeightRegular].familyName UTF8String]);
+#else
+ systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]);
+#endif
+ XCTAssertEqual(localFontFamilyName, systemFontFamilyName, @"Local font family name should match default system font name when setting an invalid font string");
+ }
+
+ // `MGLIdeographicFontFamilyName` set to a valid font family names array value
+ {
+ dic = @{@"MGLIdeographicFontFamilyName": @[@"test font 1", @"PingFang TC", @"test font 2"]};
+ std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value();
+ std::string targetFontFamilyName = std::string([@"PingFang TC" UTF8String]);
+ XCTAssertEqual(localFontFamilyName, targetFontFamilyName, @"Local font family name should match a custom valid font name in a font family names array");
+ }
+
+ // `MGLIdeographicFontFamilyName` set to an invalid font family names array value
+ {
+ dic = @{@"MGLIdeographicFontFamilyName": @[@"test font 1", @"test font 2", @"test font 3"]};
+ std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value();
+
+ std::string systemFontFamilyName;
+#if TARGET_OS_IPHONE
+ systemFontFamilyName = std::string([[UIFont systemFontOfSize:0 weight:UIFontWeightRegular].familyName UTF8String]);
+#else
+ systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]);
+#endif
+ XCTAssertEqual(localFontFamilyName, systemFontFamilyName, @"Local font family name should match default system font name when setting an invalid font family names array");
+ }
+
+ // `MGLIdeographicFontFamilyName` set to an invalid value type: NSDictionary, NSNumber, NSData, etc.
+ {
+ dic = @{@"MGLIdeographicFontFamilyName": [@"test font 1" dataUsingEncoding:NSUTF8StringEncoding]};
+ std::string localFontFamilyName = ([config _localFontFamilyNameWithPropertyDictionary:dic]).value();
+
+ std::string systemFontFamilyName;
+#if TARGET_OS_IPHONE
+ systemFontFamilyName = std::string([[UIFont systemFontOfSize:0 weight:UIFontWeightRegular].familyName UTF8String]);
+#else
+ systemFontFamilyName = std::string([[NSFont systemFontOfSize:0 weight:NSFontWeightRegular].familyName UTF8String]);
+#endif
+ XCTAssertEqual(localFontFamilyName, systemFontFamilyName, @"Local font family name should match default system font name when setting an invalid value type");
+ }
+}
+
+
+
@end