aboutsummaryrefslogtreecommitdiff
path: root/box-drawing.c
blob: 421ff54dcab50c2acb6eaee80617903622137c42 (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
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
#include "box-drawing.h"

#include <stdio.h>
#include <math.h>
#include <fenv.h>
#include <errno.h>

#define LOG_MODULE "box-drawing"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "config.h"
#include "macros.h"
#include "stride.h"
#include "terminal.h"
#include "util.h"
#include "xmalloc.h"

#define clamp(x, lower, upper) (min(upper, max(x, lower)))

enum thickness {
    LIGHT,
    HEAVY,
};

struct buf {
    uint8_t *data;
    pixman_image_t *pix;
    pixman_format_code_t format;
    int width;
    int height;
    int stride;
    bool solid_shades;

    int thickness[2];

    /* For octants, sextants and wedges */
    int x_halfs[2];
    int y_thirds[2];

    /* For octants */
    int y_quads[3];
};

static const pixman_color_t white = {0xffff, 0xffff, 0xffff, 0xffff};

static void
change_buffer_format(struct buf *buf, pixman_format_code_t new_format)
{
    int stride = stride_for_format_and_width(new_format, buf->width);
    uint8_t *new_data = xcalloc(buf->height * stride, 1);
    pixman_image_t *new_pix = pixman_image_create_bits_no_clear(
        new_format, buf->width, buf->height, (uint32_t *)new_data, stride);

    if (new_pix == NULL) {
        errno = ENOMEM;
        perror(__func__);
        abort();
    }

    pixman_image_unref(buf->pix);
    free(buf->data);

    buf->data = new_data;
    buf->pix = new_pix;
    buf->format = new_format;
    buf->stride = stride;
}

static int NOINLINE
_thickness(int base_thickness, enum thickness thick)
{
    int multiplier = thick * 2 + 1;

    xassert(base_thickness >= 1);
    xassert((thick == LIGHT && multiplier == 1) ||
            (thick == HEAVY && multiplier == 3));

    return base_thickness * multiplier;
}
#define thickness(thick) buf->thickness[thick]

static void NOINLINE
_hline(struct buf *buf, int x1, int x2, int y, int thick)
{
    pixman_box32_t box = {
        .x1 = min(max(x1, 0), buf->width),
        .x2 = min(max(x2, 0), buf->width),
        .y1 = min(max(y, 0), buf->height),
        .y2 = min(max(y + thick, 0), buf->height),
    };
    pixman_image_fill_boxes(PIXMAN_OP_SRC, buf->pix, &white, 1, &box);
}

#define hline(x1, x2, y, thick) _hline(buf, x1, x2, y, thick)

static void NOINLINE
_vline(struct buf *buf, int y1, int y2, int x, int thick)
{
    pixman_box32_t box = {
        .x1 = min(max(x, 0), buf->width),
        .x2 = min(max(x + thick, 0), buf->width),
        .y1 = min(max(y1, 0), buf->height),
        .y2 = min(max(y2, 0), buf->height),
    };
    pixman_image_fill_boxes(PIXMAN_OP_SRC, buf->pix, &white, 1, &box);
}

#define vline(y1, y2, x, thick) _vline(buf, y1, y2, x, thick)

static void NOINLINE
_rect(struct buf *buf, int x1, int y1, int x2, int y2)
{
    pixman_box32_t box = {
        .x1 = min(max(x1, 0), buf->width),
        .y1 = min(max(y1, 0), buf->height),
        .x2 = min(max(x2, 0), buf->width),
        .y2 = min(max(y2, 0), buf->height),
    };
    pixman_image_fill_boxes(PIXMAN_OP_SRC, buf->pix, &white, 1, &box);
}

#define rect(x1, y1, x2, y2) _rect(buf, x1, y1, x2, y2)

static void NOINLINE
_hline_middle(struct buf *buf, enum thickness _thick)
{
    int thick = thickness(_thick);
    hline(0, buf->width, (buf->height - thick) / 2, thick);
}

static void NOINLINE
_hline_middle_left(struct buf *buf, enum thickness _vthick, enum thickness _hthick)
{
    int vthick = thickness(_vthick);
    int hthick = thickness(_hthick);
    _hline(buf, 0, (buf->width + vthick) / 2, (buf->height - hthick) / 2, hthick);
}

static void NOINLINE
_hline_middle_right(struct buf *buf, enum thickness _vthick, enum thickness _hthick)
{
    int vthick = thickness(_vthick);
    int hthick = thickness(_hthick);
    hline((buf->width - vthick) / 2, buf->width, (buf->height - hthick) / 2, hthick);
}

static void NOINLINE
_vline_middle(struct buf *buf, enum thickness _thick)
{
    int thick = thickness(_thick);
    vline(0, buf->height, (buf->width - thick) / 2, thick);
}

static void NOINLINE
_vline_middle_up(struct buf *buf, enum thickness _vthick, enum thickness _hthick)
{
    int vthick = thickness(_vthick);
    int hthick = thickness(_hthick);
    vline(0, (buf->height + hthick) / 2, (buf->width - vthick) / 2, vthick);
}

static void NOINLINE
_vline_middle_down(struct buf *buf, enum thickness _vthick, enum thickness _hthick)
{
    int vthick = thickness(_vthick);
    int hthick = thickness(_hthick);
    vline((buf->height - hthick) / 2, buf->height, (buf->width - vthick) / 2, vthick);
}

#define hline_middle(thick) _hline_middle(buf, thick)
#define hline_middle_left(thick) _hline_middle_left(buf, thick, thick)
#define hline_middle_right(thick) _hline_middle_right(buf, thick, thick)
#define hline_middle_left_mixed(vthick, hthick) _hline_middle_left(buf, vthick, hthick)
#define hline_middle_right_mixed(vthick, hthick) _hline_middle_right(buf, vthick, hthick)
#define vline_middle(thick) _vline_middle(buf, thick)
#define vline_middle_up(thick) _vline_middle_up(buf, thick, thick)
#define vline_middle_down(thick) _vline_middle_down(buf, thick, thick)
#define vline_middle_up_mixed(vthick, hthick) _vline_middle_up(buf, vthick, hthick)
#define vline_middle_down_mixed(vthick, hthick) _vline_middle_down(buf, vthick, hthick)

static void
draw_box_drawings_light_horizontal(struct buf *buf)
{
    hline_middle(LIGHT);
}

static void
draw_box_drawings_heavy_horizontal(struct buf *buf)
{
    hline_middle(HEAVY);
}

static void
draw_box_drawings_light_vertical(struct buf *buf)
{
    vline_middle(LIGHT);
}

static void
draw_box_drawings_heavy_vertical(struct buf *buf)
{
    vline_middle(HEAVY);
}

static void
draw_box_drawings_dash_horizontal(struct buf *buf, int count, int thick, int gap)
{
    int width = buf->width;
    int height = buf->height;

    xassert(count >= 2 && count <= 4);
    const int gap_count = count - 1;

    int dash_width = (width - (gap_count * gap)) / count;
    while (dash_width <= 0 && gap > 1) {
        gap--;
        dash_width = (width - (gap_count * gap)) / count;
    }

    if (dash_width <= 0) {
        hline_middle(LIGHT);
        return;
    }

    xassert(count * dash_width + gap_count * gap <= width);

    int remaining = width - count * dash_width - gap_count * gap;

    int x[4] = {0};
    int w[4] = {dash_width, dash_width, dash_width, dash_width};

    x[0] = 0;

    x[1] = x[0] + w[0] + gap;
    if (count == 2)
        w[1] = width - x[1];
    else if (count == 3)
        w[1] += remaining;
    else
        w[1] += remaining / 2;

    if (count >= 3) {
        x[2] = x[1] + w[1] + gap;
        if (count == 3)
            w[2] = width - x[2];
        else
            w[2] += remaining - remaining / 2;
    }

    if (count >= 4) {
        x[3] = x[2] + w[2] + gap;
        w[3] = width - x[3];
    }

    hline(x[0], x[0] + w[0], (height - thick) / 2, thick);
    hline(x[1], x[1] + w[1], (height - thick) / 2, thick);
    if (count >= 3)
        hline(x[2], x[2] + w[2], (height - thick) / 2, thick);
    if (count >= 4)
        hline(x[3], x[3] + w[3], (height - thick) / 2, thick);
}

static void
draw_box_drawings_dash_vertical(struct buf *buf, int count, int thick, int gap)
{
    int width = buf->width;
    int height = buf->height;

    xassert(count >= 2 && count <= 4);
    const int gap_count = count - 1;

    int dash_height = (height - (gap_count * gap)) / count;
    while (dash_height <= 0 && gap > 1) {
        gap--;
        dash_height = (height - (gap_count * gap)) / count;
    }

    if (dash_height <= 0) {
        vline_middle(LIGHT);
        return;
    }

    xassert(count * dash_height + gap_count * gap <= height);

    int remaining = height - count * dash_height - gap_count * gap;

    int y[4] = {0};
    int h[4] = {dash_height, dash_height, dash_height, dash_height};

    y[0] = 0;

    y[1] = y[0] + h[0] + gap;
    if (count == 2)
        h[1] = height - y[1];
    else if (count == 3)
        h[1] += remaining;
    else
        h[1] += remaining / 2;

    if (count >= 3) {
        y[2] = y[1] + h[1] + gap;
        if (count == 3)
            h[2] = height - y[2];
        else
            h[2] += remaining - remaining / 2;
    }

    if (count >= 4) {
        y[3] = y[2] + h[2] + gap;
        h[3] = height - y[3];
    }

    vline(y[0], y[0] + h[0], (width - thick) / 2, thick);
    vline(y[1], y[1] + h[1], (width - thick) / 2, thick);
    if (count >= 3)
        vline(y[2], y[2] + h[2], (width - thick) / 2, thick);
    if (count >= 4)
        vline(y[3], y[3] + h[3], (width - thick) / 2, thick);
}

static void
draw_box_drawings_light_triple_dash_horizontal(struct buf *buf)
{
    draw_box_drawings_dash_horizontal(buf, 3, thickness(LIGHT), thickness(LIGHT));
}

static void
draw_box_drawings_heavy_triple_dash_horizontal(struct buf *buf)
{
    draw_box_drawings_dash_horizontal(buf, 3, thickness(HEAVY), thickness(LIGHT));
}

static void
draw_box_drawings_light_triple_dash_vertical(struct buf *buf)
{
    draw_box_drawings_dash_vertical(buf, 3, thickness(LIGHT), thickness(HEAVY));
}

static void
draw_box_drawings_heavy_triple_dash_vertical(struct buf *buf)
{
    draw_box_drawings_dash_vertical(buf, 3, thickness(HEAVY), thickness(HEAVY));
}

static void
draw_box_drawings_light_quadruple_dash_horizontal(struct buf *buf)
{
    draw_box_drawings_dash_horizontal(buf, 4, thickness(LIGHT), thickness(LIGHT));
}

static void
draw_box_drawings_heavy_quadruple_dash_horizontal(struct buf *buf)
{
    draw_box_drawings_dash_horizontal(buf, 4, thickness(HEAVY), thickness(LIGHT));
}

static void
draw_box_drawings_light_quadruple_dash_vertical(struct buf *buf)
{
    draw_box_drawings_dash_vertical(buf, 4, thickness(LIGHT), thickness(LIGHT));
}

static void
draw_box_drawings_heavy_quadruple_dash_vertical(struct buf *buf)
{
    draw_box_drawings_dash_vertical(buf, 4, thickness(HEAVY), thickness(LIGHT));
}

static void
draw_box_drawings_light_down_and_right(struct buf *buf)
{
    hline_middle_right(LIGHT);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_down_light_and_right_heavy(struct buf *buf)
{
    hline_middle_right_mixed(LIGHT, HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_down_heavy_and_right_light(struct buf *buf)
{
    hline_middle_right(LIGHT);
    vline_middle_down_mixed(HEAVY, LIGHT);
}

static void
draw_box_drawings_heavy_down_and_right(struct buf *buf)
{
    hline_middle_right(HEAVY);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_light_down_and_left(struct buf *buf)
{
    hline_middle_left(LIGHT);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_down_light_and_left_heavy(struct buf *buf)
{
    hline_middle_left_mixed(LIGHT, HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_down_heavy_and_left_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    vline_middle_down_mixed(HEAVY, LIGHT);
}

static void
draw_box_drawings_heavy_down_and_left(struct buf *buf)
{
    hline_middle_left(HEAVY);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_light_up_and_right(struct buf *buf)
{
    hline_middle_right(LIGHT);
    vline_middle_up(LIGHT);
}

static void
draw_box_drawings_up_light_and_right_heavy(struct buf *buf)
{
    hline_middle_right_mixed(LIGHT, HEAVY);
    vline_middle_up(LIGHT);
}

static void
draw_box_drawings_up_heavy_and_right_light(struct buf *buf)
{
    hline_middle_right(LIGHT);
    vline_middle_up_mixed(HEAVY, LIGHT);
}

static void
draw_box_drawings_heavy_up_and_right(struct buf *buf)
{
    hline_middle_right(HEAVY);
    vline_middle_up(HEAVY);
}

static void
draw_box_drawings_light_up_and_left(struct buf *buf)
{
    hline_middle_left(LIGHT);
    vline_middle_up(LIGHT);
}

static void
draw_box_drawings_up_light_and_left_heavy(struct buf *buf)
{
    hline_middle_left_mixed(LIGHT, HEAVY);
    vline_middle_up(LIGHT);
}

static void
draw_box_drawings_up_heavy_and_left_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    vline_middle_up_mixed(HEAVY, LIGHT);
}

static void
draw_box_drawings_heavy_up_and_left(struct buf *buf)
{
    hline_middle_left(HEAVY);
    vline_middle_up(HEAVY);
}

static void
draw_box_drawings_light_vertical_and_right(struct buf *buf)
{
    hline_middle_right(LIGHT);
    vline_middle(LIGHT);
}

static void
draw_box_drawings_vertical_light_and_right_heavy(struct buf *buf)
{
    hline_middle_right_mixed(LIGHT, HEAVY);
    vline_middle(LIGHT);
}

static void
draw_box_drawings_up_heavy_and_right_down_light(struct buf *buf)
{
    hline_middle_right(LIGHT);
    vline_middle_up_mixed(HEAVY, LIGHT);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_down_heavy_and_right_up_light(struct buf *buf)
{
    hline_middle_right(LIGHT);
    vline_middle_up(LIGHT);
    vline_middle_down_mixed(HEAVY, LIGHT);
}

static void
draw_box_drawings_vertical_heavy_and_right_light(struct buf *buf)
{
    hline_middle_right(LIGHT);
    vline_middle(HEAVY);
}

static void
draw_box_drawings_down_light_and_right_up_heavy(struct buf *buf)
{
    hline_middle_right(HEAVY);
    vline_middle_up(HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_up_light_and_right_down_heavy(struct buf *buf)
{
    hline_middle_right(HEAVY);
    vline_middle_up(LIGHT);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_heavy_vertical_and_right(struct buf *buf)
{
    hline_middle_right(HEAVY);
    vline_middle(HEAVY);
}

static void
draw_box_drawings_light_vertical_and_left(struct buf *buf)
{
    hline_middle_left(LIGHT);
    vline_middle(LIGHT);
}

static void
draw_box_drawings_vertical_light_and_left_heavy(struct buf *buf)
{
    hline_middle_left_mixed(LIGHT, HEAVY);
    vline_middle(LIGHT);
}

static void
draw_box_drawings_up_heavy_and_left_down_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    vline_middle_up_mixed(HEAVY, LIGHT);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_down_heavy_and_left_up_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    vline_middle_up(LIGHT);
    vline_middle_down_mixed(HEAVY, LIGHT);
}

static void
draw_box_drawings_vertical_heavy_and_left_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    vline_middle(HEAVY);
}

static void
draw_box_drawings_down_light_and_left_up_heavy(struct buf *buf)
{
    hline_middle_left(HEAVY);
    vline_middle_up(HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_up_light_and_left_down_heavy(struct buf *buf)
{
    hline_middle_left(HEAVY);
    vline_middle_up(LIGHT);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_heavy_vertical_and_left(struct buf *buf)
{
    hline_middle_left(HEAVY);
    vline_middle(HEAVY);
}

static void
draw_box_drawings_light_down_and_horizontal(struct buf *buf)
{
    hline_middle(LIGHT);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_left_heavy_and_right_down_light(struct buf *buf)
{
    hline_middle_left_mixed(LIGHT, HEAVY);
    hline_middle_right(LIGHT);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_right_heavy_and_left_down_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    hline_middle_right_mixed(LIGHT, HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_down_light_and_horizontal_heavy(struct buf *buf)
{
    hline_middle(HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_down_heavy_and_horizontal_light(struct buf *buf)
{
    hline_middle(LIGHT);
    vline_middle_down_mixed(HEAVY, LIGHT);
}

static void
draw_box_drawings_right_light_and_left_down_heavy(struct buf *buf)
{
    hline_middle_left(HEAVY);
    hline_middle_right(LIGHT);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_left_light_and_right_down_heavy(struct buf *buf)
{
    hline_middle_left(LIGHT);
    hline_middle_right(HEAVY);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_heavy_down_and_horizontal(struct buf *buf)
{
    hline_middle(HEAVY);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_light_up_and_horizontal(struct buf *buf)
{
    hline_middle(LIGHT);
    vline_middle_up(LIGHT);
}

static void
draw_box_drawings_left_heavy_and_right_up_light(struct buf *buf)
{
    hline_middle_left_mixed(LIGHT, HEAVY);
    hline_middle_right(LIGHT);
    vline_middle_up(LIGHT);
}

static void
draw_box_drawings_right_heavy_and_left_up_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    hline_middle_right_mixed(LIGHT, HEAVY);
    vline_middle_up(LIGHT);
}

static void
draw_box_drawings_up_light_and_horizontal_heavy(struct buf *buf)
{
    hline_middle(HEAVY);
    vline_middle_up(LIGHT);
}

static void
draw_box_drawings_up_heavy_and_horizontal_light(struct buf *buf)
{
    hline_middle(LIGHT);
    vline_middle_up_mixed(HEAVY, LIGHT);
}

static void
draw_box_drawings_right_light_and_left_up_heavy(struct buf *buf)
{
    hline_middle_left(HEAVY);
    hline_middle_right(LIGHT);
    vline_middle_up(HEAVY);
}

static void
draw_box_drawings_left_light_and_right_up_heavy(struct buf *buf)
{
    hline_middle_left(LIGHT);
    hline_middle_right(HEAVY);
    vline_middle_up(HEAVY);
}

static void
draw_box_drawings_heavy_up_and_horizontal(struct buf *buf)
{
    hline_middle(HEAVY);
    vline_middle_up(HEAVY);
}

static void
draw_box_drawings_light_vertical_and_horizontal(struct buf *buf)
{
    hline_middle(LIGHT);
    vline_middle(LIGHT);
}

static void
draw_box_drawings_left_heavy_and_right_vertical_light(struct buf *buf)
{
    hline_middle_left_mixed(LIGHT, HEAVY);
    hline_middle_right(LIGHT);
    vline_middle(LIGHT);
}

static void
draw_box_drawings_right_heavy_and_left_vertical_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    hline_middle_right_mixed(LIGHT, HEAVY);
    vline_middle(LIGHT);
}

static void
draw_box_drawings_vertical_light_and_horizontal_heavy(struct buf *buf)
{
    hline_middle(HEAVY);
    vline_middle(LIGHT);
}

static void
draw_box_drawings_up_heavy_and_down_horizontal_light(struct buf *buf)
{
    hline_middle(LIGHT);
    vline_middle_up_mixed(HEAVY, LIGHT);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_down_heavy_and_up_horizontal_light(struct buf *buf)
{
    hline_middle(LIGHT);
    vline_middle_up(LIGHT);
    vline_middle_down_mixed(HEAVY, LIGHT);
}

static void
draw_box_drawings_vertical_heavy_and_horizontal_light(struct buf *buf)
{
    hline_middle(LIGHT);
    vline_middle(HEAVY);
}

static void
draw_box_drawings_left_up_heavy_and_right_down_light(struct buf *buf)
{
    hline_middle_left(HEAVY);
    hline_middle_right(LIGHT);
    vline_middle_up(HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_right_up_heavy_and_left_down_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    hline_middle_right(HEAVY);
    vline_middle_up(HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_left_down_heavy_and_right_up_light(struct buf *buf)
{
    hline_middle_left(HEAVY);
    hline_middle_right(LIGHT);
    vline_middle_up(LIGHT);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_right_down_heavy_and_left_up_light(struct buf *buf)
{
    hline_middle_left(LIGHT);
    hline_middle_right(HEAVY);
    vline_middle_up(LIGHT);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_down_light_and_up_horizontal_heavy(struct buf *buf)
{
    hline_middle(HEAVY);
    vline_middle_up(HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_up_light_and_down_horizontal_heavy(struct buf *buf)
{
    hline_middle(HEAVY);
    vline_middle_up(LIGHT);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_right_light_and_left_vertical_heavy(struct buf *buf)
{
    hline_middle_left(HEAVY);
    hline_middle_right(LIGHT);
    vline_middle(HEAVY);
}

static void
draw_box_drawings_left_light_and_right_vertical_heavy(struct buf *buf)
{
    hline_middle_left(LIGHT);
    hline_middle_right(HEAVY);
    vline_middle(HEAVY);
}

static void
draw_box_drawings_heavy_vertical_and_horizontal(struct buf *buf)
{
    hline_middle(HEAVY);
    vline_middle(HEAVY);
}

static void
draw_box_drawings_light_double_dash_horizontal(struct buf *buf)
{
    draw_box_drawings_dash_horizontal(buf, 2, thickness(LIGHT), thickness(LIGHT));
}

static void
draw_box_drawings_heavy_double_dash_horizontal(struct buf *buf)
{
    draw_box_drawings_dash_horizontal(buf, 2, thickness(HEAVY), thickness(LIGHT));
}

static void
draw_box_drawings_light_double_dash_vertical(struct buf *buf)
{
    draw_box_drawings_dash_vertical(buf, 2, thickness(LIGHT), thickness(HEAVY));
}

static void
draw_box_drawings_heavy_double_dash_vertical(struct buf *buf)
{
    draw_box_drawings_dash_vertical(buf, 2, thickness(HEAVY), thickness(HEAVY));
}

static void
draw_box_drawings_double_horizontal(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int mid = (buf->height - thick * 3) / 2;

    hline(0, buf->width, mid, thick);
    hline(0, buf->width, mid + 2 * thick, thick);
}

static void
draw_box_drawings_double_vertical(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int mid = (buf->width - thick * 3) / 2;

    vline(0, buf->height, mid, thick);
    vline(0, buf->height, mid + 2 * thick, thick);
}

static void
draw_box_drawings_down_single_and_right_double(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick) / 2;

    vline_middle_down(LIGHT);

    hline(vmid, buf->width, hmid, thick);
    hline(vmid, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_down_double_and_right_single(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    hline_middle_right(LIGHT);

    vline(hmid, buf->height, vmid, thick);
    vline(hmid, buf->height, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_double_down_and_right(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    vline(hmid, buf->height, vmid, thick);
    vline(hmid + 2 * thick, buf->height, vmid + 2 * thick, thick);

    hline(vmid, buf->width, hmid, thick);
    hline(vmid + 2 * thick, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_down_single_and_left_double(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width + thick) / 2;

    vline_middle_down(LIGHT);

    hline(0, vmid, hmid, thick);
    hline(0, vmid, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_down_double_and_left_single(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    hline_middle_left(LIGHT);

    vline(hmid, buf->height, vmid, thick);
    vline(hmid, buf->height, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_double_down_and_left(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    vline(hmid + 2 * thick, buf->height, vmid, thick);
    vline(hmid, buf->height, vmid + 2 * thick, thick);

    hline(0, vmid + 2 * thick, hmid, thick);
    hline(0, vmid, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_up_single_and_right_double(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick) / 2;

    vline_middle_up(LIGHT);

    hline(vmid, buf->width, hmid, thick);
    hline(vmid, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_up_double_and_right_single(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height + thick) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    hline_middle_right(LIGHT);

    vline(0, hmid, vmid, thick);
    vline(0, hmid, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_double_up_and_right(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    vline(0, hmid + 2 * thick, vmid, thick);
    vline(0, hmid, vmid + 2 * thick, thick);

    hline(vmid + 2 * thick, buf->width, hmid, thick);
    hline(vmid, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_up_single_and_left_double(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width + thick) / 2;

    vline_middle_up(LIGHT);

    hline(0, vmid, hmid, thick);
    hline(0, vmid, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_up_double_and_left_single(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height + thick) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    hline_middle_left(LIGHT);

    vline(0, hmid, vmid, thick);
    vline(0, hmid, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_double_up_and_left(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    vline(0, hmid + 0 * thick + thick, vmid, thick);
    vline(0, hmid + 2 * thick + thick, vmid + 2 * thick, thick);

    hline(0, vmid, hmid, thick);
    hline(0, vmid + 2 * thick, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_vertical_single_and_right_double(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick) / 2;

    vline_middle(LIGHT);

    hline(vmid, buf->width, hmid, thick);
    hline(vmid, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_vertical_double_and_right_single(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int vmid = (buf->width - thick * 3) / 2;

    hline(vmid + 2 * thick, buf->width, (buf->height - thick) / 2, thick);

    vline(0, buf->height, vmid, thick);
    vline(0, buf->height, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_double_vertical_and_right(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    vline(0, buf->height, vmid, thick);
    vline(0, hmid, vmid + 2 * thick, thick);
    vline(hmid + 2 * thick, buf->height, vmid + 2 * thick, thick);

    hline(vmid + 2 * thick, buf->width, hmid, thick);
    hline(vmid + 2 * thick, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_vertical_single_and_left_double(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width + thick) / 2;

    vline_middle(LIGHT);

    hline(0, vmid, hmid, thick);
    hline(0, vmid, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_vertical_double_and_left_single(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int vmid = (buf->width - thick * 3) / 2;

    hline(0, vmid, (buf->height - thick) / 2, thick);

    vline(0, buf->height, vmid, thick);
    vline(0, buf->height, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_double_vertical_and_left(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    vline(0, buf->height, vmid + 2 * thick, thick);
    vline(0, hmid, vmid, thick);
    vline(hmid + 2 * thick, buf->height, vmid, thick);

    hline(0, vmid + thick, hmid, thick);
    hline(0, vmid, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_down_single_and_horizontal_double(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;

    vline(hmid + 2 * thick, buf->height, (buf->width - thick) / 2, thick);

    hline(0, buf->width, hmid, thick);
    hline(0, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_down_double_and_horizontal_single(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    hline_middle(LIGHT);

    vline(hmid, buf->height, vmid, thick);
    vline(hmid, buf->height, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_double_down_and_horizontal(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    hline(0, buf->width, hmid, thick);
    hline(0, vmid, hmid + 2 * thick, thick);
    hline(vmid + 2 * thick, buf->width, hmid + 2 * thick, thick);

    vline(hmid + 2 * thick, buf->height, vmid, thick);
    vline(hmid + 2 * thick, buf->height, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_up_single_and_horizontal_double(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick) / 2;

    vline(0, hmid, vmid, thick);

    hline(0, buf->width, hmid, thick);
    hline(0, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_up_double_and_horizontal_single(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    hline_middle(LIGHT);

    vline(0, hmid, vmid, thick);
    vline(0, hmid, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_double_up_and_horizontal(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    vline(0, hmid, vmid, thick);
    vline(0, hmid, vmid + 2 * thick, thick);

    hline(0, vmid + thick, hmid, thick);
    hline(vmid + 2 * thick, buf->width, hmid, thick);
    hline(0, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_vertical_single_and_horizontal_double(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;

    vline_middle(LIGHT);

    hline(0, buf->width, hmid, thick);
    hline(0, buf->width, hmid + 2 * thick, thick);
}

static void
draw_box_drawings_vertical_double_and_horizontal_single(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int vmid = (buf->width - thick * 3) / 2;

    hline_middle(LIGHT);

    vline(0, buf->height, vmid, thick);
    vline(0, buf->height, vmid + 2 * thick, thick);
}

static void
draw_box_drawings_double_vertical_and_horizontal(struct buf *buf)
{
    int thick = thickness(LIGHT);
    int hmid = (buf->height - thick * 3) / 2;
    int vmid = (buf->width - thick * 3) / 2;

    hline(0, vmid, hmid, thick);
    hline(vmid + 2 * thick, buf->width, hmid, thick);
    hline(0, vmid, hmid + 2 * thick, thick);
    hline(vmid + 2 * thick, buf->width, hmid + 2 * thick, thick);

    vline(0, hmid + thick, vmid, thick);
    vline(0, hmid, vmid + 2 * thick, thick);
    vline(hmid + 2 * thick, buf->height, vmid, thick);
    vline(hmid + 2 * thick, buf->height, vmid + 2 * thick, thick);
}

static inline void
set_a1_bit(uint8_t *data, size_t ofs, size_t bit_no)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
    data[ofs] |= 1 << bit_no;
#else
    data[ofs] |= 1 << (7 - bit_no);
#endif
}

static void NOINLINE
draw_box_drawings_light_arc(struct buf *buf, char32_t wc)
{
    const pixman_format_code_t fmt = buf->format;
    const int supersample = fmt == PIXMAN_a8 ? 4 : 1;
    const int height = buf->height * supersample;
    const int width = buf->width * supersample;
    const int stride = fmt == PIXMAN_a8
        ? stride_for_format_and_width(PIXMAN_a8, width) : buf->stride;
    uint8_t *data = supersample > 1 ? xcalloc(height * stride, 1) : buf->data;

    const int thick = thickness(LIGHT) * supersample;

    const int height_pixels = buf->height;
    const int width_pixels = buf->width;
    const int thick_pixels = thickness(LIGHT);

    /*
     * The general idea here is to connect the two incoming lines using a
     * circle, which is extended to the box-edges with vertical/horizontal
     * lines.
     * 
     * The radius of the quartercircle should be as big as possible, with some
     * restrictions: The radius should be the same for all of ╭ ╮ ╯ ╰ at a
     * given box-size (which won't be the case if we choose the biggest
     * possible radius for a given box, consider the following:)
     * 
     * 
     * ▕  ███   ▏
     * ▕a  │   d▔▔ 
     * ▕   │  x 
     * ▕   │    
     * ▕   │    ██
     * ▕   ╰────██
     * ▕        ██
     * ▕
     * ▕
     * ▕b      c
     *  ▔▔▔▔▔▔▔▔▔▔
     * for ╰ it would be possible to center the circle on the upper right
     * corner of d, but we have set it on x instead because ╯ can only use a
     * 2px inner radius:
     * 
     *  ▕  ███   ▏
     * ▔▔a  │   d▏
     *   x  │    ▏
     *      │    ▏
     * ██   │    ▏
     * ██───╯    ▏
     * ██        ▏
     *           ▏
     *           ▏
     *   b      c▏
     * ▔▔▔▔▔▔▔▔▔▔
     * As the incoming lines always exactly fill pixels, and are rounded down
     * (via float->int), we can use this to get the radius of the inner
     * (connecting the left/upper edge of the lines) quartercircle.
     */
    int circle_inner_edge = (min(width_pixels, height_pixels) - thick_pixels) / 2;

    /*
     * We want to draw the quartercircle by filling small circles (with r =
     * thickness/2.) whose centers are on its edge. This means to get the
     * radius of the quartercircle, we add the exact half thickness to the
     * radius of the inner circle.
     */
    double c_r = circle_inner_edge + thick_pixels/2.;

    /*
     * We need to draw short lines from the end of the quartercircle to the
     * box-edges, store one endpoint (the other is the edge of the
     * quartercircle) in these vars.
     */
    int vert_to = 0,
         hor_to = 0;

    /* Coordinates of the circle-center. */
    int c_x = 0,
        c_y = 0;

    /*
     * For a given y there are up to two solutions for the circle-equation.
     * Set to -1 for the left, and 1 for the right hemisphere.
     */
    int circle_hemisphere = 0;

    /*
     * The quarter circle only has to be evaluated for a small range of
     * y-values.
     */
    int y_min = 0,
        y_max = 0;

    switch (wc) {
        case  U'╭': {
            /*
             * Don't use supersampled coordinates yet, we want to align actual
             * pixels.
             *
             * pixel-coordinates of the lower edge of the right line and the
             * right edge of the bottom line.
             */
            int right_bottom_edge = (height_pixels + thick_pixels) / 2;
            int bottom_right_edge = (width_pixels + thick_pixels) / 2;

            /* find coordinates of circle-center. */
            c_y = right_bottom_edge + circle_inner_edge;
            c_x = bottom_right_edge + circle_inner_edge;

            /* we want to render the left, not the right hemisphere of the circle. */
            circle_hemisphere = -1;

            /* don't evaluate beyond c_y, the vertical line is drawn there. */
            y_min = 0;
            y_max = c_y;

            /*
             * the vertical line should extend to the bottom of the box, the
             * horizontal to the right.
             */
            vert_to = height_pixels;
            hor_to = width_pixels;

            break;
        }
        case U'╮': {
            int left_bottom_edge = (height_pixels + thick_pixels) / 2;
            int bottom_left_edge = (width_pixels - thick_pixels) / 2;

            c_y = left_bottom_edge + circle_inner_edge;
            c_x = bottom_left_edge - circle_inner_edge;

            circle_hemisphere = 1;

            y_min = 0;
            y_max = c_y;

            vert_to = height_pixels;
            hor_to = 0;

            break;
        }
        case U'╰': {
            int right_top_edge = (height_pixels - thick_pixels) / 2;
            int top_right_edge = (width_pixels + thick_pixels) / 2;

            c_y = right_top_edge - circle_inner_edge;
            c_x = top_right_edge + circle_inner_edge;

            circle_hemisphere = -1;

            y_min = c_y;
            y_max = height_pixels;

            vert_to = 0;
            hor_to = width_pixels;

            break;
        }
        case U'╯': {
            int left_top_edge = (height_pixels - thick_pixels) / 2;
            int top_left_edge = (width_pixels - thick_pixels) / 2;

            c_y = left_top_edge - circle_inner_edge;
            c_x = top_left_edge - circle_inner_edge;

            circle_hemisphere = 1;

            y_min = c_y;
            y_max = height_pixels;

            vert_to = 0;
            hor_to = 0;

            break;
        }
    }

    /* store for horizontal+vertical line. */
    int c_x_pixels = c_x;
    int c_y_pixels = c_y;

    /* Bring coordinates from pixel-grid to supersampled grid. */
    c_r *= supersample;
    c_x *= supersample;
    c_y *= supersample;

    y_min *= supersample;
    y_max *= supersample;

    double c_r2 = c_r * c_r;

    /*
     * To prevent gaps in the circle, each pixel is sampled multiple times.
     * As the quartercircle ends (vertically) in the middle of a pixel, an
     * uneven number helps hit that exactly.
     */
    for (double i = y_min*16; i <= y_max*16; i++) {
        errno = 0;

        double y = i / 16.;
        double x = circle_hemisphere * sqrt(c_r2 - (y - c_y) * (y - c_y)) + c_x;

        /* See math_error(7) */
        if (errno != 0)
        {
            continue;
        }

        const int row = round(y);
        const int col = round(x);

        if (col < 0)
            continue;

        /* rectangle big enough to fit entire circle with radius thick/2. */
        int row1 = row - (thick/2+1);
        int row2 = row + (thick/2+1);
        int col1 = col - (thick/2+1);
        int col2 = col + (thick/2+1);

        int row_start = min(row1, row2),
            row_end   = max(row1, row2),
            col_start = min(col1, col2),
            col_end   = max(col1, col2);

        xassert(row_end > row_start);
        xassert(col_end > col_start);

        /*
         * draw circle with radius thick/2 around x,y.
         * this is accomplished by rejecting pixels where the distance from
         * their center to x,y is greater than thick/2.
         */
        for (int r = max(row_start, 0); r < max(min(row_end, height), 0); r++) {
            double r_midpoint = r + 0.5;
            for (int c = max(col_start, 0); c < max(min(col_end, width), 0); c++) {
                double c_midpoint = c + 0.5;

                /* vector from point on quartercircle to midpoint of the current pixel. */
                double center_midpoint_x = c_midpoint - x;
                double center_midpoint_y = r_midpoint - y;

                /* distance from current point to circle-center. */
                double dist = sqrt(center_midpoint_x * center_midpoint_x + center_midpoint_y * center_midpoint_y);
                /* skip if midpoint of pixel is outside the circle. */
                if (dist > thick/2.)
                    continue;
                if (fmt == PIXMAN_a1) {
                    size_t idx = c / 8;
                    size_t bit_no = c % 8;
                    set_a1_bit(data, r * stride + idx, bit_no);
                } else
                    data[r * stride + c] = 0xff;
            }
        }
    }

    if (fmt == PIXMAN_a8) {
        xassert(data != buf->data);

        /* Downsample */
        for (size_t r = 0; r < buf->height; r++) {
            for (size_t c = 0; c < buf->width; c++) {
                uint32_t total = 0;
                for (size_t i = 0; i < supersample; i++) {
                    for (size_t j = 0; j < supersample; j++)
                        total += data[(r * supersample + i) * stride + c * supersample + j];
                }
                uint8_t average = min(total / (supersample * supersample), 0xff);
                buf->data[r * buf->stride + c] = average;
            }
        }

        free(data);
    }

    /* draw vertical/horizontal lines from quartercircle-edge to box-edge. */
    vline(min(c_y_pixels, vert_to), max(c_y_pixels, vert_to), (width_pixels - thick_pixels) / 2, thick_pixels);
    hline(min(c_x_pixels, hor_to), max(c_x_pixels, hor_to), (height_pixels - thick_pixels) / 2, thick_pixels);
}

static void NOINLINE
draw_box_drawings_light_diagonal_upper_right_to_lower_left(struct buf *buf)
{
    pixman_trapezoid_t trap = {
        .top = pixman_int_to_fixed(0),
        .bottom = pixman_int_to_fixed(buf->height),
        .left = {
            .p1 = {
                .x = pixman_double_to_fixed(buf->width - thickness(LIGHT) / 2.),
                .y = pixman_int_to_fixed(0),
            },
            .p2 = {
                .x = pixman_double_to_fixed(0 - thickness(LIGHT) / 2.),
                .y = pixman_int_to_fixed(buf->height),
            },
        },
        .right = {
            .p1 = {
                .x = pixman_double_to_fixed(buf->width + thickness(LIGHT) / 2.),
                .y = pixman_int_to_fixed(0),
            },
            .p2 = {
                .x = pixman_double_to_fixed(0 + thickness(LIGHT) / 2.),
                .y = pixman_int_to_fixed(buf->height),
            },
        },
    };

    pixman_rasterize_trapezoid(buf->pix, &trap, 0, 0);
}

static void NOINLINE
draw_box_drawings_light_diagonal_upper_left_to_lower_right(struct buf *buf)
{
    pixman_trapezoid_t trap = {
        .top = pixman_int_to_fixed(0),
        .bottom = pixman_int_to_fixed(buf->height),
        .left = {
            .p1 = {
                .x = pixman_double_to_fixed(0 - thickness(LIGHT) / 2.),
                .y = pixman_int_to_fixed(0),
            },
            .p2 = {
                .x = pixman_double_to_fixed(buf->width - thickness(LIGHT) / 2.),
                .y = pixman_int_to_fixed(buf->height),
            },
        },
        .right = {
            .p1 = {
                .x = pixman_double_to_fixed(0 + thickness(LIGHT) / 2.),
                .y = pixman_int_to_fixed(0),
            },
            .p2 = {
                .x = pixman_double_to_fixed(buf->width + thickness(LIGHT) / 2.),
                .y = pixman_int_to_fixed(buf->height),
            },
        },
    };

    pixman_rasterize_trapezoid(buf->pix, &trap, 0, 0);
}

static void
draw_box_drawings_light_diagonal_cross(struct buf *buf)
{
    draw_box_drawings_light_diagonal_upper_right_to_lower_left(buf);
    draw_box_drawings_light_diagonal_upper_left_to_lower_right(buf);
}

static void
draw_box_drawings_light_left(struct buf *buf)
{
    hline_middle_left(LIGHT);
}

static void
draw_box_drawings_light_up(struct buf *buf)
{
    vline_middle_up(LIGHT);
}

static void
draw_box_drawings_light_right(struct buf *buf)
{
    hline_middle_right(LIGHT);
}

static void
draw_box_drawings_light_down(struct buf *buf)
{
    vline_middle_down(LIGHT);
}

static void
draw_box_drawings_heavy_left(struct buf *buf)
{
    hline_middle_left(HEAVY);
}

static void
draw_box_drawings_heavy_up(struct buf *buf)
{
    vline_middle_up(HEAVY);
}

static void
draw_box_drawings_heavy_right(struct buf *buf)
{
    hline_middle_right(HEAVY);
}

static void
draw_box_drawings_heavy_down(struct buf *buf)
{
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_light_left_and_heavy_right(struct buf *buf)
{
    hline_middle_left(LIGHT);
    hline_middle_right(HEAVY);
}

static void
draw_box_drawings_light_up_and_heavy_down(struct buf *buf)
{
    vline_middle_up(LIGHT);
    vline_middle_down(HEAVY);
}

static void
draw_box_drawings_heavy_left_and_light_right(struct buf *buf)
{
    hline_middle_left(HEAVY);
    hline_middle_right(LIGHT);
}

static void
draw_box_drawings_heavy_up_and_light_down(struct buf *buf)
{
    vline_middle_up(HEAVY);
    vline_middle_down(LIGHT);
}

static void
draw_upper_half_block(struct buf *buf)
{
    rect(0, 0, buf->width, round(buf->height / 2.));
}

static void
draw_lower_one_eighth_block(struct buf *buf)
{
    rect(0, buf->height - round(buf->height / 8.), buf->width, buf->height);
}

static void
draw_lower_one_quarter_block(struct buf *buf)
{
    rect(0, buf->height - round(buf->height / 4.), buf->width, buf->height);
}

static void
draw_lower_three_eighths_block(struct buf *buf)
{
    rect(0, buf->height - round(3. * buf->height / 8.), buf->width, buf->height);
}

static void
draw_lower_half_block(struct buf *buf)
{
    rect(0, buf->height - round(buf->height / 2.), buf->width, buf->height);
}

static void
draw_lower_five_eighths_block(struct buf *buf)
{
    rect(0, buf->height - round(5. * buf->height / 8.), buf->width, buf->height);
}

static void
draw_lower_three_quarters_block(struct buf *buf)
{
    rect(0, buf->height - round(3. * buf->height / 4.), buf->width, buf->height);
}

static void
draw_lower_seven_eighths_block(struct buf *buf)
{
    rect(0, buf->height - round(7. * buf->height / 8.), buf->width, buf->height);
}

static void
draw_upper_one_quarter_block(struct buf *buf)
{
    rect(0, 0, buf->width, round(buf->height / 4.));
}

static void
draw_upper_three_eighths_block(struct buf *buf)
{
    rect(0, 0, buf->width, round(3. * buf->height / 8.));
}

static void
draw_upper_five_eighths_block(struct buf *buf)
{
    rect(0, 0, buf->width, round(5. * buf->height / 8.));
}

static void
draw_upper_three_quarters_block(struct buf *buf)
{
    rect(0, 0, buf->width, round(3. * buf->height / 4.));
}

static void
draw_upper_seven_eighths_block(struct buf *buf)
{
    rect(0, 0, buf->width, round(7. * buf->height / 8.));
}

static void
draw_full_block(struct buf *buf)
{
    rect(0, 0, buf->width, buf->height);
}

static void
draw_left_seven_eighths_block(struct buf *buf)
{
    rect(0, 0, round(7. * buf->width / 8.), buf->height);
}

static void
draw_left_three_quarters_block(struct buf *buf)
{
    rect(0, 0, round(3. * buf->width / 4.), buf->height);
}

static void
draw_left_five_eighths_block(struct buf *buf)
{
    rect(0, 0, round(5. * buf->width / 8.), buf->height);
}

static void
draw_left_half_block(struct buf *buf)
{
    rect(0, 0, round(buf->width / 2.), buf->height);
}

static void
draw_left_three_eighths_block(struct buf *buf)
{
    rect(0, 0, round(3. * buf->width / 8.), buf->height);
}

static void
draw_left_one_quarter_block(struct buf *buf)
{
    rect(0, 0, round(buf->width / 4.), buf->height);
}

static void
draw_vertical_one_eighth_block_n(struct buf *buf, int n)
{
    double x = round((double)n * buf->width / 8.);
    double w = round(buf->width / 8.);
    rect(x, 0, x + w, buf->height);
}

static void
draw_left_one_eighth_block(struct buf *buf)
{
    draw_vertical_one_eighth_block_n(buf, 0);
}

static void
draw_vertical_one_eighth_block_2(struct buf *buf)
{
    draw_vertical_one_eighth_block_n(buf, 1);
}

static void
draw_vertical_one_eighth_block_3(struct buf *buf)
{
    draw_vertical_one_eighth_block_n(buf, 2);
}

static void
draw_vertical_one_eighth_block_4(struct buf *buf)
{
    draw_vertical_one_eighth_block_n(buf, 3);
}

static void
draw_vertical_one_eighth_block_5(struct buf *buf)
{
    draw_vertical_one_eighth_block_n(buf, 4);
}

static void
draw_vertical_one_eighth_block_6(struct buf *buf)
{
    draw_vertical_one_eighth_block_n(buf, 5);
}

static void
draw_vertical_one_eighth_block_7(struct buf *buf)
{
    draw_vertical_one_eighth_block_n(buf, 6);
}

static void
draw_right_half_block(struct buf *buf)
{
    rect(round(buf->width / 2.), 0, buf->width, buf->height);
}

static void NOINLINE
draw_pixman_shade(struct buf *buf, uint16_t v)
{
    pixman_color_t shade = {.red = 0, .green = 0, .blue = 0, .alpha = v};
    pixman_image_fill_rectangles(
        PIXMAN_OP_SRC, buf->pix, &shade, 1,
        (pixman_rectangle16_t []){{0, 0, buf->width, buf->height}});
}

static void
draw_light_shade(struct buf *buf)
{
    pixman_format_code_t fmt = buf->format;

    if (buf->solid_shades && fmt == PIXMAN_a1)
        change_buffer_format(buf, PIXMAN_a8);
    else if (!buf->solid_shades && fmt == PIXMAN_a8)
        change_buffer_format(buf, PIXMAN_a1);

    if (buf->solid_shades)
        draw_pixman_shade(buf, 0x4000);
    else {
        for (size_t row = 0; row < buf->height; row += 2) {
            for (size_t col = 0; col < buf->width; col += 2) {
                size_t idx = col / 8;
                size_t bit_no = col % 8;
                set_a1_bit(buf->data, row * buf->stride + idx, bit_no);
            }
        }
    }
}

static void
draw_medium_shade(struct buf *buf)
{
    pixman_format_code_t fmt = buf->format;

    if (buf->solid_shades && fmt == PIXMAN_a1)
        change_buffer_format(buf, PIXMAN_a8);
    else if (!buf->solid_shades && fmt == PIXMAN_a8)
        change_buffer_format(buf, PIXMAN_a1);

    if (buf->solid_shades)
        draw_pixman_shade(buf, 0x8000);
    else {
        for (size_t row = 0; row < buf->height; row++) {
            for (size_t col = row % 2; col < buf->width; col += 2) {
                size_t idx = col / 8;
                size_t bit_no = col % 8;
                set_a1_bit(buf->data, row * buf->stride + idx, bit_no);
            }
        }
    }
}

static void
draw_dark_shade(struct buf *buf)
{
    pixman_format_code_t fmt = buf->format;

    if (buf->solid_shades && fmt == PIXMAN_a1)
        change_buffer_format(buf, PIXMAN_a8);
    else if (!buf->solid_shades && fmt == PIXMAN_a8)
        change_buffer_format(buf, PIXMAN_a1);

    if (buf->solid_shades)
        draw_pixman_shade(buf, 0xc000);
    else {
        for (size_t row = 0; row < buf->height; row++) {
            for (size_t col = 0; col < buf->width; col += 1 + row % 2) {
                size_t idx = col / 8;
                size_t bit_no = col % 8;
                set_a1_bit(buf->data, row * buf->stride + idx, bit_no);
            }
        }
    }
}

static void NOINLINE
draw_horizontal_one_eighth_block_n(struct buf *buf, int n)
{
    double y = round((double)n * buf->height / 8.);
    double h = round(buf->height / 8.);
    rect(0, y, buf->width, y + h);
}

static void
draw_upper_one_eighth_block(struct buf *buf)
{
    draw_horizontal_one_eighth_block_n(buf, 0);
}

static void
draw_horizontal_one_eighth_block_2(struct buf *buf)
{
    draw_horizontal_one_eighth_block_n(buf, 1);
}

static void
draw_horizontal_one_eighth_block_3(struct buf *buf)
{
    draw_horizontal_one_eighth_block_n(buf, 2);
}

static void
draw_horizontal_one_eighth_block_4(struct buf *buf)
{
    draw_horizontal_one_eighth_block_n(buf, 3);
}

static void
draw_horizontal_one_eighth_block_5(struct buf *buf)
{
    draw_horizontal_one_eighth_block_n(buf, 4);
}

static void
draw_horizontal_one_eighth_block_6(struct buf *buf)
{
    draw_horizontal_one_eighth_block_n(buf, 5);
}

static void
draw_horizontal_one_eighth_block_7(struct buf *buf)
{
    draw_horizontal_one_eighth_block_n(buf, 6);
}

static void
draw_right_one_eighth_block(struct buf *buf)
{
    rect(buf->width - round(buf->width / 8.), 0, buf->width, buf->height);
}

static void
quad_upper_left(struct buf *buf)
{
    rect(0, 0, ceil(buf->width / 2.), ceil(buf->height / 2.));
}

static void
quad_upper_right(struct buf *buf)
{
    rect(floor(buf->width / 2.), 0, buf->width, ceil(buf->height / 2.));
}

static void
quad_lower_left(struct buf *buf)
{
    rect(0, floor(buf->height / 2.), ceil(buf->width / 2.), buf->height);
}

static void
quad_lower_right(struct buf *buf)
{
    rect(floor(buf->width / 2.), floor(buf->height / 2.), buf->width, buf->height);
}

static void NOINLINE
draw_quadrant(struct buf *buf, char32_t wc)
{
    enum {
        UPPER_LEFT = 1 << 0,
        UPPER_RIGHT = 1 << 1,
        LOWER_LEFT = 1 << 2,
        LOWER_RIGHT = 1 << 3,
    };

    static const uint8_t matrix[10] = {
        LOWER_LEFT,
        LOWER_RIGHT,
        UPPER_LEFT,
        UPPER_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT,
        UPPER_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | LOWER_LEFT | LOWER_RIGHT,
    };

    xassert(wc >= 0x2596 && wc <= 0x259f);
    const size_t idx = wc - 0x2596;

    xassert(idx < ALEN(matrix));
    uint8_t encoded = matrix[idx];

    if (encoded & UPPER_LEFT)
        quad_upper_left(buf);

    if (encoded & UPPER_RIGHT)
        quad_upper_right(buf);

    if (encoded & LOWER_LEFT)
        quad_lower_left(buf);

    if (encoded & LOWER_RIGHT)
        quad_lower_right(buf);
}

static void NOINLINE
draw_braille(struct buf *buf, char32_t wc)
{
    int w = min(buf->width / 4, buf->height / 8);
    int x_spacing = buf->width / 4;
    int y_spacing = buf->height / 8;
    int x_margin = x_spacing / 2;
    int y_margin = y_spacing / 2;

    int x_px_left = buf->width - 2 * x_margin - x_spacing - 2 * w;
    int y_px_left = buf->height - 2 * y_margin - 3 * y_spacing - 4 * w;

    LOG_DBG(
        "braille: before adjusting: "
        "cell: %dx%d, margin=%dx%d, spacing=%dx%d, width=%d, left=%dx%d",
        buf->width, buf->height, x_margin, y_margin, x_spacing, y_spacing,
        w, x_px_left, y_px_left);

    /* First, try hard to ensure the DOT width is non-zero */
    if (x_px_left >= 2 && y_px_left >= 4 && w == 0) {
        w++;
        x_px_left -= 2;
        y_px_left -= 4;
    }

    /* Second, prefer a non-zero margin */
    if (x_px_left >= 2 && x_margin == 0) { x_margin = 1; x_px_left -= 2; }
    if (y_px_left >= 2 && y_margin == 0) { y_margin = 1; y_px_left -= 2; }

    /* Third, increase spacing */
    if (x_px_left >= 1) { x_spacing++; x_px_left--; }
    if (y_px_left >= 3) { y_spacing++; y_px_left -= 3; }

    /* Fourth, margins ("spacing", but on the sides) */
    if (x_px_left >= 2) { x_margin++; x_px_left -= 2; }
    if (y_px_left >= 2) { y_margin++; y_px_left -= 2; }

    /* Last - increase dot width */
    if (x_px_left >= 2 && y_px_left >= 4) {
        w++;
        x_px_left -= 2;
        y_px_left -= 4;
    }

    LOG_DBG(
        "braille: after adjusting: "
        "cell: %dx%d, margin=%dx%d, spacing=%dx%d, width=%d, left=%dx%d",
        buf->width, buf->height, x_margin, y_margin, x_spacing, y_spacing,
        w, x_px_left, y_px_left);

    xassert(x_px_left <= 1 || y_px_left <= 1);
    xassert(2 * x_margin + 2 * w + x_spacing <= buf->width);
    xassert(2 * y_margin + 4 * w + 3 * y_spacing <= buf->height);

    int x[2], y[4];
    x[0] = x_margin;
    x[1] = x_margin + w + x_spacing;
    y[0] = y_margin;
    y[1] = y[0] + w + y_spacing;
    y[2] = y[1] + w + y_spacing;
    y[3] = y[2] + w + y_spacing;

    assert(wc >= 0x2800);
    assert(wc <= 0x28ff);
    uint8_t sym = wc - 0x2800;

    /* Left side */
    if (sym & 1)
        rect(x[0], y[0], x[0] + w, y[0] + w);
    if (sym & 2)
        rect(x[0], y[1], x[0] + w, y[1] + w);
    if (sym & 4)
        rect(x[0], y[2], x[0] + w, y[2] + w);

    /* Right side */
    if (sym & 8)
        rect(x[1], y[0], x[1] + w, y[0] + w);
    if (sym & 16)
        rect(x[1], y[1], x[1] + w, y[1] + w);
    if (sym & 32)
        rect(x[1], y[2], x[1] + w, y[2] + w);

    /* 8-dot patterns */
    if (sym & 64)
        rect(x[0], y[3], x[0] + w, y[3] + w);
    if (sym & 128)
        rect(x[1], y[3], x[1] + w, y[3] + w);
}

static void
sextant_upper_left(struct buf *buf)
{
    rect(0, 0, buf->x_halfs[0], buf->y_thirds[0]);
}

static void
sextant_middle_left(struct buf *buf)
{
    rect(0, buf->y_thirds[0], buf->x_halfs[0], buf->y_thirds[1]);
}

static void
sextant_lower_left(struct buf *buf)
{
    rect(0, buf->y_thirds[1], buf->x_halfs[0], buf->height);
}

static void
sextant_upper_right(struct buf *buf)
{
    rect(buf->x_halfs[1], 0, buf->width, buf->y_thirds[0]);
}

static void
sextant_middle_right(struct buf *buf)
{
    rect(buf->x_halfs[1], buf->y_thirds[0], buf->width, buf->y_thirds[1]);
}

static void
sextant_lower_right(struct buf *buf)
{
    rect(buf->x_halfs[1], buf->y_thirds[1], buf->width, buf->height);
}

static void NOINLINE
draw_sextant(struct buf *buf, char32_t wc)
{
    /*
     * Each byte encodes one sextant:
     *
     * Bit     sextant
     *   0      upper left
     *   1      middle left
     *   2      lower left
     *   3      upper right
     *   4      middle right
     *   5      lower right
     */
    enum {
        UPPER_LEFT = 1 << 0,
        MIDDLE_LEFT = 1 << 1,
        LOWER_LEFT = 1 << 2,
        UPPER_RIGHT = 1 << 3,
        MIDDLE_RIGHT = 1 << 4,
        LOWER_RIGHT = 1 << 5,
    };

    /* TODO: move this to a separate file? */
    static const uint8_t matrix[60] = {
        /* U+1fb00 - U+1fb0f */
        UPPER_LEFT,
        UPPER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT,
        MIDDLE_LEFT,
        UPPER_LEFT | MIDDLE_LEFT,
        UPPER_RIGHT | MIDDLE_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_LEFT,
        MIDDLE_RIGHT,
        UPPER_LEFT | MIDDLE_RIGHT,
        UPPER_RIGHT | MIDDLE_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_RIGHT,
        MIDDLE_LEFT | MIDDLE_RIGHT,
        UPPER_LEFT | MIDDLE_LEFT | MIDDLE_RIGHT,
        UPPER_RIGHT | MIDDLE_LEFT | MIDDLE_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_LEFT | MIDDLE_RIGHT,
        LOWER_LEFT,

        /* U+1fb10 - U+1fb1f */
        UPPER_LEFT | LOWER_LEFT,
        UPPER_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | LOWER_LEFT,
        MIDDLE_LEFT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_LEFT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_LEFT | LOWER_LEFT,
        MIDDLE_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_RIGHT | LOWER_LEFT,
        MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_LEFT,
        LOWER_RIGHT,
        UPPER_LEFT | LOWER_RIGHT,

        /* U+1fb20 - U+1fb2f */
        UPPER_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | LOWER_RIGHT,
        MIDDLE_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_LEFT | LOWER_RIGHT,
        MIDDLE_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_RIGHT | LOWER_RIGHT,
        MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_RIGHT,
        LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | LOWER_LEFT | LOWER_RIGHT,

        /* U+1fb30 - U+1fb3b */
        UPPER_LEFT | UPPER_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_LEFT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_LEFT | MIDDLE_RIGHT | LOWER_LEFT | LOWER_RIGHT,
    };

    xassert(wc >= 0x1fb00 && wc <= 0x1fb3b);
    const size_t idx = wc - 0x1fb00;

    xassert(idx < ALEN(matrix));
    uint8_t encoded = matrix[idx];

    if (encoded & UPPER_LEFT)
        sextant_upper_left(buf);

    if (encoded & MIDDLE_LEFT)
        sextant_middle_left(buf);

    if (encoded & LOWER_LEFT)
        sextant_lower_left(buf);

    if (encoded & UPPER_RIGHT)
        sextant_upper_right(buf);

    if (encoded & MIDDLE_RIGHT)
        sextant_middle_right(buf);

    if (encoded & LOWER_RIGHT)
        sextant_lower_right(buf);
}

static void
octant_upper_left(struct buf *buf)
{
    rect(0, 0, buf->x_halfs[0], buf->y_quads[0]);
}

static void
octant_middle_up_left(struct buf *buf)
{
    rect(0, buf->y_quads[0], buf->x_halfs[0], buf->y_quads[1]);
}

static void
octant_middle_down_left(struct buf *buf)
{
    rect(0, buf->y_quads[1], buf->x_halfs[0], buf->y_quads[2]);
}

static void
octant_lower_left(struct buf *buf)
{
    rect(0, buf->y_quads[2], buf->x_halfs[0], buf->height);
}

static void
octant_upper_right(struct buf *buf)
{
    rect(buf->x_halfs[1], 0, buf->width, buf->y_quads[0]);
}

static void
octant_middle_up_right(struct buf *buf)
{
    rect(buf->x_halfs[1], buf->y_quads[0], buf->width, buf->y_quads[1]);
}

static void
octant_middle_down_right(struct buf *buf)
{
    rect(buf->x_halfs[1], buf->y_quads[1], buf->width, buf->y_quads[2]);
}

static void
octant_lower_right(struct buf *buf)
{
    rect(buf->x_halfs[1], buf->y_quads[2], buf->width, buf->height);
}

static void NOINLINE
draw_octant(struct buf *buf, char32_t wc)
{
    /*
     * Each byte encodes one octant:
     *
     * Bit      octant part
     *   0      upper left
     *   1      middle, upper left
     *   2      middle, lower left
     *   3      lower, left
     *   4      upper right
     *   5      middle, upper right
     *   6      middle, lower right
     *   7      lower right
     */
    enum {
        UPPER_LEFT = 1 << 0,
        MIDDLE_UP_LEFT = 1 << 1,
        MIDDLE_DOWN_LEFT = 1 << 2,
        LOWER_LEFT = 1 << 3,
        UPPER_RIGHT = 1 << 4,
        MIDDLE_UP_RIGHT = 1 << 5,
        MIDDLE_DOWN_RIGHT = 1 << 6,
        LOWER_RIGHT = 1 << 7,
    };

    /* TODO: move this to a separate file */
    static const uint8_t matrix[230] = {
        /* U+1CD00 - U+1CD0F */
        MIDDLE_UP_LEFT,
        MIDDLE_UP_LEFT | UPPER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | UPPER_RIGHT,
        MIDDLE_UP_RIGHT,
        UPPER_LEFT | MIDDLE_UP_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT,
        MIDDLE_DOWN_LEFT,
        UPPER_LEFT | MIDDLE_DOWN_LEFT,
        UPPER_RIGHT | MIDDLE_DOWN_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_LEFT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT,

        /* U+1CD10 - U+1CD1F */
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT,
        MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_RIGHT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT,

        /* U+1CD20 - U+1CD2F */
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT,
        MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,

        /* U+1CD30 - U+1CD3F */
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT,
        UPPER_LEFT | LOWER_LEFT,
        UPPER_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | LOWER_LEFT,
        MIDDLE_UP_LEFT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_LEFT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | LOWER_LEFT,
        MIDDLE_UP_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | LOWER_LEFT,

        /* U+1CD40 - U+1CD4F */
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | LOWER_LEFT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT,

        /* U+1CD50 - U+1CD5F */
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT,
        MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,

        /* U+1CD60 - U+1CD6F */
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,

        /* U+1CD70 - U+1CD7F */
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT,
        UPPER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | LOWER_RIGHT,
        MIDDLE_UP_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_RIGHT,

        /* U+1CD80 - U+1CD8F */
        MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_RIGHT,

        /* U+1CD90 - U+1CD9F */
        UPPER_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,

        /* U+1CDA0 - U+1CDAF */
        MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_RIGHT,
        UPPER_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | LOWER_LEFT | LOWER_RIGHT,

        /* U+1CDB0 - U+1CDBF */
        UPPER_LEFT | MIDDLE_UP_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,

        /* U+1CDC0 - U+1CDCF */
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,

        /* U+1CDD0 - U+1CDDF */
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,

        /* U+1CDE0 - U+1CDE5 */
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | UPPER_RIGHT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_LEFT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
        UPPER_RIGHT | MIDDLE_UP_LEFT | MIDDLE_UP_RIGHT | MIDDLE_DOWN_LEFT | MIDDLE_DOWN_RIGHT | LOWER_LEFT | LOWER_RIGHT,
    };

    _Static_assert(ALEN(matrix) == 230, "incorrect number of codepoints");

#if defined(_DEBUG)
    const size_t last_implemented = 0x1cde5;
    for (size_t i = 0; i < sizeof(matrix) / sizeof(matrix[0]); i++) {
        if (i + 0x1cd00 > last_implemented)
            break;

        for (size_t j = 0; j < sizeof(matrix) / sizeof(matrix[0]); j++) {
            if (j + 0x1cd00 > last_implemented)
                break;

            if (i == j)
                continue;

            if (matrix[i] == matrix[j]) {
                BUG("octant U+%05x (idx=%zu) is the same as U+%05x (idx=%zu)",
                    matrix[i], i, matrix[j], j);
            }
        }
    }
#endif

    xassert(wc >= 0x1cd00 && wc <= 0x1cde5);
    const size_t idx = wc - 0x1cd00;

    xassert(idx < ALEN(matrix));
    uint8_t encoded = matrix[idx];

    if (encoded & UPPER_LEFT)
        octant_upper_left(buf);

    if (encoded & MIDDLE_UP_LEFT)
        octant_middle_up_left(buf);

    if (encoded & MIDDLE_DOWN_LEFT)
        octant_middle_down_left(buf);

    if (encoded & LOWER_LEFT)
        octant_lower_left(buf);

    if (encoded & UPPER_RIGHT)
        octant_upper_right(buf);

    if (encoded & MIDDLE_UP_RIGHT)
        octant_middle_up_right(buf);

    if (encoded & MIDDLE_DOWN_RIGHT)
        octant_middle_down_right(buf);

    if (encoded & LOWER_RIGHT)
        octant_lower_right(buf);
}

static void NOINLINE
draw_wedge_triangle(struct buf *buf, char32_t wc)
{
    const int width = buf->width;
    const int height = buf->height;

    int halfs0 = buf->x_halfs[0];
    int halfs1 = buf->x_halfs[1];
    int thirds0 = buf->y_thirds[0];
    int thirds1 = buf->y_thirds[1];

    int p1_x, p1_y, p2_x, p2_y, p3_x, p3_y;

    switch (wc) {
    case 0x1fb3c:  /* 🬼 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = thirds1; p2_y = p3_y = height;
        break;

    case 0x1fb52:  /* 🭒 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = thirds1; p2_y = p3_y = height;
        break;

    case 0x1fb3d:  /* 🬽 */
        p1_x = p2_x = 0; p3_x = width;
        p1_y = thirds1; p2_y = p3_y = height;
        break;

    case 0x1fb53:  /* 🭓 */
        p1_x = p2_x = 0; p3_x = width;
        p1_y = thirds1; p2_y = p3_y = height;
        break;

    case 0x1fb3e: /* 🬾 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = thirds0; p2_y = p3_y = height;
        break;

    case 0x1fb54: /* 🭔 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = thirds0; p2_y = p3_y = height;
        break;

    case 0x1fb3f: /* 🬿 */
        p1_x = p2_x = 0; p3_x = width;
        p1_y = thirds0; p2_y = p3_y = height;
        break;

    case 0x1fb55: /* 🭕 */
        p1_x = p2_x = 0; p3_x = width;
        p1_y = thirds0; p2_y = p3_y = height;
        break;

    case 0x1fb40:  /* 🭀 */
    case 0x1fb56:  /* 🭖 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = 0; p2_y = p3_y = height;
        break;

    case 0x1fb47:  /* 🭇 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = thirds1; p2_y = p3_y = height;
        break;

    case 0x1fb5d:  /* 🭝 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = thirds1; p2_y = p3_y = height;
        break;

    case 0x1fb48:  /* 🭈 */
        p1_x = p2_x = width; p3_x = 0;
        p1_y = thirds1; p2_y = p3_y = height;
        break;

    case 0x1fb5e:  /* 🭞 */
        p1_x = p2_x = width; p3_x = 0;
        p1_y = thirds1; p2_y = p3_y = height;
        break;

    case 0x1fb49:  /* 🭉 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = thirds0; p2_y = p3_y = height;
        break;

    case 0x1fb5f:  /* 🭟 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = thirds0; p2_y = p3_y = height;
        break;

    case 0x1fb4a:  /* 🭊 */
        p1_x = p2_x = width; p3_x = 0;
        p1_y = thirds0; p2_y = p3_y = height;
        break;

    case 0x1fb60:  /* 🭠 */
        p1_x = p2_x = width; p3_x = 0;
        p1_y = thirds0; p2_y = p3_y = height;
        break;

    case 0x1fb4b:  /* 🭋 */
    case 0x1fb61:  /* 🭡 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = 0; p2_y = p3_y = height;
        break;

    case 0x1fb57:  /* 🭗 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = p3_y = 0; p2_y = thirds0;
        break;

    case 0x1fb41:  /* 🭁 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = p3_y = 0; p2_y = thirds0;
        break;

    case 0x1fb58:  /* 🭘 */
        p1_x = p2_x = 0; p3_x = width;
        p1_y = p3_y = 0; p2_y = thirds0;
        break;

    case 0x1fb42:  /* 🭂 */
        p1_x = p2_x = 0; p3_x = width;
        p1_y = p3_y = 0; p2_y = thirds0;
        break;

    case 0x1fb59:  /* 🭙 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = p3_y = 0; p2_y = thirds1;
        break;

    case 0x1fb43:  /* 🭃 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = p3_y = 0; p2_y = thirds1;
        break;

    case 0x1fb5a:  /* 🭚 */
        p1_x = p2_x = 0; p3_x = width;
        p1_y = p3_y = 0; p2_y = thirds1;
        break;

    case 0x1fb44:  /* 🭄 */
        p1_x = p2_x = 0; p3_x = width;
        p1_y = p3_y = 0; p2_y = thirds1;
        break;

    case 0x1fb5b:  /* 🭛 */
    case 0x1fb45:  /* 🭅 */
        p1_x = p2_x = 0; p3_x = halfs0;
        p1_y = p3_y = 0; p2_y = height;
        break;

    case 0x1fb62:  /* 🭢 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = p3_y = 0; p2_y = thirds0;
        break;

    case 0x1fb4c:  /* 🭌 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = p3_y = 0; p2_y = thirds0;
        break;

    case 0x1fb63: /* 🭣 */
        p1_x = p2_x = width; p3_x = 0;
        p1_y = p3_y = 0; p2_y = thirds0;
        break;

    case 0x1fb4d:  /* 🭍 */
        p1_x = p2_x = width; p3_x = 0;
        p1_y = p3_y = 0; p2_y = thirds0;
        break;

    case 0x1fb64:  /* 🭤 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = p3_y = 0; p2_y = thirds1;
        break;

    case 0x1fb4e:  /* 🭎 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = p3_y = 0; p2_y = thirds1;
        break;

    case 0x1fb65:  /* 🭥 */
        p1_x = p2_x = width; p3_x = 0;
        p1_y = p3_y = 0; p2_y = thirds1;
        break;

    case 0x1fb4f:  /* 🭏 */
        p1_x = p2_x = width; p3_x = 0;
        p1_y = p3_y = 0; p2_y = thirds1;
        break;

    case 0x1fb66: /* 🭦 */
    case 0x1fb50: /* 🭐 */
        p1_x = p2_x = width; p3_x = halfs1;
        p1_y = p3_y = 0; p2_y = height;
        break;

    case 0x1fb46:  /* 🭆 */
        p1_x = 0; p1_y = thirds1;
        p2_x = width; p2_y = thirds0;
        p3_x = width; p3_y = p1_y;
        break;

    case 0x1fb51:  /* 🭑 */
        p1_x = 0; p1_y = thirds0;
        p2_x = 0; p2_y = thirds1;
        p3_x = width; p3_y = p2_y;
        break;

    case 0x1fb5c:  /* 🭜 */
        p1_x = 0; p1_y = thirds0;
        p2_x = 0; p2_y = thirds1;
        p3_x = width; p3_y = p1_y;
        break;

    case 0x1fb67:  /* 🭧 */
        p1_x = 0; p1_y = thirds0;
        p2_x = width; p2_y = p1_y;
        p3_x = width; p3_y = thirds1;
        break;

    case 0x1fb6c:  /* 🭬 */
    case 0x1fb68:  /* 🭨 */
        p1_x = 0; p1_y = 0;
        p2_x = halfs0; p2_y = height / 2;
        p3_x = 0; p3_y = height;
        break;

    case 0x1fb6d:  /* 🭭 */
    case 0x1fb69:  /* 🭩 */
        p1_x = 0; p1_y = 0;
        p2_x = halfs1; p2_y = height / 2;
        p3_x = width; p3_y = 0;
        break;

    case 0x1fb6e:  /* 🭮 */
    case 0x1fb6a:  /* 🭪 */
        p1_x = width; p1_y = 0;
        p2_x = halfs1; p2_y = height / 2;
        p3_x = width; p3_y = height;
        break;

    case 0x1fb6f:  /* 🭯 */
    case 0x1fb6b:  /* 🭫 */
        p1_x = 0; p1_y = height;
        p2_x = halfs1; p2_y = height / 2;
        p3_x = width; p3_y = height;
        break;

    default:
        BUG("unimplemented Unicode codepoint");
        break;
    }

    const pixman_triangle_t tri = {
        .p1 = {.x = pixman_int_to_fixed(p1_x), .y = pixman_int_to_fixed(p1_y)},
        .p2 = {.x = pixman_int_to_fixed(p2_x), .y = pixman_int_to_fixed(p2_y)},
        .p3 = {.x = pixman_int_to_fixed(p3_x), .y = pixman_int_to_fixed(p3_y)},
    };

    pixman_image_t *src = pixman_image_create_solid_fill(&white);
    pixman_composite_triangles(
        PIXMAN_OP_OVER, src, buf->pix, buf->format, 0, 0, 0, 0, 1, &tri);
    pixman_image_unref(src);
}

static void NOINLINE
draw_wedge_triangle_inverted(struct buf *buf, char32_t wc)
{
    draw_wedge_triangle(buf, wc);

    pixman_image_t *src = pixman_image_create_solid_fill(&white);
    pixman_image_composite(PIXMAN_OP_OUT, src, NULL, buf->pix, 0, 0, 0, 0, 0, 0, buf->width, buf->height);
    pixman_image_unref(src);
}

static void NOINLINE
draw_wedge_triangle_and_box(struct buf *buf, char32_t wc)
{
    draw_wedge_triangle(buf, wc);

    const int width = buf->width;
    const int height = buf->height;

    pixman_box32_t box;

    switch (wc) {
    case 0x1fb46:
    case 0x1fb51:
        box = (pixman_box32_t){
            .x1 = 0, .y1 = buf->y_thirds[1],
            .x2 = width, .y2 = height,
        };
        break;

    case 0x1fb5c:
    case 0x1fb67:
        box = (pixman_box32_t){
            .x1 = 0, .y1 = 0,
            .x2 = width, .y2 = buf->y_thirds[0],
        };
        break;
    }

    pixman_image_fill_boxes(PIXMAN_OP_SRC, buf->pix, &white, 1, &box);
}

static void
draw_left_and_lower_one_eighth_block(struct buf *buf)
{
    draw_left_one_eighth_block(buf);
    draw_lower_one_eighth_block(buf);
}

static void
draw_left_and_upper_one_eighth_block(struct buf *buf)
{
    draw_left_one_eighth_block(buf);
    draw_upper_one_eighth_block(buf);
}

static void
draw_right_and_upper_one_eighth_block(struct buf *buf)
{
    draw_right_one_eighth_block(buf);
    draw_upper_one_eighth_block(buf);
}

static void
draw_right_and_lower_one_eighth_block(struct buf *buf)
{
    draw_right_one_eighth_block(buf);
    draw_lower_one_eighth_block(buf);
}

static void
draw_upper_and_lower_one_eighth_block(struct buf *buf)
{
    draw_upper_one_eighth_block(buf);
    draw_lower_one_eighth_block(buf);
}

static void
draw_horizontal_one_eighth_1358_block(struct buf *buf)
{
    draw_upper_one_eighth_block(buf);
    draw_horizontal_one_eighth_block_3(buf);
    draw_horizontal_one_eighth_block_5(buf);
    draw_lower_one_eighth_block(buf);
}

static void
draw_right_one_quarter_block(struct buf *buf)
{
    rect(buf->width - round(buf->width / 4.), 0, buf->width, buf->height);
}

static void
draw_right_three_eighths_block(struct buf *buf)
{
    rect(buf->width - round(3. * buf->width / 8.), 0, buf->width, buf->height);
}

static void
draw_right_five_eighths_block(struct buf *buf)
{
    rect(buf->width - round(5. * buf->width / 8.), 0, buf->width, buf->height);
}

static void
draw_right_three_quarters_block(struct buf *buf)
{
    rect(buf->width - round(3. * buf->width / 4.), 0, buf->width, buf->height);
}

static void
draw_right_seven_eighths_block(struct buf *buf)
{
    rect(buf->width - round(7. * buf->width / 8.), 0, buf->width, buf->height);
}

static void
draw_glyph(struct buf *buf, char32_t wc)
{
    IGNORE_WARNING("-Wpedantic")

    switch (wc) {
    case 0x2500: draw_box_drawings_light_horizontal(buf); break;
    case 0x2501: draw_box_drawings_heavy_horizontal(buf); break;
    case 0x2502: draw_box_drawings_light_vertical(buf); break;
    case 0x2503: draw_box_drawings_heavy_vertical(buf); break;
    case 0x2504: draw_box_drawings_light_triple_dash_horizontal(buf); break;
    case 0x2505: draw_box_drawings_heavy_triple_dash_horizontal(buf); break;
    case 0x2506: draw_box_drawings_light_triple_dash_vertical(buf); break;
    case 0x2507: draw_box_drawings_heavy_triple_dash_vertical(buf); break;
    case 0x2508: draw_box_drawings_light_quadruple_dash_horizontal(buf); break;
    case 0x2509: draw_box_drawings_heavy_quadruple_dash_horizontal(buf); break;
    case 0x250a: draw_box_drawings_light_quadruple_dash_vertical(buf); break;
    case 0x250b: draw_box_drawings_heavy_quadruple_dash_vertical(buf); break;
    case 0x250c: draw_box_drawings_light_down_and_right(buf); break;
    case 0x250d: draw_box_drawings_down_light_and_right_heavy(buf); break;
    case 0x250e: draw_box_drawings_down_heavy_and_right_light(buf); break;
    case 0x250f: draw_box_drawings_heavy_down_and_right(buf); break;

    case 0x2510: draw_box_drawings_light_down_and_left(buf); break;
    case 0x2511: draw_box_drawings_down_light_and_left_heavy(buf); break;
    case 0x2512: draw_box_drawings_down_heavy_and_left_light(buf); break;
    case 0x2513: draw_box_drawings_heavy_down_and_left(buf); break;
    case 0x2514: draw_box_drawings_light_up_and_right(buf); break;
    case 0x2515: draw_box_drawings_up_light_and_right_heavy(buf); break;
    case 0x2516: draw_box_drawings_up_heavy_and_right_light(buf); break;
    case 0x2517: draw_box_drawings_heavy_up_and_right(buf); break;
    case 0x2518: draw_box_drawings_light_up_and_left(buf); break;
    case 0x2519: draw_box_drawings_up_light_and_left_heavy(buf); break;
    case 0x251a: draw_box_drawings_up_heavy_and_left_light(buf); break;
    case 0x251b: draw_box_drawings_heavy_up_and_left(buf); break;
    case 0x251c: draw_box_drawings_light_vertical_and_right(buf); break;
    case 0x251d: draw_box_drawings_vertical_light_and_right_heavy(buf); break;
    case 0x251e: draw_box_drawings_up_heavy_and_right_down_light(buf); break;
    case 0x251f: draw_box_drawings_down_heavy_and_right_up_light(buf); break;

    case 0x2520: draw_box_drawings_vertical_heavy_and_right_light(buf); break;
    case 0x2521: draw_box_drawings_down_light_and_right_up_heavy(buf); break;
    case 0x2522: draw_box_drawings_up_light_and_right_down_heavy(buf); break;
    case 0x2523: draw_box_drawings_heavy_vertical_and_right(buf); break;
    case 0x2524: draw_box_drawings_light_vertical_and_left(buf); break;
    case 0x2525: draw_box_drawings_vertical_light_and_left_heavy(buf); break;
    case 0x2526: draw_box_drawings_up_heavy_and_left_down_light(buf); break;
    case 0x2527: draw_box_drawings_down_heavy_and_left_up_light(buf); break;
    case 0x2528: draw_box_drawings_vertical_heavy_and_left_light(buf); break;
    case 0x2529: draw_box_drawings_down_light_and_left_up_heavy(buf); break;
    case 0x252a: draw_box_drawings_up_light_and_left_down_heavy(buf); break;
    case 0x252b: draw_box_drawings_heavy_vertical_and_left(buf); break;
    case 0x252c: draw_box_drawings_light_down_and_horizontal(buf); break;
    case 0x252d: draw_box_drawings_left_heavy_and_right_down_light(buf); break;
    case 0x252e: draw_box_drawings_right_heavy_and_left_down_light(buf); break;
    case 0x252f: draw_box_drawings_down_light_and_horizontal_heavy(buf); break;

    case 0x2530: draw_box_drawings_down_heavy_and_horizontal_light(buf); break;
    case 0x2531: draw_box_drawings_right_light_and_left_down_heavy(buf); break;
    case 0x2532: draw_box_drawings_left_light_and_right_down_heavy(buf); break;
    case 0x2533: draw_box_drawings_heavy_down_and_horizontal(buf); break;
    case 0x2534: draw_box_drawings_light_up_and_horizontal(buf); break;
    case 0x2535: draw_box_drawings_left_heavy_and_right_up_light(buf); break;
    case 0x2536: draw_box_drawings_right_heavy_and_left_up_light(buf); break;
    case 0x2537: draw_box_drawings_up_light_and_horizontal_heavy(buf); break;
    case 0x2538: draw_box_drawings_up_heavy_and_horizontal_light(buf); break;
    case 0x2539: draw_box_drawings_right_light_and_left_up_heavy(buf); break;
    case 0x253a: draw_box_drawings_left_light_and_right_up_heavy(buf); break;
    case 0x253b: draw_box_drawings_heavy_up_and_horizontal(buf); break;
    case 0x253c: draw_box_drawings_light_vertical_and_horizontal(buf); break;
    case 0x253d: draw_box_drawings_left_heavy_and_right_vertical_light(buf); break;
    case 0x253e: draw_box_drawings_right_heavy_and_left_vertical_light(buf); break;
    case 0x253f: draw_box_drawings_vertical_light_and_horizontal_heavy(buf); break;

    case 0x2540: draw_box_drawings_up_heavy_and_down_horizontal_light(buf); break;
    case 0x2541: draw_box_drawings_down_heavy_and_up_horizontal_light(buf); break;
    case 0x2542: draw_box_drawings_vertical_heavy_and_horizontal_light(buf); break;
    case 0x2543: draw_box_drawings_left_up_heavy_and_right_down_light(buf); break;
    case 0x2544: draw_box_drawings_right_up_heavy_and_left_down_light(buf); break;
    case 0x2545: draw_box_drawings_left_down_heavy_and_right_up_light(buf); break;
    case 0x2546: draw_box_drawings_right_down_heavy_and_left_up_light(buf); break;
    case 0x2547: draw_box_drawings_down_light_and_up_horizontal_heavy(buf); break;
    case 0x2548: draw_box_drawings_up_light_and_down_horizontal_heavy(buf); break;
    case 0x2549: draw_box_drawings_right_light_and_left_vertical_heavy(buf); break;
    case 0x254a: draw_box_drawings_left_light_and_right_vertical_heavy(buf); break;
    case 0x254b: draw_box_drawings_heavy_vertical_and_horizontal(buf); break;
    case 0x254c: draw_box_drawings_light_double_dash_horizontal(buf); break;
    case 0x254d: draw_box_drawings_heavy_double_dash_horizontal(buf); break;
    case 0x254e: draw_box_drawings_light_double_dash_vertical(buf); break;
    case 0x254f: draw_box_drawings_heavy_double_dash_vertical(buf); break;

    case 0x2550: draw_box_drawings_double_horizontal(buf); break;
    case 0x2551: draw_box_drawings_double_vertical(buf); break;
    case 0x2552: draw_box_drawings_down_single_and_right_double(buf); break;
    case 0x2553: draw_box_drawings_down_double_and_right_single(buf); break;
    case 0x2554: draw_box_drawings_double_down_and_right(buf); break;
    case 0x2555: draw_box_drawings_down_single_and_left_double(buf); break;
    case 0x2556: draw_box_drawings_down_double_and_left_single(buf); break;
    case 0x2557: draw_box_drawings_double_down_and_left(buf); break;
    case 0x2558: draw_box_drawings_up_single_and_right_double(buf); break;
    case 0x2559: draw_box_drawings_up_double_and_right_single(buf); break;
    case 0x255a: draw_box_drawings_double_up_and_right(buf); break;
    case 0x255b: draw_box_drawings_up_single_and_left_double(buf); break;
    case 0x255c: draw_box_drawings_up_double_and_left_single(buf); break;
    case 0x255d: draw_box_drawings_double_up_and_left(buf); break;
    case 0x255e: draw_box_drawings_vertical_single_and_right_double(buf); break;
    case 0x255f: draw_box_drawings_vertical_double_and_right_single(buf); break;

    case 0x2560: draw_box_drawings_double_vertical_and_right(buf); break;
    case 0x2561: draw_box_drawings_vertical_single_and_left_double(buf); break;
    case 0x2562: draw_box_drawings_vertical_double_and_left_single(buf); break;
    case 0x2563: draw_box_drawings_double_vertical_and_left(buf); break;
    case 0x2564: draw_box_drawings_down_single_and_horizontal_double(buf); break;
    case 0x2565: draw_box_drawings_down_double_and_horizontal_single(buf); break;
    case 0x2566: draw_box_drawings_double_down_and_horizontal(buf); break;
    case 0x2567: draw_box_drawings_up_single_and_horizontal_double(buf); break;
    case 0x2568: draw_box_drawings_up_double_and_horizontal_single(buf); break;
    case 0x2569: draw_box_drawings_double_up_and_horizontal(buf); break;
    case 0x256a: draw_box_drawings_vertical_single_and_horizontal_double(buf); break;
    case 0x256b: draw_box_drawings_vertical_double_and_horizontal_single(buf); break;
    case 0x256c: draw_box_drawings_double_vertical_and_horizontal(buf); break;
    case 0x256d ... 0x2570: draw_box_drawings_light_arc(buf, wc); break;

    case 0x2571: draw_box_drawings_light_diagonal_upper_right_to_lower_left(buf); break;
    case 0x2572: draw_box_drawings_light_diagonal_upper_left_to_lower_right(buf); break;
    case 0x2573: draw_box_drawings_light_diagonal_cross(buf); break;
    case 0x2574: draw_box_drawings_light_left(buf); break;
    case 0x2575: draw_box_drawings_light_up(buf); break;
    case 0x2576: draw_box_drawings_light_right(buf); break;
    case 0x2577: draw_box_drawings_light_down(buf); break;
    case 0x2578: draw_box_drawings_heavy_left(buf); break;
    case 0x2579: draw_box_drawings_heavy_up(buf); break;
    case 0x257a: draw_box_drawings_heavy_right(buf); break;
    case 0x257b: draw_box_drawings_heavy_down(buf); break;
    case 0x257c: draw_box_drawings_light_left_and_heavy_right(buf); break;
    case 0x257d: draw_box_drawings_light_up_and_heavy_down(buf); break;
    case 0x257e: draw_box_drawings_heavy_left_and_light_right(buf); break;
    case 0x257f: draw_box_drawings_heavy_up_and_light_down(buf); break;

    case 0x2580: draw_upper_half_block(buf); break;
    case 0x2581: draw_lower_one_eighth_block(buf); break;
    case 0x2582: draw_lower_one_quarter_block(buf); break;
    case 0x2583: draw_lower_three_eighths_block(buf); break;
    case 0x2584: draw_lower_half_block(buf); break;
    case 0x2585: draw_lower_five_eighths_block(buf); break;
    case 0x2586: draw_lower_three_quarters_block(buf); break;
    case 0x2587: draw_lower_seven_eighths_block(buf); break;
    case 0x2588: draw_full_block(buf); break;
    case 0x2589: draw_left_seven_eighths_block(buf); break;
    case 0x258a: draw_left_three_quarters_block(buf); break;
    case 0x258b: draw_left_five_eighths_block(buf); break;
    case 0x258c: draw_left_half_block(buf); break;
    case 0x258d: draw_left_three_eighths_block(buf); break;
    case 0x258e: draw_left_one_quarter_block(buf); break;
    case 0x258f: draw_left_one_eighth_block(buf); break;

    case 0x2590: draw_right_half_block(buf); break;
    case 0x2591: draw_light_shade(buf); break;
    case 0x2592: draw_medium_shade(buf); break;
    case 0x2593: draw_dark_shade(buf); break;
    case 0x2594: draw_upper_one_eighth_block(buf); break;
    case 0x2595: draw_right_one_eighth_block(buf); break;
    case 0x2596 ... 0x259f: draw_quadrant(buf, wc); break;

    case 0x2800 ... 0x28ff: draw_braille(buf, wc); break;

    case 0x1cd00 ... 0x1cde5: draw_octant(buf, wc); break;
    case 0x1fb00 ... 0x1fb3b: draw_sextant(buf, wc); break;

    case 0x1fb3c ... 0x1fb40:
    case 0x1fb47 ... 0x1fb4b:
    case 0x1fb57 ... 0x1fb5b:
    case 0x1fb62 ... 0x1fb66:
    case 0x1fb6c ... 0x1fb6f:
        draw_wedge_triangle(buf, wc);
        break;

    case 0x1fb41 ... 0x1fb45:
    case 0x1fb4c ... 0x1fb50:
    case 0x1fb52 ... 0x1fb56:
    case 0x1fb5d ... 0x1fb61:
    case 0x1fb68 ... 0x1fb6b:
        draw_wedge_triangle_inverted(buf, wc);
        break;

    case 0x1fb46:
    case 0x1fb51:
    case 0x1fb5c:
    case 0x1fb67:
        draw_wedge_triangle_and_box(buf, wc);
        break;

    case 0x1fb9a:
        draw_wedge_triangle(buf, 0x1fb6d);
        draw_wedge_triangle(buf, 0x1fb6f);
        break;

    case 0x1fb9b:
        draw_wedge_triangle(buf, 0x1fb6c);
        draw_wedge_triangle(buf, 0x1fb6e);
        break;

    case 0x1fb70: draw_vertical_one_eighth_block_2(buf); break;
    case 0x1fb71: draw_vertical_one_eighth_block_3(buf); break;
    case 0x1fb72: draw_vertical_one_eighth_block_4(buf); break;
    case 0x1fb73: draw_vertical_one_eighth_block_5(buf); break;
    case 0x1fb74: draw_vertical_one_eighth_block_6(buf); break;
    case 0x1fb75: draw_vertical_one_eighth_block_7(buf); break;

    case 0x1fb76: draw_horizontal_one_eighth_block_2(buf); break;
    case 0x1fb77: draw_horizontal_one_eighth_block_3(buf); break;
    case 0x1fb78: draw_horizontal_one_eighth_block_4(buf); break;
    case 0x1fb79: draw_horizontal_one_eighth_block_5(buf); break;
    case 0x1fb7a: draw_horizontal_one_eighth_block_6(buf); break;
    case 0x1fb7b: draw_horizontal_one_eighth_block_7(buf); break;

    case 0x1fb82: draw_upper_one_quarter_block(buf); break;
    case 0x1fb83: draw_upper_three_eighths_block(buf); break;
    case 0x1fb84: draw_upper_five_eighths_block(buf); break;
    case 0x1fb85: draw_upper_three_quarters_block(buf); break;
    case 0x1fb86: draw_upper_seven_eighths_block(buf); break;

    case 0x1fb7c: draw_left_and_lower_one_eighth_block(buf); break;
    case 0x1fb7d: draw_left_and_upper_one_eighth_block(buf); break;
    case 0x1fb7e: draw_right_and_upper_one_eighth_block(buf); break;
    case 0x1fb7f: draw_right_and_lower_one_eighth_block(buf); break;
    case 0x1fb80: draw_upper_and_lower_one_eighth_block(buf); break;
    case 0x1fb81: draw_horizontal_one_eighth_1358_block(buf); break;

    case 0x1fb87: draw_right_one_quarter_block(buf); break;
    case 0x1fb88: draw_right_three_eighths_block(buf); break;
    case 0x1fb89: draw_right_five_eighths_block(buf); break;
    case 0x1fb8a: draw_right_three_quarters_block(buf); break;
    case 0x1fb8b: draw_right_seven_eighths_block(buf); break;
    }

    UNIGNORE_WARNINGS
}

struct fcft_glyph * COLD
box_drawing(const struct terminal *term, char32_t wc)
{
    int width = term->cell_width;
    int height = term->cell_height;

    pixman_format_code_t fmt =
        term->fonts[0]->antialias ? PIXMAN_a8 : PIXMAN_a1;

    int stride = stride_for_format_and_width(fmt, width);
    uint8_t *data = xcalloc(height * stride, 1);

    pixman_image_t *pix = pixman_image_create_bits_no_clear(
        fmt, width, height, (uint32_t*)data, stride);

    if (pix == NULL) {
        errno = ENOMEM;
        perror(__func__);
        abort();
    }

    double dpi = term->font_is_sized_by_dpi ? term->font_dpi : 96.;
    double scale = term->font_is_sized_by_dpi ? 1. : term->scale;
    double cell_size = sqrt(pow(term->cell_width, 2) + pow(term->cell_height, 2));

    int base_thickness =
        (double)term->conf->tweak.box_drawing_base_thickness * scale * cell_size * dpi / 72.0;
    base_thickness = max(base_thickness, 1);

    int y_third_0 = 0, y_third_1 = 0;
    switch (height % 3) {
    case 0:
        y_third_0 = height / 3;
        y_third_1 = 2 * height / 3;
        break;

    case 1:
        y_third_0 = height / 3;
        y_third_1 = 2 * height / 3 + 1;
        break;

    case 2:
        y_third_0 = height / 3 + 1;
        y_third_1 = y_third_0 + height / 3;
        break;
    }

    /* TODO */
    int y_quad_0 = 0, y_quad_1 = 0, y_quad_2 = 0;
    switch (height % 4) {
    case 0:
        y_quad_0 = height / 4;
        y_quad_1 = height / 2;
        y_quad_2 = 3 * height / 4;
        break;

    case 1:
        y_quad_0 = height / 4;
        y_quad_1 = height / 2;
        y_quad_2 = 3 * height / 4;
        break;
    case 2:
        y_quad_0 = height / 4;
        y_quad_1 = height / 2;
        y_quad_2 = 3 * height / 4;
        break;

    case 3:
        y_quad_0 = height / 4;
        y_quad_1 = height / 2;
        y_quad_2 = 3 * height / 4;
        break;
        }

    struct buf buf = {
        .data = data,
        .pix = pix,
        .format = fmt,
        .width = width,
        .height = height,
        .stride = stride,
        .solid_shades = term->conf->tweak.box_drawing_solid_shades,

        .thickness = {
            [LIGHT] = _thickness(base_thickness, LIGHT),
            [HEAVY] = _thickness(base_thickness, HEAVY),
        },

        /* Overlap when width is odd */
        .x_halfs = {
            round(width / 2.), /* Endpoint first half */
            width / 2,         /* Startpoint second half */
        },

        .y_thirds = {
            y_third_0,  /* Endpoint first third, start point second third */
            y_third_1,  /* Endpoint second third, start point last third */
        },

        .y_quads = {
            y_quad_0,
            y_quad_1,
            y_quad_2,
        },
    };

    LOG_DBG("LIGHT=%d, HEAVY=%d", buf.thickness[LIGHT], buf.thickness[HEAVY]);

    draw_glyph(&buf, wc);

    struct fcft_glyph *glyph = xmalloc(sizeof(*glyph));
    *glyph = (struct fcft_glyph){
        .cp = wc,
        .cols = 1,
        .pix = buf.pix,
        .x = -term->font_x_ofs,
        .y = term->font_baseline,
        .width = width,
        .height = height,
        .advance = {
            .x = width,
            .y = height,
        },
    };
    return glyph;
}