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