New API: Draw in Font View Glyph Cells

As of Glyphs 3.3 (3327), there is now an API for any plugin to draw in the foreground and background of glyph cells in Font View. This allows you to draw additional icons, background colors, or other decoration to indicate various aspects of the glyph or layer. For example, you can change the background color when the glyph has a certain tag. There is currently no Python wrapper, but you can use the Objective-C API from Python.

A plugin registers an object (like self) to draw in the foreground and/or the background of glyph cells like so:

GSCallbackHandler.addCallback_forOperation_(self, "DrawFontView")

Plugins written in Objective-C or Swift can use the GSDrawFontViewCallbackName constant instead of the "DrawFontView" string.

The object needs to have at least one of the following two methods:

@objc.signature(b'v@:@{CGRect={CGPoint=dd}{CGSize=dd}}')
def drawFontViewBackgroundForLayer_inFrame_(self, layer, frame):
    # code drawing for layer in `frame` behind glyph outline

@objc.signature(b'v@:@{CGRect={CGPoint=dd}{CGSize=dd}}')
def drawFontViewForegroundForLayer_inFrame_(self, layer, frame):
    # code drawing for layer in `frame` in front of glyph outline

A basic implementation that just draws a yellow background would be:

@objc.signature(b'v@:@{CGRect={CGPoint=dd}{CGSize=dd}}')
def drawFontViewBackgroundForLayer_inFrame_(self, layer, frame):
    NSColor.yellowColor().setFill()
    NSRectFill(frame)

You can find the example code here:


Reporter plugins can do the same, but they don’t need to register themselves using GSCallbackHandler. Instead, adding the methods suffices for a reporter to draw in Font View.

3 Likes

Thanks Florian!
As you implemented the git symbols in Light Table (little icons in the top left corner of each cell) I wonder: are these Icons arranged in a stack view? What if I want to add my own symbols? I don’t want to overlap them, also not the Glyphs-built-in ones (:warning:, :information_source:, :no_entry_sign:, …) but rather have them automatically arranged in a Stack-View-manner. Is there an API for that already?

There is no stack view, I am afraid. That would be my preferred API for adding badges, too, but that is not what this glyph cell drawing API was designed to do. Instead, it’s more about drawing custom backgrounds or metric lines or other decorations.

The badges in Light Table are more an abuse of this API. The API already existed, so I just polished some parts and documented it in this topic.

If you want to add badges as well, could you share some more details? (Also in private, if you prefer.) That would be helpful for us to come up with a more general API design.

Thanks! No particular use case yet, I have a certain custom plugin that will benefit from the general drawing API, and it might also want to add some badges in the near future. Once I work on it again, I am happy to let you know. Thanks for explaining.

A stack view would simplify things. But also may slow down the font view. I have to try this.

No pressure. I just stumbled upon this post and thought Abitur the Badges