Merge branch 'fret-diagram-details'
[lilypond/csorensen.git] / lily / text-metrics.cc
blobc83f561dc74876d4094d7f5026efaf114f586782
1 /*
2 text-metrics.cc -- implement text metric lookup functions
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "dimensions.hh"
10 #include "font-metric.hh"
11 #include "main.hh"
12 #include "file-path.hh"
14 static SCM text_dimension_hash_tab;
16 Box
17 lookup_tex_text_dimension (Font_metric *font, SCM text)
19 Box b;
22 Actually, it's defined in framework-texstr, but let's see how long
23 it takes before we get a bugreport. HWN 13/2/2006.
25 SCM limit = ly_lily_module_constant ("TEX_STRING_HASHLIMIT");
26 string key_str = ly_scm2string (font->font_file_name ());
27 int hash_code = scm_to_int (scm_hash (text, limit));
28 key_str = to_string (hash_code) + key_str;
30 SCM val = SCM_BOOL_F;
31 if (text_dimension_hash_tab)
33 scm_hash_ref (text_dimension_hash_tab,
34 ly_string2scm (key_str),
35 SCM_BOOL_F);
37 if (scm_is_pair (val))
39 b[X_AXIS][LEFT] = 0.0;
40 b[X_AXIS][RIGHT] = scm_to_double (scm_car (val)) * point_constant;
41 val = scm_cdr (val);
42 b[Y_AXIS][UP] = scm_to_double (scm_car (val)) * point_constant;
43 val = scm_cdr (val);
44 b[Y_AXIS][DOWN] = scm_to_double (scm_car (val)) * point_constant;
47 return b;
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 @code{(KEY . (W H D))} format"
54 " alist.")
56 if (!text_dimension_hash_tab)
58 text_dimension_hash_tab
59 = scm_gc_protect_object (scm_c_make_hash_table (113));
62 for (SCM s = dimension_alist;
63 scm_is_pair (s);
64 s = scm_cdr (s))
66 SCM key = scm_caar (s);
67 SCM val = scm_cdar (s);
69 if (scm_hash_ref (text_dimension_hash_tab, key, SCM_BOOL_F)
70 == SCM_BOOL_F)
71 scm_hash_set_x (text_dimension_hash_tab, key, val);
74 return SCM_UNSPECIFIED;
77 void
78 try_load_text_metrics (string basename)
80 string path = global_path.find (basename + ".textmetrics");
81 if (path != "")
83 string contents (gulp_file_to_string (path, true, -1));
84 contents = "(quote (" + contents + "))";
86 SCM lst = scm_c_eval_string (contents.c_str ());
87 ly_load_text_dimensions (lst);