-
Notifications
You must be signed in to change notification settings - Fork 0
/
length.go
64 lines (56 loc) · 1.01 KB
/
length.go
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
package gopdf
// LinearMeasure represents a linear measure like mm or inch.
type LinearMeasure int
const (
UnitPt LinearMeasure = iota
UnitMM
UnitInch
)
// Length represents a length using a certain measure
type Length struct {
Value float64
Unit LinearMeasure
}
// MM creates a Length measured in mm
func MM(mm float64) Length {
return Length{
Value: mm,
Unit: UnitMM,
}
}
// Inch creates a Length measured in inch
func Inch(inch float64) Length {
return Length{
Value: inch,
Unit: UnitInch,
}
}
// Pt creates a Length measured in pt
func Pt(pt float64) Length {
return Length{
Value: pt,
Unit: UnitPt,
}
}
// Pt converts the value to pt
func (q Length) Pt() float64 {
switch q.Unit {
case UnitMM:
return q.Value * 2.83464567
case UnitInch:
return q.Value * 72
default:
return q.Value
}
}
// Mm converts the value to millimeters
func (q Length) Mm() float64 {
switch q.Unit {
case UnitPt:
return q.Value * 25.4 / 72
case UnitInch:
return q.Value * 25.4
default:
return q.Value
}
}