Skip to content

Commit 4dc6e58

Browse files
committed
font,vg: migrate vg.Length to font.Length
1 parent a04713a commit 4dc6e58

3 files changed

Lines changed: 81 additions & 39 deletions

File tree

font/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright ©2021 The Gonum Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package font provides types to describe and select text font faces.
6+
package font // import "gonum.org/v1/plot/font"

font/len.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright ©2021 The Gonum Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package font
6+
7+
import (
8+
"strconv"
9+
"strings"
10+
)
11+
12+
// Length is a unit-independent representation of length.
13+
// Internally, the length is stored in postscript points.
14+
type Length float64
15+
16+
// Dots returns the length in dots for the given resolution.
17+
func (l Length) Dots(dpi float64) float64 {
18+
return float64(l) / Inch.Points() * dpi
19+
}
20+
21+
// Points returns the length in postscript points.
22+
func (l Length) Points() float64 {
23+
return float64(l)
24+
}
25+
26+
// Common lengths.
27+
const (
28+
Inch Length = 72
29+
Centimeter = Inch / 2.54
30+
Millimeter = Centimeter / 10
31+
)
32+
33+
// Points returns a length for the given number of points.
34+
func Points(pt float64) Length {
35+
return Length(pt)
36+
}
37+
38+
// ParseLength parses a Length string.
39+
// A Length string is a possible signed floating number with a unit.
40+
// e.g. "42cm" "2.4in" "66pt"
41+
// If no unit was given, ParseLength assumes it was (postscript) points.
42+
// Currently valid units are:
43+
// mm (millimeter)
44+
// cm (centimeter)
45+
// in (inch)
46+
// pt (point)
47+
func 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
68+
}

vg/len.go

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,24 @@
44

55
package 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.
1714
func Points(pt float64) Length {
18-
return Length(pt)
15+
return font.Points(pt)
1916
}
2017

2118
// Common lengths.
2219
const (
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)
4734
func 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

Comments
 (0)