diff options
| author | Micha White <botahamec@outlook.com> | 2023-02-13 00:40:32 -0500 |
|---|---|---|
| committer | Micha White <botahamec@outlook.com> | 2023-02-13 00:40:32 -0500 |
| commit | 89c4981fad88c0ae8353f6e934c1ec10d51b0fd7 (patch) | |
| tree | 35e3161fb03ca72aa944b39b7f1f6c2ad6463adb /tvg/src/lib.rs | |
| parent | 861b467b95be55db3a42182b77dba944869bf49f (diff) | |
Support custom color encodings
Diffstat (limited to 'tvg/src/lib.rs')
| -rw-r--r-- | tvg/src/lib.rs | 33 |
1 files changed, 32 insertions, 1 deletions
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<C: Color> { @@ -21,6 +21,7 @@ pub struct TvgFile<C: Color> { } impl<C: Color + std::fmt::Debug> TvgFile<C> { + /// Read a TinyVG file. pub fn read_from(reader: &mut impl Read) -> Result<Self, TvgError> { let header = TvgHeader::read(reader)?; let color_table = @@ -42,6 +43,34 @@ impl<C: Color + std::fmt::Debug> TvgFile<C> { 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<Custom: Color>( + reader: &mut impl Read, + ) -> Result<Self, TvgError> { + let header = TvgHeader::read(reader)?; + let color_table = ColorTable::read_from_encoding_with_custom::<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")] |
