Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scaling and tracking for dynamic text #1635

Merged
merged 2 commits into from
Oct 2, 2020
Merged
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
33 changes: 19 additions & 14 deletions lottie/src/main/java/com/airbnb/lottie/model/layer/TextLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void getBounds(RectF outBounds, Matrix parentMatrix, boolean applyParents
void drawLayer(Canvas canvas, Matrix parentMatrix, int parentAlpha) {
canvas.save();
if (!lottieDrawable.useTextGlyphs()) {
canvas.setMatrix(parentMatrix);
canvas.concat(parentMatrix);
}
DocumentData documentData = textAnimation.getValue();
Font font = composition.getFonts().get(documentData.fontName);
Expand Down Expand Up @@ -260,13 +260,25 @@ private void drawTextWithFont(
// Line height
float lineHeight = documentData.lineHeight * Utils.dpScale();

// Calculate tracking
float tracking = documentData.tracking / 10f;
if (trackingCallbackAnimation != null) {
tracking += trackingCallbackAnimation.getValue();
} else if (trackingAnimation != null) {
tracking += trackingAnimation.getValue();
}
tracking = tracking * Utils.dpScale() * textSize / 100.0f;

// Split full text in multiple lines
List<String> textLines = getTextLines(text);
int textLineCount = textLines.size();
for (int l = 0; l < textLineCount; l++) {

String textLine = textLines.get(l);
float textLineWidth = strokePaint.measureText(textLine);
// We have to manually add the tracking between characters as the strokePaint ignores it
float textLineWidth = strokePaint.measureText(textLine) + (textLine.length() - 1) * tracking;

canvas.save();

// Apply horizontal justification
applyJustification(documentData.justification, canvas, textLineWidth);
Expand All @@ -277,10 +289,10 @@ private void drawTextWithFont(
canvas.translate(0, translateY);

// Draw each line
drawFontTextLine(textLine, documentData, canvas, parentScale);
drawFontTextLine(textLine, documentData, canvas, tracking);

// Reset canvas
canvas.setMatrix(parentMatrix);
canvas.restore();
}
}

Expand All @@ -292,20 +304,13 @@ private List<String> getTextLines(String text) {
return Arrays.asList(textLinesArray);
}

private void drawFontTextLine(String text, DocumentData documentData, Canvas canvas, float parentScale) {
private void drawFontTextLine(String text, DocumentData documentData, Canvas canvas, float tracking) {
for (int i = 0; i < text.length(); ) {
String charString = codePointToString(text, i);
i += charString.length();
drawCharacterFromFont(charString, documentData, canvas);
float charWidth = fillPaint.measureText(charString, 0, 1);
// Add tracking
float tracking = documentData.tracking / 10f;
if (trackingCallbackAnimation != null) {
tracking += trackingCallbackAnimation.getValue();
} else if (trackingAnimation != null) {
tracking += trackingAnimation.getValue();
}
float tx = charWidth + tracking * parentScale;
float charWidth = fillPaint.measureText(charString);
float tx = charWidth + tracking;
canvas.translate(tx, 0);
}
}
Expand Down