aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowPaint.java
blob: a23985865c3beb688f2358d6d8e7af83e5277ef7 (plain)
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
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.L;
import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.N;
import static android.os.Build.VERSION_CODES.N_MR1;
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.O_MR1;
import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.Q;
import static org.robolectric.annotation.TextLayoutMode.Mode.REALISTIC;

import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.PathEffect;
import android.graphics.Shader;
import android.graphics.Typeface;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.InDevelopment;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.TextLayoutMode;
import org.robolectric.config.ConfigurationRegistry;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.robolectric.versioning.AndroidVersions.U;
import org.robolectric.versioning.AndroidVersions.V;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(value = Paint.class, looseSignatures = true)
public class ShadowPaint {

  private int color;
  private Paint.Style style;
  private Paint.Cap cap;
  private Paint.Join join;
  private float width;
  private float shadowRadius;
  private float shadowDx;
  private float shadowDy;
  private int shadowColor;
  private Shader shader;
  private int alpha;
  private ColorFilter filter;
  private boolean antiAlias;
  private boolean dither;
  private int flags;
  private PathEffect pathEffect;
  private float letterSpacing;
  private float textScaleX = 1f;
  private float textSkewX;
  private float wordSpacing;

  @RealObject Paint paint;
  private Typeface typeface;
  private float textSize;
  private Paint.Align textAlign = Paint.Align.LEFT;

  @Implementation
  protected void __constructor__(Paint otherPaint) {
    ShadowPaint otherShadowPaint = Shadow.extract(otherPaint);
    this.color = otherShadowPaint.color;
    this.style = otherShadowPaint.style;
    this.cap = otherShadowPaint.cap;
    this.join = otherShadowPaint.join;
    this.width = otherShadowPaint.width;
    this.shadowRadius = otherShadowPaint.shadowRadius;
    this.shadowDx = otherShadowPaint.shadowDx;
    this.shadowDy = otherShadowPaint.shadowDy;
    this.shadowColor = otherShadowPaint.shadowColor;
    this.shader = otherShadowPaint.shader;
    this.alpha = otherShadowPaint.alpha;
    this.filter = otherShadowPaint.filter;
    this.antiAlias = otherShadowPaint.antiAlias;
    this.dither = otherShadowPaint.dither;
    this.flags = otherShadowPaint.flags;
    this.pathEffect = otherShadowPaint.pathEffect;
    this.letterSpacing = otherShadowPaint.letterSpacing;
    this.textScaleX = otherShadowPaint.textScaleX;
    this.textSkewX = otherShadowPaint.textSkewX;
    this.wordSpacing = otherShadowPaint.wordSpacing;

    Shadow.invokeConstructor(Paint.class, paint, ClassParameter.from(Paint.class, otherPaint));
  }

  @Implementation(minSdk = N)
  protected static long nInit() {
    return 1;
  }

  @Implementation
  protected int getFlags() {
    return flags;
  }

  @Implementation
  protected void setFlags(int flags) {
    this.flags = flags;
  }

  @Implementation
  protected void setUnderlineText(boolean underlineText) {
    if (underlineText) {
      setFlags(flags | Paint.UNDERLINE_TEXT_FLAG);
    } else {
      setFlags(flags & ~Paint.UNDERLINE_TEXT_FLAG);
    }
  }

  @Implementation
  protected void setStrikeThruText(boolean strikeThruText) {
    if (strikeThruText) {
      setFlags(flags | Paint.STRIKE_THRU_TEXT_FLAG);
    } else {
      setFlags(flags & ~Paint.STRIKE_THRU_TEXT_FLAG);
    }
  }

  @Implementation
  protected Shader setShader(Shader shader) {
    this.shader = shader;
    return shader;
  }

  @Implementation
  protected int getAlpha() {
    return alpha;
  }

  @Implementation
  protected void setAlpha(int alpha) {
    this.alpha = alpha;
  }

  @Implementation
  protected Shader getShader() {
    return shader;
  }

  @Implementation
  protected void setColor(int color) {
    this.color = color;
  }

  @Implementation
  protected int getColor() {
    return color;
  }

  @Implementation
  protected void setStyle(Paint.Style style) {
    this.style = style;
  }

  @Implementation
  protected Paint.Style getStyle() {
    return style;
  }

  @Implementation
  protected void setStrokeCap(Paint.Cap cap) {
    this.cap = cap;
  }

  @Implementation
  protected Paint.Cap getStrokeCap() {
    return cap;
  }

  @Implementation
  protected void setStrokeJoin(Paint.Join join) {
    this.join = join;
  }

  @Implementation
  protected Paint.Join getStrokeJoin() {
    return join;
  }

  @Implementation
  protected void setStrokeWidth(float width) {
    this.width = width;
  }

  @Implementation
  protected float getStrokeWidth() {
    return width;
  }

  @Implementation
  protected void setShadowLayer(float radius, float dx, float dy, int color) {
    shadowRadius = radius;
    shadowDx = dx;
    shadowDy = dy;
    shadowColor = color;
  }

  @Implementation
  protected Typeface getTypeface() {
    return typeface;
  }

  @Implementation
  protected Typeface setTypeface(Typeface typeface) {
    this.typeface = typeface;
    return typeface;
  }

  @Implementation
  protected float getTextSize() {
    return textSize;
  }

  @Implementation
  protected void setTextSize(float textSize) {
    this.textSize = textSize;
  }

  @Implementation
  protected float getTextScaleX() {
    return textScaleX;
  }

  @Implementation
  protected void setTextScaleX(float scaleX) {
    this.textScaleX = scaleX;
  }

  @Implementation
  protected float getTextSkewX() {
    return textSkewX;
  }

  @Implementation
  protected void setTextSkewX(float skewX) {
    this.textSkewX = skewX;
  }

  @Implementation(minSdk = L)
  protected float getLetterSpacing() {
    return letterSpacing;
  }

  @Implementation(minSdk = L)
  protected void setLetterSpacing(float letterSpacing) {
    this.letterSpacing = letterSpacing;
  }

  @Implementation(minSdk = Q)
  protected float getWordSpacing() {
    return wordSpacing;
  }

  @Implementation(minSdk = Q)
  protected void setWordSpacing(float wordSpacing) {
    this.wordSpacing = wordSpacing;
  }

  @Implementation
  protected void setTextAlign(Paint.Align align) {
    textAlign = align;
  }

  @Implementation
  protected Paint.Align getTextAlign() {
    return textAlign;
  }

  /**
   * @return shadow radius (Paint related shadow, not Robolectric Shadow)
   */
  public float getShadowRadius() {
    return shadowRadius;
  }

  /**
   * @return shadow Dx (Paint related shadow, not Robolectric Shadow)
   */
  public float getShadowDx() {
    return shadowDx;
  }

  /**
   * @return shadow Dx (Paint related shadow, not Robolectric Shadow)
   */
  public float getShadowDy() {
    return shadowDy;
  }

  /**
   * @return shadow color (Paint related shadow, not Robolectric Shadow)
   */
  public int getShadowColor() {
    return shadowColor;
  }

  public Paint.Cap getCap() {
    return cap;
  }

  public Paint.Join getJoin() {
    return join;
  }

  public float getWidth() {
    return width;
  }

  @Implementation
  protected ColorFilter getColorFilter() {
    return filter;
  }

  @Implementation
  protected ColorFilter setColorFilter(ColorFilter filter) {
    this.filter = filter;
    return filter;
  }

  @Implementation
  protected void setAntiAlias(boolean antiAlias) {
    this.flags = (flags & ~Paint.ANTI_ALIAS_FLAG) | (antiAlias ? Paint.ANTI_ALIAS_FLAG : 0);
  }

  @Implementation
  protected void setDither(boolean dither) {
    this.dither = dither;
  }

  @Implementation
  protected boolean isDither() {
    return dither;
  }

  @Implementation
  protected boolean isAntiAlias() {
    return (flags & Paint.ANTI_ALIAS_FLAG) == Paint.ANTI_ALIAS_FLAG;
  }

  @Implementation
  protected boolean isFilterBitmap() {
    return (flags & Paint.FILTER_BITMAP_FLAG) == Paint.FILTER_BITMAP_FLAG;
  }

  @Implementation
  protected void setFilterBitmap(boolean filterBitmap) {
    this.flags =
        (flags & ~Paint.FILTER_BITMAP_FLAG) | (filterBitmap ? Paint.FILTER_BITMAP_FLAG : 0);
  }

  @Implementation
  protected PathEffect getPathEffect() {
    return pathEffect;
  }

  @Implementation
  protected PathEffect setPathEffect(PathEffect effect) {
    this.pathEffect = effect;
    return effect;
  }

  @Implementation
  protected float measureText(String text) {
    return applyTextScaleX(text.length());
  }

  @Implementation
  protected float measureText(CharSequence text, int start, int end) {
    return applyTextScaleX(end - start);
  }

  @Implementation
  protected float measureText(String text, int start, int end) {
    return applyTextScaleX(end - start);
  }

  @Implementation
  protected float measureText(char[] text, int index, int count) {
    return applyTextScaleX(count);
  }

  private float applyTextScaleX(float textWidth) {
    return Math.max(0f, textScaleX) * textWidth;
  }

  @Implementation(maxSdk = M)
  protected static int native_breakText(
      long native_object,
      long native_typeface,
      char[] text,
      int index,
      int count,
      float maxWidth,
      int bidiFlags,
      float[] measuredWidth) {
    return breakText(text, maxWidth, measuredWidth);
  }

  @Implementation(minSdk = N, maxSdk = O_MR1)
  protected static int nBreakText(
      long nObject,
      long nTypeface,
      char[] text,
      int index,
      int count,
      float maxWidth,
      int bidiFlags,
      float[] measuredWidth) {
    return breakText(text, maxWidth, measuredWidth);
  }

  @Implementation(minSdk = P)
  protected static int nBreakText(
      long nObject,
      char[] text,
      int index,
      int count,
      float maxWidth,
      int bidiFlags,
      float[] measuredWidth) {
    return breakText(text, maxWidth, measuredWidth);
  }

  private static int breakText(char[] text, float maxWidth, float[] measuredWidth) {
    if (measuredWidth != null) {
      measuredWidth[0] = maxWidth;
    }
    return text.length;
  }

  @Implementation(maxSdk = M)
  protected static int native_breakText(
      long native_object,
      long native_typeface,
      String text,
      boolean measureForwards,
      float maxWidth,
      int bidiFlags,
      float[] measuredWidth) {
    return breakText(text, maxWidth, measuredWidth);
  }

  @Implementation(minSdk = N, maxSdk = O_MR1)
  protected static int nBreakText(
      long nObject,
      long nTypeface,
      String text,
      boolean measureForwards,
      float maxWidth,
      int bidiFlags,
      float[] measuredWidth) {
    return breakText(text, maxWidth, measuredWidth);
  }

  @Implementation(minSdk = P)
  protected static int nBreakText(
      long nObject,
      String text,
      boolean measureForwards,
      float maxWidth,
      int bidiFlags,
      float[] measuredWidth) {
    return breakText(text, maxWidth, measuredWidth);
  }

  private static int breakText(String text, float maxWidth, float[] measuredWidth) {
    if (measuredWidth != null) {
      measuredWidth[0] = maxWidth;
    }
    return text.length();
  }

  @Implementation(minSdk = V.SDK_INT)
  protected static int nGetFontMetricsInt(
      long paintPtr, FontMetricsInt fmi, /* Ignored */ boolean useLocale) {
    return nGetFontMetricsInt(paintPtr, fmi);
  }

  @Implementation(minSdk = P, maxSdk = U.SDK_INT)
  protected static int nGetFontMetricsInt(long paintPtr, FontMetricsInt fmi) {
    if (ConfigurationRegistry.get(TextLayoutMode.Mode.class) == REALISTIC) {
      // TODO: hack, just set values to those we see on emulator
      int descent = 7;
      int ascent = -28;
      int leading = 0;

      if (fmi != null) {
        fmi.top = -32;
        fmi.ascent = ascent;
        fmi.descent = descent;
        fmi.bottom = 9;
        fmi.leading = leading;
      }
      return descent - ascent + leading;
    }
    return 0;
  }

  @Implementation(minSdk = O, maxSdk = O_MR1)
  protected static int nGetFontMetricsInt(
      long nativePaint, long nativeTypeface, FontMetricsInt fmi) {
    return nGetFontMetricsInt(nativePaint, fmi);
  }

  @Implementation(minSdk = N, maxSdk = N_MR1)
  protected int nGetFontMetricsInt(Object nativePaint, Object nativeTypeface, Object fmi) {
    return nGetFontMetricsInt((long) nativePaint, (FontMetricsInt) fmi);
  }

  @Implementation(maxSdk = M)
  protected int getFontMetricsInt(FontMetricsInt fmi) {
    return nGetFontMetricsInt(0, fmi);
  }

  @Implementation(minSdk = P)
  protected static float nGetRunAdvance(
      long paintPtr,
      char[] text,
      int start,
      int end,
      int contextStart,
      int contextEnd,
      boolean isRtl,
      int offset) {
    if (ConfigurationRegistry.get(TextLayoutMode.Mode.class) == REALISTIC) {
      // be consistent with measureText for measurements, and measure 1 pixel per char
      return end - start;
    }
    return 0f;
  }

  @Implementation(minSdk = U.SDK_INT, maxSdk = U.SDK_INT)
  protected static float nGetRunCharacterAdvance(
      long paintPtr,
      char[] text,
      int start,
      int end,
      int contextStart,
      int contextEnd,
      boolean isRtl,
      int offset,
      float[] advances,
      int advancesIndex) {
    return nGetRunAdvance(paintPtr, text, start, end, contextStart, contextEnd, isRtl, offset);
  }

  @Implementation(minSdk = V.SDK_INT)
  @InDevelopment
  protected static float nGetRunCharacterAdvance(
      Object /* long */ paintPtr,
      Object /* char[] */ text,
      Object /* int */ start,
      Object /* int */ end,
      Object /* int */ contextStart,
      Object /* int */ contextEnd,
      Object /* boolean */ isRtl,
      Object /* int */ offset,
      Object /* float[] */ advances,
      Object /* int */ advancesIndex,
      Object /* RectF */ drawingBounds,
      Object /* RunInfo */ runInfo) {
    return nGetRunCharacterAdvance(
        (long) paintPtr,
        (char[]) text,
        (int) start,
        (int) end,
        (int) contextStart,
        (int) contextEnd,
        (boolean) isRtl,
        (int) offset,
        (float[]) advances,
        (int) advancesIndex);
  }

  @Implementation(minSdk = N, maxSdk = O_MR1)
  protected static float nGetRunAdvance(
      long paintPtr,
      long typefacePtr,
      char[] text,
      int start,
      int end,
      int contextStart,
      int contextEnd,
      boolean isRtl,
      int offset) {
    return nGetRunAdvance(paintPtr, text, start, end, contextStart, contextEnd, isRtl, offset);
  }

  @Implementation(minSdk = M, maxSdk = M)
  protected static float native_getRunAdvance(
      long nativeObject,
      long nativeTypeface,
      char[] text,
      int start,
      int end,
      int contextStart,
      int contextEnd,
      boolean isRtl,
      int offset) {
    return nGetRunAdvance(0, text, start, end, contextStart, contextEnd, isRtl, offset);
  }

  @Implementation(maxSdk = LOLLIPOP_MR1)
  protected static float native_getTextRunAdvances(
      long nativeObject,
      long nativeTypeface,
      char[] text,
      int index,
      int count,
      int contextIndex,
      int contextCount,
      boolean isRtl,
      float[] advances,
      int advancesIndex) {
    return nGetRunAdvance(
        0, text, index, index + count, contextIndex, contextIndex + contextCount, isRtl, index);
  }

  @Implementation(maxSdk = LOLLIPOP_MR1)
  protected static float native_getTextRunAdvances(
      long nativeObject,
      long nativeTypeface,
      String text,
      int start,
      int end,
      int contextStart,
      int contextEnd,
      boolean isRtl,
      float[] advances,
      int advancesIndex) {
    return nGetRunAdvance(0, text.toCharArray(), start, end, contextStart, contextEnd, isRtl, 0);
  }
}