44
55package vg
66
7- import (
8- "strconv"
9- "strings"
10- )
7+ import "gonum.org/v1/plot/font"
118
129// A Length is a unit-independent representation of length.
1310// Internally, the length is stored in postscript points.
14- type Length float64
11+ type Length = font. Length
1512
1613// Points returns a length for the given number of points.
1714func Points (pt float64 ) Length {
18- return Length (pt )
15+ return font . Points (pt )
1916}
2017
2118// Common lengths.
2219const (
23- Inch Length = 72
24- Centimeter = Inch / 2.54
25- Millimeter = Centimeter / 10
20+ Inch = font . Inch
21+ Centimeter = font . Centimeter
22+ Millimeter = font . Millimeter
2623)
2724
28- // Dots returns the length in dots for the given resolution.
29- func (l Length ) Dots (dpi float64 ) float64 {
30- return float64 (l ) / Inch .Points () * dpi
31- }
32-
33- // Points returns the length in postscript points.
34- func (l Length ) Points () float64 {
35- return float64 (l )
36- }
37-
3825// ParseLength parses a Length string.
3926// A Length string is a possible signed floating number with a unit.
4027// e.g. "42cm" "2.4in" "66pt"
@@ -45,24 +32,5 @@ func (l Length) Points() float64 {
4532// in (inch)
4633// pt (point)
4734func ParseLength (value string ) (Length , error ) {
48- var unit Length = 1
49- switch {
50- case strings .HasSuffix (value , "in" ):
51- value = value [:len (value )- len ("in" )]
52- unit = Inch
53- case strings .HasSuffix (value , "cm" ):
54- value = value [:len (value )- len ("cm" )]
55- unit = Centimeter
56- case strings .HasSuffix (value , "mm" ):
57- value = value [:len (value )- len ("mm" )]
58- unit = Millimeter
59- case strings .HasSuffix (value , "pt" ):
60- value = value [:len (value )- len ("pt" )]
61- unit = 1
62- }
63- v , err := strconv .ParseFloat (value , 64 )
64- if err != nil {
65- return 0 , err
66- }
67- return Length (v ) * unit , nil
35+ return font .ParseLength (value )
6836}
0 commit comments