4coder-community / 4cc

59 stars 22 forks source link

Fix rectangle packing not respecting the requested maximum dimensions #10

Open mrmixer opened 9 months ago

mrmixer commented 9 months ago

4ed_font_provider_freetype.cpp: Rewrote the ft__bad_rect_pack_next function. There were several issues with the previous function that didn't manifest because the default size for the font atlas is 1024 * 1024 and the default 4coder only use about 1/8 of it.

To see the issues, you can set the font atlas size to 128 * 128 by changing line 325 "ft__bad_rect_pack_init(&pack, V2i32(1024, 1024));" to "ft__bad_rect_pack_init(&pack, V2i32(128, 128));".

The first issue was that the max_dim.y parameter was not respected. The dimension produced would always grow on Y to accommodate for more characters. And so the whole texture array thing was never use.

A second issue was that when a character didn't fit on the x axis, we created a new line, but never check that the new line fitted in the current texture slice.

A third issue was that when we ended a line because a character didn't fit vertically, we grew the line with a line height equal to the height of the character that didn't fit.

A small note, somewhat related. There is no spacing between characters in the texture atlas, and 4coder use a linear sampler which could cause sampling results different than expected: if a character side as a pixel with 0, and next to it another character has a pixel with 255, we might get a sample with a value in between. I did a test were I added a 2 pixel border between characters, but the final render was exactly the same (I did a difference between to screenshot and there was nothing different). So it seems fine, but I'm not sure why.