Chlumsky / msdf-atlas-gen

MSDF font atlas generator
MIT License
783 stars 175 forks source link

Pixel-perfect positioning of hardmask & softmask atlas #65

Closed Chlumsky closed 7 months ago

Chlumsky commented 1 year ago

In hardmask and softmask atlas -types, there are no special considerations to ensure that the glyph quads can be placed exactly onto the pixel grid. Instead, the plane bounds dictate that each glyph can be shifted by a different fractional pixel amount. This should be resolved.

pha3z commented 1 year ago

I'm curious to know why the plane bound construct exists.

I looked at this thread: https://github.com/Chlumsky/msdf-atlas-gen/discussions/47

I notice your suggested solution is to simply add planeBound (*size) to the atlasBound for each glyph to get quad placement. If I understand this math correctly, doesn't this mean calculation could be done for every single glyph once to reduce it to a single atlasBound rectangle??

The fact that there are two separate structures makes me wonder if there is a case where they would need to be handled independently at runtime. ???

Chlumsky commented 1 year ago

Where did I suggest to add plane bounds to atlas bounds? Plane bounds = world coordinates, atlas bounds = texture coordinates.

pha3z commented 1 year ago

I'm an idiot. I totally misread the code from the other thread, which colored my question poorly.

Ok now I see. The planeBounds is used to offset the quad in worldspace. I was previously only thinking about locating atlas position of the glyph.

Thank you!

Chlumsky commented 1 year ago

A fix for this issue is now available in the branch pixel-alignment. Before merging, I need to add the choice between the old and new method so that users can choose between more consistent pixel alignment and slightly more compact atlases. I'd like to also add a way to only lock alignment for just one axis as it makes much more sense to pixel-align the baseline since the glyph advances (and therefore all horizontal positions) will almost always be fractional.

Chlumsky commented 7 months ago

Merged in ae441c998920bb56b8b40346f2b287dd4497e334, the default setting is to pixel-align baseline only (vertical alignment) for all modes.