SVG parse_svg_value: update is_percent only if parsing successful

This commit is contained in:
Artem Pavlenko 2021-04-19 18:02:24 +01:00
parent 3031b7cf6a
commit a1cb756fa0

View file

@ -253,10 +253,11 @@ double parse_svg_value(T & parser, char const* str, bool & is_percent)
const char* cur = str; // phrase_parse mutates the first iterator const char* cur = str; // phrase_parse mutates the first iterator
const char* end = str + std::strlen(str); const char* end = str + std::strlen(str);
double font_size = parser.font_sizes_.back(); double font_size = parser.font_sizes_.back();
auto apply_value = [&](auto const& ctx) { val = _attr(ctx); is_percent = false; }; bool is_percent_;
auto apply_value = [&](auto const& ctx) { val = _attr(ctx); is_percent_ = false; };
auto apply_units = [&](auto const& ctx) { val *= _attr(ctx); }; auto apply_units = [&](auto const& ctx) { val *= _attr(ctx); };
auto apply_em = [&](auto const& ctx) { val *= font_size;}; auto apply_em = [&](auto const& ctx) { val *= font_size;};
auto apply_percent = [&](auto const& ctx) { val *= 0.01; is_percent = true; }; auto apply_percent = [&](auto const& ctx) { val *= 0.01; is_percent_ = true; };
if (!x3::phrase_parse(cur, end, if (!x3::phrase_parse(cur, end,
x3::double_[apply_value] x3::double_[apply_value]
@ -267,9 +268,10 @@ double parse_svg_value(T & parser, char const* str, bool & is_percent)
x3::lit('%')[apply_percent]), x3::lit('%')[apply_percent]),
x3::space) || (cur != end)) x3::space) || (cur != end))
{ {
val = 0.0; // restore to default on parsing failure
parser.err_handler().on_error("SVG parse error: failed to parse <number> with value \"" + std::string(str) + "\""); parser.err_handler().on_error("SVG parse error: failed to parse <number> with value \"" + std::string(str) + "\"");
val = 0.0; // re-initialise to default value
} }
is_percent = is_percent_; // update only on success
return val; return val;
} }