chrisjenx / Calligraphy

Custom fonts in Android the easy way...
Apache License 2.0
8.59k stars 1.1k forks source link

Support for fonts downloaded at runtime #96

Open ollie72 opened 9 years ago

ollie72 commented 9 years ago

There are scenarios (eg white label apps that are branded at runtime) where it's useful to apply a typeface across an app that is downloaded from the network dependent on some runtime factor. It would be useful to have the ability when configuring Calligraphy to specify a font file path to a location in the app's storage space, where the app can download the TTF.

chrisjenx commented 9 years ago

@ollie72 acknowledged.

Wolke commented 9 years ago

so is it can be used a download fonts?

chrisjenx commented 9 years ago

@Wolke I've been a bit lax and haven't got around to this. I'm not quite sure how this will work in practice. Trying to build an understandable API where it's clear to the developer the order of font application.

chrisjenx commented 9 years ago

@ollie72 are you able to weigh in how you would use this in practice? Would you show a loading screen until you move forward? Would you like an API to "replace" the default Calligraphy font etc.. I guess this would change to a setDefaultAssetFont("fonts/GlobalFont.ttf").setRuntimeFont("cache/fonts/RuntimeFont.ttf"); Where the runtime font would be used if it can find it (and open it). otherwise it falls back to the Asset font?

Thoughts?

ollie72 commented 9 years ago

Harder than it looked :) I think an API that allows the use of a font from a file on the file system is useful, as it allows devs all sorts of flexibility. But as you say if it is being loaded from the file system it would not be immediately available so the entry screen probably has to have a default font applied, the only question then is does the font update dynamically after it loaded (ugly), or is it only available in future TextViews. I think Calligraphy should probably stay away from doing any network calls as the dev is best to decide how to handle the latency. Def agree there should be a fallback.

chrisjenx commented 9 years ago

@ollie72 I have no intention of doing network calls etc. I think maybe enabling a FS location for fonts would be the least dense API. But I think it would need to be pragmatic. I'm just cautious of over complicating things. Applying it retrospectively. No. That would required dirty tree traversal post layout. The whole point of Calligraphy is that these things are "Styled" and not pragmatic. Would making a FS location definable from styles even be sensible?

I'm thinking this as an Activity level Override inside the ContextWrapper.wrap() call. Generally if you want to apply a new theme to an Activity you need to restart it anyway and defining it there makes most sense to me.

Something like:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase, new File(Files.CACHED_DIR+"/ClientFont.ttf")));
}
ollie72 commented 9 years ago

I think that will work well.

chrisjenx commented 9 years ago

Cool, I'll take a gander this weekend if I get around to it.

On Fri, 20 Mar 2015 at 12:51 Ollie Cornes notifications@github.com wrote:

I think that will work well.

— Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/96#issuecomment-84005717 .

ollie72 commented 9 years ago

Btw I've left the project I planned to use this feature on so my personal need has gone at least for now. If no-one else is keen for this feature, perhaps the issue can be closed. Appreciate your work on the lib.

chrisjenx commented 9 years ago

I think someone else requesting it ^^, but thanks for letting me know.

On Fri, 20 Mar 2015 at 12:59 Ollie Cornes notifications@github.com wrote:

Btw I've left the project I planned to use this feature on so my personal need has gone at least for now. If no-one else is keen for this feature, perhaps the issue can be closed. Appreciate your work on the lib.

— Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/96#issuecomment-84006635 .

AndroidDeveloperLB commented 9 years ago

So, this library does support now loading fonts at runtime, from a file that's not in the app itself ?

chrisjenx commented 9 years ago

@AndroidDeveloperLB no not yet. It was discussed, but no implementation.

AndroidDeveloperLB commented 9 years ago

@chrisjenx Do you know perhaps of an alternative ?

chrisjenx commented 9 years ago

Well you can still call setTypeface() on any TextView as part of the Android API. I would never implement a downloader anyway it would just be a way to set CalligraphyConfig#setDefaultFont() from a downloaded file.

On Tue, 9 Jun 2015 at 22:21 AndroidDeveloperLB notifications@github.com wrote:

@chrisjenx https://github.com/chrisjenx Do you know perhaps of an alternative ?

— Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/96#issuecomment-110507510 .

AndroidDeveloperLB commented 9 years ago

@chrisjenx I meant, about fonts that are of a downloaded file. I didn't ask for you to implement a downloader. Assume the file is already downloaded. EDIT: oh you mean this: http://developer.android.com/reference/android/graphics/Typeface.html#createFromFile(java.lang.String)

chrisjenx commented 9 years ago

Not yet. Thus this being an open ticket. :) I'm assuming you require this?

On Tue, 9 Jun 2015 22:35 AndroidDeveloperLB notifications@github.com wrote:

@chrisjenx https://github.com/chrisjenx I meant, about fonts that are of a downloaded file. I didn't ask for you to implement a downloader. Assume the file is already downloaded.

— Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/96#issuecomment-110510739 .

AndroidDeveloperLB commented 9 years ago

That's ok. I was sure that loading fonts is only possible from the assets folder. Maybe I'm confused with something else.

colriot commented 8 years ago

As soon as this issue is still open I'll post my suggestion.

Most of the time devs need fonts that are downloaded once and then used across the app or the screen. For screens we can use something alike to your option with attachBaseContext https://github.com/chrisjenx/Calligraphy/issues/96#issuecomment-84005000. And for apps it will be good to have a Provider of the font, that is set in setRuntimeFontProvider method or similar. While this provider returns null or TypefaceUtils loads invalid font we fallback to defaults and as soon as TypefaceUtils will load valid font we start using it in consecutive inflations.

WDYT @chrisjenx ?

chrisjenx commented 8 years ago

That's a good idea, I like the idea of a provider, that way you can implement your own way of retrieving fonts.

+1

On Thu, 4 Feb 2016, 14:46 Sergey Ryabov notifications@github.com wrote:

As soon as this issue is still open I'll post my suggestion.

Most of the time devs need fonts that are downloaded once and then used across the app or the screen. For screens we can use something alike to your option with attachBaseContext #96 (comment) https://github.com/chrisjenx/Calligraphy/issues/96#issuecomment-84005000. And for apps it will be good to have a Provider of the font, that is set in setRuntimeFontProvider method or similar. While this provider returns null or TypefaceUtils loads invalid font we fallback to defaults and as soon as TypefaceUtils will load valid font we start using it in consecutive inflations.

WDYT @chrisjenx https://github.com/chrisjenx ?

— Reply to this email directly or view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/96#issuecomment-179877684 .

andrewindayang commented 8 years ago

So, when would this solution will be implemented? @chrisjenx

LiuLei030504 commented 7 years ago

@chrisjenx Is it still not implemented, correct? Do you have plan to add this feature? Load font from file system instead of assets.

chrisjenx commented 7 years ago

There are some resources from two companies that will be spending more effort on this. Stay tuned.

On Mon, 9 Jan 2017, 21:07 Liu Lei, notifications@github.com wrote:

@chrisjenx https://github.com/chrisjenx Is it still not implemented, correct? Do you have plan to add this feature? Load font from file system instead of assets.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/96#issuecomment-271487832, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHRsetKr2bXDpw84vEo3v0T7S5HHCtyks5rQxIfgaJpZM4DATg6 .

pontusvigstrand commented 6 years ago

@chrisjenx What's the status on this?

chrisjenx commented 6 years ago

No more, as this is supported natively by the support libs. If we were to add this it would be added to ViewPump as that has inflation hooks.

On Wed, 28 Mar 2018 at 07:12 pontusvigstrand notifications@github.com wrote:

@chrisjenx https://github.com/chrisjenx What's the status on this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/chrisjenx/Calligraphy/issues/96#issuecomment-376881313, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHRsdEv9txhkdldnjjVQr77KeVLg4IIks5ti4wxgaJpZM4DATg6 .