rsvg-css.c 5.04 KB
Newer Older
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
/* 
   rsvg-css.c: Parse CSS basic data types.
 
   Copyright (C) 2000 Eazel, Inc.
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU 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
   General Public License for more details.
  
   You should have received a copy of the GNU 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: Raph Levien <raph@artofcode.com>
*/

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

#include <glib.h>

#include "rsvg-css.h"

/**
 * rsvg_css_parse_length: Parse CSS2 length to a pixel value.
 * @str: Original string.
 * @fixed: Where to store boolean value of whether length is fixed.
 *
 * Parses a CSS2 length into a pixel value.
 *
 * Returns: returns the length.
 **/
double
rsvg_css_parse_length (const char *str, gint *fixed)
{
  char *p;
  
  /* 
   *  The supported CSS length unit specifiers are: 
   *  em, ex, px, pt, pc, cm, mm, in, and percentages. 
   */
  
  *fixed = FALSE;

  p = strstr (str, "px");
  if (p != NULL)
    {
      *fixed = TRUE;
      return atof (str);
    }
  p = strstr (str, "in");
  if (p != NULL)
    {
      *fixed = TRUE;
      /* return svg->pixels_per_inch * atof (str); */
    }
  p = strstr (str, "%");
  if (p != NULL)
    {
      return 0.01 * atof (str);
    }
  return atof (str);
}

gboolean
rsvg_css_param_match (const char *str, const char *param_name)
{
  int i;

  for (i = 0; str[i] != '\0' && str[i] != ':'; i++)
    if (param_name[i] != str[i])
      return FALSE;
  return str[i] == ':' && param_name[i] == '\0';
}

int
rsvg_css_param_arg_offset (const char *str)
{
  int i;

  for (i = 0; str[i] != '\0' && str[i] != ':'; i++);
  if (str[i] != '\0') i++;
  for (; str[i] == ' '; i++);
  return i;
}

/* Parse a CSS2 color, returning rgb */
guint32
rsvg_css_parse_color (const char *str)
{
  gint val = 0;
  static GHashTable *colors = NULL;

  /* todo: better failure detection */

  /* 
   * todo: handle the rgb (r, g, b) and rgb ( r%, g%, b%), syntax 
   * defined in http://www.w3.org/TR/REC-CSS2/syndata.html#color-units 
   */
#ifdef VERBOSE
  g_print ("color = %s\n", str);
#endif
  if (str[0] == '#')
    {
      int i;
      for (i = 1; str[i]; i++)
	{
	  int hexval;
	  if (str[i] >= '0' && str[i] <= '9')
	    hexval = str[i] - '0';
	  else if (str[i] >= 'A' && str[i] <= 'F')
	    hexval = str[i] - 'A' + 10;
	  else if (str[i] >= 'a' && str[i] <= 'f')
	    hexval = str[i] - 'a' + 10;
	  else
	    break;
	  val = (val << 4) + hexval;
	}
      /* handle #rgb case */
      if (i == 4)
	{
	  val = ((val & 0xf00) << 8) |
	    ((val & 0x0f0) << 4) |
	    (val & 0x00f);
	  val |= val << 4;
	}
#ifdef VERBOSE
      printf ("val = %x\n", val);
#endif
    } 
  else
    {
      GString * string;
      if (!colors)
	{
	  colors = g_hash_table_new (g_str_hash, g_str_equal);
	  
	  g_hash_table_insert (colors, "black",    GINT_TO_POINTER (0x000000));
	  g_hash_table_insert (colors, "silver",   GINT_TO_POINTER (0xc0c0c0));
	  g_hash_table_insert (colors, "gray",     GINT_TO_POINTER (0x808080));
	  g_hash_table_insert (colors, "white",    GINT_TO_POINTER (0xFFFFFF));
	  g_hash_table_insert (colors, "maroon",   GINT_TO_POINTER (0x800000));
	  g_hash_table_insert (colors, "red",      GINT_TO_POINTER (0xFF0000));
	  g_hash_table_insert (colors, "purple",   GINT_TO_POINTER (0x800080));
	  g_hash_table_insert (colors, "fuchsia",  GINT_TO_POINTER (0xFF00FF));
	  g_hash_table_insert (colors, "green",    GINT_TO_POINTER (0x008000));
	  g_hash_table_insert (colors, "lime",     GINT_TO_POINTER (0x00FF00));
	  g_hash_table_insert (colors, "olive",    GINT_TO_POINTER (0x808000));
	  g_hash_table_insert (colors, "yellow",   GINT_TO_POINTER (0xFFFF00));
	  g_hash_table_insert (colors, "navy",     GINT_TO_POINTER (0x000080));
	  g_hash_table_insert (colors, "blue",     GINT_TO_POINTER (0x0000FF));
	  g_hash_table_insert (colors, "teal",     GINT_TO_POINTER (0x008080));
	  g_hash_table_insert (colors, "aqua",     GINT_TO_POINTER (0x00FFFF));
	}

164
      string = g_string_ascii_down (g_string_new (str));
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

      /* this will default to black on a failed lookup */
      val = GPOINTER_TO_INT (g_hash_table_lookup (colors, string->str)); 
    }

  return val;
}

guint
rsvg_css_parse_opacity (const char *str)
{
  char *end_ptr;
  double opacity;

  opacity = strtod (str, &end_ptr);

  if (end_ptr[0] == '%')
    opacity *= 0.01;

  return floor (opacity * 255 + 0.5);
}

double
rsvg_css_parse_fontsize (const char *str)
{
  char *end_ptr;
  double size;

  /* todo: handle absolute-size and relative-size tags and proper units */
  size = strtod (str, &end_ptr);

  if (end_ptr[0] == '%')
    size = (36 * size * 0.01); /* todo: egregious hack */

  return size;
}