private static string MakeFontTag(EffectAnimationPart part, ref int startTagsCount) { var attributes = string.Empty; if (part.Color != NullColor) { attributes = $" color=\"{ToHtml(part.Color)}\""; } if (!string.IsNullOrEmpty(part.Face)) { attributes += $" face=\"{part.Face}\""; } if (!string.IsNullOrEmpty(part.Size)) { attributes += $" size=\"{part.Size}\""; } var result = $"<font{attributes.TrimEnd()}>".Replace("<font>", string.Empty); if (result.Length > 0) { startTagsCount++; } return(result); }
public static List <EffectAnimationPart> MakeBase(string text) { var i = 0; var allowedStartTags = new List <string> { "<i>", "<u>", "<b>", "<font ", "<span " }; var allowedEndTags = new List <string> { "</i>", "</u>", "</b>", "</font>", "</span>" }; var list = new List <EffectAnimationPart>(); EffectAnimationPart p = null; var color = NullColor; var face = string.Empty; var size = string.Empty; var fontStack = new Stack <EffectAnimationPart>(); while (i < text.Length) { var c = text[i]; if (c == '{' && text.Substring(i).StartsWith("{\\") && text.IndexOf('}', i) > 0) { var endIndex = text.IndexOf('}', i); var tag = text.Substring(i, endIndex - i + 1); i += tag.Length; if (p == null) { p = new EffectAnimationPart { Color = color, Face = face, Size = size }; } else if (!string.IsNullOrEmpty(p.Text)) { list.Add(p); p = new EffectAnimationPart { Color = color, Face = face, Size = size }; } p.Pre += tag; } else if (c == '<' && allowedStartTags.Any(a => text.Substring(i).StartsWith(a, StringComparison.OrdinalIgnoreCase)) && text.IndexOf('>', i) > 0) { var endIndex = text.IndexOf('>', i); var tag = text.Substring(i, endIndex - i + 1); i += tag.Length; if (p == null) { p = new EffectAnimationPart { Color = color, Face = face, Size = size }; } else if (!string.IsNullOrEmpty(p.Text)) { list.Add(p); p = new EffectAnimationPart { Color = color, Face = face, Size = size }; } if (tag.StartsWith("<font", StringComparison.OrdinalIgnoreCase)) { fontStack.Push(new EffectAnimationPart { Color = color, Face = face, Size = size }); // get color var tagColor = GetColorFromFontString(tag + "a</font>", NullColor); if (tagColor != NullColor) { p.Color = tagColor; color = tagColor; } // get font name (face) var fontName = GetAttributeFromFontString(tag, "face"); if (!string.IsNullOrEmpty(fontName)) { p.Face = fontName; face = fontName; } tag = string.Empty; } p.Pre += tag; } else if (c == '<' && allowedEndTags.Any(a => text.Substring(i).StartsWith(a, StringComparison.OrdinalIgnoreCase)) && text.IndexOf('>', i) > 0) { var endIndex = text.IndexOf('>', i); var tag = text.Substring(i, endIndex - i + 1); i += tag.Length; if (p == null) { p = new EffectAnimationPart { Color = color, Face = face, Size = size }; } // set color if (tag == "</font>") { if (fontStack.Count > 0) { var f = fontStack.Pop(); color = f.Color; face = f.Face; size = f.Size; } else { color = NullColor; face = string.Empty; size = string.Empty; } } tag = string.Empty; p.Post += tag; } else if (c == '\r') { if (p == null) { p = new EffectAnimationPart { Color = color, Face = face, Size = size }; } else if (!string.IsNullOrEmpty(p.Text)) { list.Add(p); p = new EffectAnimationPart { Color = color, Face = face, Size = size }; } p.Text = c.ToString(); i++; if (i < text.Length && text[i] == '\n') // only take one part for \r\n { p.Text += '\n'; i++; } } else { if (p == null) { p = new EffectAnimationPart { Color = color, Face = face, Size = size }; } else if (!string.IsNullOrEmpty(p.Text)) { list.Add(p); p = new EffectAnimationPart { Color = color, Face = face, Size = size }; } p.Text = c.ToString(); i++; } } if (p != null && !string.IsNullOrEmpty(p.Pre + p.Text + p.Post)) { list.Add(p); } return(list); }