lilypond-1.3.145
[lilypond.git] / lily / dimen.cc
bloba03b01caf2a210a61ac9f2b527c5dbf021e80647
1 #include <ctype.h>
2 #include "dimension.hh"
3 #include "debug.hh"
4 #include "string.hh"
6 Real
7 parse_dimen (String dim)
9 int i=dim.length_i()-1;
10 char const *s = dim.ch_C ();
11 while (i > 0 && (isspace (s[i]) || isalpha (s[i])))
13 i--;
15 String unit (s + i+1);
16 return convert_dimen (dim.value_f(), unit);
20 Real
21 convert_dimen (Real quant, String unit)
23 if (unit == "cm")
24 return quant * CM_TO_PT;
25 if (unit == "pt")
26 return quant;
27 if (unit == "mm")
28 return quant*CM_TO_PT/10;
29 if (unit == "in")
30 return quant * INCH_TO_PT;
31 error (_f ("unknown length unit: `%s\'", unit));
34 String
35 print_dimen (Real r)
37 String s = to_str (r, "%.3f");
38 if (s.index_i ("NaN") != -1)
40 warning (_ ("NaN"));
41 s = "0.0";
43 s += "pt";
44 return s;