summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Davenport <qball@gmpclient.org>2017-05-10 20:54:16 +0200
committerDave Davenport <qball@gmpclient.org>2017-05-10 20:54:16 +0200
commitaaea2b2c8a5eae79f387e7c6b6cbcb72ff91b4ee (patch)
treecbdd5d90ad12471b8349875432bf4d18eba08a14
parentf5b1e4b3d426acb3fbfb3ba0a0bbc3d63d488680 (diff)
Add hsl and cmyk test, add extra error checking
-rw-r--r--lexer/theme-lexer.l8
-rw-r--r--lexer/theme-parser.y90
-rw-r--r--source/theme.c2
-rw-r--r--test/theme-parser-test.c78
4 files changed, 155 insertions, 23 deletions
diff --git a/lexer/theme-lexer.l b/lexer/theme-lexer.l
index 2365410d..9427bb03 100644
--- a/lexer/theme-lexer.l
+++ b/lexer/theme-lexer.l
@@ -179,8 +179,8 @@ HWB "hwb"
CMYK "cmyk"
HSL "hsl"
-PARENT_LEFT \(
-PARENT_RIGHT \)
+S_PARENT_LEFT \(
+S_PARENT_RIGHT \)
COMMA ,
LS_DASH "dash"
@@ -421,8 +421,8 @@ if ( queue == NULL ){
<PROPERTIES>{HWB} { return T_COL_HWB; }
<PROPERTIES>{CMYK} { return T_COL_CMYK; }
/* Fluff */
-<PROPERTIES>{PARENT_LEFT} { return PARENT_LEFT; }
-<PROPERTIES>{PARENT_RIGHT} { return PARENT_RIGHT;}
+<PROPERTIES>{S_PARENT_LEFT} { return PARENT_LEFT; }
+<PROPERTIES>{S_PARENT_RIGHT} { return PARENT_RIGHT;}
<PROPERTIES>{COMMA} { return COMMA; }
/* Position */
<PROPERTIES>{CENTER} { return T_POS_CENTER; }
diff --git a/lexer/theme-parser.y b/lexer/theme-parser.y
index 63999258..54d546e1 100644
--- a/lexer/theme-parser.y
+++ b/lexer/theme-parser.y
@@ -75,11 +75,22 @@ ThemeWidget *rofi_theme = NULL;
void yyerror(YYLTYPE *yylloc, const char *what, const char* s);
int yylex (YYSTYPE *, YYLTYPE *);
-#define IN_RANGE(index,low,high) ( ( (index) > (low) )? ( ( (index) < (high) )? (index):(high) ) : ( low ) )
+
+static int check_in_range ( double index, double low, double high, YYLTYPE *loc )
+{
+ if ( index > high || index < low ){
+ gchar *str = g_strdup_printf("Value out of range: \n\t\tValue: X = %.2lf;\n\t\tRange: %.2lf <= X <= %.2lf.", index, low, high );
+ yyerror ( loc, loc->filename, str);
+ g_free(str);
+ return FALSE;
+ }
+
+ return TRUE;
+}
static double hue2rgb(double p, double q, double t){
- t += (t<0)?1:0;
- t -= (t>1)?1:0;
+ t += (t<0)?1.0:0.0;
+ t -= (t>1)?1.0:0.0;
if( t < (1/6.0) ) {
return p + (q - p) * 6 * t;
}
@@ -183,6 +194,7 @@ static ThemeColor hsl_to_rgb ( double h, double s, double l )
%type <ival> highlight_style
%type <ival> t_line_style
%type <ival> t_unit
+%type <fval> color_val
%type <wloc> t_position
%type <wloc> t_position_ew
%type <wloc> t_position_sn
@@ -398,22 +410,42 @@ t_line_style
*/
t_color
: T_COL_RGBA PARENT_LEFT T_INT COMMA T_INT COMMA T_INT COMMA T_DOUBLE PARENT_RIGHT {
+ if ( ! check_in_range($3,0,255, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($5,0,255, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($7,0,255, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($9,0,1.00, &(@$)) ) { YYABORT; }
$$.alpha = $9;
$$.red = $3/255.0;
$$.green = $5/255.0;
$$.blue = $7/255.0;
}
+| T_COL_RGBA PARENT_LEFT T_INT COMMA T_INT COMMA T_INT COMMA color_val PERCENT PARENT_RIGHT {
+ if ( ! check_in_range($3,0,255, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($5,0,255, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($7,0,255, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($9,0,100, &(@$)) ) { YYABORT; }
+ $$.alpha = $9/100.0;
+ $$.red = $3/255.0;
+ $$.green = $5/255.0;
+ $$.blue = $7/255.0;
+}
| T_COL_RGB PARENT_LEFT T_INT COMMA T_INT COMMA T_INT PARENT_RIGHT {
+ if ( ! check_in_range($3,0,255, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($5,0,255, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($7,0,255, &(@$)) ) { YYABORT; }
$$.alpha = 1.0;
$$.red = $3/255.0;
$$.green = $5/255.0;
$$.blue = $7/255.0;
}
-| T_COL_HWB PARENT_LEFT T_INT COMMA T_DOUBLE PERCENT COMMA T_DOUBLE PERCENT PARENT_RIGHT {
+| T_COL_HWB PARENT_LEFT T_INT COMMA color_val PERCENT COMMA color_val PERCENT PARENT_RIGHT {
$$.alpha = 1.0;
- double h = IN_RANGE($3,0,360)/360.0;
- double w = IN_RANGE($5,0,100)/100.0;
- double b = IN_RANGE($8,0,100)/100.0;
+ if ( ! check_in_range($3,0,360, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($5,0,100, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($8,0,100, &(@$)) ) { YYABORT; }
+ double h = $3/360.0;
+ double w = $5/100.0;
+ double b = $8/100.0;
$$ = hsl_to_rgb ( h, 1.0, 0.5);
$$.red *= ( 1. - w - b );
$$.red += w;
@@ -422,20 +454,41 @@ t_color
$$.blue *= ( 1. - w - b );
$$.blue += w;
}
-| T_COL_CMYK PARENT_LEFT T_DOUBLE PERCENT COMMA T_DOUBLE PERCENT COMMA T_DOUBLE PERCENT COMMA T_DOUBLE PERCENT PARENT_RIGHT {
+| T_COL_CMYK PARENT_LEFT color_val PERCENT COMMA color_val PERCENT COMMA color_val PERCENT COMMA color_val PERCENT PARENT_RIGHT {
+ $$.alpha = 1.0;
+ if ( ! check_in_range($3, 0,100, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($6, 0,100, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($9, 0,100, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($12,0,100, &(@$)) ) { YYABORT; }
+ double c= $3/100.0;
+ double m= $6/100.0;
+ double y= $9/100.0;
+ double k= $12/100.0;
+ $$.red = (1.0-c)*(1.0-k);
+ $$.green = (1.0-m)*(1.0-k);
+ $$.blue = (1.0-y)*(1.0-k);
+}
+| T_COL_CMYK PARENT_LEFT color_val COMMA color_val COMMA color_val COMMA color_val PARENT_RIGHT {
$$.alpha = 1.0;
- double c= IN_RANGE($3, 0, 100)/100.0;
- double m= IN_RANGE($6, 0, 100)/100.0;
- double y= IN_RANGE($9, 0, 100)/100.0;
- double k= IN_RANGE($12, 0, 100)/100.0;
+ if ( ! check_in_range($3, 0,1.00, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($5, 0,1.00, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($7, 0,1.00, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($9, 0,1.00, &(@$)) ) { YYABORT; }
+ double c= $3;
+ double m= $5;
+ double y= $7;
+ double k= $9;
$$.red = (1.0-c)*(1.0-k);
$$.green = (1.0-m)*(1.0-k);
$$.blue = (1.0-y)*(1.0-k);
}
-| T_COL_HSL PARENT_LEFT T_INT COMMA T_DOUBLE PERCENT COMMA T_DOUBLE PERCENT PARENT_RIGHT {
- gdouble h = IN_RANGE($3, 0, 359);
- gdouble s = IN_RANGE($5, 0, 100);
- gdouble l = IN_RANGE($8, 0, 100);
+| T_COL_HSL PARENT_LEFT T_INT COMMA color_val PERCENT COMMA color_val PERCENT PARENT_RIGHT {
+ if ( ! check_in_range($3, 0,360, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($5, 0,100, &(@$)) ) { YYABORT; }
+ if ( ! check_in_range($8, 0,100, &(@$)) ) { YYABORT; }
+ gdouble h = $3;
+ gdouble s = $5;
+ gdouble l = $8;
$$ = hsl_to_rgb ( h/360.0, s/100.0, l/100.0 );
$$.alpha = 1.0;
}
@@ -444,6 +497,11 @@ t_color
}
;
+color_val
+: T_DOUBLE { $$ = $1; }
+| T_INT { $$ = $1; }
+;
+
pvalue: N_STRING { $$ = $1; }
diff --git a/source/theme.c b/source/theme.c
index 2cf1683b..1902970e 100644
--- a/source/theme.c
+++ b/source/theme.c
@@ -317,7 +317,7 @@ extern FILE* yyin;
*/
void yyerror ( YYLTYPE *yylloc, const char *what, const char* s )
{
- char *what_esc = g_markup_escape_text ( what, -1 );
+ char *what_esc = what?g_markup_escape_text ( what, -1 ):g_strdup("");
GString *str = g_string_new ( "" );
g_string_printf ( str, "<big><b>Error while parsing theme:</b></big> <i>%s</i>\n", what_esc );
g_free ( what_esc );
diff --git a/test/theme-parser-test.c b/test/theme-parser-test.c
index d9eda720..f1f63860 100644
--- a/test/theme-parser-test.c
+++ b/test/theme-parser-test.c
@@ -86,7 +86,7 @@ gboolean error = FALSE;
GString *error_msg = NULL;
void rofi_add_error_message ( GString *msg )
{
- ck_assert_ptr_null ( error_msg );
+ ck_assert_ptr_null ( error_msg );
error_msg = msg;
error = TRUE;
}
@@ -534,6 +534,33 @@ START_TEST ( test_properties_color_rgba )
ck_assert_double_eq ( p->value.color.blue , 1 );
}
END_TEST
+START_TEST ( test_properties_color_rgba_percent )
+{
+ widget wid;
+ wid.name = "blaat";
+ wid.state = NULL;
+ rofi_theme_parse_string ( "* { red: rgba(255,0,0,30%); green: rgba(0,255,0,20%); blue: rgba(0,0,255,70.0%); }");
+ ThemeWidget *twid = rofi_theme_find_widget ( wid.name, wid.state, FALSE );
+ Property *p = rofi_theme_find_property ( twid, P_COLOR, "red", FALSE );
+ ck_assert_ptr_nonnull ( p );
+ ck_assert_double_eq ( p->value.color.alpha , 0.3 );
+ ck_assert_double_eq ( p->value.color.red , 1 );
+ ck_assert_double_eq ( p->value.color.green , 0 );
+ ck_assert_double_eq ( p->value.color.blue , 0 );
+ p = rofi_theme_find_property ( twid, P_COLOR, "green", FALSE );
+ ck_assert_ptr_nonnull ( p );
+ ck_assert_double_eq ( p->value.color.alpha , 0.2 );
+ ck_assert_double_eq ( p->value.color.red , 0 );
+ ck_assert_double_eq ( p->value.color.green , 1 );
+ ck_assert_double_eq ( p->value.color.blue , 0 );
+ p = rofi_theme_find_property ( twid, P_COLOR, "blue", FALSE );
+ ck_assert_ptr_nonnull ( p );
+ ck_assert_double_eq ( p->value.color.alpha , 0.7 );
+ ck_assert_double_eq ( p->value.color.red , 0 );
+ ck_assert_double_eq ( p->value.color.green , 0 );
+ ck_assert_double_eq ( p->value.color.blue , 1 );
+}
+END_TEST
START_TEST ( test_properties_color_argb )
{
widget wid;
@@ -561,6 +588,50 @@ START_TEST ( test_properties_color_argb )
ck_assert_double_eq ( p->value.color.blue , 1 );
}
END_TEST
+START_TEST ( test_properties_color_hsl )
+{
+ widget wid;
+ wid.name = "blaat";
+ wid.state = NULL;
+ rofi_theme_parse_string ( "* { test1: hsl(127,40%,66.66666%); test2: hsl(0, 100%, 50%); }");
+ ThemeWidget *twid = rofi_theme_find_widget ( wid.name, wid.state, FALSE );
+
+ Property *p = rofi_theme_find_property ( twid, P_COLOR, "test1", FALSE );
+ ck_assert_ptr_nonnull ( p );
+ ck_assert_double_eq ( p->value.color.alpha , 1.0 );
+ ck_assert_double_eq_tol ( p->value.color.red , 0x88/255.0 , 0.004);
+ ck_assert_double_eq_tol ( p->value.color.green , 0xcd/255.0, 0.004 );
+ ck_assert_double_eq_tol ( p->value.color.blue , 0x90/255.0 , 0.004);
+ p = rofi_theme_find_property ( twid, P_COLOR, "test2", FALSE );
+ ck_assert_ptr_nonnull ( p );
+ ck_assert_double_eq ( p->value.color.alpha , 1.0 );
+ ck_assert_double_eq_tol ( p->value.color.red , 1 , 0.004);
+ ck_assert_double_eq_tol ( p->value.color.green , 0, 0.004 );
+ ck_assert_double_eq_tol ( p->value.color.blue , 0 , 0.004);
+}
+END_TEST
+START_TEST ( test_properties_color_cmyk )
+{
+ widget wid;
+ wid.name = "blaat";
+ wid.state = NULL;
+ rofi_theme_parse_string ( "* { test1: cmyk ( 41%, 0%, 100%, 0%); test2: cmyk ( 0, 1.0, 1.0, 0);}");
+ ThemeWidget *twid = rofi_theme_find_widget ( wid.name, wid.state, FALSE );
+
+ Property *p = rofi_theme_find_property ( twid, P_COLOR, "test1", FALSE );
+ ck_assert_ptr_nonnull ( p );
+ ck_assert_double_eq ( p->value.color.alpha , 1.0 );
+ ck_assert_double_eq_tol ( p->value.color.red , 0x96/255.0 , 0.004);
+ ck_assert_double_eq_tol ( p->value.color.green , 1.0, 0.004 );
+ ck_assert_double_eq_tol ( p->value.color.blue , 0.0 , 0.004);
+ p = rofi_theme_find_property ( twid, P_COLOR, "test2", FALSE );
+ ck_assert_ptr_nonnull ( p );
+ ck_assert_double_eq ( p->value.color.alpha , 1.0 );
+ ck_assert_double_eq_tol ( p->value.color.red , 1 , 0.004);
+ ck_assert_double_eq_tol ( p->value.color.green , 0, 0.004 );
+ ck_assert_double_eq_tol ( p->value.color.blue , 0 , 0.004);
+}
+END_TEST
START_TEST ( test_properties_padding_2 )
{
widget wid;
@@ -752,7 +823,10 @@ static Suite * theme_parser_suite (void)
tcase_add_test ( tc_prop_color, test_properties_color_h8);
tcase_add_test ( tc_prop_color, test_properties_color_rgb);
tcase_add_test ( tc_prop_color, test_properties_color_rgba);
+ tcase_add_test ( tc_prop_color, test_properties_color_rgba_percent);
tcase_add_test ( tc_prop_color, test_properties_color_argb);
+ tcase_add_test ( tc_prop_color, test_properties_color_hsl);
+ tcase_add_test ( tc_prop_color, test_properties_color_cmyk);
suite_add_tcase(s, tc_prop_color );
}
{
@@ -813,7 +887,7 @@ int main ( int argc, char ** argv )
return EXIT_FAILURE;
}
-
+
Suite *s;
SRunner *sr;