Created
July 2, 2021 17:22
-
-
Save okay-type/6961822aec34e3e99fad2b7478494b6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mojo.subscriber import Subscriber, registerGlyphEditorSubscriber | |
from vanilla import Button | |
import merz | |
from merz.tools.drawingTools import NSImageDrawingTools | |
''' | |
[ Print Glyph ] | |
dumps code to draw the current glyph as a symbol | |
copy/paste it into the symbol factory at the bottom | |
handles cubic curves now | |
but doesn't do counterforms (like the inside of a zero) | |
''' | |
size = 100 | |
symbolName = 'com.okay.testSymbol' | |
class symbolTest(Subscriber): | |
debug = True | |
def build(self): | |
self.merzView = merz.MerzView( | |
(0, 0, 300, 100), | |
backgroundColor=(0, 1, 1, .05), | |
delegate=self, | |
) | |
self.merzView.destroy = Button((-100, -26, -3, 22), 'Destroy', callback=self.destroyThis) | |
self.merzView.print = Button((-100, -49, -3, 22), 'Print Glyph', callback=self.print) | |
self.merzContainer = self.merzView.getMerzContainer() | |
self.getGlyphEditor().addGlyphEditorSubview(self.merzView) | |
# register symbol | |
merz.SymbolImageVendor.registerImageFactory(symbolName, mySymbolImageFactory) | |
def destroy(self): | |
self.merzContainer.clearSublayers() | |
self.getGlyphEditor().removeGlyphEditorSubview(self.merzView) | |
# unregister symbol - this seems cumbersome? | |
deleteImage = None | |
for image in merz.SymbolImageVendor.images: | |
if symbolName in image: | |
deleteImage = image | |
if deleteImage != None: | |
merz.SymbolImageVendor.images.pop(image, None) | |
def acceptsMouseMoved(self): | |
return True | |
def mouseEntered(self, sender, event): | |
self.report('mouseEntered', sender, event) | |
def mouseExited(self, sender, event): | |
self.report('mouseExited', sender, event) | |
def mouseDown(self, sender, event): | |
self.report('mouseDown', sender, event) | |
def report(self, name, sender=None, event=None): | |
print(name) | |
print('sender:', sender) | |
print('event:', event) | |
print('') | |
def destroyThis(self, sender): | |
self.destroy() | |
def print(self, sender): | |
g = CurrentGlyph() | |
bot = ['\n'] | |
for contour in g: | |
bot.append('bot.newPath()') | |
bot.append('bot.moveTo((' + str(int(contour.bPoints[0].anchor[0])) + '*s' + ', ' + str(int(contour.bPoints[0].anchor[1])) + '*s' + '))') | |
for segment in contour: | |
if len(segment.points) == 1: | |
x, y = segment.points[-1].x, segment.points[-1].y | |
bot.append('bot.lineTo((' + str(x) + '*s' + ', ' + str(y) + '*s' + '))') | |
elif len(segment.points) == 3: | |
# dont know or care how to make this work with non-ps curves | |
x, y = segment.points[-1].x, segment.points[-1].y | |
OFFAx, OFFAy = segment.points[0].x, segment.points[0].y | |
OFFBx, OFFBy = segment.points[1].x, segment.points[1].y | |
bot.append('bot.curveTo(' + | |
'(' + str(OFFAx) + '*s' + ', ' + str(OFFAy) + '*s' + '), ' + | |
'(' + str(OFFBx) + '*s' + ', ' + str(OFFBy) + '*s' + '), ' + | |
'(' + str(x) + '*s' + ', ' + str(y) + '*s' + ')' + | |
')') | |
else: | |
bot.append('bot.lineTo((' + str(int(aPt.anchor[0])) + '*s' + ', ' + str(int(aPt.anchor[1])) + '*s' + '))') | |
bot.append('bot.closePath()') | |
bot.append('bot.drawPath()') | |
bot = '\n\t'.join(bot) | |
print(bot) | |
print() | |
def glyphEditorDidSetGlyph(self, info): | |
glyph = info['glyph'] | |
self.drawSymbol(glyph) | |
def drawSymbol(self, glyph): | |
if glyph == None: | |
return | |
color = glyph.markColor | |
if color != None: | |
color = (color[0], color[1], color[2], color[3] * .8) | |
if color == None: | |
color = (0, 0, 0, .05) | |
self.merzContainer.clearSublayers() | |
self.merzContainer.appendSymbolSublayer( | |
position=(size/2, size/2), | |
imageSettings=dict( | |
name=symbolName, | |
size=(size, size), | |
# strokeColor=(0, 0, 1, 1), | |
# fillColor=color, | |
# strokeWidth=1, | |
) | |
) | |
def mySymbolImageFactory( | |
size, | |
strokeColor=(0, 1, 0, 1), | |
strokeWidth=1, | |
fillColor=(1, 1, 0, 1), | |
**kwargs | |
): | |
width, height = size | |
s = height/700 | |
bot = NSImageDrawingTools(size) | |
bot.fill(*fillColor) | |
bot.stroke(*strokeColor) | |
bot.strokeWidth(strokeWidth) | |
bot.newPath() | |
bot.moveTo((79*s, 0*s)) | |
bot.lineTo((482*s, 0*s)) | |
bot.lineTo((482*s, 90*s)) | |
bot.lineTo((435*s, 86*s)) | |
bot.lineTo((178*s, 83*s)) | |
bot.lineTo((178*s, 86*s)) | |
bot.lineTo((269*s, 174*s)) | |
bot.curveTo((420*s, 318*s), (481*s, 396*s), (481*s, 514*s)) | |
bot.curveTo((481*s, 641*s), (398*s, 706*s), (265*s, 706*s)) | |
bot.curveTo((193*s, 706*s), (117*s, 687*s), (62*s, 665*s)) | |
bot.lineTo((62*s, 581*s)) | |
bot.curveTo((111*s, 601*s), (185*s, 619*s), (231*s, 619*s)) | |
bot.curveTo((327*s, 619*s), (379*s, 574*s), (379*s, 490*s)) | |
bot.curveTo((379*s, 421*s), (341*s, 346*s), (221*s, 226*s)) | |
bot.lineTo((79*s, 81*s)) | |
bot.lineTo((79*s, 0*s)) | |
bot.closePath() | |
bot.drawPath() | |
image = bot.getImage() | |
return image | |
registerGlyphEditorSubscriber(symbolTest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment