本文整理汇总了Python中cairo.Context.text_extents方法的典型用法代码示例。如果您正苦于以下问题:Python Context.text_extents方法的具体用法?Python Context.text_extents怎么用?Python Context.text_extents使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cairo.Context
的用法示例。
在下文中一共展示了Context.text_extents方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: layout
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import text_extents [as 别名]
def layout(self, context: cairo.Context):
super().layout(context)
context.set_font_size(self.font_size)
xb, yb, w, h, xa, ya = context.text_extents(self.title)
font_shape = Rectangle(Point(h/2 + self.distance, self.distance), xa, h)
self.__title_start_point = Point(font_shape.start.x,
font_shape.start.y + h)
outer_font_box = DrawableRectangle(
Point(font_shape.start.x - self.distance,
font_shape.start.y - self.distance),
font_shape.width + 2 * self.distance,
font_shape.height + 2 * self.distance
)
self.__outer_font_box = outer_font_box
wrapper_shape = DrawableRectangle(
Point(0, outer_font_box.start.y + outer_font_box.height / 2),
outer_font_box.start.x + max(
outer_font_box.width,
self.widget.shape.width
) + self.distance,
outer_font_box.height/2 + 2*self.distance +
self.widget.shape.height
)
self.widget.set_translate(
outer_font_box.start.x,
outer_font_box.start.y + outer_font_box.height + self.distance
)
self.__wrapper_shape = wrapper_shape
self.shape = DrawableRectangle(
Point(0, 0),
wrapper_shape.width,
wrapper_shape.start.y + wrapper_shape.height
)
示例2: on_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import text_extents [as 别名]
def on_draw(self, widget: Widget, context: cairo.Context):
if not self.is_shape_set:
self.layout(context)
context.set_font_size(self.font_size)
self.shape.draw_on_context(context)
context.set_source_rgb(1, 1, 1)
context.fill_preserve()
context.set_source_rgb(0, 0, 0)
context.stroke()
shape = self.shape
label = self.label
if len(label) > 0 and label[-1] == ' ':
label += '.'
xb, yb, w, h, xa, ya = context.text_extents(label)
context.rectangle(shape.start.x + self.padding,
shape.start.y,
shape.width - self.padding,
shape.height)
context.clip()
context.move_to(shape.start.x + (shape.width - self.padding - w)/2,
shape.start.y + shape.height - self.padding)
context.show_text(self.label)
示例3: on_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import text_extents [as 别名]
def on_draw(self, widget: Widget, context: cairo.Context):
for b in self._buttons:
b.set_shape_from_context(context)
shapes = [b.shape for b in self._buttons]
context.save()
context.select_font_face("", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
xb, yb, w, h, xa, ya = context.text_extents(self.title)
width = max(shape.width for shape in shapes)
width = max(width, xa)
container_width = self.container_size[0]
translation = Point(0, self.distance)
if container_width > width:
translation += Point((container_width)/2, 0)
else:
translation += Point(width/2, 0)
context.move_to(translation.x - xa/2, h + 2 * self.distance)
context.show_text(self.title)
context.restore()
height = h + self.distance * 3
for b in self._buttons:
height += b.shape.height + self.distance
self.min_size = width + 2 * self.distance, height + self.distance
start_point = context.get_current_point()
translation += Point(0, h + self.distance * 3)
context.translate(translation.x, translation.y)
distance_offset = Point(0, self.distance)
for b in self._buttons:
context.move_to(*start_point)
b.set_translate(translation.x, translation.y)
context.save()
b.on_draw(widget, context)
context.restore()
to_translate = Point(distance_offset.x,
distance_offset.y + b.shape.height)
context.translate(to_translate.x, to_translate.y)
translation += to_translate
示例4: set_shape_from_context
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import text_extents [as 别名]
def set_shape_from_context(self, context: cairo.Context):
label = self.label
padding = self.padding
context.set_font_size(self.font_size)
xb, yb, w, h, xa, ya = context.text_extents(label)
width = padding * 2 + xa
height = padding * 2 + h
height = max(height, self.min_height)
if self.origin == self.LEFT:
start = Point(0, 0)
elif self.origin == self.CENTER:
start = Point(-width/2, 0)
else:
start = Point(-width, 0)
self.shape = DrawableRoundedRectangle(start, width, height)
示例5: __init__
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import text_extents [as 别名]
#.........这里部分代码省略.........
self.stack.append(self.affine.terms())
self.command('q')
def restore(self):
self.affine = Affine(*self.stack.pop())
self.command('Q')
def user_to_device(self, x, y):
user = Point(x, y)
device = self.affine(user)
return (device.x, device.y)
def device_to_user(self, x, y):
device = Point(x, y)
user = self.affine.inverse()(device)
return (user.x, user.y)
def move_to(self, x, y):
self.point = Point(x, y)
self.command('%.3f %.3f m' % (x, y))
def rel_line_to(self, x, y):
end = Point(x, y).add(self.point)
self.point = end
self.command('%.3f %.3f l' % (end.x, end.y))
def set_source_rgb(self, r, g, b):
self.command('%.3f %.3f %.3f rg' % (r, g, b))
def fill(self):
self.command('f')
def set_source_surface(self, surf, x, y):
"""
"""
dim = surf.get_width(), surf.get_height()
img = Image.fromstring('RGBA', dim, surf.get_data())
# weird channel order
blue, green, red, alpha = img.split()
img = Image.merge('RGB', (red, green, blue))
png_buf = StringIO()
img.save(png_buf, 'PNG')
jpg_buf = StringIO()
img.save(jpg_buf, 'JPEG', quality=75)
if len(jpg_buf.getvalue()) < len(png_buf.getvalue()):
method, buffer, suffix = 'raw_jpeg', jpg_buf, '.jpg'
else:
method, buffer, suffix = 'raw_png', png_buf, '.png'
handle, filename = mkstemp(prefix='cairoutils-', suffix=suffix)
self.command(method, filename)
self.garbage.append(filename)
write(handle, buffer.getvalue())
close(handle)
def paint(self):
pass
def set_line_width(self, w):
self.command('%.3f w' % w)
def set_dash(self, a):
a = ' '.join(['%.3f' % v for v in a])
self.command('[%s] 0 d' % a)
def stroke(self):
self.command('S')
def rel_curve_to(self, a, b, c, d, e, f):
p1 = Point(a, b).add(self.point)
p2 = Point(c, d).add(self.point)
p3 = Point(e, f).add(self.point)
self.point = p3
self.command('%.3f %.3f %.3f %.3f %.3f %.3f c' % (p1.x, p1.y, p2.x, p2.y, p3.x, p3.y))
def set_font_face(self, font):
self.context.set_font_face(font)
def set_font_size(self, size):
self.context.set_font_size(size)
# SetFont here because only the size gives a clue to the correct weight
self.command('SetFont', 'Liberation Sans', (size > 14) and 'B' or '')
self.command('SetFontSize', size)
def show_text(self, text):
x, y = self.point.x, self.point.y
text = text.decode('utf8')
# invert the vertical flip in self.show_page() before showing text.
self.command('q 1 0 0 -1 0 0 cm BT %.3f %.3f Td (%s) Tj ET Q' % (x, -y, text))
def text_extents(self, text):
return self.context.text_extents(text)
示例6: set_shape_from_context
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import text_extents [as 别名]
def set_shape_from_context(self, context: cairo.Context):
widths_heights = []
done = False
while not done:
max_width = 0
max_height = 0
context.set_font_size(self.font_size)
for e in reversed(self._elements):
xb, yb, w, h, xa, ya = context.text_extents(e.label)
widths_heights.append((xa, h))
e.width = xa
e.height = h
max_width = max(max_width, xa)
max_height = max(max_height, h)
# adjust font size in case it's too big
if self.max_size is None:
done = True
else:
if self.orientation == Orientation.HORIZONTAL:
reference = max_height
else:
reference = max_width
if reference + 2 * self.line_distance <= self.max_size:
done = True
else:
self.font_size -= 1
positions = []
width = self.element_distance
height = self.element_distance
def get_padding(actual_size):
if self.max_size is not None:
return (self.max_size - actual_size) / 2
else:
return self.line_distance
if self.orientation == Orientation.HORIZONTAL:
def handle_extents(e: _GuideElement):
nonlocal width, height, positions, max_height
width += e.width
e.position = (-width,
max_height + get_padding(max_height))
width += self.element_distance
else:
def handle_extents(e: _GuideElement):
nonlocal width, height, positions, max_width
e.position = (get_padding(e.width),
-height)
height += e.height + self.element_distance
# for w, h in widths_heights:
# handle_extents(w, h)
for element in reversed(self._elements):
handle_extents(element)
if self.orientation == Orientation.HORIZONTAL:
height = max_height + get_padding(max_height) * 2
width = width - self.element_distance + self.line_distance
base_point = Point(-width, 0)
else:
width = max_width + get_padding(max_width) * 2
height = height - self.element_distance + self.line_distance
base_point = Point(0, -height)
self.shape = Rectangle(base_point, width, height)
示例7: __init__
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import text_extents [as 别名]
#.........这里部分代码省略.........
def device_to_user(self, x, y):
device = Point(x, y)
user = self.affine.inverse()(device)
return (user.x, user.y)
def move_to(self, x, y):
self.point = Point(x, y)
self.command("%.3f %.3f m" % (x, y))
def line_to(self, x, y):
self.point = Point(x, y)
self.command("%.3f %.3f l" % (x, y))
def rel_move_to(self, x, y):
end = Point(x, y).add(self.point)
self.point = end
self.command("%.3f %.3f m" % (end.x, end.y))
def rel_line_to(self, x, y):
end = Point(x, y).add(self.point)
self.point = end
self.command("%.3f %.3f l" % (end.x, end.y))
def set_source_rgb(self, r, g, b):
self.command("%.3f %.3f %.3f rg" % (r, g, b))
def fill(self):
self.command("f")
def set_source_surface(self, surf, x, y):
"""
"""
dim = surf.get_width(), surf.get_height()
img = Image.fromstring("RGBA", dim, surf.get_data())
# weird channel order
blue, green, red, alpha = img.split()
img = Image.merge("RGB", (red, green, blue))
png_buf = StringIO()
img.save(png_buf, "PNG")
jpg_buf = StringIO()
img.save(jpg_buf, "JPEG", quality=75)
if len(jpg_buf.getvalue()) < len(png_buf.getvalue()):
method, buffer, suffix = "raw_jpeg", jpg_buf, ".jpg"
else:
method, buffer, suffix = "raw_png", png_buf, ".png"
handle, filename = mkstemp(prefix="cairoutils-", suffix=suffix)
self.command(method, filename)
self.garbage.append(filename)
write(handle, buffer.getvalue())
close(handle)
def paint(self):
pass
def set_line_width(self, w):
self.command("%.3f w" % w)
def set_dash(self, a):
a = " ".join(["%.3f" % v for v in a])
self.command("[%s] 0 d" % a)
def stroke(self):
self.command("S")
def rel_curve_to(self, a, b, c, d, e, f):
p1 = Point(a, b).add(self.point)
p2 = Point(c, d).add(self.point)
p3 = Point(e, f).add(self.point)
self.point = p3
self.command("%.3f %.3f %.3f %.3f %.3f %.3f c" % (p1.x, p1.y, p2.x, p2.y, p3.x, p3.y))
def set_font_face(self, font):
self.context.set_font_face(font)
def set_font_size(self, size):
self.context.set_font_size(size)
# SetFont here because only the size gives a clue to the correct weight
self.command("SetFont", "Helvetica", (size > 14) and "B" or "")
self.command("SetFontSize", size)
def show_text(self, text):
x, y = self.point.x, self.point.y
text = text.decode("utf8")
# invert the vertical flip in self.show_page() before showing text.
self.command("q 1 0 0 -1 0 0 cm BT %.3f %.3f Td (%s) Tj ET Q" % (x, -y, text))
def text_extents(self, text):
""" Width is the third element of the returned array.
"""
return self.context.text_extents(text)
示例8: _paint_panel
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import text_extents [as 别名]
#.........这里部分代码省略.........
max_y = math.ceil(max_y)
if typ == 2:
if min_y < 0 and max_y < 0:
max_y = 0
elif min_y > 0 and max_y > 0:
min_y = 0
# Определяем цвета
colors = [[1, 0, 0], [0, 0.65, 0.31], [0, 0, 1], [1, 0, 1]]
off_y = (max_y - min_y) / 10
min_y -= off_y
max_y += off_y
try:
kx = (max_x - min_x) / (width - left - right)
ky = (max_y - min_y) / (height - bottom)
if ky == 0:
ky = 1
except:
kx, ky = 1, 1
img = ImageSurface(FORMAT_ARGB32, width, height)
ctx = Context(img)
width -= right
ctx.set_line_width(1)
# Рисуем сетку
ctx.set_font_size(12)
try:
b_w, b_h = ctx.text_extents("00-00-0000")[2:4]
# Метки на оси Y
count = math.ceil(max_y) - math.ceil(min_y)
space_count = math.ceil(count / ((height - bottom) / (b_h * 1.5)))
sc = 0
for i in range(math.ceil(min_y), math.ceil(max_y)):
if sc == 0:
y = height - bottom + (min_y - i) / ky
ctx.set_source_rgb(*(color_x_line))
ctx.move_to(left, y)
ctx.line_to(width, y)
ctx.stroke()
ctx.set_source_rgb(0, 0, 0)
num = str(i)
tw, th = ctx.text_extents(num)[2:4]
ctx.move_to(left - 5 - tw, y + th // 2)
ctx.show_text(num)
sc = space_count
sc -= 1
# Метки на оси Х
x_step = 3600
if interval == "-6 hour" or interval == "-12 hour" or interval == "-1 day":
# Дополнительно метки часов
x_step = 3600
for i in range(math.ceil(min_x / x_step), math.ceil(max_x / x_step)):
x = (i * x_step - min_x) / kx + left
ctx.set_source_rgb(*(color_x_line_2))
ctx.move_to(x, 0)
ctx.line_to(x, height - bottom)
ctx.stroke()