//--------------------------------------------------------------------- private static void Mask() { Action <Surface> draw = surface => { using (var ctx = new Context(surface)) { ctx.Scale(500, 500); Gradient linpat = new LinearGradient(0, 0, 1, 1); linpat.AddColorStop(0, new Color(0, 0.3, 0.8)); linpat.AddColorStop(1, new Color(1, 0.8, 0.3)); Gradient radpat = new RadialGradient(0.5, 0.5, 0.25, 0.5, 0.5, 0.6); radpat.AddColorStop(0, new Color(0, 0, 0, 1)); radpat.AddColorStop(1, new Color(0, 0, 0, 0)); ctx.Source = linpat; //ctx.Paint(); ctx.Mask(radpat); } }; using (Surface surface = new ImageSurface(Format.Argb32, 500, 500)) { draw(surface); surface.WriteToPng("mask.png"); } using (Surface surface = new PdfSurface("mask.pdf", 500, 500)) draw(surface); using (Surface surface = new SvgSurface("mask.svg", 500, 500)) draw(surface); }
protected override void OnDrawPage(PrintContext context, int page_nr) { Context cairoContext; if (previewSurface != null) { var file = System.IO.Path.GetTempFileName(); var target = new SvgSurface(file, formArea.Width, formArea.Height); cairoContext = new Context(target); pageFiles.Add(file); } else { cairoContext = context.CairoContext; } pageContexts.Add(cairoContext); using (var dp = new DrawingProviderCairo(cairoContext, context.CreatePangoLayout())) { dp.DrawingScaleX = drawingScaleX; dp.DrawingScaleY = drawingScaleY; formToPrint.DrawingProvider = dp; formToPrint.Draw(page_nr, new PointD()); } }
//--------------------------------------------------------------------- private static void AntiAlias() { Action <Surface> draw = surface => { using (var ctx = new Context(surface)) { // Sets the anti-aliasing method: ctx.Antialias = Antialias.Subpixel; // Sets the line width: ctx.LineWidth = 9; // red, green, blue, alpha: ctx.Color = new Color(0, 0, 0, 1); // Sets the Context's start point: ctx.MoveTo(10, 10); // Draws a "virtual" line: ctx.LineTo(40, 60); // Stroke the line to the image surface: ctx.Stroke(); ctx.Antialias = Antialias.Gray; ctx.LineWidth = 8; ctx.Color = new Color(1, 0, 0, 1); ctx.LineCap = LineCap.Round; ctx.MoveTo(10, 50); ctx.LineTo(40, 100); ctx.Stroke(); // Fastest method but low quality: ctx.Antialias = Antialias.None; ctx.LineWidth = 7; ctx.MoveTo(10, 90); ctx.LineTo(40, 140); ctx.Stroke(); } }; using (Surface surface = new ImageSurface(Format.Argb32, 70, 150)) { draw(surface); // Save the image as a png image: surface.WriteToPng("antialias.png"); } using (Surface surface = new PdfSurface("antialias.pdf", 70, 150)) draw(surface); using (Surface surface = new SvgSurface("antialias.svg", 70, 150)) draw(surface); }
//--------------------------------------------------------------------- private static void Arrows() { Action <Surface> draw = surface => { using (var c = new Context(surface)) { c.Scale(300, 300); // Is only relevant to PNG c.Antialias = Antialias.Subpixel; // adjust line width due scaling double ux = 1, uy = 1; c.DeviceToUserDistance(ref ux, ref uy); c.LineWidth = Math.Max(ux, uy); c.MoveTo(0, 0.1); c.LineTo(1, 0.1); c.MoveTo(0, 0.9); c.LineTo(1, 0.9); c.Stroke(); var arrow = new Arrow(); c.Color = KnownColors.Blue; arrow.DrawArrow(c, 0.1, 0.1, 0.2, 0.9); arrow.DrawVector(c, 0.2, 0.1, 0.3, 0.9); arrow = new OpenArrow(); c.Color = new Color(0, 1, 0); arrow.DrawArrow(c, 0.3, 0.1, 0.4, 0.9); arrow.DrawVector(c, 0.4, 0.1, 0.5, 0.9); arrow = new CircleArrow(0.01); c.Color = new Color(1, 0, 0); arrow.DrawArrow(c, 0.5, 0.1, 0.6, 0.9); arrow.DrawVector(c, 0.6, 0.1, 0.7, 0.9); } }; using (Surface surface = new PdfSurface("arrows.pdf", 300, 300)) { draw(surface); // PNG can also be created this way surface.WriteToPng("arrows.png"); } using (Surface surface = new PSSurface("arrows.eps", 300, 300)) draw(surface); using (Surface surface = new SvgSurface("arrows.svg", 300, 300)) draw(surface); }
//--------------------------------------------------------------------- private static void Shapes() { Action <Surface> draw = surface => { using (var c = new Context(surface)) { c.Antialias = Antialias.Subpixel; // Hexagon: Shape shape = new Hexagon(50); shape.Draw(c, 50, 50); shape.Fill(c, 50, 50, new Color(0.5, 0.5, 0.5)); // Square: shape = new Square(50); shape.Draw(c, 150, 50); shape.Fill(c, 150, 50, new Color(0.5, 0.5, 0.5)); // Circle: shape = new Circle(50); shape.Draw(c, 100, 150); shape.Fill(c, 100, 150, new Color(0.5, 0.5, 0.5)); // Bounding box: var boundingBox = new Square(50); c.LineWidth = 1; var red = new Color(1, 0, 0); boundingBox.Draw(c, 50, 50, red); boundingBox.Draw(c, 150, 50, red); boundingBox.Draw(c, 100, 150, red); } }; using (Surface surface = new ImageSurface(Format.Argb32, 180, 180)) { draw(surface); surface.WriteToPng("shapes.png"); } using (Surface surface = new PdfSurface("shapes.pdf", 180, 180)) draw(surface); using (Surface surface = new PSSurface("shapes.eps", 300, 300)) draw(surface); using (Surface surface = new SvgSurface("shapes.svg", 180, 180)) draw(surface); }
//--------------------------------------------------------------------- private static void Demo02() { Action <Surface> draw = surface => { using (var c = new Context(surface)) { c.Scale(500, 500); Gradient radpat = new RadialGradient(0.25, 0.25, 0.1, 0.5, 0.5, 0.5); radpat.AddColorStop(0, new Color(1.0, 0.8, 0.8)); radpat.AddColorStop(1, new Color(0.9, 0.0, 0.0)); for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { c.Rectangle(i / 10d - 0.04, j / 10d - 0.04, 0.08, 0.08); } } c.Source = radpat; c.Fill(); Gradient linpat = new LinearGradient(0.25, 0.35, 0.75, 0.65); linpat.AddColorStop(0.00, new Color(1, 1, 1, 0)); linpat.AddColorStop(0.25, new Color(0, 1, 0, 0.5)); linpat.AddColorStop(0.50, new Color(1, 1, 1, 0)); linpat.AddColorStop(0.75, new Color(0, 0, 1, 0.5)); linpat.AddColorStop(1.00, new Color(1, 1, 1, 0)); c.Rectangle(0, 0, 1, 1); c.Source = linpat; c.Fill(); } }; using (Surface surface = new ImageSurface(Format.Argb32, 500, 500)) { draw(surface); surface.WriteToPng("demo02.png"); } using (Surface surface = new PdfSurface("demo02.pdf", 500, 500)) draw(surface); using (Surface surface = new SvgSurface("demo02.svg", 500, 500)) draw(surface); }
static void Main() { const int rectWidth = 200; const int rectHeight = 20; const int space = 5; // sorted per HSV colorspace List <(Color Color, string Name)> colors = GetKnownColors() .Select(c => (c, c.Color.ToHSV())) .OrderBy(cc => cc.Item2[0]) .ThenBy(cc => cc.Item2[1]) .ThenBy(cc => cc.Item2[2]) .Select(cc => cc.c) .ToList(); const int surfaceWidth = rectWidth + 2 * space; int surfaceHeight = (rectHeight + space) * (colors.Count + 1); using (var surface = new SvgSurface("known_colors.svg", surfaceWidth, surfaceHeight)) using (var context = new Context(surface)) { context.LineWidth = 0.35; context.SelectFontFace("Arial", FontSlant.Normal, FontWeight.Normal); context.SetFontSize(10); for (int i = 0; i < colors.Count; ++i) { int y = 10 + i * (rectHeight + space); context.Rectangle(10, y, rectWidth, rectHeight); context.Color = colors[i].Color; context.FillPreserve(); context.Color = new Color(0, 0, 0); context.Stroke(); string name = colors[i].Name; double textWidth = context.GetTextWidth(name); context.Color = colors[i].Color.GetInverseColor(); context.MoveTo((surfaceWidth - textWidth) / 2, y + context.FontExtents.Height); context.ShowText(name); } } }
public static void Main(string[] args) { // call the snippets Snippets snip = new Snippets(); foreach (string snippet in Snippets.snippets) { string filename = "./" + snippet + ".svg"; Surface surface = new SvgSurface(filename, IMAGE_WIDTH, IMAGE_WIDTH); Context cr = new Context(surface); cr.Save(); Snippets.InvokeSnippet(snip, snippet, cr, IMAGE_WIDTH, IMAGE_HEIGHT); cr.ShowPage(); cr.Restore(); surface.Finish(); } }
//--------------------------------------------------------------------- private static void Gradient() { Action <Surface> draw = surface => { using (var c = new Context(surface)) { Gradient pat = new LinearGradient(0.0, 0.0, 0.0, 256.0); pat.AddColorStopRgba(1, 0, 0, 0, 1); pat.AddColorStopRgba(0, 1, 1, 1, 1); c.Rectangle(0, 0, 256, 256); c.SetSource(pat); c.Fill(); pat.Dispose(); pat = new RadialGradient(115.2, 102.4, 25.6, 102.4, 102.4, 128.0); pat.AddColorStopRgba(0, 1, 1, 1, 1); pat.AddColorStopRgba(1, 0, 0, 0, 1); c.SetSource(pat); c.Arc(128.0, 128.0, 76.8, 0, 2 * Math.PI); c.Fill(); pat.Dispose(); } }; using (Surface surface = new ImageSurface(Format.Argb32, 500, 500)) { draw(surface); surface.WriteToPng("gradient.png"); } using (Surface surface = new PdfSurface("gradient.pdf", 500, 500)) { draw(surface); surface.WriteToPng("gradient1.png"); } using (Surface surface = new SvgSurface("gradient.svg", 500, 500)) { draw(surface); surface.WriteToPng("gradient2.png"); } }
//--------------------------------------------------------------------- private static void Demo01() { Action <Surface> draw = surface => { using (var c = new Context(surface)) { c.Scale(500, 500); c.Color = new Color(0, 0, 0); c.MoveTo(0, 0); // absetzen und neu beginnen c.LineTo(1, 1); c.MoveTo(1, 0); c.LineTo(0, 1); c.LineWidth = 0.2; c.Stroke(); // Lininen zeichnen c.Rectangle(0, 0, 0.5, 0.5); c.Color = new Color(1, 0, 0, 0.8); c.Fill(); c.Rectangle(0, 0.5, 0.5, 0.5); c.Color = new Color(0, 1, 0, 0.6); c.Fill(); c.Rectangle(0.5, 0, 0.5, 0.5); c.Color = new Color(0, 0, 0, 0.4); c.Fill(); } }; using (Surface surface = new ImageSurface(Format.Argb32, 500, 500)) { draw(surface); surface.WriteToPng("demo01.png"); } using (Surface surface = new PdfSurface("demo01.pdf", 500, 500)) draw(surface); using (Surface surface = new SvgSurface("demo01.svg", 500, 500)) draw(surface); }
public static void handleMonth2Column(int year, int month) { var surface = new SvgSurface(month.ToString("00") + ".svg", width, height); Cairo.Context cr = new Context(surface); cr.SetSourceRGBA(0.0, 0.0, 0.0, 1.0); PrintMonthHeader(cr, year, month); var d = new DateTime(year, month, 1); var lastDayOfMonth = DateTime.DaysInMonth(d.Year, d.Month); //start with the first column int currentDay; var BoxHeight = (ContentHeight + WeekdayHeader) / 15; var BoxWidth = ContentWidth - width / 2; for (; d.Day <= 15; d = d.AddDays(1)) { var DayBox = new Rectangle( MarginLeft, MarginUp + MonthHeader + (d.Day - 1) * BoxHeight, BoxWidth, BoxHeight); outputDayBox(cr, d, DayBox); } for (; d.Month == month; d = d.AddDays(1)) { var DayBox = new Rectangle( MarginLeft + width / 2, MarginUp + MonthHeader + (d.Day - 16) * BoxHeight, BoxWidth, BoxHeight); outputDayBox(cr, d, DayBox); } cr.Stroke(); surface.Finish(); }
public static void RenderSvg(SimulationCoordinateSpace coordinateSpace, INetworkDatabase ndb, IUnitDatabase units, string filename) { var w = 1000; var h = 600; var scale = 0.04; // meters/pixel var fontSize = 8; var center = new PointD(w / 2, h / 2); var surf = new SvgSurface(filename, w, h); Context cr = new Context(surf); RenderToContext(coordinateSpace, ndb, units, null, cr, center, scale, fontSize); cr.Dispose(); surf.Flush(); surf.Finish(); surf.Dispose(); }
//--------------------------------------------------------------------- private static void Arrow() { Action <Surface> draw = surface => { using (var c = new Context(surface)) { c.Scale(500, 500); // Hat nur für PNG Relevanz: c.Antialias = Antialias.Subpixel; // Linienweite, wegen Maßstab so: double ux = 1, uy = 1; c.InverseTransformDistance(ref ux, ref uy); c.LineWidth = Math.Max(ux, uy); c.Color = new Color(0, 0, 1); c.MoveTo(0.1, 0.10); c.LineTo(0.9, 0.45); c.Stroke(); c.Arrow(0.1, 0.50, 0.9, 0.95, 0.05, 10); c.Stroke(); } }; using (Surface surface = new ImageSurface(Format.Argb32, 500, 500)) { draw(surface); surface.WriteToPng("arrow.png"); } using (Surface surface = new PdfSurface("arrow.pdf", 500, 500)) draw(surface); using (Surface surface = new SvgSurface("arrow.svg", 500, 500)) draw(surface); }
//--------------------------------------------------------------------- private static void Primitives() { Action <Surface> draw = surface => { using (var c = new Context(surface)) { c.Scale(4, 4); // Stroke: c.LineWidth = 0.1; c.Color = new Color(0, 0, 0); c.Rectangle(10, 10, 10, 10); c.Stroke(); c.Save(); { c.Color = new Color(0, 0, 0); c.Translate(20, 5); c.MoveTo(0, 0); c.LineTo(10, 5); c.Stroke(); } c.Restore(); // Fill: c.Color = new Color(0, 0, 0); c.SetSourceRGB(0, 0, 0); c.Rectangle(10, 30, 10, 10); c.Fill(); // Text: c.Color = new Color(0, 0, 0); c.SelectFontFace("Georgia", FontSlant.Normal, FontWeight.Bold); c.SetFontSize(10); TextExtents te = c.TextExtents("a"); c.MoveTo( 0.5 - te.Width / 2 - te.XBearing + 10, 0.5 - te.Height / 2 - te.YBearing + 50); c.ShowText("a"); c.Color = new Color(0, 0, 0); c.SelectFontFace("Arial", FontSlant.Normal, FontWeight.Bold); c.SetFontSize(10); te = c.TextExtents("a"); c.MoveTo( 0.5 - te.Width / 2 - te.XBearing + 10, 0.5 - te.Height / 2 - te.YBearing + 60); c.ShowText("a"); } }; using (Surface surface = new ImageSurface(Format.Argb32, 200, 3200)) { draw(surface); surface.WriteToPng("primitives.png"); } using (Surface surface = new PdfSurface("primitives.pdf", 200, 3200)) draw(surface); using (Surface surface = new SvgSurface("primitives.svg", 200, 3200)) draw(surface); }
//--------------------------------------------------------------------- private static void Hexagon() { Func <double, PointD[]> getHexagonPoints = cellSize => { double ri = cellSize / 2; double r = 2 * ri / Math.Sqrt(3); var p1 = new PointD(0, r); var p2 = new PointD(ri, r / 2); var p3 = new PointD(ri, -r / 2); var p4 = new PointD(0, -r); var p5 = new PointD(-ri, -r / 2); var p6 = new PointD(-ri, r / 2); PointD[] hexagon = { p1, p2, p3, p4, p5, p6 }; return(hexagon); }; Action <Surface> draw = surface => { using (var c = new Context(surface)) { c.Scale(500, 500); // Hat nur für PNG Relevanz: c.Antialias = Antialias.Subpixel; // Linienweite, wegen Maßstab so: double ux = 1, uy = 1; c.InverseTransformDistance(ref ux, ref uy); c.LineWidth = Math.Max(ux, uy); PointD[] hexagon = getHexagonPoints(0.5); c.Save(); { c.Translate(0.5, 0.5); c.MoveTo(hexagon[0]); c.LineTo(hexagon[1]); c.LineTo(hexagon[2]); c.LineTo(hexagon[3]); c.LineTo(hexagon[4]); c.LineTo(hexagon[5]); c.ClosePath(); c.Stroke(); } c.Restore(); c.Color = new Color(0, 0, 1); ux = 0.1; uy = 0.1; c.InverseTransformDistance(ref ux, ref uy); c.LineWidth = Math.Max(ux, uy); c.MoveTo(0.5, 0); c.LineTo(0.5, 1); c.MoveTo(0, 0.5); c.LineTo(1, 0.5); c.Stroke(); } }; using (Surface surface = new ImageSurface(Format.Argb32, 500, 500)) { draw(surface); surface.WriteToPng("hexagon.png"); } using (Surface surface = new PdfSurface("hexagon.pdf", 500, 500)) { draw(surface); surface.WriteToPng("hexagon1.png"); } using (Surface surface = new SvgSurface("hexagon.svg", 500, 500)) { draw(surface); surface.WriteToPng("hexagon2.png"); } }
public static void handleMonth(int year, int month) { var surface = new SvgSurface(month.ToString("00") + ".svg", width, height); Cairo.Context cr = new Context(surface); cr.SetSourceRGBA(0.0, 0.0, 0.0, 1.0); PrintMonthHeader(cr, year, month); var d = new DateTime(year, month, 1); var DayRectArea = new Rectangle( MarginLeft, MarginUp + TotalHeader, ContentWidth, ContentHeight); //cr.Rectangle (DayRectArea); var WeekdayRect = new Rectangle( MarginLeft, MarginUp + MonthHeader, ContentWidth, WeekdayHeader); cr.Rectangle(WeekdayRect); //In worst case we have 6 rows double RowHeight = DayRectArea.Height / 6; //we always have 7 columns double Columnwidth = DayRectArea.Width / 7; int row; int column; //Calculate the Fontsize so that it will fit nicely into the columnwidth cr.SetFontSize(WeekdayHeader); var te = cr.TextExtents("Donnerstag"); cr.SetFontSize(WeekdayHeader * (Columnwidth * 0.9) / te.Width); for (column = 0; column < 7; column++) { cr.IdentityMatrix(); cr.Translate(((column + 0.5) * Columnwidth) + MarginLeft, MarginUp + MonthHeader + TextUpMargin(cr) + (WeekdayHeader - TextHeight(cr)) / 2); te = cr.TextExtents(DayNames[column]); cr.MoveTo(-(te.Width / 2) - te.XBearing, 0); cr.ShowText(DayNames[column]); } cr.SetFontSize(WeekdayHeader); var lastDayOfMonth = DateTime.DaysInMonth(d.Year, d.Month); int currentday = 1; for (row = 0; row < 6; row++) { cr.IdentityMatrix(); var r = new Rectangle(MarginLeft, TotalHeader + MarginUp + row * RowHeight, ContentWidth, RowHeight); cr.Rectangle(r); for (column = 0; column < 7; column++) { int day = (((int)d.DayOfWeek) + 6) % 7; if (!(row == 0 && column < day)) { cr.IdentityMatrix(); cr.Translate((column * Columnwidth) + MarginLeft, (row * RowHeight) + TotalHeader + MarginUp); r = new Rectangle(0, 0, Columnwidth, RowHeight); cr.Rectangle(r); cr.SetFontSize(WeekdayHeader * 0.8); string daynumber = string.Format("{0}", currentday); te = cr.TextExtents(daynumber); cr.MoveTo(milimeter - te.XBearing, milimeter + TextUpMargin(cr)); cr.ShowText(daynumber); cr.SetFontSize(WeekdayHeader * 0.8 / 2); string datestring = d.Year.ToString("0000") + d.Month.ToString("00") + currentday.ToString("00"); if (ical.holidays.ContainsKey(datestring)) { cr.IdentityMatrix(); cr.Translate((column * Columnwidth) + MarginLeft, ((row + 1) * RowHeight) + TotalHeader + MarginUp); var ww = new WordWrap(cr, Columnwidth - 2 * milimeter); foreach (var s in ical.holidays[datestring]) { var x = s.Split(' '); foreach (string w in x) { ww.AddWord(w); } ww.FinishLine(); } int i; for (i = 0; i < ww.result.Count; i++) { var s = ww.result[i]; te = cr.TextExtents(s); cr.MoveTo(milimeter - te.XBearing, (-i * (TextHeight(cr) + milimeter)) - TextDownMargin(cr) - milimeter); cr.ShowText(s); } } currentday++; } if (currentday > lastDayOfMonth) { break; } } if (currentday > lastDayOfMonth) { break; } } cr.Stroke(); surface.Finish(); }
//--------------------------------------------------------------------- private static void PaintAfter() { Pattern pattern = null; using (Surface surface = new SvgSurface("test.svg", 300, 300)) using (var c = new Context(surface)) { // Draw to group c.PushGroup(); { var hex = new Hexagon(150); hex.Fill(c, 150, 150, new Color(0.8, 0.8, 0.8)); } // Get group pattern = c.PopGroup(); c.Source = pattern; // Draw (without mask, hence everything that's in the source) c.Paint(); } using (Surface pdfSurface = new PdfSurface("test1.pdf", 300, 300)) using (Surface svgSurface = new SvgSurface("test1.svg", 300, 300)) using (var c = new Context(svgSurface)) { c.PushGroup(); { c.Source = pattern; c.Paint(); //pattern.Destroy(); c.Color = new Color(0, 0, 1); c.LineWidth = 0.1; c.MoveTo(0, 150); c.LineTo(300, 150); c.MoveTo(150, 0); c.LineTo(150, 300); c.Stroke(); c.Color = new Color(0, 0, 0); //c.SelectFontFace("Georgia", FontSlant.Normal, FontWeight.Bold); //c.SelectFontFace("Rockwell", FontSlant.Normal, FontWeight.Normal); c.SelectFontFace("Arial", FontSlant.Normal, FontWeight.Normal); //c.SelectFontFace("Times New Roman", FontSlant.Normal, FontWeight.Normal); c.SetFontSize(16); string text = "Hexagon with coordinate-axis"; //string text = "III"; // Determine the width of the text double textWidth = c.GetTextWidth(text); c.Save(); c.Translate((300 - textWidth) / 2, 16); { c.ShowText(text); } c.Restore(); c.MoveTo(0, 0); c.LineTo(300, 300); c.MoveTo(0, 300); c.LineTo(300, 0); c.Stroke(); c.Save(); c.Translate(16, 300); c.Rotate(-Math.PI / 2); c.Translate((300 - textWidth) / 2, 0); { c.ShowText(text); } c.Restore(); // Draw scala c.Save(); c.Translate(270, 20); { // Farbverlauf: Gradient linpat = new LinearGradient(0, 0, 0, 260); linpat.AddColorStop(0, new Color(0, 1, 0)); linpat.AddColorStop(0.5, new Color(0, 0, 1)); linpat.AddColorStop(1, new Color(1, 0, 0)); c.Rectangle(0, 0, 20, 260); c.Source = linpat; //c.Fill(); c.FillPreserve(); c.Color = new Color(0, 0, 0); c.LineWidth = 1; //c.Rectangle(0, 0, 20, 260); c.Stroke(); } c.Restore(); } pattern = c.PopGroup(); c.Source = pattern; c.Paint(); using (var c1 = new Context(pdfSurface)) { c1.Color = new Color(1, 1, 1, 0); c1.Rectangle(0, 0, 300, 300); c1.Fill(); c1.Source = pattern; c1.Paint(); pdfSurface.WriteToPng("test1.png"); } } }