From 47c1e42ac1073ef02093b59336588f389f287227 Mon Sep 17 00:00:00 2001 From: Molly Lloyd Date: Mon, 27 Mar 2017 17:30:58 -0700 Subject: [core] implement dds for text-offset --- .../style/conversion/make_property_setters.hpp | 2 +- include/mbgl/style/layers/symbol_layer.hpp | 6 +-- mapbox-gl-js | 2 +- .../mapboxsdk/style/layers/PropertyFactory.java | 6 +-- .../mapboxsdk/testapp/style/SymbolLayerTest.java | 45 ++++++++++++++++++++++ platform/darwin/src/MGLSymbolStyleLayer.h | 18 +++++++++ platform/darwin/src/MGLSymbolStyleLayer.mm | 6 +-- platform/darwin/test/MGLSymbolStyleLayerTests.mm | 30 ++++++++++++--- src/mbgl/layout/symbol_layout.cpp | 2 +- src/mbgl/style/layers/symbol_layer.cpp | 6 +-- src/mbgl/style/layers/symbol_layer_properties.hpp | 2 +- src/mbgl/text/glyph_set.cpp | 6 +-- 12 files changed, 106 insertions(+), 25 deletions(-) diff --git a/include/mbgl/style/conversion/make_property_setters.hpp b/include/mbgl/style/conversion/make_property_setters.hpp index 623cc09098..2f26fe75c4 100644 --- a/include/mbgl/style/conversion/make_property_setters.hpp +++ b/include/mbgl/style/conversion/make_property_setters.hpp @@ -60,7 +60,7 @@ auto makeLayoutPropertySetters() { result["text-padding"] = &setLayoutProperty, &SymbolLayer::setTextPadding>; result["text-keep-upright"] = &setLayoutProperty, &SymbolLayer::setTextKeepUpright>; result["text-transform"] = &setLayoutProperty, &SymbolLayer::setTextTransform>; - result["text-offset"] = &setLayoutProperty>, &SymbolLayer::setTextOffset>; + result["text-offset"] = &setLayoutProperty>, &SymbolLayer::setTextOffset>; result["text-allow-overlap"] = &setLayoutProperty, &SymbolLayer::setTextAllowOverlap>; result["text-ignore-placement"] = &setLayoutProperty, &SymbolLayer::setTextIgnorePlacement>; result["text-optional"] = &setLayoutProperty, &SymbolLayer::setTextOptional>; diff --git a/include/mbgl/style/layers/symbol_layer.hpp b/include/mbgl/style/layers/symbol_layer.hpp index d24f808bac..b507c992f5 100644 --- a/include/mbgl/style/layers/symbol_layer.hpp +++ b/include/mbgl/style/layers/symbol_layer.hpp @@ -151,9 +151,9 @@ public: DataDrivenPropertyValue getTextTransform() const; void setTextTransform(DataDrivenPropertyValue); - static PropertyValue> getDefaultTextOffset(); - PropertyValue> getTextOffset() const; - void setTextOffset(PropertyValue>); + static DataDrivenPropertyValue> getDefaultTextOffset(); + DataDrivenPropertyValue> getTextOffset() const; + void setTextOffset(DataDrivenPropertyValue>); static PropertyValue getDefaultTextAllowOverlap(); PropertyValue getTextAllowOverlap() const; diff --git a/mapbox-gl-js b/mapbox-gl-js index c248672670..c34b6e9bc4 160000 --- a/mapbox-gl-js +++ b/mapbox-gl-js @@ -1 +1 @@ -Subproject commit c24867267058f1e5f07ad50a003b2d32c3c532dd +Subproject commit c34b6e9bc4a2672c1046267408f1055271484b27 diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java index bfbbf1e77b..ef0bd38b56 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/PropertyFactory.java @@ -2055,11 +2055,11 @@ public class PropertyFactory { /** * Offset distance of text from its anchor. Positive values indicate right and down, while negative values indicate left and up. * - * @param the zoom parameter type - * @param function a wrapper {@link CameraFunction} for Float[] + * @param the function input type + * @param function a wrapper function for Float[] * @return property wrapper around a Float[] function */ - public static PropertyValue> textOffset(CameraFunction function) { + public static PropertyValue> textOffset(Function function) { return new LayoutPropertyValue<>("text-offset", function); } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/SymbolLayerTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/SymbolLayerTest.java index c90af339b1..cac950dfc4 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/SymbolLayerTest.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/SymbolLayerTest.java @@ -1454,6 +1454,51 @@ public class SymbolLayerTest extends BaseStyleTest { assertEquals(1, ((ExponentialStops) layer.getTextOffset().getFunction().getStops()).size()); } + @Test + public void testTextOffsetAsIdentitySourceFunction() { + checkViewIsDisplayed(R.id.mapView); + Timber.i("text-offset"); + assertNotNull(layer); + + // Set + layer.setProperties( + textOffset(property("FeaturePropertyA", Stops.identity())) + ); + + // Verify + assertNotNull(layer.getTextOffset()); + assertNotNull(layer.getTextOffset().getFunction()); + assertEquals(SourceFunction.class, layer.getTextOffset().getFunction().getClass()); + assertEquals("FeaturePropertyA", ((SourceFunction) layer.getTextOffset().getFunction()).getProperty()); + assertEquals(IdentityStops.class, layer.getTextOffset().getFunction().getStops().getClass()); + } + + @Test + public void testTextOffsetAsIntervalSourceFunction() { + checkViewIsDisplayed(R.id.mapView); + Timber.i("text-offset"); + assertNotNull(layer); + + // Set + layer.setProperties( + textOffset( + property( + "FeaturePropertyA", + interval( + stop(1, textOffset(new Float[]{0f,0f})) + ) + ) + ) + ); + + // Verify + assertNotNull(layer.getTextOffset()); + assertNotNull(layer.getTextOffset().getFunction()); + assertEquals(SourceFunction.class, layer.getTextOffset().getFunction().getClass()); + assertEquals("FeaturePropertyA", ((SourceFunction) layer.getTextOffset().getFunction()).getProperty()); + assertEquals(IntervalStops.class, layer.getTextOffset().getFunction().getStops().getClass()); + } + @Test public void testTextAllowOverlapAsConstant() { checkViewIsDisplayed(R.id.mapView); diff --git a/platform/darwin/src/MGLSymbolStyleLayer.h b/platform/darwin/src/MGLSymbolStyleLayer.h index c76efe2de7..11dc67b458 100644 --- a/platform/darwin/src/MGLSymbolStyleLayer.h +++ b/platform/darwin/src/MGLSymbolStyleLayer.h @@ -1026,6 +1026,15 @@ MGL_EXPORT * `MGLCameraStyleFunction` with an interpolation mode of: * `MGLInterpolationModeExponential` * `MGLInterpolationModeInterval` + * `MGLSourceStyleFunction` with an interpolation mode of: + * `MGLInterpolationModeExponential` + * `MGLInterpolationModeInterval` + * `MGLInterpolationModeCategorical` + * `MGLInterpolationModeIdentity` + * `MGLCompositeStyleFunction` with an interpolation mode of: + * `MGLInterpolationModeExponential` + * `MGLInterpolationModeInterval` + * `MGLInterpolationModeCategorical` */ @property (nonatomic, null_resettable) MGLStyleValue *textOffset; #else @@ -1047,6 +1056,15 @@ MGL_EXPORT * `MGLCameraStyleFunction` with an interpolation mode of: * `MGLInterpolationModeExponential` * `MGLInterpolationModeInterval` + * `MGLSourceStyleFunction` with an interpolation mode of: + * `MGLInterpolationModeExponential` + * `MGLInterpolationModeInterval` + * `MGLInterpolationModeCategorical` + * `MGLInterpolationModeIdentity` + * `MGLCompositeStyleFunction` with an interpolation mode of: + * `MGLInterpolationModeExponential` + * `MGLInterpolationModeInterval` + * `MGLInterpolationModeCategorical` */ @property (nonatomic, null_resettable) MGLStyleValue *textOffset; #endif diff --git a/platform/darwin/src/MGLSymbolStyleLayer.mm b/platform/darwin/src/MGLSymbolStyleLayer.mm index 8441931685..6e35bf5dd2 100644 --- a/platform/darwin/src/MGLSymbolStyleLayer.mm +++ b/platform/darwin/src/MGLSymbolStyleLayer.mm @@ -763,7 +763,7 @@ namespace mbgl { - (void)setTextOffset:(MGLStyleValue *)textOffset { MGLAssertStyleLayerIsValid(); - auto mbglValue = MGLStyleValueTransformer, NSValue *>().toInterpolatablePropertyValue(textOffset); + auto mbglValue = MGLStyleValueTransformer, NSValue *>().toDataDrivenPropertyValue(textOffset); self.rawLayer->setTextOffset(mbglValue); } @@ -772,9 +772,9 @@ namespace mbgl { auto propertyValue = self.rawLayer->getTextOffset(); if (propertyValue.isUndefined()) { - return MGLStyleValueTransformer, NSValue *>().toStyleValue(self.rawLayer->getDefaultTextOffset()); + return MGLStyleValueTransformer, NSValue *>().toDataDrivenStyleValue(self.rawLayer->getDefaultTextOffset()); } - return MGLStyleValueTransformer, NSValue *>().toStyleValue(propertyValue); + return MGLStyleValueTransformer, NSValue *>().toDataDrivenStyleValue(propertyValue); } - (void)setTextOptional:(MGLStyleValue *)textOptional { diff --git a/platform/darwin/test/MGLSymbolStyleLayerTests.mm b/platform/darwin/test/MGLSymbolStyleLayerTests.mm index e3e473ef78..18dfa1366f 100644 --- a/platform/darwin/test/MGLSymbolStyleLayerTests.mm +++ b/platform/darwin/test/MGLSymbolStyleLayerTests.mm @@ -1158,7 +1158,7 @@ #endif ]; layer.textOffset = constantStyleValue; - mbgl::style::PropertyValue> propertyValue = { { 1, 1 } }; + mbgl::style::DataDrivenPropertyValue> propertyValue = { { 1, 1 } }; XCTAssertEqual(rawLayer->getTextOffset(), propertyValue, @"Setting textOffset to a constant value should update text-offset."); XCTAssertEqualObjects(layer.textOffset, constantStyleValue, @@ -1175,6 +1175,29 @@ XCTAssertEqualObjects(layer.textOffset, functionStyleValue, @"textOffset should round-trip camera functions."); + functionStyleValue = [MGLStyleValue valueWithInterpolationMode:MGLInterpolationModeExponential sourceStops:@{@18: constantStyleValue} attributeName:@"keyName" options:nil]; + layer.textOffset = functionStyleValue; + + mbgl::style::ExponentialStops> exponentialStops = { {{18, { 1, 1 }}}, 1.0 }; + propertyValue = mbgl::style::SourceFunction> { "keyName", exponentialStops }; + + XCTAssertEqual(rawLayer->getTextOffset(), propertyValue, + @"Setting textOffset to a source function should update text-offset."); + XCTAssertEqualObjects(layer.textOffset, functionStyleValue, + @"textOffset should round-trip source functions."); + + functionStyleValue = [MGLStyleValue valueWithInterpolationMode:MGLInterpolationModeExponential compositeStops:@{@10: @{@18: constantStyleValue}} attributeName:@"keyName" options:nil]; + layer.textOffset = functionStyleValue; + + std::map> innerStops { {18, { 1, 1 }} }; + mbgl::style::CompositeExponentialStops> compositeStops { { {10.0, innerStops} }, 1.0 }; + + propertyValue = mbgl::style::CompositeFunction> { "keyName", compositeStops }; + + XCTAssertEqual(rawLayer->getTextOffset(), propertyValue, + @"Setting textOffset to a composite function should update text-offset."); + XCTAssertEqualObjects(layer.textOffset, functionStyleValue, + @"textOffset should round-trip composite functions."); layer.textOffset = nil; @@ -1182,11 +1205,6 @@ @"Unsetting textOffset should return text-offset to the default value."); XCTAssertEqualObjects(layer.textOffset, defaultStyleValue, @"textOffset should return the default value after being unset."); - - functionStyleValue = [MGLStyleValue valueWithInterpolationMode:MGLInterpolationModeIdentity sourceStops:nil attributeName:@"" options:nil]; - XCTAssertThrowsSpecificNamed(layer.textOffset = functionStyleValue, NSException, NSInvalidArgumentException, @"MGLStyleValue should raise an exception if it is applied to a property that cannot support it"); - functionStyleValue = [MGLStyleValue valueWithInterpolationMode:MGLInterpolationModeInterval compositeStops:@{@18: constantStyleValue} attributeName:@"" options:nil]; - XCTAssertThrowsSpecificNamed(layer.textOffset = functionStyleValue, NSException, NSInvalidArgumentException, @"MGLStyleValue should raise an exception if it is applied to a property that cannot support it"); } // text-optional diff --git a/src/mbgl/layout/symbol_layout.cpp b/src/mbgl/layout/symbol_layout.cpp index 3a2c082ad8..8e36bb84ea 100644 --- a/src/mbgl/layout/symbol_layout.cpp +++ b/src/mbgl/layout/symbol_layout.cpp @@ -250,7 +250,7 @@ void SymbolLayout::prepare(uintptr_t tileUID, /* verticalAlign */ verticalAlign, /* justify */ justify, /* spacing: ems */ layout.get() * oneEm, - /* translate */ Point(layout.get()[0], layout.get()[1]), + /* translate */ Point(layout.evaluate(zoom, feature)[0] * oneEm, layout.evaluate(zoom, feature)[1] * oneEm), /* verticalHeight */ oneEm, /* writingMode */ writingMode, /* bidirectional algorithm object */ bidi); diff --git a/src/mbgl/style/layers/symbol_layer.cpp b/src/mbgl/style/layers/symbol_layer.cpp index 42bcf0e6b0..02e1e364f1 100644 --- a/src/mbgl/style/layers/symbol_layer.cpp +++ b/src/mbgl/style/layers/symbol_layer.cpp @@ -483,15 +483,15 @@ void SymbolLayer::setTextTransform(DataDrivenPropertyValue va impl->layout.unevaluated.get() = value; impl->observer->onLayerLayoutPropertyChanged(*this, "text-transform"); } -PropertyValue> SymbolLayer::getDefaultTextOffset() { +DataDrivenPropertyValue> SymbolLayer::getDefaultTextOffset() { return TextOffset::defaultValue(); } -PropertyValue> SymbolLayer::getTextOffset() const { +DataDrivenPropertyValue> SymbolLayer::getTextOffset() const { return impl->layout.unevaluated.get(); } -void SymbolLayer::setTextOffset(PropertyValue> value) { +void SymbolLayer::setTextOffset(DataDrivenPropertyValue> value) { if (value == getTextOffset()) return; impl->layout.unevaluated.get() = value; diff --git a/src/mbgl/style/layers/symbol_layer_properties.hpp b/src/mbgl/style/layers/symbol_layer_properties.hpp index f2b7bfa00f..e7cfb8d455 100644 --- a/src/mbgl/style/layers/symbol_layer_properties.hpp +++ b/src/mbgl/style/layers/symbol_layer_properties.hpp @@ -160,7 +160,7 @@ struct TextTransform : DataDrivenLayoutProperty { static TextTransformType defaultValue() { return TextTransformType::None; } }; -struct TextOffset : LayoutProperty> { +struct TextOffset : DataDrivenLayoutProperty> { static constexpr const char * key = "text-offset"; static std::array defaultValue() { return {{ 0, 0 }}; } }; diff --git a/src/mbgl/text/glyph_set.cpp b/src/mbgl/text/glyph_set.cpp index 19a6e2cddd..ea0dd123db 100644 --- a/src/mbgl/text/glyph_set.cpp +++ b/src/mbgl/text/glyph_set.cpp @@ -45,7 +45,7 @@ const Shaping GlyphSet::getShaping(const std::u16string& logicalInput, const float verticalHeight, const WritingModeType writingMode, BiDi& bidi) const { - Shaping shaping(translate.x * 24, translate.y * 24, writingMode); + Shaping shaping(translate.x, translate.y, writingMode); std::vector reorderedLines = bidi.processText(logicalInput, @@ -66,9 +66,9 @@ void align(Shaping& shaping, const std::size_t lineCount, const Point& translate) { const float shiftX = - (justify - horizontalAlign) * maxLineLength + ::round(translate.x * 24 /* one em */); + (justify - horizontalAlign) * maxLineLength + ::round(translate.x); const float shiftY = - (-verticalAlign * lineCount + 0.5) * lineHeight + ::round(translate.y * 24 /* one em */); + (-verticalAlign * lineCount + 0.5) * lineHeight + ::round(translate.y); for (auto& glyph : shaping.positionedGlyphs) { glyph.x += shiftX; -- cgit v1.2.1