From 2a61c4ec539914336cdf20c3be967af2a7a4eff4 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 9 Mar 2020 16:32:33 -0700 Subject: [PATCH] Fabric: `roundToPixel<>()` convenience functions Summary: A bunch of convenience functions for rounding float values to be aligned to the device pixel grid. We had something like this on iOS, now we need to have that in xplat C++ code. Changelog: [Internal] Fabric-specific internal change. Reviewed By: sammy-SC Differential Revision: D20268044 fbshipit-source-id: 980b61c793eaeafb160e2b3c1d9742402800cd47 --- ReactCommon/fabric/graphics/rounding.h | 83 ++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 ReactCommon/fabric/graphics/rounding.h diff --git a/ReactCommon/fabric/graphics/rounding.h b/ReactCommon/fabric/graphics/rounding.h new file mode 100644 index 00000000000..2ef95e3721f --- /dev/null +++ b/ReactCommon/fabric/graphics/rounding.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include + +namespace facebook { +namespace react { + +/* + * Convenience functions for rounding float values to be aligned with a device + * pixel grid. + * + * Usage example: + * auto scaleFactor = Float{3}; + * auto value = Float{42.9001}; + * auto crispValue = roundToPixel<&std::ceil>(value, scaleFactor); + * auto size = Size{value, value + 42.0}; + * auto crispSize = roundToPixel<&std::ceil>(size, scaleFactor); + */ +template +Float roundToPixel(Float value, Float scaleFactor) { + return RoundingFunction(value * scaleFactor) / scaleFactor; +} + +template +Point roundToPixel(Point value, Float scaleFactor) { + return Point{roundToPixel(value.x, scaleFactor), + roundToPixel(value.y, scaleFactor)}; +} + +template +Size roundToPixel(Size value, Float scaleFactor) { + return Size{roundToPixel(value.width, scaleFactor), + roundToPixel(value.height, scaleFactor)}; +} + +template +Rect roundToPixel(Rect value, Float scaleFactor) { + return Rect{roundToPixel(value.origin), + roundToPixel(value.size)}; +} + +/* + * GCC-based Android NDK does not have rounding functions as part of STL. + */ +#include + +inline float round(float value) noexcept { + return ::roundf(value); +} +inline double round(double value) noexcept { + return ::round(value); +} +inline long double round(long double value) noexcept { + return ::roundl(value); +} +inline float ceil(float value) noexcept { + return ::ceilf(value); +} +inline double ceil(double value) noexcept { + return ::ceil(value); +} +inline long double ceil(long double value) noexcept { + return ::ceill(value); +} +inline float floor(float value) noexcept { + return ::floorf(value); +} +inline double floor(double value) noexcept { + return ::floor(value); +} +inline long double floor(long double value) noexcept { + return ::floorl(value); +} + +} // namespace react +} // namespace facebook