Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drawn underline and overline behind text #1874

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions tests/draw/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,9 @@ def test_text_underline(assert_pixels):
assert_pixels('''
_____________
_zzzzzzzzzzz_
_zRRRRRRRRRz_
_zRRRRRRRRRz_
_zBBBBBBBBBz_
_zsssssssssz_
_zsssssssssz_
_zuuuuuuuuuz_
_zzzzzzzzzzz_
_____________
''', '''
Expand All @@ -533,7 +533,7 @@ def test_text_underline(assert_pixels):
margin: 2px;
}
body {
color: red;
color: rgba(255, 0, 0, 0.5);
font-family: weasyprint;
font-size: 3px;
text-decoration: underline blue;
Expand Down Expand Up @@ -601,9 +601,9 @@ def test_text_multiple_text_decoration(assert_pixels):
assert_pixels('''
_____________
_zzzzzzzzzzz_
_zRRRRRRRRRz_
_zBBBBBBBBBz_
_zsssssssssz_
_zBBBBBBBBBz_
_zuuuuuuuuuz_
_zzzzzzzzzzz_
_____________
''', '''
Expand All @@ -614,7 +614,7 @@ def test_text_multiple_text_decoration(assert_pixels):
margin: 2px;
}
body {
color: red;
color: rgba(255, 0, 0, 0.5);
font-family: weasyprint;
font-size: 3px;
text-decoration: underline line-through blue;
Expand All @@ -628,9 +628,9 @@ def test_text_nested_text_decoration(assert_pixels):
assert_pixels('''
_____________
_zzzzzzzzzzz_
_zRRRRRRRRRz_
_zRRRBBBRRRz_
_zBBBBBBBBBz_
_zsssssssssz_
_zsssBBBsssz_
_zuuuuuuuuuz_
_zzzzzzzzzzz_
_____________
''', '''
Expand All @@ -641,7 +641,7 @@ def test_text_nested_text_decoration(assert_pixels):
margin: 2px;
}
body {
color: red;
color: rgba(255, 0, 0, 0.5);
font-family: weasyprint;
font-size: 3px;
text-decoration: underline blue;
Expand Down
43 changes: 23 additions & 20 deletions weasyprint/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,26 @@ def draw_text(stream, textbox, offset_x, text_overflow, block_ellipsis):
if textbox.style['visibility'] != 'visible':
return

text_decoration_values = textbox.style['text_decoration_line']
text_decoration_color = textbox.style['text_decoration_color']
if text_decoration_color == 'currentColor':
text_decoration_color = textbox.style['color']
if 'overline' in text_decoration_values:
thickness = textbox.pango_layout.underline_thickness
offset_y = (
textbox.baseline - textbox.pango_layout.ascent + thickness / 2)
draw_text_decoration(
stream, textbox, offset_x, offset_y, thickness,
text_decoration_color)
if 'underline' in text_decoration_values:
thickness = textbox.pango_layout.underline_thickness
offset_y = (
textbox.baseline - textbox.pango_layout.underline_position +
thickness / 2)
draw_text_decoration(
stream, textbox, offset_x, offset_y, thickness,
text_decoration_color)

x, y = textbox.position_x, textbox.position_y + textbox.baseline
stream.set_color_rgb(*textbox.style['color'][:3])
stream.set_alpha(textbox.style['color'][3])
Expand All @@ -1013,30 +1033,13 @@ def draw_text(stream, textbox, offset_x, text_overflow, block_ellipsis):

draw_emojis(stream, textbox.style['font_size'], x, y, emojis)

# Draw text decoration
values = textbox.style['text_decoration_line']
color = textbox.style['text_decoration_color']
if color == 'currentColor':
color = textbox.style['color']
if 'overline' in values:
thickness = textbox.pango_layout.underline_thickness
offset_y = (
textbox.baseline - textbox.pango_layout.ascent + thickness / 2)
draw_text_decoration(
stream, textbox, offset_x, offset_y, thickness, color)
if 'underline' in values:
thickness = textbox.pango_layout.underline_thickness
offset_y = (
textbox.baseline - textbox.pango_layout.underline_position +
thickness / 2)
draw_text_decoration(
stream, textbox, offset_x, offset_y, thickness, color)
if 'line-through' in values:
if 'line-through' in text_decoration_values:
thickness = textbox.pango_layout.strikethrough_thickness
offset_y = (
textbox.baseline - textbox.pango_layout.strikethrough_position)
draw_text_decoration(
stream, textbox, offset_x, offset_y, thickness, color)
stream, textbox, offset_x, offset_y, thickness,
text_decoration_color)

textbox.pango_layout.deactivate()

Expand Down