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
278
279
280
use crate::draw::Color;
use crate::layout::Rectangle;
use std::borrow::Cow;

/// How to wrap text
#[derive(Clone, Copy, Debug)]
pub enum TextWrap {
    /// Don't wrap text at all (fastest)
    NoWrap,
    /// Wrap text per character
    Wrap,
    /// Try to keep words on the same line (slowest)
    WordWrap,
}

/// A font loaded by the [`Ui`](../struct.Ui.html)
#[derive(Clone)]
pub struct Font {
    pub(crate) inner: super::cache::Font,
    pub(crate) id: super::cache::FontId,
    pub(crate) tex_slot: usize,
}

/// A styled paragraph of text
#[derive(Clone)]
pub struct Text<'a> {
    /// The text
    pub text: Cow<'a, str>,
    /// Font to render the text with
    pub font: Font,
    /// Font size to render the text with
    pub size: f32,
    /// Wrapping style to use
    pub wrap: TextWrap,
    /// Color to render the text with
    pub color: Color,
}

/// Iterator over characters that have been layout by the rusttype engine.
pub struct CharPositionIter<'a, 'b: 'a> {
    font: &'b rusttype::Font<'static>,
    scale: rusttype::Scale,
    last: Option<rusttype::GlyphId>,
    x: f32,
    base: std::str::Chars<'a>,
}

impl<'a, 'b> Iterator for CharPositionIter<'a, 'b> {
    type Item = (char, rusttype::ScaledGlyph<'static>, f32, f32);

    fn next(&mut self) -> Option<Self::Item> {
        self.base.next().and_then(|c| {
            let g = self.font.glyph(c);
            let g = g.scaled(self.scale);
            let w = g.h_metrics().advance_width
                + self
                    .last
                    .map(|last| self.font.pair_kerning(self.scale, last, g.id()))
                    .unwrap_or(0.0);

            self.last = Some(g.id());

            let elem = (c, g, self.x, self.x + w);
            self.x += w;
            Some(elem)
        })
    }
}

struct WordWrapper<'a, 'b: 'a> {
    x: f32,
    y: f32,
    final_x: f32,
    final_y: f32,
    width: f32,
    height: f32,
    iter: CharPositionIter<'a, 'b>,
    f: &'a mut dyn FnMut(rusttype::ScaledGlyph<'static>, f32, f32, f32),
}

impl<'a, 'b: 'a> WordWrapper<'a, 'b> {
    fn layout_word(&mut self, glyph: rusttype::ScaledGlyph<'static>, a: f32, b: f32, c: f32, mut word: bool) {
        if word {
            self.x = self.final_x;
            self.y = self.final_y;

            if let Some((ch, glyph, b, c)) = self.iter.next() {
                if ch.is_alphanumeric() {
                    if c - self.x > self.width {
                        self.x = a;
                        self.y += self.height;
                        word = false;
                    }
                    self.layout_word(glyph, a, b, c, word);
                }
            }

            (self.f)(glyph, b - self.x, c - self.x, self.y);
        } else {
            self.final_x = self.x;
            self.final_y = self.y;

            if c - self.final_x > self.width {
                self.final_x = b;
                self.final_y += self.height;
            }
            (self.f)(glyph, b - self.final_x, c - self.final_x, self.final_y);

            while let Some((ch, glyph, b, c)) = self.iter.next() {
                if c - self.final_x > self.width {
                    self.final_x = b;
                    self.final_y += self.height;
                }

                (self.f)(glyph, b - self.final_x, c - self.final_x, self.final_y);

                if !ch.is_alphanumeric() {
                    break;
                }
            }
        }
    }
}

impl<'t> Text<'t> {
    pub(crate) fn char_positions<'a, 'b>(&'b self) -> CharPositionIter<'a, 'b> {
        let scale = rusttype::Scale {
            x: self.size,
            y: self.size,
        };
        CharPositionIter {
            font: &self.font.inner,
            scale: scale,
            last: None,
            x: 0.0,
            base: self.text.chars(),
        }
    }

    pub(crate) fn layout<F: FnMut(rusttype::ScaledGlyph<'static>, f32, f32, f32)>(&self, rect: Rectangle, mut f: F) {
        let line = self.font.inner.v_metrics(rusttype::Scale {
            x: self.size,
            y: self.size,
        });

        let width = rect.width();
        let height = -line.descent + line.line_gap + line.ascent;

        match self.wrap {
            TextWrap::NoWrap => {
                for (_, g, a, b) in self.char_positions() {
                    f(g, a, b, line.ascent);
                }
            }

            TextWrap::Wrap => {
                let mut x = 0.0;
                let mut y = line.ascent;

                for (_, g, a, b) in self.char_positions() {
                    if b - x > width {
                        x = a;
                        y += height;
                    }

                    f(g, a - x, b - x, y);
                }
            }

            TextWrap::WordWrap => {
                let mut wrapper = WordWrapper {
                    x: 0.0,
                    y: line.ascent,
                    final_x: 0.0,
                    final_y: line.ascent,
                    width: width,
                    height: height,
                    iter: self.char_positions(),
                    f: &mut f,
                };

                while let Some((ch, glyph, a, b)) = wrapper.iter.next() {
                    wrapper.layout_word(glyph, a, a, b, ch.is_alphanumeric());
                }
            }
        }
    }

    /// Measure the size of the text. If a rectangle is supplied and the text wraps,
    /// the layout will stay within the width of the given rectangle.
    pub fn measure(&self, rect: Option<Rectangle>) -> Rectangle {
        let line = self.font.inner.v_metrics(rusttype::Scale {
            x: self.size,
            y: self.size,
        });

        match rect {
            None => {
                let mut w = 0.0;
                self.layout(Rectangle::from_wh(f32::INFINITY, 0.0), |_, _, new_w, _| w = new_w);

                Rectangle::from_wh(w.ceil(), (line.ascent - line.descent).ceil())
            }
            Some(r) => {
                let mut w = 0.0;
                let mut h = line.ascent;
                match self.wrap {
                    TextWrap::NoWrap => self.layout(r, |_, _, new_w, _| w = new_w),
                    TextWrap::Wrap | TextWrap::WordWrap => {
                        w = rect.map_or(0.0, |r| r.width());
                        self.layout(r, |_, _, _, new_h| h = new_h);
                    }
                }

                Rectangle::from_xywh(r.left, r.top, w.ceil(), (h - line.descent).ceil())
            }
        }
    }

    /// Measure the start and end coordinates of some selected glyphs
    pub fn measure_range(&self, from: usize, to: usize, rect: Rectangle) -> ((f32, f32), (f32, f32)) {
        let mut from_result = (0.0, 0.0);
        let mut to_result = (0.0, 0.0);

        let mut index = 0;
        self.layout(rect, |_, begin, end, y| {
            if index == from {
                from_result = (begin, y)
            }
            if index == to {
                to_result = (begin, y)
            }
            if index + 1 == from {
                from_result = (end, y)
            }
            if index + 1 == to {
                to_result = (end, y)
            }
            index += 1;
        });

        (from_result, to_result)
    }

    /// Find out the index of a character where the mouse is.
    pub fn hitdetect(&self, cursor: (f32, f32), rect: Rectangle) -> usize {
        let dist = |(x, y)| x * x + y * y;

        let mut nearest = (dist(cursor), 0);
        let mut index = 0;

        self.layout(rect, |_, begin, end, y| {
            if dist((begin - cursor.0, y - cursor.1)) < nearest.0 {
                nearest.0 = dist((begin - cursor.0, y - cursor.1));
                nearest.1 = index;
            }
            if dist((end - cursor.0, y - cursor.1)) < nearest.0 {
                nearest.0 = dist((end - cursor.0, y - cursor.1));
                nearest.1 = index + 1;
            }

            index += 1;
        });

        nearest.1
    }
}

impl<'a> Text<'a> {
    /// Convert a borrowed to text to owned text.
    pub fn to_owned(&self) -> Text<'static> {
        Text {
            text: Cow::Owned(self.text.clone().into_owned()),
            font: self.font.clone(),
            size: self.size,
            wrap: self.wrap,
            color: self.color,
        }
    }
}