Calligraphy icon indicating copy to clipboard operation
Calligraphy copied to clipboard

Support for fonts downloaded at runtime

Open ollie72 opened this issue 9 years ago • 24 comments

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.

ollie72 avatar Nov 25 '14 10:11 ollie72

@ollie72 acknowledged.

chrisjenx avatar Nov 27 '14 11:11 chrisjenx

so is it can be used a download fonts?

Wolke avatar Mar 18 '15 10:03 Wolke

@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 avatar Mar 18 '15 10:03 chrisjenx

@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?

chrisjenx avatar Mar 18 '15 10:03 chrisjenx

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.

ollie72 avatar Mar 20 '15 11:03 ollie72

@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")));
}

chrisjenx avatar Mar 20 '15 12:03 chrisjenx

I think that will work well.

ollie72 avatar Mar 20 '15 12:03 ollie72

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

On Fri, 20 Mar 2015 at 12:51 Ollie Cornes [email protected] 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 .

chrisjenx avatar Mar 20 '15 12:03 chrisjenx

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.

ollie72 avatar Mar 20 '15 12:03 ollie72

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

On Fri, 20 Mar 2015 at 12:59 Ollie Cornes [email protected] 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 .

chrisjenx avatar Mar 20 '15 13:03 chrisjenx

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

AndroidDeveloperLB avatar Jun 09 '15 08:06 AndroidDeveloperLB

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

chrisjenx avatar Jun 09 '15 21:06 chrisjenx

@chrisjenx Do you know perhaps of an alternative ?

AndroidDeveloperLB avatar Jun 09 '15 21:06 AndroidDeveloperLB

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 [email protected] 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 .

chrisjenx avatar Jun 09 '15 21:06 chrisjenx

@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)

AndroidDeveloperLB avatar Jun 09 '15 21:06 AndroidDeveloperLB

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

On Tue, 9 Jun 2015 22:35 AndroidDeveloperLB [email protected] 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 .

chrisjenx avatar Jun 09 '15 21:06 chrisjenx

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

AndroidDeveloperLB avatar Jun 09 '15 21:06 AndroidDeveloperLB

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 ?

colriot avatar Feb 04 '16 14:02 colriot

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 [email protected] 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 .

chrisjenx avatar Feb 04 '16 15:02 chrisjenx

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

andrewindayang avatar Mar 24 '16 00:03 andrewindayang

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

LiuLei030504 avatar Jan 10 '17 05:01 LiuLei030504

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, [email protected] 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 .

chrisjenx avatar Jan 10 '17 05:01 chrisjenx

@chrisjenx What's the status on this?

pontusvigstrand avatar Mar 28 '18 13:03 pontusvigstrand

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 [email protected] 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 .

chrisjenx avatar Mar 28 '18 14:03 chrisjenx