Crate svgtypes [−] [src]
svgtypes is a collection of parsers, containers and writers for SVG types.
Usage is simple as:
use svgtypes::Path; let path: Path = "M10-20A5.5.3-4 110-.1".parse().unwrap(); assert_eq!(path.to_string(), "M 10 -20 A 5.5 0.3 -4 1 1 0 -0.1");
You can also use a low-level, pull-based parser:
use svgtypes::PathParser; let p = PathParser::from("M10-20A5.5.3-4 110-.1"); for token in p { println!("{:?}", token); }
You can also tweak an output format:
use svgtypes::{Path, WriteBuffer, WriteOptions}; let path_str = "M10-20A5.5.3-4 110-.1"; let path: Path = path_str.parse().unwrap(); let opt = WriteOptions { remove_leading_zero: true, use_compact_path_notation: true, join_arc_to_flags: true, .. WriteOptions::default() }; assert_eq!(path.with_write_opt(&opt).to_string(), path_str);
Supported SVG types
| SVG Type | Rust Type | Storage | Parser |
|---|---|---|---|
| <color> | Color | Stack | |
| <number> | f64 | Stack | |
| <length> | Length | Stack | |
| <viewBox> | ViewBox | Stack | |
| <path> | Path | Heap | PathParser |
| <list-of-numbers> | NumberList | Heap | NumberListParser |
| <list-of-lengths> | LengthList | Heap | LengthListParser |
| <transform-list> | Transform | Stack | TransformListParser |
| <list-of-points> | Points | Heap | PointsParser |
| <style> | - | - | StyleParser |
| <paint> | - | - | Paint |
- All types implement from string (
FromStr,FromSpan) and to string traits (Display,WriteBuffer). - The library doesn't store transform list as is. It will premultiplied.
styleandpainttypes can only be parsed.
Benefits
- Complete support of paths, so data like
M10-20A5.5.3-4 110-.1will be parsed correctly. - Every type can be parsed separately, so you can parse just paths or transform or any other SVG value.
- Good error processing. All error types contain position (line:column) where it occurred.
- Access to pull-based parsers.
- Pretty fast.
Limitations
- All keywords must be lowercase. Case-insensitive parsing is supported only for colors (requires allocation for named colors).
- The
<color>followed by the<icccolor>is not supported. As the<icccolor>itself. - CSS styles does not processed. You should use an external CSS parser.
- Comments inside attributes value supported only for the
styleattribute. - System colors, like
fill="AppWorkspace", are not supported. - There is no separate
coordinatetype. It will be parsed as<length>, - There is no separate
opacity-valuetype. It will be parsed as<number>, but will be bound to 0..1 range. - Implicit path commands are not supported. All commands are parsed as explicit.
- Implicit MoveTo commands will be automatically converted into explicit LineTo.
Safety
- The library should not panic. Any panic considered as a critical bug and should be reported.
- The library forbids unsafe code.
Alternatives
None.
Re-exports
pub extern crate xmlparser; |
Structs
| AspectRatio |
Representation of the |
| Color |
Representation of the |
| DisplaySvg |
A wrapper to use |
| ErrorPos |
Position of the error. |
| Length |
Representation of the |
| LengthList |
Representation of the |
| LengthListParser |
A pull-based |
| NumberList |
Representation of the |
| NumberListParser |
A pull-based |
| Path |
Representation of the SVG path data. |
| PathBuilder |
A builder for |
| PathParser |
A pull-based path data parser. |
| Points |
Representation of the |
| PointsParser |
A pull-based |
| StrSpan |
An immutable string slice. |
| Stream |
A streaming text parsing interface. |
| StyleParser |
A pull-based style parser. |
| Transform |
Representation of the |
| TransformListParser |
A pull-based |
| ViewBox |
Representation of the |
| WriteOptions |
Options for SVG types writing. |
Enums
| Align |
Representation of the |
| AttributeId |
List of all SVG attributes. |
| ElementId |
List of all SVG elements. |
| Error |
List of all errors. |
| LengthUnit |
List of all SVG length units. |
| ListSeparator |
A separator type for a list of values. |
| Paint |
Representation of the |
| PaintFallback |
Representation of the fallback part of the |
| PathCommand |
List of all path commands. |
| PathSegment |
Representation of the path segment. |
| StyleToken |
Style token. |
| TransformListToken |
Transform list token. |
Traits
| FromSpan |
A trait for parsing data from a string. |
| FuzzyEq |
A trait for fuzzy/approximate equality comparisons. |
| FuzzyZero |
A trait for fuzzy/approximate comparisons of |
| StreamExt |
|
| WriteBuffer |
A trait for writing data to the buffer. |