summaryrefslogtreecommitdiff
path: root/pdf/create.go
blob: abf0284c5e78940a74ef9849eeafc42ab25b93a5 (plain)
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
package pdf

import (
	"github.com/lapingvino/lexington/lex"
	"github.com/lapingvino/lexington/rules"

	"github.com/jung-kurt/gofpdf"
)

var tr func(string) string

type Tree struct {
	PDF *gofpdf.Fpdf
	Rules rules.Set
	F   lex.Screenplay
}

func (t Tree) pr(a string, text string) {
	line(t.PDF, t.Rules.Get(a), text)
}

func (t Tree) Render() {
	for _, row := range t.F {
		if t.Rules.Get(row.Type).Hide {
			continue
		}
		t.pr(row.Type, row.Contents)
	}
}

func line(pdf *gofpdf.Fpdf, format rules.Format, text string) {
	pdf.SetFont(format.Font, format.Style, format.Size)
	pdf.SetX(format.Left)
	pdf.MultiCell(format.Width, 0.19, tr(text), "", "aligned", false)
}

func Create(file string, format rules.Set, contents lex.Screenplay) {
	pdf := gofpdf.New("P", "in", "Letter", "")
	tr = pdf.UnicodeTranslatorFromDescriptor("")
	pdf.AddPage()
	pdf.SetMargins(1, 1, 1)
	pdf.SetXY(1, 1)
	f := Tree{
		Rules: format,
		F: contents,
	}
	f.Render()
	err := pdf.OutputFileAndClose(file)
	if err != nil {
		panic(err)
	}
}