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
522 lines (419 loc) · 14.2 KB
/
Copy pathgraphics-internal.h
File metadata and controls
522 lines (419 loc) · 14.2 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
/* Copyright (C) 2003-2013 Runtime Revolution 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>
#ifndef M_PI
#define M_PI 3.141592653589793238462643
#endif
#ifdef __MOBILE
#define kMCGTextMeasureCacheTableSize 16384
#else
#define kMCGTextMeasureCacheTableSize 65535
#endif
#define kMCGTextMeasureCacheByteSize kMCGTextMeasureCacheTableSize * 256
#define kMCGTextMeasureCacheMaxOccupancy kMCGTextMeasureCacheTableSize * 0.5
#define kMCGTextMeasureCacheMaxStringLength 65536
////////////////////////////////////////////////////////////////////////////////
typedef struct __MCGPattern *MCGPatternRef;
struct __MCGPattern
{
MCGImageRef pattern;
MCGAffineTransform transform;
MCGImageFilter filter;
uint32_t references;
};
bool MCGPatternCreate(MCGImageRef image, MCGAffineTransform transform, MCGImageFilter filter, MCGPatternRef& r_pattern);
MCGPatternRef MCGPatternRetain(MCGPatternRef pattern);
void MCGPatternRelease(MCGPatternRef pattern);
bool MCGPatternToSkShader(MCGPatternRef pattern, SkShader*& r_shader);
////////////////////////////////////////////////////////////////////////////////
typedef struct __MCGGradient *MCGGradientRef;
struct __MCGGradient
{
MCGGradientFunction function;
MCGColor *colors;
MCGFloat *stops;
uindex_t ramp_length;
bool mirror;
bool wrap;
uint32_t repeats;
MCGAffineTransform transform;
MCGImageFilter filter;
uint32_t references;
};
bool MCGGradientCreate(MCGGradientFunction function, const MCGFloat* stops, const MCGColor* colors, uindex_t ramp_length, bool mirror, bool wrap, uint32_t repeats, MCGAffineTransform transform, MCGImageFilter filter, MCGGradientRef& r_gradient);
MCGGradientRef MCGGradientRetain(MCGGradientRef gradient);
void MCGGradientRelease(MCGGradientRef gradient);
bool MCGGradientToSkShader(MCGGradientRef gradient, MCGRectangle clip, SkShader*& r_shader);
////////////////////////////////////////////////////////////////////////////////
typedef struct __MCGContextState *MCGContextStateRef;
struct __MCGContextState
{
MCGFloat opacity;
MCGBlendMode blend_mode;
MCGFloat flatness;
bool should_antialias;
MCGColor fill_color;
MCGFloat fill_opacity;
MCGFillRule fill_rule;
MCGPatternRef fill_pattern;
MCGGradientRef fill_gradient;
MCGPaintStyle fill_style;
MCGColor stroke_color;
MCGFloat stroke_opacity;
MCGStrokeAttr stroke_attr;
MCGPatternRef stroke_pattern;
MCGGradientRef stroke_gradient;
MCGPaintStyle stroke_style;
bool is_layer_begin_pt;
MCGContextStateRef parent;
};
typedef struct __MCGContextLayer *MCGContextLayerRef;
struct __MCGContextLayer
{
SkCanvas *canvas;
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, SkDashPathEffect*& 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);
SkXfermode* MCGBlendModeToSkXfermode(MCGBlendMode mode);
MCGBlendMode MCGBlendModeToSkXfermode(SkXfermode* mode);
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:
MCUnreachable();
}
}
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:
MCUnreachable();
}
}
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:
MCUnreachable();
}
}
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 SkBitmap::Config MCGRasterFormatToSkBitmapConfig(MCGRasterFormat p_format)
{
switch (p_format)
{
case kMCGRasterFormat_A:
return SkBitmap::kA8_Config;
case kMCGRasterFormat_ARGB:
case kMCGRasterFormat_U_ARGB:
case kMCGRasterFormat_xRGB:
return SkBitmap::kARGB_8888_Config;
}
}
// IM-2014-05-20: [[ GraphicsPerformance ]] Use bitmap opaqueness when determining raster format
inline MCGRasterFormat MCGRasterFormatFromSkBitmapConfig(SkBitmap::Config p_config, bool p_opaque)
{
switch (p_config)
{
case SkBitmap::kA8_Config:
return kMCGRasterFormat_A;
case SkBitmap::kARGB_8888_Config:
return p_opaque ? kMCGRasterFormat_xRGB : kMCGRasterFormat_ARGB;
default:
MCUnreachable();
}
}
////////////////////////////////////////////////////////////////////////////////
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);
////////////////////////////////////////////////////////////////////////////////
// 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);
////////////////////////////////////////////////////////////////////////////////
class MCGLegacyBlendMode : public SkXfermode
{
public:
MCGLegacyBlendMode(MCGBlendMode blend_mode);
/*virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count, const SkAlpha aa[]) const;
virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count, const SkAlpha aa[]) const;
virtual void xfer4444(uint16_t dst[], const SkPMColor src[], int count, const SkAlpha aa[]) const;
virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count, const SkAlpha aa[]) const;*/
virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst) const;
virtual bool asCoeff(Coeff* src, Coeff* dst) const;
virtual bool asMode(Mode* mode) const;
virtual Factory getFactory() const SK_OVERRIDE { return nil; }
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE {}
private:
uint32_t m_function;
};
////////////////////////////////////////////////////////////////////////////////
typedef struct MCGradientAffineCombiner MCGradientCombiner_t;
class MCGLegacyGradientShader : public SkShader
{
public:
MCGLegacyGradientShader(MCGGradientRef gradient_ref, MCGRectangle clip);
~MCGLegacyGradientShader();
virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE;
virtual uint32_t getFlags() SK_OVERRIDE;
virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE;
virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count) SK_OVERRIDE;
virtual SkShader::BitmapType asABitmap(SkBitmap*, SkMatrix*, TileMode[2]) const SK_OVERRIDE;
virtual void flatten(SkFlattenableWriteBuffer& ) const SK_OVERRIDE {} ;
virtual Factory getFactory() const SK_OVERRIDE { return nil; }
private:
MCGGradientRef m_gradient_ref;
int32_t m_y;
uint8_t *m_mask;
MCGRectangle m_clip;
MCGradientCombiner_t *m_gradient_combiner;
typedef SkShader INHERITED;
};
////////////////////////////////////////////////////////////////////////////////
struct __MCGRegion
{
SkRegion region;
};
////////////////////////////////////////////////////////////////////////////////
#endif