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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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 <vaibhavmalik2018@gmail.com>
*
* 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 <fstream>
// #include <iostream>
#ifdef _WIN32
#include<direct.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
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 <Glib::ustring> 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 :
|