summaryrefslogtreecommitdiffstats
path: root/src/util/font-collections.h
blob: 5b9e088d42afe620e7994d2c96da7c4f8438fc84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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 <vaibhavmalik2018@gmail.com>
 *
 * 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 <map>
#include <set>

#include <glib/gi18n.h>
#include <giomm.h>
#include <gtkmm.h>
#include <sigc++/sigc++.h>

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 <Glib::ustring> 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 <Glib::ustring> 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 <Glib::ustring>&, bool is_system = false);
    void write_collection(const Glib::ustring& collection_name, const std::set <Glib::ustring>& 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 <Glib::ustring> get_collections(bool is_system = false);
    std::vector <Glib::ustring> get_all_collections();
    std::set <Glib::ustring> 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 <void ()> 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 <void ()> slot) {
        return selection_update_signal.connect(slot);
    }

private:
    FontCollections();

    std::set <FontCollection> _system_collections;
    std::set <FontCollection> _user_collections;
    std::set <Glib::ustring> _selected_collections;

    void _read(const Glib::ustring&, bool is_system = false);

    sigc::signal <void ()> update_signal;
    sigc::signal <void ()> 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 :