Guest User

Untitled

a guest
Mar 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. >>> from matplotlib import rcParams
  2. >>> import os.path
  3.  
  4. >>> afm_filename = os.path.join(rcParams['datapath'], 'fonts', 'afm', 'ptmr8a.afm')
  5. >>>
  6. >>> from matplotlib.afm import AFM
  7. >>> afm = AFM(open(afm_filename))
  8. >>> afm.string_width_height('What the heck?')
  9. (6220.0, 694)
  10.  
  11. # Python 3 names -- see Note below
  12. import tkinter
  13. from tkinter import font as tkFont
  14.  
  15. tkinter.Frame().destroy() # Enough to initialize resources
  16. arial36b = tkFont.Font(family='Arial', size=36, weight='bold')
  17. width = arial36b.measure("How wide is this?")
  18. print(width) # Prints: 404
  19.  
  20. import Tkinter
  21. import tkFont
  22.  
  23. import ctypes
  24.  
  25. def GetTextDimensions(text, points, font):
  26. class SIZE(ctypes.Structure):
  27. _fields_ = [("cx", ctypes.c_long), ("cy", ctypes.c_long)]
  28.  
  29. hdc = ctypes.windll.user32.GetDC(0)
  30. hfont = ctypes.windll.gdi32.CreateFontA(-points, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, font)
  31. hfont_old = ctypes.windll.gdi32.SelectObject(hdc, hfont)
  32. size = SIZE(0, 0)
  33. ctypes.windll.gdi32.GetTextExtentPoint32A(hdc, text, len(text), ctypes.byref(size))
  34. ctypes.windll.gdi32.SelectObject(hdc, hfont_old)
  35. ctypes.windll.gdi32.DeleteObject(hfont)
  36. return (size.cx, size.cy)
  37.  
  38. for text, font in [
  39. ('....', 'Arial'),
  40. ('WWWW', 'Arial'),
  41. ('WWWW', 'Arial Narrow'),
  42. ('....', 'Courier New'),
  43. ('WWWW', 'Courier New'),
  44. ("Test", "Unknown font"),
  45. ('Test', 'Calibri')]:
Add Comment
Please, Sign In to add comment