2 text-metrics.cc -- implement text metric lookup functions
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "dimensions.hh"
10 #include "font-metric.hh"
12 #include "file-path.hh"
14 static SCM text_dimension_hash_tab;
17 lookup_tex_text_dimension (Font_metric *font, SCM text)
21 SCM limit = ly_lily_module_constant ("TEX_STRING_HASHLIMIT");
22 String key_str = ly_scm2string (font->font_file_name ());
23 int hash_code = scm_to_int (scm_hash (text, limit));
24 key_str = to_string (hash_code) + key_str;
27 if (text_dimension_hash_tab)
29 scm_hash_ref (text_dimension_hash_tab,
30 scm_makfrom0str (key_str.to_str0 ()),
33 if (scm_is_pair (val))
35 b[X_AXIS][LEFT] = 0.0;
36 b[X_AXIS][RIGHT] = scm_to_double (scm_car (val)) * point_constant;
38 b[Y_AXIS][UP] = scm_to_double (scm_car (val)) * point_constant;
40 b[Y_AXIS][DOWN] = scm_to_double (scm_car (val)) * point_constant;
46 LY_DEFINE (ly_load_text_dimensions, "ly:load-text-dimensions",
48 (SCM dimension_alist),
49 "Load dimensions from TeX in a (KEY . (W H D)) format alist")
51 if (!text_dimension_hash_tab)
53 text_dimension_hash_tab
54 = scm_gc_protect_object (scm_c_make_hash_table (113));
57 for (SCM s = dimension_alist;
61 SCM key = scm_caar (s);
62 SCM val = scm_cdar (s);
64 if (scm_hash_ref (text_dimension_hash_tab, key, SCM_BOOL_F)
67 scm_hash_set_x (text_dimension_hash_tab, key, val);
71 return SCM_UNSPECIFIED;
75 try_load_text_metrics (String basename)
77 String path = global_path.find (basename + ".textmetrics");
80 String contents (gulp_file_to_string (path, true));
81 contents = "(quote (" + contents + "))";
83 SCM lst = scm_c_eval_string (contents.to_str0 ());
84 ly_load_text_dimensions (lst);