Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
font-lister.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5/*
6 * Authors:
7 * Chris Lahey <clahey@ximian.com>
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * Tavmjong Bah <tavmjong@free.fr>
10 * See Git history
11 *
12 * Copyright (C) 1999-2001 Ximian, Inc.
13 * Copyright (C) 2002 Lauris Kaplinski
14 * Copyright (C) 2013 Tavmjong Bah
15 * Copyright (C) 2018+ Authors
16 *
17 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
18 */
19
20#ifndef FONT_LISTER_H
21#define FONT_LISTER_H
22
23#include <memory>
24#include <set>
25#include <vector>
26#include <unordered_map>
27
28#include <glibmm/ustring.h>
29#include <glibmm/stringutils.h> // For strescape()
30
31#include <gtkmm/liststore.h>
32#include <gtkmm/treemodel.h>
33#include <gtkmm/treemodelcolumn.h>
34#include <gtkmm/treepath.h>
35
36inline constexpr int FONT_FAMILIES_GROUP_SIZE = 30;
37
38class SPObject;
39class SPDocument;
40class SPCSSAttr;
41class SPStyle;
42class StyleNames;
43
44namespace Gtk {
45class CellRenderer;
46} // namespace Gtk
47
48namespace Inkscape {
49
84{
85public:
86 enum class Exception
87 {
90 };
91
92 using Styles = std::vector<StyleNames>;
93
97 struct FontListClass : Gtk::TreeModelColumnRecord
98 {
100 Gtk::TreeModelColumn<Glib::ustring> family;
101
103 Gtk::TreeModelColumn<std::shared_ptr<Styles>> styles;
104
106 Gtk::TreeModelColumn<bool> onSystem;
107
115 Gtk::TreeModelColumn<PangoFontFamily *> pango_family;
116
118 {
119 add(family);
120 add(styles);
121 add(onSystem);
122 add(pango_family);
123 }
124 };
125
127
128 struct FontStyleListClass : Gtk::TreeModelColumnRecord
129 {
131 Gtk::TreeModelColumn<Glib::ustring> displayStyle;
132
134 Gtk::TreeModelColumn<Glib::ustring> cssStyle;
135
137 {
138 add(cssStyle);
139 add(displayStyle);
140 }
141 };
142
144
149 std::map<std::string, PangoFontFamily *> pango_family_map;
150
157 Glib::RefPtr<Gtk::ListStore> const &get_font_list() const { return font_list_store; }
158
162 Glib::RefPtr<Gtk::ListStore> const &get_style_list() const { return style_list_store; }
163
168 void insert_font_family(Glib::ustring const &new_family);
169
171
175 void update_font_list(SPDocument *document);
176
177public:
179
188 bool find_string_case_insensitive(std::string const &text, std::string const &pat);
189 void show_results(Glib::ustring const &search_text);
190 void apply_collections(std::set <Glib::ustring> &selected_collections);
191 void set_dragging_family(Glib::ustring const &new_family);
192
193 Glib::ustring canonize_fontspec(Glib::ustring const &fontspec) const;
194
198 Glib::ustring system_fontspec(Glib::ustring const &fontspec);
199
204 std::pair<Glib::ustring, Glib::ustring> ui_from_fontspec(Glib::ustring const &fontspec) const;
205
210 std::pair<Glib::ustring, Glib::ustring> selection_update();
211
217 void set_fontspec(Glib::ustring const &fontspec, bool check = true);
218
219 Glib::ustring get_fontspec() const { return canonize_fontspec(current_family + ", " + current_style); }
220
229 std::pair<Glib::ustring, Glib::ustring> new_font_family(Glib::ustring const &family, bool check_style = true);
230
239 std::pair<Glib::ustring, Glib::ustring> set_font_family(Glib::ustring const &family, bool check_style = true,
240 bool emit = true);
241
250 std::pair<Glib::ustring, Glib::ustring> set_font_family(int row, bool check_style = true, bool emit = true);
251
252 Glib::ustring const &get_font_family() const
253 {
254 return current_family;
255 }
256
257 Glib::ustring const &get_dragging_family() const
258 {
259 return dragging_family;
260 }
261
263 {
264 return current_family_row;
265 }
266
270 void set_font_style(Glib::ustring style, bool emit = true);
271
272 Glib::ustring const &get_font_style() const
273 {
274 return current_style;
275 }
276
277 Glib::ustring fontspec_from_style(SPStyle *style) const;
278
282 void fill_css(SPCSSAttr *css, Glib::ustring fontspec = {});
283
284 Gtk::TreeModel::Row get_row_for_font() { return get_row_for_font(current_family); }
285
286 Gtk::TreeModel::Row get_row_for_font(Glib::ustring const &family);
287
288 Gtk::TreePath get_path_for_font(Glib::ustring const &family);
289
290 bool is_path_for_font(Gtk::TreePath path, Glib::ustring family);
291
292 Gtk::TreeModel::Row get_row_for_style() { return get_row_for_style(current_style); }
293
294 Gtk::TreeModel::Row get_row_for_style(Glib::ustring const &style);
295
296 Gtk::TreePath get_path_for_style(Glib::ustring const &style);
297
298 std::pair<Gtk::TreePath, Gtk::TreePath> get_paths(Glib::ustring const &family, Glib::ustring const &style);
299
303 Glib::ustring get_best_style_match(Glib::ustring const &family, Glib::ustring const &style);
304
308 void ensureRowStyles(Gtk::TreeModel::iterator iter);
309
313 Glib::ustring get_font_family_markup(Gtk::TreeModel::const_iterator const &iter) const;
314
321 sigc::connection connectUpdate(sigc::slot<void ()> slot) {
322 return update_signal.connect(slot);
323 }
324 sigc::connection connectNewFonts(sigc::slot<void ()> slot) {
325 return new_fonts_signal.connect(slot);
326 }
327
328 bool blocked() const { return block; }
329
330 int get_font_families_size() const { return pango_family_map.size(); }
331 bool font_installed_on_system(Glib::ustring const &font) const;
332
333 void init_font_families(int group_offset = -1, int group_size = -1);
334 void init_default_styles();
335 std::pair<bool, std::string> get_font_count_label() const;
336
337private:
338 FontLister();
340
341 void update_font_data_recursive(SPObject& r, std::map<Glib::ustring, std::set<Glib::ustring>> &font_data);
342
343 void font_family_row_update(int start = 0);
344
345 Glib::RefPtr<Gtk::ListStore> font_list_store;
346 Glib::RefPtr<Gtk::ListStore> style_list_store;
347
353 Glib::ustring current_family = "sans-serif";
354 Glib::ustring dragging_family;
355 Glib::ustring current_style = "Normal";
356
360 std::shared_ptr<Styles> default_styles;
361
362 bool block = false;
363 void emit_update();
364 sigc::signal<void ()> update_signal;
365 sigc::signal<void ()> new_fonts_signal;
366};
367
368} // namespace Inkscape
369
370// Helper functions
371bool font_lister_separator_func(Glib::RefPtr<Gtk::TreeModel> const &model,
372 Gtk::TreeModel::const_iterator const &iter);
373
374void font_lister_cell_data_func(Gtk::CellRenderer *renderer,
375 Gtk::TreeModel::const_iterator const &iter);
376
377void font_lister_cell_data_func_markup(Gtk::CellRenderer *renderer,
378 Gtk::TreeModel::const_iterator const &iter);
379
380void font_lister_cell_data_func2(Gtk::CellRenderer &cell,
381 Gtk::TreeModel::const_iterator const &iter,
382 bool with_markup);
383
384void font_lister_style_cell_data_func(Gtk::CellRenderer *renderer,
385 Gtk::TreeModel::const_iterator const &iter);
386
387#endif // FONT_LISTER_H
388
389/*
390 Local Variables:
391 mode:c++
392 c-file-style:"stroustrup"
393 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
394 indent-tabs-mode:nil
395 fill-column:99
396 End:
397*/
398// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8 :
This class enumerates fonts using libnrtype into reusable data stores and allows for random access to...
Definition font-lister.h:84
Glib::ustring get_font_family_markup(Gtk::TreeModel::const_iterator const &iter) const
Get markup for font-family.
Gtk::TreeModel::Row get_row_for_style()
std::pair< bool, std::string > get_font_count_label() const
void insert_font_family(Glib::ustring const &new_family)
Inserts a font family or font-fallback list (for use when not already in document or on system).
Glib::ustring current_style
void update_font_data_recursive(SPObject &r, std::map< Glib::ustring, std::set< Glib::ustring > > &font_data)
bool find_string_case_insensitive(std::string const &text, std::string const &pat)
Takes a hand written font spec and returns a Pango generated one in standard form.
Gtk::TreePath get_path_for_style(Glib::ustring const &style)
void set_fontspec(Glib::ustring const &fontspec, bool check=true)
Sets current_fontspec, etc.
int current_family_row
Info for currently selected font (what is shown in the UI).
std::pair< Glib::ustring, Glib::ustring > new_font_family(Glib::ustring const &family, bool check_style=true)
Changes font-family, updating style list and attempting to find closest style to current_style style ...
Glib::ustring const & get_dragging_family() const
int get_font_families_size() const
bool blocked() const
Glib::ustring current_family
std::pair< Glib::ustring, Glib::ustring > ui_from_fontspec(Glib::ustring const &fontspec) const
Gets font-family and style from fontspec.
void set_font_style(Glib::ustring style, bool emit=true)
Sets style.
std::pair< Glib::ustring, Glib::ustring > selection_update()
Sets font-family and style after a selection change.
Glib::RefPtr< Gtk::ListStore > const & get_style_list() const
void update_font_list(SPDocument *document)
Updates font list to include fonts in document.
Glib::RefPtr< Gtk::ListStore > style_list_store
Glib::RefPtr< Gtk::ListStore > font_list_store
Glib::ustring canonize_fontspec(Glib::ustring const &fontspec) const
void fill_css(SPCSSAttr *css, Glib::ustring fontspec={})
Fill css using given fontspec (doesn't need to be member function).
void apply_collections(std::set< Glib::ustring > &selected_collections)
std::shared_ptr< Styles > default_styles
If a font-family is not on system, this list of styles is used.
void ensureRowStyles(Gtk::TreeModel::iterator iter)
Ensures the style list for a particular family has been created.
Gtk::TreeModel::Row get_row_for_font()
std::vector< StyleNames > Styles
Definition font-lister.h:92
Gtk::TreePath get_path_for_font(Glib::ustring const &family)
int get_font_family_row() const
Glib::ustring system_fontspec(Glib::ustring const &fontspec)
Find closest system font to given font.
sigc::signal< void()> new_fonts_signal
void font_family_row_update(int start=0)
std::map< std::string, PangoFontFamily * > pango_family_map
The list of fonts, sorted by the order they will appear in the UI.
void init_font_families(int group_offset=-1, int group_size=-1)
int add_document_fonts_at_top(SPDocument *document)
Glib::ustring const & get_font_style() const
bool font_installed_on_system(Glib::ustring const &font) const
bool is_path_for_font(Gtk::TreePath path, Glib::ustring family)
void show_results(Glib::ustring const &search_text)
static Inkscape::FontLister * get_instance()
FontStyleListClass font_style_list
std::pair< Gtk::TreePath, Gtk::TreePath > get_paths(Glib::ustring const &family, Glib::ustring const &style)
Glib::RefPtr< Gtk::ListStore > const & get_font_list() const
Glib::ustring get_best_style_match(Glib::ustring const &family, Glib::ustring const &style)
Return best style match for new font given style for old font.
std::pair< Glib::ustring, Glib::ustring > set_font_family(Glib::ustring const &family, bool check_style=true, bool emit=true)
Sets font-family, updating style list and attempting to find closest style to old current_style.
void set_dragging_family(Glib::ustring const &new_family)
sigc::connection connectNewFonts(sigc::slot< void()> slot)
Glib::ustring get_fontspec() const
sigc::connection connectUpdate(sigc::slot< void()> slot)
Let users of FontLister know to update GUI.
FontListClass font_list
Glib::ustring const & get_font_family() const
Glib::ustring dragging_family
sigc::signal< void()> update_signal
Glib::ustring fontspec_from_style(SPStyle *style) const
Typed SVG document implementation.
Definition document.h:101
SPObject is an abstract base class of all of the document nodes at the SVG document level.
Definition sp-object.h:160
An SVG style object.
Definition style.h:45
std::shared_ptr< Css const > css
constexpr int FONT_FAMILIES_GROUP_SIZE
Definition font-lister.h:36
void font_lister_cell_data_func_markup(Gtk::CellRenderer *renderer, Gtk::TreeModel::const_iterator const &iter)
void font_lister_style_cell_data_func(Gtk::CellRenderer *renderer, Gtk::TreeModel::const_iterator const &iter)
bool font_lister_separator_func(Glib::RefPtr< Gtk::TreeModel > const &model, Gtk::TreeModel::const_iterator const &iter)
void font_lister_cell_data_func(Gtk::CellRenderer *renderer, Gtk::TreeModel::const_iterator const &iter)
void font_lister_cell_data_func2(Gtk::CellRenderer &cell, Gtk::TreeModel::const_iterator const &iter, bool with_markup)
Geom::Point start
Definition desktop.h:50
Helper class to stream background task notifications as a series of messages.
GtkTreeModelColumnRecord for the font-family list Gtk::ListStore.
Definition font-lister.h:98
Gtk::TreeModelColumn< std::shared_ptr< Styles > > styles
Styles for each family name.
Gtk::TreeModelColumn< Glib::ustring > family
Family name.
Gtk::TreeModelColumn< PangoFontFamily * > pango_family
Not actually a column.
Gtk::TreeModelColumn< bool > onSystem
Whether font is on system.
Gtk::TreeModelColumn< Glib::ustring > cssStyle
Column containing the styles in CSS/Pango format.
Gtk::TreeModelColumn< Glib::ustring > displayStyle
Column containing the styles as Font designer used.