Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> from matplotlib import rcParams
- >>> import os.path
- >>> afm_filename = os.path.join(rcParams['datapath'], 'fonts', 'afm', 'ptmr8a.afm')
- >>>
- >>> from matplotlib.afm import AFM
- >>> afm = AFM(open(afm_filename))
- >>> afm.string_width_height('What the heck?')
- (6220.0, 694)
- # Python 3 names -- see Note below
- import tkinter
- from tkinter import font as tkFont
- tkinter.Frame().destroy() # Enough to initialize resources
- arial36b = tkFont.Font(family='Arial', size=36, weight='bold')
- width = arial36b.measure("How wide is this?")
- print(width) # Prints: 404
- import Tkinter
- import tkFont
- import ctypes
- def GetTextDimensions(text, points, font):
- class SIZE(ctypes.Structure):
- _fields_ = [("cx", ctypes.c_long), ("cy", ctypes.c_long)]
- hdc = ctypes.windll.user32.GetDC(0)
- hfont = ctypes.windll.gdi32.CreateFontA(-points, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, font)
- hfont_old = ctypes.windll.gdi32.SelectObject(hdc, hfont)
- size = SIZE(0, 0)
- ctypes.windll.gdi32.GetTextExtentPoint32A(hdc, text, len(text), ctypes.byref(size))
- ctypes.windll.gdi32.SelectObject(hdc, hfont_old)
- ctypes.windll.gdi32.DeleteObject(hfont)
- return (size.cx, size.cy)
- for text, font in [
- ('....', 'Arial'),
- ('WWWW', 'Arial'),
- ('WWWW', 'Arial Narrow'),
- ('....', 'Courier New'),
- ('WWWW', 'Courier New'),
- ("Test", "Unknown font"),
- ('Test', 'Calibri')]:
Add Comment
Please, Sign In to add comment