Skip to content

Commit

Permalink
Merge pull request #907 from makeleaps/nowrap
Browse files Browse the repository at this point in the history
Refactor LayoutContext building
  • Loading branch information
liZe committed Jul 23, 2019
2 parents 135dc06 + afc7dab commit 328de5e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
33 changes: 26 additions & 7 deletions weasyprint/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import warnings

import cairocffi as cairo
from weasyprint.layout import LayoutContext

from . import CSS
from .css import get_all_computed_styles
Expand Down Expand Up @@ -347,9 +348,11 @@ class Document(object):
<weasyprint.fonts.FontConfiguration>`.
"""

@classmethod
def _render(cls, html, stylesheets, enable_hinting,
presentational_hints=False, font_config=None):
def _build_layout_context(cls, html, stylesheets, enable_hinting,
presentational_hints=False,
font_config=None):
if font_config is None:
font_config = FontConfiguration()
target_collector = TargetCollector()
Expand All @@ -367,12 +370,28 @@ def _render(cls, html, stylesheets, enable_hinting,
get_image_from_uri = functools.partial(
original_get_image_from_uri, {}, html.url_fetcher)
PROGRESS_LOGGER.info('Step 4 - Creating formatting structure')
context = LayoutContext(
enable_hinting, style_for, get_image_from_uri, font_config, target_collector
)
return context

@classmethod
def _render(cls, html, stylesheets, enable_hinting,
presentational_hints=False, font_config=None):
if font_config is None:
font_config = FontConfiguration()

context = cls._build_layout_context(
html, stylesheets, enable_hinting, presentational_hints, font_config
)

root_box = build_formatting_structure(
html.etree_element, style_for, get_image_from_uri,
html.base_url, target_collector)
page_boxes = layout_document(
enable_hinting, style_for, get_image_from_uri, root_box,
font_config, html, target_collector)
html.etree_element,
context.style_for, context.get_image_from_uri,
html.base_url, context.target_collector
)

page_boxes = layout_document(html, root_box, context)
rendering = cls(
[Page(page_box, enable_hinting) for page_box in page_boxes],
DocumentMetadata(**html._get_metadata()),
Expand Down
10 changes: 3 additions & 7 deletions weasyprint/layout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def layout_fixed_boxes(context, pages, containing_page):
absolute_boxes = new_absolute_boxes


def layout_document(enable_hinting, style_for, get_image_from_uri, root_box,
font_config, html, target_collector, max_loops=8):
def layout_document(html, root_box, context, max_loops=8):
"""Lay out the whole document.
This includes line breaks, page breaks, absolute size and position for all
Expand All @@ -114,9 +113,6 @@ def layout_document(enable_hinting, style_for, get_image_from_uri, root_box,
:returns: a list of laid out Page objects.
"""
context = LayoutContext(
enable_hinting, style_for, get_image_from_uri, font_config,
target_collector)
initialize_page_maker(context, root_box)
pages = []
actual_total_pages = 0
Expand All @@ -127,7 +123,7 @@ def layout_document(enable_hinting, style_for, get_image_from_uri, root_box,
'Step 5 - Creating layout - Repagination #%i' % loop)

initial_total_pages = actual_total_pages
pages = list(make_all_pages(context, root_box, html, pages, style_for))
pages = list(make_all_pages(context, root_box, html, pages))
actual_total_pages = len(pages)

# Check whether another round is required
Expand Down Expand Up @@ -185,7 +181,7 @@ def layout_document(enable_hinting, style_for, get_image_from_uri, root_box,
state = context.page_maker[context.current_page][3]
page.children = (root,) + tuple(
make_margin_boxes(context, page, state))
layout_backgrounds(page, get_image_from_uri)
layout_backgrounds(page, context.get_image_from_uri)
yield page


Expand Down
9 changes: 5 additions & 4 deletions weasyprint/layout/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def set_page_type_computed_styles(page_type, html, style_for):
base_url=html.base_url)


def remake_page(index, context, root_box, html, style_for):
def remake_page(index, context, root_box, html):
"""Return one laid out page without margin boxes.
Start with the initial values from ``context.page_maker[index]``.
Expand Down Expand Up @@ -730,7 +730,7 @@ def remake_page(index, context, root_box, html, style_for):
next_page_name = ''
side = 'right' if right_page else 'left'
page_type = PageType(side, blank, first, index, name=next_page_name)
set_page_type_computed_styles(page_type, html, style_for)
set_page_type_computed_styles(page_type, html, context.style_for)

context.forced_break = (
initial_next_page['break'] != 'any' or initial_next_page['page'])
Expand Down Expand Up @@ -783,7 +783,7 @@ def remake_page(index, context, root_box, html, style_for):
return page, resume_at


def make_all_pages(context, root_box, html, pages, style_for):
def make_all_pages(context, root_box, html, pages):
"""Return a list of laid out pages without margin boxes.
Re-make pages only if necessary.
Expand All @@ -802,7 +802,8 @@ def make_all_pages(context, root_box, html, pages, style_for):
remake_state['anchors'] = []
remake_state['content_lookups'] = []
page, resume_at = remake_page(
i, context, root_box, html, style_for)
i, context, root_box, html
)
yield page
else:
PROGRESS_LOGGER.info(
Expand Down

0 comments on commit 328de5e

Please sign in to comment.