summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/pack-rul.mkxl
blob: 2bd768bd06bbff50357ba7241a67c7430d771c61 (plain)
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
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
%D \module
%D   [       file=pack-rul, % was core-rul,
%D        version=1998.10.16,
%D          title=\CONTEXT\ Packaging Macros,
%D       subtitle=Ruled Content,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Packaging Macros / Ruled Content}

%D The code here is expanded lots of time as framed is used in many places. This is
%D why the code here is (and gets) optimized as much as possible. Also, by avoiding
%D packaging and expansion we also keep tracing reasonable. For instance, multiple
%D stacked backgrounds can slow down a run if not optimized this way.

\registerctxluafile{pack-rul}{optimize}

\unprotect

% \definesystemvariable {ol}   % OmLijnd -> check scrn-fld too

%D \macros
%D   {linewidth, setuplinewidth}
%D
%D This module deals with rules (lines) in several ways. First we introduce two
%D macros that can be used to set some common characteristics.
%D
%D \showsetup{setuplinewidth}
%D
%D The linewidth is available in \type{\linewidth}. The preset value of .4pt equals
%D the default hard coded \TEX\ rule width.

\newdimen\linewidth

\unexpanded\def\setuplinewidth
  {\dosingleargument\pack_framed_setup_line_width}

\def\pack_framed_setup_line_width[#1]%
  {\assigndimension{#1}\linewidth{.2\points}{.4\points}{.6\points}}

%D The parameter handler:

\installcorenamespace{framed}
\installcorenamespace{framedtop}
\installcorenamespace{framedbottom}
\installcorenamespace{framedleft}
\installcorenamespace{framedright}

\installcorenamespace{regularframed}
\installcorenamespace{simplifiedframed}

\installcommandhandler \??framed {framed} \??framed

\let\pack_framed_framedparameter    \framedparameter
\let\pack_framed_framedparameterhash\framedparameterhash
\let\pack_framed_setupcurrentframed \setupcurrentframed

\def\pack_framed_initialize
  {\let\framedparameter    \pack_framed_framedparameter
   \let\framedparameterhash\pack_framed_framedparameterhash
   \let\setupcurrentframed \pack_framed_setupcurrentframed
   \inframedtrue}

%D A helper:

\def\frameddimension#1{\the\dimexpr\framedparameter{#1}\relax}

%D Inheritance:

\def\installinheritedframed#1%
  {\normalexpanded{\doinstallinheritedframed
     \expandafter\noexpand\csname current#1\endcsname
     \expandafter\noexpand\csname #1parameter\endcsname
     \expandafter\noexpand\csname #1parameterhash\endcsname
     \expandafter\noexpand\csname do#1parameter\endcsname
     \expandafter\noexpand\csname do#1parentparameter\endcsname
     \expandafter\noexpand\csname do#1rootparameter\endcsname
     \expandafter\noexpand\csname setupcurrent#1\endcsname
     \expandafter\noexpand\csname inherited#1framed\endcsname
     \expandafter\noexpand\csname inherited#1framedbox\endcsname}} % new

\unexpanded\def\doinstallinheritedframed#1#2#3#4#5#6#7#8#9%
  {\def#5##1##2{\ifx##1\relax#6{##2}\else#4{##1}{##2}\fi}%
  %\def#6##1{\ifcsname\??framed:##1\endcsname\??framed:##1\else\s!empty\fi}% root
   \def#6##1{\ifcsname\??framed:##1\endcsname\??framed:##1\else\??empty\fi}% root
   \unexpanded\def#8%
     {\bgroup
      \bgroup
      \inframedtrue
      \let\currentframed      #1%
      \let\framedparameter    #2%
      \let\framedparameterhash#3%
      \let\setupcurrentframed #7%
      \pack_framed_process_indeed}%
   \unexpanded\def#9%
     {\bgroup
      \inframedtrue
      \let\currentframed      #1%
      \let\framedparameter    #2%
      \let\framedparameterhash#3%
      \let\setupcurrentframed #7%
      \pack_framed_process_box_indeed}}

\unexpanded\def\installframedcommandhandler#1#2#3%
  {\installcommandhandler{#1}{#2}{#3}%
   \installinheritedframed{#2}}

\unexpanded\def\installframedautocommandhandler#1#2#3%
  {\installautocommandhandler{#1}{#2}{#3}%
   \installinheritedframed{#2}}

\unexpanded\def\installsimpleframedcommandhandler#1#2#3%
  {\installsimplecommandhandler{#1}{#2}{#3}%
   \installinheritedframed{#2}}

% for regular framed

\setupframed
  [\c!width=\v!fit,
   \c!height=\v!broad,
  %\c!minheight=\zeropoint,
  %\c!lines=,
   \c!offset=.25\exheight,  % \defaultframeoffset
   \c!empty=\v!no,
   \c!frame=\v!on,
  %\c!topframe=,
  %\c!bottomframe=,
  %\c!leftframe=,
  %\c!rightframe=,
   \c!radius=.5\bodyfontsize,
   \c!rulethickness=\linewidth,
   \c!corner=\v!rectangular,
   \c!depth=\zeropoint,
  %\c!foregroundcolor=,
  %\c!foregroundstyle=,
  %\c!background=,
  %\c!backgroundcolor=,
   \c!backgroundoffset=\zeropoint,
  %\c!framecolor=,
   \c!frameoffset=\zeropoint,
   \c!backgroundcorner=\framedparameter\c!corner, % use \p_ here
   \c!backgroundradius=\framedparameter\c!radius,
   \c!backgrounddepth=\framedparameter\c!depth,
   \c!framecorner=\framedparameter\c!corner,
   \c!frameradius=\framedparameter\c!radius,
   \c!framedepth=\framedparameter\c!depth,
  %\c!component=,
  %\c!region=,
  %\c!align=,
   \c!bottom=\vss,
  %\c!top=,
   \c!strut=\v!yes,
   \c!autostrut=\v!yes,
   \c!location=\v!normal,
  %\c!orientation=,
  %\c!anchoring=,
   \c!autowidth=\v!yes,
  %\c!setups=,
   \c!loffset=\zeropoint,
   \c!roffset=\zeropoint,
   \c!toffset=\zeropoint,
   \c!boffset=\zeropoint]

%D For backgrounds and such:

\defineframed
  [\??simplifiedframed]
  [\c!frame=\v!off,
   \c!depth=\zeropoint,
   \c!offset=\v!overlay,
   \c!component=,
   \c!region=,
   \c!radius=.5\bodyfontsize,
   \c!rulethickness=\linewidth,
   \c!corner=\v!rectangular,
   \c!backgroundoffset=\zeropoint,
   \c!frameoffset=\zeropoint,
   \c!backgroundcorner=\framedparameter\c!corner,  % use \p_ here
   \c!backgroundradius=\framedparameter\c!radius,
   \c!backgrounddepth=\framedparameter\c!depth,
   \c!framecorner=\framedparameter\c!corner,
   \c!frameradius=\framedparameter\c!radius,
   \c!framedepth=\framedparameter\c!depth,
   \c!location=\v!normal,
   \c!loffset=\zeropoint,
   \c!roffset=\zeropoint,
   \c!toffset=\zeropoint,
   \c!boffset=\zeropoint]

\unexpanded\def\definesimplifiedframed[#1]% no settings
  {\defineframed[#1][\??simplifiedframed]%
   \expandafter\let\csname#1\endcsname\undefined}

\expandafter\let\csname\??simplifiedframed\endcsname\undefined

%D We will communicate through module specific variables, current framed
%D parameters and some reserved dimension registers.

\newdimen\d_framed_target_wd
\newdimen\d_framed_target_ht
\newdimen\d_framed_target_dp
\newdimen\d_framed_linewidth  \let\ruledlinewidth\d_framed_linewidth % needed at lua end

\let\p_framed_frame           \empty % \framedparameter\c!frame
\let\p_framed_backgroundoffset\empty
\let\p_framed_foregroundstyle \empty
\let\p_framed_autostrut       \empty
\let\p_framed_location        \empty
\let\p_framed_orientation     \empty
\let\p_framed_anchoring       \empty
\let\p_framed_autowidth       \empty
\let\p_framed_franalyze       \empty
\let\p_framed_backgroundcorner\empty
\let\p_framed_backgroundradius\empty
\let\p_framed_framecorner     \empty
\let\p_framed_frameradius     \empty
\let\p_framed_lines           \empty
\let\p_framed_empty           \empty
\let\p_framed_backgroundcolor \empty
\let\p_framed_framecolor      \empty
\let\p_framed_component       \empty
\let\p_framed_background      \empty
\let\p_framed_rulethickness   \empty
\let\p_framed_foregroundcolor \empty
\let\p_framed_setups          \empty

%D We don't have to stick to a \TEX\ drawn rule, but also can use rounded
%D or even fancier shapes, as we will see later on.

\def\pack_framed_filled_box
  {\edef\p_framed_backgroundcorner{\framedparameter\c!backgroundcorner}%
   \ifx\p_framed_backgroundcorner\v!rectangular
     \pack_framed_filled_box_normal
   \else
     \pack_framed_filled_box_radius
   \fi}

\def\pack_framed_filled_box_normal
  {\vrule
     \s!width \d_framed_target_wd
     \s!height\d_framed_target_ht
     \s!depth \d_framed_target_dp
   \relax}

\def\ntg-context@ntg.nllled_box_radius
  {\edef\p_framed_backgroundradius{\framedparameter\c!backgroundradius}%
   \ifzeropt\dimexpr\p_framed_backgroundradius\relax % just in case of .x\bodyfontsize
     \pack_framed_filled_box_normal
   \else
     \pack_framed_filled_box_round
   \fi}

\def\pack_framed_filled_box_round
  {\raise\d_framed_target_dp\hpack{\frule
     type   fill
     width  \d_framed_target_wd
     height \d_framed_target_ht
     depth  \d_framed_target_dp
     line   \d_framed_linewidth
     radius \p_framed_backgroundradius\space
     corner {\p_framed_backgroundcorner}
   \relax}}

\def\pack_framed_stroked_box
  {\edef\p_framed_framecorner{\framedparameter\c!framecorner}%
   \ifx\p_framed_framecorner\v!rectangular
     \pack_framed_stroked_box_normal
   \else
     \pack_framed_stroked_box_radius
   \fi}

\def\pack_framed_stroked_box_radius
  {\edef\p_framed_frameradius{\framedparameter\c!frameradius}%
   \ifzeropt\dimexpr\p_framed_frameradius\relax % just in case of .x\bodyfontsize
     \pack_framed_stroked_box_normal
   \orelse\ifx\p_framed_frame\v!on
     \pack_framed_stroked_box_round
   \fi}

% \pack_framed_stroked_box_normal % later

\def\pack_framed_stroked_box_round
  {\raise\d_framed_target_dp\hpack{\frule
     width  \d_framed_target_wd
     height \d_framed_target_ht
     depth  \d_framed_target_dp
     line   \d_framed_linewidth
     radius \p_framed_frameradius\space
     corner {\p_framed_framecorner}
   \relax}}

% a lot of weird corners
%
% \startTEXpage
%     \dontleavehmode\framed
%         [corner=0,frame=on,framecolor=green,
%          background=color,backgroundcolor=yellow]{\tttf TEST \twodigits\recurselevel}%
%     \vskip1em
%     \dontleavehmode\dostepwiserecurse {1} {4}{1}{\framed
%         [corner=\recurselevel,frame=on,framecolor=green,
%          background=color,backgroundcolor=yellow]{\tttf TEST \twodigits\recurselevel}%
%         \quad}
%     \vskip1em
%     \dontleavehmode\dostepwiserecurse {5} {8}{1}{\framed
%         [corner=\recurselevel,frame=on,framecolor=green,
%          background=color,backgroundcolor=yellow]{\tttf TEST \twodigits\recurselevel}%
%         \quad}
%     \vskip1em
%     \dontleavehmode\dostepwiserecurse {1} {4}{1}{\framed
%         [corner=\recurselevel,frame=on,framecolor=green]{\tttf TEST \twodigits\recurselevel}%
%         \quad}
%     \vskip1em
%     \dontleavehmode\dostepwiserecurse {5} {8}{1}{\framed
%         [corner=\recurselevel,frame=on,framecolor=green]{\tttf TEST \twodigits\recurselevel}%
%         \quad}
%     \vskip1em
%     \dontleavehmode\dostepwiserecurse {9}{12}{1}{\framed
%         [corner=\recurselevel,frame=on,framecolor=green]{\tttf TEST \twodigits\recurselevel}%
%         \quad}
%     \vskip1em
%     \dontleavehmode\dostepwiserecurse{13}{16}{1}{\framed
%         [corner=\recurselevel,frame=on,framecolor=green]{\tttf TEST \twodigits\recurselevel}%
%         \quad}
%     \vskip1em
%     \dontleavehmode\dostepwiserecurse{17}{20}{1}{\framed
%         [corner=\recurselevel,frame=on,framecolor=green]{\tttf TEST \twodigits\recurselevel}%
%         \quad}
%     \vskip1em
%     \dontleavehmode\dostepwiserecurse{21}{24}{1}{\framed
%         [corner=\recurselevel,frame=on,framecolor=green]{\tttf TEST \twodigits\recurselevel}%
%         \quad}
%     \vskip1em
%     \dontleavehmode\dostepwiserecurse{25}{28}{1}{\framed
%         [corner=\recurselevel,frame=on,framecolor=green]{\tttf TEST \twodigits\recurselevel}%
%         \quad}
% \stopTEXpage

%D It won't be a surprise that we not only provide gray boxes, but also colored
%D ones. Here it is:

\def\pack_framed_background_box_color
  {\edef\p_framed_backgroundcolor{\framedparameter\c!backgroundcolor}%
   \ifx\p_framed_backgroundcolor\empty \else
     \doifcolor\p_framed_backgroundcolor\pack_framed_background_box_color_indeed
   \fi}

\def\pack_framed_background_box_color_indeed
  {\hpack{\dousecolorparameter\p_framed_backgroundcolor\pack_framed_filled_box}}

%D \macros
%D   {defineoverlay, doifoverlayelse, overlayoffset,
%D    overlaywidth, overlayheight, overlaydepth,
%D    overlaycolor, overlaylinecolor, overlaylinewidth}
%D
%D Before we define the macro that actually takes card of the backgrounds, we
%D introduce overlays. An overlay is something that contrary to its name lays {\em
%D under} the text. An example of an overlay definition is:
%D
%D \startbuffer[tmp-1]
%D \defineoverlay
%D   [fancy]
%D   [{\externalfigure
%D       [mp-cont.502]
%D       [width=\overlaywidth,
%D        height=\overlayheight]}]
%D \stopbuffer
%D
%D \typebuffer[tmp-1]
%D
%D  That for instance can be uses in:
%D
%D  \startbuffer[tmp-2]
%D  \framed[backgroundachtergrond=fancy]{How Fancy!}
%D  \framed[backgroundachtergrond=fancy,frame=off]{Even More Fancy!}
%D  \stopbuffer
%D
%D  and looks like:
%D
%D  \startlinecorrection
%D  \vbox{\baselineskip24pt\getbuffer[tmp-1]\getbuffer[tmp-2]}
%D  \stoplinecorrection
%D
%D The formal definition is:
%D
%D \showsetup{defineoverlay}
%D
%D This macro's definition is a bit obscure, due the many non||used arguments and
%D the two step call that enable the setting of the width, height and depth
%D variables. Multiple backgrounds are possible and are specified as:
%D
%D \starttyping
%D \framed[background={one,two,three}]{Three backgrounds!}
%D \stoptyping
%D
%D Most drawing packages only know width and height. Therefore the dimensions have a
%D slightly different meaning here:
%D
%D \startitemize[packed]
%D \item \type{\overlaywidth }: width of the overlay
%D \item \type{\overlayheight}: height plus depth of the overlay
%D \item \type{\overlaydepth }: depth of the overlay
%D \stopitemize
%D
%D The resulting box is lowered to the right depth.

%def\overlaywidth      {\the\hsize\space} % We preset the variables
%def\overlayheight     {\the\vsize\space} % to some reasonable default
%def\overlaydepth      {0pt }             % values. The attributes
%let\overlayoffset     \overlaydepth      % of the frame can be (are)
%let\overlaylinewidth  \overlaydepth      % set somewhere else.
\let\overlaycolor      \empty
\let\overlaylinecolor  \empty

\def\overlayradius{\framedparameter\c!frameradius}

\newdimen\d_overlay_width
\newdimen\d_overlay_height
\newdimen\d_overlay_depth
\newdimen\d_overlay_offset
\newdimen\d_overlay_linewidth

\let\m_overlay_region\empty

% expandable ... in a future version the space will go (in mp one can use Overlay*)

\def\overlaywidth        {\the\d_overlay_width    \space} % We preset the variables
\def\overlayheight       {\the\d_overlay_height   \space} % to some reasonable default
\def\overlaydepth        {\the\d_overlay_depth    \space} % values.
\def\overlayoffset       {\the\d_overlay_offset   \space} % of the frame can be (are)
\def\overlaylinewidth    {\the\d_overlay_linewidth\space} % set somewhere else.
\def\overlayregion       {\m_overlay_region}

% public but kind of protected

\def\usedoverlaywidth    {\dimexpr\d_overlay_width    \relax}
\def\usedoverlayheight   {\dimexpr\d_overlay_height   \relax}
\def\usedoverlaydepth    {\dimexpr\d_overlay_depth    \relax}
\def\usedoverlayoffset   {\dimexpr\d_overlay_offset   \relax}
\def\usedoverlaylinewidth{\dimexpr\d_overlay_linewidth\relax}

%D The next register is used to initialize overlays.

\newtoks\everyoverlay

%D An example of an initialization is the following (overlays can contain text
%D and be executed under an regime where interlineskip is off).

\installcorenamespace{overlay}
\installcorenamespace{overlaybuiltin}

\appendtoks
    \oninterlineskip
\to \everyoverlay

\prependtoks
    \hsize\d_overlay_width
    \vsize\d_overlay_height
\to \everyoverlay

\unexpanded\def\defineoverlay
  {\dodoubleargument\pack_framed_define_overlay}

\def\pack_framed_define_overlay[#1][#2]%
  {\def\pack_framed_define_overlay_indeed##1{\setvalue{\??overlay##1}{\executedefinedoverlay{##1}{#2}}}%
   \processcommalist[#1]\pack_framed_define_overlay_indeed}

% \unexpanded\def\executedefinedoverlay#1#2% we can share the definitions
%   {\bgroup % redundant grouping
%    \setbox\scratchbox\hbox\bgroup
%      \ifzeropt\d_framed_target_dp
%        \the\everyoverlay#2% saves wrapping (and lua call)
%      \else
%        \lower\d_framed_target_dp
%        \hbox{\the\everyoverlay#2}%
%      \fi
%    \egroup
%    \setlayoutcomponentattribute{\v!overlay:#1}%
%    \setbox\scratchbox\hpack \layoutcomponentboxattribute
%      {\kern -.5\dimexpr\wd\scratchbox-\d_framed_target_wd\relax % was \d_overlay_width
%       \raise-.5\dimexpr\ht\scratchbox-\d_framed_target_ht\relax % not \d_overlay_height !
%       \box\scratchbox}%
%    \wd\scratchbox\d_framed_target_wd
%    \ht\scratchbox\d_framed_target_ht
%    \dp\scratchbox\d_framed_target_dp
%    \box\scratchbox
%    \egroup}

% 20191231: moved from cont-exp:

\unexpanded\def\executedefinedoverlay#1#2% we can share the definitions
  {\bgroup % redundant grouping
   \setlayoutcomponentattribute{\v!overlay:#1}%
   \setbox\scratchbox\hbox \layoutcomponentboxattribute{\the\everyoverlay#2}%
   \boxxoffset\scratchbox-.5\dimexpr\wd\scratchbox-\d_framed_target_wd\relax % was \d_overlay_width
   \boxyoffset\scratchbox-.5\dimexpr\ht\scratchbox-\d_framed_target_ht+\d_framed_target_dp\relax % not \d_overlay_height !
   \wd\scratchbox\d_framed_target_wd
   \ht\scratchbox\d_framed_target_ht
   \dp\scratchbox\d_framed_target_dp
   \box\scratchbox
   \egroup}

%D \macros
%D   {overlayfakebox}

% \unexpanded\def\overlayfakebox
%   {\hpack
%      {\setbox\scratchbox\emptyhbox
%       \wd\scratchbox\d_overlay_width
%       \ht\scratchbox\d_overlay_height
%       \box\scratchbox}}

\unexpanded\def\overlayfakebox
  {\hpack % redundant but needs testing
     {\novrule
        \s!width \d_overlay_width
        \s!height\d_overlay_height
        \s!depth \zeropoint}}

%D For testing we provide:

\def\doifelseoverlay#1% only tests external overlays
  {\ifcsname\??overlay#1\endcsname
     \expandafter\firstoftwoarguments
   \else
     \expandafter\secondoftwoarguments
   \fi}

\let\doifoverlayelse\doifelseoverlay

%D The content of the box will be (temporary) saved in a box. We also have an
%D extra box for backgrounds.

\newbox\b_framed_normal
\newbox\b_framed_extra

\newtoks\everybackgroundbox

\let\m_framed_background\empty % we might need a public name

\def\pack_framed_process_background
  {\ifcsname\??overlaybuiltin\m_framed_background\endcsname
     \expandafter\pack_framed_process_background_indeed_internal\lastnamedcs
   \orelse\ifcsname\??overlay\m_framed_background\endcsname
     \expandafter\pack_framed_process_background_indeed_external\lastnamedcs
   \fi}

\def\pack_framed_process_background_indeed_internal#1% % : in name
  {\bgroup
   \setbox\b_framed_extra\hpack\bgroup
     \ifzeropt\framedbackgroundoffset\else
       \kern-\framedbackgroundoffset
     \fi
     \hbox\bgroup#1\egroup
   \egroup
   \wd\b_framed_extra\zeropoint
   \ht\b_framed_extra\framedbackgroundheight
   \dp\b_framed_extra\framedbackgrounddepth
   \box\b_framed_extra
   \egroup}

\def\pack_framed_process_background_indeed_external
  {\pack_framed_overlay_initialize
   \pack_framed_process_background_indeed_internal}

\def\pack_framed_process_backgrounds#1,#2% #2 gobbles spaces (we could avoid one catch if we have nextbackground)
  {\edef\m_framed_background{#1}%
   \ifx\m_framed_background\s!unknown\else
     \pack_framed_process_background
     \expandafter\pack_framed_process_backgrounds
   \fi#2}

%D Beware, a backgroundbox can be empty which is another reason why we set the
%D width to zero instead of back-skipping.

\newdimen\framedbackgroundwidth
\newdimen\framedbackgroundheight
\newdimen\framedbackgrounddepth
\newdimen\framedbackgroundoffset

\def\pack_framed_background_box_content% fuzzy but needed hack, this \vss, otherwise
  {\vpack to \framedbackgroundheight{\vss\box\b_framed_normal\vss}} % vertical shift \backgroundheight

\def\pack_framed_set_region % experiment
  {\ifx\m_overlay_region\v!yes
     \edef\m_overlay_region{\reservedautoregiontag}%
   \fi}

\def\pack_framed_add_region % experiment
  {\anch_mark_tagged_box\b_framed_normal\m_overlay_region}

\def\pack_framed_add_background
  {\setbox\b_framed_normal\hpack % was vbox % see also *1*
     {%\pack_framed_forgetall % can be relaxed
      \boxmaxdepth\maxdimen
      \framedbackgroundoffset\d_framed_backgroundoffset
      \framedbackgroundwidth \wd\b_framed_normal
      \framedbackgroundheight\ht\b_framed_normal
      \framedbackgrounddepth \dp\b_framed_normal
      \d_framed_target_wd\dimexpr\framedbackgroundwidth +2\framedbackgroundoffset\relax
      \d_framed_target_ht\dimexpr\framedbackgroundheight+ \framedbackgroundoffset\relax
      \d_framed_target_dp\dimexpr\framedbackgrounddepth + \framedbackgroundoffset+\framedparameter\c!backgrounddepth\relax
      \let\pack_framed_overlay_initialize\pack_framed_overlay_initialize_indeed
      \ifx\p_framed_component\empty
        \resetlayoutcomponentattribute
      \else
        \setlayoutcomponentattribute{\v!background:\p_framed_component}%
      \fi
      \let\foregroundbox\pack_framed_background_box_content
      \hpack \layoutcomponentboxattribute to \framedbackgroundwidth\bgroup % width in case 'foreground' is used as overlay
         \the\everybackgroundbox % moved
         \expandafter\pack_framed_process_backgrounds\p_framed_background,\s!unknown,\relax % hm, messy .. look into it
         \box\b_framed_normal
         \hss
      \egroup}}

\def\pack_framed_overlay_initialize_indeed
  {\d_overlay_width     \d_framed_target_wd
   \d_overlay_height    \dimexpr\d_framed_target_ht+\d_framed_target_dp\relax
   \d_overlay_depth     \d_framed_target_dp
   \d_overlay_linewidth \d_framed_linewidth
   \d_overlay_offset    \framedbackgroundoffset\relax
   \edef\overlaycolor    {\framedparameter\c!backgroundcolor}% let ?
   \edef\overlaylinecolor{\framedparameter\c!framecolor}% only needed for layers
  %\edef\overlaycorner   {\framedparameter\c!backgroundcorner}%
  %\edef\overlayradius   {\framedparameter\c!backgroundradius}%
   \let\pack_framed_overlay_initialize\relax}

%D One can explictly insert the foreground box. For that purpose we introduce the
%D overlay \type {foreground}.
%D
%D We predefine two already familiar backgrounds:

%letvalue{\??overlaybuiltin\v!screen    }\pack_framed_background_box_gray
\letvalue{\??overlaybuiltin\v!color     }\pack_framed_background_box_color
\letvalue{\??overlaybuiltin\v!foreground}\pack_framed_background_box_content % replaces: \defineoverlay[\v!foreground][\foregroundbox]

%D We can specify overlays as a comma separated list of overlays, a sometimes
%D handy feature.
%D
%D Besides backgrounds (overlays) we also need some macros to draw outlines (ruled
%D borders). Again we have to deal with square and round corners. The first category
%D can be handled by \TEX\ itself, the latter one depends on the driver. This macro
%D also support a negative offset.

\def\pack_framed_add_outline
  {\setbox\b_framed_normal\hpack % rules on top of box
     {\d_framed_target_wd\dimexpr\wd\b_framed_normal+2\d_framed_frameoffset\relax
      \d_framed_target_ht\dimexpr\ht\b_framed_normal+ \d_framed_frameoffset\relax
      \d_framed_target_dp\dimexpr\dp\b_framed_normal+ \d_framed_frameoffset+\framedparameter\c!framedepth\relax
      \ifdim\d_framed_target_dp<\zeropoint
        \advance\d_framed_target_ht \d_framed_target_dp
        \scratchdimen-\d_framed_target_dp
        \d_framed_target_dp\zeropoint
      \else
        \scratchdimen\zeropoint
      \fi
      \edef\overlaylinecolor{\framedparameter\c!framecolor}% twice, also in background
      \setbox\b_framed_extra\hpack
        {\kern-\d_framed_frameoffset
         \raise\scratchdimen
         \hpack{\ifx\overlaylinecolor\empty\else\dousecolorparameter\overlaylinecolor\fi\pack_framed_stroked_box}}%
      \wd\b_framed_extra\wd\b_framed_normal
      \ht\b_framed_extra\ht\b_framed_normal
      \dp\b_framed_extra\dp\b_framed_normal
      \wd\b_framed_normal\zeropoint
      \box\b_framed_normal
      \box\b_framed_extra}}

\def\pack_framed_stroked_box_normal_opened
  {\setbox\scratchbox\vpack \bgroup
     \csname\??framedtop\p_framed_frame\framedparameter\c!topframe\endcsname
     \nointerlineskip % new (needed for fences)
     \hpack \bgroup
       \csname\??framedleft\p_framed_frame\framedparameter\c!leftframe\endcsname
       \novrule
         \s!width \d_framed_target_wd
         \s!height\d_framed_target_ht
         \s!depth \d_framed_target_dp
       \csname\??framedright\p_framed_frame\framedparameter\c!rightframe\endcsname
     \egroup
     \nointerlineskip % new (needed for fences)
     \csname\??framedbottom\p_framed_frame\framedparameter\c!bottomframe\endcsname
   \egroup
   \wd\scratchbox\d_framed_target_wd
   \ht\scratchbox\d_framed_target_ht
   \dp\scratchbox\d_framed_target_dp
   \box\scratchbox}

\def\pack_framed_stroked_box_normal_closed
  {\hpack\bgroup
     \scratchdimen.5\d_framed_linewidth
     \hskip\scratchdimen
     \clf_framedoutline
       \dimexpr\d_framed_target_wd-\d_framed_linewidth\relax
       \dimexpr\d_framed_target_ht-\scratchdimen\relax
       \dimexpr\d_framed_target_dp-\scratchdimen\relax
       \d_framed_linewidth
     \relax
   \egroup}

\def\pack_framed_stroked_box_normal
  {\ifx\p_framed_frame\v!closed
     \pack_framed_stroked_box_normal_closed
   \else
     \pack_framed_stroked_box_normal_opened
   \fi}

\def\pack_framed_t_rule{\hrule\s!height\d_framed_linewidth\kern-\d_framed_linewidth}
\def\pack_framed_b_rule{\kern-\d_framed_linewidth\hrule\s!height\d_framed_linewidth}
\def\pack_framed_r_rule{\kern-\d_framed_linewidth\vrule\s!width\d_framed_linewidth}
\def\pack_framed_l_rule{\vrule\s!width\d_framed_linewidth\kern-\d_framed_linewidth}

\letvalue{\??framedtop   \v!on \v!on}\pack_framed_t_rule
\letvalue{\??framedtop   \v!off\v!on}\pack_framed_t_rule
\letvalue{\??framedtop   \v!on      }\pack_framed_t_rule

\letvalue{\??framedbottom\v!on \v!on}\pack_framed_b_rule
\letvalue{\??framedbottom\v!off\v!on}\pack_framed_b_rule
\letvalue{\??framedbottom\v!on      }\pack_framed_b_rule

\letvalue{\??framedleft  \v!on \v!on}\pack_framed_l_rule
\letvalue{\??framedleft  \v!off\v!on}\pack_framed_l_rule
\letvalue{\??framedleft  \v!on      }\pack_framed_l_rule

\letvalue{\??framedright \v!on \v!on}\pack_framed_r_rule
\letvalue{\??framedright \v!off\v!on}\pack_framed_r_rule
\letvalue{\??framedright \v!on      }\pack_framed_r_rule

% no overlapping rules

\def\pack_framed_t_rules{\hpack{\kern\d_framed_linewidth\vrule\s!width\dimexpr\d_framed_target_wd-2\d_framed_linewidth\relax\s!height\d_framed_linewidth}\nointerlineskip\kern-\d_framed_linewidth}
\def\pack_framed_b_rules{\kern-\d_framed_linewidth\nointerlineskip\hpack{\kern\d_framed_linewidth\vrule\s!width\dimexpr\d_framed_target_wd-2\d_framed_linewidth\relax\s!height\d_framed_linewidth}}
\def\pack_framed_r_rules{\kern-\d_framed_linewidth\vrule\s!height\dimexpr\d_framed_target_ht-\d_framed_linewidth\relax\s!depth-\d_framed_linewidth\s!width\d_framed_linewidth}
\def\pack_framed_l_rules{\vrule\s!height\dimexpr\d_framed_target_ht-\d_framed_linewidth\relax\s!depth-\d_framed_linewidth\s!width\d_framed_linewidth\kern-\d_framed_linewidth}

\letvalue{\??framedtop   \v!small\v!small}\pack_framed_t_rules
\letvalue{\??framedtop   \v!off  \v!small}\pack_framed_t_rules
\letvalue{\??framedtop   \v!small        }\pack_framed_t_rules

\letvalue{\??framedbottom\v!small\v!small}\pack_framed_b_rules
\letvalue{\??framedbottom\v!off  \v!small}\pack_framed_b_rules
\letvalue{\??framedbottom\v!small        }\pack_framed_b_rules

\letvalue{\??framedleft  \v!small\v!small}\pack_framed_l_rules
\letvalue{\??framedleft  \v!off  \v!small}\pack_framed_l_rules
\letvalue{\??framedleft  \v!small        }\pack_framed_l_rules

\letvalue{\??framedright \v!small\v!small}\pack_framed_r_rules
\letvalue{\??framedright \v!off  \v!small}\pack_framed_r_rules
\letvalue{\??framedright \v!small        }\pack_framed_r_rules

% \framed
%   [width=4cm,height=3cm,rulethickness=3mm,
%    frame=off,rightframe=on,leftframe=on,topframe=on,bottomframe=on]
%   {}
% \framed
%   [width=4cm,height=3cm,rulethickness=3mm,
%    frame=off,rightframe=small,leftframe=small,topframe=small,bottomframe=small]
%   {}
% \framed
%   [width=4cm,height=3cm,rulethickness=3mm,
%    frame=off,rightframe=small,leftframe=small,topframe=small,bottomframe=on]
%   {}

%D The next few macros are probably the most misused ones in \CONTEXT. They deal
%D with putting rules around boxes, provide backgrounds, offer alignment features,
%D and some more. We start with defining some booleans. These give an impression of
%D what we are going to take into account.

% todo   : \c_framed_hasoffset
% faster : \let\c_framed_hasoffset\falseconditional

\newconditional\c_framed_has_offset
\newconditional\c_framed_has_width
\newconditional\c_framed_has_height
\newconditional\c_framed_has_format
\newconditional\c_framed_is_overlaid
\newconditional\c_framed_has_frame
\newconditional\c_framed_has_extra_offset
\newconditional\c_framed_text_location_none

\newconstant   \c_framed_has_strut  % 0=relaxes 1=pseudostruts 2=realstruts

%D \macros
%D   {framed, setupframed}
%D
%D Ruled boxes are typeset using \type{\framed}. This command is quite versatile
%D and, although some users will probably seldom use it, one cannot overlook its
%D features.
%D
%D  \showsetup{setupframed}
%D  \showsetup{framed}
%D
%D This general macro is a special version of an even more general case, that can
%D easily be linked into other macros that need some kind of framing. The local
%D version is called with an extra parameter: the variable identifier. The reason
%D for passing this identifier between brackets lays in the mere fact that this way
%D we can use the optional argument grabbers.

\def\defaultframeoffset{.25\exheight}

\installcorenamespace{regularframedlevel}

\unexpanded\def\installregularframed#1%
  {\defineframed[#1]}

\unexpanded\def\presetlocalframed[#1]%
  {\defineframed[#1]}

% \presetlocalframed[\??framed]

\newcount\c_pack_framed_nesting

\unexpanded\def\pack_framed_process_framed[#1]%
  {\bgroup
   \iffirstargument % faster
     \setupcurrentframed[#1]% here !
   \fi
   \pack_framed_process_indeed}

\unexpanded\def\framed
  {\bgroup
   \advance\c_pack_framed_nesting\plusone
   \expandafter\let\csname\??framed>\the\c_pack_framed_nesting:\s!parent\endcsname\??framed
   \edef\currentframed{>\the\c_pack_framed_nesting}%
   \pack_framed_initialize
   \dosingleempty\pack_framed_process_framed}

\unexpanded\def\startframed
  {\dosingleempty\pack_framed_start_framed}

\def\pack_framed_start_framed[#1]%
  {\bgroup
   \doifelseassignment{#1}\pack_framed_start_framed_yes\pack_framed_start_framed_nop{#1}}

\def\pack_framed_start_framed_yes#1%
  {\advance\c_pack_framed_nesting\plusone
   \expandafter\let\csname\??framed>\the\c_pack_framed_nesting:\s!parent\endcsname\??framed
   \iffirstargument\secondargumenttrue\fi % dirty trick
   \edef\currentframed{>\the\c_pack_framed_nesting}%
   \pack_framed_initialize
   \bgroup
   \iffirstargument
     \secondargumenttrue % dirty trick
     \setupcurrentframed[#1]% here !
   \fi
   \pack_framed_process_indeed
   \bgroup
   \ignorespaces}

\def\pack_framed_start_framed_nop#1%
  {\edef\currentframed{#1}%
   \dosingleempty\pack_framed_start_framed_nop_indeed}

\def\pack_framed_start_framed_nop_indeed[#1]%
  {\pack_framed_initialize
   \bgroup
   \iffirstargument
     \setupcurrentframed[#1]% here !
   \fi
   \pack_framed_process_indeed
   \bgroup
   \ignorespaces}

% till here

\unexpanded\def\stopframed
  {\removeunwantedspaces
   \egroup}

\unexpanded\def\normalframedwithsettings[#1]%
  {\bgroup
   \advance\c_pack_framed_nesting\plusone
   \expandafter\let\csname\??framed>\the\c_pack_framed_nesting:\s!parent\endcsname\??framed
   \bgroup
   \edef\currentframed{>\the\c_pack_framed_nesting}%
   \pack_framed_initialize
   \setupcurrentframed[#1]%
   \pack_framed_process_indeed}

%D \startbuffer
%D \setupframed [framecolor=yellow]          \framed{A}
%D \defineframed[myframed] [framecolor=blue] \myframed{B}
%D \setupframed [myframed] [framecolor=red]  \myframed{C}
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D \startbuffer
%D \presetlocalframed[myframed]
%D \localframed[myframed][framecolor=green]{oeps}
%D \stopbuffer
%D
%D \typebuffer \getbuffer

%D \macros
%D   {ifinframed}
%D
%D The normal case first presets all parameters and next starts looking for the user
%D supplied ones. The first step is omitted in the local case, because these are
%D preset at declaration time and keep their values unless explictly changed. By
%D presetting the variables everytime the normal command is called, we can use this
%D command nested, without the unwanted side effect of inheritance. The boolean is
%D used to speed up the color stack.

\newif\ifinframed

%D The next one is faster on multiple backgrounds per page. No
%D dimensions can be set, only frames and backgrounds.

\unexpanded\def\fastlocalframed[#1]#2[#3]#4% 3-4
  {\bgroup
   \edef\currentframed{#1}%
   \pack_framed_initialize
   \setbox\b_framed_normal\hbox{#4}%
   \iftrialtypesetting \else
     \edef\m_overlay_region{\framedparameter\c!region}%
     \ifx\m_overlay_region\empty\else
       \pack_framed_set_region
     \fi
   \fi
   \setupcurrentframed[#3]%
   \edef\p_framed_rulethickness{\framedparameter\c!rulethickness}% also used in backgrounds
   \d_framed_frameoffset\framedparameter\c!frameoffset\relax     % also used in backgrounds
   \edef\p_framed_frame{\framedparameter\c!frame}%
   \edef\p_framed_background{\framedparameter\c!background}%
   % not here, in calling macro: setups
   \pack_framed_remove_depth
   \ifx\p_framed_frame\v!overlay \orelse \ifx\p_framed_frame\v!none \else
     \ifx\p_framed_rulethickness\empty\else
        \d_framed_linewidth\p_framed_rulethickness\relax
     \fi
     \pack_framed_add_outline % real or invisible frame
   \fi
   \ifx\p_framed_background\empty \else
     \edef\p_framed_backgroundoffset{\framedparameter\c!backgroundoffset}%
     \d_framed_backgroundoffset
       \ifx\p_framed_backgroundoffset\v!frame
         \d_framed_frameoffset
       \else
         \p_framed_backgroundoffset
       \fi
     \edef\p_framed_component{\framedparameter\c!component}%
     \pack_framed_add_background
   \fi
   \pack_framed_restore_depth
   \iftrialtypesetting\orelse\ifx\m_overlay_region\empty\else
     \pack_framed_add_region
   \fi
   \box\b_framed_normal
   \egroup}

%D The next macro uses a box and takes its natural width and height so these
%D can better be correct.

\unexpanded\def\pack_framed_process_box_indeed#1#2% component box (assumes parameters set and grouped usage)
  {\setbox\b_framed_normal\box#2% could actually be \let\b_framed_normal#2
   \edef\m_overlay_region{\framedparameter\c!region}%
   \ifx\m_overlay_region\empty\else
     \pack_framed_set_region
   \fi
   \edef\p_framed_rulethickness{\framedparameter\c!rulethickness}% also used in backgrounds
   \d_framed_frameoffset\framedparameter\c!frameoffset\relax     % also used in backgrounds
   \edef\p_framed_frame{\framedparameter\c!frame}%
   \edef\p_framed_background{\framedparameter\c!background}%
   \ifx\p_framed_frame\v!overlay \orelse \ifx\p_framed_frame\v!none \else
     \ifx\p_framed_rulethickness\empty \else
        \d_framed_linewidth\p_framed_rulethickness\relax
     \fi
     \pack_framed_add_outline % real or invisible frame
   \fi
   \ifx\p_framed_background\empty \else
     \edef\p_framed_backgroundoffset{\framedparameter\c!backgroundoffset}%
     \d_framed_backgroundoffset
       \ifx\p_framed_backgroundoffset\v!frame
         \d_framed_frameoffset
       \else
         \p_framed_backgroundoffset
       \fi
     \edef\p_framed_component{#1}%
     \pack_framed_add_background
   \fi
   \ifx\m_overlay_region\empty\else
     \pack_framed_add_region
   \fi
   \box\b_framed_normal
   \egroup}

\unexpanded\def\localbackgroundframed#1% namespace component box
  {\bgroup
   \edef\currentframed{#1}%
   \pack_framed_initialize
   \pack_framed_process_box_indeed} % group ends here

\let\postprocessframebox\relax

%D A nice example by Aditya:
%D
%D \starttyping
%D \setupframed
%D   [loffset=\framedparameter{hoffset},
%D    roffset=\framedparameter{hoffset},
%D    hoffset=\zeropoint]
%D
%D \defineframed[test][hoffset=1cm]
%D \stoptyping

\newdimen\d_framed_width
\newdimen\d_framed_height
\newdimen\d_framed_frameoffset
\newdimen\d_framed_backgroundoffset
\newdimen\d_framed_local_offset

% todo: protect local \framednames

\unexpanded\def\localframed
  {\bgroup
   \dodoubleempty\pack_framed_local}

\unexpanded\def\pack_framed_local[#1][#2]%
  {\bgroup
   \edef\currentframed{#1}%
   \pack_framed_initialize
   \ifsecondargument % faster
     \setupcurrentframed[#2]% here !
   \fi
   \pack_framed_process_indeed}

\unexpanded\def\directlocalframed[#1]% no optional
  {\bgroup
   \bgroup
   \edef\currentframed{#1}%
   \pack_framed_initialize
   \pack_framed_process_indeed}

\unexpanded\def\localframedwithsettings[#1][#2]% no checking (so no spaces between)
  {\bgroup
   \bgroup
   \edef\currentframed{#1}%
   \pack_framed_initialize
   \setupcurrentframed[#2]% here !
   \pack_framed_process_indeed}

% done

\def\c!fr!analyze{fr:analyze} % private option

\let\delayedbegstrut\relax
\let\delayedendstrut\relax
\let\delayedstrut   \relax

\let\localoffset\empty
\let\localwidth \empty
\let\localheight\empty
\let\localformat\empty
\let\localstrut \empty

\unexpanded\def\pack_framed_process_indeed
  {\d_framed_frameoffset\framedparameter\c!frameoffset
   \edef\p_framed_backgroundoffset{\framedparameter\c!backgroundoffset}%
   \d_framed_backgroundoffset
     \ifx\p_framed_backgroundoffset\v!frame
       \d_framed_frameoffset
     \else
       \p_framed_backgroundoffset
     \fi
   % new, experimental dirty hook
   \framedparameter\c!extras
   % to get the right spacing
   \edef\p_framed_foregroundstyle{\framedparameter\c!foregroundstyle}%
   \ifx\p_framed_foregroundstyle\empty\else\dousestyleparameter\p_framed_foregroundstyle\fi
   % beware, both the frame and background offset can be overruled
   %
   \edef\p_framed_setups{\framedparameter\c!setups}%
   % the next macros are visible
   \edef\localoffset{\framedparameter\c!offset}%
   \edef\localwidth {\framedparameter\c!width}%
   \edef\localheight{\framedparameter\c!height}%
   \edef\localformat{\framedparameter\c!align}%
   \edef\localstrut {\framedparameter\c!strut}%
   % these are not
   \edef\p_framed_autostrut  {\framedparameter\c!autostrut}%
   \edef\p_framed_frame      {\framedparameter\c!frame}%
   \edef\p_framed_location   {\framedparameter\c!location}%
   \edef\p_framed_orientation{\framedparameter\c!orientation}%
   \edef\p_framed_anchoring  {\framedparameter\c!anchoring}%
   %
   \edef\p_framed_autowidth  {\framedparameter\c!autowidth}%
   \edef\p_framed_franalyze  {\framedparameter\c!fr!analyze}% experimental option
   %
   \ifx\p_framed_frame\v!overlay   % no frame, no offset, no framewidth
     \setfalse\c_framed_has_frame
     \let\localoffset\v!overlay
   \orelse\ifx\p_framed_frame\v!none % no frame, no framewidth
     \setfalse\c_framed_has_frame
   \else
     \settrue\c_framed_has_frame
   \fi
   \ifconditional\c_framed_has_frame
     \edef\p_framed_rulethickness{\framedparameter\c!rulethickness}%
     \ifx\p_framed_rulethickness\empty\else
        \d_framed_linewidth\p_framed_rulethickness\relax
     \fi
   \else
     \d_framed_linewidth\zeropoint
   \fi
   % 2013/03/12: a change of order (sizes before align
   \ifx\localwidth\v!local
     \setlocalhsize
   \fi
   %
   \forgetall % should happen after \localwidth but before align
   %
   \ifx\localformat\empty
     \setfalse\c_framed_has_format
   \else
     \settrue\c_framed_has_format
     \dosetraggedcommand\localformat % not that fast
   \fi
   %
   \ifcsname\??framedoffsetalternative\localoffset\endcsname
     \lastnamedcs
   \else
     \framed_offset_alternative_unknown
   \fi
   \ifcsname\??framedwidthalternative\localwidth\endcsname
     \lastnamedcs
   \else
     \framed_width_alternative_unknown
   \fi
   \ifcsname\??framedheightalternative\localheight\endcsname
     \lastnamedcs
   \else
     \framed_height_alternative_unknown
   \fi
   % the next check could move to heightalternative
   \ifconditional\c_framed_has_height
     % obey user set height, also downward compatible
   \else
     \edef\p_framed_lines{\framedparameter\c!lines}%
     \ifx\p_framed_lines\empty
     \orelse\ifcase\p_framed_lines
     \else
       \d_framed_height\p_framed_lines\lineheight
       \edef\localheight{\the\d_framed_height}%
       \settrue\c_framed_has_height
     \fi
   \fi
   % this is now an option: width=local
   %
   % \ifdim\d_framed_width=\hsize
   %   \parindent\zeropoint
   %   \setlocalhsize
   %   \d_framed_width\localhsize
   % \fi
   % i.e. disable (colsetbackgroundproblemintechniek)
   \advance\d_framed_width  -2\d_framed_local_offset
   \advance\d_framed_height -2\d_framed_local_offset
   \ifcsname\??framedstrutalternative\localstrut\endcsname
     \lastnamedcs
   \else
     \framed_offset_alternative_unknown
   \fi
   % the next check could move to strutalternative
   \ifcase\c_framed_has_strut % none (not even noindent)
     \let\localbegstrut\relax
     \let\localendstrut\relax
     \let\localstrut   \relax
   \or % no / overlay
     \let\localbegstrut\pseudobegstrut
     \let\localendstrut\pseudoendstrut
     \let\localstrut   \pseudostrut
   \else
     \let\localbegstrut\begstrut
     \let\localendstrut\endstrut
     \let\localstrut   \strut
   \fi
   \ifx\p_framed_autostrut\v!yes
     \let\delayedbegstrut\relax
     \let\delayedendstrut\relax
     \let\delayedstrut   \relax
   \else
     \let\delayedbegstrut\localbegstrut
     \let\delayedendstrut\localendstrut
     \let\delayedstrut   \localstrut
     \let\localbegstrut  \relax
     \let\localendstrut  \relax
     \let\localstrut     \relax
   \fi
   \ifconditional\c_framed_has_height
     \let\\\pack_framed_vboxed_newline
     \ifconditional\c_framed_has_width
       \let\hairline\pack_framed_vboxed_hairline
       \ifconditional\c_framed_has_format
         \let\next\pack_framed_format_format_yes
       \else
         \let\next\pack_framed_format_format_nop
       \fi
     \else
       \let\hairline\pack_framed_hboxed_hairline
       \ifconditional\c_framed_has_format
         \let\next\pack_framed_format_format_height
       \else
         \let\next\pack_framed_format_format_vsize
       \fi
     \fi
   \orelse\ifconditional\c_framed_has_width
     \ifconditional\c_framed_has_format
       \let\hairline\pack_framed_vboxed_hairline
       \let\\\pack_framed_vboxed_newline
       \let\next\pack_framed_format_format_width
     \else
       \let\hairline\pack_framed_hboxed_hairline
       \let\\\pack_framed_hboxed_newline
       \let\next\pack_framed_format_format_hsize
     \fi
   \else
     \let\hairline\pack_framed_hboxed_hairline
     \let\\\pack_framed_hboxed_newline
     \let\next\pack_framed_format_format_no_size
   \fi
   \pack_framed_check_extra_offsets
   \edef\p_framed_background{\framedparameter\c!background}%
%    \ifx\p_framed_background\empty
%      \let\pack_framed_forgetall\forgetall
%    \else
%      \let\pack_framed_forgetall\relax
%      \forgetall
%    \fi
   \edef\framedwidth {\the\ifdim\d_framed_width >\zeropoint         \d_framed_width \else\zeropoint\fi}% public
   \edef\framedheight{\the\ifdim\d_framed_height>\zeropoint         \d_framed_height\else\zeropoint\fi}% public
   \edef\framedoffset{\the\dimexpr\ifconditional\c_framed_has_offset\localoffset    \else\zeropoint\fi}% public
   \ifx\p_framed_orientation\empty
     \let\pack_framed_stop_orientation\relax
   \else
     \pack_framed_start_orientation
   \fi
   \afterassignment\pack_framed_restart
   \setbox\b_framed_normal\next}

% alternatives for width, height, strut and offset

\installcorenamespace{framedwidthalternative}
\installcorenamespace{framedheightalternative}
\installcorenamespace{framedstrutalternative}
\installcorenamespace{framedoffsetalternative}

% widths

\setvalue{\??framedwidthalternative\empty}%
  {\ifconditional\c_framed_has_format
     \settrue\c_framed_has_width
     \d_framed_width\hsize
   \else
     \setfalse\c_framed_has_width
     \d_framed_width\zeropoint
   \fi}

\setvalue{\??framedwidthalternative\v!fit}%
  {\ifconditional\c_framed_has_format
     \settrue\c_framed_has_width
     \d_framed_width\hsize
   \else
     \setfalse\c_framed_has_width
     \d_framed_width\zeropoint
   \fi}

\setvalue{\??framedwidthalternative\v!fixed}% equals \v!fit but no shapebox
  {\ifconditional\c_framed_has_format
     \settrue\c_framed_has_width
     \d_framed_width\hsize
   \else
     \setfalse\c_framed_has_width
     \d_framed_width\zeropoint
   \fi}

\setvalue{\??framedwidthalternative\v!broad}%
  {\settrue\c_framed_has_width
   \d_framed_width\hsize}

\setvalue{\??framedwidthalternative\v!max}% idem broad
  {\settrue\c_framed_has_width
   \d_framed_width\hsize}

\setvalue{\??framedwidthalternative\v!local}%
  {\settrue\c_framed_has_width
   %\setlocalhsize
   \d_framed_width\localhsize}

\setvalue{\??framedwidthalternative\s!unknown}%
  {\settrue\c_framed_has_width
   \d_framed_width\localwidth}

\def\framed_width_alternative_unknown
  {\settrue\c_framed_has_width
   \d_framed_width\localwidth}

% heights

\setvalue{\??framedheightalternative\empty}%
  {\setfalse\c_framed_has_height
   \d_framed_height\zeropoint}

\setvalue{\??framedheightalternative\v!fit}%
  {\setfalse\c_framed_has_height
   \d_framed_height\zeropoint}

\setvalue{\??framedheightalternative\v!broad}%
  {\setfalse\c_framed_has_height
   \d_framed_height\zeropoint}

\setvalue{\??framedheightalternative\v!max}%
  {\settrue\c_framed_has_height
   \d_framed_height\vsize}

\setvalue{\??framedheightalternative\s!unknown}%
  {\settrue\c_framed_has_height
   \d_framed_height\localheight}

\def\framed_height_alternative_unknown
  {\settrue\c_framed_has_height
   \d_framed_height\localheight}

% struts (use let instead?)

\setvalue{\??framedstrutalternative\v!no}%
  {\c_framed_has_strut\plusone}

\setvalue{\??framedstrutalternative\v!global}%
  {\setstrut}

\setvalue{\??framedstrutalternative\v!local}%
  {\setfontstrut}

\setvalue{\??framedstrutalternative\v!yes}%
  {\setstrut}

\setvalue{\??framedstrutalternative\s!unknown}%
  {\setstrut}

\def\framed_strut_alternative_unknown
  {\setstrut}

\setvalue{\??framedstrutalternative\v!none}% not even pseudo struts
  {\c_framed_has_strut\zerocount}

% offsets

\setvalue{\??framedoffsetalternative\v!none}%
  {\setfalse\c_framed_has_offset
   \c_framed_has_strut\plusone
   \setfalse\c_framed_is_overlaid
   \d_framed_local_offset\d_framed_linewidth}

\setvalue{\??framedoffsetalternative\v!overlay}%
  {% \ifx\p_framed_frame\v!no \setfalse\c_framed_has_frame \fi % test first
   \setfalse\c_framed_has_offset
   \c_framed_has_strut\plusone
   \settrue\c_framed_is_overlaid
   \d_framed_local_offset\zeropoint}

% \setvalue{\??framedoffsetalternative\v!strut}%
%   {\setfalse\c_framed_has_offset
%    \c_framed_has_strut\plustwo
%    \settrue\c_framed_is_overlaid
%    \d_framed_local_offset\zeropoint}

\setvalue{\??framedoffsetalternative\v!default}% new per 2-6-2000
  {\settrue \c_framed_has_offset
   \c_framed_has_strut\plustwo
   \setfalse\c_framed_is_overlaid
   \let\localoffset\defaultframeoffset
   \letframedparameter\c!offset\defaultframeoffset % brrr
   \d_framed_local_offset\dimexpr\localoffset+\d_framed_linewidth\relax}

\def\framed_offset_alternative_unknown
  {\settrue \c_framed_has_offset
   \c_framed_has_strut\plustwo
   \setfalse\c_framed_is_overlaid
   \let\defaultframeoffset\localoffset
   \d_framed_local_offset\dimexpr\localoffset+\d_framed_linewidth\relax}

\letvalue{\??framedoffsetalternative\s!unknown}\framed_offset_alternative_unknown

% so far for alternatives

\let\pack_framed_stop_orientation\relax

\def\pack_framed_restart
  {\aftergroup\pack_framed_finish}

\def\pack_framed_do_top
  {\raggedtopcommand
   \framedparameter\c!top
   \edef\p_blank{\framedparameter\c!blank}%
   \ifx\p_blank\v!yes\else % auto or no
     \doinhibitblank
   \fi}

\def\pack_framed_do_bottom
  {\framedparameter\c!bottom
   \raggedbottomcommand}

%D Careful analysis of this macro will learn us that not all branches in the last
%D conditionals can be encountered, that is, some assignments to \type{\next} will
%D never occur. Nevertheless we implement the whole scheme, if not for future
%D extensions.

%D \macros
%D   {doassigncheckedframeoffset}
%D
%D Offset helper (see menus):

\def\doassigncheckedframeoffset#1#2% could be a fast \csname .. \endcsname
  {\edef\checkedframeoffset{#2}%
   #1%
     \ifx\checkedframeoffset\empty    \zeropoint\orelse
     \ifx\checkedframeoffset\v!overlay\zeropoint\orelse
     \ifx\checkedframeoffset\v!none   \zeropoint\orelse
     \ifx\checkedframeoffset\v!frame  \zeropoint\orelse
     \ifx\checkedframeoffset\v!default\zeropoint\else
       #2%
     \fi
   \relax}

%D \macros
%D   {ifreshapeframebox}
%D
%D The last few lines tell what to do after the content of the box is collected and
%D passed to the next macro. In the case of a fixed width and centered alignment,
%D the content is evaluated and used to determine the most natural width. The rest
%D of the code deals with backgrounds and frames.

\newif\ifreshapeframebox \reshapeframeboxtrue

%D Beware: setting \type {top} and \type {bottom} to nothing, may
%D result in a frame that is larger that the given height! try:
%D
%D \starttyping
%D \framed
%D   [height=3cm,top=,bottom=,offset=overlay]
%D   {\strut test \shapefill \strut test}
%D \stoptyping
%D
%D This is intended behaviour and not a bug! One can always set
%D
%D \starttyping
%D ...,bottom=\kern0pt,...
%D \stoptyping

% experiment ... \p_framed_franalyze -> we could support 'first' as location key
% option but then we will always do an analysis and reimplement the location
% options (btw, beware of location settings of derived functionality that bleed
% into this

\def\pack_framed_finish_a
  {\ifreshapeframebox
     \pack_framed_reshape_process
   \orelse\ifx\p_framed_franalyze\v!yes
     \pack_framed_reshape_analyze
   \else
     \pack_framed_reshape_reset
   \fi
   \setfalse\c_framed_has_width}

\def\pack_framed_finish_b
  {\ifx\p_framed_franalyze\v!yes
     \pack_framed_reshape_analyze
   \else
     \pack_framed_reshape_reset
   \fi
   \setfalse\c_framed_has_width}

\def\pack_framed_finish_c
  {\ifx\p_framed_franalyze\v!yes
     \pack_framed_reshape_analyze
   \else
     \pack_framed_reshape_reset
   \fi}

\def\pack_framed_profile_box
  {\profilegivenbox\p_profile\b_framed_normal
   \setbox\b_framed_normal\vpack{\unvbox\b_framed_normal}}

\unexpanded\def\pack_framed_finish
  {%\pack_framed_stop_orientation % hm, wrong place ! should rotate the result (after reshape) .. moved down
   \pack_framed_locator_before\p_framed_location
   \ifconditional\c_framed_has_format
    %\ifconditional\c_framed_has_height \else
    %  \edef\p_profile{\framedparameter\c!profile}%
    %  \ifx\p_profile\empty\else
    %    \pack_framed_profile_box
    %  \fi
    %\fi
     \ifx\p_framed_autowidth\v!force
       \pack_framed_finish_a
     \orelse\ifx\localwidth\v!fit
       \ifx\p_framed_autowidth\v!yes
         \pack_framed_finish_a
       \else
         \pack_framed_finish_b
       \fi
     \orelse\ifx\localwidth\v!fixed
       \pack_framed_finish_b
     \else
       \pack_framed_finish_c
     \fi
     \ifconditional\c_framed_has_height \else
       \edef\p_profile{\framedparameter\c!profile}%
       \ifx\p_profile\empty\else
         \pack_framed_profile_box
       \fi
     \fi
     \ifconditional\page_postprocessors_needed_box
       % quite late
       \page_postprocessors_linenumbers_box\b_framed_normal
     \fi
   \else
     \pack_framed_finish_c
   \fi
   \ifconditional\c_framed_has_width
     \wd\b_framed_normal\d_framed_width
   \fi
   \ifconditional\c_framed_has_height
     \ht\b_framed_normal\d_framed_height
   \else
     \edef\p_framed_minheight{\framedparameter\c!minheight}%
     \ifx\p_framed_minheight\empty \else
       \ifdim\ht\b_framed_normal<\p_framed_minheight
         \ht\b_framed_normal\p_framed_minheight
       \fi
     \fi
   \fi
   \edef\p_framed_empty{\framedparameter\c!empty}%
   \ifx\p_framed_empty\v!yes
     \pack_framed_fake_box
   \fi
   \ifx\p_framed_anchoring\empty\else
     \pack_framed_handle_anchoring
   \fi
   \pack_framed_stop_orientation % moved here at 2014-05-25
   \iftrialtypesetting \else
     \edef\m_overlay_region{\framedparameter\c!region}%
     \ifx\m_overlay_region\empty\else
       \pack_framed_set_region
     \fi
   \fi
   \d_framed_applied_offset
     \ifconditional\c_framed_is_overlaid
       \zeropoint
     \else
       \d_framed_linewidth
     \fi
   \ifconditional\c_framed_has_offset
     \advance\d_framed_applied_offset\localoffset\relax
   \fi
   \ifconditional\c_framed_has_extra_offset
     \pack_framed_apply_extra_offsets % includes \d_framed_applied_offset
   \else
     \ifzeropt\d_framed_applied_offset
     \else
       \pack_framed_widen_box
     \fi
   \fi
   %
   \ifx\postprocessframebox\relax \else
     \let\next\postprocessframebox
     \let\postprocessframebox\relax % prevent nesting
     \next\b_framed_normal
   \fi
   \iftrialtypesetting
     % new
   \else
     \ifconditional\c_framed_has_frame % real or invisible frame
       \pack_framed_add_outline
     \fi
     \ifx\p_framed_background\empty \else
       \edef\p_framed_component{\framedparameter\c!component}%
       \pack_framed_add_background
     \fi
   \fi
   \pack_framed_locator_after\p_framed_location
   \iftrialtypesetting \else
     \ifx\m_overlay_region\empty\else
       \pack_framed_add_region
     \fi
   \fi
   \box\b_framed_normal
   \global\frameddimensionstate % global so to be used directly afterwards !
     \ifconditional\c_framed_has_width
       \ifconditional\c_framed_has_height \plusthree \else \plusone   \fi
     \else
       \ifconditional\c_framed_has_height \plustwo   \else \zerocount \fi
     \fi
   \egroup
   \egroup}

%D Anchoring is experimental and was prototyped around the ctx meeting in 2018 but
%D never mede it into the core yet. It operates independent of the orientation
%D mechanism already present. It's a rather efficient feature. Best is to use
%D predefined orientations, like:
%D
%D \starttyping
%D \defineorientation[leftflushleft] [orientation=left,horizontal=flushleft]
%D
%D \framed [anchoring={leftflushleft}] {anchoring}
%D \stoptyping
%D
%D But this also works:
%D
%D \starttyping
%D \framed [anchoring={flushleft,top,up}] {anchoring}
%D \stoptyping
%D
%D When an anchoring is given (use \type {normal} for the default) you can also
%D use the \type {xanchor} and \type {yanchor} offsets.

% because we mess with the width later on, we need double wrapping:

\def\pack_framed_handle_anchoring
  {\scratchcounter\autoorientation\p_framed_anchoring\relax
   \edef\p_xanchor{\framedparameter\c!xanchor}%
   \edef\p_yanchor{\framedparameter\c!yanchor}%
   \setbox\b_framed_normal\hpack{\hpack % here
      % using the keyword approachs works ok, don't mess with orientation
      % directly here using \boxorientation ... it doesn't work that well
      \s!orientation\scratchcounter
      \ifx\p_xanchor\empty\else \s!xoffset \p_xanchor\fi
      \ifx\p_yanchor\empty\else \s!yoffset \p_yanchor\fi
     {\box\b_framed_normal}}}

\installcorenamespace{framedlocatorbefore}
\installcorenamespace{framedlocatorafter}

\newconstant\frameddimensionstate % global state: 0=unknown 1=width 2=height 3=both

\def\pack_framed_fake_box
  {\setbox\scratchbox\emptyhbox
   \wd\scratchbox\wd\b_framed_normal
   \ht\scratchbox\ht\b_framed_normal
   \dp\scratchbox\dp\b_framed_normal
   \setbox\b_framed_normal\box\scratchbox}

\def\installframedlocator#1#2#3%
  {\setvalue{\??framedlocatorbefore#1}{#2}%
   \setvalue{\??framedlocatorafter #1}{#3}}

\def\pack_framed_locator_before#1{\begincsname\??framedlocatorbefore#1\endcsname}
\def\pack_framed_locator_after #1{\begincsname\??framedlocatorafter #1\endcsname}

\newdimen\d_framed_locator_ht
\newdimen\d_framed_locator_dp
\newdimen\d_framed_locator_lo
\newdimen\d_framed_locator_ro

\def\pack_framed_locator_set#1%
  {\d_framed_locator_ht\dimexpr
     #1+\d_framed_linewidth
     \ifconditional\c_framed_has_offset
       +\framedparameter\c!offset
     \fi
     +\framedparameter\c!toffset
   \relax
   \d_framed_locator_dp\dimexpr\ht\b_framed_normal-\d_framed_locator_ht\relax}

\def\pack_framed_locator_set_lo
  {\global\d_framed_locator_lo\dimexpr
     \d_framed_linewidth
     \ifconditional\c_framed_has_offset
       +\framedparameter\c!offset
     \fi
     +\framedparameter\c!loffset
   \relax}

\def\pack_framed_locator_set_ro
  {\global\d_framed_locator_ro\dimexpr
     \d_framed_linewidth
     \ifconditional\c_framed_has_offset
       +\framedparameter\c!offset
     \fi
     +\framedparameter\c!roffset
   \relax}

% \ruledhbox
%   {A
%    \framed[width=2cm,align=middle,location=hanging]{location\\equals\\hanging}
%    \framed[width=2cm,align=middle,location=depth]  {location\\equals\\depth}
%    \framed[width=2cm,align=middle,location=height] {location\\equals\\height}
%    B}
% \vskip2cm
% \ruledhbox
%   {A
%    \framed[width=2cm,align=middle,location=low]    {location\\equals\\low}
%    \framed[width=2cm,align=middle,location=line]   {location\\equals\\line}
%    \framed[width=2cm,align=middle,location=high]   {location\\equals\\high}
%    B}
% \vskip2cm
% \ruledhbox
%  {A
%   \framed[width=2cm,align=middle,location=top]    {location\\equals\\top}
%   \framed[width=2cm,align=middle,location=bottom] {location\\equals\\bottom}
%   \framed[width=2cm,align=middle,location=lohi]   {location\\equals\\lohi}
%   \framed[width=2cm,align=middle,location=middle] {location\\equals\\middle}
%   B}

% \installframedlocator \v!hanging % best with strut=no
%   {}
%   {\dp\b_framed_normal\ht\b_framed_normal
%    \ht\b_framed_normal\zeropoint}
%
% \installframedlocator \v!depth
%   {}
%   {\ht\b_framed_normal\dimexpr\ht\b_framed_normal-\strutdp\relax
%    \dp\b_framed_normal\strutdp
%    \box\b_framed_normal}
%
% \installframedlocator \v!height
%   {}
%   {\dp\b_framed_normal\dimexpr\ht\b_framed_normal-\strutht\relax
%    \ht\b_framed_normal\strutht
%    \box\b_framed_normal}

\installframedlocator \v!hanging % best with strut=no *1* / see mail to list by SB
  {}
  {\scratchdimen\ht\b_framed_normal
   \setbox\b_framed_normal\hpack{\lower\scratchdimen\box\b_framed_normal}%
   \dp\b_framed_normal\scratchdimen
   \ht\b_framed_normal\zeropoint
   \box\b_framed_normal}

\installframedlocator \v!depth % *1*
  {}
  {\setbox\b_framed_normal\hpack{\lower\strutdp\box\b_framed_normal}%
   \ht\b_framed_normal\dimexpr\ht\b_framed_normal-\strutdp\relax
   \dp\b_framed_normal\strutdp
   \box\b_framed_normal}

\installframedlocator \v!height % *1*
  {}
  {\scratchdimen\dimexpr \ht\b_framed_normal - \strutht \relax
   \setbox\b_framed_normal\hpack{\lower\scratchdimen\box\b_framed_normal}%
   \dp\b_framed_normal\dimexpr\ht\b_framed_normal-\strutht\relax
   \ht\b_framed_normal\strutht
   \box\b_framed_normal}

\installframedlocator \v!high
  {}
  {\pack_framed_locator_set\strutht
   \setbox\b_framed_normal\hpack{\lower\d_framed_locator_dp\box\b_framed_normal}%
   \ht\b_framed_normal\strutht
   \dp\b_framed_normal\strutdp
   \hpack{\box\b_framed_normal}} %  why do we pack .. dange of loosing?

\installframedlocator \v!line
  {}
  {\setbox\b_framed_normal\hpack{\lower.5\ht\b_framed_normal\box\b_framed_normal}%
   \ht\b_framed_normal.5\lineheight
   \dp\b_framed_normal.5\lineheight
   \hpack{\box\b_framed_normal}} %  why do we pack .. dange of loosing?

\installframedlocator \v!low
  {}
  {\pack_framed_locator_set\strutdp
   \setbox\b_framed_normal\hpack{\lower\d_framed_locator_ht\box\b_framed_normal}%
   \ht\b_framed_normal\strutht
   \dp\b_framed_normal\strutdp
   \box\b_framed_normal}

\installframedlocator \v!top
  {}
  {\pack_framed_locator_set\strutht
   \setbox\b_framed_normal\hpack{\lower\d_framed_locator_dp\box\b_framed_normal}%
   \ht\b_framed_normal\d_framed_locator_ht
   \dp\b_framed_normal\d_framed_locator_dp
   \hpack{\box\b_framed_normal}} %  why do we pack .. dange of loosing?

\installframedlocator \v!middle
  {}
  {\scratchdimen.5\ht\b_framed_normal
   \setbox\b_framed_normal\hpack{\lower\scratchdimen\box\b_framed_normal}%
   \ht\b_framed_normal\scratchdimen
   \dp\b_framed_normal\scratchdimen
   \hpack{\box\b_framed_normal}} %  why do we pack .. dange of loosing?

\installframedlocator \v!lohi % maybe also \v!center
  {\pack_framed_locator_before\v!middle}
  {\pack_framed_locator_after \v!middle}

\installframedlocator \v!bottom
  {}
  {\pack_framed_locator_set\strutdp
   \setbox\b_framed_normal\hpack{\lower\d_framed_locator_ht\box\b_framed_normal}%
   \ht\b_framed_normal\d_framed_locator_dp
   \dp\b_framed_normal\d_framed_locator_ht
   \hpack{\box\b_framed_normal}} %  why do we pack .. dange of loosing?

\installframedlocator \v!keep % retains height/depth
  {\pack_framed_remove_depth}
  {\pack_framed_restore_depth}

\newdimen\d_framed_formula

\installframedlocator \v!formula % private, will become a more generic name
  {}
  {\pack_framed_locator_set\d_framed_formula
   \setbox\b_framed_normal\hpack{\lower\d_framed_locator_dp\box\b_framed_normal}%
   \ht\b_framed_normal\d_framed_locator_ht
   \dp\b_framed_normal\d_framed_locator_dp
   \hpack{\box\b_framed_normal}} %  why do we pack .. danger of loosing?

% also used in fastlocalframed

\newdimen\d_framed_original_wd
\newdimen\d_framed_original_ht
\newdimen\d_framed_original_dp

\def\pack_framed_remove_depth
  {\d_framed_original_wd\wd\b_framed_normal
   \d_framed_original_ht\ht\b_framed_normal
   \d_framed_original_dp\dp\b_framed_normal
   \ifzeropt\d_framed_original_dp\else
     \setbox\b_framed_normal\hpack{\raise\d_framed_original_dp\box\b_framed_normal}%
   \fi
   \wd\b_framed_normal\d_framed_original_wd
   \ht\b_framed_normal\dimexpr\d_framed_original_ht+\d_framed_original_dp\relax
   \dp\b_framed_normal\zeropoint}

\def\pack_framed_restore_depth
  {\ifzeropt\d_framed_original_dp \else
     \setbox\b_framed_normal\hpack{\lower\d_framed_original_dp\box\b_framed_normal}%
   \fi
   \wd\b_framed_normal\d_framed_original_wd
   \ht\b_framed_normal\d_framed_original_ht
   \dp\b_framed_normal\d_framed_original_dp}

% \framed[width=12cm,height=3cm,orientation=0]{\input ward\relax}
% \framed[width=12cm,height=3cm,orientation=90]{\input ward\relax}
% \framed[width=12cm,height=3cm,orientation=180]{\input ward\relax}
% \framed[width=12cm,height=3cm,orientation=270]{\input ward\relax}
% \framed[width=12cm,height=3cm,orientation=-90]{\input ward\relax}
% \framed[width=12cm,height=3cm,orientation=-180]{\input ward\relax}
% \framed[width=12cm,height=3cm,orientation=-270]{\input ward\relax}

\def\pack_framed_start_orientation
  {\ifcase\p_framed_orientation
     \let\pack_framed_stop_orientation\relax
   \else
     \let\pack_framed_stop_orientation\pack_framed_stop_orientation_indeed
   \fi}

\def\pack_framed_stop_orientation_indeed
  {\setbox\b_framed_normal\hpack{\dorotatebox\p_framed_orientation\hpack{\box\b_framed_normal}}%
   \d_framed_height\ht\b_framed_normal
   \d_framed_width \wd\b_framed_normal}

%D The last conditional takes care of the special situation of in||line \inframed
%D [height=3cm] {framed} boxes. Such boxes have to be \inframed {aligned} with the
%D running text.

\unexpanded\def\inframed
  {\dosingleempty\pack_framed_inline}

% \def\pack_framed_inline[#1]%
%   {\framed[\c!location=\v!low,#1]}
%
% or:

\def\pack_framed_inline[%
  {\framed[\c!location=\v!low,}

%D When we set \type{empty} to \type{yes}, we get ourselves a frame and/or background,
%D but no content, so actually we have a sort of phantom framed box.

%D \macros
%D   {mframed, minframed}
%D
%D When Tobias asked how to frame mathematical elements in formulas, Taco's posted the
%D next macro:
%D
%D \starttyping
%D \def\mframed#1%
%D   {\relax
%D    \ifmmode
%D      \vcenter{\hbox{\framed{$\ifinner\else\displaystyle\fi#1$}}}%
%D    \else
%D      \framed{$#1$}%
%D    \fi}
%D \stoptyping
%D
%D Because \type {\ifinner} does not (always) reports what one would expect, we move the
%D test to the outer level. We also want to pass arguments,
%D
%D \starttyping
%D \def\mframed%
%D   {\dosingleempty\domframed}
%D
%D \def\domframed[#1]#2% % tzt \dowithnextmathbox ?
%D   {\relax
%D    \ifmmode
%D      \ifinner
%D        \inframed[#1]{$#2$}%
%D      \else
%D        \vcenter{\hbox{\framed[#1]{$\displaystyle#2$}}}%
%D      \fi
%D    \else
%D      \inframed[#1]{$#2$}%
%D    \fi}
%D \stoptyping
%D
%D Still better is the next alternative, if only because it takes care of setting the super-
%D and subscripts styles

\newcount\c_framed_mstyle

\unexpanded\def\pack_framed_math_strut
  {\Ustartmath
   \triggermathstyle\c_framed_mstyle
   \vphantom{(}%
   \Ustopmath}

\installcorenamespace{mathframed}

\installframedcommandhandler \??mathframed {mathframed} \??mathframed

\appendtoks
    \setuevalue{\currentmathframed}{\pack_framed_mathframed{\currentmathframed}}%
\to \everydefinemathframed

\unexpanded\def\pack_framed_mathframed#1%
  {\begingroup
   \edef\currentmathframed{#1}%
   \dosingleempty\pack_framed_mathframed_indeed}

\newcount\c_pack_framed_mathframed
\newtoks \t_pack_framed_mathframed

\def\pack_framed_math_pos
  {\global\advance\c_pack_framed_mathframed\plusone
   \xdef\pack_framed_mc_one{mcf:1:\number\c_pack_framed_mathframed}%
   \xdef\pack_framed_mc_two{mcf:2:\number\c_pack_framed_mathframed}%
   \xypos\pack_framed_mc_two}

\def\pack_framed_mathframed_indeed[#1]#2% no fancy nesting supported here
  {\iffirstargument
      \setupcurrentmathframed[#1]%
   \fi
   \c_framed_mstyle\mathstyle
   \edef\m_framed_location{\mathframedparameter\c!location}%
   \ifx\m_framed_location\v!mathematics
     \let\normalstrut\pack_framed_math_pos
   \orelse\ifx\m_framed_location\v!low\else
     \let\normalstrut\pack_framed_math_strut
   \fi
   \inheritedmathframedframed\bgroup
     \Ustartmath
     \triggermathstyle\c_framed_mstyle
     \the\t_pack_framed_mathframed
     #2%
     \Ustopmath
   \egroup
   \endgroup}

\appendtoks
    \mathraggedstatus\plustwo   % makes \startalign work
    \eqalignmode     \zerocount % makes \startalign fit
\to \t_pack_framed_mathframed

\installframedlocator \v!mathematics
  {}
  {\lower\dimexpr\MPy\pack_framed_mc_two-\MPy\pack_framed_mc_one\relax
   \hpack{\xypos\pack_framed_mc_one\box\b_framed_normal}}

\definemathframed[mframed]
\definemathframed[inmframed][\c!location=\v!low]
\definemathframed[mcframed] [\c!location=\v!mathematics]

%D So instead of the rather versatile \type {\framed}, we use \type {\mframed}:
%D
%D \startbuffer
%D \startformula
%D   x \times   \mframed{y} \times y^{z_z}
%D   x \times \inmframed{y} \times y^{z_z}
%D \stopformula
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D And:
%D
%D \startbuffer
%D \startformula
%D   x \times \mframed{y} \times y^{\mframed{z}_{\mframed{z}}}
%D \stopformula
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D As usual, one can specify in what way the text should be framed. One should be
%D aware of the fact that, inorder to preserve the proper spacing, the \type
%D {offset} is set to \type {overlay} and \type {frameoffset} is used used instead.
%D
%D \startbuffer
%D \startformula
%D   x \times y^{\mframed[framecolor=red]{z}_{z}}
%D \stopformula
%D \stopbuffer
%D
%D \typebuffer \getbuffer
%D
%D For inline use, we also provide the \type {\inmframed} alternative: we want $x
%D \times \inmframed{y}$ in inline math, right?

%D This previous framing macros needs a lot of alternatives for putting rules around
%D boxes, inserting offsets and aligning text. Each step is handled by separate macros.

\newdimen\d_framed_applied_offset
\newdimen\d_framed_loffset
\newdimen\d_framed_roffset
\newdimen\d_framed_toffset
\newdimen\d_framed_boffset

\def\pack_framed_check_extra_offsets % we could check h and v indepently
  {\setfalse\c_framed_has_extra_offset
   \d_framed_loffset\framedparameter\c!loffset\relax
   \d_framed_roffset\framedparameter\c!roffset\relax
   \d_framed_toffset\framedparameter\c!toffset\relax
   \d_framed_boffset\framedparameter\c!boffset\relax
   \ifzeropt\d_framed_loffset\else \advance\d_framed_width -\d_framed_loffset \settrue\c_framed_has_extra_offset \fi
   \ifzeropt\d_framed_roffset\else \advance\d_framed_width -\d_framed_roffset \settrue\c_framed_has_extra_offset \fi
   \ifzeropt\d_framed_toffset\else \advance\d_framed_height-\d_framed_toffset \settrue\c_framed_has_extra_offset \fi
   \ifzeropt\d_framed_boffset\else \advance\d_framed_height-\d_framed_boffset \settrue\c_framed_has_extra_offset \fi}

\def\pack_framed_apply_extra_offsets
  {\setbox\b_framed_normal\vpack\bgroup
     \advance\d_framed_toffset\d_framed_applied_offset
     \advance\d_framed_boffset\d_framed_applied_offset
     \advance\d_framed_loffset\d_framed_applied_offset
     \advance\d_framed_roffset\d_framed_applied_offset
     \kern\d_framed_toffset
     \hpack\bgroup
        \kern\d_framed_loffset
        \box\b_framed_normal
        \kern\d_framed_roffset
     \egroup
     \kern\d_framed_boffset
   \egroup}

\def\pack_framed_widen_box
  {\setbox\b_framed_normal\vpack
     {\kern\d_framed_applied_offset
      \hpack{\kern\d_framed_applied_offset\box\b_framed_normal\kern\d_framed_applied_offset}%
      \kern\d_framed_applied_offset}}

%D Let's hope that the next few examples show us enough of what needs to be
%D done by the auxiliary macros.
%D
%D \startbuffer
%D \framed[height=1cm,offset=.5cm]   {rule based learning}
%D \framed[height=1cm,offset=0cm]    {rule based learning}
%D \framed[height=1cm,offset=none]   {rule based learning}
%D \framed[height=1cm,offset=overlay]{rule based learning}
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlinecorrection
%D \hbox{\getbuffer}
%D \stoplinecorrection
%D
%D \startbuffer
%D \framed[offset=.5cm]   {rule based learning}
%D \framed[offset=0cm]    {rule based learning}
%D \framed[offset=none]   {rule based learning}
%D \framed[offset=overlay]{rule based learning}
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlinecorrection
%D \hbox{\getbuffer}
%D \stoplinecorrection
%D
%D \startbuffer
%D \framed[strut=no,offset=.5cm]   {rule based learning}
%D \framed[strut=no,offset=0cm]    {rule based learning}
%D \framed[strut=no,offset=none]   {rule based learning}
%D \framed[strut=no,offset=overlay]{rule based learning}
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlinecorrection
%D \hbox{\getbuffer}
%D \stoplinecorrection
%D
%D \startbuffer
%D \framed[width=3cm,align=left]   {rule\\based\\learning}
%D \framed[width=3cm,align=middle] {rule\\based\\learning}
%D \framed[width=3cm,align=right]  {rule\\based\\learning}
%D \framed[width=fit,align=middle] {rule\\based\\learning}
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlinecorrection
%D \hbox{\dontcomplain\getbuffer}
%D \stoplinecorrection
%D
%D So now we're ready for the complicated stuff. We distinguish between borders with
%D straight lines and those with round corners. When using the first alternative it
%D is possible to turn off one or more lines. More fancy shapes are also possible by
%D specifying dedicated backgrounds. Turning lines on and off is implemented as
%D efficient as possible and as a result is interface language dependant. This next
%D implementation evolved from simpler ones. It puts for instance the rules on top
%D of the content and provides additional offset capabilities. The lot of calls to
%D other macros makes this mechanism not that easy to comprehend.
%D
%D We handle left, right or middle alignment as well as fixed or free widths and
%D heights. Each combination gets its own macro.
%D
%D The following code handles one-liners: \type {align={line,flushright}}. Beware,
%D since we entered a group and either or not grab the next bgroup token, we need to
%D finish the group in the oneliner mode.

\ifdefined\raggedonelinerstate \else \newconditional\raggedonelinerstate \fi

\def\doformatonelinerbox % beware: assumes explicit preceding bgroup
  {\ifconditional\raggedonelinerstate
     \expandafter\dodoformatonelinerbox
   \else
     \expandafter\nodoformatonelinerbox
   \fi}

\def\dodoformatonelinerbox
  {\afterassignment\redoformatonelinerbox
   \setbox\nextbox\hbox} % maybe \hpack

\def\redoformatonelinerbox
  {\aftergroup\dododoformatonelinerbox
   \ignorespaces}

\def\dododoformatonelinerbox
  {\hpack to \hsize % was \hbox
     {\ifcase\raggedstatus\or\hss\or\hss       \fi
      \unhbox\nextbox \removeunwantedspaces
      \ifcase\raggedstatus\or    \or\hss\or\hss\fi}%
   \egroup}

\def\nodoformatonelinerbox % grabs {
  {\let\next=}

%D The handlers:

% Beware, we have a \noindent so an empty line is indeed an empty line and
% the \synchronizeinlinedirection triggers a vbox instead of a line.
%
% \startTEXpage[offset=0.5ex,align={lohi,middle}]
%
%     \vbox{\hbox{x}}
% \stopTEXpage
%
% \startTEXpage[offset=0.5ex,align={lohi,middle}]
%     \vbox{\hbox{x}}
% \stopTEXpage

% \def\pack_framed_forgetall{\forgetall}

\def\pack_framed_set_foregroundcolor
  {\edef\p_framed_foregroundcolor{\framedparameter\c!foregroundcolor}%
   \ifx\p_framed_foregroundcolor\empty\else\dousecolorparameter\p_framed_foregroundcolor\fi}

\def\pack_framed_do_setups
  {\ifx\p_framed_setups\empty \else
     \setups[\p_framed_setups]% \texsetup (or only one!)
   % \fastsetup\p_framed_setup % singular would have been better
   \fi}

\def\pack_framed_format_format_yes
  {\vbox to \d_framed_height
     \bgroup
       \let\postprocessframebox\relax
     % \pack_framed_forgetall
       \iftrialtypesetting \else
         \pack_framed_set_foregroundcolor
       \fi
       \oninterlineskip
       \hsize\d_framed_width
       \vsize\d_framed_height
       \pack_framed_do_setups
       \raggedcommand
       \pack_framed_do_top
       \bgroup
       \synchronizeinlinedirection
       \localbegstrut
       \aftergroup\localendstrut
       \aftergroup\pack_framed_do_bottom
       \aftergroup\egroup
       \doformatonelinerbox}

\def\pack_framed_format_format_nop
  {\vbox to \d_framed_height
     \bgroup
       \let\postprocessframebox\relax
     % \pack_framed_forgetall
       \iftrialtypesetting \else
         \pack_framed_set_foregroundcolor
       \fi
       \oninterlineskip
       \hsize\d_framed_width
       \vsize\d_framed_height
       \pack_framed_do_setups
       \raggedcenter
       \vss
       \bgroup
       \synchronizeinlinedirection
       \localbegstrut
       \aftergroup\localendstrut
       \aftergroup\vss
       \aftergroup\egroup
       \doformatonelinerbox}

\def\pack_framed_format_format_height
  {\vbox to \d_framed_height
     \bgroup
       \let\postprocessframebox\relax
     % \pack_framed_forgetall
       \iftrialtypesetting \else
         \pack_framed_set_foregroundcolor
       \fi
       \oninterlineskip
       \pack_framed_do_setups
       \raggedcommand
       \vss
       \bgroup
       \aftergroup\localendstrut
       \aftergroup\vss
       \aftergroup\egroup
       \synchronizeinlinedirection
       \localbegstrut
       \doformatonelinerbox}

\def\pack_framed_format_format_width
  {\vbox
     \bgroup
       \let\postprocessframebox\relax
     % \pack_framed_forgetall
       \iftrialtypesetting \else
         \pack_framed_set_foregroundcolor
       \fi
       \oninterlineskip
       \hsize\d_framed_width
       \pack_framed_do_setups
       \raggedcommand
       \pack_framed_do_top
       \bgroup
       \synchronizeinlinedirection
       \localbegstrut
       \aftergroup\localendstrut
       \aftergroup\pack_framed_do_bottom
       \aftergroup\egroup
       \doformatonelinerbox}

\def\pack_framed_format_format_vsize
  {\vbox to \d_framed_height % no vpack .. maybe grid
     \bgroup
       \let\postprocessframebox\relax
     % \pack_framed_forgetall
       \iftrialtypesetting \else
         \pack_framed_set_foregroundcolor
       \fi
       \vsize\d_framed_height
       \pack_framed_do_setups
       \vss
       \bgroup
       \aftergroup\vss
       \aftergroup\egroup
       \hbox
         \bgroup
         \aftergroup\egroup
         \synchronizeinlinedirection
         \localstrut
         \doformatonelinerbox}

\def\pack_framed_format_format_hsize
  {\hbox to \d_framed_width
     \bgroup
       \let\postprocessframebox\relax
     % \pack_framed_forgetall
       \iftrialtypesetting \else
         \pack_framed_set_foregroundcolor
       \fi
       \pack_framed_do_setups
       \hss
       \synchronizeinlinedirection
       \localstrut
       \bgroup
       \aftergroup\hss
       \aftergroup\egroup
       \doformatonelinerbox}

\def\pack_framed_format_format_no_size
  {\hbox
     \bgroup
       \iftrialtypesetting \else
         \pack_framed_set_foregroundcolor
       \fi
       \let\postprocessframebox\relax
       \pack_framed_do_setups
       \synchronizeinlinedirection
       \localstrut
       \doformatonelinerbox}

%D On the next page we show some examples of how these macros come into action. The
%D examples show us how \type {fit}, \type {broad} dimensions influence the
%D formatting. Watch the visualized struts. \footnote {Here we used \type
%D {\showstruts}.}
%D
%D \startpostponing
%D \bgroup
%D \showstruts
%D \dontcomplain
%D \starttabulate[|c|c|c|c|c|c|]
%D % \HL
%D \NC \framed[width=.2\hsize, height=.2\hsize, align=]       {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=broad,    align=]       {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=fit,      align=]       {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=.2\hsize, align=]       {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=broad,    align=]       {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=fit,      align=]       {a\endgraf b\endgraf c}
%D \NC \NR
%D % \HL
%D \NC \framed[width=.2\hsize, height=.2\hsize, align=yes]    {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=broad,    align=yes]    {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=fit,      align=yes]    {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=.2\hsize, align=yes]    {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=broad,    align=yes]    {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=fit,      align=yes]    {a\endgraf b\endgraf c}
%D \NC \NR
%D % \HL
%D \NC \framed[width=.2\hsize, height=.2\hsize, align=right]  {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=broad,    align=right]  {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=fit,      align=right]  {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=.2\hsize, align=right]  {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=broad,    align=right]  {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=fit,      align=right]  {a\endgraf b\endgraf c}
%D \NC \NR
%D % \HL
%D \NC \framed[width=.2\hsize, height=.2\hsize, align=left]   {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=broad,    align=left]   {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=fit,      align=left]   {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=.2\hsize, align=left]   {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=broad,    align=left]   {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=fit,      align=left]   {a\endgraf b\endgraf c}
%D \NC \NR
%D % \HL
%D \NC \framed[width=.2\hsize, height=.2\hsize, align=middle] {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=broad,    align=middle] {a\endgraf b\endgraf c}
%D \NC \framed[width=.2\hsize, height=fit,      align=middle] {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=.2\hsize, align=middle] {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=broad,    align=middle] {a\endgraf b\endgraf c}
%D \NC \framed[width=fit,      height=fit,      align=middle] {a\endgraf b\endgraf c}
%D \NC \NR
%D % \HL
%D \stoptabulate
%D \egroup
%D \stoppostponing

%D \macros
%D   {framednoflines, framedlastlength}
%D
%D It is possible to let the frame macro calculate the width of a centered box
%D automatically (\type {fit}). When doing so, we need to reshape the box:

\newcount\framednoflines
\newdimen\framedfirstheight
\newdimen\framedlastdepth
\newdimen\framedminwidth
\newdimen\framedmaxwidth
\newdimen\framedaveragewidth

\def\pack_framed_reshape_reset
  {\framednoflines    \zerocount
   \framedfirstheight \zeropoint
   \framedlastdepth   \zeropoint
   \framedminwidth    \zeropoint
   \framedmaxwidth    \zeropoint
   \framedaveragewidth\zeropoint}

\def\pack_framed_reshape_process{\ifvbox\b_framed_normal\clf_doreshapeframedbox\b_framed_normal\relax\fi}
\def\pack_framed_reshape_analyze{\ifvbox\b_framed_normal\clf_doanalyzeframedbox\b_framed_normal\relax\fi}

% torture test / strange case (much depth) / method 2 needed
%
% \startTEXpage[frame=on]
% \startformula \startalign \NC A \NC B \NR \intertext{test} \NC C \NC D \NR \stopalign \stopformula
% test outside formula
% \startformula \startalign \NC A \NC B \NR \intertext{test} \NC C \NC D \NR \stopalign \stopformula
% \blank[big]
% \startformula \startalign \NC \int_01 \NC B \NR \intertext{test} \NC \int_01 \NC D \NR \stopalign \stopformula
% test outside formula
% \startformula \startalign \NC \int_01 \NC B \NR \intertext{test} \NC \int_01 \NC D \NR \stopalign \stopformula
% \stopTEXpage

%D The examples on the next page show how one can give the frame as well as the
%D background an additional offset and even a bit more depth. The blue outline is
%D the frame, the red box is the background and the small black outline is the
%D visualization of the resulting box, that is, we applied \type {\ruledhbox} to
%D the result.
%D
%D \startpostponing
%D \bgroup
%D \unprotect
%D \dontcomplain
%D
%D \startbuffer
%D \unprotect
%D \vbox to \vsize
%D   \bgroup
%D     \startalignment[middle]
%D     \vss
%D     \dontleavehmode\vbox to .8\vsize
%D       \bgroup
%D         \hsize=300pt
%D         \setupframed
%D           [background=color,
%D            backgroundcolorachtergrondkleur=darkred,
%D            width=300pt,
%D            height=60pt,
%D            framecolorkaderkleur=DemoBlue,
%D            rulethickness=2pt]
%D         \def\status%
%D           {backgroundoffset=\the\dimexpr\framedparameter\c!backgroundoffset\relax\\
%D            frameoffset=\the\dimexpr\framedparameter\c!frameoffset\relax\\
%D            depth=\the\dimexpr\framedparameter\c!depth\relax}
%D         \dontleavehmode \ruledhbox{\framed[backgroundoffset=0pt,frameoffset=0pt]{\status}}
%D         \vss
%D         \dontleavehmode \ruledhbox{\framed[backgroundoffset=5pt,frameoffset=0pt]{\status}}
%D         \vss
%D         \dontleavehmode \ruledhbox{\framed[backgroundoffset=0pt,frameoffset=5pt]{\status}}
%D         \vss
%D         \dontleavehmode \ruledhbox{\framed[backgroundoffset=2pt,frameoffset=5pt]{\status}}
%D         \vss
%D         \dontleavehmode \ruledhbox{\framed[backgroundoffset=5pt,frameoffset=2pt]{\status}}
%D         \vss
%D         \dontleavehmode \ruledhbox{\framed[backgroundoffset=5pt,frameoffset=5pt]{\status}}
%D       \egroup
%D     \vss
%D     \stopalignment
%D     \egroup
%D \protect
%D \stopbuffer
%D
%D \getbuffer \page
%D
%D {\setupframed[depth=4pt]\getbuffer} \page
%D
%D \protect
%D \egroup
%D \stoppostponing

%D We can draw lines from left to right and top to bottom by using the normal \type
%D {\hairline} command. Both directions need a different treatment.
%D
%D \startbuffer
%D \framed[width=4cm]           {alfa\hairline beta\hairline gamma}
%D \framed[height=2cm]            {alfa\hairline beta\hairline gamma}
%D \framed[width=4cm,height=2cm]{alfa\hairline beta\hairline gamma}
%D \stopbuffer
%D
%D \typebuffer
%D
%D \startlinecorrection
%D \hbox{\getbuffer}
%D \stoplinecorrection
%D
%D These macros try to adapt their behaviour as good as possible to the circumstances
%D and act as natural as possible.

\unexpanded\def\pack_framed_vboxed_hairline % nasty overlay mess .. needed for autowidth
  {\begingroup
   \scratchoffset\ifconditional\c_framed_has_offset \localoffset \else \zeropoint \fi
   \scratchwidth \dimexpr\scratchoffset+\d_framed_linewidth\relax
   \par
   \nointerlineskip
   \kern\scratchoffset
   \dontleavehmode
     \hrule\s!height\d_framed_linewidth\s!depth\zeropoint
   \par
   \kern-\d_framed_linewidth
   \dontleavehmode
     \hpack to \zeropoint{\hss\vrule\s!height\d_framed_linewidth\s!depth\zeropoint\s!width\scratchwidth}%
     \hfill
     \hpack to \zeropoint{\vrule\s!height\d_framed_linewidth\s!depth\zeropoint\s!width\scratchwidth\hss}%
   \par
   \nointerlineskip
   \kern\scratchoffset
   \nointerlineskip
   \endgraf
   \nointerlineskip
   \localbegstrut
   \endgroup}

\unexpanded\def\pack_framed_hboxed_hairline % use framed dimen
  {\bgroup
   \scratchoffset\ifconditional\c_framed_has_offset \localoffset \else \zeropoint \fi
   \ifconditional\c_framed_has_height
     \dimen\scratchheight\dimexpr\localheight/\plustwo+\strutdp-\plustwo\d_framed_linewidth\relax
     \dimen\scratchdepth \dimexpr\localheight/\plustwo-\strutdp+\plustwo\d_framed_linewidth\relax
   \else
     \dimen\scratchheight\dimexpr\strutht+\scratchoffset\relax
     \dimen\scratchdepth \dimexpr\strutdp+\scratchoffset\relax
   \fi
   \unskip
   \setbox\scratchbox\hpack
     {\kern\scratchoffset
      \vrule\s!height\dimen\scratchheight\s!depth\dimen\scratchdepth\s!width\d_framed_linewidth
      \kern\scratchoffset}%
   \ht\scratchbox\strutht
   \dp\scratchbox\strutdp
   \box\scratchbox
   \ignorespaces
   \egroup}

%D The argument of the frame command accepts \type{\\} as a sort of newline signal. In
%D horizontal boxes it expands to a space.

\unexpanded\def\pack_framed_vboxed_newline
  {\endgraf\ignorespaces}

\unexpanded\def\pack_framed_hboxed_newline
  {\unskip\normalspace\ignorespaces}

%D We can set each rule on or off. The default setting is inherited from
%D \type {frame}. An earlier implementation use a bit different approach, but the new
%D one seems more natural:
%D
%D \bgroup
%D \setuptyping[margin=0pt]
%D \startlinecorrection
%D \startbuffer
%D \framed[offset=overlay,frame=on]{\darkred\blackrule}
%D \stopbuffer
%D \hbox{\getbuffer\vbox{\typebuffer}}
%D
%D \startbuffer
%D \framed[offset=overlay,frame=on,bottomframe=off]{\darkred\blackrule}
%D \stopbuffer
%D \hbox{\getbuffer\vbox{\typebuffer}}
%D
%D \startbuffer
%D \framed[offset=overlay,frame=on,bottomframe=on]{\darkred\blackrule}
%D \stopbuffer
%D \hbox{\getbuffer\vbox{\typebuffer}}
%D
%D \startbuffer
%D \framed[offset=overlay,frame=off]{\darkred\blackrule}
%D \stopbuffer
%D \hbox{\getbuffer\vbox{\typebuffer}}
%D
%D \startbuffer
%D \framed[offset=overlay,frame=off,bottomframe=off]{\darkred\blackrule}
%D \stopbuffer
%D \hbox{\getbuffer\vbox{\typebuffer}}
%D
%D \startbuffer
%D \framed[offset=overlay,frame=off,bottomframe=on]{\darkred\blackrule}
%D \stopbuffer
%D \hbox{\getbuffer\vbox{\typebuffer}}
%D \stoplinecorrection
%D \egroup

%D \macros
%D   {startframedtext, setupframedtexts, defineframedtext}
%D
%D The general framing command we discussed previously, is not entirely suited for
%D what we call framed texts, as for instance used in intermezzo's. The next
%D examples show what we have in mind.
%D
%D \startbuffer[framed-0]
%D \setupframedtexts
%D   [frame=off,
%D    width=\hsize,
%D    background=screen]
%D
%D \startframedtext
%D By default the framed text is centered \dots
%D \stopframedtext
%D
%D \startframedtext[right]
%D \dots\ but we can also align left, middle and right.
%D \stopframedtext
%D \stopbuffer
%D
%D \startbuffer[framed-1]
%D \defineframedtext
%D   [Example]
%D   [width=6cm,
%D    height=5cm]
%D
%D \startExample
%D \typebuffer[framed-1]
%D \stopExample
%D \stopbuffer
%D
%D \startbuffer[framed-2]
%D \defineframedtext
%D   [Example]
%D   [width=6cm]
%D
%D \startExample
%D \typebuffer[framed-2]
%D \stopExample
%D \stopbuffer
%D
%D \startbuffer[framed-3]
%D \defineframedtext
%D   [Example]
%D   [height=5cm]
%D
%D \startExample
%D \typebuffer[framed-3]
%D \stopExample
%D \stopbuffer
%D
%D \startbuffer[framed-4]
%D \defineframedtext
%D   [Example]
%D   [width=fit,height=broad]
%D
%D \Example{a very exciting example}
%D \stopbuffer
%D
%D \bgroup \setuptyping[margin=0pt] \getbuffer[framed-0] \egroup
%D \bgroup \setuptyping[margin=0pt] \getbuffer[framed-1] \egroup
%D \bgroup \setuptyping[margin=0pt] \getbuffer[framed-2] \egroup
%D \bgroup \setuptyping[margin=0pt] \getbuffer[framed-3] \egroup
%D \bgroup \setuptyping[margin=0pt] \getbuffer[framed-4] \egroup
%D
%D Here we can see that we have a predefined framed text class as well as the
%D tools for defining our own. So we have:
%D
%D \showsetup{setupframedtexts}
%D
%D as well as the definition command:
%D
%D \showsetup{defineframedtext}
%D
%D that generates two commands:
%D
%D \showsetup{start<<framedtext>>}
%D \showsetup{<<framedtext>>}
%D
%D The next definition shows the defaults.

\installcorenamespace{framedtext}
\installcorenamespace{framedtextlocation}

\installframedcommandhandler \??framedtext {framedtext} \??framedtext

\let\setupframedtexts\setupframedtext

\setupframedtext
  [\c!width=.75\hsize,
   \c!height=\v!fit,
   \c!align=\v!yes,
  %\c!top=,
   \c!bottom=\vfill,
   \c!offset=1em,
  %\c!bodyfont=,
  %\c!style=,
  %\c!color=,
  %\c!left=,
   \c!right=\hfill,
   \c!before=\blank,
   \c!after=\blank,
  %\c!inner=,
   \c!frame=\v!on,
  %\c!topframe=,
  %\c!bottomframe=,
  %\c!leftframe=,
  %\c!rightframe=,
   \c!radius=.5\bodyfontsize,
   \c!corner=\v!rectangular,
  %\c!orientation=,
  %\c!indenting=,
  %\c!foregroundcolor=,
  %\c!foregroundstyle=,
  %\c!background=,
  %\c!backgroundcolor=,
   \c!linecorrection=\v!on,
   \c!depthcorrection=\v!on,
   \c!margin=\v!standard]

\appendtoks
    \setuevalue{\e!start\currentframedtext}{\pack_framed_text_start {\currentframedtext}}%
    \setuevalue{\e!stop \currentframedtext}{\pack_framed_text_stop                      }%
    \setuevalue        {\currentframedtext}{\pack_framed_text_direct{\currentframedtext}}%
\to \everydefineframedtext

\setvalue{\??framedtextlocation\v!left  }{\letframedtextparameter\c!left \relax
                                          \letframedtextparameter\c!right\hfill}

\setvalue{\??framedtextlocation\v!right }{\letframedtextparameter\c!left \hfill
                                          \letframedtextparameter\c!right\relax}

\setvalue{\??framedtextlocation\v!middle}{\letframedtextparameter\c!left \hfill
                                          \letframedtextparameter\c!right\hfill}

\setvalue{\??framedtextlocation\v!none  }{\letframedtextparameter\c!left \relax
                                          \letframedtextparameter\c!right\relax
                                          \settrue\c_framed_text_location_none}

\unexpanded\def\pack_framed_text_start#1%
  {\bgroup
   \edef\currentframedtext{#1}%
   \dodoubleempty\pack_framed_text_start_indeed}

\def\pack_framed_text_start_indeed[#1][#2]%
  {\doifelseassignment{#1}
     {\pack_framed_text_start_continue\empty{#1}}
     {\pack_framed_text_start_continue{#1}{#2}}}

% todo: sort out first/lastline ht/dp

\def\pack_framed_text_start_continue#1#2%
  {\setupframedtexts[\currentframedtext][#2]%
   \doifsomething{#1}{\setframedtextparameter\c!location{#1}}% does not listen to #3
   \setfalse\c_framed_text_location_none
   \csname\??framedtextlocation\framedtextparameter\c!location\endcsname
   \resetframedtextparameter\c!location
   \pack_framed_text_check
   \setbox\b_framed_normal\vbox % \vpack
     \startboxedcontent
       \hsize\localhsize
    %  \insidefloattrue % ? better
       \usebodyfontparameter\framedtextparameter
    %  \edef\p_framed_text_strut{\letframedtextparameter\c!strut}% to be used
       \letframedtextparameter\c!strut\v!no
       \inheritedframedtextframed\bgroup
         \let\\=\endgraf
         \edef\p_framed_text_depthcorrection{\framedtextparameter\c!depthcorrection}%
         \ifx\p_framed_text_depthcorrection\v!on
            \pack_framed_text_start_depth_correction
         \else
            \bgroup
         \fi
         \vskip-\strutdp % brrr why is this needed ... needs to be sorted out, see testcase 1
         \doinhibitblank
         \useindentingparameter\framedtextparameter
         \useframedtextstyleandcolor\c!style\c!color
         \framedtextparameter\c!inner
         \ignorespaces}

% testcase 1:
%
% \showstruts
% \startframedtext[align={normal,tolerant},offset=0pt] \input tufte \stopframedtext
% \startframedtext[align={normal,tolerant},offset=0pt,depthcorrection=off] \input tufte \stopframedtext
% \startframedtext[align={normal,tolerant},offset=0pt,depthcorrection=off] \inframed{x} \stopframedtext
% \framed[align={normal,tolerant},offset=0pt]{\input tufte }

%D The \type {none} option is handy for nested usage, as in the presentation
%D styles, where we don't want interference.

\defineplacement[\??framedtext][\s!parent=\??framedtext\currentframedtext]

\unexpanded\def\pack_framed_text_stop % no \baselinecorrection, see faq docs
  {\endgraf
   \removelastskip
   \ifx\p_framed_text_depthcorrection\v!on
     \pack_framed_text_stop_depth_correction
   \else
     \egroup
   \fi
   \stopboxedcontent
   \ifconditional\c_framed_text_location_none
     \egroup
     \box\b_framed_normal
   \orelse\ifinsidefloat
     \egroup
     \box\b_framed_normal
   \else
     \egroup
     \placement[\??framedtext][\c!depthcorrection=\v!off]{\box\b_framed_normal}%
   \fi
   \egroup}

%D We define the general (and original) case by just saying:

\def\pack_framed_text_check % messy dependency
  {\localhsize\hsize
   \ifinsidefloat \orelse \ifdim\d_page_sides_vsize>\zeropoint % also possible: \c_page_sides_checks_done>\zeropoint
   % \strut            % rather clean way to invoke the sidefloat OTR
   % \setbox0=\lastbox % and get the widths set, so from now on we
   % \setlocalhsize    % can have framed texts alongside sidefloats
     \checksidefloat
     \setlocalhsize
   \fi}

\def\pack_framed_text_start_depth_correction
  {\bgroup
   \ifhmode
     \par
   \fi
   \ifvmode
     \verticalstrut
     % we need \nowhitespace in case of setups setting whitespace
     % nb, not safe, text vs \vbox as next
     \vskip-\struttotal
     \nowhitespace
   \fi} % na vskip ! new 20/05/2004, fails with next content being box (\scale{..})

\def\pack_framed_text_stop_depth_correction
  {\ifhmode
     \par
   \fi
   \ifvmode
     \forgetall
     \vskip-\struttotal
     \verticalstrut
     \egroup
     \forgetall % brrr too often
     \vskip-\lineheight
     \verticalstrut
   \else
     \egroup
   \fi}

%D Placement can be ignored:
%D
%D \starttyping
%D \hbox to \hsize \bgroup
%D     \startframedtext[none][width=.5\textwidth] \input tufte \stopframedtext
%D     \startframedtext[none][width=.5\textwidth] \input zapf  \stopframedtext
%D \egroup
%D
%D \hbox to \hsize \bgroup
%D     \setupframedtexts[location=none]%
%D     \startframedtext[width=.5\textwidth] \input zapf  \stopframedtext
%D     \startframedtext[width=.5\textwidth] \input tufte \stopframedtext
%D \egroup
%D \stoptyping

%D The simple brace (or group) delimited case is typeset slightly different
%D and is not aligned.

\unexpanded\def\pack_framed_text_direct#1%
  {\bgroup
   \edef\currentframedtext{#1}%
   \dosingleempty\pack_framed_text_start_direct}

\def\pack_framed_text_start_direct[#1]%
  {\usebodyfontparameter\framedtextparameter
   \iffirstargument
     \setupcurrentframedtext[#1]%
   \fi
   \edef\p_framed_text_strut{\framedtextparameter\c!strut}%
   \letframedtextparameter\c!strut\v!no
   \inheritedframedtextframed\bgroup
     \blank[\v!disable]%
     \let\\=\endgraf
     \useframedtextstyleandcolor\c!style\c!color
     \vskip-\strutdp % brrr why is this needed ... needs to be sorted out, see testcase 1
     \framedtextparameter\c!inner
     \ifx\p_framed_text_strut\v!no
        \let\pack_framed_strut\relax
     \else
        \let\pack_framed_strut\strut
     \fi
     \bgroup
     \aftergroup\pack_framed_text_stop_direct
     \afterassignment\ignorespaces
     \afterassignment\pack_framed_strut
     \let\next=}

\def\pack_framed_text_stop_direct
  {\removelastskip
   \egroup
   \egroup}

\defineframedtext
  [\v!framedtext]

%D \macros
%D   {defineframed}
%D
%D One can also define simple framed texts, using:
%D
%D \showsetup{defineframed}
%D
%D As suggested by Wolfgang we can now use the new \MKIV\ inheritance model instead
%D of passing a combination of arguments. This also also simplified the \type
%D {\setupframed} command. There are certainly more places where such improvements
%D can be made.

\appendtoks
    \ifcsname\??regularframedlevel\currentframed\endcsname
      % already defined, keeps settings
    \else
      \expandafter\newcount\csname\??regularframedlevel\currentframed\endcsname
    \fi
\to \everypresetframed

\appendtoks
   \setuevalue\currentframed{\pack_framed_defined_process[\currentframed]}%
\to \everydefineframed

\newcount\c_temp_framed_crap

\unexpanded\def\pack_framed_defined_process[#1]% official (not much checking, todo: parent)
  {\bgroup
   \ifcsname\??regularframedlevel#1\endcsname
    %\expandafter\let\expandafter\c_pack_framed_temp\csname\??regularframedlevel#1\endcsname
     \expandafter\let\expandafter\c_pack_framed_temp\lastnamedcs
   \else
     \let\c_pack_framed_temp\c_temp_framed_crap
   \fi
   \advance\c_pack_framed_temp\plusone
   \expandafter\def\csname\??framed#1>\the\c_pack_framed_temp:\s!parent\endcsname{\??framed#1}% \inheritlocalframed
   \bgroup
   \edef\currentframed{#1>\the\c_pack_framed_temp}%
   \pack_framed_initialize
   \dosingleempty\pack_framed_defined_process_indeed}

\def\pack_framed_defined_process_indeed[#1]%
  {\iffirstargument % faster
     \setupcurrentframed[#1]% here !
   \fi
   \pack_framed_process_indeed}

\let\placeframed\pack_framed_defined_process % new per 2012/04/23

%D We can do:
%D
%D \starttyping
%D \defineframed[\v!framed]
%D \stoptyping
%D
%D but the existing one is ok as well (less csname messy too).

%D New, for the moment private; let's see when GB finds out about this one and its
%D obscure usage. It's used in:
%D
%D \startbuffer
%D \defineframedtext
%D   [tabulateframe]
%D   [offset=overlay,
%D    backgroundoffset=3pt,
%D    background=color,
%D    backgroundcolor=green]
%D
%D \setuptabulate
%D   [tabulate]
%D   [frame=tabulateframe]
%D
%D \setuptables
%D   [frame=tabulateframe]
%D
%D \input tufte
%D
%D \starttabulate[|l|l|]
%D \NC test \NC test \NC \NR \NC test \NC test \NC \NR
%D \NC test \NC test \NC \NR \NC test \NC test \NC \NR
%D \stoptabulate
%D
%D \input tufte
%D
%D \starttable[|l|l|]
%D \NC test \NC test \NC \AR \NC test \NC test \NC \AR
%D \NC test \NC test \NC \AR \NC test \NC test \NC \AR
%D \stoptable
%D \stopbuffer
%D
%D \typebuffer

\installcorenamespace{framedcontent}

\installframedcommandhandler \??framedcontent {framedcontent} \??framedcontent

\setupframedcontent
  [\c!leftoffset=\zeropoint,
  %\c!rightoffset=\framedcontentparameter\c!leftoffset,
   \c!rightoffset=\scratchleftoffset,
   \c!topoffset=\zeropoint,
  %\c!bottomoffset=\framedcontentparameter\c!topoffset,
   \c!bottomoffset=\scratchtopoffset,
   \c!strut=\v!no,
  %\c!linecorrection=\v!no,
  %\c!left=,
  %\c!right=,
  %\c!width=\v!fit,
   \c!offset=\v!overlay]

\unexpanded\def\startframedcontent
  {\dosingleempty\pack_framed_start_content}

\def\pack_framed_start_content[#1]%
  {\bgroup
   \edef\currentframedcontent{#1}%
   \ifx\currentframedcontent\v!off
     \let\stopframedcontent\egroup
   \else
     \checkframedcontentparent
     \let\stopframedcontent\pack_framed_stop_content_indeed
     \expandafter\pack_framed_start_content_indeed
   \fi}

\def\pack_framed_start_content_indeed
  {\setbox\b_framed_normal\hpack\bgroup
     \setlocalhsize
     \hsize\localhsize
     \scratchleftoffset  \framedcontentparameter\c!leftoffset  \relax
     \scratchrightoffset \framedcontentparameter\c!rightoffset \relax
     \scratchtopoffset   \framedcontentparameter\c!topoffset   \relax
     \scratchbottomoffset\framedcontentparameter\c!bottomoffset\relax
     \advance\hsize\dimexpr-\scratchleftoffset-\scratchrightoffset \relax
     \advance\vsize\dimexpr-\scratchtopoffset -\scratchbottomoffset\relax
     \kern\scratchleftoffset
     \vpack\bgroup
       \vskip\scratchtopoffset
       \vbox\bgroup
         \forgetall
         \blank[\v!disable]}

\def\pack_framed_stop_content_indeed
         {\removelastskip
       \egroup
       \vskip\scratchbottomoffset
     \egroup
     \kern\scratchrightoffset
   \egroup
   \doif{\framedcontentparameter\c!width}\v!fit
     {\letframedcontentparameter\c!width\v!fixed}% no shapebox
   \ifinsidefloat
     \donefalse
   \else
     \doifelse{\framedcontentparameter\c!linecorrection}\v!yes\donetrue\donefalse
   \fi
   % plaats ?
   \ifdone\startlinecorrection\fi
   \framedcontentparameter\c!left % new
   \inheritedframedcontentframed{\box\b_framed_normal}% hm
   \framedcontentparameter\c!right % new
   \ifdone\stoplinecorrection\fi
   \egroup}

% A shared setting.

\setuplinewidth
  [\v!medium]

%D A Goodie:

\def\v!unframed{unframed}

\defineframed
  [\v!unframed]
  [\c!frame=\v!off,
   \c!rulethickness=\zeropoint,
   \c!foregroundstyle=\framedparameter\c!style,
   \c!foregroundcolor=\framedparameter\c!color]

%D Bonus (as defined in \type {pack-rul.lua}):
%D
%D \starttyping
%D \setbox\scratchbox\vbox{a\par aa\par aaa\par}
%D \the\dimexpr\themaxboxwidth\scratchbox\relax
%D \stoptyping

\let\themaxboxwidth\clf_themaxboxwidth

%D New: slow but ok for most cases:

\unexpanded\def\doifelseframed#1%
  {\ifcase\numexpr\zerocount
     \immediateassignment\edef\tempstring{#1\c!frame      }\ifx\tempstring\v!on      +\plusone\fi
     \immediateassignment\edef\tempstring{#1\c!topframe   }\ifx\tempstring\v!on      +\plusone\fi
     \immediateassignment\edef\tempstring{#1\c!bottomframe}\ifx\tempstring\v!on      +\plusone\fi
     \immediateassignment\edef\tempstring{#1\c!leftframe  }\ifx\tempstring\v!on      +\plusone\fi
     \immediateassignment\edef\tempstring{#1\c!rightframe }\ifx\tempstring\v!on      +\plusone\fi
     \immediateassignment\edef\tempstring{#1\c!background }\ifx\tempstring\empty\else+\plusone\fi
   \relax\expandafter\secondoftwoarguments\else\expandafter\firstoftwoarguments\fi}

\protect \endinput