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

Inject default TypeFace with support library downloadable fonts #427 #432

Open
wants to merge 3 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.graphics.Typeface;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
Expand Down Expand Up @@ -91,6 +92,10 @@ public static CalligraphyConfig get() {
* The default Font Path if nothing else is setup.
*/
private final String mFontPath;
/**
* Default typeface to setup if no font path is provided
*/
private final Typeface mTypeface;
/**
* Default Font Path Attr Id to lookup
*/
Expand Down Expand Up @@ -120,6 +125,7 @@ public static CalligraphyConfig get() {
protected CalligraphyConfig(Builder builder) {
mIsFontSet = builder.isFontSet;
mFontPath = builder.fontAssetPath;
mTypeface = builder.typeface;
mAttrId = builder.attrId;
mReflection = builder.reflection;
mCustomViewCreation = builder.customViewCreation;
Expand All @@ -137,6 +143,13 @@ public String getFontPath() {
return mFontPath;
}

/**
* @return typeface provided by the config, might be null
*/
public Typeface getTypeface() {
return mTypeface;
}

/**
* @return true if set, false if null|empty
*/
Expand Down Expand Up @@ -200,6 +213,10 @@ public static class Builder {
* The default fontPath
*/
private String fontAssetPath = null;
/**
* The default typeface
*/
private Typeface typeface = null;
/**
* Additional Class Styles. Can be empty.
*/
Expand All @@ -226,11 +243,23 @@ public Builder setFontAttrId(int fontAssetAttrId) {
* @return this builder.
*/
public Builder setDefaultFontPath(String defaultFontAssetPath) {
this.isFontSet = !TextUtils.isEmpty(defaultFontAssetPath);
this.isFontSet = isFontSet || !TextUtils.isEmpty(defaultFontAssetPath);
this.fontAssetPath = defaultFontAssetPath;
return this;
}

/**
* Set the default type if you don't define in font path or else where in your styles.
*
* @param typeface typeface to set, passing null will fallback to fontpath
* @return
*/
public Builder setDefaultTypeface(Typeface typeface) {
this.isFontSet = isFontSet || typeface != null;
this.typeface = typeface;
return this;
}

/**
* <p>Turn of the use of Reflection to inject the private factory.
* This has operational consequences! Please read and understand before disabling.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private Typeface getDefaultTypeface(Context context, String fontPath) {
if (!TextUtils.isEmpty(fontPath)) {
return TypefaceUtils.load(context.getAssets(), fontPath);
}
return null;
return CalligraphyConfig.get().getTypeface();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ static void applyFontToTextView(final Context context, final TextView textView,
static void applyFontToTextView(final Context context, final TextView textView, final CalligraphyConfig config, boolean deferred) {
if (context == null || textView == null || config == null) return;
if (!config.isFontSet()) return;
applyFontToTextView(context, textView, config.getFontPath(), deferred);
if (!TextUtils.isEmpty(config.getFontPath())) {
applyFontToTextView(context, textView, config.getFontPath(), deferred);
} else {
applyFontToTextView(textView, config.getTypeface(), deferred);
}
}

/**
Expand Down