1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-11-12 23:23:51 +03:00

Add a presentational-hints option

This commit is contained in:
Guillaume Ayoub 2016-08-30 18:15:30 +02:00
parent e7b99e7bea
commit b2a5d7a98f
4 changed files with 34 additions and 19 deletions

View File

@ -110,7 +110,8 @@ class HTML(object):
def _get_metadata(self):
return get_html_metadata(self.root_element)
def render(self, stylesheets=None, enable_hinting=False):
def render(self, stylesheets=None, enable_hinting=False,
presentational_hints=False):
"""Lay out and paginate the document, but do not (yet) export it
to PDF or another format.
@ -129,10 +130,14 @@ class HTML(object):
Whether text, borders and background should be *hinted* to fall
at device pixel boundaries. Should be enabled for pixel-based
output (like PNG) but not vector based output (like PDF).
:type presentational_hints: bool
:param presentational_hints: Whether HTML presentational hints are
followed.
:returns: A :class:`~document.Document` object.
"""
return Document._render(self, stylesheets, enable_hinting)
return Document._render(
self, stylesheets, enable_hinting, presentational_hints)
def write_pdf(self, target=None, stylesheets=None, zoom=1,
attachments=None):

View File

@ -66,6 +66,10 @@ def main(argv=None, stdout=None, stdin=None):
Adds an attachment to the document which is included in the PDF output.
This option can be added multiple times to attach more files.
.. option:: -p, --presentational-hints
Follow HTML presentational hints.
.. option:: --version
Show the version number. Other options and arguments are ignored.
@ -100,6 +104,8 @@ def main(argv=None, stdout=None, stdin=None):
parser.add_argument('-a', '--attachment', action='append',
help='URL or filename of a file '
'to attach to the PDF document')
parser.add_argument('-p', '--presentational-hints', action='store_true',
help='Follow HTML presentational hints.')
parser.add_argument(
'input', help='URL or filename of the HTML input, or - for stdin')
parser.add_argument(
@ -152,7 +158,8 @@ def main(argv=None, stdout=None, stdin=None):
parser.error('--attachment only applies for the PDF format.')
html = HTML(source, base_url=args.base_url, encoding=args.encoding,
media_type=args.media_type)
media_type=args.media_type,
presentational_hints=args.presentational_hints)
getattr(html, 'write_' + format_)(output, **kwargs)

View File

@ -798,9 +798,9 @@ def preprocess_stylesheet(device_media_type, base_url, rules, url_fetcher):
yield margin_rule, selector_list, declarations
def get_all_computed_styles(html, user_stylesheets=None):
"""Compute all the computed styles of all elements
in the given ``html`` document.
def get_all_computed_styles(html, user_stylesheets=None,
presentational_hints=False):
"""Compute all the computed styles of all elements in ``html`` document.
Do everything from finding author stylesheets to parsing and applying them.
@ -848,14 +848,15 @@ def get_all_computed_styles(html, user_stylesheets=None):
cascaded_styles, name, values, weight,
element, pseudo_type)
specificity = (0, 0, 0, 0)
for element, declarations, base_url in find_presentational_hints(
element_tree):
for name, values, importance in preprocess_declarations(
base_url, declarations):
precedence = declaration_precedence('author', importance)
weight = (precedence, specificity)
add_declaration(cascaded_styles, name, values, weight, element)
if presentational_hints:
specificity = (0, 0, 0, 0)
for element, declarations, base_url in find_presentational_hints(
element_tree):
for name, values, importance in preprocess_declarations(
base_url, declarations):
precedence = declaration_precedence('author', importance)
weight = (precedence, specificity)
add_declaration(cascaded_styles, name, values, weight, element)
specificity = (1, 0, 0, 0)
for element, declarations, base_url in find_style_attributes(element_tree):

View File

@ -313,11 +313,13 @@ class Document(object):
"""
@classmethod
def _render(cls, html, stylesheets, enable_hinting):
style_for = get_all_computed_styles(html, user_stylesheets=[
css if hasattr(css, 'rules')
else CSS(guess=css, media_type=html.media_type)
for css in stylesheets or []])
def _render(cls, html, stylesheets, enable_hinting,
presentational_hints=False):
style_for = get_all_computed_styles(
html, presentational_hints=presentational_hints, user_stylesheets=[
css if hasattr(css, 'rules')
else CSS(guess=css, media_type=html.media_type)
for css in stylesheets or []])
get_image_from_uri = functools.partial(
images.get_image_from_uri, {}, html.url_fetcher)
page_boxes = layout_document(