[Tex/LaTex] How to get a list of all font features supported by current font

fontsluatexopentype

With LuaTeX, how do I get a list of OTF features supported by the current font in use printed?

For example, with otfinfo, I can get a list of OTF features by otfinfo -f font_file_name. Can I do anything similar with LuaTeX?

The goal is to produce such a list for each font I tested. For example, I would like to have such a macro,

\def\listfontfeatures{
 \directlua{... some lua code to list font features to a string, say ff ...}
 \directlua{tex.print(ff)}
}

And use it like the following,

Times Ten LT Std: \fontspec{Times Ten LT Std} \listfontfeatures \par
Palatino LT Std:  \fontspec{Palatino LT Std}  \listfontfeatures \par
Minion Pro:       \fontspec{Minion Pro}       \listfontfeatures \par

Update 12/01/2017

Here's just a small improvement to michal.h21's answer, just in case anyone will be interested.

fontspec_if_feature actually tests font features for the currently selected font family instead of the currently selected font. Normally this makes little difference and I only noticed this recently. For some fonts, different shapes have different features. For example, the italics may have swashes while the roman fonts lack it.

Below is a few macros that I now use for font feature test. It only work for LuaTeX. some modifications to the FontName macro is needed to make it to work with XeTeX.

\ExplSyntaxOff % remove the necessity to use ~ for space
\NewDocumentCommand\FontName{}{%
  \directlua{
    local i = font.current()
    local f = font.getfont(i)
    local n = f.fullname
    tex.sprint(n)
  }%
}
\ExplSyntaxOn

% Setup a new font family whose "n/m" shape is the current font.
% Limitations: The contents of the true/false clauses will be
% set in current fonts without any features selected by default,
% and it will be set in a group. For myself, this was intentionally.
\NewDocumentCommand\FontFeatureTF{m +m +m}{
  \group_begin:
  \fontspec_set_family:Nnn\thisfont:{}{\FontName}
  \fontfamily\thisfont:\selectfont
  \fontspec_if_feature:nTF{#1}{#2}{#3}
  \group_end:
}

\NewDocumentCommand\FontFeatureT{m +m}{
  \FontFeatureTF{#1}{#2}{\prg_do_nothing:}
}

\NewDocumentCommand\FontFeatureF{m +m}{
  \FontFeatureTF{#1}{\prg_do_nothing:}{#2}
}

Best Answer

You can use fontspec function \fontspec_if_feature:nTF. It will work in both xelatex and lualatex

\documentclass{article}
\usepackage{fontspec,etoolbox,expl3}
\setmainfont{Cambria}
\def\fontfeatures{abvf, abvm, abvs, aalt, akhn, nalt, halt, afrc, valt, vhal, blwf, blwm, blws, cpsp, c2pc, c2sc, case, cpct, cv01, cv02, cv03, cv04, cv05, cv06, cv07, cv08, cv09, cv10, cv11, cv12, cv13, cv14, cv15, cv16, cv17, cv18, cv19, cv20, cv21, cv22, cv23, cv24, cv25, cv26, cv27, cv28, cv29, cv30, cv31, cv32, cv33, cv34, cv35, cv36, cv37, cv38, cv39, cv40, cv41, cv42, cv43, cv44, cv45, cv46, cv47, cv48, cv49, cv50, cv51, cv52, cv53, cv54, cv55, cv56, cv57, cv58, cv59, cv60, cv61, cv62, cv63, cv64, cv65, cv66, cv67, cv68, cv69, cv70, cv71, cv72, cv73, cv74, cv75, cv76, cv77, cv78, cv79, cv80, cv81, cv82, cv83, cv84, cv85, cv86, cv87, cv88, cv89, cv90, cv91, cv92, cv93, cv94, cv95, cv96, cv97, cv98, cv99, cjct, cfar, calt, clig, cswh, curs, dnom, dlig, dist, expt, falt, frac, fwid, ccmp, haln, half, hwid, hngl, hist, hlig, hkna, hojo, init, isol, ital, jp04, jp78, jp83, jp90, jalt, kern, lnum, ljmo, lfbd, ltra, ltrm, locl, mark, mset, mkmk, mgrk, medi, med2, nlck, nukt, numr, onum, opbd, size, ordn, ornm, pcap, pref, pres, psts, pstf, palt, vpal, pnum, pkna, pwid, qwid, rand, rkrf, rphf, rlig, rtbd, rtla, rtlm, ruby, sinf, smpl, zero, smcp, liga, salt, ss01, ss02, ss03, ss04, ss05, ss06, ss07, ss08, ss09, ss10, ss11, ss12, ss13, ss14, ss15, ss16, ss17, ss18, ss19, ss20, subs, sups, swsh, tnum, fina, fin2, fin3, twid, titl, trad, tnam, tjmo, unic, vatu, vert, vrt2, vkna, vkrn, vjmo}
% see http://en.wikipedia.org/wiki/OpenType_feature_tag_list#OpenType_Typographic_Features for their list 
\ExplSyntaxOn
\newcommand\listfontfeatures{%
\renewcommand*{\do}[1]{
\fontspec_if_feature:nTF {##1}{##1,\ }{}
}
\expandafter\docsvlist\expandafter{\fontfeatures}
}
\ExplSyntaxOff
\begin{document}
\listfontfeatures
\end{document}