public float[] GetCharacterWidths(string text, int startIndex, int endIndex, out float totalWidth) { totalWidth = 0f; dfFont font = (dfFont)base.Font; float[] singleArray = new float[text.Length]; float textScale = base.TextScale * base.PixelRatio; float characterSpacing = (float)base.CharacterSpacing * textScale; for (int i = startIndex; i <= endIndex; i++) { dfFont.GlyphDefinition glyph = font.GetGlyph(text[i]); if (glyph != null) { if (i > 0) { singleArray[i - 1] = singleArray[i - 1] + characterSpacing; totalWidth = totalWidth + characterSpacing; } float single = (float)glyph.xadvance * textScale; singleArray[i] = single; totalWidth = totalWidth + single; } } return(singleArray); }
public float[] GetCharacterWidths(string text, int startIndex, int endIndex, out float totalWidth) { totalWidth = 0f; dfFont font = (dfFont)base.Font; float[] numArray = new float[text.Length]; float num = base.TextScale * base.PixelRatio; float num2 = base.CharacterSpacing * num; for (int i = startIndex; i <= endIndex; i++) { dfFont.GlyphDefinition glyph = font.GetGlyph(text[i]); if (glyph != null) { if (i > 0) { numArray[i - 1] += num2; totalWidth += num2; } float num4 = glyph.xadvance * num; numArray[i] = num4; totalWidth += num4; } } return(numArray); }
private void calculateTokenRenderSize(dfMarkupToken token) { try { dfFont font = (dfFont)base.Font; int num = 0; char previousChar = '\0'; char id = '\0'; if ((token.TokenType == dfMarkupTokenType.Whitespace) || (token.TokenType == dfMarkupTokenType.Text)) { int num2 = 0; while (num2 < token.Length) { id = token[num2]; if (id == '\t') { num += base.TabSize; } else { dfFont.GlyphDefinition glyph = font.GetGlyph(id); if (glyph != null) { if (num2 > 0) { num += font.GetKerning(previousChar, id); num += base.CharacterSpacing; } num += glyph.xadvance; } } num2++; previousChar = id; } } else if ((token.TokenType == dfMarkupTokenType.StartTag) && token.Matches("sprite")) { if (token.AttributeCount < 1) { throw new Exception("Missing sprite name in markup"); } Texture texture = font.Texture; int lineHeight = font.LineHeight; string str = token.GetAttribute(0).Value.Value; dfAtlas.ItemInfo info = font.atlas[str]; if (info != null) { float num4 = (info.region.width * texture.width) / (info.region.height * texture.height); num = Mathf.CeilToInt(lineHeight * num4); } } token.Height = base.Font.LineHeight; token.Width = num; } finally { } }
private void calculateTokenRenderSize(dfMarkupToken token) { try { dfFont font = (dfFont)base.Font; int kerning = 0; char chr = '\0'; char item = '\0'; if ((token.TokenType == dfMarkupTokenType.Whitespace ? true : token.TokenType == dfMarkupTokenType.Text)) { int num = 0; while (num < token.Length) { item = token[num]; if (item != '\t') { dfFont.GlyphDefinition glyph = font.GetGlyph(item); if (glyph != null) { if (num > 0) { kerning = kerning + font.GetKerning(chr, item); kerning = kerning + base.CharacterSpacing; } kerning = kerning + glyph.xadvance; } } else { kerning = kerning + base.TabSize; } num++; chr = item; } } else if (token.TokenType == dfMarkupTokenType.StartTag && token.Matches("sprite")) { if (token.AttributeCount < 1) { throw new Exception("Missing sprite name in markup"); } UnityEngine.Texture texture = font.Texture; int lineHeight = font.LineHeight; string value = token.GetAttribute(0).Value.Value; dfAtlas.ItemInfo itemInfo = font.atlas[value]; if (itemInfo != null) { float single = itemInfo.region.width * (float)texture.width / (itemInfo.region.height * (float)texture.height); kerning = Mathf.CeilToInt((float)lineHeight * single); } } token.Height = base.Font.LineHeight; token.Width = kerning; } finally { } }
public static Font GetFontFromdfFont(dfFont font) { Font f = new Font(); f.material = new Material(GUI.skin.font.material); f.material.mainTexture = font.Texture; f.material.mainTexture.wrapMode = TextureWrapMode.Repeat; CharacterInfo[] chars = new CharacterInfo[font.Glyphs.Count]; int maxHeight = 0; Debug.Log(font.Glyphs.Count); for (int i = 0; i < font.Glyphs.Count; i++) { CharacterInfo inf = new CharacterInfo(); dfFont.GlyphDefinition glyphDefinition = font.Glyphs[i]; dfAtlas.ItemInfo itemInfo = font.Atlas[font.Sprite]; inf.glyphWidth = glyphDefinition.width; inf.glyphHeight = glyphDefinition.height; inf.size = (int)size.x; if (inf.glyphHeight > maxHeight) { maxHeight = inf.glyphHeight; } inf.index = glyphDefinition.id; float num9 = itemInfo.region.x + (float)glyphDefinition.x * f.material.mainTexture.texelSize.x; float num10 = itemInfo.region.yMax - (float)glyphDefinition.y * f.material.mainTexture.texelSize.y; float num11 = num9 + (float)glyphDefinition.width * f.material.mainTexture.texelSize.x; float num12 = num10 - (float)glyphDefinition.height * f.material.mainTexture.texelSize.y; inf.uvBottomLeft = (new Vector2(num9, num12)); inf.uvBottomRight = (new Vector2(num11, num12)); inf.uvTopRight = (new Vector2(num11, num10)); inf.uvTopLeft = (new Vector2(num9, num10)); inf.advance = glyphDefinition.xadvance; inf.minY = 0; inf.maxY = inf.glyphHeight; inf.minX = 0; inf.maxX = inf.glyphWidth; chars[i] = inf; } f.characterInfo = chars; return(f); }
public dfFont.GlyphDefinition GetGlyph(char id) { if (this.glyphMap == null) { this.glyphMap = new Dictionary <int, dfFont.GlyphDefinition>(); for (int i = 0; i < this.glyphs.Count; i++) { dfFont.GlyphDefinition item = this.glyphs[i]; this.glyphMap[item.id] = item; } } dfFont.GlyphDefinition glyphDefinition = null; this.glyphMap.TryGetValue(id, out glyphDefinition); return(glyphDefinition); }
public static Font DFFontToUnityFont(dfFont font, int scale = 1) { Font f = new Font(font.name); f.material = new Material(UnityEngine.GUI.skin.font.material); f.material.mainTexture = font.Texture; f.material.mainTexture.wrapMode = TextureWrapMode.Repeat; CharacterInfo[] chars = new CharacterInfo[font.Glyphs.Count]; for (int i = 0; i < chars.Length; i++) { CharacterInfo inf = new CharacterInfo(); dfFont.GlyphDefinition glyphDefinition = font.Glyphs[i]; dfAtlas.ItemInfo itemInfo = font.Atlas[font.Sprite]; inf.glyphWidth = glyphDefinition.width * scale; inf.glyphHeight = glyphDefinition.height * scale; inf.size = 0; // Must be 0 inf.index = glyphDefinition.id; float left = itemInfo.region.x + glyphDefinition.x * f.material.mainTexture.texelSize.x; float right = left + glyphDefinition.width * f.material.mainTexture.texelSize.x; float top = itemInfo.region.yMax - glyphDefinition.y * f.material.mainTexture.texelSize.y; float bottom = top - glyphDefinition.height * f.material.mainTexture.texelSize.y; inf.uvTopLeft = new Vector2(left, top); inf.uvTopRight = new Vector2(right, top); inf.uvBottomLeft = new Vector2(left, bottom); inf.uvBottomRight = new Vector2(right, bottom); inf.advance = glyphDefinition.xadvance * scale; inf.minY = scale * -glyphDefinition.height / 2; inf.maxY = scale * glyphDefinition.height / 2; chars[i] = inf; } f.characterInfo = chars; return(f); }
public static dfFont CreateFromFile( GameObject owner, string path ) { if( !File.Exists( path ) ) throw new FileNotFoundException( "Font definition file not found", path ); using( var file = File.OpenText( path ) ) { var fileData = file.ReadToEnd(); var font = owner.AddComponent<dfFont>(); var typePattern = new Regex( @"^(?<Type>char|info|common|page|kerning|chars)\s+.*", RegexOptions.ExplicitCapture ); var globalProperties = new Dictionary<string, string>(); var lines = fileData.Trim().Split( '\n' ); foreach( var line in lines ) { var typeMatch = typePattern.Match( line ); if( typeMatch == null || typeMatch.Groups[ "Type" ] == null ) continue; var type = typeMatch.Groups[ "Type" ].Value; if( type != "char" ) { if( type == "kerning" ) { var kprop = new Dictionary<string, string>(); parseProperties( kprop, line ); font.AddKerning( int.Parse( kprop[ "first" ] ), int.Parse( kprop[ "second" ] ), int.Parse( kprop[ "amount" ] ) ); } else { parseProperties( globalProperties, line ); } continue; } var glyph = new dfFont.GlyphDefinition(); var glyphProperties = new Dictionary<string, string>(); parseProperties( glyphProperties, line ); foreach( string key in glyphProperties.Keys ) { var value = glyphProperties[ key ]; var field = typeof( dfFont.GlyphDefinition ).GetField( key, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic ); if( field != null ) { var convertedValue = Convert.ChangeType( value, field.FieldType ); field.SetValue( glyph, convertedValue ); } } font.Glyphs.Add( glyph ); } if( globalProperties.Count == 0 ) { Debug.LogError( "This file does not appear to be a valid BMFont file" ); DestroyImmediate( font ); return null; } foreach( string key in globalProperties.Keys ) { var value = globalProperties[ key ]; var field = typeof( dfFont ).GetField( key, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic ); if( field == null ) continue; try { var convertedValue = (object)null; if( field.FieldType == typeof( bool ) ) { convertedValue = ( value != "0" ); } else if( !field.FieldType.IsArray ) { convertedValue = Convert.ChangeType( value, field.FieldType ); } else { var elementType = field.FieldType.GetElementType(); var elements = ( (string)value ).Split( ',' ); var array = Array.CreateInstance( elementType, elements.Length ); for( int i = 0; i < elements.Length; i++ ) { array.SetValue( Convert.ChangeType( elements[ i ], elementType ), i ); } convertedValue = array; } field.SetValue( font, convertedValue ); } catch( Exception err ) { var errmsg = string.Format( "Failed to set property {0} to value {1} - {2}", key, value, err.Message ); Debug.LogError( errmsg ); } } font.Glyphs.Sort(); return font; } }
public static dfFont CreateFromFile(GameObject owner, string path) { if (!File.Exists(path)) { throw new FileNotFoundException("Font definition file not found", path); } using (var file = File.OpenText(path)) { var fileData = file.ReadToEnd(); var font = owner.AddComponent <dfFont>(); var typePattern = new Regex(@"^(?<Type>char|info|common|page|kerning|chars)\s+.*", RegexOptions.ExplicitCapture); var globalProperties = new Dictionary <string, string>(); var lines = fileData.Trim().Split('\n'); foreach (var line in lines) { var typeMatch = typePattern.Match(line); if (typeMatch == null || typeMatch.Groups["Type"] == null) { continue; } var type = typeMatch.Groups["Type"].Value; if (type != "char") { if (type == "kerning") { var kprop = new Dictionary <string, string>(); parseProperties(kprop, line); font.AddKerning( int.Parse(kprop["first"]), int.Parse(kprop["second"]), int.Parse(kprop["amount"]) ); } else { parseProperties(globalProperties, line); } continue; } var glyph = new dfFont.GlyphDefinition(); var glyphProperties = new Dictionary <string, string>(); parseProperties(glyphProperties, line); foreach (string key in glyphProperties.Keys) { var value = glyphProperties[key]; var field = typeof(dfFont.GlyphDefinition).GetField(key, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic); if (field != null) { var convertedValue = Convert.ChangeType(value, field.FieldType); field.SetValue(glyph, convertedValue); } } font.Glyphs.Add(glyph); } if (globalProperties.Count == 0) { Debug.LogError("This file does not appear to be a valid BMFont file"); DestroyImmediate(font); return(null); } foreach (string key in globalProperties.Keys) { var value = globalProperties[key]; var field = typeof(dfFont).GetField(key, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic); if (field == null) { continue; } try { var convertedValue = (object)null; if (field.FieldType == typeof(bool)) { convertedValue = (value != "0"); } else if (!field.FieldType.IsArray) { convertedValue = Convert.ChangeType(value, field.FieldType); } else { var elementType = field.FieldType.GetElementType(); var elements = ((string)value).Split(','); var array = Array.CreateInstance(elementType, elements.Length); for (int i = 0; i < elements.Length; i++) { array.SetValue(Convert.ChangeType(elements[i], elementType), i); } convertedValue = array; } field.SetValue(font, convertedValue); } catch (Exception err) { var errmsg = string.Format("Failed to set property {0} to value {1} - {2}", key, value, err.Message); Debug.LogError(errmsg); } } font.Glyphs.Sort(); return(font); } }
private void renderText(dfMarkupToken token, Color32 color, Vector3 position, dfRenderData destination) { try { dfList <Vector3> vertices = destination.Vertices; dfList <int> triangles = destination.Triangles; dfList <Color32> colors = destination.Colors; dfList <Vector2> uV = destination.UV; dfFont font = (dfFont)base.Font; dfAtlas.ItemInfo info = font.Atlas[font.sprite]; Texture texture = font.Texture; float num = 1f / ((float)texture.width); float num2 = 1f / ((float)texture.height); float num3 = num * 0.125f; float num4 = num2 * 0.125f; float num5 = base.TextScale * base.PixelRatio; char previousChar = '\0'; char id = '\0'; Color32 item = this.applyOpacity(this.multiplyColors((Color)color, (Color)base.DefaultColor)); Color32 color3 = item; if (base.BottomColor.HasValue) { color3 = this.applyOpacity(this.multiplyColors((Color)color, base.BottomColor.Value)); } int num6 = 0; while (num6 < token.Length) { id = token[num6]; if (id != '\0') { dfFont.GlyphDefinition glyph = font.GetGlyph(id); if (glyph != null) { int kerning = font.GetKerning(previousChar, id); float x = position.x + ((glyph.xoffset + kerning) * num5); float y = position.y - (glyph.yoffset * num5); float num10 = glyph.width * num5; float num11 = glyph.height * num5; float num12 = x + num10; float num13 = y - num11; Vector3 vector = new Vector3(x, y); Vector3 vector2 = new Vector3(num12, y); Vector3 vector3 = new Vector3(num12, num13); Vector3 vector4 = new Vector3(x, num13); float num14 = (info.region.x + (glyph.x * num)) - num3; float num15 = (info.region.yMax - (glyph.y * num2)) - num4; float num16 = (num14 + (glyph.width * num)) - num3; float num17 = (num15 - (glyph.height * num2)) + num4; if (base.Shadow) { addTriangleIndices(vertices, triangles); Vector3 vector5 = (Vector3)(base.ShadowOffset * num5); vertices.Add(vector + vector5); vertices.Add(vector2 + vector5); vertices.Add(vector3 + vector5); vertices.Add(vector4 + vector5); Color32 color4 = this.applyOpacity(base.ShadowColor); colors.Add(color4); colors.Add(color4); colors.Add(color4); colors.Add(color4); uV.Add(new Vector2(num14, num15)); uV.Add(new Vector2(num16, num15)); uV.Add(new Vector2(num16, num17)); uV.Add(new Vector2(num14, num17)); } if (base.Outline) { for (int i = 0; i < OUTLINE_OFFSETS.Length; i++) { addTriangleIndices(vertices, triangles); Vector3 vector6 = (Vector3)((OUTLINE_OFFSETS[i] * base.OutlineSize) * num5); vertices.Add(vector + vector6); vertices.Add(vector2 + vector6); vertices.Add(vector3 + vector6); vertices.Add(vector4 + vector6); Color32 color5 = this.applyOpacity(base.OutlineColor); colors.Add(color5); colors.Add(color5); colors.Add(color5); colors.Add(color5); uV.Add(new Vector2(num14, num15)); uV.Add(new Vector2(num16, num15)); uV.Add(new Vector2(num16, num17)); uV.Add(new Vector2(num14, num17)); } } addTriangleIndices(vertices, triangles); vertices.Add(vector); vertices.Add(vector2); vertices.Add(vector3); vertices.Add(vector4); colors.Add(item); colors.Add(item); colors.Add(color3); colors.Add(color3); uV.Add(new Vector2(num14, num15)); uV.Add(new Vector2(num16, num15)); uV.Add(new Vector2(num16, num17)); uV.Add(new Vector2(num14, num17)); position.x += ((glyph.xadvance + kerning) + base.CharacterSpacing) * num5; } } num6++; previousChar = id; } } finally { } }
private void renderText(dfMarkupToken token, Color32 color, Vector3 position, dfRenderData destination) { try { dfList <Vector3> vertices = destination.Vertices; dfList <int> triangles = destination.Triangles; dfList <Color32> colors = destination.Colors; dfList <Vector2> uV = destination.UV; dfFont font = (dfFont)base.Font; dfAtlas.ItemInfo item = font.Atlas[font.sprite]; UnityEngine.Texture texture = font.Texture; float single = 1f / (float)texture.width; float single1 = 1f / (float)texture.height; float single2 = single * 0.125f; float single3 = single1 * 0.125f; float textScale = base.TextScale * base.PixelRatio; char chr = '\0'; char item1 = '\0'; Color32 color32 = this.applyOpacity(this.multiplyColors(color, base.DefaultColor)); Color32 color321 = color32; if (base.BottomColor.HasValue) { Color color1 = color; Color32?bottomColor = base.BottomColor; color321 = this.applyOpacity(this.multiplyColors(color1, bottomColor.Value)); } int num = 0; while (num < token.Length) { item1 = token[num]; if (item1 != 0) { dfFont.GlyphDefinition glyph = font.GetGlyph(item1); if (glyph != null) { int kerning = font.GetKerning(chr, item1); float single4 = position.x + (float)(glyph.xoffset + kerning) * textScale; float single5 = position.y - (float)glyph.yoffset * textScale; float single6 = (float)glyph.width * textScale; float single7 = (float)glyph.height * textScale; float single8 = single4 + single6; float single9 = single5 - single7; Vector3 vector3 = new Vector3(single4, single5); Vector3 vector31 = new Vector3(single8, single5); Vector3 vector32 = new Vector3(single8, single9); Vector3 vector33 = new Vector3(single4, single9); float single10 = item.region.x + (float)glyph.x * single - single2; float single11 = item.region.yMax - (float)glyph.y * single1 - single3; float single12 = single10 + (float)glyph.width * single - single2; float single13 = single11 - (float)glyph.height * single1 + single3; if (base.Shadow) { dfFont.BitmappedFontRenderer.addTriangleIndices(vertices, triangles); Vector3 shadowOffset = base.ShadowOffset * textScale; vertices.Add(vector3 + shadowOffset); vertices.Add(vector31 + shadowOffset); vertices.Add(vector32 + shadowOffset); vertices.Add(vector33 + shadowOffset); Color32 color322 = this.applyOpacity(base.ShadowColor); colors.Add(color322); colors.Add(color322); colors.Add(color322); colors.Add(color322); uV.Add(new Vector2(single10, single11)); uV.Add(new Vector2(single12, single11)); uV.Add(new Vector2(single12, single13)); uV.Add(new Vector2(single10, single13)); } if (base.Outline) { for (int i = 0; i < (int)dfFont.BitmappedFontRenderer.OUTLINE_OFFSETS.Length; i++) { dfFont.BitmappedFontRenderer.addTriangleIndices(vertices, triangles); Vector3 oUTLINEOFFSETS = (dfFont.BitmappedFontRenderer.OUTLINE_OFFSETS[i] * (float)base.OutlineSize) * textScale; vertices.Add(vector3 + oUTLINEOFFSETS); vertices.Add(vector31 + oUTLINEOFFSETS); vertices.Add(vector32 + oUTLINEOFFSETS); vertices.Add(vector33 + oUTLINEOFFSETS); Color32 color323 = this.applyOpacity(base.OutlineColor); colors.Add(color323); colors.Add(color323); colors.Add(color323); colors.Add(color323); uV.Add(new Vector2(single10, single11)); uV.Add(new Vector2(single12, single11)); uV.Add(new Vector2(single12, single13)); uV.Add(new Vector2(single10, single13)); } } dfFont.BitmappedFontRenderer.addTriangleIndices(vertices, triangles); vertices.Add(vector3); vertices.Add(vector31); vertices.Add(vector32); vertices.Add(vector33); colors.Add(color32); colors.Add(color32); colors.Add(color321); colors.Add(color321); uV.Add(new Vector2(single10, single11)); uV.Add(new Vector2(single12, single11)); uV.Add(new Vector2(single12, single13)); uV.Add(new Vector2(single10, single13)); position.x = position.x + (float)(glyph.xadvance + kerning + base.CharacterSpacing) * textScale; } } num++; chr = item1; } } finally { } }