mgieseki / dvisvgm

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

Mapping from tex coordinates to svg coordinates #185

Closed ma-chengyuan closed 1 year ago

ma-chengyuan commented 2 years ago

I am writing a program where I need to clip parts of the svg generated by dvisvgm based on the output of synctex. To do that I need to know how the tex coordinates are mapped to svg coordinates. I observed that dvisvgm seems to shrink the page a little bit in the conversion, i.e.

processing PDF file
  graphic size: 614.295pt x 4096.002625pt (215.9mm x 1439.580278mm)
  output written to test.svg

becomes

<svg ... width='612pt' height='4080.7pt' viewBox='0 -4080.7 612 4080.7'>

Some empirical testing suggests that the scaling factor is about 0.996264. I plugged this in my program as a magic constant but there still seems to be some precision issues. I am very curious about where this shrinking comes from, and, if possible, how exactly does dvisvgm transform the coordinates?

Thanks!

muzimuzhi commented 2 years ago

In TeX 1 in = 72.72 pt = 72 bp, while in SVG 1 in = 72 pt (see SVG spec 1.1, sec. 7.10 Units). Or you can think of 1 SVG pt = 1 TeX bp. Since 1 in = 25.4 mm, 215.9 mm = 8.5 in = 614.295 pt (in TeX) = 612 pt (in SVG). Thus, scaling factor is exactly 1.

mgieseki commented 2 years ago

Yes, @muzimuzhi is absolutely correct. dvisvgm uses TeX's length units in the message output, i.e. 1pt = 1/72.27 in. SVG's points, however, are slightly bigger. TeX therefore calls them "big points" (1 bp = 1/72 in). SVG, on the other hand, uses the abbreviation pt rather than bp, which can lead to some confusion. So, TeX's pt is not the same as SVG's pt. That's why the values in the SVG files are smaller by factor 72/72.27.