fontdue icon indicating copy to clipboard operation
fontdue copied to clipboard

Best way to make a text fill a rectangle?

Open Kerollmops opened this issue 2 years ago • 3 comments

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.

Kerollmops avatar Aug 02 '21 15:08 Kerollmops

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.

tasogare3710 avatar Mar 02 '22 18:03 tasogare3710

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 avatar Mar 02 '22 22:03 mooman219

@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.

mooman219 avatar Mar 02 '22 23:03 mooman219