source: trunk/third/librsvg/rsvg-css.c @ 18352

Revision 18352, 5.1 KB checked in by ghudson, 21 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18351, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2   rsvg-css.c: Parse CSS basic data types.
3 
4   Copyright (C) 2000 Eazel, Inc.
5 
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Library General Public License as
8   published by the Free Software Foundation; either version 2 of the
9   License, or (at your option) any later version.
10 
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Library General Public License for more details.
15 
16   You should have received a copy of the GNU Library General Public
17   License along with this program; if not, write to the
18   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   Boston, MA 02111-1307, USA.
20 
21   Author: Raph Levien <raph@artofcode.com>
22*/
23
24#include "config.h"
25#include "rsvg-css.h"
26
27#include <glib/ghash.h>
28#include <glib/gstring.h>
29#include <math.h>
30#include <stdlib.h>
31#include <string.h>
32
33/**
34 * rsvg_css_parse_length: Parse CSS2 length to a pixel value.
35 * @str: Original string.
36 * @fixed: Where to store boolean value of whether length is fixed.
37 *
38 * Parses a CSS2 length into a pixel value.
39 *
40 * Returns: returns the length.
41 **/
42double
43rsvg_css_parse_length (const char *str, gint *fixed)
44{
45  char *p;
46 
47  /*
48   *  The supported CSS length unit specifiers are:
49   *  em, ex, px, pt, pc, cm, mm, in, and percentages.
50   */
51 
52  *fixed = FALSE;
53
54  p = strstr (str, "px");
55  if (p != NULL)
56    {
57      *fixed = TRUE;
58      return atof (str);
59    }
60  p = strstr (str, "in");
61  if (p != NULL)
62    {
63      *fixed = TRUE;
64      /* return svg->pixels_per_inch * atof (str); */
65    }
66  p = strstr (str, "%");
67  if (p != NULL)
68    {
69      return 0.01 * atof (str);
70    }
71  return atof (str);
72}
73
74gboolean
75rsvg_css_param_match (const char *str, const char *param_name)
76{
77  int i;
78
79  for (i = 0; str[i] != '\0' && str[i] != ':'; i++)
80    if (param_name[i] != str[i])
81      return FALSE;
82  return str[i] == ':' && param_name[i] == '\0';
83}
84
85int
86rsvg_css_param_arg_offset (const char *str)
87{
88  int i;
89
90  for (i = 0; str[i] != '\0' && str[i] != ':'; i++);
91  if (str[i] != '\0') i++;
92  for (; str[i] == ' '; i++);
93  return i;
94}
95
96/* Parse a CSS2 color, returning rgb */
97guint32
98rsvg_css_parse_color (const char *str)
99{
100  gint val = 0;
101  static GHashTable *colors = NULL;
102
103  /* todo: better failure detection */
104
105  /*
106   * todo: handle the rgb (r, g, b) and rgb ( r%, g%, b%), syntax
107   * defined in http://www.w3.org/TR/REC-CSS2/syndata.html#color-units
108   */
109#ifdef VERBOSE
110  g_print ("color = %s\n", str);
111#endif
112  if (str[0] == '#')
113    {
114      int i;
115      for (i = 1; str[i]; i++)
116        {
117          int hexval;
118          if (str[i] >= '0' && str[i] <= '9')
119            hexval = str[i] - '0';
120          else if (str[i] >= 'A' && str[i] <= 'F')
121            hexval = str[i] - 'A' + 10;
122          else if (str[i] >= 'a' && str[i] <= 'f')
123            hexval = str[i] - 'a' + 10;
124          else
125            break;
126          val = (val << 4) + hexval;
127        }
128      /* handle #rgb case */
129      if (i == 4)
130        {
131          val = ((val & 0xf00) << 8) |
132            ((val & 0x0f0) << 4) |
133            (val & 0x00f);
134          val |= val << 4;
135        }
136#ifdef VERBOSE
137      printf ("val = %x\n", val);
138#endif
139    }
140  else
141    {
142      GString * string;
143      if (!colors)
144        {
145          colors = g_hash_table_new (g_str_hash, g_str_equal);
146         
147          g_hash_table_insert (colors, "black",    GINT_TO_POINTER (0x000000));
148          g_hash_table_insert (colors, "silver",   GINT_TO_POINTER (0xc0c0c0));
149          g_hash_table_insert (colors, "gray",     GINT_TO_POINTER (0x808080));
150          g_hash_table_insert (colors, "white",    GINT_TO_POINTER (0xFFFFFF));
151          g_hash_table_insert (colors, "maroon",   GINT_TO_POINTER (0x800000));
152          g_hash_table_insert (colors, "red",      GINT_TO_POINTER (0xFF0000));
153          g_hash_table_insert (colors, "purple",   GINT_TO_POINTER (0x800080));
154          g_hash_table_insert (colors, "fuchsia",  GINT_TO_POINTER (0xFF00FF));
155          g_hash_table_insert (colors, "green",    GINT_TO_POINTER (0x008000));
156          g_hash_table_insert (colors, "lime",     GINT_TO_POINTER (0x00FF00));
157          g_hash_table_insert (colors, "olive",    GINT_TO_POINTER (0x808000));
158          g_hash_table_insert (colors, "yellow",   GINT_TO_POINTER (0xFFFF00));
159          g_hash_table_insert (colors, "navy",     GINT_TO_POINTER (0x000080));
160          g_hash_table_insert (colors, "blue",     GINT_TO_POINTER (0x0000FF));
161          g_hash_table_insert (colors, "teal",     GINT_TO_POINTER (0x008080));
162          g_hash_table_insert (colors, "aqua",     GINT_TO_POINTER (0x00FFFF));
163        }
164
165      string = g_string_ascii_down (g_string_new (str));
166
167      /* this will default to black on a failed lookup */
168      val = GPOINTER_TO_INT (g_hash_table_lookup (colors, string->str));
169    }
170
171  return val;
172}
173
174guint
175rsvg_css_parse_opacity (const char *str)
176{
177  char *end_ptr;
178  double opacity;
179
180  opacity = strtod (str, &end_ptr);
181
182  if (end_ptr[0] == '%')
183    opacity *= 0.01;
184
185  return floor (opacity * 255 + 0.5);
186}
187
188double
189rsvg_css_parse_fontsize (const char *str)
190{
191  char *end_ptr;
192  double size;
193
194  /* todo: handle absolute-size and relative-size tags and proper units */
195  size = strtod (str, &end_ptr);
196
197  if (end_ptr[0] == '%')
198    size = (36 * size * 0.01); /* todo: egregious hack */
199
200  return size;
201}
202
Note: See TracBrowser for help on using the repository browser.