The text "Zoom" should be as large as possible within the limits / field

First problem: you have a width of 400 pixels, and you need to fine-tune the text as much as possible within this restriction (thus, the text should use this space).

Throw a new restriction: if the text is just “A”, then it should not increase the height above 100 pixels (or a certain font size).

Then, the final situation: Linebreaks. Apply some text as much as possible, for example. 400 x 150 pixels.

The obvious way is to simply start at point 1, and then increase until you can no longer fit it. This will work for all three problems, but it will be very rude. Fitting one line inside the borders can be done by writing it with some fixed-point size, checking the resulting pixel borders of the text, and then simply scaling it with a transform (the text also scales correctly, check the TransformUI ).

Any ideas on other ways to attack this would be greatly appreciated!

+4
source share
3 answers

As what you are modeling is difficult, especially with line breaks, then your initial suggestion to try all sizes goes in the right line, especially if it needs to be accurate.

However, instead of checking each value, you can use binary search to find the appropriate font size. You know that the size is somewhere between 1 and 100 (your upper range). Using binary search, each test sets the font size and checks the resulting layout. If the text is too large, then we are looking for the lower half of the current range of possible values. If the font size is suitable, then we are looking for the upper half. Your search will use no more than 7 attempts (100 journal databases 2 are rounded up), this will be exact, finding the largest size without moving, and it will be flexible if you need to add additional requirements later, for example, a combination of fonts or more stringent restrictions on the layout.

I assume that you are using a text component that performs line breaks, and that you can set the maximum width to 400. Thus, you set the font size and it makes a layout that allows you to return the desired height by laying out the text within the specified width.

You can use the hints to try to quickly bring the algorithm to the result, for example, to make the first assumption close to the expected size, but text rendering is quick, which increase the performance may not be worth the effort of implementation.

See Wikipedia - Binary Search Algorithm

+3
source

I would do the following:

Suppose you want W text to have wide text.

Choose an arbitrary size, say 10pt , and see which bounding box the text string for that size gets. Assume that it has a width N

Set the new size to 10pt * W/N and repeat the step from the first step until you get a reasonable threshold. (Hope it will work for one iteration.)

It depends on the fact that the line width is approximately proportional to the font size.

+3
source

I would create the Font with the largest size I want: say 72 for 1-inch glyphs with a resolution of 72 dpi. Use TextLayout to get borders and scale using AffineTransform (straight) or AffineTransformOp (offscreen), keeping aspect ratio. RenderingHints help comes up.

+3
source

Source: https://habr.com/ru/post/1309819/


All Articles