-
Notifications
You must be signed in to change notification settings - Fork 0
/
picture.go
84 lines (73 loc) · 1.86 KB
/
picture.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package troff
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os/exec"
"path"
"strings"
"sevki.org/x/debug"
)
// Position of the picture
type Position int
// PictureFlag sets the picture options like scale and outline
type PictureFlag int
const (
// Left align the picture
Left Position = iota
// Center align the picture
Center
// Right align the picture
Right
// Outline the picture with a box.
Outline PictureFlag = iota
// Scale freely scales both picture dimensions.
Scale
// White out the area to be occupied by the picture.
White
)
// Figure prints
// .FG <link> <size>
func Figure(w io.Writer, link string, verticalSize string) {
w.Write(msPrint(figure, strings.TrimLeft(link, " "), verticalSize))
}
// Picture is the mpicture macro
// https://9p.io/magic/man2html/6/mpictures
func Picture(w io.Writer, source string, height, width float32, position Position, offset float32, flags PictureFlag) {
w.Write(msPrint(beginPicture, source, fmt.Sprintf("%.1fi, %.1fi", height, width)))
w.Write(msPrint(endPicture))
}
func convertToPs(filename string, w int, h int) (string, error) {
psname := strings.Replace(filename, path.Ext(filename), ".ps", -1)
_, psname = path.Split(psname)
args := []string{"convert"}
if w > 0 && h > 0 {
args = append(args, "-size", fmt.Sprintf("%dx%d", w, h))
}
args = append(args, filename, psname)
cmd := exec.CommandContext(context.Background(), "magick", args...)
stdOut := bytes.NewBuffer(nil)
stdErr := bytes.NewBuffer(nil)
cmd.Stdout = stdOut
cmd.Stderr = stdErr
fmt.Printf("running magick: %v\n", args)
if err := cmd.Run(); err != nil {
debug.Indent(stdErr, 1)
fmt.Fprintf(stdErr, `magick: %v
args= %v
%s
`,
err,
args,
stdErr.String(),
)
return "", errors.New(stdErr.String())
}
return psname, nil
}
func parseSize(s string) (w int, h int) {
fmt.Sscanf(strings.TrimSpace(s), "%dx%d", &w, &h)
return
}