public void ShouldRenderAFilledCubicBezierCurve() { // 76, 410, 93, 312, 119, 188, 193, 190, // 193, 190, 267, 190, 292, 366, 311, 521, var p0 = new Point(76, 410) * 0.4; // start point var c0 = new Point(115, 190) * 0.4; // control point 0 var c1 = new Point(273, 190) * 0.4; // control point 1 var p1 = new Point(311, 521) * 0.4; // end point var p = new Point((c0.X + c1.X) / 2, (c0.Y + c1.Y) / 2); Application.Run(new Form1(), () => { var d = new PathGeometryBuilder(); d.BeginPath(); d.MoveTo(p0); d.QuadraticCurveTo(c0, p); d.QuadraticCurveTo(c1, p1); d.Fill(); var g = Form.current.ForegroundDrawingContext; g.DrawGeometry(new Brush(Color.Black), null, d.ToGeometry()); g.DrawLine(new Pen(Color.Red, 2), p0, c0); g.DrawLine(new Pen(Color.Green, 2), c0, c1); g.DrawLine(new Pen(Color.Blue, 2), c1, p1); g.DrawEllipse(new Brush(Color.Gold), null, p, 4, 4); }); }
public void ShouldRenderAFilledCubicBezierCurve2() { // (625,1549) (1040,1508) (1444, 1168) (1442,794) var p0 = new Point(625, 1549) * 0.4; // start point var c0 = new Point(1040, 1508) * 0.4; // control point 0 var c1 = new Point(1444, 1168) * 0.4; // control point 1 var p1 = new Point(1442, 794) * 0.4; // end point var p = new Point((c0.X + c1.X) / 2, (c0.Y + c1.Y) / 2); Application.Run(new Form1(), () => { var d = new PathGeometryBuilder(); d.BeginPath(); d.MoveTo(p0); d.QuadraticCurveTo(c0, p); d.QuadraticCurveTo(c1, p1); d.Fill(); var g = Form.current.ForegroundDrawingContext; g.DrawGeometry(new Brush(Color.Black), null, d.ToGeometry()); g.DrawLine(new Pen(Color.Red, 2), p0, c0); g.DrawLine(new Pen(Color.Green, 2), c0, c1); g.DrawLine(new Pen(Color.Blue, 2), c1, p1); g.DrawEllipse(new Brush(Color.Gold), null, p, 4, 4); }); }
public void ExportGeometryCase2() { PathGeometryBuilder g = new PathGeometryBuilder(); g.BeginPath(); g.Rect(10, 10, 100, 200, false); 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(10, 10)); Assert.False(figure.IsClosed); Assert.False(figure.IsFilled); var segments = figure.Segments; Assert.Single(segments); Assert.IsType <PolyLineSegment>(segments[0]); var segment = segments[0] as PolyLineSegment; Assert.True(segment.IsStroked); Assert.Equal(segment.Points, new[] { new Point(110, 10), new Point(110, 210), new Point(10, 210), new Point(10, 10) }); }
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]); }
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)); }
public void ExportEmptyGeometry() { { PathGeometryBuilder g = new PathGeometryBuilder(); g.BeginPath(); var geometry = g.ToGeometry(); var pathGeometry = geometry as PathGeometry; Assert.NotNull(pathGeometry); Assert.Empty(pathGeometry.Figures); } { PathGeometryBuilder g = new PathGeometryBuilder(); g.BeginPath(); g.ClosePath(); var geometry = g.ToGeometry(); var pathGeometry = geometry as PathGeometry; Assert.NotNull(pathGeometry); Assert.Empty(pathGeometry.Figures); } { PathGeometryBuilder g = new PathGeometryBuilder(); g.BeginPath(); g.ClosePath(); g.Stroke(); var geometry = g.ToGeometry(); var pathGeometry = geometry as PathGeometry; Assert.NotNull(pathGeometry); Assert.Single(pathGeometry.Figures); Assert.Empty(pathGeometry.Figures[0].Segments); } }
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); }