public void FixBrokenWord() { if (chineseTxt == null) { TextAsset txt = allText; chineseTxt = txt.ToString(); } // Debug.Log (chineseTxt); if (null == baseFont) { baseFont = (UnityEngine.Font)Resources.Load("msyh"); } baseFont.RequestCharactersInTexture(chineseTxt, 16); // // NGUIText.dynamicFont=baseFont; // // Texture texture = baseFont.material.mainTexture; // Font的内部纹理 Debug.Log(string.Format("texture:{0} {1} {2}", texture.width, texture.height, chineseTxt.Length)); // 纹理大小 //Debug.Log ("FixBrokenWord"); }
/////////////////////////////////////////////////////////////////////////////// // build mesh /////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------ // Desc: This only calculate result in one line // ------------------------------------------------------------------ public static void BuildTextLine( Vector3[] _vertices, Vector2[] _uvs, string _text, Font _font, int _lineHeight, int _fontSize, int _wordSpacing, int _letterSpacing ) { int cur_x = 0; int cur_y = 0; // yes, Unity's GetCharacterInfo have y problem, you should get lowest character j's y-offset adjust it. CharacterInfo jCharInfo; _font.RequestCharactersInTexture ( "j", _fontSize, FontStyle.Normal ); _font.GetCharacterInfo('j', out jCharInfo, _fontSize, FontStyle.Normal); float ttf_offset = (_fontSize + jCharInfo.vert.yMax); // for ( int i = 0; i < _text.Length; ++i ) { char cur_char = _text[i]; // NOTE: we skip new-line operation, since we believe this function only have one-line text if ( cur_char == '\n' ) { continue; } // generate mesh CharacterInfo charInfo; _font.GetCharacterInfo ( cur_char, out charInfo, _fontSize ); int idx = 4*i; // build vertices _vertices[idx + 0] = new Vector3(cur_x + charInfo.vert.xMin, cur_y - charInfo.vert.yMin + ttf_offset, 0.0f); _vertices[idx + 1] = new Vector3(cur_x + charInfo.vert.xMax, cur_y - charInfo.vert.yMin + ttf_offset, 0.0f); _vertices[idx + 2] = new Vector3(cur_x + charInfo.vert.xMax, cur_y - charInfo.vert.yMax + ttf_offset, 0.0f); _vertices[idx + 3] = new Vector3(cur_x + charInfo.vert.xMin, cur_y - charInfo.vert.yMax + ttf_offset, 0.0f); // build uv if ( charInfo.flipped ) { _uvs[idx + 0] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMin); _uvs[idx + 1] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMax); _uvs[idx + 2] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMax); _uvs[idx + 3] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMin); } else { _uvs[idx + 0] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMax); _uvs[idx + 1] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMax); _uvs[idx + 2] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMin); _uvs[idx + 3] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMin); } // advance x cur_x += (int)charInfo.width + _letterSpacing; if ( cur_char == ' ' ) cur_x += _wordSpacing; } }
static public void FixBrokenWord() { if (chineseTxt == null) { TextAsset txt = Resources.Load("font/chinese") as TextAsset; chineseTxt = txt.ToString(); } baseFont.RequestCharactersInTexture(chineseTxt); Texture texture = baseFont.material.mainTexture; // Font的内部纹理 Debug.Log(string.Format("texture:{0} {1}", texture.width, texture.height); // 纹理大小 }
static public int RequestCharactersInTexture__String(IntPtr l) { try { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 2, out a1); self.RequestCharactersInTexture(a1); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int RequestCharactersInTexture(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 5) { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 3, out a1); System.Int32 a2; checkType(l, 4, out a2); UnityEngine.FontStyle a3; checkEnum(l, 5, out a3); self.RequestCharactersInTexture(a1, a2, a3); pushValue(l, true); return(1); } else if (argc == 4) { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 3, out a1); System.Int32 a2; checkType(l, 4, out a2); self.RequestCharactersInTexture(a1, a2); pushValue(l, true); return(1); } else if (argc == 3) { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 3, out a1); self.RequestCharactersInTexture(a1); pushValue(l, true); return(1); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function to call"); return(2); } catch (Exception e) { return(error(l, e)); } }
static public int RequestCharactersInTexture__String__Int32__FontStyle(IntPtr l) { try { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); UnityEngine.FontStyle a3; checkEnum(l, 4, out a3); self.RequestCharactersInTexture(a1, a2, a3); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int RequestCharactersInTexture(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 2, out a1); self.RequestCharactersInTexture(a1); return(0); } else if (argc == 3) { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.RequestCharactersInTexture(a1, a2); return(0); } else if (argc == 4) { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); UnityEngine.FontStyle a3; checkEnum(l, 4, out a3); self.RequestCharactersInTexture(a1, a2, a3); return(0); } LuaDLL.luaL_error(l, "No matched override function to call"); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
static int RequestCharactersInTexture(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 2) { UnityEngine.Font obj = (UnityEngine.Font)ToLua.CheckObject(L, 1, typeof(UnityEngine.Font)); string arg0 = ToLua.CheckString(L, 2); obj.RequestCharactersInTexture(arg0); return(0); } else if (count == 3) { UnityEngine.Font obj = (UnityEngine.Font)ToLua.CheckObject(L, 1, typeof(UnityEngine.Font)); string arg0 = ToLua.CheckString(L, 2); int arg1 = (int)LuaDLL.luaL_checknumber(L, 3); obj.RequestCharactersInTexture(arg0, arg1); return(0); } else if (count == 4) { UnityEngine.Font obj = (UnityEngine.Font)ToLua.CheckObject(L, 1, typeof(UnityEngine.Font)); string arg0 = ToLua.CheckString(L, 2); int arg1 = (int)LuaDLL.luaL_checknumber(L, 3); UnityEngine.FontStyle arg2 = (UnityEngine.FontStyle)ToLua.CheckObject(L, 4, typeof(UnityEngine.FontStyle)); obj.RequestCharactersInTexture(arg0, arg1, arg2); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.Font.RequestCharactersInTexture")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public static int CalculateOffsetToFit(Font font, string text) { if (((font == null) || string.IsNullOrEmpty(text)) || (current.lineWidth < 1)) { return 0; } int finalSize = current.finalSize; font.RequestCharactersInTexture(text, finalSize, current.style); float finalLineWidth = current.finalLineWidth; int length = text.Length; while ((length > 0) && (finalLineWidth > 0f)) { char ch = text[--length]; if (font.GetCharacterInfo(ch, out mTempChar, finalSize, current.style)) { finalLineWidth -= mTempChar.width; } } if (finalLineWidth < 0f) { length++; } return length; }
static public int RequestCharactersInTexture(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif int argc = LuaDLL.lua_gettop(l); if (argc == 2) { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 2, out a1); self.RequestCharactersInTexture(a1); pushValue(l, true); return(1); } else if (argc == 3) { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.RequestCharactersInTexture(a1, a2); pushValue(l, true); return(1); } else if (argc == 4) { UnityEngine.Font self = (UnityEngine.Font)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); UnityEngine.FontStyle a3; a3 = (UnityEngine.FontStyle)LuaDLL.luaL_checkinteger(l, 4); self.RequestCharactersInTexture(a1, a2, a3); pushValue(l, true); return(1); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function RequestCharactersInTexture to call"); return(2); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
/// <summary> /// Text wrapping functionality. The 'width' and 'height' should be in pixels. /// </summary> static public bool WrapText (Font font, string text, out string finalText) { if (current.lineWidth < 1 || current.lineHeight < 1 || string.IsNullOrEmpty(text)) { finalText = ""; return false; } int maxLineCount = (current.maxLines > 0) ? current.maxLines : 1000000; int size = current.finalSize; float height = (current.maxLines > 0) ? Mathf.Min(current.finalLineHeight, size * current.maxLines) : current.finalLineHeight; float sum = size + current.finalSpacingY; maxLineCount = Mathf.FloorToInt((sum > 0) ? Mathf.Min(maxLineCount, height / sum) : 0); if (maxLineCount == 0) { finalText = ""; return false; } // Ensure that we have the required characters to work with if (font != null) font.RequestCharactersInTexture(text, size, current.style); StringBuilder sb = new StringBuilder(); int textLength = text.Length; float lineWidth = current.finalLineWidth; float remainingWidth = lineWidth; float finalSpacingX = current.finalSpacingX; int start = 0; int offset = 0; int lineCount = 1; int previousChar = 0; bool lineIsEmpty = true; // 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 = lineWidth; // 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 (ParseSymbol(text, ref offset)) { --offset; continue; } // If the character is missing for any reason, skip it if (!font.GetCharacterInfo(ch, out mTempChar, size, current.style)) continue; float glyphWidth = finalSpacingX + mTempChar.width; remainingWidth -= glyphWidth; // Doesn't fit? if (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))); if (lineCount++ == maxLineCount) { start = offset; break; } EndLine(ref sb); // Start a brand-new line lineIsEmpty = true; if (ch == ' ') { start = offset + 1; remainingWidth = lineWidth; } else { start = offset; remainingWidth = lineWidth - 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 = lineWidth; offset = start - 1; previousChar = 0; if (lineCount++ == maxLineCount) break; EndLine(ref sb); continue; } } else previousChar = ch; } if (start < offset) sb.Append(text.Substring(start, offset - start)); finalText = sb.ToString(); return (offset == textLength) || (lineCount <= Mathf.Min(current.maxLines, maxLineCount)); }
/// <summary> /// Calculate the character index offset required to print the end of the specified text. /// NOTE: This function assumes that the text has been stripped of all symbols. /// </summary> static public int CalculateOffsetToFit (Font font, string text) { if (font == null || string.IsNullOrEmpty(text) || current.lineWidth < 1) return 0; // Ensure we have the characters to work with int size = current.finalSize; font.RequestCharactersInTexture(text, size, current.style); float remainingWidth = current.finalLineWidth; int textLength = text.Length; int currentCharacterIndex = textLength; while (currentCharacterIndex > 0 && remainingWidth > 0f) { char c = text[--currentCharacterIndex]; if (font.GetCharacterInfo(c, out mTempChar, size, current.style)) remainingWidth -= mTempChar.width; } if (remainingWidth < 0f) ++currentCharacterIndex; return currentCharacterIndex; }
public static Vector2 CalculatePrintedSize(Font font, string text) { Vector2 zero = Vector2.zero; if ((font == null) || string.IsNullOrEmpty(text)) { return zero; } if (current.encoding) { text = StripSymbols(text); } int finalSize = current.finalSize; font.RequestCharactersInTexture(text, finalSize, current.style); float num2 = 0f; float num3 = 0f; float num4 = 0f; float num5 = finalSize + current.finalSpacingY; float finalSpacingX = current.finalSpacingX; int length = text.Length; for (int i = 0; i < length; i++) { char ch = text[i]; if (ch == '\n') { if (num2 > num4) { num4 = num2; } num2 = 0f; num3 += num5; } else if ((ch >= ' ') && font.GetCharacterInfo(ch, out mTempChar, finalSize, current.style)) { num2 += mTempChar.width + finalSpacingX; } } zero.x = (num2 <= num4) ? num4 : num2; zero.y = num3 + finalSize; return (Vector2) (zero / current.pixelDensity); }
public static bool WrapText(Font font, string text, out string finalText) { if (((current.lineWidth < 1) || (current.lineHeight < 1)) || string.IsNullOrEmpty(text)) { finalText = string.Empty; return false; } int b = (current.maxLines <= 0) ? 0xf4240 : current.maxLines; int finalSize = current.finalSize; float num3 = (current.maxLines <= 0) ? current.finalLineHeight : Mathf.Min(current.finalLineHeight, (float) (finalSize * current.maxLines)); float num4 = finalSize + current.finalSpacingY; b = Mathf.FloorToInt((num4 <= 0f) ? 0f : Mathf.Min((float) b, num3 / num4)); if (b == 0) { finalText = string.Empty; return false; } if (font != null) { font.RequestCharactersInTexture(text, finalSize, current.style); } StringBuilder s = new StringBuilder(); int length = text.Length; float finalLineWidth = current.finalLineWidth; float num7 = finalLineWidth; float finalSpacingX = current.finalSpacingX; int startIndex = 0; int index = 0; int num11 = 1; int num12 = 0; bool flag = true; while (index < length) { char ch = text[index]; if (ch == '\n') { if (num11 == b) { break; } num7 = finalLineWidth; if (startIndex < index) { s.Append(text.Substring(startIndex, (index - startIndex) + 1)); } else { s.Append(ch); } flag = true; num11++; startIndex = index + 1; num12 = 0; } else { if (((ch == ' ') && (num12 != 0x20)) && (startIndex < index)) { s.Append(text.Substring(startIndex, (index - startIndex) + 1)); flag = false; startIndex = index + 1; num12 = ch; } if (ParseSymbol(text, ref index)) { index--; } else if (font.GetCharacterInfo(ch, out mTempChar, finalSize, current.style)) { float num13 = finalSpacingX + mTempChar.width; num7 -= num13; if (num7 < 0f) { if (flag || (num11 == b)) { s.Append(text.Substring(startIndex, Mathf.Max(0, index - startIndex))); if (num11++ == b) { startIndex = index; break; } EndLine(ref s); flag = true; if (ch == ' ') { startIndex = index + 1; num7 = finalLineWidth; } else { startIndex = index; num7 = finalLineWidth - num13; } num12 = 0; } else { while ((startIndex < length) && (text[startIndex] == ' ')) { startIndex++; } flag = true; num7 = finalLineWidth; index = startIndex - 1; num12 = 0; if (num11++ == b) { break; } EndLine(ref s); } } else { num12 = ch; } } } index++; } if (startIndex < index) { s.Append(text.Substring(startIndex, index - startIndex)); } finalText = s.ToString(); return ((index == length) || (num11 <= Mathf.Min(current.maxLines, b))); }
public static void Print(Font font, string text, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols) { if ((font != null) && !string.IsNullOrEmpty(text)) { int finalSize = current.finalSize; int size = verts.size; float num3 = finalSize + current.finalSpacingY; float num4 = 0f; font.RequestCharactersInTexture("j", finalSize, current.style); font.GetCharacterInfo('j', out mTempChar, finalSize, current.style); num4 = finalSize + mTempChar.vert.yMax; font.RequestCharactersInTexture(text, finalSize, current.style); mColors.Add(Color.white); float num5 = 0f; float num6 = 0f; float num7 = 0f; float finalSpacingX = current.finalSpacingX; float num9 = 1f / current.pixelDensity; float num10 = finalSize; Vector3 zero = Vector3.zero; Vector3 vector2 = Vector3.zero; Vector2 vector3 = Vector2.zero; Vector2 vector4 = Vector2.zero; Color a = current.tint * current.gradientBottom; Color b = current.tint * current.gradientTop; Color32 tint = current.tint; int length = text.Length; for (int i = 0; i < length; i++) { char ch = text[i]; if (ch == '\n') { if (num5 > num7) { num7 = num5; } if (current.alignment != TextAlignment.Left) { Align(verts, size, num5 - finalSpacingX); size = verts.size; } num5 = 0f; num6 += num3; } else if (ch >= ' ') { if (current.encoding && ParseSymbol(text, ref i, mColors, current.premultiply)) { Color color4 = current.tint * mColors[mColors.size - 1]; tint = color4; if (current.gradient) { a = current.gradientBottom * color4; b = current.gradientTop * color4; } i--; } else if (font.GetCharacterInfo(ch, out mTempChar, finalSize, current.style)) { zero.x = num5 + mTempChar.vert.xMin; zero.y = -((num6 - mTempChar.vert.yMax) + num4); vector2.x = zero.x + mTempChar.vert.width; vector2.y = zero.y - mTempChar.vert.height; if (num9 != 1f) { zero = (Vector3) (zero * num9); vector2 = (Vector3) (vector2 * num9); } vector3.x = mTempChar.uv.xMin; vector3.y = mTempChar.uv.yMin; vector4.x = mTempChar.uv.xMax; vector4.y = mTempChar.uv.yMax; num5 += mTempChar.width + finalSpacingX; verts.Add(new Vector3(vector2.x, zero.y)); verts.Add(new Vector3(zero.x, zero.y)); verts.Add(new Vector3(zero.x, vector2.y)); verts.Add(new Vector3(vector2.x, vector2.y)); if (mTempChar.flipped) { uvs.Add(new Vector2(vector3.x, vector4.y)); uvs.Add(new Vector2(vector3.x, vector3.y)); uvs.Add(new Vector2(vector4.x, vector3.y)); uvs.Add(new Vector2(vector4.x, vector4.y)); } else { uvs.Add(new Vector2(vector4.x, vector3.y)); uvs.Add(new Vector2(vector3.x, vector3.y)); uvs.Add(new Vector2(vector3.x, vector4.y)); uvs.Add(new Vector2(vector4.x, vector4.y)); } if (current.gradient) { float t = num10 - (-mTempChar.vert.yMax + num4); float num14 = t - mTempChar.vert.height; t /= num10; num14 /= num10; s_c0 = Color.Lerp(a, b, t); s_c1 = Color.Lerp(a, b, num14); cols.Add(s_c0); cols.Add(s_c0); cols.Add(s_c1); cols.Add(s_c1); } else { for (int j = 0; j < 4; j++) { cols.Add(tint); } } } } } if ((current.alignment != TextAlignment.Left) && (size < verts.size)) { Align(verts, size, num5 - finalSpacingX); size = verts.size; } mColors.Clear(); } }
/// <summary> /// Get the printed size of the specified string. The returned value is in pixels. /// </summary> static public Vector2 CalculatePrintedSize (string text, Font font, int size, FontStyle style, bool encoding) { Vector2 v = Vector2.zero; if (font != null && !string.IsNullOrEmpty(text)) { // When calculating printed size, get rid of all symbols first since they are invisible anyway if (encoding) text = NGUIText.StripSymbols(text); // Ensure we have characters to work with font.RequestCharactersInTexture(text, size, style); float x = 0f; float y = 0; float fs = size; float maxX = 0f; for (int i = 0, imax = text.Length; i < imax; ++i) { char c = text[i]; // Start a new line if (c == '\n') { if (x > maxX) maxX = x; x = 0f; y += fs; continue; } // Skip invalid characters if (c < ' ') continue; if (font.GetCharacterInfo(c, out mTempChar, size, style)) x += mTempChar.width; // Convert from pixel coordinates to local coordinates v.x = ((x > maxX) ? x : maxX); v.y = (y + fs); } } return v; }
/// <summary> /// Calculate the character index offset required to print the end of the specified text. /// NOTE: This function assumes that the text has been stripped of all symbols. /// </summary> static public int CalculateOffsetToFit (string text, Font font, int size, FontStyle style, int lineWidth) { if (font == null || string.IsNullOrEmpty(text) || lineWidth < 1) return 0; // Ensure we have the characters to work with font.RequestCharactersInTexture(text, size, style); int textLength = text.Length; int remainingWidth = lineWidth; int currentCharacterIndex = textLength; while (currentCharacterIndex > 0 && remainingWidth > 0) { char c = text[--currentCharacterIndex]; if (font.GetCharacterInfo(c, out mTempChar, size, style)) remainingWidth -= (int)mTempChar.width; } if (remainingWidth < 0) ++currentCharacterIndex; return currentCharacterIndex; }
/// <summary> /// Get the printed size of the specified string. The returned value is in pixels. /// </summary> static public Vector2 CalculatePrintedSize (Font font, string text) { Vector2 v = Vector2.zero; if (font != null && !string.IsNullOrEmpty(text)) { // When calculating printed size, get rid of all symbols first since they are invisible anyway if (current.encoding) text = StripSymbols(text); // Ensure we have characters to work with int size = current.finalSize; font.RequestCharactersInTexture(text, size, current.style); float x = 0; float y = 0; float maxX = 0f; float lineHeight = size + current.finalSpacingY; float spacingX = current.finalSpacingX; int chars = text.Length; for (int i = 0; i < chars; ++i) { char c = text[i]; // Start a new line if (c == '\n') { if (x > maxX) maxX = x; x = 0f; y += lineHeight; continue; } // Skip invalid characters if (c < ' ') continue; if (font.GetCharacterInfo(c, out mTempChar, size, current.style)) x += mTempChar.width + spacingX; } // Padding is always between characters, so it's one less than the number of characters v.x = ((x > maxX) ? x : maxX); v.y = (y + size); v /= current.pixelDensity; } return v; }
/// <summary> /// Print the specified text into the buffers. /// </summary> static public void Print (string text, Font font, int size, FontStyle style, Color32 color, bool encoding, TextAlignment alignment, int lineWidth, bool premultiply, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols) { if (font == null || string.IsNullOrEmpty(text)) return; float baseline = 0f; // We need to know the baseline first font.RequestCharactersInTexture("j", size, style); font.GetCharacterInfo('j', out mTempChar, size, style); baseline = size + mTempChar.vert.yMax; // Ensure that the text we're about to print exists in the font's texture font.RequestCharactersInTexture(text, size, style); // Start with the specified color mColors.Add(color); int indexOffset = verts.size; int maxX = 0; int x = 0; int y = 0; int lineHeight = size; Vector3 v0 = Vector3.zero, v1 = Vector3.zero; Vector2 u0 = Vector2.zero, u1 = Vector2.zero; int textLength = text.Length; for (int i = 0; i < textLength; ++i) { char c = text[i]; if (c == '\n') { if (x > maxX) maxX = x; if (alignment != TextAlignment.Left) { Align(verts, indexOffset, alignment, x, lineWidth); indexOffset = verts.size; } x = 0; y += lineHeight; continue; } if (c < ' ') continue; if (encoding && ParseSymbol(text, ref i, mColors, premultiply)) { color = mColors[mColors.size - 1]; --i; continue; } if (!font.GetCharacterInfo(c, out mTempChar, size, style)) continue; v0.x = (x + mTempChar.vert.xMin); v0.y = -(y - mTempChar.vert.yMax + baseline); v1.x = v0.x + mTempChar.vert.width; v1.y = v0.y - mTempChar.vert.height; u0.x = mTempChar.uv.xMin; u0.y = mTempChar.uv.yMin; u1.x = mTempChar.uv.xMax; u1.y = mTempChar.uv.yMax; x += (int)mTempChar.width; for (int b = 0; b < 4; ++b) cols.Add(color); if (mTempChar.flipped) { uvs.Add(new Vector2(u0.x, u1.y)); uvs.Add(new Vector2(u0.x, u0.y)); uvs.Add(new Vector2(u1.x, u0.y)); uvs.Add(new Vector2(u1.x, u1.y)); } else { uvs.Add(new Vector2(u1.x, u0.y)); uvs.Add(new Vector2(u0.x, u0.y)); uvs.Add(new Vector2(u0.x, u1.y)); uvs.Add(new Vector2(u1.x, u1.y)); } verts.Add(new Vector3(v1.x, v0.y)); verts.Add(new Vector3(v0.x, v0.y)); verts.Add(new Vector3(v0.x, v1.y)); verts.Add(new Vector3(v1.x, v1.y)); } if (alignment != TextAlignment.Left && indexOffset < verts.size) { Align(verts, indexOffset, alignment, x, lineWidth); indexOffset = verts.size; } mColors.Clear(); }
/// <summary> /// Ensure that we have the requested characters present. /// </summary> static public void RequestCharactersInTexture (Font font, string text) { if (font != null) { font.RequestCharactersInTexture(text, current.finalSize, current.style); } }
//フォントの文字画像の作成リクエスト void RequestCharactersInTexture(Font font, List<UguiNovelTextCharacter> characterDataList) { List<RequestCharactersInfo> infoList = MakeRequestCharactersInfoList(characterDataList); isRequestingCharactersInTexture = true; #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 font.textureRebuildCallback += FontTextureRebuildCallback; foreach (RequestCharactersInfo info in infoList) { font.RequestCharactersInTexture(info.characters, info.size, info.style); } font.textureRebuildCallback -= FontTextureRebuildCallback; #else Font.textureRebuilt += FontTextureRebuildCallback; foreach (RequestCharactersInfo info in infoList) { font.RequestCharactersInTexture(info.characters, info.size, info.style); } Font.textureRebuilt -= FontTextureRebuildCallback; #endif isRequestingCharactersInTexture = false; }
/// <summary> /// Print the specified text into the buffers. /// </summary> static public void Print (Font font, string text, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols) { if (font == null || string.IsNullOrEmpty(text)) return; int size = current.finalSize; int indexOffset = verts.size; float lineHeight = size + current.finalSpacingY; // We need to know the baseline first float baseline = 0f; font.RequestCharactersInTexture("j", size, current.style); font.GetCharacterInfo('j', out mTempChar, size, current.style); baseline = size + mTempChar.vert.yMax; // Ensure that the text we're about to print exists in the font's texture font.RequestCharactersInTexture(text, size, current.style); // Start with the white tint mColors.Add(Color.white); float x = 0f; float y = 0f; float maxX = 0f; float spacingX = current.finalSpacingX; float pixelSize = 1f / current.pixelDensity; float sizeF = size; Vector3 v0 = Vector3.zero, v1 = Vector3.zero; Vector2 u0 = Vector2.zero, u1 = Vector2.zero; Color gb = current.tint * current.gradientBottom; Color gt = current.tint * current.gradientTop; Color32 uc = current.tint; int textLength = text.Length; for (int i = 0; i < textLength; ++i) { char c = text[i]; if (c == '\n') { if (x > maxX) maxX = x; if (current.alignment != TextAlignment.Left) { Align(verts, indexOffset, x - spacingX); indexOffset = verts.size; } x = 0; y += lineHeight; continue; } if (c < ' ') continue; // Color changing symbol if (current.encoding && ParseSymbol(text, ref i, mColors, current.premultiply)) { Color fc = current.tint * mColors[mColors.size - 1]; uc = fc; if (current.gradient) { gb = current.gradientBottom * fc; gt = current.gradientTop * fc; } --i; continue; } if (!font.GetCharacterInfo(c, out mTempChar, size, current.style)) continue; v0.x = (x + mTempChar.vert.xMin); v0.y = -(y - mTempChar.vert.yMax + baseline); v1.x = v0.x + mTempChar.vert.width; v1.y = v0.y - mTempChar.vert.height; if (pixelSize != 1f) { v0 *= pixelSize; v1 *= pixelSize; } u0.x = mTempChar.uv.xMin; u0.y = mTempChar.uv.yMin; u1.x = mTempChar.uv.xMax; u1.y = mTempChar.uv.yMax; x += (mTempChar.width + spacingX); verts.Add(new Vector3(v1.x, v0.y)); verts.Add(new Vector3(v0.x, v0.y)); verts.Add(new Vector3(v0.x, v1.y)); verts.Add(new Vector3(v1.x, v1.y)); if (mTempChar.flipped) { uvs.Add(new Vector2(u0.x, u1.y)); uvs.Add(new Vector2(u0.x, u0.y)); uvs.Add(new Vector2(u1.x, u0.y)); uvs.Add(new Vector2(u1.x, u1.y)); } else { uvs.Add(new Vector2(u1.x, u0.y)); uvs.Add(new Vector2(u0.x, u0.y)); uvs.Add(new Vector2(u0.x, u1.y)); uvs.Add(new Vector2(u1.x, u1.y)); } if (current.gradient) { float min = sizeF - (-mTempChar.vert.yMax + baseline); float max = min - (mTempChar.vert.height); min /= sizeF; max /= sizeF; s_c0 = Color.Lerp(gb, gt, min); s_c1 = Color.Lerp(gb, gt, max); cols.Add(s_c0); cols.Add(s_c0); cols.Add(s_c1); cols.Add(s_c1); } else for (int b = 0; b < 4; ++b) cols.Add(uc); } if (current.alignment != TextAlignment.Left && indexOffset < verts.size) { Align(verts, indexOffset, x - spacingX); indexOffset = verts.size; } mColors.Clear(); }
/// <summary> /// Text wrapping functionality. The 'width' and 'height' should be in pixels. /// </summary> static public bool WrapText (string text, Font font, int size, FontStyle style, int width, int height, int maxLines, bool encoding, out string finalText) { 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; } // Ensure that we have the required characters to work with if (font != null) font.RequestCharactersInTexture(text, size, style); 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; // 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 (ParseSymbol(text, ref offset)) { --offset; continue; } int glyphWidth = 0; if (font.GetCharacterInfo(ch, out mTempChar, size, style)) glyphWidth = Mathf.RoundToInt(mTempChar.width); 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; } 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; EndLine(ref sb); continue; } } else previousChar = ch; } if (start < offset) sb.Append(text.Substring(start, offset - start)); finalText = sb.ToString(); return (!multiline || offset == textLength || (maxLines > 0 && lineCount <= maxLines)); }