Skip to content

Commit

Permalink
Gradient fill and glyphs work
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Peal committed Jan 2, 2018
1 parent 7b09cc0 commit 32752a2
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 103 deletions.
21 changes: 12 additions & 9 deletions lottie/src/main/java/com/airbnb/lottie/LottieComposition.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,17 +390,20 @@ private static void parseFonts(

reader.beginObject();
while (reader.hasNext()) {
if ("list".equals(reader.nextName())) {
reader.beginArray();
while (reader.hasNext()) {
Font font = Font.Factory.newInstance(reader);
composition.fonts.put(font.getName(), font);
}
reader.endArray();
} else {
reader.skipValue();
switch (reader.nextName()) {
case "list":
reader.beginArray();
while (reader.hasNext()) {
Font font = Font.Factory.newInstance(reader);
composition.fonts.put(font.getName(), font);
}
reader.endArray();
break;
default:
reader.skipValue();
}
}
reader.endObject();
}

private static void parseChars(
Expand Down
91 changes: 60 additions & 31 deletions lottie/src/main/java/com/airbnb/lottie/model/DocumentData.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.airbnb.lottie.model;

import android.graphics.Color;
import android.support.annotation.ColorInt;
import android.util.JsonReader;

import org.json.JSONArray;
import org.json.JSONObject;
import com.airbnb.lottie.utils.JsonUtils;

import java.io.IOException;

public class DocumentData {

public String text;
@SuppressWarnings("WeakerAccess") public String fontName;
public int size;
public double size;
@SuppressWarnings("WeakerAccess") int justification;
public int tracking;
@SuppressWarnings("WeakerAccess") double lineHeight;
Expand All @@ -21,7 +22,7 @@ public class DocumentData {
public boolean strokeOverFill;


DocumentData(String text, String fontName, int size, int justification, int tracking,
DocumentData(String text, String fontName, double size, int justification, int tracking,
double lineHeight, double baselineShift, @ColorInt int color, @ColorInt int strokeColor,
int strokeWidth, boolean strokeOverFill) {
this.text = text;
Expand All @@ -43,35 +44,63 @@ public static final class Factory {
private Factory() {
}

public static DocumentData newInstance(JSONObject json) {
String text = json.optString("t");
String fontName = json.optString("f");
int size = json.optInt("s");
int justification = json.optInt("j");
int tracking = json.optInt("tr");
double lineHeight = json.optDouble("lh");
double baselineShift = json.optDouble("ls");
JSONArray colorArray = json.optJSONArray("fc");
int color = Color.argb(
255,
(int) (colorArray.optDouble(0) * 255),
(int) (colorArray.optDouble(1) * 255),
(int) (colorArray.optDouble(2) * 255));
JSONArray strokeArray = json.optJSONArray("sc");
public static DocumentData newInstance(JsonReader reader) throws IOException {
String text = null;
String fontName = null;
double size = 0;
int justification = 0;
int tracking = 0;
double lineHeight = 0;
double baselineShift = 0;
int fillColor = 0;
int strokeColor = 0;
if (strokeArray != null) {
strokeColor = Color.argb(
255,
(int) (strokeArray.optDouble(0) * 255),
(int) (strokeArray.optDouble(1) * 255),
(int) (strokeArray.optDouble(2) * 255));
}
int strokeWidth = 0;
boolean strokeOverFill = true;

int strokeWidth = json.optInt("sw");
boolean strokeOverFill = json.optBoolean("of");
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "t":
text = reader.nextString();
break;
case "f":
fontName = reader.nextString();
break;
case "s":
size = reader.nextDouble();
break;
case "j":
justification = reader.nextInt();
break;
case "tr":
tracking = reader.nextInt();
break;
case "lh":
lineHeight = reader.nextDouble();
break;
case "ls":
baselineShift = reader.nextDouble();
break;
case "fc":
fillColor = JsonUtils.jsonToColor(reader);
break;
case "sc":
strokeColor = JsonUtils.jsonToColor(reader);
break;
case "sw":
strokeWidth = reader.nextInt();
break;
case "of":
strokeOverFill = reader.nextBoolean();
break;
default:
reader.skipValue();
}
}
reader.endObject();

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

Expand All @@ -80,7 +109,7 @@ public static DocumentData newInstance(JSONObject json) {
long temp;
result = text.hashCode();
result = 31 * result + fontName.hashCode();
result = 31 * result + size;
result = (int) (31 * result + size);
result = 31 * result + justification;
result = 31 * result + tracking;
temp = Double.doubleToLongBits(lineHeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.airbnb.lottie.model.content.ShapeGroup;

import java.io.IOException;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;

public class FontCharacter {
Expand Down Expand Up @@ -61,7 +61,7 @@ public static FontCharacter newInstance(
double width = 0;
String style = null;
String fontFamily = null;
List<ShapeGroup> shapes = Collections.emptyList();
List<ShapeGroup> shapes = new ArrayList<>();

reader.beginObject();
while (reader.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public static AnimatableValue<PointF, PointF> createAnimatablePathOrSplitDimensi
}

AnimatablePathValue(JsonReader reader, LottieComposition composition) throws IOException {
// TODO (json): make sure this equals hasKeyframes
if (reader.peek() == JsonToken.BEGIN_ARRAY) {
reader.beginArray();
while (reader.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ private ValueFactory() {

@Override
public DocumentData valueFromObject(JsonReader reader, float scale) throws IOException {
// STOPSHIP (json)
return null;
// return DocumentData.Factory.newInstance((JSONObject) object);
return DocumentData.Factory.newInstance(reader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public static AnimatableTextProperties newInstance(

reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("a")) {
switch (reader.nextName()) {
case "a":
anim = parseAnimatableTextProperties(reader, composition);
break;
} else {
default:
reader.skipValue();
}
}
Expand Down
101 changes: 53 additions & 48 deletions lottie/src/main/java/com/airbnb/lottie/model/content/GradientFill.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,54 +90,59 @@ private Factory() {

static GradientFill newInstance(
JsonReader reader, LottieComposition composition) throws IOException {
// TODO (json)
return new GradientFill("TODO", GradientType.Linear, Path.FillType.EVEN_ODD, null,
null, null, null, null, null);
// final String name = json.optString("nm");
//
// JSONObject jsonColor = json.optJSONObject("g");
// if (jsonColor != null && jsonColor.has("k")) {
// // This is a hack because the "p" value which contains the number of color points is outside
// // of "k" which contains the useful data.
// int points = jsonColor.optInt("p");
// jsonColor = jsonColor.optJSONObject("k");
// try {
// jsonColor.put("p", points);
// } catch (JSONException e) {
// // Do nothing. This shouldn't fail.
// }
// }
// AnimatableGradientColorValue color = null;
// if (jsonColor != null) {
// color = AnimatableGradientColorValue.Factory.newInstance(jsonColor, composition);
// }
//
// JSONObject jsonOpacity = json.optJSONObject("o");
// AnimatableIntegerValue opacity = null;
// if (jsonOpacity != null) {
// opacity = AnimatableIntegerValue.Factory.newInstance(jsonOpacity, composition);
// }
//
// int fillTypeInt = json.optInt("r", 1);
// Path.FillType fillType = fillTypeInt == 1 ? Path.FillType.WINDING : Path.FillType.EVEN_ODD;
//
// int gradientTypeInt = json.optInt("t", 1);
// GradientType gradientType = gradientTypeInt == 1 ? GradientType.Linear : GradientType.Radial;
//
// JSONObject jsonStartPoint = json.optJSONObject("s");
// AnimatablePointValue startPoint = null;
// if (jsonStartPoint != null) {
// startPoint = AnimatablePointValue.Factory.newInstance(jsonStartPoint, composition);
// }
//
// JSONObject jsonEndPoint = json.optJSONObject("e");
// AnimatablePointValue endPoint = null;
// if (jsonEndPoint != null) {
// endPoint = AnimatablePointValue.Factory.newInstance(jsonEndPoint, composition);
// }
//
// return new GradientFill(name, gradientType, fillType, color, opacity, startPoint, endPoint,
// null, null);
String name = null;
AnimatableGradientColorValue color = null;
AnimatableIntegerValue opacity = null;
GradientType gradientType = null;
AnimatablePointValue startPoint = null;
AnimatablePointValue endPoint = null;
Path.FillType fillType = null;

while (reader.hasNext()) {
switch (reader.nextName()) {
case "nm":
name = reader.nextString();
break;
case "g":
int points = -1;
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "p":
points = reader.nextInt();
break;
case "k":
color = AnimatableGradientColorValue.Factory
.newInstance(reader, composition, points);
break;
default:
reader.skipValue();
}
}
reader.endObject();
break;
case "o":
opacity = AnimatableIntegerValue.Factory.newInstance(reader, composition);
break;
case "t":
gradientType = reader.nextInt() == 1 ? GradientType.Linear : GradientType.Radial;
break;
case "s":
startPoint = AnimatablePointValue.Factory.newInstance(reader, composition);
break;
case "e":
endPoint = AnimatablePointValue.Factory.newInstance(reader, composition);
break;
case "r":
fillType = reader.nextInt() == 1 ? Path.FillType.WINDING : Path.FillType.EVEN_ODD;
break;
default:
reader.skipValue();
}
}

return new GradientFill(name, gradientType, fillType, color, opacity, startPoint, endPoint,
null, null);
}
}
}
18 changes: 13 additions & 5 deletions lottie/src/main/java/com/airbnb/lottie/model/layer/Layer.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,16 @@ public static Layer newInstance(
break;
case "a":
reader.beginArray();
textProperties = AnimatableTextProperties.Factory.newInstance(reader, composition);
if (reader.hasNext()) {
textProperties = AnimatableTextProperties.Factory.newInstance(reader, composition);
}
while (reader.hasNext()) {
reader.skipValue();
}
reader.endArray();
break;
default:
reader.skipValue();
}
}
reader.endObject();
Expand All @@ -331,10 +336,13 @@ public static Layer newInstance(
while (reader.hasNext()) {
reader.beginObject();
while (reader.hasNext()) {
if (reader.nextName().equals("nm")) {
effectNames.add(reader.nextString());
} else {
reader.skipValue();
switch (reader.nextName()) {
case "nm":
effectNames.add(reader.nextString());
break;
default:
reader.skipValue();

}
}
reader.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private void drawTextWithFont(
text = textDelegate.getTextInternal(text);
}
fillPaint.setTypeface(typeface);
fillPaint.setTextSize(documentData.size * Utils.dpScale());
fillPaint.setTextSize((float) (documentData.size * Utils.dpScale()));
strokePaint.setTypeface(fillPaint.getTypeface());
strokePaint.setTextSize(fillPaint.getTextSize());
for (int i = 0; i < text.length(); i++) {
Expand Down
17 changes: 17 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/utils/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.airbnb.lottie.utils;

import android.graphics.Color;
import android.graphics.PointF;
import android.support.annotation.ColorInt;
import android.util.JsonReader;
import android.util.JsonToken;

Expand All @@ -13,6 +15,21 @@ public class JsonUtils {
private JsonUtils() {
}

/**
* [r,g,b]
*/
@ColorInt public static int jsonToColor(JsonReader reader) throws IOException {
reader.beginArray();
int r = (int) (reader.nextDouble() * 255);
int g = (int) (reader.nextDouble() * 255);
int b = (int) (reader.nextDouble() * 255);
while (reader.hasNext()) {
reader.skipValue();
}
reader.endArray();
return Color.argb(255, r, g, b);
}

public static List<PointF> jsonToPoints(JsonReader reader, float scale) throws IOException {
List<PointF> points = new ArrayList<>();

Expand Down

0 comments on commit 32752a2

Please sign in to comment.