MacOS – Rasterising text automatically

graphicsmacostext;

I'm working on a typeface, and am looking at generating test-cases of how specific strings are rendered. What's the simplest way of automatically rasterising many different text-files into a bitmap?

Best Answer

Add a html header with font selection (by mean of cp and >>) and then usewkhtmltoimage (https://wkhtmltopdf.org/) to generate images from that. Alternatively, look at the source code and write some 20 lines of Qt code using the same approach.

Proof of concept in PyQt5:

from PyQt5 import QtCore, QtGui, QtWidgets
app = QtWidgets.QApplication([])
w = QtWidgets.QWidget()
w.setWindowTitle('Textbox to image')
font = QtGui.QFont()
font.setFamily("Herculanum")
font.setBold(True)
textbox = QtWidgets.QTextEdit(w)
textbox.setFont(font)
textbox.resize(280, 40)
textbox.setText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr")
textbox.grab().save("image.png")

Result (png):

enter image description here

That, with custom distortion is how I actually created a large ML dataset for OCR from custom fonts.