Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ public class CustomTypefaceSpan extends ReplacementSpan {
private final float iconSizePx;
private final float iconSizeRatio;
private final int iconColor;
private final boolean rotate;
private final boolean spin;
private final long rotationStartTime;
private final int rotation;
private final boolean flip_vertical;
private final boolean flip_horizontal;

public CustomTypefaceSpan(Icon icon, Typeface type, float iconSizePx, float iconSizeRatio, int iconColor, boolean rotate) {
this.rotate = rotate;
public CustomTypefaceSpan(Icon icon, Typeface type, float iconSizePx, float iconSizeRatio, int iconColor, boolean spin, int rotation, boolean flip_vertical, boolean flip_horizontal) {
this.spin = spin;
this.icon = String.valueOf(icon.character());
this.type = type;
this.iconSizePx = iconSizePx;
this.iconSizeRatio = iconSizeRatio;
this.iconColor = iconColor;
this.rotation = rotation;
this.flip_vertical = flip_vertical;
this.flip_horizontal = flip_horizontal;
this.rotationStartTime = System.currentTimeMillis();
}

Expand All @@ -49,13 +55,19 @@ public int getSize(Paint paint, CharSequence text, int start, int end, Paint.Fon
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
applyCustomTypeFace(paint, type);
paint.getTextBounds(icon, 0, 1, TEXT_BOUNDS);
float centerX = x + TEXT_BOUNDS.width() / 2f;
float centerY = y - TEXT_BOUNDS.height() / 2f + TEXT_BOUNDS.height() * BASELINE_RATIO;
canvas.save();
if (rotate) {
float rotation = (System.currentTimeMillis() - rotationStartTime) / (float) ROTATION_DURATION * 360f;
float centerX = x + TEXT_BOUNDS.width() / 2f;
float centerY = y - TEXT_BOUNDS.height() / 2f + TEXT_BOUNDS.height() * BASELINE_RATIO;
canvas.rotate(rotation, centerX, centerY);
if (spin) {
float angle = (System.currentTimeMillis() - rotationStartTime) / (float) ROTATION_DURATION * 360f;
canvas.rotate(angle, centerX, centerY);
}
if(rotation > 0)
canvas.rotate(rotation, centerX, centerY);
if(flip_horizontal)
canvas.scale(-1, 1, centerX, centerY);
if (flip_vertical)
canvas.scale(-1, 1, centerX, centerY);

canvas.drawText(icon,
x - TEXT_BOUNDS.left,
Expand All @@ -64,14 +76,14 @@ public void draw(Canvas canvas, CharSequence text, int start, int end, float x,
}

public boolean isAnimated() {
return rotate;
return spin;
}

private void applyCustomTypeFace(Paint paint, Typeface tf) {
paint.setFakeBoldText(false);
paint.setTextSkewX(0f);
paint.setTypeface(tf);
if (rotate) paint.clearShadowLayer();
if (spin) paint.clearShadowLayer();
if (iconSizeRatio > 0) paint.setTextSize(paint.getTextSize() * iconSizeRatio);
else if (iconSizePx > 0) paint.setTextSize(iconSizePx);
if (iconColor < Integer.MAX_VALUE) paint.setColor(iconColor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ private static void recursivePrepareSpannableIndexes(
int iconColor = Integer.MAX_VALUE;
float iconSizeRatio = -1;
boolean spin = false;
int rotation = 0;
boolean flip_vertical = false;
boolean flip_horizontal = false;
for (int i = 1; i < strokes.length; i++) {
String stroke = strokes[i];

Expand Down Expand Up @@ -149,7 +152,25 @@ else if (stroke.matches("#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})")) {
iconColor = getColorFromResource(context, stroke.substring(7));
if (iconColor == Integer.MAX_VALUE)
throw new IllegalArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
} else {
}

// look for icon rotation
else if (stroke.matches("rotate-([0-9]+)")) {
try{
rotation = Integer.parseInt(stroke.substring(8));
}catch (NumberFormatException e){
throw new IllegalArgumentException("Bad number format " + stroke + " in \"" + fullText + "\"");
}
}

// look for icon mirroring
else if (stroke.equals("flip-vertical")) {
flip_vertical = true;
} else if (stroke.equals("flip-horizontal")) {
flip_horizontal = true;
}

else {
throw new IllegalArgumentException("Unknown expression " + stroke + " in \"" + fullText + "\"");
}

Expand All @@ -160,7 +181,8 @@ else if (stroke.matches("#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})")) {
text = text.replace(startIndex, endIndex, "" + icon.character());
text.setSpan(new CustomTypefaceSpan(icon,
iconFontDescriptor.getTypeface(context),
iconSizePx, iconSizeRatio, iconColor, spin),
iconSizePx, iconSizeRatio, iconColor, spin, rotation,
flip_vertical, flip_horizontal),
startIndex, startIndex + 1,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
recursivePrepareSpannableIndexes(context, fullText, text, iconFontDescriptors, startIndex);
Expand Down