/// <summary> /// Returns the character index where the text hits the margin, using either fixed-width or variable-width rules. /// </summary> /// <param name="messageText">The text to look inside</param> /// <param name="position"></param> /// <returns></returns> private int FindCharacterIndexOfRightMargin(string messageText, int position) { if (!this.UseVariableWidthFont) { int stringLength = messageText.Length; int x = 0; int targetX = position; int i = 0; while (x < targetX && i < stringLength) { char c = messageText[i]; if (c >= 0x80 && !(c >= 0xFF61 && c <= 0xFF9F)) { x += 2; } else { x++; } i++; } return(i); } else { int stringLength = messageText.Length; float targetX = position + 0.25f; float x = 0; int i = 0; while (x < targetX && i < stringLength) { x += characterWidthCache.GetRelativeWidth(messageText[i]); if (x < targetX) { i++; } } return(i); } }