hf_repo4 / src /core /layout.cpp
fava21's picture
Backup: patch pack applied + verified on Win11 22000 (d446cc1, ba8617e, dcf5cb8)
2edf277 verified
Raw History Blame Contribute Delete
1.71 kB
// 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