Skip to content

Commit 1460522

Browse files
committed
Improve SVG text-anchor attribute
1 parent 8bccb7f commit 1460522

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

weasyprint/svg/__init__.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from cssselect2 import ElementWrapper
99

1010
from ..urls import get_url_attribute
11-
from .bounding_box import bounding_box, is_valid_bounding_box
11+
from .bounding_box import (
12+
bounding_box, extend_bounding_box, is_valid_bounding_box)
1213
from .css import parse_declarations, parse_stylesheets
1314
from .defs import (
1415
apply_filters, clip_path, draw_gradient_or_pattern, paint_mask, use)
@@ -433,6 +434,12 @@ def draw_node(self, node, font_size, fill_stroke=True):
433434
display = node.get('display') != 'none'
434435
visible = display and (node.get('visibility') != 'hidden')
435436

437+
# Handle text anchor
438+
text_anchor = node.get('text-anchor')
439+
if node.tag == 'text' and text_anchor in ('middle', 'end'):
440+
group = self.stream.add_group(0, 0, 0, 0) # BBox set after drawing
441+
original_stream, self.stream = self.stream, group
442+
436443
# Draw node
437444
if visible and node.tag in TAGS:
438445
with suppress(PointError):
@@ -442,6 +449,27 @@ def draw_node(self, node, font_size, fill_stroke=True):
442449
if display and node.tag not in DEF_TYPES:
443450
for child in node:
444451
self.draw_node(child, font_size, fill_stroke)
452+
if node.tag in ('text', 'tspan'):
453+
if not is_valid_bounding_box(child.text_bounding_box):
454+
continue
455+
x1, y1 = child.text_bounding_box[:2]
456+
x2 = x1 + child.text_bounding_box[2]
457+
y2 = y1 + child.text_bounding_box[3]
458+
node.text_bounding_box = extend_bounding_box(
459+
node.text_bounding_box, ((x1, y1), (x2, y2)))
460+
461+
# Handle text anchor
462+
if node.tag == 'text' and text_anchor in ('middle', 'end'):
463+
group_id = self.stream.id
464+
self.stream = original_stream
465+
self.stream.push_state()
466+
if is_valid_bounding_box(node.text_bounding_box):
467+
x, y, width, height = node.text_bounding_box
468+
group.extra['BBox'][:] = (x, y, x + width, y + height)
469+
x_align = width / 2 if text_anchor == 'middle' else width
470+
self.stream.transform(e=-x_align)
471+
self.stream.draw_x_object(group_id)
472+
self.stream.pop_state()
445473

446474
# Apply mask
447475
mask = self.masks.get(parse_url(node.get('mask')).fragment)

weasyprint/svg/text.py

+7-23
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ def text(svg, node, font_size):
4444

4545
layout, _, _, width, height, _ = split_first_line(
4646
node.text, style, svg.context, inf, 0)
47-
# TODO: get real values
48-
x_bearing, y_bearing = 0, 0
4947

5048
# Get rotations and translations
5149
x, y, dx, dy, rotate = [], [], [], [], [0]
@@ -89,19 +87,8 @@ def text(svg, node, font_size):
8987
letter_spacing = (text_length - width) / spaces_count
9088
width = text_length
9189

92-
# Align text box horizontally
93-
x_align = 0
94-
text_anchor = node.get('text-anchor')
9590
# TODO: use real values
9691
ascent, descent = font_size * .8, font_size * .2
97-
if text_anchor == 'middle':
98-
x_align = - (width / 2 + x_bearing)
99-
if letter_spacing and node.text:
100-
x_align -= (len(node.text) - 1) * letter_spacing / 2
101-
elif text_anchor == 'end':
102-
x_align = - (width + x_bearing)
103-
if letter_spacing and node.text:
104-
x_align -= (len(node.text) - 1) * letter_spacing
10592

10693
# Align text box vertically
10794
# TODO: This is a hack. Other baseline alignment tags are not supported.
@@ -111,11 +98,11 @@ def text(svg, node, font_size):
11198
alignment_baseline = node.get(
11299
'dominant-baseline', node.get('alignment-baseline'))
113100
if display_anchor == 'middle':
114-
y_align = -height / 2 - y_bearing
101+
y_align = -height / 2
115102
elif display_anchor == 'top':
116-
y_align = -y_bearing
103+
pass
117104
elif display_anchor == 'bottom':
118-
y_align = -height - y_bearing
105+
y_align = -height
119106
elif alignment_baseline in ('central', 'middle'):
120107
# TODO: This is wrong, we use font top-to-bottom
121108
y_align = (ascent + descent) / 2 - descent
@@ -157,16 +144,14 @@ def text(svg, node, font_size):
157144
width *= scale_x
158145
if i:
159146
x += letter_spacing
147+
svg.cursor_position = x + width, y
160148

161-
x_position = x + svg.cursor_d_position[0] + x_align
149+
x_position = x + svg.cursor_d_position[0]
162150
y_position = y + svg.cursor_d_position[1] + y_align
163-
cursor_position = x + width, y
164151
angle = last_r if r is None else r
165152
points = (
166-
(cursor_position[0] + x_align + svg.cursor_d_position[0],
167-
cursor_position[1] + y_align + svg.cursor_d_position[1]),
168-
(cursor_position[0] + x_align + width + svg.cursor_d_position[0],
169-
cursor_position[1] + y_align + height + svg.cursor_d_position[1]))
153+
(x_position, y_position),
154+
(x_position + width, y_position - height))
170155
node.text_bounding_box = extend_bounding_box(
171156
node.text_bounding_box, points)
172157

@@ -179,7 +164,6 @@ def text(svg, node, font_size):
179164
emojis = draw_first_line(
180165
svg.stream, TextBox(layout, style), 'none', 'none', matrix)
181166
emoji_lines.append((font_size, x, y, emojis))
182-
svg.cursor_position = cursor_position
183167

184168
svg.stream.end_text()
185169
svg.stream.pop_state()

0 commit comments

Comments
 (0)