On 2020-02-05 3:34 pm, Paolo Prete wrote:
is it possible to have a text with height set in staff-space units?

For example, I would like that the height of " \markup { foo } " is X * (1
staff-space)
where X can be integer of decimal.

The default font size--11/20 of the staff height--results in an x-height for the default font being just about one staff space. As such, you should only need to use \fontsize with magnification->font-size to adjust the font by a simple scaling factor (i.e. number of staff spaces):

%%%%
\version "2.19.83"
% Make the text x-height about three staff spaces:
\markup \fontsize #(magnification->font-size 3) "hello"
%%%%

As this default behavior is not precise and since fonts vary in their x-height, it may be necessary to measure text to obtain the actual dimensions and scale accordingly:

%%%%
\version "2.19.83"

#(define-markup-command
  (staff-fontsize layout props size arg)
  (number? markup?)
  (let* ((staff-space (ly:output-def-lookup layout 'staff-space 1))
         (sten (interpret-markup layout props (markup "x")))
         (yex (ly:stencil-extent sten Y))
         (height (interval-length yex))
         (magnification (* size (/ staff-space height)))
         (font-size (magnification->font-size magnification)))
    (interpret-markup layout props
      (markup (#:fontsize font-size arg)))))

test =
\new Staff
  \with {
    \omit Clef
    \omit TimeSignature
    \override NoteHead.stencil =
      #(lambda (grob)
        (grob-interpret-markup grob #{
          \markup {
            \staff-fontsize #1 "bxq"
            \staff-fontsize #2 "bxq"
            \staff-fontsize #3 "bxq"
            \staff-fontsize #4 "bxq"
          } #}))
  }
  { e'1 }

\score { \test \layout { #(layout-set-staff-size 12) } }
\score { \test \layout { #(layout-set-staff-size 20) } }
\score { \test \layout { #(layout-set-staff-size 28) } }
%%%%

'x' usually has a flat top and bottom, making it a stable reference. If the x-height is not your interest, you can substitute 'x' for something else. Just be aware that some glyphs often have larger physical dimensions than their ideal bounds as a way to compensate for optic effects. This is most noticeable with rounded or pointed features. If you wanted to measure 'o', for instance, you would need to adjust the height computation:

%%%%
         (height (+ (interval-length yex) (* 2 (car yex))))
%%%%

The principle here is to assume that the extent with which the character descends is the optic adjustment, so we subtract it from the top and bottom to get the effective height. This only works with glyphs that are vertically symmetric in this regard. An 'A', for instance, usually has a flat bottom but the point at the top often extends slightly beyond its nominal height.


-- Aaron Hill

Reply via email to