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