Skip to content

Commit

Permalink
Add ShowSymmetries plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Nov 2, 2015
1 parent 4b5616f commit 531dc80
Show file tree
Hide file tree
Showing 13 changed files with 775 additions and 0 deletions.
90 changes: 90 additions & 0 deletions ShowSymmetries.glyphsReporter/Contents/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDisplayName</key>
<string>ShowSymmetries</string>
<key>CFBundleExecutable</key>
<string>ShowSymmetries</string>
<key>CFBundleIdentifier</key>
<string>org.simon-cozens.ShowSymmetries</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>ShowSymmetries</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>4</string>
<key>CFBundleShortVersionString</key>
<string>1.1.1</string>
<key>UpdateFeedURL</key>
<string></string>
<key>productPageURL</key>
<string></string>
<key>LSHasLocalizedDisplayName</key>
<false/>
<key>NSAppleScriptEnabled</key>
<false/>
<key>NSHumanReadableCopyright</key>
<string>Copyright, by Simon Cozens, 2015</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>ShowSymmetries</string>
<key>PyMainFileNames</key>
<array>
<string>__boot__</string>
</array>
<key>PyOptions</key>
<dict>
<key>alias</key>
<true/>
<key>argv_emulation</key>
<false/>
<key>no_chdir</key>
<false/>
<key>optimize</key>
<integer>0</integer>
<key>prefer_ppc</key>
<false/>
<key>site_packages</key>
<false/>
<key>use_pythonpath</key>
<false/>
</dict>
<key>PyResourcePackages</key>
<array>
<string>lib/python2.6</string>
<string>lib/python2.6/lib-dynload</string>
<string>lib/python2.6/site-packages.zip</string>
<string>lib/python26.zip</string>
</array>
<key>PyRuntimeLocations</key>
<array>
<string>@executable_path/../Frameworks/Python.framework/Versions/2.6/Python</string>
<string>/System/Library/Frameworks/Python.framework/Versions/2.6/Python</string>
</array>
<key>PythonInfoDict</key>
<dict>
<key>PythonExecutable</key>
<string>/usr/bin/python2.6</string>
<key>PythonLongVersion</key>
<string>2.6.7 (r267:88850, Oct 11 2012, 20:15:00)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)]</string>
<key>PythonShortVersion</key>
<string>2.6</string>
<key>py2app</key>
<dict>
<key>template</key>
<string>bundle</string>
<key>version</key>
<string>0.6.3</string>
</dict>
</dict>
</dict>
</plist>
Binary file not shown.
1 change: 1 addition & 0 deletions ShowSymmetries.glyphsReporter/Contents/MacOS/python
1 change: 1 addition & 0 deletions ShowSymmetries.glyphsReporter/Contents/PkgInfo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BNDL????
152 changes: 152 additions & 0 deletions ShowSymmetries.glyphsReporter/Contents/Resources/ShowSymmetries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/usr/bin/env python
# encoding: utf-8
import objc
from Foundation import *
from AppKit import *
import sys, os, re
from Quartz import CGContextGetCTM, CGAffineTransformInvert, CGContextConcatCTM, CGContextRestoreGState, CGContextSaveGState

MainBundle = NSBundle.mainBundle()
path = MainBundle.bundlePath() + "/Contents/Scripts"
if not path in sys.path:
sys.path.append( path )

import GlyphsApp
import glyphmonkey
from glyphmonkey import GSNodeSet

GlyphsReporterProtocol = objc.protocolNamed( "GlyphsReporter" )

class ShowSymmetries ( NSObject, GlyphsReporterProtocol ):

def init( self ):
bundle = NSBundle.bundleForClass_(ShowSymmetries)
self.rotational = bundle.imageForResource_("rotational")
self.reflectional = bundle.imageForResource_("reflectional")
self.reflecty = bundle.imageForResource_("reflecty")
return self

def interfaceVersion( self ):
return 1

def title( self ):
return "Symmetries"

def keyEquivalent( self ):
return None

def modifierMask( self ):
return 0

def drawForegroundForLayer_( self, Layer ):
pass

def drawSymmetries( self, Layer ):
currentZoom = self.getScale()
l = Layer.copy()
if l.pathCount() > 1:
l.removeOverlap()
ns = l.selectedNodeSet()
if len(ns) == 1: return
if len(ns) == 0:
sel = []
for p in Layer.paths:
for n in p.nodes:
sel.append(n)
ns = GSNodeSet(sel)

ox, oy = ns.center

height = self.controller.view().bounds().size.height
width = self.controller.view().bounds().size.width
context = NSGraphicsContext.currentContext().CGContext()
oldat = CGContextGetCTM(context)
CGContextSaveGState(context)
inverted = CGAffineTransformInvert(oldat)
CGContextConcatCTM(context, inverted)

if ns.equal(ns.copy().rotate(angle=180, ox=ox, oy=oy)):
self.rotational.drawInRect_(NSMakeRect(width-125,height-(15+25),25,25))

if ns.equal(ns.copy().reflect()):
self.reflectional.drawInRect_(NSMakeRect(width-95,height-(15+25),25,25))

if ns.equal(ns.copy().reflect(NSMakePoint(ox,oy), NSMakePoint(ox+100,oy))):
self.reflecty.drawInRect_(NSMakeRect(width-65,height-(15+25),25,25))

CGContextConcatCTM(context, oldat)
CGContextRestoreGState(context)

def drawBackgroundForLayer_( self, Layer ):
try:
self.drawSymmetries( Layer )
except Exception as e:
self.logToConsole( "drawBackgroundForLayer_: %s" % str(e) )

def drawBackgroundForInactiveLayer_( self, Layer ):
pass

def drawTextAtPoint( self, text, textPosition, fontSize=10.0, fontColor=NSColor.colorWithCalibratedRed_green_blue_alpha_( 1, 0, .5, 1 ) ):
"""
Use self.drawTextAtPoint( "blabla", myNSPoint ) to display left-aligned text at myNSPoint.
"""
try:
glyphEditView = self.controller.graphicView()
currentZoom = self.getScale()
fontAttributes = {
NSFontAttributeName: NSFont.labelFontOfSize_( fontSize/currentZoom ),
NSForegroundColorAttributeName: fontColor }
displayText = NSAttributedString.alloc().initWithString_attributes_( text, fontAttributes )
textAlignment = 2 # top left: 6, top center: 7, top right: 8, center left: 3, center center: 4, center right: 5, bottom left: 0, bottom center: 1, bottom right: 2
glyphEditView.drawText_atPoint_alignment_( displayText, textPosition, textAlignment )
except Exception as e:
self.logToConsole( "drawTextAtPoint: %s" % str(e) )

def needsExtraMainOutlineDrawingForInactiveLayer_( self, Layer ):
return True

def getHandleSize( self ):
"""
Returns the current handle size as set in user preferences.
Use: self.getHandleSize() / self.getScale()
to determine the right size for drawing on the canvas.
"""
try:
Selected = NSUserDefaults.standardUserDefaults().integerForKey_( "GSHandleSize" )
if Selected == 0:
return 5.0
elif Selected == 2:
return 10.0
else:
return 7.0 # Regular
except Exception as e:
self.logToConsole( "getHandleSize: HandleSize defaulting to 7.0. %s" % str(e) )
return 7.0

def getScale( self ):
"""
self.getScale() returns the current scale factor of the Edit View UI.
Divide any scalable size by this value in order to keep the same apparent pixel size.
"""
try:
return self.controller.graphicView().scale()
except:
self.logToConsole( "Scale defaulting to 1.0" )
return 1.0

def setController_( self, Controller ):
"""
Use self.controller as object for the current view controller.
"""
try:
self.controller = Controller
except Exception as e:
self.logToConsole( "Could not set controller" )

def logToConsole( self, message ):
"""
The variable 'message' will be passed to Console.app.
Use self.logToConsole( "bla bla" ) for debugging.
"""
myLog = "Show %s plugin:\n%s" % ( self.title(), message )
NSLog( myLog )
47 changes: 47 additions & 0 deletions ShowSymmetries.glyphsReporter/Contents/Resources/__boot__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# def _site_packages():
# import site, sys, os
# paths = []
# prefixes = [sys.prefix]
# if sys.exec_prefix != sys.prefix:
# prefixes.append(sys.exec_prefix)
# for prefix in prefixes:
# if prefix == sys.prefix:
# paths.append(os.path.join("/Library/Python", sys.version[:3], "site-packages"))
# paths.append(os.path.join(sys.prefix, "Extras", "lib", "python"))
# else:
# paths.append(os.path.join(prefix, 'lib', 'python' + sys.version[:3], 'site-packages'))
# if os.path.join('.framework', '') in os.path.join(sys.prefix, ''):
# home = os.environ.get('HOME')
# if home:
# paths.append(os.path.join(home, 'Library', 'Python', sys.version[:3], 'site-packages'))
#
# # Workaround for a misfeature in setuptools: easy_install.pth places
# # site-packages way too early on sys.path and that breaks py2app bundles.
# # NOTE: this hacks into an undocumented feature of setuptools and
# # might stop to work without warning.
# sys.__egginsert = len(sys.path)
#
# for path in paths:
# site.addsitedir(path)
#
# _site_packages()
#
# def _path_inject():
# import sys
# sys.path[:0] = sys.path[0]
#
# _path_inject()

def _run(*scripts):
global __file__
import os, sys# , site
sys.frozen = 'macosx_plugin'
base = os.environ['RESOURCEPATH']
# site.addsitedir(base)
# site.addsitedir(os.path.join(base, 'Python', 'site-packages'))
for script in scripts:
path = os.path.join(base, script)
__file__ = path
execfile(path, globals(), globals())

_run('ShowSymmetries.py')
12 changes: 12 additions & 0 deletions ShowSymmetries.glyphsReporter/Contents/Resources/__error__.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
#
# This is the default bundletemplate error script
# Note that this DOES NOT present a GUI dialog, because
# it has no output on stdout, and has a return value of 0.
#
if ( test -n "$2" ) ; then
echo "[$1] Unexpected Exception:" 1>&2
echo "$2: $3" 1>&2
else
echo "[$1] Could not find a suitable Python runtime" 1>&2
fi
Loading

0 comments on commit 531dc80

Please sign in to comment.