File: tests.rs

package info (click to toggle)
rust-font-kit 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 696 kB
  • sloc: makefile: 4
file content (740 lines) | stat: -rw-r--r-- 23,504 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// font-kit/tests/tests.rs
//
// Copyright © 2019 The Pathfinder Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// General tests.

use font_kit::canvas::{Canvas, Format, RasterizationOptions};
use font_kit::family_name::FamilyName;
use font_kit::file_type::FileType;
use font_kit::font::Font;
use font_kit::hinting::HintingOptions;
use font_kit::outline::{Contour, Outline, OutlineBuilder, PointFlags};
use font_kit::properties::{Properties, Stretch, Weight};
use pathfinder_geometry::rect::{RectF, RectI};
use pathfinder_geometry::transform2d::Transform2F;
use pathfinder_geometry::vector::{Vector2F, Vector2I};
use std::fs::File;
use std::io::Read;
use std::sync::Arc;

#[cfg(feature = "source")]
use font_kit::source::SystemSource;

static TEST_FONT_FILE_PATH: &str = "resources/tests/eb-garamond/EBGaramond12-Regular.otf";
static TEST_FONT_POSTSCRIPT_NAME: &str = "EBGaramond12-Regular";
static TEST_FONT_COLLECTION_FILE_PATH: &str = "resources/tests/eb-garamond/EBGaramond12.otc";
static TEST_FONT_COLLECTION_POSTSCRIPT_NAME: [&str; 2] =
    ["EBGaramond12-Regular", "EBGaramond12-Italic"];

static FILE_PATH_EB_GARAMOND_TTF: &str = "resources/tests/eb-garamond/EBGaramond12-Regular.ttf";
static FILE_PATH_INCONSOLATA_TTF: &str = "resources/tests/inconsolata/Inconsolata-Regular.ttf";

#[cfg(not(target_os = "linux"))]
static KNOWN_SYSTEM_FONT_NAME: &'static str = "Arial";
#[cfg(target_os = "linux")]
static KNOWN_SYSTEM_FONT_NAME: &str = "DejaVu Sans";

static SFNT_VERSIONS: [[u8; 4]; 4] = [
    [0x00, 0x01, 0x00, 0x00],
    [b'O', b'T', b'T', b'O'],
    [b't', b'r', b'u', b'e'],
    [b't', b'y', b'p', b'1'],
];

const OPENTYPE_TABLE_TAG_HEAD: u32 = 0x68656164;

#[cfg(feature = "source")]
#[test]
pub fn get_font_full_name() {
    let font = SystemSource::new()
        .select_best_match(
            &[FamilyName::Title(KNOWN_SYSTEM_FONT_NAME.to_string())],
            &Properties::new(),
        )
        .unwrap()
        .load()
        .unwrap();
    assert_eq!(font.full_name(), KNOWN_SYSTEM_FONT_NAME);
}

#[cfg(feature = "source")]
#[test]
pub fn get_font_full_name_from_lowercase_family_name() {
    let font = SystemSource::new()
        .select_best_match(
            &[FamilyName::Title(
                KNOWN_SYSTEM_FONT_NAME.to_ascii_lowercase(),
            )],
            &Properties::new(),
        )
        .unwrap()
        .load()
        .unwrap();
    assert_eq!(font.full_name(), KNOWN_SYSTEM_FONT_NAME);
}

#[cfg(feature = "source")]
#[test]
pub fn get_glyph_for_char() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph = font.glyph_for_char('a').expect("No glyph for char!");
    assert_eq!(glyph, 68);
}

#[cfg(all(
    feature = "source",
    any(target_family = "windows", target_os = "macos")
))]
#[test]
pub fn get_glyph_outline() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph = font.glyph_for_char('i').expect("No glyph for char!");
    let mut outline_builder = OutlineBuilder::new();
    font.outline(glyph, HintingOptions::None, &mut outline_builder)
        .unwrap();

    let outline = outline_builder.into_outline();
    assert_eq!(
        outline,
        Outline {
            contours: vec![
                Contour {
                    positions: vec![
                        Vector2F::new(136.0, 1259.0),
                        Vector2F::new(136.0, 1466.0),
                        Vector2F::new(316.0, 1466.0),
                        Vector2F::new(316.0, 1259.0),
                    ],
                    flags: vec![PointFlags::empty(); 4],
                },
                Contour {
                    positions: vec![
                        Vector2F::new(136.0, 0.0),
                        Vector2F::new(136.0, 1062.0),
                        Vector2F::new(316.0, 1062.0),
                        Vector2F::new(316.0, 0.0),
                    ],
                    flags: vec![PointFlags::empty(); 4],
                },
            ],
        }
    );
}

#[cfg(all(
    feature = "source",
    not(any(target_family = "windows", target_os = "macos", target_os = "ios"))
))]
#[test]
pub fn get_glyph_outline() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph = font.glyph_for_char('i').expect("No glyph for char!");
    let mut outline_builder = OutlineBuilder::new();
    font.outline(glyph, HintingOptions::None, &mut outline_builder)
        .unwrap();

    let outline = outline_builder.into_outline();
    assert_eq!(
        outline,
        Outline {
            contours: vec![
                Contour {
                    positions: vec![
                        Vector2F::new(193.0, 1120.0),
                        Vector2F::new(377.0, 1120.0),
                        Vector2F::new(377.0, 0.0),
                        Vector2F::new(193.0, 0.0),
                    ],
                    flags: vec![PointFlags::empty(); 4],
                },
                Contour {
                    positions: vec![
                        Vector2F::new(193.0, 1556.0),
                        Vector2F::new(377.0, 1556.0),
                        Vector2F::new(377.0, 1323.0),
                        Vector2F::new(193.0, 1323.0),
                    ],
                    flags: vec![PointFlags::empty(); 4],
                },
            ],
        }
    );
}

#[cfg(all(
    feature = "source",
    any(target_family = "windows", target_os = "macos", target_os = "ios")
))]
#[test]
pub fn get_glyph_typographic_bounds() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph = font.glyph_for_char('a').expect("No glyph for char!");
    assert_eq!(
        font.typographic_bounds(glyph),
        Ok(RectF::new(
            Vector2F::new(74.0, -24.0),
            Vector2F::new(978.0, 1110.0)
        ))
    );
}

#[cfg(all(
    feature = "source",
    not(any(target_family = "windows", target_os = "macos", target_os = "ios"))
))]
#[test]
pub fn get_glyph_typographic_bounds() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph = font.glyph_for_char('a').expect("No glyph for char!");
    assert_eq!(
        font.typographic_bounds(glyph),
        Ok(RectF::new(
            Vector2F::new(123.0, -29.0),
            Vector2F::new(946.0, 1176.0)
        ))
    );
}

#[cfg(all(feature = "source", target_family = "windows"))]
#[test]
pub fn get_glyph_advance_and_origin() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph = font.glyph_for_char('a').expect("No glyph for char!");
    assert_eq!(font.advance(glyph), Ok(Vector2F::new(1139.0, 0.0)));
    assert_eq!(font.origin(glyph), Ok(Vector2F::new(74.0, 1898.0)));
}

#[cfg(all(feature = "source", target_os = "macos"))]
#[test]
pub fn get_glyph_advance_and_origin() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph = font.glyph_for_char('a').expect("No glyph for char!");
    assert_eq!(font.advance(glyph), Ok(Vector2F::new(1139.0, 0.0)));
    assert_eq!(font.origin(glyph), Ok(Vector2F::default()));
}

#[cfg(all(
    feature = "source",
    not(any(target_family = "windows", target_os = "macos", target_os = "ios"))
))]
#[test]
pub fn get_glyph_advance_and_origin() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph = font.glyph_for_char('a').expect("No glyph for char!");
    assert_eq!(font.advance(glyph), Ok(Vector2F::new(1255.0, 0.0)));
    assert_eq!(font.origin(glyph), Ok(Vector2F::default()));
}

#[cfg(all(
    feature = "source",
    any(target_family = "windows", target_os = "macos")
))]
#[test]
pub fn get_font_metrics() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let metrics = font.metrics();
    assert_eq!(metrics.units_per_em, 2048);
    assert_eq!(metrics.ascent, 1854.0);
    assert_eq!(metrics.descent, -434.0);
    assert_eq!(metrics.line_gap, 67.0);
    assert_eq!(metrics.underline_position, -217.0);
    assert_eq!(metrics.underline_thickness, 150.0);
    assert_eq!(metrics.cap_height, 1467.0);
    assert_eq!(metrics.x_height, 1062.0);

    // Different versions of the font can have different max heights, so ignore that.
    let bounding_box = metrics.bounding_box;
    assert_eq!(bounding_box.origin(), Vector2F::new(-1361.0, -665.0));
    assert_eq!(bounding_box.width(), 5457.0);
}

#[cfg(feature = "source")]
#[test]
pub fn get_font_properties() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let properties = font.properties();
    assert_eq!(properties.weight, Weight(400.0));
    assert_eq!(properties.stretch, Stretch(1.0));
}

#[cfg(feature = "source")]
#[test]
pub fn get_font_data() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let data = font.copy_font_data().unwrap();
    debug_assert!(SFNT_VERSIONS.iter().any(|version| data[0..4] == *version));
}

#[cfg(feature = "source")]
#[test]
pub fn load_font_table() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let head_table = font
        .load_font_table(OPENTYPE_TABLE_TAG_HEAD)
        .expect("Where's the `head` table?");
    assert_eq!(&head_table[12..16], &[0x5f, 0x0f, 0x3c, 0xf5]);
}

#[cfg(feature = "source")]
#[test]
pub fn rasterize_glyph_with_grayscale_aa() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph_id = font.glyph_for_char('L').unwrap();
    let size = 32.0;
    let raster_rect = font
        .raster_bounds(
            glyph_id,
            size,
            Transform2F::default(),
            HintingOptions::None,
            RasterizationOptions::GrayscaleAa,
        )
        .unwrap();
    let mut canvas = Canvas::new(raster_rect.size(), Format::A8);
    font.rasterize_glyph(
        &mut canvas,
        glyph_id,
        size,
        Transform2F::from_translation(-raster_rect.origin().to_f32()),
        HintingOptions::None,
        RasterizationOptions::GrayscaleAa,
    )
    .unwrap();
    check_L_shape(&canvas);
}

#[cfg(feature = "source")]
#[test]
pub fn rasterize_glyph_bilevel() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph_id = font.glyph_for_char('L').unwrap();
    let size = 16.0;
    let raster_rect = font
        .raster_bounds(
            glyph_id,
            size,
            Transform2F::default(),
            HintingOptions::None,
            RasterizationOptions::Bilevel,
        )
        .unwrap();
    let mut canvas = Canvas::new(raster_rect.size(), Format::A8);
    font.rasterize_glyph(
        &mut canvas,
        glyph_id,
        size,
        Transform2F::from_translation(-raster_rect.origin().to_f32()),
        HintingOptions::None,
        RasterizationOptions::Bilevel,
    )
    .unwrap();
    assert!(canvas
        .pixels
        .iter()
        .all(|&value| value == 0 || value == 0xff));
    check_L_shape(&canvas);
}

#[cfg(feature = "source")]
#[test]
pub fn rasterize_glyph_bilevel_offset() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph_id = font.glyph_for_char('L').unwrap();
    let size = 32.0;
    let raster_rect = font
        .raster_bounds(
            glyph_id,
            size,
            Transform2F::from_translation(Vector2F::new(30.0, 100.0)),
            HintingOptions::None,
            RasterizationOptions::Bilevel,
        )
        .unwrap();
    let mut canvas = Canvas::new(raster_rect.size(), Format::A8);
    font.rasterize_glyph(
        &mut canvas,
        glyph_id,
        size,
        Transform2F::from_translation(-raster_rect.origin().to_f32() + Vector2F::new(30.0, 100.0)),
        HintingOptions::None,
        RasterizationOptions::Bilevel,
    )
    .unwrap();

    assert!(canvas
        .pixels
        .iter()
        .all(|&value| value == 0 || value == 0xff));
    check_L_shape(&canvas);
}

#[cfg(all(
    feature = "source",
    any(
        not(any(target_os = "macos", target_os = "ios", target_family = "windows")),
        feature = "loader-freetype-default"
    )
))]
#[test]
pub fn rasterize_glyph_with_full_hinting() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph_id = font.glyph_for_char('L').unwrap();
    let size = 32.0;
    let raster_rect = font
        .raster_bounds(
            glyph_id,
            size,
            Transform2F::default(),
            HintingOptions::None,
            RasterizationOptions::Bilevel,
        )
        .unwrap();
    let origin: Vector2F = -raster_rect.origin().to_f32();
    let mut canvas = Canvas::new(raster_rect.size(), Format::A8);
    font.rasterize_glyph(
        &mut canvas,
        glyph_id,
        size,
        Transform2F::from_translation(origin),
        HintingOptions::Full(size),
        RasterizationOptions::GrayscaleAa,
    )
    .unwrap();
    check_L_shape(&canvas);

    // Make sure the top and bottom (non-blank) rows have some fully black pixels in them.
    let mut top_row = &canvas.pixels[0..canvas.stride];
    if top_row.iter().all(|&value| value == 0) {
        top_row = &canvas.pixels[canvas.stride..(2 * canvas.stride)];
    }

    assert!(top_row.iter().any(|&value| value == 0xff));
    for y in (0..(canvas.size.y() as usize)).rev() {
        let bottom_row = &canvas.pixels[(y * canvas.stride)..((y + 1) * canvas.stride)];
        if bottom_row.iter().all(|&value| value == 0) {
            continue;
        }
        assert!(bottom_row.iter().any(|&value| value == 0xff));
        break;
    }
}

// https://github.com/servo/font-kit/issues/252
// Panic when targeting Canvas larger than glyph with SubpixelAa option in Freetype.
#[cfg(all(
    feature = "source",
    any(
        not(any(target_os = "macos", target_os = "ios", target_family = "windows")),
        feature = "loader-freetype-default"
    )
))]
#[test]
pub fn rasterize_glyph_with_full_hinting_subpixel() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph_id = font.glyph_for_char('L').unwrap();
    let size = 32.0;
    let raster_rect = font
        .raster_bounds(
            glyph_id,
            size,
            Transform2F::default(),
            HintingOptions::Full(size),
            RasterizationOptions::SubpixelAa,
        )
        .unwrap();
    let origin: Vector2F = -raster_rect.origin().to_f32();
    let mut canvas = Canvas::new(raster_rect.size(), Format::Rgb24);
    font.rasterize_glyph(
        &mut canvas,
        glyph_id,
        size,
        Transform2F::from_translation(origin),
        HintingOptions::Full(size),
        RasterizationOptions::SubpixelAa,
    )
    .unwrap();
    check_L_shape(&canvas);

    // Test with larger canvas
    let mut canvas = Canvas::new(Vector2I::new(100, 100), Format::Rgb24);
    font.rasterize_glyph(
        &mut canvas,
        glyph_id,
        size,
        Transform2F::from_translation(origin),
        HintingOptions::Full(size),
        RasterizationOptions::SubpixelAa,
    )
    .unwrap();
    check_L_shape(&canvas);
}

#[cfg(all(feature = "source", target_family = "windows"))]
#[test]
pub fn rasterize_glyph() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph_id = font.glyph_for_char('{').unwrap();
    let size = 32.0;
    let raster_rect = font
        .raster_bounds(
            glyph_id,
            size,
            Transform2F::default(),
            HintingOptions::None,
            RasterizationOptions::GrayscaleAa,
        )
        .unwrap();
    let mut canvas = Canvas::new(raster_rect.size(), Format::A8);
    font.rasterize_glyph(
        &mut canvas,
        glyph_id,
        size,
        Transform2F::from_translation(-raster_rect.origin().to_f32()),
        HintingOptions::None,
        RasterizationOptions::GrayscaleAa,
    )
    .unwrap();
    check_curly_shape(&canvas);
}

#[cfg(feature = "source")]
#[test]
pub fn font_transform() {
    let font = SystemSource::new()
        .select_best_match(&[FamilyName::SansSerif], &Properties::new())
        .unwrap()
        .load()
        .unwrap();
    let glyph_id = font.glyph_for_char('L').unwrap();
    let size = 16.0;
    let raster_rect = font
        .raster_bounds(
            glyph_id,
            size,
            Transform2F::from_translation(Vector2F::splat(8.0)),
            HintingOptions::None,
            RasterizationOptions::Bilevel,
        )
        .unwrap();
    let raster_rect2 = font
        .raster_bounds(
            glyph_id,
            size,
            Transform2F::row_major(3.0, 0.0, 0.0, 3.0, 8.0, 8.0),
            HintingOptions::None,
            RasterizationOptions::Bilevel,
        )
        .unwrap();
    assert!((raster_rect2.width() - raster_rect.width() * 3).abs() <= 3);
    assert!((raster_rect2.height() - raster_rect.height() * 3).abs() <= 3);
    assert!((raster_rect2.origin_x() - ((raster_rect.origin_x() - 8) * 3 + 8)).abs() <= 3);
    assert!((raster_rect2.origin_y() - ((raster_rect.origin_y() - 8) * 3 + 8)).abs() <= 3);
}

// Makes sure that a canvas has an "L" shape in it. This is used to test rasterization.
#[allow(non_snake_case)]
fn check_L_shape(canvas: &Canvas) {
    // Find any empty rows at the start.
    let mut y = 0;
    while y < canvas.size.y() {
        let (row_start, row_end) = (canvas.stride * y as usize, canvas.stride * (y + 1) as usize);
        if canvas.pixels[row_start..row_end].iter().any(|&p| p != 0) {
            break;
        }
        y += 1;
    }
    assert!(y < canvas.size.y());

    // Find the top part of the L.
    let mut top_stripe_width = None;
    while y < canvas.size.y() {
        let (row_start, row_end) = (canvas.stride * y as usize, canvas.stride * (y + 1) as usize);
        if let Some(stripe_width) = stripe_width(&canvas.pixels[row_start..row_end]) {
            if let Some(top_stripe_width) = top_stripe_width {
                if stripe_width > top_stripe_width {
                    break;
                }
                assert_eq!(stripe_width, top_stripe_width);
            }
            top_stripe_width = Some(stripe_width);
        }
        y += 1;
    }
    assert!(y < canvas.size.y());

    // Find the bottom part of the L.
    let mut bottom_stripe_width = None;
    while y < canvas.size.y() {
        let (row_start, row_end) = (canvas.stride * y as usize, canvas.stride * (y + 1) as usize);
        y += 1;
        if let Some(stripe_width) = stripe_width(&canvas.pixels[row_start..row_end]) {
            if let Some(bottom_stripe_width) = bottom_stripe_width {
                assert!(bottom_stripe_width > top_stripe_width.unwrap());
                assert_eq!(stripe_width, bottom_stripe_width);
            }
            bottom_stripe_width = Some(stripe_width);
        }
    }

    // Find any empty rows at the end.
    while y < canvas.size.y() {
        let (row_start, row_end) = (canvas.stride * y as usize, canvas.stride * (y + 1) as usize);
        y += 1;
        if canvas.pixels[row_start..row_end].iter().any(|&p| p != 0) {
            break;
        }
    }

    // Make sure we made it to the end.
    assert_eq!(y, canvas.size.y());
}

// Makes sure that a canvas has an "{" shape in it. This is used to test rasterization.
#[cfg(target_family = "windows")]
fn check_curly_shape(canvas: &Canvas) {
    let mut y = 0;
    let height = canvas.size.y();
    // check the upper row and the lower rows are symmetrical
    while y < height / 2 {
        let (upper_row_start, upper_row_end) =
            (canvas.stride * y as usize, canvas.stride * (y + 1) as usize);
        let (lower_row_start, lower_row_end) = (
            canvas.stride * (height - y - 1) as usize,
            canvas.stride * (height - y) as usize,
        );
        let upper_row = &canvas.pixels[upper_row_start..upper_row_end];
        let lower_row = &canvas.pixels[lower_row_start..lower_row_end];
        let top_row_width = stripe_width(upper_row).unwrap();
        let lower_row_width = stripe_width(lower_row).unwrap();
        let upper_row_pixel_start = stride_pixel_start(upper_row).unwrap();
        let lower_row_pixel_start = stride_pixel_start(lower_row).unwrap();
        if top_row_width == lower_row_width {
            // check non-zero pixels start at the same index
            assert_eq!(upper_row_pixel_start, lower_row_pixel_start);
        } else {
            //if not, assert that the difference is not greater than 1
            assert_eq!((lower_row_width as i32 - top_row_width as i32).abs(), 1);
            // and assert that the non-zero pixel index difference is not greater than 1
            assert_eq!(
                (upper_row_pixel_start as i32 - lower_row_pixel_start as i32).abs(),
                1
            );
        }
        y += 1;
    }
}

// return the first non-zero pixel index
#[cfg(target_family = "windows")]
fn stride_pixel_start(pixels: &[u8]) -> Option<u32> {
    let mut index = 0;
    for x in pixels {
        if *x != 0 {
            return Some(index);
        }
        index += 1;
    }
    None
}

fn stripe_width(pixels: &[u8]) -> Option<u32> {
    let mut x = 0;
    // Find the initial empty part.
    while x < pixels.len() && pixels[x] == 0 {
        x += 1
    }
    if x == pixels.len() {
        return None;
    }
    // Find the stripe width.
    let mut stripe_width = 0;
    while x < pixels.len() && pixels[x] != 0 {
        x += 1;
        stripe_width += 1;
    }
    // Find the last empty part.
    while x < pixels.len() && pixels[x] == 0 {
        x += 1;
    }
    assert_eq!(x, pixels.len());
    Some(stripe_width)
}