[luatex] First question: Retrieve *family* name of a font / the three main families

Hans Hagen pragma at wxs.nl
Fri Jan 19 15:00:26 CET 2018


On 1/19/2018 2:37 PM, Urs Liska wrote:
> Hi Knut,
> 
> 
> Am 19.01.2018 um 13:38 schrieb Knut Petersen:
>> Am 19.01.2018 um 09:37 schrieb Urs Liska:
>>> I'm contributing to a LuaLaTeX package 
>>> (https://github.com/jperon/lyluatex) that uses an external program 
>>> (LilyPond) to generate images to be included in the LaTeX document. I 
>>> need to pass the used fonts to LilyPond, and I didn't manage to 
>>> retrieve the necessary information in Lua.
>>>
>>> What I need is
>>>
>>> a) which families are currently set as roman, sans and mono fonts
>>> b) the "family" name of these.  [...]
>>
>> So you want to teach lilypond to use the fonts latex uses.
> 
> Yes, it's awesome when included scores use the same fonts as the 
> surrounding text.
>>
>> In a similar project I use this code fragment to get information about 
>> the current TeX font and to pass it to lilypond:
>>
>>       \else
>>         {
>>           \mdseries\@llftmp
>>           \immediate\write18{
>>             LANG=c
>>             FONT=`echo '\fontname\font'
>>             | sed -e 's/"//g'
>>             | sed -e
>>     's/\@esc[\@esc([[:print:]]*\@esc)\@esc][[:print:]]*/\@esc1/'`
>>             ;
>>             otfinfo --info `kpsewhich $FONT`
>>             | grep "Full name:"
>>             | sed -e 's/Full
>>     name:[[:blank:]]*\@esc([[:print:]]*\@esc)/\@esc\@esc
>>               def \@esc\@esc fontfullname\@cbopen\@esc 1\@cbclose/'
>>             > ourfont.tex}
>>           \input "ourfont.tex"
>>           \@imwrtof{\@ht(define lyinlaFontName "\fontfullname")}
>>           \@imwrtof{\@ht(define lyinlaFontSize (absFontSize \f at size))}
>>         }
>>       \fi
>>
>>
>> \@imwrtof is defined to immediately write to an output file that will 
>> be processed by lilypond,
>> \@esc gives a literal "\",
>> \@ht gives a literal "#",
>> \@cbopen gives a literal "{",
>> \@cbclose gives a  literal "}"
>>
>> Obviously it does not compile, it requires --shell-escape, 
> 
> That's not an issue, the lyluatex package needs that anyway to start 
> LilyPond.
> 
>> and it is not OS-independent. 
> 
> That's probably true.
> 
>> Bt it might be a starting point for you.
>>
>> The idea of the code is to take \fontname\font,
>> to process it as shown,
>> to get the font filename from kpsewhich,
>> and to use otfinfo to get the needed information.
>>
>> The output of otfinfo is filtered and written to a TeX file,
>> that file is read after return from the shell,
>> and two scheme expression (for fontname and fontsize) are
>> written to a file that later will be processed by lilypond.
>>
> 
> I can do that with little effort in the Lua layer (I'm working in anyway):
> 
>   * get the absolute filename from the font object.
>   * run otfinfo -a with that filename
> 
> This should be straightforward to implement but hard to make OS 
> independent. While I personally don't care about that I vividly recall 
> that in an earlier life I felt strongly offended by the attitude "YOU 
> made the mistake of using Windows, so don't complain now" ...
> 
> otfinfo seems to be available on Windows and even included in 
> http://w32tex.org/ ,  but I'm not sure if that will be an acceptable 
> dependency. Of course I can start with implementing it for Linux and 
> make the option be limited to that.
> 
> 
> But still I can't really believe that Lua can tell me the 
> postscriptname, "fullname", filename, but not the family.
> 
> Does anybody know if that's a deliberate limitation?
Something like this

\directlua {
     function FontInfoField(id,name)
         local t = fonts.hashes.identifiers[id]
         if t and t.shared and t.shared.rawdata and 
t.shared.rawdata.metadata then
             local s = t.shared.rawdata.metadata[name]
             if t then
                 tex.print(s)
             end
         end
     end
}

\def\CurrentFontName 
{\directlua{tex.sprint(FontInfoField(\fontid\font,"fontname"))}}
\def\CurrentFullName 
{\directlua{tex.sprint(FontInfoField(\fontid\font,"fullname"))}}
\def\CurrentFamily 
{\directlua{tex.sprint(FontInfoField(\fontid\font,"family"))}}
\def\CurrentFamilyName 
{\directlua{tex.sprint(FontInfoField(\fontid\font,"familyname"))}}
\def\CurrentSubFamily 
{\directlua{tex.sprint(FontInfoField(\fontid\font,"subfamily"))}}
\def\CurrentSubFamilyName 
{\directlua{tex.sprint(FontInfoField(\fontid\font,"subfamilyname"))}}
\def\CurrentCompatibleName{\directlua{tex.sprint(FontInfoField(\fontid\font,"compatiblename"))}}

\def\test{%
     \CurrentFontName      \par
     \CurrentFullName      \par
     \CurrentFamily        \par
     \CurrentFamilyName    \par
     \CurrentSubFamily     \par
     \CurrentSubFamilyName \par
     \CurrentCompatibleName\par
}

\test \bf \test

should give

LMRoman10-Regular
LMRoman10-Regular
LM Roman 10
Latin Modern Roman
Regular
10 Regular
LM Roman 10 Regular
LMRoman10-Bold
LMRoman10-Bold
LM Roman 10
Latin Modern Roman
Bold
10 Bold
LM Roman 10 Bold

but it depends on the macro package how you'd really do it (this is a 
somewhat generic approach which might work ok for latex but i didn't 
test it)

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the luatex mailing list