6

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

2 Answers2

3

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
Taco Hoekwater
  • 13,724
  • 43
  • 67
3

A bit late, but anyway. A plain LuaTeX solution:

The luafile 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\font\sample={file:]]},{-2,entry},{[[}\sample Sphinx of black quartz, judge my vow.\par]]})
    end
  end 
end

And the small driver file fontsampler.tex:

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

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

\bye

and call with

luatex fontsampler.tex "/my/path/"

This will create a PDF file with font samples from the given dir and below.

topskip
  • 37,020