/// <summary> /// formats a string into the specified rectangle. /// -1 height means no limit /// </summary> public StringLayoutInfo layoutRectangle(int w, int h, string str) { str = str.Replace("\r\n", "\n"); StringLayoutInfo ret = new StringLayoutInfo(); if(h == -1) h = int.MaxValue; //special case if(str.Length == 0) return ret; int backtrack = 0; int xo = 0, yo = 0; int cursor = -1; int lineheight = _scale*10; //includes line spacing ret.parts.Add(""); for(;;) { cursor++; //if this character completes the string, or a chunk, then check whether it fits and then output it if(cursor == str.Length || str[cursor] == ' ') { string chunk = str.Substring(backtrack, cursor-backtrack); int chunksize = MeasureWidth(chunk); if(xo+chunksize>w) { //ohno too wide yo += lineheight; xo = 0; ret.parts.Add(""); } //have we run out of vertical room? theres not room for anything else if(yo+10*_scale>h) break; //ok we can render it //render(b, x+xo, y+yo, chunk); ret.parts[ret.parts.Count-1] += chunk + " "; //increase width of this line and push out the bounds xo += chunksize; ret.size.Width = Math.Max(ret.size.Width, xo); //if we're continuing this line, we'll want to leave room for a space. xo += MeasureWidth(" "); //if we finished things off, then bail if(cursor == str.Length) { ret.complete = true; break; } //advance string cursor cursor++; backtrack = cursor; } else if(str[cursor] == '\n') { backtrack = cursor; yo += lineheight; xo = 0; ret.parts.Add(""); //have we run out of vertical room? theres not room for anything else if(yo+lineheight>h) break; } } ret.size.Height = yo + lineheight; return ret; }
public void renderLayout(Blitter b, int x, int y, StringLayoutInfo sli) { int lineheight = _scale*10; //includes line spacing foreach(string s in sli.parts) { Render(b, x, y, s); y += lineheight; } }