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