-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-ramen34.go
141 lines (132 loc) · 3.38 KB
/
gen-ramen34.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"unicode/utf8"
"github.com/fogleman/gg"
)
type Config struct {
BaseFile string `json:"base_file"`
FontName string `json:"font_name"`
FontSize int `json:"font_size"`
Bubbles []struct {
Id int `json:"id"`
Lines []struct {
X int `json:"x"`
Y int `json:"y"`
Length int `json:"length"`
} `json:"lines"`
} `json:"bubbles"`
}
var config Config
var FontSize int
func toRotate(tc *gg.Context, s string, w float64, h float64) {
switch s {
case "ー", "〜", "~":
tc.Rotate(gg.Radians(90))
tc.DrawStringAnchored(s, h/2, 0-w/2, 0.5, 0.5)
/*
case "。":
tc.Rotate(gg.Radians(90))
tc.DrawStringAnchored(s, h/2 + h/10, 0 - w, 0.5, 0.5)
*/
case "。", "、":
tc.DrawString(s, w/2, h/3)
default:
tc.DrawStringAnchored(s, w/2, h/2, 0.5, 0.5)
}
}
func measureString(s string) (float64, float64) {
tempCampus := gg.NewContext(FontSize*2, FontSize*2)
tempCampus.SetRGB(1, 1, 1)
tempCampus.Clear()
tempCampus.SetRGB(0, 0, 0)
if err := tempCampus.LoadFontFace(config.FontName, float64(FontSize)); err != nil {
panic(err)
}
w, h := tempCampus.MeasureString(s)
return w, h
}
func createCharacterImage(s string) *gg.Context {
w, h := measureString(s)
w = w + (float64(FontSize) / 4)
h = h + (float64(FontSize) / 4)
tempCampus := gg.NewContext(int(w), int(h))
tempCampus.SetRGB(1, 1, 1)
tempCampus.Clear()
tempCampus.SetRGB(0, 0, 0)
if err := tempCampus.LoadFontFace(config.FontName, float64(FontSize)); err != nil {
panic(err)
}
toRotate(tempCampus, s, w, h)
return tempCampus
}
func PutStringInBubble(dc *gg.Context, msg string, x int, y int) {
for _, c := range msg {
s := string(c)
ci := createCharacterImage(s)
ci_size := ci.Image().Bounds().Size()
dc.DrawImage(ci.Image(), x, y)
y += ci_size.Y
}
}
func main() {
config_file := "config.json"
quotes_file := "quotes.json"
output_file := "output.png"
if "" != os.Getenv("CONFIG_FILE") {
config_file = os.Getenv("CONFIG_FILE")
}
if "" != os.Getenv("QUOTES_FILE") {
quotes_file = os.Getenv("QUOTES_FILE")
}
if "" != os.Getenv("OUTPUT_FILE") {
output_file = os.Getenv("OUTPUT_FILE")
}
bytes, err := ioutil.ReadFile(config_file)
if err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(bytes, &config); err != nil {
log.Fatal(err)
}
ramen, err := gg.LoadPNG(config.BaseFile)
if err != nil {
panic(err)
}
ramen_size := ramen.Bounds().Size()
dc := gg.NewContext(ramen_size.X, ramen_size.Y)
dc.DrawImage(ramen, 0, 0)
if err := dc.LoadFontFace(config.FontName, float64(FontSize)); err != nil {
panic(err)
}
var quotes [][]string
bytes, err = ioutil.ReadFile(quotes_file)
if err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(bytes, "es); err != nil {
log.Fatal(err)
}
for quote_index, lines := range quotes {
minFontSize := config.FontSize
for line_index, line := range lines {
FontSize = config.FontSize
length := config.Bubbles[quote_index].Lines[line_index].Length
line_len := utf8.RuneCountInString(line)
if line_len > length {
delta := line_len - length
if minFontSize > FontSize-(delta*2) {
minFontSize = FontSize - (delta * 2)
}
}
}
FontSize = minFontSize
for line_index, line := range lines {
PutStringInBubble(dc, line, config.Bubbles[quote_index].Lines[line_index].X, config.Bubbles[quote_index].Lines[line_index].Y)
}
}
dc.SavePNG(output_file)
}