-
Notifications
You must be signed in to change notification settings - Fork 1
/
layout.go
186 lines (159 loc) · 3.23 KB
/
layout.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2014 by Christoph Hack <christoph@tux21b.org>
// All rights reserved. Distributed under the Simplified BSD License.
package main
import (
"errors"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
type lengthUnit int
const (
Fit lengthUnit = 0
Expand lengthUnit = 1
Exact lengthUnit = 2
Proportional lengthUnit = 3
)
type Length struct {
Value float32
Unit lengthUnit
Computed float32
}
func ParseLength(s string) (Length, error) {
var value float32
s = strings.TrimSpace(s)
split := 0
for split < len(s) {
r, n := utf8.DecodeRuneInString(s[split:])
if r != '.' && !unicode.IsNumber(r) {
break
}
split += n
}
if split > 0 {
v, err := strconv.ParseFloat(s[:split], 32)
if err != nil {
return Length{}, err
}
value = float32(v)
}
unit := strings.TrimSpace(s[split:])
switch unit {
case "mm":
return Length{value * 72.0 / 25.4, Exact, value * 72.0 / 25.4}, nil
case "cm":
return Length{value * 720 / 25.4, Exact, value * 720 / 25.4}, nil
}
return Length{}, errors.New("invalid length")
}
func MustParseLength(s string) Length {
length, err := ParseLength(s)
if err != nil {
panic(err.Error())
}
return length
}
type Box struct {
MarginTop, MarginRight, MarginBottom, MarginLeft Length
PaddingTop, PaddingRight, PaddingBottom, PaddingLeft Length
Width, Height Length
}
func (b *Box) TotalWidth() float32 {
return b.MarginLeft.Computed + b.PaddingLeft.Computed + b.Width.Computed + b.PaddingRight.Computed + b.MarginRight.Computed
}
func (b *Box) TotalHeight() float32 {
return b.MarginTop.Computed + b.PaddingTop.Computed + b.Height.Computed + b.PaddingBottom.Computed + b.MarginBottom.Computed
}
/*
type Point struct {
X int
Y int
}
func P(x, y float32) Point {
return Point{mm(x), mm(y)}
}
type Box struct {
Size Point
Pos Point
Anchor Point
}
func (b *Box) Box() *Box {
return b
}
type Object interface {
Box() *Box
}
type Container interface {
Object
Children() []Object
}
type HBox struct {
box Box
objs []Object
}
func NewHBox(objs ...Object) *HBox {
h := &HBox{objs: objs}
maxAsc, maxDesc := 0, 0
for i := range h.objs {
b := h.objs[i].Box()
if b.Anchor.Y > maxDesc {
maxDesc = b.Anchor.Y
}
if b.Size.Y-b.Anchor.Y > maxAsc {
maxAsc = b.Size.Y - b.Anchor.Y
}
}
x := 0
for i := range h.objs {
b := h.objs[i].Box()
b.Pos.X = x
b.Pos.Y = maxDesc - b.Anchor.Y
x += b.Size.X
}
h.box.Size.X = x
h.box.Size.Y = maxAsc + maxDesc
h.box.Anchor.Y = maxDesc
return h
}
func (h *HBox) Box() *Box {
return &h.box
}
func (h *HBox) Children() []Object {
return h.objs
}
type VBox struct {
box Box
objs []Object
}
func NewVBox(objs ...Object) *VBox {
v := &VBox{objs: objs}
maxAsc, maxDesc := 0, 0
for i := range v.objs {
b := v.objs[i].Box()
if b.Anchor.X > maxDesc {
maxDesc = b.Anchor.X
}
if b.Size.X-b.Anchor.X > maxAsc {
maxAsc = b.Size.X - b.Anchor.X
}
}
y := 0
for i := range v.objs {
b := v.objs[i].Box()
b.Pos.X = maxDesc - b.Anchor.X
b.Pos.Y = y
y += b.Size.Y
}
v.box.Size.X = maxAsc + maxDesc
v.box.Size.Y = y
v.box.Anchor.X = maxDesc
return v
}
func (v *VBox) Box() *Box {
return &v.box
}
func (v *VBox) Children() []Object {
return v.objs
}
*/