]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-metrics.cc
new file.
[lilypond.git] / lily / text-metrics.cc
1 /*
2   text-metrics.cc -- implement text metric lookup functions
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "dimensions.hh"
10 #include "font-metric.hh"
11 #include "main.hh"
12 #include "file-path.hh"
13
14 static SCM text_dimension_hash_tab;
15
16
17 Box
18 lookup_tex_text_dimension (Font_metric *font,
19                            SCM text)
20 {
21   Box b;
22
23   SCM limit = ly_lily_module_constant ("TEX_STRING_HASHLIMIT");
24   String key_str = ly_scm2string (font->font_file_name());
25   int hash_code = scm_to_int (scm_hash (text, limit));
26   key_str = to_string (hash_code)  + key_str;
27   
28   SCM val = SCM_BOOL_F;
29   if (text_dimension_hash_tab)
30     {
31       scm_hash_ref (text_dimension_hash_tab,
32                     scm_makfrom0str (key_str.to_str0 ()),
33                     SCM_BOOL_F);
34     }
35   if (scm_is_pair (val))
36     {
37       b[X_AXIS][LEFT] = 0.0;
38       b[X_AXIS][RIGHT] = scm_to_double (scm_car (val)) * point_constant;
39       val = scm_cdr (val);
40       b[Y_AXIS][UP] = scm_to_double (scm_car (val)) * point_constant;
41       val = scm_cdr (val);
42       b[Y_AXIS][DOWN] = scm_to_double (scm_car (val)) * point_constant; 
43     }
44   
45   return b; 
46 }
47
48
49
50 LY_DEFINE(ly_load_text_dimensions, "ly:load-text-dimensions",
51           1, 0, 0,
52           (SCM dimension_alist),
53           "Load dimensions from TeX in a (KEY . (W H D)) format alist")
54 {
55   if (!text_dimension_hash_tab)
56     {
57       text_dimension_hash_tab =
58         scm_gc_protect_object (scm_make_hash_table (scm_from_int (113)));
59     }
60
61   for (SCM s = dimension_alist;
62        scm_is_pair (s);
63        s = scm_cdr (s))
64     {
65       SCM key = scm_caar (s);
66       SCM val = scm_cdar (s);
67       
68       if (scm_hash_ref (text_dimension_hash_tab, key, SCM_BOOL_F)
69           == SCM_BOOL_F)
70         {
71           scm_hash_set_x (text_dimension_hash_tab, key, val);
72         }
73     }
74
75   return SCM_UNSPECIFIED;
76 }
77
78 void
79 try_load_text_metrics (String basename)
80 {
81   String path =  global_path.find (basename +  ".textmetrics");
82   if (path != "")
83     {
84       String contents (gulp_file_to_string (path, true));
85       contents = "(quote (" +  contents + "))";
86
87       SCM lst = scm_c_eval_string (contents.to_str0 ());
88       ly_load_text_dimensions (lst);
89     }
90 }