Example usage for android.text Layout getLineEnd

List of usage examples for android.text Layout getLineEnd

Introduction

In this page you can find the example usage for android.text Layout getLineEnd.

Prototype

public final int getLineEnd(int line) 

Source Link

Document

Return the text offset after the last character on the specified line.

Usage

From source file:com.duy.pascal.ui.editor.view.LineUtils.java

/**
 * Gets the lineInfo from the index of the letter in the text
 *///from   w ww . j  a va2 s.  c om
public static int getLineFromIndex(int index, int lineCount, Layout layout) {
    int line;
    int currentIndex = 0;

    for (line = 0; line < lineCount; line++) {
        currentIndex += layout.getLineEnd(line) - layout.getLineStart(line);
        if (currentIndex > index) {
            break;
        }
    }
    return line;
}

From source file:com.duy.pascal.ui.editor.view.LineUtils.java

/**
 * Gets the line from the index of the letter in the text
 * <p>//from   w w  w  .  jav  a2 s . co  m
 * 1  2  3  |
 * ^  ^  ^  ^
 * 0  1  2  cursor at 4, return (line;col) = (0;4), line start at 0, column start at 0
 */
@NonNull
public static Pair<Integer, Integer> getLineColFromIndex(int cursorIndex, int length, int lineCount,
        Layout layout) {
    int line;
    int currentIndex = 0, oldIndex = 0;

    line = 0;
    while (line < lineCount) {
        oldIndex = currentIndex;
        currentIndex += layout.getLineEnd(line) - layout.getLineStart(line);
        if (currentIndex > cursorIndex) {
            break;
        }
        if (line < lineCount - 1) {
            line++;
        } else {
            break;
        }
    }
    Pair<Integer, Integer> result = new Pair<>(line, cursorIndex - oldIndex);
    DLog.d(TAG, "getLineColFromIndex() returned: " + result);
    return result;

}

From source file:com.duy.pascal.ui.editor.view.LineUtils.java

public void updateHasNewLineArray(int lineCount, Layout layout, String text) {
    boolean[] hasNewLineArray = new boolean[lineCount];
    toCountLinesArray = new boolean[lineCount];
    realLines = new int[lineCount];
    if (TextUtils.isEmpty(text)) {
        toCountLinesArray[0] = false;/*from  www.ja v a  2  s  . c  o  m*/
        realLines[0] = 0;
        return;
    }

    if (lineCount == 0)
        return;

    int i;

    // for every lineInfo on the edittext
    for (i = 0; i < lineCount; i++) {
        // check if this lineInfo contains "\n"
        if (layout.getLineEnd(i) == 0) {
            hasNewLineArray[i] = false;
        } else {
            hasNewLineArray[i] = text.charAt(layout.getLineEnd(i) - 1) == '\n';
        }
        // if true
        if (hasNewLineArray[i]) {
            int j = i - 1;
            while (j >= 0 && !hasNewLineArray[j]) {
                j--;
            }
            toCountLinesArray[j + 1] = true;

        }
    }

    toCountLinesArray[lineCount - 1] = true;

    int realLine = 0;
    for (i = 0; i < toCountLinesArray.length; i++) {
        realLines[i] = realLine;
        if (toCountLinesArray[i]) {
            realLine++;
        }
    }
}

From source file:com.taobao.weex.dom.WXTextDomObject.java

/**
 * Update layout according to {@link #mText} and span
 * @param width the specified width./*ww w  .j av a2  s  .c  o m*/
 * @param forceWidth If true, force the text width to the specified width, otherwise, text width
 *                   may equals to or be smaller than the specified width.
 * @param previousLayout the result of previous layout, could be null.
 */
private @NonNull Layout createLayout(float width, boolean forceWidth, @Nullable Layout previousLayout) {
    float textWidth;
    textWidth = getTextWidth(mTextPaint, width, forceWidth);
    Layout layout;
    if (!FloatUtil.floatsEqual(previousWidth, textWidth) || previousLayout == null) {
        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;
    }
    if (mNumberOfLines != UNSET && mNumberOfLines > 0 && mNumberOfLines < layout.getLineCount()) {
        int lastLineStart, lastLineEnd;
        lastLineStart = layout.getLineStart(mNumberOfLines - 1);
        lastLineEnd = layout.getLineEnd(mNumberOfLines - 1);
        if (lastLineStart < lastLineEnd) {
            SpannableStringBuilder builder = null;
            if (lastLineStart > 0) {
                builder = new SpannableStringBuilder(spanned.subSequence(0, lastLineStart));
            } else {
                builder = new SpannableStringBuilder();
            }
            Editable lastLine = new SpannableStringBuilder(spanned.subSequence(lastLineStart, lastLineEnd));
            builder.append(truncate(lastLine, mTextPaint, (int) Math.ceil(textWidth), textOverflow));
            adjustSpansRange(spanned, builder);
            spanned = builder;
            return new StaticLayout(spanned, mTextPaint, (int) Math.ceil(textWidth),
                    Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
        }
    }
    return layout;
}