Sign up

Cairo setDash() Args

Hi Mike,

I'm trying to understand the arguments passed into cairo.setDash(), but the only docs are for the C version and it's vastly different from the D implementation.

Can you explain them? All I've managed to work out so far is that the first argument determines the length of the first dash. And (maybe) the second designates the position along the line where the pattern starts to repeat.

Re: Cairo setDash() Args

On 10-06-2019 16:49, Ron Tarrant wrote:

Hi Mike,

I'm trying to understand the arguments passed into cairo.setDash(), but the only docs are for the C version and it's vastly different from the D implementation.

Can you explain them? All I've managed to work out so far is that the first argument determines the length of the first dash. And (maybe) the second designates the position along the line where the pattern starts to repeat.

The values of the dash array alternately represent the length of the
dash and the skipped part.
offset shifts the pattern if you don't want it to start at the start of
the line.

This example: https://www.cairographics.org/samples/dash/ in D would be:

double[] dashes = [50.0,  /* ink */
                    10.0,  /* skip */
                    10.0,  /* ink */
                    10.0   /* skip*/
                   ];
double offset = -50.0;

cr.setDash (dashes, offset);
cr.setLineWidth (10.0);

cr.moveTo (128.0, 25.6);
cr.lineTo (230.4, 230.4);
cr.relLineTo (-102.4, 0.0);
cr.curveTo (51.2, 230.4, 51.2, 128.0, 128.0, 128.0);

cr.stroke ();

Re: Cairo setDash() Args

On Mon, 10 Jun 2019 19:52:46 +0200, Mike Wey wrote:

The values of the dash array alternately represent the length of the
dash and the skipped part.

Well, now I'm really confused. I tried your code and got the expected results, but I don't understand why the following code results in a dashed line that looks like the dashPattern is [5, 5, 10, 10] instead of [5, 10, 15, 20]. Any ideas? Is there something else in here that's throwing things off, something I've missed?

import std.stdio;
import std.conv;

import gtk.MainWindow;
import gtk.Main;
import gtk.Box;
import gtk.Widget;
import cairo.Context;
import gtk.DrawingArea;

void main(string[] args)
{
	Main.init(args);

	TestRigWindow myTestRig = new TestRigWindow("Test Rig");
	
	Main.run();
	
} // main()


class TestRigWindow : MainWindow
{
	AppBox appBox;
	
	this(string title)
	{
		super(title);
		setSizeRequest(640, 360);
		
		addOnDestroy(&quitApp);
		
		appBox = new AppBox();
		add(appBox);
		
		showAll();

	} // this() CONSTRUCTOR
	
		
	void quitApp(Widget widget)
	{
		writeln("Bye.");
		Main.quit();
		
	} // quitApp()

} // class myAppWindow


class AppBox : Box
{
	MyDrawingArea myDrawingArea;
	
	this()
	{
		super(Orientation.VERTICAL, 10);
		
		myDrawingArea = new MyDrawingArea();
		
		packStart(myDrawingArea, true, true, 0); // LEFT justify
		
	} // this()

} // class AppBox


class MyDrawingArea : DrawingArea
{
	this()
	{
		addOnDraw(&onDraw);
		
	} // this()
	
	bool onDraw(Scoped!Context context, Widget w)
	{
		double[] dashPattern = [5.0, 10.0, 15.0, 20.0];
		// set up and draw a dashed-line rectangle
		context.setLineWidth(3);
		context.setLineCap(CairoLineCap.SQUARE);
		context.setSourceRgba(0.1, 0.2, 0.3, 0.8); // pen color with alpha
		context.rectangle(150, 100, 340, 170); // rectangle upper-left/width/height
		context.setDash(dashPattern, 0.0);
		context.stroke();
		
		return(true);
		
	} // onDraw()
	
} // class MyDrawingArea

Re: Cairo setDash() Args

On Mon, 10 Jun 2019 21:02:28 GMT, Ron Tarrant wrote:

On Mon, 10 Jun 2019 19:52:46 +0200, Mike Wey wrote:
Is there something else in here that's throwing things off, something I've missed?

double[] dashPattern = [5.0, 10.0, 15.0, 20.0];
// set up and draw a dashed-line rectangle
context.setLineWidth(3);
context.setLineCap(CairoLineCap.SQUARE);
context.setSourceRgba(0.1, 0.2, 0.3, 0.8); // pen color with alpha
context.rectangle(150, 100, 340, 170); // rectangle upper-left/width/height
context.setDash(dashPattern, 0.0);
context.stroke();

Never mind, Mike. I figured it out. Setting the line caps throws things off visually.