summaryrefslogtreecommitdiff
path: root/core/java/android/text/MeasuredText.java
diff options
context:
space:
mode:
authorDoug Felt <dougfelt@google.com>2010-03-29 14:58:40 -0700
committerDoug Felt <dougfelt@google.com>2010-04-07 14:54:51 -0700
commite8e45f2c05cb3b6d23f30c8f96d8e0b3699cea7a (patch)
tree6af998e3ce59000a4edcf0a40e49e1eb9636acab /core/java/android/text/MeasuredText.java
parent5d470d03df643801aa4eef11a26deee70da7b952 (diff)
Refactor Styled utility functions into reusable objects.
This takes utility functions from Styled and a few other classes and incorporates them into two new utility classes, TextLine and MeasuredText. The main point of this is to support shaping by skia, to experiment with how this will look, this also introduces character-based Arabic shaping. MeasuredText is used by code that determines line breaks by generating and examining character widths in logical order. Factoring the code in this way makes it usable by the ellipsize functions in TextUtils as well as by StaticLayout. This class takes over the caching of widths and chars arrays that was previously performed by StyledText. A small number of MeasuredText objects are themselves cached by the class and accesed using static obtain and recycle methods. Generally only these few cached instances are ever created. TextLine is used by code that draws or measures text on a line. This unifies the line measuring and rendering code, and pushes assumptions about how rtl text is treated closer to the points where skia code is invoked. TextLine implements the functions that were previously provided by Styled, working on member arrays rather than explicitly-passed arguments. It implements the same kind of static cache as MeasuredText. TextLine and MeasureText simulate arabic glyph generation and shaping by using ArabicShaping, ported with very minor changes from ICU4J's ArabicShaping. This class generates shaped Arabic glyphs and Lam-Alef ligatures using Unicode presentation forms. ArabicShaping is not intended to be permanent, but to be replaced by real shaping from the skia layer. It is introduced in order to emulate the behavior of real shaping so that higher level code dealing with rendering shaped text and cursor movement over ligatures can be developed and tested; it also provides basic-level support for Arabic. Since cursor movement depends on conjuncts whose formation is font-dependent, cursor movement code that was formerly in Layout and StaticLayout was moved into TextLine so that it can work on the shaped text. Other than these changes, the other major change is a rework of the ellipsize utility functions to combine multiple branches into fewer branches with additional state. Updated copyright notices on new files. Change-Id: I492cb58b51f5aaf6f14cb1419bdbed49eac5ba29
Diffstat (limited to 'core/java/android/text/MeasuredText.java')
-rw-r--r--core/java/android/text/MeasuredText.java250
1 files changed, 250 insertions, 0 deletions
diff --git a/core/java/android/text/MeasuredText.java b/core/java/android/text/MeasuredText.java
new file mode 100644
index 000000000000..e3a113da2de3
--- /dev/null
+++ b/core/java/android/text/MeasuredText.java
@@ -0,0 +1,250 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed 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 android.text;
+
+import com.android.internal.util.ArrayUtils;
+
+import android.graphics.Paint;
+import android.icu.text.ArabicShaping;
+import android.text.style.MetricAffectingSpan;
+import android.text.style.ReplacementSpan;
+import android.util.Log;
+
+/**
+ * @hide
+ */
+class MeasuredText {
+ /* package */ CharSequence mText;
+ /* package */ int mTextStart;
+ /* package */ float[] mWidths;
+ /* package */ char[] mChars;
+ /* package */ byte[] mLevels;
+ /* package */ int mDir;
+ /* package */ boolean mEasy;
+ /* package */ int mLen;
+ private int mPos;
+ private float[] mWorkWidths; // temp buffer for Paint.measureText, arrgh
+ private TextPaint mWorkPaint;
+
+ private MeasuredText() {
+ mWorkPaint = new TextPaint();
+ }
+
+ private static MeasuredText[] cached = new MeasuredText[3];
+
+ /* package */
+ static MeasuredText obtain() {
+ MeasuredText mt;
+ synchronized (cached) {
+ for (int i = cached.length; --i >= 0;) {
+ if (cached[i] != null) {
+ mt = cached[i];
+ cached[i] = null;
+ return mt;
+ }
+ }
+ }
+ mt = new MeasuredText();
+ Log.e("MEAS", "new: " + mt);
+ return mt;
+ }
+
+ /* package */
+ static MeasuredText recycle(MeasuredText mt) {
+ mt.mText = null;
+ if (mt.mLen < 1000) {
+ synchronized(cached) {
+ for (int i = 0; i < cached.length; ++i) {
+ if (cached[i] == null) {
+ cached[i] = mt;
+ break;
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Analyzes text for
+ * bidirectional runs. Allocates working buffers.
+ */
+ /* package */
+ void setPara(CharSequence text, int start, int end, int bidiRequest) {
+ mText = text;
+ mTextStart = start;
+
+ int len = end - start;
+ mLen = len;
+ mPos = 0;
+
+ if (mWidths == null || mWidths.length < len) {
+ mWidths = new float[ArrayUtils.idealFloatArraySize(len)];
+ mWorkWidths = new float[mWidths.length];
+ }
+ if (mChars == null || mChars.length < len) {
+ mChars = new char[ArrayUtils.idealCharArraySize(len)];
+ }
+ TextUtils.getChars(text, start, end, mChars, 0);
+
+ if (text instanceof Spanned) {
+ Spanned spanned = (Spanned) text;
+ ReplacementSpan[] spans = spanned.getSpans(start, end,
+ ReplacementSpan.class);
+
+ for (int i = 0; i < spans.length; i++) {
+ int startInPara = spanned.getSpanStart(spans[i]) - start;
+ int endInPara = spanned.getSpanEnd(spans[i]) - start;
+ for (int j = startInPara; j < endInPara; j++) {
+ mChars[j] = '\uFFFC';
+ }
+ }
+ }
+
+ if (TextUtils.doesNotNeedBidi(mChars, 0, len)) {
+ mDir = 1;
+ mEasy = true;
+ } else {
+ if (mLevels == null || mLevels.length < len) {
+ mLevels = new byte[ArrayUtils.idealByteArraySize(len)];
+ }
+ mDir = AndroidBidi.bidi(bidiRequest, mChars, mLevels, len, false);
+ mEasy = false;
+
+ // shape
+ if (mLen > 0) {
+ byte[] levels = mLevels;
+ char[] chars = mChars;
+ byte level = levels[0];
+ int pi = 0;
+ for (int i = 1, e = mLen;; ++i) {
+ if (i == e || levels[i] != level) {
+ if ((level & 0x1) != 0) {
+ AndroidCharacter.mirror(chars, pi, i - pi);
+ ArabicShaping.SHAPER.shape(chars, pi, i - pi);
+ }
+ if (i == e) {
+ break;
+ }
+ pi = i;
+ level = levels[i];
+ }
+ }
+ }
+ }
+ }
+
+ float addStyleRun(TextPaint paint, int len, Paint.FontMetricsInt fm) {
+ int p = mPos;
+ float[] w = mWidths, ww = mWorkWidths;
+ int count = paint.getTextWidths(mChars, p, len, ww);
+ int width = 0;
+ if (count < len) {
+ // must have surrogate pairs in here, pad out the array with zero
+ // for the trailing surrogates
+ char[] chars = mChars;
+ for (int i = 0, e = mLen; i < count; ++i) {
+ width += (w[p++] = ww[i]);
+ if (p < e && chars[p] >= '\udc00' && chars[p] < '\ue000' &&
+ chars[p-1] >= '\ud800' && chars[p-1] < '\udc00') {
+ w[p++] = 0;
+ }
+ }
+ } else {
+ for (int i = 0; i < len; ++i) {
+ width += (w[p++] = ww[i]);
+ }
+ }
+ mPos = p;
+ if (fm != null) {
+ paint.getFontMetricsInt(fm);
+ }
+ return width;
+ }
+
+ float addStyleRun(TextPaint paint, MetricAffectingSpan[] spans, int len,
+ Paint.FontMetricsInt fm) {
+
+ TextPaint workPaint = mWorkPaint;
+ workPaint.set(paint);
+ // XXX paint should not have a baseline shift, but...
+ workPaint.baselineShift = 0;
+
+ ReplacementSpan replacement = null;
+ for (int i = 0; i < spans.length; i++) {
+ MetricAffectingSpan span = spans[i];
+ if (span instanceof ReplacementSpan) {
+ replacement = (ReplacementSpan)span;
+ } else {
+ span.updateMeasureState(workPaint);
+ }
+ }
+
+ float wid;
+ if (replacement == null) {
+ wid = addStyleRun(workPaint, len, fm);
+ } else {
+ // Use original text. Shouldn't matter.
+ wid = replacement.getSize(workPaint, mText, mTextStart + mPos,
+ mTextStart + mPos + len, fm);
+ float[] w = mWidths;
+ w[mPos] = wid;
+ for (int i = mPos + 1, e = mPos + len; i < e; i++)
+ w[i] = 0;
+ }
+
+ if (fm != null) {
+ if (workPaint.baselineShift < 0) {
+ fm.ascent += workPaint.baselineShift;
+ fm.top += workPaint.baselineShift;
+ } else {
+ fm.descent += workPaint.baselineShift;
+ fm.bottom += workPaint.baselineShift;
+ }
+ }
+
+ return wid;
+ }
+
+ int breakText(int start, int limit, boolean forwards, float width) {
+ float[] w = mWidths;
+ if (forwards) {
+ for (int i = start; i < limit; ++i) {
+ if ((width -= w[i]) < 0) {
+ return i - start;
+ }
+ }
+ } else {
+ for (int i = limit; --i >= start;) {
+ if ((width -= w[i]) < 0) {
+ return limit - i -1;
+ }
+ }
+ }
+
+ return limit - start;
+ }
+
+ float measure(int start, int limit) {
+ float width = 0;
+ float[] w = mWidths;
+ for (int i = start; i < limit; ++i) {
+ width += w[i];
+ }
+ return width;
+ }
+} \ No newline at end of file