static int EndLine(IntPtr L) { LuaScriptMgr.CheckArgsCount(L, 1); System.Text.StringBuilder arg0 = (System.Text.StringBuilder)LuaScriptMgr.GetNetObject(L, 1, typeof(System.Text.StringBuilder)); NGUIText.EndLine(ref arg0); LuaScriptMgr.PushObject(L, arg0); return(1); }
static public int EndLine_s(IntPtr l) { try { System.Text.StringBuilder a1; checkType(l, 1, out a1); NGUIText.EndLine(ref a1); pushValue(l, true); pushValue(l, a1); return(2); } catch (Exception e) { return(error(l, e)); } }
static int EndLine(IntPtr L) { try { ToLua.CheckArgsCount(L, 1); System.Text.StringBuilder arg0 = (System.Text.StringBuilder)ToLua.CheckObject(L, 1, typeof(System.Text.StringBuilder)); NGUIText.EndLine(ref arg0); ToLua.PushObject(L, arg0); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
private static int EndLine(IntPtr L) { int result; try { ToLua.CheckArgsCount(L, 1); StringBuilder o = (StringBuilder)ToLua.CheckObject(L, 1, typeof(StringBuilder)); NGUIText.EndLine(ref o); ToLua.PushObject(L, o); result = 1; } catch (Exception e) { result = LuaDLL.toluaL_exception(L, e, null); } return(result); }
/// <summary> /// Text wrapping functionality. The 'width' and 'height' should be in pixels. /// </summary> public bool WrapText(string text, int size, out string finalText, int width, int height, int maxLines, bool encoding, SymbolStyle symbolStyle) { if (mReplacement != null) { return(mReplacement.WrapText(text, size, out finalText, width, height, maxLines, encoding, symbolStyle)); } #if DYNAMIC_FONT if (isDynamic) { return(NGUIText.WrapText(text, mDynamicFont, size, mDynamicFontStyle, width, height, maxLines, encoding, out finalText)); } #endif if (width < 1 || height < 1) { finalText = ""; return(false); } int maxLineCount = (maxLines > 0) ? maxLines : 999999; maxLineCount = Mathf.Min(maxLineCount, height / size); if (maxLineCount == 0) { finalText = ""; return(false); } StringBuilder sb = new StringBuilder(); int textLength = text.Length; int remainingWidth = width; int previousChar = 0; int start = 0; int offset = 0; bool lineIsEmpty = true; bool multiline = (maxLines != 1); int lineCount = 1; bool useSymbols = encoding && symbolStyle != SymbolStyle.None && hasSymbols; // Run through all characters for (; offset < textLength; ++offset) { char ch = text[offset]; // New line character -- start a new line if (ch == '\n') { if (!multiline || lineCount == maxLineCount) { break; } remainingWidth = width; // Add the previous word to the final string if (start < offset) { sb.Append(text.Substring(start, offset - start + 1)); } else { sb.Append(ch); } lineIsEmpty = true; ++lineCount; start = offset + 1; previousChar = 0; continue; } // If this marks the end of a word, add it to the final string. if (ch == ' ' && previousChar != ' ' && start < offset) { sb.Append(text.Substring(start, offset - start + 1)); lineIsEmpty = false; start = offset + 1; previousChar = ch; } // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them if (NGUIText.ParseSymbol(text, ref offset)) { --offset; continue; } // See if there is a symbol matching this text BMSymbol symbol = useSymbols ? MatchSymbol(text, offset, textLength) : null; // Calculate how wide this symbol or character is going to be int glyphWidth = mSpacingX; if (symbol != null) { glyphWidth += symbol.advance; } else { // Find the glyph for this character BMGlyph glyph = (symbol == null) ? mFont.GetGlyph(ch) : null; if (glyph != null) { glyphWidth += (previousChar != 0) ? glyph.advance + glyph.GetKerning(previousChar) : glyph.advance; } else { continue; } } // Remaining width after this glyph gets printed remainingWidth -= glyphWidth; // Doesn't fit? if (remainingWidth < 0) { // Can't start a new line if (lineIsEmpty || !multiline || lineCount == maxLineCount) { // This is the first word on the line -- add it up to the character that fits sb.Append(text.Substring(start, Mathf.Max(0, offset - start))); if (!multiline || lineCount == maxLineCount) { start = offset; break; } NGUIText.EndLine(ref sb); // Start a brand-new line lineIsEmpty = true; ++lineCount; if (ch == ' ') { start = offset + 1; remainingWidth = width; } else { start = offset; remainingWidth = width - glyphWidth; } previousChar = 0; } else { // Skip all spaces before the word while (start < textLength && text[start] == ' ') { ++start; } // Revert the position to the beginning of the word and reset the line lineIsEmpty = true; remainingWidth = width; offset = start - 1; previousChar = 0; if (!multiline || lineCount == maxLineCount) { break; } ++lineCount; NGUIText.EndLine(ref sb); continue; } } else { previousChar = ch; } // Advance the offset past the symbol if (symbol != null) { offset += symbol.length - 1; previousChar = 0; } } if (start < offset) { sb.Append(text.Substring(start, offset - start)); } finalText = sb.ToString(); return(!multiline || offset == textLength || (maxLines > 0 && lineCount <= maxLines)); }
static public bool WrapText(string text, out string finalText, bool keepCharCount, bool wrapLineColors, bool eastern) { if (NGUIText.regionWidth < 1 || NGUIText.regionHeight < 1 || NGUIText.finalLineHeight < 1f) { finalText = ""; return(false); } float height = (NGUIText.maxLines > 0) ? Mathf.Min(NGUIText.regionHeight, NGUIText.finalLineHeight * NGUIText.maxLines) : NGUIText.regionHeight; int maxLineCount = (NGUIText.maxLines > 0) ? NGUIText.maxLines : 1000000; maxLineCount = Mathf.FloorToInt(Mathf.Min(maxLineCount, height / NGUIText.finalLineHeight) + 0.01f); if (maxLineCount == 0) { finalText = ""; return(false); } if (string.IsNullOrEmpty(text)) { text = " "; } NGUIText.Prepare(text); StringBuilder sb = new StringBuilder(); int textLength = text.Length; float remainingWidth = NGUIText.regionWidth; int start = 0, offset = 0, lineCount = 1, prev = 0; bool lineIsEmpty = true; bool fits = true; Color c = NGUIText.tint; int subscriptMode = 0; // 0 = normal, 1 = subscript, 2 = superscript bool bold = false; bool italic = false; bool underline = false; bool strikethrough = false; bool ignoreColor = false; if (!NGUIText.useSymbols) { wrapLineColors = false; } if (wrapLineColors) { NGUIText.mColors.Add(c); } // Run through all characters for (; offset < textLength; ++offset) { char ch = text[offset]; // New line character -- start a new line if (ch == '\n') { if (lineCount == maxLineCount) { break; } remainingWidth = NGUIText.regionWidth; // Add the previous word to the final string if (start < offset) { sb.Append(text.Substring(start, offset - start + 1)); } else { sb.Append(ch); } if (wrapLineColors) { for (int i = 0; i < NGUIText.mColors.size; ++i) { sb.Insert(sb.Length - 1, "[-]"); } for (int i = 0; i < NGUIText.mColors.size; ++i) { sb.Append("["); sb.Append(NGUIText.EncodeColor(NGUIText.mColors[i])); sb.Append("]"); } } lineIsEmpty = true; ++lineCount; start = offset + 1; prev = 0; continue; } // When encoded symbols such as [RrGgBb] or [-] are encountered, skip past them if (NGUIText.encoding) { if (!wrapLineColors) { if (NGUIText.ParseSymbol(text, ref offset)) { --offset; continue; } } else if (NGUIText.ParseSymbol(text, ref offset, NGUIText.mColors, NGUIText.premultiply, ref subscriptMode, ref bold, ref italic, ref underline, ref strikethrough, ref ignoreColor)) { if (ignoreColor) { c = NGUIText.mColors[NGUIText.mColors.size - 1]; c.a *= NGUIText.mAlpha * NGUIText.tint.a; } else { c = NGUIText.tint * NGUIText.mColors[NGUIText.mColors.size - 1]; c.a *= NGUIText.mAlpha; } for (int b = 0, bmax = NGUIText.mColors.size - 2; b < bmax; ++b) { c.a *= NGUIText.mColors[b].a; } --offset; continue; } } // See if there is a symbol matching this text BMSymbol symbol = NGUIText.useSymbols ? NGUIText.GetSymbol(text, offset, textLength) : null; // Calculate how wide this symbol or character is going to be float glyphWidth; if (symbol == null) { // Find the glyph for this character float w = NGUIText.GetGlyphWidth(ch, prev); if (w == 0f) { continue; } glyphWidth = NGUIText.finalSpacingX + w; } else { glyphWidth = NGUIText.finalSpacingX + symbol.advance * NGUIText.fontScale; } // Reduce the width remainingWidth -= glyphWidth; // If this marks the end of a word, add it to the final string. if (NGUIText.IsSpace(ch) && !eastern && start < offset) { int end = offset - start + 1; // Last word on the last line should not include an invisible character if (lineCount == maxLineCount && remainingWidth <= 0f && offset < textLength) { char cho = text[offset]; if (cho < ' ' || NGUIText.IsSpace(cho)) { --end; } } sb.Append(text.Substring(start, end)); lineIsEmpty = false; start = offset + 1; prev = ch; } // Doesn't fit? if (Mathf.RoundToInt(remainingWidth) < 0) { // Can't start a new line if (lineIsEmpty || lineCount == maxLineCount) { // This is the first word on the line -- add it up to the character that fits sb.Append(text.Substring(start, Mathf.Max(0, offset - start))); bool space = NGUIText.IsSpace(ch); if (!space && !eastern) { fits = false; } if (wrapLineColors && NGUIText.mColors.size > 0) { sb.Append("[-]"); } if (lineCount++ == maxLineCount) { start = offset; break; } if (keepCharCount) { NGUIText.ReplaceSpaceWithNewline(ref sb); } else { NGUIText.EndLine(ref sb); } if (wrapLineColors) { for (int i = 0; i < NGUIText.mColors.size; ++i) { sb.Insert(sb.Length - 1, "[-]"); } for (int i = 0; i < NGUIText.mColors.size; ++i) { sb.Append("["); sb.Append(NGUIText.EncodeColor(NGUIText.mColors[i])); sb.Append("]"); } } // Start a brand-new line lineIsEmpty = true; start = offset; remainingWidth = NGUIText.regionWidth - glyphWidth; prev = 0; } else { // Revert the position to the beginning of the word and reset the line lineIsEmpty = true; remainingWidth = NGUIText.regionWidth; offset = start - 1; prev = 0; if (lineCount++ == maxLineCount) { break; } if (keepCharCount) { NGUIText.ReplaceSpaceWithNewline(ref sb); } else { NGUIText.EndLine(ref sb); } if (wrapLineColors) { for (int i = 0; i < NGUIText.mColors.size; ++i) { sb.Insert(sb.Length - 1, "[-]"); } for (int i = 0; i < NGUIText.mColors.size; ++i) { sb.Append("["); sb.Append(NGUIText.EncodeColor(NGUIText.mColors[i])); sb.Append("]"); } } continue; } } else { prev = ch; } // Advance the offset past the symbol if (symbol != null) { offset += symbol.length - 1; prev = 0; } } if (start < offset) { sb.Append(text.Substring(start, offset - start)); } if (wrapLineColors && NGUIText.mColors.size > 0) { sb.Append("[-]"); } finalText = sb.ToString(); NGUIText.mColors.Clear(); return(fits && ((offset == textLength) || (lineCount <= Mathf.Min(NGUIText.maxLines, maxLineCount)))); }