-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
195 lines (166 loc) · 4.72 KB
/
process.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
package gompdf
import (
"fmt"
"io"
"strings"
"time"
"github.com/jung-kurt/gofpdf/v2"
"github.com/mazzegi/gompdf/style"
)
type ProcessOption func(p *Processor) error
type Processor struct {
doc *Document
pdf *gofpdf.Fpdf
fontDir string
codePage string
transformText func(string) string
currStyles style.Styles
}
func WithFontDir(dir string) ProcessOption {
return func(p *Processor) error {
p.fontDir = dir
return nil
}
}
func WithCodePage(cp string) ProcessOption {
return func(p *Processor) error {
p.codePage = cp
return nil
}
}
func NewProcessor(doc *Document, options ...ProcessOption) (*Processor, error) {
p := &Processor{
doc: doc,
fontDir: "fonts",
codePage: "",
currStyles: DefaultStyle,
}
for _, o := range options {
err := o(p)
if err != nil {
return nil, err
}
}
return p, nil
}
func (p *Processor) Process(w io.Writer) error {
start := time.Now()
fmt.Printf("run instructions ...\n")
p.pdf = gofpdf.New(
fpdfOrientation(p.doc.Default.Orientation),
fpdfUnit(p.doc.Default.Unit),
fpdfFormat(p.doc.Default.Format),
p.fontDir,
)
p.pdf.AliasNbPages("{np}")
translateUnicode := p.pdf.UnicodeTranslatorFromDescriptor(p.codePage)
p.transformText = func(s string) string {
ts := strings.Replace(s, "{cp}", fmt.Sprintf("%d", p.pdf.PageNo()), -1)
return translateUnicode(ts)
}
p.pdf.SetHeaderFunc(func() {
p.processInstructions(p.doc.Header)
})
p.pdf.SetFooterFunc(func() {
p.processInstructions(p.doc.Footer)
})
p.applyDefaults()
p.applyFont(p.currStyles.Font)
p.pdf.AddPage()
p.processInstructions(p.doc.Body)
err := p.pdf.Error()
if err != nil {
return err
}
fmt.Printf("run instructions ... in (%s)\n", time.Since(start))
return p.pdf.Output(w)
}
func (p *Processor) applyDefaults() {
p.pdf.SetAutoPageBreak(p.doc.Default.PageBreaks == PageBreakModeAuto, p.doc.Default.PageMargins.Bottom)
p.pdf.SetMargins(p.doc.Default.PageMargins.Left, p.doc.Default.PageMargins.Top, p.doc.Default.PageMargins.Right)
}
func (p *Processor) appliedStyles(i Instruction) style.Styles {
st := p.currStyles
i.Apply(p.doc.styleClasses, &st)
return st
}
func (p *Processor) processInstructions(is Instructions) {
for _, i := range is.iss {
switch i := i.(type) {
case *Font:
i.Apply(p.doc.styleClasses, &p.currStyles)
p.applyFont(p.currStyles.Font)
case *LineFeed:
p.processLineFeed(i, p.appliedStyles(i))
case *SetX:
p.pdf.SetX(i.X)
case *SetY:
p.pdf.SetY(i.Y)
case *SetXY:
p.pdf.SetXY(i.X, i.Y)
case *Box:
p.renderTextBox(i.Text, p.appliedStyles(i))
case *Text:
p.renderText(i, p.appliedStyles(i))
case *Table:
p.renderTable(i, p.appliedStyles(i))
case *Image:
p.renderImage(i, p.appliedStyles(i))
}
}
}
func (p *Processor) effectiveWidth(width float64) float64 {
if width > 0 {
return width
}
pw, _ := p.pdf.GetPageSize()
l, _, r, _ := p.pdf.GetMargins()
return pw - (l + r) - 3 // without substracting 3 it doesn't fit
}
func (p *Processor) applyFont(fnt style.Font) {
p.pdf.SetFont(string(fnt.Family), fpdfFontStyle(fnt), float64(fnt.PointSize))
}
func (p *Processor) processLineFeed(lf *LineFeed, sty style.Styles) {
_, fontHeight := p.pdf.GetFontSize()
height := fontHeight * lf.Lines
p.pdf.Ln(height)
}
func (p *Processor) renderText(text *Text, sty style.Styles) {
p.write(text.Text, p.effectiveWidth(sty.Dimension.Width), sty.Dimension.LineHeight, sty.Align.HAlign, sty.Font, sty.Color.Text)
}
func (p *Processor) renderTextBox(text string, sty style.Styles) {
width := p.effectiveWidth(sty.Dimension.Width)
textWidth := width - sty.Box.Padding.Left - sty.Box.Padding.Right - 3 //without -2 it writes over the border
var height float64
if sty.Dimension.Height < 0 {
if text != "" {
height = p.textHeight(text, textWidth, sty.Dimension.LineHeight, sty.Font)
} else {
height = p.textHeight("Üg", textWidth, sty.Dimension.LineHeight, sty.Font)
}
} else {
height = sty.Dimension.Height
}
x0, y0 := p.pdf.GetXY()
_, ph := p.pdf.GetPageSize()
if y0+height >= ph {
p.pdf.AddPage()
x0, y0 = p.pdf.GetXY()
}
x0 += sty.Dimension.OffsetX
y0 += sty.Dimension.OffsetY
y1 := y0 + height + sty.Box.Padding.Top + sty.Box.Padding.Bottom
x1 := x0 + width
p.drawBox(x0, y0, x1, y1, sty)
//Reset, to start writing at top left
p.pdf.SetY(y0 + sty.Box.Padding.Top)
p.pdf.SetX(x0 + sty.Box.Padding.Left)
p.write(text, textWidth, sty.Dimension.LineHeight, sty.Align.HAlign, sty.Font, sty.Color.Text)
p.pdf.Ln(sty.Dimension.LineHeight + sty.Box.Padding.Bottom)
}
func (p *Processor) renderImage(img *Image, sty style.Styles) {
x0, y0 := p.pdf.GetXY()
x0 += sty.Dimension.OffsetX
y0 += sty.Dimension.OffsetY
p.pdf.ImageOptions(img.Source, x0, y0, sty.Dimension.Width, sty.Dimension.Height, false, gofpdf.ImageOptions{}, 0, "")
}