1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2018 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "spv-css-parser.h"
24 #include "libpspp/str.h"
25 #include "output/options.h"
26 #include "output/pivot-table.h"
29 #include "gl/c-ctype.h"
30 #include "gl/xalloc.h"
31 #include "gl/xmemdup0.h"
46 enum css_token_type type;
51 css_skip_spaces (char *p)
57 else if (!strncmp (p, "<!--", 4))
59 else if (!strncmp (p, "-->", 3))
67 css_is_separator (unsigned char c)
69 return c_isspace (c) || strchr ("{}:;", c);
73 css_token_get (char **p_, struct css_token *token)
80 p = css_skip_spaces (p);
85 token->type = T_LCURLY;
90 token->type = T_RCURLY;
95 token->type = T_COLON;
100 token->type = T_SEMICOLON;
107 while (!css_is_separator (*p))
109 token->s = xmemdup0 (start, p - start);
115 css_decode_key_value (const char *key, const char *value,
116 struct font_style *font)
118 if (!strcmp (key, "color"))
120 struct cell_color color;
121 if (parse_color__ (value, &color))
122 font->fg[0] = font->bg[0] = color;
124 else if (!strcmp (key, "font-weight"))
125 font->bold = !strcmp (value, "bold");
126 else if (!strcmp (key, "font-style"))
127 font->italic = !strcmp (value, "italic");
128 else if (!strcmp (key, "font-decoration"))
129 font->underline = !strcmp (value, "underline");
130 else if (!strcmp (key, "font-family"))
132 free (font->typeface);
133 font->typeface = xstrdup (value);
135 else if (!strcmp (key, "font-size"))
136 font->size = atoi (value) * 3 / 4;
143 spv_parse_css_style (char *style, struct font_style *font)
145 *font = (struct font_style) FONT_STYLE_INITIALIZER;
148 struct css_token token = { .s = NULL };
149 css_token_get (&p, &token);
150 while (token.type != T_EOF)
152 if (token.type != T_ID || !strcmp (token.s, "p"))
154 css_token_get (&p, &token);
160 css_token_get (&p, &token);
162 if (token.type == T_COLON)
164 struct string value = DS_EMPTY_INITIALIZER;
167 css_token_get (&p, &token);
168 if (token.type != T_ID)
170 if (!ds_is_empty (&value))
171 ds_put_byte (&value, ' ');
172 ds_put_cstr (&value, token.s);
175 css_decode_key_value (key, ds_cstr (&value), font);