summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMinh Nguyễn <mxn@1ec5.org>2016-08-18 12:19:47 -0700
committerMinh Nguyễn <mxn@1ec5.org>2016-08-19 15:45:14 -0700
commit0a4f0e40ec6820a052ae8c68b3d58b1b300bb900 (patch)
tree18087731fc875e444ac24243e143bab78f05dd05
parent7f1b325b0c8424d82a3de209a9bdee7a8602b1e0 (diff)
downloadqtlocation-mapboxgl-0a4f0e40ec6820a052ae8c68b3d58b1b300bb900.tar.gz
[ios, macos] Offsets as vectors, padding as edge insets
An offset style attribute is now exposed publicly as an NSValue representing a CGVector instead of an NSArray of NSNumbers. A padding style attribute is now exposed publicly as an NSValue representing an NSEdgeInsets or UIEdgeInsets instead of an NSArray of NSNumbers. This change also fixes round-tripping of padding values due to a difference between the style specification and Foundation regarding the order of edges around a box. Used a designated initializer on NSEdgeInsets/UIEdgeInsets to ensure correct order when converting from C++ to Objective-C. Fixes #5947, fixes #6065.
-rw-r--r--platform/darwin/scripts/generate-style-code.js8
-rw-r--r--platform/darwin/src/MGLStyleAttribute.mm7
-rw-r--r--platform/darwin/src/MGLStyleAttributeFunction.mm22
-rw-r--r--platform/darwin/src/MGLSymbolStyleLayer.h2
-rw-r--r--platform/darwin/src/NSArray+MGLStyleAttributeAdditions.mm18
-rw-r--r--platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm64
-rw-r--r--platform/darwin/src/NSValue+MGLStyleAttributeAdditions_Private.h9
-rw-r--r--platform/darwin/test/MGLRuntimeStylingHelper.m21
8 files changed, 103 insertions, 48 deletions
diff --git a/platform/darwin/scripts/generate-style-code.js b/platform/darwin/scripts/generate-style-code.js
index 08b358b798..c7dace62a9 100644
--- a/platform/darwin/scripts/generate-style-code.js
+++ b/platform/darwin/scripts/generate-style-code.js
@@ -176,10 +176,10 @@ global.describeValue = function (value, property, layerType) {
units = ` ${units}`.replace(/pixel/, 'point');
}
if (property.name.indexOf('padding') !== -1) {
- //if (value.reduce((a, b) => a + b, 0) === 0) {
- // return '`NSEdgeInsetsZero` or `UIEdgeInsetsZero`';
- //}
- return `${value[0]}${units} on the top, ${value[1]}${units} on the right, ${value[2]}${units} on the bottom, and ${value[3]}${units} on the left`;
+ if (value.reduce((a, b) => a + b, 0) === 0) {
+ return '`NSEdgeInsetsZero` or `UIEdgeInsetsZero`';
+ }
+ return `${value[0]}${units} on the top, ${value[3]}${units} on the left, ${value[2]}${units} on the bottom, and ${value[1]}${units} on the right`;
}
if (property.name.indexOf('offset') !== -1 || property.name.indexOf('translate') !== -1) {
return `${value[0]}${units} from the left and ${value[1]}${units} from the top`;
diff --git a/platform/darwin/src/MGLStyleAttribute.mm b/platform/darwin/src/MGLStyleAttribute.mm
index 8c51f80758..9e3c22914b 100644
--- a/platform/darwin/src/MGLStyleAttribute.mm
+++ b/platform/darwin/src/MGLStyleAttribute.mm
@@ -2,8 +2,7 @@
#import "MGLStyleAttributeValue_Private.h"
#import "MGLStyleAttributeFunction_Private.h"
-
-#import "MGLTypes.h"
+#import "NSValue+MGLStyleAttributeAdditions_Private.h"
@interface MGLStyleAttribute()
@end
@@ -58,7 +57,7 @@
{
if (property.isConstant()) {
auto offset = property.asConstant();
- return @[@(offset[0]), @(offset[1])];
+ return [NSValue mgl_valueWithOffsetArray:offset];
} else if (property.isFunction()) {
return [MGLStyleAttributeFunction functionWithOffsetPropertyValue:property.asFunction()];
} else {
@@ -70,7 +69,7 @@
{
if (property.isConstant()) {
auto padding = property.asConstant();
- return @[@(padding[0]), @(padding[1]), @(padding[2]), @(padding[3])];
+ return [NSValue mgl_valueWithPaddingArray:padding];
} else if (property.isFunction()) {
return [MGLStyleAttributeFunction functionWithPaddingPropertyValue:property.asFunction()];
} else {
diff --git a/platform/darwin/src/MGLStyleAttributeFunction.mm b/platform/darwin/src/MGLStyleAttributeFunction.mm
index 5216e32d97..46fa3ec6a7 100644
--- a/platform/darwin/src/MGLStyleAttributeFunction.mm
+++ b/platform/darwin/src/MGLStyleAttributeFunction.mm
@@ -93,14 +93,9 @@
- (mbgl::style::PropertyValue<std::array<float, 4> >)mbgl_paddingPropertyValue
{
__block std::vector<std::pair<float, std::array<float, 4>>> stops;
- [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSArray * _Nonnull padding, BOOL * _Nonnull stop) {
- NSAssert([padding isKindOfClass:[NSArray class]], @"Stops should be NSArray");
- NSNumber *top = padding[0];
- NSNumber *left = padding[1];
- NSNumber *bottom = padding[2];
- NSNumber *right = padding[2];
- auto pad = std::array<float, 4>({{top.floatValue, left.floatValue, bottom.floatValue, right.floatValue}});
- stops.emplace_back(zoomKey.floatValue, pad);
+ [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSValue * _Nonnull padding, BOOL * _Nonnull stop) {
+ NSAssert([padding isKindOfClass:[NSArray class]], @"Stops should be NSValue");
+ stops.emplace_back(zoomKey.floatValue, padding.mgl_paddingArrayValue);
}];
return mbgl::style::Function<std::array<float, 4>>({{stops}}, _base.floatValue);
}
@@ -108,12 +103,9 @@
- (mbgl::style::PropertyValue<std::array<float, 2> >)mbgl_offsetPropertyValue
{
__block std::vector<std::pair<float, std::array<float, 2>>> stops;
- [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSArray * _Nonnull offset, BOOL * _Nonnull stop) {
- NSAssert([offset isKindOfClass:[NSArray class]], @"Stops should be NSArray");
- NSNumber *dx = offset[0];
- NSNumber *dy = offset[1];
- auto off = std::array<float, 2>({{dx.floatValue, dy.floatValue}});
- stops.emplace_back(zoomKey.floatValue, off);
+ [self.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSValue * _Nonnull offset, BOOL * _Nonnull stop) {
+ NSAssert([offset isKindOfClass:[NSValue class]], @"Stops should be NSValue");
+ stops.emplace_back(zoomKey.floatValue, offset.mgl_offsetArrayValue);
}];
return mbgl::style::Function<std::array<float, 2>>({{stops}}, _base.floatValue);
}
@@ -176,7 +168,7 @@
auto stops = property.getStops();
NSMutableDictionary *convertedStops = [NSMutableDictionary dictionaryWithCapacity:stops.size()];
for (auto stop : stops) {
- convertedStops[@(stop.first)] = @[@(stop.second[0]), @(stop.second[1])];
+ convertedStops[@(stop.first)] = [NSValue mgl_valueWithOffsetArray:stop.second];
}
function.base = @(property.getBase());
function.stops = convertedStops;
diff --git a/platform/darwin/src/MGLSymbolStyleLayer.h b/platform/darwin/src/MGLSymbolStyleLayer.h
index d77488bdca..643f0fce20 100644
--- a/platform/darwin/src/MGLSymbolStyleLayer.h
+++ b/platform/darwin/src/MGLSymbolStyleLayer.h
@@ -155,7 +155,7 @@ typedef NS_ENUM(NSUInteger, MGLSymbolStyleLayerTextTranslateAnchor) {
This property is measured in points.
- If this property is set to `nil`, the layer uses an implicit default value of 0 points on the top, 0 points on the right, 0 points on the bottom, and 0 points on the left.
+ If this property is set to `nil`, the layer uses an implicit default value of `NSEdgeInsetsZero` or `UIEdgeInsetsZero`.
This property is only applied to the style if `iconImage` is non-`nil`, and `iconTextFit` is non-`nil`, and `textField` is non-`nil`. Otherwise, it is ignored.
*/
diff --git a/platform/darwin/src/NSArray+MGLStyleAttributeAdditions.mm b/platform/darwin/src/NSArray+MGLStyleAttributeAdditions.mm
index 1456068938..fb60cf0f72 100644
--- a/platform/darwin/src/NSArray+MGLStyleAttributeAdditions.mm
+++ b/platform/darwin/src/NSArray+MGLStyleAttributeAdditions.mm
@@ -10,24 +10,6 @@
@implementation NSArray (MGLStyleAttributeAdditions)
-- (mbgl::style::PropertyValue<std::array<float, 2>>)mbgl_offsetPropertyValue
-{
- NSAssert(self.count == 2, @"Offset must contain 2 values (dx, dy)");
- NSNumber *dx = self[0];
- NSNumber *dy = self[1];
- return {{dx.floatValue, dy.floatValue}};
-}
-
-- (mbgl::style::PropertyValue<std::array<float, 4> >)mbgl_paddingPropertyValue
-{
- NSAssert(self.count == 4, @"Padding must contain 4 values (top, left, bottom & right)");
- NSNumber *top = self[0];
- NSNumber *left = self[1];
- NSNumber *bottom = self[2];
- NSNumber *right = self[3];
- return {{top.floatValue, left.floatValue, bottom.floatValue, right.floatValue}};
-}
-
- (mbgl::style::PropertyValue<std::vector<std::string> >)mbgl_stringArrayPropertyValue
{
std::vector<std::string>fonts;
diff --git a/platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm b/platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm
index 633a74d276..79038a66fd 100644
--- a/platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm
+++ b/platform/darwin/src/NSValue+MGLStyleAttributeAdditions.mm
@@ -2,8 +2,36 @@
#import "NSValue+MGLStyleAttributeAdditions_Private.h"
+#include <array>
+
+#if TARGET_OS_IPHONE
+ #import <UIKit/UIKit.h>
+ #define MGLEdgeInsets UIEdgeInsets
+#else
+ #define MGLEdgeInsets NSEdgeInsets
+#endif
+
@implementation NSValue (MGLStyleAttributeAdditions)
++ (instancetype)mgl_valueWithOffsetArray:(std::array<float, 2>)offsetArray
+{
+ CGVector vector = CGVectorMake(offsetArray[0], offsetArray[1]);
+ return [NSValue value:&vector withObjCType:@encode(CGVector)];
+}
+
++ (instancetype)mgl_valueWithPaddingArray:(std::array<float, 4>)paddingArray
+{
+ // Style specification defines padding in clockwise order: top, right, bottom, left.
+ // Foundation defines padding in counterclockwise order: top, left, bottom, right.
+ MGLEdgeInsets insets = {
+ .top = paddingArray[0],
+ .right = paddingArray[1],
+ .bottom = paddingArray[2],
+ .left = paddingArray[3],
+ };
+ return [NSValue value:&insets withObjCType:@encode(MGLEdgeInsets)];
+}
+
- (BOOL)isFunction
{
return NO;
@@ -11,9 +39,45 @@
- (mbgl::style::PropertyValue<uint8_t>)mbgl_enumPropertyValue
{
+ NSAssert(strcmp(self.objCType, @encode(uint8_t)) == 0, @"Value does not represent a uint8_t");
uint8_t value = 0;
[self getValue:&value];
return mbgl::style::PropertyValue<uint8_t> { value };
}
+- (mbgl::style::PropertyValue<std::array<float, 2>>)mbgl_offsetPropertyValue
+{
+ return { self.mgl_offsetArrayValue };
+}
+
+- (std::array<float, 2>)mgl_offsetArrayValue
+{
+ NSAssert(strcmp(self.objCType, @encode(CGVector)) == 0, @"Value does not represent a CGVector");
+ CGVector vector;
+ [self getValue:&vector];
+ return {
+ static_cast<float>(vector.dx),
+ static_cast<float>(vector.dy),
+ };
+}
+
+- (mbgl::style::PropertyValue<std::array<float, 4>>)mbgl_paddingPropertyValue
+{
+ return { self.mgl_paddingArrayValue };
+}
+
+- (std::array<float, 4>)mgl_paddingArrayValue
+{
+ NSAssert(strcmp(self.objCType, @encode(MGLEdgeInsets)) == 0, @"Value does not represent an NSEdgeInsets/UIEdgeInsets");
+ MGLEdgeInsets insets;
+ [self getValue:&insets];
+ // Style specification defines padding in clockwise order: top, right, bottom, left.
+ return {
+ static_cast<float>(insets.top),
+ static_cast<float>(insets.right),
+ static_cast<float>(insets.bottom),
+ static_cast<float>(insets.left),
+ };
+}
+
@end
diff --git a/platform/darwin/src/NSValue+MGLStyleAttributeAdditions_Private.h b/platform/darwin/src/NSValue+MGLStyleAttributeAdditions_Private.h
index 5bca9407ac..578e0ad9c0 100644
--- a/platform/darwin/src/NSValue+MGLStyleAttributeAdditions_Private.h
+++ b/platform/darwin/src/NSValue+MGLStyleAttributeAdditions_Private.h
@@ -5,5 +5,14 @@
#include <mbgl/style/property_value.hpp>
@interface NSValue (MGLStyleAttributeAdditions_Private) <MGLStyleAttributeValue>
+
++ (instancetype)mgl_valueWithOffsetArray:(std::array<float, 2>)offsetArray;
++ (instancetype)mgl_valueWithPaddingArray:(std::array<float, 4>)paddingArray;
+
- (mbgl::style::PropertyValue<uint8_t>)mbgl_enumPropertyValue;
+- (mbgl::style::PropertyValue<std::array<float, 2>>)mbgl_offsetPropertyValue;
+
+- (std::array<float, 2>)mgl_offsetArrayValue;
+- (std::array<float, 4>)mgl_paddingArrayValue;
+
@end
diff --git a/platform/darwin/test/MGLRuntimeStylingHelper.m b/platform/darwin/test/MGLRuntimeStylingHelper.m
index fbdc8e5225..f44b855ef4 100644
--- a/platform/darwin/test/MGLRuntimeStylingHelper.m
+++ b/platform/darwin/test/MGLRuntimeStylingHelper.m
@@ -1,21 +1,30 @@
#import "MGLRuntimeStylingHelper.h"
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
-#import <UIKit/UIKit.h>
+ #import <UIKit/UIKit.h>
+ #define MGLEdgeInsets UIEdgeInsets
#else
-#import <Cocoa/Cocoa.h>
+ #import <Cocoa/Cocoa.h>
+ #define MGLEdgeInsets NSEdgeInsets
#endif
@implementation MGLRuntimeStylingHelper
-+ (NSArray *)testPadding
++ (NSValue *)testPadding
{
- return @[@1.0f, @1.0f, @1.0f, @1.0f];
+ MGLEdgeInsets insets = {
+ .top = 1,
+ .left = 1,
+ .bottom = 1,
+ .right = 1,
+ };
+ return [NSValue value:&insets withObjCType:@encode(MGLEdgeInsets)];
}
-+ (NSArray *)testOffset
++ (NSValue *)testOffset
{
- return @[@1.0f, @1.0f];
+ CGVector vector = CGVectorMake(1, 1);
+ return [NSValue value:&vector withObjCType:@encode(CGVector)];
}
+ (NSArray *)testFont