1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 00:21:15 +03:00
WeasyPrint/weasy/html/__init__.py

134 lines
4.1 KiB
Python
Raw Normal View History

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/>.
"""
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
from ..css.values import get_single_keyword, make_keyword
from ..formatting_structure import boxes
from ..utils import get_url_attribute
from ..draw.helpers import get_image_surface_from_uri
2011-08-10 16:51:18 +04:00
2011-05-25 17:54:46 +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
def handle_element(document, element):
"""Return a :class:`Box` for ``element`` or None."""
if element.tag in HTML_HANDLERS:
handler = HTML_HANDLERS[element.tag]
return handler(document, element)
2011-08-20 20:02:04 +04:00
def handler(tag):
2011-08-22 19:55:30 +04:00
"""
Return a decorator that registers a function handling `tag` HTML elements.
2011-08-22 19:55:30 +04:00
"""
def decorator(function):
HTML_HANDLERS[tag] = function
return function
return decorator
2011-08-20 20:02:04 +04:00
def make_replaced_box(document, element, replacement):
display = get_single_keyword(document.style_for(element).display)
if display in ('block', 'list-item', 'table'):
type_ = boxes.BlockLevelReplacedBox
elif display in ('inline', 'inline-table', 'inline-block'):
type_ = boxes.InlineLevelReplacedBox
else:
raise NotImplementedError('Unsupported display: ' + display)
return type_(document, element, replacement)
@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.
"""
# TODO: somehow use the alt-text on broken images.
src = get_url_attribute(element, 'src')
replacement = ImageReplacement(src)
return make_replaced_box(document, element, replacement)
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.
:param image_uri: uri where to get the image.
"""
def __init__(self, image_uri):
self.surface = get_image_surface_from_uri(image_uri)
2011-08-09 12:15:53 +04:00
def intrinsic_width(self):
if self.surface:
return self.surface.get_width()
2011-08-09 12:15:53 +04:00
def intrinsic_height(self):
if self.surface:
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."""
if not self.surface:
# TODO Draw the alternative text ?
pass
else:
pattern = cairo.SurfacePattern(self.surface)
context.set_source(pattern)
context.paint()