rhwp 0.9.0

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
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
use std::collections::HashMap;
use std::sync::Arc;

use crate::paint::font::{BinaryResourceKind, BinaryResourceRef, FontResourceTable};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ImageResourceId(pub usize);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SvgResourceId(pub usize);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FontBlobResourceId(pub usize);

pub const RESOURCE_KEY_ALGORITHM: &str = "blake3";

/// 레이어 replay가 공유하는 바이너리/문자열 자원 저장소.
///
/// 현재 leaf payload는 대부분 직접 보관하지만, P12부터 font blob/face
/// identity는 glyph replay contract에서 참조할 수 있게 한다.
#[derive(Debug, Clone, Default)]
pub struct ResourceArena {
    /// 그림 신원 키에 섞는 문서 단위 세대. 기존 공개 `PageLayerTree` 모양은 바꾸지 않는다.
    source_image_epoch: u32,
    /// 문서가 명시한 Print 래스터 세대. 직접 만든 layer는 기존 기본값을 쓴다.
    print_2024_raster: Option<bool>,
    image_bytes: Vec<Vec<u8>>,
    image_hashes: Vec<u64>,
    image_fingerprints: Vec<[u8; 16]>,
    image_resource_keys: Vec<String>,
    image_lookup: HashMap<u64, Vec<ImageResourceId>>,
    svg_fragments: Vec<String>,
    svg_hashes: Vec<u64>,
    svg_fingerprints: Vec<[u8; 16]>,
    svg_resource_keys: Vec<String>,
    svg_lookup: HashMap<u64, Vec<SvgResourceId>>,
    font_blob_bytes: Vec<Arc<[u8]>>,
    font_blob_hashes: Vec<u64>,
    font_blob_fingerprints: Vec<[u8; 16]>,
    font_blob_resource_keys: Vec<String>,
    font_blob_lookup: HashMap<u64, Vec<FontBlobResourceId>>,
    font_blob_ref_lookup: HashMap<String, FontBlobResourceId>,
    font_resources: FontResourceTable,
}

impl ResourceArena {
    pub(crate) fn set_print_2024_raster(&mut self, enabled: Option<bool>) {
        self.print_2024_raster = enabled;
    }

    pub(crate) fn print_2024_raster(&self) -> Option<bool> {
        self.print_2024_raster
    }
    pub(crate) fn set_source_image_epoch(&mut self, epoch: u32) {
        self.source_image_epoch = epoch;
    }

    pub(crate) fn source_image_epoch(&self) -> u32 {
        self.source_image_epoch
    }

    pub fn intern_image_bytes(&mut self, bytes: &[u8]) -> ImageResourceId {
        let hash = resource_hash(bytes);
        if let Some(candidates) = self.image_lookup.get(&hash) {
            for id in candidates {
                if self.image_bytes[id.0].as_slice() == bytes {
                    return *id;
                }
            }
        }

        let id = ImageResourceId(self.image_bytes.len());
        let digest = blake3::hash(bytes);
        let mut fingerprint = [0; 16];
        fingerprint.copy_from_slice(&digest.as_bytes()[..16]);
        let resource_key = image_resource_key(bytes.len(), digest.to_hex().as_str());
        self.image_bytes.push(bytes.to_vec());
        self.image_hashes.push(hash);
        self.image_fingerprints.push(fingerprint);
        self.image_resource_keys.push(resource_key);
        self.image_lookup.entry(hash).or_default().push(id);
        id
    }

    pub fn image_bytes(&self, id: ImageResourceId) -> Option<&[u8]> {
        self.image_bytes.get(id.0).map(Vec::as_slice)
    }

    pub fn image_count(&self) -> usize {
        self.image_bytes.len()
    }

    pub fn image_hash(&self, id: ImageResourceId) -> Option<u64> {
        self.image_hashes.get(id.0).copied()
    }

    pub fn image_fingerprint(&self, id: ImageResourceId) -> Option<[u8; 16]> {
        self.image_fingerprints.get(id.0).copied()
    }

    pub fn image_resource_key(&self, id: ImageResourceId) -> Option<&str> {
        self.image_resource_keys.get(id.0).map(String::as_str)
    }

    pub fn image_resources(&self) -> impl Iterator<Item = (ImageResourceId, &[u8])> + '_ {
        self.image_bytes
            .iter()
            .enumerate()
            .map(|(index, bytes)| (ImageResourceId(index), bytes.as_slice()))
    }

    pub fn intern_svg_fragment(&mut self, svg: &str) -> SvgResourceId {
        let hash = resource_hash(svg);
        if let Some(candidates) = self.svg_lookup.get(&hash) {
            for id in candidates {
                if self.svg_fragments[id.0].as_str() == svg {
                    return *id;
                }
            }
        }

        let id = SvgResourceId(self.svg_fragments.len());
        let digest = blake3::hash(svg.as_bytes());
        let mut fingerprint = [0; 16];
        fingerprint.copy_from_slice(&digest.as_bytes()[..16]);
        let resource_key = svg_resource_key(svg.len(), digest.to_hex().as_str());
        self.svg_fragments.push(svg.to_string());
        self.svg_hashes.push(hash);
        self.svg_fingerprints.push(fingerprint);
        self.svg_resource_keys.push(resource_key);
        self.svg_lookup.entry(hash).or_default().push(id);
        id
    }

    pub fn svg_fragment(&self, id: SvgResourceId) -> Option<&str> {
        self.svg_fragments.get(id.0).map(String::as_str)
    }

    pub fn svg_count(&self) -> usize {
        self.svg_fragments.len()
    }

    pub fn svg_hash(&self, id: SvgResourceId) -> Option<u64> {
        self.svg_hashes.get(id.0).copied()
    }

    pub fn svg_fingerprint(&self, id: SvgResourceId) -> Option<[u8; 16]> {
        self.svg_fingerprints.get(id.0).copied()
    }

    pub fn svg_resource_key(&self, id: SvgResourceId) -> Option<&str> {
        self.svg_resource_keys.get(id.0).map(String::as_str)
    }

    pub fn svg_resources(&self) -> impl Iterator<Item = (SvgResourceId, &str)> + '_ {
        self.svg_fragments
            .iter()
            .enumerate()
            .map(|(index, svg)| (SvgResourceId(index), svg.as_str()))
    }

    pub fn intern_font_blob_bytes(&mut self, bytes: &[u8]) -> FontBlobResourceId {
        let hash = resource_hash(bytes);
        if let Some(candidates) = self.font_blob_lookup.get(&hash) {
            for id in candidates {
                if self.font_blob_bytes[id.0].as_ref() == bytes {
                    return *id;
                }
            }
        }

        let id = FontBlobResourceId(self.font_blob_bytes.len());
        let digest = blake3::hash(bytes);
        let mut fingerprint = [0; 16];
        fingerprint.copy_from_slice(&digest.as_bytes()[..16]);
        let digest_hex = digest.to_hex();
        let resource_key = font_blob_resource_key(bytes.len(), digest_hex.as_str());
        self.font_blob_bytes.push(Arc::from(bytes));
        self.font_blob_hashes.push(hash);
        self.font_blob_fingerprints.push(fingerprint);
        self.font_blob_resource_keys.push(resource_key.clone());
        self.font_blob_lookup.entry(hash).or_default().push(id);
        self.font_blob_ref_lookup.insert(resource_key, id);
        id
    }

    /// Intern an immutable exact-source Arc whose complete-content identity was
    /// prepared and certified before the atomic paint commit.
    ///
    /// This avoids copying and re-hashing a multi-megabyte font during every
    /// layer rebuild. The caller must already have validated the canonical
    /// resource key and conflicts against this arena; general callers continue
    /// to use [`Self::intern_font_blob_bytes`].
    pub(crate) fn intern_prepared_font_blob_arc(
        &mut self,
        bytes: Arc<[u8]>,
        hash: u64,
        fingerprint: [u8; 16],
        resource_key: String,
    ) -> FontBlobResourceId {
        if let Some(id) = self.font_blob_ref_lookup.get(&resource_key).copied() {
            debug_assert_eq!(self.font_blob_bytes(id), Some(bytes.as_ref()));
            return id;
        }
        if let Some(candidates) = self.font_blob_lookup.get(&hash) {
            for id in candidates {
                if self.font_blob_bytes[id.0].as_ref() == bytes.as_ref() {
                    self.font_blob_ref_lookup.insert(resource_key, *id);
                    return *id;
                }
            }
        }

        let id = FontBlobResourceId(self.font_blob_bytes.len());
        self.font_blob_bytes.push(bytes);
        self.font_blob_hashes.push(hash);
        self.font_blob_fingerprints.push(fingerprint);
        self.font_blob_resource_keys.push(resource_key.clone());
        self.font_blob_lookup.entry(hash).or_default().push(id);
        self.font_blob_ref_lookup.insert(resource_key, id);
        id
    }

    pub fn font_blob_bytes(&self, id: FontBlobResourceId) -> Option<&[u8]> {
        self.font_blob_bytes.get(id.0).map(Arc::as_ref)
    }

    pub fn font_blob_count(&self) -> usize {
        self.font_blob_bytes.len()
    }

    pub fn font_blob_hash(&self, id: FontBlobResourceId) -> Option<u64> {
        self.font_blob_hashes.get(id.0).copied()
    }

    pub fn font_blob_fingerprint(&self, id: FontBlobResourceId) -> Option<[u8; 16]> {
        self.font_blob_fingerprints.get(id.0).copied()
    }

    pub fn font_blob_resource_key(&self, id: FontBlobResourceId) -> Option<&str> {
        self.font_blob_resource_keys.get(id.0).map(String::as_str)
    }

    pub fn font_blob_resources(&self) -> impl Iterator<Item = (FontBlobResourceId, &[u8])> + '_ {
        self.font_blob_bytes
            .iter()
            .enumerate()
            .map(|(index, bytes)| (FontBlobResourceId(index), bytes.as_ref()))
    }

    pub fn font_blob_bytes_for_ref(&self, data_ref: &BinaryResourceRef) -> Option<&[u8]> {
        if data_ref.kind != BinaryResourceKind::FontBlob {
            return None;
        }
        self.font_blob_ref_lookup
            .get(&data_ref.id)
            .and_then(|id| self.font_blob_bytes(*id))
    }

    pub fn font_resources(&self) -> &FontResourceTable {
        &self.font_resources
    }

    pub fn font_resources_mut(&mut self) -> &mut FontResourceTable {
        &mut self.font_resources
    }
}

fn resource_hash(bytes: impl AsRef<[u8]>) -> u64 {
    const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
    const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;

    let mut hash = FNV_OFFSET_BASIS;
    for byte in bytes.as_ref() {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(FNV_PRIME);
    }
    hash
}

fn resource_fingerprint(bytes: impl AsRef<[u8]>) -> [u8; 16] {
    let digest = blake3::hash(bytes.as_ref());
    let mut fingerprint = [0; 16];
    fingerprint.copy_from_slice(&digest.as_bytes()[..16]);
    fingerprint
}

pub fn resource_digest_hex(bytes: impl AsRef<[u8]>) -> String {
    blake3::hash(bytes.as_ref()).to_hex().to_string()
}

pub fn image_resource_key(byte_len: usize, digest: &str) -> String {
    resource_key("img", byte_len, digest)
}

/// 그림 op 이 내보내는 바이트의 신원 키 (Task #3315).
///
/// 위의 `image_resource_key` 는 arena 에 담긴 바이트의 **내용** 키(blake3)다. 이쪽은
/// 리페인트마다 다시 계산돼야 하므로 내용 해시를 쓸 수 없다 — 그림 1장에 수 MB 를 매 키
/// 입력마다 훑게 된다. 대신 문서가 이미 가진 신원을 쓴다.
///
/// - `bin_data_id`: 등록이 append-only 라 세션 중 id→바이트가 안정하다.
/// - `epoch`: 문서를 통째로 갈아끼우는 연산(스냅샷 복원, 새 문서, `set_document`)만이 이
///   안정성을 깨므로, 그때만 올라간다. 그림을 **추가**하는 것은 기존 id 의 바이트를 바꾸지
///   않으므로 올리지 않는다 — 올리면 무관한 그림의 캐시까지 함께 버려진다.
/// - variant: JPEG 워터마크 bake는 같은 원본에서도 base64 payload를 바꾸므로 구분한다.
///   BMP/PCX/TIFF/회색 JPEG 변환 여부는 원본 바이트만으로 결정되므로 별도 variant가
///   필요 없다. 이렇게 해야 작은 키 조회가 변환 메모의 전체 바이트 해시를 되풀이하지 않는다.
///
/// 접두어를 `img:` 가 아니라 `bin:` 으로 둔 것은 위 내용 키(`img:blake3:…`)와 한눈에
/// 구분되게 하기 위해서다. 두 키는 서로 다른 이름공간이다.
///
/// 바이트를 내보내지 않는 op, 그리고 문서 BinData 에 대응하지 않는 합성 이미지
/// (`bin_data_id == 0`)는 안정된 신원이 없으므로 `None` 이다 — 소비자는 캐시 대상에서
/// 제외한다.
pub fn source_image_key(
    bin_data_epoch: u32,
    image: &crate::renderer::render_tree::ImageNode,
) -> Option<String> {
    if image.data.is_none() || image.bin_data_id == 0 {
        return None;
    }

    // 실제로 내보내는 바이트와 같은 출력 변형을 단일 권위에서 고른다.
    let variant = crate::renderer::image_resolver::source_image_variant(image);
    let tag = match variant {
        SourceImageVariant::WmfPictureTone {
            brightness,
            contrast,
            grayscale,
        } => format!("wmftone_{brightness}_{contrast}_{}", u8::from(grayscale)),
        SourceImageVariant::PictureTone {
            brightness,
            contrast,
            effect,
        } => {
            format!("pictone_{brightness}_{contrast}_{effect}")
        }
        _ => variant.as_str().to_owned(),
    };
    Some(format!(
        "bin:{bin_data_epoch}:{}:{}",
        image.bin_data_id, tag
    ))
}

/// 창·장치별 바이트는 원본 bin 재조회로 복원할 수 없다.
pub(crate) fn source_image_key_for_payload(
    bin_data_epoch: u32,
    image: &crate::renderer::render_tree::ImageNode,
    resolved: Option<&crate::paint::ResolvedImagePayload>,
) -> Option<String> {
    if resolved.is_some_and(|p| {
        matches!(
            p.kind,
            crate::paint::ResolvedImageKind::OutputDevice
                | crate::paint::ResolvedImageKind::OutputDeviceWindow
                | crate::paint::ResolvedImageKind::SourceWindow
        )
    }) {
        None
    } else {
        source_image_key(bin_data_epoch, image)
    }
}

/// `source_image_key` 의 variant — 같은 원본에서 다른 바이트가 나오는 갈림 (Task #3315).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceImageVariant {
    /// 원본 바이트. 브라우저가 못 읽는 포맷(BMP/PCX/TIFF/회색 JPEG)이면 PNG 변환까지.
    Source,
    /// JPEG 워터마크를 한컴 규칙으로 bake 한 PNG.
    BakedWatermarkPng,
    /// 원본 해상도에서 흑백을 확정한 PNG.
    BakedBlackWhitePng,
    /// 일반 raster의 원본 포맷 변환 뒤 밝기·대비·효과를 확정한 PNG.
    PictureTone {
        brightness: i8,
        contrast: i8,
        effect: u8,
    },
    /// WMF 색 처리. producer의 밝기·명암·회색조 결과를 재현한다.
    WmfPictureTone {
        brightness: i8,
        contrast: i8,
        grayscale: bool,
    },
    /// 한컴 2024 Print의 원본색 bitmap 전달. 팔레트 범위면 원본 경로를 보존한다.
    Hancom2024Bitmap,
}

impl SourceImageVariant {
    pub fn as_str(self) -> &'static str {
        match self {
            SourceImageVariant::Source => "src",
            SourceImageVariant::BakedWatermarkPng => "wmpng",
            SourceImageVariant::BakedBlackWhitePng => "bwpng",
            SourceImageVariant::PictureTone { .. } => "pictone",
            SourceImageVariant::WmfPictureTone { .. } => "wmftone",
            SourceImageVariant::Hancom2024Bitmap => "h24bmp",
        }
    }

    pub fn bakes_watermark(self) -> bool {
        matches!(self, SourceImageVariant::BakedWatermarkPng)
    }
}

/// `source_image_key` 가 만든 키를 되읽는다 (Task #3315).
///
/// 발급과 해석을 같은 모듈에 묶는다 — 소비자가 문자열을 직접 쪼개게 두면 접두어나 variant
/// 를 바꿀 때 조용히 어긋난다. 모르는 variant 는 받아들이지 않는다: `src` 로 넘겨 버리면
/// 워터마크 그림에 원본 JPEG 을 돌려주고도 성공한 것처럼 보인다.
pub fn parse_source_image_key(key: &str) -> Option<(u32, u16, SourceImageVariant)> {
    let mut parts = key.split(':');
    if parts.next()? != "bin" {
        return None;
    }
    let epoch = parts.next()?.parse::<u32>().ok()?;
    let bin_data_id = parts.next()?.parse::<u16>().ok()?;
    let variant = match parts.next()? {
        "src" => SourceImageVariant::Source,
        "wmpng" => SourceImageVariant::BakedWatermarkPng,
        "bwpng" => SourceImageVariant::BakedBlackWhitePng,
        tag if tag.starts_with("pictone_") => {
            let mut values = tag.strip_prefix("pictone_")?.split('_');
            let brightness = values.next()?.parse::<i8>().ok()?;
            let contrast = values.next()?.parse::<i8>().ok()?;
            let effect = values.next()?.parse::<u8>().ok()?;
            if values.next().is_some()
                || !(-100..=100).contains(&brightness)
                || !(-100..=100).contains(&contrast)
                || effect > 2
            {
                return None;
            }
            SourceImageVariant::PictureTone {
                brightness,
                contrast,
                effect,
            }
        }
        tag if tag.starts_with("wmftone_") => {
            let mut values = tag.strip_prefix("wmftone_")?.split('_');
            let brightness = values.next()?.parse::<i8>().ok()?;
            let contrast = values.next()?.parse::<i8>().ok()?;
            let grayscale = match values.next()? {
                "0" => false,
                "1" => true,
                _ => return None,
            };
            if values.next().is_some() {
                return None;
            }
            SourceImageVariant::WmfPictureTone {
                brightness,
                contrast,
                grayscale,
            }
        }
        "h24bmp" => SourceImageVariant::Hancom2024Bitmap,
        _ => return None,
    };
    if parts.next().is_some() {
        return None;
    }
    Some((epoch, bin_data_id, variant))
}

pub fn svg_resource_key(byte_len: usize, digest: &str) -> String {
    resource_key("svg", byte_len, digest)
}

pub fn font_blob_resource_key(byte_len: usize, digest: &str) -> String {
    resource_key("font", byte_len, digest)
}

/// Exact portable font resource key를 검증하고 `(byte_len, digest)`로 푼다.
///
/// 길이와 digest의 표기까지 canonical해야 한다. 느슨하게 해석하면 같은 font bytes에 여러
/// cache key가 생겨 document-generation cache의 상한과 무효화 계약을 우회할 수 있다.
pub fn parse_font_blob_resource_key(key: &str) -> Option<(usize, &str)> {
    let value = key.strip_prefix("font:blake3:")?;
    let (byte_len_text, digest) = value.split_once(':')?;
    if byte_len_text.is_empty()
        || (byte_len_text.len() > 1 && byte_len_text.starts_with('0'))
        || digest.len() != 64
        || !digest
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    {
        return None;
    }
    let byte_len = byte_len_text.parse::<usize>().ok()?;
    (byte_len > 0).then_some((byte_len, digest))
}

fn resource_key(kind: &str, byte_len: usize, digest: &str) -> String {
    format!("{kind}:{RESOURCE_KEY_ALGORITHM}:{byte_len}:{digest}")
}

#[cfg(test)]
mod tests {
    use super::{
        font_blob_resource_key, image_resource_key, resource_digest_hex, resource_fingerprint,
        source_image_key, svg_resource_key, BinaryResourceKind, BinaryResourceRef,
        FontBlobResourceId, ImageResourceId, ResourceArena, SvgResourceId,
    };
    use crate::model::image::ImageEffect;
    use crate::renderer::render_tree::ImageNode;

    #[test]
    fn interns_duplicate_resources_once() {
        let mut arena = ResourceArena::default();
        let image_a = arena.intern_image_bytes(&[1, 2, 3, 4]);
        let image_b = arena.intern_image_bytes(&[1, 2, 3, 4]);
        let svg_a = arena.intern_svg_fragment("<svg/>");
        let svg_b = arena.intern_svg_fragment("<svg/>");
        let font_a = arena.intern_font_blob_bytes(&[5, 6, 7, 8]);
        let font_b = arena.intern_font_blob_bytes(&[5, 6, 7, 8]);

        assert_eq!(image_a, ImageResourceId(0));
        assert_eq!(image_b, ImageResourceId(0));
        assert_eq!(arena.image_count(), 1);
        assert_eq!(arena.image_bytes(image_a), Some(&[1, 2, 3, 4][..]));
        assert_eq!(arena.image_hash(image_a), arena.image_hash(image_b));
        assert!(arena.image_hash(image_a).is_some());
        assert_eq!(
            arena.image_fingerprint(image_a),
            Some(resource_fingerprint([1, 2, 3, 4]))
        );
        let image_key = image_resource_key(4, &resource_digest_hex([1, 2, 3, 4]));
        assert_eq!(arena.image_resource_key(image_a), Some(image_key.as_str()));
        assert_eq!(
            arena.image_resources().collect::<Vec<_>>(),
            vec![(ImageResourceId(0), &[1, 2, 3, 4][..])]
        );

        assert_eq!(svg_a, SvgResourceId(0));
        assert_eq!(svg_b, SvgResourceId(0));
        assert_eq!(arena.svg_count(), 1);
        assert_eq!(arena.svg_fragment(svg_a), Some("<svg/>"));
        assert_eq!(arena.svg_hash(svg_a), arena.svg_hash(svg_b));
        assert!(arena.svg_hash(svg_a).is_some());
        assert_eq!(
            arena.svg_fingerprint(svg_a),
            Some(resource_fingerprint("<svg/>"))
        );
        let svg_key = svg_resource_key(6, &resource_digest_hex("<svg/>"));
        assert_eq!(arena.svg_resource_key(svg_a), Some(svg_key.as_str()));
        assert_eq!(
            arena.svg_resources().collect::<Vec<_>>(),
            vec![(SvgResourceId(0), "<svg/>")]
        );

        assert_eq!(font_a, FontBlobResourceId(0));
        assert_eq!(font_b, FontBlobResourceId(0));
        assert_eq!(arena.font_blob_count(), 1);
        assert_eq!(arena.font_blob_bytes(font_a), Some(&[5, 6, 7, 8][..]));
        assert_eq!(arena.font_blob_hash(font_a), arena.font_blob_hash(font_b));
        assert!(arena.font_blob_hash(font_a).is_some());
        assert_eq!(
            arena.font_blob_fingerprint(font_a),
            Some(resource_fingerprint([5, 6, 7, 8]))
        );
        let font_key = font_blob_resource_key(4, &resource_digest_hex([5, 6, 7, 8]));
        assert_eq!(
            arena.font_blob_resource_key(font_a),
            Some(font_key.as_str())
        );
        assert_eq!(
            arena.font_blob_resources().collect::<Vec<_>>(),
            vec![(FontBlobResourceId(0), &[5, 6, 7, 8][..])]
        );
    }

    #[test]
    fn resolves_font_blob_bytes_by_versioned_resource_ref() {
        let mut arena = ResourceArena::default();
        let font_id = arena.intern_font_blob_bytes(&[9, 8, 7, 6]);
        let digest = resource_digest_hex([9, 8, 7, 6]);
        let data_ref = BinaryResourceRef {
            kind: BinaryResourceKind::FontBlob,
            id: font_blob_resource_key(4, &digest),
        };

        assert_eq!(font_id, FontBlobResourceId(0));
        assert_eq!(
            arena.font_blob_bytes_for_ref(&data_ref),
            Some(&[9, 8, 7, 6][..])
        );
        assert_eq!(
            arena.font_blob_bytes_for_ref(&BinaryResourceRef {
                kind: BinaryResourceKind::ExternalFont,
                id: font_blob_resource_key(4, &digest),
            }),
            None
        );
        assert_eq!(
            arena.font_blob_bytes_for_ref(&BinaryResourceRef {
                kind: BinaryResourceKind::FontBlob,
                id: font_blob_resource_key(5, &digest),
            }),
            None
        );
    }

    #[test]
    fn resource_digest_is_stable_and_content_dependent() {
        let digest = resource_digest_hex([1, 2, 3, 4]);
        assert_eq!(digest.len(), 64);
        assert_eq!(digest, resource_digest_hex([1, 2, 3, 4]));
        assert_ne!(digest, resource_digest_hex([1, 2, 3, 5]));
    }

    #[test]
    fn resource_keys_include_kind_algorithm_length_and_digest() {
        assert_eq!(image_resource_key(4, "abcd"), "img:blake3:4:abcd");
        assert_eq!(
            svg_resource_key(6, "0123456789abcdef"),
            "svg:blake3:6:0123456789abcdef"
        );
        assert_eq!(font_blob_resource_key(8, "feed"), "font:blake3:8:feed");
    }

    #[test]
    fn source_image_key_tracks_picture_tones_and_legacy_watermarks() {
        // MIME 판정은 다른 포맷의 8-byte magic과 같은 최소 길이 계약을 사용한다.
        let mut image = ImageNode::new(
            7,
            Some(vec![0xff, 0xd8, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00]),
        );
        assert_eq!(source_image_key(3, &image).as_deref(), Some("bin:3:7:src"));

        image.effect = ImageEffect::GrayScale;
        image.brightness = 1;
        // 일반 색조는 legacy 워터마크와 구분한 요청 신원을 사용한다.
        // 실제 바이트·RGB·decode 실패 계약은 공개 API 회귀에서 따로 검사한다.
        assert_eq!(
            source_image_key(3, &image).as_deref(),
            Some("bin:3:7:pictone_1_0_1")
        );

        image.brightness = 0;
        image.contrast = 1;
        assert_eq!(
            source_image_key(3, &image).as_deref(),
            Some("bin:3:7:pictone_0_1_1")
        );

        // 실제 워터마크의 두 축과 JPEG 형식이 함께 있을 때만 전용 bake다.
        image.brightness = 1;
        assert_eq!(
            source_image_key(3, &image).as_deref(),
            Some("bin:3:7:wmpng")
        );
        image.effect = ImageEffect::RealPic;
        assert_eq!(
            source_image_key(3, &image).as_deref(),
            Some("bin:3:7:pictone_1_1_0")
        );
        image.effect = ImageEffect::GrayScale;

        // JPEG 이 아니면 효과 필드는 별도 JSON 속성일 뿐 base64 payload는 바뀌지 않는다.
        image.data = Some(vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]);
        assert_eq!(source_image_key(3, &image).as_deref(), Some("bin:3:7:src"));

        image.bin_data_id = 0;
        assert_eq!(source_image_key(3, &image), None);
    }
}