]> git.donarmstrong.com Git - lilypond.git/blob - lily/lookup.cc
release: 1.1.39
[lilypond.git] / lily / lookup.cc
1 /*
2   lookup.cc -- implement simple Lookup methods.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8   Jan Nieuwenhuizen <janneke@gnu.org>
9
10   TODO
11       Glissando
12
13       merge rulesym and filledbox.
14 */
15
16 #include <ctype.h>
17 #include "lookup.hh"
18 #include "debug.hh"
19 #include "dimensions.hh"
20 #include "scalar.hh"
21 #include "paper-def.hh"
22 #include "string-convert.hh"
23 #include "file-path.hh"
24 #include "main.hh"
25 #include "lily-guile.hh"
26 #include "all-fonts.hh"
27 #include "afm.hh"
28 #include "scope.hh"
29 #include "molecule.hh"
30 #include "atom.hh"
31 #include "lily-guile.hh"
32
33 SCM
34 array_to_list (SCM *a , int l)
35 {
36   SCM list = SCM_EOL;
37   for (int i= l; i--;  )
38     {
39       list =  gh_cons (a[i], list);
40     }
41   return list;
42 }
43
44
45 Lookup::Lookup ()
46 {
47   paper_l_ = 0;
48   afm_l_ = 0;  
49 }
50
51 Lookup::Lookup (Lookup const& s)
52 {
53   font_name_ = s.font_name_;
54   paper_l_ = 0;
55   afm_l_ = 0;  
56 }
57
58
59 Molecule
60 Lookup::ledger_line (Interval xwid) const
61 {
62   Molecule end (afm_find ("noteheads-ledgerending"));
63   Interval ed = end.dim_[X_AXIS];
64   xwid = Interval (xwid[LEFT] - ed[LEFT],
65                    xwid[RIGHT] - ed[RIGHT]);
66   Molecule mid = filledbox (Box (xwid, end.dim_[Y_AXIS]));
67   Molecule l (mid);
68
69   Molecule e2 = end;
70   Molecule e1 = end;
71   e1.translate_axis (xwid[RIGHT], X_AXIS);
72   e2.translate_axis (xwid[LEFT], X_AXIS);
73
74   l.add_molecule (e1);
75   l.add_molecule (e2);
76   return l;
77 }
78
79
80 Molecule
81 Lookup::accidental (int j, bool cautionary) const
82 {
83   Molecule m(afm_find (String ("accidentals-") + to_str (j)));
84   if (cautionary) 
85     {
86       Molecule open = afm_find (String ("accidentals-("));
87       Molecule close = afm_find (String ("accidentals-)"));
88       m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
89       m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
90     }
91   return m;
92 }
93
94
95
96 Molecule
97 Lookup::afm_find (String s, bool warn) const
98 {
99   if (!afm_l_)      
100     {
101       Lookup * me =     (Lookup*)(this);
102       me->afm_l_ = all_fonts_global_p->find_afm (font_name_);
103       if (!me->afm_l_)
104         {
105           warning (_f("Can't open `%s'\n", font_name_));
106           warning (_f("Search path %s\n", global_path.str ().ch_C()));
107           error (_f("Aborting"));
108         }
109     }
110   Adobe_font_char_metric cm = afm_l_->find_char (s, warn);
111   Molecule m;
112   if (cm.code () < 0)
113     {
114       /*
115         don't want people relying on this kind of dimension. 
116        */
117       m.set_empty (false);
118       return m;
119     }
120   
121   Atom at (gh_list (char_scm_sym,
122                     gh_int2scm (cm.code ()),
123                     SCM_UNDEFINED));
124   at.font_ = ly_symbol (font_name_.ch_C());
125   m.dim_ = cm.dimensions();
126   m.add_atom (&at);
127   return m;
128 }
129
130 Molecule
131 Lookup::notehead (int j, String type) const
132 {
133   if (j > 2)
134     j = 2;
135
136   return afm_find (String ("noteheads-") + to_str (j) + type);
137 }
138
139 Molecule
140 Lookup::simple_bar (String type, Real h) const
141 {
142   SCM thick = ly_symbol ("barthick_" + type);
143   Real w = 0.1 PT;
144   if (paper_l_->scope_p_->elem_b (thick))
145     {
146       w = paper_l_->get_realvar (thick);
147     }
148
149   return filledbox (Box (Interval(0,w), Interval(-h/2, h/2)));
150 }
151
152   
153 Molecule
154 Lookup::bar (String str, Real h) const
155 {
156   if (str == "[")
157     return staff_bracket (h);
158   else if (str == "{")
159     return staff_brace (h);
160   
161   Real kern = paper_l_->get_var ("bar_kern");
162   Real thinkern = paper_l_->get_var ("bar_thinkern");  
163   Molecule thin = simple_bar ("thin", h);
164   Molecule thick = simple_bar ("thick", h);
165   Molecule colon = afm_find ("dots-repeatcolon");  
166
167   Molecule m;
168   
169   if (str == "")
170     {
171       return fill (Box (Interval(0, 0), Interval (-h/2, h/2)));
172     }
173   if (str == "scorepostbreak")
174     {
175       return simple_bar ("score", h);
176     }
177   else if (str == "|")
178     {
179       return thin;
180     }
181   else if (str == "|.")
182     {
183       m.add_at_edge (X_AXIS, LEFT, thick, 0);      
184       m.add_at_edge (X_AXIS, LEFT, thin, kern);
185     }
186   else if (str == ".|")
187     {
188       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
189       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
190     }
191   else if (str == ":|")
192     {
193       m.add_at_edge (X_AXIS, LEFT, thick, 0);
194       m.add_at_edge (X_AXIS, LEFT, thin, kern);
195       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
196     }
197   else if (str == "|:")
198     {
199       m.add_at_edge (X_AXIS, RIGHT, thick, 0);
200       m.add_at_edge (X_AXIS, RIGHT, thin, kern);
201       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
202     }
203   else if (str == ":|:")
204     {
205       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
206       m.add_at_edge (X_AXIS, LEFT, colon, kern);      
207       m.add_at_edge (X_AXIS, RIGHT, thick, kern);
208       m.add_at_edge (X_AXIS, RIGHT, colon, kern);      
209     }
210   else if (str == ".|.")
211     {
212       m.add_at_edge (X_AXIS, LEFT, thick, thinkern);
213       m.add_at_edge (X_AXIS, RIGHT, thick, kern);      
214     }
215   else if (str == "||")
216     {
217       m.add_at_edge (X_AXIS, RIGHT, thin, 0);
218       m.add_at_edge (X_AXIS, RIGHT, thin, thinkern);      
219     }
220
221   return m;
222 }
223
224 Molecule 
225 Lookup::beam (Real slope, Real width, Real thick) const
226 {
227   Real height = slope * width; 
228   Real min_y = (0 <? height) - thick/2;
229   Real max_y = (0 >? height) + thick/2;
230
231   
232   Molecule m;
233   Atom at
234      (gh_list (beam_scm_sym,
235                                 gh_double2scm (width),
236                                 gh_double2scm (slope),
237                                 gh_double2scm (thick),
238                                 SCM_UNDEFINED));
239
240   m.dim_[X_AXIS] = Interval (0, width);
241   m.dim_[Y_AXIS] = Interval (min_y, max_y);
242   m.add_atom (&at);
243   return m;
244 }
245
246 Molecule
247 Lookup::clef (String st) const
248 {
249   return afm_find (String ("clefs-" + st));
250 }
251
252 SCM
253 offset2scm (Offset o)
254 {
255   return gh_list (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]),
256                   SCM_UNDEFINED);
257 }
258
259 Molecule
260 Lookup::dashed_slur (Array<Offset> controls, Real thick, Real dash) const
261 {
262   assert (controls.size () == 8);
263   Offset d = controls[3] - controls[0];
264   
265   Real dx = d[X_AXIS];
266   Real dy = d[Y_AXIS];
267
268   Molecule m;
269
270
271   m.dim_[X_AXIS] = Interval (0, dx);
272   m.dim_[Y_AXIS] = Interval (0 <? dy, 0 >? dy);
273
274   SCM sc[4];
275   for (int i=0; i<  4; i++)
276     {
277       sc[i] =  offset2scm (controls[i]);
278     }
279
280   Atom at
281     (gh_list (ly_symbol ("dashed-slur"),
282               gh_double2scm (thick), 
283               gh_double2scm (dash),
284               ly_quote_scm (array_to_list (sc, 4)),
285               SCM_UNDEFINED));
286   
287   
288   m.add_atom (&at);
289   return m;
290 }
291
292 Molecule
293 Lookup::dots () const
294 {
295   return afm_find (String ("dots-dot"));
296 }
297
298
299
300 Molecule
301 Lookup::fill (Box b) const
302 {
303   Molecule m;
304   m.dim_ = b;
305   return m;
306 }
307
308 Molecule
309 Lookup::flag (int j, Direction d) const
310 {
311   char c = (d == UP) ? 'u' : 'd';
312   return  afm_find (String ("flags-") + to_str (c) + to_str (j));
313 }
314
315 Molecule
316 Lookup::rest (int j, bool o, String style) const
317 {
318   return afm_find (String ("rests-") + to_str (j) + (o ? "o" : "") + style);
319 }
320
321 Molecule
322 Lookup::rule_symbol (Real height, Real width) const
323 {
324   Atom at  (gh_list (rulesym_scm_sym,
325                      gh_double2scm (height),
326                      gh_double2scm (width),
327                      SCM_UNDEFINED));
328
329   Molecule m;
330   m.dim_.x () = Interval (0, width);
331   m.dim_.y () = Interval (0, height);
332   
333   m.add_atom (&at);
334
335   return m;
336 }
337
338 Molecule
339 Lookup::special_time_signature (String s, int n, int d) const
340 {
341   // First guess: s contains only the signature style
342   String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
343   
344   Molecule m = afm_find (symbolname, false);
345   if (!m.empty_b()) 
346     return m;
347
348   // Second guess: s contains the full signature name
349   m = afm_find ("timesig-"+s, false);
350   if (!m.empty_b ()) 
351     return m;
352
353   // Resort to default layout with numbers
354   return time_signature (n,d);
355 }
356
357 Molecule
358 Lookup::filledbox (Box b ) const
359 {
360   Molecule m;
361   
362   Atom at  (gh_list (filledbox_scm_sym,
363                      gh_double2scm (-b[X_AXIS][LEFT]),
364                      gh_double2scm (b[X_AXIS][RIGHT]),                 
365                      gh_double2scm (-b[Y_AXIS][DOWN]),
366                      gh_double2scm (b[Y_AXIS][UP]),                    
367                      SCM_UNDEFINED));
368
369   m.dim_ = b;
370   m.add_atom (&at);
371   return m;
372 }
373
374 Molecule
375 Lookup::stem (Real y1, Real y2) const
376 {
377   if (y1 > y2)
378     {
379       Real t = y1;
380       y1 = y2;
381       y2 = t;
382     }
383   Real stem_width = paper_l_->get_var ("stemthickness");
384   return filledbox (Box (Interval (-stem_width/2,stem_width/2),
385                          Interval (y1, y2)));
386 }
387
388
389
390 /**
391    Magnification steps.  These are powers of 1.2. The numbers are
392  taken from Knuth's plain.tex: */
393 static Real mag_steps[] = {1, 1, 1.200, 1.440, 1.7280,  2.074, 2.488};
394
395
396
397 Molecule
398 Lookup::text (String style, String text) const
399 {
400   Molecule m;
401   if (style.empty_b ())
402     style = "roman";
403   
404   int font_mag = 1;
405   Real font_h = paper_l_->get_var ("font_normal");
406   if (paper_l_->scope_p_->elem_b ("font_" + style))
407     {
408       font_h = paper_l_->get_var ("font_" + style);
409     }
410    
411   if (paper_l_->scope_p_->elem_b ("magnification_" + style))
412     {
413       font_mag = (int)paper_l_->get_var ("magnification_" + style);
414     }
415
416   /*
417     UGH.
418   */
419   SCM l = gh_eval_str (("(style-to-cmr \"" + style + "\")").ch_C());
420   if (l != SCM_BOOL_F)
421     {
422       style = ly_scm2string (SCM_CDR(l)) +to_str  ((int)font_h);
423     }
424
425   Real w = 0;
426   Interval ydims (0,0);
427
428   Font_metric* afm_l = all_fonts_global_p->find_font (style);
429   DOUT << "\nChars: ";
430   
431   for (int i = 0; i < text.length_i (); i++) 
432     {
433       if (text[i]=='\\')
434         for (i++; (i < text.length_i ()) && isalpha(text[i]); i++)
435           ;
436       else
437         {
438           Character_metric *c = afm_l->get_char ((unsigned char)text[i],false);
439
440           w += c->dimensions()[X_AXIS].length ();
441           ydims.unite (c->dimensions()[Y_AXIS]);
442         }
443     }
444
445   if (font_mag > 1 && font_mag < 7 )
446     {
447       /* UGH  */ 
448       style = style + String(" scaled \\magstep ") + to_str (font_mag);
449       w *= mag_steps[font_mag];
450       ydims *= mag_steps[font_mag];
451     }
452
453   DOUT << "\n" << to_str (w) << "\n";
454   m.dim_.x () = Interval (0, w);
455   m.dim_.y () = ydims;
456
457   
458   Atom at  (gh_list (text_scm_sym,
459                      gh_str02scm (text.ch_C()),
460                      SCM_UNDEFINED));
461   at.font_ = ly_symbol (style);
462   
463   m.add_atom (&at);
464   return m;
465 }
466   
467
468 Molecule
469 Lookup::time_signature (int num, int den) const
470 {
471   String sty = "number";
472   Molecule n (text (sty, to_str (num)));
473   Molecule d (text (sty, to_str (den)));
474   n.align_to (X_AXIS, CENTER);
475   d.align_to (X_AXIS, CENTER);
476   Molecule m;
477   if (den)
478     {
479       m.add_at_edge (Y_AXIS, UP, n, 0.0);
480       m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
481     }
482   else
483     {
484       m = n;
485       m.align_to (Y_AXIS, CENTER);
486     }
487   return m;
488 }
489
490 Molecule
491 Lookup::staff_brace (Real y) const
492 {
493   Molecule m;
494   
495   Atom at  (gh_list (pianobrace_scm_sym,
496                      gh_double2scm (y),
497                      SCM_UNDEFINED
498                      ));
499   
500   m.dim_[Y_AXIS] = Interval (-y/2,y/2);
501   m.dim_[X_AXIS] = Interval (0,0);
502   m.add_atom (&at);
503   return m;
504 }
505
506 Molecule
507 Lookup::hairpin (Real width, bool decresc, bool continued) const
508 {
509   Molecule m;   
510   Real height = paper_l_->staffheight_f () / 6;
511
512   String hairpin = String (decresc ? "de" : "") + "crescendo";
513   Atom at  (gh_list (ly_symbol (hairpin),
514                      gh_double2scm (width),
515                      gh_double2scm (height),
516                      gh_double2scm (continued ? height/2 : 0.0),
517                      SCM_UNDEFINED));
518   m.dim_.x () = Interval (0, width);
519   m.dim_.y () = Interval (-2*height, 2*height);
520
521   m.add_atom (&at);
522   return m;
523 }
524
525 Molecule
526 Lookup::plet (Real dy , Real dx, Direction dir) const
527 {
528   Molecule m;
529   SCM thick = tuplet_thick_scm_sym;
530   Real t = 0.1 PT;
531   if (paper_l_->scope_p_->elem_b (thick))
532     {
533       t = paper_l_->get_realvar (thick);
534     }
535
536   Real interline_f = paper_l_->get_realvar (interline_scm_sym);
537   Atom at  (gh_list(tuplet_scm_sym,
538                     gh_double2scm (interline_f), 
539                     gh_double2scm (dx),
540                     gh_double2scm (dy),
541                     gh_double2scm (t),
542                     gh_int2scm (dir),
543                     SCM_UNDEFINED));
544   m.add_atom (&at);
545
546   return m;
547 }
548
549 /*
550   Make a smooth curve along the points 
551  */
552 Molecule
553 Lookup::slur (Array<Offset> controls) const
554 {
555   Offset  delta_off = controls[3]- controls[0];
556   Molecule m; 
557
558   SCM scontrols [8];
559   int indices[] = {5,6,7,4,1,2,3,0};
560
561   for (int i= 0; i < 8; i++)
562     scontrols[i] = offset2scm (controls[indices[i]]);
563
564
565   Atom at  (gh_list (ly_symbol ("bezier-sandwich"),
566                      ly_quote_scm (array_to_list (scontrols, 8)),
567                      SCM_UNDEFINED));
568
569   m.dim_[X_AXIS] = Interval (0, delta_off[X_AXIS]);
570   m.dim_[Y_AXIS] = Interval (0 <? delta_off[Y_AXIS], 0 >? delta_off[Y_AXIS]);
571   m.add_atom (&at);
572   return m;
573 }
574
575 Molecule
576 Lookup::staff_bracket (Real y) const
577 {
578   Molecule m; 
579   Atom at  ( gh_list (bracket_scm_sym,
580                       gh_double2scm (y),
581                       SCM_UNDEFINED));
582   m.add_atom (&at);                              
583   m.dim_[Y_AXIS] = Interval (-y/2,y/2);
584   m.dim_[X_AXIS] = Interval (0,4 PT);
585
586   m.translate_axis (- 4. / 3. * m.dim_[X_AXIS].length (), X_AXIS);
587   return m;
588 }
589
590 Molecule
591 Lookup::volta (Real w, bool last_b) const
592 {
593   Molecule m; 
594   SCM thick = volta_thick_scm_sym;
595   Real t = 0.1 PT;
596   if (paper_l_->scope_p_->elem_b (thick))
597     {
598       t = paper_l_->get_realvar (thick);
599     }
600   Atom at  (gh_list (volta_scm_sym,
601                      gh_double2scm (w),
602                      gh_double2scm (t),
603                      gh_int2scm (last_b),
604                      SCM_UNDEFINED));
605
606   Real interline_f = paper_l_->get_realvar (interline_scm_sym);
607
608   m.dim_[Y_AXIS] = Interval (-interline_f, interline_f);
609   m.dim_[X_AXIS] = Interval (0, w);
610
611   m.add_atom (&at);
612   return m;
613 }
614
615