From 9643cc6974e3a53ad8ee6e31220f2ac33f224493 Mon Sep 17 00:00:00 2001 From: Dan Backes Date: Thu, 17 Jul 2014 13:40:48 -0500 Subject: [PATCH] Colors Fix, Perf Improvement, Thread Safety -Fixes issues with colors not parsing correctly. Saving an SvgDocument resulted in RGB color values being converted to well known color names. -Compile regex statements to improve performance. -Make rendering a document threadsafe. --- Source/Painting/SvgColourConverter.cs | 2 +- Source/Paths/SvgPathBuilder.cs | 2 +- Source/SvgElementFactory.cs | 26 +++++++++++++++----------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Source/Painting/SvgColourConverter.cs b/Source/Painting/SvgColourConverter.cs index 278f9d7..e36d2cf 100644 --- a/Source/Painting/SvgColourConverter.cs +++ b/Source/Painting/SvgColourConverter.cs @@ -115,7 +115,7 @@ namespace Svg if (destinationType == typeof(string)) { var colour = (Color)value; - return ColorTranslator.ToHtml(colour); + return "#" + colour.R.ToString("X2", null) + colour.G.ToString("X2", null) + colour.B.ToString("X2", null); } return base.ConvertTo(context, culture, value, destinationType); diff --git a/Source/Paths/SvgPathBuilder.cs b/Source/Paths/SvgPathBuilder.cs index 793261e..7595c91 100644 --- a/Source/Paths/SvgPathBuilder.cs +++ b/Source/Paths/SvgPathBuilder.cs @@ -276,7 +276,7 @@ namespace Svg private static IEnumerable ParseCoordinates(string coords) { - var parts = Regex.Split(coords.Remove(0, 1), @"[\s,]|(?=(?> _propertyDescriptors = new Dictionary>(); + private static object syncLock = new object(); private static void SetPropertyValue(SvgElement element, string attributeName, string attributeValue, SvgDocument document) { var elementType = element.GetType(); PropertyDescriptorCollection properties; - if (_propertyDescriptors.Keys.Contains(elementType)) + lock (syncLock) { - if (_propertyDescriptors[elementType].Keys.Contains(attributeName)) + if (_propertyDescriptors.Keys.Contains(elementType)) { - properties = _propertyDescriptors[elementType][attributeName]; + if (_propertyDescriptors[elementType].Keys.Contains(attributeName)) + { + properties = _propertyDescriptors[elementType][attributeName]; + } + else + { + properties = TypeDescriptor.GetProperties(elementType, new[] { new SvgAttributeAttribute(attributeName) }); + _propertyDescriptors[elementType].Add(attributeName, properties); + } } else { properties = TypeDescriptor.GetProperties(elementType, new[] { new SvgAttributeAttribute(attributeName) }); - _propertyDescriptors[elementType].Add(attributeName, properties); - } - } - else - { - properties = TypeDescriptor.GetProperties(elementType, new[] { new SvgAttributeAttribute(attributeName) }); - _propertyDescriptors.Add(elementType, new Dictionary()); + _propertyDescriptors.Add(elementType, new Dictionary()); - _propertyDescriptors[elementType].Add(attributeName, properties); + _propertyDescriptors[elementType].Add(attributeName, properties); + } } if (properties.Count > 0) -- GitLab