Search in sources :

Example 96 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 97 with TextPaint

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

the class SuggestionsPopupWindowTest method testTextAppearanceInSuggestionsPopup.

@SmallTest
public void testTextAppearanceInSuggestionsPopup() {
    final String text = "abc def ghi";
    final String[] singleWordCandidates = { "DEF", "Def" };
    final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(), singleWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final String[] multiWordCandidates = { "ABC DEF GHI", "Abc Def Ghi" };
    final SuggestionSpan multiWordSuggestionSpan = new SuggestionSpan(getActivity(), multiWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final TypedArray array = getActivity().obtainStyledAttributes(com.android.internal.R.styleable.Theme);
    final int id = array.getResourceId(com.android.internal.R.styleable.Theme_textEditSuggestionHighlightStyle, 0);
    array.recycle();
    final TextAppearanceSpan expectedSpan = new TextAppearanceSpan(getActivity(), id);
    final TextPaint tmpTp = new TextPaint();
    expectedSpan.updateDrawState(tmpTp);
    final int expectedHighlightTextColor = tmpTp.getColor();
    final float expectedHighlightTextSize = tmpTp.getTextSize();
    final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
    // *XX* means that XX is highlighted.
    for (int i = 0; i < 2; i++) {
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(replaceText(text));
        setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
        setSuggestionSpan(multiWordSuggestionSpan, 0, text.length());
        showSuggestionsPopup();
        assertSuggestionsPopupIsDisplayed();
        assertSuggestionsPopupContainsItem("abc DEF ghi");
        assertSuggestionsPopupContainsItem("abc Def ghi");
        assertSuggestionsPopupContainsItem("ABC DEF GHI");
        assertSuggestionsPopupContainsItem("Abc Def Ghi");
        assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
        onSuggestionsPopup().check(new ViewAssertion() {

            @Override
            public void check(View view, NoMatchingViewException e) {
                final ListView listView = (ListView) view.findViewById(com.android.internal.R.id.suggestionContainer);
                assertNotNull(listView);
                final int childNum = listView.getChildCount();
                assertEquals(singleWordCandidates.length + multiWordCandidates.length, childNum);
                for (int j = 0; j < childNum; j++) {
                    final TextView suggestion = (TextView) listView.getChildAt(j);
                    assertNotNull(suggestion);
                    final Spanned spanned = (Spanned) suggestion.getText();
                    assertNotNull(spanned);
                    // Check that the suggestion item order is kept.
                    final String expectedText;
                    if (j < singleWordCandidates.length) {
                        expectedText = "abc " + singleWordCandidates[j] + " ghi";
                    } else {
                        expectedText = multiWordCandidates[j - singleWordCandidates.length];
                    }
                    assertEquals(expectedText, spanned.toString());
                    // Check that the text is highlighted with correct color and text size.
                    final TextAppearanceSpan[] taSpan = spanned.getSpans(text.indexOf('d'), text.indexOf('f') + 1, TextAppearanceSpan.class);
                    assertEquals(1, taSpan.length);
                    TextPaint tp = new TextPaint();
                    taSpan[0].updateDrawState(tp);
                    assertEquals(expectedHighlightTextColor, tp.getColor());
                    assertEquals(expectedHighlightTextSize, tp.getTextSize());
                    // Check the correct part of the text is highlighted.
                    final int expectedStart;
                    final int expectedEnd;
                    if (j < singleWordCandidates.length) {
                        expectedStart = text.indexOf('d');
                        expectedEnd = text.indexOf('f') + 1;
                    } else {
                        expectedStart = 0;
                        expectedEnd = text.length();
                    }
                    assertEquals(expectedStart, spanned.getSpanStart(taSpan[0]));
                    assertEquals(expectedEnd, spanned.getSpanEnd(taSpan[0]));
                }
            }
        });
        pressBack();
        onView(withId(R.id.textview)).inRoot(withDecorView(is(getActivity().getWindow().getDecorView()))).perform(clearText());
    }
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) RootMatchers.withDecorView(android.support.test.espresso.matcher.RootMatchers.withDecorView) DragHandleUtils.onHandleView(android.widget.espresso.DragHandleUtils.onHandleView) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint) ViewAssertion(android.support.test.espresso.ViewAssertion) TypedArray(android.content.res.TypedArray) SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 98 with TextPaint

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

the class TextAppearanceSpan method updateMeasureState.

@Override
public void updateMeasureState(TextPaint ds) {
    if (mTypeface != null || mStyle != 0) {
        Typeface tf = ds.getTypeface();
        int style = 0;
        if (tf != null) {
            style = tf.getStyle();
        }
        style |= mStyle;
        if (mTypeface != null) {
            tf = Typeface.create(mTypeface, style);
        } else if (tf == null) {
            tf = Typeface.defaultFromStyle(style);
        } else {
            tf = Typeface.create(tf, style);
        }
        int fake = style & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            ds.setFakeBoldText(true);
        }
        if ((fake & Typeface.ITALIC) != 0) {
            ds.setTextSkewX(-0.25f);
        }
        ds.setTypeface(tf);
    }
    if (mTextSize > 0) {
        ds.setTextSize(mTextSize);
    }
}
Also used : Typeface(android.graphics.Typeface) TextPaint(android.text.TextPaint)

Example 99 with TextPaint

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

the class BiDiTestViewDrawText method onDraw.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    final int width = getWidth();
    final int height = getHeight();
    final TextPaint paint = new TextPaint();
    paint.setTextSize(mSize);
    paint.setColor(mColor);
    paint.setTextAlign(Align.CENTER);
    canvas.drawText(mText, width / 2, height * 2 / 3, paint);
}
Also used : TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint)

Example 100 with TextPaint

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

the class MediaProjectionPermissionActivity method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    mPackageName = getCallingPackage();
    IBinder b = ServiceManager.getService(MEDIA_PROJECTION_SERVICE);
    mService = IMediaProjectionManager.Stub.asInterface(b);
    if (mPackageName == null) {
        finish();
        return;
    }
    PackageManager packageManager = getPackageManager();
    ApplicationInfo aInfo;
    try {
        aInfo = packageManager.getApplicationInfo(mPackageName, 0);
        mUid = aInfo.uid;
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "unable to look up package name", e);
        finish();
        return;
    }
    try {
        if (mService.hasProjectionPermission(mUid, mPackageName)) {
            setResult(RESULT_OK, getMediaProjectionIntent(mUid, mPackageName, false));
            finish();
            return;
        }
    } catch (RemoteException e) {
        Log.e(TAG, "Error checking projection permissions", e);
        finish();
        return;
    }
    TextPaint paint = new TextPaint();
    paint.setTextSize(42);
    String label = aInfo.loadLabel(packageManager).toString();
    // If the label contains new line characters it may push the security
    // message below the fold of the dialog. Labels shouldn't have new line
    // characters anyways, so just truncate the message the first time one
    // is seen.
    final int labelLength = label.length();
    int offset = 0;
    while (offset < labelLength) {
        final int codePoint = label.codePointAt(offset);
        final int type = Character.getType(codePoint);
        if (type == Character.LINE_SEPARATOR || type == Character.CONTROL || type == Character.PARAGRAPH_SEPARATOR) {
            label = label.substring(0, offset) + ELLIPSIS;
            break;
        }
        offset += Character.charCount(codePoint);
    }
    if (label.isEmpty()) {
        label = mPackageName;
    }
    String unsanitizedAppName = TextUtils.ellipsize(label, paint, MAX_APP_NAME_SIZE_PX, TextUtils.TruncateAt.END).toString();
    String appName = BidiFormatter.getInstance().unicodeWrap(unsanitizedAppName);
    String actionText = getString(R.string.media_projection_dialog_text, appName);
    SpannableString message = new SpannableString(actionText);
    int appNameIndex = actionText.indexOf(appName);
    if (appNameIndex >= 0) {
        message.setSpan(new StyleSpan(Typeface.BOLD), appNameIndex, appNameIndex + appName.length(), 0);
    }
    mDialog = new AlertDialog.Builder(this).setIcon(aInfo.loadIcon(packageManager)).setMessage(message).setPositiveButton(R.string.media_projection_action_text, this).setNegativeButton(android.R.string.cancel, this).setView(R.layout.remember_permission_checkbox).setOnCancelListener(this).create();
    mDialog.create();
    mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true);
    ((CheckBox) mDialog.findViewById(R.id.remember)).setOnCheckedChangeListener(this);
    mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    mDialog.show();
}
Also used : SpannableString(android.text.SpannableString) IBinder(android.os.IBinder) PackageManager(android.content.pm.PackageManager) CheckBox(android.widget.CheckBox) StyleSpan(android.text.style.StyleSpan) ApplicationInfo(android.content.pm.ApplicationInfo) SpannableString(android.text.SpannableString) RemoteException(android.os.RemoteException) TextPaint(android.text.TextPaint) 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