From d196fe0d713a82945a1427a9f77be39a90087467 Mon Sep 17 00:00:00 2001 From: Joop Kiefte Date: Tue, 24 Nov 2020 20:28:05 +0200 Subject: Add inline markup support --- pdf/create.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'pdf/create.go') diff --git a/pdf/create.go b/pdf/create.go index 639b78c..c4450a7 100644 --- a/pdf/create.go +++ b/pdf/create.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" + "regexp" "github.com/phpdave11/gofpdf" ) @@ -21,10 +22,6 @@ func (t Tree) pr(a string, text string) { line(t.PDF, t.Rules.Get(a), t.Rules.Get(a).Prefix+text+t.Rules.Get(a).Postfix) } -func pagenumber() { - -} - func (t Tree) Render() { var block string var lastsection int @@ -34,8 +31,8 @@ func (t Tree) Render() { block = "" t.PDF.AddPage() t.PDF.SetHeaderFuncMode(func() { - t.PDF.SetY(0.5) - t.PDF.SetX(-1) + t.PDF.SetFont("CourierPrime", "", 12) + t.PDF.SetXY(-1, 0.5) t.PDF.Cell(0, 0, strconv.Itoa(t.PDF.PageNo()-1)+".") }, true) continue @@ -74,11 +71,30 @@ func (t Tree) Render() { } } +var ( + bolditalic = regexp.MustCompile("\\*{3}([^\\*\n]+)\\*{3}") + bold = regexp.MustCompile("\\*{2}([^\\*\n]+)\\*{2}") + italic = regexp.MustCompile("\\*{1}([^\\*\n]+)\\*{1}") + underline = regexp.MustCompile("_{1}([^\\*\n]+)_{1}") +) + func line(pdf *gofpdf.Fpdf, format rules.Format, text string) { + if format.Align == "C" { + text = "
"+text+"
" + } pdf.SetFont(format.Font, format.Style, format.Size) - pdf.SetX(format.Left) - pdf.MultiCell(format.Width, 0.165, text, "", format.Align, false) - // TODO: create liner to do away with multicell and add inline markup support + pdf.SetX(0) + pdf.SetLeftMargin(format.Left) + pdf.SetRightMargin(format.Right) + + text = bolditalic.ReplaceAllString(text, "$1") + text = bold.ReplaceAllString(text, "$1") + text = italic.ReplaceAllString(text, "$1") + text = underline.ReplaceAllString(text, "$1") + + html := pdf.HTMLBasicNew() + html.Write(0.165, text) + pdf.SetY(pdf.GetY()+0.165) } func Create(file string, format rules.Set, contents lex.Screenplay) { -- cgit v1.2.3-70-g09d2