from AppKit import NSFontManager from string import letters, punctuation, whitespace def fonts(s): return [f for f in NSFontManager.sharedFontManager().availableFonts() if f.lower().startswith(s.lower())] def typeset(s,x,y,h): if x + textwidth(s) > WIDTH: x = 0 y += h*10 h = 0 text(s, x, y) # Line height is equal to biggest font h = max(h, textheight(s_buff)) x += textwidth(s_buff) return x,y,h s = 'the lazy fox jumps over\n' s_buff, f_buff = '', '' x, y, h= 0, 100, 0 for c in s: if c in letters: s_buff += c if fonts(s_buff) != []: f_buff = fonts(s_buff)[0] elif c in whitespace or c in punctuation: font(f_buff) print f_buff x,y,h = typeset(c,x,y,h) x,y,h = typeset(s_buff,x,y,h) s_buff, f_buff= '', ''