def __init__(self, parentWindow): data = getExtensionDefault(self.identifier, dict()) self.w = Sheet((470, 580), parentWindow=parentWindow) self.w.tabs = Tabs((10, 10, -10, -40), ["TTF AutoHint", "HTML Preview"]) self.w.tabs[0].settings = self.settings = TTFAutoHintGroup( (0, 0, -0, -0)) self.settings.set(data) y = 10 self.w.tabs[1].htmlText = TextBox((10, y, 100, 22), "HTML preview:") y += 30 self.w.tabs[1].html = self.html = CodeEditor( (10, y, -10, 250), getExtensionDefault("%s.htmlPreview" % settingsIdentifier, htmlPreviewDefault), lexer="html") self.html.showLineNumbers(False) y += 260 self.w.tabs[1].globalCssText = TextBox((10, y, 100, 22), "CSS Style:") y += 30 self.w.tabs[1].globalCss = self.globalCss = CodeEditor( (10, y, -10, -10), getExtensionDefault("%s.globalCSSPreview" % settingsIdentifier, ""), lexer="css") self.globalCss.showLineNumbers(False) self.w.saveButton = Button((-100, -30, -10, 20), "Save settings", callback=self.saveCallback, sizeStyle="small") self.w.setDefaultButton(self.w.saveButton) self.w.closeButton = Button((-190, -30, -110, 20), "Cancel", callback=self.closeCallback, sizeStyle="small") self.w.closeButton.bind(".", ["command"]) self.w.closeButton.bind(chr(27), []) self.w.resetButton = Button((-280, -30, -200, 20), "Reset", callback=self.resetCallback, sizeStyle="small") self.w.open()
def _open_sheet(self, sender): self.s = vanilla.Sheet((self._width - 50, 220), self.w, (self._width - 50, 220)) #self.s.contents = vanilla.EditText((10, 10, -10, -40), continuous=False, sizeStyle="small") self.s.contents = CodeEditor((10, 10, -10, -40), "", lexer="xml") self.s.ok_button = vanilla.Button( (-80, -30, -10, 20), "OK", callback=self._close_sheet, sizeStyle="small", ) _key = sender.get()[sender.getSelection()[0]] #["Key"] self.s.contents.set(self._key_contents[_key["Key"]]) self._sheet = True self.s.open()
def __init__(self): self.currentGlyph = None self.w = vanilla.Window((500, 500), "Plist Viewer", minSize=(100, 100)) self.w.xml = CodeEditor((0, 0, -0, -30), "", lexer="xml") self.w.applyButton = vanilla.Button((-70, -25, -20, 22), "Apply", callback=self.applyCallback, sizeStyle="small") addObserver(self, "currentGlyphChanged", "currentGlyphChanged") self.setUpBaseWindowBehavior() self.currentGlyphChanged({}) self.w.open()
def __init__(self, font): self.font = None self._glyphs = [] statusBarHeight = 20 self.w = Window((900, 700), "Glyph Builder", minSize=(400, 400)) self.w.getNSWindow().setCollectionBehavior_( 128) # NSWindowCollectionBehaviorFullScreenPrimary toolbarItems = [ dict( itemIdentifier="save", label="Save", imageNamed="toolbarScriptSave", callback=self.saveFile, ), dict( itemIdentifier="open", label="Open", imageNamed="toolbarScriptOpen", callback=self.openFile, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict( itemIdentifier="reload", label="Update", imageNamed="toolbarScriptReload", callback=self.reload, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict( itemIdentifier="analyse", label="Analyse", imageNamed="prefToolbarSort", callback=self.analyse, ), dict(itemIdentifier=NSToolbarFlexibleSpaceItemIdentifier), dict(itemIdentifier="buildGlyphs", label="Build Glyphs", imageNamed="toolbarRun", callback=self.generateGlyphs), ] self.w.addToolbar(toolbarIdentifier="GlyphBuilderControllerToolbar", toolbarItems=toolbarItems, addStandardItems=False) self.constructions = CodeEditor((0, 0, -0, -0), constructions, lexer=GlyphConstructionLexer()) # self.constructions.wrapWord(False) # in only availbel in the RoboFont 1.7 beta self.constructions.getNSScrollView().setBorderType_(NSNoBorder) self.preview = MultiLineView( (0, 0, -0, -0), pointSize=50, lineHeight=500, applyKerning=False, displayOptions={ "Beam": False, "displayMode": "Multi Line" }, selectionCallback=self.previewSelectionCallback) self.analyser = AnalyserTextEditor((0, 0, -0, -0), readOnly=True) self.analyserPreview = Group((0, 0, -0, -0)) constructionColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( 0, 0, 0, .6) self.analyserPreview.construction = GlyphPreview( (0, 0, -0, -0), contourColor=constructionColor, componentColor=constructionColor) self.analyserPreview.construction.getNSView()._buffer = 100 originColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( 1, 0, 0, .6) self.analyserPreview.origin = GlyphPreview((0, 0, -0, -0), contourColor=originColor, componentColor=originColor) self.analyserPreview.origin.getNSView()._buffer = 100 self.analyserPreview.build = Button((10, -30, -10, 20), "Build", sizeStyle="small", callback=self.buildSingleGlyph) self.analyserPreview.build.enable(False) paneDescriptions = [ dict(view=self.analyser, identifier="analyserText", canCollapse=False, minSize=100), dict(view=self.analyserPreview, identifier="analyserPreview", canCollapse=False, minSize=100), ] self.analyserSplit = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, isVertical=False, dividerThickness=1) paneDescriptions = [ dict(view=self.constructions, identifier="constructions", canCollapse=False, minSize=200, maxSize=600, liveResizeable=False), dict(view=self.preview, identifier="preview", canCollapse=False, minSize=300, liveResizeable=True), dict(view=self.analyserSplit, identifier="analyser", canCollapse=True, minSize=100, maxSize=300, liveResizeable=False) ] self.w.split = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, dividerThickness=1) # self.w.split.showPane("analyser", True) self.w.statusBar = StatusBar( (0, -statusBarHeight, -0, statusBarHeight)) self.w.statusBar.hiddenReload = Button((0, 0, -0, -0), "Reload", self.reload) button = self.w.statusBar.hiddenReload.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenReload.bind("\r", ["command"]) self.w.statusBar.hiddenSave = Button((0, 0, -0, -0), "Reload", self.saveFile) button = self.w.statusBar.hiddenSave.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenSave.bind("s", ["command"]) self.subscribeFont(font) self.setUpBaseWindowBehavior() addObserver(self, "fontBecameCurrent", "fontBecameCurrent") addObserver(self, "fontResignCurrent", "fontResignCurrent") self.w.open()
class GlyphBuilderController(BaseWindowController): fileNameKey = "%s.lastSavedFileName" % defaultKey def __init__(self, font): self.font = None self._glyphs = [] statusBarHeight = 20 self.w = Window((900, 700), "Glyph Builder", minSize=(400, 400)) self.w.getNSWindow().setCollectionBehavior_( 128) # NSWindowCollectionBehaviorFullScreenPrimary toolbarItems = [ dict( itemIdentifier="save", label="Save", imageNamed="toolbarScriptSave", callback=self.saveFile, ), dict( itemIdentifier="open", label="Open", imageNamed="toolbarScriptOpen", callback=self.openFile, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict( itemIdentifier="reload", label="Update", imageNamed="toolbarScriptReload", callback=self.reload, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict( itemIdentifier="analyse", label="Analyse", imageNamed="prefToolbarSort", callback=self.analyse, ), dict(itemIdentifier=NSToolbarFlexibleSpaceItemIdentifier), dict(itemIdentifier="buildGlyphs", label="Build Glyphs", imageNamed="toolbarRun", callback=self.generateGlyphs), ] self.w.addToolbar(toolbarIdentifier="GlyphBuilderControllerToolbar", toolbarItems=toolbarItems, addStandardItems=False) self.constructions = CodeEditor((0, 0, -0, -0), constructions, lexer=GlyphConstructionLexer()) # self.constructions.wrapWord(False) # in only availbel in the RoboFont 1.7 beta self.constructions.getNSScrollView().setBorderType_(NSNoBorder) self.preview = MultiLineView( (0, 0, -0, -0), pointSize=50, lineHeight=500, applyKerning=False, displayOptions={ "Beam": False, "displayMode": "Multi Line" }, selectionCallback=self.previewSelectionCallback) self.analyser = AnalyserTextEditor((0, 0, -0, -0), readOnly=True) self.analyserPreview = Group((0, 0, -0, -0)) constructionColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( 0, 0, 0, .6) self.analyserPreview.construction = GlyphPreview( (0, 0, -0, -0), contourColor=constructionColor, componentColor=constructionColor) self.analyserPreview.construction.getNSView()._buffer = 100 originColor = NSColor.colorWithCalibratedRed_green_blue_alpha_( 1, 0, 0, .6) self.analyserPreview.origin = GlyphPreview((0, 0, -0, -0), contourColor=originColor, componentColor=originColor) self.analyserPreview.origin.getNSView()._buffer = 100 self.analyserPreview.build = Button((10, -30, -10, 20), "Build", sizeStyle="small", callback=self.buildSingleGlyph) self.analyserPreview.build.enable(False) paneDescriptions = [ dict(view=self.analyser, identifier="analyserText", canCollapse=False, minSize=100), dict(view=self.analyserPreview, identifier="analyserPreview", canCollapse=False, minSize=100), ] self.analyserSplit = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, isVertical=False, dividerThickness=1) paneDescriptions = [ dict(view=self.constructions, identifier="constructions", canCollapse=False, minSize=200, maxSize=600, liveResizeable=False), dict(view=self.preview, identifier="preview", canCollapse=False, minSize=300, liveResizeable=True), dict(view=self.analyserSplit, identifier="analyser", canCollapse=True, minSize=100, maxSize=300, liveResizeable=False) ] self.w.split = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, dividerThickness=1) # self.w.split.showPane("analyser", True) self.w.statusBar = StatusBar( (0, -statusBarHeight, -0, statusBarHeight)) self.w.statusBar.hiddenReload = Button((0, 0, -0, -0), "Reload", self.reload) button = self.w.statusBar.hiddenReload.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenReload.bind("\r", ["command"]) self.w.statusBar.hiddenSave = Button((0, 0, -0, -0), "Reload", self.saveFile) button = self.w.statusBar.hiddenSave.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenSave.bind("s", ["command"]) self.subscribeFont(font) self.setUpBaseWindowBehavior() addObserver(self, "fontBecameCurrent", "fontBecameCurrent") addObserver(self, "fontResignCurrent", "fontResignCurrent") self.w.open() def subscribeFont(self, font): self.unsubscribeFont() self.font = font if font is not None: self.preview.setFont(font) self.font.naked().addObserver(self, "fontChanged", "Font.Changed") self.constructionsCallback(self.constructions) def unsubscribeFont(self): if self.font is not None: self.preview.setFont(None) self.preview.set([]) self.font.removeObserver(self, notification="Font.Changed") self.font = None def constructionsCallback(self, sender, update=True): if self.font is None: return font = self.font.naked() self.glyphConstructorFont = GlyphConstructorFont(font) self._glyphs = [] errors = [] try: constructions = ParseGlyphConstructionListFromString( sender.get(), font) except GlyphBuilderError as err: constructions = [] errors.append(str(err)) for construction in constructions: if not construction: glyph = self.preview.createNewLineGlyph() elif construction in self.glyphConstructorFont.glyphsDone: glyph = self.glyphConstructorFont.glyphsDone[construction] else: try: constructionGlyph = GlyphConstructionBuilder( construction, self.glyphConstructorFont, characterMap=None) except GlyphBuilderError as err: errors.append(str(err)) continue if constructionGlyph.name is None: errors.append(construction) continue if RoboFontVersion < "2.0": glyph = font._instantiateGlyphObject() else: glyph = font.layers.defaultLayer.instantiateGlyphObject() glyph.name = constructionGlyph.name glyph.unicode = constructionGlyph.unicode glyph.note = constructionGlyph.note glyph.markColor = constructionGlyph.markColor if RoboFontVersion < "2.0": glyph.setParent(self.glyphConstructorFont) glyph.dispatcher = font.dispatcher else: glyph._font = weakref.ref(self.glyphConstructorFont) # glyph._dispatcher = font._dispatcher glyph.width = constructionGlyph.width constructionGlyph.draw(glyph.getPen()) self.glyphConstructorFont.glyphsDone[glyph.name] = glyph self._glyphs.append(glyph) if errors: print("Errors:") print("\n".join(errors)) if update: self.preview.set(self._glyphs) self.analyser.set(analyseConstructions(font, self._glyphs)) # preview def previewSelectionCallback(self, sender): def _niceNumber(value): i = int(value) if i == value: return "%i" % value else: return "%.2f" % value glyph = sender.getSelectedGlyph() if glyph is not None and glyph.name is None: glyph = None status = [] if glyph is not None: width = _niceNumber(glyph.width) leftMargin = _niceNumber(glyph.leftMargin) rightMargin = _niceNumber(glyph.rightMargin) status = [ glyph.name, "width: %s left: %s right: %s" % (width, leftMargin, rightMargin), "components: %s" % (", ".join( [component.baseGlyph for component in glyph.components])) ] if glyph.unicode: status.append("unicode: %04X" % glyph.unicode) if glyph.note: status.append("note: %s" % (glyph.note[:30] + (glyph.note[30:] and unichr(0x2026)))) if glyph.markColor: status.append("mark: %s" % ", ".join([str(c) for c in glyph.markColor])) self.w.statusBar.set(status) self.analyserPreview.construction.setGlyph(glyph) self.analyserPreview.build.enable(glyph is not None) if glyph is not None: self.analyserPreview.build.setTitle("Build %s" % glyph.name) else: self.analyserPreview.build.setTitle("Build") if glyph is not None and glyph.name in self.font: self.analyserPreview.origin.setGlyph(self.font[glyph.name]) else: self.analyserPreview.origin.setGlyph(None) def buildSingleGlyph(self, sender): glyph = self.preview.getSelectedGlyph() if glyph is None: return if self.font is None: return dest = self.font.newGlyph(glyph.name) dest.clear() glyph.draw(dest.getPen()) dest.unicode = glyph.unicode dest.note = glyph.note if glyph.markColor: dest.markColor = glyph.markColor dest.width = glyph.width # toolbar def generateGlyphs(self, sender): self.reload(update=False) if not self._glyphs: return if self.font is None: return BuildGlyphsSheet(self._glyphs, self.font, self.w) def reload(self, sender=None, update=True): self.constructionsCallback(self.constructions, update) def _saveFile(self, path): if self.font is not None: self.font.lib[self.fileNameKey] = os.path.splitext( os.path.basename(path))[0] txt = self.constructions.get() f = open(path, "w") f.write(txt) f.close() def saveFile(self, sender=None): preferredName = None if self.font is not None and self.font.path is not None: preferredName = os.path.splitext(os.path.basename( self.font.path))[0] if self.fileNameKey in self.font.lib.keys(): # see if we have saved this file before and use that as first choice preferredName = self.font.lib.get(self.fileNameKey) self.showPutFile(["glyphConstruction"], fileName=preferredName, callback=self._saveFile) def _openFile(self, paths): if paths: path = paths[0] f = open(path, "r") txt = f.read() f.close() self.constructions.set(txt) def openFile(self, sender=None): directory = fileName = None if self.font is not None and self.font.path is not None: if self.fileNameKey in self.font.lib.keys(): fileName = self.font.lib.get(self.fileNameKey, "") fileName += ".glyphConstruction" directory = os.path.dirname(self.font.path) fileName = os.path.join(directory, fileName) directory = None getFile(fileTypes=["glyphConstruction"], parentWindow=self.w.getNSWindow(), directory=directory, fileName=fileName, resultCallback=self._openFile) # self.showGetFile(["glyphConstruction"], callback=self._openFile) def analyse(self, sender=None): self.w.split.togglePane("analyser", False) self.reload() # notifications def fontChanged(self, notification): self.reload() def fontBecameCurrent(self, notification): font = notification["font"] self.subscribeFont(font) def fontResignCurrent(self, notification): self.unsubscribeFont() def windowCloseCallback(self, sender): self.unsubscribeFont() removeObserver(self, "fontBecameCurrent") removeObserver(self, "fontResignCurrent") super(GlyphBuilderController, self).windowCloseCallback(sender)
def __init__(self, font): self.font = None self._glyphs = [] statusBarHeight = 20 self.w = Window((900, 700), "Glyph Builder", minSize=(400, 400)) if NSApp().runningOnLion(): self.w.getNSWindow().setCollectionBehavior_(128) #NSWindowCollectionBehaviorFullScreenPrimary toolbarItems = [ dict(itemIdentifier="save", label="Save", imageNamed="toolbarScriptSave", callback=self.saveFile, ), dict(itemIdentifier="open", label="Open", imageNamed="toolbarScriptOpen", callback=self.openFile, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict(itemIdentifier="reload", label="Update", imageNamed="toolbarScriptReload", callback=self.reload, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict(itemIdentifier="analyse", label="Analyse", imageNamed="prefToolbarSort", callback=self.analyse, ), dict(itemIdentifier=NSToolbarFlexibleSpaceItemIdentifier), dict(itemIdentifier="buildGlyphs", label="Build Glyphs", imageNamed="toolbarRun", callback=self.generateGlyphs ), ] toolbar = self.w.addToolbar(toolbarIdentifier="GlyphBuilderControllerToolbar", toolbarItems=toolbarItems, addStandardItems=False) self.constructions = CodeEditor((0, 0, -0, -0),constructions, lexer=GlyphConstructionLexer()) #self.constructions.wrapWord(False) # in only availbel in the RoboFont 1.7 beta self.constructions.getNSScrollView().setBorderType_(NSNoBorder) self.preview = MultiLineView((0, 0, -0, -0), pointSize=50, lineHeight=500, applyKerning=False, displayOptions={"Beam" : False, "displayMode" : "Multi Line"}, selectionCallback=self.previewSelectionCallback) self.analyser = AnalyserTextEditor((0, 0, -0, -0), readOnly=True) self.analyserPreview = Group((0, 0, -0, -0)) self.analyserPreview.construction = GlyphPreview((0, 0, -0, -0), contourColor=NSColor.redColor(), componentColor=NSColor.redColor()) self.analyserPreview.construction.getNSView()._buffer = 100 self.analyserPreview.origin = GlyphPreview((0, 0, -0, -0),contourColor=NSColor.blackColor(), componentColor=NSColor.blackColor()) self.analyserPreview.origin.getNSView()._buffer = 100 self.analyserPreview.build = Button((10, -25, -10, 19), "Build", sizeStyle="small", callback=self.buildSingleGlyph) self.analyserPreview.build.enable(False) paneDescriptions = [ dict(view=self.analyser, identifier="analyserText", canCollapse=False, minSize=100), dict(view=self.analyserPreview, identifier="analyserPreview", canCollapse=False, minSize=100), ] self.analyserSplit = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, isVertical=False, dividerThickness=1) paneDescriptions = [ dict(view=self.constructions, identifier="constructions", canCollapse=False, minSize=200, maxSize=600, liveResizeable=False), dict(view=self.preview, identifier="preview", canCollapse=False, minSize=300, liveResizeable=True), dict(view=self.analyserSplit, identifier="analyser", canCollapse=True, minSize=100, maxSize=300, liveResizeable=False) ] self.w.split = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, dividerThickness=1) #self.w.split.showPane("analyser", True) self.w.statusBar = StatusBar((0, -statusBarHeight, -0, statusBarHeight)) self.w.statusBar.hiddenReload = Button((0, 0, -0, -0), "Reload", self.reload) button = self.w.statusBar.hiddenReload.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenReload.bind("\r", ["command"]) self.w.statusBar.hiddenSave = Button((0, 0, -0, -0), "Reload", self.saveFile) button = self.w.statusBar.hiddenSave.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenSave.bind("s", ["command"]) self.subscribeFont(font) self.setUpBaseWindowBehavior() addObserver(self, "fontBecameCurrent", "fontBecameCurrent") addObserver(self, "fontResignCurrent", "fontResignCurrent") self.w.open()
class GlyphBuilderController(BaseWindowController): def __init__(self, font): self.font = None self._glyphs = [] statusBarHeight = 20 self.w = Window((900, 700), "Glyph Builder", minSize=(400, 400)) if NSApp().runningOnLion(): self.w.getNSWindow().setCollectionBehavior_(128) #NSWindowCollectionBehaviorFullScreenPrimary toolbarItems = [ dict(itemIdentifier="save", label="Save", imageNamed="toolbarScriptSave", callback=self.saveFile, ), dict(itemIdentifier="open", label="Open", imageNamed="toolbarScriptOpen", callback=self.openFile, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict(itemIdentifier="reload", label="Update", imageNamed="toolbarScriptReload", callback=self.reload, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict(itemIdentifier="analyse", label="Analyse", imageNamed="prefToolbarSort", callback=self.analyse, ), dict(itemIdentifier=NSToolbarFlexibleSpaceItemIdentifier), dict(itemIdentifier="buildGlyphs", label="Build Glyphs", imageNamed="toolbarRun", callback=self.generateGlyphs ), ] toolbar = self.w.addToolbar(toolbarIdentifier="GlyphBuilderControllerToolbar", toolbarItems=toolbarItems, addStandardItems=False) self.constructions = CodeEditor((0, 0, -0, -0),constructions, lexer=GlyphConstructionLexer()) #self.constructions.wrapWord(False) # in only availbel in the RoboFont 1.7 beta self.constructions.getNSScrollView().setBorderType_(NSNoBorder) self.preview = MultiLineView((0, 0, -0, -0), pointSize=50, lineHeight=500, applyKerning=False, displayOptions={"Beam" : False, "displayMode" : "Multi Line"}, selectionCallback=self.previewSelectionCallback) self.analyser = AnalyserTextEditor((0, 0, -0, -0), readOnly=True) self.analyserPreview = Group((0, 0, -0, -0)) self.analyserPreview.construction = GlyphPreview((0, 0, -0, -0), contourColor=NSColor.redColor(), componentColor=NSColor.redColor()) self.analyserPreview.construction.getNSView()._buffer = 100 self.analyserPreview.origin = GlyphPreview((0, 0, -0, -0),contourColor=NSColor.blackColor(), componentColor=NSColor.blackColor()) self.analyserPreview.origin.getNSView()._buffer = 100 self.analyserPreview.build = Button((10, -25, -10, 19), "Build", sizeStyle="small", callback=self.buildSingleGlyph) self.analyserPreview.build.enable(False) paneDescriptions = [ dict(view=self.analyser, identifier="analyserText", canCollapse=False, minSize=100), dict(view=self.analyserPreview, identifier="analyserPreview", canCollapse=False, minSize=100), ] self.analyserSplit = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, isVertical=False, dividerThickness=1) paneDescriptions = [ dict(view=self.constructions, identifier="constructions", canCollapse=False, minSize=200, maxSize=600, liveResizeable=False), dict(view=self.preview, identifier="preview", canCollapse=False, minSize=300, liveResizeable=True), dict(view=self.analyserSplit, identifier="analyser", canCollapse=True, minSize=100, maxSize=300, liveResizeable=False) ] self.w.split = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, dividerThickness=1) #self.w.split.showPane("analyser", True) self.w.statusBar = StatusBar((0, -statusBarHeight, -0, statusBarHeight)) self.w.statusBar.hiddenReload = Button((0, 0, -0, -0), "Reload", self.reload) button = self.w.statusBar.hiddenReload.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenReload.bind("\r", ["command"]) self.w.statusBar.hiddenSave = Button((0, 0, -0, -0), "Reload", self.saveFile) button = self.w.statusBar.hiddenSave.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenSave.bind("s", ["command"]) self.subscribeFont(font) self.setUpBaseWindowBehavior() addObserver(self, "fontBecameCurrent", "fontBecameCurrent") addObserver(self, "fontResignCurrent", "fontResignCurrent") self.w.open() def subscribeFont(self, font): self.unsubscribeFont() self.font = font if font is not None: self.preview.setFont(font) self.font.naked().addObserver(self, "fontChanged", "Font.Changed") self.constructionsCallback(self.constructions) def unsubscribeFont(self): if self.font is not None: self.preview.setFont(None) self.preview.set([]) self.font.removeObserver(self, notification="Font.Changed") self.font = None def constructionsCallback(self, sender, update=True): if self.font is None: return font = self.font.naked() self.glyphConstructorFont = GlyphConstructorFont(font) self._glyphs = [] errors = [] try: constructions = ParseGlyphConstructionListFromString(sender.get()) except GlyphBuilderError, err: constructions = [] errors.append(str(err)) for construction in constructions: if not construction: glyph = self.preview.createNewLineGlyph() elif construction in self.glyphConstructorFont.glyphsDone: glyph = self.glyphConstructorFont.glyphsDone[construction] else: try: constructionGlyph = GlyphConstructionBuilder(construction, self.glyphConstructorFont) except GlyphBuilderError, err: errors.append(str(err)) continue if constructionGlyph.name is None: errors.append(construction) continue glyph = font._instantiateGlyphObject() glyph.name = constructionGlyph.name glyph.unicode = constructionGlyph.unicode glyph.note = constructionGlyph.note glyph.mark = constructionGlyph.mark glyph.setParent(self.glyphConstructorFont) glyph.dispatcher = font.dispatcher glyph.width = constructionGlyph.width constructionGlyph.draw(glyph.getPen()) self.glyphConstructorFont.glyphsDone[glyph.name] = glyph self._glyphs.append(glyph)
class GlyphBuilderController(BaseWindowController): fileNameKey = "%s.lastSavedFileName" % defaultKey def __init__(self, font): self.font = None self._glyphs = [] statusBarHeight = 20 self.w = Window((900, 700), "Glyph Builder", minSize=(400, 400)) self.w.getNSWindow().setCollectionBehavior_(128) # NSWindowCollectionBehaviorFullScreenPrimary toolbarItems = [ dict( itemIdentifier="save", label="Save", imageNamed="toolbarScriptSave", callback=self.saveFile, ), dict( itemIdentifier="open", label="Open", imageNamed="toolbarScriptOpen", callback=self.openFile, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict( itemIdentifier="reload", label="Update", imageNamed="toolbarScriptReload", callback=self.reload, ), dict(itemIdentifier=NSToolbarSpaceItemIdentifier), dict( itemIdentifier="analyse", label="Analyse", imageNamed="prefToolbarSort", callback=self.analyse, ), dict(itemIdentifier=NSToolbarFlexibleSpaceItemIdentifier), dict( itemIdentifier="buildGlyphs", label="Build Glyphs", imageNamed="toolbarRun", callback=self.generateGlyphs ), ] self.w.addToolbar(toolbarIdentifier="GlyphBuilderControllerToolbar", toolbarItems=toolbarItems, addStandardItems=False) self.constructions = CodeEditor((0, 0, -0, -0), constructions, lexer=GlyphConstructionLexer()) # self.constructions.wrapWord(False) # in only availbel in the RoboFont 1.7 beta self.constructions.getNSScrollView().setBorderType_(NSNoBorder) self.preview = MultiLineView( (0, 0, -0, -0), pointSize=50, lineHeight=500, applyKerning=False, displayOptions={ "Beam": False, "displayMode": "Multi Line" }, selectionCallback=self.previewSelectionCallback ) self.analyser = AnalyserTextEditor((0, 0, -0, -0), readOnly=True) self.analyserPreview = Group((0, 0, -0, -0)) constructionColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(0, 0, 0, .6) self.analyserPreview.construction = GlyphPreview((0, 0, -0, -0), contourColor=constructionColor, componentColor=constructionColor) self.analyserPreview.construction.getNSView()._buffer = 100 originColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(1, 0, 0, .6) self.analyserPreview.origin = GlyphPreview((0, 0, -0, -0), contourColor=originColor, componentColor=originColor) self.analyserPreview.origin.getNSView()._buffer = 100 self.analyserPreview.build = Button((10, -25, -10, 19), "Build", sizeStyle="small", callback=self.buildSingleGlyph) self.analyserPreview.build.enable(False) paneDescriptions = [ dict(view=self.analyser, identifier="analyserText", canCollapse=False, minSize=100), dict(view=self.analyserPreview, identifier="analyserPreview", canCollapse=False, minSize=100), ] self.analyserSplit = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, isVertical=False, dividerThickness=1) paneDescriptions = [ dict(view=self.constructions, identifier="constructions", canCollapse=False, minSize=200, maxSize=600, liveResizeable=False), dict(view=self.preview, identifier="preview", canCollapse=False, minSize=300, liveResizeable=True), dict(view=self.analyserSplit, identifier="analyser", canCollapse=True, minSize=100, maxSize=300, liveResizeable=False) ] self.w.split = Splitter((0, 0, -0, -statusBarHeight), paneDescriptions=paneDescriptions, drawBorderLine=False, dividerThickness=1) # self.w.split.showPane("analyser", True) self.w.statusBar = StatusBar((0, -statusBarHeight, -0, statusBarHeight)) self.w.statusBar.hiddenReload = Button((0, 0, -0, -0), "Reload", self.reload) button = self.w.statusBar.hiddenReload.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenReload.bind("\r", ["command"]) self.w.statusBar.hiddenSave = Button((0, 0, -0, -0), "Reload", self.saveFile) button = self.w.statusBar.hiddenSave.getNSButton() button.setBezelStyle_(NSRoundRectBezelStyle) button.setAlphaValue_(0) self.w.statusBar.hiddenSave.bind("s", ["command"]) self.subscribeFont(font) self.setUpBaseWindowBehavior() addObserver(self, "fontBecameCurrent", "fontBecameCurrent") addObserver(self, "fontResignCurrent", "fontResignCurrent") self.w.open() def subscribeFont(self, font): self.unsubscribeFont() self.font = font if font is not None: self.preview.setFont(font) self.font.naked().addObserver(self, "fontChanged", "Font.Changed") self.constructionsCallback(self.constructions) def unsubscribeFont(self): if self.font is not None: self.preview.setFont(None) self.preview.set([]) self.font.removeObserver(self, notification="Font.Changed") self.font = None def constructionsCallback(self, sender, update=True): if self.font is None: return font = self.font.naked() self.glyphConstructorFont = GlyphConstructorFont(font) self._glyphs = [] errors = [] try: constructions = ParseGlyphConstructionListFromString(sender.get(), font) except GlyphBuilderError as err: constructions = [] errors.append(str(err)) for construction in constructions: if not construction: glyph = self.preview.createNewLineGlyph() elif construction in self.glyphConstructorFont.glyphsDone: glyph = self.glyphConstructorFont.glyphsDone[construction] else: try: constructionGlyph = GlyphConstructionBuilder(construction, self.glyphConstructorFont) except GlyphBuilderError as err: errors.append(str(err)) continue if constructionGlyph.name is None: errors.append(construction) continue if RoboFontVersion < "2.0": glyph = font._instantiateGlyphObject() else: glyph = font.layers.defaultLayer.instantiateGlyphObject() glyph.name = constructionGlyph.name glyph.unicode = constructionGlyph.unicode glyph.note = constructionGlyph.note glyph.markColor = constructionGlyph.markColor if RoboFontVersion < "2.0": glyph.setParent(self.glyphConstructorFont) glyph.dispatcher = font.dispatcher else: glyph._font = weakref.ref(self.glyphConstructorFont) # glyph._dispatcher = font._dispatcher glyph.width = constructionGlyph.width constructionGlyph.draw(glyph.getPen()) self.glyphConstructorFont.glyphsDone[glyph.name] = glyph self._glyphs.append(glyph) if errors: print("Errors:") print("\n".join(errors)) if update: self.preview.set(self._glyphs) self.analyser.set(analyseConstructions(font, self._glyphs)) # preview def previewSelectionCallback(self, sender): def _niceNumber(value): i = int(value) if i == value: return "%i" % value else: return "%.2f" % value glyph = sender.getSelectedGlyph() if glyph is not None and glyph.name is None: glyph = None status = [] if glyph is not None: width = _niceNumber(glyph.width) leftMargin = _niceNumber(glyph.leftMargin) rightMargin = _niceNumber(glyph.rightMargin) status = [ glyph.name, "width: %s left: %s right: %s" % (width, leftMargin, rightMargin), "components: %s" % (", ".join([component.baseGlyph for component in glyph.components])) ] if glyph.unicode: status.append("unicode: %04X" % glyph.unicode) if glyph.note: status.append("note: %s" % (glyph.note[:30] + (glyph.note[30:] and unichr(0x2026)))) if glyph.markColor: status.append("mark: %s" % ", ".join([str(c) for c in glyph.markColor])) self.w.statusBar.set(status) self.analyserPreview.construction.setGlyph(glyph) self.analyserPreview.build.enable(glyph is not None) if glyph is not None: self.analyserPreview.build.setTitle("Build %s" % glyph.name) else: self.analyserPreview.build.setTitle("Build") if glyph is not None and glyph.name in self.font: self.analyserPreview.origin.setGlyph(self.font[glyph.name]) else: self.analyserPreview.origin.setGlyph(None) def buildSingleGlyph(self, sender): glyph = self.preview.getSelectedGlyph() if glyph is None: return if self.font is None: return dest = self.font.newGlyph(glyph.name) dest.clear() glyph.draw(dest.getPen()) dest.unicode = glyph.unicode dest.note = glyph.note if glyph.markColor: dest.markColor = glyph.markColor dest.width = glyph.width # toolbar def generateGlyphs(self, sender): self.reload(update=False) if not self._glyphs: return if self.font is None: return BuildGlyphsSheet(self._glyphs, self.font, self.w) def reload(self, sender=None, update=True): self.constructionsCallback(self.constructions, update) def _saveFile(self, path): if self.font is not None: self.font.lib[self.fileNameKey] = os.path.splitext(os.path.basename(path))[0] txt = self.constructions.get() f = open(path, "w") f.write(txt) f.close() def saveFile(self, sender=None): preferredName = None if self.font is not None and self.font.path is not None: preferredName = os.path.splitext(os.path.basename(self.font.path))[0] if self.fileNameKey in self.font.lib.keys(): # see if we have saved this file before and use that as first choice preferredName = self.font.lib.get(self.fileNameKey) self.showPutFile(["glyphConstruction"], fileName=preferredName, callback=self._saveFile) def _openFile(self, paths): if paths: path = paths[0] f = open(path, "r") txt = f.read() f.close() self.constructions.set(txt) def openFile(self, sender=None): directory = fileName = None if self.font is not None and self.font.path is not None: if self.fileNameKey in self.font.lib.keys(): fileName = self.font.lib.get(self.fileNameKey, "") fileName += ".glyphConstruction" directory = os.path.dirname(self.font.path) fileName = os.path.join(directory, fileName) directory = None getFile(fileTypes=["glyphConstruction"], parentWindow=self.w.getNSWindow(), directory=directory, fileName=fileName, resultCallback=self._openFile) # self.showGetFile(["glyphConstruction"], callback=self._openFile) def analyse(self, sender=None): self.w.split.togglePane("analyser", False) self.reload() # notifications def fontChanged(self, notification): self.reload() def fontBecameCurrent(self, notification): font = notification["font"] self.subscribeFont(font) def fontResignCurrent(self, notification): self.unsubscribeFont() def windowCloseCallback(self, sender): self.unsubscribeFont() removeObserver(self, "fontBecameCurrent") removeObserver(self, "fontResignCurrent") super(GlyphBuilderController, self).windowCloseCallback(sender)
def __init__(self): self.w = Window((400, 400), minSize=(300, 300)) self.w.e = CodeEditor((0, 0, -0, -0), t, lexer=GlyphConstructionLexer()) self.w.open()