mln-rs-renderer 0.0.13

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
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
use glam::Vec2;
use mln_math::{deg2radf, fmaxf, fminf};
use mln_style::layers::symbol_layer_properties::SymbolLayoutProperties;
use mln_style_types::image::{ImagePosition, ImagePositions, ImageStretches, RectU16};
use mln_style_types::{SymbolPlacementType, TextRotationAlignmentType};
use mln_text::PositionedIcon;
use mln_text::glyph::{Shaping, WritingModeType};
use mln_util::constants::ONE_EM;

use crate::layout::symbol_instance::SymbolContent;

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct SymbolQuad {
    pub(crate) tl: Vec2,
    pub(crate) tr: Vec2,
    pub(crate) bl: Vec2,
    pub(crate) br: Vec2,
    pub(crate) tex: RectU16,
    pub(crate) pixel_offset_tl: Vec2,
    pub(crate) pixel_offset_br: Vec2,
    pub(crate) glyph_offset: Vec2,
    pub(crate) writing_mode: WritingModeType,
    pub(crate) is_sdf: bool,
    pub(crate) section_index: usize,
    pub(crate) min_font_scale: Vec2,
}

impl SymbolQuad {
    #[expect(clippy::too_many_arguments, reason = "each argument is a field of the quad")]
    #[must_use]
    pub(crate) fn new(
        tl: Vec2,
        tr: Vec2,
        bl: Vec2,
        br: Vec2,
        tex: RectU16,
        writing_mode: WritingModeType,
        glyph_offset: Vec2,
        is_sdf: bool,
        pixel_offset_tl: Vec2,
        pixel_offset_br: Vec2,
        min_font_scale: Vec2,
        section_index: usize,
    ) -> Self {
        SymbolQuad {
            tl,
            tr,
            bl,
            br,
            tex,
            pixel_offset_tl,
            pixel_offset_br,
            glyph_offset,
            writing_mode,
            is_sdf,
            section_index,
            min_font_scale,
        }
    }
}

pub(crate) type SymbolQuads = Vec<SymbolQuad>;

/// The border around an image in the atlas: its padding.
const BORDER: u16 = ImagePosition::PADDING;

fn matrix_multiply(m: &[f32; 4], p: Vec2) -> Vec2 {
    Vec2::new(m[0] * p.x + m[1] * p.y, m[2] * p.x + m[3] * p.y)
}

#[must_use]
pub(crate) fn compute_stretch_sum(stretches: &ImageStretches) -> f32 {
    let mut sum: f32 = 0.0;
    for stretch in stretches {
        sum += stretch.1 - stretch.0;
    }
    sum
}

#[must_use]
pub(crate) fn sum_within_range(stretches: &ImageStretches, min: f32, max: f32) -> f32 {
    let mut sum: f32 = 0.0;
    for stretch in stretches {
        sum += fmaxf(min, fminf(max, stretch.1)) - fmaxf(min, fminf(max, stretch.0));
    }
    sum
}

#[inline]
#[must_use]
pub(crate) fn get_em_offset(stretch_offset: f32, stretch_size: f32, icon_size: f32, icon_offset: f32) -> f32 {
    icon_offset + icon_size * stretch_offset / stretch_size
}

#[inline]
#[must_use]
pub(crate) fn get_px_offset(fixed_offset: f32, fixed_size: f32, stretch_offset: f32, stretch_size: f32) -> f32 {
    fixed_offset - fixed_size * stretch_offset / stretch_size
}

/// `Cut`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Cut {
    pub(crate) fixed: f32,
    pub(crate) stretch: f32,
}

/// `Cuts`.
pub(crate) type Cuts = Vec<Cut>;

#[must_use]
pub(crate) fn stretch_zones_to_cuts(stretch_zones: &ImageStretches, fixed_size: f32, stretch_size: f32) -> Cuts {
    // `-border` is the `int` -1.
    let mut cuts: Cuts = vec![Cut { fixed: -i32::from(BORDER) as f32, stretch: 0.0 }];

    let mut last_stretch = 0.0;
    for zone in stretch_zones {
        let c1 = zone.0;
        let c2 = zone.1;
        cuts.push(Cut { fixed: c1 - last_stretch, stretch: last_stretch });
        cuts.push(Cut { fixed: c1 - last_stretch, stretch: last_stretch + (c2 - c1) });
        last_stretch += c2 - c1;
    }
    cuts.push(Cut { fixed: fixed_size + f32::from(BORDER), stretch: stretch_size });
    cuts
}

#[must_use]
pub(crate) fn get_icon_quads(
    shaped_icon: &PositionedIcon,
    icon_rotate: f32,
    icon_type: SymbolContent,
    has_icon_text_fit: bool,
) -> SymbolQuads {
    let mut quads = SymbolQuads::new();

    let image: &ImagePosition = shaped_icon.image();
    let pixel_ratio = image.pixel_ratio;
    // `int` arithmetic narrowed to `uint16_t`.
    let image_width = (i32::from(image.padded_rect.w) - 2 * i32::from(BORDER)) as u16;
    let image_height = (i32::from(image.padded_rect.h) - 2 * i32::from(BORDER)) as u16;

    let stretch_x_full: ImageStretches = vec![(0.0, f32::from(image_width))];
    let stretch_y_full: ImageStretches = vec![(0.0, f32::from(image_height))];
    let stretch_x: &ImageStretches = if image.stretch_x.is_empty() { &stretch_x_full } else { &image.stretch_x };
    let stretch_y: &ImageStretches = if image.stretch_y.is_empty() { &stretch_y_full } else { &image.stretch_y };

    let stretch_width = compute_stretch_sum(stretch_x);
    let stretch_height = compute_stretch_sum(stretch_y);
    let fixed_width = f32::from(image_width) - stretch_width;
    let fixed_height = f32::from(image_height) - stretch_height;

    let mut stretch_offset_x: f32 = 0.0;
    let mut stretch_content_width = stretch_width;
    let mut stretch_offset_y: f32 = 0.0;
    let mut stretch_content_height = stretch_height;
    let mut fixed_offset_x: f32 = 0.0;
    let mut fixed_content_width = fixed_width;
    let mut fixed_offset_y: f32 = 0.0;
    let mut fixed_content_height = fixed_height;

    let mut icon = shaped_icon.clone();

    if has_icon_text_fit && let Some(content) = &image.content {
        let content_width = content.right - content.left;
        let content_height = content.bottom - content.top;
        if image.text_fit_width.is_some() || image.text_fit_height.is_some() {
            icon = icon.apply_text_fit();
        }
        stretch_offset_x = sum_within_range(stretch_x, 0.0, content.left);
        stretch_offset_y = sum_within_range(stretch_y, 0.0, content.top);
        stretch_content_width = sum_within_range(stretch_x, content.left, content.right);
        stretch_content_height = sum_within_range(stretch_y, content.top, content.bottom);
        fixed_offset_x = content.left - stretch_offset_x;
        fixed_offset_y = content.top - stretch_offset_y;
        fixed_content_width = content_width - stretch_content_width;
        fixed_content_height = content_height - stretch_content_height;
    }

    let mut matrix: Option<[f32; 4]> = None;
    if icon_rotate != 0.0 {
        let angle = deg2radf(icon_rotate);
        let angle_sin = angle.sin();
        let angle_cos = angle.cos();
        matrix = Some([angle_cos, -angle_sin, angle_sin, angle_cos]);
    }

    let icon_left = icon.left();
    let icon_top = icon.top();
    let icon_width = icon.right() - icon_left;
    let icon_height = icon.bottom() - icon_top;

    let mut make_box = |left: Cut, top: Cut, right: Cut, bottom: Cut| {
        let left_em = get_em_offset(left.stretch - stretch_offset_x, stretch_content_width, icon_width, icon_left);
        let left_px = get_px_offset(left.fixed - fixed_offset_x, fixed_content_width, left.stretch, stretch_width);

        let top_em = get_em_offset(top.stretch - stretch_offset_y, stretch_content_height, icon_height, icon_top);
        let top_px = get_px_offset(top.fixed - fixed_offset_y, fixed_content_height, top.stretch, stretch_height);

        let right_em = get_em_offset(right.stretch - stretch_offset_x, stretch_content_width, icon_width, icon_left);
        let right_px = get_px_offset(right.fixed - fixed_offset_x, fixed_content_width, right.stretch, stretch_width);

        let bottom_em = get_em_offset(bottom.stretch - stretch_offset_y, stretch_content_height, icon_height, icon_top);
        let bottom_px =
            get_px_offset(bottom.fixed - fixed_offset_y, fixed_content_height, bottom.stretch, stretch_height);

        let mut tl = Vec2::new(left_em, top_em);
        let mut tr = Vec2::new(right_em, top_em);
        let mut br = Vec2::new(right_em, bottom_em);
        let mut bl = Vec2::new(left_em, bottom_em);
        let pixel_offset_tl = Vec2::new(left_px / pixel_ratio, top_px / pixel_ratio);
        let pixel_offset_br = Vec2::new(right_px / pixel_ratio, bottom_px / pixel_ratio);

        if let Some(matrix) = &matrix {
            tl = matrix_multiply(matrix, tl);
            tr = matrix_multiply(matrix, tr);
            bl = matrix_multiply(matrix, bl);
            br = matrix_multiply(matrix, br);
        }

        let x1 = left.stretch + left.fixed;
        let x2 = right.stretch + right.fixed;
        let y1 = top.stretch + top.fixed;
        let y2 = bottom.stretch + bottom.fixed;

        // TODO: consider making texture coordinates float instead of uint16_t
        // (`paddedRect.x + border` is an `int`; adding the float makes it a
        // float, then `static_cast<uint16_t>`.)
        let sub_rect = RectU16 {
            x: ((i32::from(image.padded_rect.x) + i32::from(BORDER)) as f32 + x1) as u16,
            y: ((i32::from(image.padded_rect.y) + i32::from(BORDER)) as f32 + y1) as u16,
            w: (x2 - x1) as u16,
            h: (y2 - y1) as u16,
        };

        let min_font_scale_x = fixed_content_width / pixel_ratio / icon_width;
        let min_font_scale_y = fixed_content_height / pixel_ratio / icon_height;

        // Icon quad is padded, so texture coordinates also need to be padded.
        quads.push(SymbolQuad::new(
            tl,
            tr,
            bl,
            br,
            sub_rect,
            WritingModeType::None,
            Vec2::ZERO,
            icon_type == SymbolContent::IconSDF,
            pixel_offset_tl,
            pixel_offset_br,
            Vec2::new(min_font_scale_x, min_font_scale_y),
            0,
        ));
    };

    if !has_icon_text_fit || (image.stretch_x.is_empty() && image.stretch_y.is_empty()) {
        make_box(
            Cut { fixed: 0.0, stretch: -1.0 },
            Cut { fixed: 0.0, stretch: -1.0 },
            Cut { fixed: 0.0, stretch: (i32::from(image_width) + 1) as f32 },
            Cut { fixed: 0.0, stretch: (i32::from(image_height) + 1) as f32 },
        );
    } else {
        let x_cuts = stretch_zones_to_cuts(stretch_x, fixed_width, stretch_width);
        let y_cuts = stretch_zones_to_cuts(stretch_y, fixed_height, stretch_height);

        for xi in 0..x_cuts.len() - 1 {
            let x1 = x_cuts[xi];
            let x2 = x_cuts[xi + 1];
            for yi in 0..y_cuts.len() - 1 {
                let y1 = y_cuts[yi];
                let y2 = y_cuts[yi + 1];
                make_box(x1, y1, x2, y2);
            }
        }
    }

    quads
}

#[must_use]
pub(crate) fn get_glyph_quads(
    shaped_text: &Shaping,
    text_offset: [f32; 2],
    layout: &SymbolLayoutProperties::Values,
    placement: SymbolPlacementType,
    icons: &ImagePositions,
    allow_vertical_placement: bool,
) -> SymbolQuads {
    let text_rotate = deg2radf(layout.text_rotate);
    let along_line = layout.text_rotation_alignment != TextRotationAlignmentType::Viewport
        && placement != SymbolPlacementType::Point;

    let mut quads = SymbolQuads::new();

    for line in &shaped_text.positioned_lines {
        for positioned_glyph in &line.positioned_glyphs {
            // `rect.hasArea()`.
            if !(positioned_glyph.rect.w != 0 && positioned_glyph.rect.h != 0) {
                continue;
            }

            // The rects have an additional buffer that is not included in their size;
            let glyph_padding: f32 = 1.0;
            let mut rect_buffer: f32 = 3.0 + glyph_padding;
            let mut pixel_ratio: f32 = 1.0;
            let mut line_offset: f32 = 0.0;
            let rotate_vertical_glyph = (along_line || allow_vertical_placement) && positioned_glyph.vertical;
            let half_advance = positioned_glyph.metrics.advance as f32 * positioned_glyph.scale / 2.0;
            let rect = &positioned_glyph.rect;
            let mut is_sdf = true;

            // Align images and scaled glyphs in the middle of a vertical line.
            if allow_vertical_placement && shaped_text.verticalizable {
                let scaled_glyph_offset = (positioned_glyph.scale - 1.0) * ONE_EM;
                let image_offset = (ONE_EM - positioned_glyph.metrics.width as f32 * positioned_glyph.scale) / 2.0;
                line_offset = line.line_offset / 2.0
                    - (if positioned_glyph.image_id.is_some() { -image_offset } else { scaled_glyph_offset });
            }

            if let Some(image_id) = &positioned_glyph.image_id {
                let Some(image) = icons.get(image_id) else {
                    continue;
                };
                pixel_ratio = image.pixel_ratio;
                rect_buffer = f32::from(ImagePosition::PADDING) / pixel_ratio;
                is_sdf = image.sdf;
            }

            let glyph_offset =
                if along_line { Vec2::new(positioned_glyph.x + half_advance, positioned_glyph.y) } else { Vec2::ZERO };

            let mut built_in_offset = if along_line {
                Vec2::ZERO
            } else {
                Vec2::new(
                    positioned_glyph.x + half_advance + text_offset[0],
                    positioned_glyph.y + text_offset[1] - line_offset,
                )
            };

            let mut verticalized_label_offset = Vec2::ZERO;
            if rotate_vertical_glyph {
                // Vertical POI labels, that are rotated 90deg CW and whose
                // glyphs must preserve upright orientation need to be rotated
                // 90deg CCW. After quad is rotated, it is translated to the
                // original built-in offset.
                verticalized_label_offset = built_in_offset;
                built_in_offset = Vec2::ZERO;
            }

            // rectBuffer is in 1x logical units; only rect.w/h carry the texture
            // scale. GL JS keeps 2x glyphs centered by storing fractional
            // left/top metrics, so native local glyphs do the same.
            let texture_scale: f32 = if positioned_glyph.metrics.is_double_resolution { 2.0 } else { 1.0 };
            let x1 = (positioned_glyph.metrics.left - rect_buffer) * positioned_glyph.scale - half_advance
                + built_in_offset.x;
            let y1 = (-positioned_glyph.metrics.top - rect_buffer) * positioned_glyph.scale + built_in_offset.y;
            let x2 = x1 + f32::from(rect.w) / texture_scale * positioned_glyph.scale / pixel_ratio;
            let y2 = y1 + f32::from(rect.h) / texture_scale * positioned_glyph.scale / pixel_ratio;

            let mut tl = Vec2::new(x1, y1);
            let mut tr = Vec2::new(x2, y1);
            let mut bl = Vec2::new(x1, y2);
            let mut br = Vec2::new(x2, y2);

            if rotate_vertical_glyph {
                // Vertical-supporting glyphs are laid out in 24x24 point boxes
                // (1 square em) In horizontal orientation, the y values for
                // glyphs are below the midline and we use a "yOffset" of -17 to
                // pull them up to the middle. By rotating counter-clockwise
                // around the point at the center of the left edge of a 24x24
                // layout box centered below the midline, we align the center of
                // the glyphs with the horizontal midline, so the yOffset is no
                // longer necessary, but we also pull the glyph to the left
                // along the x axis. The y coordinate includes baseline yOffset,
                // therefore, needs to be accounted for when glyph is rotated
                // and translated.

                let center = Vec2::new(-half_advance, half_advance - Shaping::Y_OFFSET as f32);
                let vertical_rotation = -std::f32::consts::PI / 2.0;

                // xHalfWidhtOffsetcorrection is a difference between full-width
                // and half-width advance, should be 0 for full-width glyphs and
                // will pull up half-width glyphs.
                let x_half_widht_offsetcorrection = ONE_EM / 2.0 - half_advance;
                let y_image_offset_correction =
                    if positioned_glyph.image_id.is_some() { x_half_widht_offsetcorrection } else { 0.0 };
                let x_offset_correction = Vec2::new(
                    5.0 - Shaping::Y_OFFSET as f32 - x_half_widht_offsetcorrection,
                    -y_image_offset_correction,
                );

                let transform = |p: Vec2| {
                    Vec2::from_angle(vertical_rotation).rotate(p - center)
                        + center
                        + x_offset_correction
                        + verticalized_label_offset
                };
                tl = transform(tl);
                tr = transform(tr);
                bl = transform(bl);
                br = transform(br);
            }

            if text_rotate != 0.0 {
                // Compute the transformation matrix.
                let angle_sin = text_rotate.sin();
                let angle_cos = text_rotate.cos();
                let matrix: [f32; 4] = [angle_cos, -angle_sin, angle_sin, angle_cos];

                tl = matrix_multiply(&matrix, tl);
                tr = matrix_multiply(&matrix, tr);
                bl = matrix_multiply(&matrix, bl);
                br = matrix_multiply(&matrix, br);
            }

            let pixel_offset_tl = Vec2::ZERO;
            let pixel_offset_br = Vec2::ZERO;
            let min_font_scale = Vec2::ZERO;

            quads.push(SymbolQuad::new(
                tl,
                tr,
                bl,
                br,
                *rect,
                shaped_text.writing_mode,
                glyph_offset,
                is_sdf,
                pixel_offset_tl,
                pixel_offset_br,
                min_font_scale,
                positioned_glyph.section_index,
            ));
        }
    }

    quads
}

#[cfg(test)]
mod tests {
    use mln_style_types::image::Impl as ImageImpl;
    use mln_style_types::{IconTextFitType, SymbolAnchorType};
    use mln_text::glyph::{GlyphID, GlyphMetrics, PositionedGlyph, PositionedLine};
    use mln_tile_id::geometry_tile_data::GeometryCoordinates;
    use mln_util::image::PremultipliedImage;
    use mln_util::size::Size;

    use super::*;
    use crate::geometry::anchor::Anchor;

    /// gtest `EXPECT_FLOAT_EQ`: within 4 ULPs.
    fn assert_float_eq(actual: f32, expected: f32) {
        fn biased(f: f32) -> u32 {
            let bits = f.to_bits();
            if bits & 0x8000_0000 != 0 { !bits + 1 } else { bits | 0x8000_0000 }
        }
        let (a, b) = (biased(actual), biased(expected));
        let distance = a.abs_diff(b);
        assert!(distance <= 4, "expected {expected}, got {actual}");
    }

    fn test_image_impl() -> ImageImpl {
        ImageImpl::new(
            "test".to_owned(),
            PremultipliedImage::new(Size { width: 1, height: 1 }),
            1.0,
            false,
            Vec::new(),
            Vec::new(),
            None,
            None,
            None,
        )
        .expect("valid image")
    }

    fn rect(x: u16, y: u16, w: u16, h: u16) -> RectU16 {
        RectU16 { x, y, w, h }
    }

    fn positioned_glyph(code: u32, rect: RectU16, metrics: GlyphMetrics) -> PositionedGlyph {
        PositionedGlyph::new(GlyphID::new(code), 0.0, 0.0, false, 0, 1.0, rect, metrics, None, 0)
    }

    // Atlas padding is not scaled with textureScale, so a 2x glyph quad is 1
    // logical pixel smaller than its 1x equivalent. GL JS-style fractional metrics
    // keep its center aligned with the 1x quad.
    // gtest: getGlyphQuads.DoubleResolutionGlyphPreservesLogicalQuadSize
    #[test]
    fn get_glyph_quads_double_resolution_glyph_preserves_logical_quad_size() {
        let metrics1x =
            GlyphMetrics { width: 24, height: 24, left: 0.0, top: -8.0, advance: 24, is_double_resolution: false };

        let mut metrics2x = metrics1x;
        metrics2x.left += 0.5;
        metrics2x.top -= 0.5;
        metrics2x.is_double_resolution = true;

        // Rects reflect the real DynamicTextureAtlas output: bitmap.size + 2 *
        // ImagePosition::padding (= 2). 1x bitmap 30 -> rect 32; 2x bitmap 60 -> rect 62.
        let mut shaped_text1x = Shaping::default();
        shaped_text1x.positioned_lines.push(PositionedLine::default());
        shaped_text1x.positioned_lines.last_mut().unwrap().positioned_glyphs.push(positioned_glyph(
            'A' as u32,
            rect(0, 0, 32, 32),
            metrics1x,
        ));

        let mut shaped_text2x = Shaping::default();
        shaped_text2x.positioned_lines.push(PositionedLine::default());
        shaped_text2x.positioned_lines.last_mut().unwrap().positioned_glyphs.push(positioned_glyph(
            'A' as u32,
            rect(0, 0, 62, 62),
            metrics2x,
        ));

        let layout = SymbolLayoutProperties::Values::default();
        let image_map = ImagePositions::default();
        let quads1x =
            get_glyph_quads(&shaped_text1x, [0.0, 0.0], &layout, SymbolPlacementType::Point, &image_map, false);
        let quads2x =
            get_glyph_quads(&shaped_text2x, [0.0, 0.0], &layout, SymbolPlacementType::Point, &image_map, false);

        assert_eq!(quads1x.len(), 1);
        assert_eq!(quads2x.len(), 1);

        // The 2x quad is 1 logical pixel smaller, and shifted by 0.5px to keep the
        // same center as the 1x quad.
        assert_float_eq(quads1x[0].tl.x + 0.5, quads2x[0].tl.x);
        assert_float_eq(quads1x[0].tl.y + 0.5, quads2x[0].tl.y);
        assert_float_eq(quads1x[0].br.x - 0.5, quads2x[0].br.x);
        assert_float_eq(quads1x[0].br.y - 0.5, quads2x[0].br.y);

        assert_float_eq(quads1x[0].br.x - quads1x[0].tl.x, 32.0);
        assert_float_eq(quads2x[0].br.x - quads2x[0].tl.x, 31.0);
        assert_float_eq(quads1x[0].br.y - quads1x[0].tl.y, 32.0);
        assert_float_eq(quads2x[0].br.y - quads2x[0].tl.y, 31.0);
    }

    // gtest: getIconQuads.normal
    #[test]
    fn get_icon_quads_normal() {
        let _layout = SymbolLayoutProperties::Values::default();
        let _anchor = Anchor::new(2.0, 3.0, 0.0, Some(0));
        let image = ImagePosition::new(rect(0, 0, 15, 11), &test_image_impl());

        let shaped_icon = PositionedIcon::shape_icon(&image, &[-6.5, -4.5], SymbolAnchorType::Center);

        let _line = GeometryCoordinates::new();

        let quads = get_icon_quads(&shaped_icon, 0.0, SymbolContent::IconRGBA, false);

        assert_eq!(quads.len(), 1);
        let quad = &quads[0];
        assert_float_eq(quad.tl.x, -14.0);
        assert_float_eq(quad.tl.y, -10.0);
        assert_float_eq(quad.tr.x, 1.0);
        assert_float_eq(quad.tr.y, -10.0);
        assert_float_eq(quad.bl.x, -14.0);
        assert_float_eq(quad.bl.y, 1.0);
        assert_float_eq(quad.br.x, 1.0);
        assert_float_eq(quad.br.y, 1.0);
    }

    fn assert_quad(quad: &SymbolQuad, left: f32, top: f32, right: f32, bottom: f32) {
        assert_float_eq(quad.tl.x, left);
        assert_float_eq(quad.tl.y, top);
        assert_float_eq(quad.tr.x, right);
        assert_float_eq(quad.tr.y, top);
        assert_float_eq(quad.bl.x, left);
        assert_float_eq(quad.bl.y, bottom);
        assert_float_eq(quad.br.x, right);
        assert_float_eq(quad.br.y, bottom);
    }

    // gtest: getIconQuads.style
    #[test]
    fn get_icon_quads_style() {
        let _anchor = Anchor::new(0.0, 0.0, 0.0, Some(0));
        let image = ImagePosition::new(rect(0, 0, 20, 20), &test_image_impl());

        let _line = GeometryCoordinates::new();
        let mut shaped_text = Shaping { top: -10.0, bottom: 30.0, left: -60.0, right: 20.0, ..Default::default() };
        shaped_text.positioned_lines.push(PositionedLine::default());
        shaped_text.positioned_lines.last_mut().unwrap().positioned_glyphs.push(positioned_glyph(
            32,
            RectU16::default(),
            GlyphMetrics::default(),
        ));

        let fit = |text_fit: IconTextFitType, padding: [f32; 4], font_scale: f32| {
            let mut shaped_icon = PositionedIcon::shape_icon(&image, &[-9.5, -9.5], SymbolAnchorType::Center);
            shaped_icon.fit_icon_to_text(&shaped_text, text_fit, &padding, &[0.0, 0.0], font_scale);
            let quads = get_icon_quads(&shaped_icon, 0.0, SymbolContent::IconRGBA, false);
            assert_eq!(quads.len(), 1);
            quads[0].clone()
        };

        // none
        {
            let shaped_icon = PositionedIcon::shape_icon(&image, &[-9.5, -9.5], SymbolAnchorType::Center);

            assert_float_eq(-18.5, shaped_icon.top());
            assert_float_eq(-0.5, shaped_icon.right());
            assert_float_eq(-0.5, shaped_icon.bottom());
            assert_float_eq(-18.5, shaped_icon.left());

            let quads = get_icon_quads(&shaped_icon, 0.0, SymbolContent::IconRGBA, false);

            assert_eq!(quads.len(), 1);
            assert_quad(&quads[0], -19.5, -19.5, 0.5, 0.5);
        }

        // width
        assert_quad(&fit(IconTextFitType::Width, [0.0, 0.0, 0.0, 0.0], 24.0 / 24.0), -64.444_44, 0.0, 24.444_443, 20.0);

        // width x textSize
        assert_quad(
            &fit(IconTextFitType::Width, [0.0, 0.0, 0.0, 0.0], 12.0 / 24.0),
            -32.222_22,
            -5.0,
            12.222_221,
            15.0,
        );

        // width x textSize + padding
        assert_quad(
            &fit(IconTextFitType::Width, [5.0, 10.0, 5.0, 10.0], 12.0 / 24.0),
            -43.333_332,
            -5.0,
            23.333_332,
            15.0,
        );

        // height
        assert_quad(
            &fit(IconTextFitType::Height, [0.0, 0.0, 0.0, 0.0], 24.0 / 24.0),
            -30.0,
            -12.222_221,
            -10.0,
            32.222_22,
        );

        // height x textSize
        assert_quad(
            &fit(IconTextFitType::Height, [0.0, 0.0, 0.0, 0.0], 12.0 / 24.0),
            -20.0,
            -6.111_110_7,
            0.0,
            16.111_11,
        );

        // height x textSize + padding
        assert_quad(
            &fit(IconTextFitType::Height, [5.0, 10.0, 5.0, 20.0], 12.0 / 24.0),
            -20.0,
            -11.666_666,
            0.0,
            21.666_666,
        );

        // both
        assert_quad(
            &fit(IconTextFitType::Both, [0.0, 0.0, 0.0, 0.0], 24.0 / 24.0),
            -64.444_44,
            -12.222_221,
            24.444_443,
            32.222_22,
        );

        // both x textSize
        assert_quad(
            &fit(IconTextFitType::Both, [0.0, 0.0, 0.0, 0.0], 12.0 / 24.0),
            -32.222_22,
            -6.111_110_7,
            12.222_221,
            16.111_11,
        );

        // both x textSize + padding
        assert_quad(
            &fit(IconTextFitType::Both, [5.0, 10.0, 5.0, 10.0], 12.0 / 24.0),
            -43.333_332,
            -11.666_666,
            23.333_332,
            21.666_666,
        );

        // both x textSize + padding t/r/b/l
        // (C++ also sets `layout.get<TextSize>() = 12.0f` on an unused layout.)
        assert_quad(
            &fit(IconTextFitType::Both, [0.0, 5.0, 10.0, 15.0], 12.0 / 24.0),
            -48.333_332,
            -6.666_666,
            18.333_332,
            26.666_666,
        );
    }
}