[core] implement dds for text-offset

This commit is contained in:
Molly Lloyd
2017-03-27 17:30:58 -07:00
committed by John Firebaugh
parent 5f40297526
commit 47c1e42ac1
12 changed files with 106 additions and 25 deletions

View File

@@ -60,7 +60,7 @@ auto makeLayoutPropertySetters() {
result["text-padding"] = &setLayoutProperty<V, SymbolLayer, PropertyValue<float>, &SymbolLayer::setTextPadding>;
result["text-keep-upright"] = &setLayoutProperty<V, SymbolLayer, PropertyValue<bool>, &SymbolLayer::setTextKeepUpright>;
result["text-transform"] = &setLayoutProperty<V, SymbolLayer, DataDrivenPropertyValue<TextTransformType>, &SymbolLayer::setTextTransform>;
result["text-offset"] = &setLayoutProperty<V, SymbolLayer, PropertyValue<std::array<float, 2>>, &SymbolLayer::setTextOffset>;
result["text-offset"] = &setLayoutProperty<V, SymbolLayer, DataDrivenPropertyValue<std::array<float, 2>>, &SymbolLayer::setTextOffset>;
result["text-allow-overlap"] = &setLayoutProperty<V, SymbolLayer, PropertyValue<bool>, &SymbolLayer::setTextAllowOverlap>;
result["text-ignore-placement"] = &setLayoutProperty<V, SymbolLayer, PropertyValue<bool>, &SymbolLayer::setTextIgnorePlacement>;
result["text-optional"] = &setLayoutProperty<V, SymbolLayer, PropertyValue<bool>, &SymbolLayer::setTextOptional>;

View File

@@ -151,9 +151,9 @@ public:
DataDrivenPropertyValue<TextTransformType> getTextTransform() const;
void setTextTransform(DataDrivenPropertyValue<TextTransformType>);
static PropertyValue<std::array<float, 2>> getDefaultTextOffset();
PropertyValue<std::array<float, 2>> getTextOffset() const;
void setTextOffset(PropertyValue<std::array<float, 2>>);
static DataDrivenPropertyValue<std::array<float, 2>> getDefaultTextOffset();
DataDrivenPropertyValue<std::array<float, 2>> getTextOffset() const;
void setTextOffset(DataDrivenPropertyValue<std::array<float, 2>>);
static PropertyValue<bool> getDefaultTextAllowOverlap();
PropertyValue<bool> getTextAllowOverlap() const;

View File

@@ -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 <Z> the zoom parameter type
* @param function a wrapper {@link CameraFunction} for Float[]
* @param <T> the function input type
* @param function a wrapper function for Float[]
* @return property wrapper around a Float[] function
*/
public static <Z extends Number> PropertyValue<CameraFunction<Z, Float[]>> textOffset(CameraFunction<Z, Float[]> function) {
public static <T> PropertyValue<Function<T, Float[]>> textOffset(Function<T, Float[]> function) {
return new LayoutPropertyValue<>("text-offset", function);
}

View File

@@ -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.<Float[]>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);

View File

@@ -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<NSValue *> *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<NSValue *> *textOffset;
#endif

View File

@@ -763,7 +763,7 @@ namespace mbgl {
- (void)setTextOffset:(MGLStyleValue<NSValue *> *)textOffset {
MGLAssertStyleLayerIsValid();
auto mbglValue = MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toInterpolatablePropertyValue(textOffset);
auto mbglValue = MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toDataDrivenPropertyValue(textOffset);
self.rawLayer->setTextOffset(mbglValue);
}
@@ -772,9 +772,9 @@ namespace mbgl {
auto propertyValue = self.rawLayer->getTextOffset();
if (propertyValue.isUndefined()) {
return MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toStyleValue(self.rawLayer->getDefaultTextOffset());
return MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toDataDrivenStyleValue(self.rawLayer->getDefaultTextOffset());
}
return MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toStyleValue(propertyValue);
return MGLStyleValueTransformer<std::array<float, 2>, NSValue *>().toDataDrivenStyleValue(propertyValue);
}
- (void)setTextOptional:(MGLStyleValue<NSNumber *> *)textOptional {

View File

@@ -1158,7 +1158,7 @@
#endif
];
layer.textOffset = constantStyleValue;
mbgl::style::PropertyValue<std::array<float, 2>> propertyValue = { { 1, 1 } };
mbgl::style::DataDrivenPropertyValue<std::array<float, 2>> 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<NSValue *> valueWithInterpolationMode:MGLInterpolationModeExponential sourceStops:@{@18: constantStyleValue} attributeName:@"keyName" options:nil];
layer.textOffset = functionStyleValue;
mbgl::style::ExponentialStops<std::array<float, 2>> exponentialStops = { {{18, { 1, 1 }}}, 1.0 };
propertyValue = mbgl::style::SourceFunction<std::array<float, 2>> { "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<NSValue *> valueWithInterpolationMode:MGLInterpolationModeExponential compositeStops:@{@10: @{@18: constantStyleValue}} attributeName:@"keyName" options:nil];
layer.textOffset = functionStyleValue;
std::map<float, std::array<float, 2>> innerStops { {18, { 1, 1 }} };
mbgl::style::CompositeExponentialStops<std::array<float, 2>> compositeStops { { {10.0, innerStops} }, 1.0 };
propertyValue = mbgl::style::CompositeFunction<std::array<float, 2>> { "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<NSValue *> 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<NSValue *> 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

View File

@@ -250,7 +250,7 @@ void SymbolLayout::prepare(uintptr_t tileUID,
/* verticalAlign */ verticalAlign,
/* justify */ justify,
/* spacing: ems */ layout.get<TextLetterSpacing>() * oneEm,
/* translate */ Point<float>(layout.get<TextOffset>()[0], layout.get<TextOffset>()[1]),
/* translate */ Point<float>(layout.evaluate<TextOffset>(zoom, feature)[0] * oneEm, layout.evaluate<TextOffset>(zoom, feature)[1] * oneEm),
/* verticalHeight */ oneEm,
/* writingMode */ writingMode,
/* bidirectional algorithm object */ bidi);

View File

@@ -483,15 +483,15 @@ void SymbolLayer::setTextTransform(DataDrivenPropertyValue<TextTransformType> va
impl->layout.unevaluated.get<TextTransform>() = value;
impl->observer->onLayerLayoutPropertyChanged(*this, "text-transform");
}
PropertyValue<std::array<float, 2>> SymbolLayer::getDefaultTextOffset() {
DataDrivenPropertyValue<std::array<float, 2>> SymbolLayer::getDefaultTextOffset() {
return TextOffset::defaultValue();
}
PropertyValue<std::array<float, 2>> SymbolLayer::getTextOffset() const {
DataDrivenPropertyValue<std::array<float, 2>> SymbolLayer::getTextOffset() const {
return impl->layout.unevaluated.get<TextOffset>();
}
void SymbolLayer::setTextOffset(PropertyValue<std::array<float, 2>> value) {
void SymbolLayer::setTextOffset(DataDrivenPropertyValue<std::array<float, 2>> value) {
if (value == getTextOffset())
return;
impl->layout.unevaluated.get<TextOffset>() = value;

View File

@@ -160,7 +160,7 @@ struct TextTransform : DataDrivenLayoutProperty<TextTransformType> {
static TextTransformType defaultValue() { return TextTransformType::None; }
};
struct TextOffset : LayoutProperty<std::array<float, 2>> {
struct TextOffset : DataDrivenLayoutProperty<std::array<float, 2>> {
static constexpr const char * key = "text-offset";
static std::array<float, 2> defaultValue() { return {{ 0, 0 }}; }
};

View File

@@ -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<std::u16string> reorderedLines =
bidi.processText(logicalInput,
@@ -66,9 +66,9 @@ void align(Shaping& shaping,
const std::size_t lineCount,
const Point<float>& 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;