summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorJabier Arraiza Cenoz <jabier.arraiza@marker.es>2013-09-20 17:43:57 +0000
committerJabiertxof <jtx@jtx.marker.es>2013-09-20 17:43:57 +0000
commit0a836d1870bb87d5be3e4d900718f903371c8e56 (patch)
tree7d218dc0e9d81e2f7d3eddcefad9640769dc89b3 /src/util
parentCompact of SVG icons whith the help of ~suv and Martin Owens (diff)
parentMerge Google Summer of Code unit improvement. (diff)
downloadinkscape-0a836d1870bb87d5be3e4d900718f903371c8e56.tar.gz
inkscape-0a836d1870bb87d5be3e4d900718f903371c8e56.zip
Update to trunk
(bzr r11950.1.146)
Diffstat (limited to 'src/util')
-rw-r--r--src/util/expression-evaluator.cpp664
-rw-r--r--src/util/expression-evaluator.h112
-rw-r--r--src/util/units.cpp72
-rw-r--r--src/util/units.h24
4 files changed, 425 insertions, 447 deletions
diff --git a/src/util/expression-evaluator.cpp b/src/util/expression-evaluator.cpp
index dc59c67f4..3b7e77c6c 100644
--- a/src/util/expression-evaluator.cpp
+++ b/src/util/expression-evaluator.cpp
@@ -6,6 +6,7 @@
* Copyright (C) 2008 Martin Nordholts <martinn@svn.gnome.org>
* Modified for Inkscape by Johan Engelen
* Copyright (C) 2011 Johan Engelen
+ * Copyright (C) 2013 Matthew Petroff
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -27,6 +28,7 @@
#include "util/expression-evaluator.h"
#include "util/units.h"
+#include <math.h>
#include <string.h>
using Inkscape::Util::unit_table;
@@ -34,495 +36,353 @@ using Inkscape::Util::unit_table;
namespace Inkscape {
namespace Util {
-enum
+EvaluatorQuantity::EvaluatorQuantity(double value, unsigned int dimension) :
+ value(value),
+ dimension(dimension)
{
- GIMP_EEVL_TOKEN_NUM = 30000,
- GIMP_EEVL_TOKEN_IDENTIFIER = 30001,
-
- GIMP_EEVL_TOKEN_ANY = 40000,
-
- GIMP_EEVL_TOKEN_END = 50000
-};
-
-typedef int GimpEevlTokenType;
-
-
-typedef struct
-{
- GimpEevlTokenType type;
-
- union
- {
- gdouble fl;
-
- struct
- {
- const gchar *c;
- gint size;
- };
-
- } value;
-
-} GimpEevlToken;
+}
-typedef struct
+EvaluatorToken::EvaluatorToken()
{
- const gchar *string;
- GimpEevlUnitResolverProc unit_resolver_proc;
- Unit *unit;
-
- GimpEevlToken current_token;
- const gchar *start_of_current_token;
-} GimpEevl;
+ type = 0;
+ value.fl = 0;
+}
-/** Unit Resolver...
- */
-static bool unitresolverproc (const gchar* identifier, GimpEevlQuantity *result, Unit* unit)
+ExpressionEvaluator::ExpressionEvaluator(const char *string, Unit *unit) :
+ string(string),
+ unit(unit)
{
- if (!unit) {
- result->value = 1;
- result->dimension = 1;
- return true;
- }else if (!identifier) {
- result->value = 1;
- result->dimension = unit->isAbsolute() ? 1 : 0;
- return true;
- } else if (unit_table.hasUnit(identifier)) {
- Unit identifier_unit = unit_table.getUnit(identifier);
-
- // Catch the case of zero or negative unit factors (error!)
- if (identifier_unit.factor < 0.0000001) {
- return false;
- }
-
- result->value = unit->factor / identifier_unit.factor;
- result->dimension = identifier_unit.isAbsolute() ? 1 : 0;
- return true;
- } else {
- return false;
- }
+ current_token.type = TOKEN_END;
+
+ // Preload symbol
+ parseNextToken();
}
-static void gimp_eevl_init (GimpEevl *eva,
- const gchar *string,
- GimpEevlUnitResolverProc unit_resolver_proc,
- Unit *unit);
-static GimpEevlQuantity gimp_eevl_complete (GimpEevl *eva);
-static GimpEevlQuantity gimp_eevl_expression (GimpEevl *eva);
-static GimpEevlQuantity gimp_eevl_term (GimpEevl *eva);
-static GimpEevlQuantity gimp_eevl_signed_factor (GimpEevl *eva);
-static GimpEevlQuantity gimp_eevl_factor (GimpEevl *eva);
-static gboolean gimp_eevl_accept (GimpEevl *eva,
- GimpEevlTokenType token_type,
- GimpEevlToken *consumed_token);
-static void gimp_eevl_lex (GimpEevl *eva);
-static void gimp_eevl_lex_accept_count (GimpEevl *eva,
- gint count,
- GimpEevlTokenType token_type);
-static void gimp_eevl_lex_accept_to (GimpEevl *eva,
- gchar *to,
- GimpEevlTokenType token_type);
-static void gimp_eevl_move_past_whitespace (GimpEevl *eva);
-static gboolean gimp_eevl_unit_identifier_start (gunichar c);
-static gboolean gimp_eevl_unit_identifier_continue (gunichar c);
-static gint gimp_eevl_unit_identifier_size (const gchar *s,
- gint start);
-static void gimp_eevl_expect (GimpEevl *eva,
- GimpEevlTokenType token_type,
- GimpEevlToken *value);
-static void gimp_eevl_error (GimpEevl *eva,
- const char *msg);
-
-
/**
* Evaluates the given arithmetic expression, along with an optional dimension
* analysis, and basic unit conversions.
*
- * @param string The NULL-terminated string to be evaluated.
- * @param unit_resolver_proc Unit resolver callback.
- *
* All units conversions factors are relative to some implicit
- * base-unit (which in GIMP is inches). This is also the unit of the
- * returned value.
+ * base-unit. This is also the unit of the returned value.
*
- * Returns: A #GimpEevlQuantity with a value given in the base unit along with
- * the order of the dimension (i.e. if the base unit is inches, a dimension
- * order of two menas in^2).
+ * Returns: An EvaluatorQuantity with a value given in the base unit along with
+ * the order of the dimension (e.g. if the base unit is inches, a dimension
+ * order of two means in^2).
*
* @return Result of evaluation.
* @throws Inkscape::Util::EvaluatorException There was a parse error.
**/
-GimpEevlQuantity
-gimp_eevl_evaluate (const gchar* string, Unit* unit)
+EvaluatorQuantity ExpressionEvaluator::evaluate()
{
- if (! g_utf8_validate (string, -1, NULL)) {
+ if (!g_utf8_validate(string, -1, NULL)) {
throw EvaluatorException("Invalid UTF8 string", NULL);
}
-
- GimpEevl eva;
- gimp_eevl_init (&eva, string, unitresolverproc, unit);
-
- return gimp_eevl_complete(&eva);
-}
-
-static void
-gimp_eevl_init (GimpEevl *eva,
- const gchar *string,
- GimpEevlUnitResolverProc unit_resolver_proc,
- Unit *unit)
-{
- eva->string = string;
- eva->unit_resolver_proc = unit_resolver_proc;
- eva->unit = unit;
-
- eva->current_token.type = GIMP_EEVL_TOKEN_END;
-
- /* Preload symbol... */
- gimp_eevl_lex (eva);
-}
-
-static GimpEevlQuantity
-gimp_eevl_complete (GimpEevl *eva)
-{
- GimpEevlQuantity result = {0, 0};
- GimpEevlQuantity default_unit_factor;
-
- /* Empty expression evaluates to 0 */
- if (gimp_eevl_accept (eva, GIMP_EEVL_TOKEN_END, NULL))
- return result;
-
- result = gimp_eevl_expression (eva);
-
- /* There should be nothing left to parse by now */
- gimp_eevl_expect (eva, GIMP_EEVL_TOKEN_END, 0);
-
- eva->unit_resolver_proc (NULL, &default_unit_factor, eva->unit);
-
- /* Entire expression is dimensionless, apply default unit if
- * applicable
- */
- if (result.dimension == 0 && default_unit_factor.dimension != 0)
- {
- result.value /= default_unit_factor.value;
- result.dimension = default_unit_factor.dimension;
+
+ EvaluatorQuantity result = EvaluatorQuantity();
+ EvaluatorQuantity default_unit_factor;
+
+ // Empty expression evaluates to 0
+ if (acceptToken(TOKEN_END, NULL)) {
+ return result;
+ }
+
+ result = evaluateExpression();
+
+ // There should be nothing left to parse by now
+ isExpected(TOKEN_END, 0);
+
+ resolveUnit(NULL, &default_unit_factor, unit);
+
+ // Entire expression is dimensionless, apply default unit if applicable
+ if ( result.dimension == 0 && default_unit_factor.dimension != 0 ) {
+ result.value /= default_unit_factor.value;
+ result.dimension = default_unit_factor.dimension;
}
- return result;
+ return result;
}
-static GimpEevlQuantity
-gimp_eevl_expression (GimpEevl *eva)
+EvaluatorQuantity ExpressionEvaluator::evaluateExpression()
{
- gboolean subtract;
- GimpEevlQuantity evaluated_terms;
-
- evaluated_terms = gimp_eevl_term (eva);
-
- /* continue evaluating terms, chained with + or -. */
- for (subtract = FALSE;
- gimp_eevl_accept (eva, '+', NULL) ||
- (subtract = gimp_eevl_accept (eva, '-', NULL));
- subtract = FALSE)
+ bool subtract;
+ EvaluatorQuantity evaluated_terms;
+
+ evaluated_terms = evaluateTerm();
+
+ // Continue evaluating terms, chained with + or -.
+ for (subtract = FALSE;
+ acceptToken('+', NULL) || (subtract = acceptToken('-', NULL));
+ subtract = FALSE)
{
- GimpEevlQuantity new_term = gimp_eevl_term (eva);
-
- /* If dimensions missmatch, attempt default unit assignent */
- if (new_term.dimension != evaluated_terms.dimension)
- {
- GimpEevlQuantity default_unit_factor;
-
- eva->unit_resolver_proc (NULL,
- &default_unit_factor,
- eva->unit);
-
- if (new_term.dimension == 0 &&
- evaluated_terms.dimension == default_unit_factor.dimension)
+ EvaluatorQuantity new_term = evaluateTerm();
+
+ // If dimensions mismatch, attempt default unit assignent
+ if ( new_term.dimension != evaluated_terms.dimension ) {
+ EvaluatorQuantity default_unit_factor;
+
+ resolveUnit(NULL, &default_unit_factor, unit);
+
+ if ( new_term.dimension == 0
+ && evaluated_terms.dimension == default_unit_factor.dimension )
{
- new_term.value /= default_unit_factor.value;
- new_term.dimension = default_unit_factor.dimension;
- }
- else if (evaluated_terms.dimension == 0 &&
- new_term.dimension == default_unit_factor.dimension)
+ new_term.value /= default_unit_factor.value;
+ new_term.dimension = default_unit_factor.dimension;
+ } else if ( evaluated_terms.dimension == 0
+ && new_term.dimension == default_unit_factor.dimension )
{
- evaluated_terms.value /= default_unit_factor.value;
- evaluated_terms.dimension = default_unit_factor.dimension;
- }
- else
- {
- gimp_eevl_error (eva, "Dimension missmatch during addition");
+ evaluated_terms.value /= default_unit_factor.value;
+ evaluated_terms.dimension = default_unit_factor.dimension;
+ } else {
+ throwError("Dimension mismatch during addition");
}
}
-
- evaluated_terms.value += (subtract ? -new_term.value : new_term.value);
+
+ evaluated_terms.value += (subtract ? -new_term.value : new_term.value);
}
-
- return evaluated_terms;
+
+ return evaluated_terms;
}
-static GimpEevlQuantity
-gimp_eevl_term (GimpEevl *eva)
+EvaluatorQuantity ExpressionEvaluator::evaluateTerm()
{
- gboolean division;
- GimpEevlQuantity evaluated_signed_factors;
-
- evaluated_signed_factors = gimp_eevl_signed_factor (eva);
-
- for (division = FALSE;
- gimp_eevl_accept (eva, '*', NULL) ||
- (division = gimp_eevl_accept (eva, '/', NULL));
- division = FALSE)
+ bool division;
+ EvaluatorQuantity evaluated_exp_terms = evaluateExpTerm();
+
+ for ( division = false;
+ acceptToken('*', NULL) || (division = acceptToken('/', NULL));
+ division = false )
{
- GimpEevlQuantity new_signed_factor = gimp_eevl_signed_factor (eva);
-
- if (division)
- {
- evaluated_signed_factors.value /= new_signed_factor.value;
- evaluated_signed_factors.dimension -= new_signed_factor.dimension;
-
- }
- else
- {
- evaluated_signed_factors.value *= new_signed_factor.value;
- evaluated_signed_factors.dimension += new_signed_factor.dimension;
+ EvaluatorQuantity new_exp_term = evaluateExpTerm();
+
+ if (division) {
+ evaluated_exp_terms.value /= new_exp_term.value;
+ evaluated_exp_terms.dimension -= new_exp_term.dimension;
+ } else {
+ evaluated_exp_terms.value *= new_exp_term.value;
+ evaluated_exp_terms.dimension += new_exp_term.dimension;
}
}
-
- return evaluated_signed_factors;
-}
-
-static GimpEevlQuantity
-gimp_eevl_signed_factor (GimpEevl *eva)
-{
- GimpEevlQuantity result;
- gboolean negate = FALSE;
-
- if (! gimp_eevl_accept (eva, '+', NULL))
- negate = gimp_eevl_accept (eva, '-', NULL);
-
- result = gimp_eevl_factor (eva);
-
- if (negate) result.value = -result.value;
-
- return result;
+
+ return evaluated_exp_terms;
}
-static GimpEevlQuantity
-gimp_eevl_factor (GimpEevl *eva)
+EvaluatorQuantity ExpressionEvaluator::evaluateExpTerm()
{
- GimpEevlQuantity evaluated_factor = { 0, 0 };
- GimpEevlToken consumed_token;
-
- if (gimp_eevl_accept (eva,
- GIMP_EEVL_TOKEN_NUM,
- &consumed_token))
- {
- evaluated_factor.value = consumed_token.value.fl;
- }
- else if (gimp_eevl_accept (eva, '(', NULL))
- {
- evaluated_factor = gimp_eevl_expression (eva);
- gimp_eevl_expect (eva, ')', 0);
- }
- else
- {
- gimp_eevl_error (eva, "Expected number or '('");
- }
-
- if (eva->current_token.type == GIMP_EEVL_TOKEN_IDENTIFIER)
- {
- gchar *identifier;
- GimpEevlQuantity result;
-
- gimp_eevl_accept (eva,
- GIMP_EEVL_TOKEN_ANY,
- &consumed_token);
-
- identifier = g_newa (gchar, consumed_token.value.size + 1);
-
- strncpy (identifier, consumed_token.value.c, consumed_token.value.size);
- identifier[consumed_token.value.size] = '\0';
-
- if (eva->unit_resolver_proc (identifier,
- &result,
- eva->unit))
- {
- evaluated_factor.value /= result.value;
- evaluated_factor.dimension += result.dimension;
- }
- else
- {
- gimp_eevl_error (eva, "Unit was not resolved");
+ EvaluatorQuantity evaluated_signed_factors = evaluateSignedFactor();
+
+ while(acceptToken('^', NULL)) {
+ EvaluatorQuantity new_signed_factor = evaluateSignedFactor();
+
+ if (new_signed_factor.dimension == 0) {
+ evaluated_signed_factors.value = pow(evaluated_signed_factors.value,
+ new_signed_factor.value);
+ evaluated_signed_factors.dimension *= new_signed_factor.value;
+ } else {
+ throwError("Unit in exponent");
}
}
-
- return evaluated_factor;
+
+ return evaluated_signed_factors;
}
-static gboolean
-gimp_eevl_accept (GimpEevl *eva,
- GimpEevlTokenType token_type,
- GimpEevlToken *consumed_token)
+EvaluatorQuantity ExpressionEvaluator::evaluateSignedFactor()
{
- gboolean existed = FALSE;
-
- if (token_type == eva->current_token.type ||
- token_type == GIMP_EEVL_TOKEN_ANY)
- {
- existed = TRUE;
-
- if (consumed_token)
- *consumed_token = eva->current_token;
-
- /* Parse next token */
- gimp_eevl_lex (eva);
+ EvaluatorQuantity result;
+ bool negate = FALSE;
+
+ if (!acceptToken('+', NULL)) {
+ negate = acceptToken ('-', NULL);
}
-
- return existed;
+
+ result = evaluateFactor();
+
+ if (negate) {
+ result.value = -result.value;
+ }
+
+ return result;
}
-static void
-gimp_eevl_lex (GimpEevl *eva)
+EvaluatorQuantity ExpressionEvaluator::evaluateFactor()
{
- const gchar *s;
-
- gimp_eevl_move_past_whitespace (eva);
- s = eva->string;
- eva->start_of_current_token = s;
-
- if (! s || s[0] == '\0')
- {
- /* We're all done */
- eva->current_token.type = GIMP_EEVL_TOKEN_END;
- }
- else if (s[0] == '+' || s[0] == '-')
- {
- /* Snatch these before the g_strtod() does, othewise they might
- * be used in a numeric conversion.
- */
- gimp_eevl_lex_accept_count (eva, 1, s[0]);
+ EvaluatorQuantity evaluated_factor = EvaluatorQuantity();
+ EvaluatorToken consumed_token = EvaluatorToken();
+
+ if (acceptToken(TOKEN_NUM, &consumed_token)) {
+ evaluated_factor.value = consumed_token.value.fl;
+ } else if (acceptToken('(', NULL)) {
+ evaluated_factor = evaluateExpression();
+ isExpected(')', 0);
+ } else {
+ throwError("Expected number or '('");
}
- else
- {
- /* Attempt to parse a numeric value */
- gchar *endptr = NULL;
- gdouble value = g_strtod (s, &endptr);
-
- if (endptr && endptr != s)
- {
- /* A numeric could be parsed, use it */
- eva->current_token.value.fl = value;
-
- gimp_eevl_lex_accept_to (eva, endptr, GIMP_EEVL_TOKEN_NUM);
+ if ( current_token.type == TOKEN_IDENTIFIER ) {
+ char *identifier;
+ EvaluatorQuantity result;
+
+ acceptToken(TOKEN_ANY, &consumed_token);
+
+ identifier = g_newa(char, consumed_token.value.size + 1);
+
+ strncpy(identifier, consumed_token.value.c, consumed_token.value.size);
+ identifier[consumed_token.value.size] = '\0';
+
+ if (resolveUnit(identifier, &result, unit)) {
+ evaluated_factor.value /= result.value;
+ evaluated_factor.dimension += result.dimension;
+ } else {
+ throwError("Unit was not resolved");
}
- else if (gimp_eevl_unit_identifier_start (s[0]))
- {
- /* Unit identifier */
- eva->current_token.value.c = s;
- eva->current_token.value.size = gimp_eevl_unit_identifier_size (s, 0);
+ }
+
+ return evaluated_factor;
+}
- gimp_eevl_lex_accept_count (eva,
- eva->current_token.value.size,
- GIMP_EEVL_TOKEN_IDENTIFIER);
- }
- else
- {
- /* Everything else is a single character token */
- gimp_eevl_lex_accept_count (eva, 1, s[0]);
+bool ExpressionEvaluator::acceptToken(TokenType token_type,
+ EvaluatorToken *consumed_token)
+{
+ bool existed = FALSE;
+
+ if ( token_type == current_token.type || token_type == TOKEN_ANY ) {
+ existed = TRUE;
+
+ if (consumed_token) {
+ *consumed_token = current_token;
}
+
+ // Parse next token
+ parseNextToken();
}
+
+ return existed;
}
-static void
-gimp_eevl_lex_accept_count (GimpEevl *eva,
- gint count,
- GimpEevlTokenType token_type)
+void ExpressionEvaluator::parseNextToken()
{
- eva->current_token.type = token_type;
- eva->string += count;
+ const char *s;
+
+ movePastWhiteSpace();
+ s = string;
+ start_of_current_token = s;
+
+ if ( !s || s[0] == '\0' ) {
+ // We're all done
+ current_token.type = TOKEN_END;
+ } else if ( s[0] == '+' || s[0] == '-' ) {
+ // Snatch these before the g_strtod() does, othewise they might
+ // be used in a numeric conversion.
+ acceptTokenCount(1, s[0]);
+ } else {
+ // Attempt to parse a numeric value
+ char *endptr = NULL;
+ gdouble value = g_strtod(s, &endptr);
+
+ if ( endptr && endptr != s ) {
+ // A numeric could be parsed, use it
+ current_token.value.fl = value;
+
+ current_token.type = TOKEN_NUM;
+ string = endptr;
+ } else if (isUnitIdentifierStart(s[0])) {
+ // Unit identifier
+ current_token.value.c = s;
+ current_token.value.size = getIdentifierSize(s, 0);
+
+ acceptTokenCount(current_token.value.size, TOKEN_IDENTIFIER);
+ } else {
+ // Everything else is a single character token
+ acceptTokenCount(1, s[0]);
+ }
+ }
}
-static void
-gimp_eevl_lex_accept_to (GimpEevl *eva,
- gchar *to,
- GimpEevlTokenType token_type)
+void ExpressionEvaluator::acceptTokenCount (int count, TokenType token_type)
{
- eva->current_token.type = token_type;
- eva->string = to;
+ current_token.type = token_type;
+ string += count;
}
-static void
-gimp_eevl_move_past_whitespace (GimpEevl *eva)
+void ExpressionEvaluator::isExpected(TokenType token_type,
+ EvaluatorToken *value)
{
- if (! eva->string)
- return;
-
- while (g_ascii_isspace (*eva->string))
- eva->string++;
+ if (!acceptToken(token_type, value)) {
+ throwError("Unexpected token");
+ }
}
-static gboolean
-gimp_eevl_unit_identifier_start (gunichar c)
+void ExpressionEvaluator::movePastWhiteSpace()
{
- return (g_unichar_isalpha (c) ||
- c == (gunichar) '%' ||
- c == (gunichar) '\'');
+ if (!string) {
+ return;
+ }
+
+ while (g_ascii_isspace(*string)) {
+ string++;
+ }
}
-static gboolean
-gimp_eevl_unit_identifier_continue (gunichar c)
+bool ExpressionEvaluator::isUnitIdentifierStart(gunichar c)
{
- return (gimp_eevl_unit_identifier_start (c) ||
- g_unichar_isdigit (c));
+ return (g_unichar_isalpha (c)
+ || c == (gunichar) '%'
+ || c == (gunichar) '\'');
}
/**
- * gimp_eevl_unit_identifier_size:
+ * getIdentifierSize:
* @s:
* @start:
*
* Returns: Size of identifier in bytes (not including NULL
* terminator).
**/
-static gint
-gimp_eevl_unit_identifier_size (const gchar *string,
- gint start_offset)
+int ExpressionEvaluator::getIdentifierSize(const char *string, int start_offset)
{
- const gchar *start = g_utf8_offset_to_pointer (string, start_offset);
- const gchar *s = start;
- gunichar c = g_utf8_get_char (s);
- gint length = 0;
-
- if (gimp_eevl_unit_identifier_start (c))
- {
- s = g_utf8_next_char (s);
- c = g_utf8_get_char (s);
- length++;
-
- while (gimp_eevl_unit_identifier_continue (c))
- {
- s = g_utf8_next_char (s);
- c = g_utf8_get_char (s);
- length++;
+ const char *start = g_utf8_offset_to_pointer(string, start_offset);
+ const char *s = start;
+ gunichar c = g_utf8_get_char(s);
+ int length = 0;
+
+ if (isUnitIdentifierStart(c)) {
+ s = g_utf8_next_char (s);
+ c = g_utf8_get_char (s);
+ length++;
+
+ while ( isUnitIdentifierStart (c) || g_unichar_isdigit (c) ) {
+ s = g_utf8_next_char(s);
+ c = g_utf8_get_char(s);
+ length++;
}
}
-
- return g_utf8_offset_to_pointer (start, length) - start;
+
+ return g_utf8_offset_to_pointer(start, length) - start;
}
-static void
-gimp_eevl_expect (GimpEevl *eva,
- GimpEevlTokenType token_type,
- GimpEevlToken *value)
+bool ExpressionEvaluator::resolveUnit (const char* identifier,
+ EvaluatorQuantity *result,
+ Unit* unit)
{
- if (! gimp_eevl_accept (eva, token_type, value))
- gimp_eevl_error (eva, "Unexpected token");
+ if (!unit) {
+ result->value = 1;
+ result->dimension = 1;
+ return true;
+ }else if (!identifier) {
+ result->value = 1;
+ result->dimension = unit->isAbsolute() ? 1 : 0;
+ return true;
+ } else if (unit_table.hasUnit(identifier)) {
+ Unit identifier_unit = unit_table.getUnit(identifier);
+ result->value = Quantity::convert(1, *unit, identifier_unit);
+ result->dimension = identifier_unit.isAbsolute() ? 1 : 0;
+ return true;
+ } else {
+ return false;
+ }
}
-static void
-gimp_eevl_error (GimpEevl *eva,
- const char *msg)
+void ExpressionEvaluator::throwError(const char *msg)
{
- throw EvaluatorException(msg, eva->start_of_current_token);
+ throw EvaluatorException(msg, start_of_current_token);
}
} // namespace Util
diff --git a/src/util/expression-evaluator.h b/src/util/expression-evaluator.h
index 4b1065268..6412dfea7 100644
--- a/src/util/expression-evaluator.h
+++ b/src/util/expression-evaluator.h
@@ -6,6 +6,7 @@
* Copyright (C) 2008-2009 Martin Nordholts <martinn@svn.gnome.org>
* Modified for Inkscape by Johan Engelen
* Copyright (C) 2011 Johan Engelen
+ * Copyright (C) 2013 Matthew Petroff
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -22,8 +23,8 @@
* <http://www.gnu.org/licenses/>.
*/
-#ifndef SEEN_GIMP_EEVL_H
-#define SEEN_GIMP_EEVL_H
+#ifndef INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H
+#define INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H
#include "util/units.h"
@@ -33,7 +34,7 @@
/**
* @file
- * Introducing eevl eva, the evaluator. A straightforward recursive
+ * Expression evaluator: A straightforward recursive
* descent parser, no fuss, no new dependencies. The lexer is hand
* coded, tedious, not extremely fast but works. It evaluates the
* expression as it goes along, and does not create a parse tree or
@@ -43,8 +44,8 @@
*
* It relies on external unit resolving through a callback and does
* elementary dimensionality constraint check (e.g. "2 mm + 3 px * 4
- * in" is an error, as L + L^2 is a missmatch). It uses g_strtod() for numeric
- * conversions and it's non-destructive in terms of the paramters, and
+ * in" is an error, as L + L^2 is a mismatch). It uses g_strtod() for numeric
+ * conversions and it's non-destructive in terms of the parameters, and
* it's reentrant.
*
* EBNF:
@@ -52,7 +53,9 @@
* expression ::= term { ('+' | '-') term }* |
* <empty string> ;
*
- * term ::= signed factor { ( '*' | '/' ) signed factor }* ;
+ * term ::= exponent { ( '*' | '/' ) exponent }* ;
+ *
+ * exponent ::= signed factor { '^' signed factor }* ;
*
* signed factor ::= ( '+' | '-' )? factor ;
*
@@ -79,37 +82,104 @@ namespace Util {
class Unit;
/**
-* GimpEevlQuantity:
-* @value: In reference units.
-* @dimension: in has a dimension of 1, in^2 has a dimension of 2 etc
-*/
-typedef struct
+ * EvaluatorQuantity:
+ * @param value In reference units.
+ * @param dimension mm has a dimension of 1, mm^2 has a dimension of 2, etc.
+ */
+class EvaluatorQuantity
{
+public:
+ EvaluatorQuantity(double value = 0, unsigned int dimension = 0);
+
double value;
- gint dimension;
-} GimpEevlQuantity;
+ unsigned int dimension;
+};
-typedef bool (* GimpEevlUnitResolverProc) (const gchar *identifier,
- GimpEevlQuantity *result,
- Unit* unit);
+/**
+ * TokenType
+ */
+enum {
+ TOKEN_NUM = 30000,
+ TOKEN_IDENTIFIER = 30001,
+ TOKEN_ANY = 40000,
+ TOKEN_END = 50000
+};
+typedef int TokenType;
-GimpEevlQuantity gimp_eevl_evaluate (const gchar* string, Unit* unit = NULL);
+/**
+ * EvaluatorToken
+ */
+class EvaluatorToken
+{
+public:
+ EvaluatorToken();
+
+ TokenType type;
+
+ union {
+ double fl;
+ struct {
+ const char *c;
+ int size;
+ };
+ } value;
+};
+
+/**
+ * ExpressionEvaluator
+ * @param string NULL terminated input string to evaluate
+ * @param unit Unit output should be in
+ */
+class ExpressionEvaluator
+{
+public:
+ ExpressionEvaluator(const char *string, Unit *unit = NULL);
+
+ EvaluatorQuantity evaluate();
+
+private:
+ const char *string;
+ Unit *unit;
+
+ EvaluatorToken current_token;
+ const char *start_of_current_token;
+
+ EvaluatorQuantity evaluateExpression();
+ EvaluatorQuantity evaluateTerm();
+ EvaluatorQuantity evaluateExpTerm();
+ EvaluatorQuantity evaluateSignedFactor();
+ EvaluatorQuantity evaluateFactor();
+
+ bool acceptToken(TokenType token_type, EvaluatorToken *consumed_token);
+ void parseNextToken();
+ void acceptTokenCount(int count, TokenType token_type);
+ void isExpected(TokenType token_type, EvaluatorToken *value);
+
+ void movePastWhiteSpace();
+
+ static bool isUnitIdentifierStart(gunichar c);
+ static int getIdentifierSize(const char *s, int start);
+
+ static bool resolveUnit(const char *identifier, EvaluatorQuantity *result, Unit *unit);
+
+ void throwError(const char *msg);
+};
/**
* Special exception class for the expression evaluator.
*/
class EvaluatorException : public std::exception {
public:
- EvaluatorException(const char * message, const char *at_position) {
+ EvaluatorException(const char *message, const char *at_position) {
std::ostringstream os;
- const char* token = at_position ? at_position : "<End of input>";
+ const char *token = at_position ? at_position : "<End of input>";
os << "Expression evaluator error: " << message << " at '" << token << "'";
msgstr = os.str();
}
virtual ~EvaluatorException() throw() {} // necessary to destroy the string object!!!
- virtual const char* what() const throw () {
+ virtual const char *what() const throw () {
return msgstr.c_str();
}
protected:
@@ -119,4 +189,4 @@ protected:
}
}
-#endif // SEEN_GIMP_EEVL_H
+#endif // INKSCAPE_UTIL_EXPRESSION_EVALUATOR_H
diff --git a/src/util/units.cpp b/src/util/units.cpp
index 3fdd77916..e5c6f74fb 100644
--- a/src/util/units.cpp
+++ b/src/util/units.cpp
@@ -114,6 +114,7 @@ Unit::Unit(UnitType type,
abbr(abbr),
description(description)
{
+ g_return_if_fail(factor <= 0);
}
void Unit::clear()
@@ -166,25 +167,25 @@ bool operator!= (const Unit &u1, const Unit &u2)
int Unit::svgUnit() const
{
if (!abbr.compare("px"))
- return 1;
+ return SVGLength::PX;
if (!abbr.compare("pt"))
- return 2;
+ return SVGLength::PT;
if (!abbr.compare("pc"))
- return 3;
+ return SVGLength::PC;
if (!abbr.compare("mm"))
- return 4;
+ return SVGLength::MM;
if (!abbr.compare("cm"))
- return 5;
+ return SVGLength::CM;
if (!abbr.compare("in"))
- return 6;
+ return SVGLength::INCH;
if (!abbr.compare("ft"))
- return 7;
+ return SVGLength::FOOT;
if (!abbr.compare("em"))
- return 8;
+ return SVGLength::EM;
if (!abbr.compare("ex"))
- return 9;
+ return SVGLength::EX;
if (!abbr.compare("%"))
- return 10;
+ return SVGLength::PERCENT;
return 0;
}
@@ -220,6 +221,36 @@ Unit UnitTable::getUnit(Glib::ustring const &unit_abbr) const
return Unit();
}
}
+Unit UnitTable::getUnit(SVGLength::Unit const u) const
+{
+ Glib::ustring u_str;
+ switch(u) {
+ case SVGLength::PX:
+ u_str = "px"; break;
+ case SVGLength::PT:
+ u_str = "pt"; break;
+ case SVGLength::PC:
+ u_str = "pc"; break;
+ case SVGLength::MM:
+ u_str = "mm"; break;
+ case SVGLength::CM:
+ u_str = "cm"; break;
+ case SVGLength::INCH:
+ u_str = "in"; break;
+ case SVGLength::FOOT:
+ u_str = "ft"; break;
+ case SVGLength::EM:
+ u_str = "em"; break;
+ case SVGLength::EX:
+ u_str = "ex"; break;
+ case SVGLength::PERCENT:
+ u_str = "%"; break;
+ default:
+ u_str = "";
+ }
+
+ return getUnit(u_str);
+}
Quantity UnitTable::getQuantity(Glib::ustring const& q) const
{
@@ -421,6 +452,27 @@ double Quantity::convert(const double from_dist, const Glib::ustring from, const
return convert(from_dist, unit_table.getUnit(from), unit_table.getUnit(to));
}
+bool operator< (const Quantity &ql, const Quantity &qr)
+{
+ if (ql.unit->type != qr.unit->type) {
+ g_warning("Incompatible units");
+ return false;
+ }
+ return ql.quantity < qr.value(*ql.unit);
+}
+bool operator> (const Quantity &ql, const Quantity &qr)
+{
+ if (ql.unit->type != qr.unit->type) {
+ g_warning("Incompatible units");
+ return false;
+ }
+ return ql.quantity > qr.value(*ql.unit);
+}
+bool operator!= (const Quantity &q1, const Quantity &q2)
+{
+ return (*q1.unit != *q2.unit) || (q1.quantity != q2.quantity);
+}
+
} // namespace Util
} // namespace Inkscape
diff --git a/src/util/units.h b/src/util/units.h
index bb202b96a..7ba6e1e86 100644
--- a/src/util/units.h
+++ b/src/util/units.h
@@ -1,5 +1,6 @@
/*
* Inkscape Units
+ * These classes are used for defining different unit systems.
*
* Authors:
* Matthew Petroff <matthew@mpetroff.net>
@@ -9,25 +10,12 @@
* Released under GNU GPL, read the file 'COPYING' for more information
*/
-/*
-This is a rough draft of a global 'units' thingee, to allow dialogs and
-the ruler to share info about unit systems... Dunno if this is the
-right kind of object though, so we may have to redo this or shift things
-around later when it becomes clearer what we need.
-
-This object is used for defining different unit systems.
-
-This is intended to eventually replace inkscape/helper/units.*.
-
-Need to review the Units support that's in Gtkmm already...
-
-*/
-
#ifndef INKSCAPE_UTIL_UNITS_H
#define INKSCAPE_UTIL_UNITS_H
#include <map>
#include <glibmm/ustring.h>
+#include "svg/svg-length.h"
namespace Inkscape {
namespace Util {
@@ -112,6 +100,11 @@ public:
static double convert(const double from_dist, const Glib::ustring from, const Unit &to);
static double convert(const double from_dist, const Unit &from, const Glib::ustring to);
static double convert(const double from_dist, const Glib::ustring from, const Glib::ustring to);
+
+ /** Comparison operators. */
+ friend bool operator< (const Quantity &ql, const Quantity &qr);
+ friend bool operator> (const Quantity &ql, const Quantity &qr);
+ friend bool operator!= (const Quantity &q1, const Quantity &q2);
};
class UnitTable {
@@ -132,6 +125,9 @@ class UnitTable {
/** Retrieve a given unit based on its string identifier */
Unit getUnit(Glib::ustring const &name) const;
+ /** Retrieve a given unit based on its SVGLength unit */
+ Unit getUnit(SVGLength::Unit const u) const;
+
/** Retrieve a quantity based on its string identifier */
Quantity getQuantity(Glib::ustring const &q) const;