Exemple #1
0
        private static void RenderToGraphics(Render.RenderContext ctx, int rot, float translateX, float translateY, XGraphics graphics)
        {
            graphics.TranslateTransform(translateX, translateY);
            graphics.RotateTransform(rot * 90);

            using (Maps.Rendering.RenderUtil.SaveState(graphics))
            {
                if (ctx.clipPath != null)
                {
                    XMatrix m = ctx.ImageSpaceToWorldSpace;
                    graphics.MultiplyTransform(m);
                    graphics.IntersectClip(ctx.clipPath);
                    m.Invert();
                    graphics.MultiplyTransform(m);
                }

                ctx.graphics = graphics;
                Maps.Rendering.Render.RenderTile(ctx);
            }


            if (ctx.border && ctx.clipPath != null)
            {
                using (Maps.Rendering.RenderUtil.SaveState(graphics))
                {
                    // Render border in world space
                    XMatrix m = ctx.ImageSpaceToWorldSpace;
                    graphics.MultiplyTransform(m);
                    XPen pen = new XPen(ctx.styles.imageBorderColor, 0.2f);

                    // PdfSharp can't ExcludeClip so we take advantage of the fact that we know
                    // the path starts on the left edge and proceeds clockwise. We extend the
                    // path with a counterclockwise border around it, then use that to exclude
                    // the original path's region for rendering the border.
                    ctx.clipPath.Flatten();
                    RectangleF bounds = PathUtil.Bounds(ctx.clipPath);
                    bounds.Inflate(2 * (float)pen.Width, 2 * (float)pen.Width);
                    List <byte>   types  = new List <byte>(ctx.clipPath.Internals.GdiPath.PathTypes);
                    List <PointF> points = new List <PointF>(ctx.clipPath.Internals.GdiPath.PathPoints);

                    PointF key = points[0];
                    points.Add(new PointF(bounds.Left, key.Y)); types.Add(1);
                    points.Add(new PointF(bounds.Left, bounds.Bottom)); types.Add(1);
                    points.Add(new PointF(bounds.Right, bounds.Bottom)); types.Add(1);
                    points.Add(new PointF(bounds.Right, bounds.Top)); types.Add(1);
                    points.Add(new PointF(bounds.Left, bounds.Top)); types.Add(1);
                    points.Add(new PointF(bounds.Left, key.Y)); types.Add(1);
                    points.Add(new PointF(key.X, key.Y)); types.Add(1);

                    XGraphicsPath path = new XGraphicsPath(points.ToArray(), types.ToArray(), XFillMode.Winding);
                    graphics.IntersectClip(path);
                    graphics.DrawPath(pen, ctx.clipPath);
                }
            }
        }
Exemple #2
0
        public static void DrawLabel(XGraphics g, string text, PointF labelPos, XFont font, XBrush brush, LabelStyle labelStyle)
        {
            using (RenderUtil.SaveState(g))
            {
                XTextFormatter tf = new XTextFormatter(g);
                tf.Alignment = XParagraphAlignment.Center;

                XMatrix matrix = new XMatrix();
                matrix.TranslatePrepend(labelPos.X, labelPos.Y);
                matrix.ScalePrepend(1.0f / Astrometrics.ParsecScaleX, 1.0f / Astrometrics.ParsecScaleY);

                if (labelStyle.Uppercase)
                {
                    text = text.ToUpper();
                }
                if (labelStyle.Wrap)
                {
                    text = text.Replace(' ', '\n');
                }

                matrix.TranslatePrepend(labelStyle.Translation.X, labelStyle.Translation.Y);
                matrix.RotatePrepend(labelStyle.Rotation);
                matrix.ScalePrepend(labelStyle.Scale.Width, labelStyle.Scale.Height);
                g.MultiplyTransform(matrix, XMatrixOrder.Prepend);

                XSize size = g.MeasureString(text, font);
                size.Width *= 2; // prevent cut-off e.g. when rotated
                XRect bounds = new XRect(-size.Width / 2, -size.Height / 2, size.Width, size.Height);
                tf.DrawString(text, font, brush, bounds);
            }
        }
Exemple #3
0
        /// <summary>
        /// Resets the clipping.
        /// </summary>
        private void RestoreInitialClip()
        {
            //Get back to the initial state
            _graphics.Restore(_initialState);
            _initialState = _graphics.Save();

            //Restore transform
            _graphics.MultiplyTransform(_transform);

            _clippingRegion = new Region();
        }
Exemple #4
0
        /// <summary>
        /// Resets the clipping.
        /// </summary>
        private void RestoreInitialClip()
        {
            //Get back to the initial state
            graphics.Restore(initialState);
            initialState = graphics.Save();

            //Restore transform
            graphics.MultiplyTransform(transform);

            clippingRegion = new Region();
        }
Exemple #5
0
        public override void PutText(string text, double x, double y, int fontSize, bool isBold)
        {
            var font = new XFont(FontFamily, XUnit.FromPoint(fontSize).Millimeter, isBold ? XFontStyle.Bold : XFontStyle.Regular);

            // With our transformation to mirror the y axis, text printed using DrawString also gets mirrored.
            // As a workaround we move our current transformation to the x/y position and then mirror the y axis again, and then restore the previous transformation.
            XGraphics.Save();
            XGraphics.TranslateTransform(x, y);
            XGraphics.MultiplyTransform(new XMatrix(1.0, 0.0, 0.0, -1.0, 0.0, 0.0));
            XGraphics.DrawString(text, font, XBrushes.Black, 0.0, 0.0);
            XGraphics.Restore();
        }
Exemple #6
0
        public PdfSharpCanvas(PdfPage page, string fontFamily)
        {
            FontFamily = fontFamily;
            SetupFontMetrics(FontFamily);

            XGraphics = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append, XGraphicsUnit.Millimeter, XPageDirection.Downwards);

            // The origin of an XGraphics canvas is in the top left corner, with the y axis pointing downwards.
            // This transform mirrors the y axis. Unfortunately this also mirrors text output (see workaround in PutText).
            // XGraphics.FromPdfPage can be called with XPageDirection.Upwards, which is marked as obsolete because it is not implemented properly
            // it suffers from the same problem as our matrix below.
            XGraphics.MultiplyTransform(new XMatrix(1.0, 0.0, 0.0, -1.0, 0.0, XGraphics.PageSize.Height));

            // Save the current state so that we can always revert any transformations we apply.
            XGraphics.Save();
        }
        public void DrawName(XGraphics graphics, RectangleF rect, MapOptions options, XFont font, XBrush textBrush, LabelStyle labelStyle)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }

            RectangleF bounds = TransformedBounds;

            if (bounds.IntersectsWith(rect))
            {
                if (Name != null)
                {
                    string str = Name;
                    if (labelStyle.Uppercase)
                    {
                        str = str.ToUpperInvariant();
                    }


                    PointF pos = NamePosition;// PointF( bounds.Left + bounds.Width / 2, bounds.Top + bounds.Height / 2 );

                    using (RenderUtil.SaveState(graphics))
                    {
                        XMatrix matrix = new XMatrix();
                        matrix.TranslatePrepend(pos.X, pos.Y);
                        matrix.ScalePrepend(1.0f / Astrometrics.ParsecScaleX, 1.0f / Astrometrics.ParsecScaleY);
                        matrix.RotatePrepend(-labelStyle.Rotation); // Rotate it
                        graphics.MultiplyTransform(matrix, XMatrixOrder.Prepend);

                        // TODO: Accomodate wrapping
                        //SizeF size = graphics.MeasureString( str, font, bounds.Size);
                        XSize size = graphics.MeasureString(str, font);
                        graphics.TranslateTransform(-size.Width / 2, -size.Height / 2);                          // Center the text
                        //graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                        RectangleF textBounds = new RectangleF(0, 0, (float)size.Width, (float)size.Height * 2); // *2 or it gets cut off at high sizes
                        graphics.DrawString(str, font, textBrush, textBounds, RenderUtil.StringFormatTopCenter);
                    }
                }
            }
        }
        public void Fill(XGraphics graphics, RectangleF rect, Brush fillBrush)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }

            RectangleF bounds = TransformedBounds;

            if (bounds.IntersectsWith(rect))
            {
                XGraphicsPath path = Path;

                using (RenderUtil.SaveState(graphics))
                {
                    XMatrix matrix = new XMatrix();
                    matrix.ScalePrepend(ScaleX, ScaleY);
                    matrix.TranslatePrepend(-OriginX, -OriginY);
                    graphics.MultiplyTransform(matrix);
                    graphics.DrawPath(fillBrush, path);
                }
            }
        }
        public void Draw(XGraphics graphics, RectangleF rect, MapOptions options, XPen pen)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }

            RectangleF bounds = TransformedBounds;

            //graphics.DrawRectangle( new XPen(XColors.Yellow, 1), bounds.X, bounds.Y, bounds.Width, bounds.Height );

            if (bounds.IntersectsWith(rect))
            {
                XGraphicsPath path = Path;
                using (RenderUtil.SaveState(graphics))
                {
                    XMatrix matrix = new XMatrix();
                    matrix.ScalePrepend(ScaleX, ScaleY);
                    matrix.TranslatePrepend(-OriginX, -OriginY);
                    graphics.MultiplyTransform(matrix, XMatrixOrder.Prepend);
                    graphics.DrawPath(pen, path);
                }
            }
        }
Exemple #10
0
        public override void RenderPage(XGraphics gfx)
        {
            base.RenderPage(gfx);

            PointF[] origins = new PointF[]
            {
                new PointF(100, 200), new PointF(300, 200),
                new PointF(100, 400), new PointF(300, 400),
                new PointF(100, 600), new PointF(350, 600),
            };
            PointF             origin;
            XGraphicsContainer container;
            float length = 100;

            // Not transformed
            origin = origins[0];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, origin);

            // Translation
            container = gfx.BeginContainer(new RectangleF(10, 10, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[1];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(20, -30);
            DrawAxes(gfx, XPens.DarkGray, origin, length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, origin);
            gfx.EndContainer(container);

            // Scaling
            container = gfx.BeginContainer(new RectangleF(0, 0, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[2];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.ScaleTransform(1.3f, 1.5f);
            DrawAxes(gfx, XPens.DarkGray, new PointF(), length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, 0, 0);
            gfx.EndContainer(container);

            // Rotation
            container = gfx.BeginContainer(new RectangleF(0, 0, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[3];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.RotateTransform(-45);
            DrawAxes(gfx, XPens.DarkGray, new PointF(), length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, 0, 0);
            gfx.EndContainer(container);

            // Skewing (or shearing)
            container = gfx.BeginContainer(new RectangleF(0, 0, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[4];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.MultiplyTransform(new Matrix(1, -0.3f, -0.4f, 1, 0, 0));
            DrawAxes(gfx, XPens.DarkGray, new PointF(), length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, 0, 0);
            gfx.EndContainer(container);

            // Reflection
            container = gfx.BeginContainer(new RectangleF(0, 0, 1, 1), new RectangleF(0, 0, 1, 1), XGraphicsUnit.Point);
            origin    = origins[5];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.MultiplyTransform(new Matrix(-1, 0, 0, -1, 0, 0));
            DrawAxes(gfx, XPens.DarkGray, new PointF(), length);
            gfx.DrawString(this.properties.Font2.Text, this.properties.Font2.Font, this.properties.Font2.Brush, 0, 0);
            gfx.EndContainer(container);
        }
Exemple #11
0
        public override void RenderPage(XGraphics gfx)
        {
            //base.RenderPage(gfx);

            XPoint[] origins = new XPoint[]
            {
                new XPoint(100, 200), new XPoint(300, 200),
                new XPoint(100, 400), new XPoint(300, 400),
                new XPoint(100, 600), new XPoint(350, 600),
            };

            XPoint         origin;
            XGraphicsState state;
            float          length = 100;

            // Not transformed
            origin = origins[0];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.DrawString(this.properties.Font1.Text, this.properties.Font1.Font, this.properties.Font1.Brush, origin);

            // Translation
            state  = gfx.Save();
            origin = origins[1];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(20, -30);
            DrawAxes(gfx, XPens.DarkGray, origin, length);
            gfx.DrawString(this.properties.Font1.Text, this.properties.Font1.Font, this.properties.Font1.Brush, origin);
            gfx.Restore(state);

#if true
            // Scaling
            state  = gfx.Save();
            origin = origins[2];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.ScaleTransform(1.3, 1.5);
            DrawAxes(gfx, XPens.DarkGray, new XPoint(), length);
            gfx.DrawString(this.properties.Font1.Text, this.properties.Font1.Font, this.properties.Font1.Brush, 0, 0);
            gfx.Restore(state);

            // Rotation
            state  = gfx.Save();
            origin = origins[3];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.RotateTransform(-45);
            DrawAxes(gfx, XPens.DarkGray, new XPoint(), length);
            gfx.DrawString(this.properties.Font1.Text, this.properties.Font1.Font, this.properties.Font1.Brush, 0, 0);
            gfx.Restore(state);

            // Skewing (or shearing)
            state  = gfx.Save();
            origin = origins[4];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.MultiplyTransform(new Matrix(1, -0.3f, -0.4f, 1, 0, 0));
            DrawAxes(gfx, XPens.DarkGray, new XPoint(), length);
            gfx.DrawString(this.properties.Font1.Text, this.properties.Font1.Font, this.properties.Font1.Brush, 0, 0);
            gfx.Restore(state);

            // Reflection
            state  = gfx.Save();
            origin = origins[5];
            DrawAxes(gfx, XPens.Black, origin, length);
            gfx.TranslateTransform(origin.X, origin.Y);
            gfx.MultiplyTransform(new Matrix(-1, 0, 0, -1, 0, 0));
            DrawAxes(gfx, XPens.DarkGray, new XPoint(), length);
            gfx.DrawString(this.properties.Font1.Text, this.properties.Font1.Font, this.properties.Font1.Brush, 0, 0);
            gfx.Restore(state);
#endif
        }