From d040b798a0217e6c58ddcb2f5d9256a2325daf1a Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Thu, 11 Jan 2018 01:01:14 +0100 Subject: [PATCH] Handle justify-content --- weasyprint/css/properties.py | 1 + weasyprint/css/validation.py | 8 ++++++++ weasyprint/layout/flex.py | 27 ++++++++++++++++++++------- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/weasyprint/css/properties.py b/weasyprint/css/properties.py index b24c091f..b92a7283 100644 --- a/weasyprint/css/properties.py +++ b/weasyprint/css/properties.py @@ -182,6 +182,7 @@ INITIAL_VALUES = { 'flex_grow': 0, 'flex_shrink': 1, 'flex_wrap': 'nowrap', + 'justify_content': 'flex-start', 'order': 0, # Proprietary diff --git a/weasyprint/css/validation.py b/weasyprint/css/validation.py index 14a513a7..743ce7a3 100644 --- a/weasyprint/css/validation.py +++ b/weasyprint/css/validation.py @@ -1392,6 +1392,14 @@ def flex_wrap(keyword): return keyword in ('nowrap', 'wrap', 'wrap-reverse') +@validator() +@single_keyword +def justify_content(keyword): + """``justify_content`` property validation.""" + return keyword in ( + 'flex-start', 'flex-end', 'center', 'space-between', 'space-around') + + @validator(unstable=True) @single_keyword def image_rendering(keyword): diff --git a/weasyprint/layout/flex.py b/weasyprint/layout/flex.py index d6784340..45922a16 100644 --- a/weasyprint/layout/flex.py +++ b/weasyprint/layout/flex.py @@ -316,7 +316,7 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block, # Step 12 free_space = available_main_space - sum( - child.width for line in flex_lines for child in line) + child.margin_width() for line in flex_lines for child in line) if free_space: margins = 0 @@ -326,20 +326,33 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block, margins += 1 if child.margin_right == 'auto': margins += 1 + if margins: free_space /= margins - else: - free_space = 0 + for line in flex_lines: + for child in line: + if child.margin_left == 'auto': + child.margin_left = free_space + if child.margin_right == 'auto': + child.margin_right = free_space for line in flex_lines: + # TODO: handle rtl position_x = box.content_box_x() + if box.style['justify_content'] == 'flex-end': + position_x += free_space + elif box.style['justify_content'] == 'center': + position_x += free_space / 2 + elif box.style['justify_content'] == 'space-around': + position_x += free_space / len(line) / 2 for child in line: - if child.margin_left == 'auto': - child.margin_left = free_space - if child.margin_right == 'auto': - child.margin_right = free_space child.position_x = position_x position_x += child.margin_width() + if box.style['justify_content'] == 'space-around': + position_x += free_space / len(line) + elif box.style['justify_content'] == 'space-between': + if len(line) > 1: + position_x += free_space / (len(line) - 1) # TODO: align according to justify-content