Skip to content

Instantly share code, notes, and snippets.

@tammoippen
Created July 22, 2019 10:13
Show Gist options
  • Save tammoippen/aa7249d34c506de75f07e7179fa8a896 to your computer and use it in GitHub Desktop.
Save tammoippen/aa7249d34c506de75f07e7179fa8a896 to your computer and use it in GitHub Desktop.
Print ttf fonts using braille / plottille canvas. `pip install plotille freetype-py` get font from https://fonts.google.com/specimen/Ubuntu+Mono . Inspired by https://mrandri19.github.io/2019/07/18/modern-text-rendering-linux-ep1.html
import math
import sys
import freetype
import plotille
def chunks(l, n):
n = max(1, n)
return (l[i:i+n] for i in range(0, len(l), n))
def init_face():
face = freetype.Face('./Ubuntu_Mono/UbuntuMono-Regular.ttf')
face.set_char_size(64*64)
return face
def char_buffer(face, char):
face.load_char(char)
return list(chunks(face.glyph.bitmap.buffer, face.glyph.bitmap.width))
def print_buffers_canvas(buffers):
assert len(buffers) > 0
height = max(len(b) for b in buffers)
width = sum(len(b[0]) for b in buffers) + len(buffers) // 2
canvas = plotille.Canvas(math.ceil(width / 2), math.ceil(height / 8), xmax=width, ymax=height)
for row_idx in range(height):
col_idx = 0
for bm in buffers:
diff = height - len(bm)
curr_idx = row_idx - diff
if curr_idx < 0:
col_idx += len(bm[0])
else:
row = bm[curr_idx]
for val in row:
if val > 0:
canvas.point(col_idx, height - row_idx)
col_idx += 1
col_idx += 1
print(canvas.plot())
face = init_face()
for val in sys.argv[1:]:
print_buffers_canvas([char_buffer(face, c) for c in val])
@tammoippen
Copy link
Author

Screenshot 2019-07-22 at 12 15 05

@tammoippen
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment