|
| 1 | +/// DRCS (Dynamically Redefinable Character Set) support for VT320 terminal |
| 2 | +/// Implements handling for the soft character set functionality |
| 3 | +/// Based on https://vt100.net/dec/vt320/soft_characters |
| 4 | +
|
| 5 | +use rustc_hash::FxHashMap; |
| 6 | +use std::str; |
| 7 | +use base64::engine::general_purpose::STANDARD as Base64; |
| 8 | +use base64::Engine; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +pub struct DrcsCharacter { |
| 12 | + pub data: Vec<u8>, |
| 13 | + pub width: u8, |
| 14 | + pub height: u8, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Default, Debug)] |
| 18 | +pub struct DrcsSet { |
| 19 | + characters: FxHashMap<u8, DrcsCharacter>, |
| 20 | + // Current active DRCS set ID |
| 21 | + active_set: u8, |
| 22 | +} |
| 23 | + |
| 24 | +impl DrcsSet { |
| 25 | + pub fn new() -> Self { |
| 26 | + Self { |
| 27 | + characters: FxHashMap::default(), |
| 28 | + active_set: 0, |
| 29 | + } |
| 30 | + } |
| 31 | + pub fn define_character(&mut self, char_code: u8, width: u8, height: u8, data: Vec<u8>) { |
| 32 | + self.characters.insert( |
| 33 | + char_code, |
| 34 | + DrcsCharacter { |
| 35 | + data, |
| 36 | + width, |
| 37 | + height, |
| 38 | + }, |
| 39 | + ); |
| 40 | + } |
| 41 | + pub fn set_active_set(&mut self, set_id: u8) { |
| 42 | + // Set the active DRCS character set |
| 43 | + self.active_set = set_id; |
| 44 | + } |
| 45 | + pub fn get_character(&self, char_code: u8) -> Option<&DrcsCharacter> { |
| 46 | + self.characters.get(&char_code) |
| 47 | + } |
| 48 | + pub fn clear(&mut self) { |
| 49 | + self.characters.clear(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// Parse OSC parameters for DRCS soft character definition |
| 54 | +pub fn parse_drcs(params: &[&[u8]]) -> Option<(u8, u8, u8, Vec<u8>)> { |
| 55 | + // Format: OSC 53 ; char_code ; width ; height ; base64_data ST |
| 56 | + if params.len() < 5 { |
| 57 | + return None; |
| 58 | + } |
| 59 | + |
| 60 | + // Parse character code |
| 61 | + let char_code = str::from_utf8(params[1]) |
| 62 | + .ok()? |
| 63 | + .parse::<u8>() |
| 64 | + .ok()?; |
| 65 | + |
| 66 | + // Parse width and height |
| 67 | + let width = str::from_utf8(params[2]) |
| 68 | + .ok()? |
| 69 | + .parse::<u8>() |
| 70 | + .ok()?; |
| 71 | + |
| 72 | + let height = str::from_utf8(params[3]) |
| 73 | + .ok()? |
| 74 | + .parse::<u8>() |
| 75 | + .ok()?; |
| 76 | + |
| 77 | + // Parse base64 data |
| 78 | + let bitmap_data = Base64.decode(params[4]).ok()?; |
| 79 | + |
| 80 | + // Verify the bitmap data has the expected size |
| 81 | + let expected_size = ((width as usize) * (height as usize) + 7) / 8; // ceil(width * height / 8) |
| 82 | + if bitmap_data.len() != expected_size { |
| 83 | + return None; |
| 84 | + } |
| 85 | + |
| 86 | + Some((char_code, width, height, bitmap_data)) |
| 87 | +} |
| 88 | + |
| 89 | +/// Parse OSC parameters for selecting a DRCS set |
| 90 | +pub fn parse_drcs_select(params: &[&[u8]]) -> Option<u8> { |
| 91 | + // Format: OSC 54 ; set_id ST |
| 92 | + if params.len() < 2 { |
| 93 | + return None; |
| 94 | + } |
| 95 | + |
| 96 | + // Parse set ID |
| 97 | + str::from_utf8(params[1]) |
| 98 | + .ok()? |
| 99 | + .parse::<u8>() |
| 100 | + .ok() |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +// Convert a DRCS bitmap to a displayable format as string |
| 105 | +// Rio case need to be sugarloaf |
| 106 | +pub fn drcs_to_string(data: &[u8], width: u8, height: u8) -> String { |
| 107 | + let mut result = String::new(); |
| 108 | + |
| 109 | + for y in 0..height { |
| 110 | + for x in 0..width { |
| 111 | + let byte_index = (y as usize * width as usize + x as usize) / 8; |
| 112 | + let bit_index = 7 - ((y as usize * width as usize + x as usize) % 8); |
| 113 | + |
| 114 | + if byte_index < data.len() { |
| 115 | + let pixel = (data[byte_index] >> bit_index) & 1; |
| 116 | + result.push(if pixel == 1 { '█' } else { ' ' }); |
| 117 | + } else { |
| 118 | + result.push('?'); |
| 119 | + } |
| 120 | + } |
| 121 | + result.push('\n'); |
| 122 | + } |
| 123 | + |
| 124 | + result |
| 125 | +} |
| 126 | + |
| 127 | +/// Create a DRCS bitmap from a text representation |
| 128 | +pub fn string_to_drcs(text: &str, width: u8, height: u8) -> Vec<u8> { |
| 129 | + let mut data = vec![0u8; ((width as usize * height as usize) + 7) / 8]; |
| 130 | + |
| 131 | + for (i, c) in text.chars().enumerate() { |
| 132 | + if i >= width as usize * height as usize { |
| 133 | + break; |
| 134 | + } |
| 135 | + |
| 136 | + let y = i / width as usize; |
| 137 | + let x = i % width as usize; |
| 138 | + |
| 139 | + if c != ' ' { |
| 140 | + let byte_index = (y * width as usize + x) / 8; |
| 141 | + let bit_index = 7 - ((y * width as usize + x) % 8); |
| 142 | + |
| 143 | + data[byte_index] |= 1 << bit_index; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + data |
| 148 | +} |
| 149 | + |
| 150 | +pub fn test() { |
| 151 | + // Define a simple character (a smiley face) |
| 152 | + let _smiley_data = string_to_drcs( |
| 153 | + " #### \ |
| 154 | + # #\ |
| 155 | + # # # #\ |
| 156 | + # #\ |
| 157 | + # ## #\ |
| 158 | + # # # #\ |
| 159 | + # #\ |
| 160 | + # #\ |
| 161 | + #### ", |
| 162 | + 8, 8 |
| 163 | + ); |
| 164 | + |
| 165 | + // Define the smiley face as character code 65 ('A') |
| 166 | + // terminal.define_soft_character(65, 8, 8, smiley_data); |
| 167 | + |
| 168 | + // if terminal.is_drcs_character(65) { |
| 169 | + // if let Some(bitmap) = terminal.render_drcs_character(65) { |
| 170 | + // let visual = utils::drcs_to_string(&bitmap, 8, 8); |
| 171 | + // println!("DRCS character 65:\n{}", visual); |
| 172 | + // } |
| 173 | +} |
0 commit comments