AtlatestRepositorysigil-cairo
sigil-cairo / tree / srccairo.sgl
1
;;; (cairo) — Cairo 2D graphics bindings for Sigil2
;;;3
;;; Pure-Scheme bindings using the dynamic FFI. No native C code required.4
;;; Requires libcairo.so / libcairo.dylib to be installed on the system.6
(define-library (cairo)7
(import (sigil core)8
(sigil ffi))10
(export11
;; Library handle12
cairo-lib14
;; Constants — Format15
CAIRO_FORMAT_INVALID16
CAIRO_FORMAT_ARGB3217
CAIRO_FORMAT_RGB2418
CAIRO_FORMAT_A819
CAIRO_FORMAT_A121
;; Constants — Status22
CAIRO_STATUS_SUCCESS23
CAIRO_STATUS_NO_MEMORY24
CAIRO_STATUS_INVALID_RESTORE25
CAIRO_STATUS_INVALID_POP_GROUP26
CAIRO_STATUS_NO_CURRENT_POINT27
CAIRO_STATUS_INVALID_MATRIX28
CAIRO_STATUS_INVALID_STATUS29
CAIRO_STATUS_NULL_POINTER30
CAIRO_STATUS_INVALID_STRING31
CAIRO_STATUS_INVALID_PATH_DATA32
CAIRO_STATUS_READ_ERROR33
CAIRO_STATUS_WRITE_ERROR34
CAIRO_STATUS_SURFACE_FINISHED35
CAIRO_STATUS_SURFACE_TYPE_MISMATCH36
CAIRO_STATUS_PATTERN_TYPE_MISMATCH38
;; Constants — Operator39
CAIRO_OPERATOR_CLEAR40
CAIRO_OPERATOR_SOURCE41
CAIRO_OPERATOR_OVER42
CAIRO_OPERATOR_IN43
CAIRO_OPERATOR_OUT44
CAIRO_OPERATOR_ATOP45
CAIRO_OPERATOR_DEST46
CAIRO_OPERATOR_DEST_OVER47
CAIRO_OPERATOR_DEST_IN48
CAIRO_OPERATOR_DEST_OUT49
CAIRO_OPERATOR_DEST_ATOP50
CAIRO_OPERATOR_XOR51
CAIRO_OPERATOR_ADD52
CAIRO_OPERATOR_SATURATE53
CAIRO_OPERATOR_MULTIPLY54
CAIRO_OPERATOR_SCREEN55
CAIRO_OPERATOR_OVERLAY56
CAIRO_OPERATOR_DARKEN57
CAIRO_OPERATOR_LIGHTEN58
CAIRO_OPERATOR_COLOR_DODGE59
CAIRO_OPERATOR_COLOR_BURN60
CAIRO_OPERATOR_HARD_LIGHT61
CAIRO_OPERATOR_SOFT_LIGHT62
CAIRO_OPERATOR_DIFFERENCE63
CAIRO_OPERATOR_EXCLUSION65
;; Constants — Line cap/join66
CAIRO_LINE_CAP_BUTT67
CAIRO_LINE_CAP_ROUND68
CAIRO_LINE_CAP_SQUARE69
CAIRO_LINE_JOIN_MITER70
CAIRO_LINE_JOIN_ROUND71
CAIRO_LINE_JOIN_BEVEL73
;; Constants — Fill rule74
CAIRO_FILL_RULE_WINDING75
CAIRO_FILL_RULE_EVEN_ODD77
;; Constants — Antialias78
CAIRO_ANTIALIAS_DEFAULT79
CAIRO_ANTIALIAS_NONE80
CAIRO_ANTIALIAS_GRAY81
CAIRO_ANTIALIAS_SUBPIXEL82
CAIRO_ANTIALIAS_FAST83
CAIRO_ANTIALIAS_GOOD84
CAIRO_ANTIALIAS_BEST86
;; Constants — Font87
CAIRO_FONT_SLANT_NORMAL88
CAIRO_FONT_SLANT_ITALIC89
CAIRO_FONT_SLANT_OBLIQUE90
CAIRO_FONT_WEIGHT_NORMAL91
CAIRO_FONT_WEIGHT_BOLD93
;; Constants — Extend94
CAIRO_EXTEND_NONE95
CAIRO_EXTEND_REPEAT96
CAIRO_EXTEND_REFLECT97
CAIRO_EXTEND_PAD99
;; Constants — Content100
CAIRO_CONTENT_COLOR101
CAIRO_CONTENT_ALPHA102
CAIRO_CONTENT_COLOR_ALPHA104
;; Struct layouts105
cairo-text-extents-layout106
cairo-font-extents-layout107
cairo-matrix-layout109
;; Surfaces110
cairo-image-surface-create111
cairo-image-surface-create-from-png112
cairo-surface-destroy113
cairo-surface-write-to-png114
cairo-surface-flush115
cairo-surface-finish116
cairo-surface-status117
cairo-surface-create-similar118
cairo-image-surface-get-width119
cairo-image-surface-get-height120
cairo-image-surface-get-stride121
cairo-image-surface-get-format122
cairo-image-surface-get-data123
cairo-pdf-surface-create124
cairo-svg-surface-create125
cairo-pdf-surface-set-size127
;; Context128
cairo-create129
cairo-destroy130
cairo-save131
cairo-restore132
cairo-status133
cairo-status-to-string134
cairo-push-group135
cairo-pop-group136
cairo-pop-group-to-source137
cairo-get-target139
;; Drawing state140
cairo-set-source-rgb141
cairo-set-source-rgba142
cairo-set-source-surface143
cairo-set-source144
cairo-set-line-width145
cairo-get-line-width146
cairo-set-line-cap147
cairo-set-line-join148
cairo-set-miter-limit149
cairo-set-dash150
cairo-set-fill-rule151
cairo-set-operator152
cairo-set-antialias154
;; Paths155
cairo-new-path156
cairo-new-sub-path157
cairo-close-path158
cairo-move-to159
cairo-line-to160
cairo-curve-to161
cairo-arc162
cairo-arc-negative163
cairo-rectangle164
cairo-rel-move-to165
cairo-rel-line-to166
cairo-rel-curve-to168
;; Drawing ops169
cairo-stroke170
cairo-fill171
cairo-paint172
cairo-stroke-preserve173
cairo-fill-preserve174
cairo-paint-with-alpha175
cairo-clip176
cairo-clip-preserve177
cairo-reset-clip178
cairo-mask179
cairo-mask-surface181
;; Text182
cairo-select-font-face183
cairo-set-font-size184
cairo-show-text185
cairo-text-extents186
cairo-font-extents188
;; Glyph rendering189
cairo-glyph-layout190
cairo-show-glyphs191
cairo-glyph-path192
cairo-ft-font-face-create193
cairo-set-font-face194
cairo-font-face-destroy196
;; Transforms197
cairo-translate198
cairo-scale199
cairo-rotate200
cairo-identity-matrix201
cairo-get-matrix202
cairo-set-matrix204
;; Patterns205
cairo-pattern-create-rgb206
cairo-pattern-create-rgba207
cairo-pattern-create-linear208
cairo-pattern-create-radial209
cairo-pattern-add-color-stop-rgb210
cairo-pattern-add-color-stop-rgba211
cairo-pattern-destroy212
cairo-pattern-set-extend213
cairo-pattern-create-for-surface215
;; Convenience216
with-cairo-surface217
with-cairo-context)219
(begin221
;; ============================================================222
;; Library Loading223
;; ============================================================225
(define cairo-lib (c-library "libcairo"))227
;; ============================================================228
;; Constants229
;; ============================================================231
;; Format232
(define CAIRO_FORMAT_INVALID -1)233
(define CAIRO_FORMAT_ARGB32 0)234
(define CAIRO_FORMAT_RGB24 1)235
(define CAIRO_FORMAT_A8 2)236
(define CAIRO_FORMAT_A1 3)238
;; Status239
(define CAIRO_STATUS_SUCCESS 0)240
(define CAIRO_STATUS_NO_MEMORY 1)241
(define CAIRO_STATUS_INVALID_RESTORE 2)242
(define CAIRO_STATUS_INVALID_POP_GROUP 3)243
(define CAIRO_STATUS_NO_CURRENT_POINT 4)244
(define CAIRO_STATUS_INVALID_MATRIX 5)245
(define CAIRO_STATUS_INVALID_STATUS 6)246
(define CAIRO_STATUS_NULL_POINTER 7)247
(define CAIRO_STATUS_INVALID_STRING 8)248
(define CAIRO_STATUS_INVALID_PATH_DATA 9)249
(define CAIRO_STATUS_READ_ERROR 10)250
(define CAIRO_STATUS_WRITE_ERROR 11)251
(define CAIRO_STATUS_SURFACE_FINISHED 12)252
(define CAIRO_STATUS_SURFACE_TYPE_MISMATCH 13)253
(define CAIRO_STATUS_PATTERN_TYPE_MISMATCH 14)255
;; Operator256
(define CAIRO_OPERATOR_CLEAR 0)257
(define CAIRO_OPERATOR_SOURCE 1)258
(define CAIRO_OPERATOR_OVER 2)259
(define CAIRO_OPERATOR_IN 3)260
(define CAIRO_OPERATOR_OUT 4)261
(define CAIRO_OPERATOR_ATOP 5)262
(define CAIRO_OPERATOR_DEST 6)263
(define CAIRO_OPERATOR_DEST_OVER 7)264
(define CAIRO_OPERATOR_DEST_IN 8)265
(define CAIRO_OPERATOR_DEST_OUT 9)266
(define CAIRO_OPERATOR_DEST_ATOP 10)267
(define CAIRO_OPERATOR_XOR 11)268
(define CAIRO_OPERATOR_ADD 12)269
(define CAIRO_OPERATOR_SATURATE 13)270
(define CAIRO_OPERATOR_MULTIPLY 14)271
(define CAIRO_OPERATOR_SCREEN 15)272
(define CAIRO_OPERATOR_OVERLAY 16)273
(define CAIRO_OPERATOR_DARKEN 17)274
(define CAIRO_OPERATOR_LIGHTEN 18)275
(define CAIRO_OPERATOR_COLOR_DODGE 19)276
(define CAIRO_OPERATOR_COLOR_BURN 20)277
(define CAIRO_OPERATOR_HARD_LIGHT 21)278
(define CAIRO_OPERATOR_SOFT_LIGHT 22)279
(define CAIRO_OPERATOR_DIFFERENCE 23)280
(define CAIRO_OPERATOR_EXCLUSION 24)282
;; Line cap283
(define CAIRO_LINE_CAP_BUTT 0)284
(define CAIRO_LINE_CAP_ROUND 1)285
(define CAIRO_LINE_CAP_SQUARE 2)287
;; Line join288
(define CAIRO_LINE_JOIN_MITER 0)289
(define CAIRO_LINE_JOIN_ROUND 1)290
(define CAIRO_LINE_JOIN_BEVEL 2)292
;; Fill rule293
(define CAIRO_FILL_RULE_WINDING 0)294
(define CAIRO_FILL_RULE_EVEN_ODD 1)296
;; Antialias297
(define CAIRO_ANTIALIAS_DEFAULT 0)298
(define CAIRO_ANTIALIAS_NONE 1)299
(define CAIRO_ANTIALIAS_GRAY 2)300
(define CAIRO_ANTIALIAS_SUBPIXEL 3)301
(define CAIRO_ANTIALIAS_FAST 4)302
(define CAIRO_ANTIALIAS_GOOD 5)303
(define CAIRO_ANTIALIAS_BEST 6)305
;; Font slant/weight306
(define CAIRO_FONT_SLANT_NORMAL 0)307
(define CAIRO_FONT_SLANT_ITALIC 1)308
(define CAIRO_FONT_SLANT_OBLIQUE 2)309
(define CAIRO_FONT_WEIGHT_NORMAL 0)310
(define CAIRO_FONT_WEIGHT_BOLD 1)312
;; Extend313
(define CAIRO_EXTEND_NONE 0)314
(define CAIRO_EXTEND_REPEAT 1)315
(define CAIRO_EXTEND_REFLECT 2)316
(define CAIRO_EXTEND_PAD 3)318
;; Content319
(define CAIRO_CONTENT_COLOR #x1000)320
(define CAIRO_CONTENT_ALPHA #x2000)321
(define CAIRO_CONTENT_COLOR_ALPHA #x3000)323
;; ============================================================324
;; Struct Layouts325
;; ============================================================327
(define cairo-text-extents-layout328
(c-struct-layout329
x-bearing: ffi/double330
y-bearing: ffi/double331
width: ffi/double332
height: ffi/double333
x-advance: ffi/double334
y-advance: ffi/double))336
(define cairo-font-extents-layout337
(c-struct-layout338
ascent: ffi/double339
descent: ffi/double340
height: ffi/double341
max-x-advance: ffi/double342
max-y-advance: ffi/double))344
(define cairo-matrix-layout345
(c-struct-layout346
xx: ffi/double347
yx: ffi/double348
xy: ffi/double349
yy: ffi/double350
x0: ffi/double351
y0: ffi/double))353
;; cairo_glyph_t — for glyph-level text rendering354
(define cairo-glyph-layout355
(c-struct-layout356
index: ffi/ulong357
x: ffi/double358
y: ffi/double))360
;; ============================================================361
;; Finalizer Pointers (looked up once)362
;; ============================================================364
(define %cairo-destroy-ptr (c-symbol cairo-lib "cairo_destroy"))365
(define %surface-destroy-ptr (c-symbol cairo-lib "cairo_surface_destroy"))366
(define %pattern-destroy-ptr (c-symbol cairo-lib "cairo_pattern_destroy"))367
(define %font-face-destroy-ptr (c-symbol cairo-lib "cairo_font_face_destroy"))369
;; ============================================================370
;; Raw Function Bindings371
;; ============================================================373
;; Surfaces374
(define %image-surface-create375
(c-function cairo-lib "cairo_image_surface_create"376
(list ffi/int ffi/int ffi/int) ffi/pointer))378
(define %image-surface-create-from-png379
(c-function cairo-lib "cairo_image_surface_create_from_png"380
(list ffi/string) ffi/pointer))382
(define %surface-destroy383
(c-function cairo-lib "cairo_surface_destroy"384
(list ffi/pointer) ffi/void))386
(define %surface-write-to-png387
(c-function cairo-lib "cairo_surface_write_to_png"388
(list ffi/pointer ffi/string) ffi/int))390
(define %surface-flush391
(c-function cairo-lib "cairo_surface_flush"392
(list ffi/pointer) ffi/void))394
(define %surface-finish395
(c-function cairo-lib "cairo_surface_finish"396
(list ffi/pointer) ffi/void))398
(define %surface-status399
(c-function cairo-lib "cairo_surface_status"400
(list ffi/pointer) ffi/int))402
(define %surface-create-similar403
(c-function cairo-lib "cairo_surface_create_similar"404
(list ffi/pointer ffi/int ffi/int ffi/int) ffi/pointer))406
(define %image-surface-get-width407
(c-function cairo-lib "cairo_image_surface_get_width"408
(list ffi/pointer) ffi/int))410
(define %image-surface-get-height411
(c-function cairo-lib "cairo_image_surface_get_height"412
(list ffi/pointer) ffi/int))414
(define %image-surface-get-stride415
(c-function cairo-lib "cairo_image_surface_get_stride"416
(list ffi/pointer) ffi/int))418
(define %image-surface-get-format419
(c-function cairo-lib "cairo_image_surface_get_format"420
(list ffi/pointer) ffi/int))422
(define %image-surface-get-data423
(c-function cairo-lib "cairo_image_surface_get_data"424
(list ffi/pointer) ffi/pointer))426
;; PDF/SVG surfaces (may not be available on all systems)427
(define %pdf-surface-create428
(c-function cairo-lib "cairo_pdf_surface_create"429
(list ffi/string ffi/double ffi/double) ffi/pointer))431
(define %svg-surface-create432
(c-function cairo-lib "cairo_svg_surface_create"433
(list ffi/string ffi/double ffi/double) ffi/pointer))435
(define %pdf-surface-set-size436
(c-function cairo-lib "cairo_pdf_surface_set_size"437
(list ffi/pointer ffi/double ffi/double) ffi/void))439
;; Context440
(define %cairo-create441
(c-function cairo-lib "cairo_create"442
(list ffi/pointer) ffi/pointer))444
(define %cairo-destroy445
(c-function cairo-lib "cairo_destroy"446
(list ffi/pointer) ffi/void))448
(define %cairo-save449
(c-function cairo-lib "cairo_save"450
(list ffi/pointer) ffi/void))452
(define %cairo-restore453
(c-function cairo-lib "cairo_restore"454
(list ffi/pointer) ffi/void))456
(define %cairo-status457
(c-function cairo-lib "cairo_status"458
(list ffi/pointer) ffi/int))460
(define %cairo-status-to-string461
(c-function cairo-lib "cairo_status_to_string"462
(list ffi/int) ffi/string))464
(define %cairo-push-group465
(c-function cairo-lib "cairo_push_group"466
(list ffi/pointer) ffi/void))468
(define %cairo-pop-group469
(c-function cairo-lib "cairo_pop_group"470
(list ffi/pointer) ffi/pointer))472
(define %cairo-pop-group-to-source473
(c-function cairo-lib "cairo_pop_group_to_source"474
(list ffi/pointer) ffi/void))476
(define %cairo-get-target477
(c-function cairo-lib "cairo_get_target"478
(list ffi/pointer) ffi/pointer))480
;; Drawing state481
(define %set-source-rgb482
(c-function cairo-lib "cairo_set_source_rgb"483
(list ffi/pointer ffi/double ffi/double ffi/double) ffi/void))485
(define %set-source-rgba486
(c-function cairo-lib "cairo_set_source_rgba"487
(list ffi/pointer ffi/double ffi/double ffi/double ffi/double) ffi/void))489
(define %set-source-surface490
(c-function cairo-lib "cairo_set_source_surface"491
(list ffi/pointer ffi/pointer ffi/double ffi/double) ffi/void))493
(define %set-source494
(c-function cairo-lib "cairo_set_source"495
(list ffi/pointer ffi/pointer) ffi/void))497
(define %set-line-width498
(c-function cairo-lib "cairo_set_line_width"499
(list ffi/pointer ffi/double) ffi/void))501
(define %get-line-width502
(c-function cairo-lib "cairo_get_line_width"503
(list ffi/pointer) ffi/double))505
(define %set-line-cap506
(c-function cairo-lib "cairo_set_line_cap"507
(list ffi/pointer ffi/int) ffi/void))509
(define %set-line-join510
(c-function cairo-lib "cairo_set_line_join"511
(list ffi/pointer ffi/int) ffi/void))513
(define %set-miter-limit514
(c-function cairo-lib "cairo_set_miter_limit"515
(list ffi/pointer ffi/double) ffi/void))517
(define %set-dash518
(c-function cairo-lib "cairo_set_dash"519
(list ffi/pointer ffi/pointer ffi/int ffi/double) ffi/void))521
(define %set-fill-rule522
(c-function cairo-lib "cairo_set_fill_rule"523
(list ffi/pointer ffi/int) ffi/void))525
(define %set-operator526
(c-function cairo-lib "cairo_set_operator"527
(list ffi/pointer ffi/int) ffi/void))529
(define %set-antialias530
(c-function cairo-lib "cairo_set_antialias"531
(list ffi/pointer ffi/int) ffi/void))533
;; Paths534
(define %new-path535
(c-function cairo-lib "cairo_new_path"536
(list ffi/pointer) ffi/void))538
(define %new-sub-path539
(c-function cairo-lib "cairo_new_sub_path"540
(list ffi/pointer) ffi/void))542
(define %close-path543
(c-function cairo-lib "cairo_close_path"544
(list ffi/pointer) ffi/void))546
(define %move-to547
(c-function cairo-lib "cairo_move_to"548
(list ffi/pointer ffi/double ffi/double) ffi/void))550
(define %line-to551
(c-function cairo-lib "cairo_line_to"552
(list ffi/pointer ffi/double ffi/double) ffi/void))554
(define %curve-to555
(c-function cairo-lib "cairo_curve_to"556
(list ffi/pointer ffi/double ffi/double ffi/double ffi/double ffi/double ffi/double) ffi/void))558
(define %arc559
(c-function cairo-lib "cairo_arc"560
(list ffi/pointer ffi/double ffi/double ffi/double ffi/double ffi/double) ffi/void))562
(define %arc-negative563
(c-function cairo-lib "cairo_arc_negative"564
(list ffi/pointer ffi/double ffi/double ffi/double ffi/double ffi/double) ffi/void))566
(define %rectangle567
(c-function cairo-lib "cairo_rectangle"568
(list ffi/pointer ffi/double ffi/double ffi/double ffi/double) ffi/void))570
(define %rel-move-to571
(c-function cairo-lib "cairo_rel_move_to"572
(list ffi/pointer ffi/double ffi/double) ffi/void))574
(define %rel-line-to575
(c-function cairo-lib "cairo_rel_line_to"576
(list ffi/pointer ffi/double ffi/double) ffi/void))578
(define %rel-curve-to579
(c-function cairo-lib "cairo_rel_curve_to"580
(list ffi/pointer ffi/double ffi/double ffi/double ffi/double ffi/double ffi/double) ffi/void))582
;; Drawing ops583
(define %stroke584
(c-function cairo-lib "cairo_stroke"585
(list ffi/pointer) ffi/void))587
(define %fill588
(c-function cairo-lib "cairo_fill"589
(list ffi/pointer) ffi/void))591
(define %paint592
(c-function cairo-lib "cairo_paint"593
(list ffi/pointer) ffi/void))595
(define %stroke-preserve596
(c-function cairo-lib "cairo_stroke_preserve"597
(list ffi/pointer) ffi/void))599
(define %fill-preserve600
(c-function cairo-lib "cairo_fill_preserve"601
(list ffi/pointer) ffi/void))603
(define %paint-with-alpha604
(c-function cairo-lib "cairo_paint_with_alpha"605
(list ffi/pointer ffi/double) ffi/void))607
(define %clip608
(c-function cairo-lib "cairo_clip"609
(list ffi/pointer) ffi/void))611
(define %clip-preserve612
(c-function cairo-lib "cairo_clip_preserve"613
(list ffi/pointer) ffi/void))615
(define %reset-clip616
(c-function cairo-lib "cairo_reset_clip"617
(list ffi/pointer) ffi/void))619
(define %mask620
(c-function cairo-lib "cairo_mask"621
(list ffi/pointer ffi/pointer) ffi/void))623
(define %mask-surface624
(c-function cairo-lib "cairo_mask_surface"625
(list ffi/pointer ffi/pointer ffi/double ffi/double) ffi/void))627
;; Text628
(define %select-font-face629
(c-function cairo-lib "cairo_select_font_face"630
(list ffi/pointer ffi/string ffi/int ffi/int) ffi/void))632
(define %set-font-size633
(c-function cairo-lib "cairo_set_font_size"634
(list ffi/pointer ffi/double) ffi/void))636
(define %show-text637
(c-function cairo-lib "cairo_show_text"638
(list ffi/pointer ffi/string) ffi/void))640
(define %text-extents641
(c-function cairo-lib "cairo_text_extents"642
(list ffi/pointer ffi/string ffi/pointer) ffi/void))644
(define %font-extents645
(c-function cairo-lib "cairo_font_extents"646
(list ffi/pointer ffi/pointer) ffi/void))648
;; Glyph rendering649
(define %show-glyphs650
(c-function cairo-lib "cairo_show_glyphs"651
(list ffi/pointer ffi/pointer ffi/int) ffi/void))653
(define %set-font-face654
(c-function cairo-lib "cairo_set_font_face"655
(list ffi/pointer ffi/pointer) ffi/void))657
(define %font-face-destroy658
(c-function cairo-lib "cairo_font_face_destroy"659
(list ffi/pointer) ffi/void))661
(define %ft-font-face-create662
(c-function cairo-lib "cairo_ft_font_face_create_for_ft_face"663
(list ffi/pointer ffi/int) ffi/pointer))665
;; Transforms666
(define %translate667
(c-function cairo-lib "cairo_translate"668
(list ffi/pointer ffi/double ffi/double) ffi/void))670
(define %scale671
(c-function cairo-lib "cairo_scale"672
(list ffi/pointer ffi/double ffi/double) ffi/void))674
(define %rotate675
(c-function cairo-lib "cairo_rotate"676
(list ffi/pointer ffi/double) ffi/void))678
(define %identity-matrix679
(c-function cairo-lib "cairo_identity_matrix"680
(list ffi/pointer) ffi/void))682
(define %get-matrix683
(c-function cairo-lib "cairo_get_matrix"684
(list ffi/pointer ffi/pointer) ffi/void))686
(define %set-matrix687
(c-function cairo-lib "cairo_set_matrix"688
(list ffi/pointer ffi/pointer) ffi/void))690
;; Patterns691
(define %pattern-create-rgb692
(c-function cairo-lib "cairo_pattern_create_rgb"693
(list ffi/double ffi/double ffi/double) ffi/pointer))695
(define %pattern-create-rgba696
(c-function cairo-lib "cairo_pattern_create_rgba"697
(list ffi/double ffi/double ffi/double ffi/double) ffi/pointer))699
(define %pattern-create-linear700
(c-function cairo-lib "cairo_pattern_create_linear"701
(list ffi/double ffi/double ffi/double ffi/double) ffi/pointer))703
(define %pattern-create-radial704
(c-function cairo-lib "cairo_pattern_create_radial"705
(list ffi/double ffi/double ffi/double ffi/double ffi/double ffi/double) ffi/pointer))707
(define %pattern-add-color-stop-rgb708
(c-function cairo-lib "cairo_pattern_add_color_stop_rgb"709
(list ffi/pointer ffi/double ffi/double ffi/double ffi/double) ffi/void))711
(define %pattern-add-color-stop-rgba712
(c-function cairo-lib "cairo_pattern_add_color_stop_rgba"713
(list ffi/pointer ffi/double ffi/double ffi/double ffi/double ffi/double) ffi/void))715
(define %pattern-destroy716
(c-function cairo-lib "cairo_pattern_destroy"717
(list ffi/pointer) ffi/void))719
(define %pattern-set-extend720
(c-function cairo-lib "cairo_pattern_set_extend"721
(list ffi/pointer ffi/int) ffi/void))723
(define %pattern-create-for-surface724
(c-function cairo-lib "cairo_pattern_create_for_surface"725
(list ffi/pointer) ffi/pointer))727
;; ============================================================728
;; Public API — Surfaces729
;; ============================================================731
;;; Create an image surface with the given format and dimensions.732
(define (cairo-image-surface-create format width height)733
(: integer? integer? integer? -> pointer?)734
(let ((surface (%image-surface-create format width height)))735
(set-pointer-finalizer! surface %surface-destroy-ptr)736
surface))738
;;; Create an image surface from a PNG file.739
(define (cairo-image-surface-create-from-png filename)740
(: string? -> pointer?)741
(let ((surface (%image-surface-create-from-png filename)))742
(set-pointer-finalizer! surface %surface-destroy-ptr)743
surface))745
;;; Destroy a surface, freeing its resources.746
(define (cairo-surface-destroy surface)747
(: pointer? -> void?)748
(set-pointer-finalizer! surface #f)749
(%surface-destroy surface))751
;;; Write surface contents to a PNG file.752
(define (cairo-surface-write-to-png surface filename)753
(: pointer? string? -> integer?)754
(%surface-write-to-png surface filename))756
;;; Flush pending drawing operations to the surface.757
(define (cairo-surface-flush surface)758
(: pointer? -> void?)759
(%surface-flush surface))761
;;; Finish the surface, preventing further drawing.762
(define (cairo-surface-finish surface)763
(: pointer? -> void?)764
(%surface-finish surface))766
;;; Get the status of a surface.767
(define (cairo-surface-status surface)768
(: pointer? -> integer?)769
(%surface-status surface))771
;;; Create a new surface similar to an existing one.772
(define (cairo-surface-create-similar surface content width height)773
(: pointer? integer? integer? integer? -> pointer?)774
(let ((s (%surface-create-similar surface content width height)))775
(set-pointer-finalizer! s %surface-destroy-ptr)776
s))778
;;; Get the width of an image surface in pixels.779
(define (cairo-image-surface-get-width surface)780
(: pointer? -> integer?)781
(%image-surface-get-width surface))783
;;; Get the height of an image surface in pixels.784
(define (cairo-image-surface-get-height surface)785
(: pointer? -> integer?)786
(%image-surface-get-height surface))788
;;; Get the stride of an image surface in bytes.789
(define (cairo-image-surface-get-stride surface)790
(: pointer? -> integer?)791
(%image-surface-get-stride surface))793
;;; Get the pixel format of an image surface.794
(define (cairo-image-surface-get-format surface)795
(: pointer? -> integer?)796
(%image-surface-get-format surface))798
;;; Get a pointer to the raw pixel data of an image surface.799
(define (cairo-image-surface-get-data surface)800
(: pointer? -> pointer?)801
(%image-surface-get-data surface))803
;;; Create a PDF surface that writes to a file.804
(define (cairo-pdf-surface-create filename width-pts height-pts)805
(: string? number? number? -> pointer?)806
(let ((surface (%pdf-surface-create filename width-pts height-pts)))807
(set-pointer-finalizer! surface %surface-destroy-ptr)808
surface))810
;;; Create an SVG surface that writes to a file.811
(define (cairo-svg-surface-create filename width-pts height-pts)812
(: string? number? number? -> pointer?)813
(let ((surface (%svg-surface-create filename width-pts height-pts)))814
(set-pointer-finalizer! surface %surface-destroy-ptr)815
surface))817
;;; Set the size of a PDF surface page in points.818
(define (cairo-pdf-surface-set-size surface width-pts height-pts)819
(: pointer? number? number? -> void?)820
(%pdf-surface-set-size surface width-pts height-pts))822
;; ============================================================823
;; Public API — Context824
;; ============================================================826
;;; Create a new drawing context for a surface.827
(define (cairo-create surface)828
(: pointer? -> pointer?)829
(let ((cr (%cairo-create surface)))830
(set-pointer-finalizer! cr %cairo-destroy-ptr)831
cr))833
;;; Destroy a drawing context.834
(define (cairo-destroy cr)835
(: pointer? -> void?)836
(set-pointer-finalizer! cr #f)837
(%cairo-destroy cr))839
;;; Save the current drawing state.840
(define (cairo-save cr)841
(: pointer? -> void?)842
(%cairo-save cr))844
;;; Restore a previously saved drawing state.845
(define (cairo-restore cr)846
(: pointer? -> void?)847
(%cairo-restore cr))849
;;; Get the status of a drawing context.850
(define (cairo-status cr)851
(: pointer? -> integer?)852
(%cairo-status cr))854
;;; Convert a status code to a human-readable string.855
(define (cairo-status-to-string status)856
(: integer? -> string?)857
(%cairo-status-to-string status))859
;;; Push a temporary group for compositing.860
(define (cairo-push-group cr)861
(: pointer? -> void?)862
(%cairo-push-group cr))864
;;; Pop a group and return as a pattern.865
(define (cairo-pop-group cr)866
(: pointer? -> pointer?)867
(let ((pat (%cairo-pop-group cr)))868
(set-pointer-finalizer! pat %pattern-destroy-ptr)869
pat))871
;;; Pop a group and set it as the source.872
(define (cairo-pop-group-to-source cr)873
(: pointer? -> void?)874
(%cairo-pop-group-to-source cr))876
;;; Get the target surface for a context.877
(define (cairo-get-target cr)878
(: pointer? -> pointer?)879
(%cairo-get-target cr))881
;; ============================================================882
;; Public API — Drawing State883
;; ============================================================885
;;; Set the source color as RGB (each component 0.0 to 1.0).886
(define (cairo-set-source-rgb cr r g b)887
(: pointer? number? number? number? -> void?)888
(%set-source-rgb cr r g b))890
;;; Set the source color as RGBA (each component 0.0 to 1.0).891
(define (cairo-set-source-rgba cr r g b a)892
(: pointer? number? number? number? number? -> void?)893
(%set-source-rgba cr r g b a))895
;;; Set a surface as the source pattern with origin offset.896
(define (cairo-set-source-surface cr surface x y)897
(: pointer? pointer? number? number? -> void?)898
(%set-source-surface cr surface x y))900
;;; Set a pattern as the source.901
(define (cairo-set-source cr pattern)902
(: pointer? pointer? -> void?)903
(%set-source cr pattern))905
;;; Set the line width for stroking.906
(define (cairo-set-line-width cr width)907
(: pointer? number? -> void?)908
(%set-line-width cr width))910
;;; Get the current line width.911
(define (cairo-get-line-width cr)912
(: pointer? -> number?)913
(%get-line-width cr))915
;;; Set the line cap style.916
(define (cairo-set-line-cap cr cap)917
(: pointer? integer? -> void?)918
(%set-line-cap cr cap))920
;;; Set the line join style.921
(define (cairo-set-line-join cr join)922
(: pointer? integer? -> void?)923
(%set-line-join cr join))925
;;; Set the miter limit for line joins.926
(define (cairo-set-miter-limit cr limit)927
(: pointer? number? -> void?)928
(%set-miter-limit cr limit))930
;;; Set the dash pattern from a list of doubles.931
(define (cairo-set-dash cr dashes offset)932
(: pointer? list? number? -> void?)933
(let* ((n (length dashes))934
(buf (c-alloc (* n 8))))935
(let loop ((i 0) (ds dashes))936
(when (pair? ds)937
(pointer-set! buf ffi/double (* i 8) (car ds))938
(loop (+ i 1) (cdr ds))))939
(%set-dash cr buf n offset)940
(c-free buf)))942
;;; Set the fill rule.943
(define (cairo-set-fill-rule cr rule)944
(: pointer? integer? -> void?)945
(%set-fill-rule cr rule))947
;;; Set the compositing operator.948
(define (cairo-set-operator cr op)949
(: pointer? integer? -> void?)950
(%set-operator cr op))952
;;; Set the antialiasing mode.953
(define (cairo-set-antialias cr antialias)954
(: pointer? integer? -> void?)955
(%set-antialias cr antialias))957
;; ============================================================958
;; Public API — Paths959
;; ============================================================961
;;; Clear the current path.962
(define (cairo-new-path cr)963
(: pointer? -> void?)964
(%new-path cr))966
;;; Start a new sub-path.967
(define (cairo-new-sub-path cr)968
(: pointer? -> void?)969
(%new-sub-path cr))971
;;; Close the current sub-path.972
(define (cairo-close-path cr)973
(: pointer? -> void?)974
(%close-path cr))976
;;; Move to a point without drawing.977
(define (cairo-move-to cr x y)978
(: pointer? number? number? -> void?)979
(%move-to cr x y))981
;;; Draw a line from the current point.982
(define (cairo-line-to cr x y)983
(: pointer? number? number? -> void?)984
(%line-to cr x y))986
;;; Draw a cubic Bezier curve.987
(define (cairo-curve-to cr x1 y1 x2 y2 x3 y3)988
(: pointer? number? number? number? number? number? number? -> void?)989
(%curve-to cr x1 y1 x2 y2 x3 y3))991
;;; Draw a circular arc.992
(define (cairo-arc cr xc yc radius angle1 angle2)993
(: pointer? number? number? number? number? number? -> void?)994
(%arc cr xc yc radius angle1 angle2))996
;;; Draw a circular arc in the negative direction.997
(define (cairo-arc-negative cr xc yc radius angle1 angle2)998
(: pointer? number? number? number? number? number? -> void?)999
(%arc-negative cr xc yc radius angle1 angle2))1001
;;; Add a rectangle sub-path.1002
(define (cairo-rectangle cr x y width height)1003
(: pointer? number? number? number? number? -> void?)1004
(%rectangle cr x y width height))1006
;;; Move by a relative offset.1007
(define (cairo-rel-move-to cr dx dy)1008
(: pointer? number? number? -> void?)1009
(%rel-move-to cr dx dy))1011
;;; Draw a line by a relative offset.1012
(define (cairo-rel-line-to cr dx dy)1013
(: pointer? number? number? -> void?)1014
(%rel-line-to cr dx dy))1016
;;; Draw a curve by relative offsets.1017
(define (cairo-rel-curve-to cr dx1 dy1 dx2 dy2 dx3 dy3)1018
(: pointer? number? number? number? number? number? number? -> void?)1019
(%rel-curve-to cr dx1 dy1 dx2 dy2 dx3 dy3))1021
;; ============================================================1022
;; Public API — Drawing Operations1023
;; ============================================================1025
;;; Stroke the current path.1026
(define (cairo-stroke cr)1027
(: pointer? -> void?)1028
(%stroke cr))1030
;;; Fill the current path.1031
(define (cairo-fill cr)1032
(: pointer? -> void?)1033
(%fill cr))1035
;;; Paint the entire surface with the source.1036
(define (cairo-paint cr)1037
(: pointer? -> void?)1038
(%paint cr))1040
;;; Stroke the current path, preserving it.1041
(define (cairo-stroke-preserve cr)1042
(: pointer? -> void?)1043
(%stroke-preserve cr))1045
;;; Fill the current path, preserving it.1046
(define (cairo-fill-preserve cr)1047
(: pointer? -> void?)1048
(%fill-preserve cr))1050
;;; Paint with a given alpha transparency.1051
(define (cairo-paint-with-alpha cr alpha)1052
(: pointer? number? -> void?)1053
(%paint-with-alpha cr alpha))1055
;;; Set the clip region to the current path.1056
(define (cairo-clip cr)1057
(: pointer? -> void?)1058
(%clip cr))1060
;;; Set the clip region, preserving the path.1061
(define (cairo-clip-preserve cr)1062
(: pointer? -> void?)1063
(%clip-preserve cr))1065
;;; Reset the clip region to the entire surface.1066
(define (cairo-reset-clip cr)1067
(: pointer? -> void?)1068
(%reset-clip cr))1070
;;; Mask with a pattern (paint where pattern has alpha).1071
(define (cairo-mask cr pattern)1072
(: pointer? pointer? -> void?)1073
(%mask cr pattern))1075
;;; Mask with a surface at the given offset.1076
(define (cairo-mask-surface cr surface x y)1077
(: pointer? pointer? number? number? -> void?)1078
(%mask-surface cr surface x y))1080
;; ============================================================1081
;; Public API — Text1082
;; ============================================================1084
;;; Select a font by family, slant, and weight.1085
(define (cairo-select-font-face cr family slant weight)1086
(: pointer? string? integer? integer? -> void?)1087
(%select-font-face cr family slant weight))1089
;;; Set the font size.1090
(define (cairo-set-font-size cr size)1091
(: pointer? number? -> void?)1092
(%set-font-size cr size))1094
;;; Show text at the current position.1095
(define (cairo-show-text cr text)1096
(: pointer? string? -> void?)1097
(%show-text cr text))1099
;;; Get text extents as a dict with keys: x-bearing, y-bearing, width, height, x-advance, y-advance.1100
(define (cairo-text-extents cr text)1101
(: pointer? string? -> any?)1102
(with-c-alloc cairo-text-extents-layout1103
(lambda (extents)1104
(%text-extents cr text extents)1105
(c-struct->dict extents cairo-text-extents-layout))))1107
;;; Get font extents as a dict with keys: ascent, descent, height, max-x-advance, max-y-advance.1108
(define (cairo-font-extents cr)1109
(: pointer? -> any?)1110
(with-c-alloc cairo-font-extents-layout1111
(lambda (extents)1112
(%font-extents cr extents)1113
(c-struct->dict extents cairo-font-extents-layout))))1115
;; ============================================================1116
;; Public API — Glyph Rendering1117
;; ============================================================1119
;;; Render glyphs at specified positions on a Cairo context.1120
;;;1121
;;; GLYPHS is a list of dicts with keys: index: (glyph index),1122
;;; x: (x position), y: (y position).1123
(define (cairo-show-glyphs cr glyphs)1124
(: pointer? list? -> void?)1125
(let* ((n (length glyphs))1126
(glyph-size (c-struct-size cairo-glyph-layout))1127
(buf (c-alloc (* n glyph-size))))1128
(let loop ((i 0) (gs glyphs))1129
(when (pair? gs)1130
(let ((g (car gs))1131
(ptr (pointer+ buf (* i glyph-size))))1132
(c-struct-set! ptr cairo-glyph-layout index: (dict-ref g index:))1133
(c-struct-set! ptr cairo-glyph-layout x: (dict-ref g x:))1134
(c-struct-set! ptr cairo-glyph-layout y: (dict-ref g y:))1135
(loop (+ i 1) (cdr gs)))))1136
(%show-glyphs cr buf n)1137
(c-free buf)))1139
;;; Add glyphs to the current path without rendering.1140
;;;1141
;;; GLYPHS is a list of dicts with keys: index: (glyph index),1142
;;; x: (x position), y: (y position).1143
(define (cairo-glyph-path cr glyphs)1144
(: pointer? list? -> void?)1145
(let* ((gp (c-function cairo-lib "cairo_glyph_path"1146
(list ffi/pointer ffi/pointer ffi/int) ffi/void))1147
(n (length glyphs))1148
(glyph-size (c-struct-size cairo-glyph-layout))1149
(buf (c-alloc (* n glyph-size))))1150
(let loop ((i 0) (gs glyphs))1151
(when (pair? gs)1152
(let ((g (car gs))1153
(ptr (pointer+ buf (* i glyph-size))))1154
(c-struct-set! ptr cairo-glyph-layout index: (dict-ref g index:))1155
(c-struct-set! ptr cairo-glyph-layout x: (dict-ref g x:))1156
(c-struct-set! ptr cairo-glyph-layout y: (dict-ref g y:))1157
(loop (+ i 1) (cdr gs)))))1158
(gp cr buf n)1159
(c-free buf)))1161
;;; Create a Cairo font face from a FreeType face.1162
;;;1163
;;; Requires Cairo to be built with FreeType support.1164
(define (cairo-ft-font-face-create ft-face load-flags)1165
(: pointer? integer? -> pointer?)1166
(let ((face (%ft-font-face-create ft-face load-flags)))1167
(set-pointer-finalizer! face %font-face-destroy-ptr)1168
face))1170
;;; Set the font face for a Cairo context.1171
(define (cairo-set-font-face cr face)1172
(: pointer? pointer? -> void?)1173
(%set-font-face cr face))1175
;;; Destroy a Cairo font face.1176
(define (cairo-font-face-destroy face)1177
(: pointer? -> void?)1178
(set-pointer-finalizer! face #f)1179
(%font-face-destroy face))1181
;; ============================================================1182
;; Public API — Transforms1183
;; ============================================================1185
;;; Translate the coordinate origin.1186
(define (cairo-translate cr tx ty)1187
(: pointer? number? number? -> void?)1188
(%translate cr tx ty))1190
;;; Scale the coordinate system.1191
(define (cairo-scale cr sx sy)1192
(: pointer? number? number? -> void?)1193
(%scale cr sx sy))1195
;;; Rotate the coordinate system by angle in radians.1196
(define (cairo-rotate cr angle)1197
(: pointer? number? -> void?)1198
(%rotate cr angle))1200
;;; Reset the transformation matrix to identity.1201
(define (cairo-identity-matrix cr)1202
(: pointer? -> void?)1203
(%identity-matrix cr))1205
;;; Get the current transformation matrix.1206
;;; Returns a dict with xx: yx: xy: yy: x0: y0: fields.1207
(define (cairo-get-matrix cr)1208
(: pointer? -> dict?)1209
(let* ((matrix-size (c-struct-size cairo-matrix-layout))1210
(buf (c-alloc matrix-size)))1211
(%get-matrix cr buf)1212
(let ((result (dict xx: (c-struct-ref buf cairo-matrix-layout xx:)1213
yx: (c-struct-ref buf cairo-matrix-layout yx:)1214
xy: (c-struct-ref buf cairo-matrix-layout xy:)1215
yy: (c-struct-ref buf cairo-matrix-layout yy:)1216
x0: (c-struct-ref buf cairo-matrix-layout x0:)1217
y0: (c-struct-ref buf cairo-matrix-layout y0:))))1218
(c-free buf)1219
result)))1221
;;; Set the transformation matrix from a dict with xx: yx: xy: yy: x0: y0:.1222
(define (cairo-set-matrix cr matrix)1223
(: pointer? dict? -> void?)1224
(let* ((matrix-size (c-struct-size cairo-matrix-layout))1225
(buf (c-alloc matrix-size)))1226
(c-struct-set! buf cairo-matrix-layout xx: (dict-ref matrix xx:))1227
(c-struct-set! buf cairo-matrix-layout yx: (dict-ref matrix yx:))1228
(c-struct-set! buf cairo-matrix-layout xy: (dict-ref matrix xy:))1229
(c-struct-set! buf cairo-matrix-layout yy: (dict-ref matrix yy:))1230
(c-struct-set! buf cairo-matrix-layout x0: (dict-ref matrix x0:))1231
(c-struct-set! buf cairo-matrix-layout y0: (dict-ref matrix y0:))1232
(%set-matrix cr buf)1233
(c-free buf)))1235
;; ============================================================1236
;; Public API — Patterns1237
;; ============================================================1239
;;; Create a solid RGB pattern.1240
(define (cairo-pattern-create-rgb r g b)1241
(: number? number? number? -> pointer?)1242
(let ((pat (%pattern-create-rgb r g b)))1243
(set-pointer-finalizer! pat %pattern-destroy-ptr)1244
pat))1246
;;; Create a solid RGBA pattern.1247
(define (cairo-pattern-create-rgba r g b a)1248
(: number? number? number? number? -> pointer?)1249
(let ((pat (%pattern-create-rgba r g b a)))1250
(set-pointer-finalizer! pat %pattern-destroy-ptr)1251
pat))1253
;;; Create a linear gradient pattern between two points.1254
(define (cairo-pattern-create-linear x0 y0 x1 y1)1255
(: number? number? number? number? -> pointer?)1256
(let ((pat (%pattern-create-linear x0 y0 x1 y1)))1257
(set-pointer-finalizer! pat %pattern-destroy-ptr)1258
pat))1260
;;; Create a radial gradient pattern between two circles.1261
(define (cairo-pattern-create-radial cx0 cy0 r0 cx1 cy1 r1)1262
(: number? number? number? number? number? number? -> pointer?)1263
(let ((pat (%pattern-create-radial cx0 cy0 r0 cx1 cy1 r1)))1264
(set-pointer-finalizer! pat %pattern-destroy-ptr)1265
pat))1267
;;; Add an RGB color stop to a gradient pattern.1268
(define (cairo-pattern-add-color-stop-rgb pattern offset r g b)1269
(: pointer? number? number? number? number? -> void?)1270
(%pattern-add-color-stop-rgb pattern offset r g b))1272
;;; Add an RGBA color stop to a gradient pattern.1273
(define (cairo-pattern-add-color-stop-rgba pattern offset r g b a)1274
(: pointer? number? number? number? number? number? -> void?)1275
(%pattern-add-color-stop-rgba pattern offset r g b a))1277
;;; Destroy a pattern.1278
(define (cairo-pattern-destroy pattern)1279
(: pointer? -> void?)1280
(set-pointer-finalizer! pattern #f)1281
(%pattern-destroy pattern))1283
;;; Set the extend mode for a pattern.1284
(define (cairo-pattern-set-extend pattern extend)1285
(: pointer? integer? -> void?)1286
(%pattern-set-extend pattern extend))1288
;;; Create a pattern from a surface.1289
(define (cairo-pattern-create-for-surface surface)1290
(: pointer? -> pointer?)1291
(let ((pat (%pattern-create-for-surface surface)))1292
(set-pointer-finalizer! pat %pattern-destroy-ptr)1293
pat))1295
;; ============================================================1296
;; Convenience1297
;; ============================================================1299
;;; Create a surface, call proc with it, then destroy it.1300
(define (with-cairo-surface surface proc)1301
(: pointer? procedure? -> any?)1302
(let ((result (proc surface)))1303
(cairo-surface-destroy surface)1304
result))1306
;;; Create a context for a surface, call proc with it, then destroy it.1307
(define (with-cairo-context surface proc)1308
(: pointer? procedure? -> any?)1309
(let* ((cr (cairo-create surface))1310
(result (proc cr)))1311
(cairo-destroy cr)1312
result))))