-
Notifications
You must be signed in to change notification settings - Fork 0
/
precess.go
95 lines (79 loc) · 1.99 KB
/
precess.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
package imagegenerator
import (
"image"
"math"
"github.com/hmmftg/image/font"
"github.com/hmmftg/image/font/opentype"
)
const (
BmpAndPng = "bmp+png"
JustPng = "png"
)
func RelationalCoordinate(r float64, v int) int {
return int(math.Round(r * float64(v)))
}
func CoordinationToRelation(c, v int) float64 {
return float64(c) / float64(v)
}
func (tx *PrintTx) RelationalX(rx float64) int {
return RelationalCoordinate(rx, tx.Rgba.Bounds().Dx())
}
func (tx *PrintTx) CoordinationX(c int) float64 {
return CoordinationToRelation(c, tx.Rgba.Bounds().Dx())
}
func (tx *PrintTx) RelationalY(rx float64) int {
return RelationalCoordinate(rx, tx.Rgba.Bounds().Dy())
}
func (tx *PrintTx) CoordinationY(c int) float64 {
return CoordinationToRelation(c, tx.Rgba.Bounds().Dy())
}
func ProcessRequest(
requests map[string]PrintRequest,
fonts map[string]opentype.Font,
images map[string]image.Image,
) (map[string]string, error) {
fullResp := make(map[string]string, 0)
faces := make(map[string]font.Face, 0)
for name, request := range requests {
resp := make(map[string]string, 0)
for imageID := range request.Images {
tx := PrintTx{
Faces: &faces,
Dpi: request.Images[imageID].Dpi,
Images: images,
Fonts: fonts,
Margin: request.Margin,
}
if len(request.Images[imageID].BackgroundFile) > 0 {
tx.Bg = image.Transparent
tx.Src = images[request.Images[imageID].BackgroundFile]
} else {
tx.Bg = image.White
tx.Src = nil
}
request.Images[imageID].InitializeImage(images, &tx)
tx.Bg = image.Black
// Draw the text.
for id := range request.Drawings {
request.Drawings[id].Draw(&tx)
}
err := GetImage(&request.Images[imageID], resp, tx.Rgba)
if err != nil {
return resp, err
}
}
switch name {
case BmpAndPng:
mn, ok := resp[BmpMonoChrome]
if ok {
fullResp[Bmp] = mn
} else {
fullResp[Bmp] = resp[Bmp]
}
fullResp[Png] = resp[Png]
default:
fullResp[name] = resp[Png]
}
}
return fullResp, nil
}