summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Guerra <fabian.guerra@mapbox.com>2019-03-21 17:34:57 -0700
committerFabian Guerra <fabian.guerra@mapbox.com>2019-03-21 17:34:57 -0700
commit429481c805b0e52b518b1d3bfa0f5e24451f9d4c (patch)
tree99bd91e12abcd98db53679a3a4265daedc4b3db0
parentd6727c0bb9e888d4cdf72a09143e3fb2354d04dd (diff)
downloadqtlocation-mapboxgl-upstream/fabian-format-exp-subexp-support.tar.gz
[ios, macos] Support expressions in formatting parameters.upstream/fabian-format-exp-subexp-support
-rw-r--r--platform/darwin/docs/guides/Predicates and Expressions.md6
-rw-r--r--platform/darwin/src/MGLAttributedExpression.h28
-rw-r--r--platform/darwin/src/MGLAttributedExpression.m8
-rw-r--r--platform/darwin/src/NSExpression+MGLAdditions.mm27
-rw-r--r--platform/darwin/test/MGLDocumentationExampleTests.swift8
-rw-r--r--platform/darwin/test/MGLExpressionTests.mm26
6 files changed, 47 insertions, 56 deletions
diff --git a/platform/darwin/docs/guides/Predicates and Expressions.md b/platform/darwin/docs/guides/Predicates and Expressions.md
index bb2e6c2d0e..87c0b7bd75 100644
--- a/platform/darwin/docs/guides/Predicates and Expressions.md
+++ b/platform/darwin/docs/guides/Predicates and Expressions.md
@@ -550,9 +550,9 @@ with the `MGLSymbolStyleLayer.text` property.
Key | Value Type
--- | ---
- `MGLFontNamesAttribute` | `NSArray<NSString *>*`
- `MGLFontScaleAttribute` | `NSNumber`
- `MGLFontColorAttribute` | `UIColor` or `NSColor` on macos
+ `MGLFontNamesAttribute` | `NSExpression`
+ `MGLFontScaleAttribute` | `NSExpression`
+ `MGLFontColorAttribute` | `NSExpression`
This function corresponds to the
[`format`](https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-types-format)
diff --git a/platform/darwin/src/MGLAttributedExpression.h b/platform/darwin/src/MGLAttributedExpression.h
index 3f8bef49c2..3e22d8b771 100644
--- a/platform/darwin/src/MGLAttributedExpression.h
+++ b/platform/darwin/src/MGLAttributedExpression.h
@@ -16,10 +16,10 @@ FOUNDATION_EXTERN MGL_EXPORT MGLAttributedExpressionKey const MGLFontColorAttrib
```swift
let redColor = UIColor.red
let expression = NSExpression(forConstantValue: "Foo")
- let attributes: Dictionary<MGLAttributedExpressionKey, Any> = [.fontNamesAttribute : ["DIN Offc Pro Italic",
- "Arial Unicode MS Regular"],
- .fontScaleAttribute: 1.2,
- .fontColorAttribute: redColor]
+ let attributes: Dictionary<MGLAttributedExpressionKey, NSExpression> = [.fontNamesAttribute : NSExpression(forConstantValue: ["DIN Offc Pro Italic",
+ "Arial Unicode MS Regular"]),
+ .fontScaleAttribute: NSExpression(forConstantValue: 1.2),
+ .fontColorAttribute: NSExpression(forConstantValue: redColor)]
let attributedExpression = MGLAttributedExpression(expression, attributes:attributes)
```
@@ -37,22 +37,22 @@ MGL_EXPORT
The formatting attributes dictionary.
Key | Value Type
--- | ---
- `MGLFontNamesAttribute` | `NSArray<NSString *>*`
- `MGLFontScaleAttribute` | `NSNumber`
- `MGLFontColorAttribute` | `UIColor`
+ `MGLFontNamesAttribute` | `NSExpression`
+ `MGLFontScaleAttribute` | `NSExpression`
+ `MGLFontColorAttribute` | `NSExpression`
*/
-@property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, id> *attributes;
+@property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, NSExpression *> *attributes;
#else
/**
The formatting attributes dictionary.
Key | Value Type
--- | ---
- `MGLFontNamesAttribute` | `NSArray<NSString *>*`
- `MGLFontScaleAttribute` | `NSNumber`
- `MGLFontColorAttribute` | `NSColor`
+ `MGLFontNamesAttribute` | `NSExpression`
+ `MGLFontScaleAttribute` | `NSExpression`
+ `MGLFontColorAttribute` | `NSExpression`
*/
-@property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, id> *attributes;
+@property (strong, nonatomic, readonly) NSDictionary<MGLAttributedExpressionKey, NSExpression *> *attributes;
#endif
@@ -64,7 +64,7 @@ MGL_EXPORT
/**
Returns an `MGLAttributedExpression` object initialized with an expression and text format attributes.
*/
-- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, id> *)attrs;
+- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, NSExpression *> *)attrs;
/**
Creates an `MGLAttributedExpression` object initialized with an expression and the format attributes for font names and font size.
@@ -74,7 +74,7 @@ MGL_EXPORT
/**
Creates an `MGLAttributedExpression` object initialized with an expression and the format attributes dictionary.
*/
-+ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, id> *)attrs;
++ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary <MGLAttributedExpressionKey, NSExpression *> *)attrs;
@end
diff --git a/platform/darwin/src/MGLAttributedExpression.m b/platform/darwin/src/MGLAttributedExpression.m
index 4283e98b94..a34480a957 100644
--- a/platform/darwin/src/MGLAttributedExpression.m
+++ b/platform/darwin/src/MGLAttributedExpression.m
@@ -18,18 +18,18 @@ const MGLAttributedExpressionKey MGLFontColorAttribute = @"text-color";
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
if (fontNames && fontNames.count > 0) {
- attrs[MGLFontNamesAttribute] = fontNames;
+ attrs[MGLFontNamesAttribute] = [NSExpression expressionForConstantValue:fontNames];
}
if (fontScale) {
- attrs[MGLFontScaleAttribute] = fontScale;
+ attrs[MGLFontScaleAttribute] = [NSExpression expressionForConstantValue:fontScale];
}
attributedExpression = [[self alloc] initWithExpression:expression attributes:attrs];
return attributedExpression;
}
-+ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey,id> *)attrs {
++ (instancetype)attributedExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey, NSExpression *> *)attrs {
MGLAttributedExpression *attributedExpression;
attributedExpression = [[self alloc] initWithExpression:expression attributes:attrs];
@@ -37,7 +37,7 @@ const MGLAttributedExpressionKey MGLFontColorAttribute = @"text-color";
return attributedExpression;
}
-- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey,id> *)attrs {
+- (instancetype)initWithExpression:(NSExpression *)expression attributes:(nonnull NSDictionary<MGLAttributedExpressionKey, NSExpression *> *)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 b509c12172..4b1fdb818e 100644
--- a/platform/darwin/src/NSExpression+MGLAdditions.mm
+++ b/platform/darwin/src/NSExpression+MGLAdditions.mm
@@ -882,19 +882,8 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
attrs = [NSMutableDictionary dictionaryWithDictionary:argumentObjects[index + 1]];
}
- if (attrs.count) {
- if (NSArray *fontNames = MGL_OBJC_DYNAMIC_CAST(attrs[MGLFontNamesAttribute], NSArray)) {
- attrs[MGLFontNamesAttribute] = fontNames[1];
- }
- if (NSArray *colorArray = MGL_OBJC_DYNAMIC_CAST(attrs[MGLFontColorAttribute], NSArray)) {
- if ([colorArray[0] isEqualToString:@"rgb"] || [colorArray[0] isEqualToString:@"rgba"]) {
- NSArray *colorArguments = [colorArray subarrayWithRange:NSMakeRange(1, colorArray.count - 1)];
- NSArray *subexpressions = MGLSubexpressionsWithJSONObjects(colorArguments);
- MGLColor *color = [NSExpression mgl_colorWithRGBComponents:subexpressions];
-
- attrs[MGLFontColorAttribute] = color;
- }
- }
+ for (NSString *key in attrs.allKeys) {
+ attrs[key] = [NSExpression expressionWithMGLJSONObject:attrs[key]];
}
MGLAttributedExpression *attributedExpression = [[MGLAttributedExpression alloc] initWithExpression:expression attributes:attrs];
@@ -1016,17 +1005,15 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) {
if ([constantValue isKindOfClass:[MGLAttributedExpression class]]) {
MGLAttributedExpression *attributedExpression = (MGLAttributedExpression *)constantValue;
id jsonObject = attributedExpression.expression.mgl_jsonExpressionObject;
- NSMutableDictionary *attributedDictionary = [NSMutableDictionary dictionary];
+ NSMutableDictionary<MGLAttributedExpressionKey, NSExpression *> *attributedDictionary = [NSMutableDictionary dictionary];
if (attributedExpression.attributes) {
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];
- attributedDictionary[MGLFontColorAttribute] = color.mgl_jsonExpressionObject;
+
+ for (NSString *key in attributedExpression.attributes.allKeys) {
+ attributedDictionary[key] = attributedExpression.attributes[key].mgl_jsonExpressionObject;
}
+
}
return @[jsonObject, attributedDictionary];
}
diff --git a/platform/darwin/test/MGLDocumentationExampleTests.swift b/platform/darwin/test/MGLDocumentationExampleTests.swift
index c571b15a96..c6062cf7b5 100644
--- a/platform/darwin/test/MGLDocumentationExampleTests.swift
+++ b/platform/darwin/test/MGLDocumentationExampleTests.swift
@@ -550,10 +550,10 @@ class MGLDocumentationExampleTests: XCTestCase, MGLMapViewDelegate {
let redColor = UIColor.red
#endif
let expression = NSExpression(forConstantValue: "Foo")
- let attributes: Dictionary<MGLAttributedExpressionKey, Any> = [.fontNamesAttribute : ["DIN Offc Pro Italic",
- "Arial Unicode MS Regular"],
- .fontScaleAttribute: 1.2,
- .fontColorAttribute: redColor]
+ let attributes: Dictionary<MGLAttributedExpressionKey, NSExpression> = [.fontNamesAttribute : NSExpression(forConstantValue: ["DIN Offc Pro Italic",
+ "Arial Unicode MS Regular"]),
+ .fontScaleAttribute: NSExpression(forConstantValue: 1.2),
+ .fontColorAttribute: NSExpression(forConstantValue: redColor)]
let attributedExpression = MGLAttributedExpression(expression, attributes:attributes)
//#-end-example-code
diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm
index bc0348343b..68806e38f6 100644
--- a/platform/darwin/test/MGLExpressionTests.mm
+++ b/platform/darwin/test/MGLExpressionTests.mm
@@ -1048,8 +1048,8 @@ using namespace std::string_literals;
}
{
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"]
- attributes:@{ MGLFontScaleAttribute: @(1.2),
- MGLFontColorAttribute: @"yellow"}] ;
+ attributes:@{ MGLFontScaleAttribute: MGLConstantExpression(@(1.2)),
+ MGLFontColorAttribute: MGLConstantExpression(@"yellow") }] ;
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];
NSExpression *compatibilityExpression = [NSExpression expressionForFunction:@"mgl_attributed:" arguments:@[MGLConstantExpression(attribute1)]];
@@ -1068,22 +1068,25 @@ using namespace std::string_literals;
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
+ NSExpression *fontNames = [NSExpression expressionForAggregate:@[ MGLConstantExpression(@"DIN Offc Pro Bold"), MGLConstantExpression(@"Arial Unicode MS Bold") ]];
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"]
- attributes:@{ MGLFontScaleAttribute: @(1.2),
- MGLFontColorAttribute: @"yellow",
- MGLFontNamesAttribute: @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]
+ attributes:@{ MGLFontScaleAttribute: MGLConstantExpression(@(1.2)),
+ MGLFontColorAttribute: MGLConstantExpression(@"yellow"),
+ MGLFontNamesAttribute: fontNames
}] ;
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);
+ NSExpression *exp = [NSExpression expressionWithMGLJSONObject:jsonExpression];
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
+ NSExpression *fontNames = [NSExpression expressionForAggregate:@[ MGLConstantExpression(@"DIN Offc Pro Bold"), MGLConstantExpression(@"Arial Unicode MS Bold") ]];
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionForConstantValue:@"foo"]
- attributes:@{ MGLFontScaleAttribute: @(1.2),
- MGLFontColorAttribute: [MGLColor redColor],
- MGLFontNamesAttribute: @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]
+ attributes:@{ MGLFontScaleAttribute: MGLConstantExpression(@(1.2)),
+ MGLFontColorAttribute: MGLConstantExpression([MGLColor redColor]),
+ MGLFontNamesAttribute: fontNames
}] ;
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];
@@ -1092,10 +1095,11 @@ using namespace std::string_literals;
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
+ NSExpression *fontNames = [NSExpression expressionForAggregate:@[ MGLConstantExpression(@"DIN Offc Pro Bold"), MGLConstantExpression(@"Arial Unicode MS Bold") ]];
MGLAttributedExpression *attribute1 = [[MGLAttributedExpression alloc] initWithExpression:[NSExpression expressionWithFormat:@"CAST(x, 'NSString')"]
- attributes:@{ MGLFontScaleAttribute: @(1.2),
- MGLFontColorAttribute: [MGLColor redColor],
- MGLFontNamesAttribute: @[ @"DIN Offc Pro Bold", @"Arial Unicode MS Bold" ]
+ attributes:@{ MGLFontScaleAttribute: MGLConstantExpression(@(1.2)),
+ MGLFontColorAttribute: MGLConstantExpression([MGLColor redColor]),
+ MGLFontNamesAttribute: fontNames
}] ;
NSExpression *expression = [NSExpression expressionWithFormat:@"mgl_attributed:(%@)", MGLConstantExpression(attribute1)];