Exemple #1
0
svgint_status_t
_svg_str_parse_csv_doubles (const char *str, double *value, int num_values, const char **end)
{
    int i;
    double val;
    const char *fail_pos = str;
    svg_status_t status = SVG_STATUS_SUCCESS;

    for (i=0; i < num_values; i++) {
	_svg_str_skip_space_or_char (&str, ',');
	if (*str == '\0') {
	    fail_pos = str;
	    status = SVGINT_STATUS_ARGS_EXHAUSTED;
	    break;
	}

	val = _svg_ascii_strtod (str, &fail_pos);
	if (fail_pos == str) {
	    status = SVGINT_STATUS_ARGS_EXHAUSTED;
	    break;
	}
	str = fail_pos;

	value[i] = val;
    }

    if (end)
	*end = fail_pos;
    return status;
}
static svg_status_t
_svg_color_parse_component (const char **str, unsigned int *component)
{
    const char *s, *end;
    double c;

    s = *str;

    c = _svg_ascii_strtod (s, &end);
    if (end == s)
	return SVG_STATUS_PARSE_ERROR;
    s = end;

    _svg_str_skip_space (&s);

    if (*s == '%') {
	c *= 2.55;
	s++;
    }

    _svg_str_skip_space (&s);

    if (c > 255)
	c = 255;
    else if (c < 0)
	c = 0;

    *component = (unsigned int) c;
    *str = s;

    return SVG_STATUS_SUCCESS;
}
static svg_status_t
_svg_style_parse_stroke_miter_limit (svg_style_t *style, const char *str)
{
    const char *end;

    style->stroke_miter_limit = _svg_ascii_strtod (str, &end);
    if (end == (char *)str)
	return SVG_STATUS_PARSE_ERROR;

    style->flags |= SVG_STYLE_FLAG_STROKE_MITER_LIMIT;

    return SVG_STATUS_SUCCESS;
}
static svg_status_t
_svg_style_str_to_opacity (const char *str, double *ret)
{
    const char *end_ptr;
    double opacity;
    
    opacity = _svg_ascii_strtod (str, &end_ptr);

    if (end_ptr == str)
	return SVG_STATUS_PARSE_ERROR;
    
    if (end_ptr && end_ptr[0] == '%')
	opacity *= 0.01;
    
    *ret = opacity;

    return SVG_STATUS_SUCCESS;
}
svg_status_t
_svg_length_init_from_str (svg_length_t *length, const char *str)
{
    double value;
    svg_length_unit_t unit;
    const char *unit_str;

    value = _svg_ascii_strtod (str, &unit_str);

    if (unit_str == str)
	return SVG_STATUS_PARSE_ERROR;

    if (unit_str) {
	if (strcmp (unit_str, "px") == 0)
	    unit = SVG_LENGTH_UNIT_PX;
	else if (strcmp (unit_str, "pt") == 0)
	    unit = SVG_LENGTH_UNIT_PT;
	else if (strcmp (unit_str, "in") == 0)
	    unit = SVG_LENGTH_UNIT_IN;
	else if (strcmp (unit_str, "cm") == 0)
	    unit = SVG_LENGTH_UNIT_CM;
	else if (strcmp (unit_str, "mm") == 0)
	    unit = SVG_LENGTH_UNIT_MM;
	else if (strcmp (unit_str, "pc") == 0)
	    unit = SVG_LENGTH_UNIT_PC;
	else if (strcmp (unit_str, "em") == 0)
	    unit = SVG_LENGTH_UNIT_EM;
	else if (strcmp (unit_str, "ex") == 0)
	    unit = SVG_LENGTH_UNIT_EX;
	else if (strcmp (unit_str, "%") == 0)
	    unit = SVG_LENGTH_UNIT_PCT;
	else {
	    unit = SVG_LENGTH_UNIT_PX;
	}
    } else {
	unit = SVG_LENGTH_UNIT_PX;
    }

    return _svg_length_init_unit (length, value, unit, length->orientation);
}
svgint_status_t
_svg_attribute_get_double (const char	**attributes,
			   const char	*name,
			   double	*value,
			   double	default_value)
{
    int i;

    *value = default_value;

    if (attributes == NULL)
	return SVGINT_STATUS_ATTRIBUTE_NOT_FOUND;

    for (i=0; attributes[i]; i += 2) {
	if (strcmp (attributes[i], name) == 0) {
	    *value = _svg_ascii_strtod (attributes[i+1], NULL);
	    return SVG_STATUS_SUCCESS;
	}
    }

    return SVGINT_STATUS_ATTRIBUTE_NOT_FOUND;
}
static svg_status_t
_svg_style_parse_font_weight (svg_style_t *style, const char *str)
{
    if (strcmp (str, "normal") == 0)
	style->font_weight = 400;
    else if (strcmp (str, "bold") == 0)
	style->font_weight = 700;
    else if (strcmp (str, "lighter") == 0)
	style->font_weight -= 100;
    else if (strcmp (str, "bolder") ==0)
	style->font_weight += 100;
    else
	style->font_weight = _svg_ascii_strtod(str, NULL);

    if (style->font_weight < 100)
	style->font_weight = 100;
    if (style->font_weight > 900)
	style->font_weight = 900;

    style->flags |= SVG_STYLE_FLAG_FONT_WEIGHT;

    return SVG_STATUS_SUCCESS;
}
Exemple #8
0
/* The following parse function is:

   Copyright (C) 2000 Eazel, Inc.
   Copyright (C) 2002 Dom Lachowicz <*****@*****.**>

   Author: Raph Levien <*****@*****.**>

   Parse an SVG transform string into an affine matrix. Reference: SVG
   working draft dated 1999-07-06, section 8.5.
*/
extern svg_status_t
_svg_transform_parse_str (svg_transform_t *transform, const char *str)
{
    intptr_t idx;
    svg_status_t status;
    char keyword[32];
    double args[6];
    int n_args;
    unsigned int key_len;
    svg_transform_t tmp_transform;

    status = _svg_transform_init (transform);
    if (status)
		return status;

    idx = 0;
    while (str[idx]) {
		/* skip initial whitespace */
		while (_svg_ascii_isspace (str[idx]) || str[idx] == ',')
			idx++;

		/* parse keyword */
		for (key_len = 0; key_len < sizeof (keyword); key_len++) {
			char c;

			c = str[idx];
			if (_svg_ascii_isalpha (c) || c == '-')
				keyword[key_len] = str[idx++];
			else
				break;
		}
		/* XXX: This size limitation looks bogus */
		if (key_len >= sizeof (keyword))
			return SVG_STATUS_PARSE_ERROR;
		keyword[key_len] = '\0';

		/* skip whitespace */
		while (_svg_ascii_isspace (str[idx]))
			idx++;

		if (str[idx] != '(')
			return SVG_STATUS_PARSE_ERROR;
		idx++;
		
		for (n_args = 0; ; n_args++) {
			char c;
			const char *end_ptr;

			/* skip whitespace */
			while (_svg_ascii_isspace (str[idx]))
				idx++;
			c = str[idx];
			if (_svg_ascii_isdigit (c) || c == '+' || c == '-' || c == '.') {
				if (n_args == SVG_ARRAY_SIZE (args))
					return SVG_STATUS_PARSE_ERROR;
				args[n_args] = _svg_ascii_strtod (str + idx, &end_ptr);
				idx = end_ptr - str;

				while (_svg_ascii_isspace (str[idx]))
					idx++;

				/* skip optional comma */
				if (str[idx] == ',')
					idx++;
			} else if (c == ')')
				break;
			else
				return SVG_STATUS_PARSE_ERROR;
		}
		idx++;

		/* ok, have parsed keyword and args, now modify the transform */
		if (strcmp (keyword, "matrix") == 0) {
			if (n_args != 6)
				return SVG_STATUS_PARSE_ERROR;
			_svg_transform_init_matrix (&tmp_transform,
										args[0], args[1],
										args[2], args[3],
										args[4], args[5]);
			_svg_transform_multiply_into_right (&tmp_transform, transform);
		} else if (strcmp (keyword, "translate") == 0) {
			if (n_args == 1)
				args[1] = 0;
			else if (n_args != 2)
				return SVG_STATUS_PARSE_ERROR;
			_svg_transform_add_translate (transform, args[0], args[1]);
		} else if (strcmp (keyword, "scale") == 0) {
			if (n_args == 1)
				args[1] = args[0];
			else if (n_args != 2)
				return SVG_STATUS_PARSE_ERROR;
			_svg_transform_add_scale (transform, args[0], args[1]);
		} else if (strcmp (keyword, "rotate") == 0) {
			if (n_args != 1)
				return SVG_STATUS_PARSE_ERROR;
			_svg_transform_add_rotate (transform, args[0]);
		} else if (strcmp (keyword, "skewX") == 0) {
			if (n_args != 1)
				return SVG_STATUS_PARSE_ERROR;
			_svg_transform_add_skew_x (transform, args[0]);
		} else if (strcmp (keyword, "skewY") == 0) {
			if (n_args != 1)
				return SVG_STATUS_PARSE_ERROR;
			_svg_transform_add_skew_y (transform, args[0]);
		} else
			return SVG_STATUS_PARSE_ERROR;
    }

    return SVG_STATUS_SUCCESS;
}