Пример #1
0
            public void ExportGeometryCase1()
            {
                PathGeometryBuilder g = new PathGeometryBuilder();

                g.BeginPath();
                g.MoveTo(50, 140);
                g.LineTo(150, 60);
                g.LineTo(250, 140);
                g.ClosePath();
                g.Stroke();
                var geometry = g.ToGeometry();

                var pathGeometry = geometry as PathGeometry;

                Assert.NotNull(pathGeometry);
                Assert.Single(pathGeometry.Figures);

                var figure = pathGeometry.Figures[0];

                Assert.NotNull(figure);
                Assert.Equal(figure.StartPoint, new Point(50, 140));
                Assert.True(figure.IsClosed);
                Assert.False(figure.IsFilled);

                var segments = figure.Segments;

                Assert.Equal(3, segments.Count);

                Assert.IsType <LineSegment>(segments[0]);
                var segment0 = segments[0] as LineSegment;

                Assert.True(segment0.IsStroked);
                Assert.Equal(segment0.Point, new Point(150, 60));

                Assert.IsType <LineSegment>(segments[1]);
                var segment1 = segments[1] as LineSegment;

                Assert.True(segment1.IsStroked);
                Assert.Equal(segment1.Point, new Point(250, 140));

                Assert.IsType <LineSegment>(segments[2]);
                var segment2 = segments[2] as LineSegment;

                Assert.True(segment2.IsStroked);
                Assert.Equal(segment2.Point, new Point(50, 140));
            }
Пример #2
0
            public void ExportGeometryMultipleFigures()
            {
                PathGeometryBuilder g = new PathGeometryBuilder();

                g.BeginPath();
                g.MoveTo(50, 140);
                g.LineTo(150, 60);
                g.LineTo(250, 140);
                g.ClosePath();
                g.Stroke();

                g.BeginPath();
                g.Rect(10, 10, 100, 200, false);
                g.Stroke();

                var geometry = g.ToGeometry();

                var pathGeometry = geometry as PathGeometry;

                Assert.NotNull(pathGeometry);
                Assert.Equal(2, pathGeometry.Figures.Count);
                Assert.NotNull(pathGeometry.Figures[0]);
                Assert.NotNull(pathGeometry.Figures[1]);
            }
Пример #3
0
        public void ShowGlyphAsDirectedContours_Builtin()
        {
            //load the glyph
            Typeface typeFace;

            using (var fs = Utility.ReadFile(Utility.FontDir + fontFileName))
            {
                var reader = new OpenFontReader();
                typeFace = reader.Read(fs);
            }
            Glyph  glyph       = typeFace.Lookup(character);
            Bounds boundingBox = typeFace.Bounds;
            short  ascender    = typeFace.Ascender;

            //read polygons and quadratic bezier segments
            GlyphLoader.Read(glyph, out var polygons, out var quadraticBezierSegments);

            //The actual position of points should apply ascender offset
            var offset = new Vector(0, ascender);

            //polygons
            Geometry polygonGeometry;
            {
                var d = new PathGeometryBuilder();
                d.BeginPath();
                foreach (var polygon in polygons)
                {
                    var startPoint = polygon[0] + offset;
                    d.MoveTo(startPoint);
                    var previousPoint = startPoint;
                    foreach (var point in polygon.Skip(1))
                    {
                        var p = point + offset;
                        d.LineTo(p);
                        //DrawArrow(d, lastPoint, point);
                        previousPoint = p;
                    }
                    d.LineTo(startPoint);
                    //DrawArrow(d, lastPoint, point);
                    d.Stroke();
                }
                polygonGeometry = d.ToGeometry();
            }
            var polygonPen = new Pen(polygonColor, polygonLineWidth);

            //quadratic bezier segments
            Geometry quadraticGeometry;
            {
                var d = new PathGeometryBuilder();
                d.BeginPath();
                foreach (var qs in quadraticBezierSegments)
                {
                    d.MoveTo(qs.Item1 + offset);
                    d.QuadraticCurveTo(qs.Item2 + offset, qs.Item3 + offset);
                    d.Stroke();
                }
                quadraticGeometry = d.ToGeometry();
            }
            var quadraticPen = new Pen(quadraticSegmentColor, quadraticLineWidth);

            //start points
            Geometry startPointGeometry;
            {
                var d = new PathGeometryBuilder();
                for (var i = 0; i < polygons.Count; i++)
                {
                    var polygon    = polygons[i];
                    var startPoint = polygon[0] + offset;
                    d.Circle(startPoint, 10);
                    d.Fill();
                }
                startPointGeometry = d.ToGeometry();
            }
            var startPointBrush = new Brush(startPointColor);

            //create a DrawingVisual to hold geometries
            DrawingVisual drawingVisual = new DrawingVisual(0);

            //create geometries and save to DrawingVisual's content
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            drawingContext.DrawGeometry(null, polygonPen, polygonGeometry);
            drawingContext.DrawGeometry(null, quadraticPen, quadraticGeometry);
            drawingContext.DrawGeometry(startPointBrush, null, startPointGeometry);
            drawingContext.Close();

            //draw the drawingVisual to image
            int width = boundingBox.XMax - boundingBox.XMin, height = boundingBox.YMax - boundingBox.YMin;

            Util.DrawDrawingVisualToImage(out var imageBytes, width, height, drawingVisual);

            //save and show the image
            var path = $"{OutputDir}{Path.DirectorySeparatorChar}{nameof(ShowGlyphAsDirectedContours_Builtin)}_{fontFileName}_{character}.png";

            Util.ShowRawPixelsFrom_glReadPixels_NotOpenFolder(imageBytes, width, height, path);
        }
Пример #4
0
        public void ShowGlyphAsOverlappingPolygon_Builtin()
        {
            //load the glyph
            Typeface typeFace;

            using (var fs = Utility.ReadFile(Utility.FontDir + fontFileName))
            {
                var reader = new OpenFontReader();
                typeFace = reader.Read(fs);
            }
            Glyph  glyph       = typeFace.Lookup(character);
            Bounds boundingBox = typeFace.Bounds;
            short  ascender    = typeFace.Ascender;

            //read polygons and quadratic bezier segments
            GlyphLoader.Read(glyph, out var polygons, out var quadraticBezierSegments);

            //The actual position of points should apply ascender offset
            var offset = new Vector(0, ascender);

            //polygons
            var      polygonBrush = new Brush(Color.Argb(128, 10, 10, 10));
            Geometry polygonGeometry;
            {
                var d = new PathGeometryBuilder();
                foreach (var polygon in polygons)
                {
                    if (polygon.Count == 0)
                    {
                        continue;
                    }

                    d.MoveTo(polygon[0] + offset);
                    foreach (var point in polygon.Skip(1))
                    {
                        d.LineTo(point + offset);
                    }
                    d.Fill();
                }
                polygonGeometry = d.ToGeometry();
            }

            //polygon points
            Geometry pointsGeometry;
            {
                var d = new PathGeometryBuilder();
                foreach (var polygon in polygons)
                {
                    foreach (var point in polygon)
                    {
                        d.Circle(point + offset, 5);
                        d.Fill();
                    }
                }
                pointsGeometry = d.ToGeometry();
            }
            var pointsBrush = new Brush(Color.Argb(200, 0, 0, 0));

            //create a DrawingVisual to hold geometries
            DrawingVisual drawingVisual = new DrawingVisual(0);

            //create geometries and save to DrawingVisual's content
            DrawingContext drawingContext = drawingVisual.RenderOpen();

            drawingContext.DrawGeometry(pointsBrush, null, pointsGeometry);
            drawingContext.DrawGeometry(polygonBrush, null, polygonGeometry);
            drawingContext.Close();

            //draw the drawingVisual to image
            int width = boundingBox.XMax - boundingBox.XMin, height = boundingBox.YMax - boundingBox.YMin;

            Util.DrawDrawingVisualToImage(out var imageBytes, width, height, drawingVisual);

            //save and show the image
            var path = $"{OutputDir}{Path.DirectorySeparatorChar}{nameof(ShowGlyphAsOverlappingPolygon_Builtin)}_{fontFileName}_{character}.png";

            Util.ShowRawPixelsFrom_glReadPixels_NotOpenFolder(imageBytes, width, height, path);
        }