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
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
use resvg::{tiny_skia, usvg};
use skia_safe::{
    canvas::SrcRectConstraint, codec, color_filters, image::RequiredProperties, AlphaType, Codec,
    Color, ColorSpace, ColorType, CubicResampler, Data, FilterMode, IRect, Image, ImageInfo,
    Matrix, MipmapMode, Paint, Rect, SamplingOptions, TileMode,
};
use std::sync::{Arc, OnceLock};

use super::cache::byte_key;
use super::image_cache::{CachedImageResource, ImageResourceCache};
use super::image_effect::binary_effect_png;

use crate::model::image::ImageEffect;
use crate::model::style::ImageFillMode;
use crate::renderer::image_resolver::{detect_image_mime_type, grayscale_jpeg_bytes_to_png_bytes};

const MAX_SVG_FRAGMENT_BYTES: usize = 4 * 1024 * 1024;
// 완결된 WMF/EMF/EPS 그림은 내장 래스터를 data URI로 포함한다.
// 조각과 별도 예산을 사용하며 EPS 변환기의 16MiB 출력 범위도 수용한다.
// 조각 상한과 래스터 픽셀 상한은 유지한다.
const MAX_SVG_DOCUMENT_BYTES: usize = 16 * 1024 * 1024;
const MAX_SVG_RASTER_PIXELS: u64 = 67_108_864;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ImageSampling {
    filter_mode: FilterMode,
    mipmap_mode: MipmapMode,
    profile: crate::paint::RenderProfile,
    paper_grid: Option<crate::renderer::bitmap_raster::PrintPaperGrid>,
    print_page: Option<[u64; 3]>,
    strict_resource_failures: bool,
}

impl ImageSampling {
    pub(super) fn nearest() -> Self {
        Self {
            filter_mode: FilterMode::Nearest,
            mipmap_mode: MipmapMode::None,
            profile: crate::paint::RenderProfile::Screen,
            paper_grid: None,
            print_page: None,
            strict_resource_failures: false,
        }
    }

    pub(super) fn linear_mipmap() -> Self {
        Self {
            filter_mode: FilterMode::Linear,
            mipmap_mode: MipmapMode::Linear,
            profile: crate::paint::RenderProfile::Screen,
            paper_grid: None,
            print_page: None,
            strict_resource_failures: false,
        }
    }

    pub fn linear() -> Self {
        Self {
            filter_mode: FilterMode::Linear,
            mipmap_mode: MipmapMode::None,
            profile: crate::paint::RenderProfile::Screen,
            paper_grid: None,
            print_page: None,
            strict_resource_failures: false,
        }
    }

    pub fn for_profile(profile: crate::paint::RenderProfile) -> Self {
        Self {
            profile,
            ..Self::linear()
        }
    }

    pub(super) fn with_profile(mut self, profile: crate::paint::RenderProfile) -> Self {
        self.profile = profile;
        self
    }

    pub(super) fn with_strict_resources(mut self, strict: bool) -> Self {
        self.strict_resource_failures = strict;
        self
    }

    pub(super) fn with_paper_grid(
        mut self,
        grid: Option<crate::renderer::bitmap_raster::PrintPaperGrid>,
    ) -> Self {
        self.paper_grid = grid;
        self
    }

    pub(super) fn options(self) -> SamplingOptions {
        SamplingOptions::new(self.filter_mode, self.mipmap_mode)
    }

    pub(super) fn with_print_page(mut self, page: Option<(f64, f64, f64)>) -> Self {
        self.print_page = page.map(|(w, h, scale)| [w.to_bits(), h.to_bits(), scale.to_bits()]);
        self
    }
}

/// SkImage::from_encoded only creates a lazy image: even invalid/incomplete
/// compressed pixels can pass that header check and later disappear at draw.
/// Decode this image resource, not the destination surface, and accept only a
/// complete codec success. Skia remains the format/color-profile authority.
fn full_image_decode_succeeds(encoded: &Data) -> bool {
    use crate::renderer::image_header::{
        CANVASKIT_MAX_IMAGE_DIMENSION, CANVASKIT_MAX_IMAGE_PIXELS,
    };

    let Some(mut codec) = Codec::from_data(encoded.clone()) else {
        return false;
    };
    let source = codec.info();
    let (Ok(width), Ok(height)) = (
        u32::try_from(source.width()),
        u32::try_from(source.height()),
    ) else {
        return false;
    };
    let pixels = u64::from(width) * u64::from(height);
    if width == 0
        || height == 0
        || width > CANVASKIT_MAX_IMAGE_DIMENSION
        || height > CANVASKIT_MAX_IMAGE_DIMENSION
        || pixels > CANVASKIT_MAX_IMAGE_PIXELS
    {
        return false;
    }
    let info = ImageInfo::new(
        (source.width(), source.height()),
        ColorType::RGBA8888,
        AlphaType::Premul,
        source.color_space(),
    );
    let row_bytes = info.min_row_bytes();
    let Some(byte_len) = row_bytes.checked_mul(height as usize) else {
        return false;
    };
    let max_decode_memory = (CANVASKIT_MAX_IMAGE_PIXELS * 4) as usize;
    if byte_len > max_decode_memory {
        return false;
    }
    let mut decoded = Vec::new();
    if decoded.try_reserve_exact(byte_len).is_err() {
        return false;
    }
    decoded.resize(byte_len, 0);
    let options = codec::Options {
        zero_initialized: codec::ZeroInitialized::Yes,
        max_decode_memory: Some(max_decode_memory),
        ..Default::default()
    };
    codec.get_pixels_with_options(&info, &mut decoded, row_bytes, Some(&options))
        == codec::Result::Success
}

pub(super) fn draw_svg_fragment(
    canvas: &skia_safe::Canvas,
    cache: &ImageResourceCache,
    svg_fragment: &str,
    x: f32,
    y: f32,
    width: f32,
    height: f32,
    sampling: ImageSampling,
    raster_scale: f32,
) -> bool {
    // [Issue #2292] RawSvg 조각은 페이지 절대 좌표로 방출된다(SVG 백엔드
    // 직접 삽입·web_canvas 와 동일 계약). viewBox 원점에 조각의 페이지
    // 위치(x, y)를 넘겨 bbox 창만 래스터한다 — (0,0) 가정 시 창 밖 콘텐츠
    // 전부 클리핑 + bbox 재배치 이중 오프셋으로 차트가 잘렸다.
    let key = byte_key(
        b"raw-svg",
        svg_fragment.as_bytes(),
        &(x, y, width, height, raster_scale),
    );
    let cached = cache.get(key, false);
    let png = if let Some(CachedImageResource::SvgPng(png)) = cached {
        png
    } else {
        let Some(png) =
            rasterize_svg_fragment_to_png(svg_fragment, x, y, width, height, raster_scale)
        else {
            return false;
        };
        let png = Arc::new(png);
        if let Some(key) = key {
            cache.insert(
                key,
                CachedImageResource::SvgPng(png.clone()),
                png.capacity(),
            );
        }
        png
    };
    draw_image_bytes(
        canvas,
        cache,
        &png,
        x,
        y,
        width,
        height,
        Some(ImageFillMode::FitToSize),
        None,
        None,
        None,
        ImageEffect::RealPic,
        0,
        0,
        sampling,
    )
}

pub(super) fn draw_image_bytes(
    canvas: &skia_safe::Canvas,
    cache: &ImageResourceCache,
    bytes: &[u8],
    x: f32,
    y: f32,
    width: f32,
    height: f32,
    fill_mode: Option<ImageFillMode>,
    original_size: Option<(f64, f64)>,
    crop: Option<(i32, i32, i32, i32)>,
    crop_reference_size: Option<(u32, u32)>,
    effect: ImageEffect,
    brightness: i8,
    contrast: i8,
    sampling: ImageSampling,
) -> bool {
    // 빈 자르기 선택은 정상적인 빈 출력이며 누락 그림 대체 표시 대상이 아니다.
    if crop.is_some_and(|(l, t, r, b)| r <= l || b <= t) {
        return true;
    }
    let is_valid_destination_rect = |x: f32, y: f32, width: f32, height: f32| {
        x.is_finite()
            && y.is_finite()
            && width.is_finite()
            && height.is_finite()
            && width > 0.0
            && height > 0.0
    };
    let is_valid_image_size = |width: f32, height: f32| {
        width.is_finite() && height.is_finite() && width > 0.0 && height > 0.0
    };
    let grayscale_filter = |scale: f32, translate: f32| {
        let r = 0.299 * scale;
        let g = 0.587 * scale;
        let b = 0.114 * scale;
        color_filters::matrix_row_major(
            &[
                r, g, b, 0.0, translate, r, g, b, 0.0, translate, r, g, b, 0.0, translate, 0.0,
                0.0, 0.0, 1.0, 0.0,
            ],
            None,
        )
    };
    let image_effect_filter = |effect: ImageEffect| match effect {
        ImageEffect::RealPic => None,
        ImageEffect::GrayScale => Some(grayscale_filter(1.0, 0.0)),
        ImageEffect::BlackWhite => Some(grayscale_filter(255.0, -127.5)),
        ImageEffect::Pattern8x8 => Some(grayscale_filter(1.0, 0.0)),
    };
    let brightness_contrast_filter = |brightness: i8, contrast: i8| {
        let brightness = brightness.clamp(-100, 100) as f32 / 100.0;
        let slope = (100.0 + contrast.clamp(-100, 100) as f32) / 100.0;
        // Skia의 matrix_4x5는 정규화된 RGBA에 적용된 뒤 0..1로 제한된다.
        // SVG와 같은 절편을 전달한다. 255를 곱하면 양수 밝기의 그림이 흰색으로 소실된다.
        let intercept = (0.5 - 0.5 * slope) + brightness;
        color_filters::matrix_row_major(
            &[
                slope, 0.0, 0.0, 0.0, intercept, 0.0, slope, 0.0, 0.0, intercept, 0.0, 0.0, slope,
                0.0, intercept, 0.0, 0.0, 0.0, 1.0, 0.0,
            ],
            None,
        )
    };
    let resolve_image_placement = |fill_mode: ImageFillMode,
                                   x: f32,
                                   y: f32,
                                   width: f32,
                                   height: f32,
                                   image_width: f32,
                                   image_height: f32| {
        match fill_mode {
            ImageFillMode::LeftTop => (x, y),
            ImageFillMode::CenterTop => (x + (width - image_width) / 2.0, y),
            ImageFillMode::RightTop => (x + width - image_width, y),
            ImageFillMode::LeftCenter => (x, y + (height - image_height) / 2.0),
            ImageFillMode::Center => (
                x + (width - image_width) / 2.0,
                y + (height - image_height) / 2.0,
            ),
            ImageFillMode::RightCenter => {
                (x + width - image_width, y + (height - image_height) / 2.0)
            }
            ImageFillMode::LeftBottom => (x, y + height - image_height),
            ImageFillMode::CenterBottom => {
                (x + (width - image_width) / 2.0, y + height - image_height)
            }
            ImageFillMode::RightBottom => (x + width - image_width, y + height - image_height),
            _ => (x, y),
        }
    };
    let draw_missing_image_placeholder = |x: f32, y: f32, width: f32, height: f32| {
        let rect = Rect::from_xywh(x, y, width, height);
        let mut fill = Paint::default();
        fill.set_anti_alias(true);
        fill.set_style(skia_safe::paint::Style::Fill);
        fill.set_color(Color::from_argb(48, 96, 96, 96));
        canvas.draw_rect(rect, &fill);

        let mut stroke = Paint::default();
        stroke.set_anti_alias(true);
        stroke.set_style(skia_safe::paint::Style::Stroke);
        stroke.set_stroke_width(1.0);
        stroke.set_color(Color::from_argb(160, 96, 96, 96));
        canvas.draw_rect(rect, &stroke);
    };

    if !is_valid_destination_rect(x, y, width, height) {
        return false;
    }
    // Check the original span before JPEG normalization, tone/effect expansion
    // and print fast paths. The normalized span is checked again below.
    if sampling.strict_resource_failures && bytes.len() > crate::model::bin_data::MAX_BIN_DATA_BYTES
    {
        return false;
    }
    if effect == ImageEffect::RealPic
        && brightness == 0
        && contrast == 0
        && super::metafile_print::draw(canvas, bytes)
    {
        return true;
    }
    if crate::renderer::image_resolver::png_sampling::print_separate_alpha(bytes) {
        if let Some([w, h, scale]) = sampling.print_page {
            if super::print_image_sampling::draw_masked(
                canvas,
                bytes,
                Rect::from_xywh(x, y, width, height),
                (f64::from_bits(w), f64::from_bits(h)),
                f64::from_bits(scale),
            ) {
                return true;
            }
        }
    }
    // [#3460] HWPX BinData 는 SVG 를 그대로 담는다(`<hc:img>` → `Format="svg"`). skia 디코더는
    // SVG 를 읽지 못해 종전에는 회색 자리표시자만 남았다. 같은 모듈의 resvg 로 목적지 크기에
    // 맞춰 먼저 래스터화하면 이후 크롭·채움·효과 경로를 그대로 탈 수 있다.
    let svg_size = crate::renderer::svg_fragment::is_svg_prefix(bytes)
        .then(|| (width, height, canvas_raster_scale(canvas)));
    let binary_effect =
        matches!(effect, ImageEffect::BlackWhite | ImageEffect::Pattern8x8).then_some(effect);
    // 같은 원본의 중립 캐시가 색조 변환을 우회하지 않도록 변환 조건도 구분한다.
    let tone = (brightness != 0 || contrast != 0).then_some((effect, brightness, contrast));
    // 목적지를 늘려 채우는 SVG는 장치 픽셀 격자에서 바로 래스터화한다. 목적지 크기 래스터를
    // 소수 좌표에 다시 놓으면 재표본이 0.5px 선과 작은 글자를 이웃 두 픽셀로 흐린다.
    // crop·타일·정렬 배치와 원본 픽셀을 바꾸는 이진·색조 효과는 기존 경로를 유지한다.
    let svg_device_window = svg_size
        .filter(|_| !is_raster_only_svg(bytes))
        .filter(|_| {
            binary_effect.is_none()
                && tone.is_none()
                && matches!(
                    fill_mode.unwrap_or(ImageFillMode::FitToSize),
                    ImageFillMode::FitToSize | ImageFillMode::Total
                )
        })
        .and_then(|(_, _, scale)| {
            SvgDeviceWindow::new(
                &canvas.local_to_device_as_3x3(),
                Rect::from_xywh(x, y, width, height),
                scale,
            )
        })
        .filter(|window| {
            // 원본 전체를 고른 자르기 값은 아래 배치 경로의 판정과 같이 자르지 않은 것으로 본다.
            crop.is_none_or(|crop_rect| {
                let (src_x, src_y, src_w, src_h) = crate::renderer::svg::compute_image_crop_src(
                    crop_rect,
                    crop_reference_size,
                    f64::from(window.width),
                    f64::from(window.height),
                );
                crate::renderer::svg::image_source_is_full(
                    (src_x, src_y, src_w, src_h),
                    f64::from(window.width),
                    f64::from(window.height),
                )
            })
        });
    let key = byte_key(
        b"decoded-image",
        bytes,
        &(
            svg_size,
            svg_device_window,
            binary_effect,
            tone,
            sampling.strict_resource_failures,
        ),
    );
    let cached = cache.get(key, binary_effect.is_some());
    let (image, binary_preprocessed, encoded) = if let Some(CachedImageResource::Decoded {
        image,
        encoded,
        binary_preprocessed,
        ..
    }) = cached
    {
        (image, binary_preprocessed, encoded)
    } else {
        let svg_raster = svg_size.and_then(|(width, height, scale)| match svg_device_window {
            Some(window) => rasterize_svg_document_in_device_window(bytes, window),
            None => rasterize_svg_document_to_png(bytes, width, height, scale),
        });
        let bytes = svg_raster.as_deref().unwrap_or(bytes);
        let normalized_bytes = if detect_image_mime_type(bytes) == "image/jpeg" {
            grayscale_jpeg_bytes_to_png_bytes(bytes)
        } else {
            None
        };
        let encoded_bytes = normalized_bytes.as_deref().unwrap_or(bytes);
        // 한컴 색조를 원본 픽셀에 먼저 확정하고 동일 캐시·배치 경로로 재생한다.
        // 재귀 입력은 보정 PNG와 중립 효과이므로 색조를 두 번 적용하지 않는다.
        if let Some(adjusted) = crate::renderer::image_resolver::adjusted_picture_bytes(
            encoded_bytes,
            effect,
            brightness,
            contrast,
        ) {
            return draw_image_bytes(
                canvas,
                cache,
                &adjusted,
                x,
                y,
                width,
                height,
                fill_mode,
                original_size,
                crop,
                crop_reference_size,
                ImageEffect::RealPic,
                0,
                0,
                sampling,
            );
        }
        // Binary effects are applied in source pixels before placement/filtering.
        // Embedded SVG is keyed by destination and device scale above, so its
        // raster-specific dither cannot leak to a differently sized placement.
        let binary_png = binary_effect.and_then(|effect| binary_effect_png(encoded_bytes, effect));
        let encoded_bytes = binary_png.as_deref().unwrap_or(encoded_bytes);
        if sampling.strict_resource_failures
            && encoded_bytes.len() > crate::model::bin_data::MAX_BIN_DATA_BYTES
        {
            return false;
        }
        // 디코더와 캐시가 같은 immutable Data를 공유한다. 캐시 hit도 실제
        // 정규화·전처리된 바이트를 전달해 원본/색조 전 바이트로 되돌리지 않는다.
        let encoded = Data::new_copy(encoded_bytes);
        // Strict cache entries are inserted only after a successful full
        // decode. Their policy key cannot reuse an unvalidated CPU lazy image.
        if sampling.strict_resource_failures && !full_image_decode_succeeds(&encoded) {
            return false;
        }
        let Some(image) = Image::from_encoded(encoded.clone()) else {
            if binary_effect.is_some() {
                cache.record_preprocessing(None);
            }
            draw_missing_image_placeholder(x, y, width, height);
            return false;
        };
        if binary_effect.is_some() {
            cache.record_preprocessing(binary_png.as_ref().map(|_| {
                (image.width().max(0) as usize)
                    .saturating_mul(image.height().max(0) as usize)
                    .saturating_mul(4)
            }));
        }
        if let Some(key) = key {
            let cost = (image.width().max(0) as usize)
                .saturating_mul(image.height().max(0) as usize)
                .saturating_mul(4)
                .saturating_add(encoded_bytes.len());
            cache.insert(
                key,
                CachedImageResource::Decoded {
                    image: image.clone(),
                    encoded: encoded.clone(),
                    binary_requested: binary_effect.is_some(),
                    binary_preprocessed: binary_png.is_some(),
                },
                cost,
            );
        }
        (image, binary_png.is_some(), encoded)
    };

    // Interpolation after source-pixel thresholding would reintroduce gray
    // pixels and shift ordered-dither phase. Failed preprocessing retains the
    // old filter/sampling fallback, including on a decoded-image cache hit.
    let (filter_effect, sampling) = if binary_preprocessed {
        (ImageEffect::RealPic, ImageSampling::nearest())
    } else {
        if binary_effect.is_some() {
            cache.record_filter_fallback();
        }
        (effect, sampling)
    };

    let dst = Rect::from_xywh(x, y, width, height);
    let mut paint = Paint::default();
    paint.set_anti_alias(true);
    let has_adjustments = brightness != 0 || contrast != 0;
    let use_linear_adjustments =
        has_adjustments && matches!(effect, ImageEffect::RealPic | ImageEffect::GrayScale);
    let effect_key = byte_key(b"image-effect", &[], &(filter_effect, brightness, contrast));
    let cached_effect = cache.get(effect_key, false);
    let color_filter = if let Some(CachedImageResource::Effect(filter)) = cached_effect {
        filter
    } else {
        let effect_filter = image_effect_filter(filter_effect);
        let adjustment_filter = (brightness != 0 || contrast != 0)
            .then(|| brightness_contrast_filter(brightness, contrast));
        let filter = match (effect_filter, adjustment_filter) {
            (Some(effect), Some(adjustment)) if use_linear_adjustments => {
                color_filters::compose(effect, adjustment)
            }
            (Some(effect), Some(adjustment)) => color_filters::compose(adjustment, effect),
            (Some(effect), None) => Some(effect),
            (None, Some(adjustment)) => Some(adjustment),
            (None, None) => None,
        };
        if let Some(key) = effect_key {
            cache.insert(key, CachedImageResource::Effect(filter.clone()), 256);
        }
        filter
    };
    if let Some(color_filter) = color_filter {
        // SVG 필터의 기본 색 공간은 linearRGB다. 검증한 일반 그림·회색조의
        // 보정 체인 전체를 같은 공간에서 계산한다.
        let color_filter = if use_linear_adjustments {
            let Some(filter) = color_filter.with_working_color_space(ColorSpace::new_srgb_linear())
            else {
                return false;
            };
            filter
        } else {
            color_filter
        };
        paint.set_color_filter(color_filter);
    }

    let mode = fill_mode.unwrap_or(ImageFillMode::FitToSize);
    let decoded_width = image.width() as f32;
    let decoded_height = image.height() as f32;
    let crop_src = crop.and_then(|crop_rect| {
        if decoded_width <= 0.0 || decoded_height <= 0.0 {
            return None;
        }
        let (src_x, src_y, src_w, src_h) = crate::renderer::svg::compute_image_crop_src(
            crop_rect,
            crop_reference_size,
            decoded_width as f64,
            decoded_height as f64,
        );
        let is_cropped = !crate::renderer::svg::image_source_is_full(
            (src_x, src_y, src_w, src_h),
            f64::from(decoded_width),
            f64::from(decoded_height),
        );
        let (src_x, src_y, src_w, src_h) = (src_x as f32, src_y as f32, src_w as f32, src_h as f32);
        if is_cropped && src_w > 0.0 && src_h > 0.0 {
            Some(Rect::from_xywh(src_x, src_y, src_w, src_h))
        } else {
            None
        }
    });

    let draw_image_rect = |src: Option<Rect>, dst: Rect| {
        if sampling.mipmap_mode == MipmapMode::Linear && src.is_none() && !binary_preprocessed {
            if let Some(frame) = super::super::print_raster::PrintRasterFrame::read(bytes) {
                if super::anisotropic_image::draw_print_frame(canvas, &image, frame, &paint) {
                    return;
                }
            }
        }
        if src.is_none() && !binary_preprocessed && sampling.mipmap_mode == MipmapMode::Linear {
            if let Some([w, h, scale]) = sampling.print_page {
                if super::print_image_sampling::draw(
                    canvas,
                    &image,
                    dst,
                    &paint,
                    (f64::from_bits(w), f64::from_bits(h)),
                    f64::from_bits(scale),
                ) {
                    return;
                }
            }
        }
        let matrix = canvas.local_to_device_as_3x3();
        let scale_x = matrix.scale_x().hypot(matrix.skew_y());
        let scale_y = matrix.skew_x().hypot(matrix.scale_y());
        let source = src.unwrap_or(Rect::from_xywh(0., 0., decoded_width, decoded_height));
        let interpolation = super::super::image_resolver::png_sampling::magnification(
            super::super::image_resolver::png_sampling::interpolation(bytes),
            (source.width() as f64, source.height() as f64),
            (
                (dst.width() * scale_x) as f64,
                (dst.height() * scale_y) as f64,
            ),
        );
        // 확대 선언과 binary effect의 우선순위를 보존한다.
        let sampling = if binary_preprocessed {
            sampling
        } else {
            match interpolation {
                Some(false) => ImageSampling::nearest().with_profile(sampling.profile),
                Some(true) => ImageSampling::linear().with_profile(sampling.profile),
                None => sampling,
            }
        };
        if sampling.mipmap_mode == MipmapMode::Linear
            && src.is_none()
            && !binary_preprocessed
            && super::super::image_resolver::png_sampling::print_jpeg_transfer(bytes)
            && super::anisotropic_image::draw(canvas, &image, dst, &paint)
        {
            return;
        }
        let transform = canvas.local_to_device_as_3x3();
        // SVG는 위에서 이미 실제 장치 크기로 raster했다. 이를 다시 bitmap
        // 축소 경로에 넣으면 벡터의 가는 선까지 두 번 필터링하게 된다.
        let prepared = (svg_size.is_none()
            && !binary_preprocessed
            && !matrix.has_perspective()
            && sampling.filter_mode != FilterMode::Nearest)
            .then(|| {
                let bbox = crate::renderer::render_tree::BoundingBox::new(
                    dst.x() as f64,
                    dst.y() as f64,
                    dst.width() as f64,
                    dst.height() as f64,
                );
                let matrix = [
                    transform.scale_x() as f64,
                    transform.skew_y() as f64,
                    transform.skew_x() as f64,
                    transform.scale_y() as f64,
                    transform.translate_x() as f64,
                    transform.translate_y() as f64,
                ];
                if let Some(source) = src {
                    crate::renderer::bitmap_raster::prepare_cropped_bitmap(
                        encoded.as_bytes(),
                        bbox,
                        matrix,
                        sampling.profile,
                        sampling.paper_grid,
                        [
                            source.x() as f64,
                            source.y() as f64,
                            source.width() as f64,
                            source.height() as f64,
                        ],
                    )
                } else {
                    crate::renderer::bitmap_raster::prepare_bitmap(
                        encoded.as_bytes(),
                        bbox,
                        matrix,
                        sampling.profile,
                        sampling.paper_grid,
                    )
                }
            })
            .flatten();
        let filtered = prepared
            .as_ref()
            .and_then(|prepared| Image::from_encoded(Data::new_copy(prepared.bytes.as_slice())));
        let (image, dst) = match (filtered.as_ref(), prepared.as_ref()) {
            (Some(image), Some(prepared)) => (
                image,
                Rect::from_xywh(
                    prepared.bbox.x as f32,
                    prepared.bbox.y as f32,
                    prepared.bbox.width as f32,
                    prepared.bbox.height as f32,
                ),
            ),
            _ => (&image, dst),
        };
        // 전체 원본의 축소에는 가는 선을 보존하는 저역 통과를 적용한다.
        // crop의 strict 경계와 binary·빠른 미리보기의 명시적 nearest를 유지한다.
        let cubic = filtered.is_none()
            && sampling.mipmap_mode == MipmapMode::Linear
            && src.is_none()
            && !binary_preprocessed
            && super::super::image_resolver::png_sampling::print_bitmap_reconstruction(bytes)
            && !matrix.has_perspective()
            && matrix.skew_x() == 0.0
            && matrix.skew_y() == 0.0
            && matrix.scale_x() > 0.0
            && matrix.scale_y() > 0.0
            && super::super::cubic_sampling::modest_reduction(
                (f64::from(image.width()), f64::from(image.height())),
                (
                    f64::from(dst.width() * scale_x),
                    f64::from(dst.height() * scale_y),
                ),
            );
        let reduced = (filtered.is_none()
            && !cubic
            && sampling.filter_mode != FilterMode::Nearest
            && src.is_none()
            && !binary_preprocessed
            && !matrix.has_perspective()
            && scale_x.is_finite()
            && scale_y.is_finite()
            && (image.width() as f32 > dst.width() * scale_x
                || image.height() as f32 > dst.height() * scale_y))
            .then(|| image.with_default_mipmaps())
            .flatten();
        let actual_image = reduced.as_ref().unwrap_or(image);
        let options = if filtered.is_some() {
            SamplingOptions::new(FilterMode::Nearest, MipmapMode::None)
        } else if cubic {
            SamplingOptions::from(CubicResampler::catmull_rom())
        } else if reduced.is_some() {
            SamplingOptions::new(FilterMode::Linear, MipmapMode::Linear)
        } else {
            sampling.options()
        };
        if let Some(src) = src.as_ref().filter(|_| filtered.is_none()) {
            canvas.draw_image_rect_with_sampling_options(
                actual_image,
                Some((src, SrcRectConstraint::Strict)),
                dst,
                options,
                &paint,
            );
        } else {
            canvas.draw_image_rect_with_sampling_options(actual_image, None, dst, options, &paint);
        }
    };

    if let Some(window) = svg_device_window {
        // 래스터 한 픽셀이 장치 한 픽셀이다. 다시 표본하지 않고 창의 정수 원점에 놓는다.
        canvas.save();
        canvas.reset_matrix();
        canvas.draw_image_rect_with_sampling_options(
            &image,
            None,
            window.device_rect(),
            SamplingOptions::new(FilterMode::Nearest, MipmapMode::None),
            &paint,
        );
        canvas.restore();
        return true;
    }
    if matches!(mode, ImageFillMode::FitToSize | ImageFillMode::Total) {
        draw_image_rect(crop_src, dst);
        return true;
    }
    if matches!(mode, ImageFillMode::None | ImageFillMode::Zoom) {
        // [#7235] 채우기 유형 15(NONE): 종횡비를 지켜 영역 가운데에 맞춘다.
        let (source_width, source_height) = crop_src
            .map(|src| (src.width(), src.height()))
            .unwrap_or((decoded_width, decoded_height));
        if !is_valid_image_size(source_width, source_height) {
            draw_missing_image_placeholder(x, y, width, height);
            return false;
        }
        let scale = (width / source_width).min(height / source_height);
        let fit_width = source_width * scale;
        let fit_height = source_height * scale;
        draw_image_rect(
            crop_src,
            Rect::from_xywh(
                x + (width - fit_width) / 2.0,
                y + (height - fit_height) / 2.0,
                fit_width,
                fit_height,
            ),
        );
        return true;
    }

    let image_width = original_size
        .map(|(width, _)| width as f32)
        .unwrap_or_else(|| image.width() as f32);
    let image_height = original_size
        .map(|(_, height)| height as f32)
        .unwrap_or_else(|| image.height() as f32);
    if !is_valid_image_size(image_width, image_height) {
        draw_missing_image_placeholder(x, y, width, height);
        return false;
    }

    canvas.save();
    canvas.clip_rect(dst, None, Some(true));

    if matches!(
        mode,
        ImageFillMode::TileAll
            | ImageFillMode::Total
            | ImageFillMode::TileHorzTop
            | ImageFillMode::TileHorzBottom
            | ImageFillMode::TileVertLeft
            | ImageFillMode::TileVertRight
    ) {
        let shader_image = crop_src
            .and_then(|src| {
                let left = src.left.floor().max(0.0) as i32;
                let top = src.top.floor().max(0.0) as i32;
                let right = src.right.ceil().min(decoded_width) as i32;
                let bottom = src.bottom.ceil().min(decoded_height) as i32;
                if right <= left || bottom <= top {
                    return None;
                }
                image.make_subset(
                    None,
                    IRect::from_xywh(left, top, right - left, bottom - top),
                    RequiredProperties::default(),
                )
            })
            .unwrap_or_else(|| image.clone());
        let shader_source_width = shader_image.width() as f32;
        let shader_source_height = shader_image.height() as f32;
        let draw_tiled_shader = |tile_rect: Rect, origin_x: f32, origin_y: f32| -> bool {
            if shader_source_width <= 0.0 || shader_source_height <= 0.0 {
                return false;
            }
            let scale_x = image_width / shader_source_width;
            let scale_y = image_height / shader_source_height;
            if !scale_x.is_finite() || !scale_y.is_finite() || scale_x <= 0.0 || scale_y <= 0.0 {
                return false;
            }
            // Skia's local matrix maps source pixels into logical canvas
            // coordinates; shader lookup performs the inverse internally.
            let local_matrix = Matrix::scale_translate((scale_x, scale_y), (origin_x, origin_y));
            let Some(shader) = shader_image.to_shader(
                Some((TileMode::Repeat, TileMode::Repeat)),
                sampling.options(),
                Some(&local_matrix),
            ) else {
                return false;
            };
            let mut shader_paint = paint.clone();
            shader_paint.set_shader(shader);
            canvas.draw_rect(tile_rect, &shader_paint);
            true
        };

        if matches!(mode, ImageFillMode::TileAll | ImageFillMode::Total)
            && draw_tiled_shader(dst, x, y)
        {
            canvas.restore();
            return true;
        }
        if matches!(
            mode,
            ImageFillMode::TileHorzTop | ImageFillMode::TileHorzBottom
        ) {
            let tile_y = if matches!(mode, ImageFillMode::TileHorzTop) {
                y
            } else {
                y + height - image_height
            };
            if draw_tiled_shader(Rect::from_xywh(x, tile_y, width, image_height), x, tile_y) {
                canvas.restore();
                return true;
            }
        }
        if matches!(
            mode,
            ImageFillMode::TileVertLeft | ImageFillMode::TileVertRight
        ) {
            let tile_x = if matches!(mode, ImageFillMode::TileVertLeft) {
                x
            } else {
                x + width - image_width
            };
            if draw_tiled_shader(Rect::from_xywh(tile_x, y, image_width, height), tile_x, y) {
                canvas.restore();
                return true;
            }
        }
        // A valid decoded image can still lack a usable shader matrix. Retain
        // the committed bounded draw fallback instead of dropping that image.
        let step_x = image_width.max(1.0);
        let step_y = image_height.max(1.0);
        let horizontal = matches!(
            mode,
            ImageFillMode::TileHorzTop | ImageFillMode::TileHorzBottom
        );
        let vertical = matches!(
            mode,
            ImageFillMode::TileVertLeft | ImageFillMode::TileVertRight
        );
        let columns = if vertical {
            1
        } else {
            (width / step_x).ceil() as usize
        };
        let rows = if horizontal {
            1
        } else {
            (height / step_y).ceil() as usize
        };
        let draws = columns.saturating_mul(rows);
        let origin_x = if mode == ImageFillMode::TileVertRight {
            x + width - image_width
        } else {
            x
        };
        let origin_y = if mode == ImageFillMode::TileHorzBottom {
            y + height - image_height
        } else {
            y
        };
        const MAX_TILE_DRAWS: usize = 4096;
        for index in 0..draws.min(MAX_TILE_DRAWS) {
            let tile_x = origin_x + (index % columns) as f32 * step_x;
            let tile_y = origin_y + (index / columns) as f32 * step_y;
            draw_image_rect(
                crop_src,
                Rect::from_xywh(tile_x, tile_y, image_width, image_height),
            );
        }
        if draws > MAX_TILE_DRAWS {
            cache.record_tile_cap();
        }
        canvas.restore();
        return true;
    } else {
        let (image_x, image_y) =
            resolve_image_placement(mode, x, y, width, height, image_width, image_height);
        draw_image_rect(
            crop_src,
            Rect::from_xywh(image_x, image_y, image_width, image_height),
        );
    }

    canvas.restore();
    true
}

/// [#3460] 캔버스 변환에서 래스터 배율을 얻는다 (확대 출력에서 SVG 가 뭉개지지 않게).
fn canvas_raster_scale(canvas: &skia_safe::Canvas) -> f32 {
    let matrix = canvas.local_to_device_as_3x3();
    // Host canvases can rotate or shear the page. Diagonal entries alone lose
    // that scale (a quarter turn has zero diagonals); use the largest stretch
    // while preserving the existing bounded SVG document rasterization policy.
    let scale = matrix.max_scale();
    if scale.is_finite() && scale > 0.0 {
        scale.clamp(1.0, 4.0)
    } else {
        1.0
    }
}

/// [#3460] 완결된 SVG 문서(BinData 그림)를 목적지 크기에 맞춰 PNG 로 래스터화한다.
///
/// 조각(fragment) 경로와 달리 원본이 자체 `viewBox`/크기를 가진 문서이므로, 트리 크기를
/// 목적지 픽셀 크기로 맞추는 스케일 변환만 적용한다.
fn rasterize_svg_document_to_png(
    bytes: &[u8],
    width: f32,
    height: f32,
    raster_scale: f32,
) -> Option<Vec<u8>> {
    if bytes.is_empty()
        || bytes.len() > MAX_SVG_DOCUMENT_BYTES
        || !width.is_finite()
        || !height.is_finite()
        || !raster_scale.is_finite()
        || width <= 0.0
        || height <= 0.0
        || raster_scale <= 0.0
    {
        return None;
    }
    let raster_width = (width * raster_scale).ceil().max(1.0) as u64;
    let raster_height = (height * raster_scale).ceil().max(1.0) as u64;
    if raster_width
        .checked_mul(raster_height)
        .is_none_or(|pixels| pixels > MAX_SVG_RASTER_PIXELS)
    {
        return None;
    }

    let options = svg_parse_options();
    let tree = usvg::Tree::from_data(bytes, &options).ok()?;
    let size = tree.size();
    if !(size.width() > 0.0 && size.height() > 0.0) {
        return None;
    }

    let transform = tiny_skia::Transform::from_scale(
        raster_width as f32 / size.width(),
        raster_height as f32 / size.height(),
    );
    if let Some(png) = super::svg_bitmap_mask::rasterize_before_reduction(
        &tree,
        raster_width as u32,
        raster_height as u32,
        transform,
        MAX_SVG_RASTER_PIXELS,
    ) {
        return Some(png);
    }

    rasterize_svg_with_mask_coverage(
        &tree,
        raster_width as u32,
        raster_height as u32,
        transform,
        raster_scale,
    )
}

/// 실제 paint가 래스터 그림뿐이면 기존 비트맵 재표본 경로를 유지한다.
/// 설명·미사용 정의·그룹 유무로 작은 로고의 표본 위상을 바꾸지 않는다.
fn is_raster_only_svg(bytes: &[u8]) -> bool {
    if bytes.len() > MAX_SVG_DOCUMENT_BYTES {
        return false;
    }
    let Ok(xml) = std::str::from_utf8(bytes) else {
        return false;
    };
    let Ok(document) = roxmltree::Document::parse(xml) else {
        return false;
    };
    let root = document.root_element();
    if root.tag_name().name() != "svg" {
        return false;
    }
    let mut work = vec![root];
    let mut has_raster = false;
    while let Some(node) = work.pop() {
        match node.tag_name().name() {
            "svg" | "g" => work.extend(node.children().filter(|child| child.is_element())),
            // use와 별도 vector paint는 아래에서 제외하므로 미사용 정의는 내용이 아니다.
            "defs" | "title" | "desc" | "metadata" | "style" => {}
            "image" => {
                let href = node
                    .attribute("href")
                    .or_else(|| node.attribute(("http://www.w3.org/1999/xlink", "href")))
                    .unwrap_or_default();
                if ![
                    "data:image/png;base64,",
                    "data:image/jpeg;base64,",
                    "data:image/gif;base64,",
                    "data:image/webp;base64,",
                ]
                .iter()
                .any(|prefix| href.starts_with(prefix))
                    || node.children().any(|child| child.is_element())
                {
                    return false;
                }
                has_raster = true;
            }
            _ => return false,
        }
    }
    has_raster
}

/// 축 정렬 캔버스에서 그림 목적지를 덮는 장치 픽셀 창과 그 안의 소수 위상.
#[derive(Clone, Copy, Debug, PartialEq)]
struct SvgDeviceWindow {
    left: f32,
    top: f32,
    width: u32,
    height: u32,
    phase_x: f32,
    phase_y: f32,
    device_width: f32,
    device_height: f32,
    raster_scale: f32,
}

impl SvgDeviceWindow {
    fn new(matrix: &Matrix, dst: Rect, raster_scale: f32) -> Option<Self> {
        // 장치 픽셀이 CSS 픽셀보다 거친 캔버스(direct PDF의 pt 좌표 등)는 기존 해상도 하한을 지킨다.
        if !matrix.is_scale_translate() || matrix.scale_x() < 1.0 || matrix.scale_y() < 1.0 {
            return None;
        }
        let (device, _) = matrix.map_rect(dst);
        let (left, top) = (device.left.floor(), device.top.floor());
        let (width, height) = (device.right.ceil() - left, device.bottom.ceil() - top);
        let valid = [left, top, width, height, device.width(), device.height()]
            .iter()
            .all(|value| value.is_finite())
            && device.width() > 0.0
            && device.height() > 0.0
            && (width as f64) * (height as f64) <= MAX_SVG_RASTER_PIXELS as f64;
        valid.then(|| Self {
            left,
            top,
            width: width as u32,
            height: height as u32,
            phase_x: device.left - left,
            phase_y: device.top - top,
            device_width: device.width(),
            device_height: device.height(),
            raster_scale,
        })
    }

    fn device_rect(&self) -> Rect {
        Rect::from_xywh(self.left, self.top, self.width as f32, self.height as f32)
    }
}

/// 완결된 SVG 문서를 장치 목적지 크기로 늘리고 창 안의 소수 위상만큼 옮겨 래스터화한다.
fn rasterize_svg_document_in_device_window(
    bytes: &[u8],
    window: SvgDeviceWindow,
) -> Option<Vec<u8>> {
    if bytes.is_empty() || bytes.len() > MAX_SVG_FRAGMENT_BYTES {
        return None;
    }
    let options = svg_parse_options();
    let tree = usvg::Tree::from_data(bytes, &options).ok()?;
    let size = tree.size();
    if !(size.width() > 0.0 && size.height() > 0.0) {
        return None;
    }
    let transform = tiny_skia::Transform::from_scale(
        window.device_width / size.width(),
        window.device_height / size.height(),
    )
    .post_translate(window.phase_x, window.phase_y);
    if let Some(png) = super::svg_bitmap_mask::rasterize_before_reduction(
        &tree,
        window.width,
        window.height,
        transform,
        MAX_SVG_RASTER_PIXELS,
    ) {
        return Some(png);
    }
    rasterize_svg_with_mask_coverage(
        &tree,
        window.width,
        window.height,
        transform,
        window.raster_scale,
    )
}

fn rasterize_svg_fragment_to_png(
    svg_fragment: &str,
    src_x: f32,
    src_y: f32,
    width: f32,
    height: f32,
    raster_scale: f32,
) -> Option<Vec<u8>> {
    if svg_fragment.is_empty()
        || svg_fragment.len() > MAX_SVG_FRAGMENT_BYTES
        || !src_x.is_finite()
        || !src_y.is_finite()
        || !width.is_finite()
        || !height.is_finite()
        || !raster_scale.is_finite()
        || width <= 0.0
        || height <= 0.0
        || raster_scale <= 0.0
    {
        return None;
    }
    let output_width = width * raster_scale;
    let output_height = height * raster_scale;
    let raster_width = output_width.ceil() as u64;
    let raster_height = output_height.ceil() as u64;
    if raster_width
        .checked_mul(raster_height)
        .is_none_or(|pixels| pixels > MAX_SVG_RASTER_PIXELS)
    {
        return None;
    }

    // [Issue #2292] 조각은 페이지 절대 좌표 — viewBox 를 조각의 페이지 좌표
    // 창(src_x, src_y 원점)으로 지정해 bbox 영역만 (0,0) 래스터로 사상한다.
    // WMF·OLE 조각의 xlink:href도 같은 문서에서 해석한다. 선언이 빠지면
    // 포함 비트맵까지 XML 파싱에서 탈락한다. 외부 참조 차단은 resolver가 유지한다.
    let svg = format!(
        "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"{output_width:.2}\" height=\"{output_height:.2}\" viewBox=\"{src_x:.2} {src_y:.2} {width:.2} {height:.2}\">{svg_fragment}</svg>"
    );
    let options = svg_parse_options();
    let tree = usvg::Tree::from_str(&svg, &options).ok()?;
    let size = tree.size().to_int_size();
    let pixels = u64::from(size.width()).checked_mul(u64::from(size.height()))?;
    if pixels == 0 || pixels > MAX_SVG_RASTER_PIXELS {
        return None;
    }

    rasterize_svg_with_mask_coverage(
        &tree,
        size.width(),
        size.height(),
        tiny_skia::Transform::default(),
        raster_scale,
    )
}

/// 축소된 패턴 마스크의 피복률을 면적으로 적분한다. 위치·viewport·최종 크기는 보존한다.
fn rasterize_svg_with_mask_coverage(
    tree: &usvg::Tree,
    width: u32,
    height: u32,
    transform: tiny_skia::Transform,
    raster_scale: f32,
) -> Option<Vec<u8>> {
    // WMF의 미세한 흑백 마스크를 목적지 픽셀에서 한 번만 샘플링하면
    // 검정/흰색 중 한쪽만 선택되어 칠과 글자가 사라진다. 마스크가 있는
    // SVG에만 축마다 최대 4배 표본을 두고 최종 출력 크기로 저역 통과한다.
    // 이미 고배율인 출력과 마스크 없는 SVG는 기존 경로를 유지한다.
    let mut samples = if tree.masks().is_empty() {
        1
    } else {
        (4.0 / raster_scale.max(1.0)).floor().clamp(1.0, 4.0) as u32
    };
    let pixels = u64::from(width).checked_mul(u64::from(height))?;
    if pixels == 0 || pixels > MAX_SVG_RASTER_PIXELS {
        return None;
    }
    while pixels.checked_mul(u64::from(samples).pow(2))? > MAX_SVG_RASTER_PIXELS {
        samples -= 1;
    }
    let render_width = width.checked_mul(samples)?;
    let render_height = height.checked_mul(samples)?;
    let mut pixmap = tiny_skia::Pixmap::new(render_width, render_height)?;
    let transform =
        tiny_skia::Transform::from_scale(samples as f32, samples as f32).post_concat(transform);
    resvg::render(tree, transform, &mut pixmap.as_mut());
    if samples == 1 {
        return pixmap.encode_png().ok();
    }

    // 투명 가장자리의 색 번짐을 막기 위해 premultiplied RGBA 상태에서 적분한다.
    // PNG 인코더가 마지막에만 알파를 되돌린다. Lanczos의 작은 overshoot도
    // premultiplied 불변식(R/G/B <= A) 안으로 제한한다.
    super::svg_bitmap_mask::encode_reduced(&pixmap, width, height)
}

fn svg_parse_options() -> usvg::Options<'static> {
    let mut options = usvg::Options::default();
    options.resources_dir = None;
    options.image_href_resolver = usvg::ImageHrefResolver {
        resolve_data: usvg::ImageHrefResolver::default_data_resolver(),
        resolve_string: Box::new(|_, _| None),
    };
    options.fontdb = svg_fontdb();
    options
}

fn svg_fontdb() -> Arc<usvg::fontdb::Database> {
    static SVG_FONTDB: OnceLock<Arc<usvg::fontdb::Database>> = OnceLock::new();

    SVG_FONTDB
        .get_or_init(|| {
            // [Issue #2293] PDF 경로(renderer/pdf.rs::create_fontdb)와 동일
            // 규약: 시스템 폰트 + 프로젝트 ttfs/(재귀) + WSL 윈도우 폰트.
            // 종전에는 시스템 폰트만 로드하고 generic 폴백을 존재 확인 없이
            // 하드 고정("Noto Sans CJK KR")해, 해당 폰트가 없는 환경에서
            // resvg 가 조각의 텍스트를 통째로 드롭했다.
            let mut fontdb = usvg::fontdb::Database::new();
            fontdb.load_system_fonts();
            // [#2864] 조달 순서는 renderer::font_paths 가 단일 정의한다.
            // ttfs/opensource 번들이 최후 폴백으로 남아 한국어 드롭을 막는다(#2293).
            crate::renderer::font_paths::load_into_fontdb(&mut fontdb, &[]);

            // generic 폴백은 실존하는 첫 후보로 (매칭 실패 = 텍스트 드롭 방지).
            let sans = first_existing_family(
                &fontdb,
                &[
                    "Noto Sans CJK KR",
                    "Noto Sans KR",
                    "함초롬돋움",
                    "HCR Dotum",
                    "맑은 고딕",
                    "Malgun Gothic",
                    "NanumGothic",
                    "나눔고딕",
                    "DejaVu Sans",
                ],
            );
            // [작업지시자 권고] 폴백은 한국어 가용 폰트를 우선한다 — 스타일
            // (serif/mono) 정합보다 한글이 보이는 것이 우선이므로, 라틴 전용
            // 최후 폴백(DejaVu) 앞에 한국어 sans 를 둔다. 저장소 체크아웃에는
            // ttfs/opensource/NotoSansKR 이 항상 있어 한국어 폴백이 보장된다.
            let serif = first_existing_family(
                &fontdb,
                &[
                    "Noto Serif CJK KR",
                    "Noto Serif KR",
                    "함초롬바탕",
                    "HCR Batang",
                    "바탕",
                    "Batang",
                    "NanumMyeongjo",
                    "나눔명조",
                    "Noto Sans KR",
                    "DejaVu Serif",
                ],
            );
            let mono = first_existing_family(
                &fontdb,
                &[
                    "D2Coding",
                    "D2Coding ligature",
                    "Noto Sans KR",
                    "DejaVu Sans Mono",
                ],
            );
            if let Some(f) = sans {
                fontdb.set_sans_serif_family(f);
            }
            if let Some(f) = serif {
                fontdb.set_serif_family(f);
            }
            if let Some(f) = mono {
                fontdb.set_monospace_family(f);
            }
            Arc::new(fontdb)
        })
        .clone()
}

/// [Issue #2293] fontdb 에 실존하는 첫 패밀리 — 후보가 전부 없으면 None
/// (usvg 기본 generic 매핑 유지, 폴백 지정으로 오히려 드롭되는 것을 방지).
fn first_existing_family(fontdb: &usvg::fontdb::Database, candidates: &[&str]) -> Option<String> {
    let mut families = std::collections::HashSet::new();
    for face in fontdb.faces() {
        for (name, _) in &face.families {
            families.insert(name.clone());
        }
    }
    candidates
        .iter()
        .find(|c| families.contains(**c))
        .map(|c| c.to_string())
}