]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/internals.itely
patch::: 1.3.126.jcn3
[lilypond.git] / Documentation / user / internals.itely
1 @c -*-texinfo-*-
2 @c Note:
3 @c
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
8
9
10 @node Internals
11 @chapter Internals
12
13 @menu
14 * Conversion stages::           Lilypond is a multi-pass program.
15 * Moment::                      
16 * Grobs::                       Graphical object  
17 * Duration::                    
18 * Pitch data type::             
19 * Engraver::                    
20 * Music_iterator::              
21 * Music::                       
22 * Molecules::                   Molecules are stand-alone descriptions of output
23 * Font metrics::                Font metrics
24 * Miscellaneous Scheme functions::  
25 @end menu
26
27 @node Conversion stages
28 @section Conversion stages
29
30 When translating the input to notation, there are number of distinct
31 phases.  We list them here:
32
33 @table @code
34
35 @item Parsing:
36
37 The .ly file is read, and converted to a list of @code{Scores}, which
38 each contain @code{Music} and paper/midi-definitions.
39
40 @item Interpreting music
41 @cindex interpreting music
42
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,
47 @cindex engraver
48 which use them to build grobs (or MIDI objects, for MIDI output).
49
50 @item Prebreaking
51
52 @cindex prebreaking
53
54 At places where line breaks may occur, clefs and bars are prepared for
55 a possible line break. 
56
57 @item Preprocessing
58
59 @cindex preprocessing
60
61 In this stage, all information that is needed to determine line breaking
62 is computed. 
63
64 @item Break calculation:
65
66 The lines and horizontal positions of the columns are determined.
67
68 @item Breaking
69
70 Relations between all grobs are modified to reflect line breaks. See
71 also @ref{Pointer substitution}.
72
73 @item Outputting:
74
75 All vertical dimensions and spanning objects are computed, and all grobs
76 are output, line by line.
77
78 @end table
79
80 @node Moment
81 @section Moment
82
83 Moment is a rational number. Since GUILE doesn't support them natively,
84 so we created our own rational data type.
85
86 @defun moment?
87 @end defun
88
89 @defun make-moment num den
90 create the rational number @var{num}/@var{den}. 
91 @end defun
92
93 @node Grobs
94 @section Grobs
95
96 This section is about Grobs (short for Graphical Objects), which are
97 formatting objects used to create the final output. This material is
98 normally the domain of LilyPond gurus, but occasionally, a normal user
99 also has to deal with grobs.
100
101 The most simple interaction with Grobs are when you use
102 @code{\override}:
103
104 @example
105         \property Voice.Stem \override #'direction = #1
106 @end example
107
108 This piece of lily input causes all stem objects to be stem-up
109 henceforth.  In effect, you are telling lilypond to extend the defintion
110 of the "Stem" grob with the setting @code{direction := 1}.  Of course
111 there are many more ways of customizing Lily output, and since most of
112 them involve Grobs in some form, this section explains some details of
113 how grobs work.
114
115 @menu
116 * What is a grob?::             
117 * Callbacks::                   
118 * Setting grob properties::     
119 * Items and Spanners::          
120 * Pointer substitution::        
121 * Grob Scheme functions::       
122 @end menu
123
124 @node What is a grob?
125 @subsection What is a grob?
126
127 In music notation, lots of symbols are related in some way.  You can
128 think of music notation as a graph where nodes are formed by the
129 symbols, and the arcs by their relations. A grob is node in that
130 graph. A grob stores references to other grobs, the directed edges in
131 the graph.
132
133 The objective of this big graph of grobs, is to specify the notation
134 problem. The solution of this problem is a description of the printout
135 that is in closed form, i.e. but a list of values.  These values are
136 Molecules. (see @ref{Molecules})
137
138 All grobs have an X and Y-position on the page.  These X and Y positions
139 are stored in a relative format, so they can easily be combined by
140 stacking them, hanging one grob to the side of another, and coupling
141 them into a grouping-grob.
142
143 Each grob has a reference point, or parent: the position of a grob is
144 stored relative to that reference point. For example the X-reference
145 point of a staccato dot usually is the note head that it applies
146 to. Whenever the note head is moved, the staccato dot moves along
147 automatically.
148
149 If you keep following offset reference points, you will always end up at
150 the root-object. This root object is called @rgrob{Line_of_score}, and it
151 represents a system (ie. a line of music).
152
153 All grobs carry a set of grob-properties.  In the Stem example above,
154 the property @code{direction} is set to value @code{1}.  The function
155 that draws the symbol (@code{Stem::brew_molecule}) uses the value of
156 @code{direction} to determine how to print the stem and the flag.  The
157 appearance of a grob is determined solely by the values of its
158 properties.
159
160 Often, a grob also is associated with a symbol. On the other hand,
161 Some grobs do not print any symbols, but take care of grouping
162 objects. For example, there is a separate grob that stacks staffs
163 vertically, so they are not printed in overstrike. The
164 @rgrob{NoteCollision} is another example of an abstract grob.  It only
165 moves around chords, but doesn't print anything.
166
167 A complete list of grob types is found in
168 @ref{(lilypond-internals)LilyPond backend}
169
170 Grobs are created in the "Interpreting music" phase, by things in
171 LilyPond called engravers.  In this phase of the translation, a load of
172 grobs are created, and they are linked into a giant network of objects.
173 This network of grobs forms the "specification" of the print
174 problem. This problem is then solved: configurations, directions,
175 dimensions, line breaks, etc.  are calculated. Finally,   the printing
176 description in the form of Molecules (@ref{Molecules})  is extracted from
177 the network. These are then dumped into the output file
178
179 @node Callbacks
180 @subsection Callbacks
181
182 Offsets of grobs are relative to a parent reference point. Most
183 positions are not known when an object is created, so these are
184 calculated as needed. This is done by adding a callback for a specific
185 direction.
186
187 Suppose you have the following code in a .ly file.
188 @example
189         #(define (my-callback gr axis)
190                 (*  2.0 (get-gr-property grob 'direction))
191         )
192
193 ....
194
195         \property Voice.Stem \override #'Y-offset-callbacks = #(list
196                         my-callback)
197 @end example
198
199 When the Y-offset of a Stem object is needed, LilyPond will
200 automatically execute all callbacks for that object. In this case, it
201 will find @code{my-callback}, and execute that. The result is that the
202 stem is translated by two staff spaces in its direction.
203
204 (note: Y-offset-callbacks is also a property) 
205
206
207 Offset callbacks can be stacked, ie.
208
209 @example
210         \property .... \override #'Y-offset-callbacks = #(list
211                 callback1 callback2 callback3)
212
213 @end example
214
215 The callbacks will be executed in the order callback3 callback2
216 callback1. This is used for quantized positioning: the staccato dot is
217 above or below a note head, and it must not be on a staff-line.
218
219 To achieve this, for the staccato there are two callbacks: one callback
220 that positions the grob above or below the note head, and one callback
221 that rounds the Y-position of the grob to the nearest open space.
222
223 Similarly, the size of a grob are determined through callbacks, settable
224 with grob properties @code{X-extent-callback} and @code{Y-extent-callback}.
225 There can be only one extent-callback for each axis. No callback (value #f)
226 means: "empty in this direction". If you fill in a pair, that pair
227 hard-codes the extent in that coordinate.
228
229
230 @node Setting grob properties
231 @subsection Setting grob properties
232
233 Grob properties are stored as GUILE association lists, with symbols as
234 keys.   From C++, element properties can be accessed using the functions
235
236 @example
237   SCM  get_grob_property (SCM) const;
238   void set_grob_property (const char * , SCM val);
239   void set_immutable_grob_property (const char * , SCM val);
240   void set_immutable_grob_property (SCM key, SCM val);  
241   void set_grob_property (SCM , SCM val);  
242   void set_grob_pointer (const char*, SCM val);
243   SCM  remove_grob_property (const char* nm);
244 @end example
245
246 In GUILE, LilyPond provides
247
248 @example
249         ly-get-grob-property GROB SYMBOL
250         ly-set-grob-property GROB SYMBOL VALUE
251 @end example
252
253 All lookup functions identify undefined properties with 
254 end-of-list (ie. @code{'()} in Scheme or @code{SCM_EOL} in C)
255
256 Properties are stored in two ways:
257 @itemize @bullet
258 @item mutable properties:
259 element properties that change from object to object. The storage of
260 these are private to a grob. Typically this is used to store lists of
261 pointers to other grobs
262
263 @item immutable properties:
264 element properties that are shared across different grobs of the same
265 type. The storage is shared, and hence it is read-only. Typically, this
266 is used to store function callbacks, and values for shared element
267 properties are read from @file{scm/element-description.scm}.
268 @end itemize
269
270 There are two ways to manually set grob properties.
271
272 You can change immutable grob properties. This is done with the
273 \override syntax:
274
275 @example
276         \property Voice.Stem \override #'direction = #1
277 @end example
278
279 This will push the entry @code{'(direction . 1)} on the immutable
280 property list for stems, in effect overriding the setting from
281 @file{scm/element-description.scm}. This can be undone by 
282
283 @example
284         \property Voice.stem \revert #'direction
285 @end example
286
287 If you use this a lot, this gets old quickly. So we also have a
288 shorthand,
289
290 @example
291         \property Context.GrobType \set #'prop = #VAL
292 @end example
293
294 this does a @code{\revert} followed by a @code{\override}
295
296 The second way is \outputproperty. This construct looks like
297
298 @example
299         \context ContextName \outputproperty @var{pred} #@var{sym} = #@var{val}
300 @end example
301
302 In this case, in every grob that satisfies @var{pred}, the property
303 assignment @var{sym} = @var{val} is done.  For example
304
305 @example
306         \outputproperty
307                 #(lambda (gr) (string? (ly-get-grob-property gr
308                         'text)))
309                 #'extra-offset = #'(-1.0 . 0.0)
310 @end example
311
312 This shifts all elements that have a @code{text} property one staff
313 space to the left. This mechanism is rather clumsy to use, but it allows
314 you tweak any setting of any grob.
315
316 @node Items and Spanners
317 @unnumberedsubsec Items and Spanners
318
319 Grobs can also be distinguished in their role in the horizontal spacing.
320 A lot of grobs define constraints on the spacing by their sizes. For
321 example, note heads, clefs, stems, and all other symbols with a fixed
322 shape.  These grobs form a subtype called @code{Item}.
323
324 Other grobs have a shape that depends on the horizontal spacing. For
325 example, slur, beam, tie, etc. These grobs form a subtype called
326 @code{Spanner}. All spanners have two span-points (these must be
327 @code{Item}s), one on the left and one on the right. The left bound is
328 also the X-reference point.
329
330 Some items need special treatment for line breaking. For example, a
331 clef is normally only printed at the start of a line (ie. after a line
332 break).  To model this, `breakable' items (clef, key signature, bar lines,
333 etc.) are copied twice. Then we have three versions of each breakable
334 item: one version if there is no line break, one version that is printed
335 before the line break (at the end of a system), one version that is
336 printed after the line break.
337
338 Whether these versions are visible and take up space, is determined by
339 the outcome of the visibility-lambda. This is a function taking a
340 direction (-1, 0 or 1) and returns a cons of booleans, signifying wether
341 this grob should be transparent and have no extent.
342
343 @node Pointer substitution
344 @unnumberedsubsec Pointer substitution
345
346
347 Symbols that cross line-breaks (such as slurs) cause some more
348 complications. When a  spanner crosses a line-break, then the spanner is
349 "broken into pieces", for every line that the spanner is in, a copy of
350 the grob is made. A substitution process redirects all grob-reference
351 so that spanner grob will only reference other grobs in the same line.
352
353 @node Grob Scheme functions
354 @unnumberedsubsec Grob Scheme functions
355
356
357 @defun ly-get-grob-property g sym
358   Get the value of a value in grob @var{g} of property @var{sym}. It
359 will return @code{'()} (end-of-list) if @var{g} doesn't have @var{sym} set.
360 @end  defun
361
362 @defun ly-set-grob-property g sym val
363 @end defun
364
365 @defun ly-get-spanner-bound spanner dir
366 @end defun
367
368 @defun ly-grob? g
369 @end defun
370
371 @node Duration
372 @section Duration
373
374 @menu
375 @end menu
376 @defun make-duration length dotcount
377
378 @var{length} is the negative logarithm (base 2) of the duration:
379 1 is a half note, 2 is a quarter note, 3 is an eighth
380 note, etc.  The number of dots after the note is given by
381 @var{dotcount}.
382
383 @end defun
384
385
386 @node Pitch data type
387 @section Pitch data type
388
389
390
391 @defun make-pitch octave note shift
392
393 @var{octave} is specified by an integer, zero for the octave
394 containing middle C.  @var{note} is a number from 0 to 7, with 0
395 corresponding to C and 7 corresponding to B.  The shift is zero for a
396 natural, negative to add flats, or positive to add sharps.
397 @end defun
398
399 @defun pitch-octave
400 @end defun
401
402 @defun pitch-notename
403 @end defun
404
405 @defun pitch-alteration
406 @end defun
407
408 @defun pitch-semitones
409 @end defun
410
411 @defun Pitch::transpose
412 @end defun
413
414 @node Engraver
415 @section Engraver
416
417 @defun ly-get-trans-property
418 @end defun
419
420 @defun ly-set-trans-property
421 @end defun
422
423 @node Music_iterator
424 @section Music_iterator
425
426 @defun c++-function?
427 @end defun
428
429 @node Music
430 @section Music
431
432 @defun ly-get-mus-property
433 @end defun
434
435 @defun ly-set-mus-property
436 @end defun
437
438
439 @node Molecules
440 @section Molecules
441
442 @cindex Molecule
443 @cindex Atom
444 @cindex Output description
445
446 The objective of any typesetting system is to put ink on paper in the
447 right places. For LilyPond, this final stage is left to the TeX and the
448 printer subsystem. For lily, the last stage in processing a score is
449 outputting a description of what to put where.  This description roughly
450 looks like
451
452 @example
453         PUT glyph AT (x,y)
454         PUT glyph AT (x,y)
455         PUT glyph AT (x,y) 
456 @end example
457
458 you merely have to look at the tex output of lily to see this.
459 Internally these instructions are encoded in Molecules:@footnote{At some
460 point LilyPond also contained Atom-objects, but they have been replaced
461 by Scheme expressions, making the name outdated.}.  A molecule is an
462 object that combines dimension information (how large is this glyph ?)
463 with what-to-print-where.
464
465 Conceptually, Molecules can be constructed from Scheme code, by
466 translating a Molecule and by combining two molecules. In BNF
467 notation:
468
469 @example
470  Molecule = COMBINE Molecule Molecule
471            | TRANSLATE Offset Molecule
472            | GLYPH-DESCRIPTION
473            ;
474 @end example
475
476 If you are interested in seeing how this information is stored, you
477 can run with the @code{-f scm} option. The scheme expressions are then
478 dumped onto the output file.
479
480 (refer to the C++ code for more details). All visible,
481 ie. non-transparant, grobs have a callback to create a Molecule. The
482 name of the property is @code{molecule-callback}, and its value should
483 be a Scheme function taking one argument (the grob) and returning a
484 Molecule.
485
486 @defun molecule? m
487 @end defun
488
489 @defun ly-combine-molecule-at-edge  mol1 axis dir mol2 padding
490 @end defun
491
492 @defun ly-get-molecule-extent! mol axis
493 @end defun
494
495 @defun ly-set-molecule-extent! mol axis extent
496 @end defun
497
498 @node Font metrics
499 @section Font metrics
500
501 The font object represents the metric information of a font. Every font
502 that is loaded into LilyPond can be accessed via Scheme. 
503
504 LilyPond only needs to know the dimension of glyph to be able to process
505 them. This information is present in font-metric files. LilyPond can
506 read two types of font-metrics: @TeX{} Font Metric files (tfm files) and
507 Adobe Font Metric files (afm files).  AFM files are more versatile, and
508 LilyPond needs those features to typeset musical symbols. So LilyPond
509 will always try to load afm files first. 
510
511
512 @defun ly-get-default-font gr
513 This returns the default font for grob @var{gr}.
514 @end defun
515
516 @defun ly-find-glyph-by-name font name
517 This function retrieves a Molecule for the glyph named @var{name} in
518 @var{font}.  The font must be available as a afm file.
519 @cindex afm file
520
521 @end defun
522
523 @node Miscellaneous Scheme functions
524 @section Miscellaneous Scheme functions
525
526 @defun ly-input-location?
527 @end defun
528
529 @defun ly-warn
530 @end defun
531
532 @defun ly-version
533 @end defun
534
535 @defun ly-gulp-file
536 @end defun
537
538 @defun dir?
539 @end defun
540
541 @defun ly-number->string
542 @end defun
543
544