-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.go
More file actions
61 lines (52 loc) · 1.22 KB
/
Copy pathhelpers.go
File metadata and controls
61 lines (52 loc) · 1.22 KB
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
// Copyright 2026 Brent Rowland.
// Use of this source code is governed the Apache License, Version 2.0, as described in the LICENSE file.
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/rowland/leadtype/font"
"github.com/rowland/leadtype/pdf"
)
func sampleOutputPath(name string) string {
_, file, _, ok := runtime.Caller(0)
if !ok {
return name
}
return filepath.Join(filepath.Dir(file), name)
}
func writeDoc(outputName string, build func(*pdf.DocWriter) error) (string, error) {
outputPath := sampleOutputPath(outputName)
f, err := os.Create(outputPath)
if err != nil {
return "", err
}
defer f.Close()
doc := pdf.NewDocWriter()
if err := build(doc); err != nil {
return "", err
}
if _, err := doc.WriteTo(f); err != nil {
return "", err
}
return outputPath, nil
}
func openFile(path string, openArgs ...string) error {
args := append(append([]string(nil), openArgs...), path)
cmd := exec.Command("open", args...)
if err := cmd.Start(); err != nil {
return err
}
return cmd.Process.Release()
}
func mustHaveFonts(fonts []*font.Font, err error) error {
if err != nil {
return err
}
if len(fonts) == 0 {
return fmt.Errorf("no fonts returned")
}
return nil
}