Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / misc.cc
blob2499a09e371b90662daa22d2d628c5da09b74947
1 /*
2 misc.cc -- implement various stuff
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Jan Nieuwenhuizen <janneke@gnu.org>
8 */
11 #include "misc.hh"
12 #include "warn.hh"
15 Return the 2-log, rounded down
17 int
18 intlog2 (int d)
20 assert (d);
21 int i = 0;
22 while ((d != 1))
24 d /= 2;
25 i++;
28 assert (! (d / 2));
29 return i;
32 double
33 log_2 (double x)
35 return log (x) / log (2.0);
38 Real
39 directed_round (Real f, Direction d)
41 if (d < 0)
42 return floor (f);
43 else
44 return ceil (f);
50 0 at threshold, 1 at 0, with 1/x falloff.
52 Real
53 peak_around (Real epsilon, Real threshold, Real x)
55 if (x < 0)
56 return 1.0;
57 return max (- epsilon * (x - threshold) / ((x + epsilon) * threshold), 0.0);
61 0 at 0, 1 at standard_x, and increasing thereafter.
63 Real
64 convex_amplifier (Real standard_x, Real increase_factor, Real x)
66 return (exp (increase_factor * x / standard_x) - 1.0) / (exp (increase_factor) - 1.0);
69 string
70 camel_case_to_lisp_identifier (string in)
72 vector<char> out;
74 /* don't add '-' before first character */
75 out.push_back (char (tolower (in[0])));
77 for (size_t inpos = 1; inpos < in.size (); inpos++)
79 if (isupper (in[inpos]))
80 out.push_back ('-');
81 out.push_back ( char(tolower (in[inpos])));
84 string result (&out[0], out.size ());
85 replace_all (&result, '_', '-');
87 return result;
90 vsize
91 utf8_char_len (char current)
93 vsize char_len = 1;
95 // U+10000 - U+10FFFF
96 if ((current & 0xF0) == 0xF0)
97 char_len = 4;
98 // U+0800 - U+FFFF
99 else if ((current & 0xE0) == 0xE0)
100 char_len = 3;
101 // U+0080 - U+07FF
102 else if ((current & 0xC0) == 0xC0)
103 char_len = 2;
104 else if (current & 0x80)
105 programming_error ("invalid UTF-8 string");
107 return char_len;