| 1 |
import os |
| 2 |
import sys |
| 3 |
import importlib |
| 4 |
import fontforge |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
def composeAccented(glyph, glyphComponents): |
| 12 |
"""Compose accented glyphs. |
| 13 |
|
| 14 |
Get the components from the dictionary and build the new glyph |
| 15 |
from them.""" |
| 16 |
|
| 17 |
glif = glyph |
| 18 |
|
| 19 |
components = glyphComponents[glif.glyphname] |
| 20 |
|
| 21 |
base = components[0] |
| 22 |
marks = components[1:] |
| 23 |
|
| 24 |
greekUC = ['Alpha', 'Epsilon', 'Eta', 'Iota', 'Omicron', 'Rho', 'Upsilon', 'uni03A9'] |
| 25 |
|
| 26 |
glif.clear() |
| 27 |
glif.addReference(base) |
| 28 |
|
| 29 |
glif.useRefsMetrics(base) |
| 30 |
|
| 31 |
for mark in marks: |
| 32 |
glif.appendAccent(mark) |
| 33 |
|
| 34 |
l = glif.left_side_bearing |
| 35 |
|
| 36 |
if base in greekUC: |
| 37 |
if l < -19: |
| 38 |
glif.useRefsMetrics(base, False) |
| 39 |
glif.left_side_bearing = -20 |
| 40 |
else: |
| 41 |
glif.useRefsMetrics(base, True) |
| 42 |
|
| 43 |
elif base == 'space.stack': |
| 44 |
glif.useRefsMetrics(base, False) |
| 45 |
glif.left_side_bearing = 30 |
| 46 |
glif.right_side_bearing = 30 |
| 47 |
else: |
| 48 |
glif.useRefsMetrics(base, True) |
| 49 |
|
| 50 |
def buildAccentedGlyphs(junk,object): |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
font = object if type(object).__name__ == "font" else object.font |
| 58 |
font_file = os.path.normpath(font.path) |
| 59 |
top_level = os.path.split(os.path.split(font_file)[0])[0] |
| 60 |
tool_path = os.path.join(top_level, "tools") |
| 61 |
if tool_path not in sys.path: |
| 62 |
sys.path.append(tool_path) |
| 63 |
|
| 64 |
import_successful = True |
| 65 |
try: |
| 66 |
glyphcomponents = importlib.import_module("glyphcomponents") |
| 67 |
except: |
| 68 |
import_successful = False |
| 69 |
fontforge.postError("Cannot import Python module glyphcomponents", |
| 70 |
"This script is made to work with the SFDs residing in top_level/SFD and the module in top_level/tools.") |
| 71 |
|
| 72 |
if import_successful: |
| 73 |
try: |
| 74 |
reload(glyphcomponents) |
| 75 |
glyphComponents = glyphcomponents.glyphComponents |
| 76 |
|
| 77 |
if type(object).__name__ == "font": |
| 78 |
for glyph in object.selection.byGlyphs: |
| 79 |
if glyph.glyphname in glyphComponents.keys(): |
| 80 |
composeAccented(glyph, glyphComponents) |
| 81 |
else: |
| 82 |
continue |
| 83 |
else: |
| 84 |
if object.glyphname in glyphComponents.keys(): |
| 85 |
composeAccented(object, glyphComponents) |
| 86 |
else: |
| 87 |
logWarning('This glyph has no entry in the dictionary') |
| 88 |
|
| 89 |
except BaseException as e: |
| 90 |
fontforge.postError("Building glyphs failed", str(e)) |
| 91 |
|
| 92 |
fontforge.registerMenuItem(buildAccentedGlyphs,None,None,("Font","Glyph"),None,"Glyphbuilder"); |