-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update svg backend code to write all data directly into output buffer #634
update svg backend code to write all data directly into output buffer #634
Conversation
b1a86de
to
627a18d
Compare
plotters-svg/src/svg.rs
Outdated
value.chars().for_each(|c| match c { | ||
'<' => buf.push_str("<"), | ||
'>' => buf.push_str(">"), | ||
'&' => buf.push_str("&"), | ||
'"' => buf.push_str("""), | ||
'\'' => buf.push_str("'"), | ||
other => buf.push(other), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is duplicated code, you could reuse the implementation of FormatEscaped
for char
plotters-svg/src/svg.rs
Outdated
attrwriter | ||
.write_key("opacity") | ||
.write_value(make_svg_color(color)); | ||
attrwriter.write_key("fill").write_value(color.alpha); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think opacity and color are accidentally mixed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good overall, but there are a couple of new clippy warnings, code duplication and correctness issues. Once those are addressed, I'll do another review.
Good catch. Thanks. Code updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just found one more thing.
Also, would you mind running a couple of tests to verify that the generated SVG does work as before, at least for the examples? With changes like this it's easy to overlook something and I don't want to deal with avoidable regressions.
plotters-svg/src/svg.rs
Outdated
.write_key("points") | ||
.write_value(FormatEscapedIter(path.into_iter())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can't be correct given that 446 does it differently. Plus in line 446 there might be a whitespace missing at the end of each point.
You were right about the missing spaces between line points. I've just pushed a fix for that. This time I compared the files generated by the tests byte-for-byte against those generated by
returns no errors. Also, as a sanity check, I manually inspected each line side-by-side and confirmed visually that the outputs are equal. Thanks for your careful review. Next time I will be more thorough to ensure behavior is unchanged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution and for checking against the master branch!
This update will restructure the SVGBackend to write all tag attribute values such as for
width
,dx
,viewBox
etc directly into the backend's string buffer, rather than creating temporary strings using theformat!
macro.The existing workflow for opening a tag includes providing a tag type, a list of attrs, and whether the tag is self-closing or not. Because the attrs are provided in a list, all list items' values must have a common type of
&str
. As such, attrs which exist in the code in the form of integers or any other non-string type must first be written to a string before being included in the attrs list.To avoid the cost of allocating many temporary strings, we instead use an
AttrWriter
. WhenSVGBackend::open_tag
is called, you are given anAttrWriter
, which includes methods for writing a key followed by a value. Typestate is used to ensure that a value is only written if there is an associated key. TheAttrWriter::write_value
method is generic over its parameter so that each type can decide how it is written to the buffer: string-like types utilize the existing html-escaping functionality, while number-like types are written directly into the buffer with their Display impl. When the user has finished writing all attrs to the tag, they will then callAttrWriter::close
to finish a self-closing element with/>
, orAttrWriter::finish_without_closing
to finish the opening tag with>
and then push the tag onto the tag_stack so that it can be closed in the future. Thus, it preserves existing behavior.