English 中文(简体)
Algorithm 选用2D表面的纸张
原标题:Algorithm to draw centered text on a 2D surface

我期待着稳定算法,以2D表面为基础,从横向和纵向上提取可能很长的文本。 这不是具体的措辞,因此,如果你能够以任何语文提供实例,那将非常有益。

关于我想要提的案文和表面的以下信息:

<>Text infomation:

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

<>Surface information:

  • width and height
  • x and y coordinates in the global coordinate system (assuming the origin is left top corner of the screen)

我确实采用了我自己的做法,但这种方式非常不稳定:案文散布在表面上,不正确,有时根本不集中。 还有一些好的做法,虽然 could不出一些好处。 我希望,停滞不前的社区能够帮助我。 非常感谢。

最佳回答

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

关键部分是第一步。 其余部分比较容易。

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.

有了变形的字体,事情就更加困难,因为你可以只将字面列入<条码>* 的性质——带宽/代码。 相反,你们不得不猜测、测试、精炼gues等。 每一图形子系统一号都使用某种<代码>MeasureString方法,这种方法将告诉我,鉴于某一个字体,它要做些什么样子。 有鉴于此,我过去所做的是:

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.

一旦找到了适合的子体,将你的开端指数推向下线的开端,并在你到达地势结束之前再次这样做。

Vertically centering the group of lines:

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

横向集中一条线很容易实现:

starting_position.X = (Surface_width - Measured_string_width) / 2

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

问题回答

因此,你有两条规则。

  • center every line of text
  • if a line s length > page width, start a new line
  • vertically center the entire text

因此,你表示对空间的投入。 你们将需要收集插图和现有的Index。 虽然线路(现为Index) + token.length < pageWidth,但可以添加到线路上。 一旦情况失败,你便会增加目前的Index,并继续这样做。

投入一旦完成,你将各行各业。 你们必须补偿餐厅每一行的宽度。 然后,你做x = 网页Width/2 - LineWidth/2。

然后,对你的案文封顶进行同样的计算,但对所有线进行核算(可能使用案文上的束缚性改动)。 然后,你可以获得同样的公式和标准,即Hal/2页,正文2。

这是个人的做法。 如果案文长或少有30个特性,在空间不超过15个特点之前的第一字,则将作这项工作。 然而,这种特征很容易改变。 案文将在2个层面阵列中保存,如果一条线不够大,将按2行予以保存。 在将案文集中放在2个层面之后,简单地加以表述。 我希望这将有助益。

char[][] t = new char[2][15];

public void putTextInMatrix(String text) {
    int len = text.length();
    int start;
    if(len<=15) {
        start = (15-len)/2;
        for (int i=0, j=0; i<15; i++)
            if(i<start || i>=len+start)
                t[0][i] =    ;
            else {
                t[0][i] = text.charAt(j);
                j++;
            }
    }
    else {
        int last = len;
        for (int i=0; i<15; i++) {
            if (text.charAt(i) ==    )
                last = i;
        }
        start = (15-last)/2;
        for (int i=0, j=0; i<15; i++)
            if(i<start || i>=last+start)
                t[0][i] =    ;
            else {
                t[0][i] = text.charAt(j);
                j++;
            }

        start = (15-(len-last))/2;
        for (int i=0, j=last+1; i<15; i++)
            if(i<start || j>=len)
                t[1][i] =    ;
            else {
                t[1][i] = text.charAt(j);
                j++;
            }
    }
}




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签