]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/user/internals.itely
patch::: 1.3.136.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: 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.
75
76 @item Outputting:
77
78 All vertical dimensions and spanning objects are computed, and all grobs
79 are output, line by line.
80
81 @end table
82
83 @node Moment
84 @section Moment
85
86 Moment is a rational number. Since GUILE doesn't support them natively,
87 so we created our own rational data type.
88
89 @defun moment?
90 @end defun
91
92 @defun make-moment num den
93 create the rational number @var{num}/@var{den}. 
94 @end defun
95
96 @node Grobs
97 @section Grobs
98
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.
103
104 The most simple interaction with Grobs are when you use
105 @code{\override}:
106
107 @example
108         \property Voice.Stem \override #'direction = #1
109 @end example
110
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
116 how grobs work.
117
118 @menu
119 * What is a grob?::             
120 * Callbacks::                   
121 * Setting grob properties::     
122 * Items and Spanners::          
123 * Grob Scheme functions::       
124 @end menu
125
126
127
128 @node What is a grob?
129 @subsection What is a grob?
130
131 [TODO: document/explain interfaces]
132
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
137 the graph.
138
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})
143
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.
148
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
153 automatically.
154
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).
158
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
164 properties.
165
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
171 print anything.
172
173 A complete list of grob types is found in 
174 @ref{(lilypond-internals)LilyPond backend}
175
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
184
185 @node Callbacks
186 @subsection Callbacks
187
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
191 direction.
192
193 Suppose you have the following code in a .ly file.
194 @example
195         #(define (my-callback gr axis)
196                 (*  2.0 (get-grob-property gr 'direction))
197         )
198
199 ....
200
201         \property Voice.Stem \override #'Y-offset-callbacks = #(list
202                         my-callback)
203 @end example
204
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.
209
210 (note: Y-offset-callbacks is also a property) 
211
212
213 Offset callbacks can be stacked, ie.
214
215 @example
216         \property .... \override #'Y-offset-callbacks = #(list
217                 callback1 callback2 callback3)
218
219 @end example
220
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.
227
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.
234
235
236 @node Setting grob properties
237 @subsection Setting grob properties
238
239 Grob properties are stored as GUILE association lists, with symbols as
240 keys.   From C++, element properties can be accessed using the functions
241
242 @example
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);
250 @end example
251
252 In GUILE, LilyPond provides
253
254 @example
255         ly-get-grob-property GROB SYMBOL
256         ly-set-grob-property GROB SYMBOL VALUE
257 @end example
258
259 All lookup functions identify undefined properties with 
260 end-of-list (ie. @code{'()} in Scheme or @code{SCM_EOL} in C)
261
262 Properties are stored in two ways:
263 @itemize @bullet
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
268
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}.
274 @end itemize
275
276 There are two ways to manually set grob properties.
277
278 You can change immutable grob properties. This is done with the
279 \override syntax:
280
281 @example
282         \property Voice.Stem \override #'direction = #1
283 @end example
284
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 
288
289 @example
290         \property Voice.stem \revert #'direction
291 @end example
292
293 If you use this a lot, this gets old quickly. So we also have a
294 shorthand,
295
296 @example
297         \property Context.GrobType \set #'prop = #VAL
298 @end example
299
300 this does a @code{\revert} followed by a @code{\override}
301
302 The second way is \outputproperty. This construct looks like
303
304 @example
305         \context ContextName \outputproperty @var{pred} #@var{sym} = #@var{val}
306 @end example
307
308 In this case, in every grob that satisfies @var{pred}, the property
309 assignment @var{sym} = @var{val} is done.  For example
310
311 @example
312         \outputproperty
313                 #(lambda (gr) (string? (ly-get-grob-property gr
314                         'text)))
315                 #'extra-offset = #'(-1.0 . 0.0)
316 @end example
317
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.
321
322 @node Items and Spanners
323 @unnumberedsubsec Items and Spanners
324
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}.
329
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.
335
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.
343
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.
348
349
350 @node Grob Scheme functions
351 @unnumberedsubsec Grob Scheme functions
352
353
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.
357 @end  defun
358
359 @defun ly-set-grob-property g sym val
360 Set @var{sym} in grob @var{g} to value @var{val}
361 @end defun
362
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.
366 @end defun
367
368 @defun ly-grob? g
369 Typecheck: is @var{g} a grob?
370 @end defun
371
372 @node Duration
373 @section Duration
374
375 @defun make-duration length dotcount
376
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
380 @var{dotcount}.
381 @end defun
382
383
384 @defun duration? d
385 type predicate for Duration
386 @end defun
387
388 @node Pitch data type
389 @section Pitch data type
390
391
392
393 @defun make-pitch octave note shift
394
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.
399 @end defun
400
401 @defun pitch-octave p 
402 extract the octave from pitch @var{p}.
403 @end defun
404
405 @defun pitch-notename
406 extract the note name from pitch  @var{p}.
407 @end defun
408
409 @defun pitch-alteration
410 extract the alteration from pitch  @var{p}.
411 @end defun
412
413 @defun pitch-semitones
414 calculate the number of semitones of @var{p} from central C.
415 @end defun
416
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. 
420 @end defun
421
422 @node Engraver
423 @section Engraver
424
425 Engravers are building blocks of contexts. They are not yet user accessible.
426
427 @defun ly-get-trans-property tr sym
428 retrieve the value of @var{sym} from context @var{tr}
429 @end defun
430
431 @defun ly-set-trans-property tr sym val
432 set value of property @var{sym} in context @var{tr} to @var{val}.
433 @end defun
434
435 @node Music_iterator
436 @section Music_iterator
437
438 This data-type is a direct hook into some C++ constructor functions. It
439 is not yet user-serviceable.
440
441 @defun c++-function?
442 type predicate for c++-function.
443 @end defun
444
445 @node Music
446 @section Music
447
448 Music is the data type that music expressions are stored in. The data
449 type does not yet offer many manipulations.
450
451 @defun ly-get-mus-property m sym
452 Get the property @var{sym} of music expression @var{m}.
453 @end defun
454
455 @defun ly-set-mus-property m sym val
456 Set property @var{sym} in music expression @var{m} to @var{val}.
457 @end defun
458
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.
462 @end defun
463
464 @node Molecules
465 @section Molecules
466
467 @cindex Molecule
468 @cindex Atom
469 @cindex Output description
470
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
475 looks like
476
477 @example
478         PUT glyph AT (x,y)
479         PUT glyph AT (x,y)
480         PUT glyph AT (x,y) 
481 @end example
482
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.
489
490 Conceptually, Molecules can be constructed from Scheme code, by
491 translating a Molecule and by combining two molecules. In BNF
492 notation:
493
494 @example
495  Molecule = COMBINE Molecule Molecule
496            | TRANSLATE Offset Molecule
497            | GLYPH-DESCRIPTION
498            ;
499 @end example
500
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.
504
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
509 Molecule.
510
511 [insert example of write your own.]
512
513 @defun molecule? m
514 type predicate.
515 @end defun
516
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
522 @end defun
523
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).
527 @end defun
528
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).
532 @end defun
533
534 @node Font metrics
535 @section Font metrics
536
537 The font object represents the metric information of a font. Every font
538 that is loaded into LilyPond can be accessed via Scheme. 
539
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.
545
546 @defun ly-get-default-font gr
547 This returns the default font for grob @var{gr}.
548 @end defun
549
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.
553 @cindex afm file
554
555 @end defun
556
557 @node Miscellaneous Scheme functions
558 @section Miscellaneous Scheme functions
559
560 @defun ly-input-location?
561 type predicate
562 @end defun
563  
564 @defun ly-warn msg
565 Scheme callable function to issue the warning @code{msg}.
566 @end defun
567
568 @defun ly-version
569 Return the current lilypond version as a list, e.g.
570 @code{(1 3 127 uu1)}. 
571 @end defun
572
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.
576
577 @end defun
578
579 @defun dir?
580 type predicate. A direction is a -1, 0 or 1.
581 @end defun
582
583 @defun ly-number->string num
584  converts @var{num} without generating many decimals. It leaves a space
585 at the end.
586 @end defun
587
588