Esempio n. 1
0
        protected virtual bool ReadTextLocation(XElement element, XmlRenderText command)
        {
            if (!element.GetAttribute("x", logger, out var x) ||
                !element.GetAttribute("y", logger, out var y))
            {
                return(false);
            }

            if (!componentPointParser.TryParse(x, y, out var location))
            {
                return(false);
            }
            command.Location = location;

            return(true);
        }
        public bool ReadRenderCommand(XElement element, ComponentDescription description, out IXmlRenderCommand command)
        {
            var textCommand = new XmlRenderText();

            command = textCommand;

            if (!ReadTextLocation(element, textCommand))
            {
                return(false);
            }

            string tAlignment = "TopLeft";

            if (element.Attribute("align") != null)
            {
                tAlignment = element.Attribute("align").Value;
            }

            if (!Enum.TryParse(tAlignment, out TextAlignment alignment))
            {
                logger.LogError(element.Attribute("align"), $"Invalid value for text alignment: '{tAlignment}'");
                return(false);
            }
            textCommand.Alignment = alignment;

            var tRotation = "0";

            if (description.Metadata.FormatVersion >= TextRotationMinFormatVersion && element.Attribute("rotate") != null)
            {
                tRotation = element.Attribute("rotate").Value;
            }

            var rotation = TextRotation.None;

            switch (tRotation)
            {
            case "0":
                rotation = TextRotation.None;
                break;

            case "90":
                rotation = TextRotation.Rotate90;
                break;

            case "180":
                rotation = TextRotation.Rotate180;
                break;

            case "270":
                rotation = TextRotation.Rotate270;
                break;

            default:
                logger.LogError(element.Attribute("rotate"), $"Invalid value for text rotation: '{tRotation}'");
                break;
            }
            textCommand.Rotation = rotation;

            double size = 11d;

            if (element.Attribute("size") != null)
            {
                if (element.Attribute("size").Value.ToLowerInvariant() == "large")
                {
                    size = 12d;
                }
            }

            var textValueNode = element.Element(XmlLoader.ComponentNamespace + "value");

            if (textValueNode != null)
            {
                foreach (var spanNode in textValueNode.Elements())
                {
                    string nodeValue  = spanNode.Value;
                    var    formatting = TextRunFormatting.Normal;

                    if (spanNode.Name.LocalName == "sub")
                    {
                        formatting = TextRunFormatting.Subscript;
                    }
                    else if (spanNode.Name.LocalName == "sup")
                    {
                        formatting = TextRunFormatting.Superscript;
                    }
                    else if (spanNode.Name.LocalName != "span")
                    {
                        logger.LogWarning(spanNode, $"Unknown node '{spanNode.Name}' will be treated as <span>");
                    }

                    var textRun = new TextRun(nodeValue, formatting);

                    if (!ValidateText(element, description, textRun.Text))
                    {
                        return(false);
                    }

                    textCommand.TextRuns.Add(textRun);
                }
            }
            else if (element.GetAttribute("value", logger, out var value))
            {
                var textRun = new TextRun(value.Value, new TextRunFormatting(TextRunFormattingType.Normal, size));

                if (!ValidateText(value, description, textRun.Text))
                {
                    return(false);
                }

                textCommand.TextRuns.Add(textRun);
            }
            else
            {
                return(false);
            }

            return(true);
        }