summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <git@njw.name>2019-10-31 17:57:41 +0000
committerNick White <git@njw.name>2019-10-31 17:57:41 +0000
commit64983a29e380a04eaf1b730e54cfe6f3399809f1 (patch)
tree587cf646b8b2dc19dd300a7b58257d85f6f674b4
parentb9aeada4e573643985d6df03f672f2c2fec169d8 (diff)
Add capability to embed font files into tool
-rw-r--r--cmd/fonttobytes/main.go32
-rw-r--r--pdf.go7
2 files changed, 33 insertions, 6 deletions
diff --git a/cmd/fonttobytes/main.go b/cmd/fonttobytes/main.go
new file mode 100644
index 0000000..9db47e4
--- /dev/null
+++ b/cmd/fonttobytes/main.go
@@ -0,0 +1,32 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "strings"
+)
+
+func main() {
+ flag.Usage = func() {
+ fmt.Fprintln(flag.CommandLine.Output(), "Usage: fonttobytes font.ttf")
+ flag.PrintDefaults()
+ }
+ flag.Parse()
+
+ if flag.NArg() != 1 {
+ flag.Usage()
+ return
+ }
+
+ b, err := ioutil.ReadFile(flag.Arg(0))
+ if err != nil {
+ log.Fatalln(err)
+ }
+ s := fmt.Sprintf("%v", b)
+ s1 := strings.Replace(s, "[", "{", -1)
+ s2 := strings.Replace(s1, "]", "}", -1)
+ s3 := strings.Replace(s2, " ", ", ", -1)
+ fmt.Printf("[]byte%s\n", s3)
+}
diff --git a/pdf.go b/pdf.go
index 69e2daf..f1ffe19 100644
--- a/pdf.go
+++ b/pdf.go
@@ -28,14 +28,11 @@ type Fpdf struct {
}
// Setup creates a new PDF with appropriate settings and fonts
-// TODO: find a font that's closer to the average dimensions of the
-// text we're dealing with
-// TODO: once we have a good font, embed it in the binary as bytes
func (p *Fpdf) Setup() error {
p.fpdf = gofpdf.New("P", "pt", "A4", "")
// Even though it's invisible, we need to add a font which can do
// UTF-8 so that text renders correctly.
- p.fpdf.AddUTF8Font("dejavu", "", "DejaVuSansCondensed.ttf")
+ p.fpdf.AddUTF8FontFromBytes("dejavu", "", DejaVuCondensedBytes)
p.fpdf.SetFont("dejavu", "", 10)
p.fpdf.SetAutoPageBreak(false, float64(0))
return p.fpdf.Error()
@@ -66,8 +63,6 @@ func (p *Fpdf) AddPage(imgpath, hocrpath string) error {
b := img.Bounds()
p.fpdf.AddPageFormat("P", gofpdf.SizeType{Wd: pxToPt(b.Dx()), Ht: pxToPt(b.Dy())})
- // TODO: check for errors in pdf as going through
-
_ = p.fpdf.RegisterImageOptions(imgpath, gofpdf.ImageOptions{})
p.fpdf.ImageOptions(imgpath, 0, 0, pxToPt(b.Dx()), pxToPt(b.Dy()), false, gofpdf.ImageOptions{}, 0, "")