static RichTextTextNode CopyAttributes(RichTextTextNode src) { return(new RichTextTextNode() { Bold = src.Bold, Italic = src.Italic, Underline = src.Underline, FontName = src.FontName, FontSize = src.FontSize, Color = src.Color, Alignment = src.Alignment }); }
void DrawText(UiContext context, RectangleF myRect) { var sizeF = context.TextSize(FontSize); var node0 = new RichTextTextNode() { Contents = CurrentEntry, FontName = "Arial", FontSize = sizeF, Color = Color4.Green, Shadow = new TextShadow(Color4.Black) }; var node1 = new RichTextTextNode() { Contents = CurrentText, FontName = "Arial", FontSize = sizeF, Shadow = new TextShadow(Color4.Black) }; var rtf = context.RenderContext.Renderer2D.CreateRichTextEngine(); var rect = context.PointsToPixels(myRect); var built = rtf.BuildText(new[] { node0, node1 }, (int)rect.Width - 4, 1f); context.RenderContext.ScissorEnabled = true; context.RenderContext.ScissorRectangle = rect; rtf.RenderText(built, (int)rect.X + 2, (int)rect.Y + 2); context.RenderContext.ScissorEnabled = false; built.Dispose(); }
static void ParseTextRenderAttributes(Dictionary <string, string> attrs, RichTextTextNode node, FontManager fonts, int defaultFont) { uint data = 0; uint mask = 0; uint def = 0; string temp; if (attrs.TryGetValue("DATA", out temp)) { data = (uint)int.Parse(temp); } if (attrs.TryGetValue("MASK", out temp)) { mask = (uint)int.Parse(temp); } if (attrs.TryGetValue("DEF", out temp)) { def = (uint)int.Parse(temp); } if (attrs.TryGetValue("COLOR", out temp)) { mask |= TRA_color; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_color; } else { data &= ~TRA_color; data |= GetColor(temp); } } if (attrs.TryGetValue("FONT", out temp)) { mask |= TRA_font; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_font; } else { data &= ~TRA_font; data |= (uint.Parse(temp)) << 3; } } if (attrs.TryGetValue("BOLD", out temp)) { mask |= TRA_bold; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_bold; } else if (temp.ToUpperInvariant() == "TRUE") { data |= TRA_bold; } else { data &= ~TRA_bold; } } if (attrs.TryGetValue("ITALIC", out temp)) { mask |= TRA_italic; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_italic; } else if (temp.ToUpperInvariant() == "TRUE") { data |= TRA_italic; } else { data &= ~TRA_italic; } } if (attrs.TryGetValue("UNDERLINE", out temp)) { mask |= TRA_underline; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_underline; } else if (temp.ToUpperInvariant() == "TRUE") { data |= TRA_underline; } else { data &= ~TRA_underline; } } if ((def & TRA_bold) != 0) { node.Bold = false; } else if ((mask & TRA_bold) != 0) { node.Bold = (data & TRA_bold) != 0; } if ((def & TRA_italic) != 0) { node.Italic = false; } else if ((mask & TRA_italic) != 0) { node.Italic = (data & TRA_italic) != 0; } if ((def & TRA_font) != 0) { var d = fonts.GetInfocardFont(defaultFont); node.FontName = d.FontName; node.FontSize = d.FontSize; //node.FontIndex = 0; } else if ((mask & TRA_font) != 0) { int fnt = (int)((data & TRA_font) >> 3); var d = fonts.GetInfocardFont(fnt); node.FontName = d.FontName; node.FontSize = d.FontSize; } if ((def & TRA_underline) != 0) { node.Underline = false; } else if ((mask & TRA_underline) != 0) { node.Underline = (data & TRA_underline) != 0; } if ((def & TRA_color) != 0) { node.Color = Color4.White; } else if ((mask & TRA_color) != 0) { var bytes = BitConverter.GetBytes((data & TRA_color)); node.Color = new Color4((float)bytes[1] / 255f, (float)bytes[2] / 255f, (float)bytes[3] / 255f, 1f); } }
static Infocard ParseInternal(string input, FontManager fonts, int defaultFont) { if (input == null) { return new Infocard() { Nodes = new List <RichTextNode>() { new RichTextTextNode() { Contents = "IDS??" } } } } ; var nodes = new List <RichTextNode>(); var current = new RichTextTextNode(); var fn = fonts.GetInfocardFont(defaultFont); //default font current.FontName = fn.FontName; current.FontSize = fn.FontSize; using (var reader = XmlReader.Create(new StringReader(input))) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: string elemname = reader.Name.ToUpperInvariant(); if (SkipElements.Contains(elemname)) { continue; } Dictionary <string, string> attrs = null; if (reader.HasAttributes) { attrs = new Dictionary <string, string>(); for (int attInd = 0; attInd < reader.AttributeCount; attInd++) { reader.MoveToAttribute(attInd); attrs.Add(reader.Name.ToUpper(), reader.Value); } reader.MoveToElement(); } switch (elemname) { case "PARA": nodes.Add(new RichTextParagraphNode()); break; case "JUST": TextAlignment v; if (Aligns.TryGetValue(attrs["LOC"].ToUpperInvariant(), out v)) { current.Alignment = v; } break; case "TRA": ParseTextRenderAttributes(attrs, current, fonts, defaultFont); break; case "TEXT": break; default: throw new Exception("Unexpected element " + elemname); } break; case XmlNodeType.Text: current.Contents = reader.Value; nodes.Add(current); current = CopyAttributes(current); break; } } } return(new Infocard() { Nodes = nodes }); }