rsvg-cond.c 6.16 KB
Newer Older
Hiroyuki Ikezoe's avatar
Hiroyuki Ikezoe committed
1 2
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 sts=4 ts=4 expandtab: */
Dom Lachowicz's avatar
Dom Lachowicz committed
3
/*
Dom Lachowicz's avatar
Dom Lachowicz committed
4
   rsvg-cond.c: Handle SVG conditionals
Dom Lachowicz's avatar
Dom Lachowicz committed
5

Dom Lachowicz's avatar
Dom Lachowicz committed
6
   Copyright (C) 2004-2005 Dom Lachowicz <cinamod@hotmail.com>
Dom Lachowicz's avatar
Dom Lachowicz committed
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

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Dom Lachowicz
*/

#include "config.h"

#include "rsvg.h"
#include "rsvg-private.h"
#include "rsvg-css.h"
#include "rsvg-styles.h"

#include <string.h>
#include <stdlib.h>
#include <locale.h>

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
static const char *implemented_features[] = {
    "http://www.w3.org/TR/SVG11/feature#BasicText",
    "http://www.w3.org/TR/SVG11/feature#BasicFilter",
    "http://www.w3.org/TR/SVG11/feature#BasicGraphicsAttribute",
    "http://www.w3.org/TR/SVG11/feature#BasicPaintAttribute",
    "http://www.w3.org/TR/SVG11/feature#BasicStructure",
    "http://www.w3.org/TR/SVG11/feature#ConditionalProcessing",
    "http://www.w3.org/TR/SVG11/feature#ContainerAttribute",
    "http://www.w3.org/TR/SVG11/feature#Filter",
    "http://www.w3.org/TR/SVG11/feature#Gradient",
    "http://www.w3.org/TR/SVG11/feature#Image",
    "http://www.w3.org/TR/SVG11/feature#Marker",
    "http://www.w3.org/TR/SVG11/feature#Mask",
    "http://www.w3.org/TR/SVG11/feature#OpacityAttribute",
    "http://www.w3.org/TR/SVG11/feature#Pattern",
    "http://www.w3.org/TR/SVG11/feature#Shape",
    "http://www.w3.org/TR/SVG11/feature#Structure",
    "http://www.w3.org/TR/SVG11/feature#Style",
    "http://www.w3.org/TR/SVG11/feature#SVG",
    "http://www.w3.org/TR/SVG11/feature#SVG-static",
    "http://www.w3.org/TR/SVG11/feature#View",
    "org.w3c.svg.static"        /* deprecated SVG 1.0 feature string */
};
static const guint nb_implemented_features = G_N_ELEMENTS (implemented_features);

static const char **implemented_extensions = NULL;
63
static const guint nb_implemented_extensions = 0;
Dom Lachowicz's avatar
Dom Lachowicz committed
64

65 66
static int
rsvg_feature_compare (const void *a, const void *b)
Dom Lachowicz's avatar
Dom Lachowicz committed
67
{
68
    return strcmp ((const char *) a, *(const char **) b);
Dom Lachowicz's avatar
Dom Lachowicz committed
69 70 71
}

/* http://www.w3.org/TR/SVG/struct.html#RequiredFeaturesAttribute */
72 73
static gboolean
rsvg_cond_fulfills_requirement (const char *value, const char **features, guint nb_features)
Dom Lachowicz's avatar
Dom Lachowicz committed
74
{
75 76 77
    guint nb_elems = 0;
    char **elems;
    gboolean permitted = TRUE;
Dom Lachowicz's avatar
Dom Lachowicz committed
78

79
    elems = rsvg_css_parse_list (value, &nb_elems);
Dom Lachowicz's avatar
Dom Lachowicz committed
80

81 82
    if (elems && nb_elems) {
        guint i;
Dom Lachowicz's avatar
Dom Lachowicz committed
83

84 85 86
        for (i = 0; (i < nb_elems) && permitted; i++)
            if (!bsearch (elems[i], features, nb_features, sizeof (char *), rsvg_feature_compare))
                permitted = FALSE;
Dom Lachowicz's avatar
Dom Lachowicz committed
87

88 89 90
        g_strfreev (elems);
    } else
        permitted = FALSE;
Dom Lachowicz's avatar
Dom Lachowicz committed
91

92
    return permitted;
Dom Lachowicz's avatar
Dom Lachowicz committed
93 94
}

95
/* http://www.w3.org/TR/SVG/struct.html#SystemLanguageAttribute */
Dom Lachowicz's avatar
Dom Lachowicz committed
96
static gboolean
97
rsvg_locale_compare (const char *a, const char *b)
Dom Lachowicz's avatar
Dom Lachowicz committed
98
{
99
    const char *hyphen;
100

101 102 103
    /* check for an exact-ish match first */
    if (!g_ascii_strncasecmp (a, b, strlen (b)))
        return TRUE;
104

105 106 107 108
    /* check to see if there's a hyphen */
    hyphen = strstr (b, "-");
    if (!hyphen)
        return FALSE;
109

110 111
    /* compare up to the hyphen */
    return !g_ascii_strncasecmp (a, b, (hyphen - b));
Dom Lachowicz's avatar
Dom Lachowicz committed
112 113 114
}

/* http://www.w3.org/TR/SVG/struct.html#SystemLanguageAttribute */
115 116
static gboolean
rsvg_cond_parse_system_language (const char *value)
Dom Lachowicz's avatar
Dom Lachowicz committed
117
{
118 119 120
    guint nb_elems = 0;
    char **elems;
    gboolean permitted = TRUE;
Dom Lachowicz's avatar
Dom Lachowicz committed
121

122
    elems = rsvg_css_parse_list (value, &nb_elems);
Dom Lachowicz's avatar
Dom Lachowicz committed
123

124 125 126 127 128 129
    if (elems && nb_elems) {
        guint i;
        gchar *locale = NULL;

        /* we're required to be pessimistic until we hit a language we recognize */
        permitted = FALSE;
Dom Lachowicz's avatar
Dom Lachowicz committed
130 131

#if defined(G_OS_WIN32)
132 133 134 135 136 137 138
        if (!locale)
            locale = g_win32_getlocale ();
#endif

        if (!locale)
            locale = g_strdup (g_getenv ("LANG"));

139
#if defined(HAVE_LC_MESSAGES)
140 141
        if (!locale)
            locale = g_strdup (setlocale (LC_MESSAGES, NULL));
Dom Lachowicz's avatar
Dom Lachowicz committed
142 143
#endif

144 145
        if (!locale)
            locale = g_strdup (setlocale (LC_ALL, NULL));
Dom Lachowicz's avatar
Dom Lachowicz committed
146

147 148 149 150
        if (!locale || strcmp (locale, "C") == 0) {
            g_free (locale);
            locale = g_strdup ("en");
        }
Dom Lachowicz's avatar
Dom Lachowicz committed
151

152 153 154 155 156
        if (locale) {
            for (i = 0; (i < nb_elems) && !permitted; i++) {
                if (rsvg_locale_compare (locale, elems[i]))
                    permitted = TRUE;
            }
Dom Lachowicz's avatar
Dom Lachowicz committed
157

158 159
            g_free (locale);
        }
Dom Lachowicz's avatar
Dom Lachowicz committed
160

161 162 163
        g_strfreev (elems);
    } else
        permitted = FALSE;
Dom Lachowicz's avatar
Dom Lachowicz committed
164

165 166
    return permitted;
}
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
/* returns TRUE if this element should be processed according to <switch> semantics
   http://www.w3.org/TR/SVG/struct.html#SwitchElement */
gboolean
rsvg_eval_switch_attributes (RsvgPropertyBag * atts, gboolean * p_has_cond)
{
    gboolean permitted = TRUE;
    gboolean has_cond = FALSE;

    if (atts && rsvg_property_bag_size (atts)) {
        const char *value;

        if ((value = rsvg_property_bag_lookup (atts, "requiredFeatures"))) {
            permitted =
                rsvg_cond_fulfills_requirement (value, implemented_features,
                                                nb_implemented_features);
            has_cond = TRUE;
        }

        if (permitted && (value = rsvg_property_bag_lookup (atts, "requiredExtensions"))) {
            permitted =
                rsvg_cond_fulfills_requirement (value, implemented_extensions,
                                                nb_implemented_extensions);
            has_cond = TRUE;
        }

        if (permitted && (value = rsvg_property_bag_lookup (atts, "systemLanguage"))) {
            permitted = rsvg_cond_parse_system_language (value);
            has_cond = TRUE;
        }
    }

    if (p_has_cond)
        *p_has_cond = has_cond;

    return permitted;
Dom Lachowicz's avatar
Dom Lachowicz committed
203
}