Skip to content

Commit

Permalink
Support box position in Document Data (#2139)
Browse files Browse the repository at this point in the history
Fixes #2131
  • Loading branch information
gpeal authored Nov 11, 2022
1 parent 8093687 commit 592c5f6
Show file tree
Hide file tree
Showing 7 changed files with 3,702 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.airbnb.lottie.animation.keyframe;

import androidx.annotation.Nullable;

import com.airbnb.lottie.model.DocumentData;
import com.airbnb.lottie.value.Keyframe;
import com.airbnb.lottie.value.LottieFrameInfo;
Expand Down Expand Up @@ -39,7 +37,7 @@ public DocumentData getValue(LottieFrameInfo<DocumentData> frameInfo) {
DocumentData baseDocumentData = frameInfo.getInterpolatedKeyframeProgress() == 1f ? frameInfo.getEndValue() : frameInfo.getStartValue();
documentData.set(text, baseDocumentData.fontName, baseDocumentData.size, baseDocumentData.justification, baseDocumentData.tracking,
baseDocumentData.lineHeight, baseDocumentData.baselineShift, baseDocumentData.color, baseDocumentData.strokeColor,
baseDocumentData.strokeWidth, baseDocumentData.strokeOverFill);
baseDocumentData.strokeWidth, baseDocumentData.strokeOverFill, baseDocumentData.boxPosition);
return documentData;
}
});
Expand Down
11 changes: 8 additions & 3 deletions lottie/src/main/java/com/airbnb/lottie/model/DocumentData.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import static androidx.annotation.RestrictTo.Scope.LIBRARY;

import android.graphics.PointF;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;

@RestrictTo(LIBRARY)
Expand All @@ -25,20 +28,21 @@ public enum Justification {
@ColorInt public int strokeColor;
public float strokeWidth;
public boolean strokeOverFill;
@Nullable public PointF boxPosition;


public DocumentData(String text, String fontName, float size, Justification justification, int tracking,
float lineHeight, float baselineShift, @ColorInt int color, @ColorInt int strokeColor,
float strokeWidth, boolean strokeOverFill) {
set(text, fontName, size, justification, tracking, lineHeight, baselineShift, color, strokeColor, strokeWidth, strokeOverFill);
float strokeWidth, boolean strokeOverFill, PointF boxPosition) {
set(text, fontName, size, justification, tracking, lineHeight, baselineShift, color, strokeColor, strokeWidth, strokeOverFill, boxPosition);
}

public DocumentData() {
}

public void set(String text, String fontName, float size, Justification justification, int tracking,
float lineHeight, float baselineShift, @ColorInt int color, @ColorInt int strokeColor,
float strokeWidth, boolean strokeOverFill) {
float strokeWidth, boolean strokeOverFill, PointF positionOffset) {
this.text = text;
this.fontName = fontName;
this.size = size;
Expand All @@ -50,6 +54,7 @@ public void set(String text, String fontName, float size, Justification justific
this.strokeColor = strokeColor;
this.strokeWidth = strokeWidth;
this.strokeOverFill = strokeOverFill;
this.boxPosition = positionOffset;
}

@Override public int hashCode() {
Expand Down
24 changes: 16 additions & 8 deletions lottie/src/main/java/com/airbnb/lottie/model/layer/TextLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.airbnb.lottie.animation.keyframe.TextKeyframeAnimation;
import com.airbnb.lottie.animation.keyframe.ValueCallbackKeyframeAnimation;
import com.airbnb.lottie.model.DocumentData;
import com.airbnb.lottie.model.DocumentData.Justification;
import com.airbnb.lottie.model.Font;
import com.airbnb.lottie.model.FontCharacter;
import com.airbnb.lottie.model.animatable.AnimatableTextProperties;
Expand Down Expand Up @@ -193,12 +192,15 @@ private void drawTextGlyphs(
canvas.save();

// Apply horizontal justification
applyJustification(documentData.justification, canvas, textLineWidth);
applyJustification(documentData, canvas, textLineWidth);

// Center text vertically
float multilineTranslateY = (textLineCount - 1) * lineHeight / 2;
float translateY = l * lineHeight - multilineTranslateY;
canvas.translate(0, translateY);
if (documentData.boxPosition != null) {
canvas.translate(documentData.boxPosition.x, documentData.boxPosition.y);
}

// Draw each line
drawGlyphTextLine(textLine, documentData, parentMatrix, font, canvas, parentScale, fontScale);
Expand Down Expand Up @@ -277,7 +279,7 @@ private void drawTextWithFont(DocumentData documentData, Font font, Canvas canva
canvas.save();

// Apply horizontal justification
applyJustification(documentData.justification, canvas, textLineWidth);
applyJustification(documentData, canvas, textLineWidth);

// Center text vertically
float multilineTranslateY = (textLineCount - 1) * lineHeight / 2;
Expand Down Expand Up @@ -341,16 +343,22 @@ private float getTextLineWidthForGlyphs(
return textLineWidth;
}

private void applyJustification(Justification justification, Canvas canvas, float textLineWidth) {
switch (justification) {
private void applyJustification(DocumentData documentData, Canvas canvas, float textLineWidth) {
float lineStart = 0f;
float lineTop = 0f;
if (documentData.boxPosition != null) {
lineStart = documentData.boxPosition.x;
lineTop = documentData.boxPosition.y;
}
switch (documentData.justification) {
case LEFT_ALIGN:
// Do nothing. Default is left aligned.
canvas.translate(-lineStart, -lineTop);
break;
case RIGHT_ALIGN:
canvas.translate(-textLineWidth, 0);
canvas.translate(-lineStart - textLineWidth, -lineTop);
break;
case CENTER:
canvas.translate(-textLineWidth / 2, 0);
canvas.translate(-lineStart - textLineWidth / 2, -lineTop);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static AnimatableShapeValue parseShapeData(

static AnimatableTextFrame parseDocumentData(
JsonReader reader, LottieComposition composition) throws IOException {
return new AnimatableTextFrame(parse(reader, composition, DocumentDataParser.INSTANCE));
return new AnimatableTextFrame(parse(reader, Utils.dpScale(), composition, DocumentDataParser.INSTANCE));
}

static AnimatableColorValue parseColor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.airbnb.lottie.parser;


import android.graphics.PointF;

import com.airbnb.lottie.model.DocumentData;
import com.airbnb.lottie.model.DocumentData.Justification;
import com.airbnb.lottie.parser.moshi.JsonReader;
Expand All @@ -20,7 +22,8 @@ public class DocumentDataParser implements ValueParser<DocumentData> {
"fc", // 7
"sc", // 8
"sw", // 9
"of" // 10
"of", // 10
"ps" // 11
);

private DocumentDataParser() {
Expand All @@ -39,6 +42,7 @@ public DocumentData parse(JsonReader reader, float scale) throws IOException {
int strokeColor = 0;
float strokeWidth = 0f;
boolean strokeOverFill = true;
PointF boxPosition = null;

reader.beginObject();
while (reader.hasNext()) {
Expand Down Expand Up @@ -81,6 +85,11 @@ public DocumentData parse(JsonReader reader, float scale) throws IOException {
case 10:
strokeOverFill = reader.nextBoolean();
break;
case 11:
reader.beginArray();
boxPosition = new PointF((float) reader.nextDouble() * scale, (float) reader.nextDouble() * scale);
reader.endArray();
break;
default:
reader.skipName();
reader.skipValue();
Expand All @@ -89,6 +98,6 @@ public DocumentData parse(JsonReader reader, float scale) throws IOException {
reader.endObject();

return new DocumentData(text, fontName, size, justification, tracking, lineHeight,
baselineShift, fillColor, strokeColor, strokeWidth, strokeOverFill);
baselineShift, fillColor, strokeColor, strokeWidth, strokeOverFill, boxPosition);
}
}
Loading

0 comments on commit 592c5f6

Please sign in to comment.