aboutsummaryrefslogtreecommitdiff
path: root/units.go
blob: fbcd7569e6f171d655de0c59de475022435e0f2c (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package robo

import (
	"fmt"
	"strings"
)

const (
	MM = Unit(20.0)
	CM = 10 * MM
	M  = 100 * CM
	IN = 25.4 * MM
	FT = 12 * IN
	PT = IN / 72
)

var A4 = Point{272 * MM, 200 * MM}

// TODO
type Page struct {
	o  Orientation
	sz Point // A4
	m  Media
}

type Unit float64

func (u Unit) String() string {
	if u == Unit(int(u)) {
		return fmt.Sprint(int(u))
	} else {
		return fmt.Sprintf("%.3f", u)
	}
}

func parseUnit(s string) (u Unit) {
	fmt.Sscanf(s, "%v", &u)
	return
}

type Orientation int

const (
	Portrait Orientation = iota
	Landscape
)

var orientation = Portrait

type Point struct {
	X, Y Unit
}

func (p Point) Swap() Point { return Point{X: p.Y, Y: p.X} }

func (p Point) String() (s string) {
	switch orientation {
	case Portrait:
	case Landscape:
		p = p.Swap()
	}
	return fmt.Sprintf("%v,%v", p.X, p.Y)
}

func (p Point) Scale(f Unit) Point {
	return Point{p.X * f, p.Y * f}
}

func parsePoint(s string) (p Point) {
	fmt.Sscanf(s, "%v,%v", &p.X, &p.Y)
	return
}

type Triple struct {
	U, V, W Unit
}

func (t Triple) String() string {
	return fmt.Sprintf("%v,%v,%v", t.U, t.V, t.W)
}

func parseTriple(s string) (t Triple) {
	fmt.Sscanf(s, "%v,%v,%v", &t.U, &t.V, &t.W)
	return
}

type Path []Point

func (p Path) String() string {
	pp := make([]string, len(p))
	for i, pt := range p {
		pp[i] = pt.String()
	}
	return strings.Join(pp, ",")
}

func (p Path) Scale(f Unit) Path {
	ret := make(Path, len(p))
	for i, pt := range p {
		ret[i] = pt.Scale(f)
	}
	return ret
}

type LineStyle int

const (
	Solid LineStyle = iota
	Dots
	ShortDash
	Dash
	LongDash
	DashDot
	DashLongDot
	DashDoubleDot
	DashLongDoubleDot
)

type Direction byte

const (
	Stop Direction = 1 << iota >> 1
	Down
	Up
	Right
	Left
)