From 2187980a871cc22f59c1078a949e42db0ab7c1a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Mon, 14 Oct 2019 15:16:14 +0200 Subject: [core] fix icon-text-fit This fixes rendering by account for the 1px texture padding around icons that were stretched with icon-text-fit. We've added the 1px padding before, but didn't scale it accordingly when we are resizing the icon when it is stretched to fit the text. Adjusts the code to match the logic in GL JS. --- test/text/quads.test.cpp | 329 ++++++++++++++++++++--------------------------- 1 file changed, 139 insertions(+), 190 deletions(-) (limited to 'test/text') diff --git a/test/text/quads.test.cpp b/test/text/quads.test.cpp index 7aaeb4870d..b04617a40b 100644 --- a/test/text/quads.test.cpp +++ b/test/text/quads.test.cpp @@ -36,10 +36,8 @@ TEST(getIconQuads, normal) { TEST(getIconQuads, style) { Anchor anchor(0.0, 0.0, 0.0, 0.5f, 0); - ImagePosition image = { - mapbox::Bin(-1, 20, 20, 0, 0, 0, 0), - style::Image::Impl("test", PremultipliedImage({1,1}), 1.0) - }; + const ImagePosition image = {mapbox::Bin(-1, 20, 20, 0, 0, 0, 0), + style::Image::Impl("test", PremultipliedImage({1, 1}), 1.0)}; GeometryCoordinates line; Shaping shapedText; @@ -51,235 +49,186 @@ TEST(getIconQuads, style) { // none { - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + + EXPECT_FLOAT_EQ(-18.5f, shapedIcon.top()); + EXPECT_FLOAT_EQ(-0.5f, shapedIcon.right()); + EXPECT_FLOAT_EQ(-0.5f, shapedIcon.bottom()); + EXPECT_FLOAT_EQ(-18.5f, shapedIcon.left()); + SymbolLayoutProperties::Evaluated layout; - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -19.5); - EXPECT_EQ(quad.tl.y, -19.5); - EXPECT_EQ(quad.tr.x, 0.5); - EXPECT_EQ(quad.tr.y, -19.5); - EXPECT_EQ(quad.bl.x, -19.5); - EXPECT_EQ(quad.bl.y, 0.5); - EXPECT_EQ(quad.br.x, 0.5); - EXPECT_EQ(quad.br.y, 0.5); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -19.5); + EXPECT_FLOAT_EQ(quad.tl.y, -19.5); + EXPECT_FLOAT_EQ(quad.tr.x, 0.5); + EXPECT_FLOAT_EQ(quad.tr.y, -19.5); + EXPECT_FLOAT_EQ(quad.bl.x, -19.5); + EXPECT_FLOAT_EQ(quad.bl.y, 0.5); + EXPECT_FLOAT_EQ(quad.br.x, 0.5); + EXPECT_FLOAT_EQ(quad.br.y, 0.5); } // width { - SymbolLayoutProperties::Evaluated layout; - layout.get() = 24.0f; - layout.get() = IconTextFitType::Width; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 24.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -61); - EXPECT_EQ(quad.tl.y, 0); - EXPECT_EQ(quad.tr.x, 21); - EXPECT_EQ(quad.tr.y, 0); - EXPECT_EQ(quad.bl.x, -61); - EXPECT_EQ(quad.bl.y, 20); - EXPECT_EQ(quad.br.x, 21); - EXPECT_EQ(quad.br.y, 20); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Width, {{0, 0, 0, 0}}, {{0, 0}}, 24.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -64.4444427); + EXPECT_FLOAT_EQ(quad.tl.y, 0); + EXPECT_FLOAT_EQ(quad.tr.x, 24.4444427); + EXPECT_FLOAT_EQ(quad.tr.y, 0); + EXPECT_FLOAT_EQ(quad.bl.x, -64.4444427); + EXPECT_FLOAT_EQ(quad.bl.y, 20); + EXPECT_FLOAT_EQ(quad.br.x, 24.4444427); + EXPECT_FLOAT_EQ(quad.br.y, 20); } // width x textSize { - SymbolLayoutProperties::Evaluated layout; - layout.get() = 12.0f; - layout.get() = IconTextFitType::Width; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 12.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -31); - EXPECT_EQ(quad.tl.y, -5); - EXPECT_EQ(quad.tr.x, 11); - EXPECT_EQ(quad.tr.y, -5); - EXPECT_EQ(quad.bl.x, -31); - EXPECT_EQ(quad.bl.y, 15); - EXPECT_EQ(quad.br.x, 11); - EXPECT_EQ(quad.br.y, 15); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Width, {{0, 0, 0, 0}}, {{0, 0}}, 12.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -32.2222214); + EXPECT_FLOAT_EQ(quad.tl.y, -5); + EXPECT_FLOAT_EQ(quad.tr.x, 12.2222214); + EXPECT_FLOAT_EQ(quad.tr.y, -5); + EXPECT_FLOAT_EQ(quad.bl.x, -32.2222214); + EXPECT_FLOAT_EQ(quad.bl.y, 15); + EXPECT_FLOAT_EQ(quad.br.x, 12.2222214); + EXPECT_FLOAT_EQ(quad.br.y, 15); } // width x textSize + padding { - SymbolLayoutProperties::Evaluated layout; - layout.get() = 12.0f; - layout.get() = IconTextFitType::Width; - layout.get()[0] = 5.0f; - layout.get()[1] = 10.0f; - layout.get()[2] = 5.0f; - layout.get()[3] = 10.0f; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 12.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -41); - EXPECT_EQ(quad.tl.y, -10); - EXPECT_EQ(quad.tr.x, 21); - EXPECT_EQ(quad.tr.y, -10); - EXPECT_EQ(quad.bl.x, -41); - EXPECT_EQ(quad.bl.y, 20); - EXPECT_EQ(quad.br.x, 21); - EXPECT_EQ(quad.br.y, 20); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Width, {{5, 10, 5, 10}}, {{0, 0}}, 12.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -43.3333321); + EXPECT_FLOAT_EQ(quad.tl.y, -5); + EXPECT_FLOAT_EQ(quad.tr.x, 23.3333321); + EXPECT_FLOAT_EQ(quad.tr.y, -5); + EXPECT_FLOAT_EQ(quad.bl.x, -43.3333321); + EXPECT_FLOAT_EQ(quad.bl.y, 15); + EXPECT_FLOAT_EQ(quad.br.x, 23.3333321); + EXPECT_FLOAT_EQ(quad.br.y, 15); } // height { - SymbolLayoutProperties::Evaluated layout; - layout.get() = 24.0f; - layout.get() = IconTextFitType::Height; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 24.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -30); - EXPECT_EQ(quad.tl.y, -11); - EXPECT_EQ(quad.tr.x, -10); - EXPECT_EQ(quad.tr.y, -11); - EXPECT_EQ(quad.bl.x, -30); - EXPECT_EQ(quad.bl.y, 31); - EXPECT_EQ(quad.br.x, -10); - EXPECT_EQ(quad.br.y, 31); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Height, {{0, 0, 0, 0}}, {{0, 0}}, 24.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -30); + EXPECT_FLOAT_EQ(quad.tl.y, -12.2222214); + EXPECT_FLOAT_EQ(quad.tr.x, -10); + EXPECT_FLOAT_EQ(quad.tr.y, -12.2222214); + EXPECT_FLOAT_EQ(quad.bl.x, -30); + EXPECT_FLOAT_EQ(quad.bl.y, 32.2222214); + EXPECT_FLOAT_EQ(quad.br.x, -10); + EXPECT_FLOAT_EQ(quad.br.y, 32.2222214); } // height x textSize { SymbolLayoutProperties::Evaluated layout; - layout.get() = 12.0f; - layout.get() = IconTextFitType::Height; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 12.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -20); - EXPECT_EQ(quad.tl.y, -6); - EXPECT_EQ(quad.tr.x, 0); - EXPECT_EQ(quad.tr.y, -6); - EXPECT_EQ(quad.bl.x, -20); - EXPECT_EQ(quad.bl.y, 16); - EXPECT_EQ(quad.br.x, 0); - EXPECT_EQ(quad.br.y, 16); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Height, {{0, 0, 0, 0}}, {{0, 0}}, 12.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -20); + EXPECT_FLOAT_EQ(quad.tl.y, -6.11111069); + EXPECT_FLOAT_EQ(quad.tr.x, 0); + EXPECT_FLOAT_EQ(quad.tr.y, -6.11111069); + EXPECT_FLOAT_EQ(quad.bl.x, -20); + EXPECT_FLOAT_EQ(quad.bl.y, 16.1111107); + EXPECT_FLOAT_EQ(quad.br.x, 0); + EXPECT_FLOAT_EQ(quad.br.y, 16.1111107); } // height x textSize + padding { - SymbolLayoutProperties::Evaluated layout; - layout.get() = 12.0f; - layout.get() = IconTextFitType::Height; - layout.get()[0] = 5.0f; - layout.get()[1] = 10.0f; - layout.get()[2] = 5.0f; - layout.get()[3] = 10.0f; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 12.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -30); - EXPECT_EQ(quad.tl.y, -11); - EXPECT_EQ(quad.tr.x, 10); - EXPECT_EQ(quad.tr.y, -11); - EXPECT_EQ(quad.bl.x, -30); - EXPECT_EQ(quad.bl.y, 21); - EXPECT_EQ(quad.br.x, 10); - EXPECT_EQ(quad.br.y, 21); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Height, {{5, 10, 5, 20}}, {{0, 0}}, 12.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -20); + EXPECT_FLOAT_EQ(quad.tl.y, -11.666666); + EXPECT_FLOAT_EQ(quad.tr.x, 0); + EXPECT_FLOAT_EQ(quad.tr.y, -11.666666); + EXPECT_FLOAT_EQ(quad.bl.x, -20); + EXPECT_FLOAT_EQ(quad.bl.y, 21.666666); + EXPECT_FLOAT_EQ(quad.br.x, 0); + EXPECT_FLOAT_EQ(quad.br.y, 21.666666); } // both { - SymbolLayoutProperties::Evaluated layout; - layout.get() = 24.0f; - layout.get() = IconTextFitType::Both; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 24.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -61); - EXPECT_EQ(quad.tl.y, -11); - EXPECT_EQ(quad.tr.x, 21); - EXPECT_EQ(quad.tr.y, -11); - EXPECT_EQ(quad.bl.x, -61); - EXPECT_EQ(quad.bl.y, 31); - EXPECT_EQ(quad.br.x, 21); - EXPECT_EQ(quad.br.y, 31); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Both, {{0, 0, 0, 0}}, {{0, 0}}, 24.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -64.4444427); + EXPECT_FLOAT_EQ(quad.tl.y, -12.2222214); + EXPECT_FLOAT_EQ(quad.tr.x, 24.4444427); + EXPECT_FLOAT_EQ(quad.tr.y, -12.2222214); + EXPECT_FLOAT_EQ(quad.bl.x, -64.4444427); + EXPECT_FLOAT_EQ(quad.bl.y, 32.2222214); + EXPECT_FLOAT_EQ(quad.br.x, 24.4444427); + EXPECT_FLOAT_EQ(quad.br.y, 32.2222214); } // both x textSize { - SymbolLayoutProperties::Evaluated layout; - layout.get() = 12.0f; - layout.get() = IconTextFitType::Both; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 12.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -31); - EXPECT_EQ(quad.tl.y, -6); - EXPECT_EQ(quad.tr.x, 11); - EXPECT_EQ(quad.tr.y, -6); - EXPECT_EQ(quad.bl.x, -31); - EXPECT_EQ(quad.bl.y, 16); - EXPECT_EQ(quad.br.x, 11); - EXPECT_EQ(quad.br.y, 16); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Both, {{0, 0, 0, 0}}, {{0, 0}}, 12.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -32.2222214); + EXPECT_FLOAT_EQ(quad.tl.y, -6.11111069); + EXPECT_FLOAT_EQ(quad.tr.x, 12.2222214); + EXPECT_FLOAT_EQ(quad.tr.y, -6.11111069); + EXPECT_FLOAT_EQ(quad.bl.x, -32.2222214); + EXPECT_FLOAT_EQ(quad.bl.y, 16.1111107); + EXPECT_FLOAT_EQ(quad.br.x, 12.2222214); + EXPECT_FLOAT_EQ(quad.br.y, 16.1111107); } // both x textSize + padding { - SymbolLayoutProperties::Evaluated layout; - layout.get() = 12.0f; - layout.get() = IconTextFitType::Both; - layout.get()[0] = 5.0f; - layout.get()[1] = 10.0f; - layout.get()[2] = 5.0f; - layout.get()[3] = 10.0f; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 12.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -41); - EXPECT_EQ(quad.tl.y, -11); - EXPECT_EQ(quad.tr.x, 21); - EXPECT_EQ(quad.tr.y, -11); - EXPECT_EQ(quad.bl.x, -41); - EXPECT_EQ(quad.bl.y, 21); - EXPECT_EQ(quad.br.x, 21); - EXPECT_EQ(quad.br.y, 21); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Both, {{5, 10, 5, 10}}, {{0, 0}}, 12.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -43.3333321); + EXPECT_FLOAT_EQ(quad.tl.y, -11.666666); + EXPECT_FLOAT_EQ(quad.tr.x, 23.3333321); + EXPECT_FLOAT_EQ(quad.tr.y, -11.666666); + EXPECT_FLOAT_EQ(quad.bl.x, -43.3333321); + EXPECT_FLOAT_EQ(quad.bl.y, 21.666666); + EXPECT_FLOAT_EQ(quad.br.x, 23.3333321); + EXPECT_FLOAT_EQ(quad.br.y, 21.666666); } // both x textSize + padding t/r/b/l { SymbolLayoutProperties::Evaluated layout; layout.get() = 12.0f; - layout.get() = IconTextFitType::Both; - layout.get()[0] = 0.0f; - layout.get()[1] = 5.0f; - layout.get()[2] = 10.0f; - layout.get()[3] = 15.0f; - auto shapedIcon = PositionedIcon::shapeIcon(image, {{ -9.5f, -9.5f }}, SymbolAnchorType::Center, 0); - shapedIcon.fitIconToText(layout, shapedText, 12.0f); - SymbolQuad quad = - getIconQuad(shapedIcon, WritingModeType::Horizontal); - - EXPECT_EQ(quad.tl.x, -46); - EXPECT_EQ(quad.tl.y, -6); - EXPECT_EQ(quad.tr.x, 16); - EXPECT_EQ(quad.tr.y, -6); - EXPECT_EQ(quad.bl.x, -46); - EXPECT_EQ(quad.bl.y, 26); - EXPECT_EQ(quad.br.x, 16); - EXPECT_EQ(quad.br.y, 26); + auto shapedIcon = PositionedIcon::shapeIcon(image, {{-9.5f, -9.5f}}, SymbolAnchorType::Center, 0); + shapedIcon.fitIconToText(shapedText, IconTextFitType::Both, {{0, 5, 10, 15}}, {{0, 0}}, 12.0f / 24.0f); + SymbolQuad quad = getIconQuad(shapedIcon, WritingModeType::Horizontal); + + EXPECT_FLOAT_EQ(quad.tl.x, -48.3333321); + EXPECT_FLOAT_EQ(quad.tl.y, -6.66666603); + EXPECT_FLOAT_EQ(quad.tr.x, 18.3333321); + EXPECT_FLOAT_EQ(quad.tr.y, -6.66666603); + EXPECT_FLOAT_EQ(quad.bl.x, -48.3333321); + EXPECT_FLOAT_EQ(quad.bl.y, 26.666666); + EXPECT_FLOAT_EQ(quad.br.x, 18.3333321); + EXPECT_FLOAT_EQ(quad.br.y, 26.666666); } } - -- cgit v1.2.1