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 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); }