This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathdrawing.cpp
More file actions
2165 lines (1835 loc) · 69.8 KB
/
drawing.cpp
File metadata and controls
2165 lines (1835 loc) · 69.8 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
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "graphics.h"
#include "graphics-internal.h"
/******************************************************************************/
inline MCGPoint MCGPointReflect(const MCGPoint &origin, const MCGPoint &point)
{
return MCGPointMake(origin.x - (point.x - origin.x), origin.y - (point.y - origin.y));
}
/*******************************************************************************
*
* DRAWING FORMAT - VERSION 0
*
* A drawing is a simple bytecode format for representing graphics expressed
* using SVG. It consists of a header, followed by a sequence of bytecodes
* encoded in bytes, and a sequence of scalars encoded as floats.
*
* The format is such that it requires no extra data structures at runtime
* except whilst rendering.
*
* The header has the following format:
* uint8_t[4] ident = 'L', 'C', 'D', 0
* float width
* float height
* uint32_t scalar_count
* float[scalar_count] scalars
* uint32_t opcode_count
* uint8_t[opcode_count] opcodes
*
* The ident field is used to identify a drawing, allowing simple 'sniffing' of
* the first four bytes to help determine the format.
*
* The last byte of the ident field is the version of the format - currently 0.
* This will be incremented each time the format changes in a backwards
* incompatible way.
*
* The width and height fields are the 'natural' size of the drawing. This is
* used (in the engine) to present as the formattedWidth/Height and to provide
* the 'natural' size used for generating pixels (in which case the ceiling is
* used).
*
* There are several kinds of scalar which may be present:
*
* - coordinate : any scalar value
*
* - unit : a value between 0 and 1
*
* - length : a value greater or equal to 0
*
* - positive : a value greater or equal to 1
*
* - size : a value greater or equal to -1; values less than 0 represent
* a percentage (divided by -100), values greater or equal to 0 represent
* absolute values.
*
*/
/* MCGSolidColor represents a rgba color value. */
typedef MCGColor4f MCGDrawingSolidColor;
struct MCGDrawingRamp
{
ssize_t count;
const MCGDrawingSolidColor *colors;
const MCGFloat *offsets;
};
struct MCGDrawingGradient
{
MCGGradientSpreadMethod spread;
MCGAffineTransform transform;
MCGDrawingRamp ramp;
};
struct MCGDrawingLinearGradient: public MCGDrawingGradient
{
};
struct MCGDrawingRadialGradient: public MCGDrawingGradient
{
};
struct MCGDrawingConicalGradient: public MCGDrawingGradient
{
MCGPoint focal_point;
MCGFloat focal_radius;
};
enum MCGDrawingPaintType
{
kMCGDrawingPaintTypeNone,
kMCGDrawingPaintTypeSolidColor,
kMCGDrawingPaintTypeLinearGradient,
kMCGDrawingPaintTypeRadialGradient,
kMCGDrawingPaintTypeConicalGradient,
kMCGDrawingPaintTypeCurrentColor,
};
struct MCGDrawingPaint
{
MCGDrawingPaintType type;
union
{
MCGDrawingSolidColor solid_color;
MCGDrawingGradient gradient;
MCGDrawingLinearGradient linear_gradient;
MCGDrawingRadialGradient radial_gradient;
MCGDrawingConicalGradient conical_gradient;
};
MCGDrawingPaint(void)
: type(kMCGDrawingPaintTypeNone)
{
}
};
/* Constants describing the first four bytes of a drawing */
enum : uint8_t
{
kMCGDrawingIdent0 = 'L',
kMCGDrawingIdent1 = 'C',
kMCGDrawingIdent2 = 'D',
kMCGDrawingVersion = 0,
};
/* MCGDrawingHeaderFlags is a bit set indicating which 'initialization' fields
* are present in the drawing. */
enum MCGDrawingHeaderFlags : uint32_t
{
/* The drawing has a width specified which is not 100% */
kMCGDrawingHeaderFlagHasWidthBit = 1 << 0,
/* The drawing has a height specified which is not 100% */
kMCGDrawingHeaderFlagHasHeightBit = 1 << 1,
/* The drawing has a viewport specified (viewBox and preserveAspectRatio) */
kMCGDrawingHeaderFlagHasViewportBit = 1 << 2,
/* The mask to use to check no undefined flags are set. */
kMCGDrawingHeaderFlagValidMask = 0x07,
};
/* MCGDrawingHeader defines the layout of a drawing's header.
*
* Note: The definition is partial as the scalar_count and opcode_count are
* not fixed. */
struct MCGDrawingHeader
{
uint8_t ident[4];
float width;
float height;
uint32_t scalar_count;
// float[scalar_count] scalars;
// uint32_t opcode_count;
// uint8_t[opcode_count] opcodes;
};
/* MAIN OPCODES */
/* MCGDrawingOpcode defines the main opcodes which appear in the opcode stream.
*/
enum MCGDrawingOpcode : uint8_t
{
/* End defines the end of an opcode stream */
kMCGDrawingOpcodeEnd = 0,
/* Transform defines a transform attribute change. The transform is defined
* by a following transform opcode stream. */
kMCGDrawingOpcodeTransform = 1,
/* FillPaint defines a fill-paint attribute change. The paint is defined by
* a following paint opcode stream. */
kMCGDrawingOpcodeFillPaint = 2,
/* FillOpacity defines a fill-opacity attribute change. The opacity is
* defined by a unit scalar. */
kMCGDrawingOpcodeFillOpacity = 3,
/* FillRule defines a fill-rule attribute change. The fill rule is defined
* by a FillRule opcode. */
kMCGDrawingOpcodeFillRule = 4,
/* StrokePaint defines a stroke-paint attribute change. The paint is defined
* by a following paint opcode stream. */
kMCGDrawingOpcodeStrokePaint = 5,
/* StrokeOpacity defines a stroke-opacity attribute change. The opacity is
* defined by a unit length scalar. */
kMCGDrawingOpcodeStrokeOpacity = 6,
/* StrokeWidth defines a stroke-width attribute change. The stroke-width is
* defined by a length scalar. */
kMCGDrawingOpcodeStrokeWidth = 7,
/* StrokeLineJoin defines a stroke-line-join attribute change. The
* stroke-line-join is defined by a StrokeLineJoin opcode. */
kMCGDrawingOpcodeStrokeLineJoin = 8,
/* StrokeLineCap defines a stroke-line-cap attribute change. The
* stroke-line-cap is defined by a StrokeLineCap opcode. */
kMCGDrawingOpcodeStrokeLineCap = 9,
/* StrokeDashArray defines a stroke-dash-array attribute change. The
* stroke-dash-array is defined by a length scalar array. */
kMCGDrawingOpcodeStrokeDashArray = 10,
/* StrokeDashOffset defines a stroke-dash-offset attribute change. The
* stroke-dash-offset is defined by a length scalar. */
kMCGDrawingOpcodeStrokeDashOffset = 11,
/* StrokeMiterLimit defines a stroke-miter-limit attribute change. The
* stroke-miter-limit is defined by a positive length scalar. */
kMCGDrawingOpcodeStrokeMiterLimit = 12,
/* Rectangle defines a rectangle shape. The rectangle is defined as:
* x, y: coordinate scalars
* width, height: length scalars */
kMCGDrawingOpcodeRectangle = 13,
/* RoundedRectangle defines a rounded rectangle shape. The rounded rectangle
* is defined as:
* x, y: coordinate scalars
* width, height: length scalars
* rx, ry: length scalars */
kMCGDrawingOpcodeRoundedRectangle = 14,
/* Circle defines a circle shape. The circle is defined as:
* x, y: coordinate scalars
* r: length scalar */
kMCGDrawingOpcodeCircle = 15,
/* Ellipse defines an ellipse shape. The ellipse is defined as:
* x, y: coordinate scalars
* rx, ry: length scalars */
kMCGDrawingOpcodeEllipse = 16,
/* Line defines a line shape. The line is defined as:
* x1, y1: coordinate scalars
* x2, y2: coordinate scalars */
kMCGDrawingOpcodeLine = 17,
/* Polyline defines a polyline shape. The polyline is defined as an array
* of coordinate scalar pairs. */
kMCGDrawingOpcodePolyline = 18,
/* Polyline defines a polygon shape. The polygon is defined as an array
* of coordinate scalar pairs. */
kMCGDrawingOpcodePolygon = 19,
/* Path defines a path shape. The path is defined by a path opcode stream.
*/
kMCGDrawingOpcodePath = 20,
/* _Last is used for range checking on the main opcodes. */
kMCGDrawingOpcode_Last = kMCGDrawingOpcodePath,
};
/* TRANSFORM OPCODES */
/* MCGDrawingTransformOpcode defines the opcodes used to build a transform. */
enum MCGDrawingTransformOpcode : uint8_t
{
/* Identity defines the identity transform */
kMCGDrawingTransformOpcodeIdentity = 0,
/* Affine defines a general affine transformation. It is defined by 6
* scalars - a b c d tx ty. */
kMCGDrawingTransformOpcodeAffine = 1,
/* _Last is used for range checking on the transform opcodes. */
kMCGDrawingTransformOpcode_Last = kMCGDrawingTransformOpcodeAffine,
};
/* PAINT OPCODES */
/* MCGDrawingPaintOpcode defines the opcodes used to build a paint. */
enum MCGDrawingPaintOpcode : uint8_t
{
/* None defines no paint. */
kMCGDrawingPaintOpcodeNone = 0,
/* SolidColor defines a solid color paint. It is defined by 4 unit scalars -
* red green blue alpha. */
kMCGDrawingPaintOpcodeSolidColor = 1,
/* SolidLinearGradient defines a linear gradient paint. It is defined by:
* spread method : spread method opcode
* transform : transform opcode stream
* count : ramp length
* colors : count sets of 4 unit scalars - red green blue alpha
* stops : count array of unit scalars
*/
kMCGDrawingPaintOpcodeLinearGradient = 2,
kMCGDrawingPaintOpcodeRadialGradient = 3,
kMCGDrawingPaintOpcodeConicalGradient = 4,
kMCGDrawingPaintOpcodeCurrentColor = 5,
/* _Last is used for range checking on the paint opcodes. */
kMCGDrawingPaintOpcode_Last = kMCGDrawingPaintOpcodeCurrentColor,
};
/* SPREAD METHOD OPCODES */
/* MCGDrawingSpreadMethodOpcode defines the opcodes used to specify the spread
* method of a gradient. */
enum MCGDrawingSpreadMethodOpcode : uint8_t
{
kMCGDrawingSpreadMethodOpcodePad = 0,
kMCGDrawingSpreadMethodOpcodeReflect = 1,
kMCGDrawingSpreadMethodOpcodeRepeat = 2,
kMCGDrawingSpreadMethodOpcode_Last = kMCGDrawingSpreadMethodOpcodeRepeat,
};
/* FILL RULE OPCODES */
/* MCGDrawingFillRuleOpcode defines the opcodes used to specify the fill-rule
* attribute. */
enum MCGDrawingFillRuleOpcode : uint8_t
{
/* NonZero indicates that the non-zero winding rule should be used. */
kMCGDrawingFillRuleOpcodeNonZero = 0,
/* EvenOdd indicates that the even-odd rule should be used. */
kMCGDrawingFillRuleOpcodeEvenOdd = 1,
/* _Last is used for range checking on the fill rule opcodes. */
kMCGDrawingFillRuleOpcode_Last = kMCGDrawingFillRuleOpcodeEvenOdd,
};
/* LINE JOIN OPCODES */
/* MCGDrawingStrokeLineJoinOpcode defines the opcodes used to specify the
* stroke-line-join attribute. */
enum MCGDrawingStrokeLineJoinOpcode : uint8_t
{
/* Bevel indicates that the bevel line join should be used. A bevel line
* join uses a single line segment between the outer points of a join. */
kMCGDrawingStrokeLineJoinOpcodeBevel = 0,
/* Round indicates that the round line join should be used. A round line
* join uses a circular arc between the outer points of a join. */
kMCGDrawingStrokeLineJoinOpcodeRound = 1,
/* Miter indicates that the miter line join should be used. A miter line
* join extends the outer edges of a join so they meet in a point. Miter
* joins fallback to a bevel join if the ratio of the angle and size between
* them exceeds the miter-limit. */
kMCGDrawingStrokeLineJoinOpcodeMiter = 2,
/* _Last is used for range checking on the line join opcodes. */
kMCGDrawingStrokeLineJoinOpcode_Last = kMCGDrawingStrokeLineJoinOpcodeMiter
};
/* LINE CAP OPCODES */
/* MCGDrawingStrokeLineCapOpcode defines the opcodes used to specify the
* stroke-line-cap attribute. */
enum MCGDrawingStrokeLineCapOpcode : uint8_t
{
/* Butt indicates that the butt line cap should be used. A butt line cap
* adds no cap to the ends of stroked lines. */
kMCGDrawingStrokeLineCapOpcodeButt = 0,
/* Round indicates that the round line cap should be used. A round line cap
* adds a half circle of diameter the stroke width to the ends of stroked
* lines. */
kMCGDrawingStrokeLineCapOpcodeRound = 1,
/* Square indicates that the square line cap should be used. A square line
* cap adds a half square of size the stroke width to the ends of stroked
* lines. */
kMCGDrawingStrokeLineCapOpcodeSquare = 2,
/* _Last is used for range checking on the line cap opcodes. */
kMCGDrawingStrokeLineCapOpcode_Last = kMCGDrawingStrokeLineCapOpcodeSquare
};
/* PATH OPCODES */
/* MGDrawingPathOpcodes defines the opcodes used to specify a path shape. */
enum MCGDrawingPathOpcode : uint8_t
{
/* End terminates a path definition. */
kMCGDrawingPathOpcodeEnd = 0,
kMCGDrawingPathOpcodeMoveTo = 1,
kMCGDrawingPathOpcodeLineTo = 2,
kMCGDrawingPathOpcodeHorizontalTo = 3,
kMCGDrawingPathOpcodeVerticalTo = 4,
kMCGDrawingPathOpcodeCubicTo = 5,
kMCGDrawingPathOpcodeSmoothCubicTo = 6,
kMCGDrawingPathOpcodeQuadraticTo= 7,
kMCGDrawingPathOpcodeSmoothQuadraticTo = 8,
kMCGDrawingPathOpcodeArcTo = 9,
kMCGDrawingPathOpcodeReflexArcTo = 10,
kMCGDrawingPathOpcodeReverseArcTo = 11,
kMCGDrawingPathOpcodeReverseReflexArcTo = 12,
kMCGDrawingPathOpcodeCloseSubpath = 13,
kMCGDrawingPathOpcode_Last = kMCGDrawingPathOpcodeCloseSubpath,
};
inline bool MCGDrawingPathOpcodeIsReflexArcTo(MCGDrawingPathOpcode p_opcode)
{
switch(p_opcode)
{
case kMCGDrawingPathOpcodeReflexArcTo:
case kMCGDrawingPathOpcodeReverseReflexArcTo:
return true;
default:
break;
}
return false;
}
inline bool MCGDrawingPathOpcodeIsReverseArcTo(MCGDrawingPathOpcode p_opcode)
{
switch(p_opcode)
{
case kMCGDrawingPathOpcodeReverseArcTo:
case kMCGDrawingPathOpcodeReverseReflexArcTo:
return true;
default:
break;
}
return false;
}
/**/
/* MCGDrawingVisitor is the interface expected from the Visitor type used by
* the Execute methods in MCGDrawingContext. */
struct MCGDrawingVisitor
{
/* Path Operations */
void PathBegin(void);
void PathMoveTo(bool is_relative, MCGPoint p_point);
void PathLineTo(bool is_relative, MCGPoint p_point);
void PathHorizontalTo(bool is_relative, MCGFloat x);
void PathVerticalTo(bool is_relative, MCGFloat y);
void PathCubicTo(bool is_relative, MCGPoint p_a, MCGPoint p_b, MCGPoint p_point);
void PathSmoothCubicTo(bool is_relative, MCGPoint p_b, MCGPoint p_point);
void PathQuadraticTo(bool is_relative, MCGPoint p_a, MCGPoint p_point);
void PathSmoothQuadraticTo(bool is_relative, MCGPoint p_point);
void PathArcTo(bool is_relative, bool is_reflex, bool is_reverse, MCGPoint p_center, MCGFloat rotation, MCGPoint p_point);
void PathCloseSubpath(void);
void PathEnd(void);
/* General Attribute Operations */
void Transform(MCGAffineTransform transform);
/* Fill Attribute Operations */
void FillPaint(const MCGDrawingPaint& paint);
void FillOpacity(MCGFloat opacity);
void FillRule(MCGFillRule fill_rule);
/* Stroke Attribute Operations */
void StrokePaint(const MCGDrawingPaint& paint);
void StrokeOpacity(MCGFloat opacity);
void StrokeWidth(MCGFloat width);
void StrokeLineJoin(MCGJoinStyle join_style);
void StrokeLineCap(MCGCapStyle cap_style);
void StrokeDashArray(MCSpan<const MCGFloat> lengths);
void StrokeDashOffset(MCGFloat offset);
void StrokeMiterLimit(MCGFloat miter_limit);
/* Shape Operations */
void Rectangle(MCGRectangle p_bounds, MCGPoint p_radii);
void Circle(MCGPoint p_center, MCGFloat r);
void Ellipse(MCGPoint p_center, MCGSize p_radii);
void Line(MCGPoint p_from, MCGPoint p_to);
void Polyline(MCSpan<const MCGPoint> p_points);
void Polygon(MCSpan<const MCGPoint> p_points);
/* Lifecycle Operations */
void Start(MCGRectangle p_src_rect, MCGRectangle p_dst_rect);
void Finish(bool p_error);
};
/* DRAWING CONTEXT */
/* MCGDrawingStatus is used to indicate the current status of playback of a
* drawing. */
enum MCGDrawingStatus
{
/* None indicates that there are no problems with the drawing so far. */
kMCGDrawingStatusNone,
/* InvalidDrawing indicates that the drawing data is malformed and cannot
* be unpacked. */
kMCGDrawingStatusInvalidDrawing,
/* InvalidIdent indicates that the 3 main ident bytes do not match 'LCD'. */
kMCGDrawingStatusInvalidIdent,
/* InvalidVersion indicates that the version is not recognised. */
kMCGDrawingStatusInvalidVersion,
/* InvalidWidth indicates that the width field is not a valid value. */
kMCGDrawingStatusInvalidWidth,
/* InvalidHeight indicates that the height field is not a valid value. */
kMCGDrawingStatusInvalidHeight,
/* ScalarOverflow indicates that more scalars are required than are present.
*/
kMCGDrawingStatusScalarOverflow,
/* OpcodeOverflow indicates that more opcodes are required than are present.
*/
kMCGDrawingStatusOpcodeOverflow,
/* InvalidOpcode indicates that an undefined main opcode has been
* encountered. */
kMCGDrawingStatusInvalidOpcode,
/* InvalidPathOpcode indicates that an undefined path opcode has been
* encountered. */
kMCGDrawingStatusInvalidPathOpcode
,
/* InvalidPaintOpcode indicates that an undefined paint opcode has been
* encountered. */
kMCGDrawingStatusInvalidPaintOpcode,
/* InvalidTransformOpcode indicates that an undefined transform opcode has
* been encountered. */
kMCGDrawingStatusInvalidTransformOpcode,
/* InvalidFillRuleOpcode indicates that an undefined fill rule opcode has
* been encountered. */
kMCGDrawingStatusInvalidFillRuleOpcode,
/* InvalidStrokeLineJoinOpcode indicates that an undefined line join opcode
* has been encountered. */
kMCGDrawingStatusInvalidStrokeLineJoinOpcode,
/* InvalidStrokeLineCapOpcode indicates that an undefined line cap opcode
* has been encountered. */
kMCGDrawingStatusInvalidStrokeLineCapOpcode,
/* InvalidSpreadMethodOpcode indicates that an undefined spread method
* opcode has been encountered. */
kMCGDrawingStatusInvalidSpreadMethodOpcode,
/* InvalidPointArray indicates that a point array with an odd number of
* scalars has been encountered. */
kMCGDrawingStatusInvalidPointArray,
};
/* MCGDrawingByteStream is a simple class which wraps a byte array, allowing
* decoding of individual and sequences of specific types. It is used to unpack
* the drawing header. */
class MCGDrawingByteStream
{
public:
MCGDrawingByteStream(MCSpan<const byte_t> p_bytes)
: m_bytes(p_bytes)
{
}
/* Returns true if there are no more bytes to process. */
bool IsFinished(void) const
{
return m_bytes.empty();
}
/* Singleton attempts to unpack a single field of type T. It returns true
* if successful, or false if there are not enough bytes (sizeof(T)). */
template<typename T>
bool Singleton(T& r_singleton)
{
ssize_t t_singleton_size = sizeof(T);
if (m_bytes.size() < t_singleton_size)
{
return false;
}
auto t_singleton_ptr = reinterpret_cast<const T *>(m_bytes.data());
r_singleton = *t_singleton_ptr;
m_bytes = m_bytes.subspan(t_singleton_size);
return true;
}
/* Array attempts to unpack p_count fields of type T. It returns true if
* successful, or false if there are not enough bytes (sizeof(T) * p_count).
*/
template<typename T>
bool Sequence(MCSpan<const T>& r_sequence)
{
uint32_t t_size;
if (!Singleton(t_size))
{
return false;
}
ssize_t t_byte_size = t_size * sizeof(T);
if (m_bytes.size() < t_byte_size)
{
return false;
}
auto t_seq_ptr = reinterpret_cast<const T *>(m_bytes.data());
r_sequence = MCMakeSpan(t_seq_ptr, t_size);
m_bytes = m_bytes.subspan(t_byte_size);
return true;
}
private:
MCSpan<const byte_t> m_bytes;
};
/* MCGDrawingContext is a class which implements a visitor pattern over a
* drawing. It unpacks the header on construction, and then allows execution
* of the drawing by calling 'Execute' and passing a suitably defined Visitor
* type. */
class MCGDrawingContext
{
public:
/* MCGDrawingContext is the main constructor. It attempts to unpack the
* drawing header, and sets up for main execution. If an error occurs
* with setting up, the status field is set appropriately. */
MCGDrawingContext(MCSpan<const byte_t> p_bytes)
{
MCGDrawingByteStream t_stream(p_bytes);
/* Unpack the header. If the size of the provided data does not match
* what is expected by unpacking the data, then it is an
* 'InvalidDrawing' */
uint8_t t_ident_0, t_ident_1, t_ident_2, t_ident_version;
if (!t_stream.Singleton(t_ident_0) ||
!t_stream.Singleton(t_ident_1) ||
!t_stream.Singleton(t_ident_2) ||
!t_stream.Singleton(t_ident_version) ||
!t_stream.Singleton(m_width) ||
!t_stream.Singleton(m_height) ||
!t_stream.Sequence(m_scalars) ||
!t_stream.Sequence(m_opcodes) ||
!t_stream.IsFinished())
{
m_status = kMCGDrawingStatusInvalidDrawing;
return;
}
/* Check the ident field is correct. */
if (t_ident_0 != kMCGDrawingIdent0 ||
t_ident_1 != kMCGDrawingIdent1 ||
t_ident_2 != kMCGDrawingIdent2)
{
m_status = kMCGDrawingStatusInvalidIdent;
return;
}
/* Check the version field is correct. */
if (t_ident_version != kMCGDrawingVersion)
{
m_status = kMCGDrawingStatusInvalidVersion;
return;
}
/* Check the width is valid. */
if (m_width < 0)
{
m_status = kMCGDrawingStatusInvalidWidth;
return;
}
/* Check the height is valid. */
if (m_height < 0)
{
m_status = kMCGDrawingStatusInvalidHeight;
return;
}
}
/* IsRunning() returns true if the execution has encountered no problems.
*/
bool IsRunning(void) const
{
return m_status == kMCGDrawingStatusNone;
}
/* Opcode attempts to read the next opcode as a main opcode. */
bool Opcode(MCGDrawingOpcode& r_opcode)
{
return GeneralOpcode<MCGDrawingOpcode,
kMCGDrawingOpcode_Last,
kMCGDrawingStatusInvalidOpcode>(r_opcode);
}
/* TransformOpcode attempts to read the next opcode as a transform opcode. */
bool TransformOpcode(MCGDrawingTransformOpcode& r_opcode)
{
return GeneralOpcode<MCGDrawingTransformOpcode,
kMCGDrawingTransformOpcode_Last,
kMCGDrawingStatusInvalidTransformOpcode>(r_opcode);
}
/* PaintOpcode attempts to read the next opcode as a paint opcode. */
bool PaintOpcode(MCGDrawingPaintOpcode& r_opcode)
{
return GeneralOpcode<MCGDrawingPaintOpcode,
kMCGDrawingPaintOpcode_Last,
kMCGDrawingStatusInvalidPaintOpcode>(r_opcode);
}
/* PathOpcode attempts to read the next opcode as a path opcode. */
bool PathOpcode(MCGDrawingPathOpcode& r_opcode)
{
return GeneralOpcode<MCGDrawingPathOpcode,
kMCGDrawingPathOpcode_Last,
kMCGDrawingStatusInvalidPathOpcode>(r_opcode);
}
/* FillRuleOpcode attempts to read the next opcode as a fill rule opcode. */
bool FillRuleOpcode(MCGDrawingFillRuleOpcode& r_opcode)
{
return GeneralOpcode<MCGDrawingFillRuleOpcode,
kMCGDrawingFillRuleOpcode_Last,
kMCGDrawingStatusInvalidFillRuleOpcode>(r_opcode);
}
/* StrokeLineJoin attempts to read the next opcode as a line join opcode. */
bool StrokeLineJoinOpcode(MCGDrawingStrokeLineJoinOpcode& r_opcode)
{
return GeneralOpcode<MCGDrawingStrokeLineJoinOpcode,
kMCGDrawingStrokeLineJoinOpcode_Last,
kMCGDrawingStatusInvalidStrokeLineJoinOpcode>(r_opcode);
}
/* StrokeLineCape attempts to read the next opcode as a line cap opcode. */
bool StrokeLineCapOpcode(MCGDrawingStrokeLineCapOpcode& r_opcode)
{
return GeneralOpcode<MCGDrawingStrokeLineCapOpcode,
kMCGDrawingStrokeLineCapOpcode_Last,
kMCGDrawingStatusInvalidStrokeLineCapOpcode>(r_opcode);
}
/* SpreadMethodOpcode attempts to read the next opcode as a spread method
* opcode. */
bool SpreadMethodOpcode(MCGDrawingSpreadMethodOpcode& r_opcode)
{
return GeneralOpcode<MCGDrawingSpreadMethodOpcode,
kMCGDrawingSpreadMethodOpcode_Last,
kMCGDrawingStatusInvalidSpreadMethodOpcode>(r_opcode);
}
/* Count attempts to read an non-negative integer from the opcode stream.
* Non-negative integers are encoded as a sequence of bytes, most
* significant byte first. The top bit of each byte is used to indicate
* whether there is another following. */
bool Count(ssize_t& r_count)
{
/* Start the read count at zero, each byte will be accumulated into it
*/
ssize_t t_count = 0;
/* Opcode bytes are processed until we reach a termination condition. */
for(;;)
{
/* If the end of the opcode stream is reached, then indicate
* overflow. */
if (m_pc == m_opcodes.size())
{
m_status = kMCGDrawingStatusOpcodeOverflow;
return false;
}
/* Fetch the next limb of the integer and increment the pc. */
uint8_t t_next_limb = m_opcodes[m_pc++];
/* Accumulate the limb into the current count, making sure to mask
* out the top bit of the limb. */
t_count = (t_count << 8) | (t_next_limb & 0x7f);
/* If the top bit of the limb is zero, then the integer is complete.
*/
if ((t_next_limb & 0x40) == 0)
{
break;
}
}
/* Return the accumulated integer. */
r_count = t_count;
return true;
}
/* Scalar attempts to read the next scalar from the scalar stream. If the
* end of the scalar stream has been reached, ScalarOverflow is indicated
* and false is returned. Otherwise, the next scalar is placed in r_scalar
* and true is returned. */
bool Scalar(MCGFloat& r_scalar)
{
/* If the scalar counter is at the end of the scalar stream, then
* indicate ScalarOverflow and return false. */
if (m_sc == m_scalars.size())
{
m_status = kMCGDrawingStatusScalarOverflow;
return false;
}
/* Place the next scalar in r_scalar and increment the scalar counter. */
r_scalar = m_scalars[m_sc++];
return true;
}
/* Scalars attempts to read p_count scalars from the scalar stream. If there
* are not enough scalars left then ScalarOverflow is indicated and false is
* returned. Otherwise, the pointer to the base of the scalars is returned
* in r_scalars and true is returned. */
bool Scalars(ssize_t p_count, MCSpan<const MCGFloat>& r_scalars)
{
/* If there are not enough scalars left to satisfy the request then
* indicate ScalarOverflow and return false. */
if (m_sc + p_count > m_scalars.size())
{
m_status = kMCGDrawingStatusScalarOverflow;
return false;
}
/* Place the base of the scalar array in r_scalars. */
r_scalars = m_scalars.subspan(m_sc, p_count);
/* Increment the scalar counter */
m_sc += p_count;
return true;
}
bool UnitScalars(ssize_t p_count, MCSpan<const MCGFloat>& r_scalars)
{
return Scalars(p_count, r_scalars);
}
/* LengthScalars attempts to read p_count length scalars - a length scalar
* is non-negative scalar. */
bool LengthScalars(ssize_t p_count, MCSpan<const MCGFloat>& r_scalars)
{
return Scalars(p_count, r_scalars);
}
/* CoordinateScalars attempts to read p_count coordinate scalars. */
bool CoordinateScalars(ssize_t p_count, MCSpan<const MCGFloat>& r_scalars)
{
return Scalars(p_count, r_scalars);
}
/* Coordinate scalar attempte to read a single coordinate scalar. */
bool CoordinateScalar(MCGFloat& r_coord_scalar)
{
return Scalar(r_coord_scalar);
}
/* UnitScalar attempts to read a single unit scalar - a unit scalar is a
* scalar in the range [0, 1]. */
bool UnitScalar(MCGFloat& r_unit_scalar)
{
return Scalar(r_unit_scalar);
}
/* LengthScalar attempts to read a single length scalar. */
bool LengthScalar(MCGFloat& r_length_scalar)
{
return Scalar(r_length_scalar);
}
/* PositiveScalar attempts to read a single positive scalar - a positive
* scalar is a scalar in the range [1, +inf). */
bool PositiveScalar(MCGFloat& r_positive_scalar)
{
return Scalar(r_positive_scalar);
}
/* AngleScalar attempts to read a single angle scalar. */
bool AngleScalar(MCGFloat& r_angle_scalar)
{
return Scalar(r_angle_scalar);
}
/* Point attempts to read a point defined as two coordinate scalars - x y. */
bool Point(MCGPoint& r_point)
{
if (!CoordinateScalar(r_point.x) ||
!CoordinateScalar(r_point.y))
{
return false;
}
return true;
}
/* Size attempts to read a size defined as two length scalars - rx ry. */
bool Size(MCGSize& r_size)
{
if (!LengthScalar(r_size.width) ||
!LengthScalar(r_size.height))
{
return false;
}
return true;
}
/* AffineTransform attempts to read an transform defined as size scalars. */
bool AffineTransform(MCGAffineTransform& r_transform)
{
if (!Scalar(r_transform.a) ||
!Scalar(r_transform.b) ||
!Scalar(r_transform.c) ||
!Scalar(r_transform.d) ||
!Scalar(r_transform.tx) ||
!Scalar(r_transform.ty))
{
return false;
}
return true;
}
/* Rectangle attempts to read a rectangle defined as four scalars - x, y
* width, height. */
bool Rectangle(MCGRectangle& r_rect)
{
if (!CoordinateScalar(r_rect.origin.x) ||
!CoordinateScalar(r_rect.origin.y) ||
!LengthScalar(r_rect.size.width) ||
!LengthScalar(r_rect.size.height))
{
return false;
}
return true;
}
/* SolidColor attempts to read a solid color defined as three unit scalars -
* red, green, blue. */
bool SolidColor(MCGDrawingSolidColor& r_color)
{
if (!UnitScalar(r_color.red) ||
!UnitScalar(r_color.green) ||
!UnitScalar(r_color.blue) ||
!UnitScalar(r_color.alpha))
{
return false;
}
return true;
}
/* LengthArray attempts to read an array of scalars, using a count fetched
* from the opcode stream. */
bool LengthArray(MCSpan<const MCGFloat>& r_lengths)
{
ssize_t t_count;
if (!Count(t_count))
{
return false;
}
if (!Scalars(t_count, r_lengths))
{
return false;
}
return true;
}
/* PointArray attempts to read an array of coordinate scalar pairs, using
* a count fetched from the opcode stream. Currently the count encoded in