~eliasnaur/gio

ref: dee53b364560 gio/unit/unit.go -rw-r--r-- 1.7 KiB
dee53b36Elias Naur app: fix Windows IME caret positioning 2 months ago
                                                                                
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: Unlicense OR MIT

/*
Package unit implements device independent units.

Device independent pixel, or dp, is the unit for sizes independent of
the underlying display device.

Scaled pixels, or sp, is the unit for text sizes. An sp is like dp with
text scaling applied.

Finally, pixels, or px, is the unit for display dependent pixels. Their
size vary between platforms and displays.

To maintain a constant visual size across platforms and displays, always
use dps or sps to define user interfaces. Only use pixels for derived
values.
*/
package unit

import (
	"math"
)

// Metric converts Values to device-dependent pixels, px. The zero
// value represents a 1-to-1 scale from dp, sp to pixels.
type Metric struct {
	// PxPerDp is the device-dependent pixels per dp.
	PxPerDp float32
	// PxPerSp is the device-dependent pixels per sp.
	PxPerSp float32
}

type (
	// Dp represents device independent pixels. 1 dp will
	// have the same apparent size across platforms and
	// display resolutions.
	Dp float32
	// Sp is like UnitDp but for font sizes.
	Sp float32
)

// Dp converts v to pixels, rounded to the nearest integer value.
func (c Metric) Dp(v Dp) int {
	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 {
	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
}