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

98 lines
2.8 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/>.
"""
2011-08-19 18:52:46 +04:00
Classes and helpers for HTML replaced elements.
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-09 12:15:53 +04:00
from .utils import get_url_attribute
2011-08-10 16:51:18 +04:00
from .draw.helpers import get_image_surface_from_uri
2011-05-25 17:54:46 +04:00
REPLACEMENT_HANDLERS = {}
2011-08-20 20:02:04 +04:00
2011-05-25 17:54:46 +04:00
def get_replaced_element(element):
2011-08-19 18:52:46 +04:00
"""Return a :class:`Replacement` object if ``element`` is replaced."""
if element.tag in REPLACEMENT_HANDLERS:
handler = REPLACEMENT_HANDLERS[element.tag]
return handler(element)
2011-08-20 20:02:04 +04:00
def register(tag):
def decorator(function):
REPLACEMENT_HANDLERS[tag] = function
return function
return decorator
2011-08-20 20:02:04 +04:00
@register('img')
2011-08-20 20:02:04 +04:00
def handle_img(element):
# TODO: somehow use the alt-text on broken images.
src = get_url_attribute(element, 'src')
return ImageReplacement(src)
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."""
2011-08-09 12:15:53 +04:00
pattern = cairo.SurfacePattern(self.surface)
context.set_source(pattern)
context.paint()