How to use a objectsGS.RGlyph for drawBackgroundForLayer_ in a glyphsReporter

I am using @loicsander’s RoboFab pens, particularly the broad nibber as it produces better results at the moment.

I want to create a plugin that takes an open outline from the front window and draws it’s Broad Nib counterpart in the background.

I can get an objectsGS.RGlyph but I don’t know how to use this for the drawBackgroundForLayer_ in a .glyphsReporter? Kind of like show Offset preview but for Broad nib

you need to find a pen that produces a NSBezierPath. fonttools has a CocoaPen that does this.

It looks like layer.paths[0].bezierPath creates a NSBezierPath.

Is it possible to get a GSPath from an NSBezierPath (so to say, the other way around)? I want to add an NSBezierPath to a layer.

That is possibel but not implemented. You could implement a path.draw(pen) methods that traverses the bezierpath and draws itself in the pen. Have a look at the NSBezierPath documentation from Apple.

Where did you get the bezierpath from?

I create it rudimetally with the following bit:

path = NSBezierPath.bezierPath()
sysFont = NSFont.systemFontOfSize_(12)
glyph = sysFont.glyphWithName_("a")
path.moveToPoint_((0, 0))
path.appendBezierPathWithGlyph_inFont_(glyph, thisFont)

Now path is the NSBezierPath object, which appears to be the same as a GSPath.bezierPath.

So you mean, next step would be to use the GSPen? If so, how to traverse trough the NSBezierPath? Cannot find this in the Apple docu.

I do not think it is possible easily, you would have to get your hands dirty and wade through the path elements. You can step across its path elements with the NSBezierPath.elementCount() and NSBezierPath.elementAtIndex_() attributes. The latter will yield a NSBezierPathElement object, which you could query for its attributes and points, and step by step, reconstruct a GSPath with it.

That’s what I assumed. NSBezierPath.elementCount() and NSBezierPath.elementAtIndex_()where the missing pieces. Good to know. Will postpone to write a Pen for that. Thank you so much guys.

Hey @Mark, did you make something to turn a Robofab produced shape into a NSBezierPath? Like for example

from robofab.world import CurrentGlyph

g = CurrentGlyph()
g.clear()

pen = g.getPen()

pen.moveTo((10, 0))
pen.lineTo((540, 0))
pen.lineTo((540, 432))
pen.lineTo((10, 432))
pen.closePath()

I’m trying to make a plugin that will draw something in the background, but using this getPen() method?

How about this? No RoboFab needed:

thisLayer = Glyphs.font.selectedLayers[0]
thisLayer.paths = []
newPath = GSPath()
newPath.addNode_(GSNode((10, 0)))
newPath.addNode_(GSNode((540, 0)))
newPath.addNode_(GSNode((540, 432)))
newPath.addNode_(GSNode((10, 432)))
newPath.setClosed_(True)
thisLayer.addPath_(newPath)

(it is just a shortcut, you could also initialize each node, set x, y, type and connection before passing it in the addNode_() method

and then just draw to the background layer instead of the layer. Otherwise, why using the NSBezierPath?

OR: do you mean, you want to draw a shape/Path to the background of the view? Like the reporters are capable of doing? In that case, why bother with the getPen? This is for drawing paths to the glyph/layer. Something kind of different.

thanks Mark! I just couldn’t find the glyphs app way of doing it!
I also wanted to use Loic Sander’s broadnibbing pen for something else https://github.com/loicsander/RobofabPens

I see. Then you are perhaps right to use the RoboFab pen. Unfortunately I’ll have to pass on the question and come over to the side of the curious ones. This is beyond my current knowledge.

1 Like

You can use a NSBezierPath as a pen directly. So everywhere you can put in a pen, you can put in a NSBezierPath.

@oneweioranother did you get Loic Sander’s RoboFabPens to work? Trying the same …
Especially interested in something like the JitterPen or SpikePen.

The latest cutting edge version has some improvements.