/
graphics.go
222 lines (198 loc) · 4.59 KB
/
graphics.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Coordinated wrapper for cairo based graphics handling
package graphics
// #cgo pkg-config: cairo
// #include <cairo-pdf.h>
// #include <cairo-ps.h>
// #include <cairo-svg.h>
import "C"
import (
"errors"
"fmt"
"github.com/ungerik/go-cairo/extimage"
"image"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"unsafe"
)
const (
A5_WIDTH = 419.5276
A5_HEIGHT = 595.2756
)
type Graphic struct {
filename string
format string
surface *C.cairo_surface_t
cr *C.cairo_t
}
// Format is determined from filename extension
// Supported formats: eps, jpeg, pdf, png, ps, svg
//
// Width and height are in pts for eps, pdf, ps, and svg; pixels for jpeg and png.
// Pixel measures will be truncated into integers
//
// Close the graphic to write the file
func Create(filename string, width float64, height float64) (*Graphic, error) {
g := &Graphic{}
var err error
filename = filepath.Clean(filename)
g.filename, err = filepath.Abs(filename)
if err != nil {
return nil, err
}
g.format = filepath.Ext(filename)[1:]
// create the surface
switch g.format {
case "pdf":
g.surface = C.cairo_pdf_surface_create(
C.CString(filename),
C.double(width),
C.double(height),
)
case "png", "jpeg":
g.surface = C.cairo_image_surface_create(
C.CAIRO_FORMAT_ARGB32,
C.int(width),
C.int(height),
)
case "ps", "eps":
g.surface = C.cairo_ps_surface_create(
C.CString(filename),
C.double(width),
C.double(height),
)
if g.format == "eps" {
C.cairo_ps_surface_set_eps(g.surface, 1)
}
case "svg":
g.surface = C.cairo_svg_surface_create(
C.CString(filename),
C.double(width),
C.double(height),
)
default:
return nil, errors.New("cairo: unsupported format: " + g.format)
}
err = g.cairoSurfaceStatus()
if err != nil {
return nil, err
}
// create the cairo context
g.cr = C.cairo_create(g.surface)
err = g.cairoStatus()
if err != nil {
return nil, err
}
return g, nil
}
// completes and closes the file being written to
func (g *Graphic) Close() error {
// destroy the context
err := g.cairoStatus()
if err != nil {
return err
}
C.cairo_destroy(g.cr)
// finishing the surface writes pdf, ps, and svg files
C.cairo_surface_finish(g.surface)
err = g.cairoSurfaceStatus()
if err != nil {
return err
}
// write other formats
switch g.format {
case "eps", "pdf", "ps", "svg":
// cairo_surface_finish writes the surface to file
case "png":
img, err := g.Image()
if err != nil {
return err
}
file, err := os.Create(g.filename)
if err != nil {
return err
}
defer file.Close()
err = png.Encode(file, img)
if err != nil {
return err
}
case "jpeg":
img, err := g.Image()
if err != nil {
return err
}
file, err := os.Create(g.filename)
if err != nil {
return err
}
defer file.Close()
err = jpeg.Encode(file, img, nil)
if err != nil {
return err
}
default:
panic("unsupported format: " + g.format)
}
// destroy the surface
C.cairo_surface_destroy(g.surface)
err = g.cairoSurfaceStatus()
if err != nil {
return err
}
return nil
}
// pdf, ps, and svg surfaces use the current surface (page)
func (g *Graphic) Image() (image.Image, error) {
var surface *C.cairo_surface_t
switch g.format {
case "eps", "pdf", "ps", "svg":
// map vector surfaces to an image surface
surface = C.cairo_surface_map_to_image(g.surface, nil)
defer C.cairo_surface_unmap_image(g.surface, surface)
status := C.cairo_surface_status(surface)
err := statusToError(status)
if err != nil {
return nil, err
}
case "png", "jpeg":
// no conversion needed
surface = g.surface
}
width := int(C.cairo_image_surface_get_width(surface))
height := int(C.cairo_image_surface_get_height(surface))
stride := int(C.cairo_image_surface_get_stride(surface))
format := C.cairo_image_surface_get_format(surface)
dataPtr := C.cairo_image_surface_get_data(surface)
data := C.GoBytes(unsafe.Pointer(dataPtr), C.int(stride*height))
var img image.Image
switch format {
case C.CAIRO_FORMAT_ARGB32:
img = &extimage.ARGB{
Pix: data,
Stride: stride,
Rect: image.Rect(0, 0, width, height),
}
case C.CAIRO_FORMAT_RGB24:
img = &extimage.RGB{
Pix: data,
Stride: stride,
Rect: image.Rect(0, 0, width, height),
}
case C.CAIRO_FORMAT_A8:
img = &image.Alpha{
Pix: data,
Stride: stride,
Rect: image.Rect(0, 0, width, height),
}
default:
// known unsupported formats:
// CAIRO_FORMAT_INVALID = -1,
// CAIRO_FORMAT_A1 = 3,
// CAIRO_FORMAT_RGB16_565 = 4,
// CAIRO_FORMAT_RGB30 = 5
panic(fmt.Sprintf("unsupported cairo image surface format: %d", int(format)))
}
return img, nil
}