Skip to content

Commit

Permalink
Adding parser for fill-opacity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mbodin committed Nov 29, 2023
1 parent a496fd1 commit 1efb435
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
25 changes: 25 additions & 0 deletions syntax/attribute_value.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ->
Expand Down
6 changes: 6 additions & 0 deletions syntax/attribute_value.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-opacity>
*)

val fill_rule : parser
(** Parses an SVG fill-rule value.
Expand Down

0 comments on commit 1efb435

Please sign in to comment.