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  */
16 package android.support.text.emoji;
17 
18 import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
19 
20 import android.graphics.Canvas;
21 import android.graphics.Paint;
22 import android.support.annotation.IntRange;
23 import android.support.annotation.NonNull;
24 import android.support.annotation.RequiresApi;
25 import android.support.annotation.RestrictTo;
26 import 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)
35 public 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      */
TypefaceEmojiSpan(final EmojiMetadata metadata)47     public TypefaceEmojiSpan(final EmojiMetadata metadata) {
48         super(metadata);
49     }
50 
51     @Override
draw(@onNull final Canvas canvas, final CharSequence text, @IntRange(from = 0) final int start, @IntRange(from = 0) final int end, final float x, final int top, final int y, final int bottom, @NonNull final Paint paint)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 
getDebugPaint()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