diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2019-03-18 16:32:04 -0700 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2019-03-18 16:32:04 -0700 |
commit | cc670911b498761a1e30a92a90d46642ae1c0bdb (patch) | |
tree | 168d3f7ed64bd3bc8167ce6c328fabb1d276e6ac | |
parent | c2fe532b9bbc9db9b2bbab109f0f47f7cc9584d7 (diff) | |
download | qtlocation-mapboxgl-upstream/fabian-text-color-14102.tar.gz |
[ios, macos] Add text-color support to format expressions.upstream/fabian-text-color-14102
-rw-r--r-- | platform/darwin/src/MGLAttributedExpression.h | 34 | ||||
-rw-r--r-- | platform/darwin/src/MGLAttributedExpression.m | 13 | ||||
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 29 | ||||
-rw-r--r-- | platform/darwin/test/MGLExpressionTests.mm | 37 | ||||
-rw-r--r-- | platform/ios/src/UIColor+MGLAdditions.h | 1 |
5 files changed, 108 insertions, 6 deletions
diff --git a/platform/darwin/src/MGLAttributedExpression.h b/platform/darwin/src/MGLAttributedExpression.h index aa5d51c66e..c30739a348 100644 --- a/platform/darwin/src/MGLAttributedExpression.h +++ b/platform/darwin/src/MGLAttributedExpression.h @@ -6,10 +6,23 @@ typedef NSString * MGLAttributedExpressionKey NS_EXTENSIBLE_STRING_ENUM; FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontNamesAttribute; FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontSizeAttribute; +FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontColorAttribute; /** An `MGLAttributedExpression` object associates text formatting attibutes (such as font size or font names) to an `NSExpression`. + + ### Example + ```swift + let expression = NSExpression(forConstantValue: "Foo") + let attributes = [.fontNamesAttribute : ["DIN Offc Pro Italic", + "Arial Unicode MS Regular"], + .fontSizeAttribute: 1.2, + .fontColorAttribute: UIColor.red] + let attributedExpression = MGLAttributedExpression(expression, attributes:attributes) + + ``` + */ MGL_EXPORT @interface MGLAttributedExpression : NSObject @@ -19,9 +32,21 @@ MGL_EXPORT */ @property (strong, nonatomic) NSExpression *expression; +#if TARGET_OS_IPHONE +/** + The formatting attributes dictionary. + `MGLFontNamesAttribute` : `NSArray<NSString *>*` + `MGLFontSizeAttribute` : `NSNumber` + `MGLFontColorAttribute` : `UIColor` + */ +#else /** - The formatting attributes. + The formatting attributes dictionary. + `MGLFontNamesAttribute` : `NSArray<NSString *>*` + `MGLFontSizeAttribute` : `NSNumber` + `MGLFontColorAttribute` : `NSColor` */ +#endif @property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, id> *attributes; /** @@ -32,13 +57,18 @@ MGL_EXPORT /** Returns an `MGLAttributedExpression` object initialized with an expression and text format attributes. */ -- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nullable NSDictionary <MGLAttributedExpressionKey, id> *)attrs; +- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, id> *)attrs; /** Creates an `MGLAttributedExpression` object initialized with an expression and the format attributes for font names and font size. */ + (instancetype)attributedExpression:(NSExpression *)expression fontNames:(nullable NSArray<NSString*> *)fontNames fontSize:(nullable NSNumber *)fontSize; +/** + Creates an `MGLAttributedExpression` object initialized with an expression and the format attributes dictionary. + */ ++ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, id> *)attrs; + @end NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLAttributedExpression.m b/platform/darwin/src/MGLAttributedExpression.m index 715f74e42f..000701f7cf 100644 --- a/platform/darwin/src/MGLAttributedExpression.m +++ b/platform/darwin/src/MGLAttributedExpression.m @@ -3,11 +3,12 @@ const MGLAttributedExpressionKey MGLFontNamesAttribute = @"text-font"; const MGLAttributedExpressionKey MGLFontSizeAttribute = @"font-scale"; +const MGLAttributedExpressionKey MGLFontColorAttribute = @"text-color"; @implementation MGLAttributedExpression - (instancetype)initWithExpression:(NSExpression *)expression { - self = [self initWithExpression:expression attributes:nil]; + self = [self initWithExpression:expression attributes:@{}]; return self; } @@ -28,7 +29,15 @@ const MGLAttributedExpressionKey MGLFontSizeAttribute = @"font-scale"; return attributedExpression; } -- (instancetype)initWithExpression:(NSExpression *)expression attributes:(NSDictionary<MGLAttributedExpressionKey,id> *)attrs { ++ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey,id> *)attrs { + MGLAttributedExpression *attributedExpression; + + attributedExpression = [[self alloc] initWithExpression:expression attributes:attrs]; + + return attributedExpression; +} + +- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey,id> *)attrs { if (self = [super init]) { MGLLogInfo(@"Starting %@ initialization.", NSStringFromClass([self class])); diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index c4e2908888..130353d52e 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -878,9 +878,25 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { NSExpression *expression = [NSExpression expressionWithMGLJSONObject:argumentObjects[index]]; NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; if ((index + 1) < argumentObjects.count) { - attrs = argumentObjects[index + 1]; + attrs = [NSMutableDictionary dictionaryWithDictionary:argumentObjects[index + 1]]; } + if (attrs.count) { + if (attrs[MGLFontNamesAttribute]) { + NSArray *fontNames = (NSArray *)attrs[MGLFontNamesAttribute]; + attrs[MGLFontNamesAttribute] = fontNames[1]; + } + if (attrs[MGLFontColorAttribute] && [attrs[MGLFontColorAttribute] isKindOfClass:[NSArray class]]) { + NSArray *colorArray = attrs[MGLFontColorAttribute]; + if ([colorArray[0] isEqualToString:@"rgb"] || [colorArray[0] isEqualToString:@"rgba"]) { + NSArray *colorArguments = [colorArray subarrayWithRange:NSMakeRange(1, colorArray.count - 1)]; + NSArray *subexpressions = MGLSubexpressionsWithJSONObjects(colorArguments); + UIColor *color = [NSExpression mgl_colorWithRGBComponents:subexpressions]; + + attrs[MGLFontColorAttribute] = color; + } + } + } MGLAttributedExpression *attributedExpression = [[MGLAttributedExpression alloc] initWithExpression:expression attributes:attrs]; [attributedExpressions addObject:[NSExpression expressionForConstantValue:attributedExpression]]; @@ -1008,7 +1024,16 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { [attributes addObject:jsonObject]; } if (attributedExpression.attributes) { - [attributes addObject:attributedExpression.attributes]; + NSMutableDictionary *attributedDictionary = [NSMutableDictionary dictionaryWithDictionary:attributedExpression.attributes]; + if (attributedDictionary[MGLFontNamesAttribute]) { + attributedDictionary[MGLFontNamesAttribute] = @[@"literal", attributedDictionary[MGLFontNamesAttribute]]; + } + if (attributedDictionary[MGLFontColorAttribute] && [attributedDictionary[MGLFontColorAttribute] isKindOfClass:[MGLColor class]]) { + MGLColor *color = attributedDictionary[MGLFontColorAttribute]; + NSExpression *colorExpression = [NSExpression expressionForConstantValue:color]; + attributedDictionary[MGLFontColorAttribute] = colorExpression.mgl_jsonExpressionObject; + } + [attributes addObject:attributedDictionary]; } else { [attributes addObject:@{}]; } diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index 8b8a79f184..8e99ed596e 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -1050,6 +1050,43 @@ using namespace std::string_literals; XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression); } { + MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"] + attributes:@{ MGLFontSizeAttribute: @(1.2), + MGLFontColorAttribute: @"yellow"}] ; + NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)]; + + NSExpression *compatibilityExpression = [NSExpression expressionForFunction:@"mgl_attributed:" arguments:@[MGLConstantExpression(attribute1)]]; + NSArray *jsonExpression = @[ @"format", @"foo", @{ @"font-scale": @1.2, @"text-color": @"yellow" } ]; + XCTAssertEqualObjects(compatibilityExpression.mgl_jsonExpressionObject, expression.mgl_jsonExpressionObject); + XCTAssertEqualObjects(compatibilityExpression, expression); + XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); + XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression); + } + { + MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"] + attributes:@{ MGLFontSizeAttribute: @(1.2), + MGLFontColorAttribute: @"yellow", + MGLFontNamesAttribute: @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ] + }] ; + NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)]; + + NSArray *jsonExpression = @[ @"format", @"foo", @{ @"font-scale": @1.2, @"text-color": @"yellow" , @"text-font" : @[ @"literal", @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]]} ]; + XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); + XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression); + } + { + MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"] + attributes:@{ MGLFontSizeAttribute: @(1.2), + MGLFontColorAttribute: [MGLColor redColor], + MGLFontNamesAttribute: @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ] + }] ; + NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)]; + + NSArray *jsonExpression = @[ @"format", @"foo", @{ @"font-scale": @1.2, @"text-color": @[@"rgb", @255, @0, @0] , @"text-font" : @[ @"literal", @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]]} ]; + XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); + XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression); + } + { MGLAttributedExpression *attribute1 = [MGLAttributedExpression attributedExpression:[NSExpression expressionForConstantValue:@"foo"] fontNames:nil fontSize:@(1.2)]; diff --git a/platform/ios/src/UIColor+MGLAdditions.h b/platform/ios/src/UIColor+MGLAdditions.h index 60cfe1c58b..19702fa105 100644 --- a/platform/ios/src/UIColor+MGLAdditions.h +++ b/platform/ios/src/UIColor+MGLAdditions.h @@ -17,5 +17,6 @@ + (NSExpression *)mgl_expressionForRGBComponents:(NSArray<NSExpression *> *)components; + (NSExpression *)mgl_expressionForRGBAComponents:(NSArray<NSExpression *> *)components; ++ (UIColor *)mgl_colorWithRGBComponents:(NSArray<NSExpression *> *)components; @end |