mooman219 / fontdue

The fastest font renderer in the world, written in pure rust.
Apache License 2.0
1.44k stars 71 forks source link

Best way to make a text fill a rectangle? #81

Open Kerollmops opened 3 years ago

Kerollmops commented 3 years ago

Hey,

I was trying your library, it seems to be very complete, thank you. I am trying to find the biggest scale of a text to make it fill a rectangle.

I found the Layout struct that outputs the list of glyphs positions, problem is that appending text to this layout requires a px parameter.

What I har in mind was that I could use the Font and my text to know the max ascent and max descent of the glyphs of my text, also using those glyphs to know the width it takes. Then, by using a simple calculus I could compute the scale my text needs to be to fit in height and width in my rectangle.

What do you think? I am not even sure I can rely on a simple multiplication to compute the ideal scale.

tasogare3710 commented 2 years ago

Hi,

To use unscaled metrics, You can pass Font::units_per_em() to the px parameter, but I couldn't find a way to get side bearing.

In my opinion, we cannot get the other values we need. For instance, the outline rectangle(a.k.a bounding box)*1 is (xMin,xMax,yMin,yMax) in the glyf table.


1) (advanceWidth, ascender - descender + lineGap) is the outside of bounding box.

mooman219 commented 2 years ago

In my opinion, we cannot get the other values we need. For instance, the outline rectangle(a.k.a bounding box)*1 is (xMin,xMax,yMin,yMax) in the glyf table.

@tasogare3710 This exists and is well documented https://docs.rs/fontdue/latest/fontdue/struct.Metrics.html

mooman219 commented 2 years ago

@Kerollmops What you want to do is achievable in the API, but you would need to provide your own layout. I don't have any intentions currently to add variably sized layouts, but it's definitely neat. Scale factoring is also well documented.