Dvisvgm: Generating Scalable Vector Graphics from DVI and EPS Files
Total Page:16
File Type:pdf, Size:1020Kb
TUGboat, Volume 38 (2017), No. 3 359 dvisvgm: Generating scalable vector both utilities apparently stalled, and the website of graphics from DVI and EPS files dvi2svg disappeared several months later. The al- ternative approach of creating SVG files from PDF Martin Gieseking didn't work satisfactorily either, due to the missing Abstract conversion of hyperlinks across pages inside the doc- ument, and the weak support of METAFONT-based dvisvgm is a command-line utility that converts DVI fonts, which were embedded as bitmap fonts if no and EPS files to the XML-based vector graphics for- vector versions were available. mat SVG, an open standard developed by the W3C. Since I had already written a couple of small DVI Today, SVG is supported by many applications in- utilities before and therefore had working DVI and cluding text processors, graphics editors, and web TFM readers available, I started to build a simple browsers. Therefore, it's a convenient format with SVG converter on top of them. The first public which to enrich websites and non-T X documents E release of dvisvgm was in August 2005. Since then, with self-contained, arbitrarily scalable T X output. E it's been a private free-time project and has evolved This article gives an overview of selected features of a lot over the years, largely because of wonderful dvisvgm and addresses some challenges faced in its feedback, detailed bug reports and interesting feature development. suggestions. dvisvgm is included in TEX Live and MiKTEX, and is also available through MacPorts. 1 How it all started In 2005 I was working on a wiki-based cross media 2 About dvisvgm and basic usage publishing system called media2mult [3], which was dvisvgm is a command-line utility written in C++. It supposed to produce documents in various output for- supports standard DVI files with a version identifier mats from a single source without the need to force of 2, as well as DVI files created by pTEX in vertical the authors to scatter format and layout specific mode (version 3) and X TE EX (versions 5 to 7).1 The settings throughout the input document. Since the latter are also known as XDV files and are created if conversion back-end was built on the XML format- X TE EX is called with option -no-pdf. ting technology XSL-FO, which at that time didn't The basic usage of dvisvgm is straightforward provide sufficient math support through MathML,I and similar to other DVI drivers. If no other options needed a way to embed scalable TEX output in the are specified, it converts the first page of the given XSL-FO files. The preferred format for this task was DVI file to an SVG file with the same name. If the SVG, because it is XML-based and therefore fit nicely DVI file has more than one page, the page number into the other involved XML technologies; for exam- is appended to the base name. For example, ple, the files could be post-processed easily by apply- dvisvgm myfile.dvi ing XSLT and XQuery scripts. Furthermore, SVG was creates the file myfile.svg if myfile.dvi consists decently supported by Apache's open-source XSL-FO of a single page only, else myfile-01.svg. This processor FOP and the related Batik SVG toolkit. was originally because the initial releases of dvisvgm Fortunately, two DVI to SVG converters were could process only single DVI pages in one run; the already available, dvisvg [7] by Rudolf Sabo and behavior is still retained for compatibility. To select a dvi2svg [2] by Adrian Frischauf. Both utilities looked different page or a sequence of page ranges, the option promising, and created nice results from my initial --page is required. It accepts a single page number test files. dvi2svg even supported color and hyperref or a comma-separated list of ranges. Regardless of specials, which was another advanced requirement whether any page numbers are specified multiple for my needs. However, the main drawback for the times, e.g. by overlapping range specification, all planned document conversion engine was that both selected pages are converted to separate SVG files tools relied on pre-converted SVG font files derived only once. The command from a selection of common TEX fonts, notably the dvisvgm --page=1,3,5-9,8-10 myfile.dvi Computer Modern family. There was no simple way is identical to to process DVI files referencing arbitrary fonts sup- dvisvgm --page=1,3,5-10 myfile.dvi ported by the TEX ecosystem. The latter had to be generated in advance by some kind of DVI pre- and converts the pages 1, 3, and 5 through 10. The processing and by extracting the glyph data from file names get the corresponding number suffixes as PostScript or TrueType fonts, e.g. as described in [4, 1 When an incompatible change in X TE EX's XDV format pp. 272{274]. is made, the DVI version identifier is increased. The recent Sadly, around that time, the development of X TE EX revision 0.99998 creates DVI files of version 7. dvisvgm: Generating scalable vector graphics from DVI and EPS files 360 TUGboat, Volume 38 (2017), No. 3 --output=hpatterni SVG file name of page 1 ment. While this approach keeps DVI files compact, it also reduces their cross-platform portability and %f myfile.svg delegates significant processing to the driver. On %f-%p myfile-01.svg the other hand, the requirement for a working T X newfile-%p newfile-01.svg E system enables full access to all font data, including %f-%4p-of-%P myfile-0001-of-20.svg those usually not embedded into PDF files. This %f-%4(p-1) myfile-0000.svg is especially important regarding METAFONT-based %f-%(P-p+1) myfile-20.svg fonts not available in other formats. ../%f/svg/%3p ../myfile/svg/001.svg Since I was confronted with a wide variety of Table 1: Effect of several output patterns applied to documents using a wide variety of fonts, it was impor- myfile.dvi consisting of 20 pages. tant to provide dvisvgm with comprehensive font sup- port including virtual fonts, various font encodings, above. It's also possible to give open page ranges by CMaps, sub-font definitions, font maps, handling of omitting the start or end number: glyph names and Japanese fonts which often use an dvisvgm --page=-5,10- extended TFM format called JFM. The proper map- converts all pages from the beginning up to page 5, ping of PostScript character names, as used in Type 1 as well as page 10 and all following ones. Regardless fonts, to corresponding Unicode points, required the of the number of pages converted, dvisvgm always inclusion of the Adobe Glyph List (AGL).3 prescans the entire DVI file in advance to collect Furthermore, the generated SVG files needed global data, like font definitions, PostScript headers to be compatible with XSL-FO converters and SVG and hyperlink targets. In this way it is possible renderers, like web browsers. It turned out that each to convert selected pages correctly even if required type of applications evidently focused on different information is located on excluded pages. aspects of the SVG standard, as some elements are In order to change the names of the generated not evaluated completely, leading to incorrect or in- SVG files, --output can be used. It supports pat- complete visual results. To work around this, I added terns containing the placeholders %f, %p, and %P command-line options to alter the representation of which expand to the base name of the DVI file, the glyphs and other graphic components in the gen- current physical page number, and the total number erated SVG files as needed. The following sections of pages in the DVI file, respectively. The command cover some of the challenges involved in this area. dvisvgm --output=%f-%p-%P myfile converts the first page of myfile.dvi to the SVG file 3.1 Vectorization of bitmap fonts myfile-01-20.svg, given that the DVI file contains Today, many popular fonts used in (LA)TEX docu- 20 pages. The number of digits used for %p is adapted ments are available in OpenType, TrueType or Post- to that of %P but can be explicitly determined by a Script Type 1 format, greatly simplifying their con- prepended number, e.g., . Table 1 shows some %4p version to SVG as they are already vectorized. This further examples of specifying the naming scheme of also includes many fonts originally developed with the generated files. More details can be found in the METAFONT 2 , such as the beautiful Old German deco- dvisvgm manual page. rative initials by Yannis Haralambous. However, dur- Because of the lengthy text-based nature of XML ing the first releases of dvisvgm, I regularly stumbled documents, SVG files tend to be bigger than other over documents that could not be converted com- vector graphics formats. To reduce the file size, the pletely because they relied on fonts only available as SVG standard specifies gzip- and deflate-compressed METAFONT source. Some of these were designed by SVG files which normally use the extension .svgz. the document authors to provide special characters To create compressed files on the fly during a DVI or little drawings. conversion, the option --zip is available. The problem is that although METAFONT allows detailed, high-level vectorial descriptions of glyphs, 3 Font support it doesn't create vector but bitmap output, in the In contrast to PostScript and PDF, DVI provides no form of GF (Generic Font) files.