• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.N;
4 
5 import android.graphics.ColorFilter;
6 import android.graphics.Paint;
7 import android.graphics.PathEffect;
8 import android.graphics.Shader;
9 import android.graphics.Typeface;
10 import org.robolectric.annotation.Implementation;
11 import org.robolectric.annotation.Implements;
12 import org.robolectric.annotation.RealObject;
13 import org.robolectric.shadow.api.Shadow;
14 import org.robolectric.util.ReflectionHelpers.ClassParameter;
15 
16 @SuppressWarnings({"UnusedDeclaration"})
17 @Implements(Paint.class)
18 public class ShadowPaint {
19 
20   private int color;
21   private Paint.Style style;
22   private Paint.Cap cap;
23   private Paint.Join join;
24   private float width;
25   private float shadowRadius;
26   private float shadowDx;
27   private float shadowDy;
28   private int shadowColor;
29   private Shader shader;
30   private int alpha;
31   private ColorFilter filter;
32   private boolean antiAlias;
33   private boolean dither;
34   private int flags;
35   private PathEffect pathEffect;
36 
37   @RealObject Paint paint;
38   private Typeface typeface;
39   private float textSize;
40   private Paint.Align textAlign = Paint.Align.LEFT;
41 
42   @Implementation
__constructor__(Paint otherPaint)43   protected void __constructor__(Paint otherPaint) {
44     ShadowPaint otherShadowPaint = Shadow.extract(otherPaint);
45     this.color = otherShadowPaint.color;
46     this.style = otherShadowPaint.style;
47     this.cap = otherShadowPaint.cap;
48     this.join = otherShadowPaint.join;
49     this.width = otherShadowPaint.width;
50     this.shadowRadius = otherShadowPaint.shadowRadius;
51     this.shadowDx = otherShadowPaint.shadowDx;
52     this.shadowDy = otherShadowPaint.shadowDy;
53     this.shadowColor = otherShadowPaint.shadowColor;
54     this.shader = otherShadowPaint.shader;
55     this.alpha = otherShadowPaint.alpha;
56     this.filter = otherShadowPaint.filter;
57     this.antiAlias = otherShadowPaint.antiAlias;
58     this.dither = otherShadowPaint.dither;
59     this.flags = otherShadowPaint.flags;
60     this.pathEffect = otherShadowPaint.pathEffect;
61 
62     Shadow.invokeConstructor(Paint.class, paint, ClassParameter.from(Paint.class, otherPaint));
63   }
64 
65   @Implementation(minSdk = N)
nInit()66   protected static long nInit() {
67     return 1;
68   }
69 
70   @Implementation
getFlags()71   protected int getFlags() {
72     return flags;
73   }
74 
75   @Implementation
setFlags(int flags)76   protected void setFlags(int flags) {
77     this.flags = flags;
78   }
79 
80   @Implementation
setShader(Shader shader)81   protected Shader setShader(Shader shader) {
82     this.shader = shader;
83     return shader;
84   }
85 
86   @Implementation
getAlpha()87   protected int getAlpha() {
88     return alpha;
89   }
90 
91   @Implementation
setAlpha(int alpha)92   protected void setAlpha(int alpha) {
93     this.alpha = alpha;
94   }
95 
96   @Implementation
getShader()97   protected Shader getShader() {
98     return shader;
99   }
100 
101   @Implementation
setColor(int color)102   protected void setColor(int color) {
103     this.color = color;
104   }
105 
106   @Implementation
getColor()107   protected int getColor() {
108     return color;
109   }
110 
111   @Implementation
setStyle(Paint.Style style)112   protected void setStyle(Paint.Style style) {
113     this.style = style;
114   }
115 
116   @Implementation
getStyle()117   protected Paint.Style getStyle() {
118     return style;
119   }
120 
121   @Implementation
setStrokeCap(Paint.Cap cap)122   protected void setStrokeCap(Paint.Cap cap) {
123     this.cap = cap;
124   }
125 
126   @Implementation
getStrokeCap()127   protected Paint.Cap getStrokeCap() {
128     return cap;
129   }
130 
131   @Implementation
setStrokeJoin(Paint.Join join)132   protected void setStrokeJoin(Paint.Join join) {
133     this.join = join;
134   }
135 
136   @Implementation
getStrokeJoin()137   protected Paint.Join getStrokeJoin() {
138     return join;
139   }
140 
141   @Implementation
setStrokeWidth(float width)142   protected void setStrokeWidth(float width) {
143     this.width = width;
144   }
145 
146   @Implementation
getStrokeWidth()147   protected float getStrokeWidth() {
148     return width;
149   }
150 
151   @Implementation
setShadowLayer(float radius, float dx, float dy, int color)152   protected void setShadowLayer(float radius, float dx, float dy, int color) {
153     shadowRadius = radius;
154     shadowDx = dx;
155     shadowDy = dy;
156     shadowColor = color;
157   }
158 
159   @Implementation
getTypeface()160   protected Typeface getTypeface() {
161     return typeface;
162   }
163 
164   @Implementation
setTypeface(Typeface typeface)165   protected Typeface setTypeface(Typeface typeface) {
166     this.typeface = typeface;
167     return typeface;
168   }
169 
170   @Implementation
getTextSize()171   protected float getTextSize() {
172     return textSize;
173   }
174 
175   @Implementation
setTextSize(float textSize)176   protected void setTextSize(float textSize) {
177     this.textSize = textSize;
178   }
179 
180   @Implementation
setTextAlign(Paint.Align align)181   protected void setTextAlign(Paint.Align align) {
182     textAlign = align;
183   }
184 
185   @Implementation
getTextAlign()186   protected Paint.Align getTextAlign() {
187     return textAlign;
188   }
189 
190   /**
191    * @return shadow radius (Paint related shadow, not Robolectric Shadow)
192    */
getShadowRadius()193   public float getShadowRadius() {
194     return shadowRadius;
195   }
196 
197   /**
198    * @return shadow Dx (Paint related shadow, not Robolectric Shadow)
199    */
getShadowDx()200   public float getShadowDx() {
201     return shadowDx;
202   }
203 
204   /**
205    * @return shadow Dx (Paint related shadow, not Robolectric Shadow)
206    */
getShadowDy()207   public float getShadowDy() {
208     return shadowDy;
209   }
210 
211   /**
212    * @return shadow color (Paint related shadow, not Robolectric Shadow)
213    */
getShadowColor()214   public int getShadowColor() {
215     return shadowColor;
216   }
217 
getCap()218   public Paint.Cap getCap() {
219     return cap;
220   }
221 
getJoin()222   public Paint.Join getJoin() {
223     return join;
224   }
225 
getWidth()226   public float getWidth() {
227     return width;
228   }
229 
230   @Implementation
getColorFilter()231   protected ColorFilter getColorFilter() {
232     return filter;
233   }
234 
235   @Implementation
setColorFilter(ColorFilter filter)236   protected ColorFilter setColorFilter(ColorFilter filter) {
237     this.filter = filter;
238     return filter;
239   }
240 
241   @Implementation
setAntiAlias(boolean antiAlias)242   protected void setAntiAlias(boolean antiAlias) {
243     this.flags = (flags & ~Paint.ANTI_ALIAS_FLAG) | (antiAlias ? Paint.ANTI_ALIAS_FLAG : 0);
244   }
245 
246   @Implementation
setDither(boolean dither)247   protected void setDither(boolean dither) {
248     this.dither = dither;
249   }
250 
251   @Implementation
isDither()252   protected final boolean isDither() {
253     return dither;
254   }
255 
256   @Implementation
isAntiAlias()257   protected final boolean isAntiAlias() {
258     return (flags & Paint.ANTI_ALIAS_FLAG) == Paint.ANTI_ALIAS_FLAG;
259   }
260 
261   @Implementation
getPathEffect()262   protected PathEffect getPathEffect() {
263     return pathEffect;
264   }
265 
266   @Implementation
setPathEffect(PathEffect effect)267   protected PathEffect setPathEffect(PathEffect effect) {
268     this.pathEffect = effect;
269     return effect;
270   }
271 
272   @Implementation
measureText(String text)273   protected float measureText(String text) {
274     return text.length();
275   }
276 
277   @Implementation
measureText(CharSequence text, int start, int end)278   protected float measureText(CharSequence text, int start, int end) {
279     return end - start;
280   }
281 
282   @Implementation
measureText(String text, int start, int end)283   protected float measureText(String text, int start, int end) {
284     return end - start;
285   }
286 
287   @Implementation
measureText(char[] text, int index, int count)288   protected float measureText(char[] text, int index, int count) {
289     return count;
290   }
291 }
292