-1

how to implement manually string splitting algorithm to fit it into the rectangle.(insert ... or \n where needed) i can calculate the length of the string in pixels.

i have standart std::string and rect struct containing left, top ,width ,height how to split string into words phrases that it size was not larger than rectangle of the given size

returns width of string in pixel

int GetStringWidth(std::string str)
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
Yevhen
  • 1,897
  • 2
  • 14
  • 25
  • 4
    Do you just assume everyone knows what you're talking about, and all programmers in the world are using the same compiler/operating system/gizmo toolbox as you? – John Dibling Nov 08 '10 at 15:55
  • Please specify your working OS and compiler – Ahmed Nov 08 '10 at 15:57
  • standart c++ compiler, os does not matter – Yevhen Nov 08 '10 at 16:02
  • Are talking about text layout algorithms? Maybe that would be a better title/description. – Pierre-Antoine LaFayette Nov 08 '10 at 16:20
  • Do you have to format the string such that the words butt exactly at the left and right edge of the rectangle, or is a ragged right edge ok? Can you split the string anywhere, or only between words? Do you have to fill up the whole height of the rectangle, or can you leave some empty? – Lars Nov 08 '10 at 16:22
  • I will be good if an algorithm will be similar to windows. Empty space can be left belove the text, string can be split only between words – Yevhen Nov 08 '10 at 16:26
  • I have to ask: is this homework? If yes, it would be helpful if you shared what you came up with so far. – Lars Nov 08 '10 at 16:37
  • @TGadfly: OS may not matter, but font library certainly does. – Ben Voigt Nov 08 '10 at 20:56
  • there is no font library everything is drawn manually – Yevhen Nov 08 '10 at 20:58
  • @TGadfly: then it depends on how the text is being drawn manually. – jalf Nov 11 '10 at 20:54

3 Answers3

3

Ok, assuming left justification only, a very basic approach would be this:

You introduce two indices lineStart/lineEnd which mark the start and end of a possible text line in your input string. You then loop over all words in the input string, and for each word you check if it could be added to the current line without exceeding the rectangle width. If yes, increase lineEnd accordingly. If not, add the current fragment [lineStart..lineEnd] to the result, then reset lineStart/lineEnd to the start of the current word (which will be the first word on the next line).

There are a couple of border cases to consider, including (but likely not limited to) the possibility that a single word may be wider than the rectangle's width; and the very last line probably needs explicit handling as well.

Lars
  • 1,377
  • 6
  • 6
1

Split the string (using the delimiter of your choice - I'm assuming you want it to be whitespace), then loop through the string tokens, printing each one until you can't fit it in the remaining space, then go to the next line and keep going.

See this post for how to split a string.

Community
  • 1
  • 1
MahlerFive
  • 5,159
  • 5
  • 30
  • 40
0

You haven't given any background information, so it's impossible to answer your question. From what you've given me, just calculate the width of the rectangle, and set the length of the string to be the width of the rectangle minus some extra space for inset.

Mike
  • 73
  • 5