/
cairo.go
624 lines (502 loc) · 13.9 KB
/
cairo.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package cairo
// #cgo LDFLAGS: -lcairo
// #include <cairo/cairo.h>
// #include <stdlib.h>
import "C"
import (
"unsafe";
)
type Status int
// cairo_status_t values
const (
StatusSuccess Status = iota;
StatusNoMemory
StatusInvalidRestore
StatusInvalidPopGroup
StatusNoCurrentPoint
StatusInvalidMatrix
StatusInvalidStatus
StatusNullPointer
StatusInvalidString
StatusInvalidPathData
StatusReadError
StatusWriteError
StatusSurfaceFinished
StatusSurfaceTypeMismatch
StatusPatternTypeMismatch
StatusInvalidContent
StatusInvalidFormat
StatusInvalidVisual
StatusFileNotFound
StatusInvalidDash
StatusInvalidDscComment
StatusInvalidIndex
StatusClipNotRepresentable
StatusTempFileError
StatusInvalidStride
StatusFontTypeMismatch
StatusUserFontImmutable
StatusUserFontError
StatusNegativeCount
StatusInvalidClusters
StatusInvalidSlant
StatusInvalidWeight
StatusInvalidSize
StatusUserFontNotImplemented
StatusDeviceTypeMismatch
StatusDeviceError
StatusLast
)
type Content int
// cairo_content_t values
const (
ContentColor Content = iota * 0x1000
ContentAlpha
ContentColorAlpha
)
type Operator int
// cairo_operator_t values
const (
OperatorClear Operator = iota
OperatorSource
OperatorOver
OperatorIn
OperatorOut
OperatorAtop
OperatorDest
OperatorDestOver
OperatorDestIn
OperatorDestOut
OperatorDestAtop
OperatorXor
OperatorAdd
OperatorSaturate
OperatorMultiply
OperatorScreen
OperatorOverlay
OperatorDarken
OperatorLighten
OperatorColorDodge
OperatorColorBurn
OperatorHardLight
OperatorSoftLight
OperatorDifference
OperatorExclusion
OperatorHSLHue
OperatorHSLSaturation
OperatorHSLColor
OperatorHSLLuminosity
)
type Antialias int
// cairo_antialias_t values
const (
AntialiasDefault Antialias = iota
AntialiasNone
AntialiasGray
AntialiasSubpixel
)
type FillRule int
// cairo_fill_rule_t values
const (
FillRuleWinding FillRule = iota
FillRuleEvenOdd
)
type LineCap int
// cairo_line_cap_t values
const (
LineCapButt LineCap = iota
LineCapRound
LineCapSquare
)
type LineJoin int
// cairo_line_cap_join_t values
const (
LineJoinMiter LineJoin = iota
LineJoinRound
LineJoinBevel
)
type TextClusterFlag int
// cairo_text_cluster_flag_t value(s)
const (
TextClusterFlagBackward TextClusterFlag = 1 << iota
)
type FontSlant int
// cairo_font_slant_t values
const (
FontSlantNormal FontSlant = iota
FontSlantItalic
FontSlantOblique
)
type FontWeight int
// cairo_font_weight_t values
const (
FontWeightNormal FontWeight = iota
FontWeightBold
)
type SubpixelOrder int
// cairo_subpixel_order_t values
const (
SubpixelOrderDefault SubpixelOrder = iota
SubpixelOrderRGB
SubpixelOrderBGR
SubpixelOrderVRGB
SubpixelOrderVBGR
)
type HintStyle int
// cairo_hint_style_t values
const (
HintStyleDefault HintStyle = iota
HintStyleNone
HintStyleSlight
HintStyleMedium
HintStyleFull
)
type HintMetrics int
// cairo_hint_metrics_t values
const (
HintMetricsDefault HintMetrics = iota
HintMetricsOff
HintMetricsOn
)
type FontType int
// cairo_font_type_t values
const (
FontTypeToy FontType = iota
FontTypeFT
FontTypeWin32
FontTypeQuartz
FontTypeUser
)
type PathDataType int
// cairo_path_data_type_t values
const (
PathMoveTo PathDataType = iota
PathLineTo
PathCurveTo
PathClosePath
)
type SurfaceType int
// cairo_surface_type_t values
const (
SurfaceTypeImage SurfaceType = iota
SurfaceTypePDF
SurfaceTypePS
SurfaceTypeXlib
SurfaceTypeXCB
SurfaceTypeGlitz
SurfaceTypeQuartz
SurfaceTypeWin32
SurfaceTypeBeOS
SurfaceTypeDirectFB
SurfaceTypeSVG
SurfaceTypeOS2
SurfaceTypeWin32Printing
SurfaceTypeQuartzImage
SurfaceTypeScript
SurfaceTypeQt
SurfaceTypeRecording
SurfaceTypeVG
SurfaceTypeGL
SurfaceTypeDRM
SurfaceTypeTEE
SurfaceTypeXML
SurfaceTypeSkia
SurfaceTypeSubsurface
)
type Format int
// cairo_format_t values
const (
FormatArgB32 Format = iota
FormatRGB24
FormatA8
FormatA1
FormatRGB16_565
)
type PatternType int
// cairo_pattern_type_t values
const (
PatternTypeSolid PatternType = iota
PatternTypeSurface
PatternTypeLinear
PatternTypeRadial
)
type Extend int
// cairo_extend_t value
const (
ExtendNone Extend = iota
ExtendRepeat
ExtendReflect
ExtendPad
)
type Filter int
// cairo_filter_t values
const (
FilterFast Filter = iota
FilterGood
FilterBest
FilterNearest
FilterBilinear
FilterGaussian
)
// Utility functions
func cairobool2bool(flag C.cairo_bool_t) bool {
if int(flag) > 0 {
return true
}
return false;
}
type Matrix struct {
matrix *C.cairo_matrix_t;
}
type Pattern struct {
pattern *C.cairo_pattern_t;
}
// Golang struct to hold both a cairo surface and a cairo context
type Surface struct {
surface *C.cairo_surface_t;
context *C.cairo_t;
}
func Version() int { return int(C.cairo_version()) }
func NewSurface(format Format, width, height int) *Surface {
surface := new(Surface);
surface.surface = C.cairo_image_surface_create(C.cairo_format_t(format), C.int(width), C.int(height));
surface.context = C.cairo_create(surface.surface);
return surface;
}
func NewSurfaceFromPNG(filename string) *Surface {
surface := new(Surface);
p := C.CString(filename);
surface.surface = C.cairo_image_surface_create_from_png(p);
C.free(unsafe.Pointer(p));
surface.context = C.cairo_create(surface.surface);
return surface;
}
func (surface *Surface) SurfaceStatus() int {
return int(C.cairo_surface_status(surface.surface))
}
func (self *Surface) Save() { C.cairo_save(self.context) }
func (self *Surface) Restore() { C.cairo_restore(self.context) }
func (self *Surface) PushGroup() { C.cairo_push_group(self.context) }
func (self *Surface) PushGroupWithContent(content Content) {
C.cairo_push_group_with_content(self.context, C.cairo_content_t(content))
}
func (self *Surface) PopGroup() (pattern *Pattern) {
pattern = new(Pattern);
pattern.pattern = C.cairo_pop_group(self.context);
return;
}
func (self *Surface) PopGroupToSource() { C.cairo_pop_group_to_source(self.context) }
func (self *Surface) SetOperator(operator Operator) {
C.cairo_set_operator(self.context, C.cairo_operator_t(operator))
}
func (self *Surface) SetSource(pattern *Pattern) {
C.cairo_set_source(self.context, pattern.pattern)
}
func (self *Surface) SetSourceRGB(red, green, blue float64) {
C.cairo_set_source_rgb(self.context, C.double(red), C.double(green), C.double(blue))
}
func (self *Surface) SetSourceRGBA(red, green, blue, alpha float64) {
C.cairo_set_source_rgba(self.context, C.double(red), C.double(green), C.double(blue), C.double(alpha))
}
func (self *Surface) SetSourceSurface(surface *Surface, x, y float64) {
C.cairo_set_source_surface(self.context, surface.surface, C.double(x), C.double(y))
}
func (self *Surface) SetTolerance(tolerance float64) {
C.cairo_set_tolerance(self.context, C.double(tolerance))
}
func (self *Surface) SetAntialias(antialias Antialias) {
C.cairo_set_antialias(self.context, C.cairo_antialias_t(antialias))
}
func (self *Surface) SetFillRule(fill_rule FillRule) {
C.cairo_set_fill_rule(self.context, C.cairo_fill_rule_t(fill_rule))
}
func (self *Surface) SetLineWidth(width float64) {
C.cairo_set_line_width(self.context, C.double(width))
}
func (self *Surface) SetLineCap(line_cap LineCap) {
C.cairo_set_line_cap(self.context, C.cairo_line_cap_t(line_cap))
}
func (self *Surface) SetLineJoin(line_join LineJoin) {
C.cairo_set_line_join(self.context, C.cairo_line_join_t(line_join))
}
// TODO: Figure out how to convert a slice into C
/*func (self *Surface) SetDash(dashes []float64, num_dashes int, offset float64){
dashesp := (*C.double)(&dashes);
C.cairo_set_dash(self.context, dashesp, C.int(num_dashes), C.double(offset));
}
*/
func (self *Surface) SetMiterLimit(limit float64) {
C.cairo_set_miter_limit(self.context, C.double(limit))
}
func (self *Surface) Translate(tx, ty float64) {
C.cairo_translate(self.context, C.double(tx), C.double(ty))
}
func (self *Surface) Scale(sx, sy float64) {
C.cairo_scale(self.context, C.double(sx), C.double(sy))
}
func (self *Surface) Rotate(angle float64) { C.cairo_rotate(self.context, C.double(angle)) }
func (self *Surface) Transform(matrix *Matrix) {
C.cairo_transform(self.context, matrix.matrix)
}
func (self *Surface) SetMatrix(matrix *Matrix) {
C.cairo_set_matrix(self.context, matrix.matrix)
}
func (self *Surface) IdentityMatrix() { C.cairo_identity_matrix(self.context) }
func (self *Surface) UserToDevice(x, y float64) (x1, y1 float64) {
ux, uy := (*C.double)(&x), (*C.double)(&y);
C.cairo_user_to_device(self.context, ux, uy);
x1, y1 = float64(*ux), float64(*uy);
return;
}
func (self *Surface) UserToDeviceDistance(dx, dy float64) (dx1, dy1 float64) {
ux, uy := (*C.double)(&dx), (*C.double)(&dy);
C.cairo_user_to_device_distance(self.context, ux, uy);
dx1, dy1 = float64(*ux), float64(*uy);
return;
}
// path creation methods
func (self *Surface) NewPath() { C.cairo_new_path(self.context) }
func (self *Surface) MoveTo(x, y float64) {
C.cairo_move_to(self.context, C.double(x), C.double(y))
}
func (self *Surface) NewSubPath() { C.cairo_new_sub_path(self.context) }
func (self *Surface) LineTo(x, y float64) {
C.cairo_line_to(self.context, C.double(x), C.double(y))
}
func (self *Surface) CurveTo(x1, y1, x2, y2, x3, y3 float64) {
C.cairo_curve_to(self.context,
C.double(x1), C.double(y1),
C.double(x2), C.double(y2),
C.double(x3), C.double(y3))
}
func (self *Surface) Arc(xc, yc, radius, angle1, angle2 float64) {
C.cairo_arc(self.context,
C.double(xc), C.double(yc),
C.double(radius),
C.double(angle1), C.double(angle2))
}
func (self *Surface) ArcNegative(xc, yc, radius, angle1, angle2 float64) {
C.cairo_arc_negative(self.context,
C.double(xc), C.double(yc),
C.double(radius),
C.double(angle1), C.double(angle2))
}
func (self *Surface) RelMoveTo(dx, dy float64) {
C.cairo_rel_move_to(self.context, C.double(dx), C.double(dy))
}
func (self *Surface) RelLineTo(dx, dy float64) {
C.cairo_rel_line_to(self.context, C.double(dx), C.double(dy))
}
func (self *Surface) RelCurveTo(dx1, dy1, dx2, dy2, dx3, dy3 float64) {
C.cairo_rel_curve_to(self.context,
C.double(dx1), C.double(dy1),
C.double(dx2), C.double(dy2),
C.double(dx3), C.double(dy3))
}
func (self *Surface) Rectangle(x, y, width, height float64) {
C.cairo_rectangle(self.context,
C.double(x), C.double(y),
C.double(width), C.double(height))
}
func (self *Surface) ClosePath() { C.cairo_close_path(self.context) }
func (self *Surface) PathExtents() (left, top, right, bottom float64) {
var x1, y1, x2, y2 float64;
px1 := (*C.double)(&x1);
py1 := (*C.double)(&y1);
px2 := (*C.double)(&x2);
py2 := (*C.double)(&y2);
C.cairo_path_extents(self.context, px1, py1, px2, py2);
left = float64(*px1);
top = float64(*py1);
right = float64(*px2);
bottom = float64(*py2);
return;
}
// Painting methods
func (self *Surface) Paint() { C.cairo_paint(self.context) }
func (self *Surface) PaintWithAlpha(alpha float64) {
C.cairo_paint_with_alpha(self.context, C.double(alpha))
}
func (self *Surface) Mask(pattern *Pattern) { C.cairo_mask(self.context, pattern.pattern) }
func (self *Surface) MaskSurface(surface *Surface, surface_x, surface_y float64) {
C.cairo_mask_surface(self.context, surface.surface, C.double(surface_x), C.double(surface_y))
}
func (self *Surface) Stroke() { C.cairo_stroke(self.context) }
func (self *Surface) StrokePreserve() { C.cairo_stroke_preserve(self.context) }
func (self *Surface) Fill() { C.cairo_fill(self.context) }
func (self *Surface) FillPreserve() { C.cairo_fill_preserve(self.context) }
func (self *Surface) CopyPage() { C.cairo_copy_page(self.context) }
func (self *Surface) ShowPage() { C.cairo_show_page(self.context) }
// Insideness testing
func (self *Surface) InStroke(x, y float64) bool {
ret := C.cairo_in_stroke(self.context, C.double(x), C.double(y));
return cairobool2bool(ret);
}
func (self *Surface) InFill(x, y float64) bool {
ret := C.cairo_in_fill(self.context, C.double(x), C.double(y));
return cairobool2bool(ret);
}
// Rectangular extents
func (self *Surface) StrokeExtents() (left, top, right, bottom float64) {
var x1, y1, x2, y2 float64;
px1 := (*C.double)(&x1);
py1 := (*C.double)(&y1);
px2 := (*C.double)(&x2);
py2 := (*C.double)(&y2);
C.cairo_stroke_extents(self.context, px1, py1, px2, py2);
left = float64(*px1);
top = float64(*py1);
right = float64(*px2);
bottom = float64(*py2);
return;
}
func (self *Surface) FillExtents() (left, top, right, bottom float64) {
var x1, y1, x2, y2 float64;
px1 := (*C.double)(&x1);
py1 := (*C.double)(&y1);
px2 := (*C.double)(&x2);
py2 := (*C.double)(&y2);
C.cairo_fill_extents(self.context, px1, py1, px2, py2);
left = float64(*px1);
top = float64(*py1);
right = float64(*px2);
bottom = float64(*py2);
return;
}
// Clipping methods
func (self *Surface) ResetClip() { C.cairo_reset_clip(self.context) }
func (self *Surface) Clip() { C.cairo_clip(self.context) }
func (self *Surface) ClipPreserve() { C.cairo_clip_preserve(self.context) }
func (self *Surface) ClipExtents() (left, top, right, bottom float64) {
var x1, y1, x2, y2 float64;
px1 := (*C.double)(&x1);
py1 := (*C.double)(&y1);
px2 := (*C.double)(&x2);
py2 := (*C.double)(&y2);
C.cairo_clip_extents(self.context, px1, py1, px2, py2);
left = float64(*px1);
top = float64(*py1);
right = float64(*px2);
bottom = float64(*py2);
return;
}
func (self *Surface) SelectFontFace(name string, font_slant FontSlant, font_weight FontWeight) {
p := C.CString(name);
C.cairo_select_font_face(self.context, p, C.cairo_font_slant_t(font_slant), C.cairo_font_weight_t(font_weight));
C.free(unsafe.Pointer(p));
}
func (self *Surface) SetFontSize(size float64) {
C.cairo_set_font_size(self.context, C.double(size))
}
func (self *Surface) ShowText(text string) {
p := C.CString(text);
C.cairo_show_text(self.context, p);
C.free(unsafe.Pointer(p));
}
func (self *Surface) Finish() { C.cairo_destroy(self.context) }
func (self *Surface) WriteToPNG(filename string) {
p := C.CString(filename);
C.cairo_surface_write_to_png(self.surface, p);
C.free(unsafe.Pointer(p));
}
func (self *Surface) Destroy() { C.cairo_surface_destroy(self.surface) }