Chlumsky/msdf-atlas-gen

Feature request: extra margin between glyphs

Closed this issue · 8 comments

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

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.

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

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.

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

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

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;
               }
            }

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.

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