[Tex/LaTex] LuaLaTeX: Font table with examples of font files (not system or TEXMF installed)

fontsfontspecluatex

Looking at the answer to the question here, it is very close to what I wanted. What I wanted is to create font table with samples for font files that are not system installed or in TEXMF trees, but font files that reside in a path or on CD (no sub-folders, just the current folder will do).

I am still learning and appreciate your help!

Using MiKTeX 2.9 and Windows 7, with LuaLaTeX.


EDIT:

Inspired by Taco's answer, I have come out with the following (modified from Leo's Code):

\documentclass{article}
\usepackage{fontspec}
\setmainfont{Latin Modern Mono Light}
\usepackage{luacode}
\usepackage[margin=18mm]{geometry}
\parindent=0pt
\usepackage{longtable,makecell}
\renewcommand\arraystretch{2}
\begin{document}
\begin{luacode}
teststring = "0123456789 ABCDEFabcdef"
tex.print(-2, os.getenv('FONTSPEC'))
tex.print('\\\\')
tex.print("\\begin{longtable}{ll}\\hline")
for i,v in ipairs(dir.glob(os.getenv('FONTSPEC'))) do
  -- get rid of './' in front of filenames, fontspec don't like that
  local f = string.gsub(v, "^./", "")
  -- font name
  local info = fontloader.info(v)
  tex.print('\\makecell[l]{\\bfseries')
  tex.print(-2, info.fontname) -- font name here
  tex.print('\\\\[-1ex] \\scriptsize')
  tex.print(-2, f) -- filename
  tex.print('} & \\large\\fontspec{')
  tex.print(-2, f)
  tex.print('}')
  tex.print(teststring)
  tex.print('\\\\ \\hline')
end
tex.print("\\end{longtable}")
\end{luacode}
\end{document}

It utilises an environment variable, FONTSPEC to specify the font files search pattern, eg. *.ttf for TrueType files in the current directory. Note that the TEX file needs to be at the same directory where the font files is and the glob pattern is CASE SENSITIVE!

Sample run:

set FONTSPEC=*.ttf
lualatex fontsampler.ltx

Result:
Font Sampler Example

Best Answer

I am a context user and I am sure I could come up with a macro-based context hack to do what you want in an hour or so if I tried hard enough. Because of that, I am sure similar hackery can be done by a knowledgeable lualatex user, but ... why spend time creating a complicated hack if a trivial script /batch file can do the same simply by running luatex with an adjusted TEXMF or OSFONTDIR environment variable?

In fact, you could probably even change OSFONTDIR on the fly inside the document by putting something like

\directlua { os.setenv('OSFONTDIR', '<foldernamehere>') }

in the document preamble (untested, not behind a pc right now)

Edit: it is even easier than that (at least in ConTeXt). I just tried some simple things, and absolute path names work in a font specification in ConTeXt, so this runs ok for me:

\starttext
\startluacode
context.starttabulate{"|p|"}
for i,v in ipairs(dir.glob('/home/taco/tmp/itc/*.otf')) do
  local info = fontloader.info(v);
  context.NC()
  context(string.format('\\mono{%s}\\crlf %s\\crlf\\definedfont[%s]',v,info.fontname,v))
  context("Sphinx of black quartz, judge my vow.")
  context.NC()
  context.NR()
end
context.stoptabulate()
\stopluacode
\stoptext