示例#1
0
        public WpfDrawingContext(bool isFragment, WpfDrawingSettings settings)
        {
            var sysParam = typeof(SystemParameters);

            var dpiXProperty = sysParam.GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
            var dpiYProperty = sysParam.GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);

            _dpiX = (int)dpiXProperty.GetValue(null, null);
            _dpiY = (int)dpiYProperty.GetValue(null, null);

            if (settings == null)
            {
                settings = new WpfDrawingSettings();
            }
            _quickBounds   = Rect.Empty;
            _isFragment    = isFragment;
            _settings      = settings;
            _drawStack     = new Stack <DrawingGroup>();
            _paintContexts = new Dictionary <string, WpfSvgPaintContext>(StringComparer.Ordinal);
            _baseUrls      = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            _registeredIds = settings[RegisteredIdKey] as HashSet <string>;
            if (_registeredIds == null)
            {
                _registeredIds            = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                settings[RegisteredIdKey] = _registeredIds;
            }

            var visitors = settings.Visitors;

            if (visitors != null)
            {
                WpfLinkVisitor linkVisitor = visitors.LinkVisitor;
                if (linkVisitor != null)
                {
                    _linkVisitor = linkVisitor;
                }
                WpfFontFamilyVisitor fontFamilyVisitor = visitors.FontFamilyVisitor;
                if (fontFamilyVisitor != null)
                {
                    _fontFamilyVisitor = fontFamilyVisitor;
                }
                WpfEmbeddedImageVisitor imageVisitor = visitors.ImageVisitor;
                if (imageVisitor != null)
                {
                    _imageVisitor = imageVisitor;
                }
                WpfIDVisitor idVisitor = visitors.IDVisitor;
                if (idVisitor != null)
                {
                    _idVisitor = idVisitor;
                }
                WpfClassVisitor classVisitor = visitors.ClassVisitor;
                if (classVisitor != null)
                {
                    _classVisitor = classVisitor;
                }
            }
        }
示例#2
0
        public void Initialize(WpfLinkVisitor linkVisitor, WpfFontFamilyVisitor fontFamilyVisitor,
                               WpfEmbeddedImageVisitor imageVisitor)
        {
            //TODO: Depreciate this operation as we have more visitors...
            if (linkVisitor != null)
            {
                _linkVisitor = linkVisitor;
            }
            if (fontFamilyVisitor != null)
            {
                _fontFamilyVisitor = fontFamilyVisitor;
            }
            if (imageVisitor != null)
            {
                _imageVisitor = imageVisitor;
            }

            _rootDrawing = new DrawingGroup();

            this.Push(_rootDrawing);


            if (_idVisitor != null && !_idVisitor.IsInitialized)
            {
                _idVisitor.Initialize(this);
            }
            if (_linkVisitor != null && !_linkVisitor.IsInitialized)
            {
                _linkVisitor.Initialize(this);
            }
            if (_classVisitor != null && !_classVisitor.IsInitialized)
            {
                _classVisitor.Initialize(this);
            }
            if (_fontFamilyVisitor != null && !_fontFamilyVisitor.IsInitialized)
            {
                _fontFamilyVisitor.Initialize(this);
            }
            if (_imageVisitor != null && !_imageVisitor.IsInitialized)
            {
                _imageVisitor.Initialize(this);
            }
            if (_linkVisitor != null && _linkVisitor.Aggregates)
            {
                _linkDrawing = new DrawingGroup();

                string groupId = _linkVisitor.AggregatedLayerName;
                if (!string.IsNullOrWhiteSpace(groupId))
                {
                    Runtime.SvgObject.SetName(_linkDrawing, groupId);
                }

                _linkVisitor.Initialize(_linkDrawing, this);
            }
        }
示例#3
0
        public WpfDrawingContext(bool isFragment, WpfDrawingSettings settings)
        {
            if (settings == null)
            {
                settings = new WpfDrawingSettings();
            }
            _quickBounds   = Rect.Empty;
            _isFragment    = isFragment;
            _settings      = settings;
            _drawStack     = new Stack <DrawingGroup>();
            _paintContexts = new Dictionary <string, WpfSvgPaintContext>(StringComparer.Ordinal);

            _registeredIds = settings[RegisteredIdKey] as HashSet <string>;
            if (_registeredIds == null)
            {
                _registeredIds            = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                settings[RegisteredIdKey] = _registeredIds;
            }

            var visitors = settings.Visitors;

            if (visitors != null)
            {
                WpfLinkVisitor linkVisitor = visitors.LinkVisitor;
                if (linkVisitor != null)
                {
                    _linkVisitor = linkVisitor;
                }
                WpfFontFamilyVisitor fontFamilyVisitor = visitors.FontFamilyVisitor;
                if (fontFamilyVisitor != null)
                {
                    _fontFamilyVisitor = fontFamilyVisitor;
                }
                WpfEmbeddedImageVisitor imageVisitor = visitors.ImageVisitor;
                if (imageVisitor != null)
                {
                    _imageVisitor = imageVisitor;
                }
                WpfIDVisitor idVisitor = visitors.IDVisitor;
                if (idVisitor != null)
                {
                    _idVisitor = idVisitor;
                }
                WpfClassVisitor classVisitor = visitors.ClassVisitor;
                if (classVisitor != null)
                {
                    _classVisitor = classVisitor;
                }
            }
        }
        public void Initialize(WpfLinkVisitor linkVisitor, WpfFontFamilyVisitor fontFamilyVisitor,
                               WpfEmbeddedImageVisitor imageVisitor)
        {
            _linkVisitor       = linkVisitor;
            _fontFamilyVisitor = fontFamilyVisitor;
            _imageVisitor      = imageVisitor;

            _rootDrawing = new DrawingGroup();

            this.Push(_rootDrawing);

            if (_linkVisitor != null && _linkVisitor.Aggregates)
            {
                _linkDrawing = new DrawingGroup();

                string groupId = _linkVisitor.AggregatedLayerName;
                if (!string.IsNullOrWhiteSpace(groupId))
                {
                    Runtime.SvgObject.SetName(_linkDrawing, groupId);
                }

                linkVisitor.Initialize(_linkDrawing, this);
            }
        }
示例#5
0
        public void Initialize(WpfLinkVisitor linkVisitor, WpfFontFamilyVisitor fontFamilyVisitor,
                               WpfEmbeddedImageVisitor imageVisitor)
        {
            _linkVisitor       = linkVisitor;
            _fontFamilyVisitor = fontFamilyVisitor;
            _imageVisitor      = imageVisitor;

            _rootDrawing = new DrawingGroup();

            this.Push(_rootDrawing);

            if (_linkVisitor != null && _linkVisitor.Aggregates)
            {
                _linkDrawing = new DrawingGroup();

                string groupId = _linkVisitor.AggregatedLayerName;
                if (!string.IsNullOrWhiteSpace(groupId))
                {
                    _linkDrawing.SetValue(FrameworkElement.NameProperty, groupId);
                }

                linkVisitor.Initialize(_linkDrawing, this);
            }
        }
        private ImageSource GetBitmap(SvgImageElement element, WpfDrawingContext context)
        {
            if (element.IsSvgImage)
            {
                return(null);
            }

            if (element.Href == null)
            {
                return(null);
            }

            if (!element.Href.AnimVal.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
            {
                SvgUriReference svgUri      = element.UriReference;
                string          absoluteUri = svgUri.AbsoluteUri;
                if (string.IsNullOrWhiteSpace(absoluteUri))
                {
                    return(null); // most likely, the image does not exist...
                }
                if (absoluteUri.StartsWith("#", StringComparison.OrdinalIgnoreCase))
                {
                    Trace.WriteLine("Uri: " + absoluteUri); // image elements can't reference elements in an svg file
                    return(null);
                }

                Uri imageUri = new Uri(svgUri.AbsoluteUri);
                if (imageUri.IsFile)
                {
                    if (File.Exists(imageUri.LocalPath))
                    {
                        BitmapImage imageSource = new BitmapImage();

                        imageSource.BeginInit();
                        imageSource.CacheOption   = BitmapCacheOption.OnLoad;
                        imageSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache
                                                    | BitmapCreateOptions.PreservePixelFormat;
                        imageSource.UriSource = imageUri;
                        imageSource.EndInit();

//                        imageSource.Freeze();

                        return(imageSource);
                    }

                    return(null);
                }
                else
                {
                    Stream stream = svgUri.ReferencedResource.GetResponseStream();

                    BitmapImage imageSource = new BitmapImage();
                    imageSource.BeginInit();
                    imageSource.CacheOption   = BitmapCacheOption.OnLoad;
                    imageSource.CreateOptions = BitmapCreateOptions.IgnoreImageCache
                                                | BitmapCreateOptions.PreservePixelFormat;
                    imageSource.StreamSource = stream;
                    imageSource.EndInit();

//                    imageSource.Freeze();

                    return(imageSource);
                }
            }
            else
            {
                WpfEmbeddedImageVisitor imageVisitor = context.ImageVisitor;
                if (imageVisitor != null)
                {
                    ImageSource visitorSource = imageVisitor.Visit(element, context);
                    if (visitorSource != null)
                    {
                        return(visitorSource);
                    }
                }

                string sURI       = element.Href.AnimVal.Replace(" ", "");
                int    nColon     = sURI.IndexOf(":", StringComparison.OrdinalIgnoreCase);
                int    nSemiColon = sURI.IndexOf(";", StringComparison.OrdinalIgnoreCase);
                int    nComma     = sURI.IndexOf(",", StringComparison.OrdinalIgnoreCase);

                string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1);

                string sContent = sURI.Substring(nComma + 1);
                byte[] bResult  = Convert.FromBase64CharArray(sContent.ToCharArray(),
                                                              0, sContent.Length);

                BitmapImage imageSource = new BitmapImage();
                imageSource.BeginInit();
                imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                imageSource.StreamSource  = new MemoryStream(bResult);
                imageSource.EndInit();

//                imageSource.Freeze();

                return(imageSource);
            }
        }
示例#7
0
        private BitmapSource GetBitmap(SvgImageElement element, WpfDrawingContext context)
        {
            if (element.IsSvgImage)
            {
                return(null);
            }

            if (!element.Href.AnimVal.StartsWith("data:"))
            {
                SvgUriReference svgUri      = element.UriReference;
                string          absoluteUri = svgUri.AbsoluteUri;
                if (String.IsNullOrEmpty(absoluteUri))
                {
                    return(null); // most likely, the image does not exist...
                }

                Uri imageUri = new Uri(svgUri.AbsoluteUri);
                if (imageUri.IsFile)
                {
                    if (File.Exists(imageUri.LocalPath))
                    {
                        BitmapImage imageSource = new BitmapImage();

                        imageSource.BeginInit();
                        imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                        imageSource.UriSource     = imageUri;
                        imageSource.EndInit();

                        return(imageSource);
                    }

                    return(null);
                }
                else
                {
                    Stream stream = svgUri.ReferencedResource.GetResponseStream();

                    BitmapImage imageSource = new BitmapImage();
                    imageSource.BeginInit();
                    imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                    imageSource.StreamSource  = stream;
                    imageSource.EndInit();

                    return(imageSource);
                }
            }
            else
            {
                WpfEmbeddedImageVisitor imageVisitor = context.ImageVisitor;
                if (imageVisitor != null)
                {
                    BitmapSource visitorSource = imageVisitor.Visit(element, context);
                    if (visitorSource != null)
                    {
                        return(visitorSource);
                    }
                }

                string sURI       = element.Href.AnimVal;
                int    nColon     = sURI.IndexOf(":");
                int    nSemiColon = sURI.IndexOf(";");
                int    nComma     = sURI.IndexOf(",");

                string sMimeType = sURI.Substring(nColon + 1, nSemiColon - nColon - 1);

                string sContent = sURI.Substring(nComma + 1);
                byte[] bResult  = Convert.FromBase64CharArray(sContent.ToCharArray(),
                                                              0, sContent.Length);

                BitmapImage imageSource = new BitmapImage();
                imageSource.BeginInit();
                imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
                imageSource.StreamSource  = new MemoryStream(bResult);
                imageSource.EndInit();

                return(imageSource);
            }
        }