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
use glyph_brush::VariedSection;
use glyph_brush::rusttype::{Font, SharedBytes, Rect, point};
use std::borrow::Cow;

const IDENTITY_MATRIX4: [[f32; 4]; 4] = [
    [1.0, 0.0, 0.0, 0.0],
    [0.0, 1.0, 0.0, 0.0],
    [0.0, 0.0, 1.0, 0.0],
    [0.0, 0.0, 0.0, 1.0],
];

pub struct GlyphBrushBuilder<'a, H = glyph_brush::DefaultSectionHasher> {
	inner: glyph_brush::GlyphBrushBuilder<'a, H>,
}

impl<'a> GlyphBrushBuilder<'a> {
    #[inline]
    pub fn using_font_bytes<F: Into<SharedBytes<'a>>>(font: F) -> Self {
		Self::using_font(Font::from_bytes(font).unwrap())
    }

    #[inline]
    pub fn using_fonts_bytes<B, V>(font_data: V) -> Self
    where
        B: Into<SharedBytes<'a>>,
        V: Into<Vec<B>>,
    {
        Self::using_fonts(
            font_data
                .into()
                .into_iter()
                .map(|data| Font::from_bytes(data).unwrap())
                .collect::<Vec<_>>(),
        )
    }

    #[inline]
    pub fn using_font(font_0: Font<'a>) -> Self {
        Self::using_fonts(vec![font_0])
    }

    pub fn using_fonts<V: Into<Vec<Font<'a>>>>(fonts: V) -> Self {
        GlyphBrushBuilder {
            inner: glyph_brush::GlyphBrushBuilder::using_fonts(fonts),
        }
    }
}
impl<'a, H: std::hash::BuildHasher> GlyphBrushBuilder<'a, H> {
    glyph_brush::delegate_glyph_brush_builder_fns!(inner);

    pub fn build<'g>(self, grr: &'g grr::Device) -> GlyphBrush<'a, 'g, H> {
        let vs = grr.create_shader(grr::ShaderStage::Vertex, include_bytes!("shaders/vert.glsl")).unwrap();
        let fs = grr.create_shader(grr::ShaderStage::Fragment, include_bytes!("shaders/frag.glsl")).unwrap();
        let pipeline = grr.create_graphics_pipeline(grr::GraphicsPipelineDesc {
            vertex_shader: &vs,
            tessellation_control_shader: None,
            tessellation_evaluation_shader: None,
            geometry_shader: None,
            fragment_shader: Some(&fs),
        }).unwrap();
        grr.delete_shaders(&[vs, fs]);

        let vertex_array = grr.create_vertex_array(&[
            // left top
            grr::VertexAttributeDesc {
                location: 0,
                binding: 0,
                format: grr::VertexFormat::Xyz32Float,
                offset: 0,
            },
            // right bottom
            grr::VertexAttributeDesc {
                location: 1,
                binding: 0,
                format: grr::VertexFormat::Xy32Float,
                offset: (3 * std::mem::size_of::<f32>()) as _,
            },
            // left top (tex)
            grr::VertexAttributeDesc {
                location: 2,
                binding: 0,
                format: grr::VertexFormat::Xy32Float,
                offset: (5 * std::mem::size_of::<f32>()) as _,
            },
            // right bottom (tex)
            grr::VertexAttributeDesc {
                location: 3,
                binding: 0,
                format: grr::VertexFormat::Xy32Float,
                offset: (7 * std::mem::size_of::<f32>()) as _,
            },
            // color
            grr::VertexAttributeDesc {
                location: 4,
                binding: 0,
                format: grr::VertexFormat::Xyzw32Float,
                offset: (9 * std::mem::size_of::<f32>()) as _,
            },
        ]).unwrap();

        let sampler = grr.create_sampler(grr::SamplerDesc {
            min_filter: grr::Filter::Linear,
            mag_filter: grr::Filter::Linear,
            mip_map: Some(grr::Filter::Linear),
            address: (
                grr::SamplerAddress::ClampEdge,
                grr::SamplerAddress::ClampEdge,
                grr::SamplerAddress::ClampEdge,
            ),
            lod_bias: 0.0,
            lod: 0.0..1024.0,
            compare: None,
            border_color: [0.0, 0.0, 0.0, 1.0],
        }).unwrap();

        let brush = self.inner.build();

        let glyph_image = {
            let (width, height) = brush.texture_dimensions();
            grr.create_image(grr::ImageType::D2 { width, height, layers: 1, samples: 1 }, grr::Format::R8_UNORM, 1).unwrap()
        };
        let glyph_image_view = grr.create_image_view(
            &glyph_image,
            grr::ImageViewType::D2,
            grr::Format::R8_UNORM,
            grr::SubresourceRange {
                layers: 0..1,
                levels: 0..1,
            },
        ).unwrap();

        GlyphBrush {
            inner: brush,
            glyph_cache: GlyphCache {
                image: glyph_image,
                view: glyph_image_view,
            },
            grr,
            pipeline,
            vertex_array,
            sampler,
            draw: None,
        }
    }
}

struct DrawCommand {
    buffer: grr::Buffer,
    vertices: u32,
}

struct GlyphCache {
    image: grr::Image,
    view: grr::ImageView,
}

pub struct GlyphBrush<'font, 'grr, H = glyph_brush::DefaultSectionHasher> {
    inner: glyph_brush::GlyphBrush<'font, H>,
    grr: &'grr grr::Device,
    pipeline: grr::Pipeline,
    vertex_array: grr::VertexArray,
    sampler: grr::Sampler,
    glyph_cache: GlyphCache,
    draw: Option<DrawCommand>,
}

impl<'font, 'grr> GlyphBrush<'font, 'grr> {
    #[inline]
    pub fn queue_custom_layout<'a, S, G>(&mut self, section: S, custom_layout: &G)
    where
        G: glyph_brush::GlyphPositioner,
        S: Into<Cow<'a, VariedSection<'a>>>,
    {
        self.inner.queue_custom_layout(section, custom_layout)
    }

    #[inline]
    pub fn queue<'a, S>(&mut self, section: S)
    where
        S: Into<Cow<'a, VariedSection<'a>>>,
    {
        self.inner.queue(section)
    }

    #[inline]
    pub fn keep_cached_custom_layout<'a, S, G>(&mut self, section: S, custom_layout: &G)
    where
        G: glyph_brush::GlyphPositioner,
        S: Into<Cow<'a, VariedSection<'a>>>,
    {
        self.inner.keep_cached_custom_layout(section, custom_layout)
    }

    #[inline]
    pub fn keep_cached<'a, S>(&mut self, section: S)
    where
        S: Into<Cow<'a, VariedSection<'a>>>,
    {
        self.inner.keep_cached(section)
    }

    #[inline]
    pub fn draw_queued(
        &mut self,
        dims: (u32, u32),
    ) -> Result<(), String> {
        self.draw_queued_with_transform(IDENTITY_MATRIX4, dims)
    }

    pub fn draw_queued_with_transform(
        &mut self,
        transform: [[f32; 4]; 4],
        dims: (u32, u32),
    ) -> Result<(), String> {
        let mut brush_action;
        loop {
            let grr = self.grr;
            let glyph_cache = &self.glyph_cache;
            brush_action = self.inner.process_queued(
                dims,
                |rect, tex_data| {
                    grr.copy_host_to_image(
                        &glyph_cache.image,
                        grr::SubresourceLevel {
                            level: 0,
                            layers: 0..1,
                        },
                        grr::Offset { x: rect.min.x as _, y: rect.min.y as _, z: 0 },
                        grr::Extent {
                            width: rect.width(),
                            height: rect.height(),
                            depth: 1,
                        },
                        &tex_data,
                        grr::SubresourceLayout {
                            base_format: grr::BaseFormat::R,
                            format_layout: grr::FormatLayout::U8,
                            row_pitch: rect.width(),
                            image_height: rect.height(),
                            alignment: 1,
                        },
                    );
                },
                to_vertex,
            );

            match brush_action {
                Ok(_) => break,
                Err(glyph_brush::BrushError::TextureTooSmall { suggested }) => {
                    unimplemented!()
                }
            }
        }

        match brush_action.unwrap() {
            glyph_brush::BrushAction::Draw(verts) => {
                if let Some(draw) = self.draw.take() {
                    self.grr.delete_buffer(draw.buffer);
                }

                if !verts.is_empty() {
                    self.draw = Some(DrawCommand {
                        buffer:self.grr.create_buffer_from_host(grr::as_u8_slice(verts.as_slice()), grr::MemoryFlags::empty()).unwrap(),
                        vertices: verts.len() as _,
                    });
                }
            }
            glyph_brush::BrushAction::ReDraw => {}
        };

        if let Some(ref cmd) = self.draw {
            let color_blend = grr::ColorBlend {
                attachments: vec![grr::ColorBlendAttachment {
                    blend_enable: true,
                    color: grr::BlendChannel {
                        src_factor: grr::BlendFactor::SrcAlpha,
                        dst_factor: grr::BlendFactor::OneMinusSrcAlpha,
                        blend_op: grr::BlendOp::Add,
                    },
                    alpha: grr::BlendChannel {
                        src_factor: grr::BlendFactor::One,
                        dst_factor: grr::BlendFactor::One,
                        blend_op: grr::BlendOp::Add,
                    },
                }],
            };
            let depth_stencil = grr::DepthStencil {
                depth_test: true,
                depth_write: true,
                depth_compare_op: grr::Compare::LessEqual,
                stencil_test: false,
                stencil_front: grr::StencilFace::KEEP,
                stencil_back: grr::StencilFace::KEEP,
            };

            self.grr.bind_pipeline(&self.pipeline);
            self.grr.bind_vertex_array(&self.vertex_array);
            self.grr.bind_vertex_buffers(&self.vertex_array, 0, &[grr::VertexBufferView {
                buffer: &cmd.buffer,
                offset: 0,
                stride: (std::mem::size_of::<f32>() * 13) as _,
                input_rate: grr::InputRate::Instance { divisor: 1 },
            }]);
            self.grr.bind_color_blend_state(&color_blend);
            self.grr.bind_depth_stencil_state(&depth_stencil);
            self.grr.bind_samplers(0, &[&self.sampler]);
            self.grr.bind_image_views(0, &[&self.glyph_cache.view]);
            self.grr.draw(grr::Primitive::TriangleStrip, 0..4, 0..cmd.vertices as _);
        }

        Ok(())
    }

    #[inline]
    pub fn fonts(&self) -> &[Font<'_>] {
        self.inner.fonts()
    }

    pub fn add_font_bytes<'a: 'font, B: Into<SharedBytes<'a>>>(&mut self, font_data: B) -> glyph_brush::FontId {
        self.inner.add_font_bytes(font_data)
    }

    pub fn add_font<'a: 'font>(&mut self, font_data: Font<'a>) -> glyph_brush::FontId {
        self.inner.add_font(font_data)
    }
}

type Vertex = [f32; 13];

#[inline]
fn to_vertex(
    glyph_brush::GlyphVertex {
        mut tex_coords,
        pixel_coords,
        bounds,
        screen_dimensions: (screen_w, screen_h),
        color,
        z,
    }: glyph_brush::GlyphVertex,
) -> Vertex {
    let gl_bounds = Rect {
        min: point(
            2.0 * (bounds.min.x / screen_w - 0.5),
            2.0 * (0.5 - bounds.min.y / screen_h),
        ),
        max: point(
            2.0 * (bounds.max.x / screen_w - 0.5),
            2.0 * (0.5 - bounds.max.y / screen_h),
        ),
    };

    let mut gl_rect = Rect {
        min: point(
            2.0 * (pixel_coords.min.x as f32 / screen_w - 0.5),
            2.0 * (0.5 - pixel_coords.min.y as f32 / screen_h),
        ),
        max: point(
            2.0 * (pixel_coords.max.x as f32 / screen_w - 0.5),
            2.0 * (0.5 - pixel_coords.max.y as f32 / screen_h),
        ),
    };

    // handle overlapping bounds, modify uv_rect to preserve texture aspect
    if gl_rect.max.x > gl_bounds.max.x {
        let old_width = gl_rect.width();
        gl_rect.max.x = gl_bounds.max.x;
        tex_coords.max.x = tex_coords.min.x + tex_coords.width() * gl_rect.width() / old_width;
    }
    if gl_rect.min.x < gl_bounds.min.x {
        let old_width = gl_rect.width();
        gl_rect.min.x = gl_bounds.min.x;
        tex_coords.min.x = tex_coords.max.x - tex_coords.width() * gl_rect.width() / old_width;
    }
    // note: y access is flipped gl compared with screen,
    // texture is not flipped (ie is a headache)
    if gl_rect.max.y < gl_bounds.max.y {
        let old_height = gl_rect.height();
        gl_rect.max.y = gl_bounds.max.y;
        tex_coords.max.y = tex_coords.min.y + tex_coords.height() * gl_rect.height() / old_height;
    }
    if gl_rect.min.y > gl_bounds.min.y {
        let old_height = gl_rect.height();
        gl_rect.min.y = gl_bounds.min.y;
        tex_coords.min.y = tex_coords.max.y - tex_coords.height() * gl_rect.height() / old_height;
    }

    [
        gl_rect.min.x,
        gl_rect.max.y,
        z,
        gl_rect.max.x,
        gl_rect.min.y,
        tex_coords.min.x,
        tex_coords.max.y,
        tex_coords.max.x,
        tex_coords.min.y,
        color[0],
        color[1],
        color[2],
        color[3],
    ]
}