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

84 lines
2.7 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
def get_replaced_element(element):
2011-08-19 18:52:46 +04:00
"""Return a :class:`Replacement` object if ``element`` is replaced."""
2011-05-25 17:54:46 +04:00
# TODO: maybe allow registering new replaced elements
if element.tag == 'img':
return ImageReplacement(element)
class Replacement(object):
2011-08-19 18:52:46 +04:00
"""Abstract base class for replaced elements. """
2011-05-25 17:54:46 +04:00
def __init__(self, element):
self.element = element
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-19 18:52:46 +04:00
"""Replaced ``<img>`` element."""
2011-08-09 12:15:53 +04:00
def __init__(self, element):
2011-08-19 18:52:46 +04:00
super(ImageReplacement, self).__init__(element)
2011-08-09 12:15:53 +04:00
self.src = get_url_attribute(element, 'src')
self.alt_text = element.get('alt')
2011-08-10 16:51:18 +04:00
self.surface = get_image_surface_from_uri(self.src)
# TODO: if surface is None, use alt attribute
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()