def get_font_array(sz, chars=DEFAULT_CHARS): from guidata.qt.QtGui import QFont, QPainter, QColor font = QFont() font.setFixedPitch(True) font.setPixelSize(sz) font.setStyleStrategy(QFont.NoAntialias) dummy = QImage(10, 10, QImage.Format_ARGB32) pnt = QPainter(dummy) pnt.setFont(font) metric = pnt.fontMetrics() rct = metric.boundingRect(chars) pnt.end() h = rct.height() w = rct.width() img = QImage(w, h, QImage.Format_ARGB32) paint = QPainter() paint.begin(img) paint.setFont(font) paint.setBrush( QColor(255, 255, 255) ) paint.setPen( QColor(255, 255, 255) ) paint.drawRect(0, 0, w+1, h+1) paint.setPen( QColor(0, 0, 0)) paint.setBrush( QColor(0, 0, 0) ) paint.drawText(0, paint.fontMetrics().ascent(), chars) paint.end() try: data = img.bits().asstring(img.numBytes()) except SystemError: # Python 3 return npy = np.frombuffer(data, np.uint8) npy.shape = img.height(), img.bytesPerLine()/4, 4 return npy[:,:, 0]
def paintEvent(self, evt): painter = QPainter(self) if self.color is not None: painter.setPen(QPen(self.color)) painter.rotate(self.angle) transform = painter.transform().inverted()[0] rct = transform.mapRect(self.rect()) painter.drawText(rct, self.alignment(), self.text())