Search in sources :

Example 1 with TextPaintWebpageUrlSpan

use of org.telegram.ui.Components.TextPaintWebpageUrlSpan 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)

Example 2 with TextPaintWebpageUrlSpan

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

the class ArticleViewer method createLayoutForText.

private DrawingText createLayoutForText(View parentView, CharSequence plainText, TLRPC.RichText richText, int width, int textY, TLRPC.PageBlock parentBlock, Layout.Alignment align, int maxLines, WebpageAdapter parentAdapter) {
    if (plainText == null && (richText == null || richText instanceof TLRPC.TL_textEmpty)) {
        return null;
    }
    if (width < 0) {
        width = AndroidUtilities.dp(10);
    }
    CharSequence text;
    if (plainText != null) {
        text = plainText;
    } else {
        text = getText(parentAdapter, parentView, richText, richText, parentBlock, width);
    }
    if (TextUtils.isEmpty(text)) {
        return null;
    }
    int additionalSize = AndroidUtilities.dp(SharedConfig.ivFontSize - 16);
    TextPaint paint;
    if (parentBlock instanceof TLRPC.TL_pageBlockEmbedPost && richText == null) {
        TLRPC.TL_pageBlockEmbedPost pageBlockEmbedPost = (TLRPC.TL_pageBlockEmbedPost) parentBlock;
        if (pageBlockEmbedPost.author == plainText) {
            if (embedPostAuthorPaint == null) {
                embedPostAuthorPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
                embedPostAuthorPaint.setColor(getTextColor());
            }
            embedPostAuthorPaint.setTextSize(AndroidUtilities.dp(15) + additionalSize);
            paint = embedPostAuthorPaint;
        } else {
            if (embedPostDatePaint == null) {
                embedPostDatePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
                embedPostDatePaint.setColor(getGrayTextColor());
            }
            embedPostDatePaint.setTextSize(AndroidUtilities.dp(14) + additionalSize);
            paint = embedPostDatePaint;
        }
    } else if (parentBlock instanceof TLRPC.TL_pageBlockChannel) {
        if (channelNamePaint == null) {
            channelNamePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            channelNamePaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
            channelNamePhotoPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            channelNamePhotoPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
        }
        channelNamePaint.setColor(getTextColor());
        channelNamePaint.setTextSize(AndroidUtilities.dp(15));
        channelNamePhotoPaint.setColor(0xffffffff);
        channelNamePhotoPaint.setTextSize(AndroidUtilities.dp(15));
        paint = parentAdapter.channelBlock != null ? channelNamePhotoPaint : channelNamePaint;
    } else if (parentBlock instanceof TL_pageBlockRelatedArticlesChild) {
        TL_pageBlockRelatedArticlesChild pageBlockRelatedArticlesChild = (TL_pageBlockRelatedArticlesChild) parentBlock;
        if (plainText == pageBlockRelatedArticlesChild.parent.articles.get(pageBlockRelatedArticlesChild.num).title) {
            if (relatedArticleHeaderPaint == null) {
                relatedArticleHeaderPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
                relatedArticleHeaderPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
            }
            relatedArticleHeaderPaint.setColor(getTextColor());
            relatedArticleHeaderPaint.setTextSize(AndroidUtilities.dp(15) + additionalSize);
            paint = relatedArticleHeaderPaint;
        } else {
            if (relatedArticleTextPaint == null) {
                relatedArticleTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            }
            relatedArticleTextPaint.setColor(getGrayTextColor());
            relatedArticleTextPaint.setTextSize(AndroidUtilities.dp(14) + additionalSize);
            paint = relatedArticleTextPaint;
        }
    } else if (isListItemBlock(parentBlock) && plainText != null) {
        if (listTextPointerPaint == null) {
            listTextPointerPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            listTextPointerPaint.setColor(getTextColor());
        }
        if (listTextNumPaint == null) {
            listTextNumPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            listTextNumPaint.setColor(getTextColor());
        }
        listTextPointerPaint.setTextSize(AndroidUtilities.dp(19) + additionalSize);
        listTextNumPaint.setTextSize(AndroidUtilities.dp(16) + additionalSize);
        if (parentBlock instanceof TL_pageBlockListItem && !((TL_pageBlockListItem) parentBlock).parent.pageBlockList.ordered) {
            paint = listTextPointerPaint;
        } else {
            paint = listTextNumPaint;
        }
    } else {
        paint = getTextPaint(richText, richText, parentBlock);
    }
    StaticLayout result;
    if (maxLines != 0) {
        if (parentBlock instanceof TLRPC.TL_pageBlockPullquote) {
            result = StaticLayoutEx.createStaticLayout(text, paint, width, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false, TextUtils.TruncateAt.END, width, maxLines);
        } else {
            result = StaticLayoutEx.createStaticLayout(text, paint, width, align, 1.0f, AndroidUtilities.dp(4), false, TextUtils.TruncateAt.END, width, maxLines);
        }
    } else {
        if (text.charAt(text.length() - 1) == '\n') {
            text = text.subSequence(0, text.length() - 1);
        }
        if (parentBlock instanceof TLRPC.TL_pageBlockPullquote) {
            result = new StaticLayout(text, paint, width, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
        } else {
            result = new StaticLayout(text, paint, width, align, 1.0f, AndroidUtilities.dp(4), false);
        }
    }
    if (result == null) {
        return null;
    }
    CharSequence finalText = result.getText();
    LinkPath textPath = null;
    LinkPath markPath = null;
    if (textY >= 0) {
        if (result != null && !searchResults.isEmpty() && searchText != null) {
            String lowerString = text.toString().toLowerCase();
            int startIndex = 0;
            int index;
            while ((index = lowerString.indexOf(searchText, startIndex)) >= 0) {
                startIndex = index + searchText.length();
                if (index == 0 || AndroidUtilities.isPunctuationCharacter(lowerString.charAt(index - 1))) {
                    adapter[0].searchTextOffset.put(searchText + parentBlock + richText + index, textY + result.getLineTop(result.getLineForOffset(index)));
                }
            }
        }
    }
    if (result != null && finalText instanceof Spanned) {
        Spanned spanned = (Spanned) finalText;
        try {
            AnchorSpan[] innerSpans = spanned.getSpans(0, spanned.length(), AnchorSpan.class);
            int linesCount = result.getLineCount();
            if (innerSpans != null && innerSpans.length > 0) {
                for (int a = 0; a < innerSpans.length; a++) {
                    if (linesCount <= 1) {
                        parentAdapter.anchorsOffset.put(innerSpans[a].getName(), textY);
                    } else {
                        parentAdapter.anchorsOffset.put(innerSpans[a].getName(), textY + result.getLineTop(result.getLineForOffset(spanned.getSpanStart(innerSpans[a]))));
                    }
                }
            }
        } catch (Exception ignore) {
        }
        try {
            TextPaintWebpageUrlSpan[] innerSpans = spanned.getSpans(0, spanned.length(), TextPaintWebpageUrlSpan.class);
            if (innerSpans != null && innerSpans.length > 0) {
                textPath = new LinkPath(true);
                textPath.setAllowReset(false);
                for (int a = 0; a < innerSpans.length; a++) {
                    int start = spanned.getSpanStart(innerSpans[a]);
                    int end = spanned.getSpanEnd(innerSpans[a]);
                    textPath.setCurrentLayout(result, start, 0);
                    int shift = innerSpans[a].getTextPaint() != null ? innerSpans[a].getTextPaint().baselineShift : 0;
                    textPath.setBaselineShift(shift != 0 ? shift + AndroidUtilities.dp(shift > 0 ? 5 : -2) : 0);
                    result.getSelectionPath(start, end, textPath);
                }
                textPath.setAllowReset(true);
            }
        } catch (Exception ignore) {
        }
        try {
            TextPaintMarkSpan[] innerSpans = spanned.getSpans(0, spanned.length(), TextPaintMarkSpan.class);
            if (innerSpans != null && innerSpans.length > 0) {
                markPath = new LinkPath(true);
                markPath.setAllowReset(false);
                for (int a = 0; a < innerSpans.length; a++) {
                    int start = spanned.getSpanStart(innerSpans[a]);
                    int end = spanned.getSpanEnd(innerSpans[a]);
                    markPath.setCurrentLayout(result, start, 0);
                    int shift = innerSpans[a].getTextPaint() != null ? innerSpans[a].getTextPaint().baselineShift : 0;
                    markPath.setBaselineShift(shift != 0 ? shift + AndroidUtilities.dp(shift > 0 ? 5 : -2) : 0);
                    result.getSelectionPath(start, end, markPath);
                }
                markPath.setAllowReset(true);
            }
        } catch (Exception ignore) {
        }
    }
    DrawingText drawingText = new DrawingText();
    drawingText.textLayout = result;
    drawingText.textPath = textPath;
    drawingText.markPath = markPath;
    drawingText.parentBlock = parentBlock;
    drawingText.parentText = richText;
    return drawingText;
}
Also used : TextPaintWebpageUrlSpan(org.telegram.ui.Components.TextPaintWebpageUrlSpan) TextPaintMarkSpan(org.telegram.ui.Components.TextPaintMarkSpan) StaticLayout(android.text.StaticLayout) Spanned(android.text.Spanned) TLRPC(org.telegram.tgnet.TLRPC) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) TextPaint(android.text.TextPaint) LinkPath(org.telegram.ui.Components.LinkPath) AnchorSpan(org.telegram.ui.Components.AnchorSpan)

Aggregations

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