Example #1
0
        public Pen GetPen(Geometry geometry)
        {
            double strokeWidth = GetStrokeWidth();
            if (strokeWidth == 0)
                return null;

            WpfSvgPaint stroke;
            if (PaintType == SvgPaintType.None)
            {
                return null;
            }
            else if (PaintType == SvgPaintType.CurrentColor)
            {
                stroke = new WpfSvgPaint(_context, _element, "color");
            }
            else
            {
                stroke = this;
            }

            Pen pen = new Pen(stroke.GetBrush(geometry, "stroke", true),
                strokeWidth);

            pen.StartLineCap  = pen.EndLineCap = GetLineCap();
            pen.LineJoin      = GetLineJoin();
            double miterLimit = GetMiterLimit(strokeWidth);
            if (miterLimit > 0)
            {
                pen.MiterLimit = miterLimit;
            }

            //pen.MiterLimit = 1.0f;

            DoubleCollection dashArray = GetDashArray(strokeWidth);
            if (dashArray != null && dashArray.Count != 0)
            {
                bool isValidDashes = true;
                //Do not draw if dash array had a zero value in it
                for (int i = 0; i < dashArray.Count; i++)
                {
                    if (dashArray[i] == 0)
                    {
                        isValidDashes = false;
                    }
                }

                if (isValidDashes)
                {
                    DashStyle dashStyle = new DashStyle(dashArray, GetDashOffset(strokeWidth));

                    pen.DashStyle = dashStyle;
                    // This is the one that works well for the XAML, the default is not Flat as
                    // stated in the documentations...
                    pen.DashCap   = PenLineCap.Flat;
                }
            }

            return pen;
        }
        private void DrawTextRun(SvgTextContentElement element, ref Point ctp,
            WpfTextRun textRun, double rotate, WpfTextPlacement placement)
        {
            if (textRun == null || textRun.IsEmpty)
                return;

            string text           = textRun.Text;
            double emSize         = GetComputedFontSize(element);
            FontFamily fontFamily = GetTextFontFamily(element, emSize);

            FontStyle fontStyle   = GetTextFontStyle(element);
            FontWeight fontWeight = GetTextFontWeight(element);

            FontStretch fontStretch = GetTextFontStretch(element);

            WpfTextStringFormat stringFormat = GetTextStringFormat(element);

            // Fix the use of Postscript fonts...
            WpfFontFamilyVisitor fontFamilyVisitor = _drawContext.FontFamilyVisitor;
            if (!String.IsNullOrEmpty(_actualFontName) && fontFamilyVisitor != null)
            {
                WpfFontFamilyInfo currentFamily = new WpfFontFamilyInfo(fontFamily, fontWeight,
                    fontStyle, fontStretch);
                WpfFontFamilyInfo familyInfo = fontFamilyVisitor.Visit(_actualFontName,
                    currentFamily, _drawContext);
                if (familyInfo != null && !familyInfo.IsEmpty)
                {
                    fontFamily  = familyInfo.Family;
                    fontWeight  = familyInfo.Weight;
                    fontStyle   = familyInfo.Style;
                    fontStretch = familyInfo.Stretch;
                }
            }

            WpfSvgPaint fillPaint = new WpfSvgPaint(_drawContext, element, "fill");
            Brush textBrush       = fillPaint.GetBrush();

            WpfSvgPaint strokePaint = new WpfSvgPaint(_drawContext, element, "stroke");
            Pen textPen = strokePaint.GetPen();

            if (textBrush == null && textPen == null)
            {
                return;
            }
            else if (textBrush == null)
            {
                // If here, then the pen is not null, and so the fill cannot be null.
                // We set this to transparent for stroke only text path...
                textBrush = Brushes.Transparent;
            }

            TextDecorationCollection textDecors = GetTextDecoration(element);
            TextAlignment alignment = stringFormat.Alignment;

            string letterSpacing = element.GetAttribute("letter-spacing");
            if (String.IsNullOrEmpty(letterSpacing))
            {
                FormattedText formattedText = new FormattedText(text,
                    textRun.IsLatin ? _drawContext.EnglishCultureInfo : _drawContext.CultureInfo,
                    stringFormat.Direction, new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
                    emSize, textBrush);

                formattedText.TextAlignment = stringFormat.Alignment;
                formattedText.Trimming      = stringFormat.Trimming;

                if (textDecors != null && textDecors.Count != 0)
                {
                    formattedText.SetTextDecorations(textDecors);
                }

                //float xCorrection = 0;
                //if (alignment == TextAlignment.Left)
                //    xCorrection = emSize * 1f / 6f;
                //else if (alignment == TextAlignment.Right)
                //    xCorrection = -emSize * 1f / 6f;

                double yCorrection = formattedText.Baseline;

                Point textPoint = new Point(ctp.X, ctp.Y - yCorrection);

                RotateTransform rotateAt = new RotateTransform(90, ctp.X, ctp.Y);
                _textContext.PushTransform(rotateAt);

                _textContext.DrawText(formattedText, textPoint);

                //float bboxWidth = (float)formattedText.Width;
                double bboxWidth = formattedText.WidthIncludingTrailingWhitespace;
                if (alignment == TextAlignment.Center)
                    bboxWidth /= 2f;
                else if (alignment == TextAlignment.Right)
                    bboxWidth = 0;

                //ctp.X += bboxWidth + emSize / 4;
                ctp.X += bboxWidth;

                if (rotateAt != null)
                {
                    _textContext.Pop();
                }
            }
            else
            {
                RotateTransform rotateAt = new RotateTransform(90, ctp.X, ctp.Y);
                _textContext.PushTransform(rotateAt);

                float spacing = Convert.ToSingle(letterSpacing);
                for (int i = 0; i < text.Length; i++)
                {
                    FormattedText formattedText = new FormattedText(new string(text[i], 1),
                        textRun.IsLatin ? _drawContext.EnglishCultureInfo : _drawContext.CultureInfo,
                        stringFormat.Direction,
                        new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
                        emSize, textBrush);

                    formattedText.Trimming      = stringFormat.Trimming;
                    formattedText.TextAlignment = stringFormat.Alignment;

                    if (textDecors != null && textDecors.Count != 0)
                    {
                        formattedText.SetTextDecorations(textDecors);
                    }

                    //float xCorrection = 0;
                    //if (alignment == TextAlignment.Left)
                    //    xCorrection = emSize * 1f / 6f;
                    //else if (alignment == TextAlignment.Right)
                    //    xCorrection = -emSize * 1f / 6f;

                    double yCorrection = formattedText.Baseline;

                    Point textPoint = new Point(ctp.X, ctp.Y - yCorrection);

                    _textContext.DrawText(formattedText, textPoint);

                    //float bboxWidth = (float)formattedText.Width;
                    double bboxWidth = formattedText.WidthIncludingTrailingWhitespace;
                    if (alignment == TextAlignment.Center)
                        bboxWidth /= 2f;
                    else if (alignment == TextAlignment.Right)
                        bboxWidth = 0;

                    //ctp.X += bboxWidth + emSize / 4 + spacing;
                    ctp.X += bboxWidth + spacing;
                }

                if (rotateAt != null)
                {
                    _textContext.Pop();
                }
            }
        }
        public override void RenderTextRun(SvgTextContentElement element, ref Point ctp,
            string text, double rotate, WpfTextPlacement placement)
        {
            if (String.IsNullOrEmpty(text))
                return;

            double emSize         = GetComputedFontSize(element);
            FontFamily fontFamily = GetTextFontFamily(element, emSize);

            FontStyle fontStyle   = GetTextFontStyle(element);
            FontWeight fontWeight = GetTextFontWeight(element);

            FontStretch fontStretch = GetTextFontStretch(element);

            WpfTextStringFormat stringFormat = GetTextStringFormat(element);

            // Fix the use of Postscript fonts...
            WpfFontFamilyVisitor fontFamilyVisitor = _drawContext.FontFamilyVisitor;
            if (!String.IsNullOrEmpty(_actualFontName) && fontFamilyVisitor != null)
            {
                WpfFontFamilyInfo currentFamily = new WpfFontFamilyInfo(fontFamily, fontWeight,
                    fontStyle, fontStretch);
                WpfFontFamilyInfo familyInfo = fontFamilyVisitor.Visit(_actualFontName,
                    currentFamily,_drawContext);
                if (familyInfo != null && !familyInfo.IsEmpty)
                {
                    fontFamily  = familyInfo.Family;
                    fontWeight  = familyInfo.Weight;
                    fontStyle   = familyInfo.Style;
                    fontStretch = familyInfo.Stretch;
                }
            }

            WpfSvgPaint fillPaint = new WpfSvgPaint(_drawContext, element, "fill");
            Brush textBrush = fillPaint.GetBrush();

            WpfSvgPaint strokePaint = new WpfSvgPaint(_drawContext, element, "stroke");
            Pen textPen = strokePaint.GetPen();

            if (textBrush == null && textPen == null)
            {
                return;
            }
            else if (textBrush == null)
            {
                // If here, then the pen is not null, and so the fill cannot be null.
                // We set this to transparent for stroke only text path...
                textBrush = Brushes.Transparent;
            }

            TextDecorationCollection textDecors = GetTextDecoration(element);
            if (textDecors == null)
            {
                SvgTextElement textElement = element.ParentNode as SvgTextElement;

                if (textElement != null)
                {
                    textDecors = GetTextDecoration(textElement);
                }
            }

            TextAlignment alignment = stringFormat.Alignment;

            bool hasWordSpacing  = false;
            string wordSpaceText = element.GetAttribute("word-spacing");
            double wordSpacing = 0;
            if (!String.IsNullOrEmpty(wordSpaceText) &&
                Double.TryParse(wordSpaceText, out wordSpacing) && (float)wordSpacing != 0)
            {
                hasWordSpacing = true;
            }

            bool hasLetterSpacing = false;
            string letterSpaceText = element.GetAttribute("letter-spacing");
            double letterSpacing = 0;
            if (!String.IsNullOrEmpty(letterSpaceText) &&
                Double.TryParse(letterSpaceText, out letterSpacing) && (float)letterSpacing != 0)
            {
                hasLetterSpacing = true;
            }

            bool isRotatePosOnly = false;

            IList<WpfTextPosition> textPositions = null;
            int textPosCount = 0;
            if ((placement != null && placement.HasPositions))
            {
                textPositions   = placement.Positions;
                textPosCount    = textPositions.Count;
                isRotatePosOnly = placement.IsRotateOnly;
            }

            if (hasLetterSpacing || hasWordSpacing || textPositions != null)
            {
                double spacing = Convert.ToDouble(letterSpacing);
                for (int i = 0; i < text.Length; i++)
                {
                    FormattedText formattedText = new FormattedText(new string(text[i], 1),
                        _drawContext.CultureInfo, stringFormat.Direction,
                        new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
                        emSize, textBrush);

                    if (this.IsMeasuring)
                    {
                        this.AddTextWidth(formattedText.WidthIncludingTrailingWhitespace);
                        continue;
                    }

                    formattedText.Trimming      = stringFormat.Trimming;
                    formattedText.TextAlignment = stringFormat.Alignment;

                    if (textDecors != null && textDecors.Count != 0)
                    {
                        formattedText.SetTextDecorations(textDecors);
                    }

                    WpfTextPosition? textPosition = null;
                    if (textPositions != null && i < textPosCount)
                    {
                        textPosition = textPositions[i];
                    }

                    //float xCorrection = 0;
                    //if (alignment == TextAlignment.Left)
                    //    xCorrection = emSize * 1f / 6f;
                    //else if (alignment == TextAlignment.Right)
                    //    xCorrection = -emSize * 1f / 6f;

                    double yCorrection = formattedText.Baseline;

                    float rotateAngle = (float)rotate;
                    if (textPosition != null)
                    {
                        if (!isRotatePosOnly)
                        {
                            Point pt = textPosition.Value.Location;
                            ctp.X = pt.X;
                            ctp.Y = pt.Y;
                        }
                        rotateAngle = (float)textPosition.Value.Rotation;
                    }
                    Point textStart = ctp;

                    RotateTransform rotateAt = null;
                    if (rotateAngle != 0)
                    {
                        rotateAt = new RotateTransform(rotateAngle, textStart.X, textStart.Y);
                        _textContext.PushTransform(rotateAt);
                    }

                    Point textPoint = new Point(ctp.X, ctp.Y - yCorrection);

                    if (textPen != null || _drawContext.TextAsGeometry)
                    {
                        Geometry textGeometry = formattedText.BuildGeometry(textPoint);
                        if (textGeometry != null && !textGeometry.IsEmpty())
                        {
                            _textContext.DrawGeometry(textBrush, textPen,
                                ExtractTextPathGeometry(textGeometry));

                            this.IsTextPath = true;
                        }
                        else
                        {
                            _textContext.DrawText(formattedText, textPoint);
                        }
                    }
                    else
                    {
                        _textContext.DrawText(formattedText, textPoint);
                    }

                    //float bboxWidth = (float)formattedText.Width;
                    double bboxWidth = formattedText.WidthIncludingTrailingWhitespace;
                    if (alignment == TextAlignment.Center)
                        bboxWidth /= 2f;
                    else if (alignment == TextAlignment.Right)
                        bboxWidth = 0;

                    //ctp.X += bboxWidth + emSize / 4 + spacing;
                    if (hasLetterSpacing)
                    {
                        ctp.X += bboxWidth + letterSpacing;
                    }
                    if (hasWordSpacing && Char.IsWhiteSpace(text[i]))
                    {
                        if (hasLetterSpacing)
                        {
                            ctp.X += wordSpacing;
                        }
                        else
                        {
                            ctp.X += bboxWidth + wordSpacing;
                        }
                    }
                    else
                    {
                        if (!hasLetterSpacing)
                        {
                            ctp.X += bboxWidth;
                        }
                    }

                    if (rotateAt != null)
                    {
                        _textContext.Pop();
                    }
                }
            }
            else
            {
                FormattedText formattedText = new FormattedText(text, _drawContext.CultureInfo,
                    stringFormat.Direction, new Typeface(fontFamily, fontStyle, fontWeight, fontStretch),
                    emSize, textBrush);

                if (this.IsMeasuring)
                {
                    this.AddTextWidth(formattedText.WidthIncludingTrailingWhitespace);
                    return;
                }

                if (alignment == TextAlignment.Center && this.TextWidth > 0)
                {
                    alignment = TextAlignment.Left;
                }

                formattedText.TextAlignment = alignment;
                formattedText.Trimming      = stringFormat.Trimming;

                if (textDecors != null && textDecors.Count != 0)
                {
                    formattedText.SetTextDecorations(textDecors);
                }

                //float xCorrection = 0;
                //if (alignment == TextAlignment.Left)
                //    xCorrection = emSize * 1f / 6f;
                //else if (alignment == TextAlignment.Right)
                //    xCorrection = -emSize * 1f / 6f;

                double yCorrection = formattedText.Baseline;

                float rotateAngle  = (float)rotate;
                Point textPoint    = new Point(ctp.X, ctp.Y - yCorrection);

                RotateTransform rotateAt = null;
                if (rotateAngle != 0)
                {
                    rotateAt = new RotateTransform(rotateAngle, ctp.X, ctp.Y);
                    _textContext.PushTransform(rotateAt);
                }

                if (textPen != null || _drawContext.TextAsGeometry)
                {
                    Geometry textGeometry = formattedText.BuildGeometry(textPoint);
                    if (textGeometry != null && !textGeometry.IsEmpty())
                    {
                        _textContext.DrawGeometry(textBrush, textPen,
                            ExtractTextPathGeometry(textGeometry));

                        this.IsTextPath = true;
                    }
                    else
                    {
                        _textContext.DrawText(formattedText, textPoint);
                    }
                }
                else
                {
                    _textContext.DrawText(formattedText, textPoint);
                }

                //float bboxWidth = (float)formattedText.Width;
                double bboxWidth = formattedText.WidthIncludingTrailingWhitespace;
                if (alignment == TextAlignment.Center)
                    bboxWidth /= 2f;
                else if (alignment == TextAlignment.Right)
                    bboxWidth = 0;

                //ctp.X += bboxWidth + emSize / 4;
                ctp.X += bboxWidth;

                if (rotateAt != null)
                {
                    _textContext.Pop();
                }
            }
        }
Example #4
0
        private Brush GetBrush(Geometry geometry, string propPrefix, 
            bool setOpacity)
        {
            SvgPaint fill;
            if (PaintType == SvgPaintType.None)
            {
                return null;
            }
            else if (PaintType == SvgPaintType.CurrentColor)
            {
                fill = new WpfSvgPaint(_context, _element, "color");
            }
            else
            {
                fill = this;
            }

            SvgPaintType paintType = fill.PaintType;
            if (paintType == SvgPaintType.Uri     || paintType == SvgPaintType.UriCurrentColor ||
                paintType == SvgPaintType.UriNone || paintType == SvgPaintType.UriRgbColor     ||
                paintType == SvgPaintType.UriRgbColorIccColor)
            {
                _paintFill = GetPaintFill(fill.Uri);
                if (_paintFill != null)
                {
                    Brush brush = null;
                    if (geometry != null)
                    {
                        brush = _paintFill.GetBrush(geometry.Bounds, _context);
                    }
                    else
                    {
                        brush = _paintFill.GetBrush(Rect.Empty, _context);
                    }

                    if (brush != null)
                    {
                        brush.Opacity = GetOpacity(propPrefix);
                    }

                    return brush;
                }
                else
                {
                    if (PaintType == SvgPaintType.UriNone || PaintType == SvgPaintType.Uri)
                    {
                        return null;
                    }
                    else if (PaintType == SvgPaintType.UriCurrentColor)
                    {
                        fill = new WpfSvgPaint(_context, _element, "color");
                    }
                    else
                    {
                        fill = this;
                    }
                }
            }

            if (fill == null || fill.RgbColor == null)
            {
                return null;
            }

            Color? solidColor = WpfConvert.ToColor(fill.RgbColor);
            if (solidColor == null)
            {
                return null;
            }

            SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value);
            //int opacity = GetOpacity(propPrefix);
            //solidBrush.Color = Color.FromArgb(opacity, brush.Color);
            if (setOpacity)
            {
                solidBrush.Opacity = GetOpacity(propPrefix);
            }
            return solidBrush;
        }
Example #5
0
        private WpfFill GetPaintFill(string uri)
        {
            string absoluteUri = _element.ResolveUri(uri);

            if (_element.Imported && _element.ImportDocument != null &&
                _element.ImportNode != null)
            {
                // We need to determine whether the provided URI refers to element in the
                // original document or in the current document...
                SvgStyleableElement styleElm = _element.ImportNode as SvgStyleableElement;
                if (styleElm != null)
                {
                    string propertyValue =
                        styleElm.GetComputedStyle("").GetPropertyValue(_propertyName);

                    if (!String.IsNullOrEmpty(propertyValue))
                    {
                        WpfSvgPaint importFill = new WpfSvgPaint(_context, styleElm, _propertyName);
                        if (String.Equals(uri, importFill.Uri, StringComparison.OrdinalIgnoreCase))
                        {
                            WpfFill fill = WpfFill.CreateFill(_element.ImportDocument, absoluteUri);
                            if (fill != null)
                            {
                                return fill;
                            }
                        }
                    }
                }
            }

            return WpfFill.CreateFill(_element.OwnerDocument, absoluteUri);
        }
Example #6
0
        protected Brush GetBrush()
        {
            WpfSvgPaint paint = new WpfSvgPaint(_drawContext, _textElement, "fill");

            return paint.GetBrush();
        }
Example #7
0
        protected Pen GetPen()
        {
            WpfSvgPaint paint = new WpfSvgPaint(_drawContext, _textElement, "stroke");

            return paint.GetPen();
        }
Example #8
0
        public override void Render(WpfDrawingRenderer renderer)
        {
            WpfDrawingContext context = renderer.Context;

            SvgRenderingHint hint = _svgElement.RenderingHint;
            if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping)
            {
                return;
            }
            // We do not directly render the contents of the clip-path, unless specifically requested...
            if (String.Equals(_svgElement.ParentNode.LocalName, "clipPath") &&
                !context.RenderingClipRegion)
            {
                return;
            }

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            string sVisibility = styleElm.GetPropertyValue("visibility");
            string sDisplay    = styleElm.GetPropertyValue("display");
            if (String.Equals(sVisibility, "hidden") || String.Equals(sDisplay, "none"))
            {
                return;
            }

            DrawingGroup drawGroup = context.Peek();
            Debug.Assert(drawGroup != null);

            Geometry geometry = CreateGeometry(_svgElement, context.OptimizePath);

            if (geometry != null && !geometry.IsEmpty())
            {
                SetClip(context);

                WpfSvgPaint fillPaint = new WpfSvgPaint(context, styleElm, "fill");

                string fileValue = styleElm.GetAttribute("fill");

                Brush brush = fillPaint.GetBrush(geometry);

                WpfSvgPaint strokePaint = new WpfSvgPaint(context, styleElm, "stroke");
                Pen pen = strokePaint.GetPen();

                if (brush != null || pen != null)
                {
                    Transform transform = this.Transform;
                    if (transform != null && !transform.Value.IsIdentity)
                    {
                        geometry.Transform = transform;
                        if (brush != null)
                        {
                            Transform brushTransform = brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                brush.Transform = transform;
                            }
                            else
                            {
                                TransformGroup groupTransform = new TransformGroup();
                                groupTransform.Children.Add(brushTransform);
                                groupTransform.Children.Add(transform);
                                brush.Transform = groupTransform;
                            }
                        }
                    }
                    else
                    {
                        transform = null; // render any identity transform useless...
                    }

                    GeometryDrawing drawing = new GeometryDrawing(brush, pen, geometry);

                    string elementId = this.GetElementName();
                    if (!String.IsNullOrEmpty(elementId) && !context.IsRegisteredId(elementId))
                    {
                        drawing.SetValue(FrameworkElement.NameProperty, elementId);

                        context.RegisterId(elementId);

                        if (context.IncludeRuntime)
                        {
                            SvgObject.SetId(drawing, elementId);
                        }
                    }

                    Brush maskBrush = this.Masking;
                    Geometry clipGeom = this.ClipGeometry;
                    if (clipGeom != null || maskBrush != null)
                    {
                        //Geometry clipped = Geometry.Combine(geometry, clipGeom,
                        //    GeometryCombineMode.Exclude, null);

                        //if (clipped != null && !clipped.IsEmpty())
                        //{
                        //    geometry = clipped;
                        //}
                        DrawingGroup clipMaskGroup = new DrawingGroup();

                        Rect geometryBounds = geometry.Bounds;

                        if (clipGeom != null)
                        {
                            clipMaskGroup.ClipGeometry = clipGeom;

                            SvgUnitType clipUnits = this.ClipUnits;
                            if (clipUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }

                                TransformGroup transformGroup = new TransformGroup();

                                // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                transformGroup.Children.Add(
                                    new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
                                transformGroup.Children.Add(
                                    new TranslateTransform(drawingBounds.X, drawingBounds.Y));

                                clipGeom.Transform = transformGroup;
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    clipGeom.Transform = transform;
                                }
                            }
                        }
                        if (maskBrush != null)
                        {
                            SvgUnitType maskUnits = this.MaskUnits;
                            if (maskUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }

                                TransformGroup transformGroup = new TransformGroup();

                                // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                transformGroup.Children.Add(
                                    new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
                                transformGroup.Children.Add(
                                    new TranslateTransform(drawingBounds.X, drawingBounds.Y));

                                DrawingGroup maskGroup = ((DrawingBrush)maskBrush).Drawing as DrawingGroup;
                                if (maskGroup != null)
                                {
                                    DrawingCollection maskDrawings = maskGroup.Children;
                                    for (int i = 0; i < maskDrawings.Count; i++)
                                    {
                                        Drawing maskDrawing = maskDrawings[i];
                                        GeometryDrawing maskGeomDraw = maskDrawing as GeometryDrawing;
                                        if (maskGeomDraw != null)
                                        {
                                            if (maskGeomDraw.Brush != null)
                                            {
                                                ConvertColors(maskGeomDraw.Brush);
                                            }
                                            if (maskGeomDraw.Pen != null)
                                            {
                                                ConvertColors(maskGeomDraw.Pen.Brush);
                                            }
                                        }
                                    }
                                }

                                //if (transformGroup != null)
                                //{
                                //    drawingBounds = transformGroup.TransformBounds(drawingBounds);
                                //}

                                //maskBrush.Viewbox = drawingBounds;
                                //maskBrush.ViewboxUnits = BrushMappingMode.Absolute;

                                //maskBrush.Stretch = Stretch.Uniform;

                                //maskBrush.Viewport = drawingBounds;
                                //maskBrush.ViewportUnits = BrushMappingMode.Absolute;

                                maskBrush.Transform = transformGroup;
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    maskBrush.Transform = transform;
                                }
                            }

                            clipMaskGroup.OpacityMask = maskBrush;
                        }

                        clipMaskGroup.Children.Add(drawing);
                        drawGroup.Children.Add(clipMaskGroup);
                    }
                    else
                    {
                        drawGroup.Children.Add(drawing);
                    }
                }
            }

            RenderMarkers(renderer, styleElm, context);
        }
        private void RenderTextPath(SvgTextContentElement element, WpfTextOnPathDrawing pathDrawing,
            string text, Point origin, double rotate, WpfTextPlacement placement)
        {
            if (String.IsNullOrEmpty(text))
            {
                return;
            }

            double emSize         = GetComputedFontSize(element);
            FontFamily fontFamily = GetTextFontFamily(element, emSize);

            FontStyle fontStyle = GetTextFontStyle(element);
            FontWeight fontWeight = GetTextFontWeight(element);

            FontStretch fontStretch = GetTextFontStretch(element);

            WpfTextStringFormat stringFormat = GetTextStringFormat(element);

            // Fix the use of Postscript fonts...
            WpfFontFamilyVisitor fontFamilyVisitor = _drawContext.FontFamilyVisitor;
            if (!String.IsNullOrEmpty(_actualFontName) && fontFamilyVisitor != null)
            {
                WpfFontFamilyInfo currentFamily = new WpfFontFamilyInfo(fontFamily, fontWeight,
                    fontStyle, fontStretch);
                WpfFontFamilyInfo familyInfo = fontFamilyVisitor.Visit(_actualFontName,
                    currentFamily, _drawContext);
                if (familyInfo != null && !familyInfo.IsEmpty)
                {
                    fontFamily  = familyInfo.Family;
                    fontWeight  = familyInfo.Weight;
                    fontStyle   = familyInfo.Style;
                    fontStretch = familyInfo.Stretch;
                }
            }

            WpfSvgPaint fillPaint = new WpfSvgPaint(_drawContext, element, "fill");
            Brush textBrush = fillPaint.GetBrush();

            WpfSvgPaint strokePaint = new WpfSvgPaint(_drawContext, element, "stroke");
            Pen pen = strokePaint.GetPen();

            TextDecorationCollection textDecors = GetTextDecoration(element);
            TextAlignment alignment = stringFormat.Alignment;

            pathDrawing.FontSize = emSize;

            pathDrawing.FontFamily  = fontFamily;
            pathDrawing.FontWeight  = fontWeight;
            pathDrawing.FontStretch = fontStretch;

            pathDrawing.Foreground = textBrush;

            pathDrawing.AddTextPath(text, origin);
        }
Example #10
0
        private void RenderPath(WpfDrawingRenderer renderer)
        {
            WpfDrawingContext context = renderer.Context;

            SvgRenderingHint hint = _svgElement.RenderingHint;

            if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping)
            {
                return;
            }
            var parentNode = _svgElement.ParentNode;

            // We do not directly render the contents of the clip-path, unless specifically requested...
            if (string.Equals(parentNode.LocalName, "clipPath") &&
                !context.RenderingClipRegion)
            {
                return;
            }

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            //string sVisibility = styleElm.GetPropertyValue("visibility");
            //string sDisplay    = styleElm.GetPropertyValue("display");
            //if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            //{
            //    return;
            //}

            DrawingGroup drawGroup = context.Peek();

            Debug.Assert(drawGroup != null);

            Geometry geometry = CreateGeometry(_svgElement, context.OptimizePath);

            string elementId    = this.GetElementName();
            string elementClass = this.GetElementClass();

            GeometryDrawing drawing = null;

            if (geometry != null && !geometry.IsEmpty())
            {
                context.UpdateBounds(geometry.Bounds);

//                SetClip(context);

                WpfSvgPaint fillPaint = new WpfSvgPaint(context, styleElm, "fill");

                string fileValue = styleElm.GetAttribute("fill");

                Brush brush            = fillPaint.GetBrush(geometry, _setBrushOpacity);
                bool  isFillTransmable = fillPaint.IsFillTransformable;

                WpfSvgPaint strokePaint = new WpfSvgPaint(context, styleElm, "stroke");
                Pen         pen         = strokePaint.GetPen(geometry, _setBrushOpacity);

                // By the SVG Specifications:
                // Keyword 'objectBoundingBox' should not be used when the geometry of the applicable
                // element has no width or no height, such as the case of a horizontal or vertical line,
                // even when the line has actual thickness when viewed due to having a non-zero stroke
                // width since stroke width is ignored for bounding box calculations. When the geometry
                // of the applicable element has no width or height and 'objectBoundingBox' is specified,
                // then the given effect (e.g., a gradient) will be ignored.
                if (pen != null && _isLineSegment && strokePaint.FillType == WpfFillType.Gradient)
                {
                    LineGeometry    lineGeometry = geometry as LineGeometry;
                    WpfGradientFill gradientFill = (WpfGradientFill)strokePaint.PaintServer;
                    if (gradientFill.IsUserSpace == false && lineGeometry != null)
                    {
                        bool invalidGrad = SvgObject.IsEqual(lineGeometry.EndPoint.X, lineGeometry.StartPoint.X) ||
                                           SvgObject.IsEqual(lineGeometry.EndPoint.Y, lineGeometry.StartPoint.Y);
                        if (invalidGrad)
                        {
                            // Brush is not likely inherited, we need to support fallback too
                            WpfSvgPaint fallbackPaint = strokePaint.WpfFallback;
                            if (fallbackPaint != null)
                            {
                                pen.Brush = fallbackPaint.GetBrush(geometry, _setBrushOpacity);
                            }
                            else
                            {
                                var scopePaint = strokePaint.GetScopeStroke();
                                if (scopePaint != null)
                                {
                                    if (scopePaint != strokePaint)
                                    {
                                        pen.Brush = scopePaint.GetBrush(geometry, _setBrushOpacity);
                                    }
                                    else
                                    {
                                        pen.Brush = null;
                                    }
                                }
                                else
                                {
                                    pen.Brush = null;
                                }
                            }
                        }
                    }
                }

                if (_paintContext != null)
                {
                    _paintContext.Fill   = fillPaint;
                    _paintContext.Stroke = strokePaint;
                    _paintContext.Tag    = geometry;
                }

                if (brush != null || pen != null)
                {
                    Transform transform = this.Transform;
                    if (transform != null && !transform.Value.IsIdentity)
                    {
                        geometry.Transform = transform;
                        if (brush != null && isFillTransmable)
                        {
                            Transform brushTransform = brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                brush.Transform = transform;
                            }
                            else
                            {
                                TransformGroup groupTransform = new TransformGroup();
                                groupTransform.Children.Add(brushTransform);
                                groupTransform.Children.Add(transform);
                                brush.Transform = groupTransform;
                            }
                        }
                        if (pen != null && pen.Brush != null)
                        {
                            Transform brushTransform = pen.Brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                pen.Brush.Transform = transform;
                            }
                            else
                            {
                                TransformGroup groupTransform = new TransformGroup();
                                groupTransform.Children.Add(brushTransform);
                                groupTransform.Children.Add(transform);
                                pen.Brush.Transform = groupTransform;
                            }
                        }
                    }
                    else
                    {
                        transform = null; // render any identity transform useless...
                    }

                    drawing = new GeometryDrawing(brush, pen, geometry);

                    if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId))
                    {
                        SvgObject.SetName(drawing, elementId);

                        context.RegisterId(elementId);

                        if (context.IncludeRuntime)
                        {
                            SvgObject.SetId(drawing, elementId);
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime)
                    {
                        SvgObject.SetClass(drawing, elementClass);
                    }

                    Brush    maskBrush = this.Masking;
                    Geometry clipGeom  = this.ClipGeometry;
                    if (clipGeom != null || maskBrush != null)
                    {
                        //Geometry clipped = Geometry.Combine(geometry, clipGeom,
                        //    GeometryCombineMode.Exclude, null);

                        //if (clipped != null && !clipped.IsEmpty())
                        //{
                        //    geometry = clipped;
                        //}
                        DrawingGroup clipMaskGroup = new DrawingGroup();

                        Rect geometryBounds = geometry.Bounds;

                        if (clipGeom != null)
                        {
                            clipMaskGroup.ClipGeometry = clipGeom;

                            SvgUnitType clipUnits = this.ClipUnits;
                            if (clipUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }

                                TransformGroup transformGroup = new TransformGroup();

                                // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                transformGroup.Children.Add(new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
                                transformGroup.Children.Add(new TranslateTransform(drawingBounds.X, drawingBounds.Y));

                                clipGeom.Transform = transformGroup;
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    clipGeom.Transform = transform;
                                }
                            }
                        }
                        if (maskBrush != null)
                        {
                            DrawingBrush drawingBrush = (DrawingBrush)maskBrush;

                            SvgUnitType maskUnits        = this.MaskUnits;
                            SvgUnitType maskContentUnits = this.MaskContentUnits;
                            if (maskUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }
                                DrawingGroup maskGroup = drawingBrush.Drawing as DrawingGroup;
                                if (maskGroup != null)
                                {
                                    DrawingCollection maskDrawings = maskGroup.Children;
                                    for (int i = 0; i < maskDrawings.Count; i++)
                                    {
                                        Drawing         maskDrawing  = maskDrawings[i];
                                        GeometryDrawing maskGeomDraw = maskDrawing as GeometryDrawing;
                                        if (maskGeomDraw != null)
                                        {
                                            if (maskGeomDraw.Brush != null)
                                            {
                                                ConvertColors(maskGeomDraw.Brush);
                                            }
                                            if (maskGeomDraw.Pen != null)
                                            {
                                                ConvertColors(maskGeomDraw.Pen.Brush);
                                            }
                                        }
                                    }
                                }

                                if (maskContentUnits == SvgUnitType.ObjectBoundingBox)
                                {
                                    TransformGroup transformGroup = new TransformGroup();

                                    // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                    var scaleTransform = new ScaleTransform(drawingBounds.Width, drawingBounds.Height);
                                    transformGroup.Children.Add(scaleTransform);
                                    var translateTransform = new TranslateTransform(drawingBounds.X, drawingBounds.Y);
                                    transformGroup.Children.Add(translateTransform);

                                    Matrix scaleMatrix     = new Matrix();
                                    Matrix translateMatrix = new Matrix();

                                    scaleMatrix.Scale(drawingBounds.Width, drawingBounds.Height);
                                    translateMatrix.Translate(drawingBounds.X, drawingBounds.Y);

                                    Matrix matrix = Matrix.Multiply(scaleMatrix, translateMatrix);
                                    //maskBrush.Transform = transformGroup;
                                    maskBrush.Transform = new MatrixTransform(matrix);
                                }
                                else
                                {
                                    drawingBrush.Viewbox      = drawingBounds;
                                    drawingBrush.ViewboxUnits = BrushMappingMode.Absolute;

                                    drawingBrush.Stretch = Stretch.Uniform;

                                    drawingBrush.Viewport      = drawingBounds;
                                    drawingBrush.ViewportUnits = BrushMappingMode.Absolute;
                                }
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    maskBrush.Transform = transform;
                                }
                            }

                            clipMaskGroup.OpacityMask = maskBrush;
                        }

                        clipMaskGroup.Children.Add(drawing);
                        drawGroup.Children.Add(clipMaskGroup);
                    }
                    else
                    {
                        drawGroup.Children.Add(drawing);
                    }
                }
            }

            // If this is not the child of a "marker", then try rendering a marker...
            if (!string.Equals(parentNode.LocalName, "marker"))
            {
                RenderMarkers(renderer, styleElm, context);
            }

            // Register this drawing with the Drawing-Document...
            if (drawing != null)
            {
                this.Rendered(drawing);
            }
        }
Example #11
0
        public override void Visit(DrawingGroup group, SvgAElement element,
            WpfDrawingContext context, float opacity)
        {
            _isAggregated = false;

            if (group == null || element == null || context == null)
            {
                return;
            }

            AddExtraLinkInformation(group, element);

            //string linkId = element.GetAttribute("id");
            string linkId = GetElementName(element);
            if (String.IsNullOrEmpty(linkId))
            {
                return;
            }
            SvgLink.SetKey(group, linkId);

            if (_dicLinks.ContainsKey(linkId))
            {
                _isAggregated = _dicLinks[linkId];
                return;
            }

            string linkAction = element.GetAttribute("onclick");
            if (String.IsNullOrEmpty(linkAction))
            {
                linkAction = element.GetAttribute("onmouseover");
                if (!String.IsNullOrEmpty(linkAction) &&
                    linkAction.StartsWith("parent.svgMouseOverName", StringComparison.OrdinalIgnoreCase))
                {
                    SvgLink.SetAction(group, SvgLinkAction.LinkTooltip);
                }
                else
                {
                    SvgLink.SetAction(group, SvgLinkAction.LinkNone);
                }
            }
            else
            {
                if (linkAction.StartsWith("parent.svgClick", StringComparison.OrdinalIgnoreCase))
                {
                    SvgLink.SetAction(group, SvgLinkAction.LinkPage);
                }
                else if (linkAction.StartsWith("parent.svgOpenHtml", StringComparison.OrdinalIgnoreCase))
                {
                    SvgLink.SetAction(group, SvgLinkAction.LinkHtml);
                }
                else
                {
                    SvgLink.SetAction(group, SvgLinkAction.LinkNone);
                }
            }

            if (!String.IsNullOrEmpty(linkAction))
            {
                if (linkAction.IndexOf("'Top'") > 0)
                {
                    SvgLink.SetLocation(group, "Top");
                }
                else if (linkAction.IndexOf("'Bottom'") > 0)
                {
                    SvgLink.SetLocation(group, "Bottom");
                }
                else
                {
                    SvgLink.SetLocation(group, "Top");
                }
            }

            this.AggregateChildren(element, context, opacity);
            if (_isAggregated)
            {
                Geometry drawGeometry = null;
                if (_aggregatedGeom.Count == 1)
                {
                    drawGeometry = _aggregatedGeom[0];
                }
                else
                {
                    GeometryGroup geomGroup = new GeometryGroup();
                    geomGroup.FillRule = FillRule.Nonzero;

                    for (int i = 0; i < _aggregatedGeom.Count; i++)
                    {
                        geomGroup.Children.Add(_aggregatedGeom[i]);
                    }

                    drawGeometry = geomGroup;
                }

                WpfSvgPaint fillPaint = new WpfSvgPaint(context, _aggregatedFill, "fill");
                Brush brush = fillPaint.GetBrush(false);

                brush.SetValue(FrameworkElement.NameProperty, linkId + "_Brush");

                GeometryDrawing drawing = new GeometryDrawing(brush, null, drawGeometry);

                group.Children.Add(drawing);
            }

            _dicLinks.Add(linkId, _isAggregated);
        }
Example #12
0
        private Brush GetBrush(Geometry geometry, string propPrefix, bool setOpacity)
        {
            WpfSvgPaintContext paintContext = null;

            SvgPaintType paintType = this.PaintType;

            WpfSvgPaint fill;

            if (paintType == SvgPaintType.None)
            {
                return(null);
            }
            if (paintType == SvgPaintType.CurrentColor)
            {
                //TODO: Find a better way to support currentColor specified on parent element.
                var deferredFill = this.GetDeferredFill();
                if (deferredFill == null)
                {
                    fill = new WpfSvgPaint(_context, _element, "color");
                }
                else
                {
                    fill = deferredFill;
                }
            }
            else if (paintType == SvgPaintType.ContextFill)
            {
                paintContext = GetFillContext();
                if (paintContext != null)
                {
                    fill = paintContext.Fill;
                }
                else
                {
                    fill = this;
                }
            }
            else if (paintType == SvgPaintType.ContextStroke)
            {
                paintContext = GetStrokeContext();
                if (paintContext != null)
                {
                    fill = paintContext.Stroke;
                }
                else
                {
                    fill = this;
                }
            }
            else
            {
                fill = this;
            }

            SvgPaintType fillType = fill.PaintType;

            if (fillType == SvgPaintType.Uri || fillType == SvgPaintType.UriCurrentColor ||
                fillType == SvgPaintType.UriNone || fillType == SvgPaintType.UriRgbColor ||
                fillType == SvgPaintType.UriRgbColorIccColor)
            {
                _paintFill = GetPaintFill(fill.Uri);
                if (_paintFill != null)
                {
                    Brush brush = null;
                    if (geometry != null)
                    {
                        brush = _paintFill.GetBrush(geometry.Bounds, _context, geometry.Transform);
                    }
                    else
                    {
                        brush = _paintFill.GetBrush(Rect.Empty, _context, null);
                    }

                    if (brush != null)
                    {
                        brush.Opacity = GetOpacity(propPrefix);
                    }

                    return(brush);
                }

                if (paintType == SvgPaintType.UriNone || paintType == SvgPaintType.Uri)
                {
                    return(null);
                }
                if (paintType == SvgPaintType.UriCurrentColor)
                {
                    fill = new WpfSvgPaint(_context, _element, "color");
                }
                else
                {
                    fill = this;
                }
            }

            if (fill == null || fill.RgbColor == null)
            {
                return(null);
            }

            if (fill.RgbColor.IsVarColor)
            {
                var cssVar = this.GetVarsValue(fill);
                if (cssVar != null)
                {
                    var cssVariables = _context.Settings.CssVariables;
                    if (cssVariables != null && cssVariables.ContainsKey(cssVar.VarName))
                    {
                        var   cssColor = new CssColor(cssVariables[cssVar.VarName]);
                        Color?varColor = WpfConvert.ToColor(cssColor);
                        if (varColor != null)
                        {
                            var varBrush = new SolidColorBrush(varColor.Value);
                            if (setOpacity)
                            {
                                varBrush.Opacity = GetOpacity(propPrefix);
                            }
                            return(varBrush);
                        }
                    }

                    var cssValue = _element.GetComputedCssValue(cssVar.VarName, string.Empty) as CssAbsPrimitiveValue;
                    if (cssValue != null)
                    {
                        Color?varColor = WpfConvert.ToColor(cssValue.GetRgbColorValue());
                        if (varColor != null)
                        {
                            var varBrush = new SolidColorBrush(varColor.Value);
                            if (setOpacity)
                            {
                                varBrush.Opacity = GetOpacity(propPrefix);
                            }
                            return(varBrush);
                        }
                    }

                    var fallbackValue = cssVar.VarValue;
                    if (!string.IsNullOrWhiteSpace(fallbackValue))
                    {
                        var   cssColor = new CssColor(fallbackValue);
                        Color?varColor = WpfConvert.ToColor(cssColor);
                        if (varColor != null)
                        {
                            var varBrush = new SolidColorBrush(varColor.Value);
                            if (setOpacity)
                            {
                                varBrush.Opacity = GetOpacity(propPrefix);
                            }
                            return(varBrush);
                        }
                    }
                }
            }

            Color?solidColor = WpfConvert.ToColor(fill.RgbColor);

            if (solidColor == null)
            {
                return(null);
            }

            SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value);

            if (setOpacity)
            {
                solidBrush.Opacity = GetOpacity(propPrefix);
            }
            return(solidBrush);
        }
Example #13
0
        public Pen GetPen(Geometry geometry, bool setOpacity = true)
        {
            double strokeWidth = GetStrokeWidth();

            if (strokeWidth.Equals(0.0d))
            {
                return(null);
            }

            WpfSvgPaintContext paintContext = null;

            SvgPaintType paintType = this.PaintType;

            WpfSvgPaint stroke;

            if (paintType == SvgPaintType.None)
            {
                return(null);
            }
            if (paintType == SvgPaintType.CurrentColor)
            {
                stroke = new WpfSvgPaint(_context, _element, "color");
            }
            else if (paintType == SvgPaintType.ContextFill)
            {
                paintContext = GetFillContext();
                if (paintContext != null)
                {
                    stroke = paintContext.Fill;
                }
                else
                {
                    stroke = this;
                }
            }
            else if (paintType == SvgPaintType.ContextStroke)
            {
                paintContext = GetStrokeContext();
                if (paintContext != null)
                {
                    stroke = paintContext.Stroke;
                }
                else
                {
                    stroke = this;
                }
            }
            else
            {
                stroke = this;
            }

            Brush brush = stroke.GetBrush(geometry, "stroke", setOpacity);

            if (brush == null)
            {
                WpfSvgPaint fallbackPaint = stroke.WpfFallback;
                if (fallbackPaint != null)
                {
                    brush = fallbackPaint.GetBrush(geometry, "stroke", setOpacity);
                }
            }
            Pen pen = new Pen(brush, strokeWidth);

            pen.StartLineCap = pen.EndLineCap = GetLineCap();
            pen.LineJoin     = GetLineJoin();
            double miterLimit = GetMiterLimit(strokeWidth);

            if (miterLimit > 0)
            {
                pen.MiterLimit = miterLimit;
            }

            //pen.MiterLimit = 1.0f;

            DoubleCollection dashArray = GetDashArray(strokeWidth);

            if (dashArray != null && dashArray.Count != 0)
            {
                bool isValidDashes = true;
                //Do not draw if dash array had a zero value in it
                for (int i = 0; i < dashArray.Count; i++)
                {
                    if (dashArray[i].Equals(0.0d))
                    {
                        isValidDashes = false;
                    }
                }

                if (isValidDashes)
                {
                    DashStyle dashStyle = new DashStyle(dashArray, GetDashOffset(strokeWidth));

                    pen.DashStyle = dashStyle;
                    // This is the one that works well for the XAML, the default is not Flat as
                    // stated in the documentations...
                    pen.DashCap = PenLineCap.Flat;
                }
            }
            return(pen);
        }
Example #14
0
        public override void Render(WpfDrawingRenderer renderer)
        {
            WpfDrawingContext context = renderer.Context;

            SvgRenderingHint hint = _svgElement.RenderingHint;

            if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping)
            {
                return;
            }
            var parentNode = _svgElement.ParentNode;

            // We do not directly render the contents of the clip-path, unless specifically requested...
            if (string.Equals(parentNode.LocalName, "clipPath") &&
                !context.RenderingClipRegion)
            {
                return;
            }

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            string sVisibility = styleElm.GetPropertyValue("visibility");
            string sDisplay    = styleElm.GetPropertyValue("display");

            if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            {
                return;
            }

            DrawingGroup drawGroup = context.Peek();

            Debug.Assert(drawGroup != null);

            Geometry geometry = CreateGeometry(_svgElement, context.OptimizePath);

            string elementId = this.GetElementName();

            if (geometry != null && !geometry.IsEmpty())
            {
                SetClip(context);

                WpfSvgPaint fillPaint = new WpfSvgPaint(context, styleElm, "fill");

                string fileValue = styleElm.GetAttribute("fill");

                Brush brush            = fillPaint.GetBrush(geometry);
                bool  isFillTransmable = fillPaint.IsFillTransformable;

                WpfSvgPaint strokePaint = new WpfSvgPaint(context, styleElm, "stroke");
                Pen         pen         = strokePaint.GetPen(geometry);

                if (brush != null || pen != null)
                {
                    Transform transform = this.Transform;
                    if (transform != null && !transform.Value.IsIdentity)
                    {
                        geometry.Transform = transform;
                        if (brush != null && isFillTransmable)
                        {
                            Transform brushTransform = brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                brush.Transform = transform;
                            }
                            else
                            {
                                TransformGroup groupTransform = new TransformGroup();
                                groupTransform.Children.Add(brushTransform);
                                groupTransform.Children.Add(transform);
                                brush.Transform = groupTransform;
                            }
                        }
                        if (pen != null && pen.Brush != null)
                        {
                            Transform brushTransform = pen.Brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                pen.Brush.Transform = transform;
                            }
                            else
                            {
                                TransformGroup groupTransform = new TransformGroup();
                                groupTransform.Children.Add(brushTransform);
                                groupTransform.Children.Add(transform);
                                pen.Brush.Transform = groupTransform;
                            }
                        }
                    }
                    else
                    {
                        transform = null; // render any identity transform useless...
                    }

                    GeometryDrawing drawing = new GeometryDrawing(brush, pen, geometry);

                    if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId))
                    {
                        drawing.SetValue(FrameworkElement.NameProperty, elementId);

                        context.RegisterId(elementId);

                        if (context.IncludeRuntime)
                        {
                            SvgObject.SetId(drawing, elementId);
                        }
                    }

                    Brush    maskBrush = this.Masking;
                    Geometry clipGeom  = this.ClipGeometry;
                    if (clipGeom != null || maskBrush != null)
                    {
                        //Geometry clipped = Geometry.Combine(geometry, clipGeom,
                        //    GeometryCombineMode.Exclude, null);

                        //if (clipped != null && !clipped.IsEmpty())
                        //{
                        //    geometry = clipped;
                        //}
                        DrawingGroup clipMaskGroup = new DrawingGroup();

                        Rect geometryBounds = geometry.Bounds;

                        if (clipGeom != null)
                        {
                            clipMaskGroup.ClipGeometry = clipGeom;

                            SvgUnitType clipUnits = this.ClipUnits;
                            if (clipUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }

                                TransformGroup transformGroup = new TransformGroup();

                                // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                transformGroup.Children.Add(new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
                                transformGroup.Children.Add(new TranslateTransform(drawingBounds.X, drawingBounds.Y));

                                clipGeom.Transform = transformGroup;
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    clipGeom.Transform = transform;
                                }
                            }
                        }
                        if (maskBrush != null)
                        {
                            DrawingBrush drawingBrush = (DrawingBrush)maskBrush;

                            SvgUnitType maskUnits        = this.MaskUnits;
                            SvgUnitType maskContentUnits = this.MaskContentUnits;
                            if (maskUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }
                                DrawingGroup maskGroup = drawingBrush.Drawing as DrawingGroup;
                                if (maskGroup != null)
                                {
                                    DrawingCollection maskDrawings = maskGroup.Children;
                                    for (int i = 0; i < maskDrawings.Count; i++)
                                    {
                                        Drawing         maskDrawing  = maskDrawings[i];
                                        GeometryDrawing maskGeomDraw = maskDrawing as GeometryDrawing;
                                        if (maskGeomDraw != null)
                                        {
                                            if (maskGeomDraw.Brush != null)
                                            {
                                                ConvertColors(maskGeomDraw.Brush);
                                            }
                                            if (maskGeomDraw.Pen != null)
                                            {
                                                ConvertColors(maskGeomDraw.Pen.Brush);
                                            }
                                        }
                                    }
                                }

                                if (maskContentUnits == SvgUnitType.ObjectBoundingBox)
                                {
                                    TransformGroup transformGroup = new TransformGroup();

                                    // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                    var scaleTransform = new ScaleTransform(drawingBounds.Width, drawingBounds.Height);
                                    transformGroup.Children.Add(scaleTransform);
                                    var translateTransform = new TranslateTransform(drawingBounds.X, drawingBounds.Y);
                                    transformGroup.Children.Add(translateTransform);

                                    Matrix matrix = new Matrix();
                                    matrix.Scale(drawingBounds.Width, drawingBounds.Height);
                                    matrix.Translate(drawingBounds.X, drawingBounds.Y);

                                    //maskBrush.Transform = transformGroup;
                                    maskBrush.Transform = new MatrixTransform(matrix);
                                }
                                else
                                {
                                    drawingBrush.Viewbox      = drawingBounds;
                                    drawingBrush.ViewboxUnits = BrushMappingMode.Absolute;

                                    drawingBrush.Stretch = Stretch.Uniform;

                                    drawingBrush.Viewport      = drawingBounds;
                                    drawingBrush.ViewportUnits = BrushMappingMode.Absolute;
                                }
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    maskBrush.Transform = transform;
                                }
                            }

                            clipMaskGroup.OpacityMask = maskBrush;
                        }

                        clipMaskGroup.Children.Add(drawing);
                        drawGroup.Children.Add(clipMaskGroup);
                    }
                    else
                    {
                        drawGroup.Children.Add(drawing);
                    }
                }
            }

            // If this is not the child of a "marker", then try rendering a marker...
            if (!string.Equals(parentNode.LocalName, "marker"))
            {
                RenderMarkers(renderer, styleElm, context);
            }
        }
        private Brush GetBrush(Geometry geometry, string propPrefix,
                               bool setOpacity)
        {
            SvgPaint fill;

            if (PaintType == SvgPaintType.None)
            {
                return(null);
            }
            else if (PaintType == SvgPaintType.CurrentColor)
            {
                fill = new WpfSvgPaint(_context, _element, "color");
            }
            else
            {
                fill = this;
            }

            SvgPaintType paintType = fill.PaintType;

            if (paintType == SvgPaintType.Uri || paintType == SvgPaintType.UriCurrentColor ||
                paintType == SvgPaintType.UriNone || paintType == SvgPaintType.UriRgbColor ||
                paintType == SvgPaintType.UriRgbColorIccColor)
            {
                _paintFill = GetPaintFill(fill.Uri);
                if (_paintFill != null)
                {
                    Brush brush = null;
                    if (geometry != null)
                    {
                        brush = _paintFill.GetBrush(geometry.Bounds, _context);
                    }
                    else
                    {
                        brush = _paintFill.GetBrush(Rect.Empty, _context);
                    }

                    if (brush != null)
                    {
                        brush.Opacity = GetOpacity(propPrefix);
                    }

                    return(brush);
                }
                else
                {
                    if (PaintType == SvgPaintType.UriNone || PaintType == SvgPaintType.Uri)
                    {
                        return(null);
                    }
                    else if (PaintType == SvgPaintType.UriCurrentColor)
                    {
                        fill = new WpfSvgPaint(_context, _element, "color");
                    }
                    else
                    {
                        fill = this;
                    }
                }
            }

            if (fill == null || fill.RgbColor == null)
            {
                return(null);
            }

            Color?solidColor = WpfConvert.ToColor(fill.RgbColor);

            if (solidColor == null)
            {
                return(null);
            }

            SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value);

            //int opacity = GetOpacity(propPrefix);
            //solidBrush.Color = Color.FromArgb(opacity, brush.Color);
            if (setOpacity)
            {
                solidBrush.Opacity = GetOpacity(propPrefix);
            }
            return(solidBrush);
        }
Example #16
0
        private Brush GetBrush(Geometry geometry, string propPrefix, bool setOpacity)
        {
            WpfSvgPaintContext paintContext = null;

            SvgPaintType paintType = this.PaintType;

            WpfSvgPaint fill;

            if (paintType == SvgPaintType.None)
            {
                return(null);
            }
            if (paintType == SvgPaintType.CurrentColor)
            {
                //TODO: Find a better way to support currentColor specified on parent element.
                var deferredFill = this.GetDeferredFill();
                if (deferredFill == null)
                {
                    fill = new WpfSvgPaint(_context, _element, "color");
                }
                else
                {
                    fill = deferredFill;
                }
            }
            else if (paintType == SvgPaintType.ContextFill)
            {
                paintContext = GetFillContext();
                if (paintContext != null)
                {
                    fill = paintContext.Fill;
                }
                else
                {
                    fill = this;
                }
            }
            else if (paintType == SvgPaintType.ContextStroke)
            {
                paintContext = GetStrokeContext();
                if (paintContext != null)
                {
                    fill = paintContext.Stroke;
                }
                else
                {
                    fill = this;
                }
            }
            else
            {
                fill = this;
            }

            SvgPaintType fillType = fill.PaintType;

            if (fillType == SvgPaintType.Uri || fillType == SvgPaintType.UriCurrentColor ||
                fillType == SvgPaintType.UriNone || fillType == SvgPaintType.UriRgbColor ||
                fillType == SvgPaintType.UriRgbColorIccColor)
            {
                _paintFill = GetPaintFill(fill.Uri);
                if (_paintFill != null)
                {
                    Brush brush = null;
                    if (geometry != null)
                    {
                        brush = _paintFill.GetBrush(geometry.Bounds, _context, geometry.Transform);
                    }
                    else
                    {
                        brush = _paintFill.GetBrush(Rect.Empty, _context, null);
                    }

                    if (brush != null)
                    {
                        brush.Opacity = GetOpacity(propPrefix);
                    }

                    return(brush);
                }

                if (paintType == SvgPaintType.UriNone || paintType == SvgPaintType.Uri)
                {
                    return(null);
                }
                if (paintType == SvgPaintType.UriCurrentColor)
                {
                    fill = new WpfSvgPaint(_context, _element, "color");
                }
                else
                {
                    fill = this;
                }
            }

            if (fill == null || fill.RgbColor == null)
            {
                return(null);
            }

            Color?solidColor = WpfConvert.ToColor(fill.RgbColor);

            if (solidColor == null)
            {
                return(null);
            }

            SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value);

            if (setOpacity)
            {
                solidBrush.Opacity = GetOpacity(propPrefix);
            }
            return(solidBrush);
        }