From 638b70e3e38ed8cca9a8a3c8d4f4a698d33a10b1 Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Wed, 21 Oct 2020 21:03:01 +0200 Subject: [PATCH] Deduplicate flex logic for rtl Related to #1225. --- weasyprint/layout/flex.py | 83 ++++++++++++++------------------------- 1 file changed, 30 insertions(+), 53 deletions(-) diff --git a/weasyprint/layout/flex.py b/weasyprint/layout/flex.py index 7a6579321..09648884a 100644 --- a/weasyprint/layout/flex.py +++ b/weasyprint/layout/flex.py @@ -495,7 +495,7 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block, not_collected_items = [] for i, child in line: align_self = child.style['align_self'] - if (box.style['flex_direction'].startswith('row') and + if (axis == 'width' and align_self == 'baseline' and child.margin_top != 'auto' and child.margin_bottom != 'auto'): @@ -649,62 +649,39 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block, child.margin_bottom = free_space free_space = 0 - if (box.style['direction'] == 'rtl' and - box.style['flex_direction'].startswith('row')): - if justify_content == 'flex-end': - position_axis -= free_space - elif justify_content == 'center': - position_axis -= free_space / 2 - elif justify_content == 'space-around': - position_axis -= free_space / len(line) / 2 - elif justify_content == 'space-evenly': - position_axis -= free_space / (len(line) - 1) + if box.style['direction'] == 'rtl' and axis == 'width': + free_space *= -1 - for i, child in line: - if axis == 'width': - child.position_x = position_axis - if justify_content == 'stretch': - child.width -= free_space / len(line) - else: - child.position_y = position_axis - position_axis -= ( - child.margin_width() if axis == 'width' - else child.margin_height()) - if justify_content == 'space-around': - position_axis -= free_space / len(line) - elif justify_content == 'space-between': - if len(line) > 1: - position_axis -= free_space / (len(line) + 1) - elif justify_content == 'space-evenly': - position_axis -= free_space / (len(line) - 1) - else: - if justify_content == 'flex-end': - position_axis += free_space - elif justify_content == 'center': - position_axis += free_space / 2 - elif justify_content == 'space-around': - position_axis += free_space / len(line) / 2 + if justify_content == 'flex-end': + position_axis += free_space + elif justify_content == 'center': + position_axis += free_space / 2 + elif justify_content == 'space-around': + position_axis += free_space / len(line) / 2 + elif justify_content == 'space-evenly': + position_axis += free_space / (len(line) + 1) + + for i, child in line: + if axis == 'width': + child.position_x = position_axis + if justify_content == 'stretch': + child.width += free_space / len(line) + else: + child.position_y = position_axis + margin_axis = ( + child.margin_width() if axis == 'width' + else child.margin_height()) + if box.style['direction'] == 'rtl' and axis == 'width': + margin_axis *= -1 + position_axis += margin_axis + if justify_content == 'space-around': + position_axis += free_space / len(line) + elif justify_content == 'space-between': + if len(line) > 1: + position_axis += free_space / (len(line) - 1) elif justify_content == 'space-evenly': position_axis += free_space / (len(line) + 1) - for i, child in line: - if axis == 'width': - child.position_x = position_axis - if justify_content == 'stretch': - child.width += free_space / len(line) - else: - child.position_y = position_axis - position_axis += ( - child.margin_width() if axis == 'width' - else child.margin_height()) - if justify_content == 'space-around': - position_axis += free_space / len(line) - elif justify_content == 'space-between': - if len(line) > 1: - position_axis += free_space / (len(line) - 1) - elif justify_content == 'space-evenly': - position_axis += free_space / (len(line) + 1) - # Step 13 position_cross = ( box.content_box_y() if cross == 'height'