1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 00:21:15 +03:00

Implement text-align: sub, super, text-top, text-bottom

This commit is contained in:
Simon Sapin 2011-11-24 11:00:33 +01:00
parent 8944c651e0
commit ef711d45fe
4 changed files with 60 additions and 2 deletions

View File

@ -549,6 +549,12 @@ def text_align(computer, name, value):
@Computer.register('vertical_align')
def vertical_align(computer, name, value):
"""Compute the ``word-spacing`` property."""
# Use +/- half an em for super and sub, same as Pango.
# (See the SUPERSUB_RISE constant in pango-markup.c)
if value == 'super':
return computer.get_computed('font_size') * 0.5
if value == 'sub':
return computer.get_computed('font_size') * -0.5
if getattr(value, 'type', 'other') == 'PERCENTAGE':
return computer.get_computed('line_height') * value.value / 100.
return length(computer, name, value)

View File

@ -449,9 +449,10 @@ def vertical_align(value):
if is_dimension_or_percentage(value, negative=True):
return value
keyword = get_keyword(value)
if keyword in ('sub', 'sup', 'text-top', 'text-bottom', 'top', 'bottom'):
if keyword in ('top', 'bottom'):
raise InvalidValues('value not supported yet')
if keyword in ('baseline', 'middle'):
if keyword in ('baseline', 'middle', 'sub', 'super',
'text-top', 'text-bottom'):
return keyword

View File

@ -489,6 +489,16 @@ def inline_box_verticality(box, baseline_y):
one_ex = box.style.font_size * 0.5
top = baseline_y - (one_ex + child.margin_height()) / 2.
child_baseline_y = top + child.baseline
elif vertical_align == 'text-top':
# align top with the top of the parents content area
top = (baseline_y - box.baseline + box.margin_top +
box.border_top_width + box.padding_top)
child_baseline_y = top + child.baseline
elif vertical_align == 'text-bottom':
# align bottom with the bottom of the parents content area
bottom = (baseline_y - box.baseline + box.margin_top +
box.border_top_width + box.padding_top + box.height)
child_baseline_y = bottom - child.margin_height() + child.baseline
else:
# Numeric value: The childs baseline is `vertical_align` above
# (lower y) the parents baseline.

View File

@ -888,6 +888,47 @@ def test_vertical_align():
assert line.height == 77
assert body.height == line.height
# sup and sub currently mean +/- 0.5 em
# With the initial 16px font-size, thats 8px.
page, = parse('''
<span style="line-height: 10px">
<img src="pattern.png" style="width: 60px">
<img src="pattern.png" style="width: 40px; vertical-align: super">
<img src="pattern.png" style="width: 40px; vertical-align: sub">
</span>
''')
html = page.root_box
body, = html.children
line, = body.children
span, = line.children
img_1, img_2, img_3 = span.children
assert img_1.height == 60
assert img_2.height == 40
assert img_3.height == 40
assert img_1.position_y == 0
assert img_2.position_y == 12 # 20 - 16 * 0.5
assert img_3.position_y == 28 # 20 + 16 * 0.5
assert line.height == 68
assert body.height == line.height
# Pango gives a height of 19px for font-size of 16px
page, = parse('''
<span style="line-height: 10px">
<img src="pattern.png" style="vertical-align: text-top">
<img src="pattern.png" style="vertical-align: text-bottom">
</span>
''')
html = page.root_box
body, = html.children
line, = body.children
span, = line.children
img_1, img_2 = span.children
assert img_1.height == 4
assert img_2.height == 4
assert img_1.position_y == 0
assert img_2.position_y == 15 # 19 - 4
assert line.height == 19
assert body.height == line.height
@SUITE.test