draftwright 0.6.0

Dimensioned engineering drawings from STEP, with SVG, PDF and editable DXF output.
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
//! Placement intent relative to views and feature anchors (ADR R1 invariant 1).
use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::items::{Op, key};
use crate::model::{Analysis, Invalid, Plan, PlannedSheet, PlannedView, Problem, Spec};

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Place {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub view: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub side: Option<Side>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lane: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub along: Option<Along>,
    /// A pinned callout's bend from its automatic tip, or a linear label from its
    /// canonical label position, `[dx, dy]` in paper mm (y up). Excludes side/lane/along.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<[f64; 2]>,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub pinned: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Side {
    Above,
    Below,
    Left,
    Right,
}
impl Side {
    pub fn name(self) -> &'static str {
        match self {
            Self::Above => "above",
            Self::Below => "below",
            Self::Left => "left",
            Self::Right => "right",
        }
    }
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Along {
    Fraction(f64),
    Slot(Slot),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Slot {
    Start,
    Middle,
    End,
    Outside,
}
impl Along {
    pub fn fraction(self) -> f64 {
        match self {
            Self::Fraction(f) => f,
            Self::Slot(Slot::Start) => 0.0,
            Self::Slot(Slot::Middle) => 0.5,
            Self::Slot(Slot::End) => 1.0,
            Self::Slot(Slot::Outside) => 1.25,
        }
    }
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct AnnotationSpec {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<crate::label_text::Text>,
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub omit: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub place: Option<Place>,
}
/// View alignment is separate from annotation slots: only views have projection origins.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ViewAlign {
    Start,
    Middle,
    End,
    Outside,
    Projection,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ViewPlace {
    /// Another view's name: a view is placed only relative to another view, never to the page
    /// (ADR R1 invariant 1).
    pub relative_to: String,
    pub side: Side,
    /// Clear paper distance between the views’ annotation bands, not their part outlines, in mm.
    #[serde(default = "default_gap")]
    pub gap: f64,
    #[serde(default = "middle")]
    pub align: ViewAlign,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub pinned: bool,
}
fn default_gap() -> f64 {
    10.0
}
fn middle() -> ViewAlign {
    ViewAlign::Middle
}

pub(crate) fn carrier(plan: &Plan, id: &str) -> Option<String> {
    let owner = plan
        .omitted
        .iter()
        .find(|o| o.id == id)
        .and_then(|o| match &o.state {
            crate::items::State::Covered { by } => Some(by.as_str()),
            _ => None,
        })
        .unwrap_or(id);
    plan.dimensions
        .iter()
        .find(|d| d.id == owner)
        .map(|d| d.view.clone())
        .or_else(|| plan.gdt.iter().find(|g| g.id == id).map(|g| g.view.clone()))
}

pub(crate) fn has_pins(s: &Spec) -> bool {
    s.dimensions
        .values()
        .any(|d| d.place.as_ref().is_some_and(|p| p.pinned))
        || s.annotations
            .values()
            .any(|d| d.place.as_ref().is_some_and(|p| p.pinned))
        || s.views
            .values()
            .any(|v| v.place.as_ref().is_some_and(|p| p.pinned))
}
pub(crate) fn check(s: &Spec) -> Result<(), Invalid> {
    let error = |message: String, choices: Vec<String>| Invalid {
        message,
        choices,
        path: None,
        also: Vec::new(),
    };
    for (id, p) in s
        .dimensions
        .iter()
        .filter_map(|(id, d)| Some((id, d.place.as_ref()?)))
        .chain(
            s.annotations
                .iter()
                .filter_map(|(id, d)| Some((id, d.place.as_ref()?))),
        )
    {
        if let Some(v) = &p.view
            && !["front", "plan", "side"].contains(&v.as_str())
            && !crate::detail::NAMES.contains(&v.as_str())
        {
            return Err(error(
                format!("The placement view of {id} must be a principal or detail view."),
                ["front", "plan", "side"].map(String::from).to_vec(),
            ));
        }
        if let Some(o) = p.offset {
            if s.annotations.contains_key(id) {
                return Err(error(
                    format!(
                        "The placement offset of {id} applies only to callouts and linear dimensions."
                    ),
                    ["side", "lane", "along"].map(String::from).to_vec(),
                ));
            }
            if p.side.is_some() || p.lane.is_some() || p.along.is_some() || !p.pinned {
                return Err(error(
                    format!(
                        "The placement offset of {id} is a pinned form of its own: set pinned, and no side, lane or along."
                    ),
                    vec!["offset, pinned".into(), "side, lane, along".into()],
                ));
            }
            if o.iter().any(|v| !v.is_finite() || v.abs() > 1000.0) {
                return Err(error(
                    format!("The placement offset of {id} must be within ±1000 paper mm."),
                    vec!["-1000..1000".into()],
                ));
            }
        }
        if p.lane.is_some_and(|n| n > 1000) {
            return Err(error(
                format!("The placement lane of {id} exceeds 1000."),
                vec!["0..1000".into()],
            ));
        }
        if let Some(Along::Fraction(f)) = p.along
            && (!f.is_finite() || !(0.0..=1.0).contains(&f))
        {
            return Err(error(
                format!("The along fraction of {id} must be between 0 and 1."),
                vec![
                    "0..1".into(),
                    "start".into(),
                    "middle".into(),
                    "end".into(),
                    "outside".into(),
                ],
            ));
        }
    }
    for (name, v) in &s.views {
        if let Some(p) = &v.place {
            if !p.gap.is_finite() || !(0.0..=1000.0).contains(&p.gap) {
                return Err(error(
                    format!("The gap of {name} must be between 0 and 1000 paper mm."),
                    vec!["0..1000".into()],
                ));
            }
            if p.relative_to == *name
                || (!["front", "plan", "side", "iso", "iso_below", "section_aa"]
                    .contains(&p.relative_to.as_str())
                    && !crate::detail::NAMES.contains(&p.relative_to.as_str()))
            {
                return Err(error(
                    format!("The placement anchor of {name} is not another view."),
                    ["front", "plan", "side", "iso", "iso_below", "section_aa"]
                        .into_iter()
                        .filter(|v| *v != name)
                        .map(String::from)
                        .collect(),
                ));
            }
            if p.align == ViewAlign::Projection && !projection_related(name, &p.relative_to) {
                return Err(error(
                    format!(
                        "The alignment of {name} relative to {} cannot be projection; valid choices are start, middle, end, outside.",
                        p.relative_to
                    ),
                    align_choices(name, &p.relative_to)
                        .iter()
                        .map(|s| (*s).into())
                        .collect(),
                ));
            }
            let mut seen = BTreeSet::new();
            let mut at = name.as_str();
            while let Some(p) = s.views.get(at).and_then(|v| v.place.as_ref()) {
                if !seen.insert(at) {
                    return Err(error(
                        format!(
                            "View placement anchors form a cycle at {name}; anchor one of them to a view outside the cycle."
                        ),
                        vec![],
                    ));
                }
                at = &p.relative_to;
            }
        }
    }
    Ok(())
}

fn projection_side(name: &str, anchor: &str, first: bool) -> Option<Side> {
    match (name, anchor, first) {
        ("plan", "front", false) | ("front", "plan", true) => Some(Side::Above),
        ("plan", "front", true) | ("front", "plan", false) => Some(Side::Below),
        ("side", "front", false) | ("front", "side", true) => Some(Side::Right),
        ("side", "front", true) | ("front", "side", false) => Some(Side::Left),
        _ => None,
    }
}

pub(crate) fn projection_related(name: &str, anchor: &str) -> bool {
    projection_side(name, anchor, false).is_some()
}

pub(crate) fn align_choices(name: &str, anchor: &str) -> &'static [&'static str] {
    if projection_related(name, anchor) {
        &["projection", "start", "middle", "end", "outside"]
    } else {
        &["start", "middle", "end", "outside"]
    }
}

fn unconventional(s: &Spec, name: &str, p: &ViewPlace) -> bool {
    let expected = projection_side(
        name,
        &p.relative_to,
        s.sheet.projection.as_deref() == Some("first"),
    );
    matches!(
        (name, p.relative_to.as_str()),
        ("side", "plan") | ("plan", "side")
    ) || expected.is_some_and(|side| {
        p.side != side || !matches!(p.align, ViewAlign::Middle | ViewAlign::Projection)
    })
}

fn overlap(a: [f64; 4], b: [f64; 4]) -> bool {
    a[0] < b[2] && b[0] < a[2] && a[1] < b[3] && b[1] < a[3]
}
fn shift(v: &mut PlannedView, d: [f64; 2]) {
    // A relation already satisfied within the paper-boundary tolerance keeps its exact frame.
    if d.iter().all(|x| x.abs() <= 1e-9) {
        return;
    }
    v.at = [v.at[0] + d[0], v.at[1] + d[1]];
    v.area = [
        v.area[0] + d[0],
        v.area[1] + d[1],
        v.area[2] + d[0],
        v.area[3] + d[1],
    ];
}
fn problem(id: &str, code: &str, message: String, path: &str) -> Problem {
    Problem {
        id: format!("p:{code}:{id}"),
        code: code.into(),
        severity: "warning".into(),
        message,
        subjects: vec![id.into()],
        fix: vec![if path.starts_with("views/") {
            Op::remove(path)
        } else {
            Op::set(path, json!({}))
        }],
        ..Default::default()
    }
}

/// Explicit positions resolve in dependency order. Pinned carriers retain the canonical frame;
/// unrelated omissions, text edits and recovery cannot change their geometry.
pub(crate) fn views(
    s: &Spec,
    views: &mut [PlannedView],
    canonical: Option<&Plan>,
    sheet: &PlannedSheet,
    obstacles: &[[f64; 4]],
    problems: &mut Vec<Problem>,
) {
    if crate::engine_pins::loading() {
        crate::engine_pins::views(views);
        return;
    }
    if crate::incremental::active() {
        if let Some(base) = canonical {
            for v in views.iter_mut() {
                if let Some(old) = base.views.iter().find(|b| b.id == v.id) {
                    *v = old.clone();
                }
            }
        }
        return;
    }
    let page = sheet.page;
    for (name, requested) in &s.views {
        if let Some(p) = &requested.place
            && views.iter().any(|v| v.id == format!("v:{name}"))
            && unconventional(s, name, p)
        {
            let mut finding = problem(
                &format!("v:{name}"),
                "projection_relation_infeasible",
                format!(
                    "The authored {name} relation is infeasible in {}-angle projection; principal views must keep their conventional side and aligned projection origins.",
                    if s.sheet.projection.as_deref() == Some("first") {
                        "first"
                    } else {
                        "third"
                    }
                ),
                &format!("views/{name}/place"),
            );
            finding.severity = "error".into();
            finding.refs = vec!["sheet/projection".into()];
            if !problems.iter().any(|q| q.id == finding.id) {
                problems.push(finding);
            }
        }
    }
    if let Some(c) = canonical {
        for v in views.iter_mut() {
            if let Some(base) = c.views.iter().find(|b| b.id == v.id) {
                v.at = base.at;
                v.area = base.area;
                v.scale = base.scale;
            }
        }
    }
    let mut resolved = BTreeSet::new();
    for _ in 0..views.len() {
        for i in 0..views.len() {
            let name = views[i].id.trim_start_matches("v:").to_string();
            let Some(p) = s.views.get(&name).and_then(|v| v.place.as_ref()) else {
                resolved.insert(name);
                continue;
            };
            if resolved.contains(&name) {
                continue;
            }
            if s.views
                .get(&p.relative_to)
                .is_some_and(|v| v.place.is_some())
                && !resolved.contains(&p.relative_to)
            {
                continue;
            }
            let Some(anchor) = views
                .iter()
                .find(|v| v.id == format!("v:{}", p.relative_to))
            else {
                continue;
            };
            let b = anchor.area;
            let a = views[i].area;
            let horizontal = matches!(p.side, Side::Left | Side::Right);
            let k = usize::from(horizontal);
            let principal_pair = projection_related(&name, &p.relative_to);
            // Retain middle's historical principal-view behavior for saved Specs.
            let offset = if principal_pair && p.align == ViewAlign::Middle {
                anchor.at[k] - views[i].at[k]
            } else {
                match p.align {
                    ViewAlign::Start => b[k] - a[k],
                    ViewAlign::Middle => 0.5 * (b[k] + b[k + 2] - a[k] - a[k + 2]),
                    ViewAlign::End | ViewAlign::Outside => b[k + 2] - a[k + 2],
                    ViewAlign::Projection => anchor.at[k] - views[i].at[k],
                }
            };
            let out = match p.side {
                Side::Above => b[3] + p.gap - a[1],
                Side::Below => b[1] - p.gap - a[3],
                Side::Right => b[2] + p.gap - a[0],
                Side::Left => b[0] - p.gap - a[2],
            };
            let exact = |d: f64| if d.abs() < 1e-9 { 0.0 } else { d };
            let previous = views[i].at;
            shift(
                &mut views[i],
                if horizontal {
                    [exact(out), exact(offset)]
                } else {
                    [exact(offset), exact(out)]
                },
            );
            if name == "front" {
                let delta = [0, 1].map(|k| views[i].at[k] - previous[k]);
                for partner in views
                    .iter_mut()
                    .filter(|v| matches!(v.id.as_str(), "v:plan" | "v:side"))
                {
                    let is_plan = partner.id == "v:plan";
                    let key = partner.id.trim_start_matches("v:");
                    let pinned = s.views.get(key).is_some_and(|v| v.place.is_some())
                        || canonical.is_some_and(|p| {
                            s.dimensions
                                .iter()
                                .filter_map(|(id, d)| {
                                    d.place.as_ref().filter(|place| place.pinned).map(|_| id)
                                })
                                .chain(s.annotations.iter().filter_map(|(id, d)| {
                                    d.place.as_ref().filter(|place| place.pinned).map(|_| id)
                                }))
                                .any(|id| carrier(p, id).as_deref() == Some(partner.id.as_str()))
                        });
                    if !pinned {
                        shift(
                            partner,
                            if is_plan {
                                [delta[0], 0.0]
                            } else {
                                [0.0, delta[1]]
                            },
                        );
                    }
                }
            }
            // The isometric's area is a reserved region, not a dimensioned projection frame.
            // Resolve its ink inside that region before fixing it, as the automatic fitter does.
            if name == "iso"
                && p.pinned
                && let Some(c) = canonical
            {
                let a = views[i].area;
                let margin = crate::layout::FRAME;
                // A settled automatic iso carries its ink band, not the old empty
                // fitting rectangle. Offer the remaining paper on the authored side
                // before resolving obstacles; the pinned gap still locates that region.
                let region = match p.side {
                    Side::Right | Side::Above => [
                        a[0].max(margin),
                        a[1].max(margin),
                        page[0] - margin,
                        page[1] - margin,
                    ],
                    Side::Left => [
                        margin,
                        a[1].max(margin),
                        a[2].min(page[0] - margin),
                        page[1] - margin,
                    ],
                    Side::Below => [
                        a[0].max(margin),
                        margin,
                        page[0] - margin,
                        a[3].min(page[1] - margin),
                    ],
                };
                let occupied: Vec<_> = c
                    .views
                    .iter()
                    .filter(|v| {
                        v.id != "v:iso"
                            && !(v.id == "v:iso_below"
                                && s.views.get("iso_below").is_none_or(|v| v.place.is_none()))
                    })
                    .map(|v| {
                        if s.views
                            .get(v.id.trim_start_matches("v:"))
                            .is_some_and(|v| v.place.as_ref().is_some_and(|p| p.pinned))
                        {
                            views
                                .iter()
                                .find(|p| p.id == v.id)
                                .map_or(v.area, |p| p.area)
                        } else {
                            v.area
                        }
                    })
                    .chain([sheet.title_block])
                    .chain(
                        c.hole_tables
                            .iter()
                            .flat_map(|t| &t.blocks)
                            .filter_map(|b| b.placed.as_ref().map(|p| p.cells)),
                    )
                    .collect();
                if region[0] < region[2]
                    && region[1] < region[3]
                    && (region != a || occupied.iter().any(|b| overlap(a, *b)))
                {
                    let size = [a[2] - a[0], a[3] - a[1]];
                    let fit = crate::layout::largest_empty_rect(region, &occupied, size);
                    if !occupied.iter().any(|b| overlap(fit, *b)) {
                        let factor = ((fit[2] - fit[0]) / size[0])
                            .min((fit[3] - fit[1]) / size[1])
                            .min(1.0);
                        let v = &mut views[i];
                        v.at = [0, 1].map(|k| {
                            0.5 * (fit[k] + fit[k + 2])
                                + factor * (v.at[k] - 0.5 * (a[k] + a[k + 2]))
                        });
                        v.scale *= factor;
                        // At the established scale the settled ink/caption band already fits.
                        // The larger search rectangle is free room, not occupied ink: keeping
                        // it as the reservation consumes the adjacent schedule's clearance.
                        v.area = if factor >= 1.0 - 1e-9 {
                            let centre = [0, 1].map(|k| 0.5 * (fit[k] + fit[k + 2]));
                            [
                                centre[0] - 0.5 * factor * size[0],
                                centre[1] - 0.5 * factor * size[1],
                                centre[0] + 0.5 * factor * size[0],
                                centre[1] + 0.5 * factor * size[1],
                            ]
                        } else {
                            fit
                        };
                        v.basis += "; isometric ink fitted inside its relative placement region";
                    }
                }
            }
            views[i].default = false;
            views[i].basis += "; placed relative to its anchor by the spec";
            resolved.insert(name);
        }
    }
    // Dragging either pictorial leaves its unpinned partner automatic. Find room using
    // settled peer reservations rather than rejecting a valid editor placement.
    let mut held_pictorials = BTreeSet::new();
    for (name, partner) in [("iso_below", "iso"), ("iso", "iso_below")] {
        if s.views.get(name).is_some_and(|v| v.place.is_some())
            || !s.views.get(partner).is_some_and(|v| v.place.is_some())
        {
            continue;
        }
        let Some(i) = views.iter().position(|v| v.id == format!("v:{name}")) else {
            continue;
        };
        // A relative pin also holds its ancestor frames. Moving this partner alone
        // would silently change the authored gap/alignment of the pinned view.
        let mut ancestor = partner;
        let mut held = false;
        for _ in 0..s.views.len() {
            let Some(place) = s.views.get(ancestor).and_then(|v| v.place.as_ref()) else {
                break;
            };
            if place.relative_to == name {
                held = true;
                break;
            }
            ancestor = &place.relative_to;
        }
        if held {
            continue;
        }
        let occupied: Vec<_> = views
            .iter()
            .enumerate()
            .filter(|(j, _)| *j != i)
            .map(|(_, v)| v.area)
            .chain([sheet.title_block])
            .chain(obstacles.iter().copied())
            .collect();
        let region = [
            crate::layout::FRAME,
            crate::layout::FRAME,
            page[0] - crate::layout::FRAME,
            page[1] - crate::layout::FRAME,
        ];
        let a = views[i].area;
        let size = [a[2] - a[0], a[3] - a[1]];
        let fit = crate::layout::largest_empty_rect(region, &occupied, size);
        if fit[2] > fit[0] && fit[3] > fit[1] && !occupied.iter().any(|b| overlap(fit, *b)) {
            let factor = ((fit[2] - fit[0]) / size[0])
                .min((fit[3] - fit[1]) / size[1])
                .min(1.);
            if name == "iso" && s.iso_scale_factor.is_some() && factor < 1. - 1e-9 {
                let mut finding = problem(
                    &views[i].id,
                    "pictorial_placement_unresolved",
                    format!(
                        "The authored iso_scale_factor does not fit beside the pinned {partner}; move {partner}, enlarge the paper or remove iso_scale_factor."
                    ),
                    "iso_scale_factor",
                );
                finding.fix = vec![Op::remove("iso_scale_factor")];
                // Keep the later generic obstacle fitter from shrinking this authored frame.
                held_pictorials.insert(views[i].id.clone());
                problems.push(finding);
                continue;
            }
            let v = &mut views[i];
            let centre = [0, 1].map(|k| (fit[k] + fit[k + 2]) / 2.);
            v.at = [0, 1].map(|k| centre[k] + factor * (v.at[k] - (a[k] + a[k + 2]) / 2.));
            v.area = [
                centre[0] - factor * size[0] / 2.,
                centre[1] - factor * size[1] / 2.,
                centre[0] + factor * size[0] / 2.,
                centre[1] + factor * size[1] / 2.,
            ];
            v.scale *= factor;
        } else {
            problems.push(problem(&views[i].id, "pictorial_placement_unresolved", format!("No space remains for {name}; move the pinned {partner}, enlarge the paper or omit {name}."), &format!("views/{partner}/place")));
        }
    }
    for (name, v) in &s.views {
        if v.place.is_some() && !resolved.contains(name) {
            let id = format!("v:{name}");
            let mut p = problem(
                &id,
                "placement_anchor_not_shown",
                format!(
                    "The placement anchor of {} is not shown; its intent is retained.",
                    crate::wording::view(&id)
                ),
                &format!("views/{name}/place"),
            );
            if !views.iter().any(|v| v.id == id) {
                p.subjects.clear();
                p.refs.push(id);
            }
            problems.push(p);
        }
    }
    // Relative view pins have a free root when no annotation carrier is held.
    // Translate the connected orthographic group to fit a roomy named sheet,
    // preserving every gap and alignment, rather than centring its root so high
    // that a feasible relation is incorrectly reported off-sheet.
    if s.views.values().any(|v| v.place.is_some())
        && !s
            .dimensions
            .values()
            .any(|d| d.place.as_ref().is_some_and(|p| p.pinned))
        && !s
            .annotations
            .values()
            .any(|a| a.place.as_ref().is_some_and(|p| p.pinned))
    {
        let mut group: BTreeSet<String> = ["front", "plan", "side", "section_aa"]
            .map(String::from)
            .into_iter()
            .collect();
        for _ in 0..s.views.len() {
            for (name, v) in &s.views {
                if v.place
                    .as_ref()
                    .is_some_and(|p| group.contains(&p.relative_to))
                {
                    group.insert(name.clone());
                }
            }
        }
        let members: Vec<_> = views
            .iter()
            .filter(|v| group.contains(v.id.trim_start_matches("v:")))
            .collect();
        let margin = crate::layout::FRAME;
        let mut delta = [0.0; 2];
        for k in 0..2 {
            let lo = members
                .iter()
                .map(|v| v.area[k])
                .fold(f64::INFINITY, f64::min);
            let hi = members
                .iter()
                .map(|v| v.area[k + 2])
                .fold(f64::NEG_INFINITY, f64::max);
            if hi - lo <= page[k] - 2.0 * margin {
                delta[k] = if hi > page[k] - margin {
                    page[k] - margin - hi
                } else if lo < margin {
                    margin - lo
                } else {
                    0.0
                };
            }
        }
        for v in views
            .iter_mut()
            .filter(|v| group.contains(v.id.trim_start_matches("v:")))
        {
            shift(v, delta);
        }
    }
    let mut fixed: BTreeSet<String> = s
        .views
        .iter()
        // An explicit projection relation must also survive automatic obstacle packing.
        .filter(|(_, v)| {
            v.place
                .as_ref()
                .is_some_and(|p| p.pinned || p.align == ViewAlign::Projection)
        })
        .map(|(n, _)| format!("v:{n}"))
        .collect();
    fixed.extend(held_pictorials);
    if s.views.values().any(|v| {
        v.place
            .as_ref()
            .is_some_and(|p| p.align == ViewAlign::Projection)
    }) {
        // Front's automatic partners share its projection origins too. Packing one of
        // them independently after moving front would break an otherwise valid relation.
        fixed.extend(
            views
                .iter()
                .filter(|v| matches!(v.id.as_str(), "v:front" | "v:plan" | "v:side"))
                .map(|v| v.id.clone()),
        );
    }
    // Annotation anchors are fixed too, so filling around one cannot move its carrying view.
    if canonical.is_some() {
        for (id, p) in s
            .dimensions
            .iter()
            .filter_map(|(id, d)| Some((id, d.place.as_ref()?)))
            .chain(
                s.annotations
                    .iter()
                    .filter_map(|(id, d)| Some((id, d.place.as_ref()?))),
            )
        {
            if !p.pinned {
                continue;
            }
            let name = p
                .view
                .as_deref()
                .map(|n| format!("v:{n}"))
                .or_else(|| canonical.and_then(|c| carrier(c, id)));
            if let Some(n) = name {
                fixed.insert(n);
            }
        }
    }
    for _ in 0..views.len() {
        for v in views
            .iter()
            .filter(|v| fixed.contains(&v.id))
            .collect::<Vec<_>>()
        {
            if let Some(p) = s
                .views
                .get(v.id.trim_start_matches("v:"))
                .and_then(|v| v.place.as_ref())
            {
                fixed.insert(format!("v:{}", p.relative_to));
            }
        }
    }
    let margin = crate::layout::FRAME;
    // A constrained principal arrangement may consume the orientation budget.
    // Prefer the sheet scale whenever it fits; keep the existing five-percent
    // snapping rule and honour an authored isometric scale exactly.
    if s.iso_scale_factor.is_none()
        && !s.views.contains_key("iso")
        && s.views.iter().any(|(name, v)| {
            ["front", "plan", "side"].contains(&name.as_str())
                && v.place.as_ref().is_some_and(|p| p.pinned)
        })
        && let Some(iso) = views.iter_mut().find(|v| v.id == "v:iso")
        && iso.scale > sheet.scale_factor
    {
        iso.scale = sheet.scale_factor;
    }
    let mut occupied: Vec<[f64; 4]> = views
        .iter()
        .filter(|v| fixed.contains(&v.id))
        .map(|v| v.area)
        .collect();
    occupied.push(sheet.title_block);
    occupied.extend_from_slice(obstacles);
    for v in views.iter_mut().filter(|v| !fixed.contains(&v.id)) {
        if v.id == "v:iso" && occupied.iter().any(|b| overlap(v.area, *b)) {
            let a = v.area;
            let size = [a[2] - a[0], a[3] - a[1]];
            let fit = crate::layout::largest_empty_rect(
                [margin, margin, page[0] - margin, page[1] - margin],
                &occupied,
                size,
            );
            if !occupied.iter().any(|b| overlap(fit, *b)) {
                let factor = ((fit[2] - fit[0]) / size[0])
                    .min((fit[3] - fit[1]) / size[1])
                    .min(1.0);
                let scale = v.scale * factor;
                let scale = if s.iso_scale_factor.is_none() {
                    crate::layout::keep_sheet_scale(scale, sheet.scale_factor)
                } else {
                    scale
                };
                let factor = scale / v.scale;
                v.at = [0, 1].map(|k| {
                    0.5 * (fit[k] + fit[k + 2]) + factor * (v.at[k] - 0.5 * (a[k] + a[k + 2]))
                });
                v.scale = scale;
                v.area = fit;
                v.basis += "; fitted around fixed placement obstacles";
            }
        }
        if occupied.iter().any(|b| overlap(v.area, *b)) {
            let base = v.clone();
            'search: for shrink in 0..if v.id == "v:iso" { 10 } else { 1 } {
                let factor = 0.9_f64.powi(shrink);
                let mut trial = base.clone();
                trial.scale *= factor;
                trial.area = [
                    base.at[0] + factor * (base.area[0] - base.at[0]),
                    base.at[1] + factor * (base.area[1] - base.at[1]),
                    base.at[0] + factor * (base.area[2] - base.at[0]),
                    base.at[1] + factor * (base.area[3] - base.at[1]),
                ];
                let (w, h) = (trial.area[2] - trial.area[0], trial.area[3] - trial.area[1]);
                let mut xs = vec![trial.area[0], margin, page[0] - margin - w];
                let mut ys = vec![trial.area[1], margin, page[1] - margin - h];
                for b in &occupied {
                    xs.extend([b[2] + 1.0, b[0] - w - 1.0]);
                    ys.extend([b[3] + 1.0, b[1] - h - 1.0]);
                }
                let mut candidates: Vec<[f64; 2]> = xs
                    .iter()
                    .flat_map(|x| ys.iter().map(move |y| [*x, *y]))
                    .collect();
                candidates.sort_by(|a, b| {
                    ((a[0] - base.area[0]).abs() + (a[1] - base.area[1]).abs())
                        .total_cmp(&((b[0] - base.area[0]).abs() + (b[1] - base.area[1]).abs()))
                        .then(a[0].total_cmp(&b[0]))
                        .then(a[1].total_cmp(&b[1]))
                });
                for at in candidates {
                    let a = [at[0], at[1], at[0] + w, at[1] + h];
                    if a[0] >= margin
                        && a[1] >= margin
                        && a[2] <= page[0] - margin
                        && a[3] <= page[1] - margin
                        && !occupied.iter().any(|b| overlap(a, *b))
                    {
                        let d = [at[0] - trial.area[0], at[1] - trial.area[1]];
                        shift(&mut trial, d);
                        trial.basis += "; fitted around fixed placement obstacles";
                        *v = trial;
                        break 'search;
                    }
                }
            }
        }
        occupied.push(v.area);
    }
    if let Some(front) = views.iter().find(|v| v.id == "v:front") {
        let first = s.sheet.projection.as_deref() == Some("first");
        for v in views
            .iter()
            .filter(|v| matches!(v.id.as_str(), "v:plan" | "v:side"))
        {
            let align_axis = usize::from(v.id == "v:side");
            let outward_axis = 1 - align_axis;
            let outward =
                (v.at[outward_axis] - front.at[outward_axis]) * if first { -1.0 } else { 1.0 };
            if (v.at[align_axis] - front.at[align_axis]).abs() > 1e-6 || outward <= 0.0 {
                let mut finding = problem(
                    &v.id,
                    "projection_relation_infeasible",
                    format!(
                        "The authored layout is infeasible in {}-angle projection: {} must keep its conventional side and projection-origin alignment with front.",
                        if first { "first" } else { "third" },
                        crate::wording::view(&v.id)
                    ),
                    &format!("views/{}/place", v.id.trim_start_matches("v:")),
                );
                if s.views
                    .get(v.id.trim_start_matches("v:"))
                    .is_none_or(|v| v.place.is_none())
                {
                    finding.fix.clear();
                }
                finding.severity = "error".into();
                finding.refs = vec!["sheet/projection".into()];
                if !problems.iter().any(|q| q.id == finding.id) {
                    problems.push(finding);
                }
            }
        }
    }
    // Explicit relations can move a view off the sheet even without `pinned: true`.
    // Audit every authored view placement, not only the obstacle packer's fixed set.
    for v in views.iter().filter(|v| {
        s.views
            .get(v.id.trim_start_matches("v:"))
            .is_some_and(|v| v.place.is_some())
    }) {
        let a = v.area;
        let path = format!("views/{}/place", v.id.trim_start_matches("v:"));
        // Anchor arithmetic can put a boundary a few ulps beyond the paper margin.
        let tolerance = 1e-9;
        if a[0] < margin - tolerance
            || a[1] < margin - tolerance
            || a[2] > page[0] - margin + tolerance
            || a[3] > page[1] - margin + tolerance
        {
            let mut finding = problem(
                &v.id,
                "pinned_view_off_sheet",
                format!(
                    "{} is outside the drawing area. Remove or adjust its placement to bring it onto the sheet.",
                    crate::wording::capital(&crate::wording::view(&v.id))
                ),
                &path,
            );
            finding.severity = "error".into();
            problems.push(finding);
        }
        if fixed.contains(&v.id)
            && (views
                .iter()
                .any(|other| other.id != v.id && overlap(a, other.area))
                || overlap(a, sheet.title_block))
            && s.views
                .get(v.id.trim_start_matches("v:"))
                .is_some_and(|v| v.place.is_some())
        {
            problems.push(problem(
                &v.id,
                "pinned_view_overlap",
                format!(
                    "{} overlaps another view or the title block and is kept at its pinned position.",
                    crate::wording::capital(&crate::wording::view(&v.id))
                ),
                &path,
            ));
        }
    }
}

pub(crate) fn obstacles(
    s: &Spec,
    dimensions: &[crate::dims::PlannedDimension],
    fixed: &[crate::dims::PlannedDimension],
) -> Vec<[f64; 4]> {
    dimensions
        .iter()
        .filter(|d| {
            s.dimensions
                .get(&d.id)
                .and_then(|e| e.place.as_ref())
                .is_some_and(|p| p.pinned)
        })
        .chain(fixed)
        .filter_map(crate::dims::ink_box)
        .collect()
}

pub(crate) fn finish(plan: &mut Plan, analysis: &Analysis) {
    // Finishing runs again after geometry/table audits. Refresh this owned
    // finding instead of appending a second record with the same ID: collection
    // deltas address findings by ID and must replay the complete plan losslessly.
    plan.problems.retain(|p| p.code != "pinned_item_not_drawn");
    let pins: BTreeMap<String, String> = plan
        .spec
        .dimensions
        .iter()
        .filter(|(_, d)| d.place.as_ref().is_some_and(|p| p.pinned))
        .map(|(id, _)| (id.clone(), format!("dimensions/{}/place", key(id))))
        .chain(
            plan.spec
                .annotations
                .iter()
                .filter(|(_, d)| d.place.as_ref().is_some_and(|p| p.pinned))
                .map(|(id, _)| (id.clone(), format!("annotations/{}/place", key(id)))),
        )
        .collect();
    // A pinned callout is not routed, so the router's report of a leader cutting back into its
    // view's material (`dims::leaders`, ADR2-8) is made here, by the same measure lint's
    // `leader_crosses_silhouette` uses (ISSUE-798-d). Before the loop below, which gives it the
    // unpinning fix; once only, as this runs again after a hole table is chosen.
    for d in plan
        .dimensions
        .iter()
        .filter(|d| d.kind == "callout" && pins.contains_key(&d.id))
    {
        let cut = crate::dims::leader_reentry(analysis, &plan.views, d);
        let subjects = vec![d.id.clone(), d.view.clone()];
        let id = format!("p:annotation_line_over_view:{}", subjects.join("|"));
        if cut > 0.0 && !plan.problems.iter().any(|p| p.id == id) {
            plan.problems.push(Problem {
                id,
                code: "annotation_line_over_view".into(),
                severity: "warning".into(),
                message: format!(
                    "The pinned leader of {} runs back across its own view, {}, cutting {cut:.1} mm of it. Its bend and label are kept at the prescribed position.",
                    crate::wording::item(&d.id, &d.kind, &d.label),
                    crate::wording::view(&d.view)
                ),
                subjects,
                ..Default::default()
            });
        }
    }
    for p in &mut plan.problems {
        if p.code == "leader_through_feature"
            && let Some(id) = p.subjects.first().filter(|id| {
                pins.contains_key(*id)
                    && plan
                        .dimensions
                        .iter()
                        .any(|d| d.id == **id && d.kind == "callout")
            })
        {
            let label = plan
                .dimensions
                .iter()
                .find(|d| d.id == **id)
                .map_or_else(String::new, |d| d.label.clone());
            p.message = format!(
                "The pinned label's leader crosses part features: the leader of {} crosses {}. Its bend and label are kept at the prescribed position.",
                crate::wording::item(id, "callout", &label),
                crate::wording::feature_id(analysis, &p.subjects[1])
            );
            if !p.refs.contains(id) {
                p.refs.push(id.clone());
            }
        }
        if p.code.starts_with("annotation_")
            && let Some((_, path)) = p.subjects.iter().find_map(|id| pins.get_key_value(id))
        {
            // Only this unpinning fix is stated here. No move is guessed: one nobody has
            // measured can leave the drawing worse, and `fixes::attach` offers the placement
            // it has checked against the settled ink.
            p.fix = vec![Op::set(path, json!({}))];
        }
    }
    for (id, path) in pins {
        let item = crate::items::items(plan).into_iter().find(|i| i.id == id);
        let named = item.as_ref().map_or_else(
            || "a pinned item no longer in the plan".to_string(),
            |i| crate::wording::item(&id, &i.kind, i.label.as_deref().unwrap_or("")),
        );
        let state = item.map(|i| i.state);
        if !matches!(state, Some(crate::items::State::Shown)) {
            let mut p = problem(
                &id,
                "pinned_item_not_drawn",
                format!(
                    "{} cannot be drawn at its pinned anchor; its placement is retained.",
                    crate::wording::capital(&named)
                ),
                &path,
            );
            if state.is_none() {
                p.subjects.clear();
                p.refs.push(id);
            }
            plan.problems.push(p);
        }
    }
}

/// A side alone is an author constraint on the shared solve, rather than a fixed station.
pub(crate) fn side_hint(p: &Place) -> Option<Side> {
    (!p.pinned && p.lane.is_none() && p.along.is_none() && p.offset.is_none())
        .then_some(p.side)
        .flatten()
}

pub(crate) fn automatic_pin(p: &Place) -> bool {
    p.pinned && p.side.is_none() && p.lane.is_none() && p.along.is_none() && p.offset.is_none()
}

/// Bare dimension pins keep settled peers in their carrier frames. Explicit view
/// relations transport those frames too; they do not change annotation intent.
pub(crate) fn automatic_pins_only(spec: &Spec) -> bool {
    spec.dimensions
        .values()
        .any(|d| d.place.as_ref().is_some_and(automatic_pin))
        && spec
            .dimensions
            .values()
            .all(|d| d.place.as_ref().is_none_or(automatic_pin))
        && spec.annotations.is_empty()
        && spec
            .dimensions
            .values()
            .all(|d| d.tolerance.is_none() && d.text.is_none() && d.style.is_none())
        && spec.features.values().all(|f| f.fit.is_none())
}

/// Whether an offset preserves canonical ink: zero for linear labels, or the
/// automatic bend minus the leader tip for callouts.
pub(crate) fn automatic_offset(base: &Plan, id: &str, offset: [f64; 2]) -> bool {
    if base
        .dimensions
        .iter()
        .any(|d| d.id == id && d.kind == "linear" && d.placed.is_some())
    {
        return offset.iter().all(|v| v.abs() < 1e-6);
    }
    base.dimensions
        .iter()
        .find(|d| d.id == id && d.kind == "callout")
        .and_then(|d| d.placed.as_ref()?.lines.first().copied())
        .is_some_and(|[tip, bend]| {
            (bend[0] - tip[0] - offset[0]).abs() < 1e-6
                && (bend[1] - tip[1] - offset[1]).abs() < 1e-6
        })
}

pub(crate) fn anchor_delta(base: &Plan, views: &[PlannedView], view: &str) -> Option<[f64; 2]> {
    let before = base.views.iter().find(|v| v.id == view)?;
    let after = views.iter().find(|v| v.id == view)?;
    Some([after.at[0] - before.at[0], after.at[1] - before.at[1]])
}

pub(crate) fn move_point(p: &mut [f64; 2], delta: [f64; 2]) {
    if delta != [0.0, 0.0] {
        p[0] += delta[0];
        p[1] += delta[1];
    }
}
pub(crate) fn move_box(b: &mut [f64; 4], delta: [f64; 2]) {
    if delta != [0.0, 0.0] {
        for k in 0..4 {
            b[k] += delta[k % 2];
        }
    }
}

pub(crate) fn held_dimensions(
    base: Option<&Plan>,
    views: &[PlannedView],
) -> Vec<crate::dims::PlannedDimension> {
    let Some(base) = base else {
        return Vec::new();
    };
    base.dimensions
        .iter()
        .filter_map(|d| {
            let delta = anchor_delta(base, views, &d.view)?;
            let mut d = d.clone();
            if let Some(p) = &mut d.placed {
                for line in p.lines.iter_mut().chain(&mut p.arrows) {
                    for q in line {
                        move_point(q, delta);
                    }
                }
                move_point(&mut p.label_at, delta);
                move_box(&mut p.label_box, delta);
            }
            Some(d)
        })
        .collect()
}

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

    #[test]
    fn an_underside_pin_cannot_silently_shrink_an_authored_iso_scale() {
        let (_, a) = crate::model::analyse_native(
            &std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("tests/fixtures/parts/plate.step"),
        )
        .unwrap();
        let spec: Spec = serde_json::from_value(json!({"views":{"iso_below":{}}})).unwrap();
        let plan = crate::model::plan(&a, &spec).unwrap();
        let mut anchor = plan
            .views
            .iter()
            .find(|v| v.id == "v:front")
            .unwrap()
            .clone();
        anchor.area = [35., 70., 45., 80.];
        anchor.at = [40., 75.];
        let mut pair: Vec<_> = plan
            .views
            .into_iter()
            .filter(|v| matches!(v.id.as_str(), "v:iso" | "v:iso_below"))
            .collect();
        let top = pair.iter_mut().find(|v| v.id == "v:iso").unwrap();
        top.area = [0., 0., 80., 80.];
        top.at = [40., 40.];
        top.scale = 2.;
        let bottom = pair.iter_mut().find(|v| v.id == "v:iso_below").unwrap();
        bottom.area = [0., 0., 10., 10.];
        bottom.at = [5., 5.];
        pair.push(anchor);
        let mut spec: Spec = serde_json::from_value(json!({"views":{"iso_below":{"place":{"relative_to":"front","side":"above","gap":0,"pinned":true}}}})).unwrap();
        let mut sheet = plan.sheet;
        sheet.page = [100., 100.];
        sheet.title_block = [5., 5., 95., 65.];
        for authored in [false, true] {
            spec.iso_scale_factor = authored.then_some(1.);
            let mut trial = pair.clone();
            let mut problems = Vec::new();
            views(&spec, &mut trial, None, &sheet, &[], &mut problems);
            let top = trial.iter().find(|v| v.id == "v:iso").unwrap();
            if authored {
                assert_eq!(top.scale, 2.);
                assert!(
                    problems
                        .iter()
                        .any(|p| p.code == "pictorial_placement_unresolved"
                            && p.message.contains("remove iso_scale_factor")
                            && p.fix == vec![Op::remove("iso_scale_factor")]
                            && serde_json::to_value(p).unwrap()["impact"] == "decision")
                );
            } else {
                assert!(top.scale < 2.);
            }
        }
    }
    #[test]
    fn issue109_projection_origin_survives_lopsided_bands_and_obstacle_packing() {
        let sheet: PlannedSheet = serde_json::from_value(json!({
            "size":"A3", "page":[420,297], "scale":"1:1", "scale_factor":1,
            "arrangement":"columns", "title_block":[350,10,410,40],
            "default":true, "basis":"test", "state":{"kind":"shown"}, "title":{}
        }))
        .unwrap();
        for pinned in [false, true] {
            for gap in [0., 5., 15., 30., 1000.] {
                let mut pair: Vec<PlannedView> = serde_json::from_str(include_str!(
                    "../tests/fixtures/projection_alignment/issue109.views.json"
                ))
                .unwrap();
                let spec: Spec = serde_json::from_value(json!({"views":{"plan":{"place":{
                    "relative_to":"front", "side":"above", "align":"projection", "gap":gap, "pinned":pinned
                }}}})).unwrap();
                // This obstacle cannot be cleared by sliding the projection off its line.
                views(
                    &spec,
                    &mut pair,
                    None,
                    &sheet,
                    &[[15., 125., 230., 285.]],
                    &mut Vec::new(),
                );
                assert!((pair[1].at[0] - pair[0].at[0]).abs() < 1e-6);
                assert!((pair[1].area[1] - pair[0].area[3] - gap).abs() < 1e-6);
            }
        }
    }

    #[test]
    fn pinned_view_bounds_distinguish_roundoff_from_overruns_on_every_edge() {
        let sheet: PlannedSheet = serde_json::from_value(json!({
            "size":"A4", "page":[210,297], "scale":"1:1", "scale_factor":1,
            "arrangement":"columns", "title_block":[150,10,200,40],
            "default":true, "basis":"test", "state":{"kind":"shown"}, "title":{}
        }))
        .unwrap();
        let view: PlannedView = serde_json::from_value(json!({
            "id":"v:plan", "direction":[0,0,1], "up":[0,1,0], "at":[50,50],
            "scale":1, "area":[10,10,200,287], "default":true,
            "basis":"test", "state":{"kind":"shown"}
        }))
        .unwrap();
        let spec: Spec = serde_json::from_value(json!({"views":{"plan":{"place":{
            "relative_to":"front", "side":"above", "pinned":true
        }}}}))
        .unwrap();
        for edge in 0..4 {
            for (error, off_sheet) in [(0.0, false), (1e-12, false), (1e-4, true)] {
                let mut held = view.clone();
                held.area[edge] += if edge < 2 { -error } else { error };
                let area = held.area;
                let mut problems = Vec::new();
                views(
                    &spec,
                    std::slice::from_mut(&mut held),
                    None,
                    &sheet,
                    &[],
                    &mut problems,
                );
                assert_eq!(held.area, area);
                assert_eq!(
                    problems
                        .iter()
                        .any(|p| p.code == "pinned_view_off_sheet" && p.subjects == ["v:plan"]),
                    off_sheet,
                    "edge {edge}, error {error}"
                );
            }
        }
    }
}