]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-interface.cc
patch::: 1.3.136.jcn3
[lilypond.git] / lily / font-interface.cc
1 /*   
2   font-interface.cc --  implement Font_interface
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "all-font-metrics.hh"
11 #include "font-metric.hh"
12 #include "font-interface.hh"
13 #include "grob.hh"
14 #include "paper-def.hh"
15 #include "warn.hh"
16
17
18 SCM
19 Font_interface::font_alist_chain (Grob *me)
20 {
21   SCM defaults = gh_cdr (scm_assoc (ly_symbol2scm ("font-defaults"),
22                                     me->paper_l ()->style_sheet_));
23
24   SCM ch = gh_list (me->mutable_property_alist_,
25                     me->immutable_property_alist_,
26                     defaults,
27                     SCM_UNDEFINED);
28
29   return ch;
30 }
31
32 /*
33   todo: split up this func, reuse in text_item? 
34  */
35 Font_metric *
36 Font_interface::get_default_font (Grob*me)
37 {
38   Font_metric * fm =  unsmob_metrics (me->get_grob_property ("font"));
39   if (fm)
40     return fm;
41
42   fm = get_font (me,  font_alist_chain (me));
43   me->set_grob_property ("font", fm->self_scm ());
44   return fm;
45 }
46
47
48 SCM
49 ly_font_interface_get_default_font (SCM grob)
50 {
51   Grob * gr  = unsmob_grob (grob);
52
53   if (!gr)
54     {
55       warning ("ly_font_interface_get_default_font (): invalid argument");
56       return SCM_UNDEFINED;
57     }
58
59   return Font_interface::get_default_font (gr)->self_scm ();
60 }
61
62 Font_metric *
63 Font_interface::get_font (Grob *me, SCM chain)
64 {
65   
66   SCM ss = me->paper_l ()->style_sheet_;
67
68   SCM proc = gh_cdr (scm_assoc (ly_symbol2scm ("properties-to-font"),
69                                 ss));
70
71   SCM fonts = gh_cdr (scm_assoc (ly_symbol2scm ("fonts"), ss));
72
73   assert (gh_procedure_p (proc));
74   SCM font_name = gh_call2 (proc, fonts, chain);
75
76   Font_metric *fm = me->paper_l ()->find_font (font_name, 1.0);
77   return fm;
78 }
79
80 SCM
81 Font_interface::add_style (Grob* me, SCM style, SCM chain)
82 {
83   assert (gh_symbol_p (style));
84   
85   SCM sheet = me->paper_l ()->style_sheet_;
86       
87   SCM style_alist = gh_cdr (scm_assoc (ly_symbol2scm ("style-alist"), sheet));
88   SCM entry = scm_assoc (style, style_alist);
89   if (gh_pair_p (entry))
90     {
91       chain = gh_cons (gh_cdr (entry), chain);
92     }
93   return chain;
94 }
95
96 /*
97 SCM routines:  
98
99 Interpreting music...
100 MIDI output to wtk1-fugue2.midi...
101 Track ... 
102
103 real    0m31.862s
104 user    0m29.110s
105 sys     0m0.260s
106
107 real    0m26.964s
108 user    0m24.850s
109 sys     0m0.280s
110
111
112 so a 14% speedup.
113
114 */
115
116 static SCM name_sym, shape_sym, family_sym, series_sym, rel_sz_sym, pt_sz_sym, wild_sym;
117
118
119 static void
120 init_syms ()
121 {
122   name_sym = scm_permanent_object (ly_symbol2scm ("font-name"));
123   shape_sym  = scm_permanent_object (ly_symbol2scm ("font-shape"));
124   family_sym = scm_permanent_object (ly_symbol2scm ("font-family"));
125   series_sym = scm_permanent_object (ly_symbol2scm ("font-series"));
126   rel_sz_sym = scm_permanent_object (ly_symbol2scm ("font-relative-size"));
127   pt_sz_sym = scm_permanent_object (ly_symbol2scm ("font-point-size"));
128   wild_sym = scm_permanent_object (ly_symbol2scm ("*"));
129
130   scm_make_gsubr ("ly-get-default-font", 1 , 0, 0, (Scheme_function_unknown) ly_font_interface_get_default_font);
131 }
132
133
134 bool
135 Font_interface::wild_compare (SCM field_val, SCM val)
136 {
137   return (val == SCM_BOOL_F || field_val == wild_sym || field_val == val);
138 }
139
140 ADD_SCM_INIT_FUNC (Font_interface_syms,init_syms);
141
142
143 MAKE_SCHEME_CALLBACK (Font_interface,properties_to_font_name,2);
144 SCM
145 Font_interface::properties_to_font_name (SCM fonts, SCM alist_chain)
146 {
147   SCM name = ly_assoc_chain (name_sym, alist_chain);
148
149   SCM shape = SCM_BOOL_F;
150   SCM family = SCM_BOOL_F;
151   SCM series = SCM_BOOL_F;
152
153   
154   SCM point_sz = ly_assoc_chain (pt_sz_sym, alist_chain);
155   SCM rel_sz = SCM_BOOL_F;
156
157   if (!gh_pair_p (name))
158     {
159        shape = ly_assoc_chain (shape_sym, alist_chain);
160        family = ly_assoc_chain (family_sym, alist_chain);
161        series = ly_assoc_chain (series_sym, alist_chain);
162
163        if (gh_pair_p (shape))
164          shape = gh_cdr (shape);
165        if (gh_pair_p (family))
166          family = gh_cdr (family);
167        if (gh_pair_p (series))
168          series = gh_cdr (series);
169     }
170   else
171     name = gh_cdr (name);
172
173
174   if (gh_pair_p (point_sz))
175     point_sz = gh_cdr (point_sz);
176   else
177     {
178       rel_sz = ly_assoc_chain (rel_sz_sym, alist_chain);
179       if (gh_pair_p (rel_sz))
180         rel_sz = gh_cdr (rel_sz);
181     }
182
183   for (SCM s = fonts ; gh_pair_p (s); s = gh_cdr (s))
184     {
185       SCM qlist = gh_caar (s);
186
187       if (name != SCM_BOOL_F)
188         {
189           if (!wild_compare (scm_list_ref (qlist, gh_int2scm (4)), name))
190             continue;
191         }
192       else
193         {
194           if (!wild_compare (scm_list_ref (qlist, gh_int2scm (1)), series))
195             continue;
196           if (!wild_compare (scm_list_ref (qlist, gh_int2scm (2)), shape))
197             continue;
198           if (!wild_compare (scm_list_ref (qlist, gh_int2scm (3)), family))
199             continue;
200         }
201   
202       if (point_sz != SCM_BOOL_F)
203         {
204           // This if statement will always be true since name must 
205           // be SCM_BOOL_F here, right?  /MB
206           if (!wild_compare (scm_list_ref (qlist, gh_int2scm (4)), name))
207             continue;
208         }
209       else
210         {
211           if (!wild_compare (gh_car (qlist), rel_sz))
212             continue;
213         }
214
215       
216       SCM qname = gh_cdar (s);
217       return qname;
218     }
219
220   warning (_ ("couldn't find any font satisfying "));
221   scm_write (gh_list (name, point_sz, shape, series , family, rel_sz, SCM_UNDEFINED), scm_current_error_port ());
222   scm_flush (scm_current_error_port ());
223  
224   return ly_str02scm ("cmr10");
225   
226 }