def effective_point_size_for_width( font: QFont, width: float, width_for_font: Callable[[QFont], float], step=1.0, minsize=1., ) -> float: start = max(QFontInfo(font).pointSizeF(), minsize) font.setPointSizeF(start) fix = 0 while width_for_font(font) > width and start - (fix + step) >= minsize: fix += step font.setPointSizeF(start - fix) return QFontInfo(font).pointSizeF()
def font_resize(font, factor, minsize=None, maxsize=None): font = QFont(font) fontinfo = QFontInfo(font) size = fontinfo.pointSizeF() * factor if minsize is not None: size = max(size, minsize) if maxsize is not None: size = min(size, maxsize) font.setPointSizeF(size) return font
def effective_point_size_for_height( font: QFont, height: float, step=0.25, minsize=1. ) -> float: font = QFont(font) start = max(math.ceil(height), minsize) font.setPointSizeF(start) fix = 0 while QFontMetrics(font).height() > height and start - (fix + step) > minsize: fix += step font.setPointSizeF(start - fix) return QFontInfo(font).pointSizeF()