-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathText.java
234 lines (183 loc) · 5.2 KB
/
Text.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package su.levenetc.android.textsurface;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import su.levenetc.android.textsurface.common.Position;
import su.levenetc.android.textsurface.common.ScaleValue;
import su.levenetc.android.textsurface.interfaces.ITextEffect;
import su.levenetc.android.textsurface.utils.Utils;
/**
* Created by Eugene Levenetc.
*/
public class Text implements Comparable<Text> {
private Paint paint;
private final String text;
private RectF defaultSize;
private RectF currentSize = new RectF();
private Position position;
private RectF padding;
private int index;
private ScaleValue scale = new ScaleValue();
private Matrix matrix = new Matrix();
private ArrayList<ITextEffect> effects = new ArrayList<>();
private float dx;
private float fontDescent;
public Text(String text, Position position, RectF padding, Paint paint) {
this.text = text;
this.position = position;
this.padding = padding;
this.paint = paint;
initBounds(text);
}
public void addEffect(ITextEffect effect) {
effects.add(effect);
}
public float getFontDescent() {
return fontDescent;
}
private void initBounds(String text) {
String trimmed = text.trim();
if (trimmed.length() < text.length()) {
int length = text.length();
int start = text.lastIndexOf(trimmed);
int end = length - (start + trimmed.length());
text = Utils.genString(start) + text + Utils.genString(end);
}
Rect tmp = new Rect();
paint.getTextBounds(text, 0, text.length(), tmp);
fontDescent = paint.getFontMetrics().descent;
defaultSize = new RectF(tmp);
//a little workaround because getTextBounds returns smaller width than it is
dx = paint.measureText(text) - tmp.width();
defaultSize.left = 0;
defaultSize.right = tmp.width() + dx;
defaultSize.top = -paint.getFontSpacing();
defaultSize.bottom = 0;
defaultSize.set(
defaultSize.left,
defaultSize.top,
defaultSize.right,
defaultSize.bottom
);
currentSize.set(
defaultSize.left,
defaultSize.top,
defaultSize.right,
defaultSize.bottom
);
}
public void setIndex(int index) {
this.index = index;
}
public int getIndex() {
return index;
}
public void onDraw(Canvas canvas, TextSurface textSurface) {
layout(textSurface);
canvas.save();
canvas.concat(matrix);
final float finalX = padding.left;
if (effects.isEmpty()) {
canvas.drawText(text, finalX, -padding.bottom - fontDescent, paint);
} else {
for (ITextEffect effect : effects) {
canvas.save();
effect.apply(canvas, text, finalX, -padding.bottom, paint);
canvas.drawText(text, finalX, -padding.bottom, paint);
canvas.restore();
}
}
canvas.restore();
if (Debug.ENABLED) {
canvas.drawRect(
currentSize.left,
currentSize.top - padding.bottom - padding.top,
currentSize.right + padding.left + padding.right,
currentSize.bottom,
Debug.RED_STROKE
);
}
}
void layout(TextSurface textSurface) {
currentSize.set(defaultSize.left, defaultSize.top, defaultSize.right, defaultSize.bottom);
final float sx = scale.getScaleX();
final float sy = scale.getScaleY();
final float sPivotX = position.getRelativeX((int) scale.getPivot().x, this, false);
final float sPivotY = position.getRelativeY((int) scale.getPivot().y, this, false);
final float x = position.getX(textSurface, getWidth() * sx);
final float y = position.getY(textSurface, getHeight() * sy);
matrix.reset();
matrix.preTranslate(x, y);
matrix.preScale(sx, sy, sPivotX, sPivotY);
matrix.mapRect(currentSize);
}
public float getY(TextSurface textSurface) {
return position.getY(textSurface, getHeight());
}
public float getX(TextSurface textSurface) {
return position.getX(textSurface, getWidth());
}
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
public RectF bounds() {
return defaultSize;
}
public void setPosition(Position position) {
this.position = position;
}
public void setScaleX(float value) {
scale.setValueX(value);
}
public void setScaleY(float value) {
scale.setValueY(value);
}
public void setScalePivot(float x, float y) {
scale.getPivot().set(x, y);
}
public float getScaleY() {
return scale.getScaleY();
}
public void setTranslationX(float value) {
position.setTranslationX(value);
}
public void setTranslationY(float value) {
position.setTranslationY(value);
}
@Override public int compareTo(@NonNull Text another) {
return text.compareTo(another.text);
}
public float getWidth() {
return (currentSize.width() + padding.left + padding.right);
}
public float getHeight() {
return (currentSize.height() + padding.top + padding.bottom);
}
public Position getPosition() {
return position;
}
public void onAnimationEnd() {
position.onAnimationEnd();
}
public float getScaleX() {
return scale.getScaleX();
}
@Override public String toString() {
return "Text{" +
"text='" + text + '\'' +
'}';
}
public void removeEffect(ITextEffect effect) {
effects.remove(effect);
}
public String getValue() {
return text;
}
public Paint getPaint() {
return paint;
}
}