From d001a23ead415ad5b622315d5ae8a718a426b5c7 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Tue, 25 Jul 2023 14:46:18 +0200 Subject: [PATCH] Give matrix instead of separated values to draw_first_line --- weasyprint/draw.py | 13 +++---------- weasyprint/svg/text.py | 10 +++++++--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/weasyprint/draw.py b/weasyprint/draw.py index b34ad61de..b82a4f3c7 100644 --- a/weasyprint/draw.py +++ b/weasyprint/draw.py @@ -4,7 +4,7 @@ import operator from colorsys import hsv_to_rgb, rgb_to_hsv from io import BytesIO -from math import ceil, cos, floor, pi, sin, sqrt, tan +from math import ceil, floor, pi, sqrt, tan from xml.etree import ElementTree from PIL import Image @@ -1073,7 +1073,7 @@ def draw_text(stream, textbox, offset_x, text_overflow, block_ellipsis): textbox.pango_layout.reactivate(textbox.style) stream.begin_text() emojis = draw_first_line( - stream, textbox, text_overflow, block_ellipsis, x, y) + stream, textbox, text_overflow, block_ellipsis, Matrix(d=-1, e=x, f=y)) stream.end_text() draw_emojis(stream, textbox.style['font_size'], x, y, emojis) @@ -1097,8 +1097,7 @@ def draw_emojis(stream, font_size, x, y, emojis): stream.pop_state() -def draw_first_line(stream, textbox, text_overflow, block_ellipsis, x, y, - angle=0, scale_x=1): +def draw_first_line(stream, textbox, text_overflow, block_ellipsis, matrix): """Draw the given ``textbox`` line to the document ``stream``.""" # Don’t draw lines with only invisible characters if not textbox.text.strip(): @@ -1152,12 +1151,6 @@ def draw_first_line(stream, textbox, text_overflow, block_ellipsis, x, y, utf8_text = textbox.pango_layout.text.encode() previous_utf8_position = 0 - - matrix = Matrix(scale_x, 0, 0, -1, x, y) - if angle: - a, c = cos(angle), sin(angle) - b, d = -c, a - matrix = Matrix(a, b, c, d) @ matrix stream.text_matrix(*matrix.values) last_font = None string = '' diff --git a/weasyprint/svg/text.py b/weasyprint/svg/text.py index 02862ac86..2af46a8f0 100644 --- a/weasyprint/svg/text.py +++ b/weasyprint/svg/text.py @@ -1,7 +1,8 @@ """Draw text.""" -from math import inf, radians +from math import cos, inf, radians, sin +from ..matrix import Matrix from .bounding_box import EMPTY_BOUNDING_BOX, extend_bounding_box from .utils import normalize, size @@ -187,9 +188,12 @@ def text(svg, node, font_size): layout.reactivate(style) svg.fill_stroke(node, font_size, text=True) + matrix = Matrix(a=scale_x, d=-1, e=x_position, f=y_position) + if angle: + a, c = cos(angle), sin(angle) + matrix = Matrix(a, -c, c, a) @ matrix emojis = draw_first_line( - svg.stream, TextBox(layout, style), 'none', 'none', - x_position, y_position, angle, scale_x) + svg.stream, TextBox(layout, style), 'none', 'none', matrix) emoji_lines.append((font_size, x, y, emojis)) svg.cursor_position = cursor_position