From 4faa21ad78c41b863ad0ca9b205cf48a025c229e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Ni=C3=9Fl?= Date: Fri, 2 Sep 2022 10:55:40 +1000 Subject: Add PixelAlignCeil and call PixelAlign* to avoid repeated code. --- doc/ScintillaHistory.html | 2 ++ src/Geometry.cxx | 20 ++++++++++++-------- src/Geometry.h | 1 + test/unit/testGeometry.cxx | 13 +++++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/doc/ScintillaHistory.html b/doc/ScintillaHistory.html index 20c932742..e24bd5dc6 100644 --- a/doc/ScintillaHistory.html +++ b/doc/ScintillaHistory.html @@ -571,6 +571,8 @@ Christian Schmitz Michael Berlenz Jacky Yang + + Reinhard Nißl

Releases

diff --git a/src/Geometry.cxx b/src/Geometry.cxx index 97e99d431..c6247f755 100644 --- a/src/Geometry.cxx +++ b/src/Geometry.cxx @@ -75,10 +75,14 @@ XYPOSITION PixelAlignFloor(XYPOSITION xy, int pixelDivisions) noexcept { return std::floor(xy * pixelDivisions) / pixelDivisions; } +XYPOSITION PixelAlignCeil(XYPOSITION xy, int pixelDivisions) noexcept { + return std::ceil(xy * pixelDivisions) / pixelDivisions; +} + Point PixelAlign(const Point &pt, int pixelDivisions) noexcept { return Point( - std::round(pt.x * pixelDivisions) / pixelDivisions, - std::round(pt.y * pixelDivisions) / pixelDivisions); + PixelAlign(pt.x, pixelDivisions), + PixelAlign(pt.y, pixelDivisions)); } PRectangle PixelAlign(const PRectangle &rc, int pixelDivisions) noexcept { @@ -88,19 +92,19 @@ PRectangle PixelAlign(const PRectangle &rc, int pixelDivisions) noexcept { // On retina displays, the positions should be moved to the nearest device // pixel which is the nearest half logical pixel. return PRectangle( - std::round(rc.left * pixelDivisions) / pixelDivisions, + PixelAlign(rc.left, pixelDivisions), PixelAlignFloor(rc.top, pixelDivisions), - std::round(rc.right * pixelDivisions) / pixelDivisions, + PixelAlign(rc.right, pixelDivisions), PixelAlignFloor(rc.bottom, pixelDivisions)); } PRectangle PixelAlignOutside(const PRectangle &rc, int pixelDivisions) noexcept { // Move left and right side to extremes (floor(left) ceil(right)) to avoid blurry visuals. return PRectangle( - std::floor(rc.left * pixelDivisions) / pixelDivisions, - std::floor(rc.top * pixelDivisions) / pixelDivisions, - std::ceil(rc.right * pixelDivisions) / pixelDivisions, - std::floor(rc.bottom * pixelDivisions) / pixelDivisions); + PixelAlignFloor(rc.left, pixelDivisions), + PixelAlignFloor(rc.top, pixelDivisions), + PixelAlignCeil(rc.right, pixelDivisions), + PixelAlignFloor(rc.bottom, pixelDivisions)); } ColourRGBA ColourRGBA::MixedWith(ColourRGBA other) const noexcept { diff --git a/src/Geometry.h b/src/Geometry.h index 5a9b3e291..4fd90afad 100644 --- a/src/Geometry.h +++ b/src/Geometry.h @@ -149,6 +149,7 @@ Interval HorizontalBounds(PRectangle rc) noexcept; XYPOSITION PixelAlign(XYPOSITION xy, int pixelDivisions) noexcept; XYPOSITION PixelAlignFloor(XYPOSITION xy, int pixelDivisions) noexcept; +XYPOSITION PixelAlignCeil(XYPOSITION xy, int pixelDivisions) noexcept; Point PixelAlign(const Point &pt, int pixelDivisions) noexcept; diff --git a/test/unit/testGeometry.cxx b/test/unit/testGeometry.cxx index 6ccd63cd0..4e7b436be 100644 --- a/test/unit/testGeometry.cxx +++ b/test/unit/testGeometry.cxx @@ -148,36 +148,49 @@ TEST_CASE("PRectangle") { // Whole pixels REQUIRE(PixelAlign(1.0, 1) == 1.0); REQUIRE(PixelAlignFloor(1.0, 1) == 1.0); + REQUIRE(PixelAlignCeil(1.0, 1) == 1.0); REQUIRE(PixelAlign(1.25, 1) == 1.0); REQUIRE(PixelAlignFloor(1.25, 1) == 1.0); + REQUIRE(PixelAlignCeil(1.25, 1) == 2.0); REQUIRE(PixelAlign(1.5, 1) == 2.0); REQUIRE(PixelAlignFloor(1.5, 1) == 1.0); + REQUIRE(PixelAlignCeil(1.5, 1) == 2.0); REQUIRE(PixelAlign(1.75, 1) == 2.0); REQUIRE(PixelAlignFloor(1.75, 1) == 1.0); + REQUIRE(PixelAlignCeil(1.75, 1) == 2.0); + + REQUIRE(PixelAlign(Point(1.75, 1.25), 1) == Point(2.0, 1.0)); + REQUIRE(PixelAlign(Point(1.5, 1.0), 1) == Point(2.0, 1.0)); // Half pixels REQUIRE(PixelAlign(1.0, 2) == 1.0); REQUIRE(PixelAlignFloor(1.0, 2) == 1.0); + REQUIRE(PixelAlignCeil(1.0, 2) == 1.0); REQUIRE(PixelAlign(1.25, 2) == 1.5); REQUIRE(PixelAlignFloor(1.25, 2) == 1.0); + REQUIRE(PixelAlignCeil(1.25, 2) == 1.5); REQUIRE(PixelAlign(1.5, 2) == 1.5); REQUIRE(PixelAlignFloor(1.5, 2) == 1.5); + REQUIRE(PixelAlignCeil(1.5, 2) == 1.5); REQUIRE(PixelAlign(1.75, 2) == 2.0); REQUIRE(PixelAlignFloor(1.75, 2) == 1.5); + REQUIRE(PixelAlignCeil(1.75, 2) == 2.0); REQUIRE(PixelAlign(Point(1.75, 1.25), 2) == Point(2.0, 1.5)); REQUIRE(PixelAlign(Point(1.5, 1.0), 2) == Point(1.5, 1.0)); // x->round, y->floored + REQUIRE(PixelAlign(PRectangle(1.0, 1.25, 1.5, 1.75), 1) == PRectangle(1.0, 1.0, 2.0, 1.0)); REQUIRE(PixelAlign(PRectangle(1.0, 1.25, 1.5, 1.75), 2) == PRectangle(1.0, 1.0, 1.5, 1.5)); // x->outside(floor left, ceil right), y->floored + REQUIRE(PixelAlignOutside(PRectangle(1.1, 1.25, 1.6, 1.75), 1) == PRectangle(1.0, 1.0, 2.0, 1.0)); REQUIRE(PixelAlignOutside(PRectangle(1.1, 1.25, 1.6, 1.75), 2) == PRectangle(1.0, 1.0, 2.0, 1.5)); } -- cgit v1.2.3