Search in sources :

Example 1 with ReplacementSpan

use of jp.sblo.pandora.jota.text.style.ReplacementSpan in project Jota-Text-Editor-old by jiro-aqua.

the class Styled method drawUniformRun.

/**
     * Draws and/or measures a uniform run of text on a single line. No span of
     * interest should start or end in the middle of this run (if not
     * drawing, character spans that don't affect metrics can be ignored).
     * Neither should the run direction change in the middle of the run.
     *
     * <p>The x position is the leading edge of the text. In a right-to-left
     * paragraph, this will be to the right of the text to be drawn. Paint
     * should not have an Align value other than LEFT or positioning will get
     * confused.
     *
     * <p>On return, workPaint will reflect the original paint plus any
     * modifications made by character styles on the run.
     *
     * <p>The returned width is signed and will be < 0 if the paragraph
     * direction is right-to-left.
     */
private static float drawUniformRun(Canvas canvas, Spanned text, int start, int end, int dir, boolean runIsRtl, float x, int top, int y, int bottom, Paint.FontMetricsInt fmi, TextPaint paint, TextPaint workPaint, boolean needWidth) {
    boolean haveWidth = false;
    float ret = 0;
    CharacterStyle[] spans = text.getSpans(start, end, CharacterStyle.class);
    ReplacementSpan replacement = null;
    // XXX: This shouldn't be modifying paint, only workPaint.
    // However, the members belonging to TextPaint should have default
    // values anyway.  Better to ensure this in the Layout constructor.
    paint.bgColor = 0;
    paint.baselineShift = 0;
    workPaint.set(paint);
    if (spans.length > 0) {
        for (int i = 0; i < spans.length; i++) {
            CharacterStyle span = spans[i];
            if (span instanceof ReplacementSpan) {
                replacement = (ReplacementSpan) span;
            } else {
                span.updateDrawState(workPaint);
            }
        }
    }
    if (replacement == null) {
        CharSequence tmp;
        int tmpstart, tmpend;
        if (runIsRtl) {
            tmp = TextUtils.getReverse(text, start, end);
            tmpstart = 0;
            // XXX: assumes getReverse doesn't change the length of the text
            tmpend = end - start;
        } else {
            tmp = text;
            tmpstart = start;
            tmpend = end;
        }
        if (fmi != null) {
            workPaint.getFontMetricsInt(fmi);
        }
        if (canvas != null) {
            if (workPaint.bgColor != 0) {
                int c = workPaint.getColor();
                Paint.Style s = workPaint.getStyle();
                workPaint.setColor(workPaint.bgColor);
                workPaint.setStyle(Paint.Style.FILL);
                if (!haveWidth) {
                    ret = workPaint.measureText(tmp, tmpstart, tmpend);
                    haveWidth = true;
                }
                if (dir == Layout.DIR_RIGHT_TO_LEFT)
                    canvas.drawRect(x - ret, top, x, bottom, workPaint);
                else
                    canvas.drawRect(x, top, x + ret, bottom, workPaint);
                workPaint.setStyle(s);
                workPaint.setColor(c);
            }
            if (dir == Layout.DIR_RIGHT_TO_LEFT) {
                if (!haveWidth) {
                    ret = workPaint.measureText(tmp, tmpstart, tmpend);
                    haveWidth = true;
                }
                canvas.drawText(tmp, tmpstart, tmpend, x - ret, y + workPaint.baselineShift, workPaint);
            } else {
                if (needWidth) {
                    if (!haveWidth) {
                        ret = workPaint.measureText(tmp, tmpstart, tmpend);
                        haveWidth = true;
                    }
                }
                canvas.drawText(tmp, tmpstart, tmpend, x, y + workPaint.baselineShift, workPaint);
            }
        } else {
            if (needWidth && !haveWidth) {
                ret = workPaint.measureText(tmp, tmpstart, tmpend);
                haveWidth = true;
            }
        }
    } else {
        ret = replacement.getSize(workPaint, text, start, end, fmi);
        if (canvas != null) {
            if (dir == Layout.DIR_RIGHT_TO_LEFT)
                replacement.draw(canvas, text, start, end, x - ret, top, y, bottom, workPaint);
            else
                replacement.draw(canvas, text, start, end, x, top, y, bottom, workPaint);
        }
    }
    if (dir == Layout.DIR_RIGHT_TO_LEFT)
        return -ret;
    else
        return ret;
}
Also used : TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) ReplacementSpan(jp.sblo.pandora.jota.text.style.ReplacementSpan) CharacterStyle(android.text.style.CharacterStyle) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 2 with ReplacementSpan

use of jp.sblo.pandora.jota.text.style.ReplacementSpan in project Jota-Text-Editor-old by jiro-aqua.

the class Layout method getOffsetAtStartOf.

private int getOffsetAtStartOf(int offset) {
    if (offset == 0)
        return 0;
    CharSequence text = mText;
    char c = text.charAt(offset);
    if (c >= '�' && c <= '�') {
        char c1 = text.charAt(offset - 1);
        if (c1 >= '�' && c1 <= '�')
            offset -= 1;
    }
    if (mSpannedText) {
        ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset, ReplacementSpan.class);
        for (int i = 0; i < spans.length; i++) {
            int start = ((Spanned) text).getSpanStart(spans[i]);
            int end = ((Spanned) text).getSpanEnd(spans[i]);
            if (start < offset && end > offset)
                offset = start;
        }
    }
    return offset;
}
Also used : ReplacementSpan(jp.sblo.pandora.jota.text.style.ReplacementSpan) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 3 with ReplacementSpan

use of jp.sblo.pandora.jota.text.style.ReplacementSpan in project Jota-Text-Editor-old by jiro-aqua.

the class TextUtils method getOffsetAfter.

public static int getOffsetAfter(CharSequence text, int offset) {
    int len = text.length();
    if (offset == len)
        return len;
    if (offset == len - 1)
        return len;
    char c = text.charAt(offset);
    if (c >= '�' && c <= '�') {
        char c1 = text.charAt(offset + 1);
        if (c1 >= '�' && c1 <= '�')
            offset += 2;
        else
            offset += 1;
    } else {
        offset += 1;
    }
    if (text instanceof Spanned) {
        ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset, ReplacementSpan.class);
        for (int i = 0; i < spans.length; i++) {
            int start = ((Spanned) text).getSpanStart(spans[i]);
            int end = ((Spanned) text).getSpanEnd(spans[i]);
            if (start < offset && end > offset)
                offset = end;
        }
    }
    return offset;
}
Also used : ReplacementSpan(jp.sblo.pandora.jota.text.style.ReplacementSpan) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint)

Example 4 with ReplacementSpan

use of jp.sblo.pandora.jota.text.style.ReplacementSpan in project Jota-Text-Editor-old by jiro-aqua.

the class TextUtils method getOffsetBefore.

public static int getOffsetBefore(CharSequence text, int offset) {
    if (offset == 0)
        return 0;
    if (offset == 1)
        return 0;
    char c = text.charAt(offset - 1);
    if (c >= '�' && c <= '�') {
        char c1 = text.charAt(offset - 2);
        if (c1 >= '�' && c1 <= '�')
            offset -= 2;
        else
            offset -= 1;
    } else {
        offset -= 1;
    }
    if (text instanceof Spanned) {
        ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset, ReplacementSpan.class);
        for (int i = 0; i < spans.length; i++) {
            int start = ((Spanned) text).getSpanStart(spans[i]);
            int end = ((Spanned) text).getSpanEnd(spans[i]);
            if (start < offset && end > offset)
                offset = start;
        }
    }
    return offset;
}
Also used : ReplacementSpan(jp.sblo.pandora.jota.text.style.ReplacementSpan) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint)

Aggregations

TextPaint (android.text.TextPaint)4 ReplacementSpan (jp.sblo.pandora.jota.text.style.ReplacementSpan)4 Spanned (android.text.Spanned)3 Paint (android.graphics.Paint)2 CharacterStyle (android.text.style.CharacterStyle)1