Code

User message context in extensions
[inkscape.git] / src / svg / css-ostringstream.cpp
1 #include "svg/css-ostringstream.h"
2 #include "svg/strip-trailing-zeros.h"
3 #include "preferences.h"
4 #include <glib/gmessages.h>
5 #include <glib/gstrfuncs.h>
7 Inkscape::CSSOStringStream::CSSOStringStream()
8 {
9     /* These two are probably unnecessary now that we provide our own operator<< for float and
10      * double. */
11     ostr.imbue(std::locale::classic());
12     ostr.setf(std::ios::showpoint);
14     /* This one is (currently) needed though, as we currently use ostr.precision as a sort of
15        variable for storing the desired precision: see our two precision methods and our operator<<
16        methods for float and double. */
17     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
18     ostr.precision(prefs->getInt("/options/svgoutput/numericprecision", 8));
19 }
21 static void
22 write_num(Inkscape::CSSOStringStream &os, unsigned const prec, double const d)
23 {
24     char buf[32];  // haven't thought about how much is really required.
25     switch (prec) {
26         case 9: g_ascii_formatd(buf, sizeof(buf), "%.9f", d); break;
27         case 8: g_ascii_formatd(buf, sizeof(buf), "%.8f", d); break;
28         case 7: g_ascii_formatd(buf, sizeof(buf), "%.7f", d); break;
29         case 6: g_ascii_formatd(buf, sizeof(buf), "%.6f", d); break;
30         case 5: g_ascii_formatd(buf, sizeof(buf), "%.5f", d); break;
31         case 4: g_ascii_formatd(buf, sizeof(buf), "%.4f", d); break;
32         case 3: g_ascii_formatd(buf, sizeof(buf), "%.3f", d); break;
33         case 2: g_ascii_formatd(buf, sizeof(buf), "%.2f", d); break;
34         case 1: g_ascii_formatd(buf, sizeof(buf), "%.1f", d); break;
35         case 0: g_ascii_formatd(buf, sizeof(buf), "%.0f", d); break;
36         case 10: default: g_ascii_formatd(buf, sizeof(buf), "%.10f", d); break;
37     }
38     os << strip_trailing_zeros(buf);
39 }
41 Inkscape::CSSOStringStream &
42 operator<<(Inkscape::CSSOStringStream &os, float const d)
43 {
44     /* Try as integer first. */
45     {
46         long const n = long(d);
47         if (d == n) {
48             os << n;
49             return os;
50         }
51     }
53     write_num(os, os.precision(), d);
54     return os;
55 }
57 Inkscape::CSSOStringStream &
58 operator<<(Inkscape::CSSOStringStream &os, double const d)
59 {
60     /* Try as integer first. */
61     {
62         long const n = long(d);
63         if (d == n) {
64             os << n;
65             return os;
66         }
67     }
69     write_num(os, os.precision(), d);
70     return os;
71 }
74 /*
75   Local Variables:
76   mode:c++
77   c-file-style:"stroustrup"
78   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
79   indent-tabs-mode:nil
80   fill-column:99
81   End:
82 */
83 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :