-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.go
52 lines (43 loc) · 1.33 KB
/
font.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
// Copyright 2016 Manlio Perillo. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Support for CSS fonts, as specified in
//
// CSS Fonts Module Level 3
//
// Only a simple syntax is supported, and it is different from the one in the
// font shorthand property.
package css
import (
"fmt"
"io"
)
// Font represents a CSS font. Only family, size and line height are
// supported, and they must all be provided. Font family must always be
// quoted.
type Font struct {
Family string
Size Dimension
LineHeight Dimension
}
// String implements the Stringer interface.
func (f Font) String() string {
return fmt.Sprintf("%q %v/%v", f.Family, f.Size, f.LineHeight)
}
// Set implements the Value interface.
func (f *Font) Set(s string) error {
var v Font
_, err := fmt.Sscanf(s, "%q %v/%v", &v.Family, &v.Size, &v.LineHeight)
if err == io.EOF {
return fmt.Errorf("invalid font: %q", s)
} else if err == io.ErrUnexpectedEOF {
// The '/' character was missing.
return fmt.Errorf("invalid font: %q: line height is required", s)
} else if err != nil {
// TODO(mperillo): Improve error message in Font.Set, when Family is
// not quoted and when Size or LineHeight is missing.
return fmt.Errorf("invalid font: %q: %v", s, err)
}
*f = v
return nil
}