Search in sources :

Example 91 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by crdroidandroid.

the class PackageItemInfo method loadSafeLabel.

/**
     * Same as {@link #loadLabel(PackageManager)} with the addition that
     * the returned label is safe for being presented in the UI since it
     * will not contain new lines and the length will be limited to a
     * reasonable amount. This prevents a malicious party to influence UI
     * layout via the app label misleading the user into performing a
     * detrimental for them action. If the label is too long it will be
     * truncated and ellipsized at the end.
     *
     * @param pm A PackageManager from which the label can be loaded; usually
     * the PackageManager from which you originally retrieved this item
     * @return Returns a CharSequence containing the item's label. If the
     * item does not have a label, its name is returned.
     *
     * @hide
     */
@SystemApi
@NonNull
public CharSequence loadSafeLabel(@NonNull PackageManager pm) {
    // loadLabel() always returns non-null
    String label = loadLabel(pm).toString();
    // strip HTML tags to avoid <br> and other tags overwriting original message
    String labelStr = Html.fromHtml(label).toString();
    // If the label contains new line characters it may push the UI
    // down to hide a part of it. Labels shouldn't have new line
    // characters, so just truncate at the first time one is seen.
    final int labelLength = labelStr.length();
    int offset = 0;
    while (offset < labelLength) {
        final int codePoint = labelStr.codePointAt(offset);
        final int type = Character.getType(codePoint);
        if (type == Character.LINE_SEPARATOR || type == Character.CONTROL || type == Character.PARAGRAPH_SEPARATOR) {
            labelStr = labelStr.substring(0, offset);
            break;
        }
        // replace all non-break space to " " in order to be trimmed
        if (type == Character.SPACE_SEPARATOR) {
            labelStr = labelStr.substring(0, offset) + " " + labelStr.substring(offset + Character.charCount(codePoint));
        }
        offset += Character.charCount(codePoint);
    }
    labelStr = labelStr.trim();
    if (labelStr.isEmpty()) {
        return packageName;
    }
    TextPaint paint = new TextPaint();
    paint.setTextSize(42);
    return TextUtils.ellipsize(labelStr, paint, MAX_LABEL_SIZE_PX, TextUtils.TruncateAt.END);
}
Also used : TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint) SystemApi(android.annotation.SystemApi) NonNull(android.annotation.NonNull)

Example 92 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by crdroidandroid.

the class StaticLayoutTest method moveCursorToLeftCursorableOffset.

private void moveCursorToLeftCursorableOffset(EditorState state, TextPaint paint) {
    assertEquals("The editor has selection", state.mSelectionStart, state.mSelectionEnd);
    final Layout layout = builder().setText(state.mText.toString()).setPaint(paint).build();
    final int newOffset = layout.getOffsetToLeftOf(state.mSelectionStart);
    state.mSelectionStart = state.mSelectionEnd = newOffset;
}
Also used : TextPaint(android.text.TextPaint)

Example 93 with TextPaint

use of android.text.TextPaint in project android_frameworks_base by crdroidandroid.

the class StaticLayoutTest method testEmojiOffset.

/**
     * Tests for keycap, variation selectors, flags are in CTS.
     * See {@link android.text.cts.StaticLayoutTest}.
     */
public void testEmojiOffset() {
    EditorState state = new EditorState();
    TextPaint paint = new TextPaint();
    // Odd numbered regional indicator symbols.
    // U+1F1E6 is REGIONAL INDICATOR SYMBOL LETTER A, U+1F1E8 is REGIONAL INDICATOR SYMBOL
    // LETTER C.
    state.setByString("| U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 | U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 | U+1F1E6");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6 |");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6 |");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 | U+1F1E6");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("U+1F1E6 U+1F1E8 | U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("| U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("| U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6");
    moveCursorToLeftCursorableOffset(state, paint);
    // Zero width sequence
    final String zwjSequence = "U+1F468 U+200D U+2764 U+FE0F U+200D U+1F468";
    state.setByString("| " + zwjSequence + " " + zwjSequence + " " + zwjSequence);
    moveCursorToRightCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " | " + zwjSequence + " " + zwjSequence);
    moveCursorToRightCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " " + zwjSequence + " | " + zwjSequence);
    moveCursorToRightCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " " + zwjSequence + " " + zwjSequence + " |");
    moveCursorToRightCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " " + zwjSequence + " " + zwjSequence + " |");
    moveCursorToLeftCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " " + zwjSequence + " | " + zwjSequence);
    moveCursorToLeftCursorableOffset(state, paint);
    state.assertEquals(zwjSequence + " | " + zwjSequence + " " + zwjSequence);
    moveCursorToLeftCursorableOffset(state, paint);
    state.assertEquals("| " + zwjSequence + " " + zwjSequence + " " + zwjSequence);
    moveCursorToLeftCursorableOffset(state, paint);
    state.assertEquals("| " + zwjSequence + " " + zwjSequence + " " + zwjSequence);
    moveCursorToLeftCursorableOffset(state, paint);
    // Emoji modifiers
    // U+261D is WHITE UP POINTING INDEX, U+1F3FB is EMOJI MODIFIER FITZPATRICK TYPE-1-2.
    state.setByString("| U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB | U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB U+261D U+1F3FB | U+261D U+1F3FB");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB |");
    moveCursorToRightCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB |");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB U+261D U+1F3FB | U+261D U+1F3FB");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("U+261D U+1F3FB | U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("| U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToLeftCursorableOffset(state, paint);
    state.setByString("| U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB");
    moveCursorToLeftCursorableOffset(state, paint);
}
Also used : EditorState(android.text.method.EditorState) TextPaint(android.text.TextPaint)

Example 94 with TextPaint

use of android.text.TextPaint in project JustAndroid by chinaltz.

the class AbGraphicUtil method getDrawRowStr.

/**
      * 解析成行.
      * @param text the text
      * @param maxWPix the max w pix
      * @param paint the paint
      * @return the draw row count
      */
public static List<String> getDrawRowStr(String text, int maxWPix, TextPaint paint) {
    String[] texts = null;
    if (text.indexOf("\n") != -1) {
        texts = text.split("\n");
    } else {
        texts = new String[1];
        texts[0] = text;
    }
    //共多少行
    List<String> mStrList = new ArrayList<String>();
    for (int i = 0; i < texts.length; i++) {
        String textLine = texts[i];
        //计算这个文本显示为几行
        while (true) {
            //可容纳的最后一个字的位置
            int endIndex = subStringLength(textLine, maxWPix, paint);
            if (endIndex <= 0) {
                mStrList.add(textLine);
            } else {
                if (endIndex == textLine.length() - 1) {
                    mStrList.add(textLine);
                } else {
                    mStrList.add(textLine.substring(0, endIndex + 1));
                }
            }
            //获取剩下的
            if (textLine.length() > endIndex + 1) {
                //还有剩下的
                textLine = textLine.substring(endIndex + 1);
            } else {
                break;
            }
        }
    }
    return mStrList;
}
Also used : ArrayList(java.util.ArrayList) TextPaint(android.text.TextPaint)

Example 95 with TextPaint

use of android.text.TextPaint in project JustAndroid by chinaltz.

the class AbGraphicUtil method getDrawRowCount.

/**
      * 
      * 获取这段文本多少行.
      * @param text
      * @param maxWPix
      * @param paint
      * @return
      */
public static int getDrawRowCount(String text, int maxWPix, TextPaint paint) {
    String[] texts = null;
    if (text.indexOf("\n") != -1) {
        texts = text.split("\n");
    } else {
        texts = new String[1];
        texts[0] = text;
    }
    //共多少行
    List<String> mStrList = new ArrayList<String>();
    for (int i = 0; i < texts.length; i++) {
        String textLine = texts[i];
        //计算这个文本显示为几行
        while (true) {
            //可容纳的最后一个字的位置
            int endIndex = subStringLength(textLine, maxWPix, paint);
            if (endIndex <= 0) {
                mStrList.add(textLine);
            } else {
                if (endIndex == textLine.length() - 1) {
                    mStrList.add(textLine);
                } else {
                    mStrList.add(textLine.substring(0, endIndex + 1));
                }
            }
            //获取剩下的
            if (textLine.length() > endIndex + 1) {
                //还有剩下的
                textLine = textLine.substring(endIndex + 1);
            } else {
                break;
            }
        }
    }
    return mStrList.size();
}
Also used : ArrayList(java.util.ArrayList) TextPaint(android.text.TextPaint)

Aggregations

TextPaint (android.text.TextPaint)216 Paint (android.graphics.Paint)86 StaticLayout (android.text.StaticLayout)29 View (android.view.View)17 Bitmap (android.graphics.Bitmap)14 Typeface (android.graphics.Typeface)14 TypedArray (android.content.res.TypedArray)12 SpannableString (android.text.SpannableString)12 RectF (android.graphics.RectF)11 Spanned (android.text.Spanned)11 TextView (android.widget.TextView)11 Rect (android.graphics.Rect)8 StyleSpan (android.text.style.StyleSpan)8 ClickableSpan (android.text.style.ClickableSpan)7 PackageManager (android.content.pm.PackageManager)6 Canvas (android.graphics.Canvas)6 IBinder (android.os.IBinder)6 SpannableStringBuilder (android.text.SpannableStringBuilder)6 Context (android.content.Context)5 Resources (android.content.res.Resources)5