[Tex/LaTex] convert LaTeX symbols into svg files using font files directly

exportfontssvgsymbols

Is there a programmatic way of converting a set of LaTeX symbols into a set of svg files using the font files directly? In particular, I would like to create svg files for some of the symbols that are listed in the comprehensive LaTeX symbol list, i.e., http://anorien.csc.warwick.ac.uk/mirrors/CTAN/info/symbols/comprehensive/symbols-a4.pdf
And if I were to chose one of these packages of symbols and get all the directories and files (atm/, tfm/, etc) from e.g. http://tug.ctan.org/fonts/linearA/, could I use these dirs and files directly and covert all the symbols defined therein to svg.

I suppose I am looking for something like:

 magic_convert_fonts_to_svg --input_dir font_package_dir/ --output all_symbols_in_package.svg  

I understand that there may be more manual ways of doing this involving e.g. dvisvgm (http://dvisvgm.bplaced.net/), but I would like to know if there is a straightforward automatic way of convert from font files directly.

Best Answer

You can loop over all glyphs in the font, print each of them on separate page, generate a dvi file and convert it to svg using dvisvgm. We can automate it using Lua script which takes a font name and some other options as argument. fonttosvg.lua:

kpse.set_program_name "luatex"
local lapp = require "lapp-mk4"

local args = lapp [[
Convert TeX font to SVG
-s,--scale (default 1) Glyph scaling
<fontname> (string)
]]

local fontname = args.fontname

if fontname == nil then
  print "Missing font name"
  os.exit()
end

local latex = io.popen("dviluatex -jobname=".. fontname, "w")

latex:write(string.format([[
\font\sample=%s at 48pt
\nopagenumbers
\count55=0
\newif\ifvalidchar
\loop\ifnum\count55<256
  \validchartrue
  \setbox0=\hbox{\sample\char\count55}
  \ifdim\wd0=0pt\relax
    \ifdim\ht0=0pt\relax
     \ifdim\dp0=0pt\relax
       \global\validcharfalse
     \fi
    \fi
  \fi
  \ifvalidchar
    \box0
    \vfill\eject
  \fi
  \advance\count55 by1\relax
\repeat
\bye
]], fontname))
latex:close()

os.execute(string.format("dvisvgm -b none -t 0,2 -n -p 1- -c %s  %s.dvi", args.scale, fontname))

this script writes plain TeX document which loops over all possible characters in the tfm file and writes each existing character to standalone page. dvisvgm then traces glyph curves (-n option) and save each character in file fontname-pagenumber.svg.

For example if we want to create svgs for linearA font, we can call the script as following:

texlua fonttosvg.lua -s 3 LinearA

LinearA if name of the tfm file provided by the package, -s option is used to scale the svg files. Some resulting glyph:

enter image description here