From 89c4981fad88c0ae8353f6e934c1ec10d51b0fd7 Mon Sep 17 00:00:00 2001 From: Micha White Date: Mon, 13 Feb 2023 00:40:32 -0500 Subject: Support custom color encodings --- tvg/src/lib.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'tvg/src/lib.rs') diff --git a/tvg/src/lib.rs b/tvg/src/lib.rs index 5cbe33c..011b5c7 100644 --- a/tvg/src/lib.rs +++ b/tvg/src/lib.rs @@ -11,7 +11,7 @@ mod commands; mod header; mod path; -pub use colors::Rgba16; +pub use colors::{Rgb565, Rgba16, Rgba8888, RgbaF32}; pub use header::SUPPORTED_VERSION; pub struct TvgFile { @@ -21,6 +21,7 @@ pub struct TvgFile { } impl TvgFile { + /// Read a TinyVG file. pub fn read_from(reader: &mut impl Read) -> Result { let header = TvgHeader::read(reader)?; let color_table = @@ -42,6 +43,34 @@ impl TvgFile { commands: commands.into_boxed_slice(), }) } + + /// Read a TinyVG file. If a Custom color encoding if found, use the specified encoding. + pub fn read_with_custom_encoding( + reader: &mut impl Read, + ) -> Result { + let header = TvgHeader::read(reader)?; + let color_table = ColorTable::read_from_encoding_with_custom::( + reader, + header.color_count(), + header.color_encoding(), + )?; + + let mut commands = Vec::new(); + loop { + let command = Command::read(reader, &header)?; + commands.push(command.clone()); + + if command.is_end_of_document() { + break; + } + } + + Ok(Self { + header, + color_table, + commands: commands.into_boxed_slice(), + }) + } } #[derive(Debug, Error)] @@ -52,6 +81,8 @@ pub enum TvgError { UnsupportedVersion(u8), #[error("Found a coordinate range with an index of 3, which is invalid")] InvalidCoordinateRange, + #[error("Found a Custom color encoding, which is unsupported")] + UnsupportedColorEncoding, #[error("Found a command with an index of {}, which is invalid", .0)] InvalidCommand(u8), #[error("Found a style kind with an index of 3, which is invalid")] -- cgit v1.2.3