pythonfontforge_1.py 6.27 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 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)
    
def CenterGlyph(junk, afont):
    '''Main function.'''
    center = (afont.ascent - afont.descent)/2

    # total 5% margin
    em = afont.ascent + afont.descent
    lbearing = int(em*0.05/2)

    for glyph in afont.selection.byGlyphs:
        center_glyph(glyph, center, lbearing)

def CenterHeight(junk, afont):
    '''Main function.'''
    center = (afont.ascent - afont.descent)/2

    for glyph in afont.selection.byGlyphs:
        center_glyph_height(glyph, center)

def fit_glyph(glyph, em, percent=0.95):
    '''Scale a glyph horizontally 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/gwidth
    #print gwidth, rate, em
    # Don't excess the height 
    if rate*gheight > em:
        rate = em*percent/gheight
    matrix = psMat.scale(rate)
    glyph.transform(matrix)

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 ScaleToEm(percent, afont):
    '''Scale a font to fit 5% less than the em.'''
    em = afont.ascent + afont.descent
    center = (afont.ascent - afont.descent)/2
    lbearing = int(em*0.05/2)
    if not percent:
        ret = fontforge.askString('Scale Percent?',
                'Percent of Line Height (em)?', '90' )
        if not ret:
            return
        try:
            percent = float(ret)/100
            if percent <= 0 or percent > 1:
                raise ValueError
        except ValueError:
            fontforge.postError('Wrong Percent!', "Need a value <= 100.")
            return
    for glyph in afont.selection.byGlyphs:
        fit_glyph(glyph, em, percent)
        #center_glyph(glyph, center, lbearing) # Maybe not move glyph?
        center_glyph_height(glyph, center) 

def get_max_size(afont):
    '''Get the max size of the selected glyphs.'''
    wmax = 0
    hmax = 0
    for glyph in afont.selection.byGlyphs:
        bbox = glyph.boundingBox()
        wmax = max(wmax, bbox[2] - bbox[0])
        hmax = max(hmax, bbox[3] - bbox[1])
    em = afont.ascent + afont.descent
    wpct = wmax and em/wmax*100
    hpct = hmax and em/hmax*100
    return wmax, hmax, wpct, hpct

def GetSelectedBound(junk, afont):
    '''Show max height and width of the selected glyphs.'''
    wmax, hmax, wpct, hpct = get_max_size(afont)
    msg = ('The max width and height of the selected glyphs are:\n'
            'Points:\t%d\t%d\nScale Factor to 100%%:\t%.3f\t  %.3f%%')
    msg = msg % (wmax, hmax, wpct, hpct)
    fontforge.postError('Max Demension', msg)









print ('start')
font = fontforge.font()
#font_in = "/home/dev/git/Magento/AncImage/ancSetup/ancfont.sfd"
#font_in = "/home/dev/git/Magento/AncImage/ancSetup/movie.sfd"
#font = fontforge.open(font_in)
#ScaleToEm(10,font)

fontfile = sys.argv[1]
fontpath = sys.argv[2]
fontchar = sys.argv[3]
# Create a char in the unicode 41 pos (an "A")
#glyph = font.createChar(41, 'B')
glyph =  font.createMappedChar(fontchar)

glyph.importOutlines(fontpath+fontfile+'.svg')
#print(glyph.boundingBox())
#print(glyph.activeLayer)
#glyph.removeoverlap()


#GetSelectedBound(glyph ,font)
#print (get_max_size(font))

##
#Keine Ahnung Warum aber so funzt es allerdings buggy
#
#fit_glyph(glyph, 1000, 1)
#fit_glyph(glyph, 1000,0.5);
fit_glyph_height(glyph, 1000, 0.95)
#CenterHeight(glyph, font)
#CenterMain(glyph, font)
center_glyph(glyph, 0, 15, None)

#matrix = psMat.scale(2,.5)
#glyph.transform(matrix)
#CenterHeight(glyph,font)
#matrix = psMat.translate(0,-1800)
#glyph.transform(matrix)
print(glyph.width)


#glyph.intersect()
#font['a'].unicode 

glyph =  font.createMappedChar('a')
glyph.importOutlines(fontpath+fontfile+'.svg')

#glyph.importOutlines()
#print( glyph.width )
print( font )
# Write the font from memory to a TTF file

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.simplify()
font.removeOverlap()
font.round()
font.autoHint()
#font.em =200
#font.addExtrema()
glyph.getPosSub('*')

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




















#amb=fontforge.open("Ambrosia.sfd")               #Open a font
#amb.selection.select(("ranges",None),"A","Z")    #select A-Z
#amb.copy()                                       #Copy those glyphs into the clipboard

#n=fontforge.font()                               #Create a new font
#n.selection.select(("ranges",None),"A","Z")      #select A-Z of it
#n.paste()                                        #paste the glyphs above in
#print n["A"].foreground                          #test to see that something
                                                 #  actually got pasted
#n.fontname="NewFont"                             #Give the new font a name
#n.save("NewFont.sfd")                            #and save it.