-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Some hack to include SVG #798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pfernique
wants to merge
3
commits into
python-openxml:develop
Choose a base branch
from
pfernique:feature/add_svg_picture
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,48 @@ | ||
# encoding: utf-8 | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
from struct import Struct | ||
|
||
from .constants import MIME_TYPE | ||
from .image import BaseImageHeader | ||
|
||
|
||
class Svg(BaseImageHeader): | ||
""" | ||
Image header parser for GIF images. Note that the GIF format does not | ||
support resolution (DPI) information. Both horizontal and vertical DPI | ||
default to 72. | ||
""" | ||
@classmethod | ||
def from_stream(cls, stream): | ||
""" | ||
Return |Gif| instance having header properties parsed from GIF 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` for | ||
SVG images. | ||
""" | ||
return MIME_TYPE.SVG | ||
|
||
@property | ||
def default_ext(self): | ||
""" | ||
Default filename extension, always 'gif' for GIF images. | ||
""" | ||
return 'svg' | ||
|
||
@classmethod | ||
def _dimensions_from_stream(cls, stream): | ||
from xml.etree import ElementTree | ||
stream.seek(0) | ||
text = stream.read() | ||
root = ElementTree.fromstring(text) | ||
x_min, y_min, x_max, y_max = [float(coord) for coord in root.attrib['viewBox'].split()] | ||
return x_max - x_min, y_max - y_min |
This file contains hidden or 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 hidden or 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,92 @@ | ||
# encoding: utf-8 | ||
|
||
"""Unit test suite for docx.image.svg module""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
import pytest | ||
|
||
from docx.compat import BytesIO | ||
from docx.image.constants import MIME_TYPE | ||
from docx.image.svg import Svg | ||
|
||
from ..unitutil.mock import ANY, initializer_mock | ||
|
||
|
||
class DescribeSvg(object): | ||
|
||
def it_can_construct_from_a_svg_stream(self, Svg__init__): | ||
cx, cy = 81.56884, 17.054602 | ||
bytes_ = b"""\ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<svg | ||
xmlns:dc="http://purl.org/dc/elements/1.1/" | ||
xmlns:cc="http://creativecommons.org/ns#" | ||
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | ||
xmlns:svg="http://www.w3.org/2000/svg" | ||
xmlns="http://www.w3.org/2000/svg" | ||
id="svg883" | ||
version="1.1" | ||
viewBox="0 0 81.56884 17.054602" | ||
height="17.054602mm" | ||
width="81.56884mm"> | ||
<defs | ||
id="defs877" /> | ||
<metadata | ||
id="metadata880"> | ||
<rdf:RDF> | ||
<cc:Work | ||
rdf:about=""> | ||
<dc:format>image/svg+xml</dc:format> | ||
<dc:type | ||
rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> | ||
<dc:title /> | ||
</cc:Work> | ||
</rdf:RDF> | ||
</metadata> | ||
<g | ||
transform="translate(238.27068,33.733892)" | ||
id="layer1"> | ||
<text | ||
id="text843" | ||
y="-16.976948" | ||
x="-238.27068" | ||
style="font-style:normal;font-weight:normal;font-size:22.57777786px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332" | ||
xml:space="preserve"><tspan | ||
style="stroke-width:0.26458332" | ||
y="-16.976948" | ||
x="-238.27068" | ||
id="tspan841">Test 2 !</tspan></text> | ||
<flowRoot | ||
transform="matrix(0.26458333,0,0,0.26458333,-238.27068,-33.392139)" | ||
style="font-style:normal;font-weight:normal;font-size:85.33333588px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none" | ||
id="flowRoot814" | ||
xml:space="preserve"><flowRegion | ||
id="flowRegion816"><rect | ||
y="-63.976192" | ||
x="-182.85715" | ||
height="248.57143" | ||
width="880" | ||
id="rect818" /></flowRegion><flowPara | ||
id="flowPara820" /></flowRoot> </g> | ||
</svg>""" | ||
stream = BytesIO(bytes_) | ||
|
||
svg = Svg.from_stream(stream) | ||
|
||
Svg__init__.assert_called_once_with(ANY, cx, cy, 72, 72) | ||
assert isinstance(svg, Svg) | ||
|
||
def it_knows_its_content_type(self): | ||
svg = Svg(None, None, None, None) | ||
assert svg.content_type == MIME_TYPE.SVG | ||
|
||
def it_knows_its_default_ext(self): | ||
svg = Svg(None, None, None, None) | ||
assert svg.default_ext == 'svg' | ||
|
||
# fixture components --------------------------------------------- | ||
|
||
@pytest.fixture | ||
def Svg__init__(self, request): | ||
return initializer_mock(request, Svg) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm misreading this - this would only capture SVG is actually provided as a file (that has a
filename
go with it). There are many use cases where pictures are generated on the fly in in-memory buffers and therefore not provided as files. Any way to account for that?(For example the PNG equivalent for matplotlib + docx would be something like:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true that this seems strange but when dispatch is done, a filename
image.svg
is given for a stream (must be the same forpng
). So even if you provide in-memory buffers, it's good.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't realize that - thank you for calling it out. I looked a bit further (which I should have from the beginning):filename is generated here in
image.py
.