Created
June 3, 2015 21:45
-
-
Save dz0ny/3c89fcd43c8a50bb3f94 to your computer and use it in GitHub Desktop.
Width from font for badges.svg
This file contains hidden or 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
| def get_size(t): | |
| """ | |
| import string | |
| from PIL import ImageFont | |
| font = ImageFont.truetype('DejaVuSans.ttf', 12) | |
| sizes = dict() | |
| for c in list(string.printable): | |
| sizes.update({c:font.getsize(c)[0]}) | |
| """ | |
| sizes = {'o': 8, '[': 5, '*': 6, 'x': 6, 'M': 10, 'Q': 9, ')': 5, 'N': 9, | |
| 's': 7, '%': 11, 'C': 8, 'k': 7, '^': 10, 'e': 8, 'y': 7, '(': 5, | |
| 'X': 7, 'a': 8, '@': 13, 'b': 8, 'P': 8, ' ': 4, ':': 4, 'Z': 9, | |
| 'I': 3, '>': 10, 'G': 9, ';': 4, 'W': 11, 'K': 8, 'F': 7, 'E': 8, | |
| 'f': 4, "'": 3, 'T': 7, 'A': 8, '2': 8, '\n': 7, 'O': 9, 'c': 7, | |
| '$': 8, '?': 6, 'q': 8, 'S': 8, 'p': 8, '}': 8, '\r': 7, 'n': 8, | |
| '<': 10, 'w': 9, '7': 8, 'U': 9, '0': 8, '+': 10, 'h': 8, '_': 6, | |
| '6': 8, 'm': 11, '9': 8, 'j': 3, '1': 8, '\\': 4, 'V': 9, ']': 5, | |
| 'g': 8, '`': 6, '-': 4, '~': 10, '8': 8, 'J': 3, '|': 4, 'D': 9, | |
| '=': 10, '"': 5, 'z': 5, '{': 8, 'Y': 7, '3': 8, '5': 8, 'H': 9, | |
| '\x0c': 7, '#': 10, 'u': 8, 'R': 8, 'L': 6, 't': 5, '\t': 7, | |
| '4': 8, 'B': 8, '.': 4, 'r': 5, '/': 4, 'd': 8, ',': 4, 'i': 3, | |
| '\x0b': 7, 'l': 3, 'v': 7, '!': 5, '&': 10} | |
| return sum(map(lambda x: sizes.get(x, 0), list(t))) * 1.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment