MCPcopy Create free account
hub / github.com/Snapchat/Valdi / roundValueToPixelGrid

Function roundValueToPixelGrid

third-party/yoga/src/yoga/algorithm/PixelGrid.cpp:15–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13namespace facebook::yoga {
14
15float roundValueToPixelGrid(
16 const double value,
17 const double pointScaleFactor,
18 const bool forceCeil,
19 const bool forceFloor) {
20 double scaledValue = value * pointScaleFactor;
21 // We want to calculate `fractial` such that `floor(scaledValue) = scaledValue
22 // - fractial`.
23 double fractial = fmod(scaledValue, 1.0);
24 if (fractial < 0) {
25 // This branch is for handling negative numbers for `value`.
26 //
27 // Regarding `floor` and `ceil`. Note that for a number x, `floor(x) <= x <=
28 // ceil(x)` even for negative numbers. Here are a couple of examples:
29 // - x = 2.2: floor( 2.2) = 2, ceil( 2.2) = 3
30 // - x = -2.2: floor(-2.2) = -3, ceil(-2.2) = -2
31 //
32 // Regarding `fmodf`. For fractional negative numbers, `fmodf` returns a
33 // negative number. For example, `fmodf(-2.2) = -0.2`. However, we want
34 // `fractial` to be the number such that subtracting it from `value` will
35 // give us `floor(value)`. In the case of negative numbers, adding 1 to
36 // `fmodf(value)` gives us this. Let's continue the example from above:
37 // - fractial = fmodf(-2.2) = -0.2
38 // - Add 1 to the fraction: fractial2 = fractial + 1 = -0.2 + 1 = 0.8
39 // - Finding the `floor`: -2.2 - fractial2 = -2.2 - 0.8 = -3
40 ++fractial;
41 }
42 if (yoga::inexactEquals(fractial, 0)) {
43 // First we check if the value is already rounded
44 scaledValue = scaledValue - fractial;
45 } else if (yoga::inexactEquals(fractial, 1.0)) {
46 scaledValue = scaledValue - fractial + 1.0;
47 } else if (forceCeil) {
48 // Next we check if we need to use forced rounding
49 scaledValue = scaledValue - fractial + 1.0;
50 } else if (forceFloor) {
51 scaledValue = scaledValue - fractial;
52 } else {
53 // Finally we just round the value
54 scaledValue = scaledValue - fractial +
55 (!std::isnan(fractial) &&
56 (fractial > 0.5 || yoga::inexactEquals(fractial, 0.5))
57 ? 1.0
58 : 0.0);
59 }
60 return (std::isnan(scaledValue) || std::isnan(pointScaleFactor))
61 ? YGUndefined
62 : (float)(scaledValue / pointScaleFactor);
63}
64
65void roundLayoutResultsToPixelGrid(
66 yoga::Node* const node,

Callers 3

YGRoundValueToPixelGridFunction · 0.85
canUseCachedMeasurementFunction · 0.85

Calls 1

inexactEqualsFunction · 0.50

Tested by

no test coverage detected