diff --git a/modules/juce_graphics/geometry/juce_Rectangle.h b/modules/juce_graphics/geometry/juce_Rectangle.h index 357904d736..780f46dfc5 100644 --- a/modules/juce_graphics/geometry/juce_Rectangle.h +++ b/modules/juce_graphics/geometry/juce_Rectangle.h @@ -26,6 +26,34 @@ namespace juce { +#ifndef DOXYGEN +namespace detail +{ + +template struct Tag {}; + +inline auto getNumericValue (StringRef s, Tag) { return s.text.getIntValue32(); } +inline auto getNumericValue (StringRef s, Tag) { return s.text.getDoubleValue(); } +inline auto getNumericValue (StringRef s, Tag) { return static_cast (s.text.getDoubleValue()); } + +template +ValueType parseAfterSpace (StringRef s) noexcept +{ + return static_cast (getNumericValue (s.text.findEndOfWhitespace(), + Tag{})); +} + +inline int floorAsInt (int n) noexcept { return n; } +inline int floorAsInt (float n) noexcept { return n > (float) std::numeric_limits::min() ? (int) std::floor (n) : std::numeric_limits::min(); } +inline int floorAsInt (double n) noexcept { return n > (double) std::numeric_limits::min() ? (int) std::floor (n) : std::numeric_limits::min(); } + +inline int ceilAsInt (int n) noexcept { return n; } +inline int ceilAsInt (float n) noexcept { return n < (float) std::numeric_limits::max() ? (int) std::ceil (n) : std::numeric_limits::max(); } +inline int ceilAsInt (double n) noexcept { return n < (double) std::numeric_limits::max() ? (int) std::ceil (n) : std::numeric_limits::max(); } + +} // namespace detail +#endif + //============================================================================== /** Manages a rectangle and allows geometric operations to be performed on it. @@ -811,10 +839,10 @@ public: */ Rectangle getSmallestIntegerContainer() const noexcept { - return Rectangle::leftTopRightBottom (floorAsInt (pos.x), - floorAsInt (pos.y), - ceilAsInt (pos.x + w), - ceilAsInt (pos.y + h)); + return Rectangle::leftTopRightBottom (detail::floorAsInt (pos.x), + detail::floorAsInt (pos.y), + detail::ceilAsInt (pos.x + w), + detail::ceilAsInt (pos.y + h)); } /** Casts this rectangle to a Rectangle. @@ -937,7 +965,7 @@ public: /** Parses a string containing a rectangle's details. - The string should contain 4 integer tokens, in the form "x y width height". They + The string should contain 4 numeric tokens, in the form "x y width height". They can be comma or whitespace separated. This method is intended to go with the toString() method, to form an easy way @@ -950,10 +978,10 @@ public: StringArray toks; toks.addTokens (stringVersion.text.findEndOfWhitespace(), ",; \t\r\n", ""); - return { parseIntAfterSpace (toks[0]), - parseIntAfterSpace (toks[1]), - parseIntAfterSpace (toks[2]), - parseIntAfterSpace (toks[3]) }; + return { detail::parseAfterSpace (toks[0]), + detail::parseAfterSpace (toks[1]), + detail::parseAfterSpace (toks[2]), + detail::parseAfterSpace (toks[3]) }; } #ifndef DOXYGEN @@ -967,19 +995,9 @@ private: Point pos; ValueType w {}, h {}; - static ValueType parseIntAfterSpace (StringRef s) noexcept - { return static_cast (s.text.findEndOfWhitespace().getIntValue32()); } - - void copyWithRounding (Rectangle& result) const noexcept { result = getSmallestIntegerContainer(); } - void copyWithRounding (Rectangle& result) const noexcept { result = toFloat(); } + void copyWithRounding (Rectangle& result) const noexcept { result = getSmallestIntegerContainer(); } + void copyWithRounding (Rectangle& result) const noexcept { result = toFloat(); } void copyWithRounding (Rectangle& result) const noexcept { result = toDouble(); } - - static int floorAsInt (int n) noexcept { return n; } - static int floorAsInt (float n) noexcept { return n > (float) std::numeric_limits::min() ? (int) std::floor (n) : std::numeric_limits::min(); } - static int floorAsInt (double n) noexcept { return n > (double) std::numeric_limits::min() ? (int) std::floor (n) : std::numeric_limits::min(); } - static int ceilAsInt (int n) noexcept { return n; } - static int ceilAsInt (float n) noexcept { return n < (float) std::numeric_limits::max() ? (int) std::ceil (n) : std::numeric_limits::max(); } - static int ceilAsInt (double n) noexcept { return n < (double) std::numeric_limits::max() ? (int) std::ceil (n) : std::numeric_limits::max(); } }; } // namespace juce diff --git a/modules/juce_graphics/geometry/juce_Rectangle_test.cpp b/modules/juce_graphics/geometry/juce_Rectangle_test.cpp new file mode 100644 index 0000000000..fe9a8fe4d3 --- /dev/null +++ b/modules/juce_graphics/geometry/juce_Rectangle_test.cpp @@ -0,0 +1,51 @@ +/* + ============================================================================== + + This file is part of the JUCE library. + Copyright (c) 2020 - Raw Material Software Limited + + JUCE is an open source library subject to commercial or open-source + licensing. + + By using JUCE, you agree to the terms of both the JUCE 6 End-User License + Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020). + + End User License Agreement: www.juce.com/juce-6-licence + Privacy Policy: www.juce.com/juce-privacy-policy + + Or: You may also use this code under the terms of the GPL v3 (see + www.gnu.org/licenses). + + JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER + EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE + DISCLAIMED. + + ============================================================================== +*/ + +namespace juce +{ + +struct RectangleUnitTest : public UnitTest +{ + RectangleUnitTest() : UnitTest ("Rectangle", UnitTestCategories::graphics) {} + + void runTest() override + { + beginTest ("Rectangle/string conversions can be round-tripped"); + { + const Rectangle a (0.1f, 0.2f, 0.3f, 0.4f); + expect (Rectangle::fromString (a.toString()) == a); + + const Rectangle b (0.1, 0.2, 0.3, 0.4); + expect (Rectangle::fromString (b.toString()) == b); + + const Rectangle c (1, 2, 3, 4); + expect (Rectangle::fromString (c.toString()) == c); + } + } +}; + +static RectangleUnitTest rectangleUnitTest; + +} // namespace juce diff --git a/modules/juce_graphics/juce_graphics.cpp b/modules/juce_graphics/juce_graphics.cpp index 8c30c8c5fe..77d8270802 100644 --- a/modules/juce_graphics/juce_graphics.cpp +++ b/modules/juce_graphics/juce_graphics.cpp @@ -126,6 +126,10 @@ #include "effects/juce_DropShadowEffect.cpp" #include "effects/juce_GlowEffect.cpp" +#if JUCE_UNIT_TESTS + #include "geometry/juce_Rectangle_test.cpp" +#endif + #if JUCE_USE_FREETYPE #include "native/juce_freetype_Fonts.cpp" #endif