mgieseki / dvisvgm

A fast DVI, EPS, and PDF to SVG converter
https://dvisvgm.de
GNU General Public License v3.0
304 stars 31 forks source link

PNG in XDV is missing #146

Closed linsui closed 3 months ago

linsui commented 3 years ago
  1. With tikz, the tex file
\documentclass[tikz, margin=3pt, dvisvgm]{standalone}
\usepackage{graphicx}

\begin{document}
\begin{tikzpicture}
\node [draw] {\includegraphics[width=1cm]{./test.png}};
\end{tikzpicture}
\end{document}

The generate svg file is

<?xml version='1.0' encoding='UTF-8'?>
<!-- This file was generated by dvisvgm 2.11 -->
<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='-69.011212 -69.011212 35.385983 35.382665'>
<g id='page1'>
<g stroke-miterlimit='10' transform='translate(-51.318223,-51.31988)scale(0.996264,-0.996264)'>
<g fill='#000' stroke='#000'>
<g stroke-width='0.4'>
<path d='M -17.55934 -17.55768 h 35.11868 v 35.11536 h -35.11868 Z' fill='none'/>
<g transform='translate(-14.22636,-14.2247)'>
<g stroke='none' transform='scale(-1.00375,1.00375)translate(-51.318223,-51.31988)scale(-1,-1)'>
<g fill='#000'>
<g stroke='none'/>
</g>
</g>
</g>
</g>
</g>
</g>
</g>
</svg>
  1. Without tikz, the tex file
\documentclass[margin=3pt, dvisvgm]{standalone}
\usepackage{graphicx}

\begin{document}
\includegraphics[width=1cm]{./test.png}
\end{document}

The generate svg file is

<?xml version='1.0' encoding='UTF-8'?>
<!-- This file was generated by dvisvgm 2.11 -->
<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 0 0'>
<g id='page1'>
<g transform='translate(-69.011212,-69.011121)'> <svg height='28.34311' overflow='visible' viewBox='0 0 500 500' width='28.34637'> <image height='500' width='500' xlink:href='./test.png'/> </svg> </g>
</g>
</svg>

I generate the svg with

xelatex -no-pdf test
dvisvgm --zoom=-1 --exact --font-format=woff test.xdv
mgieseki commented 3 years ago

This seems to be a bug in the dvisvgm backend of LaTeX/XeLaTeX. It creates a pdf:image special to reference the PNG file which dvisvgm doesn't support. Here's a excerpt of the XDV file:

  xxx: 'dvisvgm:raw <g fill="#000">{?nl} '
  xxx: 'dvisvgm:raw <g stroke="none"> '
  push:
    push:
      xxx: 'pdf:image bbox 0 0 479.20183 289.91212 clip 0 width 28.45274pt (./test.png) '
    pop:
  pop:
  right: 28.452744pt
  xxx: 'dvisvgm:raw </g> '
  xxx: 'dvisvgm:raw </g>{?nl} '

The missing image in the SVG is expected here and therefore not a bug in dvisvgm. The issue must be fixed in the dvisvgm driver provided by the LaTeX team.

agrahn commented 3 years ago

TikZ provides its own dvisvgm backend driver for graphics inclusion, which overrides the one (dvisvgm.def) provided by the graphic[xs]/graphics-def package. (Disclaimer: I edited the current version 2020/03/22 v1.2 of dvisvgm.def.)

The TikZ driver for dvisvgm seems to be outdated regarding graphics inclusion. Maybe it is worth notifying its author, @hmenke, about this.

As a quick fix you can force using dvisvgm.def:

\documentclass[tikz, margin=3pt, dvisvgm]{standalone}
\usepackage{graphicx}

% force dvisvgm.def from pkg graphics-def
\makeatletter
\input{dvisvgm.def}
\makeatother

\begin{document}
\begin{tikzpicture}
\node [draw] {\includegraphics[width=1cm]{example-image.png}};
\end{tikzpicture}
\end{document}
hmenke commented 3 years ago

Grrr. That's also a problem of standalone with its nice implicit package loading (e.g. the tikz package option). I'll see what needs to be fixed in PGF.

linsui commented 3 years ago

Thanks! The quick fix works well with my tikz picture. Without tikz (the second case) the image is still missing. Is this expected? I don't know how these tools work so this may be a stupid question.

agrahn commented 3 years ago

Works for me (test.png replaced with example-image.png).

Make sure the PNG is in the current directory for the web browser to find it. Unlike PDF/PS, included PNG is not inlined in the output SVG.

hmenke commented 3 years ago

I've investigated. It's not the fault of PGF but of xcolor (which is loaded by PGF). MWE:

\documentclass[dvisvgm]{standalone}
\usepackage{graphicx}
\usepackage{xcolor}
\begin{document}
\includegraphics[width=1cm]{example-image.png}
\end{document}

It looks like xcolor doesn't inspect \Gin@driver but just loads xetex.def because that's the engine that has been detected.

Log and dviasm ``` $ xelatex --no-pdf test.tex This is XeTeX, Version 3.14159265-2.6-0.999992 (TeX Live 2020) (preloaded format=xelatex) restricted \write18 enabled. entering extended mode (./test.tex LaTeX2e <2020-10-01> patch level 2 L3 programming layer <2020-12-07> xparse <2020-03-03> (/opt/texlive/2020/texmf-dist/tex/latex/standalone/standalone.cls Document Class: standalone 2018/03/26 v1.3a Class to compile TeX sub-files stan dalone (/opt/texlive/2020/texmf-dist/tex/latex/tools/shellesc.sty) (/opt/texlive/2020/texmf-dist/tex/generic/iftex/ifluatex.sty (/opt/texlive/2020/texmf-dist/tex/generic/iftex/iftex.sty)) (/opt/texlive/2020/texmf-dist/tex/latex/xkeyval/xkeyval.sty (/opt/texlive/2020/texmf-dist/tex/generic/xkeyval/xkeyval.tex (/opt/texlive/2020/texmf-dist/tex/generic/xkeyval/xkvutils.tex (/opt/texlive/2020/texmf-dist/tex/generic/xkeyval/keyval.tex)))) (/opt/texlive/2020/texmf-dist/tex/latex/standalone/standalone.cfg) (/opt/texlive/2020/texmf-dist/tex/latex/base/article.cls Document Class: article 2020/04/10 v1.4m Standard LaTeX document class (/opt/texlive/2020/texmf-dist/tex/latex/base/size10.clo))) (/opt/texlive/2020/texmf-dist/tex/latex/graphics/graphicx.sty (/opt/texlive/2020/texmf-dist/tex/latex/graphics/graphics.sty (/opt/texlive/2020/texmf-dist/tex/latex/graphics/trig.sty) (/opt/texlive/2020/texmf-dist/tex/latex/graphics-cfg/graphics.cfg) (/opt/texlive/2020/texmf-dist/tex/latex/graphics-def/dvisvgm.def))) (/opt/texlive/2020/texmf-dist/tex/latex/xcolor/xcolor.sty (/opt/texlive/2020/texmf-dist/tex/latex/graphics-cfg/color.cfg) (/opt/texlive/2020/texmf-dist/tex/latex/graphics-def/xetex.def)) (/opt/texlive/2020/texmf-dist/tex/latex/l3backend/l3backend-dvisvgm.def) (./test.aux) (/opt/texlive/2020/texmf-dist/tex/latex/base/ts1cmr.fd) [1] (./test.aux) ) Output written on test.xdv (1 page, 464 bytes). Transcript written on test.log. $ dviasm test.xdv [preamble] id: 7 numerator: 25400000 denominator: 473628672 magnification: 1000 comment: ' XeTeX output 2020.12.15:1304' [postamble] maxv: 0pt maxh: 272.729996pt maxs: 6 pages: 1 [font definitions] [page 1 0 0 0 0 0 0 0 0 0] xxx: 'pdf:pagesize width 28.45274pt height 21.33821pt' down: -50.931793pt push: y: -21.338211pt push: push: right: -72.270004pt xxx: 'color push gray 0' right: 345pt xxx: 'color pop' pop: pop: z: 21.338211pt push: right: -72.270004pt y0: xxx: 'color push gray 0' z0: push: push: xxx: 'color push gray 0' push: xxx: 'color push gray 0' push: xxx: 'pdf:image bbox 0 0 399.99902 299.99927 clip 0 width 28.45274pt (example-image.png) ' pop: right: 28.452744pt xxx: 'color pop' pop: right: 28.452744pt xxx: 'color pop' pop: pop: pop: push: right: -72.270004pt xxx: 'color push gray 0' right: 345pt xxx: 'color pop' pop: pop: ```
agrahn commented 3 years ago

Yes, I remember. I once found out this too. xcolor doesn't know about dvisvgm. I even contacted its author (in April 2020) and asked for including dvisvgm.def in xcolor's set-up, but he did not reply.

agrahn commented 3 years ago

To make xcolor aware of dvisvgm, I suggested the following line to be updated in xcolor.sty:

\@tfor\@@tmp:={dvipdf}{dvipdfm}{dvipdfmx}{dvips}{dvipsone}{dvisvgm}{dviwin}%
{emtex}{luatex}{pctex32}{pctexhp}{pctexps}{pctexwin}{pdftex}%
{tcidvi}{textures}{truetex}{vtex}{xetex}\do
agrahn commented 3 years ago

Fooling xcolor by loading it as the first package works as well. (There is a related topic on TeX.SX: https://tex.stackexchange.com/a/537711 )

\RequirePackage{xcolor}
\documentclass[tikz, margin=3pt, dvisvgm]{standalone}
\usepackage{graphicx}

\begin{document}
\begin{tikzpicture}
\node [draw] {\includegraphics[width=1cm]{example-image.png}};
\end{tikzpicture}
\end{document}