Code

362eb7662ffca771509e75c5d02239776c314ce8
[inkscape.git] / src / svg / css-ostringstream.cpp
1 #include "svg/css-ostringstream.h"
2 #include "svg/strip-trailing-zeros.h"
3 #include "prefs-utils.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     ostr.precision(prefs_get_int_attribute("options.svgoutput", "numericprecision", 8));
18 }
20 static void
21 write_num(Inkscape::CSSOStringStream &os, unsigned const prec, double const d)
22 {
23     char buf[32];  // haven't thought about how much is really required.
24     switch (prec) {
25         case 9: g_ascii_formatd(buf, sizeof(buf), "%.9f", d); break;
26         case 8: g_ascii_formatd(buf, sizeof(buf), "%.8f", d); break;
27         case 7: g_ascii_formatd(buf, sizeof(buf), "%.7f", d); break;
28         case 6: g_ascii_formatd(buf, sizeof(buf), "%.6f", d); break;
29         case 5: g_ascii_formatd(buf, sizeof(buf), "%.5f", d); break;
30         case 4: g_ascii_formatd(buf, sizeof(buf), "%.4f", d); break;
31         case 3: g_ascii_formatd(buf, sizeof(buf), "%.3f", d); break;
32         case 2: g_ascii_formatd(buf, sizeof(buf), "%.2f", d); break;
33         case 1: g_ascii_formatd(buf, sizeof(buf), "%.1f", d); break;
34         case 0: g_ascii_formatd(buf, sizeof(buf), "%.0f", d); break;
35         case 10: default: g_ascii_formatd(buf, sizeof(buf), "%.10f", d); break;
36     }
37     os << strip_trailing_zeros(buf);
38 }
40 Inkscape::CSSOStringStream &
41 operator<<(Inkscape::CSSOStringStream &os, float const d)
42 {
43     /* Try as integer first. */
44     {
45         long const n = long(d);
46         if (d == n) {
47             os << n;
48             return os;
49         }
50     }
52     write_num(os, os.precision(), d);
53     return os;
54 }
56 Inkscape::CSSOStringStream &
57 operator<<(Inkscape::CSSOStringStream &os, double const d)
58 {
59     /* Try as integer first. */
60     {
61         long const n = long(d);
62         if (d == n) {
63             os << n;
64             return os;
65         }
66     }
68     write_num(os, os.precision(), d);
69     return os;
70 }
73 /*
74   Local Variables:
75   mode:c++
76   c-file-style:"stroustrup"
77   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
78   indent-tabs-mode:nil
79   fill-column:99
80   End:
81 */
82 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :