android.text.style.LeadingMarginSpan.LeadingMarginSpan2 Java Examples

The following examples show how to use android.text.style.LeadingMarginSpan.LeadingMarginSpan2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Layout.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the effective leading margin (unsigned) for this line,
 * taking into account LeadingMarginSpan and LeadingMarginSpan2.
 * @param line the line index
 * @return the leading margin of this line
 */
private int getParagraphLeadingMargin(int line) {
    if (!mSpannedText) {
        return 0;
    }
    Spanned spanned = (Spanned) mText;

    int lineStart = getLineStart(line);
    int lineEnd = getLineEnd(line);
    int spanEnd = spanned.nextSpanTransition(lineStart, lineEnd,
            LeadingMarginSpan.class);
    LeadingMarginSpan[] spans = getParagraphSpans(spanned, lineStart, spanEnd,
                                            LeadingMarginSpan.class);
    if (spans.length == 0) {
        return 0; // no leading margin span;
    }

    int margin = 0;

    boolean useFirstLineMargin = lineStart == 0 || spanned.charAt(lineStart - 1) == '\n';
    for (int i = 0; i < spans.length; i++) {
        if (spans[i] instanceof LeadingMarginSpan2) {
            int spStart = spanned.getSpanStart(spans[i]);
            int spanLine = getLineForOffset(spStart);
            int count = ((LeadingMarginSpan2) spans[i]).getLeadingMarginLineCount();
            // if there is more than one LeadingMarginSpan2, use the count that is greatest
            useFirstLineMargin |= line < spanLine + count;
        }
    }
    for (int i = 0; i < spans.length; i++) {
        LeadingMarginSpan span = spans[i];
        margin += span.getLeadingMargin(useFirstLineMargin);
    }

    return margin;
}
 
Example #2
Source File: FrameResolver.java    From Tehreer-Android with Apache License 2.0 4 votes vote down vote up
void addParagraphLines() {
    float leadingLineExtent = layoutWidth;
    float trailingLineExtent = layoutWidth;

    // Extract all spans of this paragraph.
    paragraphSpans = mSpanned.getSpans(charStart, charEnd, ParagraphStyle.class);

    // Compute margins for leading and trailing lines.
    for (ParagraphStyle style : paragraphSpans) {
        if (style instanceof LeadingMarginSpan) {
            LeadingMarginSpan span = (LeadingMarginSpan) style;
            leadingLineExtent -= span.getLeadingMargin(true);
            trailingLineExtent -= span.getLeadingMargin(false);

            if (span instanceof LeadingMarginSpan2) {
                LeadingMarginSpan2 span2 = (LeadingMarginSpan2) span;
                int spanTotalLines = span2.getLeadingMarginLineCount();
                if (spanTotalLines > leadingLineCount) {
                    leadingLineCount = spanTotalLines;
                }
            }
        }
    }

    // Extract line height spans and create font metrics if necessary.
    pickHeightSpans = mSpanned.getSpans(charStart, charEnd, LineHeightSpan.class);
    int chooseHeightCount = pickHeightSpans.length;
    if (chooseHeightCount > 0 && fontMetrics == null) {
        fontMetrics = new Paint.FontMetricsInt();
    }

    // Setup array for caching top of first line related to each line height span.
    if (pickHeightTops == null || pickHeightTops.length < chooseHeightCount) {
        pickHeightTops = new int[chooseHeightCount];
    }

    // Compute top of first line related to each line height span.
    for (int i = 0; i < chooseHeightCount; i++) {
        int spanStart = mSpanned.getSpanStart(pickHeightSpans[i]);
        int spanTop = (int) (lineTop + 0.5f);

        // Fix span top in case it starts in a previous paragraph.
        if (spanStart < charStart) {
            int lineIndex = binarySearch(spanStart);
            ComposedLine spanLine = frameLines.get(lineIndex);
            spanTop = (int) (spanLine.getTop() + 0.5f);
        }

        pickHeightTops[i] = spanTop;
    }

    float flushFactor = computeFlushFactor();
    lineExtent = leadingLineExtent;
    resolveLeadingOffset();

    // Iterate over each line of this paragraph.
    int lineStart = charStart;
    while (lineStart != charEnd) {
        int lineEnd = BreakResolver.suggestForwardBreak(mSpanned, mRuns, mBreaks, lineStart, charEnd, lineExtent, BreakMode.LINE);
        ComposedLine composedLine = mLineResolver.createSimpleLine(lineStart, lineEnd);
        prepareLine(composedLine, flushFactor);

        float lineHeight = composedLine.getHeight();

        // Make sure that at least one line is added even if frame is smaller in height.
        if ((lineTop + lineHeight) > layoutHeight && frameLines.size() > 0) {
            filled = true;
            return;
        }

        frameLines.add(composedLine);
        lastFlushFactor = flushFactor;

        // Stop the filling process if maximum lines have been added.
        if (frameLines.size() == maxLines) {
            filled = true;
            return;
        }

        // Find out extent of next line.
        if (--leadingLineCount <= 0) {
            lineExtent = trailingLineExtent;
            resolveLeadingOffset();
        }

        lineStart = lineEnd;
        lineTop += lineHeight;
    }
}