]> git.donarmstrong.com Git - lilypond.git/blob - src/dimen.cc
release: 0.0.9
[lilypond.git] / src / dimen.cc
1 #include <ctype.h>
2 #include "dimen.hh"
3 #include "debug.hh"
4 #include "string.hh"
5
6 Real
7 parse_dimen(String dim)
8 {
9     int i=dim.len()-1;
10     const char *s = dim;
11     while  (i > 0 && (isspace(s[i]) || isalpha(s[i])) ){
12         i--;
13     }
14     String unit(s + i+1);
15     return convert_dimen(dim.fvalue(), unit); 
16 }
17
18 const Real CM_TO_PT=72/2.54;
19
20 Real
21 convert_dimen(Real quant, String unit)
22 {
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 * 72;
31     error ("unknown length unit: `" + unit+"'");
32 }
33
34 String
35 print_dimen(Real r)
36 {
37     String s(r);
38     s += "pt ";
39     return s;
40 }