4 @c A menu is needed before every deeper *section nesting of @nodes
5 @c Run M-x texinfo-all-menus-update
6 @c to automagically fill in these menus
7 @c before saving changes
14 * Conversion stages:: Lilypond is a multi-pass program.
16 * Grobs:: Graphical object
22 * Molecules:: Molecules are stand-alone descriptions of output
23 * Font metrics:: Font metrics
24 * Miscellaneous Scheme functions::
27 @node Conversion stages
28 @section Conversion stages
30 When translating the input to notation, there are number of distinct
31 phases. We list them here:
37 The .ly file is read, and converted to a list of @code{Scores}, which
38 each contain @code{Music} and paper/midi-definitions.
40 @item Interpreting music
41 @cindex interpreting music
43 All music events are "read" in the same order as they would be played
44 (or read from paper). At every step of the interpretation, musical
45 events are delivered to
46 interpretation contexts,
48 which use them to build grobs (or MIDI objects, for MIDI output).
54 At places where line breaks may occur, clefs and bars are prepared for
55 a possible line break.
61 In this stage, all information that is needed to determine line breaking
64 @item Break calculation:
66 The lines and horizontal positions of the columns are determined.
70 Relations between all grobs are modified to reflect line breaks: When a
71 spanner, eg. a slur, crosses a line-break, then the spanner is "broken
72 into pieces", for every line that the spanner is in, a copy of the grob
73 is made. A substitution process redirects all grob-reference so that
74 spanner grob will only reference other grobs in the same line.
78 All vertical dimensions and spanning objects are computed, and all grobs
79 are output, line by line.
86 Moment is a rational number. Since GUILE doesn't support them natively,
87 so we created our own rational data type.
92 @defun make-moment num den
93 create the rational number @var{num}/@var{den}.
99 This section is about Grobs (short for Graphical Objects), which are
100 formatting objects used to create the final output. This material is
101 normally the domain of LilyPond gurus, but occasionally, a normal user
102 also has to deal with grobs.
104 The most simple interaction with Grobs are when you use
108 \property Voice.Stem \override #'direction = #1
111 This piece of lily input causes all stem objects to be stem-up
112 henceforth. In effect, you are telling lilypond to extend the defintion
113 of the "Stem" grob with the setting @code{direction := 1}. Of course
114 there are many more ways of customizing Lily output, and since most of
115 them involve Grobs in some form, this section explains some details of
121 * Setting grob properties::
122 * Items and Spanners::
123 * Grob Scheme functions::
128 @node What is a grob?
129 @subsection What is a grob?
131 [TODO: document/explain interfaces]
133 In music notation, lots of symbols are related in some way. You can
134 think of music notation as a graph where nodes are formed by the
135 symbols, and the arcs by their relations. A grob is node in that
136 graph. A grob stores references to other grobs, the directed edges in
139 The objective of this big graph of grobs, is to specify the notation
140 problem. The solution of this problem is a description of the printout
141 that is in closed form, i.e. but a list of values. These values are
142 Molecules. (see @ref{Molecules})
144 All grobs have an X and Y-position on the page. These X and Y positions
145 are stored in a relative format, so they can easily be combined by
146 stacking them, hanging one grob to the side of another, and coupling
147 them into a grouping-grob.
149 Each grob has a reference point, or parent: the position of a grob is
150 stored relative to that reference point. For example the X-reference
151 point of a staccato dot usually is the note head that it applies
152 to. Whenever the note head is moved, the staccato dot moves along
155 If you keep following offset reference points, you will always end up at
156 the root-object. This root object is called @code{Line_of_score}, and it
157 represents a system (ie. a line of music).
159 All grobs carry a set of grob-properties. In the Stem example above,
160 the property @code{direction} is set to value @code{1}. The function
161 that draws the symbol (@code{Stem::brew_molecule}) uses the value of
162 @code{direction} to determine how to print the stem and the flag. The
163 appearance of a grob is determined solely by the values of its
166 Often, a grob also is associated with a symbol. However, some
167 grobs do not print any symbols, but take care of grouping objects. For
168 example, there is a separate grob that stacks staffs vertically, so they
169 are not printed in overstrike. The @code{NoteCollision} is another
170 example of an abstract grob. It only moves around chords, but doesn't
173 A complete list of grob types is found in
174 @ref{(lilypond-internals)LilyPond backend}
176 Grobs are created in the "Interpreting music" phase, by objects in
177 LilyPond called engravers. In this phase of the translation, a load of
178 grobs are created, and they are linked into a giant network of objects.
179 This network of grobs forms the "specification" of the print
180 problem. This problem is then solved: configurations, directions,
181 dimensions, line breaks, etc. are calculated. Finally, the printing
182 description in the form of Molecules (@ref{Molecules}) is extracted from
183 the network. These are then dumped into the output file
186 @subsection Callbacks
188 Offsets of grobs are relative to a parent reference point. Most
189 positions are not known when an object is created, so these are
190 calculated as needed. This is done by adding a callback for a specific
193 Suppose you have the following code in a .ly file.
195 #(define (my-callback gr axis)
196 (* 2.0 (get-grob-property gr 'direction))
201 \property Voice.Stem \override #'Y-offset-callbacks = #(list
205 When the Y-offset of a Stem object is needed, LilyPond will
206 automatically execute all callbacks for that object. In this case, it
207 will find @code{my-callback}, and execute that. The result is that the
208 stem is translated by two staff spaces in its direction.
210 (note: Y-offset-callbacks is also a property)
213 Offset callbacks can be stacked, ie.
216 \property .... \override #'Y-offset-callbacks = #(list
217 callback1 callback2 callback3)
221 The callbacks will be executed in the order callback3 callback2
222 callback1. This is used for quantized positioning: the staccato dot is
223 above or below a note head, and it must not be on a staff-line. To
224 achieve this, for the staccato there are two callbacks: one callback
225 that positions the grob above or below the note head, and one callback
226 that rounds the Y-position of the grob to the nearest open space.
228 Similarly, the size of a grob are determined through callbacks, settable
229 with grob properties @code{X-extent-callback} and
230 @code{Y-extent-callback}. There can be only one extent-callback for
231 each axis. No callback (Scheme value @code{#f}) means: "empty in this
232 direction". If you fill in a pair of numbers, that pair hard-codes the
233 extent in that coordinate.
236 @node Setting grob properties
237 @subsection Setting grob properties
239 Grob properties are stored as GUILE association lists, with symbols as
240 keys. From C++, element properties can be accessed using the functions
243 SCM get_grob_property (SCM) const;
244 void set_grob_property (const char * , SCM val);
245 void set_immutable_grob_property (const char * , SCM val);
246 void set_immutable_grob_property (SCM key, SCM val);
247 void set_grob_property (SCM , SCM val);
248 void set_grob_pointer (const char*, SCM val);
249 SCM remove_grob_property (const char* nm);
252 In GUILE, LilyPond provides
255 ly-get-grob-property GROB SYMBOL
256 ly-set-grob-property GROB SYMBOL VALUE
259 All lookup functions identify undefined properties with
260 end-of-list (ie. @code{'()} in Scheme or @code{SCM_EOL} in C)
262 Properties are stored in two ways:
264 @item mutable properties:
265 element properties that change from object to object. The storage of
266 these are private to a grob. Typically this is used to store lists of
267 pointers to other grobs
269 @item immutable properties:
270 element properties that are shared across different grobs of the same
271 type. The storage is shared, and hence it is read-only. Typically, this
272 is used to store function callbacks, and values for shared element
273 properties are read from @file{scm/element-description.scm}.
276 There are two ways to manually set grob properties.
278 You can change immutable grob properties. This is done with the
282 \property Voice.Stem \override #'direction = #1
285 This will push the entry @code{'(direction . 1)} on the immutable
286 property list for stems, in effect overriding the setting from
287 @file{scm/element-description.scm}. This can be undone by
290 \property Voice.stem \revert #'direction
293 If you use this a lot, this gets old quickly. So we also have a
297 \property Context.GrobType \set #'prop = #VAL
300 this does a @code{\revert} followed by a @code{\override}
302 The second way is \outputproperty. This construct looks like
305 \context ContextName \outputproperty @var{pred} #@var{sym} = #@var{val}
308 In this case, in every grob that satisfies @var{pred}, the property
309 assignment @var{sym} = @var{val} is done. For example
313 #(lambda (gr) (string? (ly-get-grob-property gr
315 #'extra-offset = #'(-1.0 . 0.0)
318 This shifts all elements that have a @code{text} property one staff
319 space to the left. This mechanism is rather clumsy to use, but it allows
320 you tweak any setting of any grob.
322 @node Items and Spanners
323 @unnumberedsubsec Items and Spanners
325 Grobs can also be distinguished in their role in the horizontal spacing.
326 A lot of grobs define constraints on the spacing by their sizes. For
327 example, note heads, clefs, stems, and all other symbols with a fixed
328 shape. These grobs form a subtype called @code{Item}.
330 Other grobs have a shape that depends on the horizontal spacing. For
331 example, slur, beam, tie, etc. These grobs form a subtype called
332 @code{Spanner}. All spanners have two span-points (these must be
333 @code{Item}s), one on the left and one on the right. The left bound is
334 also the X-reference point.
336 Some items need special treatment for line breaking. For example, a
337 clef is normally only printed at the start of a line (ie. after a line
338 break). To model this, `breakable' items (clef, key signature, bar lines,
339 etc.) are copied twice. Then we have three versions of each breakable
340 item: one version if there is no line break, one version that is printed
341 before the line break (at the end of a system), one version that is
342 printed after the line break.
344 Whether these versions are visible and take up space, is determined by
345 the outcome of the visibility-lambda. This is a function taking a
346 direction (-1, 0 or 1) and returns a cons of booleans, signifying wether
347 this grob should be transparent and have no extent.
350 @node Grob Scheme functions
351 @unnumberedsubsec Grob Scheme functions
354 @defun ly-get-grob-property g sym
355 Get the value of a value in grob @var{g} of property @var{sym}. It
356 will return @code{'()} (end-of-list) if @var{g} doesn't have @var{sym} set.
359 @defun ly-set-grob-property g sym val
360 Set @var{sym} in grob @var{g} to value @var{val}
363 @defun ly-get-spanner-bound spanner dir
364 Get one of the bounds of @var{spanner}. @var{dir} may be @code{-1} for
365 left, and @code{1} for right.
369 Typecheck: is @var{g} a grob?
375 @defun make-duration length dotcount
377 @var{length} is the negative logarithm (base 2) of the duration:
378 1 is a half note, 2 is a quarter note, 3 is an eighth
379 note, etc. The number of dots after the note is given by
385 type predicate for Duration
388 @node Pitch data type
389 @section Pitch data type
393 @defun make-pitch octave note shift
395 @var{octave} is specified by an integer, zero for the octave
396 containing middle C. @var{note} is a number from 0 to 7, with 0
397 corresponding to C and 7 corresponding to B. The shift is zero for a
398 natural, negative to add flats, or positive to add sharps.
401 @defun pitch-octave p
402 extract the octave from pitch @var{p}.
405 @defun pitch-notename
406 extract the note name from pitch @var{p}.
409 @defun pitch-alteration
410 extract the alteration from pitch @var{p}.
413 @defun pitch-semitones
414 calculate the number of semitones of @var{p} from central C.
417 @defun Pitch::transpose t p
418 Transpose @var{p} by the amount @var{t}, where @var{t} is the pitch that
419 central C is transposed to.
425 Engravers are building blocks of contexts. They are not yet user accessible.
427 @defun ly-get-trans-property tr sym
428 retrieve the value of @var{sym} from context @var{tr}
431 @defun ly-set-trans-property tr sym val
432 set value of property @var{sym} in context @var{tr} to @var{val}.
436 @section Music_iterator
438 This data-type is a direct hook into some C++ constructor functions. It
439 is not yet user-serviceable.
442 type predicate for c++-function.
448 Music is the data type that music expressions are stored in. The data
449 type does not yet offer many manipulations.
451 @defun ly-get-mus-property m sym
452 Get the property @var{sym} of music expression @var{m}.
455 @defun ly-set-mus-property m sym val
456 Set property @var{sym} in music expression @var{m} to @var{val}.
459 @defun ly-make-music name
460 Make a music object/expression of type @var{name}. Warning: this
461 interface will likely change in the near future.
469 @cindex Output description
471 The objective of any typesetting system is to put ink on paper in the
472 right places. For LilyPond, this final stage is left to the TeX and the
473 printer subsystem. For lily, the last stage in processing a score is
474 outputting a description of what to put where. This description roughly
483 you merely have to look at the tex output of lily to see this.
484 Internally these instructions are encoded in Molecules:@footnote{At some
485 point LilyPond also contained Atom-objects, but they have been replaced
486 by Scheme expressions, making the name outdated.}. A molecule is an
487 object that combines dimension information (how large is this glyph ?)
488 with what-to-print-where.
490 Conceptually, Molecules can be constructed from Scheme code, by
491 translating a Molecule and by combining two molecules. In BNF
495 Molecule = COMBINE Molecule Molecule
496 | TRANSLATE Offset Molecule
501 If you are interested in seeing how this information is stored, you
502 can run with the @code{-f scm} option. The scheme expressions are then
503 dumped onto the output file.
505 (refer to the C++ code for more details). All visible,
506 i.e. non-transparant, grobs have a callback to create a Molecule. The
507 name of the property is @code{molecule-callback}, and its value should
508 be a Scheme function taking one argument (the grob) and returning a
511 [insert example of write your own.]
517 @defun ly-combine-molecule-at-edge mol1 axis dir mol2 padding
518 Construct a molecule by putting @var{mol2} next to
519 @var{mol1}. @var{axis} can be 0 (x-axis) or 1 (y-axis), @var{dir} can be
520 -1 (left or down) or 1 (right or up). @var{padding} specifies extra
521 space to add in between. The unit is global staff space. is the
524 @defun ly-get-molecule-extent! mol axis
525 Return a pair of numbers signifying the extent of @var{mol} in
526 @var{axis} direction (0 or 1 for x and y axis respectively).
529 @defun ly-set-molecule-extent! mol axis extent
530 Set the extent (@var{extent} must be a pair of numbers) of @var{mol} in
531 @var{axis} direction (0 or 1 for x and y axis respectively).
535 @section Font metrics
537 The font object represents the metric information of a font. Every font
538 that is loaded into LilyPond can be accessed via Scheme.
540 LilyPond only needs to know the dimension of glyph to be able to process
541 them. This information is stored in font metric files. LilyPond can
542 read two types of font-metrics: @TeX{} Font Metric files (tfm files) and
543 Adobe Font Metric files (@file{.afm} files). LilyPond will always try
544 to load afm files first since @file{.afm} files are more versatile.
546 @defun ly-get-default-font gr
547 This returns the default font for grob @var{gr}.
550 @defun ly-find-glyph-by-name font name
551 This function retrieves a Molecule for the glyph named @var{name} in
552 @var{font}. The font must be available as a afm file.
557 @node Miscellaneous Scheme functions
558 @section Miscellaneous Scheme functions
560 @defun ly-input-location?
565 Scheme callable function to issue the warning @code{msg}.
569 Return the current lilypond version as a list, e.g.
570 @code{(1 3 127 uu1)}.
573 @defun ly-gulp-file name
574 read file named @var{name}, and return its contents in a string. This
575 uses the lilypond search path.
580 type predicate. A direction is a -1, 0 or 1.
583 @defun ly-number->string num
584 converts @var{num} without generating many decimals. It leaves a space