From 1efb435864623a0303e3ee9c4acb10630d393b6e Mon Sep 17 00:00:00 2001 From: Martin Bodin Date: Wed, 29 Nov 2023 14:43:11 +0100 Subject: [PATCH] Adding parser for fill-opacity. --- syntax/attribute_value.ml | 25 +++++++++++++++++++++++++ syntax/attribute_value.mli | 6 ++++++ 2 files changed, 31 insertions(+) diff --git a/syntax/attribute_value.ml b/syntax/attribute_value.ml index 4630d6e11..0482eb17b 100644 --- a/syntax/attribute_value.ml +++ b/syntax/attribute_value.ml @@ -484,6 +484,31 @@ let paint ?separated_by:_ ?default:_ loc name s = `Icc ([%e iri], Some [%e paint_without_icc loc name remainder])] end [@metaloc loc] +let fill_opacity = + let bad_form name loc = + Common.error loc "Value of %s must be a number or percentage" name in + + let regexp = Re_str.regexp "\\([-+0-9eE.]+\\)\\(%\\)?" in + + fun ?separated_by:_ ?default:_ loc name s -> + if not @@ does_match regexp s then bad_form name loc; + + begin + let n = + match float_exp loc (Re_str.matched_group 1 s) with + | Some n -> n + | None -> bad_form name loc + in + + let v = + if group_matched 2 s then [%expr [%e n] /. 100.] + else [%expr [%e n]] in + + if v >= 0. && v <= 1. then Some v + else + Common.error loc "Value of %s must be between 0 and 1." name in + end [@metaloc loc] + let fill_rule ?separated_by:_ ?default:_ loc _name s = begin match s with | "nonzero" -> diff --git a/syntax/attribute_value.mli b/syntax/attribute_value.mli index 339afa6bd..6214b1385 100644 --- a/syntax/attribute_value.mli +++ b/syntax/attribute_value.mli @@ -198,6 +198,12 @@ val paint : parser {:{https://www.w3.org/TR/SVG/painting.html#SpecifyingPaint} Specifying paint}. *) +val fill_opacity : parser +(** Parses an SVG fill-opacity value, converting it into a number between 0. and 1. + + @see +*) + val fill_rule : parser (** Parses an SVG fill-rule value.