unit: add Metric.DpToSp and Metric.SpToDp

It's sometimes necessary to specify padding or spacing based on
the text size.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
This commit is contained in:
Egon Elbre
2022-06-09 18:13:45 +03:00
committed by Elias Naur
parent bf6371c8e9
commit 72669e19bc
2 changed files with 51 additions and 10 deletions
+19 -10
View File
@@ -44,18 +44,27 @@ type (
// Dp converts v to pixels, rounded to the nearest integer value.
func (c Metric) Dp(v Dp) int {
s := c.PxPerDp
if s == 0. {
s = 1.
}
return int(math.Round(float64(s) * float64(v)))
return int(math.Round(float64(nonZero(c.PxPerDp)) * float64(v)))
}
// Sp converts v to pixels, rounded to the nearest integer value.
func (c Metric) Sp(v Sp) int {
s := c.PxPerSp
if s == 0. {
s = 1.
}
return int(math.Round(float64(s) * float64(v)))
return int(math.Round(float64(nonZero(c.PxPerSp)) * float64(v)))
}
// DpToSp converts v dp to sp.
func (c Metric) DpToSp(v Dp) Sp {
return Sp(float32(v) * nonZero(c.PxPerDp) / nonZero(c.PxPerSp))
}
// SpToDp converts v sp to dp.
func (c Metric) SpToDp(v Sp) Dp {
return Dp(float32(v) * nonZero(c.PxPerSp) / nonZero(c.PxPerDp))
}
func nonZero(v float32) float32 {
if v == 0. {
return 1
}
return v
}