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

Hans Hagen pragma at wxs.nl
Tue Jan 23 17:47:07 CET 2018


On 1/23/2018 5:06 PM, Urs Liska wrote:
> 
> 
> Am 22.01.2018 um 10:53 schrieb Ulrike Fischer:
>> Am Sun, 21 Jan 2018 22:33:56 +0100 schrieb Urs Liska:
>>
>>>> \csname TU/\rmdefault/m/n\endcsname
>>>>
>>>> and extract the font name "Arial" from the output:
>>>>
>>>>> \TU/Arial(0)/m/n=macro:
>>>> ->\<->"Arial:mode=node;script=latn;language=DFLT;+tlig;" .
>>>>
>>> I was just about to test what you had suggested earlier (didn't have
>>> access to my LaTeX environment over the weekend :-( )
>>>
>>> I have a hard time to understand your code examples. As far as I can see
>>> your original code "shows" the definition of \rmdefault in certain
>>> variants, and it puts in in the log output. If that's correct how would
>>> I get it not to print to the console but be stored somewhere I can
>>> process it further, and how would I process it in a way that LaTeX or
>>> Lua's objects can tell me the "family"?
>> Something like this should work:
>>
>> \documentclass{article}
>> \usepackage{expl3}
>> \usepackage{fontspec}
>> \setmainfont{Arial}
>> \begin{document}
>>
>> \ExplSyntaxOn
>> \newcommand\currentromanfamily{}
>> \cs_generate_variant:Nn \regex_extract_once:nnNTF {no}
>>
>> \tl_set:Nx \l_tmpa_tl { \tl_to_str:c {TU/\rmdefault/m/n}}
>>
>> \regex_extract_once:noNTF
>>   { \"(.+?)\: }
>>   {\l_tmpa_tl}
>>   \l_tmpa_seq
>>   {
>>    \tl_set:Nx\currentromanfamily {\seq_item:Nn \l_tmpa_seq {2}}
>>   }
>>   {
>>    No Match
>>   }
>>
>> \ExplSyntaxOff
>>
>> \currentromanfamily
>>
>> \end{document}
>>
>>
>>
> 
> Indeed this prints the name of the font specified with \setmainfont, but 
> I must admit I don't understand any of that code :-(
> 
> In order for this to work I would need a) either the family name (which 
> it isn't) or the font id (from which I can get to the family and b) all 
> three names in one invocation.
> 
> With the help of Will Robertson I came up with another approach that 
> works - almost.
> WIth the following code I correctly have the three family names stored 
> in a Lua table:
> 
> \documentclass{article}
> \usepackage{fontspec}
> \setmainfont{texgyrepagella-regular.otf}
> \begin{document}
> 
> \sffamily
> 
> We're in Sans
> \begingroup%
>    \rmfamily%
>    \directlua{
>    myfonts = {}
>      myfonts.rm = 
> fonts.hashes.identifiers[font.current()].shared.rawdata.metadata['familyname'] 
> 
>    }%
>    \sffamily%
>    \directlua{
>      myfonts.sf = 
> fonts.hashes.identifiers[font.current()].shared.rawdata.metadata['familyname'] 
> 
>    }%
>    \ttfamily%
>    \directlua{
>      myfonts.tt = 
> fonts.hashes.identifiers[font.current()].shared.rawdata.metadata['familyname'] 
> 
>      print("")
>      print('Main font: '..myfonts.rm)
>      print('Sans font: '..myfonts.sf)
>      print('Mono font: '..myfonts.tt)
>    }%
> \endgroup%
> and we should be back in Sans.
> 
> \end{document}
> 
> This iterates over the three fonts - without actually printing anything 
> - and treats every one as the "current" one.
> Although I must admit this approach is somewhat strage - like having to 
> start a car's engine in order to determine its colour.
> 
> However, one problem is left which I didn't manage to solve: I need all 
> this from within one Lua function. But when I wrap everything in *one* 
> Lua chunk the font selection doesn't seem to work like other 
> tex.print()-ing:
> 
> \begingroup%
>    \rmfamily%
>    \directlua{
>    myfonts = {}
>      tex.print('\noexpand\\rmfamily')
>      myfonts.rm = 
> fonts.hashes.identifiers[font.current()].shared.rawdata.metadata['familyname'] 
> 
>      tex.print('\noexpand\\sffamily')
>      myfonts.sf = 
> fonts.hashes.identifiers[font.current()].shared.rawdata.metadata['familyname'] 
> 
>      tex.print('\noexpand\\ttfamily')
>      myfonts.tt = 
> fonts.hashes.identifiers[font.current()].shared.rawdata.metadata['familyname'] 
> 
>      print("")
>      print('Main font: '..myfonts.rm)
>      print('Sans font: '..myfonts.sf)
>      print('Mono font: '..myfonts.tt)
>    }%
> \endgroup%
> 
> 
> While this compiles the three fonts always show the font that is active 
> before the function starts to execute.
> 
> So is there a way to select one of the three predefined font families 
> *from within Lua* so subsequent statements in the same Lua chunk see the 
> results?
I'm not sure if this thread belongs to this list as it relates more to a 
macro package than to luatex but anyhow, you can do something like this 
(untested):

\begingroup
   \rmfamily \edef\rmfamilyid{\fontid\font}%
   \ssfamily \edef\ssfamilyid{\fontid\font}%
   \ttfamily \edef\ttfamilyid{\fontid\font}%
   \directlua{
     myfonts = { }
     myfonts.rm = 
fonts.hashes.identifiers[\rmfamilyid].shared.rawdata.metadata['familyname']
     myfonts.sf = 
fonts.hashes.identifiers[\ssfamilyid].shared.rawdata.metadata['familyname']
     myfonts.tt = 
fonts.hashes.identifiers[\ttfamilyid].shared.rawdata.metadata['familyname']
     print("")
     print('Main font: '..myfonts.rm)
     print('Sans font: '..myfonts.sf)
     print('Mono font: '..myfonts.tt)
   }%
\endgroup

or just

\begingroup
   \rmfamily \edef\rmfamilyid{\fontid\font}%
   \ssfamily \edef\ssfamilyid{\fontid\font}%
   \ttfamily \edef\ttfamilyid{\fontid\font}%
   \directlua{
     print('Main font: 
'..fonts.hashes.identifiers[\rmfamilyid].shared.rawdata.metadata['familyname'])
     print('Sans font: 
'..fonts.hashes.identifiers[\ssfamilyid].shared.rawdata.metadata['familyname'])
     print('Mono font: 
'..fonts.hashes.identifiers[\ttfamilyid].shared.rawdata.metadata['familyname'])
   }%
\endgroup

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