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
/// Logic and types specific to individual glyph layout.

use super::Font;
use types::{Range, Rect, RectExt};
use std;
use rusttype;
use rusttype::LayoutIter;
use super::line::LineInfo;

/// An iterator yielding the `Rect` for each `char`'s `Glyph` in the given `text`.
pub struct GlyphRects<'a, 'b> {
    /// The *y* axis `Range` of the `Line` for which character `Rect`s are being yielded.
    ///
    /// Every yielded `Rect` will use this as its `y` `Range`.
    y: Range,
    /// The position of the next `Rect`'s left edge along the *x* axis.
    next_left: f32,
    /// `PositionedGlyphs` yielded by the RustType `LayoutIter`.
    layout: LayoutIter<'a, 'b>,
}


impl<'a, 'b> Iterator for GlyphRects<'a, 'b> {
    type Item = Rect;
    fn next(&mut self) -> Option<Self::Item> {
        let GlyphRects { ref mut next_left, ref mut layout, y } = *self;
        layout.next().map(|g| {
            let left = *next_left;
            let right = g.pixel_bounding_box()
                .map(|bb| bb.max.x as f32)
                .unwrap_or_else(|| left + g.unpositioned().h_metrics().advance_width);
            *next_left = right;
            let x = Range::new(left, right);
            Rect::from_ranges(x, y)
        })
    }
}

/// An iterator that, for every `(line, line_rect)` pair yielded by the given iterator,
/// produces an iterator that yields a `Rect` for every character in that line.
pub struct GlyphRectsPerLine<'a, I> {
    lines_with_rects: I,
    font: &'a Font,
    font_size: f32,
}

impl<'a, I> GlyphRectsPerLine<'a, I>
    where I: Iterator<Item = (&'a str, Rect)>
{
    /// Produce an iterator that, for every `(line, line_rect)` pair yielded by the given iterator,
    /// produces an iterator that yields a `Rect` for every character in that line.
    ///
    /// This is useful when information about character positioning is needed when reasoning about
    /// text layout.
    pub fn new(lines_with_rects: I, font: &'a Font, font_size: f32) -> GlyphRectsPerLine<'a, I> {
        GlyphRectsPerLine {
            lines_with_rects: lines_with_rects,
            font: font,
            font_size: font_size,
        }
    }
}
impl<'a, I> Iterator for GlyphRectsPerLine<'a, I>
    where I: Iterator<Item = (&'a str, Rect)>
{
    type Item = GlyphRects<'a, 'a>;
    fn next(&mut self) -> Option<Self::Item> {
        let GlyphRectsPerLine { ref mut lines_with_rects, font, font_size } = *self;
        let scale = super::pt_to_scale(font_size);
        lines_with_rects.next().map(|(line_text, line_rect)| {
            let (x, y) = (line_rect.left(), line_rect.top());
            let point = rusttype::Point { x: x, y: y };
            GlyphRects {
                next_left: line_rect.left(),
                layout: font.layout(line_text, scale, point),
                y: line_rect.y_range(),
            }
        })
    }
}

/// Yields a `Rect` for each selected character in a single line of text.
///
/// This iterator can only be produced by the `SelectedCharRectsPerLine` iterator.
pub struct SelectedGlyphRects<'a, 'b> {
    enumerated_rects: std::iter::Enumerate<GlyphRects<'a, 'b>>,
    end_char_idx: usize,
}
impl<'a, 'b> Iterator for SelectedGlyphRects<'a, 'b> {
    type Item = Rect;
    fn next(&mut self) -> Option<Self::Item> {
        let SelectedGlyphRects { ref mut enumerated_rects, end_char_idx } = *self;
        enumerated_rects.next()
            .and_then(|(i, rect)| if i < end_char_idx { Some(rect) } else { None })
    }
}


/// Yields an iterator yielding `Rect`s for each selected character in each line of text within
/// the given iterator yielding char `Rect`s.
///
/// Given some `start` and `end` indices, only `Rect`s for `char`s between these two indices
/// will be produced.
///
/// All lines that have no selected `Rect`s will be skipped.
pub struct SelectedGlyphRectsPerLine<'a, I> {
    enumerated_rects_per_line: std::iter::Enumerate<GlyphRectsPerLine<'a, I>>,
    start_cursor_idx: super::cursor::Index,
    end_cursor_idx: super::cursor::Index,
}

impl<'a, I> SelectedGlyphRectsPerLine<'a, I>
    where I: Iterator<Item = (&'a str, Rect)>
{
    /// Produces an iterator that yields iterators yielding `Rect`s for each selected character in
    /// each line of text within the given iterator yielding char `Rect`s.
    ///
    /// Given some `start` and `end` indices, only `Rect`s for `char`s between these two indices
    /// will be produced.
    ///
    /// All lines that have no selected `Rect`s will be skipped.
    pub fn new(lines_with_rects: I,
               font: &'a Font,
               font_size: f32,
               start: super::cursor::Index,
               end: super::cursor::Index)
               -> SelectedGlyphRectsPerLine<'a, I> {
        SelectedGlyphRectsPerLine {
            enumerated_rects_per_line: GlyphRectsPerLine::new(lines_with_rects, font, font_size)
                .enumerate(),
            start_cursor_idx: start,
            end_cursor_idx: end,
        }
    }
}
impl<'a, I> Iterator for SelectedGlyphRectsPerLine<'a, I>
    where I: Iterator<Item = (&'a str, Rect)>
{
    type Item = SelectedGlyphRects<'a, 'a>;
    fn next(&mut self) -> Option<Self::Item> {
        let SelectedGlyphRectsPerLine { ref mut enumerated_rects_per_line,
                                        start_cursor_idx,
                                        end_cursor_idx } = *self;

        enumerated_rects_per_line.next().map(|(i, rects)| {
            let end_char_idx =
                    // If this is the last line, the end is the char after the final selected char.
                    if i == end_cursor_idx.line {
                        end_cursor_idx.char
                    // Otherwise if in range, every char in the line is selected.
                    } else if start_cursor_idx.line <= i && i < end_cursor_idx.line {
                        std::u32::MAX as usize
                    // Otherwise if out of range, no chars are selected.
                    } else {
                        0
                    };

            let mut enumerated_rects = rects.enumerate();

            // If this is the first line, skip all non-selected chars.
            if i == start_cursor_idx.line {
                for _ in 0..start_cursor_idx.char {
                    enumerated_rects.next();
                }
            }

            SelectedGlyphRects {
                enumerated_rects: enumerated_rects,
                end_char_idx: end_char_idx,
            }
        })
    }
}


/// Find the index of the character that directly follows the cursor at the given `cursor_idx`.
///
/// Returns `None` if either the given `cursor::Index` `line` or `idx` fields are out of bounds
/// of the line information yielded by the `line_infos` iterator.
pub fn index_after_cursor<I>(mut line_infos: I, cursor_idx: super::cursor::Index) -> Option<usize>
    where I: Iterator<Item = LineInfo>
{
    line_infos.nth(cursor_idx.line)
        .and_then(|line_info| {
            let start_char = line_info.start_char;
            let end_char = line_info.end_char();
            let char_index = start_char + cursor_idx.char;
            if char_index <= end_char {
                Some(char_index)
            } else {
                None
            }
        })
}