//void RealizeLinearGradientBrush(LinearGradientBrush brush)
        //{
        //  // HACK
        //  int count = brush.GradientStops.Count;
        //  int idx = count / 2;
        //  RealizeFillHack(brush.GradientStops[idx].Color);
        //  //Debugger.Break();
        //}

        //void RealizeRadialGradientBrush(RadialGradientBrush brush)
        //{
        //  int count = brush.GradientStops.Count;
        //  int idx = count / 2;
        //  RealizeFillHack(brush.GradientStops[idx].Color);
        //}
        #endregion

        /// <summary>
        /// Helper for not yet implemented brush types.
        /// </summary>
        void RealizeFillHack(Color color)
        {
            this.writer.WriteRgb(color, " rg\n");

            //if (this.realizedFillColor.ScA != color.ScA)
            {
                PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(color.ScA);
                string       gs        = this.writer.Resources.AddExtGState(extGState);
                this.writer.WriteLiteral("{0} gs\n", gs);

                // Must create transparany group
                if (color.ScA < 1)
                {
                    this.writer.CreateDefaultTransparencyGroup();
                }
            }
            this.realizedFillColor = color;
        }
        public void RealizeFill(Brush brush, double opacity, ref XForm xform, ref XImage ximage) // PdfColorMode colorMode)
        {
            SolidColorBrush     sbrush;
            LinearGradientBrush lbrush;
            RadialGradientBrush rbrush;
            ImageBrush          ibrush;
            VisualBrush         vbrush;

            if ((sbrush = brush as SolidColorBrush) != null)
            {
                Color color = sbrush.Color.ToXpsColor();
                //color = ColorSpaceHelper.EnsureColorMode(colorMode, color);

                this.writer.WriteRgb(color, " rg\n");

                //if (this.renderer.Owner.Version >= 14 && this.realizedStrokeColor.A != color.A)
                //if (this.realizedFillColor.ScA != color.ScA)
                {
                    PdfExtGState extGState = this.writer.Owner.ExtGStateTable.GetExtGStateNonStroke(color.ScA);
                    string       gs        = this.writer.Resources.AddExtGState(extGState);
                    this.writer.WriteLiteral("{0} gs\n", gs);

                    // Must create transparany group
                    if (color.ScA < 1)
                    {
                        this.writer.CreateDefaultTransparencyGroup();
                    }
                }
                this.realizedFillColor = color;

                //if (colorMode != PdfColorMode.Cmyk)
                //{
                //  if (this.realizedFillColor.Rgb != color.Rgb)
                //  {
                //    this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Rgb));
                //    this.renderer.Append(" rg\n");
                //  }
                //}
                //else
                //{
                //  if (!ColorSpaceHelper.IsEqualCmyk(this.realizedFillColor, color))
                //  {
                //    this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Cmyk));
                //    this.renderer.Append(" k\n");
                //  }
                //}

                //if (this.renderer.Owner.Version >= 14 && this.realizedFillColor.A != color.A)
                //{
                //  PdfExtGState extGState = this.renderer.Owner.ExtGStateTable.GetExtGStateNonStroke(color.A);
                //  string gs = this.renderer.Resources.AddExtGState(extGState);
                //  this.renderer.AppendFormat("{0} gs\n", gs);

                //  // Must create transparany group
                //  if (this.renderer.page != null && color.A < 1)
                //    this.renderer.page.transparencyUsed = true;
                //}
                //this.realizedFillColor = color;
            }
            else if ((lbrush = brush as LinearGradientBrush) != null)
            {
                // NOT IN USE ANYMORE
                //RealizeLinearGradientBrush(lbrush, xform);
            }
            else if ((rbrush = brush as RadialGradientBrush) != null)
            {
                // NOT IN USE ANYMORE
                //RealizeRadialGradientBrush(rbrush, xform);
            }
            else if ((ibrush = brush as ImageBrush) != null)
            {
                // NOT IN USE ANYMORE
                //RealizeImageBrush(ibrush, ref xform, ref ximage);
            }
            else if ((vbrush = brush as VisualBrush) != null)
            {
                // NOT IN USE ANYMORE
                //RealizeVisualBrush(vbrush, ref xform);
            }
            else
            {
                //return new SolidColorBrush(Colors//
                //Debugger.Break();
            }
        }
        public void RealizeStroke(Path path) // XPen pen, PdfColorMode colorMode)
        {
            //XColor color = pen.Color;
            //color = ColorSpaceHelper.EnsureColorMode(colorMode, color);

            var             brush = path.Stroke;
            SolidColorBrush sBrush;

            if ((sBrush = brush as SolidColorBrush) != null)
            {
                var thickness = path.StrokeThickness;

                // Adjust based on path geometry transform since XPS doesn't apply scale to stroke thickness
                if (path.Data.Transform != null && !path.Data.Transform.Value.IsIdentity)
                {
                    var actualThickness = thickness *
                                          ((path.Data.Transform.Value.M11 + path.Data.Transform.Value.M12) + (path.Data.Transform.Value.M21 + path.Data.Transform.Value.M22)) / 2;

                    /*
                     * actual     requir
                     * ------  =  ------
                     * requir     necess
                     */
                    thickness = thickness * thickness / actualThickness;
                }

                //if (this.realizedLineWith != width)
                {
                    this.writer.WriteLiteral("{0:0.###} w\n", thickness);
                    this.realizedLineWith = thickness;
                }

                RealizeStrokeStyle(path);

                var color = sBrush.Color;

                double opacity = path.Opacity * color.ScA;
                if (opacity < 1)
                {
                    var    extGState = this.writer.Owner.ExtGStateTable.GetExtGStateStroke(color.ScA);
                    string gs        = this.writer.Resources.AddExtGState(extGState);
                    this.writer.WriteLiteral("{0} gs\n", gs);

                    // Must create transparany group
                    //if (color.ScA < 1)
                    this.writer.CreateDefaultTransparencyGroup();
                }
                this.writer.WriteRgb(color, " RG\n");

                this.realizedStrokeColor = color.ToXpsColor();
            }
            else
            {
                Debug.WriteLine("Stroke with non SolidColorBrush");
                //throw new NotImplementedException("Stroke with non SolidColorBrush");
            }



            //if (this.realizedLineCap != (int)pen.lineCap)
            //{
            //  this.renderer.AppendFormat("{0} J\n", (int)pen.lineCap);
            //  this.realizedLineCap = (int)pen.lineCap;
            //}

            //if (this.realizedLineJoin != (int)pen.lineJoin)
            //{
            //  this.renderer.AppendFormat("{0} j\n", (int)pen.lineJoin);
            //  this.realizedLineJoin = (int)pen.lineJoin;
            //}

            //if (this.realizedLineCap == (int)XLineJoin.Miter)
            //{
            //  if (this.realizedMiterLimit != (int)pen.miterLimit && (int)pen.miterLimit != 0)
            //  {
            //    this.renderer.AppendFormat("{0} M\n", (int)pen.miterLimit);
            //    this.realizedMiterLimit = (int)pen.miterLimit;
            //  }
            //}

            //if (this.realizedDashStyle != pen.dashStyle || pen.dashStyle == XDashStyle.Custom)
            //{
            //  double dot = pen.Width;
            //  double dash = 3 * dot;

            //  // Line width 0 is not recommended but valid
            //  XDashStyle dashStyle = pen.DashStyle;
            //  if (dot == 0)
            //    dashStyle = XDashStyle.Solid;

            //  switch (dashStyle)
            //  {
            //    case XDashStyle.Solid:
            //      this.renderer.Append("[]0 d\n");
            //      break;

            //    case XDashStyle.Dash:
            //      this.renderer.AppendFormat("[{0:0.##} {1:0.##}]0 d\n", dash, dot);
            //      break;

            //    case XDashStyle.Dot:
            //      this.renderer.AppendFormat("[{0:0.##}]0 d\n", dot);
            //      break;

            //    case XDashStyle.DashDot:
            //      this.renderer.AppendFormat("[{0:0.##} {1:0.##} {1:0.##} {1:0.##}]0 d\n", dash, dot);
            //      break;

            //    case XDashStyle.DashDotDot:
            //      this.renderer.AppendFormat("[{0:0.##} {1:0.##} {1:0.##} {1:0.##} {1:0.##} {1:0.##}]0 d\n", dash, dot);
            //      break;

            //    case XDashStyle.Custom:
            //      {
            //        StringBuilder pdf = new StringBuilder("[", 256);
            //        int len = pen.dashPattern == null ? 0 : pen.dashPattern.Length;
            //        for (int idx = 0; idx < len; idx++)
            //        {
            //          if (idx > 0)
            //            pdf.Append(' ');
            //          pdf.Append(PdfEncoders.ToString(pen.dashPattern[idx] * pen.width));
            //        }
            //        // Make an even number of values look like in GDI+
            //        if (len > 0 && len % 2 == 1)
            //        {
            //          pdf.Append(' ');
            //          pdf.Append(PdfEncoders.ToString(0.2 * pen.width));
            //        }
            //        pdf.AppendFormat(CultureInfo.InvariantCulture, "]{0:0.###} d\n", pen.dashOffset * pen.width);
            //        string pattern = pdf.ToString();

            //        // BUG: [email protected] reported a realizing problem
            //        // HACK: I romove the if clause
            //        //if (this.realizedDashPattern != pattern)
            //        {
            //          this.realizedDashPattern = pattern;
            //          this.renderer.Append(pattern);
            //        }
            //      }
            //      break;
            //  }
            //  this.realizedDashStyle = dashStyle;
            //}

            //if (colorMode != PdfColorMode.Cmyk)
            //{
            //if (this.realizedStrokeColor.Rgb != color.Rgb)
            //{
            //  this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Rgb));
            //  this.renderer.Append(" RG\n");
            //}
            //}
            //else
            //{
            //  if (!ColorSpaceHelper.IsEqualCmyk(this.realizedStrokeColor, color))
            //  {
            //    this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Cmyk));
            //    this.renderer.Append(" K\n");
            //  }
            //}

            //if (this.renderer.Owner.Version >= 14 && this.realizedStrokeColor.A != color.A)
            //{
            //  PdfExtGState extGState = this.renderer.Owner.ExtGStateTable.GetExtGStateStroke(color.A);
            //  string gs = this.renderer.Resources.AddExtGState(extGState);
            //  this.renderer.AppendFormat("{0} gs\n", gs);

            //  // Must create transparany group
            //  if (this.renderer.page != null && color.A < 1)
            //    this.renderer.page.transparencyUsed = true;
            //}
            //this.realizedStrokeColor = color;
        }