rsvg-css.c 10.5 KB
Newer Older
1 2
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 sts=4 expandtab: */
3 4 5 6 7 8
/* 
   rsvg-css.c: Parse CSS basic data types.
 
   Copyright (C) 2000 Eazel, Inc.
  
   This program is free software; you can redistribute it and/or
9
   modify it under the terms of the GNU Library General Public License as
10 11 12 13 14 15
   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
16
   Library General Public License for more details.
17
  
18
   You should have received a copy of the GNU Library General Public
19 20 21 22
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
23 24
   Authors: Dom Lachowicz <cinamod@hotmail.com> 
   Raph Levien <raph@artofcode.com>
25 26
*/

27
#include "config.h"
28
#include "rsvg-private.h"
29
#include "rsvg-css.h"
30
#include "rsvg-styles.h"
31

32
#include <glib.h>
Dom Lachowicz's avatar
Dom Lachowicz committed
33
#include <stdio.h>
34 35
#include <stdlib.h>
#include <string.h>
36
#ifdef HAVE_STRINGS_H
Dom Lachowicz's avatar
Dom Lachowicz committed
37
#include <strings.h>
38
#endif
39
#include <errno.h>
Dom Lachowicz's avatar
Dom Lachowicz committed
40
#include <math.h>
41

42 43
#include <libxml/parser.h>

44 45
#include <libcroco/libcroco.h>

46
#define POINTS_PER_INCH (72.0)
47 48 49
#define CM_PER_INCH     (2.54)
#define MM_PER_INCH     (25.4)
#define PICA_PER_INCH   (6.0)
50

Dom Lachowicz's avatar
Dom Lachowicz committed
51 52
#define SETINHERIT() G_STMT_START {if (inherit != NULL) *inherit = TRUE;} G_STMT_END
#define UNSETINHERIT() G_STMT_START {if (inherit != NULL) *inherit = FALSE;} G_STMT_END
53

54

55
/* Recursive evaluation of all parent elements regarding absolute font size */
56 57
static double
normalize_font_size (RsvgState * state, RsvgDrawingCtx * ctx)
58 59 60
{
    RsvgState *parent;

61 62 63 64
    switch (state->font_size.unit) {
    case LENGTH_UNIT_PERCENT:
    case LENGTH_UNIT_FONT_EM:
    case LENGTH_UNIT_FONT_EX:
65
        parent = rsvg_state_parent (state);
66 67
        if (parent) {
            double parent_size;
68
            parent_size = normalize_font_size (parent, ctx);
69 70 71 72
            return state->font_size.length * parent_size;
        }
        break;
    default:
73
        return rsvg_length_normalize (&state->font_size, ctx);
74 75 76 77 78 79
        break;
    }

    return 12.;
}

80 81 82 83 84 85
double
rsvg_drawing_ctx_get_normalized_font_size (RsvgDrawingCtx *ctx)
{
    return normalize_font_size (rsvg_current_state (ctx), ctx);
}

86 87 88 89 90 91 92 93 94 95 96
/* Recursive evaluation of all parent elements regarding basline-shift */
double
_rsvg_css_accumulate_baseline_shift (RsvgState * state, RsvgDrawingCtx * ctx)
{
    RsvgState *parent;
    double shift = 0.;

    parent = rsvg_state_parent (state);
    if (parent) {
        if (state->has_baseline_shift) {
            double parent_font_size;
97
            parent_font_size = normalize_font_size (parent, ctx); /* font size from here */
98 99 100 101 102 103 104 105
            shift = parent_font_size * state->baseline_shift;
        }
        shift += _rsvg_css_accumulate_baseline_shift (parent, ctx); /* baseline-shift for parent element */
    }

    return shift;
}

106 107
/* This is defined like this so that we can export the Rust function... just for
 * the benefit of rsvg-convert.c
108
 */
109 110 111
RsvgCssColorSpec rsvg_css_parse_color_ (const char       *str,
                                        AllowInherit      allow_inherit,
                                        AllowCurrentColor allow_current_color)
112
{
113
    return rsvg_css_parse_color (str, allow_inherit, allow_current_color);
114 115
}

116
PangoStyle
117
rsvg_css_parse_font_style (const char *str, gboolean * inherit)
118
{
119 120 121 122 123 124 125
    SETINHERIT ();

    if (str) {
        if (!strcmp (str, "oblique"))
            return PANGO_STYLE_OBLIQUE;
        if (!strcmp (str, "italic"))
            return PANGO_STYLE_ITALIC;
126 127 128
        if (!strcmp (str, "normal"))
            return PANGO_STYLE_NORMAL;
        if (!strcmp (str, "inherit")) {
129 130 131 132 133 134
            UNSETINHERIT ();
            return PANGO_STYLE_NORMAL;
        }
    }
    UNSETINHERIT ();
    return PANGO_STYLE_NORMAL;
135 136 137
}

PangoVariant
138
rsvg_css_parse_font_variant (const char *str, gboolean * inherit)
139
{
140 141 142 143 144 145 146 147 148
    SETINHERIT ();

    if (str) {
        if (!strcmp (str, "small-caps"))
            return PANGO_VARIANT_SMALL_CAPS;
        else if (!strcmp (str, "inherit")) {
            UNSETINHERIT ();
            return PANGO_VARIANT_NORMAL;
        }
149
    }
150 151
    UNSETINHERIT ();
    return PANGO_VARIANT_NORMAL;
152 153 154
}

PangoWeight
155
rsvg_css_parse_font_weight (const char *str, gboolean * inherit)
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
    SETINHERIT ();
    if (str) {
        if (!strcmp (str, "lighter"))
            return PANGO_WEIGHT_LIGHT;
        else if (!strcmp (str, "bold"))
            return PANGO_WEIGHT_BOLD;
        else if (!strcmp (str, "bolder"))
            return PANGO_WEIGHT_ULTRABOLD;
        else if (!strcmp (str, "100"))
            return (PangoWeight) 100;
        else if (!strcmp (str, "200"))
            return (PangoWeight) 200;
        else if (!strcmp (str, "300"))
            return (PangoWeight) 300;
        else if (!strcmp (str, "400"))
            return (PangoWeight) 400;
        else if (!strcmp (str, "500"))
            return (PangoWeight) 500;
        else if (!strcmp (str, "600"))
            return (PangoWeight) 600;
        else if (!strcmp (str, "700"))
            return (PangoWeight) 700;
        else if (!strcmp (str, "800"))
            return (PangoWeight) 800;
        else if (!strcmp (str, "900"))
            return (PangoWeight) 900;
        else if (!strcmp (str, "inherit")) {
            UNSETINHERIT ();
            return PANGO_WEIGHT_NORMAL;
        }
    }

    UNSETINHERIT ();
    return PANGO_WEIGHT_NORMAL;
191 192 193
}

PangoStretch
194
rsvg_css_parse_font_stretch (const char *str, gboolean * inherit)
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
    SETINHERIT ();

    if (str) {
        if (!strcmp (str, "ultra-condensed"))
            return PANGO_STRETCH_ULTRA_CONDENSED;
        else if (!strcmp (str, "extra-condensed"))
            return PANGO_STRETCH_EXTRA_CONDENSED;
        else if (!strcmp (str, "condensed") || !strcmp (str, "narrower"))       /* narrower not quite correct */
            return PANGO_STRETCH_CONDENSED;
        else if (!strcmp (str, "semi-condensed"))
            return PANGO_STRETCH_SEMI_CONDENSED;
        else if (!strcmp (str, "semi-expanded"))
            return PANGO_STRETCH_SEMI_EXPANDED;
        else if (!strcmp (str, "expanded") || !strcmp (str, "wider"))   /* wider not quite correct */
            return PANGO_STRETCH_EXPANDED;
        else if (!strcmp (str, "extra-expanded"))
            return PANGO_STRETCH_EXTRA_EXPANDED;
        else if (!strcmp (str, "ultra-expanded"))
            return PANGO_STRETCH_ULTRA_EXPANDED;
        else if (!strcmp (str, "inherit")) {
            UNSETINHERIT ();
            return PANGO_STRETCH_NORMAL;
        }
    }
    UNSETINHERIT ();
    return PANGO_STRETCH_NORMAL;
222 223 224
}

const char *
225
rsvg_css_parse_font_family (const char *str, gboolean * inherit)
226
{
227 228 229 230 231 232 233 234 235
    SETINHERIT ();

    if (!str)
        return NULL;
    else if (!strcmp (str, "inherit")) {
        UNSETINHERIT ();
        return NULL;
    } else
        return str;
236
}
Dom Lachowicz's avatar
Dom Lachowicz committed
237

Dom Lachowicz's avatar
Dom Lachowicz committed
238
#if !defined(HAVE_STRTOK_R)
Dom Lachowicz's avatar
Dom Lachowicz committed
239 240

static char *
241
strtok_r (char *s, const char *delim, char **last)
Dom Lachowicz's avatar
Dom Lachowicz committed
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
    char *p;

    if (s == NULL)
        s = *last;

    if (s == NULL)
        return NULL;

    while (*s && strchr (delim, *s))
        s++;

    if (*s == '\0') {
        *last = NULL;
        return NULL;
    }

    p = s;
    while (*p && !strchr (delim, *p))
        p++;

    if (*p == '\0')
        *last = NULL;
    else {
        *p = '\0';
        p++;
        *last = p;
    }

    return s;
Dom Lachowicz's avatar
Dom Lachowicz committed
272 273
}

274
#endif                          /* !HAVE_STRTOK_R */
Dom Lachowicz's avatar
Dom Lachowicz committed
275 276

gchar **
277
rsvg_css_parse_list (const char *in_str, guint * out_list_len)
Dom Lachowicz's avatar
Dom Lachowicz committed
278
{
279 280
    char *ptr, *tok;
    char *str;
Dom Lachowicz's avatar
Dom Lachowicz committed
281

282 283 284
    guint n = 0;
    GSList *string_list = NULL;
    gchar **string_array = NULL;
Dom Lachowicz's avatar
Dom Lachowicz committed
285

286
    str = g_strdup (in_str);
Dom Lachowicz's avatar
Dom Lachowicz committed
287
    tok = strtok_r (str, ", \t", &ptr);
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    if (tok != NULL) {
        if (strcmp (tok, " ") != 0) {
            string_list = g_slist_prepend (string_list, g_strdup (tok));
            n++;
        }

        while ((tok = strtok_r (NULL, ", \t", &ptr)) != NULL) {
            if (strcmp (tok, " ") != 0) {
                string_list = g_slist_prepend (string_list, g_strdup (tok));
                n++;
            }
        }
    }
    g_free (str);

    if (out_list_len)
        *out_list_len = n;

    if (string_list) {
        GSList *slist;

309
        string_array = g_new0 (gchar *, n + 1);
310 311 312 313 314 315 316 317 318

        string_array[n--] = NULL;
        for (slist = string_list; slist; slist = slist->next)
            string_array[n--] = (gchar *) slist->data;

        g_slist_free (string_list);
    }

    return string_array;
Dom Lachowicz's avatar
Dom Lachowicz committed
319 320
}

321 322
gboolean
rsvg_css_parse_overflow (const char *str, gboolean * inherit)
323
{
324 325 326 327 328 329 330
    SETINHERIT ();
    if (!strcmp (str, "visible") || !strcmp (str, "auto"))
        return 1;
    if (!strcmp (str, "hidden") || !strcmp (str, "scroll"))
        return 0;
    UNSETINHERIT ();
    return 0;
331
}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
332

333 334 335
static void
rsvg_xml_noerror (void *data, xmlErrorPtr error)
{
336 337
}

338 339 340 341
/* This is quite hacky and not entirely correct, but apparently 
 * libxml2 has NO support for parsing pseudo attributes as defined 
 * by the xml-styleheet spec.
 */
342 343
char **
rsvg_css_parse_xml_attribute_string (const char *attribute_string)
344
{
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    xmlSAXHandler handler;
    xmlParserCtxtPtr parser;
    xmlDocPtr doc;
    xmlNodePtr node;
    xmlAttrPtr attr;
    char *tag;
    GPtrArray *attributes;
    char **retval = NULL;

    tag = g_strdup_printf ("<rsvg-hack %s />\n", attribute_string);

    memset (&handler, 0, sizeof (handler));
    xmlSAX2InitDefaultSAXHandler (&handler, 0);
    handler.serror = rsvg_xml_noerror;
    parser = xmlCreatePushParserCtxt (&handler, NULL, tag, strlen (tag) + 1, NULL);
360 361
    parser->options |= XML_PARSE_NONET;

362
    if (xmlParseDocument (parser) != 0)
363 364 365 366
        goto done;

    if ((doc = parser->myDoc) == NULL ||
        (node = doc->children) == NULL ||
367
        strcmp ((const char *) node->name, "rsvg-hack") != 0 ||
368 369 370 371 372 373 374 375 376 377 378 379 380
        node->next != NULL ||
        node->properties == NULL)
          goto done;

    attributes = g_ptr_array_new ();
    for (attr = node->properties; attr; attr = attr->next) {
        xmlNodePtr content = attr->children;

        g_ptr_array_add (attributes, g_strdup ((char *) attr->name));
        if (content)
          g_ptr_array_add (attributes, g_strdup ((char *) content->content));
        else
          g_ptr_array_add (attributes, g_strdup (""));
381 382
    }

383 384
    g_ptr_array_add (attributes, NULL);
    retval = (char **) g_ptr_array_free (attributes, FALSE);
385

386
  done:
387 388
    if (parser->myDoc)
      xmlFreeDoc (parser->myDoc);
389 390
    xmlFreeParserCtxt (parser);
    g_free (tag);
391

392
    return retval;
393
}