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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
extern crate itertools;
extern crate unicode_segmentation;

use std::io::BufReader;
use std::{io};

use itertools::Itertools;
use std::io::BufRead;
use unicode_segmentation::UnicodeSegmentation;

const LOOKUP_TABLE : [char; 256] =
[
    '⠀', '⠁', '⠂', '⠃', '⠄', '⠅', '⠆', '⠇', '⡀', '⡁', '⡂', '⡃', '⡄', '⡅', '⡆', '⡇',
'⠈', '⠉', '⠊', '⠋', '⠌', '⠍', '⠎', '⠏', '⡈', '⡉', '⡊', '⡋', '⡌', '⡍', '⡎', '⡏',
'⠐', '⠑', '⠒', '⠓', '⠔', '⠕', '⠖', '⠗', '⡐', '⡑', '⡒', '⡓', '⡔', '⡕', '⡖', '⡗',
'⠘', '⠙', '⠚', '⠛', '⠜', '⠝', '⠞', '⠟', '⡘', '⡙', '⡚', '⡛', '⡜', '⡝', '⡞', '⡟',
'⠠', '⠡', '⠢', '⠣', '⠤', '⠥', '⠦', '⠧', '⡠', '⡡', '⡢', '⡣', '⡤', '⡥', '⡦', '⡧',
'⠨', '⠩', '⠪', '⠫', '⠬', '⠭', '⠮', '⠯', '⡨', '⡩', '⡪', '⡫', '⡬', '⡭', '⡮', '⡯',
'⠰', '⠱', '⠲', '⠳', '⠴', '⠵', '⠶', '⠷', '⡰', '⡱', '⡲', '⡳', '⡴', '⡵', '⡶', '⡷',
'⠸', '⠹', '⠺', '⠻', '⠼', '⠽', '⠾', '⠿', '⡸', '⡹', '⡺', '⡻', '⡼', '⡽', '⡾', '⡿',
'⢀', '⢁', '⢂', '⢃', '⢄', '⢅', '⢆', '⢇', '⣀', '⣁', '⣂', '⣃', '⣄', '⣅', '⣆', '⣇',
'⢈', '⢉', '⢊', '⢋', '⢌', '⢍', '⢎', '⢏', '⣈', '⣉', '⣊', '⣋', '⣌', '⣍', '⣎', '⣏',
'⢐', '⢑', '⢒', '⢓', '⢔', '⢕', '⢖', '⢗', '⣐', '⣑', '⣒', '⣓', '⣔', '⣕', '⣖', '⣗',
'⢘', '⢙', '⢚', '⢛', '⢜', '⢝', '⢞', '⢟', '⣘', '⣙', '⣚', '⣛', '⣜', '⣝', '⣞', '⣟',
'⢠', '⢡', '⢢', '⢣', '⢤', '⢥', '⢦', '⢧', '⣠', '⣡', '⣢', '⣣', '⣤', '⣥', '⣦', '⣧',
'⢨', '⢩', '⢪', '⢫', '⢬', '⢭', '⢮', '⢯', '⣨', '⣩', '⣪', '⣫', '⣬', '⣭', '⣮', '⣯',
'⢰', '⢱', '⢲', '⢳', '⢴', '⢵', '⢶', '⢷', '⣰', '⣱', '⣲', '⣳', '⣴', '⣵', '⣶', '⣷',
'⢸', '⢹', '⢺', '⢻', '⢼', '⢽', '⢾', '⢿', '⣸', '⣹', '⣺', '⣻', '⣼', '⣽', '⣾', '⣿',
];

/// Convert simple binary representation that this crate
/// uses to unicode character, by calculating the right unicode
///
/// Unicode offset format
///
/// ```norust
/// 03
/// 14
/// 25
/// 67
/// ```
///
/// This crate format:
/// ```norust
/// 04
/// 15
/// 26
/// 37
/// ```
fn byte_to_braillechar(nb : u8) -> char {

    let nb = nb as u32;
    let mut ub = 0u32;
    ub |= ((nb >> 0) & 0x7) << 0;
    ub |= ((nb >> 4) & 0x7) << 3;
    ub |= ((nb >> 3) & 0x1) << 6;
    ub |= ((nb >> 7) & 0x1) << 7;



    std::char::from_u32(0x2800u32 + ub).unwrap()
}

#[allow(unused)]
/// This is the script that generated `LOOKUP_TABLE`
fn generate_lookup_table() {
    for bin2 in 0..16 {
        println!(
            "'{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}',",
            byte_to_braillechar(bin2 * 16 + 0),
            byte_to_braillechar(bin2 * 16 + 1),
            byte_to_braillechar(bin2 * 16 + 2),
            byte_to_braillechar(bin2 * 16 + 3),
            byte_to_braillechar(bin2 * 16 + 4),
            byte_to_braillechar(bin2 * 16 + 5),
            byte_to_braillechar(bin2 * 16 + 6),
            byte_to_braillechar(bin2 * 16 + 7),
            byte_to_braillechar(bin2 * 16 + 8),
            byte_to_braillechar(bin2 * 16 + 9),
            byte_to_braillechar(bin2 * 16 + 10),
            byte_to_braillechar(bin2 * 16 + 11),
            byte_to_braillechar(bin2 * 16 + 12),
            byte_to_braillechar(bin2 * 16 + 13),
            byte_to_braillechar(bin2 * 16 + 14),
            byte_to_braillechar(bin2 * 16 + 15),
            )
    }
}

const BRAILLE_Y :usize = 4;
#[allow(unused)]
const BRAILLE_X :usize = 2;

/// Generate minimap/preview for the text read from `reader`j
pub fn to_minimap<'a, R>(reader: R, settings: Settings) -> Box<Iterator<Item = String> + 'a>
    where R: io::Read + 'a
{

    let i = to_minimap_bool(reader, settings)
        .batching(move |i| {
            let mut group : Vec<Vec<bool>> = Vec::new();
            for _ in 0..BRAILLE_Y {
                if let Some(line) = i.next() {
                    group.push(line);
                } else {
                    break;
                }
            }
            if group.is_empty() {
                None
            } else {
                Some(group)
            }
        })
        // map to binary representations of 4 bits in every column
        .map(move |group| {
            let mut line_iters : Vec<_> = group
                .iter()
                .map(|line| line.iter()).collect();

            let mut binary_column : Vec<u8> = Vec::new();


            loop {
                // reset to false if anything was found
                let mut finished = true;

                let mut binary = 0;

                for (row_i, mut line_iter) in line_iters.iter_mut().enumerate() {
                    if let Some(&printable) = line_iter.next() {
                        finished = false;
                        if printable {
                            binary |= 1 << row_i;
                        }
                    }
                }

                if finished {
                    break;
                }

                binary_column.push(binary)
            }

            binary_column
        })
        .map(|line| {
            let mut s = String::new();

            let mut iter = line.iter();
            loop {
                let bin1 = iter.next();
                let bin2 = iter.next();

                if bin1.is_none() && bin2.is_none() {
                    break;
                }

                let bin1 : u8 = *bin1.unwrap_or(&0);
                let bin2 : u8 = *bin2.unwrap_or(&0);

                debug_assert!(bin1 < 16);
                debug_assert!(bin2 < 16);

                let ch = LOOKUP_TABLE[(bin2 * 16 + bin1) as usize];
                debug_assert_eq!(ch, byte_to_braillechar((bin2 * 16 + bin1)));
                s.push(ch)
            }

            s
        });

    Box::new(i)
}

#[allow(unused)]
fn to_minimap_stars<'a, R>(reader: R, settings: Settings) -> Box<Iterator<Item = String> + 'a>
    where R: io::Read + 'a
{

    let i = to_minimap_bool(reader, settings)
        .map(|dots| {
            let v : Vec<_> = dots.iter().map(|&b| if b { "*" } else { " " }).collect();
            let line : String = v.join("");
            line
        });

    Box::new(i)
}

fn to_minimap_bool<'a, R>(reader: R,
                          settings: Settings)
                          -> Box<Iterator<Item = Vec<bool>> + 'a>
    where R: io::Read + 'a
{

    let buf_reader = BufReader::new(reader);

    let xscale = settings.xscale;
    let yscale = settings.yscale;

    let i = buf_reader
        .lines()
        .take_while(|b| b.is_ok())
        .map(|b| b.unwrap())
        // iterate over groups of n lines
        .batching(move |i| {
            let mut group : Vec<String> = Vec::new();
            for _ in 0..yscale {
                if let Some(line) = i.next() {
                    group.push(line);
                } else {
                    break;
                }
            }
            if group.is_empty() {
                None
            } else {
                Some(group)
            }
        })
        .map(move |group| {
            let mut line_iters : Vec<_> = group
                .iter()
                .map(|line| UnicodeSegmentation::graphemes(line.as_str(), true)).collect();

            let mut dots : Vec<bool> = Vec::new();
            loop {
                let mut in_this_column : Vec<bool> = Vec::new();
                for line_iter in &mut line_iters {
                    let mut contains_nonwhite = None;

                    for _ in 0..xscale {
                        if let Some(glyph) = line_iter.next() {
                            if contains_nonwhite != Some(true) {
                                if glyph.chars().any(|ch| !ch.is_whitespace()) {
                                    contains_nonwhite = Some(true);
                                } else {
                                    contains_nonwhite = Some(false);
                                }
                            }
                        }
                    }

                    if let Some(nw) = contains_nonwhite {
                        in_this_column.push(nw)
                    }
                }

                if in_this_column.is_empty() {
                    break;
                }

                dots.push(in_this_column.iter().any(|&g| g ))
            }
            dots
        });

    Box::new(i)
}

#[derive(Clone)]
pub struct Settings {
    pub xscale: usize,
    pub yscale: usize,
}

impl Settings {
    pub fn new() -> Self {
        Settings {
            xscale: 1,
            yscale: 1,
        }
    }
}