Example #1
0
 /// <summary>
 ///     Set brush
 /// </summary>
 /// <param name="AxialShading">Axial shading</param>
 /// <param name="BrushOpacity">Brush opacity (0.0 to 1.0)</param>
 public void SetBrush
 (
     PdfAxialShading AxialShading,
     double BrushOpacity = 1.0
 )
 {
     NonStroking       = AxialShading;
     this.BrushOpacity = BrushOpacity;
 }
        internal void Draw
        (
            PdfContents Contents,
            double DrawRectX,
            double DrawRectY,
            double DrawRectWidth,
            double DrawRectHeight,
            ContentAlignment Alignment = (ContentAlignment)0
        )
        {
            // save drawing rectangle in user coordinates
            this.DrawRectX      = DrawRectX;
            this.DrawRectY      = DrawRectY;
            this.DrawRectWidth  = DrawRectWidth;
            this.DrawRectHeight = DrawRectHeight;

            // test arguments
            if (DrawRectWidth == 0 && DrawRectHeight == 0 || DrawRectWidth == 0 && PathBBoxWidth != 0 || DrawRectHeight == 0 && PathBBoxHeight != 0)
            {
                throw new ApplicationException("DrawWPFPath: Drawing rectangle is empty");
            }

            // set transformation matrix
            SetTransformation(Alignment);

            // clip
            if (Stroking == null && NonStroking == null)
            {
                // build clipping path
                BuildPath(Contents, FillRule == FillRule.EvenOdd ? PaintOp.ClipPathEor : PaintOp.ClipPathWnr);
                return;
            }

            // paint operator
            PaintOp PaintOperator;

            // brush is defined as shading
            if (NonStroking != null && (NonStroking.GetType() == typeof(SysMedia.LinearGradientBrush) || NonStroking.GetType() == typeof(SysMedia.RadialGradientBrush) ||
                                        NonStroking.GetType() == typeof(PdfAxialShading) || NonStroking.GetType() == typeof(PdfRadialShading)))
            {
                // save graphics state
                Contents.SaveGraphicsState();

                // build clipping path
                BuildPath(Contents, FillRule == FillRule.EvenOdd ? PaintOp.ClipPathEor : PaintOp.ClipPathWnr);

                // set bland mode
                if (BlendMode != BlendMode.Normal)
                {
                    Contents.SetBlendMode(BlendMode);
                }

                // set opacity
                Contents.SetAlphaNonStroking(BrushOpacity);

                // draw linera gradient brush shading bounded by clip path
                if (NonStroking.GetType() == typeof(SysMedia.LinearGradientBrush))
                {
                    PdfAxialShading AxialShading = new PdfAxialShading(Contents.Document, (SysMedia.LinearGradientBrush)NonStroking);
                    AxialShading.SetBoundingBox(DrawRectX, DrawRectY, DrawRectWidth, DrawRectHeight);
                    Contents.DrawShading(AxialShading);
                }

                // draw axial shading bounded by clip path
                else if (NonStroking.GetType() == typeof(PdfAxialShading))
                {
                    ((PdfAxialShading)NonStroking).SetBoundingBox(DrawRectX, DrawRectY, DrawRectWidth, DrawRectHeight);
                    Contents.DrawShading((PdfAxialShading)NonStroking);
                }

                // draw radial gradient brush shading bounded by clip path
                else if (NonStroking.GetType() == typeof(SysMedia.RadialGradientBrush))
                {
                    PdfRadialShading RadialShading = new PdfRadialShading(Contents.Document, (SysMedia.RadialGradientBrush)NonStroking);
                    RadialShading.SetBoundingBox(DrawRectX, DrawRectY, DrawRectWidth, DrawRectHeight);
                    Contents.DrawShading(RadialShading);
                }

                // draw radial shading bounded by clip path
                else
                {
                    ((PdfRadialShading)NonStroking).SetBoundingBox(DrawRectX, DrawRectY, DrawRectWidth, DrawRectHeight);
                    Contents.DrawShading((PdfRadialShading)NonStroking);
                }

                // remove clipping path
                Contents.RestoreGraphicsState();

                // no pen defined
                if (Stroking == null)
                {
                    return;
                }

                // pen is defined
                PaintOperator = PaintOp.Stroke;
            }

            // set paint operator for all other cases (no shading)
            else
            {
                // we have pen and no brush
                if (NonStroking == null)
                {
                    PaintOperator = PaintOp.Stroke;
                }
                // we have brush but no pen
                else if (Stroking == null)
                {
                    PaintOperator = FillRule == FillRule.EvenOdd ? PaintOp.FillEor : PaintOp.Fill;
                }
                // we have brush and pen
                else
                {
                    PaintOperator = FillRule == FillRule.EvenOdd ? PaintOp.CloseFillStrokeEor: PaintOp.CloseFillStroke;
                }
            }

            // save graphics state
            Contents.SaveGraphicsState();

            // set bland mode
            if (BlendMode != BlendMode.Normal)
            {
                Contents.SetBlendMode(BlendMode);
            }

            // stroking (pen) is defined
            if (Stroking != null)
            {
                if (Stroking.GetType() == typeof(Color))
                {
                    // pen color
                    Contents.SetColorStroking((Color)Stroking);

                    // set opacity
                    if (PenOpacity != 1.0)
                    {
                        Contents.SetAlphaStroking(PenOpacity);
                    }

                    // pen width
                    if (PenWidth >= 0)
                    {
                        Contents.SetLineWidth(PenWidth);
                    }

                    // line cap
                    if (LineCap != (PdfLineCap)(-1))
                    {
                        Contents.SetLineCap(LineCap);
                    }

                    // line join
                    if (LineJoin != (PdfLineJoin)(-1))
                    {
                        Contents.SetLineJoin(LineJoin);
                    }

                    // Miter
                    if (MiterLimit != -1)
                    {
                        Contents.SetMiterLimit(MiterLimit);
                    }

                    // line is made of dashes
                    if (DashArray != null)
                    {
                        Contents.SetDashLine(DashArray, DashPhase);
                    }
                }

                else if (Stroking.GetType() == typeof(SysMedia.Pen))
                {
                    // media pen short cut
                    SysMedia.Pen Pen = (SysMedia.Pen)Stroking;

                    // media brush shortcut
                    SysMedia.SolidColorBrush Brush = (SysMedia.SolidColorBrush)Pen.Brush;

                    // media pen color short cut
                    SysMedia.Color PenColor = Brush.Color;

                    // pen color
                    Contents.SetColorStroking(Color.FromArgb(PenColor.R, PenColor.G, PenColor.B));

                    // pen opacity
                    if (PenColor.A != 255 || Brush.Opacity != 1.0)
                    {
                        Contents.SetAlphaStroking((PenColor.A / 255.0) * Brush.Opacity);
                    }

                    // pen thickness converted to user units
                    double Thickness = Pen.Thickness * Math.Max(Math.Abs(ScaleX), Math.Abs(ScaleY));
                    Contents.SetLineWidth(Thickness);

                    // line cap
                    // Note: PDF line cap is the same for start and end. We will ignore EndLineCap
                    // Triangle line cap will be round
                    switch (Pen.StartLineCap)
                    {
                    case SysMedia.PenLineCap.Flat: Contents.SetLineCap(PdfLineCap.Butt); break;

                    case SysMedia.PenLineCap.Square: Contents.SetLineCap(PdfLineCap.Square); break;

                    default: Contents.SetLineCap(PdfLineCap.Round); break;
                    }

                    // line join
                    switch (Pen.LineJoin)
                    {
                    case SysMedia.PenLineJoin.Bevel: Contents.SetLineJoin(PdfLineJoin.Bevel); break;

                    case SysMedia.PenLineJoin.Miter: Contents.SetLineJoin(PdfLineJoin.Miter); break;

                    default: Contents.SetLineJoin(PdfLineJoin.Round); break;
                    }

                    // Miter
                    Contents.SetMiterLimit(Pen.MiterLimit);

                    // dash pattern
                    if (Pen.DashStyle.Dashes.Count > 0)
                    {
                        int      End          = Pen.DashStyle.Dashes.Count;
                        double[] PenDashArray = new double[End];
                        for (int Index = 0; Index < End; Index++)
                        {
                            PenDashArray[Index] = Thickness * Pen.DashStyle.Dashes[Index];
                        }
                        Contents.SetDashLine(PenDashArray, Thickness * Pen.DashStyle.Offset);
                    }
                }
            }

            // non-stroking (brush) is defined
            // note shading brush was handled above
            if (NonStroking != null)
            {
                // set opacity
                if (BrushOpacity != 1.0)
                {
                    Contents.SetAlphaNonStroking(BrushOpacity);
                }

                // brush color
                if (NonStroking.GetType() == typeof(Color))
                {
                    Contents.SetColorNonStroking((Color)NonStroking);
                }

                else if (NonStroking.GetType() == typeof(PdfTilingPattern))
                {
                    Contents.SetPatternNonStroking((PdfTilingPattern)NonStroking);
                }
            }

            // build path
            BuildPath(Contents, PaintOperator);

            // restore graphics state
            Contents.RestoreGraphicsState();
            return;
        }
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Draw axial shading pattern
        /// </summary>
        /// <param name="Shading">Axial shading resource</param>
        ////////////////////////////////////////////////////////////////////
        public void DrawShading(
			PdfAxialShading Shading
			)
        {
            AddToUsedResources(Shading);
            ContentsString.AppendFormat("{0} sh\n", Shading.ResourceCode);
            return;
        }