summaryrefslogtreecommitdiffstats
path: root/src/conditions.cpp
blob: e092ddc8fa12115b69ea198bb7834976b34befee (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * SVG conditional attribute evaluation
 *
 * Authors:
 *   Andrius R. <knutux@gmail.com>
 *   Abhishek Sharma
 *
 * Copyright (C) 2006 authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <vector>
#include <set>

#include <glibmm/ustring.h>

#include "conditions.h"
#include "rdf.h"

#include "object/sp-item.h"

#include "xml/repr.h"

typedef bool (* condition_evaluator)(SPItem const *item, gchar const *value);

struct Condition {
    gchar const *attribute;
    condition_evaluator evaluator;
};

static bool evaluateSystemLanguage(SPItem const *item, gchar const *value);
static bool evaluateRequiredFeatures(SPItem const *item, gchar const *value);
static bool evaluateRequiredExtensions(SPItem const *item, gchar const *value);

/* define any conditional attributes and their handler functions in this array */
static Condition _condition_handlers[] = {
    { "systemLanguage", evaluateSystemLanguage },
    { "requiredFeatures", evaluateRequiredFeatures },
    { "requiredExtensions", evaluateRequiredExtensions },
};

// function which evaluates if item should be displayed
bool sp_item_evaluate(SPItem const *item) {
    bool needDisplay = true;
    for ( unsigned int i = 0 ; needDisplay && (i < sizeof(_condition_handlers) / sizeof(_condition_handlers[0])) ; i++ ) {
        gchar const *value = item->getAttribute(_condition_handlers[i].attribute);
        if ( value && !_condition_handlers[i].evaluator(item, value) ) {
            needDisplay = false;
        }
    }
    return needDisplay;
}

#define ISALNUM(c)    (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z') || ((c) >= '0' && (c) <= '9'))

// TODO: review and modernize this function
static gchar *preprocessLanguageCode(const gchar *language_code) {
    if ( nullptr == language_code || strcmp(language_code, "") == 0)
        return nullptr;

    gchar *lngcode = g_strdup(language_code);
    for ( unsigned int i = 0 ; i < strlen(lngcode) ; i++ ) {
        if ( lngcode[i] >= 'A' && lngcode[i] <= 'Z' ) {
            lngcode[i] = g_ascii_tolower(lngcode[i]);
        } else if ( '_' == lngcode[i] ) {
            lngcode[i] = '-';
        } else if ( !ISALNUM(lngcode[i]) && '-' != lngcode[i] ) {
            // only alpha numeric characters and '-' may be contained in language code
            g_free(lngcode);
            return nullptr;
        }
    }

    return lngcode;
}

static bool evaluateSystemLanguage(SPItem const *item, gchar const *value) {
    if ( nullptr == value )
        return true;

    std::set<Glib::ustring> language_codes;
    gchar *str = nullptr;
    gchar **strlist = g_strsplit( value, ",", 0);

    for ( int i = 0 ; (str = strlist[i]) ; i++ ) {
        str = g_strstrip(str);
        gchar *lngcode = preprocessLanguageCode(str);
        if (lngcode == nullptr)
            continue;
        language_codes.insert(lngcode);

        gchar *pos = strchr (lngcode, '-');
        if (pos)
        {
            // if subtag is used, primary tag is still a perfect match
            *pos = 0;
            if (strlen(lngcode) && language_codes.find(lngcode) == language_codes.end()) {
                language_codes.insert(lngcode);
            }
        }
    }
    g_strfreev(strlist);

    if (language_codes.empty())
        return false;

    SPDocument *document = item->document;
    const auto document_languages = document->getLanguages();

    if (document_languages.size() == 0)
        return false;

    for (const auto &language : document_languages) {
        gchar *lngcode = preprocessLanguageCode(language.c_str());
        if (lngcode && (language_codes.find(lngcode) != language_codes.end())) {
            g_free(lngcode);
            return true;
        }
        g_free(lngcode);
    }
    return false;
}

static std::vector<Glib::ustring> splitByWhitespace(gchar const *value) {
    std::vector<Glib::ustring> parts;
    gchar *str = nullptr;
    gchar **strlist = g_strsplit( value, ",", 0);

    for ( int i = 0 ; (str = strlist[i]) ; i++ ) {
        gchar *part = g_strstrip(str);
        if ( 0 == *part )
            continue;
        parts.emplace_back(part);
    }
    g_strfreev(strlist);
    return parts;
}

#define SVG11FEATURE    "http://www.w3.org/TR/SVG11/feature#"
#define SVG10FEATURE    "org.w3c."

static bool evaluateSVG11Feature(gchar const *feature) {
    static gchar const *_supported_features[] = {
        "SVG", // incomplete "SVG-static" - missing support for "Filter"
           /* SVG - user agent supports at least one of the following:
                "SVG-static", "SVG-animation", "SVG-dynamic" or "SVGDOM" */
        // "SVGDOM", // not sure
           /* SVGDOM - user agent supports at least one of the following:
                 "SVGDOM-static", "SVGDOM-animation" or "SVGDOM-dynamic" */
        "SVG-static", // incomplete - missing support for "Filter"
           /* SVG-static - user agent supports the following features:
                "CoreAttribute", "Structure", "ContainerAttribute",
                "ConditionalProcessing", "Image", "Style", "ViewportAttribute",
                "Shape", "Text", "PaintAttribute", "OpacityAttribute",
                "GraphicsAttribute", "Marker", "ColorProfile",
                "Gradient", "Pattern", "Clip", "Mask", "Filter",
                "XlinkAttribute", "Font", "Extensibility" */
        // "SVGDOM-static", // not sure
           /* SVGDOM-static - All of the DOM interfaces and methods
                that correspond to SVG-static */
        // "SVG-animation", // no support
           /* SVG-animation - All of the language features from "SVG-static"
                plus the feature "feature#Animation" */
        // "SVGDOM-animation", // no support
           /* SVGDOM-animation - All of the DOM interfaces and methods
                that correspond to SVG-animation */
        // "SVG-dynamic", // no support
           /* SVG-dynamic - user agent supports all "SVG-animation" and the following features:
                "Hyperlinking", "Scripting", "View", "Cursor",
                "GraphicalEventsAttribute", "DocumentEventsAttribute", "AnimationEventsAttribute" */
        // "SVGDOM-dynamic", // no support
           /* SVGDOM-dynamic - All of the DOM interfaces and methods
                that correspond to SVG-dynamic */
        "CoreAttribute",
        "Structure",
        "BasicStructure",
        "ContainerAttribute",
        "ConditionalProcessing",
        "Image",
        "Style",
        "ViewportAttribute", // not sure
        "Shape",
        "Text",
        "BasicText",
        "PaintAttribute",
        "BasicPaintAttribute",
        "OpacityAttribute",
        "GraphicsAttribute",
        "BasicGraphicsAttribute",
        "Marker",
        "ColorProfile",
        "Gradient",
        "Pattern",
        "Clip",
        "BasicClip",
        "Mask",
        // "Filter",
        // "BasicFilter",
        // "DocumentEventsAttribute",
        // "GraphicalEventsAttribute",
        // "AnimationEventsAttribute",
        // "Cursor", // not sure
        "Hyperlinking", // not sure
        "XlinkAttribute", // not sure
        "ExternalResourcesRequired", // not sure
        "View",
        // "Script",
        // "Animation",
        "Font",
        "BasicFont",
        "Extensibility", // not sure
    };
    
    for (auto & _supported_feature : _supported_features) {
        if ( 0 == strcasecmp(feature, _supported_feature) )
            return true;
    }
    return false;
}

static bool evaluateSVG10Feature(gchar const *feature) {
    static gchar const *_supported_features[] = {
        "svg.static", // incomplete - no filter effects
        "dom.svg.static", // not sure
        // "svg.animation",
        // "dom.svg.animation",
        // "svg.dynamic",
        // "dom.svg.dynamic"
        // "svg.all",
        // "dom.svg.all"
    };
    for (auto & _supported_feature : _supported_features) {
        if ( 0 == strcasecmp(feature, _supported_feature) )
            return true;
    }
    return false;
}

static bool evaluateSingleFeature(gchar const *value) {
    if ( nullptr == value )
        return false;
    gchar const *found;
    found = strstr(value, SVG11FEATURE);
    if ( value == found )
        return evaluateSVG11Feature(found + strlen(SVG11FEATURE));
    found = strstr(value, SVG10FEATURE);
    if ( value == found )
        return evaluateSVG10Feature(found + strlen(SVG10FEATURE));
    return false;
}

static bool evaluateRequiredFeatures(SPItem const */*item*/, gchar const *value) {
    if ( nullptr == value )
        return true;

    std::vector<Glib::ustring> parts = splitByWhitespace(value);
    if (parts.empty())
    {
        return false;
    }
    
    for (auto & part : parts) {
        if (!evaluateSingleFeature(part.c_str())) {
            return false;
        }
    }
    
    return true;
}

static bool evaluateRequiredExtensions(SPItem const */*item*/, gchar const *value) {
    if ( nullptr == value )
        return true;
    return false;
}

/*
 * Language codes and names:
aa Afar
ab Abkhazian
af Afrikaans
am Amharic
ar Arabic
as Assamese
ay Aymara
az Azerbaijani

ba Bashkir
be Byelorussian
bg Bulgarian
bh Bihari
bi Bislama
bn Bengali; Bangla
bo Tibetan
br Breton

ca Catalan
co Corsican
cs Czech
cy Welsh

da Danish
de German
dz Bhutani

el Greek
en English
eo Esperanto
es Spanish
et Estonian
eu Basque

fa Persian
fi Finnish
fj Fiji
fo Faroese
fr French
fy Frisian

ga Irish
gd Scots Gaelic
gl Galician
gn Guarani
gu Gujarati

ha Hausa
he Hebrew (formerly iw)
hi Hindi
hr Croatian
hu Hungarian
hy Armenian

ia Interlingua
id Indonesian (formerly in)
ie Interlingue
ik Inupiak
is Icelandic
it Italian
iu Inuktitut

ja Japanese
jw Javanese

ka Georgian
kk Kazakh
kl Greenlandic
km Cambodian
kn Kannada
ko Korean
ks Kashmiri
ku Kurdish
ky Kirghiz

la Latin
ln Lingala
lo Laothian
lt Lithuanian
lv Latvian, Lettish

mg Malagasy
mi Maori
mk Macedonian
ml Malayalam
mn Mongolian
mo Moldavian
mr Marathi
ms Malay
mt Maltese
my Burmese

na Nauru
ne Nepali
nl Dutch
no Norwegian

oc Occitan
om (Afan) Oromo
or Oriya

pa Punjabi
pl Polish
ps Pashto, Pushto
pt Portuguese

qu Quechua

rm Rhaeto-Romance
rn Kirundi
ro Romanian
ru Russian
rw Kinyarwanda

sa Sanskrit
sd Sindhi
sg Sangho
sh Serbo-Croatian
si Sinhalese
sk Slovak
sl Slovenian
sm Samoan
sn Shona
so Somali
sq Albanian
sr Serbian
ss Siswati
st Sesotho
su Sundanese
sv Swedish
sw Swahili

ta Tamil
te Telugu
tg Tajik
th Thai
ti Tigrinya
tk Turkmen
tl Tagalog
tn Setswana
to Tonga
tr Turkish
ts Tsonga
tt Tatar
tw Twi

ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek

vi Vietnamese
vo Volapuk

wo Wolof

xh Xhosa

yi Yiddish (formerly ji)
yo Yoruba

za Zhuang
zh Chinese
zu Zulu
 */


/*
  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 :