]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-metrics.cc
* scm/framework-texstr.scm (header): change extension to .textmetrics
[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
13 static SCM text_dimension_hash_tab;
14
15
16 Box
17 lookup_tex_text_dimension (Font_metric *font,
18                            SCM text)
19 {
20   Box b;
21
22   SCM limit = ly_lily_module_constant ("TEX_STRING_HASHLIMIT");
23   String key_str = ly_scm2string (font->font_file_name());
24   int hash_code = scm_to_int (scm_hash (text, limit));
25   key_str = to_string (hash_code)  + key_str;
26   
27   SCM val = SCM_BOOL_F;
28   if (text_dimension_hash_tab)
29     {
30       scm_hash_ref (text_dimension_hash_tab,
31                     scm_makfrom0str (key_str.to_str0 ()),
32                     SCM_BOOL_F);
33     }
34   if (scm_is_pair (val))
35     {
36       b[X_AXIS][LEFT] = 0.0;
37       b[X_AXIS][RIGHT] = scm_to_double (scm_car (val)) * point_constant;
38       val = scm_cdr (val);
39       b[Y_AXIS][UP] = scm_to_double (scm_car (val)) * point_constant;
40       val = scm_cdr (val);
41       b[Y_AXIS][DOWN] = scm_to_double (scm_car (val)) * point_constant; 
42     }
43   
44   return b; 
45 }
46
47
48
49 LY_DEFINE(ly_load_text_dimensions, "ly:load-text-dimensions",
50           1, 0, 0,
51           (SCM dimension_alist),
52           "Load dimensions from TeX in a (KEY . (W H D)) format alist")
53 {
54   if (!text_dimension_hash_tab)
55     {
56       text_dimension_hash_tab =
57         scm_gc_protect_object (scm_make_hash_table (scm_from_int (113)));
58     }
59
60   for (SCM s = dimension_alist;
61        scm_is_pair (s);
62        s = scm_cdr (s))
63     {
64       SCM key = scm_caar (s);
65       SCM val = scm_cdar (s);
66       
67       if (scm_hash_ref (text_dimension_hash_tab, key, SCM_BOOL_F)
68           == SCM_BOOL_F)
69         {
70           scm_hash_set_x (text_dimension_hash_tab, key, val);
71         }
72     }
73
74   return SCM_UNSPECIFIED;
75 }
76
77 void
78 try_load_text_metrics (String basename)
79 {
80   String path =  global_path.find_file (basename +  ".textmetrics");
81   if (path != "")
82     {
83       String contents (gulp_file_to_string (path));
84       contents = "(quote (" +  contents + "))";
85
86       SCM lst = scm_c_eval_string (contents.to_str0 ());
87       ly_load_text_dimensions (lst);
88     }
89 }