2011-05-25 17:54:46 +04:00
|
|
|
# coding: utf8
|
|
|
|
|
|
|
|
# WeasyPrint converts web documents (HTML, CSS, ...) to PDF.
|
|
|
|
# Copyright (C) 2011 Simon Sapin
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
2011-08-25 14:48:00 +04:00
|
|
|
Specific handling for some HTML elements, especially replaced elements.
|
2011-08-19 18:52:46 +04:00
|
|
|
|
2011-05-25 17:54:46 +04:00
|
|
|
Replaced elements (eg. <img> elements) are rendered externally and behave
|
|
|
|
as an atomic opaque box in CSS. They may or may not have intrinsic dimensions.
|
2011-08-19 18:52:46 +04:00
|
|
|
|
2011-05-25 17:54:46 +04:00
|
|
|
"""
|
|
|
|
|
2011-08-09 12:15:53 +04:00
|
|
|
from __future__ import division
|
2011-08-10 16:51:18 +04:00
|
|
|
|
2011-08-09 12:15:53 +04:00
|
|
|
import cairo
|
2011-08-10 16:51:18 +04:00
|
|
|
|
2011-08-25 14:48:00 +04:00
|
|
|
from ..css.values import get_single_keyword, make_keyword
|
|
|
|
from ..formatting_structure import boxes
|
|
|
|
from ..utils import get_url_attribute
|
2011-08-10 16:51:18 +04:00
|
|
|
|
2011-05-25 17:54:46 +04:00
|
|
|
|
2011-08-25 14:48:00 +04:00
|
|
|
# Maps HTML tag names to function taking an HTML element and returning a Box.
|
|
|
|
HTML_HANDLERS = {}
|
2011-08-20 20:02:04 +04:00
|
|
|
|
2011-08-25 19:29:16 +04:00
|
|
|
# Marker saying that handle_element() has no special handling for this element
|
|
|
|
DEFAULT_HANDLING = object()
|
|
|
|
|
2011-08-20 20:02:04 +04:00
|
|
|
|
2011-08-25 14:48:00 +04:00
|
|
|
def handle_element(document, element):
|
2011-08-25 19:29:16 +04:00
|
|
|
"""Handle HTML elements that need special care.
|
|
|
|
|
|
|
|
:returns: the :obj:`DEFAULT_HANDLING` constant if there is no special
|
|
|
|
handling for this element, a :class:`Box` built with the special
|
|
|
|
handling or, None if the element should be ignored.
|
|
|
|
"""
|
2011-08-25 14:48:00 +04:00
|
|
|
if element.tag in HTML_HANDLERS:
|
|
|
|
handler = HTML_HANDLERS[element.tag]
|
|
|
|
return handler(document, element)
|
2011-08-25 19:29:16 +04:00
|
|
|
else:
|
|
|
|
return DEFAULT_HANDLING
|
2011-08-20 20:02:04 +04:00
|
|
|
|
|
|
|
|
2011-08-25 14:48:00 +04:00
|
|
|
def handler(tag):
|
2011-08-22 19:55:30 +04:00
|
|
|
"""
|
2011-08-25 14:48:00 +04:00
|
|
|
Return a decorator that registers a function handling `tag` HTML elements.
|
2011-08-22 19:55:30 +04:00
|
|
|
"""
|
2011-08-22 19:50:32 +04:00
|
|
|
def decorator(function):
|
2011-08-25 14:48:00 +04:00
|
|
|
HTML_HANDLERS[tag] = function
|
2011-08-22 19:50:32 +04:00
|
|
|
return function
|
|
|
|
return decorator
|
2011-08-20 20:02:04 +04:00
|
|
|
|
|
|
|
|
2011-08-25 19:29:16 +04:00
|
|
|
def is_block_level(document, element):
|
|
|
|
"""
|
|
|
|
Return True if the element is block-level, False if it is inline-level,
|
|
|
|
and raise ValueError if it is neither.
|
|
|
|
"""
|
2011-08-25 14:48:00 +04:00
|
|
|
display = get_single_keyword(document.style_for(element).display)
|
|
|
|
|
|
|
|
if display in ('block', 'list-item', 'table'):
|
2011-08-25 19:29:16 +04:00
|
|
|
return True
|
2011-08-25 14:48:00 +04:00
|
|
|
elif display in ('inline', 'inline-table', 'inline-block'):
|
2011-08-25 19:29:16 +04:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
raise ValueError('Unsupported display: ' + display)
|
|
|
|
|
|
|
|
|
|
|
|
def make_replaced_box(document, element, replacement):
|
|
|
|
"""
|
|
|
|
Wrap a :class:`Replacement` object in either replaced box.
|
|
|
|
That box is either block-level or inline-level, depending on what
|
|
|
|
the element should be.
|
|
|
|
"""
|
|
|
|
if is_block_level(document, element):
|
|
|
|
type_ = boxes.BlockLevelReplacedBox
|
2011-08-25 14:48:00 +04:00
|
|
|
else:
|
2011-08-25 19:29:16 +04:00
|
|
|
type_ = boxes.InlineLevelReplacedBox
|
2011-08-25 14:48:00 +04:00
|
|
|
return type_(document, element, replacement)
|
|
|
|
|
|
|
|
|
2011-08-25 19:29:16 +04:00
|
|
|
def make_text_box(document, element, text):
|
|
|
|
"""
|
|
|
|
Make a text box and, if the element should be block-level, wrap it in
|
|
|
|
a block box.
|
|
|
|
"""
|
|
|
|
text_box = boxes.TextBox(document, element, text)
|
|
|
|
if is_block_level(document, element):
|
|
|
|
block = boxes.BlockBox(document, element)
|
|
|
|
block.add_child(text_box)
|
|
|
|
return block
|
|
|
|
else:
|
|
|
|
return text_box
|
|
|
|
|
|
|
|
|
2011-08-25 14:48:00 +04:00
|
|
|
@handler('img')
|
|
|
|
def handle_img(document, element):
|
2011-08-22 19:55:30 +04:00
|
|
|
"""
|
|
|
|
Handle <img> tags: return either an image or the alt-text.
|
2011-08-25 19:29:16 +04:00
|
|
|
|
|
|
|
http://www.w3.org/TR/html5/embedded-content-1.html#the-img-element
|
2011-08-22 19:55:30 +04:00
|
|
|
"""
|
2011-08-22 19:50:32 +04:00
|
|
|
src = get_url_attribute(element, 'src')
|
2011-08-25 19:29:16 +04:00
|
|
|
alt = element.get('alt')
|
|
|
|
if src:
|
2011-08-26 00:16:04 +04:00
|
|
|
surface = document.get_image_surface_from_uri(src)
|
|
|
|
if surface is not None:
|
|
|
|
replacement = ImageReplacement(surface)
|
|
|
|
return make_replaced_box(document, element, replacement)
|
|
|
|
else:
|
2011-08-25 19:29:16 +04:00
|
|
|
# Invalid image, use the alt-text.
|
|
|
|
if alt:
|
|
|
|
return make_text_box(document, element, alt)
|
|
|
|
elif alt == '':
|
|
|
|
# The element represents nothing
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
assert alt is None
|
|
|
|
# TODO: find some indicator that an image is missing.
|
|
|
|
# For now, just remove the image.
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
if alt:
|
|
|
|
return make_text_box(document, element, alt)
|
|
|
|
else:
|
|
|
|
return None
|
2011-05-25 17:54:46 +04:00
|
|
|
|
|
|
|
|
2011-08-25 14:50:23 +04:00
|
|
|
@handler('br')
|
|
|
|
def handle_br(document, element):
|
|
|
|
"""
|
|
|
|
Handle <br> tags: return a preserved new-line character.
|
|
|
|
"""
|
|
|
|
box = boxes.TextBox(document, element, '\n')
|
|
|
|
box.style.white_space = [make_keyword('pre')]
|
|
|
|
return box
|
|
|
|
|
|
|
|
|
2011-05-25 17:54:46 +04:00
|
|
|
class Replacement(object):
|
2011-08-19 18:52:46 +04:00
|
|
|
"""Abstract base class for replaced elements. """
|
2011-08-09 12:15:53 +04:00
|
|
|
def intrinsic_width(self):
|
2011-08-19 18:52:46 +04:00
|
|
|
"""Intrinsic width if defined."""
|
2011-08-09 12:15:53 +04:00
|
|
|
|
|
|
|
def intrinsic_height(self):
|
2011-08-19 18:52:46 +04:00
|
|
|
"""Intrinsic height if defined."""
|
2011-08-09 12:15:53 +04:00
|
|
|
|
|
|
|
def intrinsic_ratio(self):
|
2011-08-19 18:52:46 +04:00
|
|
|
"""Intrinsic ratio if defined."""
|
2011-08-09 12:15:53 +04:00
|
|
|
if (self.intrinsic_width() is not None and
|
2011-08-19 18:52:46 +04:00
|
|
|
self.intrinsic_width() != 0 and
|
|
|
|
self.intrinsic_height() is not None and
|
|
|
|
self.intrinsic_height() != 0):
|
2011-08-09 12:15:53 +04:00
|
|
|
return self.intrinsic_width() / self.intrinsic_height()
|
2011-05-25 17:54:46 +04:00
|
|
|
|
2011-08-19 18:52:46 +04:00
|
|
|
|
2011-05-25 17:54:46 +04:00
|
|
|
class ImageReplacement(Replacement):
|
2011-08-20 20:02:04 +04:00
|
|
|
"""Replaced ``<img>`` element.
|
|
|
|
|
2011-08-26 00:16:04 +04:00
|
|
|
:param surface: a cairo :class:`ImageSurface` object.
|
2011-08-20 20:02:04 +04:00
|
|
|
|
|
|
|
"""
|
2011-08-26 00:16:04 +04:00
|
|
|
def __init__(self, surface):
|
|
|
|
self.surface = surface
|
2011-08-09 12:15:53 +04:00
|
|
|
|
|
|
|
def intrinsic_width(self):
|
2011-08-26 17:52:37 +04:00
|
|
|
return self.surface.get_width()
|
2011-08-09 12:15:53 +04:00
|
|
|
|
|
|
|
def intrinsic_height(self):
|
2011-08-26 17:52:37 +04:00
|
|
|
return self.surface.get_height()
|
2011-08-09 12:15:53 +04:00
|
|
|
|
|
|
|
def draw(self, context):
|
2011-08-19 18:52:46 +04:00
|
|
|
"""Draw the element on the Cairo context."""
|
2011-08-22 20:20:23 +04:00
|
|
|
if not self.surface:
|
|
|
|
# TODO Draw the alternative text ?
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
pattern = cairo.SurfacePattern(self.surface)
|
|
|
|
context.set_source(pattern)
|
|
|
|
context.paint()
|