Page 1 of 2

Font fallback support

Posted: Mon Jan 07, 2013 03:56
by Montred
A feature that would be very useful (i'd say essential) to have is the concept of fallback fonts. Android for example has a fallback font that has all the unicode range that is used whenever a certain font doesn't contain a certain glyph.

That way you can use a nice decorative font of your choosing for the ascii range and for the CJK charactesr the fallback would be used. This is very practical and there's no way to do this in CEGUI currently (that I know of, unless the default font has this behavior). I could also contribute this if CE agrees and tells me how to approach it or how it would look.

The simplest way would be to just have a global fallback font. One way would be to add a method like this:

Code: Select all

CEGUI::System::getSingleton().setFallbackFont("UnicodeFont");


Another way would be to change the behavior of the current default font to act as a fallback:

Code: Select all

CEGUI::System::getSingleton().setDefaultFont("UnicodeFont");


I'd prefer the first method though, cause it's more specific. The problem with a global fallback font is that it would only have one size, and maybe you want to have several.

So perhaps it could be part of a font definition, any font could point to other font to use as fallback:

<Font Name="sans-10" Filename="sans.ttf" Fallback="MyUnicodeFont-10" ...


With MyUnicodeFont-10 being defined in another file.

EDIT: I think a mix of the two ways would be the best way, the Fallback parameter on the font definition and apart from that a System::setDefaultFallbackFont() would be nice in case you forget the fallback parameter in a font.

Re: Font fallback support

Posted: Mon Jan 07, 2013 15:58
by CrazyEddie
I like this idea. I basically agree with the combined solution; a global fallback and a per-font configurable fallback.

Not sure about implementations yet, will consider it when I look at the font code to come up with a preferred approach to resolve the font issue in the other topic :)

I'll try and post something regarding this (and for the other topic) by/over next weekend (sorry that I'm slow dealing with such things :P)

Re: Font fallback support

Posted: Mon Jan 07, 2013 16:38
by Montred
CrazyEddie wrote:I'll try and post something regarding this (and for the other topic) by/over next weekend (sorry that I'm slow dealing with such things :P)


Lol, don't worry. You are actually the most responsive open source maintainer that I've come across, and I've been interacting with open source project for a decade.

Re: Font fallback support

Posted: Tue Jan 15, 2013 10:52
by CrazyEddie
This should fit in with the mod I mentioned in the other thread. I'll update this again once I've committed or posted the patch for that mod.

CE.

Re: Font fallback support

Posted: Tue Jan 15, 2013 13:14
by CrazyEddie
The basic way I see this working is to effectively modify the (new) function CEGUI::Font::findFontGlyph (and overrides where appropriate) so that in the case where a glyph is not found, we then look for it in the fallback font - which will be an assigned value of CEGUI::Font - and when that is 0, to look at a system default if set.

Ideally looking up the font will be done as late as possible - so we can refer to fonts that are yet to be loaded (but should not be looked up every time for performance reasons), and some protection is likely needed to prevent cycles - where two fonts fallback to each other (which should be allowed, but the potential infinite loop when both are missing a glyph needs to be handled elegantly).

Properties and events should also be provided where that is appropriate and the system default fallback should also be settable in the CEGUI config file. All mods should also include appropriate updates to the appropriate documentation files and XML schema files.

Review the following, adhere to them and take notice of what is said above - I make no bones about rejecting stuff without a second look if I think people "can't be bothered" or show a lack of respect for the code base:
http://www.cegui.org.uk/api_reference/c ... dards.html
http://www.cegui.org.uk/api_reference/devel.html

The only thing that is slightly out of date is the mention of patches, we're happy to get pull requests on bitbucket now instead (see: http://www.youtube.com/watch?v=ZjFOvLGnAiU and make allowances for the fact we no longer host the repo at sourceforge.net).

CE.

Re: Font fallback support

Posted: Tue Jan 15, 2013 14:17
by Montred
Cool! Thanks for the analysis. Will get to it in a few weeks most likely.

Re: Font fallback support

Posted: Tue Jan 15, 2013 15:14
by Montred
By the way, any preference on how to handle the infinite recursion?

Because it sounds like you want to allow cascading fallbacks.

For example you could have a decorative ANSI font, another slightly less ugly font with arabic and cirillic, and then another super ugly font that also has CJK. So maybe you'd like to have

ANSI.ttf's fallback = arabic-cirillic.ttf
arabic-cirillic.ttf's fallback = unicode.ttf

So, shall we have a recursion limit, of, say, 10? Is a "int &recursionCounter" parameter to the findFontGlyph() function acceptable?

And then if the limit is reached, I could throw:

Code: Select all

 CEGUI_THROW(GenericException("Font::findFontGlyph - Recursion limit reached while retrieving a glyph from a fallback font."));


Should the recursion limit be configurable? Or do you prefer something more sophisticated where I keep a recursion stack and check that no same font is called twice. I'd prefer to keep it simple with a counter, though, less memory allocations.

Re: Font fallback support

Posted: Wed Jan 16, 2013 12:52
by Montred
Well, that was easier than I thought. This is Neuton, with a CJK font as fallback, in the same StaticText:

Image

I had to put the lookup in getGlyphData() as opposed to findFontGlyph(). This ensures that the pages are rasterized as needed and also allows us to do the lookup in the base font class, so it allows you to mix different font types (though I have only tried freetype.)

Now to add the anti-recursion measure (will go for a counter unless you say otherwise). What will take me the most will be figuring out mercurial and bitbucket.

Re: Font fallback support

Posted: Wed Jan 16, 2013 17:19
by CrazyEddie
Hey, cool that you basically have this working.

Umm with regards to the recursion issue, I think a counter for now, as it's the easiest to implement. If something else comes up, or this becomes inappropriate, then it can be tweaked later.

CE.

Re: Font fallback support

Posted: Fri Jan 18, 2013 18:39
by Montred
So I'm done with this and I realized the following. It's impossible to have an infinite cycle of fallbacks because the loading sequence will fail. The way I did it (I think the logical way, correct me if you disagree) you need to load the fallback font before loading a font that uses that font as fallback. Right now if you load a font that references a fallback which isn't loaded yet CEGUI will throw this exception as it usually does when something isn't found by a certain name.

18/01/2013 19:34:51 (Error) CEGUI::UnknownObjectException in file c:\cegui-last\cegui\include\CEGUINamedXMLResourceManager.h(279) : NamedXMLResourceManager::get: No object of type 'Font' named 'fallback' is present in the collection.


So I think I'm gonna remove the anti-recursion protection, because as far as I can see it can never be triggered except to get in the way (if somebody happens to have a ton of legitimate fallbacks). Agree?

Re: Font fallback support

Posted: Fri Jan 18, 2013 19:48
by Montred
Well, here's the contribution:

https://bitbucket.org/montred/cegui/com ... 308e22a9e0

The wiki said to contact you at this point. I did also read that you are now happy to accept pull requests, but for some reason Bitbucket doesn't let me do a pull request to your project (only to my own copy), though maybe I'm misunderstanding something, it's the first time I use bitbucket or hg.

EDIT: some visual fixes here too: https://bitbucket.org/montred/cegui/com ... 4f81b28540

Re: Font fallback support

Posted: Sun Jan 20, 2013 09:25
by CrazyEddie
Thanks so much for your efforts here. I have very briefly looked at the diffs and things seem reasonable at first glance. I will get around to looking more closely Soon™ :D

CE.

Re: Font fallback support

Posted: Thu Dec 18, 2014 09:50
by Montred
Hello. It seems nobody ever got around to processing my submission, so I'm bumping just a reminder :)

Re: Font fallback support

Posted: Thu Dec 18, 2014 11:22
by Kulik
Could you please submit this as a pull request? Please fix the indentation issues.

I have to think about which branch we want this added to. It's a new feature so I think it should probably go to default.

Re: Font fallback support

Posted: Thu Dec 18, 2014 11:35
by Montred
Hello.

This was my initial commit.

https://bitbucket.org/montred/cegui/com ... e09d168240

And here is another commit where I fixed the indentation issues. Both are ancients.

https://bitbucket.org/montred/cegui/com ... 40b4b5c998

Bitbucket doesn't let me create a pull request to the CEGUI project and I don't know why.