Skip to content

Instantly share code, notes, and snippets.

@arialcrime
Last active October 25, 2021 07:13
Show Gist options
  • Select an option

  • Save arialcrime/67e09fd5b392520fc6a0050fd73cf6cd to your computer and use it in GitHub Desktop.

Select an option

Save arialcrime/67e09fd5b392520fc6a0050fd73cf6cd to your computer and use it in GitHub Desktop.
Paints the background of the glyph window when you are not in the foreground
from mojo.subscriber import Subscriber, registerGlyphEditorSubscriber, unregisterGlyphEditorSubscriber
class ShowLayerColorInBackground(Subscriber):
debug = True
def build(self):
glyphEditor = self.getGlyphEditor()
container = glyphEditor.extensionContainer(
identifier="com.arialcrime.ShowLayerColorInBackground",
location="background",
clear=True
)
self.backgroundLayer = container.appendRectangleSublayer(
opacity=0.55,
visible=False
)
def roboFontDidSwitchCurrentGlyph(self, info):
g = info["glyph"]
if g is not None:
font = g.font
layer = g.layer
with self.backgroundLayer.propertyGroup():
self.backgroundLayer.addSkewTransformation(-(font.info.italicAngle or 0))
self.backgroundLayer.setPosition((font.lib.get("com.typemytype.robofont.italicSlantOffset", 0), font.info.descender))
self.backgroundLayer.setSize((g.width, font.info.unitsPerEm))
self.backgroundLayer.setFillColor(g.layer.color)
self.backgroundLayer.setVisible(g.font.defaultLayer != g.layer)
def glyphEditorLayerDidChangeColor(self, info):
layer = info["layer"]
self.backgroundLayer.setBackgroundColor(layer.color)
def glyphEditorGlyphDidChangeMetrics(self, info):
g = info["glyph"]
font = g.font
self.backgroundLayer.setSize((g.width, font.info.unitsPerEm))
registerGlyphEditorSubscriber(ShowLayerColorInBackground)
@typemytype

Copy link
Copy Markdown

dont check default layer by its order in the glyph.layers

g = CurrentGlyph()

print(g.font.defaultLayer, g.layer)
print(g.font.defaultLayer == g.layer)

some adjustments, mainly to make a lot easier :)

from mojo.subscriber import Subscriber, registerGlyphEditorSubscriber, unregisterGlyphEditorSubscriber


class ShowLayerColorInBackground(Subscriber):
    
    debug = True
        
    def build(self):
        glyphEditor = self.getGlyphEditor()
        
        container = glyphEditor.extensionContainer(
            identifier="com.arialcrime.ShowLayerColorInBackground",
            location="background",            
            clear=True
        )
        self.backgroundLayer = container.appendRectangleSublayer(
            opacity=0.55,   
            visible=False
        )
    
    def roboFontDidSwitchCurrentGlyph(self, info):
        g = info["glyph"]    
        font = g.font
        layer = g.layer
        with self.backgroundLayer.propertyGroup():
            self.backgroundLayer.addSkewTransformation(-font.info.italicAngle)
            self.backgroundLayer.setPosition((0, font.info.descender))  
            self.backgroundLayer.setSize((g.width, font.info.unitsPerEm))    
            self.backgroundLayer.setFillColor(g.layer.color)
            self.backgroundLayer.setVisible(g.font.defaultLayer != g.layer)
        
    def glyphEditorLayerDidChangeColor(self, info):
        layer = info["layer"]
        self.backgroundLayer.setBackgroundColor(layer.color)
    
    def glyphEditorGlyphDidChangeMetrics(self, info):
        g = info["glyph"]    
        font = g.font
        self.backgroundLayer.setSize((g.width, font.info.unitsPerEm))
        

registerGlyphEditorSubscriber(ShowLayerColorInBackground)

@arialcrime

Copy link
Copy Markdown
Author

Oh wow, thanks for the edits. That is indeed much cleaner. ๐Ÿ˜…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment