/// <summary> /// append the text string \a text to the RenderedString \a rs. /// </summary> /// <param name="rs"></param> /// <param name="text"></param> private void AppendRenderedText(RenderedString rs, string text) { var cpos = 0; // split the given string into lines based upon the newline character while (text.Length > cpos) { // find next newline var nlpos = text.IndexOf("\n", cpos); // calculate length of this substring var len = ((nlpos != -1) ? nlpos : text.Length) - cpos; // construct new text component and append it. var rtc = new RenderedStringTextComponent(text.Substring(cpos, len), d_fontName); rtc.SetPadding(d_padding); rtc.SetColours(new ColourRect(d_colours)); rtc.SetVerticalFormatting(d_vertAlignment); rtc.SetAspectLock(d_aspectLock); rs.AppendComponent(rtc); // break line if needed if (nlpos != -1) { rs.AppendLineBreak(); } // advance current position. +1 to skip the \n char cpos += len + 1; } }
public override RenderedStringComponent Split(Window refWnd, float splitPoint, bool firstComponent) { var fnt = GetEffectiveFont(refWnd); // This is checked, but should never fail, since if we had no font our // extent would be 0 and we would never cause a split to be needed here. if (fnt == null) { throw new InvalidRequestException("unable to split with no font set."); } // create 'left' side of split and clone our basic configuration var lhs = new RenderedStringTextComponent(); lhs.d_padding = d_padding; lhs.d_verticalFormatting = d_verticalFormatting; lhs.d_font = d_font; lhs.d_colours = d_colours; // calculate the 'best' place to split the text var leftLen = 0; var leftExtent = 0.0f; while (leftLen < d_text.Length) { var tokenLen = GetNextTokenLength(d_text, leftLen); // exit loop if no more valid tokens. if (tokenLen == 0) { break; } var tokenExtent = fnt.GetTextExtent(d_text.CEGuiSubstring(leftLen, tokenLen)); // does the next token extend past the split point? if (leftExtent + tokenExtent > splitPoint) { // if it was the first token, split the token itself if (firstComponent && leftLen == 0) { leftLen = Math.Max(1, fnt.GetCharAtPixel(d_text.CEGuiSubstring(0, tokenLen), splitPoint)); } // left_len is now the character index at which to split the line break; } // add this token to the left side leftLen += tokenLen; leftExtent += tokenExtent; } // perform the split. lhs.d_text = d_text.CEGuiSubstring(0, leftLen); // here we're trimming leading delimiters from the substring range var rhsStart = d_text.IndexNotOf(TextUtils.DefaultWrapDelimiters, leftLen); if (rhsStart == -1) { rhsStart = leftLen; } // split the selection if (d_selectionLength != 0) { var selEnd = d_selectionStart + d_selectionLength - 1; lhs.d_selectionStart = d_selectionStart; lhs.d_selectionLength = selEnd < leftLen ? d_selectionLength : leftLen - d_selectionStart; if (selEnd >= leftLen) { d_selectionStart = 0; d_selectionLength -= rhsStart; } else { SetSelection(refWnd, 0, 0); } } d_text = d_text.Substring(rhsStart); return(lhs); }