Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions include/mbgl/style/layers/symbol_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ class SymbolLayer : public Layer {
PropertyValue<AlignmentType> getTextRotationAlignment() const;
void setTextRotationAlignment(PropertyValue<AlignmentType>);

static PropertyValue<std::string> getDefaultTextField();
PropertyValue<std::string> getTextField() const;
void setTextField(PropertyValue<std::string>);
static DataDrivenPropertyValue<std::string> getDefaultTextField();
DataDrivenPropertyValue<std::string> getTextField() const;
void setTextField(DataDrivenPropertyValue<std::string>);

static PropertyValue<std::vector<std::string>> getDefaultTextFont();
PropertyValue<std::vector<std::string>> getTextFont() const;
Expand Down Expand Up @@ -147,9 +147,9 @@ class SymbolLayer : public Layer {
PropertyValue<bool> getTextKeepUpright() const;
void setTextKeepUpright(PropertyValue<bool>);

static PropertyValue<TextTransformType> getDefaultTextTransform();
PropertyValue<TextTransformType> getTextTransform() const;
void setTextTransform(PropertyValue<TextTransformType>);
static DataDrivenPropertyValue<TextTransformType> getDefaultTextTransform();
DataDrivenPropertyValue<TextTransformType> getTextTransform() const;
void setTextTransform(DataDrivenPropertyValue<TextTransformType>);

static PropertyValue<std::array<float, 2>> getDefaultTextOffset();
PropertyValue<std::array<float, 2>> getTextOffset() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ public static <Z extends Number> PropertyValue<CameraFunction<Z, String>> textRo
}

/**
* Value to use for a text label. Feature properties are specified using tokens like {field_name}.
* Value to use for a text label. Feature properties are specified using tokens like {field_name}. (Token replacement is only supported for literal `textField` values--not for property functions.)
*
* @param value a String value
* @return property wrapper around String
Expand All @@ -1754,13 +1754,13 @@ public static PropertyValue<String> textField(String value) {


/**
* Value to use for a text label. Feature properties are specified using tokens like {field_name}.
* Value to use for a text label. Feature properties are specified using tokens like {field_name}. (Token replacement is only supported for literal `textField` values--not for property functions.)
*
* @param <Z> the zoom parameter type
* @param function a wrapper {@link CameraFunction} for String
* @param <T> the function input type
* @param function a wrapper function for String
* @return property wrapper around a String function
*/
public static <Z extends Number> PropertyValue<CameraFunction<Z, String>> textField(CameraFunction<Z, String> function) {
public static <T> PropertyValue<Function<T, String>> textField(Function<T, String> function) {
return new LayoutPropertyValue<>("text-field", function);
}

Expand Down Expand Up @@ -2032,11 +2032,11 @@ public static PropertyValue<String> textTransform(@Property.TEXT_TRANSFORM Strin
/**
* Specifies how to capitalize text, similar to the CSS {@link PropertyFactory#textTransform} property.
*
* @param <Z> the zoom parameter type
* @param function a wrapper {@link CameraFunction} for String
* @param <T> the function input type
* @param function a wrapper function for String
* @return property wrapper around a String function
*/
public static <Z extends Number> PropertyValue<CameraFunction<Z, String>> textTransform(CameraFunction<Z, String> function) {
public static <T> PropertyValue<Function<T, String>> textTransform(Function<T, String> function) {
return new LayoutPropertyValue<>("text-transform", function);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,51 @@ public void testTextFieldAsCameraFunction() {
assertEquals(1, ((IntervalStops) layer.getTextField().getFunction().getStops()).size());
}

@Test
public void testTextFieldAsIdentitySourceFunction() {
checkViewIsDisplayed(R.id.mapView);
Timber.i("text-field");
assertNotNull(layer);

// Set
layer.setProperties(
textField(property("FeaturePropertyA", Stops.<String>identity()))
);

// Verify
assertNotNull(layer.getTextField());
assertNotNull(layer.getTextField().getFunction());
assertEquals(SourceFunction.class, layer.getTextField().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getTextField().getFunction()).getProperty());
assertEquals(IdentityStops.class, layer.getTextField().getFunction().getStops().getClass());
}

@Test
public void testTextFieldAsIntervalSourceFunction() {
checkViewIsDisplayed(R.id.mapView);
Timber.i("text-field");
assertNotNull(layer);

// Set
layer.setProperties(
textField(
property(
"FeaturePropertyA",
interval(
stop(1, textField(""))
)
)
)
);

// Verify
assertNotNull(layer.getTextField());
assertNotNull(layer.getTextField().getFunction());
assertEquals(SourceFunction.class, layer.getTextField().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getTextField().getFunction()).getProperty());
assertEquals(IdentityStops.class, layer.getTextField().getFunction().getStops().getClass());
}

@Test
public void testTextFontAsConstant() {
checkViewIsDisplayed(R.id.mapView);
Expand Down Expand Up @@ -1325,6 +1370,51 @@ public void testTextTransformAsCameraFunction() {
assertEquals(1, ((IntervalStops) layer.getTextTransform().getFunction().getStops()).size());
}

@Test
public void testTextTransformAsIdentitySourceFunction() {
checkViewIsDisplayed(R.id.mapView);
Timber.i("text-transform");
assertNotNull(layer);

// Set
layer.setProperties(
textTransform(property("FeaturePropertyA", Stops.<String>identity()))
);

// Verify
assertNotNull(layer.getTextTransform());
assertNotNull(layer.getTextTransform().getFunction());
assertEquals(SourceFunction.class, layer.getTextTransform().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getTextTransform().getFunction()).getProperty());
assertEquals(IdentityStops.class, layer.getTextTransform().getFunction().getStops().getClass());
}

@Test
public void testTextTransformAsIntervalSourceFunction() {
checkViewIsDisplayed(R.id.mapView);
Timber.i("text-transform");
assertNotNull(layer);

// Set
layer.setProperties(
textTransform(
property(
"FeaturePropertyA",
interval(
stop(1, textTransform(TEXT_TRANSFORM_NONE))
)
)
)
);

// Verify
assertNotNull(layer.getTextTransform());
assertNotNull(layer.getTextTransform().getFunction());
assertEquals(SourceFunction.class, layer.getTextTransform().getFunction().getClass());
assertEquals("FeaturePropertyA", ((SourceFunction) layer.getTextTransform().getFunction()).getProperty());
assertEquals(IdentityStops.class, layer.getTextTransform().getFunction().getStops().getClass());
}

@Test
public void testTextOffsetAsConstant() {
checkViewIsDisplayed(R.id.mapView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ public class <%- camelize(type) %>LayerTest extends BaseStyleTest {
<% if (property.type == 'color') { -%>
stop(Color.RED, <%- camelizeWithLeadingLowercase(property.name) %>(Color.RED))
<% } else {-%>
1, <%- camelizeWithLeadingLowercase(property.name) %>(<%- defaultValueJava(property) %>))
<% } -%>
stop(1, <%- camelizeWithLeadingLowercase(property.name) %>(<%- defaultValueJava(property) %>))
)
<% } -%>
)
)
);
Expand Down
9 changes: 8 additions & 1 deletion platform/darwin/scripts/generate-style-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ global.propertyType = function (property) {
}
};

global.isInterpolatable = function (property) {
const type = property.type === 'array' ? property.value : property.type;
return type !== 'boolean' &&
type !== 'enum' &&
type !== 'string';
};

global.valueTransformerArguments = function (property) {
let objCType = propertyType(property);
switch (property.type) {
Expand All @@ -444,7 +451,7 @@ global.valueTransformerArguments = function (property) {
case 'string':
return ['std::string', objCType];
case 'enum':
return [`mbgl::style::${mbglType(property)}`, objCType];
return [mbglType(property), 'NSValue *', mbglType(property), `MGL${camelize(property.name)}`];
case 'color':
return ['mbgl::Color', objCType];
case 'array':
Expand Down
4 changes: 2 additions & 2 deletions platform/darwin/src/MGLStyleLayer.mm.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ namespace mbgl {
auto mbglValue = MGLStyleValueTransformer<<%- valueTransformerArguments(property).join(', ') %>>().toDataDrivenPropertyValue(<%- objCName(property) %>);
<% } else { -%>
<% if (property.type == "enum") { -%>
auto mbglValue = MGLStyleValueTransformer<<%- mbglType(property) %>, NSValue *, <%- mbglType(property) %>, MGL<%- camelize(property.name) %>>().toEnumPropertyValue(<%- objCName(property) %>);
auto mbglValue = MGLStyleValueTransformer<<%- valueTransformerArguments(property).join(', ') %>>().toEnumPropertyValue(<%- objCName(property) %>);
<% } else if (property.function == "piecewise-constant") { -%>
auto mbglValue = MGLStyleValueTransformer<<%- valueTransformerArguments(property).join(', ') %>>().toPropertyValue(<%- objCName(property) %>);
<% } else { -%>
Expand Down Expand Up @@ -236,7 +236,7 @@ namespace mbgl {
auto mbglValue = MGLStyleValueTransformer<<%- valueTransformerArguments(property).join(', ') %>>().toDataDrivenPropertyValue(<%- objCName(property) %>);
<% } else { -%>
<% if (property.type == "enum") { -%>
auto mbglValue = MGLStyleValueTransformer<<%- mbglType(property) %>, NSValue *, <%- mbglType(property) %>, MGL<%- camelize(property.name) %>>().toEnumPropertyValue(<%- objCName(property) %>);
auto mbglValue = MGLStyleValueTransformer<<%- valueTransformerArguments(property).join(', ') %>>().toEnumPropertyValue(<%- objCName(property) %>);
<% } else if (property.function == "piecewise-constant") { -%>
auto mbglValue = MGLStyleValueTransformer<<%- valueTransformerArguments(property).join(', ') %>>().toPropertyValue(<%- objCName(property) %>);
<% } else { -%>
Expand Down
80 changes: 35 additions & 45 deletions platform/darwin/src/MGLStyleValue_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,16 @@ class MGLStyleValueTransformer {
return mbglValue;
}

NSObject* toRawStyleSpecValue(MGLColor *color) {
return @(color.mgl_color.stringify().c_str());
}

NSObject* toRawStyleSpecValue(NSObject *rawMGLValue) {
/**
As hack to allow converting enum => string values, we accept a second, dummy parameter in
the toRawStyleSpecValue() methods for converting 'atomic' (non-style-function) values.
This allows us to use `std::enable_if` to test (at compile time) whether or not MBGLType is an Enum.
*/
template <typename MBGLEnum = MBGLType,
class = typename std::enable_if<!std::is_enum<MBGLEnum>::value>::type,
typename MGLEnum = ObjCEnum,
class = typename std::enable_if<!std::is_enum<MGLEnum>::value>::type>
NSObject* toRawStyleSpecValue(NSObject *rawMGLValue, MBGLEnum &mbglValue) {
if ([rawMGLValue isKindOfClass:[NSValue class]]) {
const auto rawNSValue = (NSValue *)rawMGLValue;
if (strcmp([rawNSValue objCType], @encode(CGVector)) == 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I see special handling for CGVector values, but not NSEdgeInsets/UIEdgeInsets values. Do we need it too? Here’s the full correspondence between JSON and Foundation types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good catch. I've ticketed it out here as part of the completionist quest to propagate this approach to all of the MGL=>mbgl style property value conversions (or, at least the runtime-styling ones...)

Expand All @@ -198,6 +203,21 @@ class MGLStyleValueTransformer {
return rawMGLValue;
}

template <typename MBGLEnum = MBGLType,
class = typename std::enable_if<std::is_enum<MBGLEnum>::value>::type,
typename MGLEnum = ObjCEnum,
class = typename std::enable_if<std::is_enum<MGLEnum>::value>::type>
NSString* toRawStyleSpecValue(ObjCType rawValue, MBGLEnum &mbglValue) {
MGLEnum mglEnum;
[rawValue getValue:&mglEnum];
return @(mbgl::Enum<MGLEnum>::toString(mglEnum));
}

NSObject* toRawStyleSpecValue(MGLColor *color, MBGLType &mbglValue) {
return @(color.mgl_color.stringify().c_str());
}


NSObject* toRawStyleSpecValue(MGLStyleFunction<ObjCType>* styleFunction) {
NSMutableDictionary * rawFunction = [NSMutableDictionary new];
// interpolationMode => type
Expand Down Expand Up @@ -226,7 +246,8 @@ class MGLStyleValueTransformer {
// zoom-only function (no default value)
__block NSMutableArray *stops = [[NSMutableArray alloc] init];
[styleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, MGLStyleConstantValue<ObjCType> * _Nonnull outputValue, BOOL * _Nonnull stop) {
NSArray *rawStop = @[zoomKey, toRawStyleSpecValue([outputValue rawValue])];
MBGLType dummyMbglValue;
NSArray *rawStop = @[zoomKey, toRawStyleSpecValue([outputValue rawValue], dummyMbglValue)];
[stops addObject:rawStop];
}];
rawFunction[@"stops"] = stops;
Expand All @@ -237,15 +258,17 @@ class MGLStyleValueTransformer {
// property-only function
__block NSMutableArray *stops = [[NSMutableArray alloc] init];
[styleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSObject * _Nonnull propertyKey, MGLStyleConstantValue<ObjCType> * _Nonnull outputValue, BOOL * _Nonnull stop) {
NSArray *rawStop = @[propertyKey, toRawStyleSpecValue([outputValue rawValue])];
MBGLType dummyMbglValue;
NSArray *rawStop = @[propertyKey, toRawStyleSpecValue([outputValue rawValue], dummyMbglValue)];
[stops addObject:rawStop];
}];
rawFunction[@"stops"] = stops;

// defaultValue => default
if (sourceStyleFunction.defaultValue) {
NSCAssert([sourceStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
rawFunction[@"default"] = toRawStyleSpecValue([(MGLStyleConstantValue<ObjCType> *)sourceStyleFunction.defaultValue rawValue]);
MBGLType dummyMbglValue;
rawFunction[@"default"] = toRawStyleSpecValue([(MGLStyleConstantValue<ObjCType> *)sourceStyleFunction.defaultValue rawValue], dummyMbglValue);
}
} else if ([styleFunction isKindOfClass:[MGLCompositeStyleFunction class]]) {
// zoom-and-property function
Expand All @@ -261,7 +284,8 @@ class MGLStyleValueTransformer {
};
MGLStyleConstantValue<ObjCType> *outputValue = stopValue[valueKey];
NSCAssert([outputValue isKindOfClass:[MGLStyleConstantValue<ObjCType> class]], @"Stop outputs should be MGLStyleConstantValues");
NSArray *rawStop = @[stopKey, toRawStyleSpecValue([outputValue rawValue])];
MBGLType dummyMbglValue;
NSArray *rawStop = @[stopKey, toRawStyleSpecValue([outputValue rawValue], dummyMbglValue)];
[stops addObject:rawStop];
}
}];
Expand All @@ -270,7 +294,8 @@ class MGLStyleValueTransformer {
// defaultValue => default
if (compositeStyleFunction.defaultValue) {
NSCAssert([compositeStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
rawFunction[@"default"] = toRawStyleSpecValue([(MGLStyleConstantValue<ObjCType> *)compositeStyleFunction.defaultValue rawValue]);
MBGLType dummyMbglValue;
rawFunction[@"default"] = toRawStyleSpecValue([(MGLStyleConstantValue<ObjCType> *)compositeStyleFunction.defaultValue rawValue], dummyMbglValue);
}
}

Expand Down Expand Up @@ -340,41 +365,6 @@ class MGLStyleValueTransformer {
return sourceFunction;
}

mbgl::style::SourceFunction<MBGLType> toMBGLExponentialSourceFunction(MGLSourceStyleFunction<ObjCType> *sourceStyleFunction) {
__block std::map<float, MBGLType> stops = {};
[sourceStyleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, MGLStyleValue<ObjCType> * _Nonnull stopValue, BOOL * _Nonnull stop) {
NSCAssert([stopValue isKindOfClass:[MGLStyleValue class]], @"Stops should be MGLStyleValues");
auto mbglStopValue = toPropertyValue(stopValue);
NSCAssert(mbglStopValue.isConstant(), @"Stops must be constant");
stops[zoomKey.floatValue] = mbglStopValue.asConstant();
}];
mbgl::style::ExponentialStops<MBGLType> exponentialStops = {stops, (float)sourceStyleFunction.interpolationBase};
mbgl::style::SourceFunction<MBGLType> sourceFunction = {sourceStyleFunction.attributeName.UTF8String, exponentialStops};
setDefaultMBGLValue(sourceStyleFunction, sourceFunction);
return sourceFunction;
}

mbgl::style::SourceFunction<MBGLType> toMBGLIntervalSourceFunction(MGLSourceStyleFunction<ObjCType> *sourceStyleFunction) {
__block std::map<float, MBGLType> stops = {};
[sourceStyleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, MGLStyleValue<ObjCType> * _Nonnull stopValue, BOOL * _Nonnull stop) {
NSCAssert([stopValue isKindOfClass:[MGLStyleValue class]], @"Stops should be MGLStyleValues");
auto mbglStopValue = toPropertyValue(stopValue);
NSCAssert(mbglStopValue.isConstant(), @"Stops must be constant");
stops[zoomKey.floatValue] = mbglStopValue.asConstant();
}];
mbgl::style::IntervalStops<MBGLType> intervalStops = {stops};
mbgl::style::SourceFunction<MBGLType> sourceFunction = {sourceStyleFunction.attributeName.UTF8String, intervalStops};
setDefaultMBGLValue(sourceStyleFunction, sourceFunction);
return sourceFunction;
}

mbgl::style::SourceFunction<MBGLType> toMBGLIdentitySourceFunction(MGLSourceStyleFunction<ObjCType> *sourceStyleFunction) {
mbgl::style::IdentityStops<MBGLType> identityStops;
mbgl::style::SourceFunction<MBGLType> sourceFunction = {sourceStyleFunction.attributeName.UTF8String, identityStops};
setDefaultMBGLValue(sourceStyleFunction, sourceFunction);
return sourceFunction;
}

void setDefaultMBGLValue(MGLSourceStyleFunction<ObjCType> *sourceStyleFunction, mbgl::style::SourceFunction<MBGLType> &sourceFunction) {
if (sourceStyleFunction.defaultValue) {
NSCAssert([sourceStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
Expand Down
Loading