1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! # The editor fonts

use super::LoadError;
use crate::util::{data::BIT_STRING, Buf};
use nom::{
    bytes::complete::{tag, take},
    number::complete::{be_u32, u8},
    IResult,
};
use std::{ops::Deref, path::Path};

const BORDER: [&str; 17] = [
    "+|---------------+",
    "+-|--------------+",
    "+--|-------------+",
    "+---|------------+",
    "+----|-----------+",
    "+-----|----------+",
    "+------|---------+",
    "+-------|--------+",
    "+--------|-------+",
    "+---------|------+",
    "+----------|-----+",
    "+-----------|----+",
    "+------------|---+",
    "+-------------|--+",
    "+--------------|-+",
    "+---------------|+",
    "+----------------+",
];

#[derive(Debug)]
/// A single editor font
pub struct ESet<'a> {
    /// The leading buffer / header
    pub buf1: Buf<'a>,
    /// The chars in this charset
    pub chars: Vec<EChar<'a>>,
}

/// An owned version of the editor font
pub struct OwnedESet {
    inner: ESet<'static>,
    #[allow(unused)]
    buffer: Vec<u8>,
}

impl Deref for OwnedESet {
    type Target = ESet<'static>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl OwnedESet {
    /// Load an editor charset
    pub fn load(path: &Path) -> Result<Self, LoadError> {
        let buffer = std::fs::read(path)?;
        // SAFETY: this is safe, because `buffer` is plain data and
        // drop order between `inner` and `buffer` really doesn't matter.
        let input: &'static [u8] = unsafe { std::mem::transmute(&buffer[..]) };
        let (_, inner) = parse_eset(input).unwrap();
        Ok(Self { inner, buffer })
    }
}

#[derive(Debug)]
/// A single editor charset character
pub struct EChar<'a> {
    /// The width of the glyph (in document coordinates)
    pub width: u8,
    /// The height of the glyph
    pub height: u8,
    /// The distance of the top edge of the stored glyph from the top of the available box
    pub top: u8,
    /// The buffer that contains the pixels
    pub buf: &'a [u8],
}

/// The special NULL char
pub const ECHAR_NULL: EChar<'static> = EChar {
    width: 0,
    height: 0,
    top: 0,
    buf: &[],
};

impl<'a> ESet<'a> {
    /// Print a representation of the charset to the console
    ///
    /// FIXME: make this generic over `io::Write` or `fmt::Write`?
    pub fn print(&self) {
        let capacity = self.chars.len();
        let mut widths = Vec::with_capacity(capacity);
        let mut skips = Vec::with_capacity(capacity);
        for (index, ch) in self.chars.iter().enumerate() {
            println!("\nchar[{}]", index);
            let wu = ch.width as usize;
            let hu = ch.height as usize;
            widths.push(ch.width);
            skips.push(ch.top);
            println!("{}, {}x{}", ch.top, wu, hu);
            let border = BORDER[wu];
            println!("{}", border);
            for _ in 1..ch.top {
                println!("|                |");
            }
            if ch.top > 0 {
                println!("-                -");
            }
            for i in 0..hu {
                let left = ch.buf[2 * i] as usize;
                let right = ch.buf[2 * i + 1] as usize;
                println!("|{}{}|", &BIT_STRING[left], &BIT_STRING[right]);
            }
            let rest = 24 - ch.top - ch.height;
            if rest > 0 {
                println!("-                -");
            }
            for _ in 1..rest {
                println!("|                |");
            }
            println!("{}", border);
        }
        println!();
        println!("pub const WIDTH: [u8; 128] = [");
        print!("  0, ");
        for (i, w) in widths.iter().cloned().enumerate() {
            if i % 16 == 15 {
                println!();
            }
            print!("{:3},", w);
            if i % 16 != 14 {
                print!(" ");
            }
        }
        println!();
        println!("];");
        println!("pub const SKIP: [u8; 128] = [");
        print!("  0, ");
        for (i, s) in skips.iter().cloned().enumerate() {
            if i % 16 == 15 {
                println!();
            }
            print!("{:3},", s);
            if i % 16 != 14 {
                print!(" ");
            }
        }
        println!();
        println!("];");
    }
}

/// Parse a single editor char
pub fn parse_echar(input: &[u8]) -> IResult<&[u8], EChar> {
    let (input, top) = u8(input)?;
    let (input, height) = u8(input)?;
    let (input, width) = u8(input)?;
    let (input, _d) = u8(input)?;
    let (input, buf) = take((height * 2) as usize)(input)?;
    Ok((
        input,
        EChar {
            width,
            height,
            top,
            buf,
        },
    ))
}

/// Parse a full editor charset file.
///
/// This method checks for the magic bytes `eset0001`
pub fn parse_eset(input: &[u8]) -> IResult<&[u8], ESet> {
    let (input, _) = tag(b"eset")(input)?;
    let (input, _) = tag(b"0001")(input)?;
    let (input, skip) = be_u32(input)?;

    let (input, buf1) = take(skip as usize)(input)?;

    let (input, len) = be_u32(input)?;

    let (input, mut offset_buf) = take((skip - 1) as usize * 4)(input)?;
    let (input, char_buf) = take(len as usize)(input)?;

    let mut chars = Vec::with_capacity(skip as usize);
    chars.push(ECHAR_NULL);

    for _ in 1..skip {
        let (rest, offset) = be_u32(offset_buf)?;
        let (_, echar) = parse_echar(&char_buf[offset as usize..])?;
        chars.push(echar);
        offset_buf = rest;
    }

    Ok((
        input,
        ESet {
            buf1: Buf(buf1),
            chars,
        },
    ))
}