def __init__(self, brown_object, family_name, size, weight, italic):
"""
Args:
brown_object (Brush): The object this interface belongs to
family_name (str): The name of the font family
size (Unit): The size of the font
weight (int or None): The font weight. If `None`,
a normal weight will be used.
italic (bool): Italicized or not
"""
super().__init__(brown_object)
self.family_name = family_name
self.size = size
self.weight = weight
self.italic = italic
self.qt_object = QtGui.QFont(
self.family_name,
GraphicUnit(self.size).value,
self.weight if self.weight is not None else -1,
self.italic)
self._qt_font_info_object = QtGui.QFontInfo(self.qt_object)
self._qt_font_metrics_object = QtGui.QFontMetricsF(
self.qt_object,
brown._app_interface.view)
######## PUBLIC PROPERTIES ########
python类QFontMetricsF()的实例源码
def rescale(self):
fm = QFontMetricsF(self._font, self)
maxRect = fm.boundingRect(QRectF(self.rect()), Qt.AlignCenter,
str(self._maximum))
xscale = float(self.width())/maxRect.width()
yscale = float(self.height())/maxRect.height()
self._scale = min(xscale, yscale)
def reposition(self):
fm = QFontMetricsF(self._font, self)
rect = fm.boundingRect(QRectF(self.rect()), Qt.AlignCenter,
str(self._value))
self._xpos = -rect.width()/2.0
self._ypos = rect.height()/2.0 - fm.descent()
self.update()
# Provide getter and setter methods for the font property.
def rescale(self):
fm = QFontMetricsF(self._font, self)
maxRect = fm.boundingRect(QRectF(self.rect()), Qt.AlignCenter,
str(self._maximum))
xscale = float(self.width())/maxRect.width()
yscale = float(self.height())/maxRect.height()
self._scale = min(xscale, yscale)
def reposition(self):
fm = QFontMetricsF(self._font, self)
rect = fm.boundingRect(QRectF(self.rect()), Qt.AlignCenter,
str(self._value))
self._xpos = -rect.width()/2.0
self._ypos = rect.height()/2.0 - fm.descent()
self.update()
# Provide getter and setter methods for the font property.
def _add_line(self, scene, line_index, left, text_str, font, color, offset=0, align_right=False):
y = self._line_height * line_index + offset + self.h * 0.1
if line_index == 1:
self._first_lyrics_line_y = y
metrics = QFontMetricsF(font)
text_width = metrics.width(text_str)
max_text_width = (self.w - left - left)
overflow = text_width - max_text_width
if overflow <= 0:
text = scene.addText(text_str, font)
if align_right:
text.setPos(self.w - left - text_width, y)
else:
text.setPos(left, y)
text.setDefaultTextColor(color)
else:
scale_factor = max_text_width / text_width
if scale_factor >= 0.9:
text = scene.addText(text_str, font)
text.setPos(left, y)
text.setDefaultTextColor(color)
text.setTransform(QTransform().scale(scale_factor, 1.0))
else:
self._extra_lines_after.append(line_index)
idx = len(text_str) // 2
while idx < len(text_str) and not text_str[idx].isspace():
idx += 1
line_index = self._add_line(scene, line_index, left, text_str[:idx], font, color, offset)
line_index += 1
line_index = self._add_line(scene, line_index, left, "\t" + text_str[idx:], font, color,
offset - self._line_height * 0.1)
return line_index
def _calc_line_height(self, font):
metrics = QFontMetricsF(font)
line_height = metrics.height() * self.line_height_factor
return line_height
def rescale(self):
fm = QFontMetricsF(self._font, self)
maxRect = fm.boundingRect(QRectF(self.rect()), Qt.AlignCenter,
str(self._maximum))
xscale = float(self.width())/maxRect.width()
yscale = float(self.height())/maxRect.height()
self._scale = min(xscale, yscale)
def reposition(self):
fm = QFontMetricsF(self._font, self)
rect = fm.boundingRect(QRectF(self.rect()), Qt.AlignCenter,
str(self._value))
self._xpos = -rect.width()/2.0
self._ypos = rect.height()/2.0 - fm.descent()
self.update()
# Provide getter and setter methods for the font property.