summaryrefslogtreecommitdiffstats
path: root/src/ui/dialog/font-substitution.cpp
blob: bf912595ea05412f505297c9d7e961bb2e401b35 (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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *
 * Copyright (C) 2012 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <set>

#include <glibmm/i18n.h>
#include <glibmm/regex.h>

#include <gtkmm/messagedialog.h>
#include <gtkmm/checkbutton.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/textview.h>

#include "font-substitution.h"

#include "desktop.h"
#include "document.h"
#include "inkscape.h"
#include "selection-chemistry.h"
#include "text-editing.h"

#include "object/sp-root.h"
#include "object/sp-text.h"
#include "object/sp-textpath.h"
#include "object/sp-flowtext.h"
#include "object/sp-flowdiv.h"
#include "object/sp-tspan.h"

#include "libnrtype/FontFactory.h"
#include "libnrtype/font-instance.h"

#include "ui/dialog-events.h"

namespace Inkscape {
namespace UI {
namespace Dialog {

FontSubstitution::FontSubstitution()
= default;

FontSubstitution::~FontSubstitution()
= default;

void
FontSubstitution::checkFontSubstitutions(SPDocument* doc)
{
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
    int show_dlg = prefs->getInt("/options/font/substitutedlg", 0);
    if (show_dlg) {
        Glib::ustring out;
        std::vector<SPItem*> l =  getFontReplacedItems(doc, &out);
        if (out.length() > 0) {
            show(out, l);
        }
    }
}

void
FontSubstitution::show(Glib::ustring out, std::vector<SPItem*> &l)
{
   Gtk::MessageDialog warning(_("\nSome fonts are not available and have been substituted."),
                       false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true);
   warning.set_resizable(true);
   warning.set_title(_("Font substitution"));

   GtkWidget *dlg = GTK_WIDGET(warning.gobj());
   sp_transientize(dlg);

   Gtk::TextView * textview = new Gtk::TextView();
   textview->set_editable(false);
   textview->set_wrap_mode(Gtk::WRAP_WORD);
   textview->show();
   textview->get_buffer()->set_text(_(out.c_str()));

   Gtk::ScrolledWindow * scrollwindow = new Gtk::ScrolledWindow();
   scrollwindow->add(*textview);
   scrollwindow->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
   scrollwindow->set_shadow_type(Gtk::SHADOW_IN);
   scrollwindow->set_size_request(0, 100);
   scrollwindow->show();

   Gtk::CheckButton *cbSelect = new Gtk::CheckButton();
   cbSelect->set_label(_("Select all the affected items"));
   cbSelect->set_active(true);
   cbSelect->show();

   Gtk::CheckButton *cbWarning = new Gtk::CheckButton();
   cbWarning->set_label(_("Don't show this warning again"));
   cbWarning->show();

   auto box = warning.get_content_area();
   box->set_spacing(2);
   box->pack_start(*scrollwindow, true, true, 4);
   box->pack_start(*cbSelect, false, false, 0);
   box->pack_start(*cbWarning, false, false, 0);

   warning.run();

   if (cbWarning->get_active()) {
       Inkscape::Preferences *prefs = Inkscape::Preferences::get();
       prefs->setInt("/options/font/substitutedlg", 0);
   }

   if (cbSelect->get_active()) {

       SPDesktop *desktop = SP_ACTIVE_DESKTOP;
       Inkscape::Selection *selection = desktop->getSelection();
       selection->clear();
       selection->setList(l);
   }

}

/*
 * Find all the fonts that are in the document but not available on the users system
 * and have been substituted for other fonts
 *
 * Return a list of SPItems where fonts have been substituted.
 *
 * Walk through all the objects ...
 * a. Build up a list of the objects with fonts defined in the style attribute
 * b. Build up a list of the objects rendered fonts - taken for the objects layout/spans
 * If there are fonts in a. that are not in b. then those fonts have been substituted.
 */
std::vector<SPItem*> FontSubstitution::getFontReplacedItems(SPDocument* doc, Glib::ustring *out)
{
    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
    std::vector<SPItem*> allList;
    std::vector<SPItem*> outList,x,y;
    std::set<Glib::ustring> setErrors;
    std::set<Glib::ustring> setFontSpans;
    std::map<SPItem *, Glib::ustring> mapFontStyles;

    allList = get_all_items(x, doc->getRoot(), desktop, false, false, true, y);
    for(auto item : allList){
        SPStyle *style = item->style;
        Glib::ustring family = "";

        if (is_top_level_text_object (item)) {
            // Should only need to check the first span, since the others should be covered by TSPAN's etc
            family = te_get_layout(item)->getFontFamily(0);
            setFontSpans.insert(family);
        }
        else if (SP_IS_TEXTPATH(item)) {
            SPTextPath const *textpath = SP_TEXTPATH(item);
            if (textpath->originalPath != nullptr) {
                family = SP_TEXT(item->parent)->layout.getFontFamily(0);
                setFontSpans.insert(family);
            }
        }
        else if (SP_IS_TSPAN(item) || SP_IS_FLOWTSPAN(item)) {
            // is_part_of_text_subtree (item)
             // TSPAN layout comes from the parent->layout->_spans
             SPObject *parent_text = item;
             while (parent_text && !SP_IS_TEXT(parent_text)) {
                 parent_text = parent_text->parent;
             }
             if (parent_text != nullptr) {
                 family = SP_TEXT(parent_text)->layout.getFontFamily(0);
                 // Add all the spans fonts to the set
                 for (unsigned int f=0; f < parent_text->children.size(); f++) {
                     family = SP_TEXT(parent_text)->layout.getFontFamily(f);
                     setFontSpans.insert(family);
                 }
             }
        }

        if (style) {
            gchar const *style_font = nullptr;
            if (style->font_family.set)
                style_font = style->font_family.value();
            else if (style->font_specification.set)
                style_font = style->font_specification.value();
            else
                style_font = style->font_family.value();

            if (style_font) {
                if (has_visible_text(item)) {
                    mapFontStyles.insert(std::make_pair (item, style_font));
                }
            }
        }
    }

    // Check if any document styles are not in the actual layout
    std::map<SPItem *, Glib::ustring>::const_reverse_iterator mapIter;
    for (mapIter = mapFontStyles.rbegin(); mapIter != mapFontStyles.rend(); ++mapIter) {
        SPItem *item = mapIter->first;
        Glib::ustring fonts = mapIter->second;

        // CSS font fallbacks can have more that one font listed, split the font list
        std::vector<Glib::ustring> vFonts = Glib::Regex::split_simple("," , fonts);
        bool fontFound = false;
        for(auto font : vFonts) {
            // trim whitespace
            size_t startpos = font.find_first_not_of(" \n\r\t");
            size_t endpos = font.find_last_not_of(" \n\r\t");
            if(( std::string::npos == startpos ) || ( std::string::npos == endpos)) {
                continue; // empty font name
            }
            font = font.substr( startpos, endpos-startpos+1 );
            std::set<Glib::ustring>::const_iterator iter = setFontSpans.find(font);
            if (iter != setFontSpans.end() ||
                    font == Glib::ustring("sans-serif") ||
                    font == Glib::ustring("Sans") ||
                    font == Glib::ustring("serif") ||
                    font == Glib::ustring("Serif") ||
                    font == Glib::ustring("monospace") ||
                    font == Glib::ustring("Monospace")) {
                fontFound = true;
                break;
            }
        }
        if (fontFound == false) {
            Glib::ustring subName = getSubstituteFontName(fonts);
            Glib::ustring err = Glib::ustring::compose(
                    _("Font '%1' substituted with '%2'"), fonts.c_str(), subName.c_str());
            setErrors.insert(err);
            outList.push_back(item);
        }
    }

    std::set<Glib::ustring>::const_iterator setIter;
    for (setIter = setErrors.begin(); setIter != setErrors.end(); ++setIter) {
        Glib::ustring err = (*setIter);
        out->append(err + "\n");
        g_warning("%s", err.c_str());
    }

    return outList;
}


Glib::ustring FontSubstitution::getSubstituteFontName (Glib::ustring font)
{
    Glib::ustring out = font;

    PangoFontDescription *descr = pango_font_description_new();
    pango_font_description_set_family(descr,font.c_str());
    font_instance *res = (font_factory::Default())->Face(descr);
    if (res->pFont) {
        PangoFontDescription *nFaceDesc = pango_font_describe(res->pFont);
        out = sp_font_description_get_family(nFaceDesc);
    }
    pango_font_description_free(descr);

    return out;
}


} // namespace Dialog
} // namespace UI
} // 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 :