-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtest.cpp
2655 lines (2554 loc) · 115 KB
/
test.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
// canvas_ity test suite and harness v1.00 -- ISC license
// Copyright (c) 2022 Andrew Kensler
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// ======== ABOUT ========
//
// This program contains both the test suite and a standalone test harness
// for automated testing of the canvas_ity library. The harness calls each
// test with a fresh instance of a canvas. The test then exercises the public
// interface of the canvas. After it returns, the harness fetches the image
// of the canvas and hashes the contents to compare against an expected hash
// to determine if the test passed or fails.
//
// To build the test program, either just compile the one source file
// directly to an executable with a C++ compiler, e.g.:
// g++ -O3 -o test test.cpp
// or else use the accompanying CMake file. The CMake file enables extensive
// warnings and also offers targets for static analysis, dynamic analysis,
// measuring code size, and measuring test coverage.
//
// By default, the test harness simply runs each test once and reports the
// results. However, with command line arguments, it can write PNG images
// of the test results, run tests repeatedly to benchmark them, run just a
// subset of the test, or write out a new table of expected image hashes.
// Run the program with --help to see the usage guide for more on these.
//
// Beware that while the hash checks are tuned to allow tests to pass even
// with minor numerical differences due to aggressive compiler optimizations
// (e.g., -Ofast on certain architectures), some tests may still report as
// failing. This does not necessarily mean that there is a problem, but it
// does warrant manual verification of the failing test's image against a
// passing baseline test image produced with optimizations disabled.
//
// Also see test.html, the HTML5 2D canvas port of these tests. Compare
// the code for the C++ and JavaScript tests line-by-line to see how this
// library's API maps to the HTML5 API and vice-versa. Compare the images
// produced by each (run with --pngs to get the images) to see how the
// library's rendering relates to browser canvas implementations.
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#define _CRT_SECURE_NO_WARNINGS
#endif
#define CANVAS_ITY_IMPLEMENTATION
#include "../src/canvas_ity.hpp"
#if defined( __linux__ )
#include <time.h>
#include <unistd.h>
#elif defined( _WIN32 )
#include <windows.h>
#elif defined( __MACH__ )
#include <mach/mach_time.h>
#include <unistd.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace canvas_ity;
using namespace std;
// ======== RESOURCES ========
//
// The resources embedded here are mainly font files in TTF form. While
// the data could be stored directly as an array of integer literals, the
// Base64 encoding is much more compact in terms of source. It also means
// that the HTML5 port of these tests can use these resources almost as-is.
//
// The valid fonts all have the following properties in common:
//
// - The asterisk is a composite of the acute mark with a mix of simple and
// complex (i.e., 2x2) scaling transforms used to rotate them to the
// eight principal directions at 45-degree angles.
// - A glyph assigned to the high end of the private use area (at 10FFFD)
// tests all combinations of four on-curve or off-curve points.
// - Characters 'D' through 'H' are copies of a simple dot and assigned to
// the last glyph indices; being copies and at the end means that they can
// test the hmtx table having fewer horizontal metrics than glyphs, with
// the last advance width being replicated to all the glyphs beyond it.
// - Having assignments to 'C' through 'I', but in non-consecutive order also
// allows for testing the range table in the format-4 cmap subtable.
// - The 'a' glyph has a sequence of off-curve points going in the same
// direction so that the points have a consecutive sequence of identical
// flags that are compacted with a repeat flag.
// - The 's' glyph is translated 1024 font units to the right. However, it
// has a normal advance width and left side bearing so it should render
// like a normal character despite this.
// - The .notdef glyph has a couple of hinting instructions that just push a
// few values on the stack but do nothing else. These instructions must
// be skipped over to get to the point data in the glyph.
// Valid TTF file, cmap table has types 12 and 4 subtables.
vector< unsigned char > font_a;
char const font_a_base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AAZD+cAIAAZD+cAIAAZD+cPoAAZD+cAIAAZD+cAIAAZD+cAIAAZD+cPoAAZD+cAIAAZD+"
"cAIAAZD+cAIAAZD+cPoAAZD+cAIAAZD+cAIAAZD+cAIAAZD+cAD//wBkADIDIAWqECcAEgAA"
"/qIABAASAAP//wBkAZADIARMEAYAEgAA//8AZAGQAyAETBAGABIAAP//AGQBkAMgBEwQBgAS"
"AAAAAQBkAZADIARMAAMAABIgECBkArz9RARM/UQAAAAAAAAMAJYAAQAAAAAAAQAFAAAAAQAA"
"AAAAAgAHAAUAAQAAAAAAAwAFAAAAAQAAAAAABAAFAAAAAQAAAAAABQALAAwAAQAAAAAABgAF"
"AAAAAwABBAkAAQAKABcAAwABBAkAAgAOACEAAwABBAkAAwAKABcAAwABBAkABAAKABcAAwAB"
"BAkABQAWAC8AAwABBAkABgAKABdGb250QVJlZ3VsYXJWZXJzaW9uIDEuMABGAG8AbgB0AEEA"
"UgBlAGcAdQBsAGEAcgBWAGUAcgBzAGkAbwBuACAAMQAuADAAAAMAAAAAAAD/ZgBmAAAAAAAA"
"AAAAAAAAAAAAAAAAAAA=";
// Valid TTF file, cmap table has type 4 subtable only and loca table is long.
vector< unsigned char > font_b;
char const font_b_base64[] =
"AAEAAAALAIAAAwAwT1MvMmirdVEAAAE4AAAAYGNtYXAHhQC5AAAB3AAAAIJjdnQgAEQFEQAA"
"AmAAAAAEZ2x5ZjCUlAIAAAK0AAAGhmhlYWQe1LMzAAAAvAAAADZoaGVhDf8FBAAAAPQAAAAk"
"aG10eDmaBAMAAAGYAAAARGxvY2EAAEj6AAACZAAAAFBtYXhwAHUAtwAAARgAAAAgbmFtZVZp"
"OPsAAAk8AAAA23Bvc3T/aQBmAAAKGAAAACAAAAEAAAEAAIakcHRfDzz1AB0IAAAAAADcB1gv"
"AAAAANwUDqb/+f5tB5AH8wAAAAgAAgABAAAAAAABAAAFu/+6ALgIAP/5/ToHkAABAAAAAAAA"
"AAAAAAAAAAAADwABAAAAEwBAAB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cAIAAZD+cAIAAZD+cAIAAZD+cPoAAZD+"
"cAIAAZD+cAIAAZD+cAIAAZD+cPoAAZD+cAIAAZD+cAIAAZD+cAIAAZD+cAD//wBkADIDIAWq"
"ECcAEgAA/qIABAASAAP//wBkAZADIARMEAYAEgAA//8AZAGQAyAETBAGABIAAP//AGQBkAMg"
"BEwQBgASAAAAAQBkAZADIARMAAMAABIgECBkArz9RARM/UQAAAAAAAAMAJYAAQAAAAAAAQAF"
"AAAAAQAAAAAAAgAHAAUAAQAAAAAAAwAFAAAAAQAAAAAABAAFAAAAAQAAAAAABQALAAwAAQAA"
"AAAABgAFAAAAAwABBAkAAQAKABcAAwABBAkAAgAOACEAAwABBAkAAwAKABcAAwABBAkABAAK"
"ABcAAwABBAkABQAWAC8AAwABBAkABgAKABdGb250Q1JlZ3VsYXJWZXJzaW9uIDEuMABGAG8A"
"bgB0AEMAUgBlAGcAdQBsAGEAcgBWAGUAcgBzAGkAbwBuACAAMQAuADAAAAMAAAAAAAD/ZgBm"
"AAAAAAAAAAAAAAAAAAAAAAAAAAA=";
// Invalid TTF file, valid magic number but only one byte after that.
vector< unsigned char > font_d;
char const font_d_base64[] =
"AAEAAAA=";
// Invalid TTF file, offset table cut short.
vector< unsigned char > font_e;
char const font_e_base64[] =
"AAEAAAALAIAAAwAwT1MvMmisck8AAAE4AAAAYGNtYXAXewGCAAAB3AAAAUJjdnQgAEQFEQAA"
"AyAAAAAEZ2x5ZjCUlAIAAANMAAAGhmhlYWQe1bIjAAAAvAAAADZoaGVhDf8FBAAAAPQAAAAk"
"aG10eDmaBAMAAAGYAAAARGxvY2ERbxMOAAADJAAAAChtYXhwAHUAtwAAARgAAAAgbmFtZVZp"
"NvsAAAnUAAAA23Bvc3T/aQBmAAAKsAAAAA==";
// Invalid TTF file, offset table is complete but points to missing tables.
vector< unsigned char > font_f;
char const font_f_base64[] =
"AAEAAAALAIAAAwAwT1MvMmisck8AAAE4AAAAYGNtYXAXewGCAAAB3AAAAUJjdnQgAEQFEQAA"
"AyAAAAAEZ2x5ZjCUlAIAAANMAAAGhmhlYWQe1bIjAAAAvAAAADZoaGVhDf8FBAAAAPQAAAAk"
"aG10eDmaBAMAAAGYAAAARGxvY2ERbxMOAAADJAAAAChtYXhwAHUAtwAAARgAAAAgbmFtZVZp"
"NvsAAAnUAAAA23Bvc3T/aQBmAAAKsAAAACAAAQAAAAEAAEPW4v5fDzz1AB0IAAAAAADcB1gv"
"AAAAANwUDpf/+f5tB5AH8wAAAAgAAgAAAAAAAA==";
// Invalid TTF file, lacking most required TTF tables except for head.
vector< unsigned char > font_g;
char const font_g_base64[] =
"AAEAAAABABAAAAAAaGVhZB7h+0cAAAAcAAAANgABAAAAAQAAC4VXZl8PPPUAHQgAAAAAANwH"
"WC8AAAAA3CBXu//5/m0HkAfzAAAACAACAAAAAAAA";
// ======== TEST SUITE ========
//
// Note that while individual tests only use the same public interface
// as clients normally would, most tests are not good examples of the
// ordinary use of the library. They tend to make superfluous calls, make
// calls in unusual orders, and assume documented implicit behavior. They
// typically test strict conformance to the W3C (not WHATWG) HTML5 2D canvas
// specification (https://www.w3.org/TR/2015/REC-2dcontext-20151119/).
//
// For better examples of normal use of the library, see the tests prefixed
// with "example_". These are written in more orthodox ways and intended to
// demonstrate interesting things that the library can draw.
//
// To add a new test to the suite, write a function for it here with the same
// function signature as the other tests, and then register it below in the
// harness's table of tests to run. (Remember to also port it to test.html!)
namespace
{
void scale_uniform( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
float segments[] = { 1.0f };
that.set_line_dash( segments, 1 );
that.line_cap = circle;
for ( float size = 8.0f; size < min( width, height ); size *= 2.0f )
{
that.scale( 2.0f, 2.0f );
that.stroke_rectangle( 0.0f, 0.0f, 8.0f, 8.0f );
}
}
void scale_non_uniform( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
float segments[] = { 4.0f };
that.set_line_dash( segments, 1 );
that.scale( 4.0f, 0.5f );
that.stroke_rectangle( width * 0.125f / 4.0f, height * 0.125f / 0.5f,
width * 0.75f / 4.0f, height * 0.75f / 0.5f );
}
void rotate( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
for ( int step = 0; step < 64; ++step )
{
that.rotate( 3.14159265f / 2.0f / 64.0f );
that.stroke_rectangle( 0.0f, 0.0f, width, height );
}
}
void translate( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
for ( float step = 0.0f; step < 32.0f; step += 1.0f )
{
that.translate( ( 0.5f - step / 32.0f ) * width * 0.2f,
height / 32.0f );
that.begin_path();
that.arc( 0.0f, 0.0f, width * 0.125f, 0.0f, 6.28318531f );
that.close_path();
that.stroke();
}
}
void transform( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
for ( int step = 0; step < 8; ++step )
{
that.transform( 1.0f, 0.0f, 0.1f, 1.0f, width * -0.05f, 0.0f );
that.stroke_rectangle( width * 0.25f, height * 0.25f,
width * 0.5f, height * 0.5f );
}
}
void transform_fill( canvas &that, float width, float height )
{
unsigned char checker[ 1024 ];
for ( int index = 0; index < 1024; ++index )
checker[ index ] = static_cast< unsigned char >(
( ( index >> 5 & 1 ) ^ ( index >> 9 & 1 ) ^
( ( index & 3 ) == 3 ) ) * 255 );
that.set_pattern( fill_style, checker, 16, 16, 64, repeat );
that.begin_path();
that.rectangle( width * 0.2f, height * 0.2f,
width * 0.6f, height * 0.6f );
that.transform( 1.0f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f );
that.fill();
}
void transform_stroke( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 8.0f );
float segments[] = { 22.0f, 8.0f, 10.0f, 8.0f };
that.set_line_dash( segments, 4 );
that.begin_path();
that.arc( width * 0.5f, height * 0.5f, min( width, height ) * 0.4f,
0.0f, 6.28318531f );
that.close_path();
that.transform( 1.0f, 1.0f, 0.0f, 2.0f, 0.0f, 0.0f );
that.stroke();
}
void set_transform( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
for ( int step = 0; step < 8; ++step )
that.set_transform( 1.0f, 0.0f, 0.1f, 1.0f, width * -0.05f, 0.0f );
that.stroke_rectangle( width * 0.25f, height * 0.25f,
width * 0.5f, height * 0.5f );
}
void global_alpha( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 3.0f );
for ( float y = 0.0f; y < 6.0f; y += 1.0f )
for ( float x = 0.0f; x < 6.0f; x += 1.0f )
{
that.set_color( fill_style, x / 5.0f, 1.0f, y / 5.0f, x / 5.0f );
that.set_global_alpha( y / 4.0f - 0.25f );
that.begin_path();
that.rectangle(
( x + 0.1f ) / 6.0f * width, ( y + 0.1f ) / 6.0f * height,
0.8f / 6.0f * width, 0.8f / 6.0f * height );
that.fill();
that.stroke();
}
}
void global_composite_operation( canvas &that, float width, float height )
{
composite_operation const operations[] = {
source_in, source_copy, source_out, destination_in,
destination_atop, lighter, destination_over, destination_out,
source_atop, source_over, exclusive_or };
float box_width = 0.25f * width;
float box_height = 0.25f * height;
for ( int index = 0; index < 11; ++index )
{
float column = static_cast< float >( operations[ index ] % 4 );
float row = static_cast< float >( operations[ index ] / 4 );
that.save();
that.begin_path();
that.rectangle( column * box_width, row * box_height,
box_width, box_height );
that.clip();
that.set_color( fill_style, 0.0f, 0.0f, 1.0f, 1.0f );
that.fill_rectangle( ( column + 0.4f ) * box_width,
( row + 0.4f ) * box_height,
0.4f * box_width, 0.4f * box_height );
that.global_composite_operation = operations[ index ];
that.set_color( fill_style, 1.0f, 0.0f, 0.0f, 1.0f );
that.fill_rectangle( ( column + 0.2f ) * box_width,
( row + 0.2f ) * box_height,
0.4f * box_width, 0.4f * box_height );
that.restore();
}
}
void shadow_color( canvas &that, float width, float height )
{
that.shadow_offset_x = 5.0f;
that.shadow_offset_y = 5.0f;
that.set_shadow_blur( 1.0f );
for ( float row = 0.0f; row < 5.0f; row += 1.0f )
{
float y = ( row + 0.25f ) * 0.2f * height;
that.set_color( fill_style, 0.0f, 0.0f, 0.0f, 0.25f * row );
that.set_shadow_color( 1.0f, -1.0f, 0.0f, 0.25f );
that.fill_rectangle( 0.05f * width, y,
0.15f * width, 0.1f * height );
that.set_shadow_color( 0.0f, 1.0f, 0.0f, 0.5f );
that.fill_rectangle( 0.30f * width, y,
0.15f * width, 0.1f * height );
that.set_shadow_color( 0.0f, 0.0f, 2.0f, 0.75f );
that.fill_rectangle( 0.55f * width, y,
0.15f * width, 0.1f * height );
that.set_shadow_color( 1.0f, 1.0f, 1.0f, 100.0f );
that.fill_rectangle( 0.80f * width, y,
0.15f * width, 0.1f * height );
}
}
void shadow_offset( canvas &that, float width, float height )
{
that.set_shadow_blur( 2.0f );
that.set_color( fill_style, 1.0f, 1.0f, 1.0f, 1.0f );
that.set_shadow_color( 0.0f, 0.0f, 0.0f, 1.0f );
for ( float y = 0.0f; y < 5.0f; y += 1.0f )
for ( float x = 0.0f; x < 5.0f; x += 1.0f )
{
that.shadow_offset_x = ( x - 2.0f ) * 4.0f;
that.shadow_offset_y = ( y - 2.0f ) * 4.0f;
that.fill_rectangle( ( x + 0.25f ) * 0.2f * width,
( y + 0.25f ) * 0.2f * height,
0.1f * width, 0.1f * height );
}
}
void shadow_offset_offscreen( canvas &that, float width, float height )
{
that.shadow_offset_x = width;
that.set_color( fill_style, 1.0f, 0.0f, 0.0f, 1.0f );
that.set_shadow_color( 0.0f, 0.0f, 0.0f, 0.5f );
that.fill_rectangle( width * -0.6875f, height * 0.0625f,
width * 0.375f, height * 0.375f );
that.begin_path();
that.arc( width * 0.5f, height * 0.75f, min( width, height ) * 0.2f,
0.0f, 6.28318531f );
that.close_path();
that.fill();
}
void shadow_blur( canvas &that, float width, float height )
{
that.set_color( fill_style, 1.0f, 1.0f, 1.0f, 1.0f );
that.set_shadow_color( 0.0f, 0.0f, 0.0f, 1.0f );
that.shadow_offset_x = 5.0f;
that.shadow_offset_y = 5.0f;
for ( float x = 0.0f; x < 5.0f; x += 1.0f )
for ( float y = 4.0f; y >= 0.0f; y -= 1.0f )
{
that.set_shadow_blur( ( y * 5.0f + x ) * 0.5f - 0.5f );
that.fill_rectangle( ( x + 0.25f ) * 0.2f * width,
( y + 0.25f ) * 0.2f * height,
0.1f * width, 0.1f * height );
}
}
void shadow_blur_offscreen( canvas &that, float width, float height )
{
that.set_color( fill_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_shadow_color( 0.0f, 0.0f, 0.0f, 1.0f );
that.set_shadow_blur( 5.0f );
that.fill_rectangle( 0.0f, height * 2.0f, width, height );
that.fill_rectangle( 0.0f, height * -2.0f, width, height );
that.fill_rectangle( width + 1.0f, 0.0f, width, height );
that.fill_rectangle( -width - 1.0f, 0.0f, width, height );
}
void shadow_blur_composite( canvas &that, float width, float height )
{
float radius = min( width, height ) * 0.5f;
that.arc( 0.5f * width, 0.5f * height, radius, 0.0f, 6.28318531f );
that.clip();
that.set_color( fill_style, 0.0f, 0.0f, 1.0f, 1.0f );
that.set_shadow_color( 0.0f, 0.0f, 0.0f, 1.0f );
that.fill_rectangle( 0.4f * width, 0.0f, 0.2f * width, height );
that.global_composite_operation = destination_atop;
that.set_color( stroke_style, 1.0f, 0.0f, 0.0f, 1.0f );
float dashing[] = { 16.0f, 4.0f };
that.set_line_dash( dashing, 2 );
that.set_line_width( 15.0f );
that.shadow_offset_x = 5.0f;
that.shadow_offset_y = 5.0f;
that.set_shadow_blur( 6.0f );
that.set_shadow_color( 0.0f, 0.0f, 0.0f, 1.0f );
that.begin_path();
that.arc( 0.45f * width, 0.85f * height, radius * 0.5f, 0.0f, 6.28318531f );
that.close_path();
that.stroke();
that.global_composite_operation = source_over;
that.begin_path();
that.arc( 0.75f * width, 0.25f * height, radius, 0.0f, 6.28318531f );
that.close_path();
that.stroke();
}
void line_width( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 4.0f );
for ( float step = 0.0f; step < 16.0f; step += 1.0f )
{
float left = ( step + 0.25f ) / 16.0f * width;
float right = ( step + 0.75f ) / 16.0f * width;
that.begin_path();
that.move_to( left, 0.0f );
that.bezier_curve_to( left, 0.5f * height,
right, 0.5f * height,
right, height );
that.set_line_width( 0.5f * ( step - 1 ) );
that.stroke();
}
that.set_color( fill_style, 1.0f, 1.0f, 1.0f, 1.0f );
that.global_composite_operation = source_atop;
that.fill_rectangle( 0.0f, 0.5f * height, width, 0.5f * height );
that.global_composite_operation = destination_over;
that.fill_rectangle( 0.0f, 0.25f * height, width, 0.25f * height );
that.set_color( fill_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.fill_rectangle( 0.0f, 0.5f * height, width, 0.25f * height );
}
void line_width_angular( canvas &that, float width, float height )
{
for ( float step = 0.0f; step < 5.0f; step += 1.0f )
{
float grey = ( step + 1.0f ) / 5.0f;
that.set_color( stroke_style, grey, grey, grey, 1.0f );
that.begin_path();
that.move_to( 0.1f * width, 0.1f * height );
that.bezier_curve_to( 1.2f * width, 1.0f * height,
1.2f * width, -0.0f * height,
0.1f * width, 0.9f * height );
that.set_line_width( 30.0f - 7.0f * step );
that.stroke();
}
}
void line_cap( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 24.0f );
cap_style const caps[] = { butt, square, circle };
for ( int index = 0; index < 3; ++index )
{
float right = static_cast< float >( index + 1 ) / 3.0f * width - 20.0f;
that.begin_path();
that.move_to( right, 0.125f * height );
that.bezier_curve_to( right, 0.125f * height + 100.0f,
right - 100.0f, 0.875f * height,
right, 0.875f * height );
that.line_cap = caps[ index ];
that.stroke();
}
}
void line_cap_offscreen( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 36.0f );
cap_style const caps[] = { butt, square, circle };
for ( int index = 0; index < 3; ++index )
{
float x = ( static_cast< float >( index ) + 0.5f ) / 3.0f * width;
float y = ( static_cast< float >( index ) + 0.5f ) / 3.0f * height;
that.begin_path();
that.move_to( x, -19.0f );
that.line_to( x, -9.0f );
that.move_to( x, height + 17.0f );
that.line_to( x, height + 27.0f );
that.move_to( -27.0f, y );
that.line_to( -17.0f, y );
that.move_to( width + 9.0f, y );
that.line_to( width + 19.0f, y );
that.line_cap = caps[ index ];
that.stroke();
}
}
void line_join( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 16.0f );
join_style const joins[] = { miter, bevel, rounded };
for ( int index = 0; index < 3; ++index )
{
float left = ( static_cast< float >( index ) + 0.25f ) / 3.0f * width;
float right = ( static_cast< float >( index ) + 0.75f ) / 3.0f * width;
that.begin_path();
that.move_to( left, 0.2f * height );
that.line_to( left, 0.1f * height );
that.line_to( left, 0.2f * height );
that.line_to( right, 0.2f * height );
that.line_to( left, 0.2f * height );
that.line_to( left, 0.3f * height );
that.line_to( right, 0.3f * height );
that.line_to( right, 0.4f * height );
that.line_to( right, 0.5f * height );
that.line_to( left, 0.4f * height );
that.line_to( left, 0.5f * height );
that.line_to( right, 0.6f * height );
that.bezier_curve_to( right, height,
left, 0.4f * height,
left, 0.7f * height );
that.bezier_curve_to( left, 0.8f * height,
right, 0.8f * height,
right, 0.9f * height );
that.line_join = joins[ index ];
that.stroke();
}
}
void line_join_offscreen( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 36.0f );
join_style const joins[] = { miter, bevel, rounded };
for ( int index = 0; index < 3; ++index )
{
float x = ( static_cast< float >( index ) + 0.5f ) / 3.0f * width;
float y = ( static_cast< float >( index ) + 0.5f ) / 3.0f * height;
that.begin_path();
that.move_to( x - 10.0f, -55.0f );
that.line_to( x - 10.0f, -5.0f );
that.line_to( x + 10.0f, -55.0f );
that.move_to( x - 10.0f, height + 130.0f );
that.line_to( x + 10.0f, height + 80.0f );
that.line_to( x + 10.0f, height + 130.0f );
that.move_to( -130.0f, y - 10.0f );
that.line_to( -80.0f, y - 10.0f );
that.line_to( -130.0f, y + 10.0f );
that.move_to( height + 55.0f, y - 10.0f );
that.line_to( height + 5.0f, y + 10.0f );
that.line_to( height + 55.0f, y + 10.0f );
that.line_join = joins[ index ];
that.stroke();
}
}
void miter_limit( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
for ( float line = 0.0f; line < 4.0f; line += 1.0f )
{
that.set_line_width( 1.5f * line + 1.0f );
that.set_miter_limit( 20.0f );
for ( float limit = 0.0f; limit < 8.0f; limit += 1.0f )
{
float left = ( limit + 0.2f ) / 8.0f * width;
float middle = ( limit + 0.5f ) / 8.0f * width;
float right = ( limit + 0.7f ) / 8.0f * width;
float top = ( line + 0.3f ) / 4.0f * height;
float bottom = ( line + 0.7f ) / 4.0f * height;
that.begin_path();
that.move_to( left, bottom );
that.line_to( left, top );
that.line_to( right, bottom );
that.line_to( middle, top );
that.set_miter_limit( 1.5f * limit );
that.stroke();
}
}
}
void line_dash_offset( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 6.0f );
float segments[] = { 20.0f, 8.0f, 8.0f, 8.0f };
that.set_line_dash( segments, 4 );
for ( float step = 0.0f; step < 16.0f; step += 1.0f )
{
float left = ( step + 0.125f ) / 16.0f * width;
float right = ( step + 0.875f ) / 16.0f * width;
that.begin_path();
that.move_to( left, 0.0f );
that.line_to( right, 0.125f * height );
that.line_to( left, 0.375f * height );
that.line_to( right, 0.625f * height );
that.line_to( left, 0.875f * height );
that.line_to( right, height );
that.line_dash_offset = ( step / 16.0f - 0.5f ) * 44.0f;
that.stroke();
}
}
void line_dash( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 6.0f );
float segments_1[] = { 10.0f };
that.set_line_dash( segments_1, 1 );
that.stroke();
that.move_to( 0.0f, 0.0f );
that.stroke();
that.begin_path();
that.move_to( width * 0.25f, 0.0f );
that.line_to( width * 0.25f, height );
that.stroke();
float segments_2[] = { 20.0f, -8.0f };
that.set_line_dash( segments_2, 2 );
that.begin_path();
that.move_to( width * 0.375f, 0.0f );
that.line_to( width * 0.375f, height );
that.stroke();
float segments_3[] = { 20.0f, 8.0f, 8.0f, 8.0f };
that.set_line_dash( segments_3, 4 );
that.begin_path();
that.move_to( width * 0.5f, 0.0f );
that.line_to( width * 0.5f, height );
that.stroke();
float segments_4[] = { 0.0f, 8.0f, 2.0f, 8.0f };
that.set_line_dash( segments_4, 4 );
that.begin_path();
that.move_to( width * 0.625f, 0.0f );
that.line_to( width * 0.625f, height );
that.stroke();
that.set_line_dash( 0, 0 );
that.begin_path();
that.move_to( width * 0.75f, 0.0f );
that.line_to( width * 0.75f, height );
that.stroke();
}
void line_dash_closed( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 32.0f );
float segments_1[] = { 96.0f, 32.0f };
that.set_line_dash( segments_1, 2 );
that.line_dash_offset = -80.0f;
that.stroke_rectangle( 0.25f * width, 0.25f * height,
0.5f * width, 0.5f * height );
float segments_2[] = { 96.0f, 32.0f, 1024.0f, 16.0f };
that.set_line_dash( segments_2, 4 );
that.line_dash_offset = 128.0f;
that.stroke_rectangle( 0.09375f * width, 0.09375f * height,
0.8125f * width, 0.8125f * height );
}
void line_dash_overlap( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_color( fill_style, 1.0f, 0.0f, 0.0f, 1.0f );
that.line_cap = circle;
that.set_line_width( 16.0f );
float segments[] = { 14.0f, 12.0f };
that.set_line_dash( segments, 2 );
for ( int index = 0; index < 4; ++index )
{
float flip = ( index == 3 ? -1.0f : 1.0f );
float top_y = ( index & 1 ? 0.25f : 0.1f ) * height;
float bottom_y = ( index & 1 ? 0.9f : 0.75f ) * height;
float mid_x = ( index & 2 ? 0.75f : 0.25f ) * width;
float top_width = ( index & 1 ? 0.25f : 0.55f ) * flip * width;
float bottom_width = ( index & 1 ? 0.55f : 0.25f ) * flip * width;
that.move_to( mid_x, top_y );
that.bezier_curve_to( mid_x - top_width, top_y,
mid_x + bottom_width, bottom_y,
mid_x, bottom_y );
that.bezier_curve_to( mid_x - bottom_width, bottom_y,
mid_x + top_width, top_y,
mid_x, top_y );
that.close_path();
}
that.fill();
that.stroke();
}
void line_dash_offscreen( canvas &that, float width, float height )
{
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_line_width( 6.0f );
float segments[] = {
0.0f, width * 20.5f * 3.14159265f - height * 0.5f + 1.0f,
height - 2.0f, 0.0f };
that.set_line_dash( segments, 4 );
for ( float step = -2.0f; step <= 2.0f; step += 1.0f )
{
that.begin_path();
that.arc( width * -20.0f, height * 0.5f,
width * ( 20.5f - step * 0.1f ),
3.14159265f, 1.5707963268f );
that.line_dash_offset = width * step * 0.1f * 3.14159265f;
that.stroke();
}
}
void color( canvas &that, float width, float height )
{
float radius = min( width, height ) * 0.4f;
that.set_color( fill_style, 2.0f, -1.0f, 0.0f, 0.5f );
that.set_color( stroke_style, 0.0f, 0.0f, 1.0f, 1.5f );
that.set_line_width( 16.0f );
that.arc( 0.5f * width, 0.5f * height, radius, 0.0f, 6.28318531f );
that.close_path();
that.fill();
that.stroke();
}
void linear_gradient( canvas &that, float width, float height )
{
float radius = min( width, height ) * 0.4f;
that.set_linear_gradient( fill_style,
0.3f * width, 0.3f * height,
0.7f * width, 0.7f * height );
that.add_color_stop( fill_style, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f );
that.add_color_stop( fill_style, 1.0f, 1.0f, 0.0f, 1.0f, 100.0f );
that.set_linear_gradient( stroke_style,
0.3f * width, 0.7f * height,
0.7f * width, 0.3f * height );
that.add_color_stop( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f, 0.5f );
that.add_color_stop( stroke_style, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f );
that.set_line_width( 16.0f );
that.arc( 0.5f * width, 0.5f * height, radius, 0.0f, 6.28318531f );
that.close_path();
that.fill();
that.stroke();
that.set_linear_gradient( stroke_style,
0.5f * width, 0.5f * height,
0.5f * width, 0.5f * height );
that.add_color_stop( stroke_style, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f );
that.add_color_stop( stroke_style, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f );
that.stroke();
}
void radial_gradient( canvas &that, float width, float height )
{
float radius = min( width, height ) * 0.4f;
that.set_radial_gradient( fill_style,
0.0f, 0.0f, radius,
width, height, 0.5f * radius );
that.add_color_stop( fill_style, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f );
that.add_color_stop( fill_style, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f );
that.set_radial_gradient( stroke_style,
0.0f, height, radius,
width, 0.0f, 0.5f * radius );
that.add_color_stop( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f, 0.5f );
that.add_color_stop( stroke_style, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f );
that.set_line_width( 16.0f );
that.arc( 0.5f * width, 0.5f * height, radius, 0.0f, 6.28318531f );
that.close_path();
that.fill();
that.stroke();
that.set_radial_gradient( stroke_style,
0.5f * width, 0.4f * height, 10.0f,
0.5f * width, 0.6f * height, 0.0f );
that.set_radial_gradient( stroke_style,
0.0f, 0.5f * height, -10.0f,
width, 0.5f * height, 10.0f );
that.add_color_stop( stroke_style, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f );
that.add_color_stop( stroke_style, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f );
that.stroke();
that.set_radial_gradient( fill_style,
0.5f * width, 0.5f * height, 0.0f,
0.5f * width, 0.5f * height, radius );
that.add_color_stop( fill_style, 0.15f, 0.0f, 0.0f, 0.0f, 1.0f );
that.add_color_stop( fill_style, 0.20f, 0.0f, 0.0f, 0.0f, 0.0f );
that.fill();
}
void color_stop( canvas &that, float width, float height )
{
that.add_color_stop( fill_style, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f );
that.set_linear_gradient( fill_style,
0.1f * width, 0.0f,
0.9f * width, 0.0f );
that.fill_rectangle( 0.0f, 0.0f, width, 0.1f * height );
that.add_color_stop( fill_style, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f );
that.add_color_stop( fill_style, 2.0f, 1.0f, 0.0f, 0.0f, 1.0f );
that.add_color_stop( fill_style, 0.3f, -1.0f, 0.0f, 2.0f, 2.0f );
that.add_color_stop( fill_style, 0.3f, 1.0f, 1.0f, 1.0f, 1.0f );
that.add_color_stop( fill_style, 0.3f, 0.0f, 0.0f, 0.0f, 1.0f );
that.add_color_stop( fill_style, 0.0f, 0.0f, 0.0f, 0.8f, 1.0f );
that.add_color_stop( fill_style, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f );
that.add_color_stop( fill_style, 0.7f, 0.9f, 0.9f, 0.9f, 1.0f );
that.add_color_stop( fill_style, 0.6f, 0.1f, 0.1f, 0.1f, 1.0f );
that.fill_rectangle( 0.0f, 0.1f * height, width, 0.4f * height );
that.fill_rectangle( 0.0f, 0.5f * height, width, 0.4f * height );
}
void pattern( canvas &that, float width, float height )
{
unsigned char checker[ 256 ];
for ( int index = 0; index < 256; ++index )
checker[ index ] = static_cast< unsigned char >(
( ( ( index >> 2 & 1 ) ^ ( index >> 5 & 1 ) ) |
( ( index & 3 ) == 3 ) ) * 255 );
that.arc( 0.5f * width, 0.5f * height, 32.0f, 0.0f, 6.28318531f );
that.close_path();
that.set_line_width( 20.0f );
that.set_color( stroke_style, 0.0f, 0.0f, 0.0f, 1.0f );
that.set_pattern( stroke_style, 0, 8, 8, 32, repeat );
that.stroke();
that.set_line_width( 16.0f );
that.set_pattern( stroke_style, checker, 8, 8, 32, repeat );
that.stroke();
for ( float scale = 8.0f; scale >= 1.0f; scale /= 2.0f )
{
that.set_transform( 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f );
that.scale( scale, scale );
float size_x = 0.5f * width / scale;
float size_y = 0.5f * height / scale;
that.set_pattern( fill_style, checker, 8, 8, 32, no_repeat );
that.fill_rectangle( 0.0f, 0.0f, size_x, size_y );
that.set_pattern( fill_style, checker, 8, 8, 32, repeat_x );
that.fill_rectangle( size_x, 0.0f, size_x, size_y );
that.set_pattern( fill_style, checker, 8, 8, 32, repeat_y );
that.fill_rectangle( 0.0f, size_y, size_x, size_y );
that.set_pattern( fill_style, checker, 8, 8, 32, repeat );
that.fill_rectangle( size_x, size_y, size_x, size_y );
}
}
void begin_path( canvas &that, float width, float height )
{
that.move_to( 0.0f, 0.0f );
that.line_to( width, height );
that.stroke();