算法绘制居中文本上的二维表面算法、文本、表面

2023-09-07 14:09:31 作者:时间说了真话

我要寻找稳定的算法得出可能的水平和垂直方向集中在二维表面长文本。这是不特定的语言,因此,如果您可以提供的例子在任何语言这将是非常有益的。

I am looking for stable algorithm to draw possibly long text horizontally and vertically centered on a 2D surface. This is not language specific, so if you can provide examples in any language it would be very helpful.

下面的信息我有我想要绘制文本和面:

The following information I have about the text I want to draw and the surface:

文字资料:

在像素(线高度)的文字尺寸,当然比表面的高度方式较小 脸类型(可以是任何虽然) 在简单的字符从ASCII32设置为ASCII126 在文本可以比较长,单词之间用空格分隔 在文本应打破自动的空间,一旦它不会在表面的宽度适合了 文字为英文。 size of the text in px (line height), is of course way smaller than the height of the surface the type face (could be any though) simple character set from ASCII32 to ASCII126 text could be relatively long, words are separated by spaces text should break "automatically" on spaces once it will not fit in the width of the surface anymore text is in English.

面信息:

在宽度和高度 x和y的全球坐标系中的坐标(假设原点留在屏幕的右上角)

我没有实现我自己的方法,但它是非常不稳定:文本•$ P $垫在表面上,排列不正确,有时不居中的。另外一派的一些好方法,虽然不能找到一些有用的东西。我希望社会各界计算器可以帮助我。非常感谢你。

I did implement my own approach, but it was very unstable: text spread over the surface, alignment not correct, sometimes not centered at all. Also googled for some good approaches though couldn't find something useful. I hope the stackoverflow community can help me. Thank you very much.

推荐答案

该方法的概念很简单,有些参与执行。它仅涉及几个步骤:

The approach is simple in concept, a bit involved in execution. It involves only a few steps:

Split the text into lines that will fit horizontally
Compute the vertical position of the first line
for each line
    Compute its width and the X position
    Display it at the Y position
    Add the line height to the current Y position

最困难的部分是第一步。其余的是容易的。

The hard part is the first step. The rest is easy.

如果你有一个固定宽度的字体,然后分裂文成线是不是太难。您只需计算出有多少个字符将适合在给定的宽度,字符串索引到该位置,然后再返回到previous字休息。抓住从previous行的开始的子(或启动字符串第一行)到那个位置,这就是你的线。重复,直到你到达字符串的结尾。

If you have a fixed-width font, then splitting the text into lines isn't too hard. You simply compute how many characters will fit in the given width, index into the string to that position, and then back up to the previous word break. Grab the substring from the start of the previous line (or start of the string for the first line) to that position, and that's your line. Repeat until you get to the end of the string.

通过可变宽度字体,事情有点困难,因为你不能只是字符串索引由 N * character_width 字符。相反,你必须猜测,试验,改进的猜测,等,每个图形子系统,我用了某种 MeasureString 方法,将告诉我的工作有多少像素是将采取呈现一个字符串,给定特定的字体。鉴于这种情况,我已经在过去所做的是:

With a variable-width font, things are a bit harder because you can't just index into the string by n * character_width characters. Instead, you have to guess, test, refine the guess, etc. Every graphics subsystem I've worked with had some kind of MeasureString method that would tell me how many pixels it would take to render a string, given a particular font. Given that, what I've done in the past is:

Divide the surface width by the font's average character width
Index that far into the string, and find the next (or previous) word break.
Measure the string
If the result is wider than the width, go back one word and measure again.
If the result is narrower than the width, go forward one word and measure again.
Repeat the measure/adjust until you find the string that will fit.

一旦发现,将适合,前进的起始索引到下一行的开始,做一遍,直到你到达字符串结尾的字符串。

Once you've found the substring that will fit, move your start index forward to the start of the next line, and do it again until you reach the end of the string.

垂直居中的组线:

starting_position.Y = (Surface_height - (num_lines * line_height)) / 2

水平居中行是很容易实现:

Horizontally centering a line is easily accomplished:

starting_position.X = (Surface_width - Measured_string_width) / 2

您必须计算 starting_position.X 的每一行。 Y坐标为每个连续的行增加 line_height

You have to compute the starting_position.X for each line. The Y coordinate is increased by line_height for each successive line.