File size: 1,706 Bytes
2edf277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// FCS Glass - src/core/layout.cpp
//
// Implements: A2, A3. See src/core/layout.h for the constraints - notably that
// this translation unit must stay free of Windows headers so the portable
// harness can compile the logic on any machine.
#include "core/layout.h"

#include <cmath>

namespace fcs {
namespace core {

// REQ A2 - the single rounding rule, and the one the harness tests hardest.
// std::floor rather than a cast: `(dp * scale) + 0.5` cast to int truncates
// toward zero, which disagrees with round half up for EVERY negative value.
// Measured at scale 1: dp -0.6 and dp -1.4 both returned 0 where round half up
// wants -1. That was BUG-30 in DpToPx, and it survived because the comment
// promised round half up while the body did something else, and every test
// value and every caller happened to be non-negative.
int SnapPx(float dp, float scale) {
  const double v = static_cast<double>(dp) * static_cast<double>(scale);
  return static_cast<int>(std::floor(v + 0.5));
}

// REQ A3 - a negative inset is a caller error, not a shape to honour; it
// degenerates to "no inset" rather than to a negative radius.
int InnerRadiusClamp(int outer_px, int inset_px) {
  if (outer_px < 0) return 0;
  if (inset_px < 0) return outer_px;
  const int room = outer_px - inset_px;
  return room < 0 ? 0 : room;
}

// REQ A3 - height/2 only when the caller opted in. Integer division floors, so a
// 1 px pill is 0 rather than 1, which is correct: half a pixel is no pill.
int PillRadiusPx(int height_px, bool allow_pill) {
  if (!allow_pill) return 0;
  if (height_px < 0) return 0;
  return height_px / 2;
}

}  // namespace core
}  // namespace fcs