CairoSVG icon indicating copy to clipboard operation
CairoSVG copied to clipboard

Fail of image path resolution in a defs/use context

Open o314 opened this issue 1 year ago • 3 comments

Hello there. I encounter a fail of an image path resolution in a defs/use context Here is a MWE (env: win10 pro with docker desktop) The svg file is ok in chrome but fails with cairosvg

fs step

  • bug.svg
  • bug_photo.jpg
  • Dockerfile

where bug.svg content is

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" xml:space="preserve"
    width="1000" height="1000" viewBox="0 0 1000 1000">

<defs>
<image id="x_photo" x="0" y="0" width="301" height="167" xlink:href="bug_photo.jpg" />
</defs>

<g transform="scale(3)">
    <use xlink:href="#x_photo" x="0" y="0" />
</g>
</svg>

Dockerfile content is

FROM python:3.10 AS py
RUN pip install \
        ipython \
        cairosvg
        
FROM py
WORKDIR /lab
COPY . "/lab"
# CMD ["ipython"]
CMD ["/bin/bash"]

docker host step

docker image build -t o314_cairosvg_python3dot10 .
docker run -it --name o314_cairosvg_python3dot10_inst1 o314_cairosvg_python3dot10
# docker start -ai o314_cairosvg_python3dot10_inst1 # if needed
# docker cp o314_cairosvg_python3dot10_inst1:/lab/bug.png bug.png # if needed

docker container step

cairosvg bug.svg -o bug.png -u

that turns into a big nasty bug

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/urllib/request.py", line 1505, in open_local_file
    stats = os.stat(localfile)
FileNotFoundError: [Errno 2] No such file or directory: '/bug_photo.jpg'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/cairosvg", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.10/site-packages/cairosvg/__main__.py", line 78, in main
    SURFACES[output_format.upper()].convert(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/cairosvg/surface.py", line 133, in convert
    instance = cls(
  File "/usr/local/lib/python3.10/site-packages/cairosvg/surface.py", line 225, in __init__
    self.draw(tree)
  File "/usr/local/lib/python3.10/site-packages/cairosvg/surface.py", line 472, in draw
    self.draw(child)
  File "/usr/local/lib/python3.10/site-packages/cairosvg/surface.py", line 472, in draw
    self.draw(child)
  File "/usr/local/lib/python3.10/site-packages/cairosvg/surface.py", line 404, in draw
    TAGS[node.tag](self, node)
  File "/usr/local/lib/python3.10/site-packages/cairosvg/defs.py", line 365, in use
    surface.draw(tree)
  File "/usr/local/lib/python3.10/site-packages/cairosvg/surface.py", line 404, in draw
    TAGS[node.tag](self, node)
  File "/usr/local/lib/python3.10/site-packages/cairosvg/image.py", line 28, in image
    image_bytes = node.fetch_url(url, 'image/*')
  File "/usr/local/lib/python3.10/site-packages/cairosvg/parser.py", line 243, in fetch_url
    return read_url(url, self.url_fetcher, resource_type)
  File "/usr/local/lib/python3.10/site-packages/cairosvg/url.py", line 154, in read_url
    return url_fetcher(url, resource_type)
  File "/usr/local/lib/python3.10/site-packages/cairosvg/url.py", line 84, in fetch
    return urlopen(Request(url, headers=HTTP_HEADERS)).read()
  File "/usr/local/lib/python3.10/urllib/request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/lib/python3.10/urllib/request.py", line 519, in open
    response = self._open(req, data)
  File "/usr/local/lib/python3.10/urllib/request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/usr/local/lib/python3.10/urllib/request.py", line 496, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.10/urllib/request.py", line 1483, in file_open
    return self.open_local_file(req)
  File "/usr/local/lib/python3.10/urllib/request.py", line 1522, in open_local_file
    raise URLError(exp)
urllib.error.URLError: <urlopen error [Errno 2] No such file or directory: '/bug_photo.jpg'>

The jpg path is not resolved correctly I have tried to trace / fix / debug whatever without success. Is there any simple things i have missed ? Is it really a bug ?

PS

some point of interests

  • https://github.com/Kozea/CairoSVG/blob/main/test_non_regression/svg/struct-image-02-b.svg?short_path=403fdef#L55-L63
  • https://github.com/Kozea/CairoSVG/blob/main/cairosvg/parser.py
  • https://github.com/Kozea/CairoSVG/blob/main/cairosvg/url.py
  • https://github.com/Kozea/CairoSVG/blob/main/cairosvg/image.py

o314 avatar Apr 20 '24 14:04 o314

The files mwe.zip

o314 avatar Apr 20 '24 14:04 o314