@Test public void testGetSize() { final short dimensionX = 18; final short dimensionY = 20; final int fontHeight = 10; final float expectedRatio = fontHeight * 1.0f / dimensionY; final TextPaint paint = mock(TextPaint.class); when(paint.getFontMetricsInt(any(FontMetricsInt.class))).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { final FontMetricsInt fontMetrics = (FontMetricsInt) invocation.getArguments()[0]; fontMetrics.ascent = 0; fontMetrics.descent = -fontHeight; return null; } }); final EmojiMetadata metadata = mock(EmojiMetadata.class); when(metadata.getWidth()).thenReturn(dimensionX); when(metadata.getHeight()).thenReturn(dimensionY); final EmojiSpan span = new TypefaceEmojiSpan(metadata); final int resultSize = span.getSize(paint, "", 0, 0, null); assertEquals((int) (dimensionX * expectedRatio), resultSize); assertEquals(expectedRatio, span.getRatio(), 0.01f); assertEquals((int) (dimensionX * expectedRatio), span.getWidth()); assertEquals((int) (dimensionY * expectedRatio), span.getHeight()); }
@Override public int getSize(@NonNull final Paint paint, final CharSequence text, final int start, final int end, final Paint.FontMetricsInt fm) { paint.getFontMetricsInt(mTmpFontMetrics); final int fontHeight = Math.abs(mTmpFontMetrics.descent - mTmpFontMetrics.ascent); mRatio = fontHeight * 1.0f / mMetadata.getHeight(); mHeight = (short) (mMetadata.getHeight() * mRatio); mWidth = (short) (mMetadata.getWidth() * mRatio); if (fm != null) { fm.ascent = mTmpFontMetrics.ascent; fm.descent = mTmpFontMetrics.descent; fm.top = mTmpFontMetrics.top; fm.bottom = mTmpFontMetrics.bottom; } return mWidth; }
EmojiSpan extends ReplacementSpan { @Override public int getSize(@NonNull final Paint paint, final CharSequence text, final int start, final int end, final Paint.FontMetricsInt fm) { paint.getFontMetricsInt(mTmpFontMetrics); final int fontHeight = Math.abs(mTmpFontMetrics.descent - mTmpFontMetrics.ascent); mRatio = fontHeight * 1.0f / mMetadata.getHeight(); mHeight = (short) (mMetadata.getHeight() * mRatio); mWidth = (short) (mMetadata.getWidth() * mRatio); if (fm != null) { fm.ascent = mTmpFontMetrics.ascent; fm.descent = mTmpFontMetrics.descent; fm.top = mTmpFontMetrics.top; fm.bottom = mTmpFontMetrics.bottom; } return mWidth; } }
EmojiSpan extends ReplacementSpan { @Override public int getSize(@NonNull final Paint paint, final CharSequence text, final int start, final int end, final Paint.FontMetricsInt fm) { paint.getFontMetricsInt(mTmpFontMetrics); final int fontHeight = Math.abs(mTmpFontMetrics.descent - mTmpFontMetrics.ascent); mRatio = fontHeight * 1.0f / mMetadata.getHeight(); mHeight = (short) (mMetadata.getHeight() * mRatio); mWidth = (short) (mMetadata.getWidth() * mRatio); if (fm != null) { fm.ascent = mTmpFontMetrics.ascent; fm.descent = mTmpFontMetrics.descent; fm.top = mTmpFontMetrics.top; fm.bottom = mTmpFontMetrics.bottom; } return mWidth; } @RestrictTo(LIBRARY_GROUP)  EmojiSpan(@NonNull final EmojiMetadata metadata); }
EmojiSpan extends ReplacementSpan { @Override public int getSize(@NonNull final Paint paint, final CharSequence text, final int start, final int end, final Paint.FontMetricsInt fm) { paint.getFontMetricsInt(mTmpFontMetrics); final int fontHeight = Math.abs(mTmpFontMetrics.descent - mTmpFontMetrics.ascent); mRatio = fontHeight * 1.0f / mMetadata.getHeight(); mHeight = (short) (mMetadata.getHeight() * mRatio); mWidth = (short) (mMetadata.getWidth() * mRatio); if (fm != null) { fm.ascent = mTmpFontMetrics.ascent; fm.descent = mTmpFontMetrics.descent; fm.top = mTmpFontMetrics.top; fm.bottom = mTmpFontMetrics.bottom; } return mWidth; } @RestrictTo(LIBRARY_GROUP)  EmojiSpan(@NonNull final EmojiMetadata metadata); @Override int getSize(@NonNull final Paint paint, final CharSequence text, final int start,
            final int end, final Paint.FontMetricsInt fm); @RestrictTo(LIBRARY_GROUP) @VisibleForTesting final int getId(); }
EmojiSpan extends ReplacementSpan { @Override public int getSize(@NonNull final Paint paint, final CharSequence text, final int start, final int end, final Paint.FontMetricsInt fm) { paint.getFontMetricsInt(mTmpFontMetrics); final int fontHeight = Math.abs(mTmpFontMetrics.descent - mTmpFontMetrics.ascent); mRatio = fontHeight * 1.0f / mMetadata.getHeight(); mHeight = (short) (mMetadata.getHeight() * mRatio); mWidth = (short) (mMetadata.getWidth() * mRatio); if (fm != null) { fm.ascent = mTmpFontMetrics.ascent; fm.descent = mTmpFontMetrics.descent; fm.top = mTmpFontMetrics.top; fm.bottom = mTmpFontMetrics.bottom; } return mWidth; } @RestrictTo(LIBRARY_GROUP)  EmojiSpan(@NonNull final EmojiMetadata metadata); @Override int getSize(@NonNull final Paint paint, final CharSequence text, final int start,
            final int end, final Paint.FontMetricsInt fm); @RestrictTo(LIBRARY_GROUP) @VisibleForTesting final int getId();  }