diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:29:01 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:29:01 +0000 |
commit | 35a96bde514a8897f6f0fcc41c5833bf63df2e2a (patch) | |
tree | 657d15a03cc46bd099fc2c6546a7a4ad43815d9f /src/svg/css-ostringstream.cpp | |
parent | Initial commit. (diff) | |
download | inkscape-35a96bde514a8897f6f0fcc41c5833bf63df2e2a.tar.xz inkscape-35a96bde514a8897f6f0fcc41c5833bf63df2e2a.zip |
Adding upstream version 1.0.2.upstream/1.0.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/svg/css-ostringstream.cpp | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/svg/css-ostringstream.cpp b/src/svg/css-ostringstream.cpp new file mode 100644 index 0000000..e476f9f --- /dev/null +++ b/src/svg/css-ostringstream.cpp @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * TODO: insert short description here + *//* + * Authors: see git history + * + * Copyright (C) 2018 Authors + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ +#include "svg/css-ostringstream.h" +#include "svg/strip-trailing-zeros.h" +#include "preferences.h" + +Inkscape::CSSOStringStream::CSSOStringStream() +{ + /* These two are probably unnecessary now that we provide our own operator<< for float and + * double. */ + ostr.imbue(std::locale::classic()); + ostr.setf(std::ios::showpoint); + + /* This one is (currently) needed though, as we currently use ostr.precision as a sort of + variable for storing the desired precision: see our two precision methods and our operator<< + methods for float and double. */ + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + ostr.precision(prefs->getInt("/options/svgoutput/numericprecision", 8)); +} + +static void +write_num(Inkscape::CSSOStringStream &os, unsigned const prec, double const d) +{ + char buf[32]; // haven't thought about how much is really required. + switch (prec) { + case 9: g_ascii_formatd(buf, sizeof(buf), "%.9f", d); break; + case 8: g_ascii_formatd(buf, sizeof(buf), "%.8f", d); break; + case 7: g_ascii_formatd(buf, sizeof(buf), "%.7f", d); break; + case 6: g_ascii_formatd(buf, sizeof(buf), "%.6f", d); break; + case 5: g_ascii_formatd(buf, sizeof(buf), "%.5f", d); break; + case 4: g_ascii_formatd(buf, sizeof(buf), "%.4f", d); break; + case 3: g_ascii_formatd(buf, sizeof(buf), "%.3f", d); break; + case 2: g_ascii_formatd(buf, sizeof(buf), "%.2f", d); break; + case 1: g_ascii_formatd(buf, sizeof(buf), "%.1f", d); break; + case 0: g_ascii_formatd(buf, sizeof(buf), "%.0f", d); break; + case 10: default: g_ascii_formatd(buf, sizeof(buf), "%.10f", d); break; + } + os << strip_trailing_zeros(buf); +} + +Inkscape::CSSOStringStream & +operator<<(Inkscape::CSSOStringStream &os, float const d) +{ + /* Try as integer first. */ + { + long const n = long(d); + if (d == n) { + os << n; + return os; + } + } + + write_num(os, os.precision(), d); + return os; +} + +Inkscape::CSSOStringStream & +operator<<(Inkscape::CSSOStringStream &os, double const d) +{ + /* Try as integer first. */ + { + long const n = long(d); + if (d == n) { + os << n; + return os; + } + } + + write_num(os, os.precision(), d); + return os; +} + + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : |