-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
98 lines (76 loc) · 1.72 KB
/
block.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
package facepaint
import (
"fmt"
"image"
"github.com/cufee/facepaint/style"
)
func NewBlock(content BlockContent) *Block {
return &Block{
content: content,
}
}
type blockContentType int
func (t blockContentType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%d", t)), nil
}
func (t blockContentType) String() string {
return fmt.Sprintf("%d", t)
}
const (
BlockContentTypeEmpty blockContentType = iota
BlockContentTypeBlocks
BlockContentTypeImage
BlockContentTypeText
)
type Position struct {
X float64
Y float64
}
type BlockContent interface {
Type() blockContentType
// Renders the block onto an image
Render(*layerContext, Position) error
Style() style.StyleOptions
setStyle(style.StyleOptions)
Layers() map[int]struct{}
// returns final block image dimensions without rendering
dimensions() contentDimensions
}
type Block struct {
content BlockContent
}
func (b *Block) Layers() map[int]struct{} {
return b.content.Layers()
}
func (b *Block) Style() style.StyleOptions {
return b.content.Style()
}
func (b *Block) Type() blockContentType {
return b.content.Type()
}
func (b *Block) Render() (image.Image, error) {
dimensions := b.Dimensions()
layers := b.Layers()
ctx := newLayerContext(len(layers))
for idx := range layers {
ctx.layers[idx] = newLayer(dimensions.Width, dimensions.Height)
}
err := b.content.Render(ctx, Position{0, 0})
if err != nil {
return nil, err
}
return ctx.Image(), nil
}
func (b *Block) Dimensions() contentDimensions {
return b.content.dimensions()
}
type contentDimensions struct {
Width int
Height int
paddingAndGapsX float64
paddingAndGapsY float64
paddingX float64
paddingY float64
gapsX float64
gapsY float64
}