]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
dda7728f25c8fbb40a1b47474bd6fd02066208f9
[lilypond.git] / lily / text-item.cc
1 /*   
2   text-item.cc -- implement Text_item
3
4   source file of the GNU LilyPond music typesetter
5   
6  (c) 1998--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8  */
9
10 #include "debug.hh"
11 #include "text-item.hh"
12 #include "paper-def.hh"
13 #include "lookup.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "main.hh"
17 #include "all-font-metrics.hh"
18 #include "afm.hh"
19
20 /*
21   text: string | (markup sentence)
22   markup: markup-symbol | (markup-symbol . parameter)
23   sentence: text | sentence text
24   
25
26   Properties:
27
28   * Font:
29   ---* Type:
30   ------* Series: medium, bold
31   ------* Shape: upright, italic, slanted
32   ------* Family: roman, music, orator, typewriter
33
34   ---* Size:
35   ------* size: ...,-2,-1,0,1,2,... (style-sheet -> cmrXX, fetaXX)
36   ------* points: 11,13,16,20,23,26 (for feta)
37   ------* magnification: UNSIGNED
38
39   * Typesetting:
40   ---* kern: INT (staff-space)
41   ---* align: horizontal/vertical / lines / rows
42  */
43 Molecule
44 Text_item::text2molecule (Score_element *me, SCM text, SCM properties) 
45 {
46   if (gh_string_p (text))
47     return string2molecule (me, text, properties);
48   else if (gh_list_p (text))
49     {
50       if (!gh_pair_p (gh_car (text)) && gh_string_p (gh_car (text)))
51         return string2molecule (me, gh_car (text), properties);
52       else
53         return markup_sentence2molecule (me, text, properties);
54     }
55   return Molecule ();
56 }
57
58 Molecule
59 Text_item::string2molecule (Score_element *me, SCM text, SCM properties)
60 {
61   SCM style = scm_assoc (ly_symbol2scm ("font-style"), properties);
62   SCM paper = me->get_elt_property ("style-sheet");
63   SCM font_name;
64   if (gh_pair_p (style))
65     {
66       SCM f = me->get_elt_property ("style-to-font-name");
67       font_name = gh_call2 (f, paper, gh_cdr (style));
68     }
69   else
70     {
71       SCM f = me->get_elt_property ("properties-to-font-name");
72       font_name = gh_call2 (f, paper, properties);
73     }
74    
75   // should move fallback to scm
76   if (!gh_string_p (font_name))
77     font_name = ly_str02scm ("cmr10");
78     
79   SCM lookup = scm_assoc (ly_symbol2scm ("lookup"), properties);
80
81   Molecule mol;
82   if (gh_pair_p (lookup) && ly_symbol2string (gh_cdr (lookup)) == "name")
83     mol = lookup_character (me, font_name, text);
84   else
85     mol = lookup_text (me, font_name, text);
86   
87   return mol;
88 }
89
90 /*
91   caching / use some form of Lookup without 'paper'?
92 */
93 Molecule
94 Text_item::lookup_character (Score_element *me, SCM font_name, SCM char_name)
95 {
96   Adobe_font_metric *afm = all_fonts_global_p->find_afm (ly_scm2string (font_name));
97
98   if (!afm)
99     {
100       warning (_f ("can't find font: `%s'", ly_scm2string (font_name)));
101       warning (_f ("(search path: `%s')", global_path.str ().ch_C()));
102       error (_ ("Aborting"));
103     }
104   
105   AFM_CharMetricInfo const *metric =
106     afm->find_char_metric (ly_scm2string (char_name), true);
107
108   if (!metric)
109     {
110       Molecule m;
111       m.set_empty (false);
112       return m;
113     }
114
115   SCM list = gh_list (ly_symbol2scm ("char"),
116                       gh_int2scm (metric->code),
117                       SCM_UNDEFINED);
118   
119   list = fontify_atom (afm, list);
120   return Molecule (afm_bbox_to_box (metric->charBBox), list);
121 }
122
123 Molecule
124 Text_item::lookup_text (Score_element *me, SCM font_name, SCM text)
125 {
126   SCM magnification = me->get_elt_property ("font-magnification");
127   Font_metric* metric = 0;
128   if (gh_number_p (magnification))
129     metric = all_fonts_global_p->find_scaled (ly_scm2string (font_name),
130                                               gh_scm2int (magnification));
131   else
132     metric = all_fonts_global_p->find_font (ly_scm2string (font_name));
133   
134   SCM list = gh_list (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
135   list = fontify_atom (metric, list);
136   
137   return Molecule (metric->text_dimension (ly_scm2string (text)), list);
138 }
139
140 Molecule
141 Text_item::markup_sentence2molecule (Score_element *me, SCM markup_sentence,
142                                      SCM properties)
143 {
144   SCM markup = gh_car (markup_sentence);
145   SCM sentence = gh_cdr (markup_sentence);
146   SCM f = me->get_elt_property ("markup-to-properties");
147   
148   SCM p = gh_cons (gh_call1 (f, markup), properties);
149
150   Axis align = X_AXIS;
151   SCM a = scm_assoc (ly_symbol2scm ("align"), p);
152   if (gh_pair_p (a) && gh_number_p (gh_cdr (a)))
153     align = (Axis)gh_scm2int (gh_cdr (a));
154
155   Molecule mol;
156   while (gh_pair_p (sentence))
157     {
158       Molecule m = text2molecule (me, gh_car (sentence), p);
159       if (!m.empty_b ())
160         mol.add_at_edge (align, align == X_AXIS ? RIGHT : DOWN, m, 0);
161       sentence = gh_cdr (sentence);
162     }
163   return mol;
164 }
165
166 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
167 SCM 
168 Text_item::brew_molecule (SCM smob) 
169 {
170   Score_element *me = unsmob_element (smob);
171   
172   SCM text = me->get_elt_property ("scm-text");
173   Molecule mol;
174   if (text == SCM_EOL)
175     {
176       SCM style = me->get_elt_property ("style");
177       String st = gh_string_p (style) ?  ly_scm2string (style) : "";
178       SCM text = me->get_elt_property ("text");
179       String t = gh_string_p (text) ? ly_scm2string (text) : "";
180       
181       mol = me->paper_l ()->lookup_l (0)->text (st, t, me->paper_l ());
182     }
183   else
184     mol = text2molecule (me, text,
185                          gh_append2 (me->immutable_property_alist_,
186                                      me->mutable_property_alist_));
187
188   SCM space = me->get_elt_property ("word-space");
189   if (gh_number_p (space))
190     {
191       Molecule m;
192       m.set_empty (false);
193       mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
194                        * Staff_symbol_referencer::staff_space (me));
195     }
196   return mol.create_scheme (); 
197 }
198