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

Feature request: extra margin between glyphs #33

Closed
guichazan opened this issue Jan 11, 2022 · 8 comments
Closed

Feature request: extra margin between glyphs #33

guichazan opened this issue Jan 11, 2022 · 8 comments

Comments

@guichazan
Copy link

guichazan commented Jan 11, 2022

Hi,

I'm using your wonderful tool to create my fonts, but since the margin between the glyphs is only 1, some glyphs at some font sizes are getting artifacts from the other glyphs. Can you please add a parameter to define an extra margin around the glyphs?

Kind regards

guich
@Chlumsky
Copy link
Owner

No. If you think you're getting artifacts because of no margin, that means you're actually mapping the glyphs wrong. If I added such an option everyone would use it without realizing their mistake, meaning they would still have glyphs positioned wrong and on top of that less space in the atlas.

@guichazan
Copy link
Author

Let me show you how it appears.

In the first image, you can see the artifacts. On the left there's the font size. See sizes 25, 32, 33 (on right of _), 39, 45/46 (on left of |)...

You can also see how nice are the fonts.

This is the shader i'm using:

vec4 msdf = texture2D(u_texture, v_texCoord);
float distance = median(msdf.r, msdf.g, msdf.b)  - 0.5;
distance *= dot(distanceRange / u_textureSize, 0.5 / fwidth(v_texCoord));
float glyphAlpha = clamp(distance + 0.5, 0.0, 1.0);
gl_FragColor = vec4(color.rgb, glyphAlpha * color.a);

If i use the parameters you defined in the Shadron sample, it becomes this:

vec4 msdf = texture2D(u_texture, v_texCoord);
float distance = median(msdf.r, msdf.g, msdf.b) + 0.5;
distance *= dot(vec2(0.015625), 0.5 / fwidth(v_texCoord));
float glyphAlpha = clamp(distance + 0.5, 0.0, 1.0);
gl_FragColor = vec4(color.rgb, glyphAlpha * color.a);

And then the result is in the second image: the artifacts are almost gone (still on size 47, 50, 54...). However, the fonts looks very ugly, specially on small sizes. Note: my app works on desktop, so it requires sizes <= 18.

Thanks

guich

mine

yours

@guichazan
Copy link
Author

guichazan commented Jan 12, 2022

This is how i'm creating the font:

msdf-atlas-gen.exe -font roboto.ttf -charset roboto_charset.txt -size 30 -type mtsdf -format png -potr -imageout roboto.png -csv roboto.csv -json roboto.json -scanline -yorigin top -pxrange 4 -coloringstrategy inktrap

roboto_charset.txt

Btw, i miss a lot, in the md file, the default values for each parameter, and in which situations a change is recommended.

@Chlumsky
Copy link
Owner

None of that is the problem. Your texture coordinates are wrong. How do you compute them?

@guichazan
Copy link
Author

guichazan commented Jan 17, 2022

Why you add a margin of 0.5 to all coords in GlyphGeometry::getQuadPlaneBounds?
Are you sure that you made a test of all sizes and chars like i did to confirm that your coordinates are correct?
The coordinates are computed like: xAtlasCoord / textureWidth, yAtlasCoord / textureHeight

@guichazan
Copy link
Author

guichazan commented Jan 17, 2022

Found my issue. I was removing the 0.5 margin as LibGDX's BitmapFont format don't support it. I added the margin back and it worked.
Many thanks.

            float incU = 0.5f / bf.getRegion().getRegionWidth(), incV = 0.5f / bf.getRegion().getRegionHeight();
            for (int i = 0; i <= 65535; i++)
            {
               BitmapFont.Glyph g = data.getGlyph((char)i);
               if (g != null)
               {
                  g.yoffset += glf.ascent;
                  g.u += incU; g.u2 -= incU;
                  g.v += incU; g.v2 -= incU;
               }
            }

@Chlumsky
Copy link
Owner

That's good to hear. Just one thing to check - depending on what exactly happened, in case all your texture coordinates actually got rounded down, you may want to add to u2, v2 and not subtract.

@guichazan
Copy link
Author

You were right: the correct is to add.
Long and prosper life!

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