forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics-internal.h
More file actions
673 lines (528 loc) · 16.9 KB
/
graphics-internal.h
File metadata and controls
673 lines (528 loc) · 16.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
/* 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/>. */
#ifndef __MC_GRAPHICS_INTERNAL__
#define __MC_GRAPHICS_INTERNAL__
#include "graphics.h"
#include <SkCanvas.h>
#include <SkDashPathEffect.h>
#include <SkMask.h>
#include <SkShader.h>
#include <SkSurface.h>
#ifdef __MOBILE
#define kMCGTextMeasureCacheTableSize 16384
#else
#define kMCGTextMeasureCacheTableSize 65535
#endif
#define kMCGTextMeasureCacheByteSize kMCGTextMeasureCacheTableSize * 256
#define kMCGTextMeasureCacheMaxOccupancy kMCGTextMeasureCacheTableSize * 0.5
#define kMCGTextMeasureCacheMaxStringLength 65536
////////////////////////////////////////////////////////////////////////////////
typedef class MCGObject *MCGObjectRef;
class MCGObject
{
public:
#ifdef _DEBUG
static size_t s_object_count;
#endif
MCGObject(void)
: m_references(1)
{
#ifdef _DEBUG
s_object_count += 1;
#endif
}
protected:
virtual ~MCGObject(void)
{
#ifdef _DEBUG
s_object_count -= 1;
#endif
}
private:
virtual void Retain(void)
{
m_references += 1;
}
virtual void Release(void)
{
m_references -= 1;
if (m_references == 0)
{
delete this;
}
}
template<typename T>
friend T MCGRetain(T p_object)
{
if (p_object == nullptr)
{
return nullptr;
}
p_object->Retain();
return p_object;
}
template<typename T>
friend void MCGRelease(T p_object)
{
if (p_object == nullptr)
{
return;
}
p_object->Release();
}
template<typename T>
friend void MCGAssignAndRelease(T& x_target, T p_source)
{
if (x_target != nullptr)
{
x_target->Release();
}
x_target = p_source;
}
uint32_t m_references;
};
////////////////////////////////////////////////////////////////////////////////
typedef class MCGPaint *MCGPaintRef;
class MCGPaint: public MCGObject
{
public:
virtual bool Apply(SkPaint& r_paint) = 0;
};
////////////////////////////////////////////////////////////////////////////////
typedef class MCGSolidColor *MCGSolidColorRef;
class MCGSolidColor: public MCGPaint
{
public:
virtual bool Apply(SkPaint& r_paint);
static bool Create(MCGFloat p_red, MCGFloat p_green, MCGFloat p_blue, MCGFloat p_alpha, MCGSolidColorRef& r_solid_color);
private:
MCGColor m_color;
};
extern MCGSolidColorRef kMCGBlackSolidColor;
////////////////////////////////////////////////////////////////////////////////
typedef class MCGPattern *MCGPatternRef;
class MCGPattern: public MCGPaint
{
public:
virtual ~MCGPattern(void);
virtual bool Apply(SkPaint& r_paint);
static bool Create(MCGImageRef image, MCGAffineTransform transform, MCGImageFilter filter, MCGPatternRef& r_pattern);
private:
MCGImageRef m_image;
MCGAffineTransform m_transform;
MCGImageFilter m_filter;
};
////////////////////////////////////////////////////////////////////////////////
typedef class MCGRamp *MCGRampRef;
class MCGRamp: public MCGObject
{
public:
virtual ~MCGRamp(void);
static bool Create(const MCGFloat *p_stops, const MCGColor *p_colors, size_t p_ramp_length, MCGRampRef& r_ramp);
static bool Create4f(const MCGFloat *p_stops, const MCGColor4f *p_colors, size_t p_ramp_length, MCGRampRef& r_ramp);
const SkScalar* GetStops(void) const { return m_stops; }
const SkColor* GetColors(void) const { return m_colors; }
size_t GetLength(void) const { return m_ramp_length; }
private:
SkScalar *m_stops = nullptr;
SkColor *m_colors = nullptr;
size_t m_ramp_length = 0;
};
typedef class MCGGradient *MCGGradientRef;
class MCGGradient: public MCGPaint
{
public:
virtual ~MCGGradient(void);
protected:
MCGRampRef m_ramp = nullptr;
MCGGradientSpreadMethod m_spread_method;
MCGAffineTransform m_transform;
};
typedef class MCGGeneralizedGradient *MCGGeneralizedGradientRef;
class MCGGeneralizedGradient: public MCGGradient
{
public:
virtual bool Apply(SkPaint& r_paint);
static bool Create(MCGGradientFunction function, MCGRampRef ramp, bool mirror, bool wrap, uint32_t repeats, MCGAffineTransform transform, MCGImageFilter filter, MCGGradientRef& r_gradient);
private:
MCGGradientFunction m_function;
uint32_t m_repeats;
MCGImageFilter m_filter;
bool m_mirror;
bool m_wrap;
friend class MCGGeneralizedGradientShader;
};
extern bool MCGGeneralizedGradientShaderApply(MCGGeneralizedGradientRef p_gradient, SkPaint& r_paint);
typedef class MCGLinearGradient *MCGLinearGradientRef;
class MCGLinearGradient: public MCGGradient
{
public:
virtual bool Apply(SkPaint& r_paint);
static bool Create(MCGPoint p_from, MCGPoint p_to, MCGRampRef p_ramp, MCGGradientSpreadMethod p_spread_method, MCGAffineTransform p_transform, MCGGradientRef& r_gradient);
private:
MCGPoint m_from;
MCGPoint m_to;
};
typedef class MCGRadialGradient *MCGRadialGradientRef;
class MCGRadialGradient: public MCGGradient
{
public:
virtual bool Apply(SkPaint& r_paint);
static bool Create(MCGPoint p_focal_point, MCGFloat p_radius, MCGRampRef p_ramp, MCGGradientSpreadMethod p_tile_mode, MCGAffineTransform p_transform, MCGGradientRef& r_gradient);
private:
MCGPoint m_focal_point;
MCGFloat m_radius;
};
typedef class MCGConicalGradient *MCGConicalGradientRef;
class MCGConicalGradient: public MCGGradient
{
public:
virtual bool Apply(SkPaint& r_paint);
static bool Create(MCGPoint p_center_point, MCGFloat p_radius, MCGPoint p_focal_point, MCGFloat p_focal_radius, MCGRampRef p_ramp, MCGGradientSpreadMethod p_tile_mode, MCGAffineTransform p_transform, MCGGradientRef& r_gradient);
private:
MCGPoint m_center_point;
MCGFloat m_radius;
MCGPoint m_focal_point;
MCGFloat m_focal_radius;
};
typedef class MCGSweepGradient *MCGSweepGradientRef;
class MCGSweepGradient: public MCGGradient
{
public:
virtual bool Apply(SkPaint& r_paint);
static bool Create(MCGPoint p_center, MCGRampRef p_ramp, MCGGradientSpreadMethod p_tile_mode, MCGAffineTransform p_transform, MCGGradientRef& r_gradient);
private:
MCGPoint m_center;
};
////////////////////////////////////////////////////////////////////////////////
typedef struct __MCGContextState *MCGContextStateRef;
struct __MCGContextState
{
MCGAffineTransform base_transform;
MCGAffineTransform transform;
MCGFloat opacity;
MCGBlendMode blend_mode;
MCGFloat flatness;
bool should_antialias;
MCGPaintRef fill_paint;
MCGFloat fill_opacity;
MCGFillRule fill_rule;
MCGPaintStyle fill_style;
MCGPaintRef stroke_paint;
MCGFloat stroke_opacity;
MCGStrokeAttr stroke_attr;
MCGPaintStyle stroke_style;
bool is_layer_begin_pt;
MCGContextStateRef parent;
};
typedef struct __MCGContextLayer *MCGContextLayerRef;
struct __MCGContextLayer
{
SkCanvas *canvas;
sk_sp<SkSurface> m_surface;
uint32_t nesting;
int32_t origin_x, origin_y;
bool has_effects : 1;
MCGBitmapEffects effects;
MCGContextLayerRef parent;
};
////////////////////////////////////////////////////////////////////////////////
struct __MCGImage
{
SkBitmap *bitmap;
bool is_valid;
uint32_t references;
};
struct __MCGMask
{
SkMask mask;
};
struct __MCGPath
{
SkPath *path;
bool is_valid;
bool is_mutable;
uint32_t references;
};
struct __MCGContext
{
//uint32_t width;
//uint32_t height;
MCGContextStateRef state;
MCGContextLayerRef layer;
MCGPathRef path;
bool is_valid;
uint32_t references;
};
////////////////////////////////////////////////////////////////////////////////
struct __MCGDashes
{
MCGFloat phase;
MCGFloat *lengths;
uindex_t count;
uint32_t references;
};
bool MCGDashesCreate(MCGFloat phase, const MCGFloat *lengths, uindex_t arity, MCGDashesRef& r_dashes);
MCGDashesRef MCGDashesRetain(MCGDashesRef dashes);
void MCGDashesRelease(MCGDashesRef dashes);
bool MCGDashesToSkDashPathEffect(MCGDashesRef dashes, sk_sp<SkPathEffect>& r_path_effect);
////////////////////////////////////////////////////////////////////////////////
inline bool MCGRectangleIntersects(MCGRectangle p_rect_1, MCGRectangle p_rect_2)
{
return (p_rect_1 . origin . x < (p_rect_2 . origin . x + p_rect_2 . size . width)) &&
(p_rect_2 . origin . x < (p_rect_1 . origin . x + p_rect_1 . size . width)) &&
(p_rect_1 . origin . y < (p_rect_2 . origin . y + p_rect_2 . size . height)) &&
(p_rect_2 . origin . y < (p_rect_1 . origin . y + p_rect_1 . size . height));
}
struct MCGIntRectangle
{
int32_t x;
int32_t y;
int32_t width;
int32_t height;
};
inline MCGIntRectangle MCGRectangleIntegerBounds(const MCGRectangle &p_rect)
{
MCGIntRectangle t_bounds;
t_bounds . x = floor(p_rect . origin . x);
t_bounds . y = floor(p_rect . origin . y);
// IM-2013-09-02: [[ RefactorGraphics ]] don't expand size if width or height is zero
if (p_rect . size . width == 0.0)
t_bounds . width = 0;
else
t_bounds . width = ceil(p_rect . origin . x + p_rect . size . width) - t_bounds . x;
if (p_rect . size . height == 0.0)
t_bounds . height = 0;
else
t_bounds . height = ceil(p_rect . origin . y + p_rect . size . height) - t_bounds . y;
return t_bounds;
}
////////////////////////////////////////////////////////////////////////////////
enum MCGPixelOwnershipType
{
kMCGPixelOwnershipTypeBorrow,
kMCGPixelOwnershipTypeTake,
kMCGPixelOwnershipTypeCopy,
};
bool MCGRasterToSkBitmap(const MCGRaster& raster, MCGPixelOwnershipType p_ownership, SkBitmap& bitmap);
SkBlendMode MCGBlendModeToSkBlendMode(MCGBlendMode);
void MCGAffineTransformToSkMatrix(const MCGAffineTransform& transform, SkMatrix& r_matrix);
void MCGAffineTransformFromSkMatrix(const SkMatrix &matrix, MCGAffineTransform &r_transform);
////////////////////////////////////////////////////////////////////////////////
inline SkColor MCGColorToSkColor(MCGColor p_color)
{
return p_color;
}
inline MCGColor MCGColorFromSkColor(SkColor p_color)
{
return p_color;
}
////////////////////////////////////////////////////////////////////////////////
inline MCGFloat MCGFloatDegreesToRadians(MCGFloat p_degrees)
{
return p_degrees * M_PI / 180;
}
inline MCGFloat MCGFloatDegreesFromRadians(MCGFloat p_radians)
{
return p_radians * 180 / M_PI;
}
inline MCGFloat MCGFloatCos(MCGFloat p_angle)
{
return cosf(p_angle);
}
inline MCGFloat MCGFloatSin(MCGFloat p_angle)
{
return sinf(p_angle);
}
////////////////////////////////////////////////////////////////////////////////
inline SkScalar MCGFloatToSkScalar(MCGFloat p_ploat)
{
return p_ploat;
}
inline MCGFloat MCGFloatFromSkScalar(SkScalar p_scalar)
{
return p_scalar;
}
inline SkScalar MCGCoordToSkCoord(MCGFloat p_coord)
{
// TODO: Shift coordinate appropriately
return p_coord;
}
inline MCGFloat MCGCoordFromSkCoord(SkScalar p_coord)
{
// TODO: Shift coordinate appropriately
return p_coord;
}
inline SkPaint::Join MCGJoinStyleToSkJoinStyle(MCGJoinStyle p_style)
{
switch (p_style)
{
case kMCGJoinStyleBevel:
return SkPaint::kBevel_Join;
case kMCGJoinStyleRound:
return SkPaint::kRound_Join;
case kMCGJoinStyleMiter:
return SkPaint::kMiter_Join;
default:
MCUnreachableReturn(SkPaint::kMiter_Join);
}
}
inline MCGJoinStyle MCGJoinStyleFromSkJoinStyle(SkPaint::Join p_style)
{
switch (p_style)
{
case SkPaint::kBevel_Join:
return kMCGJoinStyleBevel;
case SkPaint::kRound_Join:
return kMCGJoinStyleRound;
case SkPaint::kMiter_Join:
default:
return kMCGJoinStyleMiter;
}
}
inline SkPaint::Cap MCGCapStyleToSkCapStyle(MCGCapStyle p_style)
{
switch (p_style)
{
case kMCGCapStyleButt:
return SkPaint::kButt_Cap;
case kMCGCapStyleRound:
return SkPaint::kRound_Cap;
case kMCGCapStyleSquare:
return SkPaint::kSquare_Cap;
default:
MCUnreachableReturn(SkPaint::kButt_Cap);
}
}
inline MCGCapStyle MCGCapStyleFromSkCapStyle(SkPaint::Cap p_style)
{
switch (p_style)
{
case SkPaint::kRound_Cap:
return kMCGCapStyleRound;
case SkPaint::kSquare_Cap:
return kMCGCapStyleSquare;
default:
case SkPaint::kButt_Cap:
return kMCGCapStyleButt;
}
}
inline SkRect MCGRectangleToSkRect(MCGRectangle p_rect)
{
return SkRect::MakeXYWH(MCGCoordToSkCoord(p_rect . origin . x), MCGCoordToSkCoord(p_rect . origin . y), MCGFloatToSkScalar(p_rect . size . width), MCGFloatToSkScalar(p_rect . size . height));
}
inline MCGRectangle MCGRectangleFromSkRect(SkRect p_rect)
{
return MCGRectangleMake(MCGCoordFromSkCoord(p_rect . x()), MCGCoordFromSkCoord(p_rect . y()), p_rect . width(), p_rect . height());
}
inline SkPoint MCGPointToSkPoint(MCGPoint p_point)
{
return SkPoint::Make(MCGCoordToSkCoord(p_point . x), MCGCoordToSkCoord(p_point . y));
}
inline MCGPoint MCGPointFromSkPoint(SkPoint p_point)
{
MCGPoint t_point;
t_point . x = MCGCoordFromSkCoord(p_point . x());
t_point . y = MCGCoordFromSkCoord(p_point . y());
return t_point;
}
inline SkPath::FillType MCGFillRuleToSkFillType(MCGFillRule p_rule)
{
switch (p_rule)
{
case kMCGFillRuleNonZero:
return SkPath::kWinding_FillType;
case kMCGFillRuleEvenOdd:
return SkPath::kEvenOdd_FillType;
default:
MCUnreachableReturn(SkPath::kEvenOdd_FillType);
}
}
inline MCGFillRule MCGFillRuleToSkFillType(SkPath::FillType p_fill_type)
{
switch (p_fill_type)
{
case SkPath::kWinding_FillType:
return kMCGFillRuleNonZero;
case SkPath::kEvenOdd_FillType:
default:
return kMCGFillRuleEvenOdd;
}
}
////////////////////////////////////////////////////////////////////////////////
inline SkColorType MCGRasterFormatToSkBitmapConfig(MCGRasterFormat p_format)
{
switch (p_format)
{
case kMCGRasterFormat_A:
return kAlpha_8_SkColorType;
case kMCGRasterFormat_ARGB:
case kMCGRasterFormat_U_ARGB:
case kMCGRasterFormat_xRGB:
return kN32_SkColorType;
default:
MCUnreachableReturn(kAlpha_8_SkColorType);
}
}
// IM-2014-05-20: [[ GraphicsPerformance ]] Use bitmap opaqueness when determining raster format
inline MCGRasterFormat MCGRasterFormatFromSkImageInfo(const SkImageInfo& p_config, bool p_opaque)
{
switch (p_config.colorType())
{
case kAlpha_8_SkColorType:
return kMCGRasterFormat_A;
case kBGRA_8888_SkColorType:
case kRGBA_8888_SkColorType:
return p_opaque ? kMCGRasterFormat_xRGB : kMCGRasterFormat_ARGB;
default:
MCUnreachableReturn(kMCGRasterFormat_A);
}
}
////////////////////////////////////////////////////////////////////////////////
bool MCGImageCreateWithSkBitmap(const SkBitmap &p_bitmap, MCGImageRef &r_image);
////////////////////////////////////////////////////////////////////////////////
enum MCGBlurType
{
kMCGBlurTypeNormal,
kMCGBlurTypeInner,
kMCGBlurTypeInvertedInner,
kMCGBlurTypeOuter,
};
bool MCGBlurBox(const SkMask& p_src, SkScalar p_x_radius, SkScalar p_y_radius, SkScalar p_x_spread, SkScalar p_y_spread, SkMask& r_dst);
////////////////////////////////////////////////////////////////////////////////
typedef struct __MCGCacheTable *MCGCacheTableRef;
bool MCGCacheTableCreate(uindex_t size, uindex_t max_occupancy, uindex_t max_bytes, MCGCacheTableRef &r_cache_table);
void MCGCacheTableDestroy(MCGCacheTableRef cache_table);
void MCGCacheTableCompact(MCGCacheTableRef cache_table);
void MCGCacheTableSet(MCGCacheTableRef cache_table, void *key, uint32_t key_length, void *value, uint32_t value_length);
void *MCGCacheTableGet(MCGCacheTableRef cache_table, void *key, uint32_t key_length);
////////////////////////////////////////////////////////////////////////////////
void MCGContextSetFillPaint(MCGContextRef p_context, MCGPaintRef p_paint);
void MCGContextSetStrokePaint(MCGContextRef p_context, MCGPaintRef p_paint);
bool MCGContextSetupFill(MCGContextRef p_context, SkPaint& r_paint);
// MM-2014-04-16: [[ Bug 11964 ]] Updated prototype to take transform parameter.
MCGFloat __MCGContextMeasurePlatformText(MCGContextRef self, const unichar_t *p_text, uindex_t p_length, const MCGFont &p_font, const MCGAffineTransform &p_transform);
////////////////////////////////////////////////////////////////////////////////
void MCGPlatformInitialize(void);
void MCGPlatformFinalize(void);
void MCGTextMeasureCacheInitialize(void);
void MCGTextMeasureCacheFinalize(void);
void MCGTextMeasureCacheCompact(void);
void MCGBlendModesInitialize(void);
void MCGBlendModesFinalize(void);
////////////////////////////////////////////////////////////////////////////////
struct __MCGRegion
{
SkRegion region;
};
////////////////////////////////////////////////////////////////////////////////
#endif