mirror of
https://github.com/Kozea/WeasyPrint.git
synced 2024-10-04 07:57:52 +03:00
Fix justification, letter-spacing and word-spacing
This commit is contained in:
parent
66c09acb76
commit
a90718ad50
@ -116,3 +116,115 @@ def test_text_align_rtl_trailing_whitespace():
|
||||
<p style="direction: ltr"> abc </p>
|
||||
<p style="direction: ltr"> ‏abc </p>
|
||||
''')
|
||||
|
||||
|
||||
def test_text_align_right():
|
||||
assert_pixels('text_align_right', 9, 6, '''
|
||||
_________
|
||||
__RR__RR_
|
||||
__RR__RR_
|
||||
______RR_
|
||||
______RR_
|
||||
_________
|
||||
''', '''
|
||||
<style>
|
||||
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
|
||||
@page {
|
||||
size: 9px 6px;
|
||||
background: white;
|
||||
}
|
||||
body {
|
||||
color: red;
|
||||
font-family: weasyprint;
|
||||
font-size: 2px;
|
||||
}
|
||||
div {
|
||||
line-height: 1;
|
||||
margin: 1px;
|
||||
text-align: right;
|
||||
}
|
||||
</style>
|
||||
<div>a c e</div>''')
|
||||
|
||||
|
||||
def test_text_align_justify():
|
||||
assert_pixels('text_align_justify', 9, 6, '''
|
||||
_________
|
||||
_RR___RR_
|
||||
_RR___RR_
|
||||
_RR______
|
||||
_RR______
|
||||
_________
|
||||
''', '''
|
||||
<style>
|
||||
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
|
||||
@page {
|
||||
size: 9px 6px;
|
||||
background: white;
|
||||
}
|
||||
body {
|
||||
color: red;
|
||||
font-family: weasyprint;
|
||||
font-size: 2px;
|
||||
}
|
||||
div {
|
||||
line-height: 1;
|
||||
margin: 1px;
|
||||
text-align: justify;
|
||||
}
|
||||
</style>
|
||||
<div>a c e</div>''')
|
||||
|
||||
|
||||
def test_text_word_spacing():
|
||||
assert_pixels('text_word_spacing', 19, 4, '''
|
||||
___________________
|
||||
_RR____RR____RR____
|
||||
_RR____RR____RR____
|
||||
___________________
|
||||
''', '''
|
||||
<style>
|
||||
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
|
||||
@page {
|
||||
size: 19px 4px;
|
||||
background: white;
|
||||
}
|
||||
body {
|
||||
color: red;
|
||||
font-family: weasyprint;
|
||||
font-size: 2px;
|
||||
}
|
||||
div {
|
||||
line-height: 1;
|
||||
margin: 1px;
|
||||
word-spacing: 1em;
|
||||
}
|
||||
</style>
|
||||
<div>a c e</div>''')
|
||||
|
||||
|
||||
def test_text_letter_spacing():
|
||||
assert_pixels('text_letter_spacing', 19, 4, '''
|
||||
___________________
|
||||
_RR____RR____RR____
|
||||
_RR____RR____RR____
|
||||
___________________
|
||||
''', '''
|
||||
<style>
|
||||
@font-face {src: url(weasyprint.otf); font-family: weasyprint}
|
||||
@page {
|
||||
size: 19px 4px;
|
||||
background: white;
|
||||
}
|
||||
body {
|
||||
color: red;
|
||||
font-family: weasyprint;
|
||||
font-size: 2px;
|
||||
}
|
||||
div {
|
||||
line-height: 1;
|
||||
margin: 1px;
|
||||
letter-spacing: 2em;
|
||||
}
|
||||
</style>
|
||||
<div>ace</div>''')
|
||||
|
@ -848,7 +848,7 @@ class Layout:
|
||||
attr.start_index, attr.end_index = start, end
|
||||
pango.pango_attr_list_change(attr_list, attr)
|
||||
|
||||
add_attr(0, len(bytestring) + 1, letter_spacing)
|
||||
add_attr(0, len(bytestring), letter_spacing)
|
||||
position = bytestring.find(b' ')
|
||||
while position != -1:
|
||||
add_attr(position, position + 1, space_spacing)
|
||||
@ -1370,6 +1370,10 @@ def show_first_line(context, textbox, text_overflow, x, y):
|
||||
glyph = glyphs[i].glyph
|
||||
width = glyphs[i].geometry.width
|
||||
utf8_position = utf8_positions[i]
|
||||
|
||||
offset = glyphs[i].geometry.x_offset / font_size
|
||||
if offset:
|
||||
string += f'>{-offset}<'
|
||||
string += f'{glyph:04x}'
|
||||
|
||||
# Ink bounding box and logical widths in font
|
||||
@ -1393,9 +1397,11 @@ def show_first_line(context, textbox, text_overflow, x, y):
|
||||
units_to_double(context.logical_rect.width * 1000) /
|
||||
font_size)
|
||||
|
||||
# Kerning
|
||||
# Kerning, word spacing, letter spacing
|
||||
kerning = int(
|
||||
font.widths[glyph] - units_to_double(width * 1000) / font_size)
|
||||
font.widths[glyph] -
|
||||
units_to_double(width * 1000) / font_size +
|
||||
offset)
|
||||
if kerning:
|
||||
string += f'>{kerning}<'
|
||||
|
||||
@ -1407,8 +1413,9 @@ def show_first_line(context, textbox, text_overflow, x, y):
|
||||
|
||||
# Close the last glyphs list, remove if empty
|
||||
if string[-1] == '<':
|
||||
string = string.rsplit('>', 1)[0]
|
||||
string += '>'
|
||||
string = string[:-1]
|
||||
else:
|
||||
string += '>'
|
||||
|
||||
# Draw text
|
||||
context.show_text(string)
|
||||
|
Loading…
Reference in New Issue
Block a user