Skip to content

Commit

Permalink
Implement option for tab size
Browse files Browse the repository at this point in the history
  • Loading branch information
hjr265 committed Jan 16, 2023
1 parent f35b2da commit d918935
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ marginTop = 50 # Margin at the top edge of each page.
marginRight = 25 # ... on the right edge of each page.
marginBottom = 50 # ... on the bottom edge of each page.
marginLeft = 25 # ... on the left edge of each page.
tabSize = 4 # Replaces tabs with this many spaces.
keepPDF = true # When true, does not delete generated PDF after print.

[printer]
Expand All @@ -64,3 +65,4 @@ contestID = "..." # The 24 character hex ID of the contest goes here.
## To-dos

- [x] Windows support
- [ ] Improve tab-to-spaces behavior
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
MarginRight float64
MarginBottom float64
MarginLeft float64
TabSize int
KeepPDF bool
}
Printer struct {
Expand Down
7 changes: 6 additions & 1 deletion pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"fmt"
"log"
"strings"

"github.com/signintech/gopdf"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func (b PDFBuilder) Build(name string, pr Print) error {

linesperpage -= len(headerlines) + 2

lines, err := pdf.SplitText(pr.Content, pagesize.W-pdf.MarginLeft()-pdf.MarginRight())
lines, err := pdf.SplitText(b.tabToSpaces(pr.Content), pagesize.W-pdf.MarginLeft()-pdf.MarginRight())
if err != nil {
return err
}
Expand Down Expand Up @@ -95,6 +96,10 @@ func (b PDFBuilder) newLine(pdf *gopdf.GoPdf) {
pdf.SetNewXY(pdf.GetY()+float64(b.cfg.Printd.LineHeight), pdf.MarginLeft(), float64(b.cfg.Printd.LineHeight))
}

func (b PDFBuilder) tabToSpaces(t string) string {
return strings.ReplaceAll(t, "\t", strings.Repeat(" ", b.cfg.Printd.TabSize))
}

var (
gopdfPageSizes = map[PageSize]*gopdf.Rect{
PageA4: gopdf.PageSizeA4,
Expand Down

0 comments on commit d918935

Please sign in to comment.