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
//! Contains the Text Struct used for text widgets and the TextRender which is needed for rendering
//! Text widgets
//!
//! How Text Rendering Works
//! ---
//! Text rendering works diffrently compared to standard rendering. One of the main diffrences is that
//! you do not need to set the text buffer every single frame unlike [Material](../material/struct.Material.html)
//! buffers.
//!  
//! Example:
//! ```rust,no_run
//! fn main() {
//!     // assuming you've made a engine handle already
//!     let mut text_handle = TextRenderer::new(&engine);
//!     let text_mat = TextMaterial::new("Hello World", Colour::RED, 100.0, 100.0, &mut text_render, &engine);
//!     
//!     // construct any struct that implements Game
//!     let game = UserStruct::new(text, text_buffer);
//!
//!     egnine.run(game);
//! }
//!
//! impl Game for UserStruct {
//!     fn render<'pass, 'others>(&'others mut self, mut render_handle: RenderInformation<'pass, 'others>) where 'others: 'pass {
//!         // notice how you dont need to set the text every frame, and works the same as a Material
//!         self.text_mat.add_instance(Vec2{x: 0.0, y: 0.0}, Colour::WHITE, &render_handle);
//!
//!         self.text_mat.draw(&mut self.text_handle, &mut render_handle);
//!     }
//!
//!     fn update(&mut self, engine_handle: &mut Engine) {
//!         // nothing, you dont need to update the text buffer every frame
//!     }
//! }
//!
//! ```
use std::path::Path;
use std::sync::Arc;

use crate::glyphon::fontdb::Source;
use crate::glyphon::{
    self, Attrs, Family, FontSystem, Metrics, Shaping, SwashCache, TextArea, TextAtlas, TextBounds,
};

use crate::colour::Colour;
use crate::engine_handle::{Engine, WgpuClump};
use crate::layouts;
use crate::material::{self, Material};
use crate::matrix_math::normalize_points;
use crate::render::RenderInformation;
use crate::resource::{self, InProgressResource, ResourceId, ResourceManager, ResourceType};
use crate::vectors::Vec2;
use crate::vertex::{self, Vertex};

/// Stores Important information nessicary to rendering text. Only on of these should
/// be created per application.
pub(crate) struct TextRenderer {
    pub(crate) font_system: FontSystem,
    cache: SwashCache,
    atlas: TextAtlas,
    text_renderer: glyphon::TextRenderer,
    defualt_font_name: String,
}

impl TextRenderer {
    pub fn new(wgpu: &WgpuClump) -> Self {
        let mut font_system = FontSystem::new();
        let defualt_font_id = font_system
            .db_mut()
            .load_font_source(Source::Binary(Arc::new(include_bytes!("Monocraft.ttf"))))[0];

        let defualt_font_name = font_system.db().face(defualt_font_id).unwrap().families[0]
            .0
            .clone();
        // its expensive but whatever

        let cache = SwashCache::new();
        let mut atlas = TextAtlas::new(
            &wgpu.device,
            &wgpu.queue,
            wgpu::TextureFormat::Rgba8UnormSrgb,
        );
        let text_renderer = glyphon::TextRenderer::new(
            &mut atlas,
            &wgpu.device,
            wgpu::MultisampleState::default(),
            None,
        );

        Self {
            font_system,
            cache,
            atlas,
            text_renderer,
            defualt_font_name,
        }
    }

    pub fn get_defualt_font_name(&self) -> &str {
        &self.defualt_font_name
    }

    /// Loads in a font from a byte array
    pub fn load_font_from_bytes(&mut self, data: &[u8]) -> Font {
        let id = self
            .font_system
            .db_mut()
            .load_font_source(Source::Binary(Arc::new(data.to_vec())))[0];
        // I am lying to the user here

        let name = self.font_system.db().face(id).unwrap().families[0]
            .0
            .clone();

        Font { name }
    }
}

/// This struct represents a piece of text. You only need to create
/// one peice of text per string you would like to draw
/// as you can draw multpiple instances easily.
pub struct TextMaterial {
    size: Vec2<u32>,
    font_size: f32,
    line_height: f32,
    bounds: Vec2<Vec2<i32>>,
    text_buffer: glyphon::Buffer,
    vertex_buffer: wgpu::Buffer,
    index_buffer: wgpu::Buffer,
    texture: wgpu::Texture,
    texture_view: wgpu::TextureView,
    bind_group: wgpu::BindGroup,
    vertex_count: u64,
    index_count: u64,
    change_flag: bool,
}

impl TextMaterial {
    pub fn new(
        text: &str,
        colour: Colour,
        font_size: f32,
        line_height: f32,
        engine: &mut Engine,
    ) -> Self {
        let window_size = engine.get_window_size();
        let scale_factor = engine.get_window_scale_factor();
        let font_info = FontInformation::new(engine);

        let mut text_buffer = glyphon::Buffer::new(
            &mut font_info.text_handle.font_system,
            Metrics::new(font_size, line_height),
        );
        let phyisical_width = (window_size.x as f64 * scale_factor) as f32;
        let phyiscal_hieght = (window_size.y as f64 * scale_factor) as f32;

        text_buffer.set_size(
            &mut font_info.text_handle.font_system,
            phyisical_width,
            phyiscal_hieght,
        );
        text_buffer.set_text(
            &mut font_info.text_handle.font_system,
            text,
            Attrs::new()
                .color(colour.into())
                .family(Family::Name(&font_info.text_handle.defualt_font_name)),
            Shaping::Basic,
        );

        let hieght = text_buffer.lines.len() as f32 * text_buffer.metrics().line_height;
        let run_width = text_buffer
            .layout_runs()
            .map(|run| run.line_w)
            .max_by(f32::total_cmp)
            .unwrap_or(0.0);

        let texture = font_info
            .wgpu
            .device
            .create_texture(&wgpu::TextureDescriptor {
                label: Some("Text Texture"),
                size: wgpu::Extent3d {
                    width: hieght.ceil() as u32,
                    height: run_width.ceil() as u32,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: wgpu::TextureFormat::Rgba8UnormSrgb,
                usage: wgpu::TextureUsages::RENDER_ATTACHMENT
                    | wgpu::TextureUsages::TEXTURE_BINDING,
                view_formats: &[],
            });

        let bounds = Vec2 {
            x: Vec2 { x: 0, y: 0 },
            y: Vec2 {
                x: i32::MAX,
                y: i32::MAX,
            },
        };
        let texture_size = Vec2 {
            x: run_width.ceil() as u32,
            y: hieght.ceil() as u32,
        };
        let texture_view = texture.create_view(&Default::default());

        render_text_to_texture(
            font_info.text_handle,
            &text_buffer,
            bounds,
            texture_size,
            &texture_view,
            font_info.wgpu,
        );
        let vertex_size = std::mem::size_of::<Vertex>() as u64;

        let (vertex_buffer, index_buffer) =
            Material::create_buffers(&engine.wgpu_clump.device, vertex_size, 8, 2, 12);

        let bind_group = engine
            .wgpu_clump
            .device
            .create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Text Widget BindGroup"),
                layout: &layouts::create_texture_layout(&engine.wgpu_clump.device),
                entries: &[
                    wgpu::BindGroupEntry {
                        binding: 0,
                        resource: wgpu::BindingResource::TextureView(&texture_view),
                    },
                    wgpu::BindGroupEntry {
                        binding: 1,
                        resource: wgpu::BindingResource::Sampler(engine.get_texture_sampler()),
                    },
                ],
            });

        Self {
            size: texture_size,
            font_size,
            line_height,
            bounds,
            text_buffer,
            vertex_buffer,
            index_buffer,
            texture_view,
            texture,
            bind_group,
            vertex_count: 0,
            index_count: 0,
            change_flag: false,
        }
    }

    /// Sets the text for the widget, using the defualt font.
    /// This only needs to be done once, not every frame like Materials.
    pub fn set_text(&mut self, text: &str, colour: Colour, engine: &mut Engine) {
        let font_info = FontInformation::new(engine);
        self.text_buffer.set_text(
            &mut font_info.text_handle.font_system,
            text,
            Attrs::new()
                .color(colour.into())
                .family(Family::Name(&font_info.text_handle.defualt_font_name)),
            Shaping::Basic,
        );

        self.update_measurements(font_info.wgpu);
    }

    /// Sets the text for the widget, but with a font of your choosing.
    /// This only needs to be done once, not every frame like Materials
    pub fn set_text_with_font(
        &mut self,
        text: &str,
        colour: Colour,
        font: &ResourceId<Font>,
        engine: &mut Engine,
    ) {
        let font_info = FontInformation::new(engine);

        let backup = &String::new();
        let name = font_info
            .resources
            .get_font(font)
            .map(|f| &f.name)
            .unwrap_or(backup);

        self.text_buffer.set_text(
            &mut font_info.text_handle.font_system,
            text,
            Attrs::new().color(colour.into()).family(Family::Name(name)),
            Shaping::Basic,
        );

        self.update_measurements(font_info.wgpu);
    }

    /// Sets bounds for the text. Any text drawn outside of the bounds will be cropped
    pub fn set_bounds(&mut self, position: Vec2<i32>, size: Vec2<i32>) {
        self.bounds = Vec2 {
            x: position,
            y: size,
        };
    }

    /// Sets the font size of the text
    pub fn set_font_size(&mut self, new_size: f32, engine: &mut Engine) {
        self.font_size = new_size;
        let font_info = FontInformation::new(engine);
        let metrics = Metrics::new(self.font_size, self.line_height);
        self.text_buffer
            .set_metrics(&mut font_info.text_handle.font_system, metrics);

        self.update_measurements(font_info.wgpu);
    }

    /// Sets the line hieght of the tex
    pub fn set_line_height(&mut self, new_height: f32, engine: &mut Engine) {
        self.line_height = new_height;
        let font_info = FontInformation::new(engine);
        let metrics = Metrics::new(self.font_size, self.line_height);
        self.text_buffer
            .set_metrics(&mut font_info.text_handle.font_system, metrics);

        self.update_measurements(font_info.wgpu);
    }

    /// Measuers the text contained within the widget
    pub fn get_measurements(&self) -> Vec2<u32> {
        self.size
    }

    fn update_measurements(&mut self, wgpu: &WgpuClump) {
        let hieght = self.text_buffer.lines.len() as f32 * self.text_buffer.metrics().line_height;
        let run_width = self
            .text_buffer
            .layout_runs()
            .map(|run| run.line_w)
            .max_by(f32::total_cmp)
            .unwrap_or(0.0);

        self.size = Vec2 {
            x: run_width.ceil() as u32,
            y: hieght.ceil() as u32,
        };

        self.texture = wgpu.device.create_texture(&wgpu::TextureDescriptor {
            label: Some("Text Texture"),
            size: wgpu::Extent3d {
                width: self.size.x,
                height: self.size.y,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format: wgpu::TextureFormat::Rgba8UnormSrgb,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
            view_formats: &[],
        });

        self.change_flag = true;
    }

    /// Queues a peice of text at the specified position. Its size will be the size of the entire
    /// text.
    pub fn add_instance(&mut self, position: Vec2<f32>, tint: Colour, render: &RenderInformation) {
        let window_size = render.size;
        let rect_size = Vec2 {
            x: self.size.x as f32,
            y: self.size.y as f32,
        };
        let wgpu = render.wgpu;
        let verts = vertex::from_pixels(position, rect_size, tint.as_raw(), window_size);

        self.push_rectangle(wgpu, verts);
    }

    /// Queues a piece of text at the specified postion, with rotation. Its size will be the size of the
    /// text.
    pub fn add_instance_with_rotation(
        &mut self,
        position: Vec2<f32>,
        tint: Colour,
        degrees: f32,
        render: &RenderInformation,
    ) {
        let window_size = render.size;
        let rect_size = Vec2 {
            x: self.size.x as f32,
            y: self.size.y as f32,
        };
        let wgpu = render.wgpu;
        let verts = vertex::from_pixels_with_rotation(
            position,
            rect_size,
            tint.as_raw(),
            window_size,
            degrees,
        );

        self.push_rectangle(wgpu, verts);
    }

    /// Queues a piece of text at the specified postion. This also allows you to control the uv coordinates to the texture
    /// that the text has been rendered to.
    pub fn add_instance_with_uv(
        &mut self,
        position: Vec2<f32>,
        size: Vec2<f32>,
        uv_pos: Vec2<f32>,
        uv_size: Vec2<f32>,
        tint: Colour,
        render: &RenderInformation,
    ) {
        let window_size = render.size;
        let wgpu = render.wgpu;

        let uv_pos = normalize_points(
            uv_pos,
            Vec2 {
                x: self.size.x as f32,
                y: self.size.y as f32,
            },
        );
        let uv_size = normalize_points(
            uv_size,
            Vec2 {
                x: self.size.x as f32,
                y: self.size.y as f32,
            },
        );

        let verts = vertex::from_pixels_with_uv(
            position,
            size,
            tint.as_raw(),
            window_size,
            uv_pos,
            uv_size,
        );

        self.push_rectangle(wgpu, verts);
    }

    #[allow(clippy::too_many_arguments)]
    /// Queues a piece of text at the position with, uv controll, and rotation.
    pub fn add_instace_ex(
        &mut self,
        position: Vec2<f32>,
        size: Vec2<f32>,
        uv_pos: Vec2<f32>,
        uv_size: Vec2<f32>,
        degrees: f32,
        tint: Colour,
        render: &RenderInformation,
    ) {
        let window_size = render.size;
        let wgpu = render.wgpu;

        let uv_pos = normalize_points(
            uv_pos,
            Vec2 {
                x: self.size.x as f32,
                y: self.size.y as f32,
            },
        );
        let uv_size = normalize_points(
            uv_size,
            Vec2 {
                x: self.size.x as f32,
                y: self.size.y as f32,
            },
        );

        let verts = vertex::from_pixels_ex(
            position,
            size,
            tint.as_raw(),
            window_size,
            degrees,
            uv_pos,
            uv_size,
        );

        self.push_rectangle(wgpu, verts);
    }

    /// Queues a peice with complete controll over the points, rotation, and uv coordinates. This can allow for non rectanglular shapes
    /// and the points must be in top left, top right, bottom right, bottom left order otherwise it will not draw properly.
    pub fn add_instance_custom(
        &mut self,
        points: [Vec2<f32>; 4],
        uv_points: [Vec2<f32>; 4],
        degrees: f32,
        tint: Colour,
        render: &RenderInformation,
    ) {
        let window_size = render.size;
        let wgpu = render.wgpu;

        let uv_points = [
            normalize_points(
                uv_points[0],
                Vec2 {
                    x: self.size.x as f32,
                    y: self.size.y as f32,
                },
            ),
            normalize_points(
                uv_points[1],
                Vec2 {
                    x: self.size.x as f32,
                    y: self.size.y as f32,
                },
            ),
            normalize_points(
                uv_points[2],
                Vec2 {
                    x: self.size.x as f32,
                    y: self.size.y as f32,
                },
            ),
            normalize_points(
                uv_points[3],
                Vec2 {
                    x: self.size.x as f32,
                    y: self.size.y as f32,
                },
            ),
        ];

        let verts =
            vertex::from_pixels_custom(points, uv_points, degrees, tint.as_raw(), window_size);

        self.push_rectangle(wgpu, verts);
    }

    fn push_rectangle(&mut self, wgpu: &WgpuClump, verts: [Vertex; 4]) {
        let vertex_size = std::mem::size_of::<Vertex>() as u64;
        let index_size = 2;

        let max_verts = self.vertex_buffer.size();
        if self.vertex_count + (4 * vertex_size) > max_verts {
            material::grow_buffer(&mut self.vertex_buffer, wgpu, 1, wgpu::BufferUsages::VERTEX);
        }

        let num_verts = self.get_vertex_number() as u16;
        let indicies = [
            num_verts,
            1 + num_verts,
            2 + num_verts,
            3 + num_verts,
            num_verts,
            2 + num_verts,
        ];

        let max_indicies = self.index_buffer.size();
        if self.index_count + (6 * index_size) > max_indicies {
            material::grow_buffer(&mut self.vertex_buffer, wgpu, 1, wgpu::BufferUsages::INDEX);
        }

        wgpu.queue.write_buffer(
            &self.vertex_buffer,
            self.vertex_count,
            bytemuck::cast_slice(&verts),
        );
        wgpu.queue.write_buffer(
            &self.index_buffer,
            self.index_count,
            bytemuck::cast_slice(&indicies),
        );

        self.vertex_count += 4 * vertex_size;
        self.index_count += 6 * index_size;
    }

    fn get_vertex_number(&self) -> u64 {
        self.vertex_count / std::mem::size_of::<Vertex>() as u64
    }

    fn get_index_number(&self) -> u64 {
        self.index_count / 2
    }

    pub fn prepare(&self, engine: &mut Engine) {
        let font_info = FontInformation::new(engine);
        render_text_to_texture(
            font_info.text_handle,
            &self.text_buffer,
            self.bounds,
            self.size,
            &self.texture_view,
            font_info.wgpu,
        );
    }

    /// Draws all queued text instances to the screen
    pub fn draw<'pass, 'others>(
        &'others mut self,
        information: &mut RenderInformation<'pass, 'others>,
    ) where
        'others: 'pass,
    {
        let pipeline = &information
            .resources
            .get_pipeline(&information.defualt_id)
            .unwrap()
            .pipeline;

        information.render_pass.set_pipeline(pipeline);
        information
            .render_pass
            .set_bind_group(0, &self.bind_group, &[]);

        information
            .render_pass
            .set_vertex_buffer(0, self.vertex_buffer.slice(0..self.get_vertex_number()));
        information.render_pass.set_index_buffer(
            self.index_buffer.slice(0..self.index_count),
            wgpu::IndexFormat::Uint16,
        );

        information
            .render_pass
            .draw_indexed(0..self.get_index_number() as u32, 0, 0..1);

        self.vertex_count = 0;
        self.index_count = 0;
    }
}

// fun hacky thing to get around BC
pub(crate) struct FontInformation<'a> {
    text_handle: &'a mut TextRenderer,
    wgpu: &'a WgpuClump,
    resources: &'a ResourceManager,
}

impl<'a> FontInformation<'a> {
    pub fn new(engine: &'a mut Engine) -> Self {
        Self {
            text_handle: &mut engine.text_renderer,
            wgpu: &engine.wgpu_clump,
            resources: &engine.resource_manager,
        }
    }
}

/// A struct to ensure Font Families are created properly.
pub struct Font {
    name: String,
}

impl Font {
    /// Attempts to load in a Shader from file. This will halt the engine for more information on
    /// this halting behavior check the [resource module](crate::resource).
    pub fn new<P: AsRef<Path>>(path: P, engine: &mut Engine) -> ResourceId<Font> {
        let typed_id = resource::generate_id::<Font>();
        let id = typed_id.get_id();
        let path = path.as_ref();
        let ip_resource = InProgressResource::new(path, id, ResourceType::Font);

        resource::start_load(engine, path, &ip_resource);

        engine.add_in_progress_resource(ip_resource);
        typed_id
    }

    /// Attempts to load in a Shader from a byte array. This will not halt the
    /// engine, please check the [resource module](crate::resource) for more information.
    pub fn from_bytes(data: &[u8], engine: &mut Engine) -> ResourceId<Font> {
        let font = engine.text_renderer.load_font_from_bytes(data);
        let typed_id = resource::generate_id::<Font>();

        engine.resource_manager.insert_font(typed_id, font);

        typed_id
    }

    pub(crate) fn from_str(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

fn render_text_to_texture(
    text_handle: &mut TextRenderer,
    text: &glyphon::Buffer,
    bounds: Vec2<Vec2<i32>>,
    texture_size: Vec2<u32>,
    texture_view: &wgpu::TextureView,
    wgpu: &WgpuClump,
) {
    let text_area = TextArea {
        buffer: text,
        left: 0.0,
        top: 0.0,
        scale: 1.0,
        bounds: TextBounds {
            left: bounds.x.x,
            top: bounds.x.y,
            right: bounds.y.x,
            bottom: bounds.y.y,
        },
        default_color: glyphon::Color::rgb(255, 255, 255),
    };

    text_handle
        .text_renderer
        .prepare(
            &wgpu.device,
            &wgpu.queue,
            &mut text_handle.font_system,
            &mut text_handle.atlas,
            texture_size.into(),
            [text_area],
            &mut text_handle.cache,
        )
        .unwrap_or(());

    let mut text_encoder = wgpu
        .device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("text encoder"),
        });

    let mut text_pass = text_encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
        label: Some("Text to Texture Pass"),
        color_attachments: &[Some(wgpu::RenderPassColorAttachment {
            view: texture_view,
            resolve_target: None,
            ops: wgpu::Operations {
                load: wgpu::LoadOp::Clear(wgpu::Color {
                    r: 0.0,
                    g: 0.0,
                    b: 0.0,
                    a: 0.0,
                }),
                store: wgpu::StoreOp::Store,
            },
        })],
        timestamp_writes: None,
        occlusion_query_set: None,
        depth_stencil_attachment: None,
    });

    text_handle
        .text_renderer
        .render(&text_handle.atlas, &mut text_pass)
        .unwrap();

    drop(text_pass);
    text_handle.atlas.trim();

    wgpu.queue.submit(std::iter::once(text_encoder.finish()));
}