TypefaceEmojiSpan.java revision 77b5c5b734f9f665577d1e3d178615db43ae1d4f
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.support.text.emoji;
17
18import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
19
20import android.graphics.Canvas;
21import android.graphics.Paint;
22import android.support.annotation.IntRange;
23import android.support.annotation.NonNull;
24import android.support.annotation.RequiresApi;
25import android.support.annotation.RestrictTo;
26import android.text.TextPaint;
27
28/**
29 * EmojiSpan subclass used to render emojis using Typeface.
30 *
31 * @hide
32 */
33@RestrictTo(LIBRARY_GROUP)
34@RequiresApi(19)
35public final class TypefaceEmojiSpan extends EmojiSpan {
36
37    /**
38     * Paint object used to draw a background in debug mode.
39     */
40    private static Paint sDebugPaint;
41
42    /**
43     * Default constructor.
44     *
45     * @param metadata metadata representing the emoji that this span will draw
46     */
47    public TypefaceEmojiSpan(final EmojiMetadata metadata) {
48        super(metadata);
49    }
50
51    @Override
52    public void draw(@NonNull final Canvas canvas, final CharSequence text,
53            @IntRange(from = 0) final int start, @IntRange(from = 0) final int end, final float x,
54            final int top, final int y, final int bottom, @NonNull final Paint paint) {
55        if (EmojiCompat.get().isEmojiSpanIndicatorEnabled()) {
56            canvas.drawRect(x, top , x + getWidth(), bottom, getDebugPaint());
57        }
58        getMetadata().draw(canvas, x, y, paint);
59    }
60
61    private static Paint getDebugPaint() {
62        if (sDebugPaint == null) {
63            sDebugPaint = new TextPaint();
64            sDebugPaint.setColor(EmojiCompat.get().getEmojiSpanIndicatorColor());
65            sDebugPaint.setStyle(Paint.Style.FILL);
66        }
67        return sDebugPaint;
68    }
69
70
71}
72