Example #1
0
        /// <summary>
        /// Get a title for a specified svg element.
        /// </summary>
        private static string GetLayerTitle(SvgElement element, int maxLength = 32)
        {
            string elementName = element.GetName();
            string layerName   = null;

            if (element.ID != null)
            {
                layerName = element.ID;
            }

            if (string.IsNullOrEmpty(layerName) && element.CustomAttributes != null)
            {
                // get custom title attributes.
                foreach (var titleAttribute in AllowedTitles)
                {
                    if (element.CustomAttributes.TryGetValue(titleAttribute, out string title))
                    {
                        if (!string.IsNullOrEmpty(title))
                        {
                            layerName = title;
                            break;
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(layerName) && element.Children != null)
            {
                // Get child title tag
                SvgTitle title = element.Children.OfType <SvgTitle>().FirstOrDefault();
                if (title != null && !string.IsNullOrEmpty(title.Content))
                {
                    layerName = title.Content;
                }
            }

            if (string.IsNullOrEmpty(layerName))
            {
                // Generate more meanfull name for a svg use node. Add reference element name in a case if it's local document.
                if (element is SvgUse useElement &&
                    useElement.ReferencedElement != null &&
                    !string.IsNullOrEmpty(useElement.ReferencedElement.OriginalString))
                {
                    string str = useElement.ReferencedElement.OriginalString.Trim();
                    if (str.StartsWith("#"))
                    {
                        layerName = str.Substring(1);
                    }
                }
                // Generate more meanfull name for a svg text.
                else if (element is SvgTextBase text && !string.IsNullOrEmpty(text.Text))
                {
                    layerName = text.Text;
                    if (text.Text.Length > maxLength)
                    {
                        layerName = text.Text.Substring(0, maxLength) + "...";
                    }
                }