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

[text] Review LoadTTF() spritefont generation #375

Closed
raysan5 opened this issue Nov 2, 2017 · 5 comments
Closed

[text] Review LoadTTF() spritefont generation #375

raysan5 opened this issue Nov 2, 2017 · 5 comments
Assignees

Comments

@raysan5
Copy link
Owner

raysan5 commented Nov 2, 2017

Right now using a basic font baking mechanism, no proper texture-packing just aligning characters one after the other, no sub-pixel oversampling for better font quality.

This function can be improved...

@raysan5 raysan5 self-assigned this Nov 2, 2017
@raysan5 raysan5 mentioned this issue May 8, 2018
8 tasks
@raysan5
Copy link
Owner Author

raysan5 commented Jun 17, 2018

Just for record, I've been working on this issue (along with SDF font generation) for some time now.

Current LoadTTF() implementation uses stbtt_BakeFontBitmap() from stb_truetype library. That function packs characters in an image, one after another. After that, texture is loaded from image and font.chars info data is filled. LoadTTF() is not versatile at all, neither optimum in packing terms, I needed something better.

After some time working in an improved implementation for image font atlas generation, I came to different approximations:

  1. Generate Image font atlas but also fill CharInfo *chars; data in a single function (instead of just returning an opaque Font structure:
Image GenImageFont(const char *fileName, int fontSize, int *fontChars, CharInfo *chars, int charsCount);

This method give us an Image font atlas and the CharInfo data separately that later can be used to fill Font data. It works ok but Font loading is a bit cumbersome and counter-intuitive:

Font font = { 0 };
font.baseSize = 36;
font.charsCount = 96;
font.chars = (CharInfo *)malloc(font.charsCount*sizeof(CharInfo));
Image atlas = GenImageFont("fonts/Inconsolata.ttf", font.baseSize, NULL, font.chars, font.charsCount);
font.texture = LoadTextureFromImage(atlas);
  1. Generate CharInfo *chars data from TTF file, store every character pixel data in a first pass (requires CharInfo.data added to CharInfo struct) . After that, use that data to generate the image font atlas:
CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int charsCount, bool sdf);
Image GenImageFontAtlas(CharInfo *chars, int charsCount, int fontSize, int packMethod);

I think those methods are more intuitive and give us more control over the full system, including packing mechanism inside GenImageFontAtlas(). Font loading is a bit more intuitive:

Font font = { 0 };
font.baseSize = 36;
font.charsCount = 96;
font.chars = LoadFontData("fonts/Inconsolata.ttf", font.baseSize, NULL, font.charsCount, false);
Image atlas = GenImageFontAtlas(font.chars, font.charsCount, font.baseSize, 0);
font.texture = LoadTextureFromImage(atlas);

I keep thinking on this API improvement, really need a versatile and intuitive system for future related projects. Comments are welcome!

@raysan5
Copy link
Owner Author

raysan5 commented Jun 19, 2018

Improvements implemented in commit 75ba5ac

Still some things to review:

  • SDF generation chars padding --> Should be configurable? Other SDF generation params?
  • Image font packaging chars padding --> Should be passed as parameter?

@raysan5
Copy link
Owner Author

raysan5 commented Jun 20, 2018

Reviewed on commit 0e13511.

For now I consider this implementation complete... only SDF example left. Working on it.

@raysan5 raysan5 closed this as completed Jun 20, 2018
@iva0410217
Copy link

iva0410217 commented Jul 30, 2018

... (edited by @raysan5)

@raysan5
Copy link
Owner Author

raysan5 commented Jul 30, 2018

@iva0410217 why this issue is linked to 401?

EDIT: I'll assume it was an error. Editing your post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants