예제 #1
0
        /// <summary>
        /// Builds a form XObject from a linear gradient brush that uses transparency.
        /// </summary>
        public static PdfFormXObject BuildFormFromLinearGradientBrush(DocumentRenderingContext context, LinearGradientBrush brush, PathGeometry geometry)
        {
            LinearShadingBuilder builder = new LinearShadingBuilder(context);
            PdfFormXObject       pdfForm = builder.BuildForm(brush, geometry);

            return(pdfForm);
        }
예제 #2
0
        /// <summary>
        /// Builds a shading from a linear gradient brush.
        /// </summary>
        public static PdfShading BuildShadingFromLinearGradientBrush(DocumentRenderingContext context, LinearGradientBrush brush)
        {
            LinearShadingBuilder builder = new LinearShadingBuilder(context);
            PdfShading           shading = builder.BuildShading(brush);

            return(shading);
        }
예제 #3
0
        /// <summary>
        /// Builds a pattern from a linear gradient brush.
        /// </summary>
        public static PdfShadingPattern BuildPatternFromLinearGradientBrush(DocumentRenderingContext context, LinearGradientBrush brush, XMatrix transform)
        {
            LinearShadingBuilder builder = new LinearShadingBuilder(context);
            PdfShadingPattern    pattern = builder.BuildShadingPattern(brush, transform);

            return(pattern);
        }
예제 #4
0
        /// <summary>
        /// Strokes the path geometry with the Stroke brush.
        /// </summary>
        private void WriteStrokeGeometry(Path path)
        {
            if (path.Stroke == null)
            {
                return;
            }

            SolidColorBrush     sBrush;
            LinearGradientBrush lgBrush;
            RadialGradientBrush rgBrush;
            VisualBrush         vBrush;
            ImageBrush          iBrush;

            //if (path.Stroke != null && this.renderMode == RenderMode.Default) // HACK

            if ((sBrush = path.Stroke as SolidColorBrush) != null)
            {
                RealizeStroke(path);
                WriteGeometry(path.Data);
                WriteLiteral("S\n");
            }
            else if ((lgBrush = path.Stroke as LinearGradientBrush) != null)
            {
                PdfExtGState xgState = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>();
                xgState.SetDefault1();

                double opacity = Opacity * lgBrush.Opacity;;
                if (opacity < 1)
                {
                    xgState.StrokeAlpha    = opacity;
                    xgState.NonStrokeAlpha = opacity;
                }
                RealizeExtGState(xgState);

                // /CS1 CS /P0 SCN
                // 7.5 w
                // q 1 0 0 1 15.5 462.9 cm
                // 0 0 m
                // 153 0 l
                // 153 -93 l
                // 0 -93 l
                // h
                // S
                // Q
                if (lgBrush.GradientStops.HasTransparency)
                {
                    // TODO: Create Form
                    PdfShadingPattern pattern = LinearShadingBuilder.BuildPatternFromLinearGradientBrush(Context, lgBrush, Transform);
                    string            paName  = Resources.AddPattern(pattern);
                    WriteLiteral("/Pattern CS " + paName + " SCN\n");
                    WriteLiteral("q {0:0.###} w", path.StrokeThickness);
                    WriteGeometry(path.Data);
                    WriteLiteral("S Q\n");

                    //// Create a FormXObject with a soft mask
                    //PdfFormXObject form = LinearShadingBuilder.BuildFormFromLinearGradientBrush(Context, lgBrush, path.Data);
                    //string foName = Resources.AddForm(form);
                    //WriteLiteral(foName + " Do\n");
                }
                else
                {
                    PdfShadingPattern pattern = LinearShadingBuilder.BuildPatternFromLinearGradientBrush(Context, lgBrush, Transform);
                    string            paName  = Resources.AddPattern(pattern);
                    WriteLiteral("/Pattern CS " + paName + " SCN\n");
                    WriteLiteral("q {0:0.###} w", path.StrokeThickness);
                    WriteGeometry(path.Data);
                    WriteLiteral("S Q\n");
                }
            }
            else if ((rgBrush = path.Stroke as RadialGradientBrush) != null)
            {
                // HACK
                WriteLiteral("/DeviceRGB CS 0 1 0 RG\n");
                WriteLiteral("q {0:0.###} w", path.StrokeThickness);
                WriteGeometry(path.Data);
                WriteLiteral("S Q\n");
            }
            else if ((iBrush = path.Stroke as ImageBrush) != null)
            {
                // HACK
                WriteLiteral("/DeviceRGB CS 0 1 0 RG\n");
                WriteLiteral("q {0:0.###} w", path.StrokeThickness);
                WriteGeometry(path.Data);
                WriteLiteral("S Q\n");
            }
            else if ((vBrush = path.Stroke as VisualBrush) != null)
            {
                // HACK
                WriteLiteral("/DeviceRGB CS 0 1 0 RG\n");
                WriteLiteral("q {0:0.###} w", path.StrokeThickness);
                WriteGeometry(path.Data);
                WriteLiteral("S Q\n");
            }
            else
            {
                Debug.Assert(false);
            }
        }
예제 #5
0
        /// <summary>
        /// Writes a Path to the content stream.
        /// </summary>
        private void WritePath(Path path)
        {
            if (WriteSingleLineStrokeWithSpecialCaps(path))
            {
                return;
            }

            WriteSaveState("begin Path", path.Name);

            // Transform also affects clipping and opacity mask
            if (path.RenderTransform != null && this.renderMode == RenderMode.Default)
            {
                MultiplyTransform(path.RenderTransform);
                WriteRenderTransform(path.RenderTransform);
            }

            if (path.Clip != null && this.renderMode == RenderMode.Default)
            {
                WriteClip(path.Clip);
            }

            if (path.Opacity < 1)
            {
                MultiplyOpacity(path.Opacity);
            }

            if (path.OpacityMask != null)
            {
                WriteOpacityMask(path.OpacityMask);
            }

            if (path.Fill == null)
            {
                if (path.Stroke != null)
                {
                    WriteStrokeGeometry(path);
                }
                else
                {
                    Debug.Assert(false, "??? Path with neither Stroke nor Fill encountered.");
                }
            }
            else
            {
                SolidColorBrush     sBrush;
                LinearGradientBrush lgBrush;
                RadialGradientBrush rgBrush;
                ImageBrush          iBrush;
                VisualBrush         vBrush;
                if ((sBrush = path.Fill as SolidColorBrush) != null)
                {
                    Color  color   = sBrush.Color;
                    double opacity = Opacity * color.ScA;
                    if (opacity < 1)
                    {
                        RealizeFillOpacity(opacity);
                    }

                    WriteRgb(color, " rg\n");

                    if (path.Stroke != null)
                    {
                        RealizeStroke(path);
                    }

                    WriteGeometry(path.Data);
                    WritePathFillStroke(path);
                }
                else if ((lgBrush = path.Fill as LinearGradientBrush) != null)
                {
                    // TODO: For better visual compatibility use a Shading Pattern if Opacity is not 1 and
                    // the Stroke Style is not solid. Acrobat 8 ignores this case.

                    PdfExtGState xgState = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>();
                    xgState.SetDefault1();

                    double opacity = Opacity * lgBrush.Opacity;;
                    if (opacity < 1)
                    {
                        xgState.StrokeAlpha    = opacity;
                        xgState.NonStrokeAlpha = opacity;
                    }
                    RealizeExtGState(xgState);

                    // 1st draw fill
                    if (lgBrush.GradientStops.HasTransparency)
                    {
                        // Create a FormXObject with a soft mask
                        PdfFormXObject form   = LinearShadingBuilder.BuildFormFromLinearGradientBrush(Context, lgBrush, path.Data);
                        string         foName = Resources.AddForm(form);
                        WriteLiteral(foName + " Do\n");
                    }
                    else
                    {
                        // Create just a shading
                        PdfShading shading = LinearShadingBuilder.BuildShadingFromLinearGradientBrush(Context, lgBrush);
                        string     shName  = Resources.AddShading(shading);
                        WriteLiteral("q\n");
                        WriteClip(path.Data);
                        WriteLiteral("BX " + shName + " sh EX Q\n");
                    }

                    // 2nd draw stroke
                    if (path.Stroke != null)
                    {
                        WriteStrokeGeometry(path);
                    }
                }
                else if ((rgBrush = path.Fill as RadialGradientBrush) != null)
                {
                    PdfExtGState xgState = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>();
                    xgState.SetDefault1();

                    double avGradientOpacity = rgBrush.GradientStops.GetAverageAlpha(); // HACK
                    double opacity           = Opacity * rgBrush.Opacity * avGradientOpacity;
                    if (opacity < 1)
                    {
                        xgState.StrokeAlpha    = opacity;
                        xgState.NonStrokeAlpha = opacity;
                    }
                    //RealizeExtGState(xgState);

                    XRect boundingBox = path.Data.GetBoundingBox();
                    // Always creates a pattern, because the background must be filled
                    PdfShadingPattern pattern = RadialShadingBuilder.BuildFromRadialGradientBrush(Context, rgBrush, boundingBox, Transform);
                    string            paName  = Resources.AddPattern(pattern);

                    // stream
                    // /CS0 cs /P0 scn
                    // /GS0 gs
                    // 0 480 180 -90 re
                    // f*
                    // endstream
                    // endobj
                    WriteLiteral("/Pattern cs " + paName + " scn\n");
                    // move to here: RealizeExtGState(xgState);
                    RealizeExtGState(xgState);
                    WriteGeometry(path.Data);
                    if (path.Data.FillRule == FillRule.NonZero) // NonZero means Winding
                    {
                        WriteLiteral("f\n");
                    }
                    else
                    {
                        WriteLiteral("f*\n");
                    }

                    // 2nd draw stroke
                    if (path.Stroke != null)
                    {
                        WriteStrokeGeometry(path);
                    }
                }
                else if ((iBrush = path.Fill as ImageBrush) != null)
                {
                    PdfExtGState xgState = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>();
                    xgState.SetDefault1();

                    double opacity = Opacity * iBrush.Opacity;
                    if (opacity <= 1)
                    {
                        xgState.StrokeAlpha    = opacity;
                        xgState.NonStrokeAlpha = opacity;
                    }
                    RealizeExtGState(xgState);

                    // 1st draw fill
                    PdfTilingPattern pattern = TilingPatternBuilder.BuildFromImageBrush(Context, iBrush, Transform);
                    string           name    = Resources.AddPattern(pattern);

                    WriteLiteral("/Pattern cs " + name + " scn\n");
                    WriteGeometry(path.Data);
                    WritePathFillStroke(path);

                    // 2nd draw stroke
                    if (path.Stroke != null)
                    {
                        WriteStrokeGeometry(path);
                    }
                }
                else if ((vBrush = path.Fill as VisualBrush) != null)
                {
                    PdfExtGState xgState = Context.PdfDocument.Internals.CreateIndirectObject <PdfExtGState>();
                    xgState.SetDefault1();

                    double opacity = Opacity * vBrush.Opacity;
                    if (opacity < 1)
                    {
                        xgState.StrokeAlpha    = opacity;
                        xgState.NonStrokeAlpha = opacity;
                    }
                    RealizeExtGState(xgState);

                    // 1st draw fill
                    PdfTilingPattern pattern = TilingPatternBuilder.BuildFromVisualBrush(Context, vBrush, Transform);
                    string           name    = Resources.AddPattern(pattern);

                    WriteLiteral("/Pattern cs " + name + " scn\n");
                    WriteGeometry(path.Data);
                    WritePathFillStroke(path);

                    // 2nd draw stroke
                    if (path.Stroke != null)
                    {
                        WriteStrokeGeometry(path);
                    }
                }
                else
                {
                    Debug.Assert(false, "Unknown brush type encountered.");
                }
            }
            WriteRestoreState("end Path", path.Name);
        }
 /// <summary>
 /// Builds a form XObject from a linear gradient brush that uses transparency.
 /// </summary>
 public static PdfFormXObject BuildFormFromLinearGradientBrush(DocumentRenderingContext context, LinearGradientBrush brush, PathGeometry geometry)
 {
   LinearShadingBuilder builder = new LinearShadingBuilder(context);
   PdfFormXObject pdfForm = builder.BuildForm(brush, geometry);
   return pdfForm;
 }
 /// <summary>
 /// Builds a pattern from a linear gradient brush.
 /// </summary>
 public static PdfShadingPattern BuildPatternFromLinearGradientBrush(DocumentRenderingContext context, LinearGradientBrush brush, XMatrix transform)
 {
   LinearShadingBuilder builder = new LinearShadingBuilder(context);
   PdfShadingPattern pattern = builder.BuildShadingPattern(brush, transform);
   return pattern;
 }
 /// <summary>
 /// Builds a shading from a linear gradient brush.
 /// </summary>
 public static PdfShading BuildShadingFromLinearGradientBrush(DocumentRenderingContext context, LinearGradientBrush brush)
 {
   LinearShadingBuilder builder = new LinearShadingBuilder(context);
   PdfShading shading = builder.BuildShading(brush);
   return shading;
 }