Repository navigation
Expand file tree
/
Copy path_util.cpp
More file actions
1047 lines (1001 loc) · 36.9 KB
/
Copy path_util.cpp
File metadata and controls
1047 lines (1001 loc) · 36.9 KB
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 "_util.h"
#include "_raqm.h"
#include FT_TRUETYPE_TABLES_H
#include <regex>
#include <stack>
#include "_macros.h"
using namespace std::string_literals;
namespace mplcairo {
namespace detail {
// FreeType error codes. They are loaded as documented in fterror.h (modified
// to use std::unordered_map).
// NOTE: If we require FreeType>=2.6.3 then the macro can be replaced by
// FTERRORS_H_.
#undef __FTERRORS_H__
#define FT_ERRORDEF( e, v, s ) { e, s },
#define FT_ERROR_START_LIST {
#define FT_ERROR_END_LIST }
std::unordered_map<FT_Error, std::string> const ft_errors
#include FT_ERRORS_H
;
FT_Library ft_library{};
bool has_pycairo{};
std::array<uint8_t, 0x10000> premultiplication_table{[]() {
auto table = decltype(premultiplication_table){};
for (auto a = 1; a < 0x100; ++a) {
for (auto c = 0; c < 0x100; ++c) {
table[(a << 8) + c] = a / 255. * c;
}
}
return table;
} ()};
std::array<uint8_t, 0x10000> unpremultiplication_table{[]() {
auto table = decltype(unpremultiplication_table){};
// As in cairo-png.c (unpremultiply 0 => 0).
for (auto a = 1; a < 0x100; ++a) {
for (auto c = 0; c <= a; ++c) {
table[(a << 8) + c] = (c * 0xff + a / 2) / a;
}
}
return table;
} ()};
#define DEFINE_API(name) decltype(name) name;
ITER_CAIRO_OPTIONAL_API(DEFINE_API)
#undef DEFINE_API
// Other useful values.
std::unordered_map<std::string, cairo_font_face_t*> FONT_CACHE{};
cairo_user_data_key_t const REFS_KEY{},
STATE_KEY{},
INIT_MATRIX_KEY{},
FT_KEY{},
FEATURES_KEY{},
LANGS_KEY{},
VARIATIONS_KEY{},
IS_COLOR_FONT_KEY{};
py::object RC_PARAMS{},
PIXEL_MARKER{},
UNIT_CIRCLE{};
int COLLECTION_THREADS{};
cairo_format_t IMAGE_FORMAT{CAIRO_FORMAT_ARGB32};
double MITER_LIMIT{10.};
bool DEBUG{};
MplcairoScriptSurface MPLCAIRO_SCRIPT_SURFACE{[] {
if (auto script_surface = std::getenv("MPLCAIRO_SCRIPT_SURFACE")) {
if (script_surface == "raster"s) {
return MplcairoScriptSurface::Raster;
} else if (script_surface == "vector"s) {
return MplcairoScriptSurface::Vector;
}
}
return MplcairoScriptSurface::None;
}()};
}
rgba_t AdditionalState::get_hatch_color() {
if (!hatch_color) {
hatch_color = to_rgba(rc_param("hatch.color"));
}
return *hatch_color;
}
double AdditionalState::get_hatch_linewidth() {
if (!hatch_linewidth) {
hatch_linewidth = rc_param("hatch.linewidth").cast<double>();
}
return *hatch_linewidth;
}
GlyphsAndClusters::~GlyphsAndClusters() {
cairo_glyph_free(glyphs);
cairo_text_cluster_free(clusters);
}
py::object operator""_format(char const* fmt, std::size_t size) {
return py::str(fmt, size).attr("format");
}
bool py_eq(py::object obj1, py::object obj2) {
return py::module::import("operator").attr("eq")(obj1, obj2).cast<bool>();
}
py::dict get_options()
{
return py::dict(
"cairo_circles"_a=bool(detail::UNIT_CIRCLE),
"collection_threads"_a=detail::COLLECTION_THREADS,
"image_format"_a=detail::IMAGE_FORMAT,
"miter_limit"_a=detail::MITER_LIMIT,
"raqm"_a=has_raqm(),
"_debug"_a=detail::DEBUG);
}
py::object set_options(py::kwargs kwargs)
{
auto estack = py::module::import("contextlib").attr("ExitStack")();
estack.attr("callback")(py::cpp_function{set_options}, **get_options());
auto const& pop_option =
[&](std::string key, auto dummy) -> std::optional<decltype(dummy)> {
return
kwargs.attr("pop")(key, py::none())
.cast<std::optional<decltype(dummy)>>();
};
if (auto const& cairo_circles = pop_option("cairo_circles", bool{})) {
if (*cairo_circles) {
detail::UNIT_CIRCLE =
py::module::import("matplotlib.path").attr("Path")
.attr("unit_circle")();
} else {
Py_XDECREF(detail::UNIT_CIRCLE.release().ptr());
}
}
if (auto const& image_format =
pop_option("image_format",
std::variant<cairo_format_t, std::string>{})) {
auto fmt = std::visit(overloaded {
[](cairo_format_t fmt) {
return fmt;
},
[](std::string fmt) { // operator[]() doesn't support std::string
return
py::module::import("mplcairo").attr("format_t")[fmt.c_str()]
.cast<cairo_format_t>();
}
}, *image_format);
if (fmt > CAIRO_FORMAT_RGB30
&& cairo_version() < CAIRO_VERSION_ENCODE(1, 17, 2)) {
throw std::invalid_argument{"float surfaces require cairo>=1.17.2"};
}
detail::IMAGE_FORMAT = fmt;
}
if (auto const& threads = pop_option("collection_threads", int{})) {
detail::COLLECTION_THREADS = *threads;
}
if (auto const& miter_limit = pop_option("miter_limit", double{})) {
detail::MITER_LIMIT = *miter_limit;
}
if (auto const& raqm = pop_option("raqm", bool{})) {
if (*raqm) {
load_raqm();
} else {
unload_raqm();
}
}
if (auto const& debug = pop_option("_debug", bool{})) {
detail::DEBUG = *debug;
}
if (py::bool_(kwargs)) {
throw std::runtime_error{
"unknown options passed to set_options: {}"_format(kwargs)
.cast<std::string>()};
}
return estack;
}
py::object rc_param(std::string key)
{
return py::reinterpret_borrow<py::object>( // Use a faster path.
PyDict_GetItemString(detail::RC_PARAMS.ptr(), key.data()));
}
rgba_t to_rgba(py::object color, std::optional<double> alpha)
{
return
py::module::import("matplotlib.colors")
.attr("to_rgba")(color, alpha).cast<rgba_t>();
}
cairo_matrix_t matrix_from_transform(py::object transform, double y0)
{
if (!py::bool_(py::getattr(transform, "is_affine", py::bool_(true)))) {
throw std::invalid_argument{"only affine transforms are handled"};
}
auto const& py_matrix = transform.cast<py::array_t<double>>().unchecked<2>();
if (py_matrix.shape(0) != 3 || py_matrix.shape(1) != 3) {
throw std::invalid_argument{
"transformation matrix must have shape (3, 3), "
"not {.shape}"_format(transform).cast<std::string>()};
}
return cairo_matrix_t{
py_matrix(0, 0), -py_matrix(1, 0),
py_matrix(0, 1), -py_matrix(1, 1),
py_matrix(0, 2), y0 - py_matrix(1, 2)};
}
cairo_matrix_t matrix_from_transform(
py::object transform, cairo_matrix_t const* master_matrix)
{
if (!py::bool_(py::getattr(transform, "is_affine", py::bool_(true)))) {
throw std::invalid_argument{"only affine transforms are handled"};
}
auto const& py_matrix = transform.cast<py::array_t<double>>().unchecked<2>();
if (py_matrix.shape(0) != 3 || py_matrix.shape(1) != 3) {
throw std::invalid_argument{
"transformation matrix must have shape (3, 3), "
"not {.shape}"_format(transform).cast<std::string>()};
}
// The y flip is already handled by the master matrix.
auto mtx = cairo_matrix_t{
py_matrix(0, 0), py_matrix(1, 0),
py_matrix(0, 1), py_matrix(1, 1),
py_matrix(0, 2), py_matrix(1, 2)};
cairo_matrix_multiply(&mtx, &mtx, master_matrix);
return mtx;
}
bool has_vector_surface(cairo_t* cr)
{
switch (auto const& type = cairo_surface_get_type(cairo_get_target(cr))) {
case CAIRO_SURFACE_TYPE_IMAGE:
case CAIRO_SURFACE_TYPE_XLIB:
return false;
case CAIRO_SURFACE_TYPE_PDF:
case CAIRO_SURFACE_TYPE_PS:
case CAIRO_SURFACE_TYPE_SVG:
case CAIRO_SURFACE_TYPE_RECORDING:
return true;
case CAIRO_SURFACE_TYPE_SCRIPT:
switch (detail::MPLCAIRO_SCRIPT_SURFACE) {
case detail::MplcairoScriptSurface::Raster:
return false;
case detail::MplcairoScriptSurface::Vector:
return true;
default: ;
}
default:
throw std::invalid_argument{
"unexpected surface type: " + std::to_string(type)};
}
}
// Same as GraphicsContextRenderer::get_additional_state() but with checking
// for cairo_t*'s that we may not have initialized.
AdditionalState& get_additional_state(cairo_t* cr)
{
auto const stack = static_cast<std::stack<AdditionalState>*>(
cairo_get_user_data(cr, &detail::STATE_KEY));
if (!stack || stack->empty()) {
throw std::runtime_error{"cairo_t* missing additional state"};
}
return stack->top();
}
void restore_init_matrix(cairo_t* cr)
{
auto const mtx = static_cast<cairo_matrix_t*>(
cairo_get_user_data(cr, &detail::INIT_MATRIX_KEY));
if (!mtx) {
cairo_identity_matrix(cr);
} else {
cairo_set_matrix(cr, mtx);
}
}
// Set the current path of `cr` to `path`, after transformation by `matrix`,
// ignoring the CTM ("exact").
//
// TODO: Path clipping in the general case (with codes present), and snapping
// in the presence of CLOSEPOLY (likely the correct solution is to preload the
// whole path and adjust for snapping), matplotlib#30576. Fortunately, in the
// most common case where everything is axis-aligned, the first and last points
// are already snapped due to being aligned with the second and next-to-last
// points already, so the bug is hidden.
// NOTE: Matplotlib also *rounds* the linewidth in some cases (see
// RendererAgg::_draw_path), which helps with snappiness. We do not provide
// this behavior; instead, one should set the default linewidths appropriately
// if desired.
// FIXME[cairo]: cairo requires coordinates to fit within a 24-bit signed
// integer (https://gitlab.freedesktop.org/cairo/cairo/issues/252 and
// :mpltest:`test_simplification.test_overflow`). We simply clamp the
// values in the general case (with codes) -- proper handling would involve
// clipping of polygons and of Beziers -- and use a simple clippling algorithm
// (Cohen-Sutherland) in the simple (codeless) case as we expect most segments
// to be within the clip rectangle -- cairo will run its own clipping later
// anyways.
// A helper to store the CTM without the need to cairo_save() the full state.
// (We can't simply call cairo_transform(cr, matrix) because matrix may be
// degenerate (e.g., for zero-sized markers). Fortunately, the cost of doing
// the transformation ourselves seems negligible, if any.)
struct LoadPathContext {
cairo_t* const cr;
cairo_matrix_t ctm;
bool const snap;
double (*snapper)(double);
public:
LoadPathContext(cairo_t* cr) :
cr{cr},
snap{!has_vector_surface(cr) && get_additional_state(cr).snap}
{
cairo_get_matrix(cr, &ctm);
restore_init_matrix(cr);
cairo_new_path(cr);
auto const& lw = cairo_get_line_width(cr);
snapper =
snap
? (0 < lw && (lw < 1 || std::lround(lw) % 2 == 1)
// MSVC doesn't support lambdas in ternary branches.
? static_cast<decltype(snapper)>(
[](double x) -> double { return std::floor(x) + .5; })
: static_cast<decltype(snapper)>(&std::round))
// Snap between pixels if lw is exactly zero 0 (in which case the edge is
// defined by the fill) or if lw rounds to an even value other than 0
// (minimizing the alpha due to antialiasing).
: static_cast<decltype(snapper)>([](double x) -> double { return x; });
}
~LoadPathContext()
{
cairo_set_matrix(cr, &ctm);
}
};
// This overload implements the general case.
void load_path_exact(
cairo_t* cr, py::handle path, cairo_matrix_t const* matrix)
{
[[maybe_unused]] auto const& gil = py::gil_scoped_acquire{};
auto const& min = double(-(1 << 22)), max = double(1 << 22);
auto const& lpc = LoadPathContext{cr};
auto const& vertices_keepref =
path.attr("vertices").cast<py::array_t<double>>();
auto const& codes_keepref =
path.attr("codes").cast<std::optional<py::array_t<uint8_t>>>();
auto const& n = vertices_keepref.shape(0);
if (vertices_keepref.shape(1) != 2) {
throw std::invalid_argument{
"vertices must have shape (n, 2), not {.shape}"_format(
path.attr("vertices")).cast<std::string>()};
}
if (!codes_keepref) {
load_path_exact(cr, vertices_keepref, 0, n, matrix);
return;
}
auto const& vertices = vertices_keepref.unchecked<2>();
auto const& codes = codes_keepref->unchecked<1>();
if (codes.shape(0) != n) {
throw std::invalid_argument{
"lengths of vertices ({}) and codes ({}) are mistached "_format(
n, codes.shape(0)).cast<std::string>()};
}
auto force_snap_next_lineto = bool{};
auto const& snapper = lpc.snapper;
// Main loop.
for (auto i = 0; i < n; ++i) {
auto x0 = vertices(i, 0), y0 = vertices(i, 1);
cairo_matrix_transform_point(matrix, &x0, &y0);
auto const& is_finite = std::isfinite(x0) && std::isfinite(y0);
// Better(?) than nothing.
x0 = std::clamp(x0, min, max);
y0 = std::clamp(y0, min, max);
switch (static_cast<PathCode>(codes(i))) {
case PathCode::STOP:
break;
case PathCode::MOVETO:
if (is_finite) {
// See comments re: snapping in the codeless path.
if (lpc.snap && i + 1 < n
&& static_cast<PathCode>(codes(i + 1)) == PathCode::LINETO) {
auto x1 = vertices(i + 1, 0), y1 = vertices(i + 1, 1);
cairo_matrix_transform_point(matrix, &x1, &y1);
if (x1 == x0 || y1 == y0) {
x0 = snapper(x0);
y0 = snapper(y0);
force_snap_next_lineto = true;
}
}
cairo_move_to(cr, x0, y0);
} else {
cairo_new_sub_path(cr);
}
break;
case PathCode::LINETO:
if (is_finite) {
auto x00 = x0, y00 = y0;
if (force_snap_next_lineto) {
x0 = snapper(x0);
y0 = snapper(y0);
}
force_snap_next_lineto = false;
if (lpc.snap && i + 1 < n
&& static_cast<PathCode>(codes(i + 1)) == PathCode::LINETO) {
auto x1 = vertices(i + 1, 0), y1 = vertices(i + 1, 1);
cairo_matrix_transform_point(matrix, &x1, &y1);
if (x1 == x00 || y1 == y00) {
x0 = snapper(x0);
y0 = snapper(y0);
force_snap_next_lineto = true;
}
}
cairo_line_to(cr, x0, y0);
} else {
cairo_new_sub_path(cr);
}
break;
// The semantics of nonfinite control points are tested in
// :mpltest:`test_simplification.test_simplify_curve`: if the last point
// is finite, it sets the current point for the next curve; otherwise, a
// new sub-path is created.
case PathCode::CURVE3: {
auto x1 = vertices(i + 1, 0), y1 = vertices(i + 1, 1);
cairo_matrix_transform_point(matrix, &x1, &y1);
i += 1;
auto const& last_finite = std::isfinite(x1) && std::isfinite(y1);
if (last_finite) {
auto const& x10 = x1, y10 = y1;
x1 = std::clamp(x1, min, max);
y1 = std::clamp(y1, min, max);
if (lpc.snap && i + 1 < n
&& static_cast<PathCode>(codes(i + 1)) == PathCode::LINETO) {
auto x2 = vertices(i + 1, 0), y2 = vertices(i + 1, 1);
cairo_matrix_transform_point(matrix, &x2, &y2);
if (x2 == x10 || y2 == y10) {
x1 = snapper(x1);
y1 = snapper(y1);
force_snap_next_lineto = true;
}
}
if (is_finite && cairo_has_current_point(cr)) {
double x_prev, y_prev;
cairo_get_current_point(cr, &x_prev, &y_prev);
cairo_curve_to(cr,
(x_prev + 2 * x0) / 3, (y_prev + 2 * y0) / 3,
(2 * x0 + x1) / 3, (2 * y0 + y1) / 3,
x1, y1);
} else {
cairo_move_to(cr, x1, y1);
}
} else {
cairo_new_sub_path(cr);
}
break;
}
case PathCode::CURVE4: {
auto x1 = vertices(i + 1, 0), y1 = vertices(i + 1, 1),
x2 = vertices(i + 2, 0), y2 = vertices(i + 2, 1);
cairo_matrix_transform_point(matrix, &x1, &y1);
cairo_matrix_transform_point(matrix, &x2, &y2);
i += 2;
auto const& last_finite = std::isfinite(x2) && std::isfinite(y2);
if (last_finite) {
auto const& x20 = x2, y20 = y2;
x1 = std::clamp(x1, min, max);
y1 = std::clamp(y1, min, max);
x2 = std::clamp(x2, min, max);
y2 = std::clamp(y2, min, max);
if (lpc.snap && i + 1 < n
&& static_cast<PathCode>(codes(i + 1)) == PathCode::LINETO) {
auto x3 = vertices(i + 1, 0), y3 = vertices(i + 1, 1);
cairo_matrix_transform_point(matrix, &x3, &y3);
if (x3 == x20 || y3 == y20) {
x2 = snapper(x2);
y2 = snapper(y2);
force_snap_next_lineto = true;
}
}
if (is_finite && std::isfinite(x1) && std::isfinite(y1)
&& cairo_has_current_point(cr)) {
cairo_curve_to(cr, x0, y0, x1, y1, x2, y2);
} else {
cairo_move_to(cr, x2, y2);
}
} else {
cairo_new_sub_path(cr);
}
break;
}
case PathCode::CLOSEPOLY:
cairo_close_path(cr);
break;
}
}
}
// This overload implements the case of a codeless path. Exposing start and
// stop in the signature helps implementing support for agg.path.chunksize.
void load_path_exact(
cairo_t* cr, py::array_t<double> vertices_keepref,
ssize_t start, ssize_t stop, cairo_matrix_t const* matrix)
{
[[maybe_unused]] auto const& gil = py::gil_scoped_acquire{};
auto const min = double(-(1 << 22)), max = double(1 << 22);
auto const& lpc = LoadPathContext{cr};
auto const& vertices = vertices_keepref.unchecked<2>();
auto const& n = vertices.shape(0);
if (!(0 <= start && start <= stop && stop <= n)) {
throw std::invalid_argument{
"invalid sub-path bounds ({}, {}) for path of size {}"_format(
start, stop, n).cast<std::string>()};
}
auto const& snapper = lpc.snapper;
auto path_data = std::vector<cairo_path_data_t>{};
path_data.reserve(2 * (stop - start));
auto const LEFT = 1 << 0, RIGHT = 1 << 1, BOTTOM = 1 << 2, TOP = 1 << 3;
auto const& outcode = [&](double x, double y) -> int {
auto code = 0;
if (x < min) {
code |= LEFT;
} else if (x > max) {
code |= RIGHT;
}
if (y < min) {
code |= BOTTOM;
} else if (y > max) {
code |= TOP;
}
return code;
};
// The previous point, if any, before clipping and snapping.
auto prev = std::optional<std::tuple<double, double>>{};
// Main loop.
for (auto i = start; i < stop; ++i) {
auto x = vertices(i, 0), y = vertices(i, 1);
cairo_matrix_transform_point(matrix, &x, &y);
if (std::isfinite(x) && std::isfinite(y)) {
cairo_path_data_t header, point;
if (prev) {
header.header = {CAIRO_PATH_LINE_TO, 2};
auto [x_prev, y_prev] = *prev;
prev = {x, y};
// Cohen-Sutherland clipping: we expect most segments to be within
// the 1 << 22 by 1 << 22 box.
auto code0 = outcode(x_prev, y_prev);
auto code1 = outcode(x, y);
auto accept = false, update_prev = false;
while (true) {
if (!(code0 | code1)) {
accept = true;
break;
} else if (code0 & code1) {
break;
} else {
auto xc = 0., yc = 0.;
auto code = code0 ? code0 : code1;
if (code & TOP) {
xc = x_prev + (x - x_prev) * (max - y_prev) / (y - y_prev);
yc = max;
} else if (code & BOTTOM) {
xc = x_prev + (x - x_prev) * (min - y_prev) / (y - y_prev);
yc = min;
} else if (code & RIGHT) {
yc = y_prev + (y - y_prev) * (max - x_prev) / (x - x_prev);
xc = max;
} else if (code & LEFT) {
yc = y_prev + (y - y_prev) * (min - x_prev) / (x - x_prev);
xc = min;
}
if (code == code0) {
update_prev = true;
x_prev = xc;
y_prev = yc;
code0 = outcode(x_prev, y_prev);
} else {
x = xc;
y = yc;
code1 = outcode(x, y);
}
}
}
if (accept) {
// If we accept the segment, but the previous point moved, record
// a MOVE_TO the new previous point (which will be followed by a
// LINE_TO the current point).
if (update_prev) {
cairo_path_data_t header_prev, point_prev;
header_prev.header = {CAIRO_PATH_MOVE_TO, 2};
point_prev.point = {x_prev, y_prev};
path_data.push_back(header_prev);
path_data.push_back(point_prev);
}
} else {
// If we don't accept the segment, still record a MOVE_TO the raw
// destination, as the next point may involve snapping.
header.header = {CAIRO_PATH_MOVE_TO, 2};
}
// Snapping.
if (lpc.snap && (x == x_prev || y == y_prev)) {
// If we have a horizontal or a vertical line, snap both coordinates.
// While it may make sense to only snap in the direction orthogonal
// to the displacement, this would cause e.g. axes spines to not line
// up properly, as they are drawn as independent segments.
path_data.back().point = {snapper(x_prev), snapper(y_prev)};
point.point = {snapper(x), snapper(y)};
} else {
point.point = {x, y};
}
// Record the point.
path_data.push_back(header);
path_data.push_back(point);
} else {
prev = {x, y};
header.header = {CAIRO_PATH_MOVE_TO, 2};
point.point = {x, y};
path_data.push_back(header);
path_data.push_back(point);
}
} else {
prev = {};
}
}
auto const& path =
cairo_path_t{
CAIRO_STATUS_SUCCESS, path_data.data(), int(path_data.size())};
cairo_append_path(cr, &path);
}
// Fill and/or stroke `path` onto `cr` after transformation by `matrix`,
// ignoring the CTM ("exact").
void fill_and_stroke_exact(
cairo_t* cr, py::handle path, cairo_matrix_t const* matrix,
std::optional<rgba_t> fill, std::optional<rgba_t> stroke)
{
cairo_save(cr);
auto path_loaded = false;
if (fill) {
auto const& [r, g, b, a] = *fill;
cairo_set_source_rgba(cr, r, g, b, a);
if (path.is(detail::UNIT_CIRCLE) && !has_vector_surface(cr)) {
// Abuse the degenerate-segment handling by cairo to rasterize
// circles efficiently. Don't do this on vector backends both
// because the user may technically want the actual path, and because
// Inkscape does not render circle markers on zero-sized paths
// (FIXME[inkscape] https://bugs.launchpad.net/inkscape/+bug/689562).
cairo_save(cr);
cairo_new_path(cr);
cairo_move_to(cr, matrix->x0, matrix->y0);
cairo_close_path(cr);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_width(cr, 2);
cairo_set_matrix(cr, matrix);
cairo_stroke(cr);
cairo_restore(cr);
} else {
if (!path_loaded) {
load_path_exact(cr, path, matrix);
path_loaded = true;
}
cairo_fill_preserve(cr);
}
}
if (stroke) {
auto const& [r, g, b, a] = *stroke;
cairo_set_source_rgba(cr, r, g, b, a);
if (!path_loaded) {
load_path_exact(cr, path, matrix);
path_loaded = true;
}
restore_init_matrix(cr); // Dashes are interpreted using the CTM.
cairo_stroke_preserve(cr);
}
cairo_restore(cr);
}
py::array image_surface_to_buffer(cairo_surface_t* surface) {
// Possible outputs:
// ARGB32 -> uint8, (h, w, 4)
// RGB24 -> uint8, (h, w, 3) (non-contiguous)
// A8 -> uint8, (h, w)
// A1 -> ("V{w}", void(ceil(w/8))), (h,)
// RGB16_565 -> uint16, (h, w)
// RGB30 -> uint32, (h, w)
// RGB96F -> float, (h, w, 3)
// RGB128F -> float, (h, w, 4)
if (auto const& type = cairo_surface_get_type(surface);
type != CAIRO_SURFACE_TYPE_IMAGE) {
throw std::runtime_error{
"_get_buffer only supports image surfaces, not {}"_format(type)
.cast<std::string>()};
}
cairo_surface_reference(surface);
cairo_surface_flush(surface);
auto const& h = cairo_image_surface_get_height(surface),
& w = cairo_image_surface_get_width(surface),
& stride = cairo_image_surface_get_stride(surface);
auto const& data = cairo_image_surface_get_data(surface);
auto const& capsule = py::capsule(
surface,
[](void* surface) -> void {
cairo_surface_destroy(static_cast<cairo_surface_t*>(surface));
});
switch (auto const& fmt = cairo_image_surface_get_format(surface);
// Avoid "not in enumerated type" warning with CAIRO_FORMAT_RGBA_128F.
static_cast<int>(fmt)) {
case static_cast<int>(CAIRO_FORMAT_ARGB32):
return py::array_t<uint8_t>{{h, w, 4}, {stride, 4, 1}, data, capsule};
case static_cast<int>(CAIRO_FORMAT_RGB24):
return py::array_t<uint8_t>{{h, w, 3}, {stride, 4, 1}, data, capsule};
case static_cast<int>(CAIRO_FORMAT_A8):
return py::array_t<uint8_t>{{h, w}, {stride, 1}, data, capsule};
case static_cast<int>(CAIRO_FORMAT_A1): {
auto dtype_args = py::list{};
dtype_args.append(py::make_tuple(
"V" + std::to_string(w), "V" + std::to_string((w + 7) / 8)));
return py::array{
py::dtype::from_args(dtype_args), {h}, {stride}, data, capsule};
}
case static_cast<int>(CAIRO_FORMAT_RGB16_565):
return py::array_t<uint16_t>{
{h, w}, {stride, 2}, reinterpret_cast<uint16_t*>(data), capsule};
case static_cast<int>(CAIRO_FORMAT_RGB30):
return py::array_t<uint32_t>{
{h, w}, {stride, 4}, reinterpret_cast<uint32_t*>(data), capsule};
case 6: // CAIRO_FORMAT_RGB96F.
return py::array_t<float>{
{h, w, 3}, {stride, 12, 4}, reinterpret_cast<float*>(data), capsule};
case 7: // CAIRO_FORMAT_RGBA128F.
return py::array_t<float>{
{h, w, 4}, {stride, 16, 4}, reinterpret_cast<float*>(data), capsule};
default:
throw std::invalid_argument{
"_get_buffer does not support image surfaces with format "
"{}"_format(fmt).cast<std::string>()};
}
}
auto parse_pathspec(std::string pathspec)
{
struct parse_t {
std::string path;
int face_index;
std::vector<std::string> features;
std::vector<std::tuple<std::string, int, int>> langs;
std::string variations;
};
auto match = std::smatch{};
if (!std::regex_match(pathspec, match,
std::regex{"(.*?)(#(\\d+))?(\\|([^|]*))?(\\|(.*))?"})) {
throw std::runtime_error{
"Failed to parse pathspec {}"_format(pathspec).cast<std::string>()};
}
auto parse = parse_t{
match.str(1),
std::atoi(match.str(3).c_str()), // 0 if absent.
{}, {}, // Fill below.
match.str(7)
};
auto const& features_s = match.str(5);
if (features_s.size()) {
auto const& comma = std::regex{","};
auto toks = std::sregex_token_iterator{
features_s.begin(), features_s.end(), comma, -1};
while (toks != std::sregex_token_iterator{}) {
auto const& tok = std::string{toks->str()};
if (std::regex_match(
tok,
match,
std::regex{"^language(?:\\[(\\d+)?(?::(\\d+))?\\])?=(.*)$"})) {
auto const& start = std::atoi(match.str(1).c_str()); // 0 if absent.
auto const& stop =
match[2].matched ? std::atoi(match.str(2).c_str()) : -1;
auto const& lang = match.str(3);
parse.langs.emplace_back(lang, start, stop);
} else {
parse.features.emplace_back(tok);
}
++toks;
}
}
return parse;
}
cairo_font_face_t* font_face_from_path(std::string pathspec)
{
auto& font_face = detail::FONT_CACHE[pathspec];
if (!font_face) {
auto parsed = parse_pathspec(pathspec);
FT_Face ft_face;
if (auto const& error =
FT_New_Face(
detail::ft_library, parsed.path.c_str(), parsed.face_index, &ft_face)) {
if (error == FT_Err_Cannot_Open_Resource) {
// Throw the exception that Python would throw...
py::module::import("builtins").attr("open")(parsed.path);
if (PyErr_Occurred()) { // ... if possible.
throw py::error_already_set{};
}
}
THROW_ERROR("FT_New_Face", mplcairo::detail::ft_errors.at(error));
}
font_face =
cairo_ft_font_face_create_for_ft_face(ft_face, get_hinting_flag());
auto font_face_cleanup = // In case set_user_data fails; released at end.
std::unique_ptr<
std::remove_pointer_t<cairo_font_face_t>,
decltype(&cairo_font_face_destroy)>{
font_face, cairo_font_face_destroy};
CAIRO_CHECK_SET_USER_DATA(
cairo_font_face_set_user_data, font_face, &detail::FT_KEY, ft_face,
[](void* ptr) { FT_CHECK(FT_Done_Face, reinterpret_cast<FT_Face>(ptr)); });
CAIRO_CHECK_SET_USER_DATA_NEW(
cairo_font_face_set_user_data, font_face, &detail::FEATURES_KEY,
parsed.features);
CAIRO_CHECK_SET_USER_DATA_NEW(
cairo_font_face_set_user_data, font_face, &detail::LANGS_KEY,
parsed.langs);
CAIRO_CHECK_SET_USER_DATA_NEW(
cairo_font_face_set_user_data, font_face, &detail::VARIATIONS_KEY,
parsed.variations);
// Color fonts need special handling due to cairo#404 and raqm#123; see
// corresponding sections of the code.
if (FT_IS_SFNT(ft_face)) {
auto n_tables = FT_ULong{}, table_length = FT_ULong{};
FT_CHECK(FT_Sfnt_Table_Info, ft_face, 0, nullptr, &n_tables);
for (auto i = FT_ULong{}; i < n_tables; ++i) {
auto tag = FT_ULong{};
FT_CHECK(
FT_Sfnt_Table_Info, ft_face, i, &tag, &table_length);
if (tag == FT_MAKE_TAG('C', 'O', 'L', 'R')
|| tag == FT_MAKE_TAG('C', 'P', 'A', 'L')
|| tag == FT_MAKE_TAG('C', 'B', 'D', 'T')
|| tag == FT_MAKE_TAG('C', 'B', 'L', 'C')
|| tag == FT_MAKE_TAG('s', 'b', 'i', 'x')
|| tag == FT_MAKE_TAG('S', 'V', 'G', ' ')) {
CAIRO_CHECK_SET_USER_DATA(
cairo_font_face_set_user_data,
font_face, &detail::IS_COLOR_FONT_KEY, font_face, nullptr);
break;
}
}
}
font_face_cleanup.release();
}
return cairo_font_face_reference(font_face);
}
cairo_font_face_t* font_face_from_path(py::object path) {
return
font_face_from_path(
py::reinterpret_steal<py::object>(PY_CHECK(PyOS_FSPath, path.ptr()))
.cast<std::string>());
}
std::vector<cairo_font_face_t*> font_faces_from_prop(py::object prop)
{
// It is probably not worth implementing an additional layer of caching here
// as findfont already has its cache and object equality needs would also
// need to go through Python anyways.
auto const& fm =
py::module::import("matplotlib.font_manager").attr("fontManager");
if (py::hasattr(fm, "_find_fonts_by_props")) {
auto const& paths =
fm.attr("_find_fonts_by_props")(prop).cast<std::vector<std::string>>();
auto fonts = std::vector<cairo_font_face_t*>{};
for (auto const& path: paths) {
fonts.push_back(font_face_from_path(path));
}
return fonts;
} else {
auto const& path = fm.attr("findfont")(prop);
return {font_face_from_path(path)};
}
}
long get_hinting_flag()
{
// FIXME[matplotlib]: Should be moved out of backend_agg.
auto const& hf =
py::module::import("matplotlib.backends.backend_agg")
.attr("get_hinting_flag")();
// Matplotlib 3.10 changed the flag from int to enum.Flag.
return (py::hasattr(hf, "value") ? hf.attr("value") : hf).cast<long>();
}
void adjust_font_options(cairo_t* cr, bool subpixel_antialiased_text_allowed)
{
auto const& font_face = cairo_get_font_face(cr);
auto const& options = cairo_font_options_create();
if (cairo_version() >= CAIRO_VERSION_ENCODE(1, 18, 0)
// cairo#404 (<1.18.0): Don't set antialiasing for color fonts.
|| !cairo_font_face_get_user_data(font_face, &detail::IS_COLOR_FONT_KEY)) {
auto aa = rc_param("text.antialiased"); // Normally *exactly* a bool.
cairo_font_options_set_antialias(
options,
aa.ptr() == Py_True
? (subpixel_antialiased_text_allowed
? CAIRO_ANTIALIAS_SUBPIXEL : CAIRO_ANTIALIAS_GRAY)
: aa.ptr() == Py_False ? CAIRO_ANTIALIAS_NONE
: aa.cast<cairo_antialias_t>());
}
auto const& variations = *static_cast<std::string*>(
cairo_font_face_get_user_data(font_face, &detail::VARIATIONS_KEY));
if (!variations.empty()) {
if (detail::cairo_font_options_set_variations) {
detail::cairo_font_options_set_variations(options, variations.c_str());
} else {
py::module::import("warnings").attr("warn")(
"cairo_font_options_set_variations requires cairo>=1.16.0");
}
}
// The hint style is not set here: it is passed directly as load_flags to
// cairo_ft_font_face_create_for_ft_face.
cairo_set_font_options(cr, options);
cairo_font_options_destroy(options);
}
void warn_on_missing_glyph(std::string s)
{
PY_CHECK(
PyErr_WarnEx,
PyExc_UserWarning,
"Glyph {} missing from current font."_format(s)
.cast<std::string>().c_str(),
1);
}
// TODO: Implement font fallback per
// https://www.mail-archive.com/harfbuzz@lists.freedesktop.org/msg04131.html
// https://tex.stackexchange.com/questions/520034/fallback-for-harfbuzz-fonts#comment1315285_520048
GlyphsAndClusters text_to_glyphs_and_clusters(cairo_t* cr, std::string s)
{
auto const& scaled_font = cairo_get_scaled_font(cr);
auto gac = GlyphsAndClusters{};
if (has_raqm()) {
auto const& ft_face = cairo_ft_scaled_font_lock_face(scaled_font);
[[maybe_unused]] auto const& scaled_font_unlock_cleanup =
std::unique_ptr<
std::remove_pointer_t<cairo_scaled_font_t>,
decltype(&cairo_ft_scaled_font_unlock_face)>{
scaled_font, cairo_ft_scaled_font_unlock_face};
auto const& rq = raqm::create();
[[maybe_unused]] auto const& rq_cleanup =
std::unique_ptr<std::remove_pointer_t<raqm_t>, decltype(raqm::destroy)>{
rq, raqm::destroy};
if (!rq) {
throw std::runtime_error{"failed to compute text layout"};
}
TRUE_CHECK(raqm::set_text_utf8, rq, s.c_str(), s.size());
TRUE_CHECK(raqm::set_freetype_face, rq, ft_face);
for (auto const& feature:
*static_cast<std::vector<std::string>*>(
cairo_font_face_get_user_data(
cairo_get_font_face(cr), &detail::FEATURES_KEY))) {
TRUE_CHECK(raqm::add_font_feature, rq, feature.c_str(), -1);
}
for (auto const& [lang, start, stop]:
*static_cast<std::vector<std::tuple<std::string, int, int>>*>(
cairo_font_face_get_user_data(
cairo_get_font_face(cr), &detail::LANGS_KEY))) {
TRUE_CHECK(raqm::set_language, rq, lang.c_str(), start, stop);
}
TRUE_CHECK(raqm::layout, rq);
auto num_glyphs = size_t{};
auto const& rq_glyphs = raqm::get_glyphs(rq, &num_glyphs);
// With old versions of FreeType and raqm, glyphs of color fonts end up way
// too big (raqm#123). However, shaping is actually necessary to handle
// various ligature emojis, and if only a single glyph is to be drawn, then
// things are actually OK (even metrics-wise), so allow that case.
if (raqm::bad_color_glyph_spacing
&& num_glyphs > 1
&& cairo_font_face_get_user_data(cairo_get_font_face(cr),
&detail::IS_COLOR_FONT_KEY)) {
goto no_raqm;
}
gac.num_glyphs = num_glyphs;
gac.glyphs = cairo_glyph_allocate(gac.num_glyphs);
auto x = 0., y = 0.;
for (auto i = 0; i < gac.num_glyphs; ++i) {
auto const& rq_glyph = rq_glyphs[i];
gac.glyphs[i].index = rq_glyph.index;
gac.glyphs[i].x = x + rq_glyph.x_offset / 64.;
x += rq_glyph.x_advance / 64.;
gac.glyphs[i].y = -(y + rq_glyph.y_offset / 64.);
y += rq_glyph.y_advance / 64.;
}
// raqm returns glyphs left-to-right but cairo wants them in logical order.
if (num_glyphs