-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for adding SVG pictures to docx files (#27)
* Add basic support for adding SVG pictures to docx files
- Loading branch information
Showing
8 changed files
with
180 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import xml.etree.ElementTree as ET | ||
|
||
from .constants import MIME_TYPE | ||
from .image import BaseImageHeader | ||
|
||
BASE_PX = 72 | ||
|
||
|
||
class Svg(BaseImageHeader): | ||
""" | ||
Image header parser for SVG images. | ||
""" | ||
|
||
@classmethod | ||
def from_stream(cls, stream): | ||
""" | ||
Return |Svg| instance having header properties parsed from SVG image | ||
in *stream*. | ||
""" | ||
px_width, px_height = cls._dimensions_from_stream(stream) | ||
return cls(px_width, px_height, 72, 72) | ||
|
||
@property | ||
def content_type(self): | ||
""" | ||
MIME content type for this image, unconditionally `image/svg+xml` for | ||
SVG images. | ||
""" | ||
return MIME_TYPE.SVG | ||
|
||
@property | ||
def default_ext(self): | ||
""" | ||
Default filename extension, always 'svg' for SVG images. | ||
""" | ||
return "svg" | ||
|
||
@classmethod | ||
def _dimensions_from_stream(cls, stream): | ||
stream.seek(0) | ||
data = stream.read() | ||
root = ET.fromstring(data) | ||
if root.attrib.get("width") is None: | ||
return cls._calculate_scaled_dimensions(root.attrib["viewBox"]) | ||
|
||
width = int(root.attrib["width"]) | ||
height = int(root.attrib["height"]) | ||
return width, height | ||
|
||
@classmethod | ||
def _calculate_scaled_dimensions( | ||
cls, viewbox: str, base_px: int = BASE_PX | ||
) -> tuple[int, int]: | ||
_, _, logical_width, logical_height = map(int, viewbox.split()) | ||
|
||
aspect_ratio = logical_width / logical_height | ||
|
||
if aspect_ratio >= 1: | ||
final_width = base_px | ||
final_height = base_px / aspect_ratio | ||
else: | ||
final_height = base_px | ||
final_width = base_px * aspect_ratio | ||
|
||
return int(final_width), int(final_height) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import io | ||
from xml.etree.ElementTree import Element, tostring | ||
|
||
import pytest | ||
|
||
from docx.image.svg import BASE_PX, Svg | ||
|
||
|
||
@pytest.fixture | ||
def svg_with_dimensions(): | ||
"""Fixture for SVG stream with width and height.""" | ||
root = Element("svg", width="200", height="100") | ||
return io.BytesIO(tostring(root)) | ||
|
||
|
||
@pytest.fixture | ||
def svg_with_viewbox(): | ||
"""Fixture for SVG stream with viewBox but no width and height.""" | ||
root = Element("svg", viewBox="0 0 400 200") | ||
return io.BytesIO(tostring(root)) | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
("0 0 400 200", 72, 36, 72), # Landscape | ||
("0 0 200 400", 100, 200, 200), # Portrait | ||
("0 0 100 100", 50, 50, 50), # Square | ||
] | ||
) | ||
def viewbox_data(request): | ||
"""Fixture for different viewBox test cases as tuples.""" | ||
return request.param | ||
|
||
|
||
@pytest.fixture( | ||
params=[ | ||
(b'<svg width="200" height="100"/>', 200, 100), | ||
(b'<svg viewBox="0 0 400 200"/>', BASE_PX, BASE_PX // 2), | ||
] | ||
) | ||
def svg_stream_data(request): | ||
return request.param | ||
|
||
|
||
def test_dimensions_from_stream(svg_stream_data): | ||
stream_data, expected_width, expected_height = svg_stream_data | ||
stream = io.BytesIO(stream_data) | ||
width, height = Svg._dimensions_from_stream(stream) | ||
assert width == expected_width | ||
assert height == expected_height | ||
|
||
|
||
def test_calculate_scaled_dimensions(viewbox_data): | ||
viewbox, expected_width, expected_height, base_px = viewbox_data | ||
width, height = Svg._calculate_scaled_dimensions(viewbox, base_px) | ||
assert width == expected_width | ||
assert height == expected_height |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters