rhwp 0.9.1

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
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
//! 폰트 메트릭 DB 생성 도구
//!
//! TTF 파일에서 head/cmap/hmtx/maxp/name 테이블을 파싱하여
//! 글리프 폭 데이터를 추출하고, Rust 소스코드로 출력한다.
//!
//! 사용법:
//!   cargo run --features dev-tools --bin font-metric-gen -- ttfs/windows/malgun.ttf --face-index 0
//!   cargo run --features dev-tools --bin font-metric-gen -- --plan plan.json \
//!     --generated-output generated.rs --metadata-output provenance.json
//!
//! 생성 모드는 입력 순서와 TTC face를 계획 파일에 명시해야 한다. core lookup과
//! measured/manual overlay는 이 도구의 소유가 아니므로 출력 대상으로 지정할 수 없다.

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

const GENERATOR_SOURCE_BYTES: &[u8] = include_bytes!("font_metric_gen.rs");
static STAGED_OUTPUT_SEQUENCE: AtomicU64 = AtomicU64::new(0);

// ─── TTF 바이너리 파싱 헬퍼 ───

fn read_u16_be(data: &[u8], off: usize) -> u16 {
    ((data[off] as u16) << 8) | (data[off + 1] as u16)
}

fn read_u32_be(data: &[u8], off: usize) -> u32 {
    ((data[off] as u32) << 24)
        | ((data[off + 1] as u32) << 16)
        | ((data[off + 2] as u32) << 8)
        | (data[off + 3] as u32)
}

fn read_i16_be(data: &[u8], off: usize) -> i16 {
    read_u16_be(data, off) as i16
}

fn tag_str(data: &[u8], off: usize) -> String {
    String::from_utf8_lossy(&data[off..off + 4]).to_string()
}

fn sha256_bytes(data: &[u8]) -> String {
    Sha256::digest(data)
        .iter()
        .map(|byte| format!("{:02x}", byte))
        .collect()
}

fn sha256_file(path: &Path) -> Result<String, String> {
    fs::read(path)
        .map(|data| sha256_bytes(&data))
        .map_err(|error| format!("{}: {}", path.display(), error))
}

// ─── TTF 테이블 디렉토리 ───

#[derive(Debug)]
struct TableEntry {
    tag: String,
    offset: u32,
    length: u32,
}

/// TTC 내 모든 폰트의 오프셋 배열 반환 (단일 TTF면 [0])
fn get_font_offsets(data: &[u8]) -> Vec<usize> {
    if data.len() >= 12 && &data[0..4] == b"ttcf" {
        let num_fonts = read_u32_be(data, 8) as usize;
        (0..num_fonts)
            .map(|i| read_u32_be(data, 12 + i * 4) as usize)
            .collect()
    } else {
        vec![0]
    }
}

fn parse_table_directory_at(data: &[u8], header_off: usize) -> Vec<TableEntry> {
    let num_tables = read_u16_be(data, header_off + 4);

    let mut tables = Vec::new();
    for i in 0..num_tables as usize {
        let entry_off = header_off + 12 + i * 16;
        if entry_off + 16 > data.len() {
            break;
        }
        tables.push(TableEntry {
            tag: tag_str(data, entry_off),
            offset: read_u32_be(data, entry_off + 8),
            length: read_u32_be(data, entry_off + 12),
        });
    }
    tables
}

fn table_offset(tables: &[TableEntry], tag: &str) -> Option<usize> {
    tables
        .iter()
        .find(|t| t.tag == tag)
        .map(|t| t.offset as usize)
}

// ─── head 테이블: unitsPerEm, macStyle ───

struct HeadInfo {
    units_per_em: u16,
    mac_style: u16, // bit0=Bold, bit1=Italic
}

fn parse_head(data: &[u8], tables: &[TableEntry]) -> HeadInfo {
    let off = table_offset(tables, "head").expect("head 테이블 없음");
    HeadInfo {
        units_per_em: read_u16_be(data, off + 18),
        mac_style: read_u16_be(data, off + 44),
    }
}

// ─── maxp 테이블: numGlyphs ───

fn parse_maxp(data: &[u8], tables: &[TableEntry]) -> u16 {
    let off = table_offset(tables, "maxp").expect("maxp 테이블 없음");
    read_u16_be(data, off + 4) // numGlyphs at offset 4
}

// ─── cmap 테이블: Unicode → Glyph ID ───

fn parse_cmap(data: &[u8], tables: &[TableEntry]) -> HashMap<u32, u16> {
    let cmap_off = table_offset(tables, "cmap").expect("cmap 테이블 없음");
    let num_subtables = read_u16_be(data, cmap_off + 2) as usize;

    let mut map = HashMap::new();

    // 우선순위: platformID=3(Windows) encodingID=10(UCS-4, Format 12) > encodingID=1(BMP, Format 4)
    let mut format4_off = None;
    let mut format12_off = None;

    for i in 0..num_subtables {
        let rec = cmap_off + 4 + i * 8;
        if rec + 8 > data.len() {
            break;
        }
        let platform_id = read_u16_be(data, rec);
        let encoding_id = read_u16_be(data, rec + 2);
        let subtable_off = cmap_off + read_u32_be(data, rec + 4) as usize;

        if subtable_off >= data.len() {
            continue;
        }
        let format = read_u16_be(data, subtable_off);

        if platform_id == 3 {
            if encoding_id == 1 && format == 4 {
                format4_off = Some(subtable_off);
            }
            if encoding_id == 10 && format == 12 {
                format12_off = Some(subtable_off);
            }
        }
        // platformID=0 (Unicode)도 폴백으로
        if platform_id == 0 {
            if format == 4 && format4_off.is_none() {
                format4_off = Some(subtable_off);
            }
            if format == 12 && format12_off.is_none() {
                format12_off = Some(subtable_off);
            }
        }
    }

    // Format 12 파싱 (전체 유니코드)
    if let Some(off) = format12_off {
        let n_groups = read_u32_be(data, off + 12) as usize;
        for g in 0..n_groups {
            let rec = off + 16 + g * 12;
            if rec + 12 > data.len() {
                break;
            }
            let start_char = read_u32_be(data, rec);
            let end_char = read_u32_be(data, rec + 4);
            let start_glyph = read_u32_be(data, rec + 8);
            for c in start_char..=end_char {
                let gid = start_glyph + (c - start_char);
                map.insert(c, gid as u16);
            }
        }
        return map;
    }

    // Format 4 파싱 (BMP only)
    if let Some(off) = format4_off {
        let seg_count = read_u16_be(data, off + 6) as usize / 2;
        let end_codes_off = off + 14;
        let start_codes_off = end_codes_off + seg_count * 2 + 2; // +2 for reservedPad
        let id_delta_off = start_codes_off + seg_count * 2;
        let id_range_off = id_delta_off + seg_count * 2;

        for seg in 0..seg_count {
            let end_code = read_u16_be(data, end_codes_off + seg * 2) as u32;
            let start_code = read_u16_be(data, start_codes_off + seg * 2) as u32;
            let id_delta = read_i16_be(data, id_delta_off + seg * 2) as i32;
            let id_range_offset = read_u16_be(data, id_range_off + seg * 2) as usize;

            if start_code == 0xFFFF {
                break;
            }

            for c in start_code..=end_code {
                let gid = if id_range_offset == 0 {
                    ((c as i32 + id_delta) & 0xFFFF) as u16
                } else {
                    let glyph_idx_off =
                        id_range_off + seg * 2 + id_range_offset + ((c - start_code) as usize) * 2;
                    if glyph_idx_off + 2 <= data.len() {
                        let gid = read_u16_be(data, glyph_idx_off);
                        if gid != 0 {
                            ((gid as i32 + id_delta) & 0xFFFF) as u16
                        } else {
                            0
                        }
                    } else {
                        0
                    }
                };
                if gid != 0 {
                    map.insert(c, gid);
                }
            }
        }
    }

    map
}

// ─── hmtx 테이블: Glyph ID → advance width ───

fn parse_hmtx(data: &[u8], tables: &[TableEntry], num_glyphs: u16) -> Vec<u16> {
    let hmtx_off = table_offset(tables, "hmtx").expect("hmtx 테이블 없음");
    // hhea 테이블에서 numberOfHMetrics 읽기
    let hhea_off = table_offset(tables, "hhea").expect("hhea 테이블 없음");
    let num_h_metrics = read_u16_be(data, hhea_off + 34) as usize;

    let mut widths = Vec::with_capacity(num_glyphs as usize);

    // longHorMetric[numberOfHMetrics]: advanceWidth(u16) + lsb(i16) = 4바이트씩
    let mut last_width = 0u16;
    for i in 0..num_h_metrics.min(num_glyphs as usize) {
        let w = read_u16_be(data, hmtx_off + i * 4);
        widths.push(w);
        last_width = w;
    }

    // 나머지 글리프는 마지막 width 반복 (leftSideBearing만 다름)
    for _ in num_h_metrics..num_glyphs as usize {
        widths.push(last_width);
    }

    widths
}

// ─── name 테이블 ───

const REQUIRED_NAME_IDS: [u16; 8] = [1, 2, 3, 4, 6, 16, 17, 25];

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct NamingRecord {
    record_index: usize,
    platform_id: u16,
    encoding_id: u16,
    language_id: u16,
    name_id: u16,
    value: String,
}

fn decode_name_record(platform_id: u16, bytes: &[u8]) -> Option<String> {
    if platform_id == 0 || platform_id == 3 {
        if !bytes.len().is_multiple_of(2) {
            return None;
        }
        let units = bytes
            .chunks_exact(2)
            .map(|pair| u16::from_be_bytes([pair[0], pair[1]]));
        let value: String = char::decode_utf16(units)
            .map(|item| item.unwrap_or(char::REPLACEMENT_CHARACTER))
            .collect();
        return Some(value);
    }
    if platform_id == 1 {
        return Some(String::from_utf8_lossy(bytes).into_owned());
    }
    None
}

fn parse_naming_records(data: &[u8], tables: &[TableEntry]) -> Vec<NamingRecord> {
    let Some(name_off) = table_offset(tables, "name") else {
        return Vec::new();
    };
    let count = read_u16_be(data, name_off + 2) as usize;
    let string_offset = name_off + read_u16_be(data, name_off + 4) as usize;
    let mut records = Vec::new();

    for i in 0..count {
        let rec = name_off + 6 + i * 12;
        if rec + 12 > data.len() {
            break;
        }
        let platform_id = read_u16_be(data, rec);
        let encoding_id = read_u16_be(data, rec + 2);
        let language_id = read_u16_be(data, rec + 4);
        let name_id = read_u16_be(data, rec + 6);
        let length = read_u16_be(data, rec + 8) as usize;
        let offset = string_offset + read_u16_be(data, rec + 10) as usize;

        if !REQUIRED_NAME_IDS.contains(&name_id) || offset + length > data.len() {
            continue;
        }
        if let Some(value) = decode_name_record(platform_id, &data[offset..offset + length]) {
            records.push(NamingRecord {
                record_index: i,
                platform_id,
                encoding_id,
                language_id,
                name_id,
                value,
            });
        }
    }
    records
}

fn select_family_name(records: &[NamingRecord]) -> String {
    for name_id in [16, 1] {
        if let Some(record) = records
            .iter()
            .find(|record| record.name_id == name_id && record.platform_id == 3)
        {
            return record.value.clone();
        }
        if let Some(record) = records.iter().find(|record| record.name_id == name_id) {
            return record.value.clone();
        }
    }

    String::new()
}

// ─── 폰트 메트릭 데이터 구조 ───

#[derive(Debug)]
struct FontMetric {
    family_name: String,
    file_name: String,
    source_sha256: String,
    face_index: u32,
    naming_records: Vec<NamingRecord>,
    em_size: u16,
    bold: bool,
    italic: bool,
    /// Unicode codepoint → advance width (em 단위)
    char_widths: HashMap<u32, u16>,
}

fn parse_ttf_all(path: &Path) -> Result<Vec<FontMetric>, String> {
    let data = fs::read(path).map_err(|e| format!("{}: {}", path.display(), e))?;
    if data.len() < 12 {
        return Err(format!("{}: 파일이 너무 작음", path.display()));
    }

    let offsets = get_font_offsets(&data);
    let file_name = path
        .file_name()
        .unwrap_or_default()
        .to_string_lossy()
        .to_string();
    let mut results = Vec::new();

    let source_sha256 = sha256_bytes(&data);
    for (face_index, &font_off) in offsets.iter().enumerate() {
        let tables = parse_table_directory_at(&data, font_off);
        if tables.is_empty() {
            continue;
        }

        let head = parse_head(&data, &tables);
        let num_glyphs = parse_maxp(&data, &tables);
        let cmap = parse_cmap(&data, &tables);
        let hmtx = parse_hmtx(&data, &tables, num_glyphs);
        let naming_records = parse_naming_records(&data, &tables);
        let family_name = select_family_name(&naming_records);

        let mut char_widths = HashMap::new();
        for (&codepoint, &glyph_id) in &cmap {
            if (glyph_id as usize) < hmtx.len() {
                char_widths.insert(codepoint, hmtx[glyph_id as usize]);
            }
        }

        results.push(FontMetric {
            family_name,
            file_name: file_name.clone(),
            source_sha256: source_sha256.clone(),
            face_index: face_index as u32,
            naming_records,
            em_size: head.units_per_em,
            bold: (head.mac_style & 0x01) != 0,
            italic: (head.mac_style & 0x02) != 0,
            char_widths,
        });
    }

    if results.is_empty() {
        Err(format!("{}: 폰트 없음", path.display()))
    } else {
        Ok(results)
    }
}

fn parse_ttf_face(path: &Path, face_index: u32) -> Result<FontMetric, String> {
    let metrics = parse_ttf_all(path)?;
    metrics
        .into_iter()
        .find(|metric| metric.face_index == face_index)
        .ok_or_else(|| format!("{}: TTC face index {}가 없음", path.display(), face_index))
}

// ─── 한글 음절 분해 압축 ───

const HANGUL_BASE: u32 = 0xAC00;
const HANGUL_END: u32 = 0xD7A3;
const CHO_COUNT: u32 = 19;
const JUNG_COUNT: u32 = 21;
const JONG_COUNT: u32 = 28;

fn decompose_hangul(code: u32) -> (u32, u32, u32) {
    let idx = code - HANGUL_BASE;
    let cho = idx / (JUNG_COUNT * JONG_COUNT);
    let jung = (idx % (JUNG_COUNT * JONG_COUNT)) / JONG_COUNT;
    let jong = idx % JONG_COUNT;
    (cho, jung, jong)
}

/// 한글 음절 폭을 초/중/종성 그룹으로 압축한다.
/// 그룹 수가 적을수록 압축률이 높지만 오차가 증가한다.
fn compress_hangul(
    char_widths: &HashMap<u32, u16>,
    max_cho_groups: u8,
    max_jung_groups: u8,
    max_jong_groups: u8,
) -> Option<HangulCompressed> {
    // 음절별 폭 수집
    let mut syllable_widths = Vec::new();
    for code in HANGUL_BASE..=HANGUL_END {
        if let Some(&w) = char_widths.get(&code) {
            syllable_widths.push((code, w));
        }
    }
    if syllable_widths.is_empty() {
        return None;
    }

    // 모든 음절이 동일한 폭인지 확인 (type:0)
    let first_w = syllable_widths[0].1;
    if syllable_widths.iter().all(|&(_, w)| w == first_w) {
        return Some(HangulCompressed {
            cho_groups: 1,
            jung_groups: 1,
            jong_groups: 1,
            cho_map: [0; 19],
            jung_map: [0; 21],
            jong_map: [0; 28],
            widths: vec![first_w],
            max_error: 0,
            avg_error: 0.0,
        });
    }

    // 초/중/종성별 폭 패턴 분석
    // 각 자모가 참여하는 음절들의 평균 폭으로 그룹핑
    let best = find_best_grouping(
        &syllable_widths,
        max_cho_groups,
        max_jung_groups,
        max_jong_groups,
    );

    Some(best)
}

fn find_best_grouping(
    syllable_widths: &[(u32, u16)],
    max_cho: u8,
    max_jung: u8,
    max_jong: u8,
) -> HangulCompressed {
    // 음절 폭을 3D 배열로 변환
    let mut widths_3d =
        vec![vec![vec![0u16; JONG_COUNT as usize]; JUNG_COUNT as usize]; CHO_COUNT as usize];
    let mut has_data =
        vec![vec![vec![false; JONG_COUNT as usize]; JUNG_COUNT as usize]; CHO_COUNT as usize];

    for &(code, w) in syllable_widths {
        let (cho, jung, jong) = decompose_hangul(code);
        widths_3d[cho as usize][jung as usize][jong as usize] = w;
        has_data[cho as usize][jung as usize][jong as usize] = true;
    }

    // 초성별 평균 폭 계산
    let cho_avgs = compute_axis_averages(&widths_3d, &has_data, 0);
    let jung_avgs = compute_axis_averages(&widths_3d, &has_data, 1);
    let jong_avgs = compute_axis_averages(&widths_3d, &has_data, 2);

    // K-means 클러스터링으로 그룹 할당
    let cho_map = kmeans_group(&cho_avgs, max_cho as usize);
    let jung_map = kmeans_group(&jung_avgs, max_jung as usize);
    let jong_map = kmeans_group(&jong_avgs, max_jong as usize);

    let cho_groups = *cho_map.iter().max().unwrap_or(&0) + 1;
    let jung_groups = *jung_map.iter().max().unwrap_or(&0) + 1;
    let jong_groups = *jong_map.iter().max().unwrap_or(&0) + 1;

    // 그룹 조합별 대표 폭 계산 (평균)
    let total_groups = cho_groups as usize * jung_groups as usize * jong_groups as usize;
    let mut group_sums = vec![0u64; total_groups];
    let mut group_counts = vec![0u32; total_groups];

    for &(code, w) in syllable_widths {
        let (cho, jung, jong) = decompose_hangul(code);
        let gi = cho_map[cho as usize] as usize * jung_groups as usize * jong_groups as usize
            + jung_map[jung as usize] as usize * jong_groups as usize
            + jong_map[jong as usize] as usize;
        group_sums[gi] += w as u64;
        group_counts[gi] += 1;
    }

    let group_widths: Vec<u16> = group_sums
        .iter()
        .zip(group_counts.iter())
        .map(|(&sum, &cnt)| {
            if cnt > 0 {
                (sum / cnt as u64) as u16
            } else {
                0
            }
        })
        .collect();

    // 오차 측정
    let mut max_error = 0u16;
    let mut total_error = 0u64;
    let mut count = 0u64;

    for &(code, w) in syllable_widths {
        let (cho, jung, jong) = decompose_hangul(code);
        let gi = cho_map[cho as usize] as usize * jung_groups as usize * jong_groups as usize
            + jung_map[jung as usize] as usize * jong_groups as usize
            + jong_map[jong as usize] as usize;
        let approx = group_widths[gi];
        let err = (w as i32 - approx as i32).unsigned_abs() as u16;
        max_error = max_error.max(err);
        total_error += err as u64;
        count += 1;
    }

    let mut cho_map_arr = [0u8; 19];
    for (i, &g) in cho_map.iter().enumerate() {
        cho_map_arr[i] = g;
    }
    let mut jung_map_arr = [0u8; 21];
    for (i, &g) in jung_map.iter().enumerate() {
        jung_map_arr[i] = g;
    }
    let mut jong_map_arr = [0u8; 28];
    for (i, &g) in jong_map.iter().enumerate() {
        jong_map_arr[i] = g;
    }

    HangulCompressed {
        cho_groups,
        jung_groups,
        jong_groups,
        cho_map: cho_map_arr,
        jung_map: jung_map_arr,
        jong_map: jong_map_arr,
        widths: group_widths,
        max_error,
        avg_error: if count > 0 {
            total_error as f64 / count as f64
        } else {
            0.0
        },
    }
}

/// 축(0=초성, 1=중성, 2=종성)별 평균 폭 계산
fn compute_axis_averages(
    widths: &[Vec<Vec<u16>>],
    has_data: &[Vec<Vec<bool>>],
    axis: usize,
) -> Vec<f64> {
    let count = match axis {
        0 => CHO_COUNT as usize,
        1 => JUNG_COUNT as usize,
        2 => JONG_COUNT as usize,
        _ => unreachable!(),
    };

    let mut avgs = vec![0.0; count];
    for idx in 0..count {
        let mut sum = 0u64;
        let mut cnt = 0u32;
        for cho in 0..CHO_COUNT as usize {
            for jung in 0..JUNG_COUNT as usize {
                for jong in 0..JONG_COUNT as usize {
                    let matches = match axis {
                        0 => cho == idx,
                        1 => jung == idx,
                        2 => jong == idx,
                        _ => false,
                    };
                    if matches && has_data[cho][jung][jong] {
                        sum += widths[cho][jung][jong] as u64;
                        cnt += 1;
                    }
                }
            }
        }
        avgs[idx] = if cnt > 0 {
            sum as f64 / cnt as f64
        } else {
            0.0
        };
    }
    avgs
}

/// 1D K-means 클러스터링 (간단한 정렬 기반 분할)
fn kmeans_group(values: &[f64], k: usize) -> Vec<u8> {
    let n = values.len();
    if n == 0 || k == 0 {
        return vec![0; n];
    }
    if k >= n {
        return (0..n).map(|i| i as u8).collect();
    }

    // 값-인덱스 쌍을 정렬
    let mut indexed: Vec<(f64, usize)> = values
        .iter()
        .copied()
        .enumerate()
        .map(|(i, v)| (v, i))
        .collect();
    indexed.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

    // 정렬된 순서에서 k등분
    let mut groups = vec![0u8; n];
    for (rank, &(_, orig_idx)) in indexed.iter().enumerate() {
        groups[orig_idx] = (rank * k / n).min(k - 1) as u8;
    }
    groups
}

#[derive(Debug)]
struct HangulCompressed {
    cho_groups: u8,
    jung_groups: u8,
    jong_groups: u8,
    cho_map: [u8; 19],
    jung_map: [u8; 21],
    jong_map: [u8; 28],
    widths: Vec<u16>,
    max_error: u16,
    avg_error: f64,
}

// ─── Latin 범위 폭 추출 ───

struct LatinRange {
    start: char,
    end: char,
    widths: Vec<u16>,
}

fn extract_latin_ranges(char_widths: &HashMap<u32, u16>) -> Vec<LatinRange> {
    let ranges: Vec<(u32, u32)> = vec![
        (0x0020, 0x007E), // Basic Latin (space ~ tilde)
        (0x00A0, 0x00FF), // Latin-1 Supplement
        (0x2000, 0x206F), // General Punctuation
        (0x2200, 0x22FF), // Mathematical Operators
        (0x3000, 0x303F), // CJK Symbols and Punctuation
        (0x3130, 0x318F), // Hangul Compatibility Jamo
        (0xFF00, 0xFF5E), // Fullwidth Latin
    ];

    let mut result = Vec::new();
    for (start, end) in ranges {
        let mut widths = Vec::new();
        let mut has_any = false;
        for c in start..=end {
            if let Some(&w) = char_widths.get(&c) {
                widths.push(w);
                has_any = true;
            } else {
                widths.push(0); // 미등록 글리프
            }
        }
        if has_any {
            result.push(LatinRange {
                start: char::from_u32(start).unwrap_or(' '),
                end: char::from_u32(end).unwrap_or(' '),
                widths,
            });
        }
    }
    result
}

// ─── Rust 소스코드 생성 ───

fn generate_rust_source(metrics: &[FontMetric]) -> String {
    let mut out = String::new();

    out.push_str("// 폰트 메트릭 generated data fragment (자동 생성)\n");
    out.push_str("//\n");
    out.push_str("// font-metric-gen이 명시적 plan 순서로 추출했다.\n");
    out.push_str("// core lookup과 measured/manual overlay를 포함하지 않는다.\n");
    out.push_str("// sort 또는 dedupe 금지.\n\n");

    // 각 폰트별 데이터 생성
    for (idx, m) in metrics.iter().enumerate() {
        let var_prefix = format!("FONT_{}", idx);

        // Latin 범위 데이터
        let latin_ranges = extract_latin_ranges(&m.char_widths);
        for (ri, range) in latin_ranges.iter().enumerate() {
            out.push_str(&format!(
                "static {}_LATIN_{}: [u16; {}] = {:?};\n",
                var_prefix,
                ri,
                range.widths.len(),
                range.widths
            ));
        }

        // Latin 범위 배열
        out.push_str(&format!(
            "static {}_LATIN_RANGES: [LatinRange; {}] = [\n",
            var_prefix,
            latin_ranges.len()
        ));
        for (ri, range) in latin_ranges.iter().enumerate() {
            out.push_str(&format!(
                "    LatinRange {{ start: 0x{:04X}, end: 0x{:04X}, widths: &{}_LATIN_{} }},\n",
                range.start as u32, range.end as u32, var_prefix, ri
            ));
        }
        out.push_str("];\n");

        // 한글 메트릭
        let hangul = compress_hangul(&m.char_widths, 4, 6, 3);
        if let Some(ref h) = hangul {
            out.push_str(&format!(
                "static {}_HANGUL_CHO: [u8; 19] = {:?};\n",
                var_prefix, h.cho_map
            ));
            out.push_str(&format!(
                "static {}_HANGUL_JUNG: [u8; 21] = {:?};\n",
                var_prefix, h.jung_map
            ));
            out.push_str(&format!(
                "static {}_HANGUL_JONG: [u8; 28] = {:?};\n",
                var_prefix, h.jong_map
            ));
            out.push_str(&format!(
                "static {}_HANGUL_WIDTHS: [u16; {}] = {:?};\n",
                var_prefix,
                h.widths.len(),
                h.widths
            ));
            out.push_str(&format!(
                "static {}_HANGUL: HangulMetric = HangulMetric {{\n",
                var_prefix
            ));
            out.push_str(&format!("    cho_groups: {},\n", h.cho_groups));
            out.push_str(&format!("    jung_groups: {},\n", h.jung_groups));
            out.push_str(&format!("    jong_groups: {},\n", h.jong_groups));
            out.push_str(&format!("    cho_map: &{}_HANGUL_CHO,\n", var_prefix));
            out.push_str(&format!("    jung_map: &{}_HANGUL_JUNG,\n", var_prefix));
            out.push_str(&format!("    jong_map: &{}_HANGUL_JONG,\n", var_prefix));
            out.push_str(&format!("    widths: &{}_HANGUL_WIDTHS,\n", var_prefix));
            out.push_str("};\n");
        }

        out.push('\n');
    }

    // generated 영역 배열. core facade가 overlay 뒤가 아니라 이 배열 뒤에 overlay를 잇는다.
    out.push_str(&format!(
        "static GENERATED_FONT_METRICS: [FontMetric; {}] = [\n",
        metrics.len()
    ));
    for (idx, m) in metrics.iter().enumerate() {
        let var_prefix = format!("FONT_{}", idx);
        let hangul = compress_hangul(&m.char_widths, 4, 6, 3);
        let hangul_ref = if hangul.is_some() {
            format!("Some(&{}_HANGUL)", var_prefix)
        } else {
            "None".to_string()
        };

        out.push_str(&format!(
            "    FontMetric {{ name: \"{}\", bold: {}, italic: {}, em_size: {}, latin_ranges: &{}_LATIN_RANGES, hangul: {} }},\n",
            m.family_name.replace('"', "\\\""),
            m.bold,
            m.italic,
            m.em_size,
            var_prefix,
            hangul_ref
        ));
    }
    out.push_str("];\n");

    out
}

// ─── 명시적 생성 계획과 provenance metadata ───

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct GenerationPlan {
    schema_version: u32,
    target_region: String,
    expected_entry_count: usize,
    inputs: Vec<GenerationInput>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct GenerationInput {
    order: usize,
    path: String,
    face_index: u32,
    expected_identity: ExpectedIdentity,
    license: EvidenceDeclaration,
    provenance: EvidenceDeclaration,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct ExpectedIdentity {
    family_name: String,
    bold: bool,
    italic: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct EvidenceDeclaration {
    status: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    spdx: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    source: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    evidence_path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reason: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct GeneratorManifest {
    schema_version: u32,
    generator: &'static str,
    generator_version: &'static str,
    generator_contract: &'static str,
    generator_source_sha256: String,
    target_region: String,
    expected_entry_count: usize,
    input_plan_sha256: String,
    generated_source_sha256: String,
    entries: Vec<GeneratedEntryMetadata>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct GeneratedEntryMetadata {
    order: usize,
    source_path: String,
    source_sha256: String,
    face_index: u32,
    family_name: String,
    bold: bool,
    italic: bool,
    units_per_em: u16,
    naming_records: Vec<NamingRecord>,
    license: EvidenceMetadata,
    provenance: EvidenceMetadata,
    hangul_compression: HangulCompressionMetadata,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct EvidenceMetadata {
    declaration: EvidenceDeclaration,
    #[serde(skip_serializing_if = "Option::is_none")]
    evidence_sha256: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct HangulCompressionMetadata {
    status: &'static str,
    sample_count: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    cho_groups: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    jung_groups: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    jong_groups: Option<u8>,
    #[serde(skip_serializing_if = "Option::is_none")]
    max_error: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    avg_error: Option<f64>,
}

fn validate_relative_path(path: &str, field: &str) -> Result<PathBuf, String> {
    let parsed = PathBuf::from(path);
    if parsed.is_absolute()
        || parsed
            .components()
            .any(|component| matches!(component, std::path::Component::ParentDir))
    {
        return Err(format!(
            "{}는 checkout 상대 경로여야 하고 '..'를 포함할 수 없음: {}",
            field, path
        ));
    }
    Ok(parsed)
}

fn checkout_root_from(start: &Path) -> Option<PathBuf> {
    start.ancestors().find_map(|candidate| {
        let is_checkout = candidate.join("Cargo.toml").is_file()
            && candidate
                .join("src/renderer/font_metrics_data.rs")
                .is_file()
            && candidate.join("src/tools/font_metric_gen.rs").is_file();
        is_checkout.then(|| candidate.to_path_buf())
    })
}

fn find_repository_root() -> Result<PathBuf, String> {
    let current = env::current_dir()
        .and_then(|path| path.canonicalize())
        .map_err(|error| format!("현재 경로 확인 실패: {}", error))?;
    if let Some(root) = checkout_root_from(&current) {
        return Ok(root);
    }

    // checkout 밖에서 checkout 내부의 binary를 직접 실행하는 경우만 실행 파일 경로를 fallback으로 쓴다.
    // checkout/worktree 내부에서는 공유 target binary보다 현재 worktree를 우선해 계보가 섞이지 않게 한다.
    let executable = env::current_exe()
        .and_then(|path| path.canonicalize())
        .map_err(|error| format!("실행 파일 경로 확인 실패: {}", error))?;
    checkout_root_from(&executable).ok_or_else(|| {
        "rhwp checkout root를 찾을 수 없음; checkout 내부에서 실행하거나 checkout의 binary를 사용해야 함"
            .to_string()
    })
}

fn resolve_checkout_file(
    repository_root: &Path,
    path: &str,
    field: &str,
) -> Result<PathBuf, String> {
    let relative = validate_relative_path(path, field)?;
    let resolved = repository_root
        .join(relative)
        .canonicalize()
        .map_err(|error| format!("{}({}): {}", field, path, error))?;
    if !resolved.starts_with(repository_root) {
        return Err(format!(
            "{}는 symlink를 포함해 checkout 밖을 가리킬 수 없음: {}",
            field, path
        ));
    }
    if !resolved.is_file() {
        return Err(format!("{}는 파일이어야 함: {}", field, path));
    }
    Ok(resolved)
}

fn validate_evidence(declaration: &EvidenceDeclaration, field: &str) -> Result<(), String> {
    match declaration.status.as_str() {
        "verified" => {
            if declaration.source.as_deref().unwrap_or_default().is_empty() {
                return Err(format!("{}.source는 verified 상태에서 필수", field));
            }
            if declaration
                .evidence_path
                .as_deref()
                .unwrap_or_default()
                .is_empty()
            {
                return Err(format!("{}.evidencePath는 verified 상태에서 필수", field));
            }
            if field.ends_with(".license")
                && declaration.spdx.as_deref().unwrap_or_default().is_empty()
            {
                return Err(format!("{}.spdx는 verified 상태에서 필수", field));
            }
        }
        "unknown" | "local-only" => {
            if declaration.reason.as_deref().unwrap_or_default().is_empty() {
                return Err(format!(
                    "{}.reason은 {} 상태에서 필수",
                    field, declaration.status
                ));
            }
        }
        other => return Err(format!("{}.status 미지원 값: {}", field, other)),
    }
    if let Some(path) = declaration.evidence_path.as_deref() {
        validate_relative_path(path, &format!("{}.evidencePath", field))?;
    }
    Ok(())
}

fn load_generation_plan(path: &Path) -> Result<(GenerationPlan, String), String> {
    let bytes = fs::read(path).map_err(|error| format!("{}: {}", path.display(), error))?;
    let plan: GenerationPlan =
        serde_json::from_slice(&bytes).map_err(|error| format!("{}: {}", path.display(), error))?;
    if plan.schema_version != 1 {
        return Err(format!(
            "지원하지 않는 plan schemaVersion: {}",
            plan.schema_version
        ));
    }
    if plan.inputs.is_empty() {
        return Err("plan.inputs가 비어 있음".to_string());
    }
    if plan.expected_entry_count != plan.inputs.len() {
        return Err(format!(
            "expectedEntryCount={}와 inputs 길이={}가 다름",
            plan.expected_entry_count,
            plan.inputs.len()
        ));
    }
    match plan.target_region.as_str() {
        "canary" => {}
        "historical-generated-0-594" if plan.expected_entry_count == 595 => {}
        "historical-generated-0-594" => {
            return Err(
                "canonical historical generated plan은 정확히 595개 입력이어야 함".to_string(),
            );
        }
        other => return Err(format!("지원하지 않는 targetRegion: {}", other)),
    }
    let mut seen = std::collections::HashSet::new();
    for (index, input) in plan.inputs.iter().enumerate() {
        if input.order != index {
            return Err(format!(
                "입력 순서가 명시적 연속값이 아님: inputs[{}].order={} (expected {})",
                index, input.order, index
            ));
        }
        validate_relative_path(&input.path, &format!("inputs[{}].path", index))?;
        validate_evidence(&input.license, &format!("inputs[{}].license", index))?;
        validate_evidence(&input.provenance, &format!("inputs[{}].provenance", index))?;
        if !seen.insert((input.path.as_str(), input.face_index)) {
            return Err(format!(
                "동일 source/face가 plan에 중복됨: {}#{}; 자동 dedupe하지 않음",
                input.path, input.face_index
            ));
        }
    }
    Ok((plan, sha256_bytes(&bytes)))
}

fn evidence_metadata(
    repository_root: &Path,
    declaration: &EvidenceDeclaration,
    field: &str,
) -> Result<EvidenceMetadata, String> {
    let evidence_sha256 = declaration
        .evidence_path
        .as_deref()
        .map(|path| resolve_checkout_file(repository_root, path, field))
        .transpose()?
        .as_deref()
        .map(sha256_file)
        .transpose()?;
    Ok(EvidenceMetadata {
        declaration: declaration.clone(),
        evidence_sha256,
    })
}

fn hangul_compression_metadata(metric: &FontMetric) -> HangulCompressionMetadata {
    let sample_count = metric
        .char_widths
        .keys()
        .filter(|&&codepoint| (HANGUL_BASE..=HANGUL_END).contains(&codepoint))
        .count();
    match compress_hangul(&metric.char_widths, 4, 6, 3) {
        Some(compressed) => HangulCompressionMetadata {
            status: "verified",
            sample_count,
            cho_groups: Some(compressed.cho_groups),
            jung_groups: Some(compressed.jung_groups),
            jong_groups: Some(compressed.jong_groups),
            max_error: Some(compressed.max_error),
            avg_error: Some(compressed.avg_error),
        },
        None => HangulCompressionMetadata {
            status: "not-applicable",
            sample_count,
            cho_groups: None,
            jung_groups: None,
            jong_groups: None,
            max_error: None,
            avg_error: None,
        },
    }
}

fn protected_output_name(path: &Path) -> bool {
    matches!(
        path.file_name().and_then(|name| name.to_str()),
        Some("font_metrics_data.rs" | "font_metrics_overlays.rs")
    )
}

fn resolved_output_path(repository_root: &Path, path: &Path) -> Result<PathBuf, String> {
    let anchored = if path.is_absolute() {
        path.to_path_buf()
    } else {
        repository_root.join(path)
    };
    if anchored.exists() {
        return anchored
            .canonicalize()
            .map_err(|error| format!("{}: {}", anchored.display(), error));
    }
    let parent = anchored.parent().unwrap_or(repository_root);
    let file_name = anchored
        .file_name()
        .ok_or_else(|| format!("출력 파일명이 없음: {}", anchored.display()))?;
    parent
        .canonicalize()
        .map(|canonical_parent| canonical_parent.join(file_name))
        .map_err(|error| format!("{}: {}", parent.display(), error))
}

fn validate_output_paths(
    repository_root: &Path,
    generated: &Path,
    metadata: &Path,
) -> Result<(PathBuf, PathBuf), String> {
    let generated_resolved = resolved_output_path(repository_root, generated)?;
    let metadata_resolved = resolved_output_path(repository_root, metadata)?;
    let protected_paths = [
        repository_root.join("src/renderer/font_metrics_data.rs"),
        repository_root.join("src/renderer/font_metrics_overlays.rs"),
    ];
    if protected_output_name(generated)
        || protected_output_name(metadata)
        || protected_paths.contains(&generated_resolved)
        || protected_paths.contains(&metadata_resolved)
    {
        return Err(
            "generator ownership 위반: core lookup 또는 measured/manual overlay는 출력 대상이 아님"
                .to_string(),
        );
    }
    if generated_resolved == metadata_resolved {
        return Err("generated output과 metadata output은 서로 달라야 함".to_string());
    }
    if generated.extension().and_then(|ext| ext.to_str()) != Some("rs") {
        return Err("generated output 확장자는 .rs여야 함".to_string());
    }
    if metadata.extension().and_then(|ext| ext.to_str()) != Some("json") {
        return Err("metadata output 확장자는 .json이어야 함".to_string());
    }
    Ok((generated_resolved, metadata_resolved))
}

fn is_canonical_generated_output(repository_root: &Path, path: &Path) -> bool {
    path == repository_root.join("src/renderer/font_metrics_generated.rs")
}

struct StagedOutput {
    path: Option<PathBuf>,
    target: PathBuf,
}

impl StagedOutput {
    fn new(target: &Path, bytes: &[u8]) -> Result<Self, String> {
        let parent = target
            .parent()
            .ok_or_else(|| format!("출력 부모 경로가 없음: {}", target.display()))?;
        let file_name = target
            .file_name()
            .and_then(|name| name.to_str())
            .ok_or_else(|| format!("출력 파일명이 올바른 UTF-8이 아님: {}", target.display()))?;

        for _ in 0..100 {
            let sequence = STAGED_OUTPUT_SEQUENCE.fetch_add(1, Ordering::Relaxed);
            let staged_path = parent.join(format!(
                ".{}.{}.{}.rhwp-stage",
                file_name,
                std::process::id(),
                sequence
            ));
            match OpenOptions::new()
                .write(true)
                .create_new(true)
                .open(&staged_path)
            {
                Ok(mut file) => {
                    if let Err(error) = file.write_all(bytes).and_then(|_| file.sync_all()) {
                        let _ = fs::remove_file(&staged_path);
                        return Err(format!("{}: {}", staged_path.display(), error));
                    }
                    return Ok(Self {
                        path: Some(staged_path),
                        target: target.to_path_buf(),
                    });
                }
                Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
                Err(error) => return Err(format!("{}: {}", staged_path.display(), error)),
            }
        }
        Err(format!(
            "고유한 sibling staging 파일을 만들 수 없음: {}",
            target.display()
        ))
    }

    fn commit(mut self) -> Result<(), String> {
        let staged_path = self.path.take().expect("staged path는 commit 전 존재");
        if !self.target.exists() {
            return match fs::rename(&staged_path, &self.target) {
                Ok(()) => Ok(()),
                Err(error) => {
                    self.path = Some(staged_path);
                    Err(format!("{}: {}", self.target.display(), error))
                }
            };
        }
        if !self.target.is_file() {
            self.path = Some(staged_path);
            return Err(format!(
                "출력 대상은 기존 일반 파일이거나 새 파일이어야 함: {}",
                self.target.display()
            ));
        }

        let file_name = self
            .target
            .file_name()
            .and_then(|name| name.to_str())
            .expect("StagedOutput::new에서 UTF-8 파일명 검증됨");
        let parent = self.target.parent().expect("target 부모는 검증됨");
        let sequence = STAGED_OUTPUT_SEQUENCE.fetch_add(1, Ordering::Relaxed);
        let backup_path = parent.join(format!(
            ".{}.{}.{}.rhwp-backup",
            file_name,
            std::process::id(),
            sequence
        ));
        if let Err(error) = fs::rename(&self.target, &backup_path) {
            self.path = Some(staged_path);
            return Err(format!("{} backup 실패: {}", self.target.display(), error));
        }
        if let Err(error) = fs::rename(&staged_path, &self.target) {
            self.path = Some(staged_path);
            let restore = fs::rename(&backup_path, &self.target);
            return match restore {
                Ok(()) => Err(format!("{} commit 실패: {}", self.target.display(), error)),
                Err(restore_error) => Err(format!(
                    "{} commit 실패: {}; backup 복원 실패({}): {}",
                    self.target.display(),
                    error,
                    backup_path.display(),
                    restore_error
                )),
            };
        }
        fs::remove_file(&backup_path)
            .map_err(|error| format!("{} backup 정리 실패: {}", backup_path.display(), error))?;
        Ok(())
    }
}

impl Drop for StagedOutput {
    fn drop(&mut self) {
        if let Some(path) = self.path.as_deref() {
            let _ = fs::remove_file(path);
        }
    }
}

fn generate_from_plan(
    plan_path: &Path,
    generated_output: &Path,
    metadata_output: &Path,
) -> Result<(), String> {
    let repository_root = find_repository_root()?;
    let plan_path = if plan_path.is_absolute() {
        plan_path.to_path_buf()
    } else {
        repository_root.join(plan_path)
    };
    let (generated_output, metadata_output) =
        validate_output_paths(&repository_root, generated_output, metadata_output)?;
    let (plan, input_plan_sha256) = load_generation_plan(&plan_path)?;
    if is_canonical_generated_output(&repository_root, &generated_output)
        && plan.target_region != "historical-generated-0-594"
    {
        return Err(
            "canonical generated DB 출력에는 595-entry historical-generated-0-594 plan이 필요"
                .to_string(),
        );
    }
    let mut metrics = Vec::with_capacity(plan.inputs.len());
    for (index, input) in plan.inputs.iter().enumerate() {
        let input_path = resolve_checkout_file(
            &repository_root,
            &input.path,
            &format!("inputs[{}].path", index),
        )?;
        let metric = parse_ttf_face(&input_path, input.face_index)?;
        if metric.family_name != input.expected_identity.family_name
            || metric.bold != input.expected_identity.bold
            || metric.italic != input.expected_identity.italic
        {
            return Err(format!(
                "{}#{} identity drift: 실제=({}, bold={}, italic={}), 기대=({}, bold={}, italic={})",
                input.path,
                input.face_index,
                metric.family_name,
                metric.bold,
                metric.italic,
                input.expected_identity.family_name,
                input.expected_identity.bold,
                input.expected_identity.italic
            ));
        }
        metrics.push(metric);
    }

    let generated_source = generate_rust_source(&metrics);
    let generated_source_sha256 = sha256_bytes(generated_source.as_bytes());
    let entries = plan
        .inputs
        .iter()
        .zip(metrics.iter())
        .enumerate()
        .map(|(index, (input, metric))| {
            Ok(GeneratedEntryMetadata {
                order: input.order,
                source_path: input.path.clone(),
                source_sha256: metric.source_sha256.clone(),
                face_index: metric.face_index,
                family_name: metric.family_name.clone(),
                bold: metric.bold,
                italic: metric.italic,
                units_per_em: metric.em_size,
                naming_records: metric.naming_records.clone(),
                license: evidence_metadata(
                    &repository_root,
                    &input.license,
                    &format!("inputs[{}].license.evidencePath", index),
                )?,
                provenance: evidence_metadata(
                    &repository_root,
                    &input.provenance,
                    &format!("inputs[{}].provenance.evidencePath", index),
                )?,
                hangul_compression: hangul_compression_metadata(metric),
            })
        })
        .collect::<Result<Vec<_>, String>>()?;
    let manifest = GeneratorManifest {
        schema_version: 1,
        generator: "font-metric-gen",
        generator_version: env!("CARGO_PKG_VERSION"),
        generator_contract: "generated-data-and-provenance-only-v1",
        generator_source_sha256: sha256_bytes(GENERATOR_SOURCE_BYTES),
        target_region: plan.target_region,
        expected_entry_count: plan.expected_entry_count,
        input_plan_sha256,
        generated_source_sha256,
        entries,
    };
    let metadata = serde_json::to_string_pretty(&manifest)
        .map_err(|error| format!("metadata 직렬화 실패: {}", error))?
        + "\n";

    // 두 산출물을 sibling 임시 파일에 완성한 뒤 metadata를 먼저 반영한다.
    // generated 파일이 마지막 commit point이므로 metadata 실패 시 기존 generated는 불변이다.
    let generated_staged = StagedOutput::new(&generated_output, generated_source.as_bytes())?;
    let metadata_staged = StagedOutput::new(&metadata_output, metadata.as_bytes())?;
    metadata_staged.commit()?;
    generated_staged.commit()?;
    Ok(())
}

// ─── 진단 출력 ───

fn print_diagnostic(metric: &FontMetric) {
    let total_chars = metric.char_widths.len();
    let hangul_count = metric
        .char_widths
        .keys()
        .filter(|&&c| (HANGUL_BASE..=HANGUL_END).contains(&c))
        .count();
    let latin_count = metric
        .char_widths
        .keys()
        .filter(|&&c| (0x20..=0x7E).contains(&c))
        .count();

    let style = match (metric.bold, metric.italic) {
        (false, false) => "Regular",
        (true, false) => "Bold",
        (false, true) => "Italic",
        (true, true) => "Bold Italic",
    };
    println!("  패밀리: {} [{}]", metric.family_name, style);
    println!("  파일: {}", metric.file_name);
    println!("  source SHA-256: {}", metric.source_sha256);
    println!("  TTC face index: {}", metric.face_index);
    println!("  name records: {}", metric.naming_records.len());
    println!("  em크기: {}", metric.em_size);
    println!("  총 글리프: {}", total_chars);
    println!("  한글 음절: {} / 11172", hangul_count);
    println!("  Basic Latin: {} / 95", latin_count);

    // 한글 압축 진단
    if hangul_count > 0 {
        if let Some(h) = compress_hangul(&metric.char_widths, 4, 6, 3) {
            println!(
                "  한글 압축: {}×{}×{} = {} 그룹 (최대오차: {} em단위, 평균오차: {:.1})",
                h.cho_groups,
                h.jung_groups,
                h.jong_groups,
                h.widths.len(),
                h.max_error,
                h.avg_error
            );
        }
    }

    // 샘플 폭
    let sample_chars = ['A', 'a', 'W', 'i', ' ', '가', '한', '글'];
    print!("  샘플 폭:");
    for ch in sample_chars {
        if let Some(&w) = metric.char_widths.get(&(ch as u32)) {
            print!(" {}={}", ch, w);
        }
    }
    println!();
}

fn argument_value(args: &[String], flag: &str) -> Result<Option<String>, String> {
    let Some(index) = args.iter().position(|argument| argument == flag) else {
        return Ok(None);
    };
    args.get(index + 1)
        .cloned()
        .map(Some)
        .ok_or_else(|| format!("{} 값이 없음", flag))
}

fn print_usage() {
    eprintln!("사용법:");
    eprintln!("  font-metric-gen <파일.ttf> [--face-index N]  # 단일 face 진단");
    eprintln!("  font-metric-gen --dir <폴더> --list          # 폴더 내 폰트 목록");
    eprintln!("  font-metric-gen --plan <plan.json> --generated-output <출력.rs> --metadata-output <출력.json>");
    eprintln!();
    eprintln!(
        "생성 모드는 plan의 명시적 order와 faceIndex를 그대로 보존하며 sort/dedupe하지 않습니다."
    );
}

fn run() -> Result<(), String> {
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        print_usage();
        return Err("인자가 없음".to_string());
    }

    if args[1] == "--plan" {
        let plan = argument_value(&args, "--plan")?
            .map(PathBuf::from)
            .ok_or_else(|| "--plan 값이 없음".to_string())?;
        let generated_output = argument_value(&args, "--generated-output")?
            .map(PathBuf::from)
            .ok_or_else(|| "--generated-output이 필수".to_string())?;
        let metadata_output = argument_value(&args, "--metadata-output")?
            .map(PathBuf::from)
            .ok_or_else(|| "--metadata-output이 필수".to_string())?;
        generate_from_plan(&plan, &generated_output, &metadata_output)?;
        println!("generated data: {}", generated_output.display());
        println!("provenance metadata: {}", metadata_output.display());
        return Ok(());
    }

    if args[1] == "--dir" {
        if !args.iter().any(|argument| argument == "--list") {
            return Err(
                "--dir 생성은 암묵적 sort/dedupe 때문에 폐기됨; --plan 생성 모드를 사용하세요"
                    .to_string(),
            );
        }
        let dir = argument_value(&args, "--dir")?
            .map(PathBuf::from)
            .ok_or_else(|| "--dir 값이 없음".to_string())?;

        let mut entries: Vec<_> = fs::read_dir(&dir)
            .map_err(|error| format!("{}: {}", dir.display(), error))?
            .filter_map(|e| e.ok())
            .filter(|e| {
                let name = e.file_name().to_string_lossy().to_lowercase();
                name.ends_with(".ttf") || name.ends_with(".otf") || name.ends_with(".ttc")
            })
            .collect();
        entries.sort_by_key(|e| e.file_name());

        println!("폰트 목록 ({} 파일):", entries.len());
        for entry in &entries {
            match parse_ttf_all(&entry.path()) {
                Ok(metrics) => {
                    for metric in metrics {
                        let style = match (metric.bold, metric.italic) {
                            (false, false) => "",
                            (true, false) => " [B]",
                            (false, true) => " [I]",
                            (true, true) => " [BI]",
                        };
                        println!(
                            "  {}#{} → \"{}\"{} (em={}, 글리프={})",
                            metric.file_name,
                            metric.face_index,
                            metric.family_name,
                            style,
                            metric.em_size,
                            metric.char_widths.len()
                        );
                    }
                }
                Err(error) => println!(
                    "  {} → 오류: {}",
                    entry.file_name().to_string_lossy(),
                    error
                ),
            }
        }
        return Ok(());
    } else {
        // 단일 파일 진단
        let path = PathBuf::from(&args[1]);
        let face_index = argument_value(&args, "--face-index")?
            .map(|value| {
                value
                    .parse::<u32>()
                    .map_err(|error| format!("--face-index: {}", error))
            })
            .transpose()?
            .unwrap_or(0);
        let metric = parse_ttf_face(&path, face_index)?;
        print_diagnostic(&metric);
    }
    Ok(())
}

// ─── main ───

fn main() {
    if let Err(error) = run() {
        eprintln!("오류: {}", error);
        std::process::exit(1);
    }
}