Struct roarsvg::LyonWriter
source · pub struct LyonWriter<T> { /* private fields */ }
Expand description
Translate from lyon_path::Path
to [usvg::Path
] on push
and write an SVG to a file.
Example
use roarsvg::{Color, LyonWriter, SvgTransform, fill, stroke};
use lyon_path::Path;
use lyon_path::geom::euclid::Point2D;
let file_path = "a.svg";
let mut writer = LyonWriter::new();
// let's create some path with lyon as an example
let mut path_builder = Path::builder();
path_builder.begin(Point2D::origin());
path_builder.line_to(Point2D::new(1.0, 1.0));
path_builder.quadratic_bezier_to(Point2D::new(2.0, 1.0), Point2D::new(3.0, 2.0));
path_builder.cubic_bezier_to(
Point2D::new(2.0, 1.0),
Point2D::new(5.0, 1.0),
Point2D::new(3.0, 2.0),
);
path_builder.end(true);
let path = path_builder.build();
// push the created path with some fill and stroke, in the origin
writer
.push(
&path,
Some(fill(Color::new_rgb(253, 77, 44), 0.8)),
Some(stroke(Color::new_rgb(253, 77, 44), 0.8, 2.0)),
Some(SvgTransform::from_translate(0.0, 0.0)),
)
.expect("Path 1 should be writable!");
let mut path_builder = Path::builder();
// finally, write the SVG
writer.write(file_path).expect("Writing should not panic!");
Implementations§
source§impl<T> LyonWriter<T>
impl<T> LyonWriter<T>
sourcepub fn push(
&mut self,
path: &Path,
fill: Option<Fill>,
stroke: Option<Stroke>,
transform: Option<SvgTransform>
) -> Result<(), LyonTranslationError>
pub fn push( &mut self, path: &Path, fill: Option<Fill>, stroke: Option<Stroke>, transform: Option<SvgTransform> ) -> Result<(), LyonTranslationError>
Add a Path
to the writer and translate it (eager).
sourcepub fn push_node(&mut self, node: NodeKind)
pub fn push_node(&mut self, node: NodeKind)
Push a node kind without any indirection.
For writing Text, call first Self::add_fonts
and call push_text
instead.
sourcepub fn push_png(
&mut self,
data: &[u8],
transform: SvgTransform,
width: f32,
height: f32
) -> Result<(), LyonTranslationError>
pub fn push_png( &mut self, data: &[u8], transform: SvgTransform, width: f32, height: f32 ) -> Result<(), LyonTranslationError>
Push a raster image (formatted by the caller) as a PNG.
sourcepub fn push_group(
&mut self,
nodes: Vec<NodeKind>,
transform: SvgTransform
) -> Result<(), LyonTranslationError>
pub fn push_group( &mut self, nodes: Vec<NodeKind>, transform: SvgTransform ) -> Result<(), LyonTranslationError>
Push a vector of nodes as the children of their own group (formatted by the caller).
This is relevant for applying transforms to a set of elements.
sourcepub fn with_transform(self, trans: SvgTransform) -> Self
pub fn with_transform(self, trans: SvgTransform) -> Self
Add/replace a SvgTransform
, which will be applied to the whole SVG as a group.
sourcepub fn add_fonts<Fp: FontProvider>(self, fonts: Fp) -> LyonWriter<Option<Fp>>
pub fn add_fonts<Fp: FontProvider>(self, fonts: Fp) -> LyonWriter<Option<Fp>>
Loads fonts from a font database, enabling writing [Text
] (push_text
).
sourcepub fn add_fonts_dir<P: AsRef<Path>>(
self,
font_dir: P
) -> LyonWriter<Option<Database>>
pub fn add_fonts_dir<P: AsRef<Path>>( self, font_dir: P ) -> LyonWriter<Option<Database>>
Loads fonts from a font directory, building a FontProvider
and enabling writing text.
source§impl LyonWriter<NoText>
impl LyonWriter<NoText>
pub fn new() -> LyonWriter<NoText>
sourcepub fn write<P: AsRef<Path>>(
self,
file_path: P
) -> Result<(), LyonTranslationError>
pub fn write<P: AsRef<Path>>( self, file_path: P ) -> Result<(), LyonTranslationError>
Write the contained Path
s to an SVG at file_path
. Text will NOT be written!
sourcepub fn add_fonts_source(
self,
font_source: &[u8]
) -> LyonWriter<Option<Database>>
pub fn add_fonts_source( self, font_source: &[u8] ) -> LyonWriter<Option<Database>>
Loads fonts from a font file, building a FontProvider
and enabling writing text.
source§impl<T: FontProvider> LyonWriter<Option<T>>
impl<T: FontProvider> LyonWriter<Option<T>>
Implemented for Option<T>
to be able to ergonomically take it without cloning.
sourcepub fn push_text(
&mut self,
text: String,
font_families: Vec<String>,
font_size: f32,
transform: SvgTransform,
fill: Option<Fill>,
stroke: Option<Stroke>
) -> Result<(), LyonTranslationError>
pub fn push_text( &mut self, text: String, font_families: Vec<String>, font_size: f32, transform: SvgTransform, fill: Option<Fill>, stroke: Option<Stroke> ) -> Result<(), LyonTranslationError>
Add [Text
] to the writer, filling it as an unique [TextChunk
] whose
[TextSpan
] style applies to all the text.
Requires having called LyonWriter::add_fonts
beforehand.
Example
use roarsvg::{Color, LyonWriter, SvgTransform, fill, stroke};
use lyon_path::Path;
use lyon_path::geom::euclid::Point2D;
let file_path = "text.svg";
let writer = LyonWriter::new();
let mut fontdb = usvg::fontdb::Database::new();
fontdb.load_system_fonts();
let mut writer = writer.add_fonts(fontdb);
// first we add a Path, if not, the ViewBox calculation will panic!
// this is a caveat and should be fixed in the future
let mut path_builder = Path::builder();
path_builder.begin(Point2D::origin());
path_builder.line_to(
Point2D::new(3.0, 2.0),
);
path_builder.end(true);
writer
.push(
&path_builder.build(),
None,
Some(stroke(Color::black(), 1.0, 1.0)),
Some(SvgTransform::from_translate(2.0, 2.0)),
)
.expect("Path 1 should be writable!");
// push the created path with some fill and stroke, in the origin
writer
.push_text(
"hello".to_string(),
vec!["Arial".to_string()],
12.0,
SvgTransform::from_translate(0., 0.),
Some(fill(usvg::Color::black(), 1.0)),
Some(stroke(usvg::Color::black(), 1.0, 1.0)),
)
.expect("Text should be writable!");
let mut path_builder = Path::builder();
// finally, write the SVG, Text with be converted to SvgPath
writer.write(file_path).expect("Writing should not panic!");
sourcepub fn add_fonts_source(
self,
font_source: &[u8]
) -> LyonWriter<Option<Database>>
pub fn add_fonts_source( self, font_source: &[u8] ) -> LyonWriter<Option<Database>>
Loads fonts from a font file, building a FontProvider
if needed and enabling writing text.