Search in sources :

Example 46 with CharacterStyle

use of android.text.style.CharacterStyle in project android_frameworks_base by crdroidandroid.

the class HtmlToSpannedConverter method withinParagraph.

private static void withinParagraph(StringBuilder out, Spanned text, int start, int end) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, CharacterStyle.class);
        CharacterStyle[] style = text.getSpans(i, next, CharacterStyle.class);
        for (int j = 0; j < style.length; j++) {
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("<b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("<i>");
                }
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if ("monospace".equals(s)) {
                    out.append("<tt>");
                }
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("<sup>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("<sub>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("<u>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("<span style=\"text-decoration:line-through;\">");
            }
            if (style[j] instanceof URLSpan) {
                out.append("<a href=\"");
                out.append(((URLSpan) style[j]).getURL());
                out.append("\">");
            }
            if (style[j] instanceof ImageSpan) {
                out.append("<img src=\"");
                out.append(((ImageSpan) style[j]).getSource());
                out.append("\">");
                // Don't output the dummy character underlying the image.
                i = next;
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                AbsoluteSizeSpan s = ((AbsoluteSizeSpan) style[j]);
                float sizeDip = s.getSize();
                if (!s.getDip()) {
                    Application application = ActivityThread.currentApplication();
                    sizeDip /= application.getResources().getDisplayMetrics().density;
                }
                // px in CSS is the equivalance of dip in Android
                out.append(String.format("<span style=\"font-size:%.0fpx\";>", sizeDip));
            }
            if (style[j] instanceof RelativeSizeSpan) {
                float sizeEm = ((RelativeSizeSpan) style[j]).getSizeChange();
                out.append(String.format("<span style=\"font-size:%.2fem;\">", sizeEm));
            }
            if (style[j] instanceof ForegroundColorSpan) {
                int color = ((ForegroundColorSpan) style[j]).getForegroundColor();
                out.append(String.format("<span style=\"color:#%06X;\">", 0xFFFFFF & color));
            }
            if (style[j] instanceof BackgroundColorSpan) {
                int color = ((BackgroundColorSpan) style[j]).getBackgroundColor();
                out.append(String.format("<span style=\"background-color:#%06X;\">", 0xFFFFFF & color));
            }
        }
        withinStyle(out, text, i, next);
        for (int j = style.length - 1; j >= 0; j--) {
            if (style[j] instanceof BackgroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof ForegroundColorSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof RelativeSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof AbsoluteSizeSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof URLSpan) {
                out.append("</a>");
            }
            if (style[j] instanceof StrikethroughSpan) {
                out.append("</span>");
            }
            if (style[j] instanceof UnderlineSpan) {
                out.append("</u>");
            }
            if (style[j] instanceof SubscriptSpan) {
                out.append("</sub>");
            }
            if (style[j] instanceof SuperscriptSpan) {
                out.append("</sup>");
            }
            if (style[j] instanceof TypefaceSpan) {
                String s = ((TypefaceSpan) style[j]).getFamily();
                if (s.equals("monospace")) {
                    out.append("</tt>");
                }
            }
            if (style[j] instanceof StyleSpan) {
                int s = ((StyleSpan) style[j]).getStyle();
                if ((s & Typeface.BOLD) != 0) {
                    out.append("</b>");
                }
                if ((s & Typeface.ITALIC) != 0) {
                    out.append("</i>");
                }
            }
        }
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) URLSpan(android.text.style.URLSpan) CharacterStyle(android.text.style.CharacterStyle) UnderlineSpan(android.text.style.UnderlineSpan) AbsoluteSizeSpan(android.text.style.AbsoluteSizeSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) Application(android.app.Application) BackgroundColorSpan(android.text.style.BackgroundColorSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan) ImageSpan(android.text.style.ImageSpan)

Example 47 with CharacterStyle

use of android.text.style.CharacterStyle in project android_frameworks_base by crdroidandroid.

the class TextView method spanChange.

/**
     * Not private so it can be called from an inner class without going
     * through a thunk.
     */
void spanChange(Spanned buf, Object what, int oldStart, int newStart, int oldEnd, int newEnd) {
    // XXX Make the start and end move together if this ends up
    // spending too much time invalidating.
    boolean selChanged = false;
    int newSelStart = -1, newSelEnd = -1;
    final Editor.InputMethodState ims = mEditor == null ? null : mEditor.mInputMethodState;
    if (what == Selection.SELECTION_END) {
        selChanged = true;
        newSelEnd = newStart;
        if (oldStart >= 0 || newStart >= 0) {
            invalidateCursor(Selection.getSelectionStart(buf), oldStart, newStart);
            checkForResize();
            registerForPreDraw();
            if (mEditor != null)
                mEditor.makeBlink();
        }
    }
    if (what == Selection.SELECTION_START) {
        selChanged = true;
        newSelStart = newStart;
        if (oldStart >= 0 || newStart >= 0) {
            int end = Selection.getSelectionEnd(buf);
            invalidateCursor(end, oldStart, newStart);
        }
    }
    if (selChanged) {
        mHighlightPathBogus = true;
        if (mEditor != null && !isFocused())
            mEditor.mSelectionMoved = true;
        if ((buf.getSpanFlags(what) & Spanned.SPAN_INTERMEDIATE) == 0) {
            if (newSelStart < 0) {
                newSelStart = Selection.getSelectionStart(buf);
            }
            if (newSelEnd < 0) {
                newSelEnd = Selection.getSelectionEnd(buf);
            }
            if (mEditor != null) {
                mEditor.refreshTextActionMode();
                if (!hasSelection() && mEditor.mTextActionMode == null && hasTransientState()) {
                    // User generated selection has been removed.
                    setHasTransientState(false);
                }
            }
            onSelectionChanged(newSelStart, newSelEnd);
        }
    }
    if (what instanceof UpdateAppearance || what instanceof ParagraphStyle || what instanceof CharacterStyle) {
        if (ims == null || ims.mBatchEditNesting == 0) {
            invalidate();
            mHighlightPathBogus = true;
            checkForResize();
        } else {
            ims.mContentChanged = true;
        }
        if (mEditor != null) {
            if (oldStart >= 0)
                mEditor.invalidateTextDisplayList(mLayout, oldStart, oldEnd);
            if (newStart >= 0)
                mEditor.invalidateTextDisplayList(mLayout, newStart, newEnd);
            mEditor.invalidateHandlesAndActionMode();
        }
    }
    if (MetaKeyKeyListener.isMetaTracker(buf, what)) {
        mHighlightPathBogus = true;
        if (ims != null && MetaKeyKeyListener.isSelectingMetaTracker(buf, what)) {
            ims.mSelectionModeChanged = true;
        }
        if (Selection.getSelectionStart(buf) >= 0) {
            if (ims == null || ims.mBatchEditNesting == 0) {
                invalidateCursor();
            } else {
                ims.mCursorChanged = true;
            }
        }
    }
    if (what instanceof ParcelableSpan) {
        // the current extract editor would be interested in it.
        if (ims != null && ims.mExtractedTextRequest != null) {
            if (ims.mBatchEditNesting != 0) {
                if (oldStart >= 0) {
                    if (ims.mChangedStart > oldStart) {
                        ims.mChangedStart = oldStart;
                    }
                    if (ims.mChangedStart > oldEnd) {
                        ims.mChangedStart = oldEnd;
                    }
                }
                if (newStart >= 0) {
                    if (ims.mChangedStart > newStart) {
                        ims.mChangedStart = newStart;
                    }
                    if (ims.mChangedStart > newEnd) {
                        ims.mChangedStart = newEnd;
                    }
                }
            } else {
                if (DEBUG_EXTRACT)
                    Log.v(LOG_TAG, "Span change outside of batch: " + oldStart + "-" + oldEnd + "," + newStart + "-" + newEnd + " " + what);
                ims.mContentChanged = true;
            }
        }
    }
    if (mEditor != null && mEditor.mSpellChecker != null && newStart < 0 && what instanceof SpellCheckSpan) {
        mEditor.mSpellChecker.onSpellCheckSpanRemoved((SpellCheckSpan) what);
    }
}
Also used : UpdateAppearance(android.text.style.UpdateAppearance) SpellCheckSpan(android.text.style.SpellCheckSpan) ParagraphStyle(android.text.style.ParagraphStyle) ParcelableSpan(android.text.ParcelableSpan) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) CharacterStyle(android.text.style.CharacterStyle)

Example 48 with CharacterStyle

use of android.text.style.CharacterStyle in project mongol-library by suragch.

the class MongolTextLine method draw.

/**
 * Renders the TextLine.
 *
 * @param c the canvas to render on
 * @param x the leading margin position
 * @param top the top of the line
 * @param y the baseline
 * @param bottom the bottom of the line
 */
void draw(Canvas c, float x, float top, float y, int bottom) {
    // (x, y) are the start coordinates of each vertical line
    // where x is the top of the line and y is the baseline running down.
    // Don't confuse these with Paint.drawText coordinates.
    // top and bottom are the font metrics values in the normal
    // horizontal orientation of a text line.
    boolean hasSpan = mText instanceof Spanned;
    c.save();
    c.translate(x, y);
    c.rotate(90);
    for (TextRun run : mTextRuns) {
        int start = run.offset;
        int end = run.offset + run.length;
        TextPaint wp;
        if (hasSpan) {
            wp = mWorkPaint;
            wp.set(mPaint);
            CharacterStyle[] csSpans = ((Spanned) mText).getSpans(start, end, CharacterStyle.class);
            for (CharacterStyle span : csSpans) {
                span.updateDrawState(wp);
            }
        } else {
            wp = mPaint;
        }
        float width = (run.isRotated) ? run.measuredHeight : wp.measureText(mText, start, end);
        // background color
        if (wp.bgColor != 0) {
            int previousColor = wp.getColor();
            Paint.Style previousStyle = wp.getStyle();
            wp.setColor(wp.bgColor);
            wp.setStyle(Paint.Style.FILL);
            c.drawRect(0, top, width, bottom, wp);
            wp.setStyle(previousStyle);
            wp.setColor(previousColor);
        }
        // "underline" (to the right of vertical text)
        if (wp.isUnderlineText()) {
            wp.setUnderlineText(false);
            Paint.Style previousStyle = wp.getStyle();
            wp.setStyle(Paint.Style.FILL);
            float underlineThickness = (top - bottom) * UNDERLINE_THICKNESS_PROPORTION;
            c.drawRect(0, top, width, top - underlineThickness, wp);
            wp.setStyle(previousStyle);
        }
        // text
        if (run.isRotated) {
            c.save();
            c.rotate(-90);
            c.translate(-bottom, width - bottom);
            c.drawText(mText, start, end, -wp.baselineShift, 0, wp);
            c.restore();
        } else {
            c.drawText(mText, start, end, 0, wp.baselineShift, wp);
        }
        // move into position for next text run
        c.translate(width, 0);
    }
    c.restore();
}
Also used : TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) CharacterStyle(android.text.style.CharacterStyle) TextPaint(android.text.TextPaint)

Example 49 with CharacterStyle

use of android.text.style.CharacterStyle in project mongol-library by suragch.

the class MongolTextStorage method updateGlyphTextForUnicodeRange.

private void updateGlyphTextForUnicodeRange(int start, int end) {
    // initialize glyph indexes
    final int lengthUnicode = mUnicodeText.length();
    if (mGlyphIndexes == null) {
        mGlyphIndexes = new ArrayList<>(lengthUnicode);
        for (int i = 0; i < lengthUnicode; i++) {
            mGlyphIndexes.add(i, i);
        }
    } else if (mGlyphIndexes.size() < lengthUnicode) {
        final int size = mGlyphIndexes.size();
        for (int i = size; i < lengthUnicode; i++) {
            mGlyphIndexes.add(i, i);
        }
    }
    // update glyph indexes
    // Non rendered characters (mvs, fvs) follow rendered chars
    // Exception: non rendered chars at the beginning of the string
    // FIXME: can produce index mismatches when mvs or fvs in illegal position (ex: SA + MVS + " " + MVS + SA)
    boolean indexingHasStarted = false;
    int glyphIndex = 0;
    if (start > 0) {
        glyphIndex = getGlyphIndexForUnicodeIndex(start - 1);
        if (glyphIndex > 0 || MongolCode.isRenderedGlyph(mUnicodeText.charAt(0))) {
            indexingHasStarted = true;
        }
    }
    for (int i = start; i < lengthUnicode; i++) {
        final char currentChar = mUnicodeText.charAt(i);
        if (MongolCode.isRenderedGlyph(currentChar)) {
            if (indexingHasStarted) {
                glyphIndex++;
            }
            indexingHasStarted = true;
        }
        mGlyphIndexes.set(i, glyphIndex);
    }
    if (!(mUnicodeText instanceof Spanned))
        return;
    // add spans to glyph string
    CharacterStyle[] spans = ((Spanned) mUnicodeText).getSpans(start, end, CharacterStyle.class);
    for (CharacterStyle span : spans) {
        final int unicodeStart = ((Spanned) mUnicodeText).getSpanStart(span);
        final int unicodeEnd = ((Spanned) mUnicodeText).getSpanEnd(span);
        final int glyphStart = getGlyphIndexForUnicodeIndex(unicodeStart);
        final int glyphEnd = getGlyphIndexForUnicodeIndex(unicodeEnd);
        ((SpannableStringBuilder) mGlyphText).setSpan(span, glyphStart, glyphEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}
Also used : Spanned(android.text.Spanned) CharacterStyle(android.text.style.CharacterStyle) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 50 with CharacterStyle

use of android.text.style.CharacterStyle in project PhoneProfilesPlus by henrichg.

the class ApplicationsDialogPreferenceViewHolder method setTextStyle.

private void setTextStyle(TextView textView, boolean errorColor) {
    if (textView != null) {
        CharSequence title = textView.getText();
        Spannable sbt = new SpannableString(title);
        Object[] spansToRemove = sbt.getSpans(0, title.length(), Object.class);
        for (Object span : spansToRemove) {
            if (span instanceof CharacterStyle)
                sbt.removeSpan(span);
        }
        if (errorColor) {
            sbt.setSpan(new ForegroundColorSpan(Color.RED), 0, sbt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(sbt);
        } else {
            textView.setText(sbt);
        }
    }
}
Also used : SpannableString(android.text.SpannableString) ForegroundColorSpan(android.text.style.ForegroundColorSpan) Spannable(android.text.Spannable) CharacterStyle(android.text.style.CharacterStyle)

Aggregations

CharacterStyle (android.text.style.CharacterStyle)58 SpannableStringBuilder (android.text.SpannableStringBuilder)22 ForegroundColorSpan (android.text.style.ForegroundColorSpan)18 RelativeSizeSpan (android.text.style.RelativeSizeSpan)18 Paint (android.graphics.Paint)16 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)14 StyleSpan (android.text.style.StyleSpan)13 TextPaint (android.text.TextPaint)12 ParcelableSpan (android.text.ParcelableSpan)9 SpannableString (android.text.SpannableString)9 Spanned (android.text.Spanned)9 StrikethroughSpan (android.text.style.StrikethroughSpan)9 TypefaceSpan (android.text.style.TypefaceSpan)9 UnderlineSpan (android.text.style.UnderlineSpan)9 BackgroundColorSpan (android.text.style.BackgroundColorSpan)8 ReplacementSpan (android.text.style.ReplacementSpan)8 SubscriptSpan (android.text.style.SubscriptSpan)8 SuperscriptSpan (android.text.style.SuperscriptSpan)8 URLSpan (android.text.style.URLSpan)8 ImageSpan (android.text.style.ImageSpan)7