From 199ea2a82a74cf2f7b63078e2dd4b8274c061851 Mon Sep 17 00:00:00 2001 From: Young Hahn Date: Wed, 15 Jun 2016 17:13:31 -0400 Subject: Support for icon-text-fit, icon-text-fit-padding (#5334) * Add support for icon-text-fit * Port unit tests for getIconQuads() from js => cpp * Add support for padding in all 4 directions. * Update all hashes post-merge --- test/text/quads.cpp | 266 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 test/text/quads.cpp (limited to 'test/text') diff --git a/test/text/quads.cpp b/test/text/quads.cpp new file mode 100644 index 0000000000..6fdd769fc3 --- /dev/null +++ b/test/text/quads.cpp @@ -0,0 +1,266 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace mbgl; +using namespace mbgl::style; + +TEST(getIconQuads, normal) { + auto layer = std::make_unique("symbol"); + Anchor anchor(2.0, 3.0, 0.0, 0.5f, 0); + SpriteAtlasElement image = { + Rect( 0, 0, 15, 11 ), + std::shared_ptr(), + 1.0f + }; + PositionedIcon shapedIcon(image, -5.0, 6.0, -7.0, 8.0); + GeometryCoordinates line; + Shaping shapedText; + + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads.size(), 1u); + ASSERT_EQ(quads[0].anchorPoint.x, 2); + ASSERT_EQ(quads[0].anchorPoint.y, 3); + ASSERT_EQ(quads[0].tl.x, -8); + ASSERT_EQ(quads[0].tl.y, -6); + ASSERT_EQ(quads[0].tr.x, 7); + ASSERT_EQ(quads[0].tr.y, -6); + ASSERT_EQ(quads[0].bl.x, -8); + ASSERT_EQ(quads[0].bl.y, 5); + ASSERT_EQ(quads[0].br.x, 7); + ASSERT_EQ(quads[0].br.y, 5); + ASSERT_EQ(quads[0].anchorAngle, 0.0f); + ASSERT_EQ(quads[0].glyphAngle, 0.0f); + ASSERT_EQ(quads[0].minScale, 0.5f); +} + +TEST(getIconQuads, style) { + Anchor anchor(0.0, 0.0, 0.0, 0.5f, 0); + SpriteAtlasElement image = { + Rect( 0, 0, 20, 20 ), + std::shared_ptr(), + 1.0f + }; + PositionedIcon shapedIcon(image, -10.0, 10.0, -10.0, 10.0); + GeometryCoordinates line; + Shaping shapedText; + shapedText.top = -10.0f; + shapedText.bottom = 30.0f; + shapedText.left = -60.0f; + shapedText.right = 20.0f; + shapedText.positionedGlyphs.emplace_back(PositionedGlyph(32, 0.0f, 0.0f)); + + // none + { + auto layer = std::make_unique("symbol"); + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads.size(), 1u); + ASSERT_EQ(quads[0].anchorPoint.x, 0); + ASSERT_EQ(quads[0].anchorPoint.y, 0); + ASSERT_EQ(quads[0].tl.x, -11); + ASSERT_EQ(quads[0].tl.y, -11); + ASSERT_EQ(quads[0].tr.x, 9); + ASSERT_EQ(quads[0].tr.y, -11); + ASSERT_EQ(quads[0].bl.x, -11); + ASSERT_EQ(quads[0].bl.y, 9); + ASSERT_EQ(quads[0].br.x, 9); + ASSERT_EQ(quads[0].br.y, 9); + ASSERT_EQ(quads[0].anchorAngle, 0.0f); + ASSERT_EQ(quads[0].glyphAngle, 0.0f); + ASSERT_EQ(quads[0].minScale, 0.5f); + } + + // width + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(24.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Width); + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -60); + ASSERT_EQ(quads[0].tl.y, 0); + ASSERT_EQ(quads[0].tr.x, 20); + ASSERT_EQ(quads[0].tr.y, 0); + ASSERT_EQ(quads[0].bl.x, -60); + ASSERT_EQ(quads[0].bl.y, 20); + ASSERT_EQ(quads[0].br.x, 20); + ASSERT_EQ(quads[0].br.y, 20); + } + + // width x textSize + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(12.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Width); + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -30); + ASSERT_EQ(quads[0].tl.y, -5); + ASSERT_EQ(quads[0].tr.x, 10); + ASSERT_EQ(quads[0].tr.y, -5); + ASSERT_EQ(quads[0].bl.x, -30); + ASSERT_EQ(quads[0].bl.y, 15); + ASSERT_EQ(quads[0].br.x, 10); + ASSERT_EQ(quads[0].br.y, 15); + } + + // width x textSize + padding + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(12.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Width); + layer->impl->layout.iconTextFitPadding.value[0] = 5.0f; + layer->impl->layout.iconTextFitPadding.value[1] = 10.0f; + layer->impl->layout.iconTextFitPadding.value[2] = 5.0f; + layer->impl->layout.iconTextFitPadding.value[3] = 10.0f; + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -40); + ASSERT_EQ(quads[0].tl.y, -10); + ASSERT_EQ(quads[0].tr.x, 20); + ASSERT_EQ(quads[0].tr.y, -10); + ASSERT_EQ(quads[0].bl.x, -40); + ASSERT_EQ(quads[0].bl.y, 20); + ASSERT_EQ(quads[0].br.x, 20); + ASSERT_EQ(quads[0].br.y, 20); + } + + // height + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(24.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Height); + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -30); + ASSERT_EQ(quads[0].tl.y, -10); + ASSERT_EQ(quads[0].tr.x, -10); + ASSERT_EQ(quads[0].tr.y, -10); + ASSERT_EQ(quads[0].bl.x, -30); + ASSERT_EQ(quads[0].bl.y, 30); + ASSERT_EQ(quads[0].br.x, -10); + ASSERT_EQ(quads[0].br.y, 30); + } + + // height x textSize + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(12.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Height); + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -20); + ASSERT_EQ(quads[0].tl.y, -5); + ASSERT_EQ(quads[0].tr.x, 0); + ASSERT_EQ(quads[0].tr.y, -5); + ASSERT_EQ(quads[0].bl.x, -20); + ASSERT_EQ(quads[0].bl.y, 15); + ASSERT_EQ(quads[0].br.x, 0); + ASSERT_EQ(quads[0].br.y, 15); + } + + // height x textSize + padding + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(12.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Height); + layer->impl->layout.iconTextFitPadding.value[0] = 5.0f; + layer->impl->layout.iconTextFitPadding.value[1] = 10.0f; + layer->impl->layout.iconTextFitPadding.value[2] = 5.0f; + layer->impl->layout.iconTextFitPadding.value[3] = 10.0f; + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -30); + ASSERT_EQ(quads[0].tl.y, -10); + ASSERT_EQ(quads[0].tr.x, 10); + ASSERT_EQ(quads[0].tr.y, -10); + ASSERT_EQ(quads[0].bl.x, -30); + ASSERT_EQ(quads[0].bl.y, 20); + ASSERT_EQ(quads[0].br.x, 10); + ASSERT_EQ(quads[0].br.y, 20); + } + + // both + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(24.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Both); + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -60); + ASSERT_EQ(quads[0].tl.y, -10); + ASSERT_EQ(quads[0].tr.x, 20); + ASSERT_EQ(quads[0].tr.y, -10); + ASSERT_EQ(quads[0].bl.x, -60); + ASSERT_EQ(quads[0].bl.y, 30); + ASSERT_EQ(quads[0].br.x, 20); + ASSERT_EQ(quads[0].br.y, 30); + } + + // both x textSize + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(12.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Both); + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -30); + ASSERT_EQ(quads[0].tl.y, -5); + ASSERT_EQ(quads[0].tr.x, 10); + ASSERT_EQ(quads[0].tr.y, -5); + ASSERT_EQ(quads[0].bl.x, -30); + ASSERT_EQ(quads[0].bl.y, 15); + ASSERT_EQ(quads[0].br.x, 10); + ASSERT_EQ(quads[0].br.y, 15); + } + + // both x textSize + padding + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(12.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Both); + layer->impl->layout.iconTextFitPadding.value[0] = 5.0f; + layer->impl->layout.iconTextFitPadding.value[1] = 10.0f; + layer->impl->layout.iconTextFitPadding.value[2] = 5.0f; + layer->impl->layout.iconTextFitPadding.value[3] = 10.0f; + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -40); + ASSERT_EQ(quads[0].tl.y, -10); + ASSERT_EQ(quads[0].tr.x, 20); + ASSERT_EQ(quads[0].tr.y, -10); + ASSERT_EQ(quads[0].bl.x, -40); + ASSERT_EQ(quads[0].bl.y, 20); + ASSERT_EQ(quads[0].br.x, 20); + ASSERT_EQ(quads[0].br.y, 20); + } + + // both x textSize + padding t/r/b/l + { + auto layer = std::make_unique("symbol"); + layer->impl->layout.textSize = LayoutProperty(12.0f); + layer->impl->layout.iconTextFit = LayoutProperty(IconTextFitType::Both); + layer->impl->layout.iconTextFitPadding.value[0] = 0.0f; + layer->impl->layout.iconTextFitPadding.value[1] = 5.0f; + layer->impl->layout.iconTextFitPadding.value[2] = 10.0f; + layer->impl->layout.iconTextFitPadding.value[3] = 15.0f; + SymbolQuads quads = getIconQuads(anchor, shapedIcon, line, layer->impl->layout, false, shapedText); + + ASSERT_EQ(quads[0].tl.x, -45); + ASSERT_EQ(quads[0].tl.y, -5); + ASSERT_EQ(quads[0].tr.x, 15); + ASSERT_EQ(quads[0].tr.y, -5); + ASSERT_EQ(quads[0].bl.x, -45); + ASSERT_EQ(quads[0].bl.y, 25); + ASSERT_EQ(quads[0].br.x, 15); + ASSERT_EQ(quads[0].br.y, 25); + } +} + -- cgit v1.2.1