/
pdf.go
259 lines (226 loc) · 7.2 KB
/
pdf.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package sudogo
import (
"bytes"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/jung-kurt/gofpdf"
)
type PuzzlePDF struct {
Puzzles []PuzzlePDFItem
PuzzlesWide int
PuzzlesHigh int
PuzzleSpacing float64
MarginLeft float64
MarginRight float64
MarginTop float64
MarginBottom float64
Landscape bool
PageSize string
Font string
ValueFontColor Color
ValueBackColor Color
ValueFontScale float64
CandidateColor Color
CandidateScale float64
BorderThickColor Color
BorderThickWidth float64
BorderThinColor Color
BorderThinWidth float64
StateColor Color
SolutionColor Color
}
type PuzzlePDFItem struct {
Puzzle *Puzzle
Candidates bool
StateString bool
SolutionString bool
}
type Color struct {
R int
G int
B int
}
func (c Color) Is(other Color) bool {
return c.R == other.R && c.G == other.G && c.B == other.B
}
var (
ColorWhite = Color{255, 255, 255}
ColorBlack = Color{0, 0, 0}
ColorGreen = Color{0, 255, 0}
ColorRed = Color{255, 0, 0}
ColorBlue = Color{0, 0, 255}
ColorGray = Color{127, 127, 127}
)
func NewPDF() PuzzlePDF {
return PuzzlePDF{
PageSize: "A4",
MarginBottom: 28.35,
MarginTop: 28.35,
MarginLeft: 28.35,
MarginRight: 28.35,
PuzzlesWide: 1,
PuzzlesHigh: 1,
PuzzleSpacing: 28.35,
Font: "Arial",
CandidateColor: ColorGray,
CandidateScale: 0.2,
ValueFontColor: ColorBlack,
ValueFontScale: 0.7,
ValueBackColor: ColorWhite,
BorderThickColor: ColorBlack,
BorderThickWidth: 2.0,
BorderThinColor: ColorBlack,
BorderThinWidth: 0.5,
StateColor: ColorBlack,
SolutionColor: ColorGray,
}
}
func (pdf *PuzzlePDF) Add(puzzle *Puzzle, candidates bool, stateString bool, solutionString bool) {
pdf.Puzzles = append(pdf.Puzzles, PuzzlePDFItem{
Puzzle: puzzle,
Candidates: candidates,
StateString: stateString,
SolutionString: solutionString,
})
}
func (pdf *PuzzlePDF) orientation() string {
if pdf.Landscape {
return "L"
} else {
return "P"
}
}
func (pdf *PuzzlePDF) WriteFile(path string) {
f := pdf.Generate()
f.OutputFileAndClose(path)
}
func (pdf *PuzzlePDF) Write(writer io.Writer, direct bool) {
f := pdf.Generate()
if direct {
f.Output(writer)
} else {
buf := bytes.Buffer{}
f.Output(&buf)
writer.Write(buf.Bytes())
}
f.Close()
}
func (pdf *PuzzlePDF) Send(w http.ResponseWriter, direct bool) (any, int) {
w.Header().Set("Content-Type", "application/pdf")
pdf.Write(w, direct)
return nil, -1
}
func (pdf *PuzzlePDF) Download(w http.ResponseWriter, filename string, direct bool) (any, int) {
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
pdf.Send(w, direct)
return nil, -1
}
func (pdf *PuzzlePDF) Generate() *gofpdf.Fpdf {
p := gofpdf.New(pdf.orientation(), "pt", pdf.PageSize, "")
p.SetMargins(pdf.MarginLeft, pdf.MarginTop, pdf.MarginRight)
p.SetAutoPageBreak(false, pdf.MarginBottom)
pageW, pageH, _ := p.PageSize(0)
spaceW := pageW - pdf.MarginLeft - pdf.MarginRight
spaceH := pageH - pdf.MarginTop - pdf.MarginBottom
countW := float64(pdf.PuzzlesWide)
countH := float64(pdf.PuzzlesHigh)
spacingW := (countW - 1) * pdf.PuzzleSpacing
spacingH := (countH - 1) * pdf.PuzzleSpacing
puzzleSpaceW := (spaceW - spacingW) / countW
puzzleSpaceH := (spaceH - spacingH) / countH
puzzleSize := Min(puzzleSpaceW, puzzleSpaceH)
puzzleSeparation := puzzleSize + pdf.PuzzleSpacing
offsetX := pdf.MarginLeft + (spaceW-(puzzleSize*countW+spacingW))/2
offsetY := pdf.MarginTop + (spaceH-(puzzleSize*countH+spacingH))/2
perPage := pdf.PuzzlesWide * pdf.PuzzlesHigh
for i, item := range pdf.Puzzles {
puzzle := item.Puzzle
pageIndex := i % perPage
size := puzzle.Kind.Size()
sizef := float64(size)
cellSize := puzzleSize / sizef
fontSize := cellSize * pdf.ValueFontScale
lineMargin := pdf.BorderThickWidth * 2
candidateSpace := cellSize - lineMargin
candidateSize := cellSize * pdf.CandidateScale
pageCol := pageIndex % pdf.PuzzlesWide
pageRow := pageIndex / pdf.PuzzlesWide
originX := offsetX + float64(pageCol)*puzzleSeparation
originY := offsetY + float64(pageRow)*puzzleSeparation
boxW := float64(puzzle.Kind.BoxSize.Width) * cellSize
boxH := float64(puzzle.Kind.BoxSize.Height) * cellSize
if pageIndex == 0 {
p.AddPage()
}
p.SetLineWidth(pdf.BorderThinWidth)
p.SetDrawColor(pdf.BorderThinColor.R, pdf.BorderThinColor.G, pdf.BorderThinColor.B)
p.SetFillColor(pdf.ValueBackColor.R, pdf.ValueBackColor.G, pdf.ValueBackColor.B)
for y := 0; y < size; y++ {
for x := 0; x < size; x++ {
cell := puzzle.Get(x, y)
p.SetXY(originX+float64(x)*cellSize, originY+float64(y)*cellSize)
if cell.HasValue() {
cellValue := strconv.Itoa(cell.Value)
fill := !pdf.ValueBackColor.Is(ColorWhite)
p.SetFont(pdf.Font, "B", fontSize)
p.SetTextColor(pdf.ValueFontColor.R, pdf.ValueFontColor.G, pdf.ValueFontColor.B)
p.CellFormat(cellSize, cellSize, cellValue, "1", 0, "CM", fill, 0, "")
} else {
p.SetFont(pdf.Font, "", fontSize)
p.CellFormat(cellSize, cellSize, "", "1", 0, "CM", false, 0, "")
if item.Candidates {
cand := fmt.Sprintf("%v", cell.Candidates())
cand = strings.Trim(cand, "[]")
p.SetFont(pdf.Font, "", candidateSize)
candWidth := p.GetStringWidth(cand)
if candWidth > candidateSpace {
p.SetFontSize(candidateSpace / candWidth * candidateSize)
}
p.SetTextColor(pdf.CandidateColor.R, pdf.CandidateColor.G, pdf.CandidateColor.B)
p.SetXY(originX+float64(x)*cellSize, originY+float64(y)*cellSize+lineMargin)
p.CellFormat(cellSize, cellSize, cand, "0", 0, "CT", false, 0, "")
}
}
}
}
p.SetLineWidth(pdf.BorderThickWidth)
p.SetLineCapStyle("round")
for y := 0.0; y <= puzzleSize+0.0001; y += boxH {
p.Line(originX, originY+y, originX+puzzleSize, originY+y)
}
for x := 0.0; x <= puzzleSize+0.0001; x += boxW {
p.Line(originX+x, originY, originX+x, originY+puzzleSize)
}
if item.StateString {
p.SetFontSize(fontSize)
stateString := puzzle.ToStateString(true, ".")
stateWidth := p.GetStringWidth(stateString)
stateFontSize := Min(candidateSize, puzzleSize/stateWidth*fontSize)
p.SetFontSize(stateFontSize)
p.SetTextColor(pdf.StateColor.R, pdf.StateColor.G, pdf.StateColor.B)
p.SetXY(originX, originY-stateFontSize-lineMargin)
p.CellFormat(puzzleSize, stateFontSize, stateString, "0", 0, "CT", false, 0, "")
}
if item.SolutionString {
solver := puzzle.Solver()
solution, solved := solver.Solve(SolveLimit{})
if solved {
p.SetFontSize(fontSize)
solutionString := solution.ToStateString(true, ".")
solutionWidth := p.GetStringWidth(solutionString)
solutionFontSize := Min(candidateSize, puzzleSize/solutionWidth*fontSize)
p.SetFontSize(solutionFontSize)
p.SetTextColor(pdf.SolutionColor.R, pdf.SolutionColor.G, pdf.SolutionColor.B)
p.SetXY(originX, originY+puzzleSize+lineMargin)
p.TransformBegin()
p.TransformRotate(180, originX+puzzleSize*0.5, originY+puzzleSize+lineMargin+solutionFontSize*0.5)
p.CellFormat(puzzleSize, solutionFontSize, solutionString, "0", 0, "CM", false, 0, "")
p.TransformEnd()
}
}
}
return p
}