mgieseki / dvisvgm

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

clipping implementation in PSfile and pdffile specials #176

Closed agrahn closed 2 years ago

agrahn commented 2 years ago

Currently, clipping of embedded PS and PDF files is implemented in the PSfile and pdffile specials through <clipPath> and the clip-path attribute.

Unfortunately, this significantly impairs the rendering performance in Firefox. It becomes apparent for animations produced with the animate package where frame rates needed for smooth playback cannot be achieved if clipped graphics were embedded.

Consider the following example (latex + dvisvgm --zoom=-1):

\documentclass[dvisvgm]{standalone}

\usepackage{animate}
\usepackage{graphicx}

%\makeatletter
%\input{dvisvgm.def.new} % clipping through <svg overflow="hidden" ...>
%\makeatother

\begin{document}

\begin{animateinline}[measure,controls,loop]{30} % 30 frames per sec
\multiframe{90}{i=1+1}{
  \includegraphics[clip,trim=5 5 5 5,page=\i]{rainbow2.pdf}
}
\end{animateinline}

\end{document}

rainbow2.pdf dvisvgm.def.new.zip

The average frame rates during the animation loops are printed to the JavaScript console in Firefox (Ctrl++I)

Therefore, I'd like to suggest the svg overflow="hidden" method for implementing rectangular clipping of embedded graphics. I did this on the LaTeX level in the driver file dvisvgm.def.new which I attached as dvisvgm.def.new.zip. To make use of it, unzip the file and uncomment the commented code lines in the example. With the svg overflow="hidden" method for clipping, the animation performs much better in Firefox.

mgieseki commented 2 years ago

I've pushed a quick implementation to branch tmp. If you like, please test if the changes work as expected with your examples. I'm currently a bit busy and have only done a few rough tests so far.

agrahn commented 2 years ago

Thank you @Martin, it works like charm! I am adding a simple test case below. Would you please merge this into master, so that it could be included in TL-22? That would be great.

\documentclass{article}
\usepackage[a6paper]{geometry}

\begin{document}
x\hbox to 52bp {\vrule width 0pt height 52bp\special{pdffile="example.pdf" llx=10 lly=10 urx=62 ury=62}\hss}x\quad
x\hbox to 52bp {\vrule width 0pt height 52bp\special{pdffile="example.pdf" llx=10 lly=10 urx=62 ury=62 clip}\hss}x
\end{document}

example.pdf

mgieseki commented 2 years ago

Great to hear that it works properly. I've merged the changes into master. I'm not sure, though, when I'll find the time to prepare a new release. It might take a couple of days.