Repository: incubator-weex Updated Branches: refs/heads/master 7a2743136 -> 3949bffae
+ [android] support style:direction rtl Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/8e41fb37 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/8e41fb37 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/8e41fb37 Branch: refs/heads/master Commit: 8e41fb37b19a2c8ee0f98533a46c5ea7a2e8b2ca Parents: 2077164 Author: misakuo <misa...@apache.org> Authored: Tue Sep 26 15:55:13 2017 +0800 Committer: misakuo <misa...@apache.org> Committed: Tue Sep 26 15:55:13 2017 +0800 ---------------------------------------------------------------------- .../java/com/taobao/weex/common/Constants.java | 3 + .../com/taobao/weex/dom/WXTextDomObject.java | 16 +++-- .../taobao/weex/utils/StaticLayoutProxy.java | 72 ++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/8e41fb37/android/sdk/src/main/java/com/taobao/weex/common/Constants.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/common/Constants.java b/android/sdk/src/main/java/com/taobao/weex/common/Constants.java index e74e083..c32e943 100644 --- a/android/sdk/src/main/java/com/taobao/weex/common/Constants.java +++ b/android/sdk/src/main/java/com/taobao/weex/common/Constants.java @@ -182,6 +182,9 @@ public class Constants { String ARIA_LABEL = "ariaLabel"; String ARIA_HIDDEN = "ariaHidden"; + String DIRECTION = "direction"; + String RTL = "rtl"; + interface Recycler{ String LIST_DATA = "listData"; http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/8e41fb37/android/sdk/src/main/java/com/taobao/weex/dom/WXTextDomObject.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/dom/WXTextDomObject.java b/android/sdk/src/main/java/com/taobao/weex/dom/WXTextDomObject.java index 06c4e8a..eea8df9 100644 --- a/android/sdk/src/main/java/com/taobao/weex/dom/WXTextDomObject.java +++ b/android/sdk/src/main/java/com/taobao/weex/dom/WXTextDomObject.java @@ -18,8 +18,6 @@ */ package com.taobao.weex.dom; -import static com.taobao.weex.dom.WXStyle.UNSET; - import android.graphics.Canvas; import android.graphics.Typeface; import android.os.Build; @@ -39,6 +37,7 @@ import android.text.TextUtils; import android.text.style.AbsoluteSizeSpan; import android.text.style.AlignmentSpan; import android.text.style.ForegroundColorSpan; + import com.taobao.weex.WXEnvironment; import com.taobao.weex.common.Constants; import com.taobao.weex.dom.flex.CSSConstants; @@ -47,15 +46,19 @@ import com.taobao.weex.dom.flex.FloatUtil; import com.taobao.weex.dom.flex.MeasureOutput; import com.taobao.weex.ui.component.WXText; import com.taobao.weex.ui.component.WXTextDecoration; +import com.taobao.weex.utils.StaticLayoutProxy; import com.taobao.weex.utils.WXDomUtils; import com.taobao.weex.utils.WXLogUtils; import com.taobao.weex.utils.WXResourceUtils; + import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; +import static com.taobao.weex.dom.WXStyle.UNSET; + /** * Class for calculating a given text's height and width. The calculating of width and height of * text is done by {@link Layout}. @@ -305,8 +308,13 @@ public class WXTextDomObject extends WXDomObject { textWidth = getTextWidth(mTextPaint, width, forceWidth); Layout layout; if (!FloatUtil.floatsEqual(previousWidth, textWidth) || previousLayout == null) { - layout = new StaticLayout(spanned, mTextPaint, (int) Math.ceil(textWidth), - Layout.Alignment.ALIGN_NORMAL, 1, 0, false); + boolean forceRtl = false; + Object direction = getStyles().get(Constants.Name.DIRECTION); + if (direction != null && "text".equals(mType)) { + forceRtl = direction.equals(Constants.Name.RTL); + } + layout = StaticLayoutProxy.create(spanned, mTextPaint, (int) Math.ceil(textWidth), + Layout.Alignment.ALIGN_NORMAL, 1, 0, false, forceRtl); } else { layout = previousLayout; } http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/8e41fb37/android/sdk/src/main/java/com/taobao/weex/utils/StaticLayoutProxy.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/utils/StaticLayoutProxy.java b/android/sdk/src/main/java/com/taobao/weex/utils/StaticLayoutProxy.java new file mode 100644 index 0000000..98fe0ed --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/utils/StaticLayoutProxy.java @@ -0,0 +1,72 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.utils; + +import android.text.Layout; +import android.text.StaticLayout; +import android.text.TextDirectionHeuristic; +import android.text.TextDirectionHeuristics; +import android.text.TextPaint; + +import java.lang.reflect.Constructor; + +/** + * Created by moxun on 2017/9/26. + */ + +public class StaticLayoutProxy { + public static StaticLayout create(CharSequence source, TextPaint paint, + int width, + Layout.Alignment align, float spacingmult, float spacingadd, + boolean includepad, boolean forceRtl) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2 && forceRtl) { + TextDirectionHeuristic textDir = TextDirectionHeuristics.RTL; + StaticLayout rtlLayout = createInternal(source, paint, width, align, textDir, spacingmult, spacingadd, includepad); + if (rtlLayout != null) { + return rtlLayout; + } + } + return new StaticLayout(source, paint, width, align, spacingmult, spacingadd, includepad); + } + + private static StaticLayout createInternal(CharSequence source, TextPaint paint, + int width, Layout.Alignment align, TextDirectionHeuristic textDir, + float spacingmult, float spacingadd, + boolean includepad) { + if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { + return null; + } else { + try { + Class<StaticLayout> clazz = StaticLayout.class; + Constructor<StaticLayout> constructor = clazz.getConstructor(CharSequence.class, TextPaint.class, + int.class, Layout.Alignment.class, TextDirectionHeuristic.class, + float.class, float.class, + boolean.class); + + if (constructor != null) { + return constructor.newInstance(source, paint, width, + align, textDir, spacingmult, spacingadd, includepad); + } + } catch (Throwable e) { + e.printStackTrace(); + } + } + return null; + } +}