From c853ffb5b2f75f5a889ed2e3ef89b818a736e87a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:50:49 +0200 Subject: Adding upstream version 1.3+ds. Signed-off-by: Daniel Baumann --- src/util/CMakeLists.txt | 58 + src/util/README | 9 + src/util/action-accel.cpp | 93 ++ src/util/action-accel.h | 132 ++ src/util/cached_map.h | 147 ++ src/util/cast.h | 101 ++ src/util/const_char_ptr.h | 51 + src/util/document-fonts.cpp | 80 + src/util/document-fonts.h | 69 + src/util/enums.h | 134 ++ src/util/expression-evaluator.cpp | 396 +++++ src/util/expression-evaluator.h | 196 +++ src/util/fixed_point.h | 111 ++ src/util/font-collections.cpp | 575 +++++++ src/util/font-collections.h | 140 ++ src/util/format.h | 57 + src/util/format_size.cpp | 77 + src/util/format_size.h | 35 + src/util/forward-pointer-iterator.h | 122 ++ src/util/funclog.cpp | 46 + src/util/funclog.h | 119 ++ src/util/longest-common-suffix.h | 102 ++ src/util/numeric/converters.h | 141 ++ src/util/object-renderer.cpp | 651 ++++++++ src/util/object-renderer.h | 113 ++ src/util/optstr.h | 41 + src/util/pages-skeleton.h | 154 ++ src/util/paper.cpp | 151 ++ src/util/paper.h | 66 + src/util/parse-int-range.h | 73 + src/util/pool.cpp | 74 + src/util/pool.h | 59 + src/util/preview.cpp | 92 ++ src/util/preview.h | 63 + src/util/recently-used-fonts.cpp | 235 +++ src/util/recently-used-fonts.h | 86 + src/util/reference.h | 50 + src/util/scope_exit.h | 26 + src/util/share.cpp | 45 + src/util/share.h | 112 ++ src/util/signal-blocker.h | 71 + src/util/statics.cpp | 22 + src/util/statics.h | 107 ++ src/util/trim.h | 60 + src/util/units.cpp | 582 +++++++ src/util/units.h | 233 +++ src/util/ziptool.cpp | 3043 +++++++++++++++++++++++++++++++++++ src/util/ziptool.h | 575 +++++++ 48 files changed, 9775 insertions(+) create mode 100644 src/util/CMakeLists.txt create mode 100644 src/util/README create mode 100644 src/util/action-accel.cpp create mode 100644 src/util/action-accel.h create mode 100644 src/util/cached_map.h create mode 100644 src/util/cast.h create mode 100644 src/util/const_char_ptr.h create mode 100644 src/util/document-fonts.cpp create mode 100644 src/util/document-fonts.h create mode 100644 src/util/enums.h create mode 100644 src/util/expression-evaluator.cpp create mode 100644 src/util/expression-evaluator.h create mode 100644 src/util/fixed_point.h create mode 100644 src/util/font-collections.cpp create mode 100644 src/util/font-collections.h create mode 100644 src/util/format.h create mode 100644 src/util/format_size.cpp create mode 100644 src/util/format_size.h create mode 100644 src/util/forward-pointer-iterator.h create mode 100644 src/util/funclog.cpp create mode 100644 src/util/funclog.h create mode 100644 src/util/longest-common-suffix.h create mode 100644 src/util/numeric/converters.h create mode 100644 src/util/object-renderer.cpp create mode 100644 src/util/object-renderer.h create mode 100644 src/util/optstr.h create mode 100644 src/util/pages-skeleton.h create mode 100644 src/util/paper.cpp create mode 100644 src/util/paper.h create mode 100644 src/util/parse-int-range.h create mode 100644 src/util/pool.cpp create mode 100644 src/util/pool.h create mode 100644 src/util/preview.cpp create mode 100644 src/util/preview.h create mode 100644 src/util/recently-used-fonts.cpp create mode 100644 src/util/recently-used-fonts.h create mode 100644 src/util/reference.h create mode 100644 src/util/scope_exit.h create mode 100644 src/util/share.cpp create mode 100644 src/util/share.h create mode 100644 src/util/signal-blocker.h create mode 100644 src/util/statics.cpp create mode 100644 src/util/statics.h create mode 100644 src/util/trim.h create mode 100644 src/util/units.cpp create mode 100644 src/util/units.h create mode 100644 src/util/ziptool.cpp create mode 100644 src/util/ziptool.h (limited to 'src/util') diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt new file mode 100644 index 0000000..7c76d10 --- /dev/null +++ b/src/util/CMakeLists.txt @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +set(util_SRC + action-accel.cpp + document-fonts.cpp + expression-evaluator.cpp + format_size.cpp + funclog.cpp + pool.cpp + font-collections.cpp + share.cpp + object-renderer.cpp + paper.cpp + preview.cpp + statics.cpp + recently-used-fonts.cpp + units.cpp + ziptool.cpp + + + # ------- + # Headers + action-accel.h + cached_map.h + cast.h + const_char_ptr.h + document-fonts.h + enums.h + expression-evaluator.h + font-collections.h + fixed_point.h + format.h + format_size.h + forward-pointer-iterator.h + funclog.h + longest-common-suffix.h + object-renderer.h + optstr.h + pages-skeleton.h + paper.h + parse-int-range.h + pool.h + preview.h + recently-used-fonts.h + reference.h + scope_exit.h + share.h + signal-blocker.h + statics.h + trim.h + units.h + ziptool.h + numeric/converters.h +) + +add_inkscape_lib(util_LIB "${util_SRC}") +target_link_libraries(util_LIB PUBLIC 2Geom::2geom) +# add_inkscape_source("${util_SRC}") diff --git a/src/util/README b/src/util/README new file mode 100644 index 0000000..254e3fa --- /dev/null +++ b/src/util/README @@ -0,0 +1,9 @@ + + +This directory contains a variety of utility code. + +To do: + +* Merge with 'helper' into this directory. +* Move individual files to more appropriate directories. +* Split into three sub-directories: numeric, color, svg. diff --git a/src/util/action-accel.cpp b/src/util/action-accel.cpp new file mode 100644 index 0000000..db20a66 --- /dev/null +++ b/src/util/action-accel.cpp @@ -0,0 +1,93 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * ActionAccel class implementation + * + * Authors: + * Rafael Siejakowski + * + * Copyright (C) 2022 the Authors. + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include +#include +#include +#include +#include + +#include "inkscape-application.h" +#include "ui/shortcuts.h" +#include "util/action-accel.h" + +namespace Inkscape { +namespace Util { + +ActionAccel::ActionAccel(Glib::ustring action_name) + : _action{std::move(action_name)} +{ + auto &shortcuts = Shortcuts::getInstance(); + _query(); + _prefs_changed = shortcuts.connect_changed([this]() { _onShortcutsModified(); }); +} + +ActionAccel::~ActionAccel() +{ + _prefs_changed.disconnect(); +} + +void ActionAccel::_onShortcutsModified() +{ + if (_query()) { + _we_changed.emit(); + } +} + +bool ActionAccel::_query() +{ + auto *app = InkscapeApplication::instance(); + if (!app) { + g_warn_message("Inkscape", __FILE__, __LINE__, __func__, + "Attempt to read keyboard shortcuts while running without an InkscapeApplication!"); + return false; + } + auto *gtk_app = app->gtk_app(); + if (!gtk_app) { + g_warn_message("Inkscape", __FILE__, __LINE__, __func__, + "Attempt to read keyboard shortcuts while running without a GUI!"); + return false; + } + auto accel_strings = gtk_app->get_accels_for_action(_action); + std::set new_keys; + for (auto &&name : accel_strings) { + new_keys.emplace(std::move(name)); + } + + if (new_keys != _accels) + { + _accels = std::move(new_keys); + return true; + } + return false; +} + +bool ActionAccel::isTriggeredBy(GdkEventKey *key) const +{ + auto &shortcuts = Shortcuts::getInstance(); + AcceleratorKey accelerator = shortcuts.get_from_event(key); + return _accels.find(accelerator) != _accels.end(); +} + +} // namespace Util +} // namespace Inkscape + +/* + 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 : diff --git a/src/util/action-accel.h b/src/util/action-accel.h new file mode 100644 index 0000000..bef21a8 --- /dev/null +++ b/src/util/action-accel.h @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * @file + * Action Accel + * A simple tracker for accelerator keys associated to an action + * + * Authors: + * Rafael Siejakowski + * + * Copyright (C) 2022 the Authors. + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef ACTION_ACCEL_H_SEEN +#define ACTION_ACCEL_H_SEEN + +#include +#include +#include +#include +#include +#include + +namespace Inkscape { +namespace Util { + +/** Gtk::AccelKey but with equality and less-than operators */ +class AcceleratorKey : public Gtk::AccelKey +{ +public: + bool operator==(AcceleratorKey const &other) const + { + return (get_key() == other.get_key()) && (get_mod() == other.get_mod()); + } + + bool operator<(AcceleratorKey const &other) const + { + return (get_key() < other.get_key()) || (get_key() == other.get_key() + && get_mod() < other.get_mod()); + } + + AcceleratorKey(Gtk::AccelKey const &ak) : Gtk::AccelKey{ak} {}; +}; + +/** + * \brief The ActionAccel class stores the keyboard shortcuts for a given action + * and automatically keeps track of changes in the keybindings. + * + * Additionally, a signal is emitted when the keybindings for the action change. + * + * In order to create an ActionAccel object, one must pass a Glib::ustring containing the + * action name to the constructor. The object will automatically observe the + * keybindings for that action, so you always get up-to-date keyboard shortcuts. + * To check if a given key event triggers one of these keybindings, use `isTriggeredBy()`. + * + * Typical usage example: + * \code{.cpp} + auto accel = Inkscape::Util::ActionAccel("doc.undo"); + GdkEventKey *key = get_from_somewhere(); + if (accel.isTriggeredBy(key)) { + ... // do stuff + } + accel.connectModified( []() { // This code will run when the user changes + // the keybindings for this action. + } ); + \endcode + */ +class ActionAccel +{ +private: + sigc::signal _we_changed; ///< Emitted when the keybindings for the action are changed + sigc::connection _prefs_changed; ///< To listen to changes to the keyboard shortcuts + Glib::ustring _action; ///< Name of the action + std::set _accels; ///< Stores the accelerator keys for the action + + /** Queries and updates the stored shortcuts, returning true if they have changed. */ + bool _query(); + + /** Runs when the keyboard shortcut settings have changed */ + void _onShortcutsModified(); + +public: + /** + * @brief Construct an ActionAccel object which will keep track of keybindings for a given action. + * @param action_name - the name of the action to hold and observe the keybindings of. + */ + ActionAccel(Glib::ustring action_name); + + ~ActionAccel(); + + /** + * @brief Returns all keyboard shortcuts for the action. + * @return a vector containing a Gtk::AccelKey for each of the keybindings present for the action. + */ + std::vector getKeys() const + { + return std::vector(_accels.begin(), _accels.end()); + } + + /** + * @brief Connects a void callback which will run whenever the keybindings for the action change. + * At the time when the callback runs, the values stored in the ActionAccel object will have + * already been updated. This means that the new keybindings can be queried by the callback. + * @param slot - the sigc::slot representing the callback function. + * @return the resulting sigc::connection. + */ + sigc::connection connectModified(sigc::slot const &slot) { return _we_changed.connect(slot); } + + /** + * @brief Checks whether a given key event triggers this action. + * @param key - a pointer to a GdkEventKey struct containing key event data. + * @return true if one of the keyboard shortcuts for the action is triggered by the passed event, + * false otherwise. + */ + bool isTriggeredBy(GdkEventKey *key) const; +}; + +} // namespace Util +} // namespace Inkscape + +#endif // ACTION_ACCEL_H_SEEN +/* + 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 : diff --git a/src/util/cached_map.h b/src/util/cached_map.h new file mode 100644 index 0000000..c246f72 --- /dev/null +++ b/src/util/cached_map.h @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * An abstract gadget that implements a finite cache for a factory. + */ +/* + * Authors: + * PBS + * + * Copyright (C) 2022 PBS + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ +#ifndef INKSCAPE_UTIL_CACHED_MAP_H +#define INKSCAPE_UTIL_CACHED_MAP_H + +#include +#include +#include +#include + +namespace Inkscape { +namespace Util { + +/** + * A cached_map is designed for use by a factory that takes as input keys of type Tk and + * produces objects of type std::unique_ptr in response. It allows such a factory to remember + * a finite number of previously constructed objects for later re-use. + * + * Upon constructing an object v for key k for the first time, calling + * + * my_ptr = my_cached_map.add(k, std::move(v)); + * + * will add it to the cache, returning a std::shared_ptr by which it can now be accessed. + * + * To re-use an object that might be in the cache, use + * + * my_ptr = my_cached_map.lookup(k) + * + * When all copies of the shared_ptr my_ptr have expired, the object is marked as unused. However + * it is not immediately deleted. As further objects are marked as unused, the oldest unused + * objects are gradually deleted, with their number never exceeding the value max_cache_size. + * + * Note that the cache must not be destroyed while any shared pointers to any of its objects are + * still active. This is in accord with its expected usage; if the factory loads objects from an + * external library, then it should be safe to destroy the cache just before the library is + * unloaded, as the objects should no longer be in use at that point anyway. + */ +template , typename Compare = std::equal_to> +class cached_map +{ +public: + /** + * Construct an empty cached_map. + * + * The optional max_cache_size argument specifies the maximum number of unused elements which + * will be kept in memory. + */ + cached_map(std::size_t max_cache_size = 32) : max_cache_size(max_cache_size) {} + + /** + * Given a key and a unique_ptr to a value, inserts them into the map, or discards them if the + * key is already present. + * + * Returns a non-null shared_ptr to the new value in the map corresponding to key. + */ + auto add(Tk key, std::unique_ptr value) + { + auto ret = map.emplace(std::move(key), std::move(value)); + return get_view(ret.first->second); + } + + /** + * Look up a key in the map. + * + * Returns a shared pointer to the corresponding value, or null if the key is not present. + */ + auto lookup(Tk const &key) -> std::shared_ptr + { + if (auto it = map.find(key); it != map.end()) { + return get_view(it->second); + } else { + return {}; + } + } + + void clear() + { + unused.clear(); + map.clear(); + } + +private: + struct Item + { + std::unique_ptr value; // The unique_ptr owning the actual value. + std::weak_ptr view; // A non-owning shared_ptr view that is in use by the outside world. + Item(decltype(value) value) : value(std::move(value)) {} + }; + + std::size_t const max_cache_size; + std::unordered_map map; + std::deque unused; + + auto get_view(Item &item) + { + if (auto view = item.view.lock()) { + return view; + } else { + remove_unused(item.value.get()); + auto new_view = std::shared_ptr(item.value.get(), [this] (Tv *value) { + push_unused(value); + }); + item.view = new_view; + return new_view; + } + } + + void remove_unused(Tv *value) + { + auto it = std::find(unused.begin(), unused.end(), value); + if (it != unused.end()) { + unused.erase(it); + } + } + + void push_unused(Tv *value) + { + unused.emplace_back(value); + if (unused.size() > max_cache_size) { + pop_unused(); + } + } + + void pop_unused() + { + auto value = unused.front(); + map.erase(std::find_if(map.begin(), map.end(), [value] (auto const &it) { + return it.second.value.get() == value; + })); + unused.pop_front(); + } +}; + +} // namespace Util +} // namespace Inkscape + +#endif // INKSCAPE_UTIL_CACHED_MAP_H diff --git a/src/util/cast.h b/src/util/cast.h new file mode 100644 index 0000000..06fb088 --- /dev/null +++ b/src/util/cast.h @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * Hand-rolled LLVM-style RTTI system for class hierarchies where dynamic_cast isn't fast enough. + */ +#ifndef INKSCAPE_UTIL_CAST_H +#define INKSCAPE_UTIL_CAST_H + +#include + +/* + * In order to use this system with a class hierarchy, specialize the following templates for + * every member of the hierarchy. The only requirement is that + * + * first_tag <= first_tag <= last_tag + * + * exactly when S is a derived class of T. Then add to each class the line of boilerplate + * + * int tag() const override { return tag_of; } + */ +template inline constexpr int first_tag = std::enable_if::value; +template inline constexpr int last_tag = std::enable_if::value; + +/** + * Convenience function to retrieve the tag (class id) of a given type. + */ +template inline constexpr int tag_of = first_tag>>; + +/** + * Equivalent to the boolean value of dynamic_cast(...). + * + * If the supplied pointer is null, the check fails. + * + * To help catch redundant checks, checks that are known at compile time currently generate + * a compile error. Please feel free to remove these static_asserts if they become unhelpful. + */ +template +bool is(S const *s) +{ + if (!s) return false; + if constexpr (std::is_base_of_v) { + static_assert(!sizeof(T), "check is always true"); + return true; + } else if constexpr (std::is_base_of_v) { + auto const s_tag = s->tag(); + return first_tag <= s_tag && s_tag <= last_tag; + } else { + static_assert(!sizeof(T), "check is always false"); + return false; + } +} + +/** + * Equivalent to static_cast(...) where the const is deduced. + */ +template +auto cast_unsafe(S *s) +{ + return static_cast(s); +} + +template +auto cast_unsafe(S const *s) +{ + return static_cast(s); +} + +/** + * Equivalent to dynamic_cast(...) where the const is deduced. + * + * If the supplied pointer is null, the result is null. + * + * To help catch redundant casts, casts that are known at compile time currently generate + * a compile error. Please feel free to remove these static_asserts if they become unhelpful. + */ +template +auto cast(S *s) +{ + if constexpr (std::is_base_of_v) { + // Removed static assert; it complicates template "collect_items" + // static_assert(!sizeof(T), "cast is unnecessary"); + return cast_unsafe(s); + } else if constexpr (std::is_base_of_v) { + return is(s) ? cast_unsafe(s) : nullptr; + } else { + static_assert(!sizeof(T), "cast is impossible"); + return nullptr; + } +} + +#endif // INKSCAPE_UTIL_CAST_H + +/* + 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 : diff --git a/src/util/const_char_ptr.h b/src/util/const_char_ptr.h new file mode 100644 index 0000000..44e6906 --- /dev/null +++ b/src/util/const_char_ptr.h @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * Provides `const_char_ptr` + */ +/* + * Authors: + * Sergei Izmailov + * + * Copyright (C) 2020 Sergei Izmailov + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ +#ifndef SEEN_INKSCAPE_UTIL_CONST_CHAR_PTR_H +#define SEEN_INKSCAPE_UTIL_CONST_CHAR_PTR_H +#include +#include +#include "share.h" + +namespace Inkscape { +namespace Util { + +/** + * Non-owning reference to 'const char*' + * Main-purpose: avoid overloads of type `f(char*, str&)`, `f(str&, char*)`, `f(char*, char*)`, ... + */ +class const_char_ptr{ +public: + const_char_ptr() noexcept: m_data(nullptr){}; + const_char_ptr(std::nullptr_t): const_char_ptr() {}; + const_char_ptr(const char* const data) noexcept: m_data(data) {}; + const_char_ptr(const Glib::ustring& str) noexcept: const_char_ptr(str.c_str()) {}; + const_char_ptr(const std::string& str) noexcept: const_char_ptr(str.c_str()) {}; + const_char_ptr(const ptr_shared& shared) : const_char_ptr(static_cast(shared)) {}; + + const char * data() const noexcept { return m_data; } +private: + const char * const m_data = nullptr; +}; +} +} +#endif // SEEN_INKSCAPE_UTIL_CONST_CHAR_PTR_H +/* + 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: \ No newline at end of file diff --git a/src/util/document-fonts.cpp b/src/util/document-fonts.cpp new file mode 100644 index 0000000..5711d31 --- /dev/null +++ b/src/util/document-fonts.cpp @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * This file contains document used fonts related logic. The functions to manage the + * document fonts are defined in this file. + * + * Authors: + * Vaibhav Malik + * + * The contents of this file may be used under the GNU General Public License Version 2 or later. + * + */ + +#include "document-fonts.h" + +#include + +using namespace Inkscape::IO::Resource; + +namespace Inkscape { + +// get instance method for the singleton design pattern. +DocumentFonts* DocumentFonts::get() +{ + static DocumentFonts* s_instance = new Inkscape::DocumentFonts(); + return s_instance; +} + +DocumentFonts::DocumentFonts() {} + +void DocumentFonts::clear() +{ + _document_fonts.clear(); +} + +/* +void DocumentFonts::print_document_fonts() +{ + std::cout << std::endl << "********************" << std::endl; + + for(auto const& font: _document_fonts) { + std::cout << font << std::endl; + } + + std::cout << std::endl << "********************" << std::endl; +} +*/ + +void DocumentFonts::update_document_fonts(const std::map>& font_data) +{ + // Clear the old fonts and then insert latest set. + clear(); + + // Iterate over all the fonts in this map, + // and insert these fonts into the document_fonts. + for(auto const& ele: font_data) { + _document_fonts.insert(ele.first); + } + + // Emit the update signal to keep everything consistent. + update_signal.emit(); +} + +// Returns the fonts used in the document. +const std::set DocumentFonts::get_fonts() +{ + return _document_fonts; +} + +} // Namespace + +/* + 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 : diff --git a/src/util/document-fonts.h b/src/util/document-fonts.h new file mode 100644 index 0000000..40ec30d --- /dev/null +++ b/src/util/document-fonts.h @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Header file that defines the singleton DocumentFonts class. + * This is a singleton class. + * + * Authors: + * Vaibhav Malik + * + * The contents of this file may be used under the GNU General Public License Version 2 or later. + * + */ + +#ifndef INK_DOCUMENT_FONTS_H +#define INK_DOCUMENT_FONTS_H + +#include +#include +#include +#include + +#include "io/resource.h" +#include "io/dir-util.h" + +namespace Inkscape { + +class DocumentFonts { + +public: + enum What { + All, + System, + User + }; + + static DocumentFonts* get(); + ~DocumentFonts() = default; + + void clear(); + // void print_document_fonts(); + void update_document_fonts(const std::map>& font_data); + const std::set get_fonts(); + + // Signals + sigc::connection connectUpdate(sigc::slot slot) { + return update_signal.connect(slot); + } + +private: + DocumentFonts(); + std::set _document_fonts; + + // Signals + sigc::signal update_signal; +}; + +} // Namespace Inkscape + +#endif // INK_DOCUMENT_FONTS_H + +/* + 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 : diff --git a/src/util/enums.h b/src/util/enums.h new file mode 100644 index 0000000..46c4d5e --- /dev/null +++ b/src/util/enums.h @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Authors: + * Nicholas Bishop + * Johan Engelen + * + * Copyright (C) 2007 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ +#ifndef INKSCAPE_UTIL_ENUMS_H +#define INKSCAPE_UTIL_ENUMS_H + +#include + +namespace Inkscape { +namespace Util { + +/** + * Simplified management of enumerations of svg items with UI labels. + * IMPORTANT: + * When initializing the EnumData struct, you cannot use _(...) to translate strings. + * Instead, one must use N_(...) and do the translation every time the string is retrieved. + */ +template +struct EnumData +{ + E id; + const Glib::ustring label; + const Glib::ustring key; +}; + +const Glib::ustring empty_string(""); + +/** + * Simplified management of enumerations of svg items with UI labels. + * + * @note that get_id_from_key and get_id_from_label return 0 if it cannot find an entry for that key string. + * @note that get_label and get_key return an empty string when the requested id is not in the list. + */ +template class EnumDataConverter +{ +public: + typedef EnumData Data; + + EnumDataConverter(const EnumData* cd, const unsigned int length) + : _length(length), _data(cd) + {} + + E get_id_from_label(const Glib::ustring& label) const + { + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].label == label) + return _data[i].id; + } + + return (E)0; + } + + E get_id_from_key(const Glib::ustring& key) const + { + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].key == key) + return _data[i].id; + } + + return (E)0; + } + + bool is_valid_key(const Glib::ustring& key) const + { + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].key == key) + return true; + } + + return false; + } + + bool is_valid_id(const E id) const + { + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].id == id) + return true; + } + return false; + } + + const Glib::ustring& get_label(const E id) const + { + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].id == id) + return _data[i].label; + } + + return empty_string; + } + + const Glib::ustring& get_key(const E id) const + { + for(unsigned int i = 0; i < _length; ++i) { + if(_data[i].id == id) + return _data[i].key; + } + + return empty_string; + } + + const EnumData& data(const unsigned int i) const + { + return _data[i]; + } + + const unsigned int _length; +private: + const EnumData* _data; +}; + + +} +} + +#endif + +/* + 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 : diff --git a/src/util/expression-evaluator.cpp b/src/util/expression-evaluator.cpp new file mode 100644 index 0000000..24a56b7 --- /dev/null +++ b/src/util/expression-evaluator.cpp @@ -0,0 +1,396 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +/** @file + * TODO: insert short description here + */ +/* LIBGIMP - The GIMP Library + * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball + * + * Original file from libgimpwidgets: gimpeevl.c + * Copyright (C) 2008 Fredrik Alstromer + * Copyright (C) 2008 Martin Nordholts + * Modified for Inkscape by Johan Engelen + * Copyright (C) 2011 Johan Engelen + * Copyright (C) 2013 Matthew Petroff + * + * This library is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include "util/expression-evaluator.h" +#include "util/units.h" + +#include + +#include +#include + +using Inkscape::Util::unit_table; + +namespace Inkscape { +namespace Util { + +EvaluatorQuantity::EvaluatorQuantity(double value, unsigned int dimension) : + value(value), + dimension(dimension) +{ +} + +EvaluatorToken::EvaluatorToken() +{ + type = 0; + value.fl = 0; +} + +ExpressionEvaluator::ExpressionEvaluator(const char *string, Unit const *unit) : + string(g_locale_to_utf8(string,-1,nullptr,nullptr,nullptr)), + unit(unit) +{ + current_token.type = TOKEN_END; + + // Preload symbol + parseNextToken(); +} + +/** + * Evaluates the given arithmetic expression, along with an optional dimension + * analysis, and basic unit conversions. + * + * All units conversions factors are relative to some implicit + * base-unit. This is also the unit of the returned value. + * + * Returns: An EvaluatorQuantity with a value given in the base unit along with + * the order of the dimension (e.g. if the base unit is inches, a dimension + * order of two means in^2). + * + * @return Result of evaluation. + * @throws Inkscape::Util::EvaluatorException There was a parse error. + **/ +EvaluatorQuantity ExpressionEvaluator::evaluate() +{ + if (!g_utf8_validate(string, -1, nullptr)) { + throw EvaluatorException("Invalid UTF8 string", nullptr); + } + + EvaluatorQuantity result = EvaluatorQuantity(); + EvaluatorQuantity default_unit_factor; + + // Empty expression evaluates to 0 + if (acceptToken(TOKEN_END, nullptr)) { + return result; + } + + result = evaluateExpression(); + + // There should be nothing left to parse by now + isExpected(TOKEN_END, nullptr); + + resolveUnit(nullptr, &default_unit_factor, unit); + + // Entire expression is dimensionless, apply default unit if applicable + if ( result.dimension == 0 && default_unit_factor.dimension != 0 ) { + result.value /= default_unit_factor.value; + result.dimension = default_unit_factor.dimension; + } + return result; +} + +EvaluatorQuantity ExpressionEvaluator::evaluateExpression() +{ + bool subtract; + EvaluatorQuantity evaluated_terms; + + evaluated_terms = evaluateTerm(); + + // Continue evaluating terms, chained with + or -. + for (subtract = FALSE; + acceptToken('+', nullptr) || (subtract = acceptToken('-', nullptr)); + subtract = FALSE) + { + EvaluatorQuantity new_term = evaluateTerm(); + + // If dimensions mismatch, attempt default unit assignment + if ( new_term.dimension != evaluated_terms.dimension ) { + EvaluatorQuantity default_unit_factor; + + resolveUnit(nullptr, &default_unit_factor, unit); + + if ( new_term.dimension == 0 + && evaluated_terms.dimension == default_unit_factor.dimension ) + { + new_term.value /= default_unit_factor.value; + new_term.dimension = default_unit_factor.dimension; + } else if ( evaluated_terms.dimension == 0 + && new_term.dimension == default_unit_factor.dimension ) + { + evaluated_terms.value /= default_unit_factor.value; + evaluated_terms.dimension = default_unit_factor.dimension; + } else { + throwError("Dimension mismatch during addition"); + } + } + + evaluated_terms.value += (subtract ? -new_term.value : new_term.value); + } + + return evaluated_terms; +} + +EvaluatorQuantity ExpressionEvaluator::evaluateTerm() +{ + bool division; + EvaluatorQuantity evaluated_exp_terms = evaluateExpTerm(); + + for ( division = false; + acceptToken('*', nullptr) || (division = acceptToken('/', nullptr)); + division = false ) + { + EvaluatorQuantity new_exp_term = evaluateExpTerm(); + + if (division) { + evaluated_exp_terms.value /= new_exp_term.value; + evaluated_exp_terms.dimension -= new_exp_term.dimension; + } else { + evaluated_exp_terms.value *= new_exp_term.value; + evaluated_exp_terms.dimension += new_exp_term.dimension; + } + } + + return evaluated_exp_terms; +} + +EvaluatorQuantity ExpressionEvaluator::evaluateExpTerm() +{ + EvaluatorQuantity evaluated_signed_factors = evaluateSignedFactor(); + + while(acceptToken('^', nullptr)) { + EvaluatorQuantity new_signed_factor = evaluateSignedFactor(); + + if (new_signed_factor.dimension == 0) { + evaluated_signed_factors.value = pow(evaluated_signed_factors.value, + new_signed_factor.value); + evaluated_signed_factors.dimension *= new_signed_factor.value; + } else { + throwError("Unit in exponent"); + } + } + + return evaluated_signed_factors; +} + +EvaluatorQuantity ExpressionEvaluator::evaluateSignedFactor() +{ + EvaluatorQuantity result; + bool negate = FALSE; + + if (!acceptToken('+', nullptr)) { + negate = acceptToken ('-', nullptr); + } + + result = evaluateFactor(); + + if (negate) { + result.value = -result.value; + } + + return result; +} + +EvaluatorQuantity ExpressionEvaluator::evaluateFactor() +{ + EvaluatorQuantity evaluated_factor = EvaluatorQuantity(); + EvaluatorToken consumed_token = EvaluatorToken(); + + if (acceptToken(TOKEN_END, &consumed_token)) { + return evaluated_factor; + } + else if (acceptToken(TOKEN_NUM, &consumed_token)) { + evaluated_factor.value = consumed_token.value.fl; + } else if (acceptToken('(', nullptr)) { + evaluated_factor = evaluateExpression(); + isExpected(')', nullptr); + } else { + throwError("Expected number or '('"); + } + + if ( current_token.type == TOKEN_IDENTIFIER ) { + char *identifier; + EvaluatorQuantity result; + + acceptToken(TOKEN_ANY, &consumed_token); + + identifier = g_newa(char, consumed_token.value.size + 1); + + strncpy(identifier, consumed_token.value.c, consumed_token.value.size); + identifier[consumed_token.value.size] = '\0'; + + if (resolveUnit(identifier, &result, unit)) { + evaluated_factor.value /= result.value; + evaluated_factor.dimension += result.dimension; + } else { + throwError("Unit was not resolved"); + } + } + + return evaluated_factor; +} + +bool ExpressionEvaluator::acceptToken(TokenType token_type, + EvaluatorToken *consumed_token) +{ + bool existed = FALSE; + + if ( token_type == current_token.type || token_type == TOKEN_ANY ) { + existed = TRUE; + + if (consumed_token) { + *consumed_token = current_token; + } + + // Parse next token + parseNextToken(); + } + + return existed; +} + +void ExpressionEvaluator::parseNextToken() +{ + const char *s; + + movePastWhiteSpace(); + s = string; + start_of_current_token = s; + + if ( !s || s[0] == '\0' ) { + // We're all done + current_token.type = TOKEN_END; + } else if ( s[0] == '+' || s[0] == '-' ) { + // Snatch these before the g_strtod() does, otherwise they might + // be used in a numeric conversion. + acceptTokenCount(1, s[0]); + } else { + // Attempt to parse a numeric value + char *endptr = nullptr; + gdouble value = g_strtod(s, &endptr); + + if ( endptr && endptr != s ) { + // A numeric could be parsed, use it + current_token.value.fl = value; + + current_token.type = TOKEN_NUM; + string = endptr; + } else if (isUnitIdentifierStart(s[0])) { + // Unit identifier + current_token.value.c = s; + current_token.value.size = getIdentifierSize(s, 0); + + acceptTokenCount(current_token.value.size, TOKEN_IDENTIFIER); + } else { + // Everything else is a single character token + acceptTokenCount(1, s[0]); + } + } +} + +void ExpressionEvaluator::acceptTokenCount (int count, TokenType token_type) +{ + current_token.type = token_type; + string += count; +} + +void ExpressionEvaluator::isExpected(TokenType token_type, + EvaluatorToken *value) +{ + if (!acceptToken(token_type, value)) { + throwError("Unexpected token"); + } +} + +void ExpressionEvaluator::movePastWhiteSpace() +{ + if (!string) { + return; + } + + while (g_ascii_isspace(*string)) { + string++; + } +} + +bool ExpressionEvaluator::isUnitIdentifierStart(gunichar c) +{ + return (g_unichar_isalpha (c) + || c == (gunichar) '%' + || c == (gunichar) '\''); +} + +/** + * getIdentifierSize: + * @s: + * @start: + * + * Returns: Size of identifier in bytes (not including NULL + * terminator). + **/ +int ExpressionEvaluator::getIdentifierSize(const char *string, int start_offset) +{ + const char *start = g_utf8_offset_to_pointer(string, start_offset); + const char *s = start; + gunichar c = g_utf8_get_char(s); + int length = 0; + + if (isUnitIdentifierStart(c)) { + s = g_utf8_next_char (s); + c = g_utf8_get_char (s); + length++; + + while ( isUnitIdentifierStart (c) || g_unichar_isdigit (c) ) { + s = g_utf8_next_char(s); + c = g_utf8_get_char(s); + length++; + } + } + + return g_utf8_offset_to_pointer(start, length) - start; +} + +bool ExpressionEvaluator::resolveUnit (const char* identifier, + EvaluatorQuantity *result, + Unit const* unit) +{ + if (!unit) { + result->value = 1; + result->dimension = 1; + return true; + }else if (!identifier) { + result->value = 1; + result->dimension = unit->isAbsolute() ? 1 : 0; + return true; + } else if (unit_table.hasUnit(identifier)) { + Unit const *identifier_unit = unit_table.getUnit(identifier); + result->value = Quantity::convert(1, unit, identifier_unit); + result->dimension = identifier_unit->isAbsolute() ? 1 : 0; + return true; + } else { + return false; + } +} + +void ExpressionEvaluator::throwError(const char *msg) +{ + throw EvaluatorException(msg, start_of_current_token); +} + +} // namespace Util +} // namespace Inkscape diff --git a/src/util/expression-evaluator.h b/src/util/expression-evaluator.h new file mode 100644 index 0000000..a45ad5a --- /dev/null +++ b/src/util/expression-evaluator.h @@ -0,0 +1,196 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +/** @file + * TODO: insert short description here + */ +/* LIBGIMP - The GIMP Library + * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball + * + * Original file from libgimpwidgets: gimpeevl.h + * Copyright (C) 2008-2009 Fredrik Alstromer + * Copyright (C) 2008-2009 Martin Nordholts + * Modified for Inkscape by Johan Engelen + * Copyright (C) 2011 Johan Engelen + * Copyright (C) 2013 Matthew Petroff + * + * This library is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#ifndef INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H +#define INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H + +#include "util/units.h" + +#include +#include +#include + +/** + * @file + * Expression evaluator: A straightforward recursive + * descent parser, no fuss, no new dependencies. The lexer is hand + * coded, tedious, not extremely fast but works. It evaluates the + * expression as it goes along, and does not create a parse tree or + * anything, and will not optimize anything. It uses doubles for + * precision, with the given use case, that's enough to combat any + * rounding errors (as opposed to optimizing the evaluation). + * + * It relies on external unit resolving through a callback and does + * elementary dimensionality constraint check (e.g. "2 mm + 3 px * 4 + * in" is an error, as L + L^2 is a mismatch). It uses g_strtod() for numeric + * conversions and it's non-destructive in terms of the parameters, and + * it's reentrant. + * + * EBNF: + * + * expression ::= term { ('+' | '-') term }* | + * ; + * + * term ::= exponent { ( '*' | '/' ) exponent }* ; + * + * exponent ::= signed factor { '^' signed factor }* ; + * + * signed factor ::= ( '+' | '-' )? factor ; + * + * unit factor ::= factor unit? ; + * + * factor ::= number | '(' expression ')' ; + * + * number ::= ? what g_strtod() consumes ? ; + * + * unit ::= ? what not g_strtod() consumes and not whitespace ? ; + * + * The code should match the EBNF rather closely (except for the + * non-terminal unit factor, which is inlined into factor) for + * maintainability reasons. + * + * It will allow 1++1 and 1+-1 (resulting in 2 and 0, respectively), + * but I figured one might want that, and I don't think it's going to + * throw anyone off. + */ + +namespace Inkscape { +namespace Util { + +class Unit; + +/** + * EvaluatorQuantity: + * @param value In reference units. + * @param dimension mm has a dimension of 1, mm^2 has a dimension of 2, etc. + */ +class EvaluatorQuantity +{ +public: + EvaluatorQuantity(double value = 0, unsigned int dimension = 0); + + double value; + unsigned int dimension; +}; + +/** + * TokenType + */ +enum { + TOKEN_NUM = 30000, + TOKEN_IDENTIFIER = 30001, + TOKEN_ANY = 40000, + TOKEN_END = 50000 +}; +typedef int TokenType; + +/** + * EvaluatorToken + */ +class EvaluatorToken +{ +public: + EvaluatorToken(); + + TokenType type; + + union { + double fl; + struct { + const char *c; + int size; + }; + } value; +}; + +/** + * ExpressionEvaluator + * @param string NULL terminated input string to evaluate + * @param unit Unit output should be in + */ +class ExpressionEvaluator +{ +public: + ExpressionEvaluator(const char *string, Unit const *unit = nullptr); + + EvaluatorQuantity evaluate(); + +private: + const char *string; + Unit const *unit; + + EvaluatorToken current_token; + const char *start_of_current_token; + + EvaluatorQuantity evaluateExpression(); + EvaluatorQuantity evaluateTerm(); + EvaluatorQuantity evaluateExpTerm(); + EvaluatorQuantity evaluateSignedFactor(); + EvaluatorQuantity evaluateFactor(); + + bool acceptToken(TokenType token_type, EvaluatorToken *consumed_token); + void parseNextToken(); + void acceptTokenCount(int count, TokenType token_type); + void isExpected(TokenType token_type, EvaluatorToken *value); + + void movePastWhiteSpace(); + + static bool isUnitIdentifierStart(gunichar c); + static int getIdentifierSize(const char *s, int start); + + static bool resolveUnit(const char *identifier, EvaluatorQuantity *result, Unit const *unit); + + void throwError(const char *msg); +}; + +/** + * Special exception class for the expression evaluator. + */ +class EvaluatorException : public std::exception { +public: + EvaluatorException(const char *message, const char *at_position) { + std::ostringstream os; + const char *token = at_position ? at_position : ""; + os << "Expression evaluator error: " << message << " at '" << token << "'"; + msgstr = os.str(); + } + + ~EvaluatorException() noexcept override = default; // necessary to destroy the string object!!! + + const char *what() const noexcept override { + return msgstr.c_str(); + } +protected: + std::string msgstr; +}; + +} +} + +#endif // INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H diff --git a/src/util/fixed_point.h b/src/util/fixed_point.h new file mode 100644 index 0000000..44174d0 --- /dev/null +++ b/src/util/fixed_point.h @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape::Util::FixedPoint - fixed point type + * + * Authors: + * Jasper van de Gronde + * + * Copyright (C) 2006 Jasper van de Gronde + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_UTIL_FIXED_POINT_H +#define SEEN_INKSCAPE_UTIL_FIXED_POINT_H + +#include +#include +#include + +namespace Inkscape { + +namespace Util { + +template +class FixedPoint { +public: + FixedPoint() = default; + FixedPoint(const FixedPoint& value) : v(value.v) {} + FixedPoint(char value) : v(static_cast(value)<(value)<(value)<(value)<(value)<(value)<(floor(value*(1<>half_size, bh = val.v>>half_size; + v = static_cast(al*bl)>>precision; + if ( half_size >= precision ) { + v += ((al*bh)+(ah*bl)+((ah*bh)<>(precision-half_size); + v += (ah*bh)<<(2*half_size-precision); + } + return *this; + } + + FixedPoint& operator*=(char val) { v *= val; return *this; } + FixedPoint& operator*=(unsigned char val) { v *= val; return *this; } + FixedPoint& operator*=(short val) { v *= val; return *this; } + FixedPoint& operator*=(unsigned short val) { v *= val; return *this; } + FixedPoint& operator*=(int val) { v *= val; return *this; } + FixedPoint& operator*=(unsigned int val) { v *= val; return *this; } + + FixedPoint operator+(FixedPoint val) const { FixedPoint r(*this); return r+=val; } + FixedPoint operator-(FixedPoint val) const { FixedPoint r(*this); return r-=val; } + FixedPoint operator*(FixedPoint val) const { FixedPoint r(*this); return r*=val; } + + FixedPoint operator*(char val) const { FixedPoint r(*this); return r*=val; } + FixedPoint operator*(unsigned char val) const { FixedPoint r(*this); return r*=val; } + FixedPoint operator*(short val) const { FixedPoint r(*this); return r*=val; } + FixedPoint operator*(unsigned short val) const { FixedPoint r(*this); return r*=val; } + FixedPoint operator*(int val) const { FixedPoint r(*this); return r*=val; } + FixedPoint operator*(unsigned int val) const { FixedPoint r(*this); return r*=val; } + + float operator*(float val) const { return static_cast(*this)*val; } + double operator*(double val) const { return static_cast(*this)*val; } + + operator char() const { return v>>precision; } + operator unsigned char() const { return v>>precision; } + operator short() const { return v>>precision; } + operator unsigned short() const { return v>>precision; } + operator int() const { return v>>precision; } + operator unsigned int() const { return v>>precision; } + + operator float() const { return ldexpf(v,-precision); } + operator double() const { return ldexp(v,-precision); } +private: + T v; +}; + +template FixedPoint operator *(char a, FixedPoint b) { return b*=a; } +template FixedPoint operator *(unsigned char a, FixedPoint b) { return b*=a; } +template FixedPoint operator *(short a, FixedPoint b) { return b*=a; } +template FixedPoint operator *(unsigned short a, FixedPoint b) { return b*=a; } +template FixedPoint operator *(int a, FixedPoint b) { return b*=a; } +template FixedPoint operator *(unsigned int a, FixedPoint b) { return b*=a; } + +template float operator *(float a, FixedPoint b) { return b*a; } +template double operator *(double a, FixedPoint b) { return b*a; } + +} + +} + +#endif +/* + 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 : diff --git a/src/util/font-collections.cpp b/src/util/font-collections.cpp new file mode 100644 index 0000000..efdc1f9 --- /dev/null +++ b/src/util/font-collections.cpp @@ -0,0 +1,575 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * This file contains the font collection related logic. The functions to manage the font + * collections are defined in this file. + * + * Authors: + * Vaibhav Malik + * + * The contents of this file may be used under the GNU General Public License Version 2 or later. + * + */ + +#include "font-collections.h" + +#include +// #include +#include + +#include "io/resource.h" +#include "io/dir-util.h" +#include "libnrtype/font-lister.h" + +using namespace Inkscape::IO::Resource; + +namespace Inkscape { + +// Function to manage the singleton instance. +FontCollections* FontCollections::get() +{ + static FontCollections* s_instance = new Inkscape::FontCollections(); + return s_instance; +} + +FontCollections::FontCollections() +{ + init(); +} + +void FontCollections::init() +{ + // Step 1: Get the collections directory. + Glib::ustring directory = get_path_string(USER, FONTCOLLECTIONS, ""); + + // Create the fontcollections directory if not already present. + // This should be called only once. + static bool build_dir = true; + + if(build_dir) { +#ifdef _WIN32 + mkdir(directory.c_str()); +#else + mkdir(directory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); +#endif + build_dir = false; + } + + // Clear the previous collections(we may be re-reading). + clear(); + + // Step 2: Get the names of the files present in this directory. + std::vector allowed_user_ext = {"txt"}; + std::vector allowed_system_ext = {"log"}; + std::vector user_files = {}; + std::vector system_files = {}; + Inkscape::IO::Resource::get_filenames_from_path(user_files, directory, allowed_user_ext, + (std::vector){}); + Inkscape::IO::Resource::get_filenames_from_path(system_files, directory, allowed_system_ext, + (std::vector){}); + + // Step 3: Recursively read the contents of the files, + // and load the collections into the map. + read(system_files, true); + read(user_files); + + add_system_collections(); +} + +// Clear all collections +void FontCollections::clear() +{ + // Write code to clear the collections + _user_collections.clear(); + _system_collections.clear(); +} + +/* +void FontCollections::print_collection_font_map() +{ + std::cout << "User collections" << std::endl; + for(auto const &col: _user_collections) { + std::cout << col.name << std::endl; + + for(auto const &font: col.fonts) { + std::cout << "\t" << font << std::endl; + } + } + + std::cout << "System collections" << std::endl; + for(auto const &col: _system_collections) { + std::cout << col.name << std::endl; + + for(auto const &font: col.fonts) { + std::cout << "\t" << font << std::endl; + } + } +} +*/ + +// Read collections files. +void FontCollections::read(const std::vector& files, bool is_system) +{ + // Iterate over the files vector and read each file. + for(auto const &file: files) { + _read(file, is_system); + } +} + +// Read fonts stored in a collection file. +void FontCollections::_read(const Glib::ustring& file, bool is_system) +{ + // Filestream object to read data from the file. + std::ifstream input_file(file); + + // Check if the file is open or not. + if (input_file.is_open()) { + // Generate the collection name from the file name. + Glib::ustring path = get_path_string(USER, FONTCOLLECTIONS, ""); + + Glib::ustring collection_name = file.substr(path.length() + 1, file.length() - path.length() - 5); + std::string line; + + // Now read all the fonts stored in this file. + std::set fonts; + Inkscape::FontLister *font_lister = Inkscape::FontLister::get_instance(); + + while (getline(input_file, line)) { + // Get rid of unwanted characters from the left and right. + line = trim_left_and_right(line); + Glib::ustring font = line; + + // Now check if the font is installed on the system because it is possible + // that a previously installed font may not be available now. + if (font_lister->font_installed_on_system(font)) { + fonts.insert(font); + } + } + + // Important: Close the file after use! + input_file.close(); + + // Now insert these fonts into the font collections. + FontCollection temp_col(collection_name, fonts, is_system); + + if (is_system) { + _system_collections.insert(temp_col); + } else { + _user_collections.insert(temp_col); + } + } else { + // Error: Failed to open the file. + // std::cout << "Failed to open file: " << file << std::endl; + } +} + +// Function to write a collection to file. +void FontCollections::write_collection(const Glib::ustring& collection_name, const std::set & fonts, bool is_system) +{ + std::string collection_file = generate_filename_from_collection(collection_name, is_system); + std::fstream output_file; + output_file.open(collection_file, std::fstream::out); + + // Check if the file opened or not. + if (output_file.is_open()) { + // Insert the fonts into the file. + for (auto const &font : fonts) { + output_file << font << '\n'; + } + + // Very Important: Close the file after use. + output_file.close(); + init(); + } else { + // Error: Failed to open the file. + // std::cout << "Failed to open file: " << collection_file << std::endl; + } +} + +// System collections. +void FontCollections::add_system_collections() +{ + // clear the vector, we may be re-reading. + _system_collections.clear(); + + // System collections: + // 1. Document fonts. + // 2. Recently Used fonts. + std::string col_name = DOCUMENT_FONTS; + FontCollection temp_col1(col_name, true); + col_name = RECENTLY_USED_FONTS; + FontCollection temp_col2(col_name, true); + _system_collections.insert(temp_col1); + _system_collections.insert(temp_col2); +} + +// Add a collection. +void FontCollections::add_collection(const Glib::ustring& collection_name, bool is_system) +{ + // Check for empty name. + if (collection_name == "") { + return; + } + + // Get rid of unwanted characters from left and right. + std::string col_name_copy = collection_name; + col_name_copy = trim_left_and_right(col_name_copy); + FontCollection temp_col(col_name_copy, is_system); + + if (is_system) { + // Add and save this collection as a system collection. + // No need to save system collections in a file, + // because system collections are already being managed + // by separate files. + _system_collections.insert(temp_col); + } else { + // The return value will tell if the collection was already present + // in the set or not. + auto ret_val = _user_collections.insert(temp_col); + + // Write the changes only if the collection was inserted. + if (ret_val.second == true) { + // write this collection to a file. + write_collection(col_name_copy, temp_col.fonts); + } + } + + update_signal.emit(); +} + +// Remove a collection. Only user collections are allowed to be removed. +void FontCollections::remove_collection(const Glib::ustring& collection_name) +{ + // Check if the collection is there. + FontCollection temp_col(collection_name, false); + auto it = _user_collections.find(temp_col); + + if (it != _user_collections.end()) { + // Collection exists, erase it from user and selected collections. + _user_collections.erase(it); + + Glib::ustring file_name = collection_name + ".txt"; + std::string collection_file = get_path_string(USER, FONTCOLLECTIONS, file_name.c_str()); + // std::cout << "Deleting: " << collection_file << std::endl; + + // Erase the collection file from the folder. + remove(collection_file.c_str()); + + // Emit the update signal to update the popover list in: + // (i) T and F dialog + // (ii) Text Toolbar + update_signal.emit(); + } else { + return; + } + + // Check if the collection was selected. + auto it1 = _selected_collections.find(collection_name); + + if (it1 != _selected_collections.end()) { + // Remove it from the selected collections, + // and emit the selection update signal. + _selected_collections.erase(it1); + + // Update the font list accordingly. + Inkscape::FontLister::get_instance()->apply_collections(_selected_collections); + + // Let the world know. + selection_update_signal.emit(); + } +} + +// Only user collections can be renamed. +void FontCollections::rename_collection(const Glib::ustring& old_name, const Glib::ustring &new_name) +{ + if (old_name == new_name) { + return; + } + + // 1. Copy the fonts stored under old_name. + FontCollection old_col(old_name, false); + std::set fonts = get_fonts(old_name); + + // 2. Remove the old collection from the map. + auto it = _user_collections.find(old_col); + + // Only if it exists + if (it != _user_collections.end()) { + _user_collections.erase(it); + + // 3. Rename the file. + Glib::ustring old_file = old_name + ".txt"; + Glib::ustring new_file = new_name + ".txt"; + std::string old_file_path = get_path_string(USER, FONTCOLLECTIONS, old_file.c_str()); + std::string new_file_path = get_path_string(USER, FONTCOLLECTIONS, new_file.c_str()); + rename(old_file_path.c_str(), new_file_path.c_str()); + + // 4: Insert new_collection into the map. + FontCollection new_col(new_name, fonts, false); + _user_collections.insert(new_col); + + // Check if it was selected before renaming or not. + auto it1 = _selected_collections.find(old_name); + + if (it1 != _selected_collections.end()) { + // Select the renamed collection. + _selected_collections.insert(new_name); + + // No need to update the font list, + // as the fonts under selection remains the same. + // Emit the selection update signal. + selection_update_signal.emit(); + } + } else { + // Otherwise, add it as a new collection. + add_collection(new_name); + } + + // Update the popover lists. + update_signal.emit(); +} + +void FontCollections::rename_font(const Glib::ustring& collection_name, const Glib::ustring &old_name, const Glib::ustring &new_name) +{ + // 1. Erase the old font. + remove_font(collection_name, old_name); + + // 2. Add the new font into the collection. + add_font(collection_name, new_name); +} + +// Add a font to a collection and save that collection. +void FontCollections::add_font(const Glib::ustring& collection_name, const Glib::ustring& font_name) +{ + // std::cout << "Collection: " << collection_name << ", Font: " << font_name < fonts = node.value().fonts; + _user_collections.insert(std::move(node)); + + write_collection(collection_name, fonts); + + // Update the font list if the collection was already selected. + auto it = _selected_collections.find(collection_name); + + if (it != _selected_collections.end()) { + // No need to send the update signal as the font list will be changed + // here only. No other widget is required to take any action. + Inkscape::FontLister::get_instance()->apply_collections(_selected_collections); + } + } +} + +// Remove a font. +void FontCollections::remove_font(const Glib::ustring& collection_name, const Glib::ustring& font_name) +{ + if (font_name == "" || collection_name == "") { + return; + } + + // 1. Check if the font is present in the set + FontCollection temp_col(collection_name, false); + auto node = _user_collections.extract(temp_col); + + // 2. Delete the font if it exists. + if (node) { + node.value().fonts.erase(font_name); + std::set fonts = node.value().fonts; + _user_collections.insert(std::move(node)); + + // Save the changes to the collection file. + write_collection(collection_name, fonts); + + // Update the font list if the collection was already selected. + auto it = _selected_collections.find(collection_name); + + if (it != _selected_collections.end()) { + // No need to send the update signal as the font list will be changed + // here only. No other widget is required to take any action. + Inkscape::FontLister::get_instance()->apply_collections(_selected_collections); + } + } +} + +void FontCollections::update_selected_collections(const Glib::ustring& collection_name) +{ + auto it = _selected_collections.find(collection_name); + + if (it != _selected_collections.end()) { + // Collection already present in the selected collections. + // Remove it. + _selected_collections.erase(it); + } else { + _selected_collections.insert(collection_name); + } + + // Re-generate the font list. + Inkscape::FontLister::get_instance()->apply_collections(_selected_collections); + + // Emit the selection update signal. + selection_update_signal.emit(); +} + +bool FontCollections::is_collection_selected(const Glib::ustring& collection_name) +{ + auto it = _selected_collections.find(collection_name); + + if(it != _selected_collections.end()) { + return true; + } + + return false; +} + +void FontCollections::clear_selected_collections() +{ + _selected_collections.clear(); + + // Emit the selection update signal. + selection_update_signal.emit(); +} + +// Removes unwanted characters from the left and right of the string. +std::string& FontCollections::trim_left_and_right(std::string& s, const char* t) +{ + s.erase(0, s.find_first_not_of(t)); + s.erase(s.find_last_not_of(t) + 1); + return s; +} + +int FontCollections::get_user_collection_location(const Glib::ustring& collection_name) +{ + // This is a binary-search function on the elements of a set. + // std::vector collections(_user_collection_font_map.size()); + std::vector collections(_user_collections.size()); + + // Copy the elements of the set in a vector. + int i = 0; + for(auto const &collection: _user_collections) { + collections[i++] = collection.name; + } + + int position = (lower_bound(collections.begin(), collections.end(), collection_name) - collections.begin()); + + return position + _system_collections.size(); +} + +std::string FontCollections::generate_filename_from_collection(const Glib::ustring &collection_name, bool is_system) +{ + Glib::ustring file_name; + std::string collection_file; + + if(is_system) { + file_name = collection_name + ".log"; + } else { + file_name = collection_name + ".txt"; + } + + collection_file = get_path_string(USER, FONTCOLLECTIONS, file_name.c_str()); + + return collection_file; +} + +int FontCollections::get_collections_count(bool is_system) +{ + if(is_system) { + return _system_collections.size(); + } + + return _user_collections.size(); +} + +bool FontCollections::find_collection(const Glib::ustring& collection_name, bool is_system) +{ + FontCollection temp_collection(collection_name, is_system); + + if (is_system) { + auto it = _system_collections.find(temp_collection); + + if (it != _system_collections.end()) { + return true; + } + } else { + auto it = _user_collections.find(temp_collection); + + if (it != _user_collections.end()) { + return true; + } + } + + return false; +} + +// Get a set of the collections. +std::vector FontCollections::get_collections(bool is_system) +{ + std::vector collections; + if(is_system) { + for(auto const &col: _system_collections) { + collections.push_back(col.name); + } + } else { + for(auto const &col: _user_collections) { + collections.push_back(col.name); + } + } + + return collections; +} + +// Get all of the collections. +std::vector FontCollections::get_all_collections() +{ + std::vector collections(_system_collections.size() + _user_collections.size()); + + // Iterate over all the key value pairs and + // Insert them into the set. + int i = 0; + + for(auto const &col: _system_collections) { + collections[i++] = col.name; + } + + for(auto const &col: _user_collections) { + collections[i++] = col.name; + } + + return collections; +} + +// Get the set of fonts stored in a particular collection. +std::set FontCollections::get_fonts(const Glib::ustring& collection_name, bool is_system) +{ + // Check if the collection exists. + FontCollection temp_col(collection_name, is_system); + auto it = _user_collections.find(temp_col); + + if(it != _user_collections.end()) { + // The collection exists. + return (*it).fonts; + } + + std::set temp_set; + return temp_set; +} + +} // Namespace + +/* + 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 : diff --git a/src/util/font-collections.h b/src/util/font-collections.h new file mode 100644 index 0000000..5b9e088 --- /dev/null +++ b/src/util/font-collections.h @@ -0,0 +1,140 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * This file defines the Font Collections class. A map takes care of all the collections. + * On the hard disk the font collections are stored in the user profile path under the + * "fontcollections" directory. Each collection file is a plain text file which is named + * as "collection_name.txt" and contains the fonts contained in that collection. On + * initializing the collections, it loads the font collections stored in the files and + * their respective fonts. + * + * This file further contains all the necessary functions to create a new font collection, + * update the fonts stored in a collection, rename a collection and deletion of collections + * and their fonts. + * + * Authors: + * Vaibhav Malik + * + * The contents of this file may be used under the GNU General Public License Version 2 or later. + * + */ + +#ifndef INK_COLLECTIONS_H +#define INK_COLLECTIONS_H + +#include +#include + +#include +#include +#include +#include + +namespace Inkscape { + + inline const std::string RECENTLY_USED_FONTS = _("Recently Used Fonts"); + inline const std::string DOCUMENT_FONTS = _("Document Fonts"); + +struct FontCollection { + Glib::ustring name; + std::set fonts; + bool is_system; + + bool operator == (const FontCollection& ft) const { return name == ft.name;} + bool operator < (const FontCollection& ft) const { return name < ft.name;} + + FontCollection(const Glib::ustring name, bool is_system): name(name), is_system(is_system) {} + + FontCollection(const Glib::ustring name, const std::set fonts, bool is_system): name(name), fonts(fonts), is_system(is_system) {} + + void insert_font(const Glib::ustring &font_name) + { + fonts.insert(font_name); + } +}; + +// The FontCollections class is a singleton class. +class FontCollections { + +public: + enum What { + All, + System, + User + }; + + static FontCollections* get(); + ~FontCollections() = default; + + void init(); + void clear(); + // void print_collection_font_map(); + + void read(const std::vector &, bool is_system = false); + void write_collection(const Glib::ustring& collection_name, const std::set & fonts, bool is_system = false); + + // System collections. + // We just store the names of system collections here. + // The logic to manage the system collections is written in separate files. + void add_system_collections(); + + // Add/remove user collections + void add_collection(const Glib::ustring& collection_name, bool is_system = false); + void remove_collection(const Glib::ustring& collection_name); + void rename_collection(const Glib::ustring& old_name, const Glib::ustring& new_name); + void rename_font(const Glib::ustring& collection_name, const Glib::ustring& old_name, const Glib::ustring& new_name); + void add_font(const Glib::ustring& collection_name, const Glib::ustring& font_name); + void remove_font(const Glib::ustring& collection_name, const Glib::ustring& font_name); + void update_selected_collections(const Glib::ustring& collection_name); + bool is_collection_selected(const Glib::ustring& collection_name); + void clear_selected_collections(); + + // Utility + std::string& trim_left_and_right(std::string& s, const char* t = " \t\n\r\f\v"); + int get_user_collection_location(const Glib::ustring& collection_name); + std::string generate_filename_from_collection(const Glib::ustring &collection_name, bool is_system); + int get_collections_count(bool is_system = false); + bool find_collection(const Glib::ustring& collection_name, bool is_system = false); + + std::vector get_collections(bool is_system = false); + std::vector get_all_collections(); + std::set get_fonts(const Glib::ustring& name, bool is_system = false); + + // This signal will be emitted whenever there's a change in the font collections + // This includes: Creating/deleting collection, and adding/deleting fonts. + sigc::connection connect_update(sigc::slot slot) { + return update_signal.connect(slot); + } + + // This signal will be emitted whenever the user selects or + // un-selects a font collection. + sigc::connection connect_selection_update(sigc::slot slot) { + return selection_update_signal.connect(slot); + } + +private: + FontCollections(); + + std::set _system_collections; + std::set _user_collections; + std::set _selected_collections; + + void _read(const Glib::ustring&, bool is_system = false); + + sigc::signal update_signal; + sigc::signal selection_update_signal; +}; + +} // Namespace Inkscape + +#endif // INK_COLLECTIONS_H + +/* + 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 : diff --git a/src/util/format.h b/src/util/format.h new file mode 100644 index 0000000..b38f3a6 --- /dev/null +++ b/src/util/format.h @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape::Util::format - g_strdup_printf wrapper producing shared strings + * + * Authors: + * MenTaLguY + * + * Copyright (C) 2006 MenTaLguY + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_UTIL_FORMAT_H +#define SEEN_INKSCAPE_UTIL_FORMAT_H + +#include +#include +#include "util/share.h" + +namespace Inkscape { + +namespace Util { + +inline ptr_shared vformat(char const *format, va_list args) { + char *temp=g_strdup_vprintf(format, args); + ptr_shared result=share_string(temp); + g_free(temp); + return result; +} + + // needed since G_GNUC_PRINTF can only be used on a declaration + ptr_shared format(char const *format, ...) G_GNUC_PRINTF(1,2); +inline ptr_shared format(char const *format, ...) { + va_list args; + + va_start(args, format); + ptr_shared result=vformat(format, args); + va_end(args); + + return result; +} + +} + +} + +#endif +/* + 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 : diff --git a/src/util/format_size.cpp b/src/util/format_size.cpp new file mode 100644 index 0000000..58b553a --- /dev/null +++ b/src/util/format_size.cpp @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "format_size.h" +#include +#include + +namespace Inkscape { +namespace Util { + +Glib::ustring format_size(std::size_t value) { + if (!value) { + return Glib::ustring("0"); + } + + typedef std::vector Digits; + typedef std::vector Groups; + + Groups groups; + + Digits *digits; + + while (value) { + unsigned places=3; + digits = new Digits(); + digits->reserve(places); + + while ( value && places ) { + digits->push_back('0' + (char)( value % 10 )); + value /= 10; + --places; + } + + groups.push_back(digits); + } + + Glib::ustring temp; + + while (true) { + digits = groups.back(); + while (!digits->empty()) { + temp.append(1, digits->back()); + digits->pop_back(); + } + delete digits; + + groups.pop_back(); + if (groups.empty()) { + break; + } + + temp.append(","); + } + + return temp; +} + +Glib::ustring format_file_size(std::size_t value) { + std::ostringstream ost; + if (value < 1024) { + ost << value << " B"; + } + else { + double size = value; + int index = 0; + do { + size /= 1024; + ++index; + } while (size > 1024); + + static const char* unit[] = {"", "k", "M", "G", "T", "P", "E", "Z", "Y"}; + ost.precision(1); + ost << std::fixed << size << ' ' << unit[index] << 'B'; + } + return ost.str(); +} + +}} // namespace diff --git a/src/util/format_size.h b/src/util/format_size.h new file mode 100644 index 0000000..69c8dc6 --- /dev/null +++ b/src/util/format_size.h @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape::Util::format_size - format a number into a byte display + * + * Copyright (C) 2005-2022 Inkscape Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_UTIL_FORMAT_SIZE_H +#define SEEN_INKSCAPE_UTIL_FORMAT_SIZE_H + +#include +#include + +namespace Inkscape { +namespace Util { + +Glib::ustring format_size(std::size_t value); + +Glib::ustring format_file_size(std::size_t value); + +}} +#endif + +/* + 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 : diff --git a/src/util/forward-pointer-iterator.h b/src/util/forward-pointer-iterator.h new file mode 100644 index 0000000..9fe3bb8 --- /dev/null +++ b/src/util/forward-pointer-iterator.h @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape::Util::ForwardPointerIterator - wraps a simple pointer + * with various strategies + * to determine sequence + * + * Authors: + * MenTaLguY + * + * Copyright (C) 2004 MenTaLguY + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H +#define SEEN_INKSCAPE_UTIL_FORWARD_POINTER_ITERATOR_H + +#include +#include +#include "util/reference.h" + +namespace Inkscape { + +namespace Util { + +template +class ForwardPointerIterator; + +template +class ForwardPointerIterator { +public: + typedef std::forward_iterator_tag iterator_category; + typedef typename Traits::Reference::LValue value_type; + typedef std::ptrdiff_t difference_type; + typedef typename Traits::Reference::LValue reference; + typedef typename Traits::Reference::RValue const_reference; + typedef typename Traits::Reference::Pointer pointer; + + typedef ForwardPointerIterator Self; + + ForwardPointerIterator() = default; + ForwardPointerIterator(pointer p) : _p(p) {} + + operator pointer() const { return _p; } + reference operator*() const { return *_p; } + pointer operator->() const { return _p; } + + bool operator==(Self const &other) const { + return _p == other._p; + } + bool operator!=(Self const &other) const { + return _p != other._p; + } + + Self &operator++() { + _p = Strategy::next(_p); + return *this; + } + Self operator++(int) { + Self old(*this); + operator++(); + return old; + } + + operator bool() const { return _p != nullptr; } + +private: + pointer _p; +}; + +template +class ForwardPointerIterator +: public ForwardPointerIterator +{ +public: + typedef typename Traits::Reference::LValue value_type; + typedef typename Traits::Reference::LValue reference; + typedef typename Traits::Reference::RValue const_reference; + typedef typename Traits::Reference::Pointer pointer; + + typedef ForwardPointerIterator Ancestor; + typedef ForwardPointerIterator Self; + + ForwardPointerIterator() : Ancestor() {} + ForwardPointerIterator(pointer p) : Ancestor(p) {} + + operator pointer() const { + return const_cast(Ancestor::operator->()); + } + reference operator*() const { + return const_cast(Ancestor::operator*()); + } + pointer operator->() const { + return const_cast(Ancestor::operator->()); + } + + Self &operator++() { + Ancestor::operator++(); + return *this; + } + Self operator++(int) { + Self old(*this); + operator++(); + return old; + } +}; + +} + +} + +#endif +/* + 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 : diff --git a/src/util/funclog.cpp b/src/util/funclog.cpp new file mode 100644 index 0000000..5597fea --- /dev/null +++ b/src/util/funclog.cpp @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include "funclog.h" + +namespace Inkscape { +namespace Util { + +void FuncLog::exec() +{ + for (auto h = first; h; destroy_and_advance(h)) { + try { + (*h)(); + } catch (...) { + destroy_from(h); + reset(); + std::rethrow_exception(std::current_exception()); + } + } + reset(); +} + +void FuncLog::destroy_and_advance(Header *&h) noexcept +{ + auto next = h->next; + h->~Header(); + h = next; +} + +void FuncLog::reset() noexcept +{ + pool.free_all(); + first = nullptr; + lastnext = &first; +} + +void FuncLog::movefrom(FuncLog &other) noexcept +{ + pool = std::move(other.pool); + first = other.first; + lastnext = first ? other.lastnext : &first; + + other.first = nullptr; + other.lastnext = &other.first; +} + +} // namespace Util +} // namespace Inkscape diff --git a/src/util/funclog.h b/src/util/funclog.h new file mode 100644 index 0000000..7b81e41 --- /dev/null +++ b/src/util/funclog.h @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** \file FuncLog + * A log of functions that can be appended to and played back later. + */ +#ifndef INKSCAPE_UTIL_FUNCLOG_H +#define INKSCAPE_UTIL_FUNCLOG_H + +#include +#include +#include "util/pool.h" + +namespace Inkscape { +namespace Util { + +/** + * A FuncLog is effectively a std::vector>, with the ability to hold + * move-only function types and enforced run-once semantics. + * + * The main difference is an efficient internal representation that stores the contents nearly + * contiguously. This gives a 2x speedup when std::function uses the small-lambda optimisation, + * and a 7x speedup when it has to heap-allocate. + */ +class FuncLog final +{ +public: + FuncLog() = default; + FuncLog(FuncLog &&other) noexcept { movefrom(other); } + FuncLog &operator=(FuncLog &&other) noexcept { destroy(); movefrom(other); return *this; } + ~FuncLog() { destroy(); } + + /** + * Append a callable object to the log. + * On exception, no object is inserted, though memory will not be returned immediately. + */ + template + void emplace(F &&f) + { + using Fd = typename std::decay::type; + auto entry = pool.allocate>(); + new (entry) Entry(std::forward(f)); + *lastnext = entry; + lastnext = &entry->next; + entry->next = nullptr; + } + + /** + * Execute and destroy each callable in the log. + * On exception, all remaining callables are destroyed. + * \post empty() == true + */ + void exec(); + + /// Convenience alias for exec(). + void operator()() { exec(); } + + /** + * Execute and destroy each callable in the log while condition \a c() is true, then destroy the rest. + * On exception, all remaining callables are destroyed. + * \post empty() == true + */ + template + void exec_while(C &&c) + { + for (auto h = first; h; destroy_and_advance(h)) { + try { + if (!c()) { + destroy_from(h); + break; + } + (*h)(); + } catch (...) { + destroy_from(h); + reset(); + std::rethrow_exception(std::current_exception()); + } + } + reset(); + } + + /** + * Destroy all callables in the log without executing them. + * \post empty() == true + */ + void clear() { destroy(); reset(); } + + bool empty() const { return !first; } + +private: + struct Header + { + Header *next; + virtual ~Header() = default; + virtual void operator()() = 0; + }; + + template + struct Entry : Header + { + Fd f; + template + Entry(F &&f) : f(std::forward(f)) {} + void operator()() override { f(); } + }; + + Pool pool; + Header *first = nullptr; + Header **lastnext = &first; + + void destroy() { destroy_from(first); } + static void destroy_from(Header *h) { while (h) destroy_and_advance(h); } + static void destroy_and_advance(Header *&h) noexcept; + void reset() noexcept; + void movefrom(FuncLog &other) noexcept; +}; + +} // namespace Util +} // namespace Inkscape + +#endif // INKSCAPE_UTIL_FUNCLOG_H diff --git a/src/util/longest-common-suffix.h b/src/util/longest-common-suffix.h new file mode 100644 index 0000000..1f7b228 --- /dev/null +++ b/src/util/longest-common-suffix.h @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape::Algorithms::nearest_common_ancestor + * + * Authors: + * MenTaLguY + * + * Copyright (C) 2004 MenTaLguY + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_ALGORITHMS_NEAREST_COMMON_ANCESTOR_H +#define SEEN_INKSCAPE_ALGORITHMS_NEAREST_COMMON_ANCESTOR_H + +#include +#include + +namespace Inkscape { + +namespace Algorithms { + +/** + * Time costs: + * + * The case of sharing a common successor is handled in O(1) time. + * + * If \a a is the nearest common ancestor, then runs in O(len(rest of b)) time. + * + * Otherwise, runs in O(len(a) + len(b)) time. + */ + +template +ForwardIterator nearest_common_ancestor(ForwardIterator a, ForwardIterator b, ForwardIterator end) +{ + if ( a == end || b == end ) { + return end; + } + + /* Handle in O(1) time the common cases of identical lists or tails. */ + { + /* identical lists? */ + if ( a == b ) { + return a; + } + + /* identical tails? */ + ForwardIterator tail_a(a); + ForwardIterator tail_b(b); + if ( ++tail_a == ++tail_b ) { + return tail_a; + } + } + + /* Build parallel lists of suffixes, ordered by increasing length. */ + + ForwardIterator lists[2] = { a, b }; + std::vector suffixes[2]; + + for ( int i=0 ; i < 2 ; i++ ) { + for ( ForwardIterator iter(lists[i]) ; iter != end ; ++iter ) { + if ( iter == lists[1-i] ) { + // the other list is a suffix of this one + return lists[1-i]; + } + suffixes[i].push_back(iter); + } + } + + /* Iterate in parallel through the lists of suffix lists from shortest to + * longest, stopping before the first pair of suffixes that differs + */ + + ForwardIterator longest_common(end); + + while ( !suffixes[0].empty() && !suffixes[1].empty() && + suffixes[0].back() == suffixes[1].back() ) + { + longest_common = suffixes[0].back(); + suffixes[0].pop_back(); + suffixes[1].pop_back(); + } + + return longest_common; +} + +} + +} + +#endif /* !SEEN_INKSCAPE_ALGORITHMS_NEAREST_COMMON_ANCESTOR_H */ + +/* + 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 : diff --git a/src/util/numeric/converters.h b/src/util/numeric/converters.h new file mode 100644 index 0000000..6feb14a --- /dev/null +++ b/src/util/numeric/converters.h @@ -0,0 +1,141 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * Utility functions to convert ascii representations to numbers + */ +#ifndef UTIL_NUM_CONVERTERS_H +#define UTIL_NUM_CONVERTERS_H +/* + * Authors: + * Felipe Corrêa da Silva Sanches + * Mohammad Aadil Shabier + * + * + * Copyright (C) 2006 Hugo Rodrigues + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include +#include +#include +#include + +namespace Inkscape { +namespace Util { + +// while calling read_number(string, false), it's not obvious, what +// that false stands for. read_number(string, NO_WARNING) +// can be more explicit. +constexpr bool NO_WARNING = false; + +/* convert ascii representation to double + * the function can only be used to convert numbers as given by gui elements that use localized representation + * @param value ascii representation of the number + * @return the converted number + * + * Setting warning to false disables conversion error warnings from + * this function. This can be useful in places, where the input type + * is not known beforehand. For example, see sp_feColorMatrix_set in + * sp-fecolormatrix.cpp + * Consider making warning = True by default since that's how it seems to be used everywhere? + */ +inline double read_number(gchar const *value, bool warning = true) +{ + if (!value) { + g_warning("Called Inkscape::Util::read_number with value==null_ptr, this can lead to unexpected behaviour."); + return 0; + } + char *end; + double ret = g_ascii_strtod(value, &end); + if (*end) { + if (warning) { + g_warning("Inkscape::Util::read_number() Unable to convert \"%s\" to number", value); + } + // We could leave this out, too. If strtod can't convert + // anything, it will return zero. + ret = 0; + } + return ret; +} + +inline bool read_bool(gchar const *value, bool default_value) +{ + if (!value) + return default_value; + switch (value[0]) { + case 't': + if (strncmp(value, "true", 4) == 0) + return true; + break; + case 'f': + if (strncmp(value, "false", 5) == 0) + return false; + break; + } + return default_value; +} + +/* convert ascii representation to double + * the function can only be used to convert numbers as given by gui elements that use localized representation + * numbers are delimited by space + * @param value ascii representation of the number + * @return the vector of the converted numbers + */ +inline std::vector read_vector(const gchar *value) +{ + std::vector v; + + gchar const *beg = value; + while (isspace(*beg) || (*beg == ',')) + beg++; + while (*beg) { + char *end; + double ret = g_ascii_strtod(beg, &end); + if (end == beg) { + g_warning("Inkscape::Util::read_vector() Unable to convert \"%s\" to number", beg); + break; + } + v.push_back(ret); + + beg = end; + while (isspace(*beg) || (*beg == ',')) + beg++; + } + return v; +} + +/* + * Format a number with any trailing zeros removed. + */ +inline std::string format_number(double val, unsigned int precision = 3) +{ + std::ostringstream out; + out.imbue(std::locale("C")); + out.precision(precision); + out << std::fixed << val; + std::string ret = out.str(); + + while(ret.find(".") != std::string::npos + && (ret.substr(ret.length() - 1, 1) == "0" + || ret.substr(ret.length() - 1, 1) == ".")) + ret.pop_back(); + + return ret; +} + + +} // namespace Util +} // namespace Inkscape + +#endif // UTIL_NUM_CONVERTERS_H + +/* + 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 : diff --git a/src/util/object-renderer.cpp b/src/util/object-renderer.cpp new file mode 100644 index 0000000..f9829e6 --- /dev/null +++ b/src/util/object-renderer.cpp @@ -0,0 +1,651 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * Symbol, marker, pattern, gradient renderer + * + * Copyright (C) 2023 Michael Kowalski + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "object-renderer.h" +#include +#include +#include +#include +#include +#include +#include +#include "color.h" +#include "display/cairo-utils.h" +#include "document.h" +#include "gradient-chemistry.h" +#include "object/sp-gradient.h" +#include "object/sp-image.h" +#include "object/sp-marker.h" +#include "object/sp-object.h" +#include "object/sp-pattern.h" +#include "object/sp-use.h" +#include "ui/svg-renderer.h" +#include "ui/widget/stroke-style.h" +#include "xml/node.h" +#include "object/sp-defs.h" +#include "object/sp-item.h" +#include "object/sp-root.h" +#include "object/sp-symbol.h" +#include "pattern-manager.h" +#include "display/drawing.h" +#include "util/scope_exit.h" +#include "ui/cache/svg_preview_cache.h" +#include "xml/href-attribute-helper.h" + +namespace Inkscape { + +// traverse nodes starting from given 'object' until visitor returns object that evaluates to true +template +bool visit_until(SPObject& object, V&& visitor) { + if (visitor(object)) return true; + + // SPUse inserts referenced object as a child; skip it + if (is(&object)) return false; + + for (auto&& child : object.children) { + if (visit_until(child, visitor)) return true; + } + + return false; +} + +const char* style_from_use_element(const char* id, SPDocument* document) { + if (!id || !*id || !document) return nullptr; + + auto root = document->getRoot(); + if (!root) return nullptr; + + const char* style = nullptr; + Glib::ustring ident = "#"; + ident += id; + + visit_until(*root, [&](SPObject& obj){ + if (auto use = cast(&obj)) { + if (auto href = Inkscape::getHrefAttribute(*use->getRepr()).second) { + if (ident == href) { + // style = use->getRepr()->attribute("style"); + style = use->getAttribute("style"); + return true; + } + } + } + return false; + }); + + return style; +} + + +SPDocument* symbols_preview_doc() { + auto buffer = R"A( + + + +)A"; + return SPDocument::createNewDocFromMem(buffer, strlen(buffer), false); +} + +Cairo::RefPtr draw_symbol(SPObject& symbol, double box_w, double box_h, double device_scale, SPDocument* preview_document, bool style_from_use) { + // Create a copy repr of the symbol with id="the_symbol" + Inkscape::XML::Node* repr = symbol.getRepr()->duplicate(preview_document->getReprDoc()); + repr->setAttribute("id", "the_symbol"); + + // First look for default style stored in + auto style = repr->attribute("inkscape:symbol-style"); + if (!style) { + // If no default style in , look in documents. + + // Read style from element pointing to this symbol? + if (style_from_use) { + // When symbols are inserted from a set into a new document, styles they may rely on + // are copied from original document and applied to the symbol. + // We need to use those styles to render symbols correctly, because some symbols only + // define geometry and no presentation attributes and defaults (black fill, no stroke) + // may be completely incorrect (for instance originals may have no fill and stroke). + auto id = symbol.getId(); + style = style_from_use_element(id, symbol.document); + } + else { + style = symbol.document->getReprRoot()->attribute("style"); + } + } + + // This is for display in Symbols dialog only + if (style) repr->setAttribute("style", style); + + // reach out to the document for CSS styles, in case symbol uses some class selectors + SPDocument::install_reference_document scoped(preview_document, symbol.document); + + preview_document->getDefs()->getRepr()->appendChild(repr); + Inkscape::GC::release(repr); + + // Uncomment this to get the preview_document documents saved (useful for debugging) + // FILE *fp = fopen (g_strconcat(id, ".svg", NULL), "w"); + // sp_repr_save_stream(preview_document->getReprDoc(), fp); + // fclose (fp); + + // Make sure preview_document is up-to-date. + preview_document->ensureUpToDate(); + + unsigned dkey = SPItem::display_key_new(1); + Inkscape::Drawing drawing; // New drawing for offscreen rendering. + drawing.setRoot(preview_document->getRoot()->invoke_show(drawing, dkey, SP_ITEM_SHOW_DISPLAY)); + auto invoke_hide_guard = scope_exit([&] { preview_document->getRoot()->invoke_hide(dkey); }); + // drawing.root()->setTransform(affine); + drawing.setExact(); // Maximum quality for blurs. + + // Make sure we have symbol in preview_document + SPObject* object_temp = preview_document->getObjectById("the_use"); + + auto item = cast(object_temp); + g_assert(item != nullptr); + + // We could use cache here, but it doesn't really work with the structure + // of this user interface and we've already cached the pixbuf in the gtklist + cairo_surface_t* s = nullptr; + // Find object's bbox in document. + // Note symbols can have own viewport... ignore for now. + Geom::OptRect dbox = item->documentVisualBounds(); + + if (dbox) { + double width = dbox->width(); + double height = dbox->height(); + + if (width == 0.0) width = 1.0; + if (height == 0.0) height = 1.0; + + auto scale = std::min(box_w / width, box_h / height); + if (scale > 1.0) { + scale = 1.0; + } + + s = render_surface(drawing, scale, *dbox, Geom::IntPoint(box_w, box_h), device_scale, nullptr, true); + } + + preview_document->getObjectByRepr(repr)->deleteObject(false); + + if (s) { + cairo_surface_set_device_scale(s, device_scale, device_scale); + } + + return Cairo::RefPtr(new Cairo::Surface(s, true)); +} + +void draw_gradient(const Cairo::RefPtr& cr, SPGradient* gradient, int x, int width) { + cairo_pattern_t* check = ink_cairo_pattern_create_checkerboard(); + + cairo_set_source(cr->cobj(), check); + cr->fill_preserve(); + cairo_pattern_destroy(check); + + if (gradient) { + auto p = gradient->create_preview_pattern(width); + cairo_matrix_t m; + cairo_matrix_init_translate(&m, -x, 0); + cairo_pattern_set_matrix(p, &m); + cairo_set_source(cr->cobj(), p); + cr->fill(); + cairo_pattern_destroy(p); + } +} + +Cairo::RefPtr draw_gradient(SPGradient* gradient, double width, double height, double device_scale, bool stops) { + auto surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, width * device_scale, height * device_scale); + cairo_surface_set_device_scale(surface->cobj(), device_scale, device_scale); + auto ctx = Cairo::Context::create(surface); + + auto h = stops ? height / 2 : height; + auto x = 0.5 * device_scale; + auto y = 0.5 * device_scale; + width -= device_scale; + h -= device_scale; + + ctx->rectangle(x, y, width, h); + draw_gradient(ctx, gradient, 0, width); + + // border + ctx->rectangle(x, y, width, h); + ctx->set_source_rgb(0.5, 0.5, 0.5); + ctx->set_line_width(1.0); + ctx->stroke(); + + if (stops) { + double radius = 3; + auto v = gradient->getVector(); + for (auto& stop : v->vector.stops) { + double py = h + 2 * radius; + double px = std::round(stop.offset * width); + ctx->arc(px, py, radius, 0, 2 * M_PI); + ctx->set_source_rgba(stop.color.v.c[0], stop.color.v.c[1], stop.color.v.c[2], stop.opacity); + ctx->fill_preserve(); + ctx->set_source_rgb(0.5, 0.5, 0.5); + ctx->stroke(); + } + } + + return surface; +} + + +std::unique_ptr ink_markers_preview_doc(const Glib::ustring& group_id) +{ +gchar const *buffer = R"A( + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +)A"; + + auto document = std::unique_ptr(SPDocument::createNewDocFromMem(buffer, strlen(buffer), false)); + // only leave requested group, so nothing else gets rendered + for (auto&& group : document->getObjectsByClass("group")) { + assert(group->getId()); + if (group->getId() != group_id) { + group->deleteObject(); + } + } + auto id = "line-" + group_id; + for (auto&& line : document->getObjectsByClass("line")) { + assert(line->getId()); + if (line->getId() != id) { + line->deleteObject(); + } + } + return document; +} + + +Cairo::RefPtr create_marker_image( + const Glib::ustring& group_id, + SPDocument* _sandbox, + Gdk::RGBA marker_color, + Geom::IntPoint pixel_size, + const char* mname, + SPDocument* source, + Inkscape::Drawing& drawing, + std::optional checkerboard, + bool no_clip, + double scale, + int device_scale) +{ + Cairo::RefPtr g_bad_marker; + + // Retrieve the marker named 'mname' from the source SVG document + const SPObject* marker = source ? source->getObjectById(mname) : nullptr; + if (marker == nullptr) { + g_warning("bad mname: %s", mname); + return g_bad_marker; + } + + SPObject *oldmarker = _sandbox->getObjectById("sample"); + if (oldmarker) { + oldmarker->deleteObject(false); + } + + // Create a copy repr of the marker with id="sample" + Inkscape::XML::Document *xml_doc = _sandbox->getReprDoc(); + Inkscape::XML::Node *mrepr = marker->getRepr()->duplicate(xml_doc); + mrepr->setAttribute("id", "sample"); + + // Replace the old sample in the sandbox by the new one + Inkscape::XML::Node *defsrepr = _sandbox->getObjectById("defs")->getRepr(); + + // TODO - This causes a SIGTRAP on windows + defsrepr->appendChild(mrepr); + + Inkscape::GC::release(mrepr); + + // If the marker color is a url link to a pattern or gradient copy that too + SPObject *mk = source->getObjectById(mname); + SPCSSAttr *css_marker = sp_css_attr_from_object(mk->firstChild(), SP_STYLE_FLAG_ALWAYS); + //const char *mfill = sp_repr_css_property(css_marker, "fill", "none"); + const char *mstroke = sp_repr_css_property(css_marker, "fill", "none"); + + if (!strncmp (mstroke, "url(", 4)) { + SPObject *linkObj = getMarkerObj(mstroke, source); + if (linkObj) { + Inkscape::XML::Node *grepr = linkObj->getRepr()->duplicate(xml_doc); + SPObject *oldmarker = _sandbox->getObjectById(linkObj->getId()); + if (oldmarker) { + oldmarker->deleteObject(false); + } + defsrepr->appendChild(grepr); + Inkscape::GC::release(grepr); + + if (is(linkObj)) { + SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(cast(linkObj), false); + if (vector) { + Inkscape::XML::Node *grepr = vector->getRepr()->duplicate(xml_doc); + SPObject *oldmarker = _sandbox->getObjectById(vector->getId()); + if (oldmarker) { + oldmarker->deleteObject(false); + } + defsrepr->appendChild(grepr); + Inkscape::GC::release(grepr); + } + } + } + } + +// Uncomment this to get the sandbox documents saved (useful for debugging) + // FILE *fp = fopen (g_strconcat(combo_id, mname, ".svg", nullptr), "w"); + // sp_repr_save_stream(_sandbox->getReprDoc(), fp); + // fclose (fp); + + // object to render; note that the id is the same as that of the combo we're building + SPObject *object = _sandbox->getObjectById(group_id); + + if (object == nullptr || !is(object)) { + g_warning("no obj: %s", group_id.c_str()); + return g_bad_marker; + } + + Gdk::RGBA fg = marker_color; + auto fgcolor = rgba_to_css_color(fg); + fg.set_red(1 - fg.get_red()); + fg.set_green(1 - fg.get_green()); + fg.set_blue(1 - fg.get_blue()); + auto bgcolor = rgba_to_css_color(fg); + auto objects = _sandbox->getObjectsBySelector(".colors"); + for (auto el : objects) { + if (SPCSSAttr* css = sp_repr_css_attr(el->getRepr(), "style")) { + sp_repr_css_set_property(css, "fill", bgcolor.c_str()); + sp_repr_css_set_property(css, "stroke", fgcolor.c_str()); + el->changeCSS(css, "style"); + sp_repr_css_attr_unref(css); + } + } + + auto cross = _sandbox->getObjectsBySelector(".cross"); + double stroke = 0.5; + for (auto el : cross) { + if (SPCSSAttr* css = sp_repr_css_attr(el->getRepr(), "style")) { + sp_repr_css_set_property(css, "display", checkerboard ? "block" : "none"); + sp_repr_css_set_property_double(css, "stroke-width", stroke); + el->changeCSS(css, "style"); + sp_repr_css_attr_unref(css); + } + } + + // SPDocument::install_reference_document scoped(_sandbox, source); + + _sandbox->getRoot()->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + _sandbox->ensureUpToDate(); + + auto item = cast(object); + // Find object's bbox in document + Geom::OptRect dbox = item->documentVisualBounds(); + + if (!dbox) { + g_warning("no dbox"); + return g_bad_marker; + } + + if (auto measure = cast(_sandbox->getObjectById("measure-marker"))) { + if (auto box = measure->documentVisualBounds()) { + // check size of the marker applied to a path with stroke of 1px + auto size = std::max(box->width(), box->height()); + const double small = 5.0; + // if too small, then scale up; clip needs to be enabled for scale to work + if (size > 0 && size < small) { + auto factor = 1 + small - size; + scale *= factor; + no_clip = false; + + // adjust cross stroke + stroke /= factor; + for (auto el : cross) { + if (SPCSSAttr* css = sp_repr_css_attr(el->getRepr(), "style")) { + sp_repr_css_set_property_double(css, "stroke-width", stroke); + el->changeCSS(css, "style"); + sp_repr_css_attr_unref(css); + } + } + + _sandbox->getRoot()->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG); + _sandbox->ensureUpToDate(); + } + } + } + + /* Update to renderable state */ + // const double device_scale = get_scale_factor(); + guint32 bgnd_color = checkerboard.has_value() ? *checkerboard : 0; + auto surface = render_surface(drawing, scale, *dbox, pixel_size, device_scale, checkerboard.has_value() ? &bgnd_color : nullptr, no_clip); + cairo_surface_set_device_scale(surface, device_scale, device_scale); + return Cairo::RefPtr(new Cairo::Surface(surface, true)); +} + +Cairo::RefPtr render_image(const Inkscape::Pixbuf* pixbuf, int width, int height, int device_scale) { + Cairo::RefPtr surface; + + if (!pixbuf || width <= 0 || height <= 0 || pixbuf->width() <= 0 || pixbuf->height() <= 0) return surface; + + auto src = Cairo::RefPtr(new Cairo::Surface(pixbuf->getSurfaceRaw(), false)); + surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, width * device_scale, height * device_scale); + cairo_surface_set_device_scale(surface->cobj(), device_scale, device_scale); + + auto ctx = Cairo::Context::create(surface); + + double sw = pixbuf->width(); + double sh = pixbuf->height(); + double sx = sw / width; + double sy = sh / height; + auto scale = 1.0 / std::max(sx, sy); + double dx = width - scale * sw; + double dy = height - scale * sh; + + ctx->translate(dx / 2, dy / 2); + ctx->scale(scale, scale); + ctx->set_source(src, 0, 0); + ctx->set_operator(Cairo::OPERATOR_OVER); + ctx->paint(); + + return surface; +} + +Cairo::RefPtr add_background_to_image(Cairo::RefPtr image, uint32_t rgb, double margin, double radius, int device_scale, std::optional border) { + auto w = image ? cairo_image_surface_get_width(image->cobj()) : 0; + auto h = image ? cairo_image_surface_get_height(image->cobj()) : 0; + auto width = w / device_scale + 2 * margin; + auto height = h / device_scale + 2 * margin; + + auto surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, width * device_scale, height * device_scale); + cairo_surface_set_device_scale(surface->cobj(), device_scale, device_scale); + auto ctx = Cairo::Context::create(surface); + + auto x = 0; + auto y = 0; + if (border.has_value()) { + x += 0.5 * device_scale; + y += 0.5 * device_scale; + width -= device_scale; + height -= device_scale; + } + ctx->arc(x + width - radius, y + radius, radius, -M_PI_2, 0); + ctx->arc(x + width - radius, y + height - radius, radius, 0, M_PI_2); + ctx->arc(x + radius, y + height - radius, radius, M_PI_2, M_PI); + ctx->arc(x + radius, y + radius, radius, M_PI, 3 * M_PI_2); + ctx->close_path(); + + ctx->set_source_rgb(SP_RGBA32_R_F(rgb), SP_RGBA32_G_F(rgb), SP_RGBA32_B_F(rgb)); + if (border.has_value()) { + ctx->fill_preserve(); + + auto b = *border; + ctx->set_source_rgb(SP_RGBA32_R_F(b), SP_RGBA32_G_F(b), SP_RGBA32_B_F(b)); + ctx->set_line_width(1.0); + ctx->stroke(); + } + else { + ctx->fill(); + } + + if (image) { + ctx->set_source(image, margin, margin); + ctx->paint(); + } + + return surface; +} + +Cairo::RefPtr draw_frame(Cairo::RefPtr image, double image_alpha, uint32_t frame_rgba, double thickness, std::optional checkerboard_color, int device_scale) { + if (!image) return image; + + auto w = cairo_image_surface_get_width(image->cobj()); + auto h = cairo_image_surface_get_height(image->cobj()); + auto width = w / device_scale + 2 * thickness; + auto height = h / device_scale + 2 * thickness; + + auto surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, width * device_scale, height * device_scale); + cairo_surface_set_device_scale(surface->cobj(), device_scale, device_scale); + auto ctx = Cairo::Context::create(surface); + + if (checkerboard_color) { + Cairo::RefPtr pattern(new Cairo::Pattern(ink_cairo_pattern_create_checkerboard(*checkerboard_color))); + ctx->save(); + ctx->set_operator(Cairo::OPERATOR_SOURCE); + ctx->set_source(pattern); + ctx->rectangle(thickness, thickness, width - 2*thickness, height - 2*thickness); + ctx->fill(); + ctx->restore(); + } + + ctx->rectangle(thickness / 2, thickness / 2, width - thickness, height - thickness); + + if (thickness > 0) { + ctx->set_source_rgba(SP_RGBA32_R_F(frame_rgba), SP_RGBA32_G_F(frame_rgba), SP_RGBA32_B_F(frame_rgba), SP_RGBA32_A_F(frame_rgba)); + ctx->set_line_width(thickness); + ctx->stroke(); + } + + ctx->set_source(image, thickness, thickness); + ctx->paint_with_alpha(image_alpha); + + return surface; +} + + +object_renderer:: object_renderer() { +} + +Cairo::RefPtr object_renderer::render(SPObject& object, double width, double height, double device_scale, object_renderer::options opt) { + + Cairo::RefPtr surface; + if (opt._draw_frame) { + width -= 2 * opt._stroke; + height -= 2 * opt._stroke; + } + if (width <= 0 || height <= 0) return surface; + + if (is(&object)) { + if (!_symbol_document) { + _symbol_document.reset(symbols_preview_doc()); + } + surface = draw_symbol(object, width, height, device_scale, _symbol_document.get(), opt._symbol_style_from_use); + } + else if (is(&object)) { + const auto group = "marker-mid"; + if (!_sandbox) { + _sandbox = ink_markers_preview_doc(group); + } + std::optional checkerboard; // rgb background color + bool no_clip = true; + double scale = 1.0; + + unsigned const dkey = SPItem::display_key_new(1); + Inkscape::Drawing drawing; // New drawing for offscreen rendering. + drawing.setRoot(_sandbox->getRoot()->invoke_show(drawing, dkey, SP_ITEM_SHOW_DISPLAY)); + auto invoke_hide_guard = scope_exit([&] { _sandbox->getRoot()->invoke_hide(dkey); }); + drawing.setExact(); // Maximum quality for blurs. + + surface = create_marker_image(group, _sandbox.get(), opt._foreground, Geom::IntPoint(width, height), object.getId(), + object.document, drawing, checkerboard, no_clip, scale, device_scale); + } + else if (is(&object)) { + surface = draw_gradient(cast(&object), width, height, device_scale, false); + } + else if (auto pattern = cast(&object)) { + surface = PatternManager::get().get_image(pattern, width, height, device_scale); + } + else if (auto image = cast(&object)) { + surface = render_image(image->pixbuf.get(), width, height, device_scale); + } + else { + g_warning("object_renderer: don't know how to render this object type"); + } + + if (opt._add_background) { + surface = add_background_to_image(surface, opt._background, opt._margin, opt._radius, device_scale); + } + + // extra decorators: frame, opacity change, checkerboard background + if (opt._draw_frame || opt._image_opacity != 1 || opt._checkerboard.has_value()) { + surface = draw_frame(surface, opt._image_opacity, opt._frame_rgba, opt._stroke, opt._checkerboard, device_scale); + } + + return surface; +} + +} // namespace diff --git a/src/util/object-renderer.h b/src/util/object-renderer.h new file mode 100644 index 0000000..98f8890 --- /dev/null +++ b/src/util/object-renderer.h @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#ifndef SEEN_OBJECT_RENDERER_H +#define SEEN_OBJECT_RENDERER_H + +#include +#include +#include +#include +#include +#include +#include "display/drawing.h" +#include "object/sp-object.h" +#include "document.h" + +namespace Inkscape { + +class object_renderer { +public: + object_renderer(); + + struct options { + options() {} + // foreground color, where used + options& foreground(Gdk::RGBA fg) { + _foreground = fg; + return *this; + } + // background color, where used + options& solid_background(uint32_t bg, double margin, double corner_radius = 0) { + _add_background = true; + _background = bg; + _margin = margin; + _radius = corner_radius; + return *this; + } + // use checkerboard pattern for drawing background + options& checkerboard(uint32_t color) { + _checkerboard = color; + return *this; + } + // option to add an outline to rendered image + options& frame(uint32_t rgba, double thickness = 1) { + _stroke = thickness; + _draw_frame = true; + _frame_rgba = rgba; + return *this; + } + // option to reduce opacity of rendered image + options& image_opacity(double alpha) { + _image_opacity = alpha; + return *this; + } + // for symbols only: take style from element + options& symbol_style_from_use(bool from_use_element = true) { + _symbol_style_from_use = from_use_element; + return *this; + } + + private: + friend class object_renderer; + Gdk::RGBA _foreground; + bool _add_background = false; + uint32_t _background; + double _margin = 0; + double _radius = 0; + bool _symbol_style_from_use = false; + bool _draw_frame = false; + double _stroke = 0; + uint32_t _frame_rgba = 0; + double _image_opacity = 1; + std::optional _checkerboard; + }; + + Cairo::RefPtr render(SPObject& object, double width, double height, double device_scale, options options = {}); + +private: + std::unique_ptr _symbol_document; + std::unique_ptr _sandbox; +}; + +// Place 'image' on a solid background with given color optinally adding border. +// If no image is provided, only background surface will be created. +Cairo::RefPtr add_background_to_image(Cairo::RefPtr image, uint32_t rgb, double margin, double radius, int device_scale, std::optional border = std::optional()); + +/** + * Returns a new document containing default start, mid, and end markers. + * Note 1: group IDs are matched against "group_id" to render correct preview object. + * Note 2: paths/lines are kept outside of groups, so they don't inflate visible bounds + * Note 3: invisible rects inside groups keep visual bounds from getting too small, so we can see relative marker sizes + */ +std::unique_ptr ink_markers_preview_doc(const Glib::ustring& group_id); + +/** + * Creates a copy of the marker named mname, determines its visible and renderable + * area in the bounding box, and then renders it. This allows us to fill in + * preview images of each marker in the marker combobox. + */ +Cairo::RefPtr create_marker_image( + const Glib::ustring& group_id, + SPDocument* _sandbox, + Gdk::RGBA marker_color, + Geom::IntPoint pixel_size, + const char* mname, + SPDocument* source, + Inkscape::Drawing& drawing, + std::optional checkerboard, + bool no_clip, + double scale, + int device_scale); + +} // namespace + +#endif // SEEN_OBJECT_RENDERER_H diff --git a/src/util/optstr.h b/src/util/optstr.h new file mode 100644 index 0000000..f8a0b4c --- /dev/null +++ b/src/util/optstr.h @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#ifndef INKSCAPE_UTIL_OPTSTR_H +#define INKSCAPE_UTIL_OPTSTR_H +/* + * Author: PBS + * Copyright (C) 2022 Authors + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include +#include + +namespace Inkscape { +namespace Util { + +inline bool equal(std::optional const &a, char const *b) +{ + return a && b ? *a == b : !a && !b; +} + +inline auto to_opt(char const *s) +{ + return s ? std::make_optional(s) : std::nullopt; +} + +inline auto to_cstr(std::optional const &s) +{ + return s ? s->c_str() : nullptr; +} + +inline bool assign(std::optional &a, char const *b) +{ + if (equal(a, b)) return false; + a = to_opt(b); + return true; +} + +} // namespace Util +} // namespace Inkscape + +#endif // INKSCAPE_UTIL_OPTSTR_H diff --git a/src/util/pages-skeleton.h b/src/util/pages-skeleton.h new file mode 100644 index 0000000..ac984e4 --- /dev/null +++ b/src/util/pages-skeleton.h @@ -0,0 +1,154 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * @file + * List of paper sizes + */ +/* + * Authors: + * bulia byak + * Lauris Kaplinski + * Jon Phillips + * Ralf Stephan (Gtkmm) + * Bob Jamison + * Abhishek Sharma + * + see git history + * + * Copyright (C) 2000 - 2018 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_PAGES_SKELETON_H +#define SEEN_PAGES_SKELETON_H + + + /** \note + * The ISO page sizes in the table below differ from ghostscript's idea of page sizes (by + * less than 1pt). Being off by <1pt should be OK for most purposes, but may cause fuzziness + * (antialiasing) problems when printing to 72dpi or 144dpi printers or bitmap files due to + * postscript's different coordinate system (y=0 meaning bottom of page in postscript and top + * of page in SVG). I haven't looked into whether this does in fact cause fuzziness, I merely + * note the possibility. Rounding done by extension/internal/ps.cpp (e.g. floor/ceil calls) + * will also affect whether fuzziness occurs. + * + * The remainder of this comment discusses the origin of the numbers used for ISO page sizes in + * this table and in ghostscript. + * + * The versions here, in mm, are the official sizes according to + * http://en.wikipedia.org/wiki/Paper_sizes + * at 2005-01-25. (The ISO entries in the below table + * were produced mechanically from the table on that page.) + * + * (The rule seems to be that A0, B0, ..., D0. sizes are rounded to the nearest number of mm + * from the "theoretical size" (i.e. 1000 * sqrt(2) or pow(2.0, .25) or the like), whereas + * going from e.g. A0 to A1 always take the floor of halving -- which by chance coincides + * exactly with flooring the "theoretical size" for n != 0 instead of the rounding to nearest + * done for n==0.) + * + * Ghostscript paper sizes are given in gs_statd.ps according to gs(1). gs_statd.ps always + * uses an integer number ofpt: sometimes gs_statd.ps rounds to nearest (e.g. a1), sometimes + * floors (e.g. a10), sometimes ceils (e.g. a8). + * + * I'm not sure how ghostscript's gs_statd.ps was calculated: it isn't just rounding the + * "theoretical size" of each page topt (see a0), nor is it rounding the a0 size times an + * appropriate power of two (see a1). Possibly it was prepared manually, with a human applying + * inconsistent rounding rules when converting from mm to pt. + */ + /** \todo + * Should we include the JIS B series (used in Japan) + * (JIS B0 is sometimes called JB0, and similarly for JB1 etc)? + * Should we exclude B7--B10 and A7--10 to make the list smaller ? + * Should we include any of the ISO C, D and E series (see below) ? + */ + + + + /* See http://www.hbp.com/content/PCR_envelopes.cfm for a much larger list of US envelope + sizes. */ + /* Note that `Folio' (used in QPrinter/KPrinter) is deliberately absent from this list, as it + means different sizes to different people: different people may expect the width to be + either 8, 8.25 or 8.5 inches, and the height to be either 13 or 13.5 inches, even + restricting our interpretation to foolscap folio. If you wish to introduce a folio-like + page size to the list, then please consider using a name more specific than just `Folio' or + `Foolscap Folio'. */ + +static char const pages_skeleton[] = R"(#Inkscape page sizes +#NAME, WIDTH, HEIGHT, UNIT +A4, 210, 297, mm +US Letter, 8.5, 11, in +US Legal, 8.5, 14, in +US Executive, 7.25, 10.5, in +US Ledger/Tabloid, 11, 17, in +A0, 841, 1189, mm +A1, 594, 841, mm +A2, 420, 594, mm +A3, 297, 420, mm +A5, 148, 210, mm +A6, 105, 148, mm +A7, 74, 105, mm +A8, 52, 74, mm +A9, 37, 52, mm +A10, 26, 37, mm +B0, 1000, 1414, mm +B1, 707, 1000, mm +B2, 500, 707, mm +B3, 353, 500, mm +B4, 250, 353, mm +B5, 176, 250, mm +B6, 125, 176, mm +B7, 88, 125, mm +B8, 62, 88, mm +B9, 44, 62, mm +B10, 31, 44, mm +C0, 917, 1297, mm +C1, 648, 917, mm +C2, 458, 648, mm +C3, 324, 458, mm +C4, 229, 324, mm +C5, 162, 229, mm +C6, 114, 162, mm +C7, 81, 114, mm +C8, 57, 81, mm +C9, 40, 57, mm +C10, 28, 40, mm +D1, 545, 771, mm +D2, 385, 545, mm +D3, 272, 385, mm +D4, 192, 272, mm +D5, 136, 192, mm +D6, 96, 136, mm +D7, 68, 96, mm +E3, 400, 560, mm +E4, 280, 400, mm +E5, 200, 280, mm +E6, 140, 200, mm +CSE, 462, 649, pt +US #10 Envelope, 9.5, 4.125, in +DL Envelope, 220, 110, mm +Banner 468x60, 468, 60, px +Icon 16x16, 16, 16, px +Icon 32x32, 32, 32, px +Icon 48x48, 48, 48, px +ID Card (ISO 7810), 85.60, 53.98, mm +Business Card (US), 3.5, 2, in +Business Card (Europe), 85, 55, mm +Business Card (AU/NZ), 90, 55, mm +Arch A, 9, 12, in +Arch B, 12, 18, in +Arch C, 18, 24, in +Arch D, 24, 36, in +Arch E, 36, 48, in +Arch E1, 30, 42, in +Video SD / PAL, 768, 576, px +Video SD-Widescreen / PAL, 1024, 576, px +Video SD / NTSC, 544, 480, px +Video SD-Widescreen / NTSC, 872, 486, px +Video HD 720p, 1280, 720, px +Video HD 1080p, 1920, 1080, px +Video DCI 2k (Full Frame), 2048, 1080, px +Video UHD 4k, 3840, 2160, px +Video DCI 4k (Full Frame), 4096, 2160, px +Video UHD 8k, 7680, 4320, px +)"; + +#endif diff --git a/src/util/paper.cpp b/src/util/paper.cpp new file mode 100644 index 0000000..93668e8 --- /dev/null +++ b/src/util/paper.cpp @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape PaperSize + * + * Authors: + * Bob Jamison (2006) + * Martin Owens (2021) + * + * Copyright (C) 2021 AUTHORS + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include + +#include "paper.h" +#include "pages-skeleton.h" + +#include "io/resource.h" + +namespace Inkscape { + +/** + * Returns a list of page sizes. + */ +const std::vector& PaperSize::getPageSizes() +{ + // Static makes us only load pages once. + static std::vector ret; + if (!ret.empty()) + return ret; + auto path = Inkscape::IO::Resource::profile_path("pages.csv"); + if (!g_file_test(path.c_str(), G_FILE_TEST_EXISTS)) { + if (!g_file_set_contents(path.c_str(), pages_skeleton, -1, nullptr)) { + g_warning("%s", _("Failed to create the page file.")); + } + } + gchar *content = nullptr; + if (g_file_get_contents(path.c_str(), &content, nullptr, nullptr)) { + gchar **lines = g_strsplit_set(content, "\n", 0); + + for (int i = 0; lines && lines[i]; ++i) { + gchar **line = g_strsplit_set(lines[i], ",", 5); + if (!line[0] || !line[1] || !line[2] || !line[3] || line[0][0]=='#') + continue; + + //name, width, height, unit + double width = g_ascii_strtod(line[1], nullptr); + double height = g_ascii_strtod(line[2], nullptr); + g_strstrip(line[0]); + g_strstrip(line[3]); + Glib::ustring name = line[0]; + ret.push_back(PaperSize(name, width, height, Inkscape::Util::unit_table.getUnit(line[3]))); + } + g_strfreev(lines); + g_free(content); + } + return ret; +} + + +PaperSize::PaperSize() + : name("") + , width(0.0) + , height(0.0) +{ + unit = Inkscape::Util::unit_table.getUnit("px"); +} + + +PaperSize::PaperSize(std::string name, double width, double height, Inkscape::Util::Unit const *unit) + : name(std::move(name)) + , width(width) + , height(height) + , unit(unit) +{} + +std::string PaperSize::getDescription(bool landscape) const { + return toDescription(name, size[landscape], size[!landscape], unit); +} + +std::string PaperSize::toDimsString(double x, double y, Util::Unit const *unit) +{ + return formatNumber(x) + " × " + formatNumber(y) + " " + unit->abbr; +} + +std::string PaperSize::toDescription(std::string name, double x, double y, Inkscape::Util::Unit const *unit) +{ + if (!name.empty()) { + name = _(name.c_str()); + } + return name + " (" + toDimsString(x, y, unit) + ")"; +} + +std::string PaperSize::formatNumber(double val) +{ + char buf[20]; + snprintf(buf, 19, "%0.1f", val); + auto ret = std::string(buf); + // C++ doesn't provide a good number formatting control, so hack off trailing zeros. + if ((ret.length() > 2) && (ret.back() == '0')) { + ret = ret.substr(0, ret.length() - 2); + } + return ret; +} + +void PaperSize::assign(const PaperSize &other) +{ + name = other.name; + width = other.width; + height = other.height; + auto [smaller, larger] = std::minmax(width, height); + size = Geom::Point(smaller, larger); + unit = other.unit; +} + +/** + * Returns a matching paper size, if possible. + */ +const PaperSize *PaperSize::findPaperSize(double width, double height, Inkscape::Util::Unit const *unit) +{ + auto [smaller, larger] = std::minmax(width, height); + auto size = Geom::Point(smaller, larger); + auto px = Inkscape::Util::unit_table.getUnit("px"); + + for (auto&& page_size : Inkscape::PaperSize::getPageSizes()) { + auto cmp = Geom::Point( + unit->convert(size[0], page_size.unit), + unit->convert(size[1], page_size.unit) + ); + // We want a half a pixel tollerance to catch floating point errors + auto tollerance = px->convert(0.5, page_size.unit); + if (Geom::are_near(page_size.size, cmp, tollerance)) { + return &page_size; + } + } + return nullptr; +} + +} // namespace Inkscape + +/* + 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 : diff --git a/src/util/paper.h b/src/util/paper.h new file mode 100644 index 0000000..fbefcd5 --- /dev/null +++ b/src/util/paper.h @@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape Paper Sizes + * + * Authors: + * + * Copyright (C) 2013 AUTHORS + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include +#include +#include <2geom/point.h> + +#include "units.h" + +#ifndef INKSCAPE_UTIL_PAPER_H +#define INKSCAPE_UTIL_PAPER_H + +namespace Inkscape { + +/** + * Data class used to store common paper dimensions from pages.csv + */ +class PaperSize +{ +public: + PaperSize(); + PaperSize(std::string name, double width, double height, Inkscape::Util::Unit const *unit); + PaperSize(const PaperSize &other) { assign(other); } + PaperSize &operator=(const PaperSize &other) { assign(other); return *this; } + + ~PaperSize() = default; + + std::string name; + Geom::Point size; + double width; + double height; + Inkscape::Util::Unit const *unit; /// pointer to object in UnitTable, do not delete + + std::string getDescription(bool landscape) const; + static std::string toDimsString(double x, double y, Util::Unit const *unit); + + static const std::vector& getPageSizes(); + static const PaperSize *findPaperSize(double width, double height, Inkscape::Util::Unit const *unit); + + static std::string toDescription(std::string name, double x, double y, Inkscape::Util::Unit const *unit); +private: + void assign(const PaperSize &other); + static std::string formatNumber(double val); +}; + +} // namespace Inkscape + +#endif // define INKSCAPE_UTIL_UNITS_H +/* + 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 : diff --git a/src/util/parse-int-range.h b/src/util/parse-int-range.h new file mode 100644 index 0000000..aac18d9 --- /dev/null +++ b/src/util/parse-int-range.h @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Parse a string containing number ranges. + * + * Copyright (C) 2022 Martin Owens + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ +#ifndef INKSCAPE_UTIL_PARSE_INT_RANGE_H +#define INKSCAPE_UTIL_PARSE_INT_RANGE_H + +#include +#include +#include + +namespace Inkscape { + +/** + * Parse integer ranges out of a string using regex. + * + * @param input - A string containing number ranges that can either be comma + * separated or dash separated for non and continuous ranges. + * @param start - Optional first number in the acceptable range. + * @param end - The last number in the acceptable range. + * + * @returns a sorted set of unique numbers. + */ +inline std::set parseIntRange(const std::string &input, unsigned int start=1, unsigned int end=0) +{ + // Special word based translations go here: + if (input == "all") { + return parseIntRange("-", start, end); + } + + std::set out; + auto add = [=](std::set &to, unsigned int val) { + if (start <= val && (!end || val <= end)) + to.insert(val); + }; + + std::regex re("((\\d+|)\\s?(-)\\s?(\\d+|)|,?(\\d+)([^-]|$))"); + + std::string::const_iterator sit = input.cbegin(), fit = input.cend(); + for (std::smatch match; std::regex_search(sit, fit, match, re); sit = match[0].second) + { + if (match.str(3) == "") { + add(out, std::stoul(match.str(5))); + } else { + auto r1 = match.str(2).empty() ? start : std::stoul(match.str(2)); + auto r2 = match.str(4).empty() ? (end ? end : r1) : std::stoul(match.str(4)); + for (auto i = std::min(r1, r2); i <= std::max(r1, r2); i++) { + add(out, i); + } + } + } + + return out; +} + +} // namespace Inkscape + +#endif + +/* + 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 : diff --git a/src/util/pool.cpp b/src/util/pool.cpp new file mode 100644 index 0000000..c49b8ea --- /dev/null +++ b/src/util/pool.cpp @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include "pool.h" +#include +#include + +namespace Inkscape { +namespace Util { + +// Round up x to the next multiple of m. +static std::byte *roundup(std::byte *x, std::size_t m) +{ + auto y = reinterpret_cast(x); + y = ((y - 1) / m + 1) * m; + return reinterpret_cast(y); +} + +std::byte *Pool::allocate(std::size_t size, std::size_t alignment) +{ + auto a = roundup(cur, alignment); + auto b = a + size; + + if (b <= end) { + cur = b; + return a; + } + + cursize = std::max(nextsize, size + alignment - 1); + buffers.emplace_back(std::make_unique(cursize)); + // buffers.emplace_back(std::make_unique_for_overwrite(cursize)); // Todo: C++20. + resetblock(); + nextsize = cursize * 3 / 2; + + a = roundup(cur, alignment); + b = a + size; + + assert(b <= end); + cur = b; + return a; +}; + +void Pool::free_all() noexcept +{ + if (buffers.empty()) return; + if (buffers.size() > 1) { + buffers.front() = std::move(buffers.back()); + buffers.resize(1); + } + resetblock(); +} + +void Pool::movefrom(Pool &other) noexcept +{ + buffers = std::move(other.buffers); + cur = other.cur; + end = other.end; + cursize = other.cursize; + nextsize = other.nextsize; + + other.buffers.clear(); + other.cur = nullptr; + other.end = nullptr; + other.cursize = 0; + other.nextsize = 2; +} + +void Pool::resetblock() noexcept +{ + assert(!buffers.empty()); + cur = buffers.back().get(); + end = cur + cursize; +} + +} // namespace Util +} // namespace Inkscape diff --git a/src/util/pool.h b/src/util/pool.h new file mode 100644 index 0000000..544ece6 --- /dev/null +++ b/src/util/pool.h @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** \file Pool + * Block allocator optimised for many allocations that are freed all at once. + */ +#ifndef INKSCAPE_UTIL_POOL_H +#define INKSCAPE_UTIL_POOL_H + +#include +#include +#include +#include + +namespace Inkscape { +namespace Util { + +/** + * A Pool is a block allocator with the following characteristics: + * + * - When a block cannot be allocated from the current buffer, a new 50% larger buffer is requested. + * - When all blocks are freed, the largest buffer is retained. + * + * The only difference from std::pmr::monotonic_buffer_resource is the second point. + * Like std::pmr::monotonic_buffer_resource, it is also not thread-safe. + */ +class Pool final +{ +public: + Pool() = default; + Pool(Pool const &) = delete; + Pool &operator=(Pool const &) = delete; + Pool(Pool &&other) noexcept { movefrom(other); } + Pool &operator=(Pool &&other) noexcept { movefrom(other); return *this; } + + /// Ensure that the next buffer requested has at least the specified size. + void reserve(std::size_t size) { nextsize = std::max(nextsize, size); } + + /// Allocate a block of the given size and alignment. + std::byte *allocate(std::size_t size, std::size_t alignment); + + /// Convenience function: allocate a block of size and aligment for T. + template + T *allocate() { return reinterpret_cast(allocate(sizeof(T), alignof(T))); } + + /// Free all previous allocations, retaining the largest existing buffer for re-use. + void free_all() noexcept; + +private: + std::vector> buffers; + std::byte *cur = nullptr, *end = nullptr; + std::size_t cursize = 0, nextsize = 2; + + void movefrom(Pool &other) noexcept; + void resetblock() noexcept; +}; + +} // namespace Util +} // namespace Inkscape + +#endif // INKSCAPE_UTIL_POOL_H diff --git a/src/util/preview.cpp b/src/util/preview.cpp new file mode 100644 index 0000000..58b2f64 --- /dev/null +++ b/src/util/preview.cpp @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * @file + * Utility functions for generating export previews. + */ +/* Authors: + * Anshudhar Kumar Singh + * Martin Owens + * + * Copyright (C) 2021 Anshudhar Kumar Singh + * 2021 Martin Owens + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "preview.h" + +#include "document.h" +#include "display/cairo-utils.h" +#include "display/drawing-context.h" +#include "object/sp-namedview.h" +#include "object/sp-root.h" +#include "async/async.h" + +namespace Inkscape { +namespace UI { +namespace Preview { + +Cairo::RefPtr +render_preview(SPDocument *doc, std::shared_ptr drawing, uint32_t bg, + Inkscape::DrawingItem *item, unsigned width_in, unsigned height_in, Geom::Rect const &dboxIn) +{ + if (!drawing->root()) + return {}; + + // Calculate a scaling factor for the requested bounding box. + double sf = 1.0; + Geom::IntRect ibox = dboxIn.roundOutwards(); + if (ibox.width() != width_in || ibox.height() != height_in) { + sf = std::min((double)width_in / dboxIn.width(), + (double)height_in / dboxIn.height()); + auto scaled_box = dboxIn * Geom::Scale(sf); + ibox = scaled_box.roundOutwards(); + } + + auto pdim = Geom::IntPoint(width_in, height_in); + // The unsigned width/height can wrap around when negative. + int dx = ((int)width_in - ibox.width()) / 2; + int dy = ((int)height_in - ibox.height()) / 2; + auto area = Geom::IntRect::from_xywh(ibox.min() - Geom::IntPoint(dx, dy), pdim); + + /* Actual renderable area */ + Geom::IntRect ua = *Geom::intersect(ibox, area); + auto surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, ua.width(), ua.height()); + + { + auto cr = Cairo::Context::create(surface); + cr->rectangle(0, 0, ua.width(), ua.height()); + + // We always use checkerboard to indicate transparency. + if (SP_RGBA32_A_F(bg) < 1.0) { + auto pattern = ink_cairo_pattern_create_checkerboard(bg, false); + auto background = Cairo::RefPtr(new Cairo::Pattern(pattern, true)); + cr->set_source(background); + cr->fill(); + } + + // We always draw the background on top to indicate partial backgrounds. + cr->set_source_rgba(SP_RGBA32_R_F(bg), SP_RGBA32_G_F(bg), SP_RGBA32_B_F(bg), SP_RGBA32_A_F(bg)); + cr->fill(); + } + + // Resize the contents to the available space with a scale factor. + drawing->root()->setTransform(Geom::Scale(sf)); + drawing->update(); + + auto dc = Inkscape::DrawingContext(surface->cobj(), ua.min()); + if (item) { + // Render just one item + item->render(dc, ua); + } else { + // Render drawing. + drawing->render(dc, ua); + } + + surface->flush(); + return surface; +} + +} // namespace Preview +} // namespace UI +} // namespace Inkscape diff --git a/src/util/preview.h b/src/util/preview.h new file mode 100644 index 0000000..00fd69f --- /dev/null +++ b/src/util/preview.h @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * @file + * Utility functions for previewing icon representation. + */ +/* Authors: + * Jon A. Cruz + * Bob Jamison + * Other dudes from The Inkscape Organization + * Abhishek Sharma + * Anshudhar Kumar Singh + * + * Copyright (C) 2004 Bob Jamison + * Copyright (C) 2005,2010 Jon A. Cruz + * Copyright (C) 2021 Anshudhar Kumar Singh + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef INKSCAPE_UTIL_PREVIEW_H +#define INKSCAPE_UTIL_PREVIEW_H + +#include +#include + +#include "display/drawing.h" +#include "async/channel.h" + +class SPDocument; +class SPItem; + +namespace Inkscape { +namespace UI { +namespace Preview { + +/** + * Launch a background task to render a drawing to a surface. + * + * If the area to render is invalid, nothing is returned and no action is taken. + * Otherwise, first the drawing is snapshotted, then an async task is launched to render the drawing to a surface. + * Upon completion, the drawing is unsnapshotted on the calling thread and the result passed to onfinished(). + * If the return object is destroyed before this happens, then the drawing will instead be destroyed on an unspecified + * thread while still in the snapshotted state. + * + * Contracts: (This isn't Rust, so we need a comment instead, and great trust in the caller.) + * + * - The caller must ensure onfinished() remains valid to call during the lifetime of the return object. + * (This is the same as for sigc::slots and connections.) + * + * - The caller must not call drawing->unsnapshot(), or any other method that bypasses snapshotting. + * However, it is ok to modify or destroy drawing in any other way, because the background task has shared + * ownership of the drawing (=> Sync), and snapshotting prevents modification of the data being read by the + * background task (=> Send/const). + */ +Cairo::RefPtr +render_preview(SPDocument *doc, std::shared_ptr drawing, uint32_t bg_color, Inkscape::DrawingItem *item, + unsigned width_in, unsigned height_in, Geom::Rect const &dboxIn); + +} // namespace Preview +} // namespace UI +} // namespace Inkscape + +#endif // INKSCAPE_UTIL_PREVIEW_H diff --git a/src/util/recently-used-fonts.cpp b/src/util/recently-used-fonts.cpp new file mode 100644 index 0000000..51e92f8 --- /dev/null +++ b/src/util/recently-used-fonts.cpp @@ -0,0 +1,235 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * This file contains recently used font list related logic. The functions to manage the + * recently used fonts are defined in this file. + * + * Authors: + * Vaibhav Malik + * + * The contents of this file may be used under the GNU General Public License Version 2 or later. + * + */ + +#include "recently-used-fonts.h" +#include "font-collections.h" +#include "libnrtype/font-lister.h" +#include "preferences.h" + +#include +// #include + +#ifdef _WIN32 +#include +#endif +#include +#include + +using namespace Inkscape::IO::Resource; + +namespace Inkscape { + +// get_instance method for the singleton class. +RecentlyUsedFonts* RecentlyUsedFonts::get() +{ + static RecentlyUsedFonts* s_instance = new Inkscape::RecentlyUsedFonts(); + return s_instance; +} + +RecentlyUsedFonts::RecentlyUsedFonts() +{ + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + _max_size = prefs->getInt("/tools/text/recently_used_fonts_size", 10); + + init(); +} + +void RecentlyUsedFonts::init() +{ + // Clear the previous collections(we may be re-reading). + clear(); + + // Generate the name of the file. + std::string file_path = get_path_string(USER, FONTCOLLECTIONS, RECENTFONTS_FILENAME); + std::string file_dir = get_path_string(USER, FONTCOLLECTIONS, nullptr); + + static bool create_dir = true; + + if(create_dir) { +#ifdef _WIN32 + mkdir(file_dir.c_str()); +#else + mkdir(file_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); +#endif + create_dir = false; + } + + // Read the file. + read(file_path); +} + +void RecentlyUsedFonts::clear() +{ + _recent_list.clear(); +} + +/* +void RecentlyUsedFonts::print_recently_used_fonts() +{ + std::cout << std::endl << "********************" << std::endl; + + for(auto const& font: _recent_list) { + std::cout << font << std::endl; + } + + std::cout << std::endl << "********************" << std::endl; +} +*/ + +// Read fonts stored in a collection file. +void RecentlyUsedFonts::read(const Glib::ustring& file_path) +{ + // Filestream object to read data from the file. + std::ifstream input_file(file_path); + + // Check if the file opened or not. + if (input_file.is_open()) { + // Now read all the fonts stored in this file. + std::string line; + FontCollections *font_collections = Inkscape::FontCollections::get(); + + while (getline(input_file, line)) { + // Get rid of unwanted characters from the left and right. + line = font_collections->trim_left_and_right(line); + Glib::ustring font = line; + + // Now check if the font is installed on the system because it is possible + // that a previously installed font may not be available now. + if (Inkscape::FontLister::get_instance()->font_installed_on_system(font)) { + _recent_list.push_front(font); + } + } + + // Important: Close the file after use! + input_file.close(); + } else { + // Error: Failed to open the file. + // std::cout << "Failed to open file: " << file_path << std::endl; + } +} + +// Function to write the recently used fonts to a file. +void RecentlyUsedFonts::write_recently_used_fonts() +{ + // Step 1: Fetch the collections directory from the system directory. + + // Generate the name of the file. + std::string file_path = get_path_string(USER, FONTCOLLECTIONS, RECENTFONTS_FILENAME); + + std::fstream output_file; + output_file.open(file_path, std::fstream::out); + + // Check if the file opened or not. + if (output_file.is_open()) { + for (auto it = _recent_list.rbegin(); it != _recent_list.rend(); ++it) { + output_file << (*it) << '\n'; + } + + // Important: Close the file after use. + output_file.close(); + init(); + } else { + // Error: Failed to open the file. + // std::cout << "Failed to open file: " << file_path << std::endl; + } +} + +void RecentlyUsedFonts::change_max_list_size(const int& max_size) +{ + // std::cout << "Changing size from: " << _max_size << " to: " << max_size << "\n"; + + if(max_size < 0) { + std::cerr << "Can not set negative size" << std::endl; + return; + } + + _max_size = max_size; + + // If the font that are currently stored in the recent list are more than the + // new max size of the list, then we need to remove the least recent fonts + // to make sure that the list if not longer than the max size. + int difference = _recent_list.size() - _max_size; + + if(difference > 0) { + while(difference--) { + _recent_list.pop_back(); + } + } + + update_signal.emit(); +} + +// This function is called whenever the user clicks the Apply button in +// the text and font dialog. It inserts the selected family into the +// recently used list and it's correct position. +void RecentlyUsedFonts::prepend_to_list(const Glib::ustring& font_name) { + /* + * Look-Up step + */ + auto it = std::find(_recent_list.begin(), _recent_list.end(), font_name); + + /* + * If the element is already present + * Delete it, because we'll re-insert it at the top + */ + if(it != _recent_list.end()) { + _recent_list.erase(it); + } + else { + /* + * Insert the element in the list. + */ + _recent_list.push_front(font_name); + } + + /* + * Check if the current size exceeds the max size + * If yes, then delete an element from the end + */ + if(_recent_list.size() > _max_size) { + _recent_list.pop_back(); + } + + write_recently_used_fonts(); + update_signal.emit(); +} + +/* +bool RecentlyUsedFonts::is_empty() +{ + return _max_size == 0; +} +*/ + +int RecentlyUsedFonts::get_count() +{ + return _recent_list.size(); +} + +// Returns the recently used fonts. +const std::list RecentlyUsedFonts::get_fonts() +{ + return _recent_list; +} + +} // Namespace + +/* + 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 : diff --git a/src/util/recently-used-fonts.h b/src/util/recently-used-fonts.h new file mode 100644 index 0000000..6e8f9bd --- /dev/null +++ b/src/util/recently-used-fonts.h @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Recently used fonts are stored in a separate file in the fontcollections directory under the SYSTEM + * path. Recently used fonts are managed as a list with the help of a list. + * + * Authors: + * Vaibhav Malik + * + * The contents of this file may be used under the GNU General Public License Version 2 or later. + * + */ + +#ifndef INK_RECENTLY_USED_FONTS_H +#define INK_RECENTLY_USED_FONTS_H + +#include +#include +#include + +#include "io/dir-util.h" +#include "io/resource.h" + +namespace Inkscape { +inline const char *RECENTFONTS_FILENAME = "recently_used_fonts.log"; + +class RecentlyUsedFonts { + +public: + enum What { + All, + System, + User + }; + + static RecentlyUsedFonts* get(); + ~RecentlyUsedFonts() = default; + + // To load the last saved recent font list. + void init(); + void clear(); + + // void print_recently_used_fonts(); + + // Read recently used fonts from file. + void read(const Glib::ustring& file_name); + void write_recently_used_fonts(); + + void change_max_list_size(const int& max_size); + void prepend_to_list(const Glib::ustring& font_name); + + // bool is_empty(); + const std::list get_fonts(); + int get_count(); + + sigc::connection connectUpdate(sigc::slot slot) { + return update_signal.connect(slot); + } + +private: + RecentlyUsedFonts(); + + // This list will contain the recently used fonts queue. + std::list _recent_list; + + // Defines the maximum size the recently_used_font_list can have. + // TODO: Add an option in the preferences to change the maximum size of + // the recently used font list. + int _max_size; + + sigc::signal update_signal; +}; + +} // Namespace Inkscape + +#endif // INK_RECENTLY_USED_FONTS_H + +/* + 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 : diff --git a/src/util/reference.h b/src/util/reference.h new file mode 100644 index 0000000..b4a123b --- /dev/null +++ b/src/util/reference.h @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape::Traits::Reference - traits class for dealing with reference types + * + * Authors: + * MenTaLguY + * + * Copyright (C) 2004 MenTaLguY + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_TRAITS_REFERENCE_H +#define SEEN_INKSCAPE_TRAITS_REFERENCE_H + +namespace Inkscape { + +namespace Traits { + +template +struct Reference { + typedef T const &RValue; + typedef T &LValue; + typedef T *Pointer; + typedef T const *ConstPointer; +}; + +template +struct Reference { + typedef T &RValue; + typedef T &LValue; + typedef T *Pointer; + typedef T const *ConstPointer; +}; + +} + +} + +#endif +/* + 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 : diff --git a/src/util/scope_exit.h b/src/util/scope_exit.h new file mode 100644 index 0000000..16961e5 --- /dev/null +++ b/src/util/scope_exit.h @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * Run code on scope exit. + */ + +#ifndef INKSCAPE_UTIL_SCOPE_EXIT_H +#define INKSCAPE_UTIL_SCOPE_EXIT_H + +#include +#include + +// Todo: (C++23?) Replace with now-standardised version. +template +class scope_exit +{ +public: + scope_exit(F &&f) : f(std::forward(f)) {} + scope_exit(scope_exit const &) = delete; + scope_exit &operator=(scope_exit const &) = delete; + ~scope_exit() { f(); } + +private: + std::decay_t f; +}; + +#endif // INKSCAPE_UTIL_SCOPE_EXIT_H diff --git a/src/util/share.cpp b/src/util/share.cpp new file mode 100644 index 0000000..6c436dc --- /dev/null +++ b/src/util/share.cpp @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape::Util::ptr_shared - like T const *, but stronger. + * Used to hold c-style strings for objects that are managed by the gc. + * + * Authors: + * MenTaLguY + * + * Copyright (C) 2006 MenTaLguY + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "util/share.h" +#include + +namespace Inkscape { +namespace Util { + +ptr_shared share_string(char const *string) { + g_return_val_if_fail(string != nullptr, share_unsafe(nullptr)); + return share_string(string, std::strlen(string)); +} + +ptr_shared share_string(char const *string, std::size_t length) { + g_return_val_if_fail(string != nullptr, share_unsafe(nullptr)); + char *new_string=new (GC::ATOMIC) char[length+1]; + std::memcpy(new_string, string, length); + new_string[length] = 0; + return share_unsafe(new_string); +} + +} +} + +/* + 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 : diff --git a/src/util/share.h b/src/util/share.h new file mode 100644 index 0000000..e8b9fb1 --- /dev/null +++ b/src/util/share.h @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape::Util::ptr_shared - like T const *, but stronger. + * Used to hold c-style strings for objects that are managed by the gc. + * + * Authors: + * MenTaLguY + * + * Copyright (C) 2006 MenTaLguY + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_UTIL_SHARE_H +#define SEEN_INKSCAPE_UTIL_SHARE_H + +#include "inkgc/gc-core.h" +#include +#include + +namespace Inkscape { +namespace Util { + +class ptr_shared { +public: + + ptr_shared() : _string(nullptr) {} + ptr_shared(ptr_shared const &other) = default; + + operator char const *() const { return _string; } + operator bool() const { return _string; } + + char const *pointer() const { return _string; } + char const &operator[](int i) const { return _string[i]; } + + ptr_shared operator+(int i) const { + return share_unsafe(_string+i); + } + ptr_shared operator-(int i) const { + return share_unsafe(_string-i); + } + //WARNING: No bounds checking in += and -= functions. Moving the pointer + //past the end of the string and then back could probably cause the garbage + //collector to deallocate the string inbetween, as there's temporary no + //valid reference pointing into the allocated space. + ptr_shared &operator+=(int i) { + _string += i; + return *this; + } + ptr_shared &operator-=(int i) { + _string -= i; + return *this; + } + std::ptrdiff_t operator-(ptr_shared const &other) { + return _string - other._string; + } + + ptr_shared &operator=(ptr_shared const &other) = default; + + bool operator==(ptr_shared const &other) const { + return _string == other._string; + } + bool operator!=(ptr_shared const &other) const { + return _string != other._string; + } + bool operator>(ptr_shared const &other) const { + return _string > other._string; + } + bool operator<(ptr_shared const &other) const { + return _string < other._string; + } + + friend ptr_shared share_unsafe(char const *string); + +private: + ptr_shared(char const *string) : _string(string) {} + static ptr_shared share_unsafe(char const *string) { + return ptr_shared(string); + } + + //This class (and code using it) assumes that it never has to free this + //pointer, and that the memory it points to will not be freed as long as a + //ptr_shared pointing to it exists. + char const *_string; +}; + +ptr_shared share_string(char const *string); +ptr_shared share_string(char const *string, std::size_t length); + +inline ptr_shared share_unsafe(char const *string) { + return ptr_shared::share_unsafe(string); +} + +//TODO: Do we need this function? +inline ptr_shared share_static_string(char const *string) { + return share_unsafe(string); +} + +} +} + +#endif +/* + 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 : diff --git a/src/util/signal-blocker.h b/src/util/signal-blocker.h new file mode 100644 index 0000000..8fb6256 --- /dev/null +++ b/src/util/signal-blocker.h @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Base RAII blocker for sgic++ signals. + * + * Authors: + * Jon A. Cruz + * + * Copyright (C) 2014 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_INKSCAPE_UTIL_SIGNAL_BLOCKER_H +#define SEEN_INKSCAPE_UTIL_SIGNAL_BLOCKER_H + +#include +#include + +/** + * Base RAII blocker for sgic++ signals. + */ +class SignalBlocker +{ +public: + /** + * Creates a new instance that if the signal is currently unblocked will block + * it until this instance is destructed and then will unblock it. + */ + SignalBlocker( sigc::connection *connection ) : + _connection(connection), + _wasBlocked(_connection->blocked()) + { + if (!_wasBlocked) + { + _connection->block(); + } + } + + /** + * Destructor that will unblock the signal if it was blocked initially by this + * instance. + */ + ~SignalBlocker() + { + if (!_wasBlocked) + { + _connection->block(false); + } + } + +private: + // noncopyable, nonassignable + SignalBlocker(SignalBlocker const &other) = delete; + SignalBlocker& operator=(SignalBlocker const &other) = delete; + + sigc::connection *_connection; + bool _wasBlocked; +}; + +#endif // SEEN_INKSCAPE_UTIL_SIGNAL_BLOCKER_H + +/* + 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 : diff --git a/src/util/statics.cpp b/src/util/statics.cpp new file mode 100644 index 0000000..66add67 --- /dev/null +++ b/src/util/statics.cpp @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include "statics.h" + +Inkscape::Util::StaticsBin &Inkscape::Util::StaticsBin::get() +{ + static StaticsBin instance; + return instance; +} + +void Inkscape::Util::StaticsBin::destroy() +{ + while (head) { + head->destroy(); + head = head->next; + } +} + +Inkscape::Util::StaticsBin::~StaticsBin() +{ + // If this assertion triggers, then destroy() wasn't called close enough to the end of main(). + assert(!head && "StaticsBin::destroy() must be called before main() exit"); +} diff --git a/src/util/statics.h b/src/util/statics.h new file mode 100644 index 0000000..11ac26c --- /dev/null +++ b/src/util/statics.h @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * Static objects with destruction before main() exit. + */ +#ifndef INKSCAPE_UTIL_STATICS_BIN_H +#define INKSCAPE_UTIL_STATICS_BIN_H + +#include +#include + +namespace Inkscape { +namespace Util { + +class StaticBase; + +/** + * The following system provides a framework for resolving gnarly issues to do with threads, statics, and libraries at program exit. + * Instead of the function-local static initialisation idiom + * + * X &get() + * { + * static X x; + * return x; + * } + * + * you instead write + * + * X &get() + * { + * static Static x; + * return x.get(); + * } + * + * Similarities: + * - Both allow creation of singleton objects which are destroyed in the reverse order of constructor completion. + * - Both allow dependency registration and automatically prevent dependency loops. + * + * Differences: + * - The second kind are destructed before the end of main(), the first kind after. + * - Only the second supports on-demand destruction and re-initialisation - necessary if you want to write isolated tests. + * - Only the first supports thread-safe initialisation; the second must be initialised in the main thread. + * - Only the first supports automatic destruction; the second must be destroyed manually with StaticsBin::get().destroy(). + * + * Rationale examples: + * - FontFactory depends on Harfbuzz depends on FreeType, but Harfbuzz doesn't register the dependency so FreeType fails + * to outlive both FontFactory and Harfbuzz. (We say X depends on Y if the lifetime of X must be contained in Y; such + * a situation can be guaranteed by having X::X call Y::get.) FreeType is inaccessible, so we cannot register the + * dependency ourselves without fragile hacks. Therefore, FontFactory must be destroyed before the end of main(). + * + * - Background threads can access statics of the first kind. If they run past end of main(), they may find them destructed. + * Therefore either the static that joins all threads must be destructed before the end of main(), or must register a + * dependency on every static that any background thread might use, which is infeasible and error-prone. + */ + +/** + * Maintains the list of statics that need to be destroyed, + * destroys them, and complains if it's not asked to do so in time. + */ +class StaticsBin +{ +public: + static StaticsBin &get(); + void destroy(); + ~StaticsBin(); + +private: + StaticBase *head = nullptr; + + template + friend class Static; +}; + +/// Base class for statics, allowing type-erased destruction. +class StaticBase +{ +protected: + StaticBase *next; + ~StaticBase() = default; + virtual void destroy() = 0; + friend class StaticsBin; +}; + +/// Wrapper for a static of type T. +template +class Static final : public StaticBase +{ +public: + T &get() + { + if (!opt) { + opt.emplace(); + auto &bin = StaticsBin::get(); + next = bin.head; + bin.head = this; + } + return *opt; + } + +private: + std::optional opt; + void destroy() override { opt.reset(); } +}; + +} // namespace Util +} // namespace Inkscape + +#endif // INKSCAPE_UTIL_STATICS_BIN_H diff --git a/src/util/trim.h b/src/util/trim.h new file mode 100644 index 0000000..c8c9fd5 --- /dev/null +++ b/src/util/trim.h @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * @file + * Inkscape::Util::trim - trim whitespace and other characters + * + * Authors: + * Rafael Siejakowski + * + * Copyright (C) 2022 the Authors. + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_TRIM_H +#define SEEN_TRIM_H + +#include +#include + +namespace Inkscape { +namespace Util { + +/** + * @brief + * Modifies a string in place, removing leading and trailing whitespace characters. + * Optionally, it can remove other characters or ranges in addition to whitespace. + * + * @param input - a reference to a Glib::ustring which will be modified in place. + * @param also_remove - optional range of characters to remove in addition to whitespace. + * NOTE: these characters are inserted into a regex range (square brackets) and + * therefore may need to be regex-escaped. It is the responsibility of + * the user to pass a string that will work correctly in a regex range. + */ +inline void trim(Glib::ustring &input, Glib::ustring const &also_remove = "") +{ + auto const regex = Glib::Regex::create(Glib::ustring("^[\\s") + also_remove + "]*(.+?)[\\s" + + also_remove + "]*$"); + Glib::MatchInfo match_info; + regex->match(input, match_info); + if (!match_info.matches()) { + input.clear(); + return; + } + input = match_info.fetch(1); +} + +} // namespace Util +} // namespace Inkscape + +#endif // SEEN_TRIM_H +/* + 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 : diff --git a/src/util/units.cpp b/src/util/units.cpp new file mode 100644 index 0000000..7c4681c --- /dev/null +++ b/src/util/units.cpp @@ -0,0 +1,582 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape Units + * + * Authors: + * Matthew Petroff + * + * Copyright (C) 2013 Matthew Petroff + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include <2geom/coord.h> + +#include "io/resource.h" +#include "util/units.h" +#include "path-prefix.h" +#include "streq.h" + +using Inkscape::Util::UNIT_TYPE_DIMENSIONLESS; +using Inkscape::Util::UNIT_TYPE_LINEAR; +using Inkscape::Util::UNIT_TYPE_RADIAL; +using Inkscape::Util::UNIT_TYPE_FONT_HEIGHT; + +namespace +{ + +#define MAKE_UNIT_CODE(a, b) \ + ((((unsigned)(a) & 0xdf) << 8) | ((unsigned)(b) & 0xdf)) + +enum UnitCode { + UNIT_CODE_PX = MAKE_UNIT_CODE('p','x'), + UNIT_CODE_PT = MAKE_UNIT_CODE('p','t'), + UNIT_CODE_PC = MAKE_UNIT_CODE('p','c'), + UNIT_CODE_MM = MAKE_UNIT_CODE('m','m'), + UNIT_CODE_CM = MAKE_UNIT_CODE('c','m'), + UNIT_CODE_IN = MAKE_UNIT_CODE('i','n'), + UNIT_CODE_EM = MAKE_UNIT_CODE('e','m'), + UNIT_CODE_EX = MAKE_UNIT_CODE('e','x'), + UNIT_CODE_PERCENT = MAKE_UNIT_CODE('%',0) +}; + +// TODO: convert to constexpr in C++11, so that the above constants can be eliminated +inline unsigned make_unit_code(char const *str) { + if (!str || str[0] == 0) return 0; + return MAKE_UNIT_CODE(str[0], str[1]); +} + + +// This must match SVGLength::Unit +unsigned const svg_length_lookup[] = { + 0, + UNIT_CODE_PX, + UNIT_CODE_PT, + UNIT_CODE_PC, + UNIT_CODE_MM, + UNIT_CODE_CM, + UNIT_CODE_IN, + UNIT_CODE_EM, + UNIT_CODE_EX, + UNIT_CODE_PERCENT +}; + + + +// maps unit codes obtained from their abbreviations to their SVGLength unit indexes +typedef std::unordered_map UnitCodeLookup; + +UnitCodeLookup make_unit_code_lookup() +{ + UnitCodeLookup umap; + for (unsigned i = 1; i < G_N_ELEMENTS(svg_length_lookup); ++i) { + umap[svg_length_lookup[i]] = static_cast(i); + } + return umap; +} + +UnitCodeLookup const unit_code_lookup = make_unit_code_lookup(); + + + +typedef std::unordered_map TypeMap; + +/** A std::map that gives the data type value for the string version. + * @todo consider hiding map behind hasFoo() and getFoo() type functions. */ +TypeMap make_type_map() +{ + TypeMap tmap; + tmap["DIMENSIONLESS"] = UNIT_TYPE_DIMENSIONLESS; + tmap["LINEAR"] = UNIT_TYPE_LINEAR; + tmap["RADIAL"] = UNIT_TYPE_RADIAL; + tmap["FONT_HEIGHT"] = UNIT_TYPE_FONT_HEIGHT; + // Note that code was not yet handling LINEAR_SCALED, TIME, QTY and NONE + + return tmap; +} + +TypeMap const type_map = make_type_map(); + +} // namespace + +namespace Inkscape { +namespace Util { + +class UnitParser : public Glib::Markup::Parser +{ +public: + typedef Glib::Markup::Parser::AttributeMap AttrMap; + typedef Glib::Markup::ParseContext Ctx; + + UnitParser(UnitTable *table); + ~UnitParser() override = default; + +protected: + void on_start_element(Ctx &ctx, Glib::ustring const &name, AttrMap const &attrs) override; + void on_end_element(Ctx &ctx, Glib::ustring const &name) override; + void on_text(Ctx &ctx, Glib::ustring const &text) override; + +public: + UnitTable *tbl; + bool primary; + bool skip; + Unit unit; +}; + +UnitParser::UnitParser(UnitTable *table) : + tbl(table), + primary(false), + skip(false) +{ +} + +#define BUFSIZE (255) + +Unit::Unit() : + type(UNIT_TYPE_DIMENSIONLESS), // should this or NONE be the default? + factor(1.0), + name(), + name_plural(), + abbr(), + description() +{ +} + +Unit::Unit(UnitType type, + double factor, + Glib::ustring name, + Glib::ustring name_plural, + Glib::ustring abbr, + Glib::ustring description) + : type(type) + , factor(factor) + , name(std::move(name)) + , name_plural(std::move(name_plural)) + , abbr(std::move(abbr)) + , description(std::move(description)) +{ + g_return_if_fail(factor <= 0); +} + +void Unit::clear() +{ + *this = Unit(); +} + +int Unit::defaultDigits() const +{ + int factor_digits = int(log10(factor)); + if (factor_digits < 0) { + g_warning("factor = %f, factor_digits = %d", factor, factor_digits); + g_warning("factor_digits < 0 - returning 0"); + factor_digits = 0; + } + return factor_digits; +} + +bool Unit::compatibleWith(Unit const *u) const +{ + // Percentages + if (type == UNIT_TYPE_DIMENSIONLESS || u->type == UNIT_TYPE_DIMENSIONLESS) { + return true; + } + + // Other units with same type + if (type == u->type) { + return true; + } + + // Different, incompatible types + return false; +} +bool Unit::compatibleWith(Glib::ustring const &u) const +{ + return compatibleWith(unit_table.getUnit(u)); +} + +bool Unit::operator==(Unit const &other) const +{ + return (type == other.type && name.compare(other.name) == 0); +} + +int Unit::svgUnit() const +{ + char const *astr = abbr.c_str(); + unsigned code = make_unit_code(astr); + + UnitCodeLookup::const_iterator u = unit_code_lookup.find(code); + if (u != unit_code_lookup.end()) { + return u->second; + } + return 0; +} + +double Unit::convert(double from_dist, Unit const *to) const +{ + // Percentage + if (to->type == UNIT_TYPE_DIMENSIONLESS) { + return from_dist * to->factor; + } + + // Incompatible units + if (type != to->type) { + return -1; + } + + // Compatible units + return from_dist * factor / to->factor; +} +double Unit::convert(double from_dist, Glib::ustring const &to) const +{ + return convert(from_dist, unit_table.getUnit(to)); +} +double Unit::convert(double from_dist, char const *to) const +{ + return convert(from_dist, unit_table.getUnit(to)); +} + + + +Unit UnitTable::_empty_unit; + +UnitTable::UnitTable() +{ + /* We need to defer loading units.xml from a user data location + * if we're running inside a macOS application bundle, because at this + * point we haven't set up the environment (especially XDG variables) + * for macOS yet. And the following call we trigger glib to lookup XDG + * variables and cache them forever, i.e. we can no longer modify them + * in inkscape-main.cpp. + */ + if (!g_str_has_suffix(get_program_dir(), "Contents/MacOS")) { + using namespace Inkscape::IO::Resource; + load(get_filename(UIS, "units.xml", false, true)); + } +} + +UnitTable::~UnitTable() +{ + for (auto & iter : _unit_map) + { + delete iter.second; + } +} + +void UnitTable::addUnit(Unit const &u, bool primary) +{ + _unit_map[make_unit_code(u.abbr.c_str())] = new Unit(u); + if (primary) { + _primary_unit[u.type] = u.abbr; + } +} + +Unit const *UnitTable::getUnit(char const *abbr) const +{ + UnitCodeMap::const_iterator f = _unit_map.find(make_unit_code(abbr)); + if (f != _unit_map.end()) { + return &(*f->second); + } + return &_empty_unit; +} + +Unit const *UnitTable::getUnit(Glib::ustring const &unit_abbr) const +{ + return getUnit(unit_abbr.c_str()); +} +Unit const *UnitTable::getUnit(SVGLength::Unit u) const +{ + if (u == 0 || u > SVGLength::LAST_UNIT) { + return &_empty_unit; + } + + UnitCodeMap::const_iterator f = _unit_map.find(svg_length_lookup[u]); + if (f != _unit_map.end()) { + return &(*f->second); + } + return &_empty_unit; +} + +Unit const *UnitTable::findUnit(double factor, UnitType type) const +{ + const double eps = factor * 0.01; // allow for 1% deviation + + UnitCodeMap::const_iterator cit = _unit_map.begin(); + while (cit != _unit_map.end()) { + if (cit->second->type == type) { + if (Geom::are_near(cit->second->factor, factor, eps)) { + // unit found! + break; + } + } + ++cit; + } + + if (cit != _unit_map.end()) { + return cit->second; + } else { + return getUnit(_primary_unit[type]); + } +} + +Quantity UnitTable::parseQuantity(Glib::ustring const &q) const +{ + Glib::MatchInfo match_info; + + // Extract value + double value = 0; + Glib::RefPtr value_regex = Glib::Regex::create("[-+]*[\\d+]*[\\.,]*[\\d+]*[eE]*[-+]*\\d+"); + if (value_regex->match(q, match_info)) { + std::istringstream tmp_v(match_info.fetch(0).raw()); + tmp_v >> value; + } + int start_pos, end_pos; + match_info.fetch_pos(0, end_pos, start_pos); + end_pos = q.size() - start_pos; + Glib::ustring u = q.substr(start_pos, end_pos); + + // Extract unit abbreviation + Glib::ustring abbr; + Glib::RefPtr unit_regex = Glib::Regex::create("[A-z%]+"); + if (unit_regex->match(u, match_info)) { + abbr = match_info.fetch(0); + } + + Quantity qty(value, abbr); + return qty; +} + +/* UNSAFE while passing around pointers to the Unit objects in this table +bool UnitTable::deleteUnit(Unit const &u) +{ + bool deleted = false; + // Cannot delete the primary unit type since it's + // used for conversions + if (u.abbr != _primary_unit[u.type]) { + UnitCodeMap::iterator iter = _unit_map.find(make_unit_code(u.abbr.c_str())); + if (iter != _unit_map.end()) { + delete (*iter).second; + _unit_map.erase(iter); + deleted = true; + } + } + return deleted; +} +*/ + +bool UnitTable::hasUnit(Glib::ustring const &unit) const +{ + UnitCodeMap::const_iterator iter = _unit_map.find(make_unit_code(unit.c_str())); + return (iter != _unit_map.end()); +} + +UnitTable::UnitMap UnitTable::units(UnitType type) const +{ + UnitMap submap; + for (auto iter : _unit_map) { + if (iter.second->type == type) { + submap.insert(UnitMap::value_type(iter.second->abbr, *iter.second)); + } + } + + return submap; +} + +Glib::ustring UnitTable::primary(UnitType type) const +{ + return _primary_unit[type]; +} + +bool UnitTable::load(std::string const &filename) { + UnitParser uparser(this); + Glib::Markup::ParseContext ctx(uparser); + + try { + Glib::ustring unitfile = Glib::file_get_contents(filename); + ctx.parse(unitfile); + ctx.end_parse(); + } catch (Glib::FileError const &e) { + g_warning("Units file %s is missing: %s\n", filename.c_str(), e.what().c_str()); + return false; + } catch (Glib::MarkupError const &e) { + g_warning("Problem loading units file '%s': %s\n", filename.c_str(), e.what().c_str()); + return false; + } + return true; +} + +/* +bool UnitTable::save(std::string const &filename) { + g_warning("UnitTable::save(): not implemented"); + + return false; +} +*/ + +Inkscape::Util::UnitTable unit_table; + +void UnitParser::on_start_element(Ctx &/*ctx*/, Glib::ustring const &name, AttrMap const &attrs) +{ + if (name == "unit") { + // reset for next use + unit.clear(); + primary = false; + skip = false; + + AttrMap::const_iterator f; + if ((f = attrs.find("type")) != attrs.end()) { + Glib::ustring type = f->second; + TypeMap::const_iterator tf = type_map.find(type); + if (tf != type_map.end()) { + unit.type = tf->second; + } else { + g_warning("Skipping unknown unit type '%s'.\n", type.c_str()); + skip = true; + } + } + if ((f = attrs.find("pri")) != attrs.end()) { + primary = (f->second[0] == 'y' || f->second[0] == 'Y'); + } + } +} + +void UnitParser::on_text(Ctx &ctx, Glib::ustring const &text) +{ + Glib::ustring element = ctx.get_element(); + if (element == "name") { + unit.name = text; + } else if (element == "plural") { + unit.name_plural = text; + } else if (element == "abbr") { + unit.abbr = text; + } else if (element == "factor") { + // TODO make sure we use the right conversion + unit.factor = g_ascii_strtod(text.c_str(), nullptr); + } else if (element == "description") { + unit.description = text; + } +} + +void UnitParser::on_end_element(Ctx &/*ctx*/, Glib::ustring const &name) +{ + if (name == "unit" && !skip) { + tbl->addUnit(unit, primary); + } +} + +Quantity::Quantity(double q, Unit const *u) + : unit(u) + , quantity(q) +{ +} +Quantity::Quantity(double q, Glib::ustring const &u) + : unit(unit_table.getUnit(u.c_str())) + , quantity(q) +{ +} +Quantity::Quantity(double q, char const *u) + : unit(unit_table.getUnit(u)) + , quantity(q) +{ +} + +bool Quantity::compatibleWith(Unit const *u) const +{ + return unit->compatibleWith(u); +} +bool Quantity::compatibleWith(Glib::ustring const &u) const +{ + return compatibleWith(u.c_str()); +} +bool Quantity::compatibleWith(char const *u) const +{ + return compatibleWith(unit_table.getUnit(u)); +} + +double Quantity::value(Unit const *u) const +{ + return convert(quantity, unit, u); +} +double Quantity::value(Glib::ustring const &u) const +{ + return value(u.c_str()); +} +double Quantity::value(char const *u) const +{ + return value(unit_table.getUnit(u)); +} + +Glib::ustring Quantity::string(Unit const *u) const { + return Glib::ustring::format(std::fixed, std::setprecision(2), value(u)) + " " + u->abbr; +} +Glib::ustring Quantity::string(Glib::ustring const &u) const { + return string(unit_table.getUnit(u.c_str())); +} +Glib::ustring Quantity::string() const { + return string(unit); +} + +double Quantity::convert(double from_dist, Unit const *from, Unit const *to) +{ + return from->convert(from_dist, to); +} +double Quantity::convert(double from_dist, Glib::ustring const &from, Unit const *to) +{ + return convert(from_dist, unit_table.getUnit(from.c_str()), to); +} +double Quantity::convert(double from_dist, Unit const *from, Glib::ustring const &to) +{ + return convert(from_dist, from, unit_table.getUnit(to.c_str())); +} +double Quantity::convert(double from_dist, Glib::ustring const &from, Glib::ustring const &to) +{ + return convert(from_dist, unit_table.getUnit(from.c_str()), unit_table.getUnit(to.c_str())); +} +double Quantity::convert(double from_dist, char const *from, char const *to) +{ + return convert(from_dist, unit_table.getUnit(from), unit_table.getUnit(to)); +} + +bool Quantity::operator<(Quantity const &rhs) const +{ + if (unit->type != rhs.unit->type) { + g_warning("Incompatible units"); + return false; + } + return quantity < rhs.value(unit); +} +bool Quantity::operator==(Quantity const &other) const +{ + /** \fixme This is overly strict. I think we should change this to: + if (unit->type != other.unit->type) { + g_warning("Incompatible units"); + return false; + } + return are_near(quantity, other.value(unit)); + */ + return (*unit == *other.unit) && (quantity == other.quantity); +} + +} // namespace Util +} // namespace Inkscape + +/* + 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 : diff --git a/src/util/units.h b/src/util/units.h new file mode 100644 index 0000000..d9f019f --- /dev/null +++ b/src/util/units.h @@ -0,0 +1,233 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Inkscape Units + * These classes are used for defining different unit systems. + * + * Authors: + * Matthew Petroff + * + * Copyright (C) 2013 Matthew Petroff + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef INKSCAPE_UTIL_UNITS_H +#define INKSCAPE_UTIL_UNITS_H + +#include +#include +#include +#include <2geom/coord.h> +#include "svg/svg-length.h" + +#ifndef DOXYGEN_SHOULD_SKIP_THIS + +#define DEFAULT_UNIT_NAME "mm" + +namespace std { +template <> +struct hash +{ + std::size_t operator()(Glib::ustring const &s) const + { + return hash()(s.raw()); + } +}; +} // namespace std + +#endif + +namespace Inkscape { +namespace Util { + +enum UnitType { + UNIT_TYPE_DIMENSIONLESS, /* Percentage */ + UNIT_TYPE_LINEAR, + UNIT_TYPE_LINEAR_SCALED, + UNIT_TYPE_RADIAL, + UNIT_TYPE_TIME, + UNIT_TYPE_FONT_HEIGHT, + UNIT_TYPE_QTY, + UNIT_TYPE_NONE = -1 +}; + +const char DEG[] = "°"; + +class Unit + : boost::equality_comparable +{ +public: + Unit(); + Unit(UnitType type, + double factor, + Glib::ustring name, + Glib::ustring name_plural, + Glib::ustring abbr, + Glib::ustring description); + + void clear(); + + bool isAbsolute() const { return type != UNIT_TYPE_DIMENSIONLESS; } + + /** + * Returns the suggested precision to use for displaying numbers + * of this unit. + */ + int defaultDigits() const; + + /** Checks if a unit is compatible with the specified unit. */ + bool compatibleWith(Unit const *u) const; + bool compatibleWith(Glib::ustring const &) const; + bool compatibleWith(char const *) const; + + UnitType type; + double factor; + Glib::ustring name; + Glib::ustring name_plural; + Glib::ustring abbr; + Glib::ustring description; + + /** Check if units are equal. */ + bool operator==(Unit const &other) const; + + /** Get SVG unit code. */ + int svgUnit() const; + + /** Convert value from this unit **/ + double convert(double from_dist, Unit const *to) const; + double convert(double from_dist, Glib::ustring const &to) const; + double convert(double from_dist, char const *to) const; +}; + +class Quantity + : boost::totally_ordered +{ +public: + Unit const *unit; + double quantity; + + /** Initialize a quantity. */ + Quantity(double q, Unit const *u); + Quantity(double q, Glib::ustring const &u); + Quantity(double q, char const *u); + + /** Checks if a quantity is compatible with the specified unit. */ + bool compatibleWith(Unit const *u) const; + bool compatibleWith(Glib::ustring const &u) const; + bool compatibleWith(char const *u) const; + + /** Return the quantity's value in the specified unit. */ + double value(Unit const *u) const; + double value(Glib::ustring const &u) const; + double value(char const *u) const; + + /** Return a printable string of the value in the specified unit. */ + Glib::ustring string(Unit const *u) const; + Glib::ustring string(Glib::ustring const &u) const; + Glib::ustring string() const; + + /** Convert distances. + no NULL check is performed on the passed pointers to Unit objects! */ + static double convert(double from_dist, Unit const *from, Unit const *to); + static double convert(double from_dist, Glib::ustring const &from, Unit const *to); + static double convert(double from_dist, Unit const *from, Glib::ustring const &to); + static double convert(double from_dist, Glib::ustring const &from, Glib::ustring const &to); + static double convert(double from_dist, char const *from, char const *to); + + /** Comparison operators. */ + bool operator<(Quantity const &rhs) const; + bool operator==(Quantity const &other) const; +}; + +inline bool are_near(Quantity const &a, Quantity const &b, double eps=Geom::EPSILON) +{ + return Geom::are_near(a.quantity, b.value(a.unit), eps); +} + +class UnitTable { +public: + /** + * Initializes the unit tables and identifies the primary unit types. + * + * The primary unit's conversion factor is required to be 1.00 + */ + UnitTable(); + virtual ~UnitTable(); + + typedef std::unordered_map UnitMap; + typedef std::unordered_map UnitCodeMap; + + /** Add a new unit to the table */ + void addUnit(Unit const &u, bool primary); + + /** Retrieve a given unit based on its string identifier */ + Unit const *getUnit(Glib::ustring const &name) const; + Unit const *getUnit(char const *name) const; + + /** Try to find a unit based on its conversion factor to the primary */ + Unit const *findUnit(double factor, UnitType type) const; + + /** Retrieve a given unit based on its SVGLength unit */ + Unit const *getUnit(SVGLength::Unit u) const; + + /** Retrieve a quantity based on its string identifier */ + Quantity parseQuantity(Glib::ustring const &q) const; + + /** Remove a unit definition from the given unit type table * / + * DISABLED, unsafe with the current passing around pointers to Unit objects in this table */ + //bool deleteUnit(Unit const &u); + + /** Returns true if the given string 'name' is a valid unit in the table */ + bool hasUnit(Glib::ustring const &name) const; + + /** Provides an iterable list of items in the given unit table */ + UnitMap units(UnitType type) const; + + /** Returns the default unit abbr for the given type */ + Glib::ustring primary(UnitType type) const; + + double getScale() const; + + void setScale(); + + /** Load units from an XML file. + * + * Loads and merges the contents of the given file into the UnitTable, + * possibly overwriting existing unit definitions. + * + * @param filename file to be loaded + */ + bool load(std::string const &filename); + + /* * Saves the current UnitTable to the given file. */ + //bool save(std::string const &filename); + +protected: + UnitCodeMap _unit_map; + Glib::ustring _primary_unit[UNIT_TYPE_QTY]; + + double _linear_scale; + static Unit _empty_unit; + +private: + UnitTable(UnitTable const &t); + UnitTable operator=(UnitTable const &t); + +}; + +extern UnitTable unit_table; + +} // namespace Util +} // namespace Inkscape + +#endif // define INKSCAPE_UTIL_UNITS_H +/* + 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 : diff --git a/src/util/ziptool.cpp b/src/util/ziptool.cpp new file mode 100644 index 0000000..dc87396 --- /dev/null +++ b/src/util/ziptool.cpp @@ -0,0 +1,3043 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/** @file + * TODO: insert short description here + *//* + * Authors: + * see git history + * Bob Jamison + * + * Copyright (C) 2018 Authors + * Released under GNU LGPL v2.1+, read the file 'COPYING' for more information. + */ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +/* + * This is intended to be a standalone, reduced capability + * implementation of Gzip and Zip functionality. Its + * targeted use case is for archiving and retrieving single files + * which use these encoding types. Being memory based and + * non-optimized, it is not useful in cases where very large + * archives are needed or where high performance is desired. + * However, it should hopefully work very well for smaller, + * one-at-a-time tasks. What you get in return is the ability + * to drop these files into your project and remove the dependencies + * on ZLib and Info-Zip. Enjoy. + */ + + +#include +#include +#include + +#include +#include + +#include "ziptool.h" + + + + + + +//######################################################################## +//# A D L E R 3 2 +//######################################################################## + +/** + * Constructor + */ +Adler32::Adler32() +{ + reset(); +} + +/** + * Destructor + */ +Adler32::~Adler32() += default; + +/** + * Reset Adler-32 checksum to initial value. + */ +void Adler32::reset() +{ + value = 1; +} + +// ADLER32_BASE is the largest prime number smaller than 65536 +#define ADLER32_BASE 65521 + +void Adler32::update(unsigned char b) +{ + unsigned long s1 = value & 0xffff; + unsigned long s2 = (value >> 16) & 0xffff; + s1 += b & 0xff; + s2 += s1; + value = ((s2 % ADLER32_BASE) << 16) | (s1 % ADLER32_BASE); +} + +void Adler32::update(char *str) +{ + if (str) + while (*str) + update((unsigned char)*str++); +} + + +/** + * Returns current checksum value. + */ +unsigned long Adler32::getValue() +{ + return value & 0xffffffffL; +} + + + +//######################################################################## +//# C R C 3 2 +//######################################################################## + +/** + * Constructor + */ +Crc32::Crc32() +{ + reset(); +} + +/** + * Destructor + */ +Crc32::~Crc32() += default; + +static bool crc_table_ready = false; +static unsigned long crc_table[256]; + +/** + * make the table for a fast CRC. + */ +static void makeCrcTable() +{ + if (crc_table_ready) + return; + for (int n = 0; n < 256; n++) + { + unsigned long c = n; + for (int k = 8; --k >= 0; ) + { + if ((c & 1) != 0) + c = 0xedb88320 ^ (c >> 1); + else + c >>= 1; + } + crc_table[n] = c; + } + crc_table_ready = true; +} + + +/** + * Reset CRC-32 checksum to initial value. + */ +void Crc32::reset() +{ + value = 0; + makeCrcTable(); +} + +void Crc32::update(unsigned char b) +{ + unsigned long c = ~value; + + c &= 0xffffffff; + c = crc_table[(c ^ b) & 0xff] ^ (c >> 8); + value = ~c; +} + + +void Crc32::update(char *str) +{ + if (str) + while (*str) + update((unsigned char)*str++); +} + +void Crc32::update(const std::vector &buf) +{ + std::vector::const_iterator iter; + for (iter=buf.begin() ; iter!=buf.end() ; ++iter) + { + unsigned char ch = *iter; + update(ch); + } +} + + +/** + * Returns current checksum value. + */ +unsigned long Crc32::getValue() +{ + return value & 0xffffffffL; +} + +//######################################################################## +//# I N F L A T E R +//######################################################################## + + +/** + * + */ +struct Huffman +{ + int *count; // number of symbols of each length + int *symbol; // canonically ordered symbols +}; + +/** + * + */ +class Inflater +{ +public: + + Inflater(); + + virtual ~Inflater(); + + static const int MAXBITS = 15; // max bits in a code + static const int MAXLCODES = 286; // max number of literal/length codes + static const int MAXDCODES = 30; // max number of distance codes + static const int MAXCODES = 316; // max codes lengths to read + static const int FIXLCODES = 288; // number of fixed literal/length codes + + /** + * + */ + bool inflate(std::vector &destination, + std::vector &source); + +private: + + /** + * + */ + void error(char const *fmt, ...) + #ifdef G_GNUC_PRINTF + G_GNUC_PRINTF(2, 3) + #endif + ; + + /** + * + */ + void trace(char const *fmt, ...) + #ifdef G_GNUC_PRINTF + G_GNUC_PRINTF(2, 3) + #endif + ; + + /** + * + */ + void dump(); + + /** + * + */ + int buildHuffman(Huffman *h, int *length, int n); + + /** + * + */ + bool getBits(int need, int *oval); + + /** + * + */ + int doDecode(Huffman *h); + + /** + * + */ + bool doCodes(Huffman *lencode, Huffman *distcode); + + /** + * + */ + bool doStored(); + + /** + * + */ + bool doFixed(); + + /** + * + */ + bool doDynamic(); + + + std::vectordest; + + std::vectorsrc; + unsigned long srcPos; //current read position + int bitBuf; + int bitCnt; + +}; + + +/** + * + */ +Inflater::Inflater() : + dest(), + src(), + srcPos(0), + bitBuf(0), + bitCnt(0) +{ +} + +/** + * + */ +Inflater::~Inflater() += default; + +/** + * + */ +void Inflater::error(char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stdout, "Inflater error:"); + vfprintf(stdout, fmt, args); + fprintf(stdout, "\n"); + va_end(args); +} + +/** + * + */ +void Inflater::trace(char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stdout, "Inflater:"); + vfprintf(stdout, fmt, args); + fprintf(stdout, "\n"); + va_end(args); +} + + +/** + * + */ +void Inflater::dump() +{ + for (unsigned char i : dest) + { + fputc(i, stdout); + } +} + +/** + * + */ +int Inflater::buildHuffman(Huffman *h, int *length, int n) +{ + // count number of codes of each length + for (int len = 0; len <= MAXBITS; len++) + h->count[len] = 0; + for (int symbol = 0; symbol < n; symbol++) + (h->count[length[symbol]])++; // assumes lengths are within bounds + if (h->count[0] == n) // no codes! + { + error("huffman tree will result in failed decode"); + return -1; + } + + // check for an over-subscribed or incomplete set of lengths + int left = 1; // number of possible codes left of current length + for (int len = 1; len <= MAXBITS; len++) + { + left <<= 1; // one more bit, double codes left + left -= h->count[len]; // deduct count from possible codes + if (left < 0) + { + error("huffman over subscribed"); + return -1; + } + } + + // generate offsets into symbol table for each length for sorting + int offs[MAXBITS+1]; //offsets in symbol table for each length + offs[1] = 0; + for (int len = 1; len < MAXBITS; len++) + offs[len + 1] = offs[len] + h->count[len]; + + /* + * put symbols in table sorted by length, by symbol order within each + * length + */ + for (int symbol = 0; symbol < n; symbol++) + if (length[symbol] != 0) + h->symbol[offs[length[symbol]]++] = symbol; + + // return zero for complete set, positive for incomplete set + return left; +} + + +/** + * + */ +bool Inflater::getBits(int requiredBits, int *oval) +{ + long val = bitBuf; + + //add more bytes if needed + while (bitCnt < requiredBits) + { + if (srcPos >= src.size()) + { + error("premature end of input"); + return false; + } + val |= ((long)(src[srcPos++])) << bitCnt; + bitCnt += 8; + } + + //update the buffer and return the data + bitBuf = (int)(val >> requiredBits); + bitCnt -= requiredBits; + *oval = (int)(val & ((1L << requiredBits) - 1)); + + return true; +} + + +/** + * + */ +int Inflater::doDecode(Huffman *h) +{ + int bitTmp = bitBuf; + int left = bitCnt; + int code = 0; + int first = 0; + int index = 0; + int len = 1; + int *next = h->count + 1; + while (true) + { + while (left--) + { + code |= bitTmp & 1; + bitTmp >>= 1; + int count = *next++; + if (code < first + count) + { /* if length len, return symbol */ + bitBuf = bitTmp; + bitCnt = (bitCnt - len) & 7; + return h->symbol[index + (code - first)]; + } + index += count; + first += count; + first <<= 1; + code <<= 1; + len++; + } + left = (MAXBITS+1) - len; + if (left == 0) + break; + if (srcPos >= src.size()) + { + error("premature end of input"); + dump(); + return -1; + } + bitTmp = src[srcPos++]; + if (left > 8) + left = 8; + } + + error("no end of block found"); + return -1; +} + +/** + * + */ +bool Inflater::doCodes(Huffman *lencode, Huffman *distcode) +{ + static const int lens[29] = { // Size base for length codes 257..285 + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258}; + static const int lext[29] = { // Extra bits for length codes 257..285 + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, + 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0}; + static const int dists[30] = { // Offset base for distance codes 0..29 + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, + 8193, 12289, 16385, 24577}; + static const int dext[30] = { // Extra bits for distance codes 0..29 + 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, + 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, + 12, 12, 13, 13}; + + //decode literals and length/distance pairs + while (true) + { + int symbol = doDecode(lencode); + if (symbol == 256) + break; + if (symbol < 0) + { + return false; + } + if (symbol < 256) //literal + { + dest.push_back(symbol); + } + else if (symbol > 256)//length + { + symbol -= 257; + if (symbol >= 29) + { + error("invalid fixed code"); + return false; + } + int ret; + if (!getBits(lext[symbol], &ret)) + return false; + int len = lens[symbol] + ret; + + symbol = doDecode(distcode);//distance + if (symbol < 0) + { + return false; + } + + if (!getBits(dext[symbol], &ret)) + return false; + unsigned int dist = dists[symbol] + ret; + if (dist > dest.size()) + { + error("distance too far back %d/%d", dist, dest.size()); + dump(); + //printf("pos:%d\n", srcPos); + return false; + } + + // copy length bytes from distance bytes back + //dest.push_back('{'); + while (len--) + { + dest.push_back(dest[dest.size() - dist]); + } + //dest.push_back('}'); + + } + } + + return true; +} + +/** + */ +bool Inflater::doStored() +{ + //trace("### stored ###"); + + // clear bits from current byte + bitBuf = 0; + bitCnt = 0; + + // length + if (srcPos + 4 > src.size()) + { + error("not enough input"); + return false; + } + + int len = src[srcPos++]; + len |= src[srcPos++] << 8; + //trace("### len:%d", len); + // check complement + if (src[srcPos++] != (~len & 0xff) || + src[srcPos++] != ((~len >> 8) & 0xff)) + { + error("twos complement for storage size do not match"); + return false; + } + + // copy data + if (srcPos + len > src.size()) + { + error("Not enough input for stored block"); + return false; + } + while (len--) + dest.push_back(src[srcPos++]); + + return true; +} + +/** + */ +bool Inflater::doFixed() +{ + //trace("### fixed ###"); + + static bool firstTime = true; + static int lencnt[MAXBITS+1], lensym[FIXLCODES]; + static int distcnt[MAXBITS+1], distsym[MAXDCODES]; + static Huffman lencode = {lencnt, lensym}; + static Huffman distcode = {distcnt, distsym}; + + if (firstTime) + { + firstTime = false; + + int lengths[FIXLCODES]; + + // literal/length table + int symbol = 0; + for ( ; symbol < 144; symbol++) + lengths[symbol] = 8; + for ( ; symbol < 256; symbol++) + lengths[symbol] = 9; + for ( ; symbol < 280; symbol++) + lengths[symbol] = 7; + for ( ; symbol < FIXLCODES; symbol++) + lengths[symbol] = 8; + buildHuffman(&lencode, lengths, FIXLCODES); + + // distance table + for (int symbol = 0; symbol < MAXDCODES; symbol++) + lengths[symbol] = 5; + buildHuffman(&distcode, lengths, MAXDCODES); + } + + // decode data until end-of-block code + bool ret = doCodes(&lencode, &distcode); + return ret; +} + +/** + */ +bool Inflater::doDynamic() +{ + //trace("### dynamic ###"); + int lengths[MAXCODES]; // descriptor code lengths + int lencnt[MAXBITS+1], lensym[MAXLCODES]; // lencode memory + int distcnt[MAXBITS+1], distsym[MAXDCODES]; // distcode memory + Huffman lencode = {lencnt, lensym}; // length code + Huffman distcode = {distcnt, distsym}; // distance code + static const int order[19] = // permutation of code length codes + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + + // get number of lengths in each table, check lengths + int ret; + if (!getBits(5, &ret)) + return false; + int nlen = ret + 257; + if (!getBits(5, &ret)) + return false; + int ndist = ret + 1; + if (!getBits(4, &ret)) + return false; + int ncode = ret + 4; + if (nlen > MAXLCODES || ndist > MAXDCODES) + { + error("Bad codes"); + return false; + } + + // get code length code lengths + int index = 0; + for ( ; index < ncode; index++) + { + if (!getBits(3, &ret)) + return false; + lengths[order[index]] = ret; + } + for ( ; index < 19; index++) + lengths[order[index]] = 0; + + // build huffman table for code lengths codes + if (buildHuffman(&lencode, lengths, 19) != 0) + return false; + + // read length/literal and distance code length tables + index = 0; + while (index < nlen + ndist) + { + int symbol = doDecode(&lencode); + if (symbol < 16) // length in 0..15 + lengths[index++] = symbol; + else + { // repeat instruction + int len = 0; // assume repeating zeros + if (symbol == 16) + { // repeat last length 3..6 times + if (index == 0) + { + error("no last length"); + return false; + } + len = lengths[index - 1];// last length + if (!getBits(2, &ret)) + return false; + symbol = 3 + ret; + } + else if (symbol == 17) // repeat zero 3..10 times + { + if (!getBits(3, &ret)) + return false; + symbol = 3 + ret; + } + else // == 18, repeat zero 11..138 times + { + if (!getBits(7, &ret)) + return false; + symbol = 11 + ret; + } + if (index + symbol > nlen + ndist) + { + error("too many lengths"); + return false; + } + while (symbol--) // repeat last or zero symbol times + lengths[index++] = len; + } + } + + // build huffman table for literal/length codes + int err = buildHuffman(&lencode, lengths, nlen); + if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1)) + { + error("incomplete length codes"); + //return false; + } + // build huffman table for distance codes + err = buildHuffman(&distcode, lengths + nlen, ndist); + if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1)) + { + error("incomplete dist codes"); + return false; + } + + // decode data until end-of-block code + bool retn = doCodes(&lencode, &distcode); + return retn; +} + +/** + */ +bool Inflater::inflate(std::vector &destination, + std::vector &source) +{ + dest.clear(); + src = source; + srcPos = 0; + bitBuf = 0; + bitCnt = 0; + + while (true) + { + int last; // one if last block + if (!getBits(1, &last)) + return false; + int type; // block type 0..3 + if (!getBits(2, &type)) + return false; + switch (type) + { + case 0: + if (!doStored()) + return false; + break; + case 1: + if (!doFixed()) + return false; + break; + case 2: + if (!doDynamic()) + return false; + break; + default: + error("Unknown block type %d", type); + return false; + } + if (last) + break; + } + + destination = dest; + + return true; +} + + + + + + +//######################################################################## +//# D E F L A T E R +//######################################################################## + + +#define DEFLATER_BUF_SIZE 32768 +class Deflater +{ +public: + + /** + * + */ + Deflater(); + + /** + * + */ + virtual ~Deflater(); + + /** + * + */ + virtual void reset(); + + /** + * + */ + virtual bool update(int ch); + + /** + * + */ + virtual bool finish(); + + /** + * + */ + virtual std::vector &getCompressed(); + + /** + * + */ + bool deflate(std::vector &dest, + const std::vector &src); + + void encodeDistStatic(unsigned int len, unsigned int dist); + +private: + + //debug messages + void error(char const *fmt, ...) + #ifdef G_GNUC_PRINTF + G_GNUC_PRINTF(2, 3) + #endif + ; + + void trace(char const *fmt, ...) + #ifdef G_GNUC_PRINTF + G_GNUC_PRINTF(2, 3) + #endif + ; + + bool compressWindow(); + + bool compress(); + + std::vector compressed; + + std::vector uncompressed; + + std::vector window; + + unsigned int windowPos; + + //#### Output + unsigned int outputBitBuf; + unsigned int outputNrBits; + + void put(int ch); + + void putWord(int ch); + + void putFlush(); + + void putBits(unsigned int ch, unsigned int bitsWanted); + + void putBitsR(unsigned int ch, unsigned int bitsWanted); + + //#### Huffman Encode + void encodeLiteralStatic(unsigned int ch); + + unsigned char windowBuf[DEFLATER_BUF_SIZE]; + //assume 32-bit ints + unsigned int windowHashBuf[DEFLATER_BUF_SIZE]; +}; + + +//######################################################################## +//# A P I +//######################################################################## + + +/** + * + */ +Deflater::Deflater() +{ + reset(); +} + +/** + * + */ +Deflater::~Deflater() += default; + +/** + * + */ +void Deflater::reset() +{ + compressed.clear(); + uncompressed.clear(); + window.clear(); + windowPos = 0; + outputBitBuf = 0; + outputNrBits = 0; + for (int k=0; k &Deflater::getCompressed() +{ + return compressed; +} + + +/** + * + */ +bool Deflater::deflate(std::vector &dest, + const std::vector &src) +{ + reset(); + uncompressed = src; + if (!compress()) + return false; + dest = compressed; + return true; +} + + + + + + + +//######################################################################## +//# W O R K I N G C O D E +//######################################################################## + + +//############################# +//# M E S S A G E S +//############################# + +/** + * Print error messages + */ +void Deflater::error(char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stdout, "Deflater error:"); + vfprintf(stdout, fmt, args); + fprintf(stdout, "\n"); + va_end(args); +} + +/** + * Print trace messages + */ +void Deflater::trace(char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stdout, "Deflater:"); + vfprintf(stdout, fmt, args); + fprintf(stdout, "\n"); + va_end(args); +} + + + + +//############################# +//# O U T P U T +//############################# + +/** + * + */ +void Deflater::put(int ch) +{ + compressed.push_back(ch); + outputBitBuf = 0; + outputNrBits = 0; +} + +/** + * + */ +void Deflater::putWord(int ch) +{ + int lo = (ch ) & 0xff; + int hi = (ch>>8) & 0xff; + put(lo); + put(hi); +} + +/** + * + */ +void Deflater::putFlush() +{ + if (outputNrBits > 0) + { + put(outputBitBuf & 0xff); + } + outputBitBuf = 0; + outputNrBits = 0; +} + +/** + * + */ +void Deflater::putBits(unsigned int ch, unsigned int bitsWanted) +{ + //trace("n:%4u, %d\n", ch, bitsWanted); + + while (bitsWanted--) + { + //add bits to position 7. shift right + outputBitBuf = (outputBitBuf>>1) + (ch<<7 & 0x80); + ch >>= 1; + outputNrBits++; + if (outputNrBits >= 8) + { + unsigned char b = outputBitBuf & 0xff; + //printf("b:%02x\n", b); + put(b); + } + } +} + +static unsigned int bitReverse(unsigned int code, unsigned int nrBits) +{ + unsigned int outb = 0; + while (nrBits--) + { + outb = (outb << 1) | (code & 0x01); + code >>= 1; + } + return outb; +} + + +/** + * + */ +void Deflater::putBitsR(unsigned int ch, unsigned int bitsWanted) +{ + //trace("r:%4u, %d", ch, bitsWanted); + + unsigned int rcode = bitReverse(ch, bitsWanted); + + putBits(rcode, bitsWanted); + +} + + +//############################# +//# E N C O D E +//############################# + + + +void Deflater::encodeLiteralStatic(unsigned int ch) +{ + //trace("c: %d", ch); + + if (ch < 144) + { + putBitsR(ch + 0x0030 , 8); // 00110000 + } + else if (ch < 256) + { + putBitsR(ch - 144 + 0x0190 , 9); // 110010000 + } + else if (ch < 280) + { + putBitsR(ch - 256 + 0x0000 , 7); // 0000000 + } + else if (ch < 288) + { + putBitsR(ch - 280 + 0x00c0 , 8); // 11000000 + } + else //out of range + { + error("Literal out of range: %d", ch); + } + +} + + +struct LenBase +{ + unsigned int base; + unsigned int range; + unsigned int bits; +}; + +LenBase lenBases[] = +{ + // clang-format off + { 3, 1, 0 }, + { 4, 1, 0 }, + { 5, 1, 0 }, + { 6, 1, 0 }, + { 7, 1, 0 }, + { 8, 1, 0 }, + { 9, 1, 0 }, + { 10, 1, 0 }, + { 11, 2, 1 }, + { 13, 2, 1 }, + { 15, 2, 1 }, + { 17, 2, 1 }, + { 19, 4, 2 }, + { 23, 4, 2 }, + { 27, 4, 2 }, + { 31, 4, 2 }, + { 35, 8, 3 }, + { 43, 8, 3 }, + { 51, 8, 3 }, + { 59, 8, 3 }, + { 67, 16, 4 }, + { 83, 16, 4 }, + { 99, 16, 4 }, + { 115, 16, 4 }, + { 131, 32, 5 }, + { 163, 32, 5 }, + { 195, 32, 5 }, + { 227, 32, 5 }, + { 258, 1, 0 } + // clang-format on +}; + +struct DistBase +{ + unsigned int base; + unsigned int range; + unsigned int bits; +}; + +DistBase distBases[] = +{ + // clang-format off + { 1, 1, 0 }, + { 2, 1, 0 }, + { 3, 1, 0 }, + { 4, 1, 0 }, + { 5, 2, 1 }, + { 7, 2, 1 }, + { 9, 4, 2 }, + { 13, 4, 2 }, + { 17, 8, 3 }, + { 25, 8, 3 }, + { 33, 16, 4 }, + { 49, 16, 4 }, + { 65, 32, 5 }, + { 97, 32, 5 }, + { 129, 64, 6 }, + { 193, 64, 6 }, + { 257, 128, 7 }, + { 385, 128, 7 }, + { 513, 256, 8 }, + { 769, 256, 8 }, + { 1025, 512, 9 }, + { 1537, 512, 9 }, + { 2049, 1024, 10 }, + { 3073, 1024, 10 }, + { 4097, 2048, 11 }, + { 6145, 2048, 11 }, + { 8193, 4096, 12 }, + { 12289, 4096, 12 }, + { 16385, 8192, 13 }, + { 24577, 8192, 13 } + // clang-format on +}; + +void Deflater::encodeDistStatic(unsigned int len, unsigned int dist) +{ + + //## Output length + + if (len < 3 || len > 258) + { + error("Length out of range:%d", len); + return; + } + + bool found = false; + for (int i=0 ; i<29 ; i++) + { + unsigned int base = lenBases[i].base; + unsigned int range = lenBases[i].range; + if (base + range > len) + { + unsigned int lenCode = 257 + i; + unsigned int length = len - base; + //trace("--- %d %d %d %d", len, base, range, length); + encodeLiteralStatic(lenCode); + putBits(length, lenBases[i].bits); + found = true; + break; + } + } + if (!found) + { + error("Length not found in table:%d", len); + return; + } + + //## Output distance + + if (dist < 4 || dist > 32768) + { + error("Distance out of range:%d", dist); + return; + } + + found = false; + for (int i=0 ; i<30 ; i++) + { + unsigned int base = distBases[i].base; + unsigned int range = distBases[i].range; + if (base + range > dist) + { + unsigned int distCode = i; + unsigned int distance = dist - base; + //error("--- %d %d %d %d", dist, base, range, distance); + putBitsR(distCode, 5); + putBits(distance, distBases[i].bits); + found = true; + break; + } + } + if (!found) + { + error("Distance not found in table:%d", dist); + return; + } +} + + +//############################# +//# C O M P R E S S +//############################# + + +/** + * This method does the dirty work of dictionary + * compression. Basically it looks for redundant + * strings and has the current duplicate refer back + * to the previous one. + */ +bool Deflater::compressWindow() +{ + windowPos = 0; + unsigned int windowSize = window.size(); + //### Compress as much of the window as possible + + unsigned int hash = 0; + //Have each value be a long with the byte at this position, + //plus the 3 bytes after it in the window + for (int i=windowSize-1 ; i>=0 ; i--) + { + unsigned char ch = window[i]; + windowBuf[i] = ch; + hash = ((hash<<8) & 0xffffff00) | ch; + windowHashBuf[i] = hash; + } + + while (windowPos < windowSize - 3) + { + //### Find best match, if any + unsigned int bestMatchLen = 0; + unsigned int bestMatchDist = 0; + if (windowPos >= 4) + { + for (unsigned int lookBack=0 ; lookBack= windowPos -4 ) + lookAheadMax = windowPos - 4 - lookBack; + if (lookAheadMax > 258) + lookAheadMax = 258; + unsigned char *wp = &(windowBuf[windowPos+4]); + unsigned char *lb = &(windowBuf[lookBack+4]); + while (lookAhead bestMatchLen) + { + bestMatchLen = lookAhead; + bestMatchDist = windowPos - lookBack; + } + } + } + } + if (bestMatchLen > 3) + { + //Distance encode + //trace("### distance"); + /* + printf("### 1 '"); + for (int i=0 ; i < bestMatchLen ; i++) + fputc(window[windowPos+i], stdout); + printf("'\n### 2 '"); + for (int i=0 ; i < bestMatchLen ; i++) + fputc(window[windowPos-bestMatchDist+i], stdout); + printf("'\n"); + */ + encodeDistStatic(bestMatchLen, bestMatchDist); + windowPos += bestMatchLen; + } + else + { + //Literal encode + //trace("### literal"); + encodeLiteralStatic(windowBuf[windowPos]); + windowPos++; + } + } + + while (windowPos < windowSize) + encodeLiteralStatic(windowBuf[windowPos++]); + + encodeLiteralStatic(256); + return true; +} + + +/** + * + */ +bool Deflater::compress() +{ + //trace("compress"); + unsigned long total = 0L; + windowPos = 0; + std::vector::iterator iter; + for (iter = uncompressed.begin(); iter != uncompressed.end() ; ) + { + total += windowPos; + trace("total:%ld", total); + if (windowPos > window.size()) + windowPos = window.size(); + window.erase(window.begin() , window.begin()+windowPos); + while (window.size() < 32768 && iter != uncompressed.end()) + { + window.push_back(*iter); + ++iter; + } + if (window.size() >= 32768) + putBits(0x00, 1); //0 -- more blocks + else + putBits(0x01, 1); //1 -- last block + putBits(0x01, 2); //01 -- static trees + if (!compressWindow()) + return false; + } + putFlush(); + return true; +} + + + + + +//######################################################################## +//# G Z I P F I L E +//######################################################################## + +/** + * Constructor + */ +GzipFile::GzipFile() : + data(), + fileName(), + fileBuf(), + fileBufPos(0), + compressionMethod(0) +{ +} + +/** + * Destructor + */ +GzipFile::~GzipFile() += default; + +/** + * Print error messages + */ +void GzipFile::error(char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stdout, "GzipFile error:"); + vfprintf(stdout, fmt, args); + fprintf(stdout, "\n"); + va_end(args); +} + +/** + * Print trace messages + */ +void GzipFile::trace(char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stdout, "GzipFile:"); + vfprintf(stdout, fmt, args); + fprintf(stdout, "\n"); + va_end(args); +} + +/** + * + */ +void GzipFile::put(unsigned char ch) +{ + data.push_back(ch); +} + +/** + * + */ +void GzipFile::setData(const std::vector &str) +{ + data = str; +} + +/** + * + */ +void GzipFile::clearData() +{ + data.clear(); +} + +/** + * + */ +std::vector &GzipFile::getData() +{ + return data; +} + +/** + * + */ +std::string &GzipFile::getFileName() +{ + return fileName; +} + +/** + * + */ +void GzipFile::setFileName(const std::string &val) +{ + fileName = val; +} + + + +//##################################### +//# U T I L I T Y +//##################################### + +/** + * Loads a new file into an existing GzipFile + */ +bool GzipFile::loadFile(const std::string &fName) +{ + FILE *f = fopen(fName.c_str() , "rb"); + if (!f) + { + error("Cannot open file %s", fName.c_str()); + return false; + } + while (true) + { + int ch = fgetc(f); + if (ch < 0) + break; + data.push_back(ch); + } + fclose(f); + setFileName(fName); + return true; +} + + + +//##################################### +//# W R I T E +//##################################### + +/** + * + */ +bool GzipFile::putByte(unsigned char ch) +{ + fileBuf.push_back(ch); + return true; +} + + + +/** + * + */ +bool GzipFile::putLong(unsigned long val) +{ + fileBuf.push_back( (unsigned char)((val ) & 0xff)); + fileBuf.push_back( (unsigned char)((val>> 8) & 0xff)); + fileBuf.push_back( (unsigned char)((val>>16) & 0xff)); + fileBuf.push_back( (unsigned char)((val>>24) & 0xff)); + return true; +} + + + +/** + * + */ +bool GzipFile::write() +{ + fileBuf.clear(); + + putByte(0x1f); //magic + putByte(0x8b); //magic + putByte( 8); //compression method + putByte(0x08); //flags. say we have a crc and file name + + unsigned long ltime = (unsigned long) time(nullptr); + putLong(ltime); + + //xfl + putByte(0); + //OS + putByte(0); + + //file name + for (char i : fileName) + putByte(i); + putByte(0); + + + //compress + std::vector compBuf; + Deflater deflater; + if (!deflater.deflate(compBuf, data)) + { + return false; + } + + std::vector::iterator iter; + for (iter=compBuf.begin() ; iter!=compBuf.end() ; ++iter) + { + unsigned char ch = *iter; + putByte(ch); + } + + Crc32 crcEngine; + crcEngine.update(data); + unsigned long crc = crcEngine.getValue(); + putLong(crc); + + putLong(data.size()); + + return true; +} + + +/** + * + */ +bool GzipFile::writeBuffer(std::vector &outBuf) +{ + if (!write()) + return false; + outBuf.clear(); + outBuf = fileBuf; + return true; +} + + +/** + * + */ +bool GzipFile::writeFile(const std::string &fileName) +{ + if (!write()) + return false; + FILE *f = fopen(fileName.c_str(), "wb"); + if (!f) + return false; + std::vector::iterator iter; + for (iter=fileBuf.begin() ; iter!=fileBuf.end() ; ++iter) + { + unsigned char ch = *iter; + fputc(ch, f); + } + fclose(f); + return true; +} + + +//##################################### +//# R E A D +//##################################### + +bool GzipFile::getByte(unsigned char *ch) +{ + if (fileBufPos >= fileBuf.size()) + { + error("unexpected end of data"); + return false; + } + *ch = fileBuf[fileBufPos++]; + return true; +} + +/** + * + */ +bool GzipFile::getLong(unsigned long *val) +{ + if (fileBuf.size() - fileBufPos < 4) + return false; + int ch1 = fileBuf[fileBufPos++]; + int ch2 = fileBuf[fileBufPos++]; + int ch3 = fileBuf[fileBufPos++]; + int ch4 = fileBuf[fileBufPos++]; + *val = ((ch4<<24) & 0xff000000L) | + ((ch3<<16) & 0x00ff0000L) | + ((ch2<< 8) & 0x0000ff00L) | + ((ch1 ) & 0x000000ffL); + return true; +} + +bool GzipFile::read() +{ + fileBufPos = 0; + + unsigned char ch; + + //magic cookie + if (!getByte(&ch)) + return false; + if (ch != 0x1f) + { + error("bad gzip header"); + return false; + } + if (!getByte(&ch)) + return false; + if (ch != 0x8b) + { + error("bad gzip header"); + return false; + } + + //## compression method + if (!getByte(&ch)) + return false; + compressionMethod = ch & 0xff; + + //## flags + if (!getByte(&ch)) + return false; + //bool ftext = ch & 0x01; + bool fhcrc = ch & 0x02; + bool fextra = ch & 0x04; + bool fname = ch & 0x08; + bool fcomment = ch & 0x10; + + //trace("cm:%d ftext:%d fhcrc:%d fextra:%d fname:%d fcomment:%d", + // cm, ftext, fhcrc, fextra, fname, fcomment); + + //## file time + unsigned long ltime; + if (!getLong(<ime)) + return false; + //time_t mtime = (time_t)ltime; + + //## XFL + if (!getByte(&ch)) + return false; + //int xfl = ch; + + //## OS + if (!getByte(&ch)) + return false; + //int os = ch; + + //std::string timestr = ctime(&mtime); + //trace("xfl:%d os:%d mtime:%s", xfl, os, timestr.c_str()); + + if (fextra) + { + if (!getByte(&ch)) + return false; + long xlen = ch; + if (!getByte(&ch)) + return false; + xlen = (xlen << 8) + ch; + for (long l=0 ; l compBuf; + while (fileBufPos < fileBuf.size() - 8) + { + if (!getByte(&ch)) + return false; + compBuf.push_back(ch); + } + //uncompress + data.clear(); + Inflater inflater; + if (!inflater.inflate(data, compBuf)) + { + return false; + } + + //Get the CRC and compare + Crc32 crcEngine; + crcEngine.update(data); + unsigned long calcCrc = crcEngine.getValue(); + unsigned long givenCrc; + if (!getLong(&givenCrc)) + return false; + if (givenCrc != calcCrc) + { + error("Specified crc, %ud, not what received: %ud", + givenCrc, calcCrc); + return false; + } + + //Get the file size and compare + unsigned long givenFileSize; + if (!getLong(&givenFileSize)) + return false; + if (givenFileSize != data.size()) + { + error("Specified data size, %ld, not what received: %ld", + givenFileSize, data.size()); + return false; + } + + return true; +} + + + +/** + * + */ +bool GzipFile::readBuffer(const std::vector &inbuf) +{ + fileBuf = inbuf; + if (!read()) + return false; + return true; +} + + +/** + * + */ +bool GzipFile::readFile(const std::string &fileName) +{ + fileBuf.clear(); + FILE *f = fopen(fileName.c_str(), "rb"); + if (!f) + return false; + while (true) + { + int ch = fgetc(f); + if (ch < 0) + break; + fileBuf.push_back(ch); + } + fclose(f); + if (!read()) + return false; + return true; +} + + + + + + + + +//######################################################################## +//# Z I P F I L E +//######################################################################## + +/** + * Constructor + */ +ZipEntry::ZipEntry() : + crc (0L), + fileName (), + comment (), + compressionMethod (8), + compressedData (), + uncompressedData (), + position (0) +{ +} + +/** + * + */ +ZipEntry::ZipEntry(std::string fileNameArg, + std::string commentArg) : + crc (0L), + fileName (std::move(fileNameArg)), + comment (std::move(commentArg)), + compressionMethod (8), + compressedData (), + uncompressedData (), + position (0) +{ +} + +/** + * Destructor + */ +ZipEntry::~ZipEntry() += default; + + +/** + * + */ +std::string ZipEntry::getFileName() +{ + return fileName; +} + +/** + * + */ +void ZipEntry::setFileName(const std::string &val) +{ + fileName = val; +} + +/** + * + */ +std::string ZipEntry::getComment() +{ + return comment; +} + +/** + * + */ +void ZipEntry::setComment(const std::string &val) +{ + comment = val; +} + +/** + * + */ +unsigned long ZipEntry::getCompressedSize() +{ + return (unsigned long)compressedData.size(); +} + +/** + * + */ +int ZipEntry::getCompressionMethod() +{ + return compressionMethod; +} + +/** + * + */ +void ZipEntry::setCompressionMethod(int val) +{ + compressionMethod = val; +} + +/** + * + */ +std::vector &ZipEntry::getCompressedData() +{ + return compressedData; +} + +/** + * + */ +void ZipEntry::setCompressedData(const std::vector &val) +{ + compressedData = val; +} + +/** + * + */ +unsigned long ZipEntry::getUncompressedSize() +{ + return (unsigned long)uncompressedData.size(); +} + +/** + * + */ +std::vector &ZipEntry::getUncompressedData() +{ + return uncompressedData; +} + +/** + * + */ +void ZipEntry::setUncompressedData(const std::vector &val) +{ + uncompressedData = val; +} + +void ZipEntry::setUncompressedData(const std::string &s) +{ + uncompressedData.clear(); + uncompressedData.reserve(s.size()); + uncompressedData.insert(uncompressedData.begin(), s.begin(), s.end()); +} + +/** + * + */ +unsigned long ZipEntry::getCrc() +{ + return crc; +} + +/** + * + */ +void ZipEntry::setCrc(unsigned long val) +{ + crc = val; +} + +/** + * + */ +void ZipEntry::write(unsigned char ch) +{ + uncompressedData.push_back(ch); +} + +/** + * + */ +void ZipEntry::finish() +{ + Crc32 c32; + std::vector::iterator iter; + for (iter = uncompressedData.begin() ; + iter!= uncompressedData.end() ; ++iter) + { + unsigned char ch = *iter; + c32.update(ch); + } + crc = c32.getValue(); + switch (compressionMethod) + { + case 0: //none + { + for (iter = uncompressedData.begin() ; + iter!= uncompressedData.end() ; ++iter) + { + unsigned char ch = *iter; + compressedData.push_back(ch); + } + break; + } + case 8: //deflate + { + Deflater deflater; + if (!deflater.deflate(compressedData, uncompressedData)) + { + //some error + } + break; + } + default: + { + printf("error: unknown compression method %d\n", + compressionMethod); + } + } +} + + + + +/** + * + */ +bool ZipEntry::readFile(const std::string &fileNameArg, + const std::string &commentArg) +{ + crc = 0L; + uncompressedData.clear(); + fileName = fileNameArg; + comment = commentArg; + FILE *f = fopen(fileName.c_str(), "rb"); + if (!f) + { + return false; + } + while (true) + { + int ch = fgetc(f); + if (ch < 0) + break; + uncompressedData.push_back((unsigned char)ch); + } + fclose(f); + finish(); + return true; +} + + +/** + * + */ +void ZipEntry::setPosition(unsigned long val) +{ + position = val; +} + +/** + * + */ +unsigned long ZipEntry::getPosition() +{ + return position; +} + + + + + + + +/** + * Constructor + */ +ZipFile::ZipFile() : + entries(), + fileBuf(), + fileBufPos(0), + comment() +{ +} + +/** + * Destructor + */ +ZipFile::~ZipFile() +{ + std::vector::iterator iter; + for (iter=entries.begin() ; iter!=entries.end() ; ++iter) + { + ZipEntry *entry = *iter; + delete entry; + } + entries.clear(); +} + +/** + * + */ +void ZipFile::setComment(const std::string &val) +{ + comment = val; +} + +/** + * + */ +std::string ZipFile::getComment() +{ + return comment; +} + + +/** + * + */ +std::vector &ZipFile::getEntries() +{ + return entries; +} + + + +//##################################### +//# M E S S A G E S +//##################################### + +void ZipFile::error(char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stdout, "ZipFile error:"); + vfprintf(stdout, fmt, args); + fprintf(stdout, "\n"); + va_end(args); +} + +void ZipFile::trace(char const *fmt, ...) +{ + va_list args; + va_start(args, fmt); + fprintf(stdout, "ZipFile:"); + vfprintf(stdout, fmt, args); + fprintf(stdout, "\n"); + va_end(args); +} + +//##################################### +//# U T I L I T Y +//##################################### + +/** + * + */ +ZipEntry *ZipFile::addFile(const std::string &fileName, + const std::string &comment) +{ + ZipEntry *ze = new ZipEntry(); + if (!ze->readFile(fileName, comment)) + { + delete ze; + return nullptr; + } + entries.push_back(ze); + return ze; +} + + +/** + * + */ +ZipEntry *ZipFile::newEntry(const std::string &fileName, + const std::string &comment) +{ + ZipEntry *ze = new ZipEntry(fileName, comment); + entries.push_back(ze); + return ze; +} + + +//##################################### +//# W R I T E +//##################################### + +/** + * + */ +bool ZipFile::putLong(unsigned long val) +{ + fileBuf.push_back( ((int)(val )) & 0xff); + fileBuf.push_back( ((int)(val>> 8)) & 0xff); + fileBuf.push_back( ((int)(val>>16)) & 0xff); + fileBuf.push_back( ((int)(val>>24)) & 0xff); + return true; +} + + +/** + * + */ +bool ZipFile::putInt(unsigned int val) +{ + fileBuf.push_back( (val ) & 0xff); + fileBuf.push_back( (val>> 8) & 0xff); + return true; +} + +/** + * + */ +bool ZipFile::putByte(unsigned char val) +{ + fileBuf.push_back(val); + return true; +} + +/** + * + */ +bool ZipFile::writeFileData() +{ + std::vector::iterator iter; + for (iter = entries.begin() ; iter != entries.end() ; ++iter) + { + ZipEntry *entry = *iter; + entry->setPosition(fileBuf.size()); + //##### HEADER + std::string fname = entry->getFileName(); + putLong(0x04034b50L); + putInt(20); //versionNeeded + putInt(0); //gpBitFlag + //putInt(0); //compression method + putInt(entry->getCompressionMethod()); //compression method + putInt(0); //mod time + putInt(0); //mod date + putLong(entry->getCrc()); //crc32 + putLong(entry->getCompressedSize()); + putLong(entry->getUncompressedSize()); + putInt(fname.size());//fileName length + putInt(8);//extra field length + //file name + for (char i : fname) + putByte((unsigned char)i); + //extra field + putInt(0x7855); + putInt(4); + putInt(100); + putInt(100); + + //##### DATA + std::vector &buf = entry->getCompressedData(); + std::vector::iterator iter; + for (iter = buf.begin() ; iter != buf.end() ; ++iter) + { + unsigned char ch = (unsigned char) *iter; + putByte(ch); + } + } + return true; +} + +/** + * + */ +bool ZipFile::writeCentralDirectory() +{ + unsigned long cdPosition = fileBuf.size(); + std::vector::iterator iter; + for (iter = entries.begin() ; iter != entries.end() ; ++iter) + { + ZipEntry *entry = *iter; + std::string fname = entry->getFileName(); + std::string ecomment = entry->getComment(); + putLong(0x02014b50L); //magic cookie + putInt(2386); //versionMadeBy + putInt(20); //versionNeeded + putInt(0); //gpBitFlag + putInt(entry->getCompressionMethod()); //compression method + putInt(0); //mod time + putInt(0); //mod date + putLong(entry->getCrc()); //crc32 + putLong(entry->getCompressedSize()); + putLong(entry->getUncompressedSize()); + putInt(fname.size());//fileName length + putInt(4);//extra field length + putInt(ecomment.size());//comment length + putInt(0); //disk number start + putInt(0); //internal attributes + putLong(0); //external attributes + putLong(entry->getPosition()); + + //file name + for (char i : fname) + putByte((unsigned char)i); + //extra field + putInt(0x7855); + putInt(0); + //comment + for (char i : ecomment) + putByte((unsigned char)i); + } + unsigned long cdSize = fileBuf.size() - cdPosition; + + putLong(0x06054b50L); + putInt(0);//number of this disk + putInt(0);//nr of disk with central dir + putInt(entries.size()); //number of entries on this disk + putInt(entries.size()); //number of entries total + putLong(cdSize); //size of central dir + putLong(cdPosition); //position of central dir + putInt(comment.size());//comment size + for (char i : comment) + putByte(i); + return true; +} + + + +/** + * + */ +bool ZipFile::write() +{ + fileBuf.clear(); + if (!writeFileData()) + return false; + if (!writeCentralDirectory()) + return false; + return true; +} + + +/** + * + */ +bool ZipFile::writeBuffer(std::vector &outBuf) +{ + if (!write()) + return false; + outBuf.clear(); + outBuf = fileBuf; + return true; +} + + +/** + * + */ +bool ZipFile::writeFile(const std::string &fileName) +{ + if (!write()) + return false; + FILE *f = fopen(fileName.c_str(), "wb"); + if (!f) + return false; + std::vector::iterator iter; + for (iter=fileBuf.begin() ; iter!=fileBuf.end() ; ++iter) + { + unsigned char ch = *iter; + fputc(ch, f); + } + fclose(f); + return true; +} + +//##################################### +//# R E A D +//##################################### + +/** + * + */ +bool ZipFile::getLong(unsigned long *val) +{ + if (fileBuf.size() - fileBufPos < 4) + return false; + int ch1 = fileBuf[fileBufPos++]; + int ch2 = fileBuf[fileBufPos++]; + int ch3 = fileBuf[fileBufPos++]; + int ch4 = fileBuf[fileBufPos++]; + *val = ((ch4<<24) & 0xff000000L) | + ((ch3<<16) & 0x00ff0000L) | + ((ch2<< 8) & 0x0000ff00L) | + ((ch1 ) & 0x000000ffL); + return true; +} + +/** + * + */ +bool ZipFile::getInt(unsigned int *val) +{ + if (fileBuf.size() - fileBufPos < 2) + return false; + int ch1 = fileBuf[fileBufPos++]; + int ch2 = fileBuf[fileBufPos++]; + *val = ((ch2<< 8) & 0xff00) | + ((ch1 ) & 0x00ff); + return true; +} + + +/** + * + */ +bool ZipFile::getByte(unsigned char *val) +{ + if (fileBuf.size() <= fileBufPos) + return false; + *val = fileBuf[fileBufPos++]; + return true; +} + + +/** + * + */ +bool ZipFile::readFileData() +{ + //printf("#################################################\n"); + //printf("###D A T A\n"); + //printf("#################################################\n"); + while (true) + { + unsigned long magicCookie; + if (!getLong(&magicCookie)) + { + error("magic cookie not found"); + break; + } + trace("###Cookie:%lx", magicCookie); + if (magicCookie == 0x02014b50L) //central directory + break; + if (magicCookie != 0x04034b50L) + { + error("file header not found"); + return false; + } + unsigned int versionNeeded; + if (!getInt(&versionNeeded)) + { + error("bad version needed found"); + return false; + } + unsigned int gpBitFlag; + if (!getInt(&gpBitFlag)) + { + error("bad bit flag found"); + return false; + } + unsigned int compressionMethod; + if (!getInt(&compressionMethod)) + { + error("bad compressionMethod found"); + return false; + } + unsigned int modTime; + if (!getInt(&modTime)) + { + error("bad modTime found"); + return false; + } + unsigned int modDate; + if (!getInt(&modDate)) + { + error("bad modDate found"); + return false; + } + unsigned long crc32; + if (!getLong(&crc32)) + { + error("bad crc32 found"); + return false; + } + unsigned long compressedSize; + if (!getLong(&compressedSize)) + { + error("bad compressedSize found"); + return false; + } + unsigned long uncompressedSize; + if (!getLong(&uncompressedSize)) + { + error("bad uncompressedSize found"); + return false; + } + unsigned int fileNameLength; + if (!getInt(&fileNameLength)) + { + error("bad fileNameLength found"); + return false; + } + unsigned int extraFieldLength; + if (!getInt(&extraFieldLength)) + { + error("bad extraFieldLength found"); + return false; + } + std::string fileName; + for (unsigned int i=0 ; i compBuf; + if (gpBitFlag & 0x8)//bit 3 was set. means we don't know compressed size + { + unsigned char c1, c2, c3, c4; + c2 = c3 = c4 = 0; + while (true) + { + unsigned char ch; + if (!getByte(&ch)) + { + error("premature end of data"); + break; + } + compBuf.push_back(ch); + c1 = c2; c2 = c3; c3 = c4; c4 = ch; + if (c1 == 0x50 && c2 == 0x4b && c3 == 0x07 && c4 == 0x08) + { + trace("found end of compressed data"); + //remove the cookie + compBuf.erase(compBuf.end() -4, compBuf.end()); + break; + } + } + } + else + { + for (unsigned long bnr = 0 ; bnr < compressedSize ; bnr++) + { + unsigned char ch; + if (!getByte(&ch)) + { + error("premature end of data"); + break; + } + compBuf.push_back(ch); + } + } + + printf("### data: "); + for (int i=0 ; i<10 ; i++) + printf("%02x ", compBuf[i] & 0xff); + printf("\n"); + + if (gpBitFlag & 0x8)//only if bit 3 set + { + /* this cookie was read in the loop above + unsigned long dataDescriptorSignature ; + if (!getLong(&dataDescriptorSignature)) + break; + if (dataDescriptorSignature != 0x08074b50L) + { + error("bad dataDescriptorSignature found"); + return false; + } + */ + unsigned long crc32; + if (!getLong(&crc32)) + { + error("bad crc32 found"); + return false; + } + unsigned long compressedSize; + if (!getLong(&compressedSize)) + { + error("bad compressedSize found"); + return false; + } + unsigned long uncompressedSize; + if (!getLong(&uncompressedSize)) + { + error("bad uncompressedSize found"); + return false; + } + }//bit 3 was set + //break; + + std::vector uncompBuf; + switch (compressionMethod) + { + case 8: //deflate + { + Inflater inflater; + if (!inflater.inflate(uncompBuf, compBuf)) + { + return false; + } + break; + } + default: + { + error("Unimplemented compression method %d", compressionMethod); + return false; + } + } + + if (uncompressedSize != uncompBuf.size()) + { + error("Size mismatch. Expected %ld, received %ld", + uncompressedSize, uncompBuf.size()); + return false; + } + + Crc32 crcEngine; + crcEngine.update(uncompBuf); + unsigned long crc = crcEngine.getValue(); + if (crc != crc32) + { + error("Crc mismatch. Calculated %08ux, received %08ux", crc, crc32); + return false; + } + + ZipEntry *ze = new ZipEntry(fileName, comment); + ze->setCompressionMethod(compressionMethod); + ze->setCompressedData(compBuf); + ze->setUncompressedData(uncompBuf); + ze->setCrc(crc); + entries.push_back(ze); + + + } + return true; +} + + +/** + * + */ +bool ZipFile::readCentralDirectory() +{ + //printf("#################################################\n"); + //printf("###D I R E C T O R Y\n"); + //printf("#################################################\n"); + while (true) + { + //We start with a central directory cookie already + //Check at the bottom of the loop. + unsigned int version; + if (!getInt(&version)) + { + error("bad version found"); + return false; + } + unsigned int versionNeeded; + if (!getInt(&versionNeeded)) + { + error("bad version found"); + return false; + } + unsigned int gpBitFlag; + if (!getInt(&gpBitFlag)) + { + error("bad bit flag found"); + return false; + } + unsigned int compressionMethod; + if (!getInt(&compressionMethod)) + { + error("bad compressionMethod found"); + return false; + } + unsigned int modTime; + if (!getInt(&modTime)) + { + error("bad modTime found"); + return false; + } + unsigned int modDate; + if (!getInt(&modDate)) + { + error("bad modDate found"); + return false; + } + unsigned long crc32; + if (!getLong(&crc32)) + { + error("bad crc32 found"); + return false; + } + unsigned long compressedSize; + if (!getLong(&compressedSize)) + { + error("bad compressedSize found"); + return false; + } + unsigned long uncompressedSize; + if (!getLong(&uncompressedSize)) + { + error("bad uncompressedSize found"); + return false; + } + unsigned int fileNameLength; + if (!getInt(&fileNameLength)) + { + error("bad fileNameLength found"); + return false; + } + unsigned int extraFieldLength; + if (!getInt(&extraFieldLength)) + { + error("bad extraFieldLength found"); + return false; + } + unsigned int fileCommentLength; + if (!getInt(&fileCommentLength)) + { + error("bad fileCommentLength found"); + return false; + } + unsigned int diskNumberStart; + if (!getInt(&diskNumberStart)) + { + error("bad diskNumberStart found"); + return false; + } + unsigned int internalFileAttributes; + if (!getInt(&internalFileAttributes)) + { + error("bad internalFileAttributes found"); + return false; + } + unsigned long externalFileAttributes; + if (!getLong(&externalFileAttributes)) + { + error("bad externalFileAttributes found"); + return false; + } + unsigned long localHeaderOffset; + if (!getLong(&localHeaderOffset)) + { + error("bad localHeaderOffset found"); + return false; + } + std::string fileName; + for (unsigned int i=0 ; i &inbuf) +{ + fileBuf = inbuf; + if (!read()) + return false; + return true; +} + + +/** + * + */ +bool ZipFile::readFile(const std::string &fileName) +{ + fileBuf.clear(); + FILE *f = fopen(fileName.c_str(), "rb"); + if (!f) + return false; + while (true) + { + int ch = fgetc(f); + if (ch < 0) + break; + fileBuf.push_back(ch); + } + fclose(f); + if (!read()) + return false; + return true; +} + + + + + + + + + +//######################################################################## +//# E N D O F F I L E +//######################################################################## + + diff --git a/src/util/ziptool.h b/src/util/ziptool.h new file mode 100644 index 0000000..26aaac8 --- /dev/null +++ b/src/util/ziptool.h @@ -0,0 +1,575 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/** @file + * TODO: insert short description here + *//* + * Authors: + * see git history + * Bob Jamison + * + * Copyright (C) 2018 Authors + * Released under GNU LGPL v2.1+, read the file 'COPYING' for more information. + */ +/* + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef SEEN_ZIPTOOL_H +#define SEEN_ZIPTOOL_H +/** + * This is intended to be a standalone, reduced capability + * implementation of Gzip and Zip functionality. Its + * targeted use case is for archiving and retrieving single files + * which use these encoding types. Being memory based and + * non-optimized, it is not useful in cases where very large + * archives are needed or where high performance is desired. + * However, it should hopefully work well for smaller, + * one-at-a-time tasks. What you get in return is the ability + * to drop these files into your project and remove the dependencies + * on ZLib and Info-Zip. Enjoy. + */ + + + +#include +#include + + +//######################################################################## +//# A D L E R 3 2 +//######################################################################## + +class Adler32 +{ +public: + + Adler32(); + + virtual ~Adler32(); + + void reset(); + + void update(unsigned char b); + + void update(char *str); + + unsigned long getValue(); + +private: + + unsigned long value; + +}; + + +//######################################################################## +//# C R C 3 2 +//######################################################################## + +class Crc32 +{ +public: + + Crc32(); + + virtual ~Crc32(); + + void reset(); + + void update(unsigned char b); + + void update(char *str); + + void update(const std::vector &buf); + + unsigned long getValue(); + +private: + + unsigned long value; + +}; + + + + + + +//######################################################################## +//# G Z I P S T R E A M S +//######################################################################## + +class GzipFile +{ +public: + + /** + * + */ + GzipFile(); + + /** + * + */ + virtual ~GzipFile(); + + /** + * + */ + virtual void put(unsigned char ch); + + /** + * + */ + virtual void setData(const std::vector &str); + + /** + * + */ + virtual void clearData(); + + /** + * + */ + virtual std::vector &getData(); + + /** + * + */ + virtual std::string &getFileName(); + + /** + * + */ + virtual void setFileName(const std::string &val); + + + //###################### + //# U T I L I T Y + //###################### + + /** + * + */ + virtual bool readFile(const std::string &fName); + + //###################### + //# W R I T E + //###################### + + /** + * + */ + virtual bool write(); + + /** + * + */ + virtual bool writeBuffer(std::vector &outbuf); + + /** + * + */ + virtual bool writeFile(const std::string &fileName); + + + //###################### + //# R E A D + //###################### + + + /** + * + */ + virtual bool read(); + + /** + * + */ + virtual bool readBuffer(const std::vector &inbuf); + + /** + * + */ + virtual bool loadFile(const std::string &fileName); + + + +private: + + std::vector data; + std::string fileName; + + //debug messages + void error(char const *fmt, ...) + #ifdef G_GNUC_PRINTF + G_GNUC_PRINTF(2, 3) + #endif + ; + + void trace(char const *fmt, ...) + #ifdef G_GNUC_PRINTF + G_GNUC_PRINTF(2, 3) + #endif + ; + + std::vector fileBuf; + unsigned long fileBufPos; + + bool getByte(unsigned char *ch); + bool getLong(unsigned long *val); + + bool putByte(unsigned char ch); + bool putLong(unsigned long val); + + int compressionMethod; +}; + + + + +//######################################################################## +//# Z I P F I L E +//######################################################################## + + +/** + * + */ +class ZipEntry +{ +public: + + /** + * + */ + ZipEntry(); + + /** + * + */ + ZipEntry(std::string fileName, + std::string comment); + + /** + * + */ + virtual ~ZipEntry(); + + /** + * + */ + virtual std::string getFileName(); + + /** + * + */ + virtual void setFileName(const std::string &val); + + /** + * + */ + virtual std::string getComment(); + + /** + * + */ + virtual void setComment(const std::string &val); + + /** + * + */ + virtual unsigned long getCompressedSize(); + + /** + * + */ + virtual int getCompressionMethod(); + + /** + * + */ + virtual void setCompressionMethod(int val); + + /** + * + */ + virtual std::vector &getCompressedData(); + + /** + * + */ + virtual void setCompressedData(const std::vector &val); + + /** + * + */ + virtual unsigned long getUncompressedSize(); + + /** + * + */ + virtual std::vector &getUncompressedData(); + + /** + * + */ + virtual void setUncompressedData(const std::vector &val); + virtual void setUncompressedData(const std::string &val); + + /** + * + */ + virtual void write(unsigned char ch); + + /** + * + */ + virtual void finish(); + + /** + * + */ + virtual unsigned long getCrc(); + + /** + * + */ + virtual void setCrc(unsigned long crc); + + /** + * + */ + virtual bool readFile(const std::string &fileNameArg, + const std::string &commentArg); + + /** + * + */ + virtual void setPosition(unsigned long val); + + /** + * + */ + virtual unsigned long getPosition(); + +private: + + unsigned long crc; + + std::string fileName; + std::string comment; + + int compressionMethod; + + std::vector compressedData; + std::vector uncompressedData; + + unsigned long position; +}; + + + + + + + + + +/** + * This class sits over the zlib and gzip code to + * implement a PKWare or Info-Zip .zip file reader and + * writer + */ +class ZipFile +{ +public: + + /** + * + */ + ZipFile(); + + /** + * + */ + virtual ~ZipFile(); + + //###################### + //# V A R I A B L E S + //###################### + + /** + * + */ + virtual void setComment(const std::string &val); + + /** + * + */ + virtual std::string getComment(); + + /** + * Return the list of entries currently in this file + */ + std::vector &getEntries(); + + + //###################### + //# U T I L I T Y + //###################### + + /** + * + */ + virtual ZipEntry *addFile(const std::string &fileNameArg, + const std::string &commentArg); + + /** + * + */ + virtual ZipEntry *newEntry(const std::string &fileNameArg, + const std::string &commentArg); + + //###################### + //# W R I T E + //###################### + + /** + * + */ + virtual bool write(); + + /** + * + */ + virtual bool writeBuffer(std::vector &outbuf); + + /** + * + */ + virtual bool writeFile(const std::string &fileName); + + + //###################### + //# R E A D + //###################### + + + /** + * + */ + virtual bool read(); + + /** + * + */ + virtual bool readBuffer(const std::vector &inbuf); + + /** + * + */ + virtual bool readFile(const std::string &fileName); + + +private: + + //debug messages + void error(char const *fmt, ...) + #ifdef G_GNUC_PRINTF + G_GNUC_PRINTF(2, 3) + #endif + ; + void trace(char const *fmt, ...) + #ifdef G_GNUC_PRINTF + G_GNUC_PRINTF(2, 3) + #endif + ; + + //# Private writing methods + + /** + * + */ + bool putLong(unsigned long val); + + /** + * + */ + bool putInt(unsigned int val); + + + /** + * + */ + bool putByte(unsigned char val); + + /** + * + */ + bool writeFileData(); + + /** + * + */ + bool writeCentralDirectory(); + + + //# Private reading methods + + /** + * + */ + bool getLong(unsigned long *val); + + /** + * + */ + bool getInt(unsigned int *val); + + /** + * + */ + bool getByte(unsigned char *val); + + /** + * + */ + bool readFileData(); + + /** + * + */ + bool readCentralDirectory(); + + + std::vector entries; + + std::vector fileBuf; + unsigned long fileBufPos; + + std::string comment; +}; + + + + + + +#endif // SEEN_ZIPTOOL_H + + +//######################################################################## +//# E N D O F F I L E +//######################################################################## + -- cgit v1.2.3