4

I’d like to write some current settings to a file, using io.savedata. How can I get information about the current font, or even better, the current settings for \rm, \ss and \tt?

EDIT: fontconfig must be able to cope with the font name. I want to pass the font from ConTeXt to GNU LilyPond. See http://wiki.contextgarden.net/LilyPond

5

How can I get information about the current font

The font object contains some more information read from the font file. You can access it in the metadata table e. g.:

\startluacode
  local font_info = function (id)
    local fnt = font.getfont(id)
    context.section { title = fnt.resources.filename }
    local metadata = fnt.shared and fnt.shared.rawdata and fnt.shared.rawdata.metadata
    if not metadata then return end
    local out = function (k)
      local v = metadata [k]
      context (k) context ": "
      context (v) context.endgraf()
    end
    local items = table.keys (metadata)
    context "id: " context (id) context.endgraf()
    for i=1, \letterhash items do out (items [i]) end
  end
  userdata.current_font_info = function () font_info (font.current()) end
\stopluacode

\def \showfontinfo {\ctxlua {userdata.current_font_info ()}}

\setupbodyfont [10pt]
\setuphead [section] [style=\tf]
\starttext
  \showfontinfo \endgraf
  \tt \showfontinfo \endgraf
  \definedfont[file:iwona-regular.otf at 10pt] \showfontinfo \endgraf
  \definedfont[file:antykwatorunska-bold.otf at 10pt] \showfontinfo \endgraf
\stoptext

font info dump

fontconfig must be able to cope with the font name

You can create patterns for fontconfig from the abovementioned table. Information pertaining to the font definition (e. g. the font size in sp) can be accessed in the specification table.

\startluacode

  local get_metadata = function (id)
    local fnt = font.getfont(id)
    if fnt then
      return fnt.shared and fnt.shared.rawdata and fnt.shared.rawdata.metadata
    end
  end

  local get_size = function (id)
    local fnt = font.getfont(id)
    if fnt then return fnt.specification.size end
  end

  local fc_pattern = function (id)
    local metadata = get_metadata (id)
    local size = get_size (id)
    if not metadata then return "nil" end
    local ret = string.format ([[\letterpercent s:\letterpercent s]],
                               metadata.familyname,
                               metadata.subfamily)
    if size then ret = ret .. ":size=" .. tostring(size / 2^16) end
    return ret
  end

  userdata.print_fc_pattern = function () print (fc_pattern (font.current())) end
  userdata.current_fc_pattern = function () return fc_pattern (font.current()) end

\stopluacode

\def \showfcpattern {%
  \fontname \font
  \rightarrow
  “\ctxlua {context (userdata.current_fc_pattern ())}”
  %ctxlua {userdata.print_fc_pattern ()}
  \endgraf
}

\setupbodyfont [10pt]

\starttext
  \showfcpattern \endgraf
  \tt \showfcpattern \endgraf
  \definedfont[file:iwona-regular.otf at 10pt] \showfcpattern \endgraf
  \definedfont[file:antykwatorunska-bold.otf at 10pt] \showfcpattern \endgraf
\stoptext

Combine the information from both tables to create suitable patterns for Fontconfig.

example fc patterns

The strings from above snippet can then be fed into fc-match(1) and the likes and probably Lilypond too:

$ fc-match "Iwona:Regular:size=10"
iwona-regular.otf: "Iwona" "Regular"
$ fc-match "Antykwa Torunska:Bold:size=10"
antykwatorunska-bold.otf: "Antykwa Torunska" "Bold"
$ fc-match "Latin Modern Roman:Regular:size=10"
lmroman10-regular.otf: "Latin Modern Roman" "10 Regular"
3

This is the closest I could get. You have to extract the font ID on the TeX level with \fontid\font and pass it to Lua. There you can then extract the font name and identifier. As I don't know exactly what you want to do with it, parsing and stuff is up to you. The return values of tex.fontname and tex.fontidentifier are regular Lua strings.

\def\getfontnames{%
  \startluacode
    function printothers(text)
      local catcodetableothers = -2
      tex.sprint(catcodetableothers, text)
    end

    context.startTABLE()
      context.NC() context("tex.fontname")
      context.NC() printothers(tex.fontname(\fontid\font))
      context.NC() context.NR()
      context.NC() context("tex.fontidentifier")
      context.NC() printothers(tex.fontidentifier(\fontid\font))
      context.NC() context.NR()
    context.stopTABLE()
  \stopluacode
}

\starttext
\rm\getfontnames

\it\getfontnames

\tt\getfontnames
\stoptext

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.