|
|
Donner 0.8.0-pre
Embeddable browser-grade SVG2 engine
|
Donner provides several standalone APIs for core functionality which can be used in any project.
Each library has minimal dependencies and can be used in external projects. For example, the CSS API may be used to add CSS support to your project using an adapter that satisfies the donner::ElementLike concept.
flowchart TD SVG(SVG) CSS(CSS) Rendering(SVG Rendering) SVGParsing(SVG Parsing) XML(XML Parsing) SVG --> CSS Rendering --> SVG SVGParsing --> SVG SVGParsing --> XML
Callers can if init-statements to check for success:
For example, the donner::ElementLike concept is used to make the CSS library usable without depending on the SVG library, or within a test context with FakeElement.
SVGDocument uses single-threaded access by default for the lowest overhead. Applications that need to inspect or mutate the same document from multiple threads can opt into concurrent DOM mode with SVGDocument::setThreadingMode().
For repeated reads or groups of writes, use the batching APIs: SVGDocument::withReadAccess() and SVGDocument::withWriteAccess(). Batching is more efficient because it keeps one access scope for the whole operation instead of entering document access for each individual DOM call.
See SVG DOM Threading And Lifetime for the full public behavior, including removed-element lifetime and rendering while the DOM is being modified.
To parse an SVG document, use the SVGParser class:
donner::ParseResult contains either the document or an error, which can be checked with hasError() and error():
donner::svg::parser::SVGParser::ParseSVG accepts a string containing SVG data.
Store the resulting donner::svg::SVGDocument to keep the document in-memory, and use it to inspect or modify the document.
For example, to get the donner::svg::SVGPathElement for a "<path>" element:
donner::svg::SVGElement implements a DOM-like API for querying and modifying the SVG document.
The document tree is traversable with firstChild(), lastChild(), nextSibling(), and previousSibling(). The element's tag name can be retrieved with tagName().
Example iterating over children:
Every SVG element has its own DOM type:
To add a child to an element, use one of these methods:
To remove an element from the tree:
Removed elements stay valid while user code holds an SVGElement handle to them. Donner reclaims removed subtrees after public handles and in-progress renders no longer need them. See SVG DOM Threading And Lifetime for details.
To create an element, use the element-specific Create method:
To render an SVG document, use the donner::svg::Renderer class.
Renderer is backend-agnostic and resolves to the active build backend (Skia or tiny-skia).
The output size is determined by donner::svg::SVGDocument, which can either be detected from the file itself or overridden with SVGDocument APIs:
The CSS API is used internally within the SVG library, but can be used standalone.
To parse CSS stylesheets, style strings, or selectors, use the donner::css::CSS wrapper API.
donner::css::CSS::ParseStylesheet can parse a .css file into a Stylesheet, which contains a list of SelectorRule.
Selectors are available within a parsed stylesheet, or can be parsed from a string using donner::css::CSS::ParseSelector(std::string_view).
Use Selector::matches(const ElementLike& target) to see if a selector matches against a specific element. This can be repeatedly applied to all elements of a document to find all matches.