Skip to content

Commit

Permalink
Added option to get font by name instead of font family
Browse files Browse the repository at this point in the history
  • Loading branch information
nihad92 committed Feb 12, 2020
1 parent 257d1a5 commit 6788ede
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ buildscript {
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlinVersion"
classpath 'org.ajoberstar:grgit:1.9.3'
classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.6"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
}

Expand Down
4 changes: 4 additions & 0 deletions lottie/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import net.ltgt.gradle.errorprone.CheckSeverity
apply plugin: 'com.github.dcendents.android-maven'

apply from: 'gradle-maven-push.gradle'
apply plugin: 'com.android.library'
apply plugin: 'net.ltgt.errorprone'

group='com.github.nihad92'


android {
compileSdkVersion 29
resourcePrefix 'lottie_'
Expand Down
4 changes: 4 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/FontAssetDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public Typeface fetchFont(String fontFamily) {
return null;
}

public Typeface getFontByName(String fontName) {
return null;
}

/**
* Override this if you want to specify the asset path for a given font family.
*/
Expand Down
5 changes: 3 additions & 2 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.airbnb.lottie.manager.FontAssetManager;
import com.airbnb.lottie.manager.ImageAssetManager;
import com.airbnb.lottie.model.Font;
import com.airbnb.lottie.model.KeyPath;
import com.airbnb.lottie.model.Marker;
import com.airbnb.lottie.model.layer.CompositionLayer;
Expand Down Expand Up @@ -1050,10 +1051,10 @@ private ImageAssetManager getImageAssetManager() {
}

@Nullable
public Typeface getTypeface(String fontFamily, String style) {
public Typeface getTypeface(Font font) {
FontAssetManager assetManager = getFontAssetManager();
if (assetManager != null) {
return assetManager.getTypeface(fontFamily, style);
return assetManager.getTypeface(font);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.airbnb.lottie.FontAssetDelegate;
import com.airbnb.lottie.L;
import com.airbnb.lottie.model.Font;
import com.airbnb.lottie.model.MutablePair;
import com.airbnb.lottie.utils.Logger;

Expand Down Expand Up @@ -52,42 +53,46 @@ public void setDelegate(@Nullable FontAssetDelegate assetDelegate) {
this.defaultFontFileExtension = defaultFontFileExtension;
}

public Typeface getTypeface(String fontFamily, String style) {
tempPair.set(fontFamily, style);
public Typeface getTypeface(Font font) {
tempPair.set(font.getFamily(), font.getStyle());
Typeface typeface = fontMap.get(tempPair);
if (typeface != null) {
return typeface;
}
Typeface typefaceWithDefaultStyle = getFontFamily(fontFamily);
typeface = typefaceForStyle(typefaceWithDefaultStyle, style);
Typeface typefaceWithDefaultStyle = getFontFamily(font);
typeface = typefaceForStyle(typefaceWithDefaultStyle, font.getStyle());
fontMap.put(tempPair, typeface);
return typeface;
}

private Typeface getFontFamily(String fontFamily) {
Typeface defaultTypeface = fontFamilies.get(fontFamily);
private Typeface getFontFamily(Font font) {
Typeface defaultTypeface = fontFamilies.get(font.getFamily());
if (defaultTypeface != null) {
return defaultTypeface;
}

Typeface typeface = null;
if (delegate != null) {
typeface = delegate.fetchFont(fontFamily);
typeface = delegate.getFontByName(font.getName());
}

if (delegate != null && typeface == null) {
String path = delegate.getFontPath(fontFamily);
typeface = delegate.fetchFont(font.getFamily());
}

if (delegate != null && typeface == null) {
String path = delegate.getFontPath(font.getFamily());
if (path != null) {
typeface = Typeface.createFromAsset(assetManager, path);
}
}

if (typeface == null) {
String path = "fonts/" + fontFamily + defaultFontFileExtension;
String path = "fonts/" + font.getFamily() + defaultFontFileExtension;
typeface = Typeface.createFromAsset(assetManager, path);
}

fontFamilies.put(fontFamily, typeface);
fontFamilies.put(font.getFamily(), typeface);
return typeface;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private void drawGlyphTextLine(String text, DocumentData documentData, Matrix pa
private void drawTextWithFont(
DocumentData documentData, Font font, Matrix parentMatrix, Canvas canvas) {
float parentScale = Utils.getScale(parentMatrix);
Typeface typeface = lottieDrawable.getTypeface(font.getFamily(), font.getStyle());
Typeface typeface = lottieDrawable.getTypeface(font);
if (typeface == null) {
return;
}
Expand Down

0 comments on commit 6788ede

Please sign in to comment.