mirl 9.2.0

Miners Rust Lib - A massive collection of ever growing and changing functions, structs, and enums. Check the description for compatibility and toggleable features! (Most of the lib is controlled by flags/features so the lib can continue to be lightweight despite its size)
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
#![allow(clippy::similar_names, clippy::cast_possible_wrap)]
#![allow(clippy::significant_drop_tightening)]

use super::get_character;
use crate::render::{
    draw_pixel_safe, draw_pixel_unsafe, BufferGetPixel, BufferMetrics,
    BufferMisc, BufferPointers,
};

#[allow(clippy::cast_precision_loss)]
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
/// Draw text in the specified font
pub fn draw_text_antialiased<const SAFE: bool>(
    buffer: &mut (impl BufferPointers + BufferMetrics + BufferMisc),
    text: &str,
    xy: (usize, usize),
    color: u32,
    size: f32,
    font: &fontdue::Font,
) {
    let mut pen_x = xy.0 as f32;
    let pen_y = xy.1;

    let ascent = font
        .horizontal_line_metrics(size)
        .map_or(0, |font_metrics| font_metrics.ascent as usize);

    for ch in text.chars() {
        // Try to get the glyph from cache first
        let char = get_character(ch, size, font);
        let metrics = char.0;
        let bitmap = &char.1;

        let offset_y = ascent.saturating_sub(metrics.height);
        // Draw each character into the buffer
        for gy in 0..metrics.height {
            for gx in 0..metrics.width {
                let px = pen_x as usize + gx;
                // Correcting for letter height
                let py =
                    ((pen_y + gy + offset_y) as i32 - metrics.ymin) as usize;

                if px < buffer.width() && py < buffer.height() {
                    let index = py * buffer.width() + px;
                    let alpha = bitmap[gy * metrics.width + gx]; // Alpha (0-255)

                    if alpha > 0 {
                        unsafe {
                            let bg = *buffer.pointer().add(index);
                            // Extract RGBA
                            let (br, bg, bb, ba) = (
                                (bg >> 24) & 0xFF,
                                (bg >> 16) & 0xFF,
                                (bg >> 8) & 0xFF,
                                bg & 0xFF,
                            );
                            let (tr, tg, tb, ta) = (
                                (color >> 24) & 0xFF,
                                (color >> 16) & 0xFF,
                                (color >> 8) & 0xFF,
                                color & 0xFF,
                            );

                            // Alpha blending
                            let inv_alpha: u8 = 255 - alpha;
                            let nr = ((tr as u16 * u16::from(alpha)
                                + br as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let ng = ((tg as u16 * u16::from(alpha)
                                + bg as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let nb = ((tb as u16 * u16::from(alpha)
                                + bb as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let na = ((ta as u16 * u16::from(alpha)
                                + ba as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;

                            let new = u32::from(nr) << 24
                                | u32::from(ng) << 16
                                | u32::from(nb) << 8
                                | u32::from(na);

                            if SAFE {
                                draw_pixel_safe(buffer, (px, py), new);
                            } else {
                                draw_pixel_unsafe(buffer, (px, py), new);
                            }
                        }
                    }
                }
            }
        }

        // Advance the cursor position
        pen_x += metrics.advance_width;
    }
}

/// Execute a function at every pixel position
///
/// function: `fn(original_color: u32, color_under_pixel: u32) -> u32`
pub fn draw_text_antialiased_execute_at<const SAFE: bool>(
    buffer: &mut (impl BufferPointers + BufferMetrics + BufferGetPixel),
    text: &str,
    xy: (usize, usize),
    color: u32,
    size: f32,
    font: &fontdue::Font,
    function: impl Fn(u32, u32) -> u32,
) {
    let mut pen_x = xy.0 as f32;
    let pen_y = xy.1;

    let ascent = font
        .horizontal_line_metrics(size)
        .map_or(0, |font_metrics| font_metrics.ascent as usize);

    for ch in text.chars() {
        // Try to get the glyph from cache first
        let char = get_character(ch, size, font);
        let metrics = char.0;
        let bitmap = &char.1;

        let offset_y = ascent.saturating_sub(metrics.height);
        // Draw each character into the buffer
        for gy in 0..metrics.height {
            for gx in 0..metrics.width {
                let px = pen_x as usize + gx;
                // Correcting for letter height
                let py =
                    ((pen_y + gy + offset_y) as i32 - metrics.ymin) as usize;

                if px < buffer.width() && py < buffer.height() {
                    let index = py * buffer.width() + px;
                    let alpha = bitmap[gy * metrics.width + gx]; // Alpha (0-255)
                    let new_color = function(color, buffer.get_pixel((px, py)));

                    if alpha > 0 {
                        unsafe {
                            let bg = *buffer.pointer().add(index);
                            // Extract RGBA
                            let (br, bg, bb, ba) = (
                                (bg >> 24) & 0xFF,
                                (bg >> 16) & 0xFF,
                                (bg >> 8) & 0xFF,
                                bg & 0xFF,
                            );
                            let (tr, tg, tb, ta) = (
                                (new_color >> 24) & 0xFF,
                                (new_color >> 16) & 0xFF,
                                (new_color >> 8) & 0xFF,
                                new_color & 0xFF,
                            );

                            // Alpha blending
                            let inv_alpha: u8 = 255 - alpha;
                            let nr = ((tr as u16 * u16::from(alpha)
                                + br as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let ng = ((tg as u16 * u16::from(alpha)
                                + bg as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let nb = ((tb as u16 * u16::from(alpha)
                                + bb as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let na = ((ta as u16 * u16::from(alpha)
                                + ba as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let new = u32::from(nr) << 24
                                | u32::from(ng) << 16
                                | u32::from(nb) << 8
                                | u32::from(na);

                            if SAFE {
                                draw_pixel_safe(buffer, (px, py), new);
                            } else {
                                draw_pixel_unsafe(buffer, (px, py), new);
                            }
                        }
                    }
                }
            }
        }

        // Advance the cursor position
        pen_x += metrics.advance_width;
    }
}

/// Draw text yet stretch the resulting characters
pub fn draw_text_antialiased_stretched<const SAFE: bool>(
    buffer: &mut (impl BufferPointers + BufferMetrics + BufferGetPixel),
    text: &str,
    xy: (usize, usize),
    color: u32,
    size: f32,
    font: &fontdue::Font,
    stretch: (f32, f32),
) {
    let mut pen_x = xy.0 as f32;
    let pen_y = xy.1;
    let ascent = font
        .horizontal_line_metrics(size)
        .map_or(0, |font_metrics| font_metrics.ascent as usize);

    for ch in text.chars() {
        let char = get_character(ch, size, font);
        let metrics = char.0;
        let bitmap = &char.1;

        let offset_y = ascent.saturating_sub(metrics.height);
        let w = metrics.width as f32 * stretch.0;
        let h = metrics.height as f32 * stretch.1;
        let advance_x = metrics.advance_width;

        for gy in 0..h.floor() as usize {
            let py = ((pen_y + gy + offset_y) as i32 - metrics.ymin) as usize;
            if py >= buffer.height() {
                continue;
            }

            let bitmap_row_start =
                ((gy as f32 / stretch.1) as usize) * metrics.width;
            for gx in 0..w.floor() as usize {
                let px = pen_x as usize + gx;
                if px >= buffer.width() {
                    continue;
                }

                let bitmap_index =
                    bitmap_row_start + (gx as f32 / stretch.0) as usize;
                let alpha = bitmap[bitmap_index];

                if alpha > 0 {
                    let bg = buffer.get_pixel((px, py)); // Fixed: use px instead of gx
                                                         // Extract RGBA
                    let (br, bg_g, bb, ba) = (
                        (bg >> 24) & 0xFF,
                        (bg >> 16) & 0xFF,
                        (bg >> 8) & 0xFF,
                        bg & 0xFF,
                    );
                    let (tr, tg, tb, ta) = (
                        (color >> 24) & 0xFF,
                        (color >> 16) & 0xFF,
                        (color >> 8) & 0xFF,
                        color & 0xFF,
                    );

                    // Alpha blending
                    let inv_alpha: u8 = 255 - alpha;
                    let nr = ((tr as u16 * u16::from(alpha)
                        + br as u16 * u16::from(inv_alpha))
                        / 255) as u8;
                    let ng = ((tg as u16 * u16::from(alpha)
                        + bg_g as u16 * u16::from(inv_alpha))
                        / 255) as u8;
                    let nb = ((tb as u16 * u16::from(alpha)
                        + bb as u16 * u16::from(inv_alpha))
                        / 255) as u8;
                    let na = ((ta as u16 * u16::from(alpha)
                        + ba as u16 * u16::from(inv_alpha))
                        / 255) as u8;

                    let new = u32::from(nr) << 24
                        | u32::from(ng) << 16
                        | u32::from(nb) << 8
                        | u32::from(na);

                    if SAFE {
                        draw_pixel_safe(buffer, (px, py), new);
                    } else {
                        draw_pixel_unsafe(buffer, (px, py), new);
                    }
                }
            }
        }

        pen_x += advance_x;
    }
}

/// Same as [`draw_text_antialiased`] but uses isize for positioning allowing for partially out of bounds text (left and top)
pub fn draw_text_antialiased_isize<const SAFE: bool>(
    buffer: &mut (impl BufferPointers + BufferMetrics + BufferMisc),
    text: &str,
    xy: (isize, isize),
    color: u32,
    size: f32,
    font: &fontdue::Font,
) {
    let mut pen_x = xy.0 as f32;
    let pen_y = xy.1;

    let ascent = font
        .horizontal_line_metrics(size)
        .map_or(0, |font_metrics| font_metrics.ascent as isize);

    for ch in text.chars() {
        // Try to get the glyph from cache first
        let char = get_character(ch, size, font);
        let metrics = char.0;
        let bitmap = &char.1;

        let offset_y = ascent.saturating_sub(metrics.height as isize);
        // Draw each character into the buffer
        for gy in 0..metrics.height {
            for gx in 0..metrics.width {
                let px = pen_x as isize + gx as isize;
                // Correcting for letter height
                let py = pen_y + gy as isize + offset_y - metrics.ymin as isize;

                // Check if pixel is within buffer bounds
                if px >= 0
                    && py >= 0
                    && px < buffer.width() as isize
                    && py < buffer.height() as isize
                {
                    let pixel_x_new = px as usize;
                    let pixel_y_new = py as usize;
                    let index = pixel_y_new * buffer.width() + pixel_x_new;
                    let alpha = bitmap[gy * metrics.width + gx]; // Alpha (0-255)
                    if alpha > 0 {
                        if SAFE && index >= buffer.width() * buffer.height() {
                            continue;
                        }

                        unsafe {
                            let bg = *buffer.pointer().add(index);
                            // Extract RGBA
                            let (br, bg, bb, ba) = (
                                (bg >> 24) & 0xFF,
                                (bg >> 16) & 0xFF,
                                (bg >> 8) & 0xFF,
                                bg & 0xFF,
                            );
                            let (tr, tg, tb, ta) = (
                                (color >> 24) & 0xFF,
                                (color >> 16) & 0xFF,
                                (color >> 8) & 0xFF,
                                color & 0xFF,
                            );

                            // Alpha blending
                            let inv_alpha: u8 = 255 - alpha;
                            let nr = ((tr as u16 * u16::from(alpha)
                                + br as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let ng = ((tg as u16 * u16::from(alpha)
                                + bg as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let nb = ((tb as u16 * u16::from(alpha)
                                + bb as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            let na = ((ta as u16 * u16::from(alpha)
                                + ba as u16 * u16::from(inv_alpha))
                                / 255)
                                as u8;
                            if SAFE {
                                draw_pixel_safe(
                                    buffer,
                                    (pixel_x_new, pixel_y_new),
                                    u32::from(nr) << 24
                                        | u32::from(ng) << 16
                                        | u32::from(nb) << 8
                                        | u32::from(na),
                                );
                            } else {
                                draw_pixel_unsafe(
                                    buffer,
                                    (pixel_x_new, pixel_y_new),
                                    u32::from(nr) << 24
                                        | u32::from(ng) << 16
                                        | u32::from(nb) << 8
                                        | u32::from(na),
                                );
                            }
                        }
                    }
                }
            }
        }

        // Advance the cursor position
        pen_x += metrics.advance_width;
    }
}

/// Same as [`draw_text_antialiased_stretched`] but uses isize for positioning allowing for partially out of bounds text (left and top)
pub fn draw_text_antialiased_stretched_isize<const SAFE: bool>(
    buffer: &mut (impl BufferPointers + BufferMetrics + BufferGetPixel),
    text: &str,
    xy: (isize, isize),
    color: u32,
    size: f32,
    font: &fontdue::Font,
    stretch: (f32, f32),
) {
    let mut pen_x = xy.0 as f32;
    let pen_y = xy.1;
    let ascent = font
        .horizontal_line_metrics(size)
        .map_or(0, |font_metrics| font_metrics.ascent as isize);

    for ch in text.chars() {
        let char = get_character(ch, size, font);
        let metrics = char.0;
        let bitmap = &char.1;

        let offset_y = ascent.saturating_sub(metrics.height as isize);
        let w = metrics.width as f32 * stretch.0;
        let h = metrics.height as f32 * stretch.1;
        let advance_x = metrics.advance_width;

        for gy in 0..h.floor() as usize {
            let py = pen_y + gy as isize + offset_y - metrics.ymin as isize;
            if py < 0 || py >= buffer.height() as isize {
                continue;
            }

            let bitmap_row_start =
                ((gy as f32 / stretch.1) as usize) * metrics.width;
            for gx in 0..w.floor() as usize {
                let px = pen_x as isize + gx as isize;
                if px < 0 || px >= buffer.width() as isize {
                    continue;
                }

                let bitmap_index =
                    bitmap_row_start + (gx as f32 / stretch.0) as usize;
                let alpha = bitmap[bitmap_index];

                if alpha > 0 {
                    let px_new = px as usize;
                    let py_new = py as usize;
                    let index = py_new * buffer.width() + px_new;

                    if SAFE && index >= buffer.width() * buffer.height() {
                        continue;
                    }

                    let bg = buffer.get_pixel((px_new, py_new));
                    // Extract RGBA
                    let (br, bg_g, bb, ba) = (
                        (bg >> 24) & 0xFF,
                        (bg >> 16) & 0xFF,
                        (bg >> 8) & 0xFF,
                        bg & 0xFF,
                    );
                    let (tr, tg, tb, ta) = (
                        (color >> 24) & 0xFF,
                        (color >> 16) & 0xFF,
                        (color >> 8) & 0xFF,
                        color & 0xFF,
                    );

                    // Alpha blending
                    let inv_alpha: u8 = 255 - alpha;
                    let nr = ((tr as u16 * u16::from(alpha)
                        + br as u16 * u16::from(inv_alpha))
                        / 255) as u8;
                    let ng = ((tg as u16 * u16::from(alpha)
                        + bg_g as u16 * u16::from(inv_alpha))
                        / 255) as u8;
                    let nb = ((tb as u16 * u16::from(alpha)
                        + bb as u16 * u16::from(inv_alpha))
                        / 255) as u8;
                    let na = ((ta as u16 * u16::from(alpha)
                        + ba as u16 * u16::from(inv_alpha))
                        / 255) as u8;

                    let new = u32::from(nr) << 24
                        | u32::from(ng) << 16
                        | u32::from(nb) << 8
                        | u32::from(na);

                    if SAFE {
                        draw_pixel_safe(
                            buffer,
                            (px as usize, py as usize),
                            new,
                        );
                    } else {
                        draw_pixel_unsafe(
                            buffer,
                            (px as usize, py as usize),
                            new,
                        );
                    }
                }
            }
        }

        pen_x += advance_x;
    }
}

// /// Draw text uniformly
// pub fn draw_text_antialiased_monospace<const SAFE: bool>(
//     buffer: &Buffer,
//     text: &str,
//     x: usize,
//     y: usize,
//     color: u32,
//     size: f32,
//     font: &fontdue::Font,
// ) {
//     let draw_pixel: DrawPixelFunction = {
//         if SAFE {
//             draw_pixel_safe
//         } else {
//             draw_pixel_unsafe
//         }
//     };
//     let mut pen_x = x;
//     let pen_y = xy.1

//     let font_metrics = font.horizontal_line_metrics(size).unwrap();
//     let ascent = font_metrics.ascent as usize;

//     for ch in text.chars() {
//         // Try to get the glyph from cache first
//         let char = get_character(ch, size, &font);
//         let metrics = char.0;
//         let bitmap = &char.1;

//         let offset_y = ascent.saturating_sub(metrics.height);
//         // Draw each character into the buffer
//         for gy in 0..metrics.height {
//             for gx in 0..metrics.width {
//                 let px = pen_x + gx;
//                 // Correcting for letter height
//                 let py =
//                     ((pen_y + gy + offset_y) as i32 - metrics.ymin) as usize;

//                 if px < buffer.width() && py < buffer.height() {
//                     let index = py * buffer.width() + px;
//                     let alpha = bitmap[gy * metrics.width + gx]; // Alpha (0-255)

//                     if alpha > 0 {
//                         unsafe {
//                             let bg = *buffer.pointer().add(index);
//                             // Extract RGBA
//                             let (br, bg, bb, ba) = (
//                                 (bg >> 24) & 0xFF,
//                                 (bg >> 16) & 0xFF,
//                                 (bg >> 8) & 0xFF,
//                                 bg & 0xFF,
//                             );
//                             let (tr, tg, tb, ta) = (
//                                 (color >> 24) & 0xFF,
//                                 (color >> 16) & 0xFF,
//                                 (color >> 8) & 0xFF,
//                                 color & 0xFF,
//                             );

//                             // Alpha blending
//                             let inv_alpha: u8 = 255 - alpha;
//                             let nr = ((tr as u16 * alpha as u16
//                                 + br as u16 * inv_alpha as u16)
//                                 / 255)
//                                 as u8;
//                             let ng = ((tg as u16 * alpha as u16
//                                 + bg as u16 * inv_alpha as u16)
//                                 / 255)
//                                 as u8;
//                             let nb = ((tb as u16 * alpha as u16
//                                 + bb as u16 * inv_alpha as u16)
//                                 / 255)
//                                 as u8;
//                             let na = ((ta as u16 * alpha as u16
//                                 + ba as u16 * inv_alpha as u16)
//                                 / 255)
//                                 as u8;

//                             draw_pixel(
//                                 buffer,
//                                 px,
//                                 py,
//                                 (nr as u32) << 24
//                                     | (ng as u32) << 16
//                                     | (nb as u32) << 8
//                                     | na as u32,
//                             );
//                         }
//                     }
//                 }
//             }
//         }

//         // Advance the cursor position
//         pen_x += size as usize;
//     }
// }