gif_parser/types/
plain_text_ext.rs1use crate::{
2 error::{GifParserError, PlainTextExtIo},
3 read_ext::{ReadExt, ReadSubBlockExt},
4};
5use std::io::Read;
6
7#[derive(Debug)]
8pub struct PlainTextExtension {
9 pub text_grid_left: u16,
10 pub text_grid_top: u16,
11 pub text_grid_width: u16,
12 pub text_grid_height: u16,
13 pub cell_width: u8,
14 pub cell_height: u8,
15 pub text_fg_color_index: u8,
16 pub text_bg_color_index: u8,
17 pub plain_text_data: String,
18}
19
20impl PlainTextExtension {
21 pub(crate) fn from_bytes<T: Read>(reader: &mut T) -> Result<Self, GifParserError> {
22 let data = reader
23 .read_bytes::<13>()
24 .map_err(PlainTextExtIo)
25 .map_err(GifParserError::PlainTextExtIo)?;
26
27 if data[0] != 0x0C {
28 return Err(GifParserError::PlainTextExtInvalidBlockSize);
29 }
30
31 let text = {
32 let data = reader
33 .read_subblock()
34 .map_err(PlainTextExtIo)
35 .map_err(GifParserError::PlainTextExtIo)?;
36 String::from_utf8(data).map_err(GifParserError::PlainTextExtInvalidUtf8)?
37 };
38
39 let text_grid_left = data[1..3].try_into().expect("Valid range for u16");
40 let text_grid_top = data[3..5].try_into().expect("Valid range for u16");
41 let text_grid_width = data[5..7].try_into().expect("Valid range for u16");
42 let text_grid_height = data[7..9].try_into().expect("Valid range for u16");
43
44 Ok(Self {
45 text_grid_left: u16::from_le_bytes(text_grid_left),
46 text_grid_top: u16::from_le_bytes(text_grid_top),
47 text_grid_width: u16::from_le_bytes(text_grid_width),
48 text_grid_height: u16::from_le_bytes(text_grid_height),
49 cell_width: data[9],
50 cell_height: data[10],
51 text_fg_color_index: data[11],
52 text_bg_color_index: data[12],
53 plain_text_data: text,
54 })
55 }
56}