简体   繁体   English

“缩放”文本在约束/框内尽可能大

[英]“Zoom” text to be as big as possible within constraints/box

First problem: You have 400 pixels width to go on, and need to fit some text within that constraint as large as possible (thus, the text shall use that amount of space). 第一个问题:你有400像素的宽度,并且需要在该约束内尽可能多地拟合一些文本(因此,文本应该使用该空间量)。

Throw in a new constraint: If the text is just "A", then it shall not zoom this above 100 pixels height (or some specific font size). 抛出一个新约束:如果文本只是“A”,那么它不应该超过100像素高度(或某些特定字体大小)。

Then, a final situation: Linebreaks. 然后,最后的情况:Linebreaks。 Fit some text in the largest possible way within eg 400 x 150 pixels. 以最大可能的方式拟合一些文本,例如400 x 150像素。

An obvious way is to simply start with point 1, and then increase until you can't fit it anymore. 一个显而易见的方法是简单地从第1点开始,然后增加直到你不再适合它。 This would work for all three problems, but would be very crude. 这对所有三个问题都有效,但是非常粗糙。 The fitting of a single line within bounds could be done by writing it with some fixed point size, check the resulting pixel bounds of the text, and then simply scale it with a transform (the text scales properly too then, check out TransformUI ). 边界内单条线的拟合可以通过用一些固定点大小写入来完成,检查文本的结果像素范围,然后简单地用变换进行缩放(文本也适当缩放,检查TransformUI )。

Any ideas of other ways to attack this would be greatly appreciated! 任何其他攻击方式的想法将不胜感激!

I would do the following: 我会做以下事情:

Assume you want W pixels wide text. 假设您想要W像素宽文本。

Pick an arbitrary size, say 10pt , and see what bounding box the text-string gets for that size. 选择一个任意大小,比如10pt ,然后查看文本字符串为该大小获取的边界框。 Lets say it gets N pixels wide. 可以说它的宽度为N像素。

Set the new size to 10pt * W/N , and repeat from step one, until you get within a reasonable threshold. 将新大小设置为10pt * W/N ,并从第1步开始重复,直到达到合理的阈值。 (Hopefully it would work within one iteration.) (希望它可以在一次迭代中工作。)

This relies on the fact that the width of the string, is roughly proportional to the size of the font. 这取决于字符串的宽度大致与字体大小成比例的事实。

As what you are modelling is complex, especially with line breaks, then your initial proposal of trying all sizes is along the right lines, especially if it needs to be accurate. 由于你的建模是复杂的,尤其是换行符,所以你尝试所有尺寸的初步建议都是正确的,特别是如果它需要准确的话。

However, rather than testing each value, you can use a binary search to find the appropriate font size. 但是,您可以使用二进制搜索来查找合适的字体大小,而不是测试每个值。 You know the size is somewhere between 1 and 100 (your upper range). 你知道大小在1到100之间(你的上限)。 using a binary search, each test sets the font size and checks the resulting layout. 使用二进制搜索,每个测试设置字体大小并检查生成的布局。 If the text is too large, then we search the lower half of the current range of possible values. 如果文本太大,那么我们搜索当前可能值范围的下半部分。 If the font size fits, then we search the upper half. 如果字体大小适合,那么我们搜索上半部分。 Your search will use at most 7 attempts (100 log base 2 rounded up), it will be exact, finding the largest size without going over, and it will be flexible if you need to add more requirements later, such as a mix of fonts or more stringent constraints on the layout. 您的搜索将最多使用7次尝试(100个基数2向上舍入),它将是精确的,找到最大的大小而不会过去,如果您需要稍后添加更多要求,例如混合字体,它将是灵活的或者更严格的布局限制。

I'm assuming you are using a text component that does line wrapping, and that you can set the maximum width to 400. So, you set the font size and it does the layout giving you back the required height, laying out text within the given width. 我假设你正在使用一个文本组件进行换行,并且你可以将最大宽度设置为400.所以,你设置字体大小,它确实布局给你回到所需的高度,在给定宽度。

You can use hints to try to guide the algorithm to the result quicker, such as making your first guess close to the expected size, but text rendering is fast, that the performance increase may not be worth the implementation effort. 您可以使用提示来尝试更快地引导算法到结果,例如使您的第一次猜测接近预期大小,但文本呈现速度很快,性能提升可能不值得实施。

See Wikipedia - Binary Search Algorithm 请参阅Wikipedia - 二进制搜索算法

I'd instantiate the Font at the largest desired size: say 72 for one inch glyphs at 72 dpi. 我将Font设置为所需的最大尺寸:对于72 dpi的一英寸字形,请说72。 Use TextLayout to get the bounds and scale using AffineTransform (direct) or AffineTransformOp (offscreen), while preserving the aspect ratio. 使用TextLayout使用AffineTransform (直接)或AffineTransformOp (屏幕外)获取边界和比例,同时保留纵横比。 Suitable RenderingHints help, too. 合适的RenderingHints帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM