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
// External dependencies
use cgmath::{Matrix4, Transform, Vector3, Vector4};
use gl::{self, types::{GLfloat, GLsizei, GLsizeiptr, GLuint}};
use image;
use std::{
    collections::HashMap,
    fs::File,
    io::{BufRead, BufReader},
    mem,
    os::raw::c_void,
    ptr,
};

// Internal dependencies
use crate::Error;
use super::binder::{Binder, Bindable};


/// # General Information
///
/// Representation of a character
///
/// # Fields
///
/// * `id` - Id of a character (unicode).
/// * `origin` - Place of the character in charmap image.
/// * `size` - Width and height in a tuple.
/// * `character_start` - Where the character begins from rectangle dictated in size.
/// * `xadvance` - How much should the cursor move to get to next character
///
#[derive(Debug)]
struct Character {
    // Character id
    pub id: u32,
    // Where it starts (top left corner)
    pub(crate) origin: (f32, f32),
    // Width and height of texture representing character
    pub(crate) size: (f32, f32),
    // Offset from top left corner
    pub(crate) character_start: (f32, f32),
    // Advance to next character
    pub(crate) xadvance: f32
}

/// # General Information
///
/// A list of characters with a series of important options that make it into a font.
///
/// # Fields
///
/// * `characters` - Every character literal (char) with it's corresponding character struct.
/// * `font_type` - Name of the font.
/// * `font_size` - Size of the font (pt).
/// * `is_italic` - Self-explanatory.
/// * `is_bool` - Self-explanatory.
/// * `encoding` - Type of encoding (unicode, normally)
/// * `line_height` - Where characters should start to be drawn vertically.
/// * `character_number` - Number of characters in font.
/// * `texture_file` - Where texture file is located.
/// * `texture_size` - Dimension of the texture file.
/// * `binder` - Binder associated to font.
/// * `image_as_vec` - Image as a vector.
///
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct CharacterSet {
    characters: HashMap<char, Character>,
    font_type: String,
    font_size: u32, // pt
    is_italic: bool,
    is_bold: bool,
    encoding: String,
    line_height: u32, // Pixels
    character_number: usize,
    texture_file: String,
    texture_size: (u32, u32), // Pixels
    pub(crate) binder: Binder,
    image_as_vec: Vec<u8>, // image vector
}

impl Character {
    /// New instance of a character
    pub fn new(id: u32, origin: (f32, f32), size: (f32, f32), character_start: (f32, f32), xadvance: f32) -> Self {
        Self {
            id,
            origin,
            size,
            character_start,
            xadvance
        }
    }
}

impl PartialEq for Character {
    fn eq(&self, other: &Self) -> bool {
        if self.id == other.id {
            true
        } else {
            false
        }
    }
}

impl Eq for Character {}

impl Bindable for CharacterSet {
    fn get_binder(&self) -> Result<&Binder, Error> {
        Ok(&self.binder)
    }

    fn get_mut_binder(&mut self) -> Result<&mut Binder, Error> {
        Ok(&mut self.binder)
    }
}

impl CharacterSet {
    /// # General Information
    ///
    /// Creates new character set given a character file (fnt). It reads every line, substracting information neccesary to create every struct and later
    /// create every character struct associating them to their char version. It also loads the image and generates a vector with it inside.
    ///
    /// # Parameters
    ///
    /// * `character_file` - fnt file. It is important that the file is correctly created since metadata is important to struct instance.
    ///
    pub fn new(character_file: &str) -> Result<Self,Error> {
        let binder = Binder::new();

        let file = File::open(character_file)?;
        let mut reader = BufReader::new(file).lines();

        // read general properties of font first
        let info_line = reader
            .next()
            .ok_or(Error::Parse("Could not read first line from text font file"))??;

        let info_line: Vec<&str> = info_line.split("\"").collect();

        // Font properties
        let font_type = info_line[1].to_string();

        // Need to split againd but this time via space, collecting every property from first line
        let mut property_map_one: HashMap<String, String> = info_line[2]
            .trim()
            .split(" ")
            .map(|property| {
                let key_value: Vec<&str> = property.split("=").collect();
                (key_value[0].to_string(), key_value[1].to_string())
            })
            .collect();

        // Second line also contains information
        let second_info_line = reader
            .next()
            .ok_or(Error::Parse("Could not read second line from text font file"))??;

        let mut property_map_two = second_info_line.split(" ");
        // Skip 'common' word
        property_map_two.next();
        // Shadowing
        let mut property_map_two: HashMap<String, String> = property_map_two
            .map(|property| {
                let key_value: Vec<&str> = property.split("=").collect();
                (key_value[0].to_string(), key_value[1].to_string())
            })
            .collect();

        // Third line contains texture file
        let third_info_line = reader
            .next()
            .ok_or(Error::Parse("Could not read third line from text font file"))??;
            
        let mut property_map_three = third_info_line.split(" ");
        // SKip 'page' word
        property_map_three.next();
        // Shadowing
        let mut property_map_three: HashMap<String, String> = property_map_three
            .map(|property| {
                let key_value: Vec<&str> = property.split("=").collect();
                (key_value[0].to_string(), key_value[1].to_string())
            })
            .collect();

        // After third line, image can be loaded.
        let img = image::open(format!(
            "./assets/{}",
            property_map_three
                .get("file")
                .ok_or(Error::NotFound("Text image file"))?
                .replace("\"", "")
        ))?;
        let img_vec: Vec<u8> = img.into_bytes();

        // Fourth line contains number of characters
        let fourth_info_line = reader
            .next()
            .ok_or(Error::Parse("Could not read fourth line from text font file"))??;
            
        let mut property_map_four = fourth_info_line.split(" ");
        // Skip 'chars' word
        property_map_four.next();
        // Shadowing
        let mut property_map_four: HashMap<String, usize> = property_map_four
            .map(|property| -> Result<(String, usize),Error> {

                let key_value: Vec<&str> = property.split("=").collect();
                Ok((key_value[0].to_string(), key_value[1].parse()?))
            
            })
            .collect::<Result<HashMap<String,usize>,_>>()?;

        // Processing rest of file to create the characters
        let mut characters: HashMap<char, Character> =
            HashMap::with_capacity(*property_map_four.get("count").ok_or(Error::Custom("Could not find propperty 'count' on text file".to_string()))?);
        for line in reader {

            let content = line?;
            
            // Get rid of multiple space
            let mut properties = content.split(" ").filter(|e| *e != "");
            let is_character_line = properties.next().ok_or(Error::Parse("Could not parse text file propperly"))?;

            if is_character_line != "char" {
                continue;
            } else {
                // Property map for character
                let property_map: HashMap<&str, f32> = properties
                    .map(|property| -> Result<(&str, f32),Error> {
                        
                        let key_value: Vec<&str> = property.split("=").collect();
                        Ok((key_value[0], key_value[1].parse()?))
                    
                    })
                    .collect::<Result<HashMap<&str,f32>,_>>()?;

                // Character creation

                let temp_character = Character::new(
                    property_map["id"] as u32,
                    (property_map["x"], property_map["y"]),
                    (property_map["width"], property_map["height"]),
                    (property_map["xoffset"], property_map["yoffset"]),
                    property_map["xadvance"]
                );

                // Insert character
                characters.insert(
                    char::from_u32(property_map["id"] as u32).ok_or(Error::Parse("A letter provided in text file is not a valid char"))?,
                    temp_character,
                );
            }
        }

        Ok(Self {
            font_type,
            characters,
            binder,
            image_as_vec: img_vec,
            font_size: property_map_one
                .remove("size")
                .ok_or(Error::custom("Could not find 'size' property on text file"))?
                .parse()?,
            is_bold: property_map_one
                .get("bold").ok_or(Error::custom("Could not find property 'bold' on text file"))?
                == "1",
            is_italic: property_map_one
                .get("italic").ok_or(Error::custom("Could not find property 'bold'"))?
                == "1",
            encoding: String::from("unicode"),
            line_height: property_map_two
                .remove("lineHeight").ok_or(Error::custom("Could not find property 'lineHEight' on text file"))?
                .parse()?,
            texture_size: (
                property_map_two
                    .remove("scaleW").ok_or(Error::custom("Could not find property 'scaleW' on text file"))?
                    .parse()?,
                property_map_two
                    .remove("scaleH").ok_or(Error::custom("Could not find property 'scaleH' on text file"))?
                    .parse()?,
            ),
            texture_file: property_map_three
                .remove("file").ok_or(Error::custom("Could not find property 'file' on text file"))?
                .replace("\"", ""),
            character_number: property_map_four
                .remove("count").ok_or(Error::custom("Could not find property 'count' on text file"))?,
        })
    }

    /// # General Information
    ///
    /// Struct has it's own method to send to gpu since texture has to be considered. This means send_to_gpu method inside bindable trait does not work
    /// with this struct. Text are sent by letter. Performance is not affected much since the amount of letters is small. Image vector is sent in it's entirety.
    ///
    /// # Parameters
    ///
    /// * `&self` - Only a couple properties within self are enough to configure: vector from image, size of image.
    ///
    pub(crate) fn send_to_gpu(&self) {
        unsafe {
            gl::Enable(gl::BLEND);
            gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);

            // texture wrapping parameters
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::REPEAT as i32); //how to wrap in s coordinate
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_T, gl::REPEAT as i32); // how to wrap in t coordinate
                                                                                      // texture filtering
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST as i32); // when texture is small, scall using linear
            gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST as i32); // when texture is big, scall using linear

            gl::TexImage2D(
                gl::TEXTURE_2D,  // Texture target is 2D since we created a texture for that
                0, // Mipmap level 0 which is default. Otherwise wue could specify levels and change it
                gl::RGBA as i32, // Image is given as values of RGB
                self.texture_size.0 as i32,
                self.texture_size.1 as i32,
                0,                 // Legacy sutff not explained
                gl::RGBA,          // Format of the image (this is the actual format)
                gl::UNSIGNED_BYTE, // RGB values are given as chars
                &self.image_as_vec[0] as *const u8 as *const c_void,
            ); // Pointer to first element of vector

            gl::GenerateMipmap(gl::TEXTURE_2D); // generate mipmap for texture 2d (when object is far or close)

            // set up way information will be sent
            // vertex coordinates
            gl::VertexAttribPointer(
                0,
                3,
                gl::FLOAT,
                gl::FALSE,
                5 * mem::size_of::<GLfloat>() as GLsizei,
                ptr::null(),
            );
            gl::EnableVertexAttribArray(0); // Enabling vertex atributes giving vertex location (setup in vertex shader).
                                            // texture coordinates
            gl::VertexAttribPointer(
                1,
                2,
                gl::FLOAT,
                gl::FALSE,
                5 * mem::size_of::<GLfloat>() as GLsizei,
                (3 * mem::size_of::<GLfloat>()) as *const c_void,
            );
            gl::EnableVertexAttribArray(1); // Enabling vertex atributes giving vertex location (setup in vertex shader).

            // now allocate two quads (four triangles) for information to be sent. Second quad is for spacing
            gl::BufferData(
                gl::ARRAY_BUFFER,
                (5 * 6 * mem::size_of::<GLfloat>()) as GLsizeiptr, // number of vertices * new block for spacing * number of values in each * size of float 32 bits
                ptr::null() as *const f32 as *const c_void,
                gl::DYNAMIC_DRAW,
            ); // dynamic draw since content will be altered constantly

            gl::BufferData(
                gl::ELEMENT_ARRAY_BUFFER,
                (12 * mem::size_of::<GLfloat>()) as GLsizeiptr,
                ptr::null() as *const u32 as *const c_void,
                gl::DYNAMIC_DRAW,
            );
        }
    }

    /// # General Information
    ///
    /// Obtains every letter given an entry of text, generating everything necessary to send each one to the gpu: indices, coordinates and texture coordinates.
    ///
    /// # Parameters
    ///
    /// * `&self` - Obtain character struct for a given character to access it's properties.
    /// * `text` - A text string to parse and display on screen. Every character has to be in the original font (CharacterSet).
    ///
    fn get_vertices_from_text<A: AsRef<str>>(&self, text: A) -> Result<(Vec<[f32; 30]>, Vec<[u32; 12]>),Error> {
        // Split text into chars. Should be feasible given the fact that we only operate with the alphabet, numbers and some special symbols such as '?','!'
        // Range of utf-8 values: 0,2^21 (given that there are at most 11 bits of metadata in a 4 bytes sequence)
        let text_vec: Vec<char> = text.as_ref().chars().collect();
        // Initialize vertices and indices vectors
        let mut vertices: Vec<[f32; 30]> = Vec::new();
        let mut indices: Vec<[u32; 12]> = Vec::new();

        // Obtain subset of characters from CharacterSet HashMap
        text_vec
            .iter()
            .fold((0.0_f32, 0_u32), |(width, last_index), character_string| -> (f32,u32) {
                let character_struct = self.characters.get(character_string);
                match character_struct {
                    Some(character) => {
                        // vertices obtained from character
                        let new_advance = character.xadvance / 4.0;
                        let width = width + character.size.0 + new_advance;
                        let height = character.size.1;
                        // Point order:

                        //   start ---->
                        //   ^         |
                        //   |         |     +  space quad
                        //   |         |
                        //   |         ˇ
                        //    <--------

                        let new_vertices: [f32; 30] = [
                            // First point
                            // Coordinate
                            width - character.size.0 - new_advance,
                            height,
                            0.0,
                            // Texture
                            (character.origin.0) / (self.texture_size.0 as f32),
                            (character.origin.1) / (self.texture_size.1 as f32),
                            // Second point
                            // Coordinate
                            width - new_advance,
                            height,
                            0.0,
                            // Texture
                            (character.origin.0 + character.size.0) / (self.texture_size.0 as f32),
                            (character.origin.1) / (self.texture_size.1 as f32),
                            // Third point
                            // Coordinate
                            width - new_advance,
                            0.0,
                            0.0,
                            // Texture
                            (character.origin.0 + character.size.0) / (self.texture_size.0 as f32),
                            (character.origin.1 + character.size.1) / (self.texture_size.1 as f32),
                            // Fourth point
                            // Coordinate
                            width - character.size.0 - new_advance,
                            0.0, // y always starts on 0.0
                            0.0, // z will always be 0.0 initially
                            // Texture
                            (character.origin.0) / (self.texture_size.0 as f32),
                            (character.origin.1 + character.size.1) / (self.texture_size.1 as f32),
                            // Space quad missing vertices
                            // uper vertex
                            width,
                            height,
                            0.0,
                            0.0,
                            0.0,
                            // lower vertex
                            width,
                            0.0,
                            0.0,
                            0.0,
                            0.0,

                        ];
                        let new_indices: [u32; 12] = [
                            // First index is the one passed from last iteration.
                            // There are six indices total
                            // First triangle
                            0, 1, 2,
                            // Second triangle
                            2, 3, 0, 
                            // space triangles
                            //first
                            1, 2, 5,
                            // second
                            1, 4, 5,

                        ];

                        vertices.push(new_vertices);
                        indices.push(new_indices);

                        (width, last_index + 4)
                    }
                    None => panic!("Character string {} not found",character_string)
                }
            });
        Ok((vertices, indices))
    }

    /// # General Information
    ///
    /// Draw a given text string. It can even be dynamic and, as long as the text is not too big, there will be no framerate drop.
    ///
    /// # Parameters
    ///
    /// * `&self` - Obtain vertices from text function
    /// * `text` - A given text input to draw into screen
    ///
    pub(crate) fn draw_text<A: AsRef<str>>(&self, text: A) -> Result<(),Error> {
        // use function inside event loop in dzahui window, not anywhere else.
        // obtain vertices and indices to draw
        let (vertices, indices) = self.get_vertices_from_text(text)?;

        vertices
            .iter()
            .zip(indices)
            .for_each(|(vertices_subset, indices_subset)| {
                unsafe {
                    gl::PolygonMode(gl::FRONT_AND_BACK, gl::FILL);

                    gl::BufferSubData(
                        gl::ARRAY_BUFFER,
                        0,
                        (vertices_subset.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
                        &vertices_subset[0] as *const f32 as *const c_void,
                    ); // double casting to raw pointer of c_void

                    gl::BufferSubData(
                        gl::ELEMENT_ARRAY_BUFFER,
                        0,
                        (indices_subset.len() * mem::size_of::<GLuint>()) as GLsizeiptr,
                        &indices_subset[0] as *const u32 as *const c_void,
                    );

                    gl::DrawElements(gl::TRIANGLES, 6, gl::UNSIGNED_INT, ptr::null());

                    gl::PolygonMode(gl::FRONT_AND_BACK, gl::LINE);
                }
            });

        Ok(())
    }

    /// # General Information
    ///
    /// Obtain a matrix to make text appear on screen in a certain position with a certain size. There's still a problem of scale happening: should be chosen
    /// dynamically.
    ///
    /// # Parameters
    ///
    /// * `viewport_x` - Place where text will be rendered in viewport
    /// * `viewport_y` - Place where text will be rendered in viewport
    /// * `projection_matrix` - Matrix to inverse and obtain view coordinates
    /// * `camera` - A reference to a camera to obtain a projection matrix (should change)
    /// * `window_height` - Size of viewport to normalize coordinates
    /// * `window_width` - Size of viewport to normalize coordinates
    /// * `text_scale` - Scale of text to display
    ///
    pub(crate) fn matrix_for_screen(
        viewport_x: f32,
        viewport_y: f32,
        projection_matrix: &Matrix4<f32>,
        window_height: u32,
        window_width: u32,
        text_scale: f32
    ) -> Result<Matrix4<f32>,Error> {
        let ndc_coordinates = Vector4::new(
            (viewport_x - (window_width as f32) / 2.0) / ((window_width as f32) / 2.0), // map between -1 and 1
            (viewport_y - (window_height as f32) / 2.0) / ((window_height as f32) / 2.0),
            -0.5, // between near and mesh (-1.0,0.0)
            1.0,
        );

        let inverse_projection_matrix: Matrix4<f32> = projection_matrix
            .inverse_transform().ok_or(Error::Matrix("No inverse matrix exists for projection matrix in text"))?;
        let view_coordinates = inverse_projection_matrix * ndc_coordinates;

        // need to divide by w (god knows why)
        let view_coordinates =
            Vector3::new(view_coordinates.x, view_coordinates.y, view_coordinates.z)
                / view_coordinates.w;

        Ok(Matrix4::from_translation(view_coordinates) * Matrix4::from_scale(text_scale))
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashMap;

    use crate::simulation::drawable::binder::Binder;

    use super::{CharacterSet, Character};

    #[test]
    fn read_properly() {
        let set = CharacterSet::new("./assets/dzahui-font_test.fnt").unwrap();
        let should_be_set = CharacterSet {
            characters: HashMap::from([
                ('{', Character::new(123,(0.0,0.0),(21.0,61.0),(1.0,13.0),21.)),
                (' ',Character::new(32, (0.0,0.0), (0.0,0.0), (5.0,18.0),4.)),
                ('a',Character::new(97, (211.0,153.0), (35.0,37.0), (2.0,25.0),36.)),
            ]),
            font_type: "Liberation Sans".to_string(),
            font_size: 12,
            is_italic: false,
            is_bold: false,
            line_height: 19,
            encoding: "unicode".to_string(),
            texture_file: "dzahui-font.png".to_string(),
            texture_size: (640, 394),
            character_number: 3,
            binder: Binder::new(),
            image_as_vec: set.image_as_vec.clone(),
        };
        assert!( set == should_be_set );
    }

    #[test]
    fn test_vertices_content() {
        let set = CharacterSet::new("./assets/dzahui-font_test.fnt").unwrap();
        let (vertices, indices) = set.get_vertices_from_text("{a{{{a").unwrap();
        // number of squares (quads) should be 6, equal to the number of chars in text
        assert!( indices.len() == 6 );
        assert!( vertices.len() == 6 );
    }
}