Search in sources :

Example 1 with TextPaintSpan

use of org.telegram.ui.Components.TextPaintSpan in project Telegram-FOSS by Telegram-FOSS-Team.

the class ArticleViewer method getText.

private CharSequence getText(TLRPC.WebPage page, View parentView, TLRPC.RichText parentRichText, TLRPC.RichText richText, TLRPC.PageBlock parentBlock, int maxWidth) {
    if (richText == null) {
        return null;
    }
    if (richText instanceof TLRPC.TL_textFixed) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textFixed) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textItalic) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textItalic) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textBold) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textBold) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textUnderline) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textUnderline) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textStrike) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textStrike) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textEmail) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, ((TLRPC.TL_textEmail) richText).text, parentBlock, maxWidth));
        MetricAffectingSpan[] innerSpans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MetricAffectingSpan.class);
        if (spannableStringBuilder.length() != 0) {
            spannableStringBuilder.setSpan(new TextPaintUrlSpan(innerSpans == null || innerSpans.length == 0 ? getTextPaint(parentRichText, richText, parentBlock) : null, "mailto:" + getUrl(richText)), 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textUrl) {
        TLRPC.TL_textUrl textUrl = (TLRPC.TL_textUrl) richText;
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, ((TLRPC.TL_textUrl) richText).text, parentBlock, maxWidth));
        MetricAffectingSpan[] innerSpans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MetricAffectingSpan.class);
        TextPaint paint = innerSpans == null || innerSpans.length == 0 ? getTextPaint(parentRichText, richText, parentBlock) : null;
        MetricAffectingSpan span;
        if (textUrl.webpage_id != 0) {
            span = new TextPaintWebpageUrlSpan(paint, getUrl(richText));
        } else {
            span = new TextPaintUrlSpan(paint, getUrl(richText));
        }
        if (spannableStringBuilder.length() != 0) {
            spannableStringBuilder.setSpan(span, 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textPlain) {
        return ((TLRPC.TL_textPlain) richText).text;
    } else if (richText instanceof TLRPC.TL_textAnchor) {
        TLRPC.TL_textAnchor textAnchor = (TLRPC.TL_textAnchor) richText;
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, textAnchor.text, parentBlock, maxWidth));
        spannableStringBuilder.setSpan(new AnchorSpan(textAnchor.name), 0, spannableStringBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textEmpty) {
        return "";
    } else if (richText instanceof TLRPC.TL_textConcat) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        int count = richText.texts.size();
        for (int a = 0; a < count; a++) {
            TLRPC.RichText innerRichText = richText.texts.get(a);
            TLRPC.RichText lastRichText = getLastRichText(innerRichText);
            boolean extraSpace = maxWidth >= 0 && innerRichText instanceof TLRPC.TL_textUrl && ((TLRPC.TL_textUrl) innerRichText).webpage_id != 0;
            if (extraSpace && spannableStringBuilder.length() != 0 && spannableStringBuilder.charAt(spannableStringBuilder.length() - 1) != '\n') {
                spannableStringBuilder.append(" ");
                spannableStringBuilder.setSpan(new TextSelectionHelper.IgnoreCopySpannable(), spannableStringBuilder.length() - 1, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            CharSequence innerText = getText(page, parentView, parentRichText, innerRichText, parentBlock, maxWidth);
            int flags = getTextFlags(lastRichText);
            int startLength = spannableStringBuilder.length();
            spannableStringBuilder.append(innerText);
            if (flags != 0 && !(innerText instanceof SpannableStringBuilder)) {
                if ((flags & TEXT_FLAG_URL) != 0 || (flags & TEXT_FLAG_WEBPAGE_URL) != 0) {
                    String url = getUrl(innerRichText);
                    if (url == null) {
                        url = getUrl(parentRichText);
                    }
                    MetricAffectingSpan span;
                    if ((flags & TEXT_FLAG_WEBPAGE_URL) != 0) {
                        span = new TextPaintWebpageUrlSpan(getTextPaint(parentRichText, lastRichText, parentBlock), url);
                    } else {
                        span = new TextPaintUrlSpan(getTextPaint(parentRichText, lastRichText, parentBlock), url);
                    }
                    if (startLength != spannableStringBuilder.length()) {
                        spannableStringBuilder.setSpan(span, startLength, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                } else {
                    if (startLength != spannableStringBuilder.length()) {
                        spannableStringBuilder.setSpan(new TextPaintSpan(getTextPaint(parentRichText, lastRichText, parentBlock)), startLength, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
            }
            if (extraSpace && a != count - 1) {
                spannableStringBuilder.append(" ");
                spannableStringBuilder.setSpan(new TextSelectionHelper.IgnoreCopySpannable(), spannableStringBuilder.length() - 1, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textSubscript) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textSubscript) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textSuperscript) {
        return getText(page, parentView, parentRichText, ((TLRPC.TL_textSuperscript) richText).text, parentBlock, maxWidth);
    } else if (richText instanceof TLRPC.TL_textMarked) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, ((TLRPC.TL_textMarked) richText).text, parentBlock, maxWidth));
        MetricAffectingSpan[] innerSpans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MetricAffectingSpan.class);
        if (spannableStringBuilder.length() != 0) {
            spannableStringBuilder.setSpan(new TextPaintMarkSpan(innerSpans == null || innerSpans.length == 0 ? getTextPaint(parentRichText, richText, parentBlock) : null), 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textPhone) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getText(page, parentView, parentRichText, ((TLRPC.TL_textPhone) richText).text, parentBlock, maxWidth));
        MetricAffectingSpan[] innerSpans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MetricAffectingSpan.class);
        if (spannableStringBuilder.length() != 0) {
            spannableStringBuilder.setSpan(new TextPaintUrlSpan(innerSpans == null || innerSpans.length == 0 ? getTextPaint(parentRichText, richText, parentBlock) : null, "tel:" + getUrl(richText)), 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableStringBuilder;
    } else if (richText instanceof TLRPC.TL_textImage) {
        TLRPC.TL_textImage textImage = (TLRPC.TL_textImage) richText;
        TLRPC.Document document = WebPageUtils.getDocumentWithId(page, textImage.document_id);
        if (document != null) {
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder("*");
            int w = AndroidUtilities.dp(textImage.w);
            int h = AndroidUtilities.dp(textImage.h);
            maxWidth = Math.abs(maxWidth);
            if (w > maxWidth) {
                float scale = maxWidth / (float) w;
                w = maxWidth;
                h *= scale;
            }
            if (parentView != null) {
                int color = Theme.getColor(Theme.key_windowBackgroundWhite);
                float lightness = (0.2126f * Color.red(color) + 0.7152f * Color.green(color) + 0.0722f * Color.blue(color)) / 255.0f;
                spannableStringBuilder.setSpan(new TextPaintImageReceiverSpan(parentView, document, page, w, h, false, lightness <= 0.705f), 0, spannableStringBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return spannableStringBuilder;
        } else {
            return "";
        }
    }
    return "not supported " + richText;
}
Also used : TextPaintSpan(org.telegram.ui.Components.TextPaintSpan) TLRPC(org.telegram.tgnet.TLRPC) TextPaintUrlSpan(org.telegram.ui.Components.TextPaintUrlSpan) TextPaintWebpageUrlSpan(org.telegram.ui.Components.TextPaintWebpageUrlSpan) TextSelectionHelper(org.telegram.ui.Cells.TextSelectionHelper) TextPaintMarkSpan(org.telegram.ui.Components.TextPaintMarkSpan) TextPaintImageReceiverSpan(org.telegram.ui.Components.TextPaintImageReceiverSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) TextPaint(android.text.TextPaint) SpannableStringBuilder(android.text.SpannableStringBuilder) MetricAffectingSpan(android.text.style.MetricAffectingSpan) AnchorSpan(org.telegram.ui.Components.AnchorSpan)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Paint (android.graphics.Paint)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 TextPaint (android.text.TextPaint)1 MetricAffectingSpan (android.text.style.MetricAffectingSpan)1 TLRPC (org.telegram.tgnet.TLRPC)1 TextSelectionHelper (org.telegram.ui.Cells.TextSelectionHelper)1 AnchorSpan (org.telegram.ui.Components.AnchorSpan)1 TextPaintImageReceiverSpan (org.telegram.ui.Components.TextPaintImageReceiverSpan)1 TextPaintMarkSpan (org.telegram.ui.Components.TextPaintMarkSpan)1 TextPaintSpan (org.telegram.ui.Components.TextPaintSpan)1 TextPaintUrlSpan (org.telegram.ui.Components.TextPaintUrlSpan)1 TextPaintWebpageUrlSpan (org.telegram.ui.Components.TextPaintWebpageUrlSpan)1