Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.graphics; |
| 18 | |
Xavier Ducrohet | 3f9b037 | 2011-01-13 10:59:34 -0800 | [diff] [blame] | 19 | import com.android.ide.common.rendering.api.LayoutLog; |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 20 | import com.android.layoutlib.bridge.Bridge; |
Xavier Ducrohet | 3bd9898 | 2010-11-09 18:25:03 -0800 | [diff] [blame] | 21 | import com.android.layoutlib.bridge.impl.DelegateManager; |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 22 | import com.android.tools.layoutlib.annotations.LayoutlibDelegate; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 23 | |
| 24 | import android.graphics.Paint.FontMetrics; |
| 25 | import android.graphics.Paint.FontMetricsInt; |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 26 | import android.text.TextUtils; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 27 | |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 28 | import java.awt.BasicStroke; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 29 | import java.awt.Font; |
Xavier Ducrohet | b976124 | 2010-12-23 10:22:14 -0800 | [diff] [blame] | 30 | import java.awt.Shape; |
| 31 | import java.awt.Stroke; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 32 | import java.awt.Toolkit; |
| 33 | import java.awt.font.FontRenderContext; |
| 34 | import java.awt.geom.AffineTransform; |
Xavier Ducrohet | f4d52a6 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 35 | import java.awt.geom.Rectangle2D; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.Collections; |
| 38 | import java.util.List; |
| 39 | |
| 40 | /** |
| 41 | * Delegate implementing the native methods of android.graphics.Paint |
| 42 | * |
| 43 | * Through the layoutlib_create tool, the original native methods of Paint have been replaced |
| 44 | * by calls to methods of the same name in this delegate class. |
| 45 | * |
| 46 | * This class behaves like the original native implementation, but in Java, keeping previously |
| 47 | * native data into its own objects and mapping them to int that are sent back and forth between |
| 48 | * it and the original Paint class. |
| 49 | * |
| 50 | * @see DelegateManager |
| 51 | * |
| 52 | */ |
| 53 | public class Paint_Delegate { |
| 54 | |
| 55 | /** |
| 56 | * Class associating a {@link Font} and it's {@link java.awt.FontMetrics}. |
| 57 | */ |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 58 | /*package*/ static final class FontInfo { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 59 | Font mFont; |
| 60 | java.awt.FontMetrics mMetrics; |
| 61 | } |
| 62 | |
| 63 | // ---- delegate manager ---- |
| 64 | private static final DelegateManager<Paint_Delegate> sManager = |
Xavier Ducrohet | b2de839 | 2011-02-23 16:51:08 -0800 | [diff] [blame] | 65 | new DelegateManager<Paint_Delegate>(Paint_Delegate.class); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 66 | |
| 67 | // ---- delegate helper data ---- |
| 68 | private List<FontInfo> mFonts; |
| 69 | private final FontRenderContext mFontContext = new FontRenderContext( |
| 70 | new AffineTransform(), true, true); |
| 71 | |
| 72 | // ---- delegate data ---- |
| 73 | private int mFlags; |
| 74 | private int mColor; |
| 75 | private int mStyle; |
| 76 | private int mCap; |
| 77 | private int mJoin; |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 78 | private int mTextAlign; |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 79 | private Typeface_Delegate mTypeface; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 80 | private float mStrokeWidth; |
| 81 | private float mStrokeMiter; |
| 82 | private float mTextSize; |
| 83 | private float mTextScaleX; |
| 84 | private float mTextSkewX; |
Xavier Ducrohet | b6f7f57 | 2011-08-22 13:13:16 -0700 | [diff] [blame^] | 85 | private int mHintingMode = Paint.HINTING_ON; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 86 | |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 87 | private Xfermode_Delegate mXfermode; |
| 88 | private ColorFilter_Delegate mColorFilter; |
| 89 | private Shader_Delegate mShader; |
| 90 | private PathEffect_Delegate mPathEffect; |
| 91 | private MaskFilter_Delegate mMaskFilter; |
| 92 | private Rasterizer_Delegate mRasterizer; |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 93 | |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 94 | |
| 95 | // ---- Public Helper methods ---- |
| 96 | |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 97 | public static Paint_Delegate getDelegate(int native_paint) { |
| 98 | return sManager.getDelegate(native_paint); |
| 99 | } |
| 100 | |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Returns the list of {@link Font} objects. The first item is the main font, the rest |
| 103 | * are fall backs for characters not present in the main font. |
| 104 | */ |
| 105 | public List<FontInfo> getFonts() { |
| 106 | return mFonts; |
| 107 | } |
| 108 | |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 109 | public boolean isAntiAliased() { |
| 110 | return (mFlags & Paint.ANTI_ALIAS_FLAG) != 0; |
| 111 | } |
| 112 | |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 113 | public boolean isFilterBitmap() { |
| 114 | return (mFlags & Paint.FILTER_BITMAP_FLAG) != 0; |
| 115 | } |
| 116 | |
| 117 | public int getStyle() { |
| 118 | return mStyle; |
| 119 | } |
| 120 | |
| 121 | public int getColor() { |
| 122 | return mColor; |
| 123 | } |
| 124 | |
Xavier Ducrohet | 6622522 | 2010-12-21 01:33:04 -0800 | [diff] [blame] | 125 | public int getAlpha() { |
| 126 | return mColor >>> 24; |
| 127 | } |
| 128 | |
Xavier Ducrohet | cf2030c | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 129 | public void setAlpha(int alpha) { |
| 130 | mColor = (alpha << 24) | (mColor & 0x00FFFFFF); |
| 131 | } |
| 132 | |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 133 | public int getTextAlign() { |
| 134 | return mTextAlign; |
| 135 | } |
| 136 | |
| 137 | public float getStrokeWidth() { |
| 138 | return mStrokeWidth; |
| 139 | } |
| 140 | |
Xavier Ducrohet | 6622522 | 2010-12-21 01:33:04 -0800 | [diff] [blame] | 141 | /** |
| 142 | * returns the value of stroke miter needed by the java api. |
| 143 | */ |
| 144 | public float getJavaStrokeMiter() { |
Xavier Ducrohet | cf2030c | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 145 | float miter = mStrokeMiter * mStrokeWidth; |
| 146 | if (miter < 1.f) { |
| 147 | miter = 1.f; |
| 148 | } |
| 149 | return miter; |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | public int getJavaCap() { |
| 153 | switch (Paint.sCapArray[mCap]) { |
| 154 | case BUTT: |
| 155 | return BasicStroke.CAP_BUTT; |
| 156 | case ROUND: |
| 157 | return BasicStroke.CAP_ROUND; |
| 158 | default: |
| 159 | case SQUARE: |
| 160 | return BasicStroke.CAP_SQUARE; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public int getJavaJoin() { |
| 165 | switch (Paint.sJoinArray[mJoin]) { |
| 166 | default: |
| 167 | case MITER: |
| 168 | return BasicStroke.JOIN_MITER; |
| 169 | case ROUND: |
| 170 | return BasicStroke.JOIN_ROUND; |
| 171 | case BEVEL: |
| 172 | return BasicStroke.JOIN_BEVEL; |
| 173 | } |
| 174 | } |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 175 | |
Xavier Ducrohet | b976124 | 2010-12-23 10:22:14 -0800 | [diff] [blame] | 176 | public Stroke getJavaStroke() { |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 177 | if (mPathEffect != null) { |
| 178 | if (mPathEffect.isSupported()) { |
| 179 | Stroke stroke = mPathEffect.getStroke(this); |
Xavier Ducrohet | b976124 | 2010-12-23 10:22:14 -0800 | [diff] [blame] | 180 | assert stroke != null; |
| 181 | if (stroke != null) { |
| 182 | return stroke; |
| 183 | } |
| 184 | } else { |
Xavier Ducrohet | 3f9b037 | 2011-01-13 10:59:34 -0800 | [diff] [blame] | 185 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_PATHEFFECT, |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 186 | mPathEffect.getSupportMessage(), |
Xavier Ducrohet | f69bb29 | 2011-01-14 16:40:43 -0800 | [diff] [blame] | 187 | null, null /*data*/); |
Xavier Ducrohet | b976124 | 2010-12-23 10:22:14 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | // if no custom stroke as been set, set the default one. |
| 192 | return new BasicStroke( |
| 193 | getStrokeWidth(), |
| 194 | getJavaCap(), |
| 195 | getJavaJoin(), |
| 196 | getJavaStrokeMiter()); |
| 197 | } |
| 198 | |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 199 | /** |
| 200 | * Returns the {@link Xfermode} delegate or null if none have been set |
| 201 | * |
| 202 | * @return the delegate or null. |
| 203 | */ |
| 204 | public Xfermode_Delegate getXfermode() { |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 205 | return mXfermode; |
Xavier Ducrohet | f1a17452 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 208 | /** |
| 209 | * Returns the {@link ColorFilter} delegate or null if none have been set |
| 210 | * |
| 211 | * @return the delegate or null. |
| 212 | */ |
| 213 | public ColorFilter_Delegate getColorFilter() { |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 214 | return mColorFilter; |
Xavier Ducrohet | f1a17452 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 217 | /** |
| 218 | * Returns the {@link Shader} delegate or null if none have been set |
| 219 | * |
| 220 | * @return the delegate or null. |
| 221 | */ |
| 222 | public Shader_Delegate getShader() { |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 223 | return mShader; |
Xavier Ducrohet | f1a17452 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 226 | /** |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 227 | * Returns the {@link MaskFilter} delegate or null if none have been set |
| 228 | * |
| 229 | * @return the delegate or null. |
| 230 | */ |
| 231 | public MaskFilter_Delegate getMaskFilter() { |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 232 | return mMaskFilter; |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Returns the {@link Rasterizer} delegate or null if none have been set |
| 237 | * |
| 238 | * @return the delegate or null. |
| 239 | */ |
| 240 | public Rasterizer_Delegate getRasterizer() { |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 241 | return mRasterizer; |
Xavier Ducrohet | f1a17452 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 244 | // ---- native methods ---- |
| 245 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 246 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 247 | /*package*/ static int getFlags(Paint thisPaint) { |
| 248 | // get the delegate from the native int. |
| 249 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 250 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | return delegate.mFlags; |
| 255 | } |
| 256 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 257 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 258 | /*package*/ static void setFlags(Paint thisPaint, int flags) { |
| 259 | // get the delegate from the native int. |
| 260 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 261 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | |
| 265 | delegate.mFlags = flags; |
| 266 | } |
| 267 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 268 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 269 | /*package*/ static void setFilterBitmap(Paint thisPaint, boolean filter) { |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 270 | setFlag(thisPaint, Paint.FILTER_BITMAP_FLAG, filter); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 273 | @LayoutlibDelegate |
Xavier Ducrohet | b6f7f57 | 2011-08-22 13:13:16 -0700 | [diff] [blame^] | 274 | /*package*/ static int getHinting(Paint thisPaint) { |
| 275 | // get the delegate from the native int. |
| 276 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 277 | if (delegate == null) { |
| 278 | return Paint.HINTING_ON; |
| 279 | } |
| 280 | |
| 281 | return delegate.mHintingMode; |
| 282 | } |
| 283 | |
| 284 | @LayoutlibDelegate |
| 285 | /*package*/ static void setHinting(Paint thisPaint, int mode) { |
| 286 | // get the delegate from the native int. |
| 287 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 288 | if (delegate == null) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | delegate.mHintingMode = mode; |
| 293 | } |
| 294 | |
| 295 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 296 | /*package*/ static void setAntiAlias(Paint thisPaint, boolean aa) { |
| 297 | setFlag(thisPaint, Paint.ANTI_ALIAS_FLAG, aa); |
| 298 | } |
| 299 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 300 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 301 | /*package*/ static void setSubpixelText(Paint thisPaint, boolean subpixelText) { |
| 302 | setFlag(thisPaint, Paint.SUBPIXEL_TEXT_FLAG, subpixelText); |
| 303 | } |
| 304 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 305 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 306 | /*package*/ static void setUnderlineText(Paint thisPaint, boolean underlineText) { |
| 307 | setFlag(thisPaint, Paint.UNDERLINE_TEXT_FLAG, underlineText); |
| 308 | } |
| 309 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 310 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 311 | /*package*/ static void setStrikeThruText(Paint thisPaint, boolean strikeThruText) { |
| 312 | setFlag(thisPaint, Paint.STRIKE_THRU_TEXT_FLAG, strikeThruText); |
| 313 | } |
| 314 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 315 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 316 | /*package*/ static void setFakeBoldText(Paint thisPaint, boolean fakeBoldText) { |
| 317 | setFlag(thisPaint, Paint.FAKE_BOLD_TEXT_FLAG, fakeBoldText); |
| 318 | } |
| 319 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 320 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 321 | /*package*/ static void setDither(Paint thisPaint, boolean dither) { |
| 322 | setFlag(thisPaint, Paint.DITHER_FLAG, dither); |
| 323 | } |
| 324 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 325 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 326 | /*package*/ static void setLinearText(Paint thisPaint, boolean linearText) { |
| 327 | setFlag(thisPaint, Paint.LINEAR_TEXT_FLAG, linearText); |
| 328 | } |
| 329 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 330 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 331 | /*package*/ static int getColor(Paint thisPaint) { |
| 332 | // get the delegate from the native int. |
| 333 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 334 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | return delegate.mColor; |
| 339 | } |
| 340 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 341 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 342 | /*package*/ static void setColor(Paint thisPaint, int color) { |
| 343 | // get the delegate from the native int. |
| 344 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 345 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | |
| 349 | delegate.mColor = color; |
| 350 | } |
| 351 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 352 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 353 | /*package*/ static int getAlpha(Paint thisPaint) { |
| 354 | // get the delegate from the native int. |
| 355 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 356 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
Xavier Ducrohet | 6622522 | 2010-12-21 01:33:04 -0800 | [diff] [blame] | 360 | return delegate.getAlpha(); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 363 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 364 | /*package*/ static void setAlpha(Paint thisPaint, int a) { |
| 365 | // get the delegate from the native int. |
| 366 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 367 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | |
Xavier Ducrohet | cf2030c | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 371 | delegate.setAlpha(a); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 374 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 375 | /*package*/ static float getStrokeWidth(Paint thisPaint) { |
| 376 | // get the delegate from the native int. |
| 377 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 378 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 379 | return 1.f; |
| 380 | } |
| 381 | |
| 382 | return delegate.mStrokeWidth; |
| 383 | } |
| 384 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 385 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 386 | /*package*/ static void setStrokeWidth(Paint thisPaint, float width) { |
| 387 | // get the delegate from the native int. |
| 388 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 389 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
| 393 | delegate.mStrokeWidth = width; |
| 394 | } |
| 395 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 396 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 397 | /*package*/ static float getStrokeMiter(Paint thisPaint) { |
| 398 | // get the delegate from the native int. |
| 399 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 400 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 401 | return 1.f; |
| 402 | } |
| 403 | |
| 404 | return delegate.mStrokeMiter; |
| 405 | } |
| 406 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 407 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 408 | /*package*/ static void setStrokeMiter(Paint thisPaint, float miter) { |
| 409 | // get the delegate from the native int. |
| 410 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 411 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | |
| 415 | delegate.mStrokeMiter = miter; |
| 416 | } |
| 417 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 418 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 419 | /*package*/ static void nSetShadowLayer(Paint thisPaint, float radius, float dx, float dy, |
| 420 | int color) { |
| 421 | // FIXME |
Xavier Ducrohet | 3f9b037 | 2011-01-13 10:59:34 -0800 | [diff] [blame] | 422 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
Xavier Ducrohet | f69bb29 | 2011-01-14 16:40:43 -0800 | [diff] [blame] | 423 | "Paint.setShadowLayer is not supported.", null, null /*data*/); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 426 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 427 | /*package*/ static float getTextSize(Paint thisPaint) { |
| 428 | // get the delegate from the native int. |
| 429 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 430 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 431 | return 1.f; |
| 432 | } |
| 433 | |
| 434 | return delegate.mTextSize; |
| 435 | } |
| 436 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 437 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 438 | /*package*/ static void setTextSize(Paint thisPaint, float textSize) { |
| 439 | // get the delegate from the native int. |
| 440 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 441 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | |
| 445 | delegate.mTextSize = textSize; |
Xavier Ducrohet | 42e2b28 | 2010-12-06 11:08:37 -0800 | [diff] [blame] | 446 | delegate.updateFontObject(); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 449 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 450 | /*package*/ static float getTextScaleX(Paint thisPaint) { |
| 451 | // get the delegate from the native int. |
| 452 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 453 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 454 | return 1.f; |
| 455 | } |
| 456 | |
| 457 | return delegate.mTextScaleX; |
| 458 | } |
| 459 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 460 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 461 | /*package*/ static void setTextScaleX(Paint thisPaint, float scaleX) { |
| 462 | // get the delegate from the native int. |
| 463 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 464 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
| 468 | delegate.mTextScaleX = scaleX; |
Xavier Ducrohet | 42e2b28 | 2010-12-06 11:08:37 -0800 | [diff] [blame] | 469 | delegate.updateFontObject(); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 472 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 473 | /*package*/ static float getTextSkewX(Paint thisPaint) { |
| 474 | // get the delegate from the native int. |
| 475 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 476 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 477 | return 1.f; |
| 478 | } |
| 479 | |
| 480 | return delegate.mTextSkewX; |
| 481 | } |
| 482 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 483 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 484 | /*package*/ static void setTextSkewX(Paint thisPaint, float skewX) { |
| 485 | // get the delegate from the native int. |
| 486 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 487 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | |
| 491 | delegate.mTextSkewX = skewX; |
Xavier Ducrohet | 42e2b28 | 2010-12-06 11:08:37 -0800 | [diff] [blame] | 492 | delegate.updateFontObject(); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 495 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 496 | /*package*/ static float ascent(Paint thisPaint) { |
Xavier Ducrohet | df67eab | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 497 | // get the delegate |
| 498 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 499 | if (delegate == null) { |
Xavier Ducrohet | df67eab | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | if (delegate.mFonts.size() > 0) { |
| 504 | java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics; |
| 505 | // Android expects negative ascent so we invert the value from Java. |
| 506 | return - javaMetrics.getAscent(); |
| 507 | } |
| 508 | |
| 509 | return 0; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 512 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 513 | /*package*/ static float descent(Paint thisPaint) { |
Xavier Ducrohet | df67eab | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 514 | // get the delegate |
| 515 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 516 | if (delegate == null) { |
Xavier Ducrohet | df67eab | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | if (delegate.mFonts.size() > 0) { |
| 521 | java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics; |
| 522 | return javaMetrics.getDescent(); |
| 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 529 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 530 | /*package*/ static float getFontMetrics(Paint thisPaint, FontMetrics metrics) { |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 531 | // get the delegate |
| 532 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 533 | if (delegate == null) { |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
Xavier Ducrohet | df67eab | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 537 | return delegate.getFontMetrics(metrics); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 540 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 541 | /*package*/ static int getFontMetricsInt(Paint thisPaint, FontMetricsInt fmi) { |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 542 | // get the delegate |
| 543 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 544 | if (delegate == null) { |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | if (delegate.mFonts.size() > 0) { |
| 549 | java.awt.FontMetrics javaMetrics = delegate.mFonts.get(0).mMetrics; |
| 550 | if (fmi != null) { |
| 551 | // Android expects negative ascent so we invert the value from Java. |
| 552 | fmi.top = - javaMetrics.getMaxAscent(); |
| 553 | fmi.ascent = - javaMetrics.getAscent(); |
| 554 | fmi.descent = javaMetrics.getDescent(); |
| 555 | fmi.bottom = javaMetrics.getMaxDescent(); |
| 556 | fmi.leading = javaMetrics.getLeading(); |
| 557 | } |
| 558 | |
| 559 | return javaMetrics.getHeight(); |
| 560 | } |
| 561 | |
| 562 | return 0; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 565 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 566 | /*package*/ static float native_measureText(Paint thisPaint, char[] text, int index, |
| 567 | int count) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 568 | // get the delegate |
| 569 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 570 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 574 | return delegate.measureText(text, index, count); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 577 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 578 | /*package*/ static float native_measureText(Paint thisPaint, String text, int start, int end) { |
| 579 | return native_measureText(thisPaint, text.toCharArray(), start, end - start); |
| 580 | } |
| 581 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 582 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 583 | /*package*/ static float native_measureText(Paint thisPaint, String text) { |
| 584 | return native_measureText(thisPaint, text.toCharArray(), 0, text.length()); |
| 585 | } |
| 586 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 587 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 588 | /*package*/ static int native_breakText(Paint thisPaint, char[] text, int index, int count, |
| 589 | float maxWidth, float[] measuredWidth) { |
Xavier Ducrohet | f4d52a6 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 590 | |
| 591 | // get the delegate |
| 592 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 593 | if (delegate == null) { |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | int inc = count > 0 ? 1 : -1; |
| 598 | |
| 599 | int measureIndex = 0; |
| 600 | float measureAcc = 0; |
| 601 | for (int i = index; i != index + count; i += inc, measureIndex++) { |
| 602 | int start, end; |
| 603 | if (i < index) { |
| 604 | start = i; |
| 605 | end = index; |
| 606 | } else { |
| 607 | start = index; |
| 608 | end = i; |
| 609 | } |
| 610 | |
| 611 | // measure from start to end |
| 612 | float res = delegate.measureText(text, start, end - start + 1); |
| 613 | |
| 614 | if (measuredWidth != null) { |
| 615 | measuredWidth[measureIndex] = res; |
| 616 | } |
| 617 | |
| 618 | measureAcc += res; |
| 619 | if (res > maxWidth) { |
| 620 | // we should not return this char index, but since it's 0-based |
| 621 | // and we need to return a count, we simply return measureIndex; |
| 622 | return measureIndex; |
| 623 | } |
| 624 | |
| 625 | } |
| 626 | |
| 627 | return measureIndex; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 630 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 631 | /*package*/ static int native_breakText(Paint thisPaint, String text, boolean measureForwards, |
| 632 | float maxWidth, float[] measuredWidth) { |
Xavier Ducrohet | f4d52a6 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 633 | return native_breakText(thisPaint, text.toCharArray(), 0, text.length(), maxWidth, |
| 634 | measuredWidth); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 637 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 638 | /*package*/ static int native_init() { |
| 639 | Paint_Delegate newDelegate = new Paint_Delegate(); |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 640 | return sManager.addNewDelegate(newDelegate); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 643 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 644 | /*package*/ static int native_initWithPaint(int paint) { |
| 645 | // get the delegate from the native int. |
| 646 | Paint_Delegate delegate = sManager.getDelegate(paint); |
| 647 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | Paint_Delegate newDelegate = new Paint_Delegate(delegate); |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 652 | return sManager.addNewDelegate(newDelegate); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 655 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 656 | /*package*/ static void native_reset(int native_object) { |
| 657 | // get the delegate from the native int. |
| 658 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 659 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 660 | return; |
| 661 | } |
| 662 | |
| 663 | delegate.reset(); |
| 664 | } |
| 665 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 666 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 667 | /*package*/ static void native_set(int native_dst, int native_src) { |
| 668 | // get the delegate from the native int. |
| 669 | Paint_Delegate delegate_dst = sManager.getDelegate(native_dst); |
| 670 | if (delegate_dst == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 671 | return; |
| 672 | } |
| 673 | |
| 674 | // get the delegate from the native int. |
| 675 | Paint_Delegate delegate_src = sManager.getDelegate(native_src); |
| 676 | if (delegate_src == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 677 | return; |
| 678 | } |
| 679 | |
| 680 | delegate_dst.set(delegate_src); |
| 681 | } |
| 682 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 683 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 684 | /*package*/ static int native_getStyle(int native_object) { |
| 685 | // get the delegate from the native int. |
| 686 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 687 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | return delegate.mStyle; |
| 692 | } |
| 693 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 694 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 695 | /*package*/ static void native_setStyle(int native_object, int style) { |
| 696 | // get the delegate from the native int. |
| 697 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 698 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 699 | return; |
| 700 | } |
| 701 | |
| 702 | delegate.mStyle = style; |
| 703 | } |
| 704 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 705 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 706 | /*package*/ static int native_getStrokeCap(int native_object) { |
| 707 | // get the delegate from the native int. |
| 708 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 709 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | return delegate.mCap; |
| 714 | } |
| 715 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 716 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 717 | /*package*/ static void native_setStrokeCap(int native_object, int cap) { |
| 718 | // get the delegate from the native int. |
| 719 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 720 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 721 | return; |
| 722 | } |
| 723 | |
| 724 | delegate.mCap = cap; |
| 725 | } |
| 726 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 727 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 728 | /*package*/ static int native_getStrokeJoin(int native_object) { |
| 729 | // get the delegate from the native int. |
| 730 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 731 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | return delegate.mJoin; |
| 736 | } |
| 737 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 738 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 739 | /*package*/ static void native_setStrokeJoin(int native_object, int join) { |
| 740 | // get the delegate from the native int. |
| 741 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 742 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 743 | return; |
| 744 | } |
| 745 | |
| 746 | delegate.mJoin = join; |
| 747 | } |
| 748 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 749 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 750 | /*package*/ static boolean native_getFillPath(int native_object, int src, int dst) { |
Xavier Ducrohet | b976124 | 2010-12-23 10:22:14 -0800 | [diff] [blame] | 751 | Paint_Delegate paint = sManager.getDelegate(native_object); |
| 752 | if (paint == null) { |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | Path_Delegate srcPath = Path_Delegate.getDelegate(src); |
| 757 | if (srcPath == null) { |
| 758 | return true; |
| 759 | } |
| 760 | |
| 761 | Path_Delegate dstPath = Path_Delegate.getDelegate(dst); |
| 762 | if (dstPath == null) { |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | Stroke stroke = paint.getJavaStroke(); |
| 767 | Shape strokeShape = stroke.createStrokedShape(srcPath.getJavaShape()); |
| 768 | |
| 769 | dstPath.setJavaShape(strokeShape); |
| 770 | |
| 771 | // FIXME figure out the return value? |
| 772 | return true; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 775 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 776 | /*package*/ static int native_setShader(int native_object, int shader) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 777 | // get the delegate from the native int. |
| 778 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 779 | if (delegate == null) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 780 | return shader; |
| 781 | } |
| 782 | |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 783 | delegate.mShader = Shader_Delegate.getDelegate(shader); |
| 784 | |
| 785 | return shader; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 788 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 789 | /*package*/ static int native_setColorFilter(int native_object, int filter) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 790 | // get the delegate from the native int. |
| 791 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 792 | if (delegate == null) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 793 | return filter; |
| 794 | } |
| 795 | |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 796 | delegate.mColorFilter = ColorFilter_Delegate.getDelegate(filter);; |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 797 | |
| 798 | // since none of those are supported, display a fidelity warning right away |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 799 | if (delegate.mColorFilter != null && delegate.mColorFilter.isSupported() == false) { |
Xavier Ducrohet | 3f9b037 | 2011-01-13 10:59:34 -0800 | [diff] [blame] | 800 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_COLORFILTER, |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 801 | delegate.mColorFilter.getSupportMessage(), null, null /*data*/); |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | return filter; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 807 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 808 | /*package*/ static int native_setXfermode(int native_object, int xfermode) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 809 | // get the delegate from the native int. |
| 810 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 811 | if (delegate == null) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 812 | return xfermode; |
| 813 | } |
| 814 | |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 815 | delegate.mXfermode = Xfermode_Delegate.getDelegate(xfermode); |
| 816 | |
| 817 | return xfermode; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 820 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 821 | /*package*/ static int native_setPathEffect(int native_object, int effect) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 822 | // get the delegate from the native int. |
| 823 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 824 | if (delegate == null) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 825 | return effect; |
| 826 | } |
| 827 | |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 828 | delegate.mPathEffect = PathEffect_Delegate.getDelegate(effect); |
| 829 | |
| 830 | return effect; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 833 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 834 | /*package*/ static int native_setMaskFilter(int native_object, int maskfilter) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 835 | // get the delegate from the native int. |
| 836 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 837 | if (delegate == null) { |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 838 | return maskfilter; |
| 839 | } |
| 840 | |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 841 | delegate.mMaskFilter = MaskFilter_Delegate.getDelegate(maskfilter); |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 842 | |
| 843 | // since none of those are supported, display a fidelity warning right away |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 844 | if (delegate.mMaskFilter != null && delegate.mMaskFilter.isSupported() == false) { |
Xavier Ducrohet | 3f9b037 | 2011-01-13 10:59:34 -0800 | [diff] [blame] | 845 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_MASKFILTER, |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 846 | delegate.mMaskFilter.getSupportMessage(), null, null /*data*/); |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | return maskfilter; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 852 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 853 | /*package*/ static int native_setTypeface(int native_object, int typeface) { |
| 854 | // get the delegate from the native int. |
| 855 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 856 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 857 | return 0; |
| 858 | } |
| 859 | |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 860 | delegate.mTypeface = Typeface_Delegate.getDelegate(typeface); |
Xavier Ducrohet | 42e2b28 | 2010-12-06 11:08:37 -0800 | [diff] [blame] | 861 | delegate.updateFontObject(); |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 862 | return typeface; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 865 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 866 | /*package*/ static int native_setRasterizer(int native_object, int rasterizer) { |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 867 | // get the delegate from the native int. |
| 868 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 869 | if (delegate == null) { |
| 870 | return rasterizer; |
| 871 | } |
| 872 | |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 873 | delegate.mRasterizer = Rasterizer_Delegate.getDelegate(rasterizer); |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 874 | |
| 875 | // since none of those are supported, display a fidelity warning right away |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 876 | if (delegate.mRasterizer != null && delegate.mRasterizer.isSupported() == false) { |
Xavier Ducrohet | 3f9b037 | 2011-01-13 10:59:34 -0800 | [diff] [blame] | 877 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_RASTERIZER, |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 878 | delegate.mRasterizer.getSupportMessage(), null, null /*data*/); |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | return rasterizer; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 884 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 885 | /*package*/ static int native_getTextAlign(int native_object) { |
| 886 | // get the delegate from the native int. |
| 887 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 888 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 889 | return 0; |
| 890 | } |
| 891 | |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 892 | return delegate.mTextAlign; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 895 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 896 | /*package*/ static void native_setTextAlign(int native_object, int align) { |
| 897 | // get the delegate from the native int. |
| 898 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 899 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 903 | delegate.mTextAlign = align; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 906 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 907 | /*package*/ static float native_getFontMetrics(int native_paint, FontMetrics metrics) { |
Xavier Ducrohet | df67eab | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 908 | // get the delegate from the native int. |
| 909 | Paint_Delegate delegate = sManager.getDelegate(native_paint); |
| 910 | if (delegate == null) { |
Xavier Ducrohet | df67eab | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 911 | return 0.f; |
| 912 | } |
| 913 | |
| 914 | return delegate.getFontMetrics(metrics); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 917 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 918 | /*package*/ static int native_getTextWidths(int native_object, char[] text, int index, |
| 919 | int count, float[] widths) { |
Xavier Ducrohet | f4d52a6 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 920 | // get the delegate from the native int. |
| 921 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 922 | if (delegate == null) { |
| 923 | return 0; |
| 924 | } |
| 925 | |
| 926 | if (delegate.mFonts.size() > 0) { |
| 927 | // FIXME: handle multi-char characters (see measureText) |
| 928 | float totalAdvance = 0; |
| 929 | for (int i = 0; i < count; i++) { |
| 930 | char c = text[i + index]; |
| 931 | boolean found = false; |
| 932 | for (FontInfo info : delegate.mFonts) { |
| 933 | if (info.mFont.canDisplay(c)) { |
| 934 | float adv = info.mMetrics.charWidth(c); |
| 935 | totalAdvance += adv; |
| 936 | if (widths != null) { |
| 937 | widths[i] = adv; |
| 938 | } |
| 939 | |
| 940 | found = true; |
| 941 | break; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (found == false) { |
| 946 | // no advance for this char. |
| 947 | if (widths != null) { |
| 948 | widths[i] = 0.f; |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | return (int) totalAdvance; |
| 954 | } |
| 955 | |
Xavier Ducrohet | 1eeaea0 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 956 | return 0; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 959 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 960 | /*package*/ static int native_getTextWidths(int native_object, String text, int start, |
| 961 | int end, float[] widths) { |
Xavier Ducrohet | f4d52a6 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 962 | return native_getTextWidths(native_object, text.toCharArray(), start, end - start, widths); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 965 | @LayoutlibDelegate |
Xavier Ducrohet | 81efa7f | 2011-06-15 14:43:42 -0700 | [diff] [blame] | 966 | /* package */static int native_getTextGlyphs(int native_object, String text, int start, |
| 967 | int end, int contextStart, int contextEnd, int flags, char[] glyphs) { |
| 968 | // FIXME |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 973 | /*package*/ static float native_getTextRunAdvances(int native_object, |
| 974 | char[] text, int index, int count, int contextIndex, int contextCount, |
Xavier Ducrohet | 81efa7f | 2011-06-15 14:43:42 -0700 | [diff] [blame] | 975 | int flags, float[] advances, int advancesIndex, int reserved) { |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 976 | // get the delegate from the native int. |
| 977 | Paint_Delegate delegate = sManager.getDelegate(native_object); |
| 978 | if (delegate == null) { |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 979 | return 0.f; |
| 980 | } |
| 981 | |
| 982 | if (delegate.mFonts.size() > 0) { |
Xavier Ducrohet | f1a17452 | 2010-11-01 22:02:08 -0700 | [diff] [blame] | 983 | // FIXME: handle multi-char characters (see measureText) |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 984 | float totalAdvance = 0; |
| 985 | for (int i = 0; i < count; i++) { |
| 986 | char c = text[i + index]; |
| 987 | boolean found = false; |
| 988 | for (FontInfo info : delegate.mFonts) { |
| 989 | if (info.mFont.canDisplay(c)) { |
| 990 | float adv = info.mMetrics.charWidth(c); |
| 991 | totalAdvance += adv; |
| 992 | if (advances != null) { |
| 993 | advances[i] = adv; |
| 994 | } |
| 995 | |
| 996 | found = true; |
| 997 | break; |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | if (found == false) { |
| 1002 | // no advance for this char. |
| 1003 | if (advances != null) { |
| 1004 | advances[i] = 0.f; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | return totalAdvance; |
| 1010 | } |
| 1011 | |
| 1012 | return 0; |
| 1013 | |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1016 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1017 | /*package*/ static float native_getTextRunAdvances(int native_object, |
| 1018 | String text, int start, int end, int contextStart, int contextEnd, |
Xavier Ducrohet | 81efa7f | 2011-06-15 14:43:42 -0700 | [diff] [blame] | 1019 | int flags, float[] advances, int advancesIndex, int reserved) { |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1020 | // FIXME: support contextStart, contextEnd and direction flag |
| 1021 | int count = end - start; |
| 1022 | char[] buffer = TemporaryBuffer.obtain(count); |
| 1023 | TextUtils.getChars(text, start, end, buffer, 0); |
| 1024 | |
| 1025 | return native_getTextRunAdvances(native_object, buffer, 0, count, contextStart, |
Xavier Ducrohet | 81efa7f | 2011-06-15 14:43:42 -0700 | [diff] [blame] | 1026 | contextEnd - contextStart, flags, advances, advancesIndex, reserved); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1029 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1030 | /*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, char[] text, |
| 1031 | int contextStart, int contextLength, int flags, int offset, int cursorOpt) { |
| 1032 | // FIXME |
Xavier Ducrohet | 1eeaea0 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 1033 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 1034 | "Paint.getTextRunCursor is not supported.", null, null /*data*/); |
| 1035 | return 0; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1038 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1039 | /*package*/ static int native_getTextRunCursor(Paint thisPaint, int native_object, String text, |
| 1040 | int contextStart, int contextEnd, int flags, int offset, int cursorOpt) { |
| 1041 | // FIXME |
Xavier Ducrohet | 1eeaea0 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 1042 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 1043 | "Paint.getTextRunCursor is not supported.", null, null /*data*/); |
| 1044 | return 0; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1047 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1048 | /*package*/ static void native_getTextPath(int native_object, int bidiFlags, |
| 1049 | char[] text, int index, int count, float x, float y, int path) { |
| 1050 | // FIXME |
Xavier Ducrohet | 1eeaea0 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 1051 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 1052 | "Paint.getTextPath is not supported.", null, null /*data*/); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1055 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1056 | /*package*/ static void native_getTextPath(int native_object, int bidiFlags, |
| 1057 | String text, int start, int end, float x, float y, int path) { |
| 1058 | // FIXME |
Xavier Ducrohet | 1eeaea0 | 2011-02-09 19:39:52 -0800 | [diff] [blame] | 1059 | Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, |
| 1060 | "Paint.getTextPath is not supported.", null, null /*data*/); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1063 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1064 | /*package*/ static void nativeGetStringBounds(int nativePaint, String text, int start, |
| 1065 | int end, Rect bounds) { |
Xavier Ducrohet | f4d52a6 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 1066 | nativeGetCharArrayBounds(nativePaint, text.toCharArray(), start, end - start, bounds); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1069 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1070 | /*package*/ static void nativeGetCharArrayBounds(int nativePaint, char[] text, int index, |
| 1071 | int count, Rect bounds) { |
Xavier Ducrohet | f4d52a6 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 1072 | |
| 1073 | // get the delegate from the native int. |
| 1074 | Paint_Delegate delegate = sManager.getDelegate(nativePaint); |
| 1075 | if (delegate == null) { |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | // FIXME should test if the main font can display all those characters. |
| 1080 | // See MeasureText |
| 1081 | if (delegate.mFonts.size() > 0) { |
| 1082 | FontInfo mainInfo = delegate.mFonts.get(0); |
| 1083 | |
| 1084 | Rectangle2D rect = mainInfo.mFont.getStringBounds(text, index, index + count, |
| 1085 | delegate.mFontContext); |
| 1086 | bounds.set(0, 0, (int) rect.getWidth(), (int) rect.getHeight()); |
| 1087 | } |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Xavier Ducrohet | 94c80bf | 2011-02-09 17:17:49 -0800 | [diff] [blame] | 1090 | @LayoutlibDelegate |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1091 | /*package*/ static void finalizer(int nativePaint) { |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 1092 | sManager.removeJavaReferenceFor(nativePaint); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | // ---- Private delegate/helper methods ---- |
| 1096 | |
Xavier Ducrohet | cf2030c | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1097 | /*package*/ Paint_Delegate() { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1098 | reset(); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | private Paint_Delegate(Paint_Delegate paint) { |
| 1102 | set(paint); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | private void set(Paint_Delegate paint) { |
| 1106 | mFlags = paint.mFlags; |
| 1107 | mColor = paint.mColor; |
| 1108 | mStyle = paint.mStyle; |
| 1109 | mCap = paint.mCap; |
| 1110 | mJoin = paint.mJoin; |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1111 | mTextAlign = paint.mTextAlign; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1112 | mTypeface = paint.mTypeface; |
| 1113 | mStrokeWidth = paint.mStrokeWidth; |
| 1114 | mStrokeMiter = paint.mStrokeMiter; |
| 1115 | mTextSize = paint.mTextSize; |
| 1116 | mTextScaleX = paint.mTextScaleX; |
| 1117 | mTextSkewX = paint.mTextSkewX; |
Xavier Ducrohet | a313b65 | 2010-11-01 18:45:20 -0700 | [diff] [blame] | 1118 | mXfermode = paint.mXfermode; |
| 1119 | mColorFilter = paint.mColorFilter; |
| 1120 | mShader = paint.mShader; |
| 1121 | mPathEffect = paint.mPathEffect; |
| 1122 | mMaskFilter = paint.mMaskFilter; |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 1123 | mRasterizer = paint.mRasterizer; |
Xavier Ducrohet | b6f7f57 | 2011-08-22 13:13:16 -0700 | [diff] [blame^] | 1124 | mHintingMode = paint.mHintingMode; |
Xavier Ducrohet | 42e2b28 | 2010-12-06 11:08:37 -0800 | [diff] [blame] | 1125 | updateFontObject(); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | private void reset() { |
| 1129 | mFlags = Paint.DEFAULT_PAINT_FLAGS; |
Xavier Ducrohet | cf2030c | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1130 | mColor = 0xFF000000; |
Xavier Ducrohet | 6622522 | 2010-12-21 01:33:04 -0800 | [diff] [blame] | 1131 | mStyle = Paint.Style.FILL.nativeInt; |
| 1132 | mCap = Paint.Cap.BUTT.nativeInt; |
| 1133 | mJoin = Paint.Join.MITER.nativeInt; |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1134 | mTextAlign = 0; |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 1135 | mTypeface = Typeface_Delegate.getDelegate(Typeface.sDefaults[0].native_instance); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1136 | mStrokeWidth = 1.f; |
Xavier Ducrohet | cf2030c | 2010-12-21 06:20:28 -0800 | [diff] [blame] | 1137 | mStrokeMiter = 4.f; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1138 | mTextSize = 20.f; |
| 1139 | mTextScaleX = 1.f; |
| 1140 | mTextSkewX = 0.f; |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 1141 | mXfermode = null; |
| 1142 | mColorFilter = null; |
| 1143 | mShader = null; |
| 1144 | mPathEffect = null; |
| 1145 | mMaskFilter = null; |
| 1146 | mRasterizer = null; |
Xavier Ducrohet | 42e2b28 | 2010-12-06 11:08:37 -0800 | [diff] [blame] | 1147 | updateFontObject(); |
Xavier Ducrohet | b6f7f57 | 2011-08-22 13:13:16 -0700 | [diff] [blame^] | 1148 | mHintingMode = Paint.HINTING_ON; |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * Update the {@link Font} object from the typeface, text size and scaling |
| 1153 | */ |
Xavier Ducrohet | a6e51d5 | 2010-12-23 07:16:21 -0800 | [diff] [blame] | 1154 | @SuppressWarnings("deprecation") |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1155 | private void updateFontObject() { |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 1156 | if (mTypeface != null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1157 | // Get the fonts from the TypeFace object. |
Xavier Ducrohet | 9167279 | 2011-02-22 11:54:37 -0800 | [diff] [blame] | 1158 | List<Font> fonts = mTypeface.getFonts(); |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1159 | |
| 1160 | // create new font objects as well as FontMetrics, based on the current text size |
| 1161 | // and skew info. |
| 1162 | ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size()); |
| 1163 | for (Font font : fonts) { |
| 1164 | FontInfo info = new FontInfo(); |
| 1165 | info.mFont = font.deriveFont(mTextSize); |
| 1166 | if (mTextScaleX != 1.0 || mTextSkewX != 0) { |
| 1167 | // TODO: support skew |
| 1168 | info.mFont = info.mFont.deriveFont(new AffineTransform( |
| 1169 | mTextScaleX, mTextSkewX, 0, 0, 1, 0)); |
| 1170 | } |
| 1171 | info.mMetrics = Toolkit.getDefaultToolkit().getFontMetrics(info.mFont); |
| 1172 | |
| 1173 | infoList.add(info); |
| 1174 | } |
| 1175 | |
| 1176 | mFonts = Collections.unmodifiableList(infoList); |
| 1177 | } |
| 1178 | } |
| 1179 | |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1180 | /*package*/ float measureText(char[] text, int index, int count) { |
Xavier Ducrohet | f4d52a6 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 1181 | |
| 1182 | // WARNING: the logic in this method is similar to Canvas_Delegate.native_drawText |
| 1183 | // Any change to this method should be reflected there as well |
| 1184 | |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1185 | if (mFonts.size() > 0) { |
| 1186 | FontInfo mainFont = mFonts.get(0); |
| 1187 | int i = index; |
| 1188 | int lastIndex = index + count; |
| 1189 | float total = 0f; |
| 1190 | while (i < lastIndex) { |
| 1191 | // always start with the main font. |
| 1192 | int upTo = mainFont.mFont.canDisplayUpTo(text, i, lastIndex); |
| 1193 | if (upTo == -1) { |
| 1194 | // shortcut to exit |
| 1195 | return total + mainFont.mMetrics.charsWidth(text, i, lastIndex - i); |
| 1196 | } else if (upTo > 0) { |
| 1197 | total += mainFont.mMetrics.charsWidth(text, i, upTo - i); |
| 1198 | i = upTo; |
| 1199 | // don't call continue at this point. Since it is certain the main font |
| 1200 | // cannot display the font a index upTo (now ==i), we move on to the |
| 1201 | // fallback fonts directly. |
| 1202 | } |
| 1203 | |
| 1204 | // no char supported, attempt to read the next char(s) with the |
| 1205 | // fallback font. In this case we only test the first character |
| 1206 | // and then go back to test with the main font. |
| 1207 | // Special test for 2-char characters. |
| 1208 | boolean foundFont = false; |
| 1209 | for (int f = 1 ; f < mFonts.size() ; f++) { |
| 1210 | FontInfo fontInfo = mFonts.get(f); |
| 1211 | |
| 1212 | // need to check that the font can display the character. We test |
| 1213 | // differently if the char is a high surrogate. |
| 1214 | int charCount = Character.isHighSurrogate(text[i]) ? 2 : 1; |
| 1215 | upTo = fontInfo.mFont.canDisplayUpTo(text, i, i + charCount); |
| 1216 | if (upTo == -1) { |
| 1217 | total += fontInfo.mMetrics.charsWidth(text, i, charCount); |
| 1218 | i += charCount; |
| 1219 | foundFont = true; |
| 1220 | break; |
| 1221 | |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | // in case no font can display the char, measure it with the main font. |
| 1226 | if (foundFont == false) { |
| 1227 | int size = Character.isHighSurrogate(text[i]) ? 2 : 1; |
| 1228 | total += mainFont.mMetrics.charsWidth(text, i, size); |
| 1229 | i += size; |
| 1230 | } |
| 1231 | } |
Xavier Ducrohet | f4d52a6 | 2011-02-23 09:53:56 -0800 | [diff] [blame] | 1232 | |
| 1233 | return total; |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | return 0; |
Xavier Ducrohet | 37f2180 | 2010-11-01 16:17:18 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Xavier Ducrohet | df67eab | 2010-12-13 16:42:01 -0800 | [diff] [blame] | 1239 | private float getFontMetrics(FontMetrics metrics) { |
| 1240 | if (mFonts.size() > 0) { |
| 1241 | java.awt.FontMetrics javaMetrics = mFonts.get(0).mMetrics; |
| 1242 | if (metrics != null) { |
| 1243 | // Android expects negative ascent so we invert the value from Java. |
| 1244 | metrics.top = - javaMetrics.getMaxAscent(); |
| 1245 | metrics.ascent = - javaMetrics.getAscent(); |
| 1246 | metrics.descent = javaMetrics.getDescent(); |
| 1247 | metrics.bottom = javaMetrics.getMaxDescent(); |
| 1248 | metrics.leading = javaMetrics.getLeading(); |
| 1249 | } |
| 1250 | |
| 1251 | return javaMetrics.getHeight(); |
| 1252 | } |
| 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | |
| 1258 | |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1259 | private static void setFlag(Paint thisPaint, int flagMask, boolean flagValue) { |
| 1260 | // get the delegate from the native int. |
| 1261 | Paint_Delegate delegate = sManager.getDelegate(thisPaint.mNativePaint); |
| 1262 | if (delegate == null) { |
Xavier Ducrohet | ef44aea | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | if (flagValue) { |
| 1267 | delegate.mFlags |= flagMask; |
| 1268 | } else { |
| 1269 | delegate.mFlags &= ~flagMask; |
| 1270 | } |
| 1271 | } |
| 1272 | } |