smix-annotate 1.0.2

smix-annotate — screenshot annotation library. Composes circle / arrow / text / box / line primitives over PNG bytes with named color palette and 3-preset PNG compression. Powers `smix annotate` / `smix screenshot --annotate` CLI + `takeScreenshot: {annotate: [...]}` yaml verb + `--debug-output` fail-step auto-annotate.
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
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
//! smix-annotate — screenshot annotation.
//!
//! Compose circle / arrow / text / box / line primitives onto a PNG,
//! then emit compressed output. RFC: `docs/ai-guide/rfc-v0.3.0-
//! annotated-screenshots.md`.
//!
//! # Quick tour
//!
//! ```rust,ignore
//! use smix_annotate::{Annotator, Annotation, Color, Position, Compression};
//!
//! let png = std::fs::read("input.png")?;
//! let out = Annotator::new(&png)?
//!     .add(Annotation::circle(Position::pixel(100, 100))
//!         .color(Color::RED)
//!         .radius(40))
//!     .compression(Compression::BALANCED)
//!     .render()?;
//! std::fs::write("output.png", out)?;
//! ```
//!
//! # v0.3.0 scope
//!
//! - 5 primitives: circle, arrow, text, box, line
//! - Position resolvers: absolute pixel + normalized (0..1)
//!   (selector-relative via a11y tree — wire lives in
//!   `smix-adapter-maestro` runtime, not in this crate; caller
//!   resolves selector → pixel before adding annotation)
//! - Named + hex + rgba color palette
//! - Compression 3 presets: fast, balanced, aggressive
//! - Text rendering via `ab_glyph`; font supplied by caller (v0.3.5
//!   will bundle Inter + Noto Sans SC)

use ab_glyph::{Font, FontRef, PxScale, ScaleFont};
use image::{DynamicImage, Rgba, RgbaImage};
use imageproc::drawing;
use thiserror::Error;

pub mod color;
pub mod font;
pub use color::Color;

#[derive(Debug, Error)]
pub enum AnnotateError {
    #[error("decode input PNG: {0}")]
    Decode(#[from] image::ImageError),
    #[error("compress output PNG: {0}")]
    Compress(String),
    #[error("font load: {0}")]
    Font(String),
    /// v1.0 Phase C1 retired: bundled fonts (Inter + Noto SC subset)
    /// now cover text rendering out of the box. Consumers can still
    /// override with `.font(bytes)`; this variant is retained for
    /// wire compat but no longer produced by v1.0 code paths.
    #[error("text annotation without font — call `.font(bytes)` before `.render()`")]
    MissingFont,
}

/// Position of an annotation on the image.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Position {
    /// Absolute pixel coordinates, top-left origin.
    Pixel { x: i32, y: i32 },
    /// Normalized 0..1 in each dimension. Resolved against the image
    /// size at [`Annotator::render`] time.
    Normalized { nx: f32, ny: f32 },
}

impl Position {
    pub fn pixel(x: i32, y: i32) -> Self {
        Position::Pixel { x, y }
    }

    pub fn normalized(nx: f32, ny: f32) -> Self {
        Position::Normalized { nx, ny }
    }

    fn resolve(self, w: u32, h: u32) -> (i32, i32) {
        match self {
            Position::Pixel { x, y } => (x, y),
            Position::Normalized { nx, ny } => ((nx * w as f32) as i32, (ny * h as f32) as i32),
        }
    }
}

/// One drawing primitive.
#[derive(Clone, Debug)]
pub enum Annotation {
    Circle {
        at: Position,
        color: Color,
        radius: i32,
        stroke: i32,
    },
    Arrow {
        from: Position,
        to: Position,
        color: Color,
        stroke: i32,
        head_filled: bool,
    },
    Text {
        at: Position,
        content: String,
        color: Color,
        size: f32,
    },
    Box {
        at: Position,
        width: i32,
        height: i32,
        color: Color,
        stroke: i32,
    },
    Line {
        from: Position,
        to: Position,
        color: Color,
        stroke: i32,
    },
}

impl Annotation {
    pub fn circle(at: Position) -> CircleBuilder {
        CircleBuilder {
            at,
            color: Color::RED,
            radius: 30,
            stroke: 3,
        }
    }

    pub fn arrow(from: Position, to: Position) -> ArrowBuilder {
        ArrowBuilder {
            from,
            to,
            color: Color::BLUE,
            stroke: 4,
            head_filled: true,
        }
    }

    pub fn text(at: Position, content: impl Into<String>) -> TextBuilder {
        TextBuilder {
            at,
            content: content.into(),
            color: Color::WHITE,
            size: 24.0,
        }
    }

    pub fn box_(at: Position, width: i32, height: i32) -> BoxBuilder {
        BoxBuilder {
            at,
            width,
            height,
            color: Color::YELLOW,
            stroke: 2,
        }
    }

    pub fn line(from: Position, to: Position) -> LineBuilder {
        LineBuilder {
            from,
            to,
            color: Color::CYAN,
            stroke: 2,
        }
    }
}

pub struct CircleBuilder {
    at: Position,
    color: Color,
    radius: i32,
    stroke: i32,
}

impl CircleBuilder {
    pub fn color(mut self, c: Color) -> Self {
        self.color = c;
        self
    }
    pub fn radius(mut self, r: i32) -> Self {
        self.radius = r;
        self
    }
    pub fn stroke(mut self, s: i32) -> Self {
        self.stroke = s;
        self
    }
    pub fn build(self) -> Annotation {
        Annotation::Circle {
            at: self.at,
            color: self.color,
            radius: self.radius,
            stroke: self.stroke,
        }
    }
}

impl From<CircleBuilder> for Annotation {
    fn from(b: CircleBuilder) -> Self {
        b.build()
    }
}

pub struct ArrowBuilder {
    from: Position,
    to: Position,
    color: Color,
    stroke: i32,
    head_filled: bool,
}

impl ArrowBuilder {
    pub fn color(mut self, c: Color) -> Self {
        self.color = c;
        self
    }
    pub fn stroke(mut self, s: i32) -> Self {
        self.stroke = s;
        self
    }
    pub fn head_open(mut self) -> Self {
        self.head_filled = false;
        self
    }
    pub fn build(self) -> Annotation {
        Annotation::Arrow {
            from: self.from,
            to: self.to,
            color: self.color,
            stroke: self.stroke,
            head_filled: self.head_filled,
        }
    }
}

impl From<ArrowBuilder> for Annotation {
    fn from(b: ArrowBuilder) -> Self {
        b.build()
    }
}

pub struct TextBuilder {
    at: Position,
    content: String,
    color: Color,
    size: f32,
}

impl TextBuilder {
    pub fn color(mut self, c: Color) -> Self {
        self.color = c;
        self
    }
    pub fn size(mut self, s: f32) -> Self {
        self.size = s;
        self
    }
    pub fn build(self) -> Annotation {
        Annotation::Text {
            at: self.at,
            content: self.content,
            color: self.color,
            size: self.size,
        }
    }
}

impl From<TextBuilder> for Annotation {
    fn from(b: TextBuilder) -> Self {
        b.build()
    }
}

pub struct BoxBuilder {
    at: Position,
    width: i32,
    height: i32,
    color: Color,
    stroke: i32,
}

impl BoxBuilder {
    pub fn color(mut self, c: Color) -> Self {
        self.color = c;
        self
    }
    pub fn stroke(mut self, s: i32) -> Self {
        self.stroke = s;
        self
    }
    pub fn build(self) -> Annotation {
        Annotation::Box {
            at: self.at,
            width: self.width,
            height: self.height,
            color: self.color,
            stroke: self.stroke,
        }
    }
}

impl From<BoxBuilder> for Annotation {
    fn from(b: BoxBuilder) -> Self {
        b.build()
    }
}

pub struct LineBuilder {
    from: Position,
    to: Position,
    color: Color,
    stroke: i32,
}

impl LineBuilder {
    pub fn color(mut self, c: Color) -> Self {
        self.color = c;
        self
    }
    pub fn stroke(mut self, s: i32) -> Self {
        self.stroke = s;
        self
    }
    pub fn build(self) -> Annotation {
        Annotation::Line {
            from: self.from,
            to: self.to,
            color: self.color,
            stroke: self.stroke,
        }
    }
}

impl From<LineBuilder> for Annotation {
    fn from(b: LineBuilder) -> Self {
        b.build()
    }
}

/// PNG output compression preset.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Compression {
    /// No oxipng pass; small file overhead but ~5× faster to write.
    Fast,
    /// Balanced default (oxipng optimization 2).
    Balanced,
    /// Maximum shrink (oxipng optimization 6 + zopfli).
    Aggressive,
}

impl Compression {
    pub const FAST: Self = Compression::Fast;
    pub const BALANCED: Self = Compression::Balanced;
    pub const AGGRESSIVE: Self = Compression::Aggressive;
}

#[allow(clippy::derivable_impls)]
impl Default for Compression {
    fn default() -> Self {
        Compression::Balanced
    }
}

/// Annotator builder.
pub struct Annotator {
    image: RgbaImage,
    annotations: Vec<Annotation>,
    font_bytes: Option<Vec<u8>>,
    compression: Compression,
}

impl Annotator {
    /// Decode `png_bytes` and prepare for annotation.
    pub fn new(png_bytes: &[u8]) -> Result<Self, AnnotateError> {
        let dyn_img = image::load_from_memory(png_bytes)?;
        let image = dyn_img.to_rgba8();
        Ok(Self {
            image,
            annotations: Vec::new(),
            font_bytes: None,
            compression: Compression::default(),
        })
    }

    /// Add an annotation. Accepts any builder or a pre-built
    /// [`Annotation`].
    #[allow(clippy::should_implement_trait)]
    pub fn add(mut self, a: impl Into<Annotation>) -> Self {
        self.annotations.push(a.into());
        self
    }

    /// Provide a TTF font for text annotations. Optional (unused
    /// annotations without text primitives).
    pub fn font(mut self, bytes: Vec<u8>) -> Self {
        self.font_bytes = Some(bytes);
        self
    }

    pub fn compression(mut self, c: Compression) -> Self {
        self.compression = c;
        self
    }

    /// Render all annotations and return the PNG-encoded (+ optionally
    /// compressed) output bytes.
    pub fn render(mut self) -> Result<Vec<u8>, AnnotateError> {
        let (w, h) = (self.image.width(), self.image.height());
        // v1.0 Phase C1 — if consumer supplied .font(bytes), use it
        // for the whole render (override). Otherwise text primitive
        // picks bundled Inter or Noto SC subset per codepoint.
        let override_font: Option<FontRef> = self
            .font_bytes
            .as_ref()
            .map(|b| FontRef::try_from_slice(b).map_err(|e| AnnotateError::Font(e.to_string())))
            .transpose()?;
        for ann in std::mem::take(&mut self.annotations) {
            match ann {
                Annotation::Circle {
                    at,
                    color,
                    radius,
                    stroke,
                } => {
                    let (cx, cy) = at.resolve(w, h);
                    let rgba = Rgba(color.to_rgba());
                    for i in 0..stroke {
                        drawing::draw_hollow_circle_mut(
                            &mut self.image,
                            (cx, cy),
                            (radius - i).max(1),
                            rgba,
                        );
                    }
                }
                Annotation::Arrow {
                    from,
                    to,
                    color,
                    stroke,
                    head_filled,
                } => {
                    let (fx, fy) = from.resolve(w, h);
                    let (tx, ty) = to.resolve(w, h);
                    let rgba = Rgba(color.to_rgba());
                    // Main shaft (thick line = repeat draws with offset).
                    for i in -(stroke / 2)..=stroke / 2 {
                        drawing::draw_line_segment_mut(
                            &mut self.image,
                            (fx as f32 + i as f32, fy as f32),
                            (tx as f32 + i as f32, ty as f32),
                            rgba,
                        );
                    }
                    // Arrow head — two short segments at ±30° from the shaft.
                    let dx = (tx - fx) as f32;
                    let dy = (ty - fy) as f32;
                    let len = (dx * dx + dy * dy).sqrt().max(1.0);
                    let ux = dx / len;
                    let uy = dy / len;
                    let head_len = 15.0_f32.min(len / 3.0);
                    // Rotate ±150° from tip.
                    let angle = 30.0_f32.to_radians();
                    let (sin, cos) = (angle.sin(), angle.cos());
                    let hx1 = tx as f32 - head_len * (ux * cos - uy * sin);
                    let hy1 = ty as f32 - head_len * (uy * cos + ux * sin);
                    let hx2 = tx as f32 - head_len * (ux * cos + uy * sin);
                    let hy2 = ty as f32 - head_len * (uy * cos - ux * sin);
                    drawing::draw_line_segment_mut(
                        &mut self.image,
                        (tx as f32, ty as f32),
                        (hx1, hy1),
                        rgba,
                    );
                    drawing::draw_line_segment_mut(
                        &mut self.image,
                        (tx as f32, ty as f32),
                        (hx2, hy2),
                        rgba,
                    );
                    if head_filled {
                        // Simple filled head — draw a tiny triangle
                        // (three lines connecting the three head points).
                        drawing::draw_line_segment_mut(
                            &mut self.image,
                            (hx1, hy1),
                            (hx2, hy2),
                            rgba,
                        );
                    }
                }
                Annotation::Text {
                    at,
                    content,
                    color,
                    size,
                } => {
                    let (x, y) = at.resolve(w, h);
                    let scale = PxScale::from(size);
                    let rgba = Rgba(color.to_rgba());
                    // v1.0 Phase C1 — per-codepoint font routing when no
                    // override supplied. Load each bundled font once,
                    // then pick per char.
                    let inter_font = FontRef::try_from_slice(font::INTER_REGULAR)
                        .map_err(|e| AnnotateError::Font(e.to_string()))?;
                    let cjk_font = FontRef::try_from_slice(font::NOTO_SANS_SC_SUBSET)
                        .map_err(|e| AnnotateError::Font(e.to_string()))?;
                    // Compute ascent from Inter (baseline is uniform
                    // across bundled fonts at same scale).
                    let ascent = inter_font.as_scaled(scale).ascent();
                    let mut caret_x = x as f32;
                    let baseline_y = y as f32 + ascent;
                    for ch in content.chars() {
                        // Pick font: override wins; otherwise per-codepoint.
                        let use_font: &FontRef = if let Some(f) = override_font.as_ref() {
                            f
                        } else if font::pick_font_for_codepoint(ch as u32)
                            == font::NOTO_SANS_SC_SUBSET
                        {
                            &cjk_font
                        } else {
                            &inter_font
                        };
                        let scaled = use_font.as_scaled(scale);
                        let glyph = scaled.scaled_glyph(ch);
                        let h_adv = scaled.h_advance(glyph.id);
                        if let Some(outlined) = use_font.outline_glyph(glyph) {
                            let bounds = outlined.px_bounds();
                            outlined.draw(|gx, gy, alpha| {
                                let px = caret_x + bounds.min.x + gx as f32;
                                let py = baseline_y + bounds.min.y + gy as f32;
                                if px >= 0.0 && py >= 0.0 && (px as u32) < w && (py as u32) < h {
                                    let blended = blend_pixel(
                                        self.image.get_pixel(px as u32, py as u32).0,
                                        rgba.0,
                                        alpha,
                                    );
                                    self.image.put_pixel(px as u32, py as u32, Rgba(blended));
                                }
                            });
                        }
                        caret_x += h_adv;
                    }
                }
                Annotation::Box {
                    at,
                    width,
                    height,
                    color,
                    stroke,
                } => {
                    let (x, y) = at.resolve(w, h);
                    let rgba = Rgba(color.to_rgba());
                    for i in 0..stroke {
                        let rect = imageproc::rect::Rect::at(x + i, y + i).of_size(
                            (width - 2 * i).max(1) as u32,
                            (height - 2 * i).max(1) as u32,
                        );
                        drawing::draw_hollow_rect_mut(&mut self.image, rect, rgba);
                    }
                }
                Annotation::Line {
                    from,
                    to,
                    color,
                    stroke,
                } => {
                    let (fx, fy) = from.resolve(w, h);
                    let (tx, ty) = to.resolve(w, h);
                    let rgba = Rgba(color.to_rgba());
                    for i in -(stroke / 2)..=stroke / 2 {
                        drawing::draw_line_segment_mut(
                            &mut self.image,
                            (fx as f32, fy as f32 + i as f32),
                            (tx as f32, ty as f32 + i as f32),
                            rgba,
                        );
                    }
                }
            }
        }
        // Encode.
        let mut raw = Vec::new();
        let dyn_img = DynamicImage::ImageRgba8(self.image);
        dyn_img
            .write_to(&mut std::io::Cursor::new(&mut raw), image::ImageFormat::Png)
            .map_err(AnnotateError::Decode)?;
        // Compression pass.
        match self.compression {
            Compression::Fast => Ok(raw),
            Compression::Balanced => oxipng::optimize_from_memory(
                &raw,
                &oxipng::Options {
                    optimize_alpha: false,
                    ..oxipng::Options::from_preset(2)
                },
            )
            .map_err(|e| AnnotateError::Compress(e.to_string())),
            Compression::Aggressive => oxipng::optimize_from_memory(
                &raw,
                &oxipng::Options {
                    optimize_alpha: false,
                    ..oxipng::Options::from_preset(6)
                },
            )
            .map_err(|e| AnnotateError::Compress(e.to_string())),
        }
    }
}

fn blend_pixel(bg: [u8; 4], fg: [u8; 4], alpha: f32) -> [u8; 4] {
    let a = (fg[3] as f32 / 255.0) * alpha;
    let inv_a = 1.0 - a;
    [
        (bg[0] as f32 * inv_a + fg[0] as f32 * a) as u8,
        (bg[1] as f32 * inv_a + fg[1] as f32 * a) as u8,
        (bg[2] as f32 * inv_a + fg[2] as f32 * a) as u8,
        (bg[3] as f32).max(fg[3] as f32) as u8,
    ]
}

#[cfg(test)]
mod tests {
    use super::*;

    fn blank_png() -> Vec<u8> {
        let img: RgbaImage = image::ImageBuffer::from_pixel(100, 100, Rgba([0, 0, 0, 255]));
        let mut out = Vec::new();
        DynamicImage::ImageRgba8(img)
            .write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png)
            .unwrap();
        out
    }

    #[test]
    fn circle_pixels_written() {
        let png = blank_png();
        let out = Annotator::new(&png)
            .unwrap()
            .add(
                Annotation::circle(Position::pixel(50, 50))
                    .color(Color::RED)
                    .radius(20),
            )
            .compression(Compression::Fast)
            .render()
            .unwrap();
        let img = image::load_from_memory(&out).unwrap().to_rgba8();
        // The circle passes through (50+20, 50) = (70, 50). Check that
        // pixel is red-ish (dominant R channel).
        let px = img.get_pixel(70, 50);
        assert!(px[0] > 100, "expected red-dominant, got {px:?}");
    }

    #[test]
    fn arrow_pixels_written() {
        let png = blank_png();
        let out = Annotator::new(&png)
            .unwrap()
            .add(Annotation::arrow(
                Position::pixel(10, 10),
                Position::pixel(90, 90),
            ))
            .compression(Compression::Fast)
            .render()
            .unwrap();
        let img = image::load_from_memory(&out).unwrap().to_rgba8();
        // Midpoint (50, 50) should be blue-dominant.
        let px = img.get_pixel(50, 50);
        assert!(px[2] > 100, "expected blue-dominant midpoint, got {px:?}");
    }

    #[test]
    fn box_pixels_written() {
        let png = blank_png();
        let out = Annotator::new(&png)
            .unwrap()
            .add(Annotation::box_(Position::pixel(20, 20), 60, 60).color(Color::YELLOW))
            .compression(Compression::Fast)
            .render()
            .unwrap();
        let img = image::load_from_memory(&out).unwrap().to_rgba8();
        // Top edge of the box: (25, 20) should be yellow (R + G high).
        let px = img.get_pixel(25, 20);
        assert!(
            px[0] > 100 && px[1] > 100,
            "expected yellow edge, got {px:?}"
        );
    }

    #[test]
    fn line_pixels_written() {
        let png = blank_png();
        let out = Annotator::new(&png)
            .unwrap()
            .add(Annotation::line(
                Position::pixel(0, 50),
                Position::pixel(99, 50),
            ))
            .compression(Compression::Fast)
            .render()
            .unwrap();
        let img = image::load_from_memory(&out).unwrap().to_rgba8();
        // Middle of the line: (50, 50) should be cyan (G + B high).
        let px = img.get_pixel(50, 50);
        assert!(
            px[1] > 100 && px[2] > 100,
            "expected cyan midpoint, got {px:?}"
        );
    }

    #[test]
    fn text_without_explicit_font_uses_bundled_inter() {
        // v1.0 Phase C1 — bundled Inter font renders text without any
        // .font() call. Prior to v1.0 this test asserted a MissingFont
        // error; that error is now unreachable via v1.0 code paths.
        let png = blank_png();
        let out = Annotator::new(&png)
            .unwrap()
            .add(
                Annotation::text(Position::pixel(10, 30), "hello")
                    .color(Color::WHITE)
                    .size(20.0),
            )
            .compression(Compression::Fast)
            .render()
            .expect("bundled font renders ASCII text");
        // Verify some pixel got written (text at (10-30, 20-50)).
        let img = image::load_from_memory(&out).unwrap().to_rgba8();
        let mut has_white = false;
        for x in 10..50u32 {
            for y in 20..50u32 {
                let px = img.get_pixel(x, y);
                if px[0] > 128 && px[1] > 128 && px[2] > 128 {
                    has_white = true;
                }
            }
        }
        assert!(has_white, "bundled font should render visible pixels");
    }

    #[test]
    fn cjk_char_renders_via_bundled_noto_sc() {
        // v1.0 Phase C1 — 登 = U+767B (in Noto SC subset).
        let png = blank_png();
        let out = Annotator::new(&png)
            .unwrap()
            .add(
                Annotation::text(Position::pixel(10, 30), "登录")
                    .color(Color::WHITE)
                    .size(20.0),
            )
            .compression(Compression::Fast)
            .render()
            .expect("bundled Noto SC subset renders CJK");
        let img = image::load_from_memory(&out).unwrap().to_rgba8();
        let mut has_white = false;
        for x in 10..80u32 {
            for y in 20..55u32 {
                let px = img.get_pixel(x, y);
                if px[0] > 128 && px[1] > 128 && px[2] > 128 {
                    has_white = true;
                }
            }
        }
        assert!(has_white, "bundled Noto SC should render CJK pixels");
    }

    #[test]
    fn compression_presets_all_encode() {
        for preset in [
            Compression::Fast,
            Compression::Balanced,
            Compression::Aggressive,
        ] {
            let png = blank_png();
            let out = Annotator::new(&png)
                .unwrap()
                .add(
                    Annotation::circle(Position::pixel(50, 50))
                        .color(Color::RED)
                        .radius(20),
                )
                .compression(preset)
                .render()
                .unwrap();
            assert!(!out.is_empty(), "preset {preset:?} produced empty output");
            // Verify it's valid PNG.
            let _ = image::load_from_memory(&out).unwrap();
        }
    }

    #[test]
    fn normalized_position_resolves() {
        let (x, y) = Position::normalized(0.5, 0.5).resolve(100, 100);
        assert_eq!((x, y), (50, 50));
    }

    #[test]
    fn compression_default_is_balanced() {
        assert_eq!(Compression::default(), Compression::Balanced);
    }
}