Chlumsky / msdfgen

Multi-channel signed distance field generator
MIT License
3.91k stars 404 forks source link

Question: Proper scaling and positioning with freetype #108

Closed vaheqelyan closed 3 years ago

vaheqelyan commented 4 years ago

i know this is not the right place to ask this question, but I need help. this was the command I ended up using.

./msdfgen msdf -font Lato-Regular.ttf '72' -size 96 96 -pxrange 12 -autoframe -o ../basic-gl/H.png

i used FreeType 2 to provide the kerning, size, etc.

Here is my result (Lato Font, 40px)

image

I highlighted the characters

image

Can you tell me how I can scale bitmaps or get the right metrics for rendering ? I mean, the character "H" should be a little wider How can I maintain bitmap data along with generated textures

Here is a comparison of text in a web browser image

Here's what my fragment shader looks like

#version 430 core
out vec4 color;

in vec2 uv;
in float layer_get;

layout (binding=0) uniform sampler2DArray textureArray;

float median(float r, float g, float b) {
    return max(min(r, g), min(max(r, g), b));
}

void main()
{
      vec3 flipped_texCoords = vec3(uv, layer_get);
      vec2 pos = flipped_texCoords.xy;
      vec3 dis = texture(textureArray, flipped_texCoords).rgb;
      ivec2 sz = textureSize(textureArray, 0).xy;
      float dx = dFdx(pos.x) * sz.x; 
      float dy = dFdy(pos.y) * sz.y;
      float sigDist = median(dis.r, dis.g, dis.b);
      float w = fwidth(sigDist);
      float opacity = smoothstep(0.5 - w, 0.5 + w, sigDist);
      color = mix(vec4(1.0,0.0,1.0,1.0), vec4(0.0,0.0,0.0,1.0), opacity);
}

This is the only progress I have made so far. ./msdfgen msdf -font Lato-Regular.ttf '72' -size 96 96 -pxrange 12 -autoframe -scale 5 -o ../basic-gl/H.png image

btw thank you for your software !

Chlumsky commented 4 years ago

First of all, don't use -autoframe. I even added a disclaimer on the front page but everyone still uses it and then wonders how to typeset with each letter scaled differently and centered (losing any sort of positioning). Then you gotta set the translation and scale that you compute with some sophisticated logic.

I don't know what exactly you're trying to do but I think you might want to take a look at https://github.com/Chlumsky/msdf-atlas-gen and see if it maybe solves the exact same problem you're working on.

vaheqelyan commented 4 years ago

thank you for your quick response

First of all, don't use -autoframe

yeah i know, i used this command earlier

./msdfgen msdf -font Lato-Regular.ttf '72' -size 96 96 -pxrange 12 -scale 2 -translate 3 20 -o ../basic-gl/H.png

but i practically had the same problem as now

I don't know what exactly you're trying to do but I think you might want to take a look at https://github.com/Chlumsky/msdf-atlas-gen and see if it maybe solves the exact same problem you're working on.

i store each image in a texture array, i don't use texture atlas. can this cause a problem ?

Chlumsky commented 4 years ago

You can use a texture array, that's not a problem but you seem to have a hard time getting the positioning right, so I was thinking that you may want to use something that already has this solved.

vaheqelyan commented 4 years ago

ok i will try msdf-atlas-gen. I'll let you know. thank's.