-
-
Save connordavenport/e3fef388c79fb6b2a7589ac0dc80ebcc to your computer and use it in GitHub Desktop.
Toggle Metrics Machine pair list
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
''' | |
Toggle the pair list of an open MetricsMachine (extension) window | |
from all kerning pairs (sorted by glyph order) to exceptions and back. | |
''' | |
import metricsMachine | |
from mm4.interface.documentWindow import MMDocumentWindowController | |
from mm4.mmScripting import _getMainWindowControllerForFont, MetricsMachineScriptingError | |
import mm4 | |
f = CurrentFont() | |
# code from Tal -------- | |
def getMetricsMachineController(): | |
import gc | |
for obj in gc.get_objects(): | |
if hasattr(obj, "__class__"): | |
if obj.__class__.__name__ == "MetricsMachineController": | |
return obj | |
def openMetricsMachineWindow(fontPartsFont): | |
defconFont = fontPartsFont.naked() | |
try: | |
windowController = _getMainWindowControllerForFont(fontPartsFont) | |
except MetricsMachineScriptingError: | |
mmController = getMetricsMachineController() | |
docController = MMDocumentWindowController(defconFont) | |
document = fontPartsFont.document() | |
if document is not None: | |
docController.assignToDocument(document) | |
mmController._openControllers[defconFont] = controller | |
# ---------------------- | |
def get_exception_pairs(f, kerning_pairs): | |
''' | |
Find kerning exceptions. | |
''' | |
mmFont = metricsMachine.MetricsMachineFont(f.path,False) | |
exceptions = [] | |
for pair in kerning_pairs: | |
if mmFont.kerning.isException(pair): | |
exceptions.append(pair) | |
return exceptions | |
def get_repr_glyph(item_name, glyph_order): | |
''' | |
Get a representative glyph name for a given group. | |
The level of importance is deduced by the glyph’s position in the | |
glyph order: lower = better | |
''' | |
glyph_list = f.groups.get(item_name, [item_name]) | |
sorted_glyph_list = sorted(glyph_list, key=lambda g: glyph_order.index(g)) | |
return sorted_glyph_list[0] | |
glyph_order = f.glyphOrder | |
mm_all_pairs = [] | |
mm_exc_pairs = [] | |
sorted_kerning = sorted( | |
f.kerning.keys(), key=lambda p: | |
( | |
glyph_order.index(get_repr_glyph(p[0], glyph_order)), | |
glyph_order.index(get_repr_glyph(p[1], glyph_order)))) | |
for pair in get_exception_pairs(f, sorted_kerning): | |
left, right = pair | |
mm_exc_pairs.append(( | |
get_repr_glyph(left, glyph_order), | |
get_repr_glyph(right, glyph_order))) | |
for pair in sorted_kerning: | |
left, right = pair | |
mm_all_pairs.append(( | |
get_repr_glyph(left, glyph_order), | |
get_repr_glyph(right, glyph_order))) | |
try: | |
if metricsMachine.GetPairList() == mm_all_pairs: | |
metricsMachine.SetPairList(mm_exc_pairs, f) | |
else: | |
metricsMachine.SetPairList(mm_all_pairs, f) | |
except mm4.mmScripting.MetricsMachineScriptingError: | |
openMetricsMachineWindow(f) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment