egatari 0.6.11 → 0.6.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/CHANGELOG.md +33 -1
  2. package/bin/egatari.mjs +4 -3
  3. package/components.lock.json +30 -15
  4. package/components2.lock.json +201 -1
  5. package/fonts/@fontsource/fraunces/400.css +29 -0
  6. package/fonts/@fontsource/fraunces/700.css +29 -0
  7. package/fonts/@fontsource/fraunces/LICENSE +93 -0
  8. package/fonts/@fontsource/fraunces/files/fraunces-latin-400-normal.woff2 +0 -0
  9. package/fonts/@fontsource/fraunces/files/fraunces-latin-700-normal.woff2 +0 -0
  10. package/fonts/@fontsource/fraunces/files/fraunces-latin-ext-400-normal.woff2 +0 -0
  11. package/fonts/@fontsource/fraunces/files/fraunces-latin-ext-700-normal.woff2 +0 -0
  12. package/fonts/@fontsource/fraunces/files/fraunces-vietnamese-400-normal.woff2 +0 -0
  13. package/fonts/@fontsource/fraunces/files/fraunces-vietnamese-700-normal.woff2 +0 -0
  14. package/fonts/@fontsource/fraunces/package.json +6 -0
  15. package/kit/check-messages.mjs +18 -7
  16. package/kit/check.mjs +12 -4
  17. package/kit/core/article-render.mjs +5 -5
  18. package/kit/core/build-core.mjs +25 -9
  19. package/kit/core/check-parity.mjs +1 -1
  20. package/kit/core/check-rules.mjs +2 -2
  21. package/kit/core/check-source.mjs +3 -0
  22. package/kit/core/colab-core.mjs +1 -1
  23. package/kit/core/kicker.mjs +42 -0
  24. package/kit/core/layout-scan.mjs +19 -7
  25. package/kit/core/llm-pack-data.mjs +8 -8
  26. package/kit/core/llm-pack.mjs +2 -2
  27. package/kit/core/theme-shapes.mjs +3 -1
  28. package/kit/core/themes-list.mjs +2 -0
  29. package/kit/instruction-version.json +1 -1
  30. package/kit/layouts.css +8 -0
  31. package/kit/player.json +1 -1
  32. package/kit/player.lock.json +2 -2
  33. package/package.json +2 -1
  34. package/themes/asagi/theme.json +1 -1
  35. package/themes/asagi/tokens.css +6 -0
  36. package/themes/bengara/theme.json +13 -0
  37. package/themes/bengara/tokens.css +66 -0
  38. package/themes/business/theme.json +1 -1
  39. package/themes/business/tokens.css +6 -0
  40. package/themes/default/theme.json +1 -1
  41. package/themes/default/tokens.css +9 -0
  42. package/themes/kogecha/theme.json +1 -1
  43. package/themes/kogecha/tokens.css +6 -0
  44. package/themes/kokugo/theme.json +1 -1
  45. package/themes/kokugo/tokens.css +6 -0
  46. package/themes/murasaki/theme.json +1 -1
  47. package/themes/murasaki/tokens.css +6 -0
  48. package/themes/sumi/theme.json +1 -1
  49. package/themes/sumi/tokens.css +8 -0
  50. package/themes/tokiwa/theme.json +1 -1
  51. package/themes/tokiwa/tokens.css +6 -0
  52. package/themes.lock.json +90 -24
  53. package/vendor/components/CATALOG.md +11 -11
  54. package/vendor/components/sha256/04626b77b3c683fd4c8e549339eca153615e021eb0e649c31291deb2dab30667/README.en.md +38 -0
  55. package/vendor/components/sha256/04626b77b3c683fd4c8e549339eca153615e021eb0e649c31291deb2dab30667/index.js +289 -0
  56. package/vendor/components/sha256/0c4eb83646ab8027d40697db161eb952425db78c42b36291626214d5064b9498/fixtures/ABCJS-ADAPTER.md +101 -0
  57. package/vendor/components/sha256/0c4eb83646ab8027d40697db161eb952425db78c42b36291626214d5064b9498/fixtures/LICENSE-abcjs.txt +21 -0
  58. package/vendor/components/sha256/0c4eb83646ab8027d40697db161eb952425db78c42b36291626214d5064b9498/fixtures/chords.json +89 -0
  59. package/vendor/components/sha256/0c4eb83646ab8027d40697db161eb952425db78c42b36291626214d5064b9498/fixtures/doremi.json +261 -0
  60. package/vendor/components/sha256/0c4eb83646ab8027d40697db161eb952425db78c42b36291626214d5064b9498/fixtures/extras.json +192 -0
  61. package/vendor/components/sha256/0c4eb83646ab8027d40697db161eb952425db78c42b36291626214d5064b9498/fixtures/kaeru.json +521 -0
  62. package/vendor/components/sha256/0c4eb83646ab8027d40697db161eb952425db78c42b36291626214d5064b9498/fixtures/rhythm.json +248 -0
  63. package/vendor/components/sha256/0c4eb83646ab8027d40697db161eb952425db78c42b36291626214d5064b9498/fixtures/ties.json +360 -0
  64. package/vendor/components/sha256/0c4eb83646ab8027d40697db161eb952425db78c42b36291626214d5064b9498/index.js +508 -0
  65. package/vendor/components/sha256/0dc2a0945e7a4004278fcc9b8f02223bbb87f6269bffe3dd3ebeb3a78ce23d34/index.js +404 -0
  66. package/vendor/components/sha256/0e2e45b024bb2f37e82c83a4d9cd9f9c16b5dc4924dde81735c268a3d49f9d7f/index.js +97 -0
  67. package/vendor/components/sha256/0ef62e33d330f8e31ccb1f480b5e3b349769a142cc2737a9e6583d5eff9f56ca/index.js +919 -0
  68. package/vendor/components/sha256/1b33a252d51091e2d8cdea176f10268f128640bed3aa46da88d7de31ced59b44/README.en.md +48 -0
  69. package/vendor/components/sha256/1b33a252d51091e2d8cdea176f10268f128640bed3aa46da88d7de31ced59b44/index.js +384 -0
  70. package/vendor/components/sha256/1b767b6cf1855156872f87981a7c99f0161013499d6c4b21785624435db92078/README.en.md +34 -0
  71. package/vendor/components/sha256/1b767b6cf1855156872f87981a7c99f0161013499d6c4b21785624435db92078/index.js +244 -0
  72. package/vendor/components/sha256/3ace61e46a4555eab46ca8053db2fb76a874d4d5409bc5891cc449f43ef7e0bd/README.en.md +44 -0
  73. package/vendor/components/sha256/3ace61e46a4555eab46ca8053db2fb76a874d4d5409bc5891cc449f43ef7e0bd/index.js +304 -0
  74. package/vendor/components/sha256/4079077e03343995edc5e34b8bda0cf200dc6d854a52486fe0a6a4ceaf0d4b6f/README.en.md +44 -0
  75. package/vendor/components/sha256/4079077e03343995edc5e34b8bda0cf200dc6d854a52486fe0a6a4ceaf0d4b6f/index.js +203 -0
  76. package/vendor/components/sha256/40aaaaf988e7c32460ffaf5f18b5690ee28fd1fe7e879d8434d58283992bc554/README.en.md +37 -0
  77. package/vendor/components/sha256/40aaaaf988e7c32460ffaf5f18b5690ee28fd1fe7e879d8434d58283992bc554/index.js +438 -0
  78. package/vendor/components/sha256/442be174439d106ef462c19d598c407e86d5de3fcd4700ed54ae4147c4b9969c/README.en.md +24 -0
  79. package/vendor/components/sha256/442be174439d106ef462c19d598c407e86d5de3fcd4700ed54ae4147c4b9969c/index.js +103 -0
  80. package/vendor/components/sha256/48a776e7683e1aba8cad5aa4067914c017681efaa74f175066e0ab081bf3d013/index.js +351 -0
  81. package/vendor/components/sha256/4a9331635c2dd28e4b46108eaffe49c6d29cf626ecb900ac6e9029552be0cf49/index.js +607 -0
  82. package/vendor/components/sha256/5458baef7a30298cf4b0029331f13233d162403e686f9c20085dd2104cad7141/README.en.md +23 -0
  83. package/vendor/components/sha256/5458baef7a30298cf4b0029331f13233d162403e686f9c20085dd2104cad7141/index.js +120 -0
  84. package/vendor/components/sha256/586063d3a4e24e2995923811b2b9466bde86dbaafb3bcf103546d3f28159205a/index.js +812 -0
  85. package/vendor/components/sha256/5acc5f87e29f1170bc35dbee08087d0d03c1023969c6c1b1367a0f559d60e5ee/index.js +198 -0
  86. package/vendor/components/sha256/5c032594230fe688f661b6ccf27070c84f8c0e38611186280fb9c56d89ac3319/README.en.md +32 -0
  87. package/vendor/components/sha256/5c032594230fe688f661b6ccf27070c84f8c0e38611186280fb9c56d89ac3319/index.js +222 -0
  88. package/vendor/components/sha256/603de10f0ee50d07ec610e21b45b9db78d69376b4dfd2baba78595c1002e27ec/README.en.md +50 -0
  89. package/vendor/components/sha256/603de10f0ee50d07ec610e21b45b9db78d69376b4dfd2baba78595c1002e27ec/index.js +171 -0
  90. package/vendor/components/sha256/609e45a826d07a54cbbcbf4ce2ad6b5474dca1d0be394eb9fad895db4a73422f/README.en.md +51 -0
  91. package/vendor/components/sha256/609e45a826d07a54cbbcbf4ce2ad6b5474dca1d0be394eb9fad895db4a73422f/index.js +228 -0
  92. package/vendor/components/sha256/6364043cb31bbd86aacb7432af1a8affef8cae5c6efdbe13fbebd116fd51b91f/index.js +233 -0
  93. package/vendor/components/sha256/6f9f7283226d3534aa1cb4a47ba258204aa286ead01b25cdbde14119260bc702/README.en.md +66 -0
  94. package/vendor/components/sha256/6f9f7283226d3534aa1cb4a47ba258204aa286ead01b25cdbde14119260bc702/index.js +920 -0
  95. package/vendor/components/sha256/72edfd6678b22be0bb3e32b0d8cb0dfee403767e1eacac354d278d7318d03826/README.en.md +35 -0
  96. package/vendor/components/sha256/72edfd6678b22be0bb3e32b0d8cb0dfee403767e1eacac354d278d7318d03826/index.js +179 -0
  97. package/vendor/components/sha256/7482c50bea5ef7d72dc5d4afaddb8c4e725b5a83fa0c6175f9a9c556f23a4e10/LICENSE-tabler.txt +21 -0
  98. package/vendor/components/sha256/7482c50bea5ef7d72dc5d4afaddb8c4e725b5a83fa0c6175f9a9c556f23a4e10/index.js +4868 -0
  99. package/vendor/components/sha256/7482c50bea5ef7d72dc5d4afaddb8c4e725b5a83fa0c6175f9a9c556f23a4e10/names.txt +4808 -0
  100. package/vendor/components/sha256/7cb8948c550b66bf4b34a62e7b61385aaa79896e09d82ccb1846c0ac499ad352/README.en.md +40 -0
  101. package/vendor/components/sha256/7cb8948c550b66bf4b34a62e7b61385aaa79896e09d82ccb1846c0ac499ad352/index.js +348 -0
  102. package/vendor/components/sha256/87cc60da38d209cda449dd7c24171e287ac8184c5036ca04548ef9e2015c2d9e/README.en.md +42 -0
  103. package/vendor/components/sha256/87cc60da38d209cda449dd7c24171e287ac8184c5036ca04548ef9e2015c2d9e/index.js +268 -0
  104. package/vendor/components/sha256/8cb367c71c002c689defddc6f075be911d5485421715119b5515181e962a2401/index.js +121 -0
  105. package/vendor/components/sha256/97bfd6e79c7278c55153bf4c2330da0432cf0ee312baa11650e07cdd7912831d/index.js +354 -0
  106. package/vendor/components/sha256/a426e6a3f8f4593403ba63ecc4a3f1f41064f4016fd9fb8d51e7876bf8c6f573/README.en.md +40 -0
  107. package/vendor/components/sha256/a426e6a3f8f4593403ba63ecc4a3f1f41064f4016fd9fb8d51e7876bf8c6f573/index.js +348 -0
  108. package/vendor/components/sha256/c108119117042cacc65a35ec73fcab95a2a92b2a2797129f66e326783f8db178/index.js +465 -0
  109. package/vendor/components/sha256/c3fad2047cec6a2bd8a5b907f752e2addeeea2f373fd8115914552dcee0b2f6f/README.en.md +23 -0
  110. package/vendor/components/sha256/c3fad2047cec6a2bd8a5b907f752e2addeeea2f373fd8115914552dcee0b2f6f/index.js +289 -0
  111. package/vendor/components/sha256/c59f1ac3132fdf6f56e8880c866570cd7443f09eb8802b6e7af32675ad358edf/README.en.md +31 -0
  112. package/vendor/components/sha256/c59f1ac3132fdf6f56e8880c866570cd7443f09eb8802b6e7af32675ad358edf/index.js +236 -0
  113. package/vendor/components/sha256/d3a75f67160490db1831aaa8ac58ebc5f7ffb6a6fd21e2bdeaa6fe979bfaff7f/README.en.md +42 -0
  114. package/vendor/components/sha256/d3a75f67160490db1831aaa8ac58ebc5f7ffb6a6fd21e2bdeaa6fe979bfaff7f/index.js +488 -0
  115. package/vendor/components/sha256/d998b5db828427505e39cdae67676dc6480c791d6037152dec222ae81cdbba1f/index.js +484 -0
  116. package/vendor/components/sha256/ea17602aaecca64b672b3bc827965ff378cb460250177f7b66f73081a48d4e81/index.js +278 -0
  117. package/vendor/components/sha256/f9cef6dd9580a4f19aa09d47f736ab2391f5f585b20b16a62bd781398cb6b8ac/README.en.md +22 -0
  118. package/vendor/components/sha256/f9cef6dd9580a4f19aa09d47f736ab2391f5f585b20b16a62bd781398cb6b8ac/index.js +145 -0
  119. package/vendor/components-2/manifest-2.8.0.json +2199 -0
  120. package/vendor/themes/asagi/1.2.0/theme.json +14 -0
  121. package/vendor/themes/asagi/1.2.0/tokens.css +30 -0
  122. package/vendor/themes/business/1.2.0/theme.json +5 -0
  123. package/vendor/themes/business/1.2.0/tokens.css +41 -0
  124. package/vendor/themes/default/1.4.0/theme.json +4 -0
  125. package/vendor/themes/default/1.4.0/tokens.css +86 -0
  126. package/vendor/themes/kogecha/1.2.0/theme.json +10 -0
  127. package/vendor/themes/kogecha/1.2.0/tokens.css +29 -0
  128. package/vendor/themes/kokugo/1.2.0/theme.json +5 -0
  129. package/vendor/themes/kokugo/1.2.0/tokens.css +25 -0
  130. package/vendor/themes/murasaki/1.2.0/theme.json +10 -0
  131. package/vendor/themes/murasaki/1.2.0/tokens.css +25 -0
  132. package/vendor/themes/sumi/1.2.0/theme.json +10 -0
  133. package/vendor/themes/sumi/1.2.0/tokens.css +49 -0
  134. package/vendor/themes/tokiwa/1.2.0/theme.json +10 -0
  135. package/vendor/themes/tokiwa/1.2.0/tokens.css +27 -0
@@ -0,0 +1,607 @@
1
+ // m-codetree:ハフマン木で 2 値の系列を復号する(ファクシミリの MH 符号:白と黒のランを交互に読む)
2
+ // <m-codetree style="height:530px"><script type="application/json">
3
+ // {"table": [{"run": 2, "white": "0111", "black": "11"}, {"run": 64, "white": "11011", "black": "0000001111"}, …],
4
+ // "bits": "11011011111"}
5
+ // (JSON の終わり)</m-codetree>
6
+ // 木は表から作る(根の左が白の木、右が黒の木。枝の 0 が左、1 が右)。表は [{run, white, black}] か {"white": {"符号": ラン}, "black": {…}}。
7
+ // run が 64 以上はメイクアップ符号(同じ色のまま、次に終端符号が続く)、64 未満は終端符号(次は反対の色)、"EOL" は行の終わり(次は白)。
8
+ // 1 段階 = 1 符号語。段階の中で読む 1 ビットごとに cue(口述原稿の {cue})。最後の段階は「まとめ」(復号した符号語の道をすべて出す)。
9
+ // 画面:上に符号の系列と読む位置、中に木(今の色の木だけを広げ、ほかは畳む)、下に復号した画素の並び(ランの長さだけの白・黒の升目)。
10
+ // 段階の決め方:1 ビット 1 段階にすると口述原稿が 1 ビット 1 行に細切れになるので、1 符号語 = 1 段階とし、ビットは段階の中の cue にした
11
+ // (cues(k) は読むビットごとの時刻。原稿に {cue} をビットの数だけ書けば読む位置が言葉に合い、書かなければ動きの長さのまま)。
12
+ // 段階 0 は白の木だけを広げた初めの姿。段階 k の中:木を広げ直す(move 秒)→ 根から色の木へ → 1 ビットごとに 1 本(bit 秒)→ 葉に着く →
13
+ // 画素の升目を塗る(fill 秒)。最後の段階(summary="false" で外す)は両方の木と復号したすべての道(印刷・静止の姿)
14
+ // data-mark(頭の ct は属性 mark で変える):ct-root、ct-w / ct-b(色の木の根)、ct-w-1101(白の木の符号 1101 の節・葉)、ct-w64 / ct-b2 / ct-weol
15
+ // (葉の数字)、ct-bit-3(系列の 3 桁目)、ct-cw-2(2 つ目の符号語の括弧と結果)、ct-run-1(1 つ目の画素のラン)、ct-scan(画素の並び)、ct-bad(表にない枝)、
16
+ // ct-crumb(窓の上の「ここまで」)
17
+ // 深い木(黒の符号は 13 ビットまで):入らなければ window 段の窓だけを見せ、たどる点を追って木を下から上へ流す(窓の位置も (k, t) だけで決まる)。
18
+ // 窓では木をたどる道を背骨にして並べ直し(道から分かれた枝は反対の側に三角で畳む)、窓を横にも動かして道を真ん中に置く。
19
+ // 親が窓の上にある枝は描かない(入ってくる道だけを上の縁から描く)。窓の上に「黒の木:ここまで 0000 0110」。window="auto"(既定)は段の間が 44 px より狭くなるときだけ流す。
20
+ // まとめの段階は窓を使わず、復号したすべての道の木を部品の高さに詰めて描く(段の間 SUM_GAP px 以上のとき。印刷・台本の小さい絵は最後の姿だけなので)。
21
+ // それより狭ければ、いちばん深い道の窓にして layout で知らせる。窓の段の間が 24 px より狭いときも layout で知らせる。最後の姿(t = Infinity)では窓の縁で薄れない
22
+ // 画面の文言(NAMES/NAMES_EN)は ctx.lang の日英に従う(ja なら日本語、それ以外は英語)。JSON の labels で上書きできる:
23
+ // white black start read makeup term eol invalid incomplete dangling done scan crumb({c} は色、{n} は桁)
24
+ // 復号の規則は codetree-core(試験は tools/test-codetree.mjs)
25
+ // 2.0(Egatari spec §6.1 の部品):base の MotionElement・el・prog・lerp・previewOpacity と、表・木・復号の codetree-core を使い、
26
+ // プレイヤーのグローバルは使わない
27
+ egatariParts.define({name: "codetree", version: "2.0.5", requires: {base: "1.2.0", "codetree-core": "1.0.0"}, element: "m-codetree"}, function(deps){
28
+ "use strict";
29
+ var base = deps.base;
30
+ var D = base;
31
+
32
+ // ---------------------------------------------------------------- 表・木・復号(codetree-core)
33
+ var CORE = deps["codetree-core"];
34
+ var build = CORE.build, decode = CORE.decode, runs = CORE.runs;
35
+ var COLORS = ["white", "black"]; // 色の木の順(codetree-core と同じ)
36
+
37
+
38
+ // ---------------------------------------------------------------- 部品
39
+ // 表示の文字(ctx.lang、見る人の言語ではなく文書の言語)。子の JSON の labels が勝つ
40
+ var NAMES = {white: "白", black: "黒", start: "最初は白の木から読む", read: "{c}の木をたどる", makeup: "メイクアップ符号:次も{c}",
41
+ term: "終端符号:次は{c}", eol: "行の終わり:次は白", invalid: "表にない符号({n} 桁目)", incomplete: "系列が符号の途中で終わる",
42
+ dangling: "メイクアップ符号の後に終端符号がない", done: "復号完了", scan: "画素", crumb: "{c}の木:ここまで"};
43
+ var NAMES_EN = {white: "white", black: "black", start: "Start with the white tree", read: "Following the {c} tree", makeup: "Make-up code: {c} continues",
44
+ term: "Terminating code: next is {c}", eol: "End of line: next is white", invalid: "Not in the table (digit {n})", incomplete: "The sequence ends mid-code",
45
+ dangling: "Make-up code with no terminating code after it", done: "Decoded", scan: "Pixels", crumb: "{c} tree: so far"};
46
+ var LETTER = {white: "w", black: "b"};
47
+ // 字の幅の見積もり(字の読み込みによらず同じ配置にする)
48
+ // 誤りの段階でたどれる道(表にない枝の手前まで)
49
+ function errPath(e){ return e.kind === "invalid" ? e.code.slice(0, -1) : e.code; }
50
+ // 枝の字の位置:親 a から子 b への線の f の所から、線に垂直に外側へ off px(字の中心。y は字の基線)
51
+ function labelAt(a, b, f, off){
52
+ var dx = b.x - a.x, dy = b.y - a.y, L = Math.hypot(dx, dy) || 1, nx = dx < 0 ? -dy / L : dy / L, ny = -Math.abs(dx) / L;
53
+ return {x: a.x + dx * f + nx * off, y: a.y + dy * f + ny * off + 8};
54
+ }
55
+ function fmt0(s, c){ return String(s).replace("{c}", c); }
56
+ // まとめの段階で、すべての道の木を窓なしで描く段の間の下限(px)。窓の段の間の下限(読める大きさ)は WIN_GAP
57
+ var SUM_GAP = 20, WIN_GAP = 40; // 窓の段の間は 0・1 の字(24 px)が枝の線にかからない広さ
58
+ // 字の幅(base の textWidth:font の要素の字で測った幅)
59
+ function textW(s, fs, font){ return base.textWidth(s, fs, {font: font}); }
60
+
61
+ return class extends base.MotionElement {
62
+ static partName = "codetree"; // the registry name (define meta name), for report prefixes
63
+ static shape = {
64
+ schema: {type: "object", additionalProperties: false, required: ["table"], properties: {
65
+ table: {type: ["array", "object"], items: {type: "object", required: ["run"], properties: {run: {type: ["integer", "string"]}, white: {type: "string"}, black: {type: "string"}}},
66
+ properties: {white: {type: "object"}, black: {type: "object"}}},
67
+ bits: {type: "string"}, labels: {type: "object"}}},
68
+ example: {table: [{run: 2, white: "0111", black: "11"}, {run: 64, white: "11011", black: "0000001111"}], bits: "110110111"},
69
+ };
70
+ static props = {
71
+ bits: {type: "string", default: "", doc: "復号する系列(JSON の bits より先)"},
72
+ depth: {type: "number", default: 4, doc: "畳まずに見せる深さ(読む道はいつも広げる)"},
73
+ bit: {type: "number", default: 0.6, doc: "1 ビットを読む秒数"},
74
+ move: {type: "number", default: 0.6, doc: "木を広げ直す秒数"},
75
+ fill: {type: "number", default: 0.8, doc: "画素の升目を塗る秒数"},
76
+ summary: {type: "bool", default: true, doc: "最後にまとめのステップを足す"},
77
+ mark: {type: "string", default: "ct", doc: "data-mark の頭"},
78
+ window: {type: "string", default: "auto", doc: "木を見せる段の数(auto:入らなければ、たどる点を追って木を流す)"},
79
+ };
80
+ // 復号と段階の計画(データだけから決める)
81
+ model(){
82
+ if (this._model) return this._model;
83
+ var d = this.data || {}, P = this.props;
84
+ var trees = build(d.table), r = decode(trees, P.bits || d.bits || "");
85
+ var plan = r.tokens.map(function(t, i){ return {type: "tok", tok: i}; });
86
+ if (r.error && r.error.kind !== "dangling") plan.push({type: "err"});
87
+ if (P.summary && plan.length) plan.push({type: "sum"});
88
+ return (this._model = {trees: trees, dec: r, plan: plan, runs: runs(r.tokens)});
89
+ }
90
+ get steps(){
91
+ if (this.getAttribute("steps") !== null) return +this.getAttribute("steps");
92
+ return this.model().plan.length;
93
+ }
94
+ // 段階 k(1〜)の時刻:s(i) は道の i 本目(0 本目は根から色の木へ)を引き始める時刻。cue は読む 1 ビットごと
95
+ times(k){
96
+ var m = this.model(), P = this.props, p = m.plan[k - 1];
97
+ if (!p) return null;
98
+ var nb = p.type === "tok" ? m.dec.tokens[p.tok].code.length : p.type === "err" ? m.dec.error.code.length : 0;
99
+ var s = function(i){ return P.move + i * P.bit; };
100
+ var A = s(nb) + 0.7 * P.bit; // 葉に着く
101
+ return {nb: nb, s: s, A: A, F0: A + 0.15, F1: A + 0.15 + P.fill};
102
+ }
103
+ duration(k){
104
+ var m = this.model(), p = m.plan[k - 1], T = this.times(k);
105
+ if (!p) return 0;
106
+ if (p.type === "sum") return this.props.move + 1.1;
107
+ if (p.type === "err") return T.A + 0.5;
108
+ return T.F1 + 0.1;
109
+ }
110
+ cues(k){
111
+ var p = this.model().plan[k - 1], T = this.times(k);
112
+ if (!p || p.type === "sum") return null;
113
+ var c = [];
114
+ for (var i = 1; i <= T.nb; i++) c.push(+T.s(i).toFixed(3));
115
+ return c;
116
+ }
117
+ // 段階 k の木の形:見せる節の位置と種類(int 広げた節・fan 畳んだ節・leaf 葉)
118
+ layoutFor(k){
119
+ var m = this.model(), p = k ? m.plan[k - 1] : null, dec = m.dec;
120
+ var focus, paths = {white: [], black: []}, both = false, cur = null;
121
+ if (!p){ focus = "white"; }
122
+ else if (p.type === "tok"){ var t = dec.tokens[p.tok]; focus = t.color; paths[focus].push(t.code); cur = {color: focus, code: t.code}; }
123
+ else if (p.type === "err"){ focus = dec.error.color; var ep = errPath(dec.error); paths[focus].push(dec.error.code); cur = {color: focus, code: ep, bad: dec.error.kind === "invalid" ? dec.error.code : null}; }
124
+ else {
125
+ both = true;
126
+ dec.tokens.forEach(function(t){ paths[t.color].push(t.code); });
127
+ if (dec.error && dec.error.kind !== "dangling") paths[dec.error.color].push(dec.error.code);
128
+ }
129
+ var W = this.W, G = this.G, self = this;
130
+ var spine = function(sp, a){
131
+ var nodes = {}, ghosts = {}, maxLv = 1;
132
+ var put = function(o){ nodes[o.id] = o; maxLv = Math.max(maxLv, o.lv); return o; };
133
+ var r = put({id: "r", lv: 0, kind: "int", kids: [], c: null, code: "", x: 0});
134
+ var pc = sp ? sp.color : "white";
135
+ COLORS.forEach(function(c){
136
+ var n = m.trees[c], d = c === "white" ? -1 : 1;
137
+ r.kids.push(put({id: LETTER[c], c: c, code: "", lv: 1, parent: "r", leaf: null, kids: [], kind: c === pc ? "int" : "fan", x: d * a}));
138
+ });
139
+ var node = m.trees[pc], o = nodes[LETTER[pc]], code = sp ? sp.code : "";
140
+ for (var i = 0; i < code.length && node && !node.leaf; i++){
141
+ var b = +code[i], dir = b ? 1 : -1, nx = null;
142
+ o.kind = "int";
143
+ [b, 1 - b].forEach(function(bb){
144
+ var kid = node.kids[bb], id = o.id + bb, x = o.x + (bb === b ? dir : -dir) * a;
145
+ if (!kid){ var g = {id: id, lv: o.lv + 1, ghost: true, kids: [], x: x}; ghosts[id] = g; return; }
146
+ var k2 = put({id: id, c: pc, code: kid.code, lv: o.lv + 1, parent: o.id, leaf: kid.leaf, kids: [], kind: kid.leaf ? "leaf" : "fan", x: x});
147
+ o.kids.push(k2);
148
+ if (bb === b) nx = k2;
149
+ });
150
+ node = node.kids[b]; o = nx;
151
+ if (!o) break;
152
+ }
153
+ return {nodes: nodes, slots: [], maxLv: maxLv, ghosts: ghosts};
154
+ };
155
+ var make = function(depth){
156
+ var nodes = {}, slots = [], maxLv = 1, ghosts = {};
157
+ var open = function(c, n){
158
+ if (n.leaf) return false;
159
+ if (!both && c !== focus) return false;
160
+ var onPath = paths[c].some(function(q){ return q.length > n.code.length && q.slice(0, n.code.length) === n.code; });
161
+ if (onPath) return true;
162
+ return n.code.length < depth;
163
+ };
164
+ var visit = function(c, n, id, lv, parent){
165
+ var o = {id: id, c: c, code: n.code, lv: lv, parent: parent, leaf: n.leaf, kids: []};
166
+ nodes[id] = o; maxLv = Math.max(maxLv, lv);
167
+ if (open(c, n)){
168
+ o.kind = "int";
169
+ // 表にない枝は空きの場所(ghost)にする:1 の子だけでも右に寄り、誤りの × をそこに置ける
170
+ [0, 1].forEach(function(b){
171
+ if (n.kids[b]) o.kids.push(visit(c, n.kids[b], id + b, lv + 1, id));
172
+ else { var g = {id: id + b, lv: lv + 1, ghost: true, kids: []}; ghosts[id + b] = g; slots.push(g); o.kids.push(g); }
173
+ });
174
+ if (!o.kids.length){ o.kind = "fan"; slots.push(o); }
175
+ } else { o.kind = n.leaf ? "leaf" : "fan"; slots.push(o); }
176
+ return o;
177
+ };
178
+ nodes.r = {id: "r", lv: 0, kind: "int", kids: [], c: null, code: ""};
179
+ COLORS.forEach(function(c){ nodes.r.kids.push(visit(c, m.trees[c], LETTER[c], 1, "r")); });
180
+ return {nodes: nodes, slots: slots, maxLv: maxLv, ghosts: ghosts};
181
+ };
182
+ var d = Math.max(1, this.props.depth), L = make(d);
183
+ while (d > 1 && W / L.slots.length < 64){ d--; L = make(d); }
184
+ var avail = G.treeBottom - G.treeTop - 44, wp = String(this.props.window || "auto").trim(), nwin = /^\d+$/.test(wp) ? Math.max(2, +wp) : 0;
185
+ var follow = nwin ? L.maxLv > nwin : avail / L.maxLv < 44;
186
+ // まとめ(both)は、すべての道の木(make の L)が段の間 SUM_GAP 以上で入るなら窓を使わない。入らなければいちばん深い道の窓(mount が知らせる)
187
+ var sumGap = Math.min(78, avail / L.maxLv), sumLv = L.maxLv;
188
+ if (both && follow && sumGap >= SUM_GAP) follow = false;
189
+ // 窓で追うときは、木の幅を部品に合わせず、枠の幅を決めて並べ、窓を横にも動かす(render が今の節を真ん中に置く)
190
+ // 窓で追うときは、たどる道を背骨にして並べ直す:道の子は 0 なら左・1 なら右へ a px、道から分かれた枝は反対の側に畳んで置く。
191
+ // 窓を横にも動かし(render が今の節を真ん中に置く)、窓の中を今の道のまわりに使う
192
+ var sw = W / L.slots.length;
193
+ if (follow){
194
+ var sp = cur ? {color: cur.color, code: cur.bad || cur.code} : null;
195
+ if (!sp){ COLORS.forEach(function(c){ paths[c].forEach(function(q){ if (!sp || q.length > sp.code.length) sp = {color: c, code: q}; }); }); }
196
+ L = spine(sp, Math.min(150, W / 7.5));
197
+ } else {
198
+ L.slots.forEach(function(o, i){ o.x = (i + 0.5) * sw; });
199
+ var px = function(o){ if (o.x === undefined){ o.kids.forEach(px); o.x = (o.kids[0].x + o.kids[o.kids.length - 1].x) / 2; } return o.x; };
200
+ px(L.nodes.r);
201
+ }
202
+ // 段の間:葉の数字と畳んだ節の三角の分を下に空ける
203
+ // 木が入らなければ(段の間が 44 px より狭くなる、または段の数が属性 window より多い)、win 段の窓だけを見せ、
204
+ // たどる点を追って窓を下へ流す(窓の上は「ここまで」の行)。段の位置は y0 + (lv − cam) × gap(cam は render が (k, t) から決める)
205
+ L.follow = follow;
206
+ if (!L.follow){ L.y0 = G.treeTop; L.win = L.maxLv; L.gap = Math.min(78, avail / L.maxLv); }
207
+ else { L.y0 = G.treeTop + 44; var av2 = G.treeBottom - 44 - L.y0; L.win = nwin || Math.max(2, Math.floor(av2 / 56)); L.gap = av2 / L.win; }
208
+ L.camMax = Math.max(0, L.maxLv - L.win);
209
+ var gap = L.gap;
210
+ Object.keys(L.nodes).forEach(function(id){ var o = L.nodes[id]; o.y = L.y0 + o.lv * gap; });
211
+ Object.keys(L.ghosts).forEach(function(id){ var o = L.ghosts[id]; o.y = L.y0 + o.lv * gap; });
212
+ // まとめで窓を合わせる道(いちばん深い道)
213
+ L.deep = null;
214
+ COLORS.forEach(function(c){ paths[c].forEach(function(q){ if (!L.deep || q.length > L.deep.code.length) L.deep = {color: c, code: q}; }); });
215
+ // 0 と 1 を書く枝:今の道(とまとめのすべての道)の節の 2 つの子へ。根から色の木への枝はいつも
216
+ var lab = {w: 1, b: 1};
217
+ COLORS.forEach(function(c){ paths[c].forEach(function(q){ for (var i = 0; i < q.length; i++){ var pre = LETTER[c] + q.slice(0, i); lab[pre + "0"] = 1; lab[pre + "1"] = 1; } }); });
218
+ if (cur && cur.bad){ var pre = LETTER[cur.color] + cur.code; lab[pre + "0"] = 1; lab[pre + "1"] = 1; }
219
+ L.lab = lab; L.sw = sw; L.gap = gap; L.cur = cur; L.paths = paths; L.both = both; L.focus = focus; L.sumGap = sumGap; L.sumLv = sumLv;
220
+ return L;
221
+ }
222
+ // 窓の位置(段の数で。0 は根が窓の上の段):たどる点の段 λ の 1 段下までが窓に入るように、λ から滑らかに決める
223
+ // 横は、たどる点(まとめはいちばん深い道の葉)を真ん中に置く:{lv: 窓の上の段, x: 左へずらす px}
224
+ camAt(k, t){
225
+ var L = this.lay[k];
226
+ if (!L.follow) return {lv: 0, x: 0};
227
+ var p = k ? this.model().plan[k - 1] : null, lam = 0, path = null;
228
+ if (p && (p.type === "tok" || p.type === "err")){
229
+ var T = this.times(k), P = this.props, n = L.cur.code.length + 1;
230
+ for (var i = 0; i < n; i++) lam += D.prog(t, T.s(i), 0.7 * P.bit);
231
+ path = L.cur;
232
+ var lv = Math.max(0, Math.min(L.camMax, lam + 1 - L.win));
233
+ if (L.cur.bad) lv = Math.max(0, Math.min(L.camMax, lam + 0.6 * D.prog(t, T.s(T.nb), 0.3) + 1 - L.win));
234
+ } else if (p && L.deep){ lam = L.deep.code.length + 1; path = L.deep; lv = Math.max(0, Math.min(L.camMax, lam + 1 - L.win)); }
235
+ else lv = 0;
236
+ // 道の節の x を λ で補う
237
+ var ids = ["r"];
238
+ if (path){ ids.push(LETTER[path.color]); for (var j = 1; j <= path.code.length; j++) ids.push(LETTER[path.color] + path.code.slice(0, j)); }
239
+ var xAt = function(u){
240
+ u = Math.max(0, u);
241
+ var i0 = Math.min(ids.length - 1, Math.floor(u)), i1 = Math.min(ids.length - 1, i0 + 1), f = Math.min(1, u - i0);
242
+ return D.lerp(L.nodes[ids[i0]].x, L.nodes[ids[i1]].x, f);
243
+ };
244
+ // 真ん中に置くのは、たどる点と、窓の上の段の道の節の中ほど(道が片側へ寄っても、窓の中の道が真ん中に来る)
245
+ var x = (xAt(lam) + xAt(Math.max(lv, lam - L.win + 1))) / 2;
246
+ return {lv: lv, x: x - this.W / 2};
247
+ }
248
+ mount(ctx){
249
+ var D2 = D, P = this.props, self = this, m = this.model(), dec = m.dec, d = this.data || {}, MK = P.mark;
250
+ var ja = this.ja = /^ja\b/i.test((ctx && ctx.lang) || "ja");
251
+ var Lb = this.Lb = Object.assign({}, ja ? NAMES : NAMES_EN, d.labels || {});
252
+ this.style.display = "block"; this.style.position = "relative";
253
+ var W0 = this.clientWidth, H = this.clientHeight;
254
+ this.W = W0 - 8; this.H = H;
255
+ this.G = {treeTop: 112, treeBottom: H - 96, scanY: H - 82};
256
+ var svg = D2.el("svg", {viewBox: "0 0 " + W0 + " " + H, width: W0, height: H, "aria-hidden": "true", style: "display:block;overflow:visible"}, this);
257
+ // 読み上げ:最後の形(復号した符号語の色とラン、終わりの一言)を 1 文で。作者の aria-label があればそれを使う(上書きしない)
258
+ if (this._aria === undefined) this._aria = this.getAttribute("aria-label");
259
+ this.setAttribute("role", "img");
260
+ if (!this._aria){
261
+ var er = dec.error;
262
+ var toks = dec.tokens.map(function(t){ return t.kind === "eol" ? "EOL" : Lb[t.color] + " " + t.run; });
263
+ var end = !er ? Lb.done : fmt0(Lb[er.kind], "").replace("{n}", er.pos + 1);
264
+ // 長い系列は初めの 12 語と語の数(1 文に収める)
265
+ if (toks.length > 12) toks = toks.slice(0, 12).concat([ja ? "…(全 " + toks.length + " 語)" : "… (" + toks.length + " codewords)"]);
266
+ this.setAttribute("aria-label", !dec.bits ? (ja ? "ハフマン木" : "Huffman tree") :
267
+ (ja ? "ハフマン木で " + dec.bits + " を復号:" : "Decoding " + dec.bits + " with the Huffman tree: ") + toks.join(ja ? "、" : ", ") + (ja ? "(" + end + ")" : " (" + end + ")"));
268
+ }
269
+ var root = D2.el("g", {transform: "translate(4 0)"}, svg);
270
+ this.lay = [];
271
+ for (var k = 0; k <= m.plan.length; k++) this.lay.push(this.layoutFor(k));
272
+ // 段が読めないほど詰まるとき(窓の段の間が WIN_GAP 未満、まとめがすべての道を描けない)は知らせる
273
+ var report = ctx && typeof ctx.report === "function" ? function(s){ ctx.report("layout", "[codetree] " + s); } : function(){};
274
+ var tight = this.lay.filter(function(L){ return L.follow && L.gap < WIN_GAP; })[0];
275
+ if (tight) report("the element is too short: the tree view shows " + tight.win + " levels only " + tight.gap.toFixed(1) + " px apart (at least " + WIN_GAP + " px are needed); make the element taller or show fewer levels at a time");
276
+ var sumL = this.lay[m.plan.length];
277
+ if (sumL && sumL.both && sumL.follow) report("the summary cannot show all decoded paths: the whole tree (" + sumL.sumLv + " levels deep) would be " + sumL.sumGap.toFixed(1) + " px per level (at least " + SUM_GAP + " px are needed), so it shows the deepest path only; make the element taller (about " + Math.ceil(sumL.sumLv * SUM_GAP + 252) + " px)");
278
+ // ---- 上:符号の系列と読む位置
279
+ var nb = dec.bits.length, label0 = D2.el("text", {x: 0, y: 34, "font-size": 24, fill: "var(--muted)"}, root);
280
+ label0.textContent = ja ? "符号" : "Code";
281
+ var bx0 = 64, cs = Math.max(22, Math.min(44, (this.W * 0.56 - bx0) / Math.max(1, nb))), bfs = Math.max(24, Math.min(32, cs * 0.75));
282
+ this.B = {x0: bx0, cs: cs};
283
+ var gBits = D2.el("g", {}, root);
284
+ this.ptr = D2.el("rect", {x: 0, y: 4, width: cs - 2, height: 40, rx: 6, fill: "none", stroke: "var(--emph)", "stroke-width": 3, opacity: 0}, gBits);
285
+ this.bitEls = [...dec.bits].map(function(b, j){
286
+ var e = D2.el("text", {x: bx0 + (j + 0.5) * cs, y: 35, "font-size": bfs, "text-anchor": "middle", "font-weight": 700, fill: "var(--muted)", "data-mark": MK + "-bit-" + (j + 1)}, gBits);
287
+ e.textContent = b; return e;
288
+ });
289
+ // 符号語ごとの括弧と結果(色の升目+ランの長さ)
290
+ var swatch = function(g, x, y, c, s){ return D2.el("rect", {x: x, y: y, width: s, height: s, fill: c === "black" ? "var(--fg)" : "var(--bg)", stroke: "var(--fg)", "stroke-width": 2}, g); };
291
+ var bRight = -Infinity;
292
+ this.brk = dec.tokens.map(function(t, i){
293
+ var g = D2.el("g", {opacity: 0, "data-mark": MK + "-cw-" + (i + 1)}, root);
294
+ var a = bx0 + t.start * cs + 4, b = bx0 + t.end * cs - 4;
295
+ D2.el("path", {d: "M" + a + ",48v8H" + b + "v-8", fill: "none", stroke: "var(--fg)", "stroke-width": 2}, g);
296
+ var s = t.kind === "eol" ? "EOL" : Lb[t.color] + " " + t.run, w = t.kind === "eol" ? textW(s, 24, self) : 20 + 6 + textW(s, 24, self), x = Math.max((a + b) / 2 - w / 2, bRight + 12);
297
+ bRight = x + w;
298
+ if (t.kind !== "eol") swatch(g, x, 64, t.color, 18);
299
+ var tx = D2.el("text", {x: t.kind === "eol" ? x : x + 26, y: 82, "font-size": 24, fill: "var(--fg)"}, g);
300
+ tx.textContent = s;
301
+ return g;
302
+ });
303
+ // 右上の一言(読む前と読んだ後の 2 つを重ねて入れ替える)
304
+ this.note = [0, 1].map(function(){ return D2.el("text", {x: self.W, y: 34, "font-size": 24, "text-anchor": "end", fill: "var(--fg)", opacity: 0}, root); });
305
+ // ---- 中:木。どの段階かで見せる節をまとめて作る
306
+ var gE = D2.el("g", {}, root), gP = D2.el("g", {}, root), gN = D2.el("g", {}, root);
307
+ var all = {};
308
+ this.lay.forEach(function(L){ Object.keys(L.nodes).forEach(function(id){ all[id] = L.nodes[id]; }); });
309
+ this.els = {};
310
+ Object.keys(all).forEach(function(id){
311
+ var o = all[id], e = {id: id, parent: o.parent};
312
+ if (o.parent){
313
+ e.edge = D2.el("line", {stroke: "var(--line)", "stroke-width": 2.5}, gE);
314
+ var top = o.parent === "r";
315
+ e.lab = D2.el("text", {"font-size": 24, "text-anchor": "middle", fill: "var(--muted)"}, gE);
316
+ e.lab.textContent = top ? Lb[o.c] : id.slice(-1);
317
+ e.bit = id.slice(-1);
318
+ }
319
+ var g = D2.el("g", {}, gN);
320
+ if (id !== "r") g.setAttribute("data-mark", MK + "-" + (o.c === "white" ? "w" : "b") + (o.code ? "-" + o.code : ""));
321
+ else g.setAttribute("data-mark", MK + "-root");
322
+ e.fan = D2.el("path", {d: "M0,13L-15,36L15,36Z", fill: "var(--line)", opacity: 0}, g);
323
+ if (o.leaf){
324
+ e.dot = D2.el("circle", {r: 13, fill: "var(--accent2)"}, g);
325
+ e.ring = D2.el("circle", {r: 13, fill: "none", stroke: "var(--emph)", "stroke-width": 3, opacity: 0}, g);
326
+ e.num = D2.el("text", {y: 42, "font-size": 24, "text-anchor": "middle", fill: "var(--fg)", "data-mark": MK + "-" + LETTER[o.c] + (o.leaf.run === "EOL" ? "eol" : o.leaf.run)}, g);
327
+ e.num.textContent = o.leaf.run === "EOL" ? "EOL" : String(o.leaf.run);
328
+ } else if (id === "r") e.dot = D2.el("circle", {r: 9, fill: "var(--fg)"}, g);
329
+ else e.dot = D2.el("circle", {r: o.lv === 1 ? 12 : 10, fill: o.c === "black" ? "var(--fg)" : "var(--bg)", stroke: "var(--fg)", "stroke-width": 2.5}, g);
330
+ e.g = g;
331
+ self.els[id] = e;
332
+ });
333
+ // 道(段階ごとに 1 本。まとめではすべて)
334
+ this.paths = m.plan.map(function(p, j){
335
+ var o = j < m.plan.length && p.type !== "sum" ? self.lay[j + 1].cur : null;
336
+ if (!o) return null;
337
+ var ids = ["r", LETTER[o.color]];
338
+ for (var i = 1; i <= o.code.length; i++) ids.push(LETTER[o.color] + o.code.slice(0, i));
339
+ var segs = ids.slice(1).map(function(id, i){ return {a: ids[i], b: id, line: D2.el("line", {stroke: "var(--emph)", "stroke-width": 6, "stroke-linecap": "round", opacity: 0}, gP)}; });
340
+ return {ids: ids, segs: segs, bad: o.bad};
341
+ });
342
+ // 表にない枝:破線の枝と × と、読んだビット
343
+ this.stub = D2.el("g", {opacity: 0, "data-mark": MK + "-bad"}, gP);
344
+ this.stubLine = D2.el("line", {stroke: "var(--emph)", "stroke-width": 4, "stroke-dasharray": "7 6"}, this.stub);
345
+ this.stubLab = D2.el("text", {"font-size": 24, "font-weight": 700, "text-anchor": "middle", fill: "var(--emph)"}, this.stub);
346
+ this.cross = D2.el("text", {"font-size": 34, "font-weight": 700, "text-anchor": "middle", fill: "var(--emph)"}, this.stub);
347
+ this.cross.textContent = "×";
348
+ // 窓の上の「ここまで」の行:今の符号語の色と、ここまで読んだビット
349
+ this.crumb = D2.el("text", {x: 0, y: this.G.treeTop + 14, "font-size": 24, fill: "var(--muted)", opacity: 0, "data-mark": MK + "-crumb"}, root);
350
+ this.crumbHead = D2.el("tspan", {}, this.crumb);
351
+ this.crumbBits = D2.el("tspan", {"font-weight": 700, fill: "var(--emph)"}, this.crumb);
352
+ this.token = D2.el("circle", {r: 9, fill: "var(--emph)", stroke: "var(--bg)", "stroke-width": 2.5, opacity: 0}, gP);
353
+ // ---- 下:復号した画素の並び
354
+ var G = this.G, sx0 = 70, total = m.runs.reduce(function(a, r){ return a + r.len; }, 0);
355
+ var sl = D2.el("text", {x: 0, y: G.scanY + 22, "font-size": 24, fill: "var(--muted)"}, root);
356
+ sl.textContent = Lb.scan;
357
+ var sc = total ? Math.min(20, (this.W - sx0) / total) : 0;
358
+ this.S = {x0: sx0, sc: sc, total: total};
359
+ var gS = D2.el("g", {"data-mark": MK + "-scan"}, root);
360
+ D2.el("line", {x1: sx0, x2: sx0 + total * sc, y1: G.scanY + 28, y2: G.scanY + 28, stroke: "var(--line)", "stroke-width": 2}, gS);
361
+ var cellEach = total <= 160 && sc >= 5;
362
+ this.cells = [];
363
+ var cell = 0;
364
+ dec.tokens.forEach(function(t, i){
365
+ if (t.kind === "eol") return;
366
+ var c = t.color === "black" ? "var(--fg)" : "var(--bg)";
367
+ if (cellEach) for (var q = 0; q < t.run; q++) self.cells.push({tok: i, n: q, e: D2.el("rect", {x: sx0 + (cell + q) * sc, y: G.scanY, width: sc, height: 28, fill: c, stroke: "var(--fg)", "stroke-width": 1, opacity: 0}, gS)});
368
+ else self.cells.push({tok: i, n: -1, run: t.run, x: sx0 + cell * sc, e: D2.el("rect", {x: sx0 + cell * sc, y: G.scanY, width: 0, height: 28, fill: c, stroke: "var(--fg)", "stroke-width": 1.5}, gS)});
369
+ cell += t.run;
370
+ });
371
+ // ランの括弧と長さ(メイクアップ+終端は「64 + 2 = 66」)
372
+ this.runEls = m.runs.map(function(r, j){
373
+ var g = D2.el("g", {opacity: 0, "data-mark": MK + "-run-" + (j + 1)}, root);
374
+ var br = D2.el("path", {fill: "none", stroke: "var(--fg)", "stroke-width": 2}, g);
375
+ var sw = swatch(g, 0, G.scanY + 46, r.color, 18);
376
+ var tx = D2.el("text", {y: G.scanY + 64, "font-size": 24, fill: "var(--fg)"}, g);
377
+ return {g: g, br: br, sw: sw, tx: tx, r: r};
378
+ });
379
+ }
380
+ // 色は style 属性に base.el と同じ書き方("fill:var(--x);")で書く。style.fill に代入すると "fill: var(--x);" になり、
381
+ // 作った直後と描き直した後で属性の字が変わる(docs/contract.md §3、tools/test-dom-order.mjs)
382
+ // 段階 k の今の道の進み(0〜1 の各辺)と、葉の後の進み
383
+ render(k, t, ctx){
384
+ var D2 = D, P = this.props, m = this.model(), dec = m.dec, self = this, Lb = this.Lb;
385
+ var preview = !!ctx.preview, PV = preview ? base.previewOpacity(this) : 0;
386
+ var p = k ? m.plan[k - 1] : null, T = k ? this.times(k) : null;
387
+ var A = k ? this.lay[k - 1] : this.lay[0], B = this.lay[k];
388
+ var mv = k ? D2.prog(t, 0, P.move) : 1;
389
+ // ---- 節の位置:両方にあれば滑らせ、片方だけなら近い祖先から伸びる/祖先へ縮む
390
+ var anc = function(L, id){ var o = L.nodes[id]; if (o) return o; var e = self.els[id]; while (e && !L.nodes[e.id]) e = self.els[e.parent]; return e ? L.nodes[e.id] : L.nodes.r; };
391
+ var cA = k ? this.camAt(k - 1, Infinity) : {lv: 0, x: 0}, cB = this.camAt(k, t), camA = cA.lv, camB = cB.lv, G = this.G, Wd = this.W;
392
+ var yOf = function(L, o, cam){ return L.y0 + (o.lv - cam) * L.gap; };
393
+ // 窓の上下(木が入るときは広く)。外へ出る節は 26 px で薄れる
394
+ var bnd = function(L){ return L.follow ? [L.y0 - 8, L.y0 + L.win * L.gap + 8] : [G.treeTop - 40, G.treeBottom + 40]; };
395
+ var bA = bnd(A), bB = bnd(B), wTop = D2.lerp(bA[0], bB[0], mv), wBot = D2.lerp(bA[1], bB[1], mv);
396
+ // 最後の姿(t = Infinity。印刷と台本の小さい絵)は薄れの途中を残さない:半分より濃ければ描き、薄ければ描かない
397
+ var solid = t === Infinity ? function(v){ return v >= 0.5 ? 1 : 0; } : function(v){ return v; };
398
+ var wf = function(y){ return solid(D2.clamp((y - (wTop - 26)) / 26) * D2.clamp((wBot + 26 - y) / 26)); };
399
+ // 横の縁でも薄れる(窓で追うときだけ、部品の左右の外へ出る節)
400
+ var wfx = function(x){ return solid(D2.clamp((x + 20) / 30) * D2.clamp((Wd + 20 - x) / 30)); };
401
+ var lim = wTop - 10;
402
+ // 線の始め(親)が窓の上にあれば、窓の上の縁で切る(線は上から流れ込む)
403
+ var clip = function(a, b){ if (a.y >= lim || b.y <= lim) return a; var f = (lim - a.y) / (b.y - a.y); return {x: D2.lerp(a.x, b.x, f), y: lim}; };
404
+ var pos = {};
405
+ Object.keys(this.els).forEach(function(id){
406
+ var a = A.nodes[id], b = B.nodes[id], pa = a || anc(A, id), pb = b || anc(B, id);
407
+ // 消える節は前半で消え、現れる節は後半で現れる(深い木を畳むときに途中の姿が重ならない)
408
+ var op = (a ? Math.max(0, 1 - 2 * mv) : 0) + (b ? Math.max(0, 2 * mv - 1) : 0);
409
+ if (a && b) op = 1;
410
+ var fan = ((a && a.kind === "fan" ? 1 - mv : 0) + (b && b.kind === "fan" ? mv : 0));
411
+ var y = D2.lerp(yOf(A, pa, camA), yOf(B, pb, camB), mv), x = D2.lerp(pa.x - cA.x, pb.x - cB.x, mv);
412
+ pos[id] = {x: x, y: y, op: op * wf(y) * wfx(x), fan: fan,
413
+ lab: (A.lab[id] && a ? (b ? 1 - mv : 1) : 0) + (B.lab[id] && b ? (a ? mv : 1) : 0)};
414
+ });
415
+ // 窓の上の縁:親が窓の外にある節は(今の道の節のほかは)出さない。縁に枝のない節が浮かないように
416
+ var keep = {r: 1}, kp = B.cur || B.deep;
417
+ if (kp){ keep[LETTER[kp.color]] = 1; for (var j0 = 1; j0 <= kp.code.length; j0++) keep[LETTER[kp.color] + kp.code.slice(0, j0)] = 1; }
418
+ Object.keys(pos).forEach(function(id){
419
+ var par = self.els[id].parent;
420
+ if (par && !keep[id]) pos[id].op *= solid(D2.clamp((pos[par].y - (lim - 30)) / 30));
421
+ });
422
+ // ---- 今の道・前の道
423
+ var segP = function(i){ return T ? D2.prog(t, T.s(i), 0.7 * P.bit) : 0; };
424
+ var onPath = {};
425
+ this.paths.forEach(function(q, j){
426
+ if (!q) return;
427
+ var st = j + 1, dr, op;
428
+ if (p && p.type === "sum"){ op = 1; dr = function(){ return st === k - 1 ? 1 : D2.prog(t, P.move * 0.5, 0.9); }; }
429
+ else if (st === k){ op = 1; dr = segP; }
430
+ else if (st === k - 1){ op = Math.max(0, 1 - 2 * mv); dr = function(){ return 1; }; }
431
+ else { op = 0; dr = function(){ return 0; }; }
432
+ q.segs.forEach(function(s, i){
433
+ var a = pos[s.a], b = pos[s.b], f = dr(i), tip = {x: D2.lerp(a.x, b.x, f), y: D2.lerp(a.y, b.y, f)}, a0 = clip(a, tip);
434
+ s.line.setAttribute("x1", a0.x.toFixed(2)); s.line.setAttribute("y1", a0.y.toFixed(2));
435
+ s.line.setAttribute("x2", tip.x.toFixed(2)); s.line.setAttribute("y2", tip.y.toFixed(2));
436
+ s.line.setAttribute("opacity", f > 0 && tip.y > lim ? (op * Math.min(1, wf(tip.y) * 1.5)).toFixed(3) : 0);
437
+ if (f >= 1 && op > 0.5) onPath[s.b] = 1;
438
+ });
439
+ });
440
+ // ---- 節と枝
441
+ var hit = {}; // 葉に着いた(葉の数字を強調)
442
+ if (p && p.type === "tok"){ var tk = dec.tokens[p.tok]; if (t >= T.A) hit[LETTER[tk.color] + tk.code] = 1; }
443
+ if (p && p.type === "sum") dec.tokens.forEach(function(x){ hit[LETTER[x.color] + x.code] = 1; });
444
+ Object.keys(this.els).forEach(function(id){
445
+ var e = self.els[id], q = pos[id];
446
+ e.g.setAttribute("transform", "translate(" + q.x.toFixed(2) + " " + q.y.toFixed(2) + ")");
447
+ e.g.setAttribute("opacity", q.op.toFixed(3));
448
+ e.fan.setAttribute("opacity", q.fan.toFixed(3));
449
+ if (e.edge){
450
+ var a = pos[e.parent], a1 = clip(a, q);
451
+ e.edge.setAttribute("x1", a1.x.toFixed(2)); e.edge.setAttribute("y1", a1.y.toFixed(2));
452
+ e.edge.setAttribute("x2", q.x.toFixed(2)); e.edge.setAttribute("y2", q.y.toFixed(2));
453
+ // 親が窓の上にある枝は描かない(入ってくる道は道の線が描く)
454
+ e.edge.setAttribute("opacity", a.y < lim ? 0 : Math.min(q.op, 1.6 * a.op).toFixed(3));
455
+ // 枝の字:根からの枝は線の中ほどの上、0 と 1 は親に近いところの外側
456
+ // 線に垂直に、外側(0 は左上、1 は右上)へ離す
457
+ var top = e.parent === "r", lp = labelAt(a, q, top ? 0.5 : 0.42, top ? 20 : 17);
458
+ e.lab.setAttribute("x", lp.x.toFixed(2)); e.lab.setAttribute("y", lp.y.toFixed(2));
459
+ e.lab.setAttribute("opacity", Math.min(q.op, q.lab, wf(lp.y - 8)).toFixed(3));
460
+ e.lab.setAttribute("style", "fill:" + (onPath[id] ? "var(--emph)" : "var(--muted)") + ";");
461
+ e.lab.setAttribute("font-weight", onPath[id] ? 700 : 400);
462
+ }
463
+ if (e.num){
464
+ var h = !!hit[id];
465
+ e.num.setAttribute("style", "fill:" + (h ? "var(--emph)" : "var(--fg)") + ";");
466
+ e.num.setAttribute("font-weight", h ? 700 : 400);
467
+ // 着いたときに輪が 1 回広がる(最終状態には出ない)
468
+ var u = p && p.type === "tok" && h && t !== Infinity ? (t - T.A) / 0.6 : -1;
469
+ if (u > 0 && u < 1){ e.ring.setAttribute("r", (13 + 14 * u).toFixed(2)); e.ring.setAttribute("opacity", (1 - u).toFixed(3)); }
470
+ else e.ring.setAttribute("opacity", 0);
471
+ }
472
+ });
473
+ // ---- 道をたどる点
474
+ var tokOp = 0, tp = pos.r;
475
+ if (p && (p.type === "tok" || p.type === "err") && t !== Infinity){
476
+ var q = this.paths[k - 1], i = 0;
477
+ while (i < q.segs.length - 1 && segP(i) >= 1) i++;
478
+ var s = q.segs[i], f = segP(i), a = pos[s.a], b = pos[s.b];
479
+ tp = {x: D2.lerp(a.x, b.x, f), y: D2.lerp(a.y, b.y, f)};
480
+ tokOp = D2.prog(t, T.s(0) - 0.2, 0.2) * (1 - D2.prog(t, T.A, 0.4));
481
+ }
482
+ this.token.setAttribute("cx", tp.x.toFixed(2)); this.token.setAttribute("cy", tp.y.toFixed(2));
483
+ this.token.setAttribute("opacity", tokOp.toFixed(3));
484
+ // 表にない枝の × (誤りの段階と、まとめ)
485
+ var cx = 0, stubOn = false;
486
+ if (p && dec.error && dec.error.kind === "invalid" && (p.type === "err" || p.type === "sum")){
487
+ var er = dec.error, last = pos[LETTER[er.color] + errPath(er)], gh = this.lay[k].ghosts[LETTER[er.color] + er.code];
488
+ if (gh){
489
+ stubOn = true;
490
+ gh = {x: gh.x - cB.x, y: yOf(this.lay[k], gh, camB)};
491
+ this.stubLine.setAttribute("x1", last.x.toFixed(2)); this.stubLine.setAttribute("y1", last.y.toFixed(2));
492
+ this.stubLine.setAttribute("x2", gh.x.toFixed(2)); this.stubLine.setAttribute("y2", (gh.y - 14).toFixed(2));
493
+ this.cross.setAttribute("x", gh.x.toFixed(2)); this.cross.setAttribute("y", (gh.y + 12).toFixed(2));
494
+ var sp = labelAt(last, gh, 0.42, 17);
495
+ this.stubLab.setAttribute("x", sp.x.toFixed(2)); this.stubLab.setAttribute("y", sp.y.toFixed(2));
496
+ this.stubLab.textContent = er.code.slice(-1);
497
+ cx = p.type === "err" ? D2.prog(t, T.s(T.nb), 0.3) : 1;
498
+ }
499
+ }
500
+ // 描かないとき(opacity 0)は、前に描いた位置と字を残さない(作った直後の姿に戻す。呼ぶ順で DOM が変わらないように)
501
+ if (!stubOn){
502
+ var clr = function(e, names){ names.forEach(function(nm){ e.removeAttribute(nm); }); };
503
+ clr(this.stubLine, ["x1", "y1", "x2", "y2"]); clr(this.cross, ["x", "y"]); clr(this.stubLab, ["x", "y"]);
504
+ this.stubLab.textContent = "";
505
+ }
506
+ this.stub.setAttribute("opacity", cx.toFixed(3));
507
+ // ---- 系列:読んだ符号語は fg、今の符号語の読んだビットは emph、まだのビットは muted
508
+ var cur = p && p.type === "tok" ? dec.tokens[p.tok] : p && p.type === "err" ? dec.error : null;
509
+ var doneEnd = p && p.type === "sum" ? dec.bits.length : cur ? cur.start : 0;
510
+ var readUpTo = -1; // 今の符号語で読んだビット(の桁)
511
+ if (cur && T) for (var j = 1; j <= T.nb; j++) if (t >= T.s(j)) readUpTo = cur.start + j - 1;
512
+ if (p && p.type === "err" && dec.error.kind === "invalid" && t >= T.s(T.nb)) readUpTo = dec.error.pos;
513
+ this.bitEls.forEach(function(e, j){
514
+ var c = j < doneEnd ? "var(--fg)" : j <= readUpTo ? "var(--emph)" : "var(--muted)";
515
+ if (p && p.type === "sum" && dec.error && dec.error.kind !== "dangling" && j >= dec.error.start) c = "var(--emph)";
516
+ if (p && p.type === "tok" && j >= cur.start && j < cur.end && t >= T.F0) c = "var(--fg)";
517
+ e.setAttribute("style", "fill:" + (c) + ";");
518
+ });
519
+ var po = 0, pj = 0;
520
+ if (cur && T && t !== Infinity){
521
+ var jj = 0;
522
+ for (var j2 = 1; j2 <= T.nb; j2++) if (t >= T.s(j2)) jj = j2;
523
+ pj = cur.start + (jj >= 2 ? D2.lerp(jj - 2, jj - 1, D2.prog(t, T.s(jj), 0.2)) : 0);
524
+ po = D2.prog(t, T.s(1) - 0.25, 0.25) * (p.type === "tok" ? 1 - D2.prog(t, T.F0, 0.4) : 1);
525
+ }
526
+ if (p && p.type === "err" && t === Infinity){ pj = dec.error.start + T.nb - 1; po = 1; }
527
+ this.ptr.setAttribute("x", (this.B.x0 + pj * this.B.cs + 1).toFixed(2));
528
+ this.ptr.setAttribute("opacity", po.toFixed(3));
529
+ // 符号語の括弧:読み終えた符号語
530
+ var tokDone = function(i){ if (!p) return 0; if (p.type === "sum" || p.type === "err") return 1; return i < p.tok ? 1 : i === p.tok ? D2.prog(t, T.F0, 0.3) : 0; };
531
+ // 先を薄く見せる表示:後の段階で読む符号語の括弧と、復号する画素を、最後の形のままテーマの --preview-opacity(なければ 0.45)で描く。
532
+ // 木の道は段階ごとに窓が動くので先は描かない
533
+ var ahead = function(i){ return preview && (!p || (p.type === "tok" && i > p.tok)); };
534
+ this.brk.forEach(function(g, i){ g.setAttribute("opacity", ahead(i) ? PV.toFixed(3) : tokDone(i).toFixed(3)); });
535
+ // ---- 画素:葉に着いてから升目を左から塗る
536
+ var fillOf = function(i){ if (!p) return 0; if (p.type === "sum" || p.type === "err") return 1; return i < p.tok ? 1 : i === p.tok ? D2.prog(t, T.F0, P.fill) : 0; };
537
+ var shown = {}; // 符号語ごとの塗った升目の数
538
+ dec.tokens.forEach(function(x, i){ shown[i] = x.kind === "eol" ? 0 : Math.round(x.run * fillOf(i)); });
539
+ this.cells.forEach(function(c){
540
+ var ah = ahead(c.tok);
541
+ if (c.n >= 0) c.e.setAttribute("opacity", ah ? PV : c.n < shown[c.tok] ? 1 : 0);
542
+ else {
543
+ c.e.setAttribute("width", ((ah ? c.run : shown[c.tok]) * self.S.sc).toFixed(2));
544
+ if (ah) c.e.setAttribute("opacity", PV); else c.e.removeAttribute("opacity");
545
+ }
546
+ });
547
+ // ランの括弧と長さ。字は重ならないように左から順に置く
548
+ var right = -Infinity, S = this.S, G = this.G;
549
+ this.runEls.forEach(function(o){
550
+ var vis = o.r.toks.filter(function(i){ return shown[i] > 0; });
551
+ if (!vis.length){
552
+ o.g.setAttribute("opacity", 0);
553
+ o.br.removeAttribute("d"); o.sw.setAttribute("x", "0"); o.tx.removeAttribute("x"); // 作った直後と同じ(色の升目は x="0" で作る。消すと属性の順が変わる)
554
+ o.tx.textContent = "";
555
+ return;
556
+ }
557
+ var len = vis.reduce(function(a, i){ return a + shown[i]; }, 0);
558
+ var parts = vis.map(function(i){ return dec.tokens[i].run; });
559
+ var full = vis.every(function(i){ return shown[i] === dec.tokens[i].run; });
560
+ var s = Lb[o.r.color] + " " + (parts.length > 1 ? parts.join(self.ja ? " + " : " + ") + (self.ja ? " = " : " = ") + parts.reduce(function(a, b){ return a + b; }, 0) : String(full ? parts[0] : len));
561
+ var a = S.x0 + o.r.start * S.sc, b = a + len * S.sc;
562
+ o.br.setAttribute("d", "M" + (a + 1) + "," + (G.scanY + 34) + "v6H" + Math.max(a + 2, b - 1) + "v-6");
563
+ var w = 26 + textW(s, 24, self), x = Math.max((a + b) / 2 - w / 2, right + 16, S.x0);
564
+ x = Math.min(x, self.W - w);
565
+ right = x + w;
566
+ o.sw.setAttribute("x", x.toFixed(2));
567
+ o.tx.setAttribute("x", (x + 26).toFixed(2));
568
+ o.tx.textContent = s;
569
+ o.g.setAttribute("opacity", 1);
570
+ });
571
+ // ---- 「ここまで」の行:窓が根から離れたときだけ。今の段階でまだ読んでいなければ前の符号語
572
+ var cw = null, nread = 0;
573
+ if (p && (p.type === "tok" || p.type === "err")){
574
+ cw = this.lay[k].cur; for (var j3 = 1; j3 <= T.nb; j3++) if (t >= T.s(j3)) nread = j3;
575
+ cw = {color: cw.color, code: (cw.bad || cw.code).slice(0, nread)};
576
+ } else if (p && p.type === "sum") cw = B.deep;
577
+ if ((!cw || !cw.code) && k > 1 && this.lay[k - 1].cur) cw = this.lay[k - 1].cur;
578
+ var camNow = D2.lerp(camA, camB, mv), co = cw && cw.code ? D2.clamp((camNow - 0.3) / 0.4) * (B.follow ? 1 : Math.max(0, 1 - 2 * mv)) : 0;
579
+ if (cw){
580
+ this.crumbHead.textContent = fmt0(Lb.crumb, cw.color ? Lb[cw.color] : "") + " ";
581
+ this.crumbBits.textContent = (cw.code.match(/.{1,4}/g) || []).join(" ");
582
+ } else { this.crumbHead.textContent = ""; this.crumbBits.textContent = ""; }
583
+ this.crumb.setAttribute("opacity", co.toFixed(3));
584
+ // ---- 右上の一言
585
+ var fmt = function(s, o){ return s.replace("{c}", o.c ? Lb[o.c] : "").replace("{n}", o.n || ""); };
586
+ var other = function(c){ return c === "white" ? "black" : "white"; };
587
+ var n0 = "", n1 = "", sw = 0;
588
+ if (!p){ n1 = Lb.start; sw = 1; }
589
+ else if (p.type === "tok"){
590
+ var x = dec.tokens[p.tok];
591
+ n0 = fmt(Lb.read, {c: x.color});
592
+ n1 = fmt(Lb[x.kind], {c: x.kind === "term" ? other(x.color) : x.color});
593
+ sw = D2.prog(t, T.F0, 0.4);
594
+ } else if (p.type === "err"){
595
+ n0 = fmt(Lb.read, {c: dec.error.color});
596
+ n1 = fmt(Lb[dec.error.kind], {n: dec.error.pos + 1});
597
+ sw = D2.prog(t, T.s(T.nb), 0.4);
598
+ } else {
599
+ n0 = ""; n1 = !dec.error ? Lb.done : fmt(Lb[dec.error.kind], {n: dec.error.pos + 1}); sw = D2.prog(t, P.move, 0.4);
600
+ }
601
+ this.note[0].textContent = n0; this.note[1].textContent = n1;
602
+ // 前の一言が消えてから次を出す(重ねて入れ替えると読めない)
603
+ this.note[0].setAttribute("opacity", Math.max(0, 1 - 2 * sw).toFixed(3)); this.note[1].setAttribute("opacity", Math.max(0, 2 * sw - 1).toFixed(3));
604
+ this.note[1].setAttribute("style", "fill:" + (p && (p.type === "err" || p.type === "sum") ? "var(--emph)" : "var(--fg)") + ";");
605
+ }
606
+ };
607
+ });
@@ -0,0 +1,23 @@
1
+ ---
2
+ summary: Balance at expectation
3
+ steps: Weights → pivot → sum (3)
4
+ props:
5
+ dur: Seconds
6
+ digits: Decimal places
7
+ size: Text px
8
+ ---
9
+
10
+ JSON:
11
+ - `values`: 2–12 distinct numbers.
12
+ - `probs`: sum to 1 (else reported).
13
+ - `name`: X.
14
+
15
+ <!-- llm: omit -->
16
+ Bars proportional to probabilities. Pivot: minimum → E[X]. Moment controls tilt, clamped ±8°, then levels. Beam, bars and ticks rotate together. Starts horizontal; tilts at step 2. Final E and mtext sum; first 6 terms then …; computation and accessible weights include all terms. Negative terms parenthesized.
17
+
18
+ Invalid totals stay unnormalized. Out-of-range probabilities, mismatched lengths, duplicates or unrepresentable calculations: report, no draw. dur 1.5s; digits 0–10 (2), trailing zeros removed; size 0=26px, ≥24. Bad props default/report.
19
+
20
+ 1184×440px suggested. Scaled coordinates, nice ticks. Close bars, labels or long sums report size.
21
+
22
+ ctx.lang selects ja/en. Font boxes ≥1.43em plus gap. Image role, final summary; preserve author label, hide SVG. No answer preview. Fictional demo.
23
+ <!-- llm: end -->