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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use std::collections::HashMap;

#[cfg(feature = "graphics")]
use graphics::{
    character::CharacterCache, math::Matrix2d, text as draw_text, Graphics, ImageSize, Transformed,
};
use rusttype::{Error, Font, GlyphId, Scale};

use math::{Rectangle, Scalar, Vector2, ZeroOneTwo};
use Color;

/// A horizantal text justification
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Justification {
    /// Align on the left
    Left,
    /// Center align
    Centered,
    /// Align on the right
    Right,
}

/// Lines that have starting positions
///
/// `V` usually implements `Vector2`
pub type PositionedLines<V> = Vec<(V, String)>;

/// A way of resizing text in a rectangle
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Resize {
    /// Make the text no larger than its original font size,
    /// but still try to fit it in the rectangle
    NoLarger,
    /// Make the text as large as possible while still
    /// fitting in the rectangle
    Max,
    /// Do not resize the text
    None,
}

/// A format for some text
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct TextFormat<S>
where
    S: Scalar,
{
    /// The font size
    pub font_size: u32,
    /// The horizantal justification
    pub just: Justification,
    /// The spacing between lines. This should usually be somewhere
    /// between `1.0` and `2.0`, but any scalar is valid
    pub line_spacing: S,
    /// The number of spaces to indent the first line of a paragraph
    pub first_line_indent: usize,
    /// The number of spaces to indent all lines of a paragraph
    /// after the first
    pub lines_indent: usize,
    /// The color of the text
    pub color: Color,
    /// The resize strategy
    pub resize: Resize,
}

impl<S> TextFormat<S>
where
    S: Scalar,
{
    /// Create a default `TextFormat` with the given font size
    pub fn new(font_size: u32) -> TextFormat<S> {
        TextFormat {
            font_size,
            just: Justification::Left,
            line_spacing: S::ONE,
            first_line_indent: 0,
            lines_indent: 0,
            color: [0.0, 0.0, 0.0, 1.0],
            resize: Resize::NoLarger,
        }
    }
    /// Align the `TextFormat` to the left
    pub fn left(mut self) -> Self {
        self.just = Justification::Left;
        self
    }
    /// Center-align the `TextFormat`
    pub fn centered(mut self) -> Self {
        self.just = Justification::Centered;
        self
    }
    /// Align the `TextFormat` to the right
    pub fn right(mut self) -> Self {
        self.just = Justification::Right;
        self
    }
    /// Set the font size
    pub fn font_size(mut self, font_size: u32) -> Self {
        self.font_size = font_size;
        self
    }
    /// Set the line spacing
    pub fn line_spacing(mut self, line_spacing: S) -> Self {
        self.line_spacing = line_spacing;
        self
    }
    /// Changes the type of the line spacing and thus the `TextFormat` itself
    pub fn map_line_spacing<U>(&self) -> TextFormat<U>
    where
        U: Scalar + From<S>,
    {
        TextFormat {
            font_size: self.font_size,
            just: self.just,
            line_spacing: U::from(self.line_spacing),
            first_line_indent: self.first_line_indent,
            lines_indent: self.lines_indent,
            color: self.color,
            resize: self.resize,
        }
    }
    /// Set the indentation of the first line
    pub fn first_line_indent(mut self, first_line_indent: usize) -> Self {
        self.first_line_indent = first_line_indent;
        self
    }
    /// Set the indentation of all lines after the first
    pub fn lines_indent(mut self, lines_indent: usize) -> Self {
        self.lines_indent = lines_indent;
        self
    }
    /// Set the color
    pub fn color(mut self, color: Color) -> Self {
        self.color = color;
        self
    }
    /// Set the resize strategy
    pub fn resize(mut self, resize: Resize) -> Self {
        self.resize = resize;
        self
    }
    /// Change the font size depending on the the resize strategy
    ///
    /// The given max size is not used if the strategy is `Resize::None`
    pub fn resize_font(mut self, max_size: u32) -> Self {
        match self.resize {
            Resize::NoLarger => self.font_size = self.font_size.min(max_size),
            Resize::Max => self.font_size = max_size,
            Resize::None => (),
        }
        self
    }
}

/// Defines behavior of a cache of character widths.
///
/// In general, determining the width of a character glyphs with a given font size
/// is a non-trivial calculation. Caching a width calculation for each characters
/// and font size ensures that the calculation is only done once for each pair.
pub trait CharacterWidthCache {
    /// The scalar type for the width
    type Scalar: Scalar;
    /// Get the width of a character at a font size
    fn char_width(&mut self, character: char, font_size: u32) -> Self::Scalar;
    /// Get the width of a string at a font_size
    fn width(&mut self, text: &str, font_size: u32) -> Self::Scalar {
        text.chars()
            .map(|c| self.char_width(c, font_size))
            .fold(Self::Scalar::ZERO, std::ops::Add::add)
    }
    /// Split a string into a list of lines of text with the given format where no line
    /// is wider than the given max width. Newlines (`\n`) in the string are respected
    fn format_lines(
        &mut self,
        text: &str,
        max_width: Self::Scalar,
        format: TextFormat<Self::Scalar>,
    ) -> Vec<String> {
        let mut sized_lines = Vec::new();
        let mut first_line = false;
        // Iterate through lines
        for line in text.lines() {
            // Initialize a result line
            let mut sized_line = String::new();
            // Apply the indentation
            let indent = (0..if first_line {
                format.first_line_indent
            } else {
                format.lines_indent
            })
                .map(|_| ' ')
                .collect::<String>();
            sized_line.push_str(&indent);
            let mut curr_width = self.width(&indent, format.font_size);
            // Iterate through words
            for word in line.split_whitespace() {
                // Get the word's width
                let width = self.width(word, format.font_size);
                // If the word goes past the max width...
                if !(curr_width + width < max_width || curr_width == Self::Scalar::ZERO) {
                    // Pop off the trailing space
                    sized_line.pop();
                    // Push the result line onto the result list
                    sized_lines.push(sized_line);
                    // Init next line
                    first_line = false;
                    sized_line = String::new();
                    // Apply the indentation
                    let indent = (0..if first_line {
                        format.first_line_indent
                    } else {
                        format.lines_indent
                    })
                        .map(|_| ' ')
                        .collect::<String>();
                    sized_line.push_str(&indent);
                    curr_width = self.width(&indent, format.font_size);
                }
                // Push the word onto the result line
                sized_line.push_str(word);
                sized_line.push(' ');
                curr_width = curr_width + width + self.char_width(' ', format.font_size);
            }
            // Push the result line onto the result list
            sized_line.pop();
            sized_lines.push(sized_line);
            first_line = false;
        }
        sized_lines
    }
    /// Get the width of the widest line after performing
    /// the calculation of `CharacterWidthCache::format_lines`
    fn max_line_width(
        &mut self,
        text: &str,
        max_width: Self::Scalar,
        format: TextFormat<Self::Scalar>,
    ) -> Self::Scalar {
        let lines = self.format_lines(text, max_width, format);
        lines
            .into_iter()
            .map(|line| self.width(&line, format.font_size))
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or(Self::Scalar::ZERO)
    }
    /// Calculate a set of positioned lines of text with the given format
    /// that fit within the given rectangle
    fn justify_text<R>(
        &mut self,
        text: &str,
        rect: R,
        format: TextFormat<Self::Scalar>,
    ) -> PositionedLines<R::Vector>
    where
        R: Rectangle<Scalar = Self::Scalar>,
    {
        self.format_lines(text, rect.width(), format)
            .into_iter()
            .enumerate()
            .map(|(i, line)| {
                let y_offset = rect.top()
                    + format.font_size.into()
                    + Self::Scalar::from(i as u32) * format.font_size.into() * format.line_spacing;
                use self::Justification::*;
                let line_width = self.width(&line, format.font_size);
                let x_offset = match format.just {
                    Left => rect.left(),
                    Centered => rect.center().x() - line_width / Self::Scalar::TWO,
                    Right => rect.right() - line_width,
                };
                (R::Vector::new(x_offset, y_offset), line)
            })
            .collect()
    }
    /// Check if text with the given format fits within a rectangle's width
    fn text_fits_horizontal<R>(
        &mut self,
        text: &str,
        rect: R,
        format: TextFormat<Self::Scalar>,
    ) -> bool
    where
        R: Rectangle<Scalar = Self::Scalar>,
    {
        self.max_line_width(text, rect.width(), format) < rect.width()
    }
    /// Check if text with the given format fits within a rectangle's height
    fn text_fits_vertical<R>(
        &mut self,
        text: &str,
        rect: R,
        format: TextFormat<Self::Scalar>,
    ) -> bool
    where
        R: Rectangle<Scalar = Self::Scalar>,
    {
        let lines = self.format_lines(text, rect.width(), format);
        if lines.is_empty() {
            return true;
        }
        let last_line_y = rect.top()
            + format.font_size.into()
            + Self::Scalar::from((lines.len() - 1) as u32)
                * format.font_size.into()
                * format.line_spacing;
        last_line_y < rect.bottom()
    }
    /// Check if text with the given format fits within a rectangle
    fn text_fits<R>(&mut self, text: &str, rect: R, format: TextFormat<Self::Scalar>) -> bool
    where
        R: Rectangle<Scalar = Self::Scalar>,
    {
        self.text_fits_horizontal(text, rect.clone(), format)
            && self.text_fits_vertical(text, rect, format)
    }
    /// Determine the maximum font size for text with the given format
    /// that will still allow the text to fit within a rectangle
    fn fit_max_font_size<R>(
        &mut self,
        text: &str,
        rect: R,
        mut format: TextFormat<Self::Scalar>,
    ) -> u32
    where
        R: Rectangle<Scalar = Self::Scalar>,
    {
        while !self.text_fits(text, rect.clone(), format) && format.font_size > 1 {
            format.font_size -= 1;
        }
        format.font_size
    }
    /// Determine the minumum height for a rectangle such that text
    /// with the given format will still fit within the rectangle
    ///
    /// The given delta value defines how much to increment the
    /// rectangle's height on each check. Lower deltas will yield
    /// more accurate results, but will take longer to computer.
    fn fit_min_height<R>(
        &mut self,
        text: &str,
        mut rect: R,
        format: TextFormat<Self::Scalar>,
        delta: Self::Scalar,
    ) -> Self::Scalar
    where
        R: Rectangle<Scalar = Self::Scalar>,
    {
        let delta = delta.abs().max(Self::Scalar::ONE);
        while self.text_fits_vertical(text, rect.clone(), format) {
            rect = rect
                .clone()
                .with_size(R::Vector::new(rect.width(), rect.height() - delta))
        }
        while !self.text_fits_vertical(text, rect.clone(), format) {
            rect = rect
                .clone()
                .with_size(R::Vector::new(rect.width(), rect.height() + delta))
        }
        rect.height()
    }
    /// Determine the minumum width for a rectangle such that text
    /// with the given format will still fit within the rectangle
    ///
    /// The given delta value defines how much to increment the
    /// rectangle's width on each check. Lower deltas will yield
    /// more accurate results, but will take longer to computer.
    fn fit_min_width<R>(
        &mut self,
        text: &str,
        mut rect: R,
        format: TextFormat<Self::Scalar>,
        delta: Self::Scalar,
    ) -> Self::Scalar
    where
        R: Rectangle<Scalar = Self::Scalar>,
    {
        let delta = delta.abs().max(Self::Scalar::ONE);
        while self.text_fits(text, rect.clone(), format) {
            rect = rect
                .clone()
                .with_size(R::Vector::new(rect.width() - delta, rect.height()))
        }
        while !self.text_fits(text, rect.clone(), format) {
            rect = rect
                .clone()
                .with_size(R::Vector::new(rect.width() + delta, rect.height()))
        }
        rect.width()
    }
}

/// A basic implememntor for `CharacterWidthCache`
#[derive(Clone)]
pub struct Glyphs<'f, S = f64>
where
    S: Scalar,
{
    widths: HashMap<(u32, char), S>,
    font: Font<'f>,
}

impl<'f, S> Glyphs<'f, S>
where
    S: Scalar,
{
    /// Loads a `Glyphs` from an array of font data.
    pub fn from_bytes(bytes: &'f [u8]) -> Result<Glyphs<'f, S>, Error> {
        Ok(Glyphs {
            widths: HashMap::new(),
            font: Font::from_bytes(bytes)?,
        })
    }
    /// Loads a `Glyphs` from a `Font`.
    pub fn from_font(font: Font<'f>) -> Glyphs<'f, S> {
        Glyphs {
            widths: HashMap::new(),
            font,
        }
    }
}

impl<'f, S> CharacterWidthCache for Glyphs<'f, S>
where
    S: Scalar,
{
    type Scalar = S;
    fn char_width(&mut self, character: char, font_size: u32) -> Self::Scalar {
        let font = &self.font;
        *self
            .widths
            .entry((font_size, character))
            .or_insert_with(|| {
                let scale = Scale::uniform(font_size as f32);
                let glyph = font.glyph(character).scaled(scale);
                let glyph = if glyph.id() == GlyphId(0) && glyph.shape().is_none() {
                    font.glyph('\u{FFFD}').scaled(scale)
                } else {
                    glyph
                };
                let h_metrics = glyph.h_metrics();

                h_metrics.advance_width.into()
            })
    }
}

#[cfg(feature = "graphics")]
impl<C> CharacterWidthCache for C
where
    C: CharacterCache,
{
    type Scalar = f64;
    fn char_width(&mut self, character: char, font_size: u32) -> Self::Scalar {
        if let Ok(texture) = <Self as CharacterCache>::character(self, font_size, character) {
            texture.size.x()
        } else {
            panic!("CharacterWidthCache::character returned Err")
        }
    }
}

/// Draw justified text to something using the `piston2d-graphics` crate
///
/// Text will be drawn in the given rectangle and use the given format
#[cfg(feature = "graphics")]
pub fn justified_text<U, R, T, C, G>(
    text: &str,
    rect: R,
    format: TextFormat<U>,
    glyphs: &mut C,
    transform: Matrix2d,
    graphics: &mut G,
) -> Result<(), C::Error>
where
    U: Scalar,
    f64: From<U>,
    R: Rectangle<Scalar = f64>,
    T: ImageSize,
    C: CharacterCache<Texture = T>,
    G: Graphics<Texture = T>,
{
    for (pos, line) in glyphs.justify_text(text, rect, format.map_line_spacing::<f64>()) {
        draw_text(
            format.color,
            format.font_size,
            &line,
            glyphs,
            transform.trans(pos.x(), pos.y()),
            graphics,
        )?;
    }
    Ok(())
}