8.3 Fonts

Fonts in LilyPond are handled by several libraries; two of them are of relevance to the user: FontConfig is used to detect available fonts, and selected fonts are then rendered by Pango to display text strings.

This section shows how to access fonts in LilyPond, and how to change them in scores.


8.3.1 Finding fonts

In addition to any font already installed on the operating system, more fonts may be added to the ones detected by FontConfig (and thus available in LilyPond scores) by the following commands:

#(ly:font-config-add-font "path/to/font-file")
#(ly:font-config-add-directory "path/to/directory/")

Both commands accept either absolute or relative paths, which makes it possible to compile a score on any system by simply distributing the relevant font files together with the LilyPond input files.

To verify that the desired fonts are found by FontConfig, use the command #(ly:font-config-display-fonts), which prints the complete list of available fonts to the console log. It also shows the actual font names to be used with LilyPond; these may differ from the file names themselves. Alternatively, running lilypond -dshow-available-fonts in a terminal has the same effect.


8.3.2 Font families

Three generic aliases for text font families2 are available: ‘serif’, ‘sans’, and ‘typewriter’. Depending on the backend, these families get mapped to different font family aliases.

For the svg backend:

generic familySVG font family
serifserif
sanssans-serif
typewritermonospace

‘serif’, ‘sans-serif’, and ‘monospace’ are ‘generic-family’ in SVG and CSS specifications.

For other backends:

generic familydefault font family aliasfont families contained in alias
serifLilyPond SerifC059, Century SchoolBook URW, Century Schoolbook L, TeX Gyre Schola, DejaVu Serif, …, serif
sansLilyPond Sans SerifNimbus Sans, Nimbus Sans L, TeX Gyre Heros, DejaVu Sans, …, sans-serif
typewriterLilyPond MonospaceNimbus Mono PS, Nimbus Mono, Nimbus Mono L, TeX Gyre Cursor, DejaVu Sans Mono, …, monospace

If a character does not exist in the appropriate font of the first listed family, the appropriate font of the next listed family gets used instead for that character.

Note that the URW font families distributed with LilyPond (‘C059’, ‘Nimbus Sans’, and ‘Nimbus Mono PS’) have a peculiarity: By default, in addition to the standard ligatures like ‘fl’ or ‘ffi’, they substitute the string ‘Nr.’ with the Numero Sign (U+2116) if the ‘latn’ script is selected. To circumvent this locally, insert a zero-width non-joiner character (ZWNJ, U+200C) between the ‘N’ and ‘r’ characters. To circumvent this globally, use the following code to make LilyPond always insert a ZWNJ character.

\paper {
  #(add-text-replacements!
      `(("Nr." . ,(format #f "N~ar." (ly:wide-char->utf-8 #x200C)))))
}

‘LilyPond Serif’, ‘LilyPond Sans Serif’, and ‘LilyPond Monospace’ are font family aliases defined in the additional FontConfig configuration file 00-lilypond-fonts.conf, which can be usually found in directory /usr/local/share/lilypond/2.25.27/fonts, and which is used exclusively by LilyPond.

Each font family may include different shapes and series. The following example demonstrates that, including code to also change the size. The value supplied to font-size is taken relative to the default font size.

\override Score.TextMark.font-family = #'typewriter
\textMark "Ouverture"
\override Voice.TextScript.font-shape = #'italic
\override Voice.TextScript.font-series = #'bold
d''2.^\markup "Allegro"
\override Voice.TextScript.font-size = -3
c''4^"smaller"
[image of music]

A similar syntax may be used in markup mode; however, in most cases it is preferable to use the simpler syntax explained in Selecting font and font size:

\markup {
  \column {
    \line {
      \override #'((font-shape . italic) (font-size . 4))
      Idomeneo,
    }
    \line {
      \override #'(font-family . typewriter) {
        \override #'(font-series . bold) re
        di
      }
      \override #'(font-family . sans) Creta
    }
  }
}
[image of music]

8.3.3 Font features

When using OpenType fonts, font features can be used.3 Note that not all OpenType fonts have all features. If you request a feature that does not exist in the chosen font, the feature is simply ignored. The example below uses the font ‘TeX Gyre Schola’ (this is, the roman style of the family).

\paper {
  property-defaults.fonts.serif = "TeX Gyre Schola"
}

\markup "normal style: Hello HELLO"

\markup \caps "small caps: Hello"

\markup \override #'(font-features . ("smcp")) "true small caps: Hello"

\markup "normal number style: 0123456789"

\markup \override #'(font-features . ("onum"))
  "old number style: 0123456789"

\markup \override #'(font-features . ("salt 0"))
  "stylistic alternate 0: εφπρθ"

\markup \override #'(font-features . ("salt 1"))
  "stylistic alternate 1: εφπρθ"

\markup \override #'(font-features . ("onum" "smcp" "salt 1"))
  "multiple features: Hello 0123456789 εφπρθ"
[image of music]

For the full OpenType font feature list see https://www.microsoft.com/typography/otspec/featurelist.htm; for identifying features of OpenType fonts see https://lists.gnu.org/archive/html/lilypond-devel/2017-08/msg00004.html.

See also

Notation Reference: The Emmentaler font, Music notation inside markup, Rotating objects, Selecting font and font size, Font markup.

See also

Notation Reference: Finding fonts, Font families, Changing fonts.

Snippets: Text.


8.3.4 Changing fonts

It is possible to change the fonts used in LilyPond’s default font families.

\paper {
  property-defaults.fonts.serif = "Linux Libertine O"
  property-defaults.fonts.sans = "DejaVu Sans"
  property-defaults.fonts.typewriter = "DejaVu Sans Mono"
}

\relative c'{
  c1-\markup {
    serif,
    \sans sans,
    \typewriter typewriter. }
}
[image of music]

The same syntax can be used to change the music font; see Replacing the notation font.

To change the fonts used for one specific grob, or one specific part of a markup, override the fonts property. The following example changes the font for normal tablature “note heads” (which use the serif family) while keeping the default font for those that are drawn as a cross (which use the music family).

\layout {
  \override TabVoice.TabNoteHead.property-defaults.fonts.serif =
    "Linux Libertine O"
}

\new TabStaff { c' d' e' \deadNote c' }
[image of music]

Below is an example for overriding fonts in markup:

\markup \override #'(fonts . ((serif . "Linux Libertine O")
                              (typewriter . "DejaVu Sans Mono"))) {
  Copyright © John Doe \typewriter john@doe.org
}
[image of music]

See also

Notation Reference: Finding fonts, Font families, Selecting font and font size, Font markup, Replacing the notation font.


Footnotes

(2)

In its simplest form, a font family usually contains fonts in roman, italic, bold, and bold italic styles.

(3)

Selecting OpenType font scripts and languages is not supported yet.


LilyPond Notation Reference v2.25.27 (development-branch).