pythonfontforge.py 2.16 KB
import sys  
import os
import fontforge                                 #Load the module
import psMat
import centerglyph

def center_glyph(glyph, vcenter=0, lbearing=15, rbearing=None):
    '''Center a glyph.'''
    # Shift width
    if not rbearing:
        rbearing = lbearing
    bounding = glyph.boundingBox() # xmin,ymin,xmax,ymax
    width = (bounding[2] - bounding[0])
    width_new = width+(lbearing+rbearing)
    glyph.width = width_new
    glyph.left_side_bearing = lbearing
    glyph.right_side_bearing = rbearing
    # Shift Height
    center_glyph_height(glyph, vcenter, bounding)



def fit_glyph_height(glyph, em, percent=0.95):
    '''Scale a glyph verticaly to fit the width.'''
    bounding = glyph.boundingBox() # xmin, ymin, xmax, ymax
    gwidth = (bounding[2] - bounding[0])
    gheight = (bounding[3] - bounding[1])
    if not gwidth or not gheight:
        return
    rate = em*percent/gheight
    #print gwidth,gheight, rate, em, percent
    # Don't excess the height 
    #if rate*gwidth > em:
    #    rate = em*percent/gwidth
    matrix = psMat.scale(rate,rate)
	#print matrix
    glyph.transform(matrix)


def center_glyph_height(glyph, vcenter, abound=None):
    bounding = abound or glyph.boundingBox() # xmin,ymin,xmax,ymax
    glyphcenter = (bounding[3] - bounding[1])/2+bounding[1]
    deltay = vcenter - glyphcenter

    matrix = psMat.translate(0, deltay)
    glyph.transform(matrix)
    
print ('start')
font = fontforge.font()
fontfile = sys.argv[1]
fontpath = sys.argv[2]
fontchar = sys.argv[3]
glyph =  font.createMappedChar(fontchar)

glyph.importOutlines(fontpath+fontfile+'.svg')

##
# Aufruf um Glyph hoehe auf Maximal zu skalieren und zu positionieren (hoehe und breite werden gleich skaliert) 
#

fit_glyph_height(glyph, 1000, 0.95)
center_glyph(glyph, 0, 15, None)

print( font )

font.fullname = fontfile+' Font Medium'
font.familyname = fontfile+' Font'
font.fontname = fontfile+'FontMedium'
font.weight = 'medium'
font.round() # this one is needed to make simplify more reliable
font.removeOverlap()
font.round()
font.autoHint()
glyph.getPosSub('*')

font.generate(fontpath+fontfile+'.ttf')
#font.generate(fontpath+fontfile+'.otf', flags=("round", "opentype"))