Fontsampler

From LuaTeXWiki

Sometimes I wish to get a quick overview of all fonts in a directory hierarchy and a small sample of it. Here is how I do this in plain LuaTeX. First, create a small driver file for TeX (save it in fontsampler.tex):

\input luaotfload.sty
\overfullrule 0pt
\font\mono = {file:lmmono8-regular.otf} at 6pt
\parindent 0pt

\def \samplestring {Sphinx of black quartz, judge my vow. 1234567890 äÄöÖüÜ ß !"§\$\%\&()=?}

\directlua{ 
  dofile("fontsampler.lua") 
  fontsampler(arg[2])
}

\bye

This opens a Lua file called dirlist.lua and calls the function fontsampler with the second argument of the command line. The Lua file is a bit more interesting, as it recurses through a directory hierarchy and for every otf-font found, it creates a few lines of TeX code. Save the following file in fontsampler.lua

function dirtree(dir)
  assert(dir and dir ~= "", "directory parameter is missing or empty")
  if string.sub(dir, -1) == "/" then
    dir=string.sub(dir, 1, -2)
  end

  local function yieldtree(dir)
    for entry in lfs.dir(dir) do
      if not entry:match("^%.") then
        entry=dir.."/"..entry
     	  if not lfs.isdir(entry) then
     	    coroutine.yield(entry,lfs.attributes(entry))
     	  end
     	  if lfs.isdir(entry) then
     	    yieldtree(entry)
     	  end
      end
    end
  end

  return coroutine.wrap(function() yieldtree(dir) end)
end


function fontsampler( dir )
  for entry in dirtree(dir) do
    if entry:match(".otf","-4") then
      tex.tprint({[[\mono ]]},{-2,entry},{[[ (]]},{-2,fontloader.info(entry).fontname},{[[)\par\penalty 10000\font\sample={file:]]},{-2,entry},{[[} at 12pt\sample\samplestring\par\penalty 10000\vrule width \hsize height 0.25pt depth 0pt\par]]})
    end
  end 
end

Now run LuaTeX on the command line with the directory as the second argument:

luatex fontsampler.tex "/path/to/my font directory"

The result is a PDF that looks similar to this:

Fontsampleroutput.png