Created
October 19, 2023 12:59
-
-
Save petrblahos/0e9bed90c71fd262c574be59398021f5 to your computer and use it in GitHub Desktop.
Drawing more characters from a ttf font.
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
import wx | |
from fontTools.pens.wxPen import WxPen | |
from fontTools.ttLib import TTFont | |
class MyFrame(wx.Frame): | |
def __init__(self, ttfont: TTFont): | |
wx.Frame.__init__(self, None, -1, "Font painter") | |
self.font = ttfont | |
self.Bind(wx.EVT_PAINT, self.on_paint) | |
def on_paint(self, evt): | |
evt.Skip() | |
dc = wx.PaintDC(self) | |
gc = wx.GraphicsContext.Create(dc) | |
height = self.GetSize()[1] | |
units_per_em = self.font['head'].unitsPerEm | |
scale = height / 4 / units_per_em | |
m = gc.CreateMatrix() | |
m.Translate(0, height // 2) | |
m.Scale(scale, -scale) | |
cmap = self.font['cmap'].getBestCmap() | |
for unicode in "AIai&32\u06CF87Š": | |
glyph_name = cmap.get(ord(unicode)) | |
if not glyph_name: | |
glyph_name = ".notdef" | |
glyph = self.draw_glyph(gc, glyph_name, m, wx.BLACK_BRUSH) | |
m.Translate(glyph.width, 0) | |
def draw_glyph(self, gc, glyph_name, m, brush): | |
glyph = self.font.getGlyphSet()[glyph_name] | |
pen = WxPen(self.font.getGlyphSet()) | |
glyph.draw(pen) | |
pen.path.Transform(m) | |
gc.SetBrush(brush) | |
gc.FillPath(pen.path) | |
return glyph | |
def show_ui(font): | |
app = wx.App() | |
frame = MyFrame(font) | |
frame.Show() | |
frame.Maximize(True) | |
app.MainLoop() | |
if "__main__" == __name__: | |
show_ui(TTFont("Roboto-Regular.ttf")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment