-
-
Notifications
You must be signed in to change notification settings - Fork 644
/
images.py
68 lines (59 loc) · 2.46 KB
/
images.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
weasyprint.svg.image
--------------------
Draw image and svg tags.
"""
from .utils import preserve_ratio
def svg(svg, node, font_size):
"""Draw svg tags."""
x, y = svg.point(node.get('x'), node.get('y'), font_size)
svg.stream.transform(e=x, f=y)
if svg.tree == node:
width, height = svg.concrete_width, svg.concrete_height
else:
width, height = svg.point(
node.get('width'), node.get('height'), font_size)
scale_x, scale_y, translate_x, translate_y = preserve_ratio(
svg, node, font_size, width, height)
if svg.tree != node:
svg.stream.rectangle(0, 0, width, height)
svg.stream.clip()
svg.stream.end()
svg.stream.transform(a=scale_x, d=scale_y, e=translate_x, f=translate_y)
def image(svg, node, font_size):
"""Draw image tags."""
x, y = svg.point(node.get('x'), node.get('y'), font_size)
svg.stream.transform(e=x, f=y)
base_url = node.get('{http://www.w3.org/XML/1998/namespace}base')
url = node.get_href(base_url or svg.url)
image = svg.context.get_image_from_uri(url=url, forced_mime_type='image/*')
if image is None:
return
width, height = svg.point(node.get('width'), node.get('height'), font_size)
intrinsic_width, intrinsic_height, intrinsic_ratio = (
image.get_intrinsic_size(1, font_size))
if intrinsic_width is None and intrinsic_height is None:
if intrinsic_ratio is None or (not width and not height):
intrinsic_width, intrinsic_height = 300, 150
elif not width:
intrinsic_width, intrinsic_height = (
intrinsic_ratio * height, height)
else:
intrinsic_width, intrinsic_height = width, width / intrinsic_ratio
elif intrinsic_width is None:
intrinsic_width = intrinsic_ratio * intrinsic_height
elif intrinsic_height is None:
intrinsic_height = intrinsic_width / intrinsic_ratio
width = width or intrinsic_width
height = height or intrinsic_height
scale_x, scale_y, translate_x, translate_y = preserve_ratio(
svg, node, font_size, width, height,
(0, 0, intrinsic_width, intrinsic_height))
svg.stream.rectangle(0, 0, width, height)
svg.stream.clip()
svg.stream.end()
svg.stream.push_state()
svg.stream.transform(a=scale_x, d=scale_y, e=translate_x, f=translate_y)
image.draw(
svg.stream, intrinsic_width, intrinsic_height, image_rendering='auto')
svg.stream.pop_state()