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

Adds A Config Option To Provide a FontPath Substitution Map #399

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
@@ -1,5 +1,6 @@
package uk.co.chrisjenx.calligraphy;

import android.content.res.AssetManager;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
Expand Down Expand Up @@ -71,6 +72,7 @@ private static void addAppCompatViews() {
*/
public static void initDefault(CalligraphyConfig calligraphyConfig) {
sInstance = calligraphyConfig;
TypefaceUtils.setFontPathSubstitutionMap(sInstance.getFontPathSubstitutionMap());
}

/**
Expand Down Expand Up @@ -116,6 +118,11 @@ public static CalligraphyConfig get() {
* @see uk.co.chrisjenx.calligraphy.CalligraphyConfig.Builder#addCustomViewWithSetTypeface(Class)
*/
private final Set<Class<?>> hasTypefaceViews;
/**
* A map of font paths that should be used to dynamically substitute a new fontPath for the
* fontPath that's specified in XML / passed to {@link TypefaceUtils#load(AssetManager, String)}
*/
private Map<String, String> mFontPathSubstitutionMap = null;

protected CalligraphyConfig(Builder builder) {
mIsFontSet = builder.isFontSet;
Expand All @@ -128,6 +135,7 @@ protected CalligraphyConfig(Builder builder) {
tempMap.putAll(builder.mStyleClassMap);
mClassStyleAttributeMap = Collections.unmodifiableMap(tempMap);
hasTypefaceViews = Collections.unmodifiableSet(builder.mHasTypefaceClasses);
mFontPathSubstitutionMap = builder.fontPathSubstitutionMap;
}

/**
Expand Down Expand Up @@ -171,6 +179,10 @@ public int getAttrId() {
return mAttrId;
}

public Map<String, String> getFontPathSubstitutionMap() {
return mFontPathSubstitutionMap;
}

public static class Builder {
/**
* Default AttrID if not set.
Expand Down Expand Up @@ -207,6 +219,8 @@ public static class Builder {

private Set<Class<?>> mHasTypefaceClasses = new HashSet<>();

private Map<String, String> fontPathSubstitutionMap = null;

/**
* This defaults to R.attr.fontPath. So only override if you want to use your own attrId.
*
Expand Down Expand Up @@ -312,6 +326,11 @@ public Builder addCustomViewWithSetTypeface(Class<?> clazz) {
return this;
}

public Builder setFontPathSubstitutionMap(Map<String, String> fontPathSubstitutionMap) {
this.fontPathSubstitutionMap = fontPathSubstitutionMap;
return this;
}

public CalligraphyConfig build() {
this.isFontSet = !TextUtils.isEmpty(fontAssetPath);
return new CalligraphyConfig(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.HashMap;
Expand All @@ -20,6 +21,7 @@ public final class TypefaceUtils {

private static final Map<String, Typeface> sCachedFonts = new HashMap<String, Typeface>();
private static final Map<Typeface, CalligraphyTypefaceSpan> sCachedSpans = new HashMap<Typeface, CalligraphyTypefaceSpan>();
private static Map<String, String> sFontPathSubstitutionMap = null;

/**
* A helper loading a custom font.
Expand All @@ -28,11 +30,24 @@ public final class TypefaceUtils {
* @param filePath The path of the file.
* @return Return {@link android.graphics.Typeface} or null if the path is invalid.
*/
public static Typeface load(final AssetManager assetManager, final String filePath) {
public static Typeface load(final AssetManager assetManager, String filePath) {
synchronized (sCachedFonts) {
try {
// If a valid fontPath substitution map exists, check if the filePath should be
// updated in order to load a different font.
boolean loadFromFile = false;
if (sFontPathSubstitutionMap != null
&& sFontPathSubstitutionMap.containsKey(filePath)) {
filePath = sFontPathSubstitutionMap.get(filePath);
loadFromFile = true;
}
if (!sCachedFonts.containsKey(filePath)) {
final Typeface typeface = Typeface.createFromAsset(assetManager, filePath);
final Typeface typeface;
if (loadFromFile) {
typeface = Typeface.createFromFile(filePath);
} else {
typeface = Typeface.createFromAsset(assetManager, filePath);
}
sCachedFonts.put(filePath, typeface);
return typeface;
}
Expand Down Expand Up @@ -75,4 +90,8 @@ public static boolean isLoaded(Typeface typeface) {

private TypefaceUtils() {
}

static void setFontPathSubstitutionMap(@Nullable Map<String, String> fontPathSubstitutionMap) {
sFontPathSubstitutionMap = fontPathSubstitutionMap;
}
}