-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RenderableShadowNode.java
518 lines (441 loc) · 17.3 KB
/
RenderableShadowNode.java
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
/*
* Copyright (c) 2015-present, Horcrux.
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.horcrux.svg;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.JavaOnlyArray;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.uimanager.annotations.ReactProp;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
/**
* Renderable shadow node
*/
@SuppressWarnings("WeakerAccess")
abstract public class RenderableShadowNode extends VirtualNode {
// strokeLinecap
private static final int CAP_BUTT = 0;
static final int CAP_ROUND = 1;
private static final int CAP_SQUARE = 2;
// strokeLinejoin
private static final int JOIN_BEVEL = 2;
private static final int JOIN_MITER = 0;
static final int JOIN_ROUND = 1;
// fillRule
private static final int FILL_RULE_EVENODD = 0;
static final int FILL_RULE_NONZERO = 1;
public @Nullable ReadableArray mStroke;
public @Nullable String[] mStrokeDasharray;
public String mStrokeWidth = "1";
public float mStrokeOpacity = 1;
public float mStrokeMiterlimit = 4;
public float mStrokeDashoffset = 0;
public Paint.Cap mStrokeLinecap = Paint.Cap.ROUND;
public Paint.Join mStrokeLinejoin = Paint.Join.ROUND;
public @Nullable ReadableArray mFill;
public float mFillOpacity = 1;
public Path.FillType mFillRule = Path.FillType.WINDING;
private @Nullable ArrayList<String> mLastMergedList;
private @Nullable ArrayList<Object> mOriginProperties;
protected @Nullable ArrayList<String> mPropList;
protected @Nullable ArrayList<String> mAttributeList;
static final Pattern regex = Pattern.compile("[0-9.-]+");
@ReactProp(name = "fill")
public void setFill(@Nullable Dynamic fill) {
if (fill == null || fill.isNull()) {
mFill = null;
markUpdated();
return;
}
ReadableType type = fill.getType();
if (type.equals(ReadableType.Array)) {
mFill = fill.asArray();
} else {
JavaOnlyArray arr = new JavaOnlyArray();
arr.pushInt(0);
Matcher m = regex.matcher(fill.asString());
int i = 0;
while (m.find()) {
Double parsed = Double.parseDouble(m.group());
arr.pushDouble(i++ < 3 ? parsed / 255 : parsed);
}
mFill = arr;
}
markUpdated();
}
@ReactProp(name = "fillOpacity", defaultFloat = 1f)
public void setFillOpacity(float fillOpacity) {
mFillOpacity = fillOpacity;
markUpdated();
}
@ReactProp(name = "fillRule", defaultInt = FILL_RULE_NONZERO)
public void setFillRule(int fillRule) {
switch (fillRule) {
case FILL_RULE_EVENODD:
mFillRule = Path.FillType.EVEN_ODD;
break;
case FILL_RULE_NONZERO:
break;
default:
throw new JSApplicationIllegalArgumentException(
"fillRule " + mFillRule + " unrecognized");
}
markUpdated();
}
@ReactProp(name = "stroke")
public void setStroke(@Nullable Dynamic strokeColors) {
if (strokeColors == null || strokeColors.isNull()) {
mStroke = null;
markUpdated();
return;
}
ReadableType type = strokeColors.getType();
if (type.equals(ReadableType.Array)) {
mStroke = strokeColors.asArray();
} else {
JavaOnlyArray arr = new JavaOnlyArray();
arr.pushInt(0);
Matcher m = regex.matcher(strokeColors.asString());
while (m.find()) {
Double parsed = Double.parseDouble(m.group());
arr.pushDouble(parsed);
}
mStroke = arr;
}
markUpdated();
}
@ReactProp(name = "strokeOpacity", defaultFloat = 1f)
public void setStrokeOpacity(float strokeOpacity) {
mStrokeOpacity = strokeOpacity;
markUpdated();
}
@ReactProp(name = "strokeDasharray")
public void setStrokeDasharray(@Nullable ReadableArray strokeDasharray) {
if (strokeDasharray != null) {
int fromSize = strokeDasharray.size();
mStrokeDasharray = new String[fromSize];
for (int i = 0; i < fromSize; i++) {
mStrokeDasharray[i] = strokeDasharray.getString(i);
}
} else {
mStrokeDasharray = null;
}
markUpdated();
}
@ReactProp(name = "strokeDashoffset")
public void setStrokeDashoffset(float strokeDashoffset) {
mStrokeDashoffset = strokeDashoffset * mScale;
markUpdated();
}
@ReactProp(name = "strokeWidth")
public void setStrokeWidth(String strokeWidth) {
mStrokeWidth = strokeWidth;
markUpdated();
}
@ReactProp(name = "strokeMiterlimit", defaultFloat = 4f)
public void setStrokeMiterlimit(float strokeMiterlimit) {
mStrokeMiterlimit = strokeMiterlimit;
markUpdated();
}
@ReactProp(name = "strokeLinecap", defaultInt = CAP_ROUND)
public void setStrokeLinecap(int strokeLinecap) {
switch (strokeLinecap) {
case CAP_BUTT:
mStrokeLinecap = Paint.Cap.BUTT;
break;
case CAP_SQUARE:
mStrokeLinecap = Paint.Cap.SQUARE;
break;
case CAP_ROUND:
mStrokeLinecap = Paint.Cap.ROUND;
break;
default:
throw new JSApplicationIllegalArgumentException(
"strokeLinecap " + mStrokeLinecap + " unrecognized");
}
markUpdated();
}
@ReactProp(name = "strokeLinejoin", defaultInt = JOIN_ROUND)
public void setStrokeLinejoin(int strokeLinejoin) {
switch (strokeLinejoin) {
case JOIN_MITER:
mStrokeLinejoin = Paint.Join.MITER;
break;
case JOIN_BEVEL:
mStrokeLinejoin = Paint.Join.BEVEL;
break;
case JOIN_ROUND:
mStrokeLinejoin = Paint.Join.ROUND;
break;
default:
throw new JSApplicationIllegalArgumentException(
"strokeLinejoin " + mStrokeLinejoin + " unrecognized");
}
markUpdated();
}
@ReactProp(name = "propList")
public void setPropList(@Nullable ReadableArray propList) {
if (propList != null) {
mPropList = mAttributeList = new ArrayList<>();
for (int i = 0; i < propList.size(); i++) {
String fieldName = propertyNameToFieldName(propList.getString(i));
mPropList.add(fieldName);
}
}
markUpdated();
}
static double saturate(double v) {
return v <= 0 ? 0 : (v >= 1 ? 1 : v);
}
public void render(Canvas canvas, Paint paint, float opacity) {
if (mMask != null) {
SvgViewShadowNode root = getSvgShadowNode();
MaskShadowNode mask = (MaskShadowNode) root.getDefinedMask(mMask);
Rect clipBounds = canvas.getClipBounds();
int height = clipBounds.height();
int width = clipBounds.width();
Bitmap maskBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap original = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas originalCanvas = new Canvas(original);
Canvas maskCanvas = new Canvas(maskBitmap);
Canvas resultCanvas = new Canvas(result);
// Clip to mask bounds and render the mask
float maskX = (float) relativeOnWidth(mask.mX);
float maskY = (float) relativeOnWidth(mask.mY);
float maskWidth = (float) relativeOnWidth(mask.mWidth);
float maskHeight = (float) relativeOnWidth(mask.mHeight);
maskCanvas.clipRect(maskX, maskY, maskWidth, maskHeight);
Paint maskpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mask.draw(maskCanvas, maskpaint, 1);
// Apply luminanceToAlpha filter primitive https://www.w3.org/TR/SVG11/filters.html#feColorMatrixElement
int nPixels = width * height;
int[] pixels = new int[nPixels];
maskBitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 0; i < nPixels; i++) {
int color = pixels[i];
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
int a = color >>> 24;
double luminance = saturate(((0.299 * r) + (0.587 * g) + (0.144 * b)) / 255);
int alpha = (int) (a * luminance);
int pixel = (alpha << 24);
pixels[i] = pixel;
}
maskBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
// Render content of current SVG Renderable to image
draw(originalCanvas, paint, opacity);
// Blend current element and mask
maskpaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
resultCanvas.drawBitmap(original, 0, 0, null);
resultCanvas.drawBitmap(maskBitmap, 0, 0, maskpaint);
// Render blended result into current render context
canvas.drawBitmap(result, 0, 0, paint);
} else {
draw(canvas, paint, opacity);
}
}
@Override
public void draw(Canvas canvas, Paint paint, float opacity) {
opacity *= mOpacity;
if (opacity > MIN_OPACITY_FOR_DRAW) {
if (mPath == null) {
mPath = getPath(canvas, paint);
mPath.setFillType(mFillRule);
}
Path path = mPath;
RectF clientRect = new RectF();
path.computeBounds(clientRect, true);
Matrix svgToViewMatrix = new Matrix(canvas.getMatrix());
svgToViewMatrix.mapRect(clientRect);
this.setClientRect(clientRect);
clip(canvas, paint);
if (setupFillPaint(paint, opacity * mFillOpacity)) {
canvas.drawPath(path, paint);
}
if (setupStrokePaint(paint, opacity * mStrokeOpacity)) {
canvas.drawPath(path, paint);
}
}
}
/**
* Sets up paint according to the props set on a shadow view. Returns {@code true}
* if the fill should be drawn, {@code false} if not.
*/
private boolean setupFillPaint(Paint paint, float opacity) {
if (mFill != null && mFill.size() > 0) {
paint.reset();
paint.setFlags(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
setupPaint(paint, opacity, mFill);
return true;
}
return false;
}
/**
* Sets up paint according to the props set on a shadow view. Returns {@code true}
* if the stroke should be drawn, {@code false} if not.
*/
private boolean setupStrokePaint(Paint paint, float opacity) {
paint.reset();
double strokeWidth = relativeOnOther(mStrokeWidth);
if (strokeWidth == 0 || mStroke == null || mStroke.size() == 0) {
return false;
}
paint.setFlags(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeCap(mStrokeLinecap);
paint.setStrokeJoin(mStrokeLinejoin);
paint.setStrokeMiter(mStrokeMiterlimit * mScale);
paint.setStrokeWidth((float) strokeWidth);
setupPaint(paint, opacity, mStroke);
if (mStrokeDasharray != null) {
int length = mStrokeDasharray.length;
float[] intervals = new float[length];
for (int i = 0; i < length; i++) {
intervals[i] = (float)relativeOnOther(mStrokeDasharray[i]);
}
paint.setPathEffect(new DashPathEffect(intervals, mStrokeDashoffset));
}
return true;
}
private void setupPaint(Paint paint, float opacity, ReadableArray colors) {
int colorType = colors.getInt(0);
if (colorType == 0) {
// solid color
paint.setARGB(
(int) (colors.size() > 4 ? colors.getDouble(4) * opacity * 255 : opacity * 255),
(int) (colors.getDouble(1) * 255),
(int) (colors.getDouble(2) * 255),
(int) (colors.getDouble(3) * 255));
} else if (colorType == 1) {
Brush brush = getSvgShadowNode().getDefinedBrush(colors.getString(1));
if (brush != null) {
if (mBox == null) {
mBox = new RectF();
mPath.computeBounds(mBox, true);
}
brush.setupPaint(paint, mBox, mScale, opacity);
}
}
}
abstract protected Path getPath(Canvas canvas, Paint paint);
@Override
public int hitTest(final float[] src) {
if (mPath == null || !mInvertible) {
return -1;
}
float[] dst = new float[2];
mInvMatrix.mapPoints(dst, src);
int x = Math.round(dst[0]);
int y = Math.round(dst[1]);
if (mRegion == null && mPath != null) {
mRegion = getRegion(mPath);
}
if (mRegion == null || !mRegion.contains(x, y)) {
return -1;
}
Path clipPath = getClipPath();
if (clipPath != null) {
if (mClipRegionPath != clipPath) {
mClipRegionPath = clipPath;
mClipRegion = getRegion(clipPath);
}
if (!mClipRegion.contains(x, y)) {
return -1;
}
}
return getReactTag();
}
Region getRegion(Path path) {
RectF rectF = new RectF();
path.computeBounds(rectF, true);
Region region = new Region();
region.setPath(path,
new Region(
(int) Math.floor(rectF.left),
(int) Math.floor(rectF.top),
(int) Math.ceil(rectF.right),
(int) Math.ceil(rectF.bottom)
)
);
return region;
}
private ArrayList<String> getAttributeList() {
return mAttributeList;
}
void mergeProperties(RenderableShadowNode target) {
ArrayList<String> targetAttributeList = target.getAttributeList();
if (targetAttributeList == null ||
targetAttributeList.size() == 0) {
return;
}
mOriginProperties = new ArrayList<>();
mAttributeList = mPropList == null ? new ArrayList<String>() : new ArrayList<>(mPropList);
for (int i = 0, size = targetAttributeList.size(); i < size; i++) {
try {
String fieldName = targetAttributeList.get(i);
Field field = getClass().getField(fieldName);
Object value = field.get(target);
mOriginProperties.add(field.get(this));
if (!hasOwnProperty(fieldName)) {
mAttributeList.add(fieldName);
field.set(this, value);
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
mLastMergedList = targetAttributeList;
}
void resetProperties() {
if (mLastMergedList != null && mOriginProperties != null) {
try {
for (int i = mLastMergedList.size() - 1; i >= 0; i--) {
Field field = getClass().getField(mLastMergedList.get(i));
field.set(this, mOriginProperties.get(i));
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
mLastMergedList = null;
mOriginProperties = null;
mAttributeList = mPropList;
}
}
// convert propertyName something like fillOpacity to fieldName like mFillOpacity
private String propertyNameToFieldName(String fieldName) {
Pattern pattern = Pattern.compile("^(\\w)");
Matcher matched = pattern.matcher(fieldName);
StringBuffer sb = new StringBuffer("m");
while (matched.find()) {
matched.appendReplacement(sb, matched.group(1).toUpperCase());
}
matched.appendTail(sb);
return sb.toString();
}
private boolean hasOwnProperty(String propName) {
return mAttributeList != null && mAttributeList.contains(propName);
}
}