private static Vector2 MeasureTextWidth(UIDynamicFont font, string text, float textScale, out Vector2 yBounds) { float width = 1f; int size = Mathf.CeilToInt(font.size * textScale); font.RequestCharacters(text, size, FontStyle.Normal); yBounds = new Vector2(9999999f, -999999999f); for (int i = 0; i < text.Length; i++) { char c = text[i]; font.baseFont.GetCharacterInfo(c, out CharacterInfo characterInfo, size, FontStyle.Normal); if (c == '\t') { width += 3f * characterSpacing; } else { width += ((c != ' ') ? characterInfo.maxX : (characterInfo.advance + (characterSpacing * textScale))); yBounds.x = Mathf.Min(yBounds.x, characterInfo.minY); yBounds.y = Mathf.Max(yBounds.y, characterInfo.maxY); } } if (text.Length > 2) { width += (text.Length - 2) * characterSpacing * textScale; } return(new Vector2(width + 6, yBounds.y - yBounds.x + 6)); }
private static Vector2 CalculateTextureSize(UIDynamicFont font, float textScale, ref PoolList <UIMarkupToken> tokens, out int startYPos) { float xAdvance = 0; var yBounds = new Vector2(999999999999999999999999f, -99999999999999999999999999f); for (int i = 0; i < tokens.Count; i++) { UIMarkupToken token = tokens[i]; if (token.tokenType == UIMarkupTokenType.Text) { Vector2 textDimensions = MeasureTextWidth(font, token.value, textScale, out Vector2 yBoundsCalc); xAdvance += textDimensions.x; yBounds.x = Mathf.Min(yBounds.x, yBoundsCalc.x); yBounds.y = Mathf.Max(yBounds.y, yBoundsCalc.y); } else if (token.tokenType == UIMarkupTokenType.Whitespace) { int size2 = Mathf.CeilToInt(font.size * textScale); float num2 = characterSpacing * textScale; float num = 0; font.RequestCharacters(" ", size2, FontStyle.Normal); for (int j = 0; j < token.length; j++) { char c = token[j]; int multiplier = 1; if (c == '\t') { multiplier = 4; } font.baseFont.GetCharacterInfo(' ', out CharacterInfo characterInfo, size2, FontStyle.Normal); num += (characterInfo.advance + num2) * multiplier; } token.height = Mathf.CeilToInt(num); xAdvance += token.height; yBounds.x = Mathf.Min(yBounds.x, 0); yBounds.y = Mathf.Max(yBounds.y, token.height); } else if (token.tokenType == UIMarkupTokenType.StartTag) { if (UIDynamicFontRendererRedirector.Matches(token, "sprite")) { if (token.attributeCount != 1) { tokens.RemoveAt(i); i--; continue; } UITextureAtlas.SpriteInfo spriteInfo = UIView.GetAView().defaultAtlas[token.GetAttribute(0).m_Value.value]; if (spriteInfo == null) { tokens.RemoveAt(i); i--; continue; } float targetScale = font.baseline * textScale / spriteInfo.texture.height; token.height = (int)(targetScale * spriteInfo.texture.height); xAdvance += (int)(targetScale * spriteInfo.texture.width); yBounds.x = Mathf.Min(yBounds.x, 0); yBounds.y = Mathf.Max(yBounds.y, token.height); } else if (UIDynamicFontRendererRedirector.Matches(token, UIDynamicFontRendererRedirector.TAG_LINE)) { if (token.attributeCount != 1) { tokens.RemoveAt(i); i--; continue; } string[] attrs = token.GetAttribute(0).m_Value.value.Split(','); if (attrs.Length != 3 || !Regex.IsMatch(attrs[1], "^[0-9a-fA-F]{6}$")) { tokens.RemoveAt(i); i--; continue; } UITextureAtlas.SpriteInfo spriteInfo = UIView.GetAView().defaultAtlas[attrs[0]]; if (spriteInfo == null) { tokens.RemoveAt(i); i--; continue; } float baseScale = font.baseline * textScale / spriteInfo.texture.height; float targetScale = baseScale * 2; token.height = (int)(targetScale * spriteInfo.texture.height); xAdvance += (int)(targetScale * spriteInfo.texture.width); yBounds.x = Mathf.Min(yBounds.x, -token.height / 3); yBounds.y = Mathf.Max(yBounds.y, token.height / 3 * 2); } } } if (tokens.Count == 0) { startYPos = 0; return(new Vector2(1, 1)); } float ySize = yBounds.y - yBounds.x; startYPos = (int)(-yBounds.x); return(new Vector2(xAdvance, ySize)); }