android.text.style.LineHeightSpan Java Examples

The following examples show how to use android.text.style.LineHeightSpan. 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: 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;
    }
}
 
Example #2
Source File: FrameResolver.java    From Tehreer-Android with Apache License 2.0 4 votes vote down vote up
void prepareLine(@NonNull ComposedLine composedLine, float flushFactor) {
    // Resolve line height spans.
    int chooseHeightCount = pickHeightSpans.length;
    for (int i = 0; i < chooseHeightCount; i++) {
        fontMetrics.ascent = (int) -(composedLine.getAscent() + 0.5f);
        fontMetrics.descent = (int) (composedLine.getDescent() + 0.5f);
        fontMetrics.leading = (int) (composedLine.getLeading() + 0.5f);
        fontMetrics.top = fontMetrics.ascent;
        fontMetrics.bottom = fontMetrics.descent;

        LineHeightSpan span = pickHeightSpans[i];
        int lineStart = composedLine.getCharStart();
        int lineEnd = composedLine.getCharEnd();
        int lineTop = (int) (this.lineTop + 0.5f);
        int spanTop = pickHeightTops[i];

        span.chooseHeight(mSpanned, lineStart, lineEnd, spanTop, lineTop, fontMetrics);

        // Override the line metrics.
        composedLine.setAscent(-fontMetrics.ascent);
        composedLine.setDescent(fontMetrics.descent);
        composedLine.setLeading(fontMetrics.leading);
    }

    // Resolve line height multiplier.
    if (mLineHeightMultiplier != 0.0f) {
        float oldHeight = composedLine.getHeight();
        float newHeight = oldHeight * mLineHeightMultiplier;
        float midOffset = (newHeight - oldHeight) / 2.0f;

        // Adjust metrics in such a way that text remains in the middle of line.
        composedLine.setAscent(composedLine.getAscent() + midOffset);
        composedLine.setDescent(composedLine.getDescent() + midOffset);
    }

    // Resolve extra line spacing.
    if (mExtraLineSpacing != 0.0f) {
        composedLine.setLeading(composedLine.getLeading() + mExtraLineSpacing);
    }

    // Compute the origin of line.
    float originX = leadingOffset + composedLine.getFlushPenOffset(flushFactor, lineExtent);
    float originY = lineTop + composedLine.getAscent();

    // Set the origin of line.
    composedLine.setOriginX(originX);
    composedLine.setOriginY(originY);

    // Set supporting properties of line.
    composedLine.setSpans(paragraphSpans);
    composedLine.setFirst(leadingLineCount > 0);
    composedLine.setIntrinsicMargin(layoutWidth - lineExtent);
    composedLine.setFlushFactor(flushFactor);
}