PDA

View Full Version : Modern, opensource automaticly-growing runtime font packer for games



JernejL
17-05-2018, 08:13 AM
https://i.imgur.com/woxXjnV.png

I've taken a different approach to font rendering, this is an example implementation.

In essence, it uses multiple texture sprite atlases, and depending on which characters you need, it will render them on the fly at runtime.
In a proper game you would then update your texture in opengl via texsubimage2d.
When creating a font atlas you pick your font (which can be a truetype font directly from file (it uses AddFontResourceEx) ), font size and textures size - based on max texture size that gpu supports.
Then you pre-generate glyphs for say.. ansi character range, and if at later time your game needs chinise font characters it will add them to texture - texture atlas will automaticly grow and will allocate amout of additional textures that are needed based on texture atlas resolution.

This in essence gives you really great control over your characters, you can easily add custom graphical hacks / characters into your fonts (like arrows, sprites, smileys).
It is a really great for unicode support too - if your characters are not yet generated, it'll add new ones on the fly, if a player wants to write his name in chinise, korean or arabic it should all work properly.

You can use one font atlas instance + one font size and then scale it in your gui app, or just use multiple instances of TFontAtlas for different font sizes and styles.

Demo & implementaton requires vampyre imaging library - probably the best pascal image library in existing, you should be using this anyways ;)

Currently fonts are rendered via winapi using GetGlyphOutlineW - i hope to change this to lazfreetype or something else that is more pure pascal for portability - i'll take any help i can get, if anybody wants to do this.

Benefits of this are.. you are not limited to few offline pre-generated fonts, you have access to all truetype fonts and all unicode characters with excellent rendering quality.
Downsides: I can't think of any, prove me wrong :)

Source code + test app implementation
http://partyserver.us/temp/FontExperiment.rar

The example uses partitions.pas which i cannot figure out who the real author or licence was, i guess original author was Nitrogen and / or Paul Nicholls?

I've documented some places where the files could have came from but none has a license and no clear original author is mentioned, i'm not sure who nitrogen is or where to reach him to clarify this:

// source: http://fpc4gp2x.eonclash.com/downloads/Partitions.pas
// An updated version of the TPartition class that Nitrogen wrote for his Font Studio?
// http://www.nitrogen.za.org/projectinfo.asp?id=12
// http://www.pascalgamedevelopment.com/showthread.php?4585-Image-packer-project
// http://web.archive.org/web/20101023143957/http://www.nitrogen.za.org/projectinfo.asp?id=12
// http://borland.newsgroups.archived.at/public.delphi.graphics/200806/0806267270.html

I'll put this on github later, my code (FontAtlas and demo) is under MIT license.

Ñuño Martínez
17-05-2018, 11:13 AM
This is interesting.

So, does it exports the generated atlas in to one or more bitmaps? And does it tell the UNICODE point range (or ranges) generated? I'm looking for a tool that allows to build bitmapped fonts to use with Allegro. Right now I'm using ASCII-127 only because I had to build them by myself with The GIMP, but I want to use UTF-8 enconding and it is quite annoying to use The GIMP for that (also I don't know what UNICODE points I should use ::)).

[edit] You can see the format I'm looking for in this link (http://allegro-pas.sourceforge.net/docs/5.2/al5font.html#al_grab_font_from_bitmap).

JernejL
17-05-2018, 12:46 PM
It generates bitmaps, and when you need additional characters it adds them to existing bitmaps, and if out of space it adds a new bitmap, i guess you could use it with that.

character bindings are unicode, the example uses widechar winapi for rendering - we'd need to convert this somehow to freetype2.. freetype1 in lazarus repo is buggy as hell and can't use font hinting.

if you use lazarus utf8string then you can do this to get unicode numbers:

http://wiki.freepascal.org/UTF8_strings_and_characters#Accessing_bytes_inside _one_UTF8_codepoint

SilverWarior
17-05-2018, 03:17 PM
Based on the information presented in the thread bellow the original author of partitions.pas is Nitrogen. but the unit was latter updated by Paul_Nicholls

https://www.pascalgamedevelopment.com/showthread.php?4585-Image-packer-project&highlight=partitions.pas

JernejL
17-05-2018, 03:48 PM
Based on the information presented in the thread bellow the original author of partitions.pas is Nitrogen. but the unit was latter updated by Paul_Nicholls

https://www.pascalgamedevelopment.com/showthread.php?4585-Image-packer-project&highlight=partitions.pas

That's same conclusion i had, i'm not sure who nitrogen is irl and not sure on the license, but i think it's safe to say it's in public domain.

SilverWarior
17-05-2018, 08:06 PM
i'm not sure who nitrogen is irl and not sure on the license, but i think it's safe to say it's in public domain.

I'm also not sure who Nitrogen is. But as far as licence goes I guess Paul will know best since he was the one who updated the code last. So it is best to ask him about that.

NOUR
13-02-2019, 06:42 PM
I'm also not sure who Nitrogen is. But as far as licence goes I guess Paul will know best since he was the one who updated the code last Pnr Status (https://pnrstatus.vip/) TextNow (https://textnow.vip/) VPN (https://downloader.vip/vpn/). So it is best to ask him about that.

To avoid it all,
You can check the Font Studio by mikepote on bitbucket.org, its OPen Source (so Free).

JernejL
14-02-2019, 06:58 AM
To avoid it all,
You can check the Font Studio by mikepote on bitbucket.org, its OPen Source (so Free).

It uses what looks like same unit (packing.pas), but it seems like it's an older or a modified version of it - https://bitbucket.org/mikepote/font-studio/src/e5e05d686a0dcb1ee1633a9823c7a976722bfc76/Partitions.pas?at=default&fileviewer=file-view-default

packing insert routine seems identical, except that it uses constants rather than enums, but the order of checks and all seems all identical, so i'd say this is same unit, or at least the files are related.