guidex 0.1.1

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.
package/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # Guidex
2
+
3
+ Guidex — plainchant in code, after Guido of Arezzo, whose font renders
4
+ the scores.
5
+
6
+ A web project for rendering plainchant scores from a compact script
7
+ notation, set in Book Antiqua with a true red (`#d40000`) as its hallmark.
8
+
9
+ ## Stack
10
+
11
+ - [Vite](https://vite.dev/) 8 + [TypeScript](https://www.typescriptlang.org/) — no framework, just DOM
12
+ - [Vitest](https://vitest.dev/) 4 + jsdom for tests, [ESLint](https://eslint.org/) for linting
13
+ - Plainchant notation via the custom `Guidohu` glyph font (`public/fonts/guidohu.ttf`)
14
+
15
+ ## Development
16
+
17
+ ```sh
18
+ npm install
19
+ npm run dev # dev server at http://localhost:5173
20
+ npm run build # type-check and production build
21
+ npm test # vitest run (jsdom environment)
22
+ npm run lint # eslint
23
+ ```
24
+
25
+ CI runs lint and unit tests on every push.
26
+
27
+ ## Project layout
28
+
29
+ - `src/main.ts` — the main page: the interactive editor at the top
30
+ (`#editor` with a multi-line textarea, sample-picker chips, quick-tips),
31
+ then the intro prose, the Gaudeámus score, the syllable breakdown table,
32
+ and the `GuidoRender` recap score under `#test-score`
33
+ - `src/guidoScript.ts` — the `GuidoScript` class: parses script notation
34
+ into clef, syllables (`{ text, notes, wordEnd }`), and whole words;
35
+ `toString()` round-trips; `normalized` applies typing conveniences
36
+ - `src/guidoRender.ts` — `GuidoRender`: renders a score from a
37
+ `GuidoScript` or string using canvas measurements (2× scale,
38
+ assembled-prefix measurement); supports multi-line scores via a
39
+ `maxWidthPx` parameter, with staff breaking and in-group splitting at
40
+ dashes; also exports `SCORE_MARKERS`
41
+ - `src/style.css` — the global styleset (fonts, red palette, typography,
42
+ score components); imports `src/explainers.css`
43
+ - `src/explainers.css` — main-page styles: page layout, editor, sample
44
+ chips, syllable table, debug guides
45
+ - `tests/` — Vitest suite: parser round-trip/normalization, placement
46
+ rules, junction widths, and an overlap-invariant sweep (`setup.ts`
47
+ installs a deterministic fake canvas and fake font loading)
48
+ - `public/fonts/` — Book Antiqua family and the Guidohu notation font
49
+ - `public/favicon.svg` — monospace `(g)` on five staff lines, paper + red
50
+
51
+ ## The script notation
52
+
53
+ The source alternates a leading clef group with syllables, each followed by
54
+ its notes:
55
+
56
+ ```
57
+ (MX)Ki(3)rály(3)ként(5) tró(7i)nol(7-:) *() az(7i) Úr(5-/) mind(5)ö(4t)rök(3)ké.(3-.)
58
+ ```
59
+
60
+ - The first parenthesized group is the starting clef with note modifiers.
61
+ - Every syllable of text precedes the parenthesized group of notes it is
62
+ sung to.
63
+ - Whitespace in the source marks word boundaries between syllables.
64
+ - The Guidohu dash (`-`) is pure whitespace; spaces are forbidden inside a
65
+ notes line.
66
+ - Marker characters (`,.:?'"+!%/=()ÖÜÓ;` = `SCORE_MARKERS`) carry no
67
+ pitch; at the edges of a note group they count as whitespace, so a
68
+ junction after a marker-ending group needs only 2 dashes instead of 3.
69
+
70
+ In the rendered score, notes are set in the Guidohu font at twice the
71
+ surrounding text size. Notes are measured with a hidden canvas after
72
+ webfonts load, and each syllable is aligned against the measured ink of
73
+ its notes — left-aligned when the notes are wider, centered otherwise.
74
+ When a finite max width is given, the score breaks into multiple line
75
+ pairs, repeating the clef on every line and hanging a hyphen at line ends
76
+ that split a word.
77
+
78
+ ## Embedding on other sites (issue #30)
79
+
80
+ `npm run build:embed` produces a self-contained `dist-embed/` folder that
81
+ can be hosted on any static server or CDN and dropped into a site's
82
+ `<head>`:
83
+
84
+ ```html
85
+ <link rel="stylesheet" href="https://your-host/path/guidex-embed.min.css">
86
+ <script src="https://your-host/path/guidex-embed.min.js"></script>
87
+ ```
88
+
89
+ Any element classed `guido-script` (or `guidex-script`) renders its text
90
+ content as a score, in place:
91
+
92
+ ```html
93
+ <div class="guido-script"
94
+ data-max-width="600"
95
+ data-style="Vesperale">(MX)Ki(3)rály(3)ként(5) tró(7i)nol(7-:)</div>
96
+ ```
97
+
98
+ Parameters ride data attributes on the div; site-wide defaults can be set
99
+ with `GuidexEmbed.init({ maxWidth: 'auto', style: 'BNS', … })`. Element
100
+ attributes win over init options. New elements added after page load
101
+ render automatically; already-rendered elements re-render when
102
+ `GuidexEmbed.renderAll()` is called.
103
+
104
+ | Attribute | Values | Default |
105
+ | ----------------- | ------------------------------- | -------------- |
106
+ | `data-max-width` | pixels, or `auto` (fits the element, re-rendered on resize) | `auto` |
107
+ | `data-style` | `BNS`, `Vesperale` | `BNS` |
108
+ | `data-bold` | boolean | `false` |
109
+ | `data-text-px` | base text size in px (notes render at 2×) | `18` |
110
+ | `data-transpose` | pitch-row shift (see `transposeBy`) | `0` |
111
+ | `data-debug` | boolean (measurement titles on spans) | `false` |
112
+
113
+ The build emits `guidex-embed.min.js` (minified IIFE, global
114
+ `GuidexEmbed`), `guidex-embed.min.css`, the `fonts/` folder, and a
115
+ `demo.html` — open the demo after building (e.g. `npx serve dist-embed`)
116
+ to see a rendered example. A failed render (unknown style, unloadable
117
+ font) leaves an error note in the target instead of throwing.
118
+
119
+ ## State
120
+
121
+ Parsing and rendering both work end to end: a script is parsed into
122
+ syllables and words, and the renderer produces multi-line scores with
123
+ correct spacing, junctions, and dash placement. The main page hosts a
124
+ live editor with sample scores. See `CLAUDE.md` for the detailed
125
+ rendering model and project guidance.
@@ -0,0 +1,34 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Guidex embed demo</title>
7
+ <link rel="stylesheet" href="./guidex-embed.min.css" />
8
+ <script src="./guidex-embed.min.js"></script>
9
+ <style>
10
+ body { font-family: Georgia, serif; max-width: 960px; margin: 2rem auto; padding: 0 1rem; }
11
+ .demo-box { border: 1px dashed #bbb; padding: 0 1rem; margin: 1.5rem 0; }
12
+ h2 { font-size: 1rem; font-weight: normal; color: #666; }
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <h1>Guidex embed demo</h1>
17
+ <p>Three <code>.guido-script</code> divs rendered by <code>guidex-embed.min.js</code>.</p>
18
+
19
+ <div class="demo-box">
20
+ <h2>Default: auto width, BNS style</h2>
21
+ <div class="guido-script">(MX)Ki(3)rály(3)ként(5) tró(7i)nol(7-:) **() az(7i) Úr(5-/) mind(5)ö(4t)rök(3)ké.(3-.)</div>
22
+ </div>
23
+
24
+ <div class="demo-box" style="width: 420px">
25
+ <h2>data-max-width="400"</h2>
26
+ <div class="guido-script" data-max-width="400">(M)Az(2) A(3)tyá(2)nak(1) or(4)szá(5)gá(456u)ból(7-:) e(7) vi(8)lág(7)ra(6) Is(5)ten(7) jö(zZ5)ve,(4-,) ne(4)vez(5)te(7)ték(6) ö(5)rök(5) Is(4)ten(5)nek,(5-%) üd(5)vös(7)ség(4)nek(2) kút(44)fe(5)jé(5)nek,(tGW1-:) min(4)de(4)nek(4)nek(2) kik(4) vol(5)ta(5)nak,(4-:) és(5) kik(7) lesz(4)nek(tT4) ö(eE2)rök(1w)ké.(2-.)</div>
27
+ </div>
28
+
29
+ <div class="demo-box">
30
+ <h2>data-style="Vesperale" data-transpose="2"</h2>
31
+ <div class="guido-script" data-style="Vesperale" data-transpose="2">(MX)Ki(3)rály(3)ként(5) tró(7i)nol(7-:) **() az(7i) Úr(5-/) mind(5)ö(4t)rök(3)ké.(3-.)</div>
32
+ </div>
33
+ </body>
34
+ </html>
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,2 @@
1
+ @font-face{font-family:Book Antiqua;src:url(./fonts/bkant.ttf)format("truetype");font-weight:400;font-style:normal;font-display:swap}@font-face{font-family:Book Antiqua;src:url(./fonts/antquai.ttf)format("truetype");font-weight:400;font-style:italic;font-display:swap}@font-face{font-family:Book Antiqua;src:url(./fonts/antquab.ttf)format("truetype");font-weight:700;font-style:normal;font-display:swap}@font-face{font-family:Book Antiqua;src:url(./fonts/antquabi.ttf)format("truetype");font-weight:700;font-style:italic;font-display:swap}@font-face{font-family:Guidohu;src:url(./fonts/guidohu.ttf)format("truetype");font-display:swap}@font-face{font-family:Liturgy;src:url(./fonts/liturgy.ttf)format("truetype");font-weight:400;font-display:swap}.guidex-render{--red:#d40000;--text:#2b2b2b;--text-h:#1a1a1a;color:var(--text)}@media (prefers-color-scheme:dark){.guidex-render{--red:#ff4d4d;--text:#d4cdc7;--text-h:#f5f0ea}}.guidex-render .guido{color:var(--text-h);padding-block:.3em;font-family:Guidohu,Book Antiqua,Palatino Linotype,Palatino,Georgia,serif;font-size:200%;line-height:120%;position:relative}.guidex-render p{margin:0;line-height:normal}.guidex-render p:first-letter{color:inherit;font-weight:inherit;float:none}.guidex-render .syl,.guidex-render .guido-break{position:relative}.guidex-render .guidex-error{color:var(--red);margin:0;font:14px/1.4 Book Antiqua,Palatino Linotype,Palatino,Georgia,serif}
2
+ /*$vite$:1*/
@@ -0,0 +1 @@
1
+ var GuidexEmbed=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class{source;constructor(e,t={}){this.source=(t.transforms??[]).reduce((e,t)=>t.apply(e),e)}get clef(){return this.normalized.match(/^\s*\(([^)]*)\)/)?.[1]}get syllables(){let e=[...this.normalized.replace(/^\s*\([^)]*\)/,``).matchAll(/((?:[^()\\]|\\.)*)\(((?:[^)\\]|\\.)*)\)/g)];return e.map(([,t,r],i)=>({text:n(t.trim()),notes:n(r),wordEnd:i===e.length-1||/^\s/.test(e[i+1][1])}))}get normalized(){let e=this.source;return(e.match(/(^|[^\\])\(/g)??[]).length>(e.match(/(^|[^\\])\)/g)??[]).length&&(e+=`)`),/(^|[^\\])\)$/.test(e)||(e+=`()`),e}get words(){let e=[],t;for(let n of this.syllables)t??={text:``,syllables:[]},t.syllables.push({text:n.text,notes:n.notes}),t.text=t.syllables.map(e=>e.text).join(``),n.wordEnd&&(e.push(t),t=void 0);return e}toString(){let e=this.syllables.map((e,t)=>`${t>0&&this.syllables[t-1].wordEnd?` `:``}${r(e.text)}(${r(e.notes)})`);return`${this.clef===void 0?``:`(${this.clef})`}${e.join(``)}`}};function n(e){return e.replace(/\\(.)/g,`$1`)}function r(e){return e.replace(/\\/g,`\\\\`).replace(/[()]/g,e=>`\\${e}`)}var i=[`ðñ0123456789öüó`,`^]qwertzuiopõú`,`ÝÞQWERTZUIOPÕÚ`,`×sdfghjkléáû`,`ØÙDFGHJKLÉÁÛ`,`÷øaASÍYXCVBN$m`,`ÈÊËÌÎÏÐÑÒþ¬ßòôùý`,`“”•–—˜™š›œº»`,`®−§ÿ"+!%/=()ÖÜÓ`,`Ÿ¡¢£[¥¦©ª«¬`,`~¯‚ƒ„…†‡ˆ‰Š‹Œ‘’`,`àâãäåæçèêëìîï`,`½°±²³´μ·¾¸¹`];function a(e,t,n){for(let r of i){let i=r.indexOf(e);if(i===-1)continue;let a=i+t;return a<0||a>=r.length?n===`null`?`\0`:r[Math.max(0,Math.min(r.length-1,a))]:r[a]}}function o(e,t,n){let r=``;for(let i=0;i<e.length;i++){let o=e[i],s=o===`\\`&&e[i+1]!==void 0,c=a(s?e[i+1]:o,t,n);if(c===void 0){r+=s?o+e[i+1]:o,s&&i++;continue}r+=`\\()`.includes(c)?`\\${c}`:c,s&&i++}return r}function s(e,t={}){let n=t.overflow??`clamp`;return{name:`transpose`,apply:t=>{if(e===0)return t;let r=t.match(/^\s*\(([^)]*)\)/),i=r?r[0].length:0;return t.slice(0,i)+t.slice(i).replace(/((?:[^()\\]|\\.)*)\(((?:[^)\\]|\\.)*)\)/g,(t,r,i)=>`${r}(${o(i,e,n)})`)}}}var c={chars:new Set([`*`,`†`]),fontFamily:`'Times New Roman', Times, serif`},l={weight:`400`,scale:.7,raise:.2},u={BNS:{textFontStack:`'Book Antiqua', Georgia, serif`,notesFontStack:`'Guidohu'`,markerGlyphs:c,verifiedFonts:[{family:`'Book Antiqua'`,weight:`400`},{family:`'Book Antiqua'`,weight:`700`},{family:`'Guidohu'`,weight:`400`},{family:`'Liturgy'`,weight:`700`}],decorations:{__:{fontFamily:`'Liturgy', serif`,weight:`700`,color:`var(--red)`},_:{uppercase:!0,scale:.7,color:`var(--red)`},"*":{weight:`700`,color:`var(--red)`,initial:!0}},specialStarStyle:{fontFamily:c.fontFamily,weight:`700`,scale:1,measureScale:.95,raise:.1,color:`var(--red)`},padding:12},Vesperale:{textFontStack:`'Times New Roman', Times, serif`,notesFontStack:`'Guidohu'`,verifiedFonts:[{family:`'Times New Roman', Times, serif`,weight:`400`},{family:`'Times New Roman', Times, serif`,weight:`700`},{family:`'Guidohu'`,weight:`400`},{family:`'Liturgy'`,weight:`700`}],decorations:{__:{fontFamily:`'Liturgy', serif`,weight:`700`},_:{scale:.8,italic:!0},"*":{weight:`700`,initial:!0}},specialStarStyle:{fontFamily:c.fontFamily,weight:`700`,scale:1,measureScale:.95,raise:.2},padding:12}};function d(e,t,n){if(e.length===0)return[];if(e===`**`)return[{text:`∗`,style:{...t.specialStarStyle}}];let r=e,i={};for(let n of[`__`,`_`]){let a=t.decorations[n];if(a&&e.length>=n.length*2+1&&e.startsWith(n)&&e.endsWith(n)){r=e.slice(n.length,-n.length),i={...a};break}}let a=t.decorations[`*`],o=[],s=r,c=``;for(let e=/\*([^*]+)\*/.exec(s);e;e=/\*([^*]+)\*/.exec(s))c+=s.slice(0,e.index),c&&=(o.push({text:c,marked:!1}),``),o.push({text:e[1],marked:!0}),s=s.slice(e.index+e[0].length);c+=s,c&&o.push({text:c,marked:!1});let u=o.map(({text:e,marked:t})=>({text:e,style:t?{...i,...a,initial:!0}:{...i}})),d=t.markerGlyphs,f=[];for(let e of u){e.style.uppercase&&(e.text=e.text.toUpperCase()),n&&(e.style={...e.style,weight:`700`});let t=[...e.text],r=e.style.fontFamily?.includes(`Liturgy`)??!1,i=e=>d&&d.chars.has(e)?`marker`:r&&e===`+`?`plus`:`text`;if(t.every(e=>i(e)===`text`)){f.push({text:e.text,style:{...e.style}});continue}let a=``,o=null,s=()=>{if(!a)return;let t={...e.style};o===`marker`&&d?t={...t,fontFamily:d.fontFamily}:o===`plus`&&(t={...t,...l}),f.push({text:a,style:t}),a=``};for(let e of t){let t=i(e);t!==o&&s(),o=t,a+=e}s()}return f}function f(e,t,n){return[e.italic?`italic`:`normal`,`normal`,e.weight??`400`,`${t*(e.measureScale??e.scale??1)}px`,e.fontFamily??n].join(` `)}function p(e,t){let n={};return e.fontFamily&&(n[`font-family`]=e.fontFamily),e.weight&&(n[`font-weight`]=e.weight),e.italic&&(n[`font-style`]=`italic`),e.scale!==void 0&&(n[`font-size`]=`${t*e.scale}px`),e.color&&(n.color=e.color),e.raise&&(n.position=`relative`,n.top=`${-t*e.raise}px`),n}function m(e,t){return e.verifiedFonts.map(e=>`${e.weight} ${t}px ${e.family}`)}var h=new Set(`,.:?'"+!%/=()ÖÜÓ;`.split(``)),ee=new Set(`Ÿ¡¢£[¥¦©ª«¬`.split(``)),g=[`-`,`¨`];function _(e){let t=0;for(;t<e.length&&h.has(e[t]);)t++;t>0&&t<e.length&&e[t]===`-`&&t++;let n=e.length;for(;n>t&&h.has(e[n-1]);)n--;return n<e.length&&n>t&&e[n-1]===`-`&&n--,{leading:e.slice(0,t),core:e.slice(t,n),trailing:e.slice(n)}}var v=class e{script;constructor(e){this.script=typeof e==`string`?new t(e):e}static ctx=null;static measure(t,n){return e.ctx||=document.createElement(`canvas`).getContext(`2d`),e.ctx.font=n,e.ctx.measureText(t).width}static ink(t,n){e.ctx||=document.createElement(`canvas`).getContext(`2d`),e.ctx.font=n;let r=e.ctx.measureText(t);return{left:r.actualBoundingBoxLeft,right:r.actualBoundingBoxRight}}async render(t,n={}){let{style:r=`BNS`,bold:i=!1,textPx:a=18,notesPx:o=a*2,debug:s=!1,maxWidthPx:c=1/0}=n,l=u[r];if(!l)throw Error(`Unknown style set: "${r}"`);for(let e of m(l,a)){try{await document.fonts.load(e)}catch{}if(!document.fonts.check(e))throw Error(`Font could not be loaded: ${e} (style "${r}")`)}let p=`400 ${o}px ${l.notesFontStack}`,v=l.textFontStack,y=f({weight:i?`700`:`400`},a,v),b=e=>d(e,l,i),x=e=>e.reduce((e,t)=>e+S(t.text,f(t.style,a,v)),0),S=(t,n)=>e.measure(t,n),C=S(`-`,p),w=S(`-`,y);t.style.fontSize=`${a}px`,t.style.fontFamily=v,t.dataset.style=r;let T=this.script.clef===void 0?``:`${this.script.clef}-`,E=[],D,O,k,A,j=!1,M=[],N=()=>{D&&(D.textCursor=k,D.notesEnd=D.notesParts.length?S(T+D.notesParts.join(``),p):0);let e=document.createElement(`div`);e.className=`guido`,e.style.fontSize=`${o}px`;let t=document.createElement(`p`);i&&(t.style.fontWeight=`700`),D={notesEl:e,textEl:t,notesParts:[],textCursor:0,notesEnd:0,lastWordEnd:null},E.push(D),O=T,k=0,A=!1,j=!1};N();let P=this.script.syllables,F=P.map((e,t)=>({syl:e,i:t,text:e.text,notes:e.notes,first:!0,lastSeg:!0})),I=(e,t,n)=>{for(let r=e.length-1;r>0;r--){if(!g.includes(e[r]))continue;let i=r+1;for(;i<e.length&&h.has(e[i]);)i++;if(!(i>=e.length)&&n(e.slice(0,i))<=t)return r}return-1},L=(e,t)=>{let n=e.notes,r=t+1;for(;r<n.length&&h.has(n[r]);)r++;return e.notes=n.slice(0,t)+n.slice(t+1,r),n.slice(r)},R=(e,t,n)=>{e.lastSeg=!1,F.splice(t+1,0,{syl:e.syl,i:e.i,text:``,notes:n,first:!1,lastSeg:!0}),z=!0},z=!1,B=0;for(;B<F.length;){let t=F[B],n=t.syl,r=t.i;z&&=(N(),!1);let i=t.lastSeg&&r===P.length-1,o=t.text,l=b(o),u=x(l),d=!i&&n.wordEnd?S(` `,y):0,f=M.reduce((e,t)=>e+t.textWidth+t.spaceWidth,0),m=n.notes.length===1&&ee.has(n.notes),g=k===0||k+f+u+(n.wordEnd?0:w)<=c;if(n.notes.length===0){M.push({sylParts:l,textWidth:u,spaceWidth:d,wordEnd:n.wordEnd}),B++;continue}if(t.first&&t.lastSeg&&n.text.trim()===``&&_(n.notes).core===``){let e=O[O.length-1],t=e&&e!==`-`&&!h.has(e)&&!n.notes.startsWith(`-`)?`-`:``;D.notesParts.push(t+n.notes),O+=t+n.notes,A=!0,j=!1,B++;continue}let v=P[r-1]?.notes!==n.notes;if(m&&!v&&D.notesParts.length>0&&!g&&N(),!t.first){for(;!(S(O+t.notes,p)<=c);){let e=I(t.notes,c,e=>S(O+e,p));if(e<0||e+1>=t.notes.length)break;let n=L(t,e);R(t,B,n)}let e=_(t.notes);D.notesParts.push(t.notes),O+=t.notes,t.lastSeg&&(A=e.trailing.length>0),D.lastWordEnd=!0,t.lastSeg||(z=!0),B++;continue}let T=D.notesParts.length===0||j?``:A?`--`:`---`,E=_(t.notes),V=(t=0)=>{let n=S(E.core,p),r=S(O+T+`-`.repeat(t)+E.leading,p),i=e.ink(E.core,p),a=-i.left,o=(i.right-i.left)/2;return{contentWidth:n,contentStart:r,textStart:n>=u||m?r+a:r+o-u/2}},{contentWidth:H,contentStart:U,textStart:W}=V();for(;;){let e=S(O+T+t.notes,p)>c,r=W+u+(n.wordEnd?0:w)>c;if(!e&&!r)break;if(e){let e=I(t.notes,c,e=>S(O+T+e,p));if(e>=0&&e+1<t.notes.length){let n=L(t,e);R(t,B,n),E=_(t.notes),{contentWidth:H,contentStart:U,textStart:W}=V();break}}if(D.notesParts.length===0&&k===0){if(e){let e=I(t.notes,c,e=>S(O+e,p));if(e>=0&&e+1<t.notes.length){let n=L(t,e);t.notes=t.notes.slice(0,e),R(t,B,n),E=_(t.notes),{contentWidth:H,contentStart:U,textStart:W}=V()}}break}N(),T=``,{contentWidth:H,contentStart:U,textStart:W}=V()}let G=D.notesParts.length>0&&!F[B-1].syl.wordEnd&&f===0,te=k+w,K=k+f+(j&&G?w:0),q=T+t.notes,J=0,Y=!1,X=e=>{J=e,q=T+`-`.repeat(e)+t.notes,{contentWidth:H,contentStart:U,textStart:W}=V(e)},Z=m&&!v&&D.notesParts.length>0;if(Z){T=``,W=k+f;let e=W+u+d-S(O,p),n=c-S(O+T,p),r=e>0?Math.min(Math.ceil(e/C),Math.max(0,Math.floor(n/C))):0;t.notes=`-`.repeat(r),E=_(t.notes),q=T+t.notes,Y=G}if(!Z){if(!j&&G&&W<te&&(u>H||W<k)){Y=!0,W=k;let e=H>=u?W:W+(u-H)/2;e>U&&(J=Math.round((e-U)/C),q=T+`-`.repeat(J)+t.notes)}else if(W<K){let e=H>=u?K:K+(u-H)/2;if(e>U){let n=Math.max(0,Math.floor((e-U)/C));for(X(n);W<K;)n++,X(n);if(D.notesParts.length>0&&S(O+q,p)>c&&(N(),T=``,q=T+t.notes,J=0,{contentWidth:H,contentStart:U,textStart:W}=V(),W<k+f)){let e=H>=u?k+f:k+f+(u-H)/2;if(e>U){let t=Math.max(0,Math.floor((e-U)/C));for(X(t);W<k+f;)t++,X(t)}}}}}f>0&&W<k+f&&(W=k+f);let Q=Math.max(0,W-f-k),$=e.sylSpan(l,a);if(D.notesParts.length===0){let t=e.spacer(Q);s&&(t.title=`lead-in ${Q.toFixed(1)}px → text ${W.toFixed(1)}`),D.textEl.appendChild(t)}else if(F[B-1].syl.wordEnd){let t=e.spacer(Q);s&&(t.title=`word gap ${Q.toFixed(1)}px → text ${W.toFixed(1)}`),D.textEl.appendChild(t)}else if(f===0&&Y)s&&($.title+=` | tight pair`);else if(f===0){let t=Math.max(0,(Q-w)/2),n=e.spacer(t),r=document.createElement(`span`);r.className=`dash`,r.textContent=`-`;let i=e.spacer(t);s&&(n.title=`lead pad ${t.toFixed(1)}px (gap ${Q.toFixed(1)} − dash ${w.toFixed(1)}, halved)`,r.title=`dash ${w.toFixed(1)}px`,i.title=`trail pad ${t.toFixed(1)}px`),D.textEl.append(n,r,i)}if(s){let e=(u-H)/2,t=W-U;$.title=[`text w ${u.toFixed(1)}`,`notes w ${H.toFixed(1)}`,J?`+${J} pad dash(es)`:null,`text start ${W.toFixed(1)}`,`notes start ${U.toFixed(1)}`,`theoDiff: ${e.toFixed(1)}`,`cursDiff: ${t.toFixed(1)}`].filter(Boolean).join(`, `)}for(let t of M){let n=e.sylSpan(t.sylParts,a);if(s&&(n.title=`no notes, text w ${t.textWidth.toFixed(1)}, before the next syllable with notes`),D.textEl.appendChild(n),t.spaceWidth>0){let e=document.createElement(`span`);e.className=`word-space`,e.textContent=` `,s&&(e.title=`word space ${t.spaceWidth.toFixed(1)}px`),D.textEl.appendChild(e)}}if(M=[],D.textEl.appendChild($),d>0){let e=document.createElement(`span`);e.className=`word-space`,e.textContent=` `,s&&(e.title=`word space ${d.toFixed(1)}px`),D.textEl.appendChild(e)}k=W+u+d,O+=q,D.notesParts.push(q),D.lastWordEnd=n.wordEnd,t.lastSeg&&(A=E.trailing.length>0,j=m),t.lastSeg||(z=!0),B++}if(M.length>0){for(let t of M){let n=e.sylSpan(t.sylParts,a);if(s&&(n.title=`no notes, text w ${t.textWidth.toFixed(1)}, end of score`),D.textEl.appendChild(n),t.spaceWidth>0){let e=document.createElement(`span`);e.className=`word-space`,e.textContent=` `,s&&(e.title=`word space ${t.spaceWidth.toFixed(1)}px`),D.textEl.appendChild(e)}k+=t.textWidth+t.spaceWidth}D.lastWordEnd=M[M.length-1].wordEnd,D.textCursor=k}E.forEach((t,n)=>{if(n===E.length-1||t.lastWordEnd!==!1)return;let r=t.notesEnd>t.textCursor?Math.max(0,(t.notesEnd-t.textCursor-w)/2):0,i=e.spacer(r),a=document.createElement(`span`);a.className=`dash`,a.textContent=`-`;let o=e.spacer(r);s&&(i.title=`lead pad ${r.toFixed(1)}px → line-end dash (text end ${t.textCursor.toFixed(1)}, notes end ${t.notesEnd.toFixed(1)})`,a.title=`dash ${w.toFixed(1)}px (line end)`,o.title=`trail pad ${r.toFixed(1)}px`),t.textEl.append(i,a,o)});let V=c+(l.padding??0),H=S(`*`,p);E.forEach((e,t)=>{if(t===E.length-1)return;let n=S(T+e.notesParts.join(``),p);for(;n+C<=V-H&&n+C<c;)e.notesParts.push(`-`),n+=C;e.breakMarkerLeftPx=V-n-H}),E.forEach(e=>{e.notesEl.textContent=T+e.notesParts.join(``)}),E.forEach((e,t)=>{if(t===E.length-1)return;let n=document.createElement(`span`);n.className=`guido-break`,n.textContent=`*`,n.style.position=`relative`,n.style.left=`${e.breakMarkerLeftPx}px`,s&&(n.title=`line break at ${V.toFixed(1)}px (staff filled to ${(V-e.breakMarkerLeftPx-H).toFixed(1)}px)`),e.notesEl.appendChild(n)}),t.replaceChildren(...E.flatMap(e=>[e.notesEl,e.textEl]))}static sylSpan(e,t){let n=document.createElement(`span`);return n.className=`syl`,e.forEach(e=>{let r=document.createElement(`span`);r.textContent=e.text;for(let[n,i]of Object.entries(p(e.style,t)))r.style.setProperty(n,i);e.style.initial&&(r.className=`initial`),n.appendChild(r)}),n}static spacer(e){let t=document.createElement(`span`);return t.className=`spacer`,t.style.display=`inline-block`,t.style.width=`${e}px`,t}},y=`0.1.0`,b=`.guidex-script, .guido-script`,x={maxWidth:`auto`,style:`BNS`,bold:!1,textPx:18,transpose:0,debug:!1},S={},C=new WeakMap,w=new WeakMap,T=new WeakMap;function E(e){if(e!==void 0){if(e===``||e===`true`)return!0;if(e===`false`)return!1}}function D(e){if(e===void 0)return;let t=Number(e);return Number.isFinite(t)?t:void 0}function O(e,t){if(/^auto$/i.test(e.trim()))return`auto`;let n=Number(e);return Number.isFinite(n)&&n>0?n:(console.warn(`[guidex] ignoring invalid data-max-width "${e}"`),t)}function k(e){let t=e.dataset;return{maxWidth:t.maxWidth===void 0?S.maxWidth??x.maxWidth:O(t.maxWidth,S.maxWidth??x.maxWidth),style:t.style??S.style??x.style,bold:E(t.bold)??S.bold??x.bold,textPx:D(t.textPx)??S.textPx??x.textPx,transpose:D(t.transpose)??S.transpose??x.transpose,debug:E(t.debug)??S.debug??x.debug}}function A(e){return e.querySelector(`:scope > .guidex-render`)?.clientWidth||e.clientWidth||e.parentElement?.clientWidth||0}function j(e,t,n){if(typeof t==`number`)return t;let r=A(e);if(r<=0)return 1/0;let i=r-(u[n]?.padding??0);return i>0?i:r}function M(e){for(let t of e.childNodes)if(t.nodeType===Node.TEXT_NODE&&t.nodeValue?.trim())return t.nodeValue.trim();return C.get(e)??``}function N(e){let t=e.querySelector(`:scope > .guidex-render`);if(t)return t;let n=document.createElement(`div`);return n.className=`guidex-render`,e.replaceChildren(n),n}function P(e,t){let n=document.createElement(`p`);n.className=`guidex-error`,n.textContent=t,e.replaceChildren(n)}async function F(e){let n=(w.get(e)??0)+1;w.set(e,n);let r=k(e),i=M(e);C.set(e,i);let a=N(e),o=j(e,r.maxWidth,r.style);T.set(e,o);let c=document.createElement(`div`),l=new t(i,{transforms:r.transpose===0?[]:[s(r.transpose)]});try{if(await new v(l).render(c,{style:r.style,bold:r.bold,textPx:r.textPx,debug:r.debug,maxWidthPx:o}),w.get(e)!==n)return;a.replaceChildren(...c.childNodes),a.style.cssText=c.style.cssText,c.dataset.style&&(a.dataset.style=c.dataset.style)}catch(t){if(w.get(e)!==n)return;console.error(`[guidex]`,t),P(a,t instanceof Error?t.message:String(t))}}var I=new WeakMap;function L(e){let t=k(e),n=j(e,t.maxWidth,t.style);return Math.abs(n-(T.get(e)??NaN))>1}function R(e){if(I.has(e))return;let t;if(typeof ResizeObserver<`u`){let n=new ResizeObserver(()=>{L(e)&&F(e)});n.observe(e),t=()=>n.disconnect()}else{let n=()=>{L(e)&&F(e)};window.addEventListener(`resize`,n),t=()=>window.removeEventListener(`resize`,n)}I.set(e,t)}async function z(e=document){let t=S.selector??b,n=[];return e instanceof Element&&e.matches(t)&&n.push(e),n.push(...e.querySelectorAll(t)),Promise.all(n.map(e=>(R(e),F(e)))).then(()=>void 0)}var B=!1;function V(){if(B||typeof MutationObserver>`u`)return;B=!0;let e=S.selector??b;new MutationObserver(t=>{let n=new Set;for(let r of t)for(let t of r.addedNodes)if(t instanceof Element&&!t.closest(`.guidex-render`)){t.matches(e)&&n.add(t);for(let r of t.querySelectorAll(e))n.add(r)}for(let e of n)R(e),F(e)}).observe(document.documentElement,{childList:!0,subtree:!0})}function H(e={}){S={...S,...e},V(),z()}return document.readyState===`loading`?document.addEventListener(`DOMContentLoaded`,()=>H(),{once:!0}):H(),e.init=H,e.renderAll=z,e.version=y,e})({});
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "guidex",
3
+ "version": "0.1.1",
4
+ "description": "Guidex — render plainchant scores from a compact GuidoScript notation, as a web app and an embeddable runtime",
5
+ "type": "module",
6
+ "files": [
7
+ "dist-embed"
8
+ ],
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "scripts": {
13
+ "dev": "vite --host",
14
+ "build": "tsc && vite build",
15
+ "build:embed": "vite build --config vite.embed.config.ts",
16
+ "preview": "vite preview",
17
+ "test": "vitest run",
18
+ "lint": "eslint ."
19
+ },
20
+ "keywords": [
21
+ "plainchant",
22
+ "gregorian",
23
+ "guido",
24
+ "score",
25
+ "notation",
26
+ "liturgy"
27
+ ],
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/BrownieMaar/guidex.git"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/BrownieMaar/guidex/issues"
34
+ },
35
+ "homepage": "https://github.com/BrownieMaar/guidex#readme",
36
+ "devDependencies": {
37
+ "@eslint/js": "^10.0.1",
38
+ "eslint": "^10.10.0",
39
+ "jsdom": "^29.1.1",
40
+ "typescript": "~6.0.2",
41
+ "typescript-eslint": "^8.70.0",
42
+ "vite": "^8.2.2",
43
+ "vitest": "^4.1.11"
44
+ }
45
+ }