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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
use std::array;
use std::collections::{hash_map, HashMap};
use std::fmt::{self, Debug};
use std::sync::{Arc, Mutex, PoisonError, Weak};

use cosmic_text::{Attrs, AttrsOwned, LayoutGlyph, SwashContent};
use figures::units::{Lp, Px, UPx};
use figures::{FloatConversion, Fraction, Point, Rect, Round, ScreenScale, Size, UPx2D, Zero};
use intentional::Cast;
use smallvec::SmallVec;

use crate::buffer::Buffer;
use crate::pipeline::PreparedCommand;
use crate::sealed::{ShapeSource, TextureSource};
use crate::{
    Assert, CanRenderTo, CollectedTexture, Color, DefaultHasher, DrawableSource, Graphics,
    Kludgine, PreparedGraphic, ProtoGraphics, TextureBlit, TextureCollection, VertexCollection,
};

impl Kludgine {
    /// Returns a mutable reference to the [`cosmic_text::FontSystem`] used when
    /// rendering text.
    pub fn font_system(&mut self) -> &mut cosmic_text::FontSystem {
        &mut self.text.fonts
    }

    /// Rebuilds the font system, invalidating font database caches.
    ///
    /// This function can be invoked after loading fonts into the font database
    /// to ensure that all future text rendering considers the newly loaded
    /// fonts.
    pub fn rebuild_font_system(&mut self) {
        let existing_system = std::mem::replace(
            &mut self.text.fonts,
            cosmic_text::FontSystem::new_with_fonts([]),
        );
        let (locale, db) = existing_system.into_locale_and_db();
        self.text.fonts = cosmic_text::FontSystem::new_with_locale_and_db(locale, db);
    }

    pub(crate) fn update_scratch_buffer(&mut self, text: &str, width: Option<Px>) {
        self.text.update_scratch_buffer(text, self.scale, width);
    }

    /// Sets the font size.
    pub fn set_font_size(&mut self, size: impl figures::ScreenScale<Lp = figures::units::Lp>) {
        self.text
            .set_font_size(figures::ScreenScale::into_lp(size, self.scale), self.scale);
    }

    /// Returns the current font size.
    pub fn font_size(&self) -> figures::units::Lp {
        self.text.font_size
    }

    /// Sets the line height for multi-line layout.
    pub fn set_line_height(&mut self, size: impl figures::ScreenScale<Lp = figures::units::Lp>) {
        self.text
            .set_line_height(figures::ScreenScale::into_lp(size, self.scale), self.scale);
    }

    /// Returns the current line height.
    pub fn line_height(&self) -> figures::units::Lp {
        self.text.line_height
    }

    /// Sets the current font family.
    pub fn set_font_family(&mut self, family: cosmic_text::FamilyOwned) {
        self.text.attrs.family_owned = family;
    }

    /// Returns the current font family.
    pub fn font_family(&self) -> cosmic_text::Family<'_> {
        self.text.attrs.family_owned.as_family()
    }

    /// Sets the current font style.
    pub fn set_font_style(&mut self, style: cosmic_text::Style) {
        self.text.attrs.style = style;
    }

    /// Returns the current font style.
    pub fn font_style(&self) -> cosmic_text::Style {
        self.text.attrs.style
    }

    /// Sets the current font weight.
    pub fn set_font_weight(&mut self, weight: cosmic_text::Weight) {
        self.text.attrs.weight = weight;
    }

    /// Returns the current font weight.
    pub fn font_weight(&self) -> cosmic_text::Weight {
        self.text.attrs.weight
    }

    /// Sets the current text stretching.
    pub fn set_text_stretch(&mut self, width: cosmic_text::Stretch) {
        self.text.attrs.stretch = width;
    }

    /// Returns the current text stretch.
    pub fn text_stretch(&self) -> cosmic_text::Stretch {
        self.text.attrs.stretch
    }

    /// Returns the current text attributes.
    pub fn text_attrs(&self) -> cosmic_text::Attrs<'_> {
        self.text.attrs.as_attrs()
    }

    /// Sets the current text attributes.
    pub fn set_text_attributes(&mut self, attrs: Attrs<'_>) {
        self.text.attrs = AttrsOwned::new(attrs);
    }

    /// Resets all of the text related properties to their default settings.
    pub fn reset_text_attributes(&mut self) {
        self.set_text_attributes(Attrs::new());
        self.text.font_size = DEFAULT_FONT_SIZE;
        self.text.line_height = DEFAULT_LINE_SIZE;
    }
}

pub(crate) struct TextSystem {
    pub fonts: cosmic_text::FontSystem,
    pub swash_cache: cosmic_text::SwashCache,
    pub alpha_text_atlas: TextureCollection,
    pub color_text_atlas: TextureCollection,
    pub scratch: Option<cosmic_text::Buffer>,
    pub font_size: Lp,
    pub line_height: Lp,
    pub attrs: AttrsOwned,
    glyphs: GlyphCache,
}

impl Debug for TextSystem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TextSystem")
            .field("font_size", &self.font_size)
            .field("line_height", &self.line_height)
            .field("attrs", &self.attrs)
            .field("glyphs", &self.glyphs)
            .finish_non_exhaustive()
    }
}

const DEFAULT_FONT_SIZE: Lp = Lp::points(12);
const DEFAULT_LINE_SIZE: Lp = Lp::points(16);

impl TextSystem {
    pub(crate) fn new(graphics: &ProtoGraphics<'_>) -> Self {
        let fonts = cosmic_text::FontSystem::new();

        Self {
            alpha_text_atlas: TextureCollection::new_generic(
                Size::new(512, 512).cast(),
                wgpu::TextureFormat::R8Unorm,
                wgpu::FilterMode::Linear,
                graphics,
            ),
            color_text_atlas: TextureCollection::new_generic(
                Size::new(512, 512).cast(),
                wgpu::TextureFormat::Rgba8UnormSrgb,
                wgpu::FilterMode::Linear,
                graphics,
            ),
            swash_cache: cosmic_text::SwashCache::new(),
            scratch: None,
            fonts,
            font_size: DEFAULT_FONT_SIZE,
            line_height: DEFAULT_LINE_SIZE,
            glyphs: GlyphCache::default(),
            attrs: AttrsOwned::new(Attrs::new()),
        }
    }

    pub fn new_frame(&mut self) {
        self.glyphs.clear_unused();
    }

    fn metrics(&self, scale: Fraction) -> cosmic_text::Metrics {
        let font_size = self.font_size.into_px(scale);
        let line_height = self.line_height.into_px(scale);

        cosmic_text::Metrics::new(font_size.into(), line_height.into())
    }

    pub fn set_font_size(&mut self, size: Lp, scale: Fraction) {
        self.font_size = size;
        self.update_buffer_metrics(scale);
    }

    pub fn set_line_height(&mut self, size: Lp, scale: Fraction) {
        self.line_height = size;
        self.update_buffer_metrics(scale);
    }

    pub fn scale_changed(&mut self, scale: Fraction) {
        self.update_buffer_metrics(scale);
    }

    fn update_buffer_metrics(&mut self, scale: Fraction) {
        let metrics = self.metrics(scale);
        if let Some(buffer) = &mut self.scratch {
            buffer.set_metrics(&mut self.fonts, metrics);
        }
    }

    pub fn update_scratch_buffer(&mut self, text: &str, scale: Fraction, width: Option<Px>) {
        if self.scratch.is_none() {
            let metrics = self.metrics(scale);
            let buffer = cosmic_text::Buffer::new(&mut self.fonts, metrics);
            self.scratch = Some(buffer);
        }

        let scratch = self.scratch.as_mut().expect("initialized above");
        scratch.set_text(
            &mut self.fonts,
            text,
            self.attrs.as_attrs(),
            cosmic_text::Shaping::Advanced, // TODO maybe this should be configurable?
        );
        scratch.set_size(
            &mut self.fonts,
            width.map_or(f32::MAX, Cast::cast),
            f32::MAX,
        );
        scratch.shape_until_scroll(&mut self.fonts, false);
    }
}

#[derive(Debug, Default, Clone)]
struct GlyphCache {
    glyphs: Arc<Mutex<HashMap<cosmic_text::CacheKey, CachedGlyph, DefaultHasher>>>,
}

impl GlyphCache {
    fn get_or_insert(
        &self,
        key: cosmic_text::CacheKey,
        insert_fn: impl FnOnce() -> Option<(CollectedTexture, bool)>,
    ) -> Option<CachedGlyphHandle> {
        let mut data = self.glyphs.lock().unwrap_or_else(PoisonError::into_inner);
        let cached = match data.entry(key) {
            hash_map::Entry::Occupied(cached) => {
                let cached = cached.into_mut();
                cached.ref_count += 1;
                cached
            }
            hash_map::Entry::Vacant(vacant) => {
                let (texture, is_mask) = insert_fn()?;
                vacant.insert(CachedGlyph {
                    texture,
                    is_mask,
                    ref_count: 1,
                })
            }
        };
        Some(CachedGlyphHandle {
            key,
            is_mask: cached.is_mask,
            cache: Arc::downgrade(&self.glyphs),
            texture: cached.texture.clone(),
        })
    }

    fn clear_unused(&mut self) {
        let mut data = self.glyphs.lock().unwrap_or_else(PoisonError::into_inner);
        data.retain(|_, glyph| glyph.ref_count > 0);
    }
}

#[derive(Debug)]
struct CachedGlyph {
    texture: CollectedTexture,
    is_mask: bool,
    ref_count: usize,
}

pub(crate) struct CachedGlyphHandle {
    key: cosmic_text::CacheKey,
    pub is_mask: bool,
    cache: Weak<Mutex<HashMap<cosmic_text::CacheKey, CachedGlyph, DefaultHasher>>>,
    pub texture: CollectedTexture,
}

impl Debug for CachedGlyphHandle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CachedGlyphHandle")
            .field("key", &self.key)
            .field("is_mask", &self.is_mask)
            .finish_non_exhaustive()
    }
}

impl Clone for CachedGlyphHandle {
    fn clone(&self) -> Self {
        if let Some(glyphs) = self.cache.upgrade() {
            let mut data = glyphs.lock().unwrap_or_else(PoisonError::into_inner);
            let cached = data.get_mut(&self.key).expect("cached glyph missing");
            cached.ref_count += 1;
            drop(data);
        }

        Self {
            key: self.key,
            is_mask: self.is_mask,
            cache: self.cache.clone(),
            texture: self.texture.clone(),
        }
    }
}

impl Drop for CachedGlyphHandle {
    fn drop(&mut self) {
        if let Some(glyphs) = self.cache.upgrade() {
            let mut data = glyphs.lock().unwrap_or_else(PoisonError::into_inner);
            let cached = data.get_mut(&self.key).expect("cached glyph missing");
            cached.ref_count -= 1;
        }
    }
}

impl<'gfx> Graphics<'gfx> {
    /// Prepares the text layout contained in `buffer` to be rendered.
    ///
    /// When the text in `buffer` has no color defined, `default_color` will be
    /// used.
    ///
    /// `origin` allows controlling how the text will be drawn relative to the
    /// coordinate provided in [`render()`](PreparedGraphic::render).
    pub fn prepare_text(
        &mut self,
        buffer: &cosmic_text::Buffer,
        default_color: Color,
        origin: TextOrigin<Px>,
    ) -> PreparedText {
        let mut glyphs = HashMap::default();
        let mut vertices = VertexCollection::default();
        let mut indices = Vec::new();
        let mut commands = SmallVec::<[PreparedCommand; 2]>::new();

        map_each_glyph(
            Some(buffer),
            default_color,
            origin,
            self.kludgine,
            self.device,
            self.queue,
            &mut glyphs,
            |blit, _glyph, _is_first_line, _baseline, _line_w, kludgine| {
                if let GlyphBlit::Visible {
                    blit,
                    glyph: cached,
                } = blit
                {
                    let corners: [u32; 4] =
                        array::from_fn(|index| vertices.get_or_insert(blit.verticies[index]));
                    let start_index = u32::try_from(indices.len()).assert("too many drawn indices");
                    for &index in blit.indices() {
                        indices
                            .push(corners[usize::try_from(index).assert("too many drawn indices")]);
                    }
                    let end_index = u32::try_from(indices.len()).assert("too many drawn indices");
                    match commands.last_mut() {
                        Some(last_command) if last_command.is_mask == cached.is_mask => {
                            // The last command was from the same texture source, we can stend the previous range to the new end.
                            last_command.indices.end = end_index;
                        }
                        _ => {
                            commands.push(PreparedCommand {
                                indices: start_index..end_index,
                                is_mask: cached.is_mask,
                                binding: Some(cached.texture.bind_group(&ProtoGraphics::new(
                                    self.device,
                                    self.queue,
                                    kludgine,
                                ))),
                            });
                        }
                    }
                }
            },
        );

        PreparedText {
            graphic: PreparedGraphic {
                vertices: Buffer::new(&vertices.vertices, wgpu::BufferUsages::VERTEX, self.device),
                indices: Buffer::new(&indices, wgpu::BufferUsages::INDEX, self.device),
                commands,
            },
            _glyphs: glyphs,
        }
    }
}

#[allow(clippy::too_many_lines)]
#[allow(clippy::too_many_arguments)]
pub(crate) fn map_each_glyph(
    buffer: Option<&cosmic_text::Buffer>,
    default_color: Color,
    origin: TextOrigin<Px>,
    kludgine: &mut Kludgine,
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    glyphs: &mut HashMap<cosmic_text::CacheKey, CachedGlyphHandle, DefaultHasher>,
    mut map: impl for<'a> FnMut(GlyphBlit, &'a LayoutGlyph, usize, Px, Px, &'a Kludgine),
) {
    let metrics = buffer
        .unwrap_or_else(|| kludgine.text.scratch.as_ref().expect("no buffer"))
        .metrics();

    let line_height_offset = Point::new(Px::ZERO, Px::from(metrics.line_height));
    let relative_to = match origin {
        TextOrigin::Custom(point) => point,
        TextOrigin::TopLeft => Point::default(),
        TextOrigin::Center => {
            let measured =
                measure_text::<Px, false>(buffer, default_color, kludgine, device, queue, glyphs);
            (Point::from(measured.size) / 2).round()
        }
        TextOrigin::FirstBaseline => line_height_offset.cast(),
    } + line_height_offset;

    let buffer = buffer.unwrap_or_else(|| kludgine.text.scratch.as_ref().expect("no buffer"));
    for run in buffer.layout_runs() {
        let run_origin = Point::new(Px::ZERO, Px::from(run.line_y)) - relative_to;
        for glyph in run.glyphs {
            let physical =
                glyph.physical((run_origin.x.into_float(), run_origin.y.into_float()), 1.);
            let Some(image) = kludgine
                .text
                .swash_cache
                .get_image(&mut kludgine.text.fonts, physical.cache_key)
            else {
                continue;
            };
            let invisible = image.placement.width == 0 || image.placement.height == 0;

            let mut color = glyph.color_opt.map_or(default_color, Color::from);

            let cached = if invisible {
                None
            } else {
                kludgine
                    .text
                    .glyphs
                    .get_or_insert(physical.cache_key, || match image.content {
                        SwashContent::Mask => Some((
                            kludgine.text.alpha_text_atlas.push_texture_generic(
                                &image.data,
                                wgpu::ImageDataLayout {
                                    offset: 0,
                                    bytes_per_row: Some(image.placement.width),
                                    rows_per_image: None,
                                },
                                Size::upx(image.placement.width, image.placement.height).cast(),
                                &ProtoGraphics {
                                    id: kludgine.id,
                                    device,
                                    queue,
                                    binding_layout: &kludgine.binding_layout,
                                    linear_sampler: &kludgine.linear_sampler,
                                    nearest_sampler: &kludgine.nearest_sampler,
                                    uniforms: &kludgine.uniforms.wgpu,
                                },
                            ),
                            true,
                        )),
                        SwashContent::Color => {
                            // Set the color to full white to avoid mixing.
                            color = Color::WHITE;
                            Some((
                                kludgine.text.color_text_atlas.push_texture_generic(
                                    &image.data,
                                    wgpu::ImageDataLayout {
                                        offset: 0,
                                        bytes_per_row: Some(image.placement.width * 4),
                                        rows_per_image: None,
                                    },
                                    Size::upx(image.placement.width, image.placement.height).cast(),
                                    &ProtoGraphics {
                                        id: kludgine.id,
                                        device,
                                        queue,
                                        binding_layout: &kludgine.binding_layout,
                                        linear_sampler: &kludgine.linear_sampler,
                                        nearest_sampler: &kludgine.nearest_sampler,
                                        uniforms: &kludgine.uniforms.wgpu,
                                    },
                                ),
                                false,
                            ))
                        }
                        SwashContent::SubpixelMask => None,
                    })
            };

            let blit = if let Some(cached) = cached {
                glyphs
                    .entry(physical.cache_key)
                    .or_insert_with(|| cached.clone());

                GlyphBlit::Visible {
                    blit: TextureBlit::new(
                        cached.texture.region,
                        Rect::new(
                            (Point::new(physical.x, physical.y)).cast::<Px>()
                                + Point::new(
                                    image.placement.left,
                                    metrics.line_height.cast::<i32>() - image.placement.top,
                                ),
                            Size::new(
                                i32::try_from(image.placement.width)
                                    .expect("width out of range of i32"),
                                i32::try_from(image.placement.height)
                                    .expect("height out of range of i32"),
                            )
                            .cast(),
                        ),
                        color,
                    ),
                    glyph: cached.clone(),
                }
            } else {
                GlyphBlit::Invisible {
                    location: Point::new(physical.x, physical.y).cast::<Px>(),
                    width: glyph.w.cast(),
                }
            };
            map(
                blit,
                glyph,
                (run.line_top / metrics.line_height).round().cast::<usize>(),
                relative_to.y,
                Px::from(run.line_w.ceil()),
                kludgine,
            );
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) enum GlyphBlit {
    Invisible {
        location: Point<Px>,
        width: Px,
    },
    Visible {
        blit: TextureBlit<Px>,
        glyph: CachedGlyphHandle,
    },
}

impl GlyphBlit {
    pub fn top_left(&self) -> Point<Px> {
        match self {
            GlyphBlit::Invisible { location, .. } => *location,
            GlyphBlit::Visible { blit, .. } => blit.top_left().location,
        }
    }

    pub fn bottom_right(&self, bottom: Px) -> Point<Px> {
        match self {
            GlyphBlit::Invisible { location, width } => Point::new(location.x + *width, bottom),
            GlyphBlit::Visible { blit, .. } => blit.bottom_right().location,
        }
    }
}

impl CanRenderTo for GlyphBlit {
    fn can_render_to(&self, kludgine: &Kludgine) -> bool {
        match self {
            GlyphBlit::Invisible { .. } => true,
            GlyphBlit::Visible { glyph, .. } => glyph.texture.can_render_to(kludgine),
        }
    }
}

pub(crate) fn measure_text<Unit, const COLLECT_GLYPHS: bool>(
    buffer: Option<&cosmic_text::Buffer>,
    color: Color,
    kludgine: &mut Kludgine,
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    glyphs: &mut HashMap<cosmic_text::CacheKey, CachedGlyphHandle, DefaultHasher>,
) -> MeasuredText<Unit>
where
    Unit: figures::ScreenUnit,
{
    // TODO the returned type should be able to be drawn, so that we don't have to call update_scratch_buffer again.
    let line_height = Unit::from_lp(kludgine.text.line_height, kludgine.scale);
    let mut min = Point::new(Px::MAX, Px::MAX);
    let mut first_line_max_y = Px::MIN;
    let mut last_baseline = Px::MIN;
    let mut max = Point::new(Px::MIN, Px::MIN);
    let mut measured_glyphs = Vec::new();
    map_each_glyph(
        buffer,
        color,
        TextOrigin::TopLeft,
        kludgine,
        device,
        queue,
        glyphs,
        |blit, glyph, line_index, baseline, line_width, _kludgine| {
            last_baseline = last_baseline.max(baseline);
            min = min.min(blit.top_left());
            max.x = max.x.max(line_width);
            max.y = max.y.max(blit.bottom_right(baseline).y);
            if line_index == 0 {
                first_line_max_y = first_line_max_y.max(blit.bottom_right(baseline).y);
            }
            if COLLECT_GLYPHS {
                measured_glyphs.push(MeasuredGlyph {
                    blit,
                    info: GlyphInfo::new(glyph, line_index, line_width),
                });
            }
        },
    );

    if min == Point::new(Px::MAX, Px::MAX) {
        MeasuredText {
            ascent: Unit::default(),
            descent: Unit::default(),
            left: Unit::default(),
            line_height,
            size: Size::new(Unit::default(), line_height),
            glyphs: Vec::new(),
        }
    } else {
        if first_line_max_y == Px::MIN {
            first_line_max_y = line_height.into_px(kludgine.scale);
        }

        MeasuredText {
            ascent: line_height - Unit::from_px(min.y, kludgine.scale),
            descent: line_height - Unit::from_px(first_line_max_y, kludgine.scale),
            left: Unit::from_px(min.x, kludgine.scale),
            size: Size {
                width: Unit::from_px(max.x, kludgine.scale),
                height: Unit::from_px(max.y.max(last_baseline), kludgine.scale).max(line_height),
            },
            line_height,
            glyphs: measured_glyphs,
        }
    }
}

/// Text that is ready to be rendered on the GPU.
pub struct PreparedText {
    graphic: PreparedGraphic<Px>,
    _glyphs: HashMap<cosmic_text::CacheKey, CachedGlyphHandle, DefaultHasher>,
}

impl fmt::Debug for PreparedText {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.graphic.fmt(f)
    }
}

impl std::ops::Deref for PreparedText {
    type Target = PreparedGraphic<Px>;

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

impl std::ops::DerefMut for PreparedText {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.graphic
    }
}

/// Controls the origin of [`PreparedText`].
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
pub enum TextOrigin<Unit> {
    /// Render the text such that the top-left of the first line appears at the
    /// rendered location. When rotated, the text will rotate around the
    /// top-left of the text.
    #[default]
    TopLeft,
    /// Render the text such that the center of the extents of the rendered text
    /// appears at the rendered location. When rotated, the text will rotate
    /// around the geometric center of the rendered text.
    Center,
    /// Render the text such that the leftmost pixel of the baseline of the
    /// first line of text appears at the rendered location. When rotated, the
    /// text will rotate around this point.
    FirstBaseline,
    /// Render the text such that the text is offset by a custom amount. When
    /// rotated, the text will rotate around this point.
    Custom(Point<Unit>),
}

impl<Unit> ScreenScale for TextOrigin<Unit>
where
    Unit: ScreenScale<Px = Px, Lp = Lp, UPx = UPx>,
{
    type Lp = TextOrigin<Unit::Lp>;
    type Px = TextOrigin<Unit::Px>;
    type UPx = TextOrigin<Unit::UPx>;

    fn into_px(self, scale: Fraction) -> Self::Px {
        match self {
            TextOrigin::TopLeft => TextOrigin::TopLeft,
            TextOrigin::Center => TextOrigin::Center,
            TextOrigin::FirstBaseline => TextOrigin::FirstBaseline,
            TextOrigin::Custom(pt) => TextOrigin::Custom(pt.into_px(scale)),
        }
    }

    fn from_px(px: Self::Px, scale: Fraction) -> Self {
        match px {
            TextOrigin::TopLeft => TextOrigin::TopLeft,
            TextOrigin::Center => TextOrigin::Center,
            TextOrigin::FirstBaseline => TextOrigin::FirstBaseline,
            TextOrigin::Custom(pt) => TextOrigin::Custom(Point::from_px(pt, scale)),
        }
    }

    fn into_lp(self, scale: Fraction) -> Self::Lp {
        match self {
            TextOrigin::TopLeft => TextOrigin::TopLeft,
            TextOrigin::Center => TextOrigin::Center,
            TextOrigin::FirstBaseline => TextOrigin::FirstBaseline,
            TextOrigin::Custom(pt) => TextOrigin::Custom(pt.into_lp(scale)),
        }
    }

    fn from_lp(dips: Self::Lp, scale: Fraction) -> Self {
        match dips {
            TextOrigin::TopLeft => TextOrigin::TopLeft,
            TextOrigin::Center => TextOrigin::Center,
            TextOrigin::FirstBaseline => TextOrigin::FirstBaseline,
            TextOrigin::Custom(pt) => TextOrigin::Custom(Point::from_lp(pt, scale)),
        }
    }

    fn into_upx(self, scale: Fraction) -> Self::UPx {
        match self {
            TextOrigin::TopLeft => TextOrigin::TopLeft,
            TextOrigin::Center => TextOrigin::Center,
            TextOrigin::FirstBaseline => TextOrigin::FirstBaseline,
            TextOrigin::Custom(pt) => TextOrigin::Custom(pt.into_upx(scale)),
        }
    }

    fn from_upx(px: Self::UPx, scale: Fraction) -> Self {
        match px {
            TextOrigin::TopLeft => TextOrigin::TopLeft,
            TextOrigin::Center => TextOrigin::Center,
            TextOrigin::FirstBaseline => TextOrigin::FirstBaseline,
            TextOrigin::Custom(px) => TextOrigin::Custom(Point::from_upx(px, scale)),
        }
    }
}

/// The dimensions of a measured text block.
#[derive(Debug, Clone)]
pub struct MeasuredText<Unit> {
    /// The measurement above the baseline of the text.
    pub ascent: Unit,
    /// The measurement below the baseline of the text.
    pub descent: Unit,
    /// The measurement to the leftmost pixel of the text.
    pub left: Unit,
    /// The measurement above the baseline of the text.
    pub line_height: Unit,
    /// The total size of the measured text, encompassing all lines.
    pub size: Size<Unit>,
    /// The individual glyhs that were laid out.
    pub glyphs: Vec<MeasuredGlyph>,
}

impl<Unit> CanRenderTo for MeasuredText<Unit> {
    fn can_render_to(&self, kludgine: &Kludgine) -> bool {
        self.glyphs
            .first()
            .map_or(true, |glyph| glyph.can_render_to(kludgine))
    }
}

impl<Unit> DrawableSource for MeasuredText<Unit> {}

/// Instructions for drawing a laid out glyph.
#[derive(Clone)]
pub struct MeasuredGlyph {
    pub(crate) blit: GlyphBlit,
    /// Information about what glyph this is.
    pub info: GlyphInfo,
}

impl Debug for MeasuredGlyph {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MeasuredGlyph")
            .field("blit", &self.blit)
            .field("info", &self.info)
            .finish_non_exhaustive()
    }
}

impl MeasuredGlyph {
    /// Returns the destination rectangle for this glyph.
    #[must_use]
    pub fn rect(&self) -> Rect<Px> {
        let top_left = self.blit.top_left();
        Rect::from_extents(top_left, self.blit.bottom_right(top_left.y))
    }

    /// Returns true if this measurement is for a visible glyph, as opposed to
    /// whitespace or padding.
    #[must_use]
    pub const fn visible(&self) -> bool {
        matches!(self.blit, GlyphBlit::Visible { .. })
    }
}

impl CanRenderTo for MeasuredGlyph {
    fn can_render_to(&self, kludgine: &Kludgine) -> bool {
        self.blit.can_render_to(kludgine)
    }
}

/// Information about a glyph in a [`MeasuredText`].
#[derive(Debug, Clone, Copy)]
pub struct GlyphInfo {
    /// Start index of cluster in original line
    pub start: usize,
    /// End index of cluster in original line
    pub end: usize,
    /// The line index this glyph is visually laid out on.
    pub line: usize,
    /// The width of the line this glyph is on.
    ///
    /// Because whitespace does not have glyphs, this width may be useful in
    /// measuring whitespace at the end of a line.
    pub line_width: Px,
    /// Unicode `BiDi` embedding level, character is left-to-right if `level` is divisible by 2
    pub level: unicode_bidi::Level,
    /// Custom metadata set in [`cosmic_text::Attrs`].
    pub metadata: usize,
}

impl GlyphInfo {
    fn new(glyph: &LayoutGlyph, line: usize, line_width: Px) -> Self {
        Self {
            start: glyph.start,
            end: glyph.end,
            line,
            line_width,
            metadata: glyph.metadata,
            level: glyph.level,
        }
    }
}

/// A text drawing command.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct Text<'a, Unit> {
    /// The text to be drawn.
    pub text: &'a str,
    /// The color to draw the text using.
    pub color: Color,
    /// The origin to draw the text around.
    pub origin: TextOrigin<Unit>,
    /// The width to wrap the text at. If `None`, no wrapping is performed.
    pub wrap_at: Option<Unit>,
}

impl<'a, Unit> Text<'a, Unit> {
    /// Returns a text command that draws `text` with `color`.
    #[must_use]
    pub const fn new(text: &'a str, color: Color) -> Self {
        Self {
            text,
            color,
            origin: TextOrigin::TopLeft,
            wrap_at: None,
        }
    }

    /// Sets the origin for the text drawing operation and returns self.
    #[must_use]
    pub fn origin(mut self, origin: TextOrigin<Unit>) -> Self {
        self.origin = origin;
        self
    }

    /// Sets the width to wrap text at and returns self.
    #[must_use]
    pub fn wrap_at(mut self, width: Unit) -> Self {
        self.wrap_at = Some(width);
        self
    }
}

impl<'a, Unit> From<&'a str> for Text<'a, Unit> {
    fn from(value: &'a str) -> Self {
        Self::new(value, Color::WHITE)
    }
}

impl<'a, Unit> From<&'a String> for Text<'a, Unit> {
    fn from(value: &'a String) -> Self {
        Self::new(value, Color::WHITE)
    }
}

impl<'a, Unit> DrawableSource for Text<'a, Unit> {}