-
Notifications
You must be signed in to change notification settings - Fork 38
/
Canvas.cpp
2521 lines (2174 loc) · 113 KB
/
Canvas.cpp
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "common.h"
#include <include/svg/SkSVGCanvas.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
void initCanvas(py::module &m) {
py::class_<SkAutoCanvasRestore>(m, "AutoCanvasRestore", R"docstring(
Stack helper class calls :py:meth:`Canvas.restoreToCount` when
:py:class:`AutoCanvasRestore` goes out of scope.
Use this to guarantee that the canvas is restored to a known state.
Example::
with skia.AutoCanvasRestore(canvas):
canvas.drawCircle(50., 50., 10., paint)
)docstring")
.def(py::init<SkCanvas*, bool>(),
R"docstring(
Preserves :py:meth:`Canvas.save` count.
Optionally saves :py:class:`Canvas` clip and :py:class:`Canvas` matrix.
:param skia.Canvas canvas: :py:class:`Canvas` to guard
:param bool doSave: call :py:meth:`Canvas.save`
:return: utility to restore :py:class:`Canvas` state on destructor
)docstring",
py::arg("canvas"), py::arg("doSave") = true,
py::keep_alive<0, 1>())
.def("restore", &SkAutoCanvasRestore::restore,
R"docstring(
Restores :py:class:`Canvas` to saved state immediately.
Subsequent calls and destructor have no effect.
)docstring")
.def("__enter__", [] (SkAutoCanvasRestore& self) {})
.def("__exit__",
[] (SkAutoCanvasRestore& self, py::args args) { self.restore(); })
;
py::enum_<SkClipOp>(m, "ClipOp")
.value("kDifference", SkClipOp::kDifference)
.value("kIntersect", SkClipOp::kIntersect)
.export_values();
py::class_<SkCanvas> canvas(m, "Canvas", R"docstring(
:py:class:`Canvas` provides an interface for drawing, and how the drawing is
clipped and transformed.
:py:class:`Canvas` contains a stack of :py:class:`Matrix` and clip values.
:py:class:`Canvas` and :py:class:`Paint` together provide the state to draw
into :py:class:`Surface` or :py:class:`BaseDevice`. Each :py:class:`Canvas`
draw call transforms the geometry of the object by the concatenation of all
:py:class:`Matrix` values in the stack. The transformed geometry is clipped
by the intersection of all of clip values in the stack. The
:py:class:`Canvas` draw calls use :py:class:`Paint` to supply drawing state
such as color, :py:class:`Typeface`, text size, stroke width,
:py:class:`Shader` and so on.
To draw to a pixel-based destination, create raster surface or GPU surface.
Request :py:class:`Canvas` from :py:class:`Surface` to obtain the interface
to draw. :py:class:`Canvas` generated by raster surface draws to memory
visible to the CPU. :py:class:`Canvas` generated by GPU surface uses Vulkan
or OpenGL to draw to the GPU.
To draw to a document, obtain :py:class:`Canvas` from SVG canvas, document
PDF, or :py:class:`PictureRecorder`. :py:class:`Document` based
:py:class:`Canvas` and other :py:class:`Canvas` subclasses reference
:py:class:`BaseDevice` describing the destination.
:py:class:`Canvas` can be constructed to draw to :py:class:`Bitmap` without
first creating raster surface. This approach may be deprecated in the
future.
)docstring");
py::enum_<SkCanvas::SrcRectConstraint>(canvas, "SrcRectConstraint")
.value("kStrict_SrcRectConstraint",
SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint,
R"docstring(
sample only inside bounds; slower
)docstring")
.value("kFast_SrcRectConstraint",
SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint,
R"docstring(
sample outside bounds; faster
)docstring")
.export_values();
py::enum_<SkCanvas::PointMode>(canvas, "PointMode")
.value("kPoints_PointMode", SkCanvas::PointMode::kPoints_PointMode,
R"docstring(
draw each point separately
)docstring")
.value("kLines_PointMode", SkCanvas::PointMode::kLines_PointMode,
R"docstring(
draw each pair of points as a line segment
)docstring")
.value("kPolygon_PointMode", SkCanvas::PointMode::kPolygon_PointMode,
R"docstring(
draw the array of points as a open polygon
)docstring")
.export_values();
py::enum_<SkCanvas::QuadAAFlags>(canvas, "QuadAAFlags")
.value("kLeft_QuadAAFlag", SkCanvas::QuadAAFlags::kLeft_QuadAAFlag)
.value("kTop_QuadAAFlag", SkCanvas::QuadAAFlags::kTop_QuadAAFlag)
.value("kRight_QuadAAFlag", SkCanvas::QuadAAFlags::kRight_QuadAAFlag)
.value("kBottom_QuadAAFlag", SkCanvas::QuadAAFlags::kBottom_QuadAAFlag)
.value("kNone_QuadAAFlags", SkCanvas::QuadAAFlags::kNone_QuadAAFlags)
.value("kAll_QuadAAFlags", SkCanvas::QuadAAFlags::kAll_QuadAAFlags)
.export_values();
py::enum_<SkCanvas::SaveLayerFlagsSet>(
canvas, "SaveLayerFlags", py::arithmetic())
.value("kPreserveLCDText_SaveLayerFlag",
SkCanvas::SaveLayerFlagsSet::kPreserveLCDText_SaveLayerFlag)
.value("kInitWithPrevious_SaveLayerFlag",
SkCanvas::SaveLayerFlagsSet::kInitWithPrevious_SaveLayerFlag,
"initializes with previous contents")
.value("kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag",
SkCanvas::SaveLayerFlagsSet::
kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag,
"experimental: do not use")
.value("kF16ColorType",
SkCanvas::SaveLayerFlagsSet::kF16ColorType)
.export_values();
py::class_<SkCanvas::SaveLayerRec>(canvas, "SaveLayerRec",
R"docstring(
SaveLayerRec contains the state used to create the layer.
)docstring")
.def(py::init<>(),
R"docstring(
Sets :py:attr:`fBounds`, :py:attr:`fPaint`, and :py:attr:`fBackdrop` to
nullptr.
Clears :py:attr:`fSaveLayerFlags`.
)docstring")
.def(py::init<const SkRect*, const SkPaint*, SkCanvas::SaveLayerFlags>(),
R"docstring(
Sets :py:attr:`fBounds`, :py:attr:`fPaint`, and
:py:attr:`fSaveLayerFlags`; sets :py:attr:`fBackdrop` to nullptr.
:bounds: layer dimensions; may be nullptr
:paint: applied to layer when overlaying prior layer;
may be nullptr
:saveLayerFlags: SaveLayerRec options to
modify layer
)docstring",
py::arg("bounds"), py::arg("paint"), py::arg("saveLayerFlags") = 0)
.def(py::init<const SkRect*, const SkPaint*, const SkImageFilter*,
SkCanvas::SaveLayerFlags>(),
R"docstring(
Sets :py:attr:`fBounds`, :py:attr:`fPaint`, :py:attr:`fBackdrop`, and
:py:attr:`fSaveLayerFlags`.
:bounds: layer dimensions; may be nullptr
:paint: applied to layer when overlaying prior layer;
may be nullptr
:backdrop: If not null, this causes the current
layer to be filtered by backdrop, and then drawn into the new layer
(respecting the current clip). If null, the new layer is initialized
with transparent-black.
:saveLayerFlags: SaveLayerRec options to
modify layer
)docstring",
py::arg("bounds"), py::arg("paint"), py::arg("backdrop"),
py::arg("saveLayerFlags"))
.def_readwrite("fBounds", &SkCanvas::SaveLayerRec::fBounds,
R"docstring(
hints at layer size limit
)docstring")
.def_readwrite("fPaint", &SkCanvas::SaveLayerRec::fPaint,
R"docstring(
modifies overlay
)docstring")
.def_readwrite("fBackdrop", &SkCanvas::SaveLayerRec::fBackdrop,
R"docstring(
If not null, this triggers the same initialization behavior as setting
:py:attr:`Canvas.SaveLayerFlags.kInitWithPrevious` on
:py:attr:`fSaveLayerFlags`: the current layer is copied into the new
layer, rather than initializing the new layer with transparent-black.
This is then filtered by fBackdrop (respecting the current clip).
)docstring")
.def_readwrite("fSaveLayerFlags", &SkCanvas::SaveLayerRec::fSaveLayerFlags,
R"docstring(
preserves LCD text, creates with prior layer contents
)docstring")
;
py::class_<SkCanvas::Lattice> lattice(canvas, "Lattice", R"docstring(
:py:class:`Canvas.Lattice` divides :py:class:`Bitmap` or :py:class:`Image`
into a rectangular grid.
Grid entries on even columns and even rows are fixed; these entries are
always drawn at their original size if the destination is large enough. If
the destination side is too small to hold the fixed entries, all fixed
entries are proportionately scaled down to fit. The grid entries not on even
columns and rows are scaled to fit the remaining space, if any.
)docstring");
py::enum_<SkCanvas::Lattice::RectType>(lattice, "RectType")
.value("kDefault", SkCanvas::Lattice::RectType::kDefault,
R"docstring(
draws :py:class:`Bitmap` into lattice rectangle
)docstring")
.value("kTransparent", SkCanvas::Lattice::RectType::kTransparent,
R"docstring(
skips lattice rectangle by making it transparent
)docstring")
.value("kFixedColor", SkCanvas::Lattice::RectType::kFixedColor,
R"docstring(
draws one of fColors into lattice rectangle
)docstring")
.export_values();
lattice
// .def(py::init([] (std::, py::list, SkCanvas::Lattice::RectType rectType,
// const SkIRect *bounds, const SkColor* colors) {
// return SkCanvas::Lattice lattice;
// }))
.def_readwrite("fXDivs", &SkCanvas::Lattice::fXDivs,
R"docstring(
x-axis values dividing bitmap
)docstring")
.def_readwrite("fYDivs", &SkCanvas::Lattice::fYDivs,
R"docstring(
y-axis values dividing bitmap
)docstring")
.def_readwrite("fRectTypes", &SkCanvas::Lattice::fRectTypes,
R"docstring(
array of fill types
)docstring")
.def_readwrite("fXCount", &SkCanvas::Lattice::fXCount,
R"docstring(
number of x-coordinates
)docstring")
.def_readwrite("fYCount", &SkCanvas::Lattice::fYCount,
R"docstring(
number of y-coordinates
)docstring")
.def_readwrite("fBounds", &SkCanvas::Lattice::fBounds,
R"docstring(
source bounds to draw from
)docstring")
.def_readwrite("fColors", &SkCanvas::Lattice::fColors,
R"docstring(
array of colors
)docstring")
;
canvas
.def("__repr__",
[] (const SkCanvas& canvas) {
auto size = canvas.getBaseLayerSize();
return py::str("Canvas({}, {}, saveCount={})").format(
size.width(), size.height(), canvas.getSaveCount());
})
.def(py::init<>(),
R"docstring(
Creates an empty :py:class:`Canvas` with no backing device or pixels,
with a width and height of zero.
)docstring")
.def(py::init(
[] (py::array array, SkColorType ct, SkAlphaType at,
const SkColorSpace* cs, const SkSurfaceProps *surfaceProps) {
auto imageInfo = NumPyToImageInfo(array, ct, at, cs);
auto canvas = SkCanvas::MakeRasterDirect(
imageInfo, array.mutable_data(), array.strides(0),
surfaceProps);
if (!canvas)
throw std::runtime_error("Failed to create Canvas");
return canvas;
}),
R"docstring(
Creates raster :py:class:`Canvas` backed by numpy array.
Subsequent :py:class:`Canvas` calls draw into pixels. To access pixels
after drawing, call flush() or peekPixels().
:array: numpy ndarray of shape=(height, width, channels) and appropriate
dtype. Must have non-zero width and height, and the valid number of
channels for the specified color type.
:colorType: color type of the array
:alphaType: alpha type of the array
:colorSpace: range of colors; may be nullptr
)docstring",
py::arg("array"), py::arg("colorType") = kN32_SkColorType,
py::arg("alphaType") = kUnpremul_SkAlphaType,
py::arg("colorSpace") = nullptr, py::arg("surfaceProps") = nullptr)
.def(py::init<int, int, const SkSurfaceProps*>(),
R"docstring(
Creates :py:class:`Canvas` of the specified dimensions without a
:py:class:`Surface`.
Used by subclasses with custom implementations for draw member
functions.
If props equals `None`, :py:class:`SurfaceProps` are created with
:py:class:`SurfaceProps.InitType` settings, which choose the pixel
striping direction and order. Since a platform may dynamically change
its direction when the device is rotated, and since a platform may have
multiple monitors with different characteristics, it is best not to rely
on this legacy behavior.
:width: zero or greater
:height: zero or greater
:props: LCD striping orientation and setting for device independent
fonts; may be `None`
)docstring",
py::arg("width"), py::arg("height"), py::arg("props") = nullptr)
.def(py::init<const SkBitmap&>(),
R"docstring(
Constructs a canvas that draws into bitmap.
Sets :py:attr:`SurfaceProps.kLegacyFontHost` in constructed
:py:class:`Surface`.
:py:class:`Bitmap` is copied so that subsequently editing bitmap will
not affect constructed :py:class:`Canvas`.
May be deprecated in the future.
:bitmap: width, height, :py:class:`ColorType`, :py:class:`AlphaType`,
and pixel storage of raster surface
)docstring",
py::arg("bitmap"))
.def(py::init<const SkBitmap&, const SkSurfaceProps&>(),
R"docstring(
Constructs a canvas that draws into bitmap.
Use props to match the device characteristics, like LCD striping.
bitmap is copied so that subsequently editing bitmap will not affect
constructed :py:class:`Canvas`.
:bitmap: width, height, :py:class:`ColorType`, :py:class:`AlphaType`,
and pixel storage of raster surface
:props: order and orientation of RGB striping; and whether to use device
independent fonts
)docstring",
py::arg("bitmap"), py::arg("props"))
.def("toarray", &ReadToNumpy<SkCanvas>,
R"docstring(
Exports a ``numpy.ndarray``.
:param srcX: offset into readable pixels on x-axis; may be negative
:param srcY: offset into readable pixels on y-axis; may be negative
:param colorType: target :py:class:`ColorType`
:param alphaType: target :py:class:`AlphaType`
:param colorSpace: target :py:class:`ColorSpace`
:return: numpy.ndarray
)docstring",
py::arg("srcX") = 0, py::arg("srcY") = 0,
py::arg("colorType") = kUnknown_SkColorType,
py::arg("alphaType") = kUnpremul_SkAlphaType,
py::arg("colorSpace") = nullptr)
.def("imageInfo", &SkCanvas::imageInfo,
R"docstring(
Returns :py:class:`ImageInfo` for :py:class:`Canvas`.
If :py:class:`Canvas` is not associated with raster surface or GPU
surface, returned :py:class:`ColorType` is set to
:py:attr:`ColorType.kUnknown_ColorType`.
:return: dimensions and :py:class:`ColorType` of :py:class:`Canvas`
)docstring")
.def("getProps", &SkCanvas::getProps,
R"docstring(
Copies :py:class:`SurfaceProps`, if :py:class:`Canvas` is associated
with raster surface or GPU surface, and returns true.
Otherwise, returns false and leave props unchanged.
:param skia.SurfaceProps props: storage for writable SurfaceProps
:return: true if :py:class:`SurfaceProps` was copied
)docstring",
py::arg("props"))
.def("flush", &SkCanvas::flush,
R"docstring(
Triggers the immediate execution of all pending draw operations.
If :py:class:`Canvas` is associated with GPU surface, resolves all
pending GPU operations. If :py:class:`Canvas` is associated with raster
surface, has no effect; raster draw operations are never deferred.
)docstring")
.def("getBaseLayerSize", &SkCanvas::getBaseLayerSize,
R"docstring(
Gets the size of the base or root layer in global canvas coordinates.
The origin of the base layer is always (0,0). The area available for
drawing may be smaller (due to clipping or saveLayer).
:return: integral width and height of base layer
:rtype: skia.ISize
)docstring")
.def("makeSurface", &SkCanvas::makeSurface,
R"docstring(
Creates :py:class:`Surface` matching info and props, and associates it
with :py:class:`Canvas`.
Returns nullptr if no match found.
If props is nullptr, matches :py:class:`SurfaceProps` in
:py:class:`Canvas`. If props is nullptr and :py:class:`Canvas` does not
have :py:class:`SurfaceProps`, creates :py:class:`Surface` with default
:py:class:`SurfaceProps`.
:param skia.ImageInfo info: width, height, :py:class:`ColorType`,
:py:class:`AlphaType`, and :py:class:`ColorSpace`
:param skia.SurfaceProps props: :py:class:`SurfaceProps` to match; may
be nullptr to match :py:class:`Canvas`
:return: SkSurface matching info and props, or nullptr if no match is
available
:rtype: skia.Surface or None
)docstring",
py::arg("info"), py::arg("props") = nullptr)
.def("getSurface", &SkCanvas::getSurface,
R"docstring(
Sometimes a canvas is owned by a surface.
If it is, getSurface() will return a bare pointer to that surface, else
this will return nullptr.
:rtype: skia.Surface or None
)docstring",
py::return_value_policy::reference)
.def("accessTopLayerPixels",
[] (SkCanvas& canvas, SkIPoint* origin) -> py::object {
SkImageInfo info;
size_t rowBytes;
void* addr = canvas.accessTopLayerPixels(&info, &rowBytes, origin);
if (!addr)
return py::none();
py::ssize_t bytesPerPixel = info.bytesPerPixel();
const char* format =
(bytesPerPixel == 1) ?
py::format_descriptor<uint8_t>::value :
(bytesPerPixel == 2) ?
py::format_descriptor<uint16_t>::value :
(bytesPerPixel == 4) ?
py::format_descriptor<uint32_t>::value :
(bytesPerPixel == 8) ?
py::format_descriptor<uint64_t>::value :
py::format_descriptor<uint8_t>::value;
return py::memoryview::from_buffer(
addr,
bytesPerPixel,
format,
{ info.width(), info.height() },
{ py::ssize_t(rowBytes), bytesPerPixel },
true
);
},
R"docstring(
Returns the pixel base address, and origin if the pixels can be read
directly.
The returned address is only valid while :py:class:`Canvas` is in scope
and unchanged. Any :py:class:`Canvas` call or :py:class:`Surface` call
may invalidate the returned address and other returned values.
If pixels are inaccessible, returns None.
:param origin: storage for :py:class:`Canvas` top layer origin, its
top-left corner; may be nullptr
:return: address of pixels, or nullptr if inaccessible
)docstring",
py::arg("origin") = nullptr)
// .def("accessTopRasterHandle", &SkCanvas::accessTopRasterHandle,
// "Returns custom context that tracks the SkMatrix and clip.")
.def("peekPixels", &SkCanvas::peekPixels,
R"docstring(
Returns true if :py:class:`Canvas` has direct access to its pixels.
Pixels are readable when :py:class:`BaseDevice` is raster. Pixels are
not readable when :py:class:`Canvas` is returned from GPU surface,
returned by :py:class:`Document`::beginPage, returned by
:py:class:`PictureRecorder`::beginRecording, or :py:class:`Canvas` is
the base of a utility class like DebugCanvas.
pixmap is valid only while :py:class:`Canvas` is in scope and unchanged.
Any :py:class:`Canvas` or :py:class:`Surface` call may invalidate the
pixmap values.
:param skia.Pixmap pixmap: storage for pixel state if pixels are
readable; otherwise, ignored
:return: true if :py:class:`Canvas` has direct access to pixels
)docstring",
py::arg("pixmap"))
.def("readPixels", &ReadPixels<SkCanvas>,
R"docstring(
Copies :py:class:`Rect` of pixels from :py:class:`Canvas` into
dstPixels.
:py:class:`Matrix` and clip are ignored.
Source :py:class:`Rect` corners are (srcX, srcY) and (
imageInfo().width(), imageInfo().height()). Destination :py:class:`Rect`
corners are (0, 0) and (array.shape[1], array.shape[0]). Copies each
readable pixel intersecting both rectangles, without scaling, converting
to :py:attr:`ColorType.kN32_ColorType` and
:py:attr:`AlphaType.kPremul_AlphaType` if required.
Pixels are readable when :py:class:`BaseDevice` is raster, or backed by
a GPU. Pixels are not readable when :py:class:`Canvas` is returned by
:py:meth:`Document.beginPage`, returned by
:py:meth:`PictureRecorder.beginRecording`, or :py:class:`Canvas` is the
base of a utility class like DebugCanvas.
The destination pixel storage must be allocated by the caller.
Pixel values are converted only if :py:class:`ColorType` and
:py:class:`AlphaType` do not match. Only pixels within both source and
destination rectangles are copied. array contents outside
:py:class:`Rect` intersection are unchanged.
Pass negative values for srcX or srcY to offset pixels across or down
destination.
Does not copy, and returns false if:
- Source and destination rectangles do not intersect.
- :py:class:`Canvas` pixels could not be converted to
:py:attr:`ColorType.kN32_ColorType` or
:py:attr:`AlphaType.kPremul_AlphaType`.
- :py:class:`Canvas` pixels are not readable; for instance,
- :py:class:`Canvas` is document-based.
:dstInfo: width, height, :py:class:`ColorType`, and
:py:class:`AlphaType` of dstPixels
:dstPixels: storage for pixels; dstInfo.height() times dstRowBytes, or
larger
:dstRowBytes: size of one destination row; dstInfo.width() times pixel
size, or larger. Ignored when dstPixels has more than one-dimension.
:srcX: offset into readable pixels on x-axis; may be negative
:srcY: offset into readable pixels on y-axis; may be negative
:return: true if pixels were copied
)docstring",
py::arg("dstInfo"), py::arg("dstPixels"), py::arg("dstRowBytes") = 0,
py::arg("srcX") = 0, py::arg("srcY") = 0)
.def("readPixels",
py::overload_cast<const SkPixmap&, int, int>(&SkCanvas::readPixels),
R"docstring(
Copies :py:class:`Rect` of pixels from :py:class:`Canvas` into pixmap.
:py:class:`Matrix` and clip are ignored.
Source :py:class:`Rect` corners are (srcX, srcY) and (
imageInfo().width(), imageInfo().height()). Destination
:py:class:`Rect` corners are (0, 0) and (pixmap.width(),
pixmap.height()). Copies each readable pixel intersecting both
rectangles, without scaling, converting to pixmap.colorType() and
pixmap.alphaType() if required.
Pixels are readable when :py:class:`BaseDevice` is raster, or backed by
a GPU. Pixels are not readable when :py:class:`Canvas` is returned by
:py:meth:`Document.beginPage`, returned by
:py:meth:`PictureRecorder.beginRecording`, or :py:class:`Canvas` is the
base of a utility class like DebugCanvas.
Caller must allocate pixel storage in pixmap if needed.
Pixel values are converted only if :py:class:`ColorType` and
:py:class:`AlphaType` do not match. Only pixels within both source and
destination :py:class:`Rect` are copied. pixmap pixels contents outside
:py:class:`Rect` intersection are unchanged.
Pass negative values for srcX or srcY to offset pixels across or down
pixmap.
Does not copy, and returns false if:
- Source and destination rectangles do not intersect.
- :py:class:`Canvas` pixels could not be converted to
pixmap.colorType() or pixmap.alphaType().
- :py:class:`Canvas` pixels are not readable; for instance,
:py:class:`Canvas` is document-based.
- :py:class:`Pixmap` pixels could not be allocated.
- pixmap.rowBytes() is too small to contain one row of pixels.
:pixmap: storage for pixels copied from
:py:class:`Canvas`
:srcX: offset into readable pixels on x-axis; may be negative
:srcY: offset into readable pixels on y-axis; may be negative
:return: true if pixels were copied
)docstring",
py::arg("pixmap"), py::arg("srcX") = 0, py::arg("srcY") = 0)
.def("readPixels",
py::overload_cast<const SkBitmap&, int, int>(&SkCanvas::readPixels),
R"docstring(
Copies :py:class:`Rect` of pixels from :py:class:`Canvas` into bitmap.
:py:class:`Matrix` and clip are ignored.
Source :py:class:`Rect` corners are (srcX, srcY) and (
imageInfo().width(), imageInfo().height()). Destination
:py:class:`Rect` corners are (0, 0) and (bitmap.width(),
bitmap.height()). Copies each readable pixel intersecting both
rectangles, without scaling, converting to bitmap.colorType() and
bitmap.alphaType() if required.
Pixels are readable when :py:class:`BaseDevice` is raster, or backed by
a GPU. Pixels are not readable when :py:class:`Canvas` is returned by
:py:meth:`Document.beginPage`, returned by
:py:meth:`PictureRecorder.beginRecording`, or :py:class:`Canvas` is the
base of a utility class like DebugCanvas.
Caller must allocate pixel storage in bitmap if needed.
:py:class:`Bitmap` values are converted only if :py:class:`ColorType`
and :py:class:`AlphaType` do not match. Only pixels within both source
and destination rectangles are copied. :py:class:`Bitmap` pixels outside
:py:class:`Rect` intersection are unchanged.
Pass negative values for srcX or srcY to offset pixels across or down
bitmap.
Does not copy, and returns false if:
- Source and destination rectangles do not intersect.
- :py:class:`Canvas` pixels could not be converted to bitmap.colorType()
or bitmap.alphaType().
- :py:class:`Canvas` pixels are not readable; for instance,
:py:class:`Canvas` is document-based.
- bitmap pixels could not be allocated.
- bitmap.rowBytes() is too small to contain one row of pixels.
:bitmap: storage for pixels copied from
:py:class:`Canvas`
:srcX: offset into readable pixels on x-axis; may be negative
:srcY: offset into readable pixels on y-axis; may be negative
:return: true if pixels were copied
)docstring",
py::arg("bitmap"), py::arg("srcX") = 0, py::arg("srcY") = 0)
.def("writePixels",
[] (SkCanvas& canvas, const SkImageInfo& imageInfo, py::buffer pixels,
size_t srcRowBytes, int x, int y) {
auto info = pixels.request();
auto rowBytes = ValidateBufferToImageInfo(
imageInfo, info, srcRowBytes);
return canvas.writePixels(imageInfo, info.ptr, rowBytes, x, y);
},
R"docstring(
Copies :py:class:`Rect` from pixels to :py:class:`Canvas`.
:py:class:`Matrix` and clip are ignored. Source :py:class:`Rect` corners
are (0, 0) and (info.width(), info.height()). Destination
:py:class:`Rect` corners are (x, y) and (imageInfo().width(),
imageInfo().height()).
Copies each readable pixel intersecting both rectangles, without
scaling, converting to imageInfo().colorType() and
imageInfo().alphaType() if required.
Pixels are writable when :py:class:`BaseDevice` is raster, or backed by
a GPU. Pixels are not writable when :py:class:`Canvas` is returned by
:py:meth:`Document.beginPage`, returned by
:py:meth:`PictureRecorder.beginRecording`, or :py:class:`Canvas` is the
base of a utility class like DebugCanvas.
Pixel values are converted only if :py:class:`ColorType` and
:py:class:`AlphaType` do not match. Only pixels within both source and
destination rectangles are copied. :py:class:`Canvas` pixels outside
:py:class:`Rect` intersection are unchanged.
Pass negative values for x or y to offset pixels to the left or above
:py:class:`Canvas` pixels.
Does not copy, and returns false if:
- Source and destination rectangles do not intersect.
- pixels could not be converted to :py:class:`Canvas`
imageInfo().colorType() or imageInfo().alphaType().
- :py:class:`Canvas` pixels are not writable; for instance,
:py:class:`Canvas` is document-based.
- rowBytes is too small to contain one row of pixels.
:array: pixels to copy, in native 32-bit colors.
:x: offset into :py:class:`Canvas` writable pixels on x-axis; may be
negative
:y: offset into :py:class:`Canvas` writable pixels on y-axis; may be
negative
:return: true if pixels were written to :py:class:`Canvas`
)docstring",
py::arg("info"), py::arg("pixels"), py::arg("rowBytes") = 0,
py::arg("x") = 0, py::arg("y") = 0)
.def("writePixels",
py::overload_cast<const SkBitmap&, int, int>(&SkCanvas::writePixels),
R"docstring(
Copies :py:class:`Rect` from pixels to :py:class:`Canvas`.
:py:class:`Matrix` and clip are ignored. Source :py:class:`Rect` corners
are (0, 0) and (bitmap.width(), bitmap.height()).
Destination :py:class:`Rect` corners are (x, y) and
(imageInfo().width(), imageInfo().height()).
Copies each readable pixel intersecting both rectangles, without
scaling, converting to imageInfo().colorType() and
imageInfo().alphaType() if required.
Pixels are writable when :py:class:`BaseDevice` is raster, or backed by
a GPU. Pixels are not writable when :py:class:`Canvas` is returned by
:py:meth:`Document.beginPage`, returned by
:py:meth:`PictureRecorder.beginRecording`, or :py:class:`Canvas` is the
base of a utility class like DebugCanvas.
Pixel values are converted only if :py:class:`ColorType` and
:py:class:`AlphaType` do not match. Only pixels within both source and
destination rectangles are copied. :py:class:`Canvas` pixels outside
:py:class:`Rect` intersection are unchanged.
Pass negative values for x or y to offset pixels to the left or above
:py:class:`Canvas` pixels.
Does not copy, and returns false if:
- Source and destination rectangles do not intersect.
- bitmap does not have allocated pixels.
- bitmap pixels could not be converted to
:py:meth:`Canvas.imageInfo()`.colorType() or alphaType().
- :py:class:`Canvas` pixels are not writable; for instance,
:py:class:`Canvas` is document-based.
- bitmap pixels are inaccessible; for instance, bitmap wraps a texture.
:bitmap: contains pixels copied to :py:class:`Canvas`.
:x: offset into :py:class:`Canvas` writable pixels on x-axis; may be
negative
:y: offset into :py:class:`Canvas` writable pixels on y-axis; may be
negative
:return: true if pixels were written to :py:class:`Canvas`
)docstring",
py::arg("bitmap"), py::arg("x") = 0, py::arg("y") = 0)
.def("save", &SkCanvas::save,
R"docstring(
Saves :py:class:`Matrix` and clip.
Calling :py:meth:`restore` discards changes to :py:class:`Matrix` and
clip, restoring the :py:class:`Matrix` and clip to their state when
:py:meth:`save` was called.
:py:class:`Matrix` may be changed by :py:meth:`translate`,
:py:meth:`scale`, :py:meth:`rotate`, :py:meth:`skew`, :py:meth:`concat`,
:py:meth:`setMatrix`, and :py:meth:`resetMatrix`. Clip may be changed by
:py:meth:`clipRect`, :py:meth:`clipRRect`, :py:meth:`clipPath`,
:py:meth:`clipRegion`.
Saved :py:class:`Canvas` state is put on a stack; multiple calls to
:py:meth:`save` should be balance by an equal number of calls to
:py:meth:`restore`.
Call :py:meth:`restoreToCount` with result to restore this and
subsequent saves.
:return: depth of saved stack
)docstring")
.def("saveLayer",
py::overload_cast<const SkRect*, const SkPaint*>(&SkCanvas::saveLayer),
R"docstring(
Saves :py:class:`Matrix` and clip, and allocates a :py:class:`Bitmap`
for subsequent drawing.
Calling :py:meth:`restore` discards changes to :py:class:`Matrix` and
clip, and draws the :py:class:`Bitmap`.
:py:class:`Matrix` may be changed by :py:meth:`translate`,
:py:meth:`scale`, :py:meth:`rotate`, :py:meth:`skew`, :py:meth:`concat`,
:py:meth:`setMatrix`, and :py:meth:`resetMatrix`. Clip may be changed by
:py:meth:`clipRect`, :py:meth:`clipRRect`, :py:meth:`clipPath`,
:py:meth:`clipRegion`.
:py:class:`Rect` bounds suggests but does not define the
:py:class:`Bitmap` size. To clip drawing to a specific rectangle, use
:py:meth:`clipRect`.
Optional :py:class:`Paint` paint applies alpha, :py:class:`ColorFilter`,
:py:class:`ImageFilter`, and :py:class:`BlendMode` when
:py:meth:`restore` is called.
Call :py:meth:`restoreToCount` with returned value to restore this and
subsequent saves.
:bounds: hint to limit the size of the layer; may be nullptr
:paint: graphics state for layer; may be nullptr
:return: depth of saved stack
)docstring",
py::arg("bounds") = nullptr, py::arg("paint") = nullptr)
// .def("saveLayer",
// py::overload_cast<const SkRect&, const SkPaint*>(
// &SkCanvas::saveLayer),
// "Saves SkMatrix and clip, and allocates a SkBitmap for subsequent "
// "drawing.")
.def("saveLayerAlpha", &SkCanvas::saveLayerAlpha,
R"docstring(
Saves :py:class:`Matrix` and clip, and allocates a :py:class:`Bitmap`
for subsequent drawing.
Calling :py:meth:`restore` discards changes to :py:class:`Matrix` and
clip, and blends layer with alpha opacity onto prior layer.
:py:class:`Matrix` may be changed by :py:meth:`translate`,
:py:meth:`scale`, :py:meth:`rotate`, :py:meth:`skew`, :py:meth:`concat`,
:py:meth:`setMatrix`, and :py:meth:`resetMatrix`. Clip may be changed by
:py:meth:`clipRect`, :py:meth:`clipRRect`, :py:meth:`clipPath`,
:py:meth:`clipRegion`.
:py:class:`Rect` bounds suggests but does not define the
:py:class:`Bitmap` size. To clip drawing to a specific rectangle, use
:py:meth:`clipRect`.
alpha of zero is fully transparent, 255 is fully opaque.
Call :py:meth:`restoreToCount` with returned value to restore this and
subsequent saves.
:param skia.Rect bounds: hint to limit the size of the layer; may be
nullptr
:param int alpha: opacity of layer
:return: depth of saved stack
)docstring",
py::arg("bounds"), py::arg("alpha"))
.def("saveLayer",
py::overload_cast<const SkCanvas::SaveLayerRec&>(&SkCanvas::saveLayer),
R"docstring(
Saves :py:class:`Matrix` and clip, and allocates a :py:class:`Bitmap`
for subsequent drawing.
Calling :py:meth:`restore` discards changes to :py:class:`Matrix` and
clip, and blends :py:class:`Bitmap` with alpha opacity onto the prior
layer.
:py:class:`Matrix` may be changed by :py:meth:`translate`,
:py:meth:`scale`, :py:meth:`rotate`, :py:meth:`skew`, :py:meth:`concat`,
:py:meth:`setMatrix`, and :py:meth:`resetMatrix`. Clip may be changed by
:py:meth:`clipRect`, :py:meth:`clipRRect`, :py:meth:`clipPath`,
:py:meth:`clipRegion`.
:py:class:`SaveLayerRec` contains the state used to create the layer.
Call :py:meth:`restoreToCount` with returned value to restore this and
subsequent saves.
:layerRec: layer state
:return: depth of save state stack before this call was made.
)docstring",
py::arg("layerRec"))
.def("restore", &SkCanvas::restore,
R"docstring(
Removes changes to :py:class:`Matrix` and clip since :py:class:`Canvas`
state was last saved.
The state is removed from the stack.
Does nothing if the stack is empty.
)docstring")
.def("getSaveCount", &SkCanvas::getSaveCount,
R"docstring(
Returns the number of saved states, each containing: :py:class:`Matrix`
and clip.
Equals the number of :py:meth:`save` calls less the number of
:py:meth:`restore` calls plus one. The save count of a new canvas is
one.
:return: depth of save state stack
)docstring")
.def("restoreToCount", &SkCanvas::restoreToCount,
R"docstring(
Restores state to :py:class:`Matrix` and clip values when
:py:meth:`save`, :py:meth:`saveLayer`,
:py:meth:`saveLayerPreserveLCDTextRequests`, or
:py:meth:`saveLayerAlpha` returned saveCount.
Does nothing if saveCount is greater than state stack count. Restores
state to initial values if saveCount is less than or equal to one.
:param int saveCount: depth of state stack to restore
)docstring",
py::arg("saveCount"))
.def("translate", &SkCanvas::translate,
R"docstring(
Translates :py:class:`Matrix` by dx along the x-axis and dy along the
y-axis.
Mathematically, replaces :py:class:`Matrix` with a translation matrix
premultiplied with :py:class:`Matrix`.
This has the effect of moving the drawing by (dx, dy) before
transforming the result with :py:class:`Matrix`.
:param dx: distance to translate on x-axis
:param dy: distance to translate on y-axis
)docstring",
py::arg("dx"), py::arg("dy"))
.def("scale", &SkCanvas::scale,
R"docstring(
Scales :py:class:`Matrix` by sx on the x-axis and sy on the y-axis.
Mathematically, replaces :py:class:`Matrix` with a scale matrix
premultiplied with :py:class:`Matrix`.
This has the effect of scaling the drawing by (sx, sy) before
transforming the result with :py:class:`Matrix`.
:param float sx: amount to scale on x-axis
:param float sy: amount to scale on y-axis
)docstring",
py::arg("sx"), py::arg("sy"))
.def("rotate", py::overload_cast<SkScalar>(&SkCanvas::rotate),
R"docstring(
Rotates :py:class:`Matrix` by degrees.
Positive degrees rotates clockwise.
Mathematically, replaces :py:class:`Matrix` with a rotation matrix
premultiplied with :py:class:`Matrix`.
This has the effect of rotating the drawing by degrees before
transforming the result with :py:class:`Matrix`.
:degrees: amount to rotate, in degrees
)docstring",
py::arg("degrees"))
.def("rotate",
py::overload_cast<SkScalar, SkScalar, SkScalar>(&SkCanvas::rotate),
R"docstring(
Rotates :py:class:`Matrix` by degrees about a point at (px, py).
Positive degrees rotates clockwise.
Mathematically, constructs a rotation matrix; premultiplies the rotation
matrix by a translation matrix; then replaces :py:class:`Matrix` with
the resulting matrix premultiplied with :py:class:`Matrix`.
This has the effect of rotating the drawing about a given point before
transforming the result with :py:class:`Matrix`.
:degrees: amount to rotate, in degrees
:px: x-axis value of the point to rotate about
:py: y-axis value of the point to rotate about
)docstring",
py::arg("degrees"), py::arg("px"), py::arg("py"))
.def("skew", &SkCanvas::skew,
R"docstring(
Skews :py:class:`Matrix` by sx on the x-axis and sy on the y-axis.
A positive value of sx skews the drawing right as y-axis values
increase; a positive value of sy skews the drawing down as x-axis values
increase.
Mathematically, replaces :py:class:`Matrix` with a skew matrix
premultiplied with :py:class:`Matrix`.
This has the effect of skewing the drawing by (sx, sy) before
transforming the result with :py:class:`Matrix`.
:param float sx: amount to skew on x-axis
:param float sy: amount to skew on y-axis
)docstring",
py::arg("sx"), py::arg("sy"))
.def("concat", py::overload_cast<const SkMatrix&>(&SkCanvas::concat),
R"docstring(
Replaces :py:class:`Matrix` with matrix premultiplied with existing
:py:class:`Matrix`.
This has the effect of transforming the drawn geometry by matrix, before
transforming the result with existing :py:class:`Matrix`.
:matrix: matrix to premultiply with existing :py:class:`Matrix`
)docstring",
py::arg("matrix"))
.def("concat", py::overload_cast<const SkM44&>(&SkCanvas::concat),
py::arg("m44"))
.def("setMatrix", &SkCanvas::setMatrix,
R"docstring(
Replaces :py:class:`Matrix` with matrix.
Unlike :py:meth:`concat`, any prior matrix state is overwritten.
:param skia.Matrix matrix: matrix to copy, replacing existing
:py:class:`Matrix`
)docstring",
py::arg("matrix"))
.def("resetMatrix", &SkCanvas::resetMatrix,
R"docstring(
Sets SkMatrix to the identity matrix.
Any prior matrix state is overwritten.
)docstring")
.def("clipRect",