...
1
2
3
4
5 package font
6
7 import (
8 "strconv"
9 "strings"
10 )
11
12
13
14 type Length float64
15
16
17 func (l Length) Dots(dpi float64) float64 {
18 return float64(l) / Inch.Points() * dpi
19 }
20
21
22 func (l Length) Points() float64 {
23 return float64(l)
24 }
25
26
27 const (
28 Inch Length = 72
29 Centimeter = Inch / 2.54
30 Millimeter = Centimeter / 10
31 )
32
33
34 func Points(pt float64) Length {
35 return Length(pt)
36 }
37
38
39
40
41
42
43
44
45
46
47
48 func ParseLength(value string) (Length, error) {
49 var unit Length = 1
50 switch {
51 case strings.HasSuffix(value, "in"):
52 value = value[:len(value)-len("in")]
53 unit = Inch
54 case strings.HasSuffix(value, "cm"):
55 value = value[:len(value)-len("cm")]
56 unit = Centimeter
57 case strings.HasSuffix(value, "mm"):
58 value = value[:len(value)-len("mm")]
59 unit = Millimeter
60 case strings.HasSuffix(value, "pt"):
61 value = value[:len(value)-len("pt")]
62 unit = 1
63 }
64 v, err := strconv.ParseFloat(value, 64)
65 if err != nil {
66 return 0, err
67 }
68 return Length(v) * unit, nil
69 }
70
View as plain text