Esempio n. 1
0
        protected internal override Path GetPath()
        {
            var points = ControlPoints;
            var path   = new Path();

            path.MoveTo(points[0].ToPointF());

            for (var i = 1; i < points.Length; i++)
            {
                path.LineTo(points[i].ToPointF());
            }

            var transform = Transform;

            if (!transform.IsEmpty)
            {
                using (var matrix = new Matrix())
                {
                    var bounds = ControlBounds;

                    matrix.Translate(-bounds.X - bounds.Width / 2, -bounds.Y - bounds.Height / 2);
                    matrix.Scale((float)transform.ScaleX, (float)transform.ScaleY, MatrixOrder.Append);
                    matrix.Rotate((float)transform.Angle, MatrixOrder.Append);
                    matrix.Translate(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2, MatrixOrder.Append);
                    matrix.Translate((float)transform.TranslateX, (float)transform.TranslateY, MatrixOrder.Append);

                    path.ApplyTransform(matrix);
                }
            }

            return(path);
        }
Esempio n. 2
0
        private Path[] GetPaths()
        {
            var horizontalLineLength = Cols * StepX;
            var verticalLineLength   = Rows * StepY;

            var left = ControlPoints[0].X;
            var top  = ControlPoints[0].Y;

            var verticalPath = new Path();

            for (int i = 0; i <= Cols; i++)
            {
                var x = left + i * StepX;
                verticalPath.MoveTo(x, top);
                verticalPath.LineTo(x, verticalLineLength + top);
            }

            var horizonltalPath = new Path();

            for (int i = 0; i <= Rows; i++)
            {
                var y = top + i * StepY;
                horizonltalPath.MoveTo(left, y);
                horizonltalPath.LineTo(horizontalLineLength + left, y);
            }

            return(new[] { verticalPath, horizonltalPath });
        }
Esempio n. 3
0
        internal static Path FromAdvancedPath(AdvancedPath advancedPath)
        {
            var path = new Path();

            var i = 0;

            while (i < advancedPath.Points.Count)
            {
                var point = advancedPath.Points[i];

                switch (point.Type)
                {
                case PathPointType.Close:
                    path.Close();
                    break;

                case PathPointType.Move:
                    path.MoveTo(point.X, point.Y);
                    break;

                case PathPointType.Line:
                    path.LineTo(point.X, point.Y);
                    break;

                case PathPointType.Bezier3:
                    var point1 = advancedPath.Points[i + 1];
                    if (point1.Type == PathPointType.Bezier3)
                    {
                        path.QuadraticTo(point.X, point.Y, point1.X, point1.Y);
                        i += 1;
                    }
                    else
                    {
                        path.LineTo(point.X, point.Y);
                    }
                    break;

                case PathPointType.Bezier4:
                    point1 = advancedPath.Points[i + 1];
                    var point2 = advancedPath.Points[i + 2];
                    if (point1.Type == PathPointType.Bezier4 && point2.Type == PathPointType.Bezier4)
                    {
                        path.CubicTo(point.X, point.Y, point1.X, point1.Y, point2.X, point2.Y);
                        i += 2;
                    }
                    else
                    {
                        path.LineTo(point.X, point.Y);
                    }
                    break;
                }

                i++;
            }

            return(path);
        }
Esempio n. 4
0
        protected internal override Path GetPath()
        {
            var path = new Path();

            foreach (var p in GetPaths())
            {
                path.DrawPath(p);
            }

            return(path);
        }
Esempio n. 5
0
        private Path[] GetPaths()
        {
            var path    = new Path();
            var altPath = new Path();

            var p0 = Point0;
            var p1 = Point1;

            var lineWidth  = p1.X - p0.X;
            var lineHeight = p1.Y - p0.Y;
            var lineLength = SMath.Sqrt(SMath.Pow(lineWidth, 2) + SMath.Pow(lineHeight, 2));
            var sin        = lineHeight / lineLength;
            var cos        = lineWidth / lineLength;

            var dashCount = (int)SMath.Ceiling(lineLength / (DashWidth + AltDashWidth));
            var point     = p0.Clone();

            for (var i = 0; i < dashCount; i++)
            {
                path.MoveTo(point.ToPointF());
                point.Translate(DashWidth * cos, DashWidth * sin);

                if (SMath.Abs(point.X - p0.X) <= SMath.Abs(lineWidth) && SMath.Abs(point.Y - p0.Y) <= SMath.Abs(lineHeight))
                {
                    path.LineTo(point.ToPointF());

                    altPath.MoveTo(point.ToPointF());
                    point.Translate(AltDashWidth * cos, AltDashWidth * sin);

                    if (SMath.Abs(point.X - p0.X) <= SMath.Abs(lineWidth) && SMath.Abs(point.Y - p0.Y) <= SMath.Abs(lineHeight))
                    {
                        altPath.LineTo(point.ToPointF());
                    }
                    else
                    {
                        altPath.LineTo(p1.ToPointF());
                    }
                }
                else
                {
                    path.LineTo(p1.ToPointF());
                }
            }

            return(new[] { path, altPath });
        }
        private void UpdateRectangle()
        {
            if (BoundingPaths == null || BoundingPaths.Length == 0)
            {
                return;
            }

            var union = BoundingPaths[0].ToAdvancedPath();

            for (var i = 1; i < BoundingPaths.Length; i++)
            {
                union = AdvancedPath.Union(union, BoundingPaths[i].ToAdvancedPath());
            }

            var rectangle = union.GetBounds();

            ChangeControlPoints(rectangle.X, rectangle.Y, rectangle.Right, rectangle.Bottom);
        }
Esempio n. 7
0
        internal AdvancedPath ToAdvancedPath()
        {
            var path = new AdvancedPath();

            foreach (var segment in _segments)
            {
                switch (segment.Name.ToUpper())
                {
                case "Z":
                    path.Close();
                    break;

                case "M":
                    if (segment.Points.Count == 1)
                    {
                        path.MoveTo(segment.Points[0].ToPointF());
                    }
                    break;

                case "L":
                    if (segment.Points.Count == 1)
                    {
                        path.LineTo(segment.Points[0].ToPointF());
                    }
                    break;

                case "Q":
                    if (segment.Points.Count == 2)
                    {
                        path.CurveTo(segment.Points[0].ToPointF(), segment.Points[1].ToPointF());
                    }
                    break;

                case "C":
                    if (segment.Points.Count == 3)
                    {
                        path.CurveTo(segment.Points[0].ToPointF(), segment.Points[1].ToPointF(), segment.Points[2].ToPointF());
                    }
                    break;
                }
            }

            return(path);
        }
Esempio n. 8
0
        internal override void Draw(Graphics graphics, IImageParams destImageParams, ColorManagement colorManagement)
        {
            var bounds = Bounds;

            if (bounds.Width <= 0 || bounds.Height <= 0)
            {
                return;
            }

            FillShape(graphics, destImageParams, colorManagement);

            if (!IsStubOrEmptyContent)
            {
                using (var clippingPath = GetClippingPath(graphics.DpiX))
                {
                    var globalClippingPath = graphics.ClippingPaths.FirstOrDefault();
                    if (globalClippingPath != null)
                    {
                        graphics.ClippingPaths.Remove(globalClippingPath);

                        using (var intersectedClippingPath = AdvancedPath.Intersect(clippingPath, globalClippingPath))
                        {
                            graphics.ClippingPaths.Add(intersectedClippingPath);
                            Content.Draw(graphics, destImageParams, colorManagement);
                            graphics.ClippingPaths.Remove(intersectedClippingPath);
                        }

                        graphics.ClippingPaths.Add(globalClippingPath);
                    }
                    else
                    {
                        graphics.ClippingPaths.Add(clippingPath);
                        Content.Draw(graphics, destImageParams, colorManagement);
                        graphics.ClippingPaths.Remove(clippingPath);
                    }
                }
            }

            StrokeShape(graphics, destImageParams, colorManagement);
        }
        protected Path GetWrappingPath(float dpi = 72)
        {
            var rectangle = Rectangle;
            var path      = new Path();

            foreach (var r in WrappingRectangles.Where(r => !r.IsEmpty && rectangle.IntersectsWith(r)))
            {
                var rectToWrap = r.Clone();
                rectToWrap.Width  += WrappingMargin * 2;
                rectToWrap.Height += WrappingMargin * 2;

                path.DrawPath(rectToWrap.GetPath());
            }

            if (path.Points.Count > 0)
            {
                using (var matrix = new Matrix())
                {
                    if (!Utils.EqualsOfFloatNumbers(Angle, 0))
                    {
                        matrix.RotateAt(-(float)Angle, rectangle.Center.ToPointF());
                    }

                    if (!Utils.EqualsOfFloatNumbers(1, HorizontalScale) || !Utils.EqualsOfFloatNumbers(1, VerticalScale))
                    {
                        matrix.Scale(1 / HorizontalScale, 1 / VerticalScale, MatrixOrder.Append);
                    }

                    if (!Utils.EqualsOfFloatNumbers(dpi, 72))
                    {
                        matrix.Scale(dpi / 72, dpi / 72, MatrixOrder.Append);
                    }

                    path.ApplyTransform(matrix);
                }
            }

            return(path);
        }
Esempio n. 10
0
        public void RenderLayers(PipelineElement writer,
                                 IEnumerable <Layer> layers,
                                 SizeF workspace,
                                 float dpi                       = 300f,
                                 ColorSpace colorSpace           = ColorSpace.Rgb,
                                 Color backgroundColor           = null,
                                 bool isPreview                  = false,
                                 SizeF document                  = new SizeF(),
                                 ColorManagement colorManagement = null,
                                 float?targetDpi                 = null
                                 )
        {
            if (workspace.Width <= 0 || workspace.Height <= 0)
            {
                throw new ArgumentException(null, "workspace");
            }

            if (dpi <= 0)
            {
                throw new ArgumentOutOfRangeException("dpi", dpi, Exceptions.DpiOutOfRange);
            }

            colorSpace = colorSpace == ColorSpace.Unknown ? ColorSpace.Rgb : colorSpace;

            if (colorSpace != ColorSpace.Cmyk && colorSpace != ColorSpace.Grayscale && colorSpace != ColorSpace.Rgb)
            {
                throw new ArgumentException(null, "colorSpace");
            }

            // Turn on proofing if (colorManagement != null && isPreview)
            var initialTargetColorSpace = colorManagement != null ? colorManagement.TargetColorSpace : null;

            if (colorManagement != null)
            {
                colorManagement.TargetColorSpace = isPreview ? new ColorSpace?(colorSpace) : null;
                colorSpace = isPreview ? ColorSpace.Rgb : colorSpace;
            }

            backgroundColor = backgroundColor ?? ColorManagement.GetWhiteColor(colorSpace);

            var format  = ColorManagement.GetPixelFormat(colorSpace);
            var profile = ColorManagement.GetProfile(colorManagement, colorSpace, isPreview);

            var pixelSize = new Size(
                Common.ConvertPointsToPixels(dpi, workspace.Width),
                Common.ConvertPointsToPixels(dpi, workspace.Height));

            var pdfWriter = writer as PdfWriter;

            if (pdfWriter != null)
            {
                var docWidth  = pixelSize.Width;
                var docHeight = pixelSize.Height;
                var scale     = 1f;
                var offset    = PointF.Empty;
                if (!document.IsEmpty)
                {
                    docWidth  = Common.ConvertPointsToPixels(dpi, document.Width);
                    docHeight = Common.ConvertPointsToPixels(dpi, document.Height);

                    var scaleX = docWidth / (float)pixelSize.Width;
                    var scaleY = docHeight / (float)pixelSize.Height;

                    scale = System.Math.Min(scaleX, scaleY);
                    if (Utils.EqualsOfFloatNumbers(scaleX, scaleY, 0.001f))
                    {
                        scale     = (float)System.Math.Round(scale, 2);
                        docWidth  = Common.ConvertPointsToPixels(dpi, workspace.Width * scale);
                        docHeight = Common.ConvertPointsToPixels(dpi, workspace.Height * scale);
                    }
                    else
                    {
                        offset.X = (docWidth - pixelSize.Width * scale) / 2f;
                        offset.Y = (docHeight - pixelSize.Height * scale) / 2f;
                    }
                }

                pdfWriter.AddPage(docWidth, docHeight, dpi, dpi, backgroundColor, profile);
                using (var graphics = pdfWriter.GetGraphics())
                {
                    var path = new Path();
                    path.DrawRectangle(0, 0, pixelSize.Width, pixelSize.Height);

                    graphics.Transform.Scale(scale, scale, MatrixOrder.Append);
                    graphics.Transform.Translate(offset.X, offset.Y, MatrixOrder.Append);
                    graphics.ClippingPaths.Add(path);

                    var vObjects = GetVObjects(layers, workspace).ToArray();

                    using (var imageParams = new ImageGenerator(pixelSize, format, backgroundColor)
                    {
                        DpiX = dpi, DpiY = dpi, ColorProfile = profile
                    })
                    {
                        if (colorManagement != null)
                        {
                            colorManagement.InitColorMap(from v in vObjects from c in v.GetColors() select c, imageParams);
                        }

                        DrawVObjects(vObjects, graphics, imageParams, colorManagement);
                    }

                    graphics.ClippingPaths.Remove(path);
                    graphics.Transform.Translate(-offset.X, -offset.Y, MatrixOrder.Append);
                    graphics.Transform.Scale(1 / scale, 1 / scale, MatrixOrder.Append);
                }
            }
            else
            {
                var            vObjectGroups            = new List <List <VObject> >();
                List <VObject> currentVObjectGroup      = null;
                List <VObject> currentImageVObjectGroup = null;

                var vObjects = GetVObjects(layers, workspace).ToArray();

                foreach (var vObject in vObjects)
                {
                    if (vObject is IPipelineExtender)
                    {
                        if (currentVObjectGroup != null)
                        {
                            vObjectGroups.Add(currentVObjectGroup);
                            currentVObjectGroup = null;
                        }

                        if (currentImageVObjectGroup == null)
                        {
                            currentImageVObjectGroup = new List <VObject>();
                        }

                        currentImageVObjectGroup.Add(vObject);
                    }
                    else
                    {
                        if (currentImageVObjectGroup != null)
                        {
                            vObjectGroups.Add(currentImageVObjectGroup);
                            currentImageVObjectGroup = null;
                        }

                        if (currentVObjectGroup == null)
                        {
                            currentVObjectGroup = new List <VObject>();
                        }

                        currentVObjectGroup.Add(vObject);
                    }
                }

                if (currentVObjectGroup != null)
                {
                    vObjectGroups.Add(currentVObjectGroup);
                }

                if (currentImageVObjectGroup != null)
                {
                    vObjectGroups.Add(currentImageVObjectGroup);
                }

                var pipeline = new Pipeline();
                var deps     = new List <IDisposable>();

                var scale = dpi / targetDpi ?? 1;

                try
                {
                    var bitmapGenerator = new ImageGenerator(pixelSize.Width, pixelSize.Height, format, backgroundColor)
                    {
                        DpiX         = dpi / scale,
                        DpiY         = dpi / scale,
                        ColorProfile = profile
                    };

                    if (colorManagement != null)
                    {
                        colorManagement.InitColorMap(from v in vObjects from c in v.GetColors() select c, bitmapGenerator);
                    }

                    pipeline.Add(bitmapGenerator);

                    foreach (var group in vObjectGroups)
                    {
                        if (group.Count == 0)
                        {
                            break;
                        }

                        if (group.FirstOrDefault() is IPipelineExtender)
                        {
                            foreach (var vObject in group)
                            {
                                IEnumerable <IDisposable> vObjectDeps;
                                ((IPipelineExtender)vObject).ExtendPipeline(pipeline, bitmapGenerator, colorManagement, scale, out vObjectDeps);
                                deps.AddRange(vObjectDeps);
                            }
                        }
                        else
                        {
                            var groupVObjects = group.ToArray();
                            var drawer        = new Drawer();
                            drawer.Draw += (sender, e) =>
                            {
                                if (!Utils.EqualsOfFloatNumbers(scale, 1))
                                {
                                    e.Graphics.Transform.Scale(scale, scale);
                                }

                                DrawVObjects(groupVObjects, e.Graphics, bitmapGenerator, colorManagement);

                                if (!Utils.EqualsOfFloatNumbers(scale, 1))
                                {
                                    e.Graphics.Transform.Scale(1 / scale, 1 / scale);
                                }
                            };
                            pipeline.Add(drawer);
                        }
                    }

                    // Check if writer support pixel format with or without alpha.
                    if (!writer.IsPixelFormatSupported(bitmapGenerator.PixelFormat) &&
                        writer.IsPixelFormatSupported(PixelFormat.DiscardAlpha(bitmapGenerator.PixelFormat)))
                    {
                        pipeline.Add(new RemoveAlpha(backgroundColor));
                    }

                    if (!Utils.EqualsOfFloatNumbers(scale, 1))
                    {
                        pipeline.Add(new ResolutionModifier(dpi, dpi));
                    }

                    pipeline.Add(writer);
                    pipeline.Run();
                }
                finally
                {
                    pipeline.Remove(writer);
                    pipeline.DisposeAllElements();

                    if (deps != null)
                    {
                        foreach (var dep in deps)
                        {
                            dep.Dispose();
                        }
                    }

                    if (colorManagement != null)
                    {
                        colorManagement.TargetColorSpace = initialTargetColorSpace;
                    }
                }
            }

            if (colorManagement != null)
            {
                colorManagement.TargetColorSpace = initialTargetColorSpace;
            }
        }