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
  • I've edited my answer to add printing of the fontname. Aug 8, 2011 at 11:39
  • @Taco: Thanks. I've edited my answer to reflect the changes.
    – KF Leong
    Aug 8, 2011 at 12:10

2 Answers 2

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
4
  • That's a very good idea! I will do some research and see if I can come out with a solution or somebody beats me to it... :)
    – KF Leong
    Aug 8, 2011 at 7:22
  • How to deal with spaces in the path?
    – topskip
    Aug 8, 2011 at 9:20
  • @Patrick: complain to Hans, I guess. Aug 8, 2011 at 10:49
  • @Taco: I tried the OSFONTDIR trick, but doesn't seems to work on my side. Please see my answer above for another solution based on your idea of globbing the directory.
    – KF Leong
    Aug 8, 2011 at 11:15
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.

10
  • Also created a wiki page of this: wiki.luatex.org/index.php/Fontsampler
    – topskip
    Aug 9, 2011 at 10:04
  • Good work! I have encountered problem with lua(la)tex with loading TrueType font with filename in Chinese. If I renamed it to ASCII names, then the font will load. Bug in LUAOTFLOAD?
    – KF Leong
    Aug 9, 2011 at 12:25
  • How to get an automated repetition for different trees, e.g. the system and the user tree? Besides that, a wonderfull script.
    – Keks Dose
    Aug 9, 2011 at 12:31
  • One more Question: How to increase the size of the printout, let's say \fontsize=16pt and for mono = 8pt ?
    – Keks Dose
    Aug 9, 2011 at 12:41
  • 1
    @PatrickGundlach: Can you adapt this answer to LuaLaTeX please? It would be great if I can specify the search path inside my tex-file. May 26, 2012 at 17:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .