Skip to content
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

Fix usage with a relative input SVG file path #251

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cairosvg/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

import os.path
from io import BytesIO
from urllib.parse import urlparse

from PIL import Image

from .helpers import node_format, preserve_ratio, size
from .parser import Tree
from .surface import cairo
from .url import parse_url
from .url import _parse_url

IMAGE_RENDERING = {
'optimizeQuality': cairo.FILTER_BEST,
Expand All @@ -40,7 +41,7 @@ def image(surface, node):
base_url = node.get('{http://www.w3.org/XML/1998/namespace}base')
if not base_url and node.url:
base_url = os.path.dirname(node.url) + '/'
url = parse_url(node.get_href(), base_url)
url = _parse_url(node.get('{http://www.w3.org/1999/xlink}href'), base_url)
image_bytes = node.fetch_url(url, 'image/*')

if len(image_bytes) < 5:
Expand All @@ -59,7 +60,7 @@ def image(surface, node):
if 'y' in node:
del node['y']
tree = Tree(
url=url.geturl(), url_fetcher=node.url_fetcher,
url=urlparse(url).geturl(), url_fetcher=node.url_fetcher,
bytestring=image_bytes, tree_cache=surface.tree_cache,
unsafe=node.unsafe)
tree_width, tree_height, viewbox = node_format(
Expand Down
14 changes: 10 additions & 4 deletions cairosvg/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def parse_url(url, base=None):
the "folder" part of it is prepended to the URL.

"""
return urlparse(_parse_url(url, base) or '')


def _parse_url(url, base):
if url:
match = URL.search(url)
if match:
Expand Down Expand Up @@ -142,18 +146,20 @@ def parse_url(url, base=None):
# `urljoin` automatically uses the "folder" part of `base`
url = urljoin(base, url)
url = normalize_url(url.strip('\'"'))
return urlparse(url or '')
return url


def read_url(url, url_fetcher, resource_type):
"""Get bytes in a parsed ``url`` using ``url_fetcher``.

If ``url_fetcher`` is None a default (no limitations) URLFetcher is used.
"""
if url.scheme:
url = url.geturl()
parsed_url = urlparse(url) if isinstance(url, str) else url
str_url = url if isinstance(url, str) else url.geturl()
if parsed_url.scheme:
url = parsed_url.geturl()
else:
url = 'file://{}'.format(os.path.abspath(url.geturl()))
url = 'file://{}'.format(os.path.abspath(str_url))
url = normalize_url(url)

return url_fetcher(url, resource_type)
4 changes: 4 additions & 0 deletions test_non_regression/test_non_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ def test_image(svg_filename):
raise AssertionError(
'Images are different: {} {}'.format(
ref_png.name, test_png.name))


def test_image_with_relative_path():
test_image('./struct-image-01-t.svg')