-
Notifications
You must be signed in to change notification settings - Fork 0
/
cssbuilder.go
322 lines (296 loc) · 9.22 KB
/
cssbuilder.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package htmlbag
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"github.com/PuerkitoBio/goquery"
"github.com/boxesandglue/boxesandglue/backend/bag"
"github.com/boxesandglue/boxesandglue/backend/node"
"github.com/boxesandglue/boxesandglue/frontend"
"github.com/boxesandglue/boxesandglue/frontend/pdfdraw"
"github.com/boxesandglue/csshtml"
"golang.org/x/net/html"
)
var onecm = bag.MustSP("1cm")
// CSSBuilder handles HTML chunks and CSS instructions.
type CSSBuilder struct {
pagebox []node.Node
currentPageDimensions PageDimensions
frontend *frontend.Document
css *csshtml.CSS
stylesStack StylesStack
}
// New creates an instance of the CSSBuilder.
func New(fd *frontend.Document, c *csshtml.CSS) (*CSSBuilder, error) {
cb := CSSBuilder{
css: c,
frontend: fd,
stylesStack: make(StylesStack, 0),
pagebox: []node.Node{},
}
if err := LoadIncludedFonts(fd); err != nil {
return nil, err
}
return &cb, nil
}
// PageDimensions contains the page size and the margins of the page.
type PageDimensions struct {
Width bag.ScaledPoint
Height bag.ScaledPoint
MarginLeft bag.ScaledPoint
MarginRight bag.ScaledPoint
MarginTop bag.ScaledPoint
MarginBottom bag.ScaledPoint
PageAreaLeft bag.ScaledPoint
PageAreaTop bag.ScaledPoint
ContentWidth bag.ScaledPoint
ContentHeight bag.ScaledPoint
masterpage *csshtml.Page
}
func (cb *CSSBuilder) getPageType() *csshtml.Page {
if first, ok := cb.css.Pages[":first"]; ok && len(cb.frontend.Doc.Pages) == 0 {
return &first
}
isRight := len(cb.frontend.Doc.Pages)%2 == 0
if right, ok := cb.css.Pages[":right"]; ok && isRight {
return &right
}
if left, ok := cb.css.Pages[":left"]; ok && !isRight {
return &left
}
if allPages, ok := cb.css.Pages[""]; ok {
return &allPages
}
return nil
}
// InitPage makes sure that there is a valid page in the frontend.
func (cb *CSSBuilder) InitPage() error {
if cb.frontend.Doc.CurrentPage != nil {
return nil
}
var err error
if defaultPage := cb.getPageType(); defaultPage != nil {
wdStr, htStr := csshtml.PapersizeWidthHeight(defaultPage.Papersize)
var wd, ht, mt, mb, ml, mr bag.ScaledPoint
if wd, err = bag.SP(wdStr); err != nil {
return err
}
if ht, err = bag.SP(htStr); err != nil {
return err
}
if str := defaultPage.MarginTop; str == "" {
mt = onecm
} else {
if mt, err = bag.SP(str); err != nil {
return err
}
}
if str := defaultPage.MarginBottom; str == "" {
mb = onecm
} else {
if mb, err = bag.SP(str); err != nil {
return err
}
}
if str := defaultPage.MarginLeft; str == "" {
ml = onecm
} else {
if ml, err = bag.SP(str); err != nil {
return err
}
}
if str := defaultPage.MarginRight; str == "" {
mr = onecm
} else {
if mr, err = bag.SP(str); err != nil {
return err
}
}
var res map[string]string
res, defaultPage.Attributes = csshtml.ResolveAttributes(defaultPage.Attributes)
styles := cb.stylesStack.PushStyles()
if err = StylesToStyles(styles, res, cb.frontend, cb.stylesStack.CurrentStyle().Fontsize); err != nil {
return err
}
vl := node.NewVList()
vl.Width = wd - ml - mr - styles.BorderLeftWidth - styles.BorderRightWidth - styles.PaddingLeft - styles.PaddingRight
vl.Height = ht - mt - mb - styles.PaddingTop - styles.PaddingBottom - styles.BorderTopWidth - styles.BorderBottomWidth
hv := HTMLValues{
BorderLeftWidth: styles.BorderLeftWidth,
BorderRightWidth: styles.BorderRightWidth,
BorderTopWidth: styles.BorderTopWidth,
BorderBottomWidth: styles.BorderBottomWidth,
BorderTopStyle: styles.BorderTopStyle,
BorderLeftStyle: styles.BorderLeftStyle,
BorderRightStyle: styles.BorderRightStyle,
BorderBottomStyle: styles.BorderBottomStyle,
BorderTopColor: styles.BorderTopColor,
BorderLeftColor: styles.BorderLeftColor,
BorderRightColor: styles.BorderRightColor,
BorderBottomColor: styles.BorderBottomColor,
PaddingLeft: styles.PaddingLeft,
PaddingRight: styles.PaddingRight,
PaddingBottom: styles.PaddingBottom,
PaddingTop: styles.PaddingTop,
BorderTopLeftRadius: styles.BorderTopLeftRadius,
BorderTopRightRadius: styles.BorderTopRightRadius,
BorderBottomLeftRadius: styles.BorderBottomLeftRadius,
BorderBottomRightRadius: styles.BorderBottomRightRadius,
}
vl = cb.HTMLBorder(vl, hv)
cb.stylesStack.PopStyles()
// set page width / height
cb.frontend.Doc.DefaultPageWidth = wd
cb.frontend.Doc.DefaultPageHeight = ht
cb.currentPageDimensions = PageDimensions{
Width: wd,
Height: ht,
PageAreaLeft: ml + styles.BorderLeftWidth + styles.PaddingLeft,
PageAreaTop: mt - styles.BorderTopWidth - styles.PaddingTop,
ContentWidth: wd - styles.BorderRightWidth - styles.PaddingRight - ml - mr - styles.BorderLeftWidth - styles.PaddingLeft,
ContentHeight: ht - styles.BorderBottomWidth - styles.PaddingBottom - mt - mb - styles.BorderTopWidth - styles.PaddingTop,
MarginTop: mt,
MarginBottom: mb,
MarginLeft: ml,
MarginRight: mr,
masterpage: defaultPage,
}
cb.frontend.Doc.NewPage()
if styles.BackgroundColor != nil {
r := node.NewRule()
x := pdfdraw.NewStandalone().ColorNonstroking(*styles.BackgroundColor).Rect(0, 0, wd, -ht).Fill()
r.Pre = x.String()
rvl := node.Vpack(r)
rvl.Attributes = node.H{"origin": "page background color"}
cb.frontend.Doc.CurrentPage.OutputAt(0, ht, rvl)
}
cb.frontend.Doc.CurrentPage.OutputAt(ml, ht-mt, vl)
return nil
}
// no page master found
cb.frontend.Doc.DefaultPageWidth = bag.MustSP("210mm")
cb.frontend.Doc.DefaultPageHeight = bag.MustSP("297mm")
cb.currentPageDimensions = PageDimensions{
Width: cb.frontend.Doc.DefaultPageWidth,
Height: cb.frontend.Doc.DefaultPageHeight,
ContentWidth: cb.frontend.Doc.DefaultPageWidth - 2*onecm,
ContentHeight: cb.frontend.Doc.DefaultPageHeight - 2*onecm,
PageAreaLeft: onecm,
PageAreaTop: onecm,
MarginTop: onecm,
MarginBottom: onecm,
MarginLeft: onecm,
MarginRight: onecm,
}
cb.frontend.Doc.NewPage()
return nil
}
// PageSize returns a struct with the dimensions of the current page.
func (cb *CSSBuilder) PageSize() (PageDimensions, error) {
err := cb.InitPage()
if err != nil {
return PageDimensions{}, err
}
return cb.currentPageDimensions, nil
}
// ParseCSSString reads CSS instructions from a string.
func (cb *CSSBuilder) ParseCSSString(css string) error {
var err error
if err = cb.css.AddCSSText(css); err != nil {
return err
}
return nil
}
// NewPage puts the current page into the PDF document and starts with a new page.
func (cb *CSSBuilder) NewPage() error {
if err := cb.InitPage(); err != nil {
return err
}
if err := cb.BeforeShipout(); err != nil {
return err
}
cb.frontend.Doc.CurrentPage.Shipout()
cb.frontend.Doc.NewPage()
return nil
}
// ParseHTMLFromNode interprets the HTML structure and applies all previously read CSS data.
func (cb *CSSBuilder) ParseHTMLFromNode(input *html.Node) (*frontend.Text, error) {
doc := goquery.NewDocumentFromNode(input)
gq, err := cb.css.ApplyCSS(doc)
if err != nil {
return nil, err
}
var te *frontend.Text
n := gq.Nodes[0]
if te, err = HTMLNodeToText(n, cb.stylesStack, cb.frontend); err != nil {
return nil, err
}
return te, nil
}
// HTMLToText interprets the HTML string and applies all previously read CSS data.
func (cb *CSSBuilder) HTMLToText(html string) (*frontend.Text, error) {
doc, err := cb.css.ProcessHTMLChunk(html)
if err != nil {
return nil, err
}
n := doc.Nodes[0]
var te *frontend.Text
if te, err = HTMLNodeToText(n, cb.stylesStack, cb.frontend); err != nil {
return nil, err
}
return te, nil
}
// AddCSS reads the CSS instructions in css.
func (cb *CSSBuilder) AddCSS(css string) error {
curwd, err := os.Getwd()
if err != nil {
return err
}
cb.css.PushDir(curwd)
return cb.css.AddCSSText(css)
}
type info struct {
vl *node.VList
hsize bag.ScaledPoint
x bag.ScaledPoint
marginTop bag.ScaledPoint
marginBottom bag.ScaledPoint
pagebox []node.Node
height bag.ScaledPoint
hv HTMLValues
debug string
}
func (inf *info) String() string {
return fmt.Sprintf("mt: %s mb: %s len(pb): %d vl: %v", inf.marginTop, inf.marginBottom, len(inf.pagebox), inf.vl)
}
func hasContents(areaAttributes map[string]string) bool {
return areaAttributes["content"] != "none" && areaAttributes["content"] != "normal"
}
type pageMarginBox struct {
minWidth bag.ScaledPoint
maxWidth bag.ScaledPoint
areaWidth bag.ScaledPoint
hasContents bool
widthAuto bool
halign frontend.HorizontalAlignment
x bag.ScaledPoint
y bag.ScaledPoint
wd bag.ScaledPoint
ht bag.ScaledPoint
}
// ReadCSSFile reads the given file name and tries to parse the CSS contents
// from the file.
func (cb *CSSBuilder) ReadCSSFile(filename string) error {
slog.Debug("Read file", "filename", filename)
data, err := os.ReadFile(filename)
if err != nil {
return err
}
abs, err := filepath.Abs(filepath.Dir(filename))
if err != nil {
return err
}
cb.css.PushDir(abs)
return cb.css.AddCSSText(string(data))
}