[17276] | 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 |
---|
[18274] | 7 | modify it under the terms of the GNU Library General Public License as |
---|
[17276] | 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 |
---|
[18274] | 14 | Library General Public License for more details. |
---|
[17276] | 15 | |
---|
[18274] | 16 | You should have received a copy of the GNU Library General Public |
---|
[17276] | 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 | |
---|
[18274] | 24 | #include "config.h" |
---|
| 25 | #include "rsvg-css.h" |
---|
[17276] | 26 | |
---|
[18351] | 27 | #include <glib/ghash.h> |
---|
| 28 | #include <glib/gstring.h> |
---|
[18274] | 29 | #include <math.h> |
---|
| 30 | #include <stdlib.h> |
---|
| 31 | #include <string.h> |
---|
[17276] | 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 | **/ |
---|
| 42 | double |
---|
[18351] | 43 | rsvg_css_parse_length (const char *str, gint *fixed) |
---|
[17276] | 44 | { |
---|
[18351] | 45 | char *p; |
---|
[17276] | 46 | |
---|
| 47 | /* |
---|
| 48 | * The supported CSS length unit specifiers are: |
---|
[18351] | 49 | * em, ex, px, pt, pc, cm, mm, in, and percentages. |
---|
[17276] | 50 | */ |
---|
| 51 | |
---|
[18351] | 52 | *fixed = FALSE; |
---|
| 53 | |
---|
| 54 | p = strstr (str, "px"); |
---|
| 55 | if (p != NULL) |
---|
[17276] | 56 | { |
---|
[18351] | 57 | *fixed = TRUE; |
---|
| 58 | return atof (str); |
---|
[17276] | 59 | } |
---|
[18351] | 60 | p = strstr (str, "in"); |
---|
| 61 | if (p != NULL) |
---|
[17276] | 62 | { |
---|
[18351] | 63 | *fixed = TRUE; |
---|
| 64 | /* return svg->pixels_per_inch * atof (str); */ |
---|
[17276] | 65 | } |
---|
[18351] | 66 | p = strstr (str, "%"); |
---|
| 67 | if (p != NULL) |
---|
| 68 | { |
---|
| 69 | return 0.01 * atof (str); |
---|
| 70 | } |
---|
| 71 | return atof (str); |
---|
[17276] | 72 | } |
---|
| 73 | |
---|
| 74 | gboolean |
---|
| 75 | rsvg_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 | |
---|
| 85 | int |
---|
| 86 | rsvg_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 | |
---|
[18351] | 96 | /* Parse a CSS2 color, returning rgb */ |
---|
[17276] | 97 | guint32 |
---|
| 98 | rsvg_css_parse_color (const char *str) |
---|
| 99 | { |
---|
| 100 | gint val = 0; |
---|
[18351] | 101 | static GHashTable *colors = NULL; |
---|
[17276] | 102 | |
---|
[18351] | 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 |
---|
[17276] | 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 | } |
---|
[18351] | 136 | #ifdef VERBOSE |
---|
| 137 | printf ("val = %x\n", val); |
---|
| 138 | #endif |
---|
| 139 | } |
---|
| 140 | else |
---|
[17276] | 141 | { |
---|
[18351] | 142 | GString * string; |
---|
| 143 | if (!colors) |
---|
[17276] | 144 | { |
---|
[18351] | 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)); |
---|
[17276] | 163 | } |
---|
| 164 | |
---|
[18351] | 165 | string = g_string_ascii_down (g_string_new (str)); |
---|
[17276] | 166 | |
---|
[18351] | 167 | /* this will default to black on a failed lookup */ |
---|
| 168 | val = GPOINTER_TO_INT (g_hash_table_lookup (colors, string->str)); |
---|
[17276] | 169 | } |
---|
| 170 | |
---|
| 171 | return val; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | guint |
---|
| 175 | rsvg_css_parse_opacity (const char *str) |
---|
| 176 | { |
---|
| 177 | char *end_ptr; |
---|
| 178 | double opacity; |
---|
| 179 | |
---|
[18351] | 180 | opacity = strtod (str, &end_ptr); |
---|
[17276] | 181 | |
---|
[18351] | 182 | if (end_ptr[0] == '%') |
---|
[17276] | 183 | opacity *= 0.01; |
---|
| 184 | |
---|
[18351] | 185 | return floor (opacity * 255 + 0.5); |
---|
[17276] | 186 | } |
---|
[18351] | 187 | |
---|
| 188 | double |
---|
| 189 | rsvg_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 | |
---|