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>

source

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).

source

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.

source

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.

source

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.

source

pub fn with_transform(self, trans: SvgTransform) -> Self

Add/replace a SvgTransform, which will be applied to the whole SVG as a group.

source

pub fn add_fonts<Fp: FontProvider>(self, fonts: Fp) -> LyonWriter<Option<Fp>>

Loads fonts from a font database, enabling writing [Text] (push_text).

source

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>

source

pub fn new() -> LyonWriter<NoText>

source

pub fn write<P: AsRef<Path>>( self, file_path: P ) -> Result<(), LyonTranslationError>

Write the contained Paths to an SVG at file_path. Text will NOT be written!

source

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>>

Implemented for Option<T> to be able to ergonomically take it without cloning.

source

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!");
source

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.

source

pub fn write<P: AsRef<Path>>( self, file_path: P ) -> Result<(), LyonTranslationError>

Write the contained Paths to an SVG at file_path, converting all [Text] nodes to paths.

Trait Implementations§

source§

impl Default for LyonWriter<NoText>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for LyonWriter<T>

§

impl<T> !Send for LyonWriter<T>

§

impl<T> !Sync for LyonWriter<T>

§

impl<T> Unpin for LyonWriter<T>where T: Unpin,

§

impl<T> !UnwindSafe for LyonWriter<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.