blob: 57dd429ac6b18ec3f0a47ac22a1c0ab0a114c893 [file] [log] [blame]
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -080019import com.android.ide.common.rendering.api.LayoutLog;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -080020import com.android.layoutlib.bridge.Bridge;
Xavier Ducrohet3bd98982010-11-09 18:25:03 -080021import com.android.layoutlib.bridge.impl.DelegateManager;
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -080022import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070023
Deepanshu Gupta442aee62015-05-22 14:11:22 -070024import android.annotation.NonNull;
25import android.annotation.Nullable;
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -070026import android.graphics.FontFamily_Delegate.FontVariant;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070027import android.graphics.Paint.FontMetrics;
28import android.graphics.Paint.FontMetricsInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -070029import android.text.TextUtils;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070030
Xavier Ducrohet37f21802010-11-01 16:17:18 -070031import java.awt.BasicStroke;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070032import java.awt.Font;
Xavier Ducrohetb9761242010-12-23 10:22:14 -080033import java.awt.Shape;
34import java.awt.Stroke;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070035import java.awt.Toolkit;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070036import java.awt.geom.AffineTransform;
37import java.util.ArrayList;
38import java.util.Collections;
39import java.util.List;
Xavier Ducrohet43526ab2012-04-23 17:41:37 -070040import java.util.Locale;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070041
42/**
43 * Delegate implementing the native methods of android.graphics.Paint
44 *
45 * Through the layoutlib_create tool, the original native methods of Paint have been replaced
46 * by calls to methods of the same name in this delegate class.
47 *
48 * This class behaves like the original native implementation, but in Java, keeping previously
49 * native data into its own objects and mapping them to int that are sent back and forth between
50 * it and the original Paint class.
51 *
52 * @see DelegateManager
53 *
54 */
55public class Paint_Delegate {
56
57 /**
Deepanshu Gupta017c0622014-05-19 16:14:23 -070058 * Class associating a {@link Font} and its {@link java.awt.FontMetrics}.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070059 */
Xavier Ducrohet37f21802010-11-01 16:17:18 -070060 /*package*/ static final class FontInfo {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070061 Font mFont;
62 java.awt.FontMetrics mMetrics;
63 }
64
65 // ---- delegate manager ----
66 private static final DelegateManager<Paint_Delegate> sManager =
Xavier Ducrohetb2de8392011-02-23 16:51:08 -080067 new DelegateManager<Paint_Delegate>(Paint_Delegate.class);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070068
69 // ---- delegate helper data ----
Deepanshu Gupta382256f2014-08-09 14:14:32 -070070
71 // This list can contain null elements.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070072 private List<FontInfo> mFonts;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070073
74 // ---- delegate data ----
75 private int mFlags;
76 private int mColor;
77 private int mStyle;
78 private int mCap;
79 private int mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -070080 private int mTextAlign;
Xavier Ducrohet91672792011-02-22 11:54:37 -080081 private Typeface_Delegate mTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070082 private float mStrokeWidth;
83 private float mStrokeMiter;
84 private float mTextSize;
85 private float mTextScaleX;
86 private float mTextSkewX;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -070087 private int mHintingMode = Paint.HINTING_ON;
Deepanshu Gupta9fe7fca2015-05-12 12:01:16 -070088 private int mHyphenEdit;
89 private float mLetterSpacing; // not used in actual text rendering.
Deepanshu Gupta8916b212014-06-11 15:40:47 -070090 // Variant of the font. A paint's variant can only be compact or elegant.
91 private FontVariant mFontVariant = FontVariant.COMPACT;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -070092
Xavier Ducrohet91672792011-02-22 11:54:37 -080093 private Xfermode_Delegate mXfermode;
94 private ColorFilter_Delegate mColorFilter;
95 private Shader_Delegate mShader;
96 private PathEffect_Delegate mPathEffect;
97 private MaskFilter_Delegate mMaskFilter;
98 private Rasterizer_Delegate mRasterizer;
Xavier Ducroheta313b652010-11-01 18:45:20 -070099
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700100 private Locale mLocale = Locale.getDefault();
101
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -0700102 // Used only to assert invariants.
103 public long mNativeTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700104
105 // ---- Public Helper methods ----
106
Deepanshu Gupta9fe7fca2015-05-12 12:01:16 -0700107 @Nullable
Narayan Kamath633d6882014-01-27 14:24:16 +0000108 public static Paint_Delegate getDelegate(long native_paint) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700109 return sManager.getDelegate(native_paint);
110 }
111
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700112 /**
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700113 * Returns the list of {@link Font} objects.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700114 */
115 public List<FontInfo> getFonts() {
116 return mFonts;
117 }
118
Xavier Ducroheta313b652010-11-01 18:45:20 -0700119 public boolean isAntiAliased() {
120 return (mFlags & Paint.ANTI_ALIAS_FLAG) != 0;
121 }
122
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700123 public boolean isFilterBitmap() {
124 return (mFlags & Paint.FILTER_BITMAP_FLAG) != 0;
125 }
126
127 public int getStyle() {
128 return mStyle;
129 }
130
131 public int getColor() {
132 return mColor;
133 }
134
Xavier Ducrohet66225222010-12-21 01:33:04 -0800135 public int getAlpha() {
136 return mColor >>> 24;
137 }
138
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800139 public void setAlpha(int alpha) {
140 mColor = (alpha << 24) | (mColor & 0x00FFFFFF);
141 }
142
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700143 public int getTextAlign() {
144 return mTextAlign;
145 }
146
147 public float getStrokeWidth() {
148 return mStrokeWidth;
149 }
150
Xavier Ducrohet66225222010-12-21 01:33:04 -0800151 /**
152 * returns the value of stroke miter needed by the java api.
153 */
154 public float getJavaStrokeMiter() {
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800155 float miter = mStrokeMiter * mStrokeWidth;
156 if (miter < 1.f) {
157 miter = 1.f;
158 }
159 return miter;
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700160 }
161
162 public int getJavaCap() {
163 switch (Paint.sCapArray[mCap]) {
164 case BUTT:
165 return BasicStroke.CAP_BUTT;
166 case ROUND:
167 return BasicStroke.CAP_ROUND;
168 default:
169 case SQUARE:
170 return BasicStroke.CAP_SQUARE;
171 }
172 }
173
174 public int getJavaJoin() {
175 switch (Paint.sJoinArray[mJoin]) {
176 default:
177 case MITER:
178 return BasicStroke.JOIN_MITER;
179 case ROUND:
180 return BasicStroke.JOIN_ROUND;
181 case BEVEL:
182 return BasicStroke.JOIN_BEVEL;
183 }
184 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700185
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800186 public Stroke getJavaStroke() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800187 if (mPathEffect != null) {
188 if (mPathEffect.isSupported()) {
189 Stroke stroke = mPathEffect.getStroke(this);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800190 assert stroke != null;
191 if (stroke != null) {
192 return stroke;
193 }
194 } else {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800195 Bridge.getLog().fidelityWarning(LayoutLog.TAG_PATHEFFECT,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800196 mPathEffect.getSupportMessage(),
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800197 null, null /*data*/);
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800198 }
199 }
200
201 // if no custom stroke as been set, set the default one.
202 return new BasicStroke(
203 getStrokeWidth(),
204 getJavaCap(),
205 getJavaJoin(),
206 getJavaStrokeMiter());
207 }
208
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800209 /**
210 * Returns the {@link Xfermode} delegate or null if none have been set
211 *
212 * @return the delegate or null.
213 */
214 public Xfermode_Delegate getXfermode() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800215 return mXfermode;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700216 }
217
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800218 /**
219 * Returns the {@link ColorFilter} delegate or null if none have been set
220 *
221 * @return the delegate or null.
222 */
223 public ColorFilter_Delegate getColorFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800224 return mColorFilter;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700225 }
226
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800227 /**
228 * Returns the {@link Shader} delegate or null if none have been set
229 *
230 * @return the delegate or null.
231 */
232 public Shader_Delegate getShader() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800233 return mShader;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700234 }
235
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800236 /**
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800237 * Returns the {@link MaskFilter} delegate or null if none have been set
238 *
239 * @return the delegate or null.
240 */
241 public MaskFilter_Delegate getMaskFilter() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800242 return mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800243 }
244
245 /**
246 * Returns the {@link Rasterizer} delegate or null if none have been set
247 *
248 * @return the delegate or null.
249 */
250 public Rasterizer_Delegate getRasterizer() {
Xavier Ducrohet91672792011-02-22 11:54:37 -0800251 return mRasterizer;
Xavier Ducrohetf1a174522010-11-01 22:02:08 -0700252 }
253
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700254 // ---- native methods ----
255
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800256 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700257 /*package*/ static int getFlags(Paint thisPaint) {
258 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400259 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700260 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700261 return 0;
262 }
263
264 return delegate.mFlags;
265 }
266
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700267
268
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800269 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700270 /*package*/ static void setFlags(Paint thisPaint, int flags) {
271 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400272 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700273 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700274 return;
275 }
276
277 delegate.mFlags = flags;
278 }
279
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800280 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700281 /*package*/ static void setFilterBitmap(Paint thisPaint, boolean filter) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700282 setFlag(thisPaint, Paint.FILTER_BITMAP_FLAG, filter);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700283 }
284
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800285 @LayoutlibDelegate
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700286 /*package*/ static int getHinting(Paint thisPaint) {
287 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400288 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700289 if (delegate == null) {
290 return Paint.HINTING_ON;
291 }
292
293 return delegate.mHintingMode;
294 }
295
296 @LayoutlibDelegate
297 /*package*/ static void setHinting(Paint thisPaint, int mode) {
298 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400299 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -0700300 if (delegate == null) {
301 return;
302 }
303
304 delegate.mHintingMode = mode;
305 }
306
307 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700308 /*package*/ static void setAntiAlias(Paint thisPaint, boolean aa) {
309 setFlag(thisPaint, Paint.ANTI_ALIAS_FLAG, aa);
310 }
311
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800312 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700313 /*package*/ static void setSubpixelText(Paint thisPaint, boolean subpixelText) {
314 setFlag(thisPaint, Paint.SUBPIXEL_TEXT_FLAG, subpixelText);
315 }
316
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800317 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700318 /*package*/ static void setUnderlineText(Paint thisPaint, boolean underlineText) {
319 setFlag(thisPaint, Paint.UNDERLINE_TEXT_FLAG, underlineText);
320 }
321
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800322 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700323 /*package*/ static void setStrikeThruText(Paint thisPaint, boolean strikeThruText) {
324 setFlag(thisPaint, Paint.STRIKE_THRU_TEXT_FLAG, strikeThruText);
325 }
326
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800327 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700328 /*package*/ static void setFakeBoldText(Paint thisPaint, boolean fakeBoldText) {
329 setFlag(thisPaint, Paint.FAKE_BOLD_TEXT_FLAG, fakeBoldText);
330 }
331
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800332 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700333 /*package*/ static void setDither(Paint thisPaint, boolean dither) {
334 setFlag(thisPaint, Paint.DITHER_FLAG, dither);
335 }
336
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800337 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700338 /*package*/ static void setLinearText(Paint thisPaint, boolean linearText) {
339 setFlag(thisPaint, Paint.LINEAR_TEXT_FLAG, linearText);
340 }
341
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800342 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700343 /*package*/ static int getColor(Paint thisPaint) {
344 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400345 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700346 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700347 return 0;
348 }
349
350 return delegate.mColor;
351 }
352
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800353 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700354 /*package*/ static void setColor(Paint thisPaint, int color) {
355 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400356 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700357 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700358 return;
359 }
360
361 delegate.mColor = color;
362 }
363
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800364 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700365 /*package*/ static int getAlpha(Paint thisPaint) {
366 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400367 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700368 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700369 return 0;
370 }
371
Xavier Ducrohet66225222010-12-21 01:33:04 -0800372 return delegate.getAlpha();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700373 }
374
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800375 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700376 /*package*/ static void setAlpha(Paint thisPaint, int a) {
377 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400378 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700379 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700380 return;
381 }
382
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -0800383 delegate.setAlpha(a);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700384 }
385
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800386 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700387 /*package*/ static float getStrokeWidth(Paint thisPaint) {
388 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400389 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700390 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700391 return 1.f;
392 }
393
394 return delegate.mStrokeWidth;
395 }
396
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800397 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700398 /*package*/ static void setStrokeWidth(Paint thisPaint, float width) {
399 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400400 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700401 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700402 return;
403 }
404
405 delegate.mStrokeWidth = width;
406 }
407
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800408 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700409 /*package*/ static float getStrokeMiter(Paint thisPaint) {
410 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400411 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700412 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700413 return 1.f;
414 }
415
416 return delegate.mStrokeMiter;
417 }
418
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800419 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700420 /*package*/ static void setStrokeMiter(Paint thisPaint, float miter) {
421 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400422 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700423 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700424 return;
425 }
426
427 delegate.mStrokeMiter = miter;
428 }
429
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800430 @LayoutlibDelegate
Deepanshu Gupta0bec6852014-05-15 09:30:11 -0700431 /*package*/ static void native_setShadowLayer(long paint, float radius, float dx, float dy,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700432 int color) {
433 // FIXME
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800434 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
Xavier Ducrohetf69bb292011-01-14 16:40:43 -0800435 "Paint.setShadowLayer is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700436 }
437
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800438 @LayoutlibDelegate
Deepanshu Gupta0bec6852014-05-15 09:30:11 -0700439 /*package*/ static boolean native_hasShadowLayer(long paint) {
440 // FIXME
441 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
442 "Paint.hasShadowLayer is not supported.", null, null /*data*/);
443 return false;
444 }
445
446 @LayoutlibDelegate
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700447 /*package*/ static boolean isElegantTextHeight(Paint thisPaint) {
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700448 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400449 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -0700450 return delegate != null && delegate.mFontVariant == FontVariant.ELEGANT;
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700451 }
452
453 @LayoutlibDelegate
454 /*package*/ static void setElegantTextHeight(Paint thisPaint, boolean elegant) {
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700455 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400456 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700457 if (delegate == null) {
458 return;
459 }
460
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -0700461 delegate.mFontVariant = elegant ? FontVariant.ELEGANT : FontVariant.COMPACT;
Deepanshu Guptabd49b122014-04-18 15:43:06 -0700462 }
463
464 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700465 /*package*/ static float getTextSize(Paint thisPaint) {
466 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400467 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700468 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700469 return 1.f;
470 }
471
472 return delegate.mTextSize;
473 }
474
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800475 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700476 /*package*/ static void setTextSize(Paint thisPaint, float textSize) {
477 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400478 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700479 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700480 return;
481 }
482
483 delegate.mTextSize = textSize;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800484 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700485 }
486
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800487 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700488 /*package*/ static float getTextScaleX(Paint thisPaint) {
489 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400490 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700491 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700492 return 1.f;
493 }
494
495 return delegate.mTextScaleX;
496 }
497
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800498 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700499 /*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) {
500 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400501 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700502 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700503 return;
504 }
505
506 delegate.mTextScaleX = scaleX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800507 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700508 }
509
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800510 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700511 /*package*/ static float getTextSkewX(Paint thisPaint) {
512 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400513 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700514 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700515 return 1.f;
516 }
517
518 return delegate.mTextSkewX;
519 }
520
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800521 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700522 /*package*/ static void setTextSkewX(Paint thisPaint, float skewX) {
523 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -0400524 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700525 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700526 return;
527 }
528
529 delegate.mTextSkewX = skewX;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800530 delegate.updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700531 }
532
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800533 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700534 /*package*/ static float ascent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800535 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400536 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800537 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800538 return 0;
539 }
540
541 if (delegate.mFonts.size() > 0) {
542 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
543 // Android expects negative ascent so we invert the value from Java.
544 return - javaMetrics.getAscent();
545 }
546
547 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700548 }
549
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800550 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700551 /*package*/ static float descent(Paint thisPaint) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800552 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400553 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800554 if (delegate == null) {
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800555 return 0;
556 }
557
558 if (delegate.mFonts.size() > 0) {
559 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
560 return javaMetrics.getDescent();
561 }
562
563 return 0;
564
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700565 }
566
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800567 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700568 /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700569 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400570 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700571 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700572 return 0;
573 }
574
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -0800575 return delegate.getFontMetrics(metrics);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700576 }
577
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800578 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700579 /*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700580 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400581 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700582 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700583 return 0;
584 }
585
586 if (delegate.mFonts.size() > 0) {
587 java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics;
588 if (fmi != null) {
589 // Android expects negative ascent so we invert the value from Java.
590 fmi.top = - javaMetrics.getMaxAscent();
591 fmi.ascent = - javaMetrics.getAscent();
592 fmi.descent = javaMetrics.getDescent();
593 fmi.bottom = javaMetrics.getMaxDescent();
594 fmi.leading = javaMetrics.getLeading();
595 }
596
597 return javaMetrics.getHeight();
598 }
599
600 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700601 }
602
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800603 @LayoutlibDelegate
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700604 /*package*/ static float native_measureText(Paint thisPaint, char[] text, int index,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700605 int count, int bidiFlags) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700606 // get the delegate
Derek Sollenberger82934b52014-10-15 13:50:33 -0400607 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700608 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700609 return 0;
610 }
611
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700612 RectF bounds = delegate.measureText(text, index, count, null, 0, bidiFlags);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800613 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700614 }
615
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800616 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700617 /*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end,
618 int bidiFlags) {
619 return native_measureText(thisPaint, text.toCharArray(), start, end - start, bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700620 }
621
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800622 @LayoutlibDelegate
Deepanshu Guptac398f182013-05-23 15:20:04 -0700623 /*package*/ static float native_measureText(Paint thisPaint, String text, int bidiFlags) {
624 return native_measureText(thisPaint, text.toCharArray(), 0, text.length(), bidiFlags);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700625 }
626
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800627 @LayoutlibDelegate
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700628 /*package*/ static int native_breakText(long nativePaint, long nativeTypeface, char[] text,
629 int index, int count, float maxWidth, int bidiFlags, float[] measuredWidth) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800630
631 // get the delegate
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700632 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800633 if (delegate == null) {
634 return 0;
635 }
636
637 int inc = count > 0 ? 1 : -1;
638
639 int measureIndex = 0;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800640 for (int i = index; i != index + count; i += inc, measureIndex++) {
641 int start, end;
642 if (i < index) {
643 start = i;
644 end = index;
645 } else {
646 start = index;
647 end = i;
648 }
649
650 // measure from start to end
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700651 RectF bounds = delegate.measureText(text, start, end - start + 1, null, 0, bidiFlags);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -0800652 float res = bounds.right - bounds.left;
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800653
654 if (measuredWidth != null) {
655 measuredWidth[measureIndex] = res;
656 }
657
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -0800658 if (res > maxWidth) {
659 // we should not return this char index, but since it's 0-based
660 // and we need to return a count, we simply return measureIndex;
661 return measureIndex;
662 }
663
664 }
665
666 return measureIndex;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700667 }
668
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800669 @LayoutlibDelegate
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700670 /*package*/ static int native_breakText(long nativePaint, long nativeTypeface, String text,
671 boolean measureForwards,
Deepanshu Guptac398f182013-05-23 15:20:04 -0700672 float maxWidth, int bidiFlags, float[] measuredWidth) {
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700673 return native_breakText(nativePaint, nativeTypeface, text.toCharArray(), 0, text.length(),
674 maxWidth, bidiFlags, measuredWidth);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700675 }
676
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800677 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000678 /*package*/ static long native_init() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700679 Paint_Delegate newDelegate = new Paint_Delegate();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800680 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700681 }
682
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800683 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000684 /*package*/ static long native_initWithPaint(long paint) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700685 // get the delegate from the native int.
686 Paint_Delegate delegate = sManager.getDelegate(paint);
687 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700688 return 0;
689 }
690
691 Paint_Delegate newDelegate = new Paint_Delegate(delegate);
Xavier Ducrohet91672792011-02-22 11:54:37 -0800692 return sManager.addNewDelegate(newDelegate);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700693 }
694
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800695 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000696 /*package*/ static void native_reset(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700697 // get the delegate from the native int.
698 Paint_Delegate delegate = sManager.getDelegate(native_object);
699 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700700 return;
701 }
702
703 delegate.reset();
704 }
705
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800706 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000707 /*package*/ static void native_set(long native_dst, long native_src) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700708 // get the delegate from the native int.
709 Paint_Delegate delegate_dst = sManager.getDelegate(native_dst);
710 if (delegate_dst == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700711 return;
712 }
713
714 // get the delegate from the native int.
715 Paint_Delegate delegate_src = sManager.getDelegate(native_src);
716 if (delegate_src == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700717 return;
718 }
719
720 delegate_dst.set(delegate_src);
721 }
722
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800723 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800724 /*package*/ static int native_getStyle(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700725 // get the delegate from the native int.
726 Paint_Delegate delegate = sManager.getDelegate(native_object);
727 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700728 return 0;
729 }
730
731 return delegate.mStyle;
732 }
733
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800734 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000735 /*package*/ static void native_setStyle(long native_object, int style) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700736 // get the delegate from the native int.
737 Paint_Delegate delegate = sManager.getDelegate(native_object);
738 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700739 return;
740 }
741
742 delegate.mStyle = style;
743 }
744
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800745 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800746 /*package*/ static int native_getStrokeCap(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700747 // get the delegate from the native int.
748 Paint_Delegate delegate = sManager.getDelegate(native_object);
749 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700750 return 0;
751 }
752
753 return delegate.mCap;
754 }
755
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800756 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000757 /*package*/ static void native_setStrokeCap(long native_object, int cap) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700758 // get the delegate from the native int.
759 Paint_Delegate delegate = sManager.getDelegate(native_object);
760 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700761 return;
762 }
763
764 delegate.mCap = cap;
765 }
766
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800767 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800768 /*package*/ static int native_getStrokeJoin(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700769 // get the delegate from the native int.
770 Paint_Delegate delegate = sManager.getDelegate(native_object);
771 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700772 return 0;
773 }
774
775 return delegate.mJoin;
776 }
777
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800778 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000779 /*package*/ static void native_setStrokeJoin(long native_object, int join) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700780 // get the delegate from the native int.
781 Paint_Delegate delegate = sManager.getDelegate(native_object);
782 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700783 return;
784 }
785
786 delegate.mJoin = join;
787 }
788
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800789 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000790 /*package*/ static boolean native_getFillPath(long native_object, long src, long dst) {
Xavier Ducrohetb9761242010-12-23 10:22:14 -0800791 Paint_Delegate paint = sManager.getDelegate(native_object);
792 if (paint == null) {
793 return false;
794 }
795
796 Path_Delegate srcPath = Path_Delegate.getDelegate(src);
797 if (srcPath == null) {
798 return true;
799 }
800
801 Path_Delegate dstPath = Path_Delegate.getDelegate(dst);
802 if (dstPath == null) {
803 return true;
804 }
805
806 Stroke stroke = paint.getJavaStroke();
807 Shape strokeShape = stroke.createStrokedShape(srcPath.getJavaShape());
808
809 dstPath.setJavaShape(strokeShape);
810
811 // FIXME figure out the return value?
812 return true;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700813 }
814
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800815 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000816 /*package*/ static long native_setShader(long native_object, long shader) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700817 // get the delegate from the native int.
818 Paint_Delegate delegate = sManager.getDelegate(native_object);
819 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700820 return shader;
821 }
822
Xavier Ducrohet91672792011-02-22 11:54:37 -0800823 delegate.mShader = Shader_Delegate.getDelegate(shader);
824
825 return shader;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700826 }
827
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800828 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000829 /*package*/ static long native_setColorFilter(long native_object, long filter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700830 // get the delegate from the native int.
831 Paint_Delegate delegate = sManager.getDelegate(native_object);
832 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700833 return filter;
834 }
835
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700836 delegate.mColorFilter = ColorFilter_Delegate.getDelegate(filter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800837
Deepanshu Gupta1128e782014-06-21 19:45:30 -0700838 // Log warning if it's not supported.
839 if (delegate.mColorFilter != null && !delegate.mColorFilter.isSupported()) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800840 Bridge.getLog().fidelityWarning(LayoutLog.TAG_COLORFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800841 delegate.mColorFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800842 }
843
844 return filter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700845 }
846
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800847 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000848 /*package*/ static long native_setXfermode(long native_object, long xfermode) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700849 // get the delegate from the native int.
850 Paint_Delegate delegate = sManager.getDelegate(native_object);
851 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700852 return xfermode;
853 }
854
Xavier Ducrohet91672792011-02-22 11:54:37 -0800855 delegate.mXfermode = Xfermode_Delegate.getDelegate(xfermode);
856
857 return xfermode;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700858 }
859
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800860 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000861 /*package*/ static long native_setPathEffect(long native_object, long effect) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700862 // get the delegate from the native int.
863 Paint_Delegate delegate = sManager.getDelegate(native_object);
864 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700865 return effect;
866 }
867
Xavier Ducrohet91672792011-02-22 11:54:37 -0800868 delegate.mPathEffect = PathEffect_Delegate.getDelegate(effect);
869
870 return effect;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700871 }
872
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800873 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000874 /*package*/ static long native_setMaskFilter(long native_object, long maskfilter) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700875 // get the delegate from the native int.
876 Paint_Delegate delegate = sManager.getDelegate(native_object);
877 if (delegate == null) {
Xavier Ducroheta313b652010-11-01 18:45:20 -0700878 return maskfilter;
879 }
880
Xavier Ducrohet91672792011-02-22 11:54:37 -0800881 delegate.mMaskFilter = MaskFilter_Delegate.getDelegate(maskfilter);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800882
883 // since none of those are supported, display a fidelity warning right away
Deepanshu Gupta1128e782014-06-21 19:45:30 -0700884 if (delegate.mMaskFilter != null && !delegate.mMaskFilter.isSupported()) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800885 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800886 delegate.mMaskFilter.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800887 }
888
889 return maskfilter;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700890 }
891
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800892 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000893 /*package*/ static long native_setTypeface(long native_object, long typeface) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700894 // get the delegate from the native int.
895 Paint_Delegate delegate = sManager.getDelegate(native_object);
896 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700897 return 0;
898 }
899
Xavier Ducrohet91672792011-02-22 11:54:37 -0800900 delegate.mTypeface = Typeface_Delegate.getDelegate(typeface);
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -0700901 delegate.mNativeTypeface = typeface;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -0800902 delegate.updateFontObject();
Xavier Ducrohet91672792011-02-22 11:54:37 -0800903 return typeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700904 }
905
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800906 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000907 /*package*/ static long native_setRasterizer(long native_object, long rasterizer) {
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800908 // get the delegate from the native int.
909 Paint_Delegate delegate = sManager.getDelegate(native_object);
910 if (delegate == null) {
911 return rasterizer;
912 }
913
Xavier Ducrohet91672792011-02-22 11:54:37 -0800914 delegate.mRasterizer = Rasterizer_Delegate.getDelegate(rasterizer);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800915
916 // since none of those are supported, display a fidelity warning right away
Deepanshu Gupta1128e782014-06-21 19:45:30 -0700917 if (delegate.mRasterizer != null && !delegate.mRasterizer.isSupported()) {
Xavier Ducrohet3f9b0372011-01-13 10:59:34 -0800918 Bridge.getLog().fidelityWarning(LayoutLog.TAG_RASTERIZER,
Xavier Ducrohet91672792011-02-22 11:54:37 -0800919 delegate.mRasterizer.getSupportMessage(), null, null /*data*/);
Xavier Ducroheta6e51d52010-12-23 07:16:21 -0800920 }
921
922 return rasterizer;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700923 }
924
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800925 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800926 /*package*/ static int native_getTextAlign(long native_object) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700927 // get the delegate from the native int.
928 Paint_Delegate delegate = sManager.getDelegate(native_object);
929 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700930 return 0;
931 }
932
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700933 return delegate.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700934 }
935
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800936 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000937 /*package*/ static void native_setTextAlign(long native_object, int align) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700938 // get the delegate from the native int.
939 Paint_Delegate delegate = sManager.getDelegate(native_object);
940 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700941 return;
942 }
943
Xavier Ducrohet37f21802010-11-01 16:17:18 -0700944 delegate.mTextAlign = align;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700945 }
946
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800947 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +0000948 /*package*/ static void native_setTextLocale(long native_object, String locale) {
Xavier Ducrohet43526ab2012-04-23 17:41:37 -0700949 // get the delegate from the native int.
950 Paint_Delegate delegate = sManager.getDelegate(native_object);
951 if (delegate == null) {
952 return;
953 }
954
955 delegate.setTextLocale(locale);
956 }
957
958 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700959 /*package*/ static int native_getTextWidths(long native_object, long native_typeface,
960 char[] text, int index, int count, int bidiFlags, float[] widths) {
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700961
962 if (widths != null) {
963 for (int i = 0; i< count; i++) {
964 widths[i]=0;
965 }
966 }
967 // get the delegate from the native int.
968 Paint_Delegate delegate = sManager.getDelegate(native_object);
969 if (delegate == null) {
970 return 0;
971 }
972
973 // native_typeface is passed here since Framework's old implementation did not have the
974 // typeface object associated with the Paint. Since, we follow the new framework way,
975 // we store the typeface with the paint and use it directly.
976 assert (native_typeface == delegate.mNativeTypeface);
977
978 RectF bounds = delegate.measureText(text, index, count, widths, 0, bidiFlags);
979 return ((int) (bounds.right - bounds.left));
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700980 }
981
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800982 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -0700983 /*package*/ static int native_getTextWidths(long native_object, long native_typeface,
984 String text, int start, int end, int bidiFlags, float[] widths) {
985 return native_getTextWidths(native_object, native_typeface, text.toCharArray(), start,
986 end - start, bidiFlags, widths);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700987 }
988
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -0800989 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -0800990 /* package */static int native_getTextGlyphs(long native_object, String text, int start,
Xavier Ducrohet81efa7f2011-06-15 14:43:42 -0700991 int end, int contextStart, int contextEnd, int flags, char[] glyphs) {
992 // FIXME
993 return 0;
994 }
995
996 @LayoutlibDelegate
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -0700997 /*package*/ static float native_getTextRunAdvances(long native_object, long native_typeface,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -0700998 char[] text, int index, int count, int contextIndex, int contextCount,
Deepanshu Gupta237740b2014-06-25 17:47:16 -0700999 boolean isRtl, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001000
1001 if (advances != null)
1002 for (int i = advancesIndex; i< advancesIndex+count; i++)
1003 advances[i]=0;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001004 // get the delegate from the native int.
1005 Paint_Delegate delegate = sManager.getDelegate(native_object);
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001006 if (delegate == null) {
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001007 return 0.f;
1008 }
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -07001009
1010 // native_typeface is passed here since Framework's old implementation did not have the
1011 // typeface object associated with the Paint. Since, we follow the new framework way,
1012 // we store the typeface with the paint and use it directly.
1013 assert (native_typeface == delegate.mNativeTypeface);
1014
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001015 RectF bounds = delegate.measureText(text, index, count, advances, advancesIndex, isRtl);
Deepanshu Guptaeb998d32014-01-07 11:58:44 -08001016 return bounds.right - bounds.left;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001017 }
1018
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001019 @LayoutlibDelegate
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001020 /*package*/ static float native_getTextRunAdvances(long native_object, long native_typeface,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001021 String text, int start, int end, int contextStart, int contextEnd,
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001022 boolean isRtl, float[] advances, int advancesIndex) {
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001023 // FIXME: support contextStart and contextEnd
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001024 int count = end - start;
1025 char[] buffer = TemporaryBuffer.obtain(count);
1026 TextUtils.getChars(text, start, end, buffer, 0);
1027
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001028 return native_getTextRunAdvances(native_object, native_typeface, buffer, 0, count,
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001029 contextStart, contextEnd - contextStart, isRtl, advances, advancesIndex);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001030 }
1031
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001032 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001033 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, char[] text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001034 int contextStart, int contextLength, int flags, int offset, int cursorOpt) {
1035 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001036 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1037 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1038 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001039 }
1040
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001041 @LayoutlibDelegate
Deepanshu Guptaf9a82352014-02-21 16:22:10 -08001042 /*package*/ static int native_getTextRunCursor(Paint thisPaint, long native_object, String text,
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001043 int contextStart, int contextEnd, int flags, int offset, int cursorOpt) {
1044 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001045 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1046 "Paint.getTextRunCursor is not supported.", null, null /*data*/);
1047 return 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001048 }
1049
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001050 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001051 /*package*/ static void native_getTextPath(long native_object, long native_typeface,
1052 int bidiFlags, char[] text, int index, int count, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001053 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001054 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1055 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001056 }
1057
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001058 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001059 /*package*/ static void native_getTextPath(long native_object, long native_typeface,
1060 int bidiFlags, String text, int start, int end, float x, float y, long path) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001061 // FIXME
Xavier Ducrohet1eeaea02011-02-09 19:39:52 -08001062 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
1063 "Paint.getTextPath is not supported.", null, null /*data*/);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001064 }
1065
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001066 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001067 /*package*/ static void nativeGetStringBounds(long nativePaint, long native_typeface,
1068 String text, int start, int end, int bidiFlags, Rect bounds) {
1069 nativeGetCharArrayBounds(nativePaint, native_typeface, text.toCharArray(), start,
1070 end - start, bidiFlags, bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001071 }
1072
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001073 @LayoutlibDelegate
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001074 /*package*/ static void nativeGetCharArrayBounds(long nativePaint, long native_typeface,
1075 char[] text, int index, int count, int bidiFlags, Rect bounds) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001076
1077 // get the delegate from the native int.
1078 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
Deepanshu Gupta017c0622014-05-19 16:14:23 -07001079 if (delegate == null) {
Xavier Ducrohetf4d52a62011-02-23 09:53:56 -08001080 return;
1081 }
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -07001082
1083 // assert that the typeface passed is actually the one that we had stored.
1084 assert (native_typeface == delegate.mNativeTypeface);
1085
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001086 delegate.measureText(text, index, count, null, 0, bidiFlags).roundOut(bounds);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001087 }
1088
Xavier Ducrohet94c80bf2011-02-09 17:17:49 -08001089 @LayoutlibDelegate
Narayan Kamath633d6882014-01-27 14:24:16 +00001090 /*package*/ static void finalizer(long nativePaint) {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001091 sManager.removeJavaReferenceFor(nativePaint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001092 }
1093
Deepanshu Guptaa74fdf02014-07-30 20:18:48 -07001094 @LayoutlibDelegate
1095 /*package*/ static float native_getLetterSpacing(long nativePaint) {
Deepanshu Gupta9fe7fca2015-05-12 12:01:16 -07001096 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
1097 if (delegate == null) {
1098 return 0;
1099 }
1100 return delegate.mLetterSpacing;
Deepanshu Guptaa74fdf02014-07-30 20:18:48 -07001101 }
1102
1103 @LayoutlibDelegate
1104 /*package*/ static void native_setLetterSpacing(long nativePaint, float letterSpacing) {
Deepanshu Guptae4b3f9e2015-05-13 21:47:10 -07001105 Bridge.getLog().fidelityWarning(LayoutLog.TAG_TEXT_RENDERING,
1106 "Paint.setLetterSpacing() not supported.", null, null);
Deepanshu Gupta9fe7fca2015-05-12 12:01:16 -07001107 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
1108 if (delegate == null) {
1109 return;
1110 }
1111 delegate.mLetterSpacing = letterSpacing;
Deepanshu Guptaa74fdf02014-07-30 20:18:48 -07001112 }
1113
1114 @LayoutlibDelegate
1115 /*package*/ static void native_setFontFeatureSettings(long nativePaint, String settings) {
Deepanshu Guptae4b3f9e2015-05-13 21:47:10 -07001116 Bridge.getLog().fidelityWarning(LayoutLog.TAG_TEXT_RENDERING,
Deepanshu Gupta9fe7fca2015-05-12 12:01:16 -07001117 "Paint.setFontFeatureSettings() not supported.", null, null);
1118 }
1119
1120 @LayoutlibDelegate
1121 /*package*/ static int native_getHyphenEdit(long nativePaint) {
1122 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
1123 if (delegate == null) {
1124 return 0;
1125 }
1126 return delegate.mHyphenEdit;
1127 }
1128
1129 @LayoutlibDelegate
1130 /*package*/ static void native_setHyphenEdit(long nativePaint, int hyphen) {
1131 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
1132 if (delegate == null) {
1133 return;
1134 }
1135 delegate.mHyphenEdit = hyphen;
1136 }
1137
1138 @LayoutlibDelegate
1139 /*package*/ static boolean native_hasGlyph(long nativePaint, long nativeTypeface, int bidiFlags,
1140 String string) {
1141 Paint_Delegate delegate = sManager.getDelegate(nativePaint);
1142 if (delegate == null) {
1143 return false;
1144 }
1145 if (string.length() == 0) {
1146 return false;
1147 }
1148 if (string.length() > 1) {
Deepanshu Guptae4b3f9e2015-05-13 21:47:10 -07001149 Bridge.getLog().fidelityWarning(LayoutLog.TAG_TEXT_RENDERING,
Deepanshu Gupta9fe7fca2015-05-12 12:01:16 -07001150 "Paint.hasGlyph() is not supported for ligatures.", null, null);
1151 return false;
1152 }
1153 assert nativeTypeface == delegate.mNativeTypeface;
1154 Typeface_Delegate typeface_delegate = Typeface_Delegate.getDelegate(nativeTypeface);
1155
1156 char c = string.charAt(0);
1157 for (Font font : typeface_delegate.getFonts(delegate.mFontVariant)) {
1158 if (font.canDisplay(c)) {
1159 return true;
1160 }
1161 }
1162 return false;
1163 }
1164
1165
1166 @LayoutlibDelegate
1167 /*package*/ static float native_getRunAdvance(long nativePaint, long nativeTypeface,
1168 @NonNull char[] text, int start, int end, int contextStart, int contextEnd,
1169 boolean isRtl, int offset) {
1170 int count = end - start;
1171 float[] advances = new float[count];
1172 native_getTextRunAdvances(nativePaint, nativeTypeface, text, start, count,
1173 contextStart, contextEnd - contextStart, isRtl, advances, 0);
1174 float sum = 0;
1175 for (int i = 0; i < offset; i++) {
1176 sum += advances[i];
1177 }
1178 return sum;
1179 }
1180
1181 @LayoutlibDelegate
1182 /*package*/ static int native_getOffsetForAdvance(long nativePaint, long nativeTypeface,
1183 char[] text, int start, int end, int contextStart, int contextEnd, boolean isRtl,
1184 float advance) {
1185 int count = end - start;
1186 float[] advances = new float[count];
1187 native_getTextRunAdvances(nativePaint, nativeTypeface, text, start, count,
1188 contextStart, contextEnd - contextStart, isRtl, advances, 0);
1189 float sum = 0;
1190 int i;
1191 for (i = 0; i < count && sum < advance; i++) {
1192 sum += advances[i];
1193 }
1194 float distanceToI = sum - advance;
1195 float distanceToIMinus1 = advance - (sum - advances[i]);
1196 return distanceToI > distanceToIMinus1 ? i : i - 1;
Deepanshu Guptaa74fdf02014-07-30 20:18:48 -07001197 }
1198
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001199 // ---- Private delegate/helper methods ----
1200
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001201 /*package*/ Paint_Delegate() {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001202 reset();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001203 }
1204
1205 private Paint_Delegate(Paint_Delegate paint) {
1206 set(paint);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001207 }
1208
1209 private void set(Paint_Delegate paint) {
1210 mFlags = paint.mFlags;
1211 mColor = paint.mColor;
1212 mStyle = paint.mStyle;
1213 mCap = paint.mCap;
1214 mJoin = paint.mJoin;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001215 mTextAlign = paint.mTextAlign;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001216 mTypeface = paint.mTypeface;
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -07001217 mNativeTypeface = paint.mNativeTypeface;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001218 mStrokeWidth = paint.mStrokeWidth;
1219 mStrokeMiter = paint.mStrokeMiter;
1220 mTextSize = paint.mTextSize;
1221 mTextScaleX = paint.mTextScaleX;
1222 mTextSkewX = paint.mTextSkewX;
Xavier Ducroheta313b652010-11-01 18:45:20 -07001223 mXfermode = paint.mXfermode;
1224 mColorFilter = paint.mColorFilter;
1225 mShader = paint.mShader;
1226 mPathEffect = paint.mPathEffect;
1227 mMaskFilter = paint.mMaskFilter;
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001228 mRasterizer = paint.mRasterizer;
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001229 mHintingMode = paint.mHintingMode;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001230 updateFontObject();
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001231 }
1232
1233 private void reset() {
Chris Craikf2437d32015-05-13 13:09:50 -07001234 mFlags = Paint.HIDDEN_DEFAULT_PAINT_FLAGS;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001235 mColor = 0xFF000000;
Xavier Ducrohet66225222010-12-21 01:33:04 -08001236 mStyle = Paint.Style.FILL.nativeInt;
1237 mCap = Paint.Cap.BUTT.nativeInt;
1238 mJoin = Paint.Join.MITER.nativeInt;
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001239 mTextAlign = 0;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001240 mTypeface = Typeface_Delegate.getDelegate(Typeface.sDefaults[0].native_instance);
Deepanshu Guptab1ce7c12014-06-05 14:18:54 -07001241 mNativeTypeface = 0;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001242 mStrokeWidth = 1.f;
Xavier Ducrohetcf2030c2010-12-21 06:20:28 -08001243 mStrokeMiter = 4.f;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001244 mTextSize = 20.f;
1245 mTextScaleX = 1.f;
1246 mTextSkewX = 0.f;
Xavier Ducrohet91672792011-02-22 11:54:37 -08001247 mXfermode = null;
1248 mColorFilter = null;
1249 mShader = null;
1250 mPathEffect = null;
1251 mMaskFilter = null;
1252 mRasterizer = null;
Xavier Ducrohet42e2b282010-12-06 11:08:37 -08001253 updateFontObject();
Xavier Ducrohetb6f7f572011-08-22 13:13:16 -07001254 mHintingMode = Paint.HINTING_ON;
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001255 }
1256
1257 /**
1258 * Update the {@link Font} object from the typeface, text size and scaling
1259 */
Xavier Ducroheta6e51d52010-12-23 07:16:21 -08001260 @SuppressWarnings("deprecation")
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001261 private void updateFontObject() {
Xavier Ducrohet91672792011-02-22 11:54:37 -08001262 if (mTypeface != null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001263 // Get the fonts from the TypeFace object.
Deepanshu Gupta5a6b3ab2014-05-30 17:43:33 -07001264 List<Font> fonts = mTypeface.getFonts(mFontVariant);
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001265
1266 // create new font objects as well as FontMetrics, based on the current text size
1267 // and skew info.
1268 ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
1269 for (Font font : fonts) {
Deepanshu Gupta382256f2014-08-09 14:14:32 -07001270 if (font == null) {
1271 // If the font is null, add null to infoList. When rendering the text, if this
1272 // null is reached, a warning will be logged.
1273 infoList.add(null);
1274 continue;
1275 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001276 FontInfo info = new FontInfo();
1277 info.mFont = font.deriveFont(mTextSize);
1278 if (mTextScaleX != 1.0 || mTextSkewX != 0) {
1279 // TODO: support skew
1280 info.mFont = info.mFont.deriveFont(new AffineTransform(
Xavier Ducrohet8f109102011-10-04 19:39:18 -07001281 mTextScaleX, mTextSkewX, 0, 1, 0, 0));
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001282 }
Deepanshu Guptaa6b207a2013-07-12 11:38:05 -07001283 // The metrics here don't have anti-aliasing set.
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001284 info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont);
1285
1286 infoList.add(info);
1287 }
1288
1289 mFonts = Collections.unmodifiableList(infoList);
1290 }
1291 }
1292
Deepanshu Gupta237740b2014-06-25 17:47:16 -07001293 /*package*/ RectF measureText(char[] text, int index, int count, float[] advances,
1294 int advancesIndex, int bidiFlags) {
1295 return new BidiRenderer(null, this, text)
1296 .renderText(index, index + count, bidiFlags, advances, advancesIndex, false);
1297 }
1298
1299 /*package*/ RectF measureText(char[] text, int index, int count, float[] advances,
1300 int advancesIndex, boolean isRtl) {
1301 return new BidiRenderer(null, this, text)
1302 .renderText(index, index + count, isRtl, advances, advancesIndex, false);
Xavier Ducrohet37f21802010-11-01 16:17:18 -07001303 }
1304
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001305 private float getFontMetrics(FontMetrics metrics) {
1306 if (mFonts.size() > 0) {
1307 java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics;
1308 if (metrics != null) {
1309 // Android expects negative ascent so we invert the value from Java.
1310 metrics.top = - javaMetrics.getMaxAscent();
1311 metrics.ascent = - javaMetrics.getAscent();
1312 metrics.descent = javaMetrics.getDescent();
1313 metrics.bottom = javaMetrics.getMaxDescent();
1314 metrics.leading = javaMetrics.getLeading();
1315 }
1316
1317 return javaMetrics.getHeight();
1318 }
1319
1320 return 0;
1321 }
1322
Xavier Ducrohet43526ab2012-04-23 17:41:37 -07001323 private void setTextLocale(String locale) {
1324 mLocale = new Locale(locale);
1325 }
Xavier Ducrohetdf67eab2010-12-13 16:42:01 -08001326
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001327 private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) {
1328 // get the delegate from the native int.
Derek Sollenberger82934b52014-10-15 13:50:33 -04001329 Paint_Delegate delegate = sManager.getDelegate(thisPaint.getNativeInstance());
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001330 if (delegate == null) {
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001331 return;
1332 }
1333
1334 if (flagValue) {
1335 delegate.mFlags |= flagMask;
1336 } else {
1337 delegate.mFlags &= ~flagMask;
1338 }
1339 }
Xavier Ducrohetef44aea2010-10-28 11:52:00 -07001340}