2016-02-12 05:56:14 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright © 2016 Benjamin Otte <otte@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gtkcsscalcvalueprivate.h"
|
|
|
|
|
2016-02-12 06:59:06 +00:00
|
|
|
#include <string.h>
|
2016-02-12 05:56:14 +00:00
|
|
|
|
2016-02-13 03:12:45 +00:00
|
|
|
GtkCssValue * gtk_css_calc_value_parse_sum (GtkCssParser *parser,
|
|
|
|
GtkCssNumberParseFlags flags);
|
|
|
|
|
2016-10-15 20:29:45 +00:00
|
|
|
static GtkCssValue *
|
2016-02-13 02:56:12 +00:00
|
|
|
gtk_css_calc_value_parse_value (GtkCssParser *parser,
|
|
|
|
GtkCssNumberParseFlags flags)
|
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
if (gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_OPEN_PARENS))
|
2016-02-13 03:12:45 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
GtkCssValue *result;
|
|
|
|
|
|
|
|
gtk_css_parser_start_block (parser);
|
2016-02-13 03:12:45 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
result = gtk_css_calc_value_parse_sum (parser, flags);
|
|
|
|
if (result == NULL)
|
2016-02-13 03:12:45 +00:00
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
gtk_css_parser_end_block (parser);
|
2016-02-13 03:12:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (!gtk_css_parser_has_token (parser, GTK_CSS_TOKEN_EOF))
|
2019-04-11 02:31:36 +00:00
|
|
|
{
|
|
|
|
GtkCssLocation start = *gtk_css_parser_get_start_location (parser);
|
|
|
|
gtk_css_parser_skip_until (parser, GTK_CSS_TOKEN_EOF);
|
|
|
|
gtk_css_parser_error (parser,
|
|
|
|
GTK_CSS_PARSER_ERROR_SYNTAX,
|
|
|
|
&start,
|
|
|
|
gtk_css_parser_get_start_location (parser),
|
|
|
|
"Expected closing ')' in calc() subterm");
|
|
|
|
gtk_css_value_unref (result);
|
|
|
|
gtk_css_parser_end_block (parser);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-04-09 02:47:00 +00:00
|
|
|
|
|
|
|
gtk_css_parser_end_block (parser);
|
|
|
|
|
2016-02-13 03:12:45 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-02-13 02:56:12 +00:00
|
|
|
return _gtk_css_number_value_parse (parser, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
is_number (GtkCssValue *value)
|
|
|
|
{
|
|
|
|
return gtk_css_number_value_get_dimension (value) == GTK_CSS_DIMENSION_NUMBER
|
|
|
|
&& !gtk_css_number_value_has_percent (value);
|
|
|
|
}
|
|
|
|
|
2016-10-15 20:29:45 +00:00
|
|
|
static GtkCssValue *
|
2016-02-13 02:56:12 +00:00
|
|
|
gtk_css_calc_value_parse_product (GtkCssParser *parser,
|
|
|
|
GtkCssNumberParseFlags flags)
|
|
|
|
{
|
|
|
|
GtkCssValue *result, *value, *temp;
|
|
|
|
GtkCssNumberParseFlags actual_flags;
|
2019-04-11 02:31:36 +00:00
|
|
|
GtkCssLocation start;
|
2016-02-13 02:56:12 +00:00
|
|
|
|
|
|
|
actual_flags = flags | GTK_CSS_PARSE_NUMBER;
|
2019-04-11 02:31:36 +00:00
|
|
|
gtk_css_parser_get_token (parser);
|
|
|
|
start = *gtk_css_parser_get_start_location (parser);
|
2016-02-13 02:56:12 +00:00
|
|
|
result = gtk_css_calc_value_parse_value (parser, actual_flags);
|
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-03-24 02:56:31 +00:00
|
|
|
while (TRUE)
|
2016-02-13 02:56:12 +00:00
|
|
|
{
|
|
|
|
if (actual_flags != GTK_CSS_PARSE_NUMBER && !is_number (result))
|
|
|
|
actual_flags = GTK_CSS_PARSE_NUMBER;
|
|
|
|
|
2019-04-05 00:57:14 +00:00
|
|
|
if (gtk_css_parser_try_delim (parser, '*'))
|
2016-02-13 02:56:12 +00:00
|
|
|
{
|
|
|
|
value = gtk_css_calc_value_parse_product (parser, actual_flags);
|
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
|
|
|
if (is_number (value))
|
|
|
|
temp = gtk_css_number_value_multiply (result, _gtk_css_number_value_get (value, 100));
|
|
|
|
else
|
|
|
|
temp = gtk_css_number_value_multiply (value, _gtk_css_number_value_get (result, 100));
|
|
|
|
_gtk_css_value_unref (value);
|
|
|
|
_gtk_css_value_unref (result);
|
|
|
|
result = temp;
|
|
|
|
}
|
2019-04-05 00:57:14 +00:00
|
|
|
else if (gtk_css_parser_try_delim (parser, '/'))
|
2016-02-13 02:56:12 +00:00
|
|
|
{
|
|
|
|
value = gtk_css_calc_value_parse_product (parser, GTK_CSS_PARSE_NUMBER);
|
|
|
|
if (value == NULL)
|
|
|
|
goto fail;
|
|
|
|
temp = gtk_css_number_value_multiply (result, 1.0 / _gtk_css_number_value_get (value, 100));
|
|
|
|
_gtk_css_value_unref (value);
|
|
|
|
_gtk_css_value_unref (result);
|
|
|
|
result = temp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-24 02:56:31 +00:00
|
|
|
break;
|
2016-02-13 02:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_number (result) && !(flags & GTK_CSS_PARSE_NUMBER))
|
|
|
|
{
|
2019-04-11 02:31:36 +00:00
|
|
|
gtk_css_parser_error (parser,
|
|
|
|
GTK_CSS_PARSER_ERROR_SYNTAX,
|
|
|
|
&start,
|
|
|
|
gtk_css_parser_get_start_location (parser),
|
|
|
|
"calc() product term has no units");
|
2016-02-13 02:56:12 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
_gtk_css_value_unref (result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-12 06:59:06 +00:00
|
|
|
GtkCssValue *
|
|
|
|
gtk_css_calc_value_parse_sum (GtkCssParser *parser,
|
|
|
|
GtkCssNumberParseFlags flags)
|
|
|
|
{
|
|
|
|
GtkCssValue *result;
|
|
|
|
|
2016-02-13 02:56:12 +00:00
|
|
|
result = gtk_css_calc_value_parse_product (parser, flags);
|
2016-02-12 06:59:06 +00:00
|
|
|
if (result == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-03-24 02:56:31 +00:00
|
|
|
while (TRUE)
|
2016-02-12 06:59:06 +00:00
|
|
|
{
|
|
|
|
GtkCssValue *next, *temp;
|
|
|
|
|
2019-04-05 00:57:14 +00:00
|
|
|
if (gtk_css_parser_try_delim (parser, '+'))
|
2016-02-12 06:59:06 +00:00
|
|
|
{
|
2016-02-13 02:56:12 +00:00
|
|
|
next = gtk_css_calc_value_parse_product (parser, flags);
|
|
|
|
if (next == NULL)
|
|
|
|
goto fail;
|
2016-02-12 06:59:06 +00:00
|
|
|
}
|
2019-04-05 00:57:14 +00:00
|
|
|
else if (gtk_css_parser_try_delim (parser, '-'))
|
2016-02-12 06:59:06 +00:00
|
|
|
{
|
2016-02-13 02:56:12 +00:00
|
|
|
temp = gtk_css_calc_value_parse_product (parser, flags);
|
|
|
|
if (temp == NULL)
|
|
|
|
goto fail;
|
2016-02-12 06:59:06 +00:00
|
|
|
next = gtk_css_number_value_multiply (temp, -1);
|
|
|
|
_gtk_css_value_unref (temp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-24 02:56:31 +00:00
|
|
|
break;
|
2016-02-12 06:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
temp = gtk_css_number_value_add (result, next);
|
|
|
|
_gtk_css_value_unref (result);
|
|
|
|
_gtk_css_value_unref (next);
|
|
|
|
result = temp;
|
|
|
|
}
|
2016-02-12 05:56:14 +00:00
|
|
|
|
|
|
|
return result;
|
2016-02-13 02:56:12 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
_gtk_css_value_unref (result);
|
|
|
|
return NULL;
|
2016-02-12 05:56:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GtkCssNumberParseFlags flags;
|
|
|
|
GtkCssValue *value;
|
|
|
|
} ParseCalcData;
|
|
|
|
|
|
|
|
static guint
|
|
|
|
gtk_css_calc_value_parse_arg (GtkCssParser *parser,
|
|
|
|
guint arg,
|
|
|
|
gpointer data_)
|
|
|
|
{
|
|
|
|
ParseCalcData *data = data_;
|
|
|
|
|
|
|
|
data->value = gtk_css_calc_value_parse_sum (parser, data->flags);
|
|
|
|
if (data->value == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-12 05:56:14 +00:00
|
|
|
GtkCssValue *
|
|
|
|
gtk_css_calc_value_parse (GtkCssParser *parser,
|
|
|
|
GtkCssNumberParseFlags flags)
|
|
|
|
{
|
2019-04-09 02:47:00 +00:00
|
|
|
ParseCalcData data;
|
2016-02-12 05:56:14 +00:00
|
|
|
|
2016-02-13 02:56:12 +00:00
|
|
|
/* This can only be handled at compute time, we allow '-' after all */
|
2019-04-09 02:47:00 +00:00
|
|
|
data.flags = flags & ~GTK_CSS_POSITIVE_ONLY;
|
|
|
|
data.value = NULL;
|
2016-02-13 02:56:12 +00:00
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (!gtk_css_parser_has_function (parser, "calc"))
|
2016-02-12 05:56:14 +00:00
|
|
|
{
|
2019-04-09 03:40:50 +00:00
|
|
|
gtk_css_parser_error_syntax (parser, "Expected 'calc('");
|
2016-02-12 05:56:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
if (!gtk_css_parser_consume_function (parser, 1, 1, gtk_css_calc_value_parse_arg, &data))
|
2016-02-12 05:56:14 +00:00
|
|
|
return NULL;
|
|
|
|
|
2019-04-09 02:47:00 +00:00
|
|
|
return data.value;
|
2016-02-12 05:56:14 +00:00
|
|
|
}
|
|
|
|
|