/
testpostscript.go
53 lines (50 loc) · 1 KB
/
testpostscript.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
package main
import (
"bufio"
"code.google.com/p/draw2d/draw2d"
"code.google.com/p/draw2d/postscript"
"fmt"
"image"
"image/png"
"io/ioutil"
"log"
"os"
"strings"
)
func saveToPngFile(filePath string, m image.Image) {
f, err := os.Create(filePath)
if err != nil {
log.Println(err)
os.Exit(1)
}
defer f.Close()
b := bufio.NewWriter(f)
err = png.Encode(b, m)
if err != nil {
log.Println(err)
os.Exit(1)
}
err = b.Flush()
if err != nil {
log.Println(err)
os.Exit(1)
}
fmt.Printf("Wrote %s OK.\n", filePath)
}
func main() {
i := image.NewRGBA(image.Rect(0, 0, 600, 800))
gc := draw2d.NewGraphicContext(i)
gc.Translate(0, 380)
gc.Scale(1, -1)
gc.Translate(0, -380)
src, err := os.OpenFile("../resource/postscript/tiger.ps", 0, 0)
if err != nil {
return
}
defer src.Close()
bytes, err := ioutil.ReadAll(src)
reader := strings.NewReader(string(bytes))
interpreter := postscript.NewInterpreter(gc)
interpreter.Execute(reader)
saveToPngFile("../resource/result/TestPostscript.png", i)
}