guidex 0.3.0 → 0.3.2

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.
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Glyph families: the variant glyphs of each pitch glyph (issue #76).
3
+ *
4
+ * A pitch glyph has two kinds of variants. Shape variants keep the pitch
5
+ * and change the note form (virga, longa, small head, cephalicus, the
6
+ * connecting forms); accidental variants are the sharp, flat and natural
7
+ * signs written before a note. Each variant is a separate glyph in the
8
+ * Guidohu font.
9
+ *
10
+ * The table is hand-written from the notation reference, not derived from
11
+ * `PITCH_ROWS` offsets: the accidentals do not follow row position (`1`
12
+ * takes its sharp from row 6 and its flat from row 5). A missing entry
13
+ * means the pitch has no such variant, not an untranscribed one.
14
+ */
15
+ /** The variant roles a pitch glyph can carry. */
16
+ export type VariantRole = 'virga' | 'longa' | 'smallHead' | 'cephalicus' | 'connectOneDown' | 'connectTwoDown' | 'connectThreeDown' | 'connectFourDown' | 'smallPoint' | 'modifyUp' | 'modifyDown' | 'modifyNatural';
17
+ /** The standard g-clef pitch name of a base glyph, or of the base of a
18
+ * variant glyph; undefined when the glyph has no pitch. */
19
+ export declare function gClefPitchOf(ch: string): string | undefined;
20
+ /** The variant of `ch` in `role`, or undefined when `ch` has none. */
21
+ export declare function variantOf(ch: string, role: VariantRole): string | undefined;
22
+ /** The virga (the stemmed form) of a pitch glyph. */
23
+ export declare function toVirga(ch: string): string | undefined;
24
+ /** The longa (the double-length form) of a pitch glyph. */
25
+ export declare function toLonga(ch: string): string | undefined;
26
+ /** The small head of a pitch glyph. */
27
+ export declare function toSmallHead(ch: string): string | undefined;
28
+ /** The cephalicus of a pitch glyph. */
29
+ export declare function toCephalicus(ch: string): string | undefined;
30
+ /** The connecting form that joins a pitch to the note `steps` rows below
31
+ * it (1 to 4). */
32
+ export declare function toConnectDown(ch: string, steps: 1 | 2 | 3 | 4): string | undefined;
33
+ /** The small point (the dot that lengthens a note by half) of a pitch glyph. */
34
+ export declare function toSmallPoint(ch: string): string | undefined;
35
+ /** The sharp (raised) form of a pitch glyph. */
36
+ export declare function modifyUp(ch: string): string | undefined;
37
+ /** The flat (lowered) form of a pitch glyph. */
38
+ export declare function modifyDown(ch: string): string | undefined;
39
+ /** The natural sign for a pitch glyph. */
40
+ export declare function modifyNatural(ch: string): string | undefined;
41
+ /** Reverse lookup: the base glyph and role of a variant glyph, or
42
+ * undefined when `ch` is not a variant. */
43
+ export declare function baseOf(ch: string): {
44
+ base: string;
45
+ role: VariantRole;
46
+ } | undefined;
47
+ /** Whether `ch` is a modifier glyph (a sharp, flat or natural sign). */
48
+ export declare function isModifierGlyph(ch: string): boolean;
49
+ /** The clef glyphs (issue #49) and the staff line each one sits on,
50
+ * counted from the bottom. The g-clef has two keybindings. */
51
+ export declare const CLEF_GLYPHS: Readonly<Record<string, {
52
+ clef: 'c' | 'f' | 'g';
53
+ line: 1 | 2 | 3 | 4;
54
+ }>>;
55
+ /** Whether `ch` is a clef glyph. */
56
+ export declare function isClefGlyph(ch: string): boolean;
57
+ /** The base pitch glyphs from low to high, one per staff position
58
+ * (spelled out: `Object.keys` would list the digit keys first). The
59
+ * index is the staff position the exact transposition counts with. */
60
+ export declare const STAFF: readonly string[];
61
+ /** The pitch letter (`a`…`g`, octave dropped) of a pitch glyph — a base
62
+ * glyph or any of its variants — under `clef` (a clef glyph; default the
63
+ * standard g-clef). Undefined when the glyph has no pitch or the clef
64
+ * glyph is unknown. */
65
+ export declare function pitchLetterOf(ch: string, clef?: string): string | undefined;
66
+ /** Every base pitch glyph whose pitch letter under `clef` is `letter`,
67
+ * in every octave, low to high. */
68
+ export declare function basesOfLetter(letter: string, clef?: string): string[];
@@ -1,5 +1,5 @@
1
- import { GuidoScript } from './guidoScript';
2
1
  import { StyleSetName } from './guidoStyles';
2
+ import { LayoutSource } from './chantLayout';
3
3
  export { NOTE_MODIFIERS, SCORE_MARKERS } from './chantLayout';
4
4
  /** Options for one render. All fields optional; defaults reproduce the
5
5
  * original Book Antiqua look (style set BNS). */
@@ -26,11 +26,13 @@ export interface RenderOptions {
26
26
  * `render` turns the laid-out lines into (notes `.guido` div, text `<p>`)
27
27
  * pairs — px-measured inline-block spacer spans fake the horizontal gaps.
28
28
  * The Word export (wordExport.ts) consumes the same layout with a
29
- * character-based spacer mechanism instead.
29
+ * character-based spacer mechanism instead. Given several underlays of
30
+ * one melody (stacked stanzas, issue #84 — see `stackStanzas`), every
31
+ * notes line is followed by one text `<p>` per underlay.
30
32
  */
31
33
  export declare class GuidoRender {
32
34
  private readonly script;
33
- constructor(source: GuidoScript | string);
35
+ constructor(source: LayoutSource);
34
36
  /**
35
37
  * Renders the score into `container` as (notes line, text line) pairs.
36
38
  * When `maxWidthPx` is finite, a new pair starts before any syllable
@@ -3,20 +3,60 @@ import { ScriptTransform } from './guidoScriptTransforms';
3
3
  * `spaceBefore` records whether the source had whitespace between the
4
4
  * previous group and this token's group — it feeds the previous
5
5
  * syllable's `wordEnd` and the `toString()` round-trip. */
6
- export type ScriptToken = {
6
+ export type ScriptToken = SyllableToken | StatementToken;
7
+ /** A syllable token. `held` marks a text-less group held by the syllable
8
+ * before it (its `wordEnd` is that syllable's); `empty` marks an empty
9
+ * slot (`~`, or a text-less group at the start): notes but no syllable.
10
+ * Both flags are only present when set. */
11
+ export type SyllableToken = {
7
12
  kind: 'syllable';
8
13
  text: string;
9
14
  notes: string;
10
15
  wordEnd: boolean;
11
16
  spaceBefore: boolean;
12
- } | {
17
+ held?: true;
18
+ empty?: true;
19
+ };
20
+ /** A syllable as `GuidoScript.syllables` lists it. */
21
+ export type Syllable = Pick<SyllableToken, 'text' | 'notes' | 'wordEnd' | 'held' | 'empty'>;
22
+ /** Source offsets of one token (see `GuidoScript.tokenSpans`): `start`
23
+ * is its text's first character (its group's `(` when it has no text),
24
+ * `notesStart`/`notesEnd` bound its group's content, `end` is just past
25
+ * its `)`. Offsets index `source`; only the typing conveniences
26
+ * (a closing paren or a `()` appended at the end) reach past it. */
27
+ export interface TokenSpan {
28
+ start: number;
29
+ notesStart: number;
30
+ notesEnd: number;
31
+ end: number;
32
+ }
33
+ /** A control statement token. `clef` is the clef glyph of a `(@clef …)`
34
+ * change; `key` is the key's modifier glyphs (empty when cleared). */
35
+ export type StatementToken = {
13
36
  kind: 'break';
14
37
  spaceBefore: boolean;
38
+ } | {
39
+ kind: 'clef';
40
+ clef: string;
41
+ key: string;
42
+ spaceBefore: boolean;
43
+ } | {
44
+ kind: 'key';
45
+ key: string;
46
+ spaceBefore: boolean;
15
47
  };
16
48
  /** Whether a group's content is a control statement (`@`-keyword,
17
49
  * trimmed). Shared with the transforms, which must leave statement
18
- * groups alone — their keyword characters are pitch glyphs otherwise. */
50
+ * groups alone — their keyword characters are pitch glyphs otherwise.
51
+ * A c4 clef group (`@` + modifier glyphs) counts too: the opening one is
52
+ * told apart by `isC4ClefGroup`, and mid-script it is an error. */
19
53
  export declare function isStatementGroup(content: string): boolean;
54
+ /** Whether a group's content has the shape of a c4 clef group: `@`
55
+ * followed by nothing but modifier glyphs. Statement keywords never
56
+ * consist solely of modifier glyphs, so the two cannot collide. */
57
+ export declare function isC4ClefGroup(content: string): boolean;
58
+ /** A statement token written back as its group. */
59
+ export declare function statementSource(tok: StatementToken): string;
20
60
  export declare class GuidoScript {
21
61
  readonly source: string;
22
62
  constructor(source: string, options?: {
@@ -29,13 +69,20 @@ export declare class GuidoScript {
29
69
  * control statements. Throws on a malformed statement (syllable text
30
70
  * attached to one, or an unknown `@` keyword). */
31
71
  get tokens(): ScriptToken[];
72
+ /** The raw text of the `(@stanza …)` statements at the end of the
73
+ * script — stanzas 2, 3, … in order, escapes kept (the stanza text
74
+ * grammar folds them). Throws like `tokens` on a malformed script,
75
+ * and when anything but another stanza follows a stanza statement. */
76
+ get stanzas(): string[];
77
+ /** Where each token stands in the source, parallel to `tokens`:
78
+ * offsets of its text (past leading whitespace), of its group's
79
+ * content and just past its group's `)`. The walker
80
+ * (`scriptSyllables`) reads them. */
81
+ get tokenSpans(): TokenSpan[];
82
+ private parse;
32
83
  /** The script broken into syllables, each with its sung text and
33
84
  * notes — the syllable tokens, statements filtered out. */
34
- get syllables(): {
35
- text: string;
36
- notes: string;
37
- wordEnd: boolean;
38
- }[];
85
+ get syllables(): Syllable[];
39
86
  /**
40
87
  * The source with the typing conveniences folded in, so the parser
41
88
  * itself only ever sees complete scripts:
@@ -46,7 +93,8 @@ export declare class GuidoScript {
46
93
  * renders like a `*()` syllable (text without notes width).
47
94
  */
48
95
  private get normalized();
49
- /** The syllables grouped into the words of the text. */
96
+ /** The syllables grouped into the words of the text. Held groups and
97
+ * empty slots sing no syllable of their own, so they are left out. */
50
98
  get words(): {
51
99
  text: string;
52
100
  syllables: {
@@ -55,12 +103,46 @@ export declare class GuidoScript {
55
103
  }[];
56
104
  }[];
57
105
  /** The script rebuilt from the parsed pieces — a round-trip of the
58
- * source, statements included. */
106
+ * source, statements and stanzas included. */
59
107
  toString(): string;
60
108
  }
109
+ /** One text-bearing syllable of a script (see `scriptSyllables`). */
110
+ export interface ScriptSyllable {
111
+ /** Its text, escapes folded and whitespace trimmed */
112
+ text: string;
113
+ /** Its own note group's content, escapes folded */
114
+ notes: string;
115
+ /** The groups it holds (issue #83), escapes folded, in order */
116
+ held: string[];
117
+ /** Whether it opens a word (the first syllable always does) */
118
+ wordStart: boolean;
119
+ wordEnd: boolean;
120
+ /** Offset of its text's first character in the source */
121
+ start: number;
122
+ /** Offsets of its own group's content (the `(` is at
123
+ * `notesStart - 1`, the `)` at `notesEnd`) */
124
+ notesStart: number;
125
+ notesEnd: number;
126
+ /** Offset just past the last group of its held run (just past its
127
+ * own group when it holds none) */
128
+ end: number;
129
+ /** Its index among the script's tokens (`GuidoScript.tokens`) */
130
+ token: number;
131
+ }
132
+ /**
133
+ * The syllables of a script that carry text, in order — the ones the
134
+ * sung text is made of, so their count and texts match `chantText`
135
+ * (and `GuidoScript.words`). Held groups ride on their syllable
136
+ * (`held`), `~` empty slots, statements and `(@stanza …)` are skipped.
137
+ * Offsets index the script's source; index i is the "syllable i" of
138
+ * `composeScript`'s `from`/`to`.
139
+ */
140
+ export declare function scriptSyllables(script: GuidoScript | string): ScriptSyllable[];
61
141
  /** The sung text of the script: the clef stripped, syllables joined
62
142
  * into words, and the words joined with single spaces. */
63
143
  export declare function chantText(script: string): string;
64
144
  /** Folds backslash escapes into the characters they stand for. Shared
65
145
  * with the transforms module (guidoScriptTransforms.ts). */
66
146
  export declare function unescape(text: string): string;
147
+ /** Turns literal parentheses and backslashes into their escape sequences. */
148
+ export declare function escape(text: string): string;
@@ -1,13 +1,15 @@
1
+ export { STANZA_PLACEHOLDER, padStanza, parseStanza, selectStanza, slotCount, smallHeads, stackStanzas, stackedStanzaNumbers, stanzaCount, stanzaTexts, stanzaWarnings, withStanza, type EmptySlots, type SelectStanzaOptions, type StanzaOptions, type StanzaStack, type StanzaSyllable, type StanzaWarning, } from './stanzas';
2
+ export { KEY_SIGNATURES, parseInterval, transposeExact, transposeScript, TranspositionRangeError, type SpelledInterval, type TranspositionTarget, } from './exactTranspose';
1
3
  /** A named source-to-source rewrite applied to a script before parsing. */
2
4
  export interface ScriptTransform {
3
5
  name: string;
4
6
  apply: (source: string) => string;
5
7
  }
6
8
  /** The `startWithInitial` transform: finds the first syllable group with
7
- * actual notes (the clef, no-notes groups, and control statements don't
8
- * count) and wraps the syllable's first character in the `*…*` initial
9
- * marker. Already-marked text (leading `*`) and syllables without text
10
- * are left alone. */
9
+ * actual notes and text (the clef, no-notes groups, control statements,
10
+ * text-less groups and `~` empty slots don't count) and wraps the
11
+ * syllable's first character in the `*…*` initial marker. Already-marked
12
+ * text (leading `*`) is left alone. */
11
13
  declare function startWithInitial(source: string): string;
12
14
  /**
13
15
  * The built-in transformations, keyed by name; pass the ones a feature
@@ -30,8 +32,19 @@ export declare const SCRIPT_TRANSFORMS: {
30
32
  export declare const PITCH_ROWS: readonly ["ðñ0123456789öüó", "^]qwertzuiopõú", "ÝÞQWERTZUIOPÕÚ", "×sdfghjkléáû", "ØÙDFGHJKLÉÁÛ", "÷øaASÍYXCVBN$m", "ÈÊËÌÎÏÐÑÒþßòôùý", "“”•–—˜™š›œº»", "®−§ÿ\"+!%/=()ÖÜÓ", "Ÿ¡¢£[¥¦©ª«¬", "~¯‚ƒ„…†‡ˆ‰Š‹Œ‘’", "àâãäåæçèêëìîï", "½°±²³´μ·¾¸¹"];
31
33
  /** What happens when a shift runs past the end of a pitch row:
32
34
  * `clamp` keeps the first/last available character, `null` emits
33
- * `U+0000`, which shows up as a missing glyph in the score. */
34
- export type TransposeOverflow = 'clamp' | 'null';
35
+ * `U+0000`, which shows up as a missing glyph in the score, and `error`
36
+ * throws. */
37
+ export type TransposeOverflow = 'clamp' | 'null' | 'error';
38
+ /** Transposes the pitch characters of one note group's content as
39
+ * written in the source (escapes included) — the staff-degree shift
40
+ * `transposeBy` applies to every group. Escape pairs stand for their
41
+ * underlying character (an escaped paren is a pitch glyph of the
42
+ * `®−§ÿ…` row), and a shifted result that is a control character of the
43
+ * group syntax is written escaped. A statement group's content (`@…`) is returned
44
+ * unchanged: its keyword letters are pitch glyphs, but no notes. */
45
+ export declare function transposeNotes(notes: string, shift: number, options?: {
46
+ overflow?: TransposeOverflow;
47
+ }): string;
35
48
  /** The `transposeBy` transform factory: shifts every pitch character of
36
49
  * every notes group `shift` positions along its pitch row. The clef
37
50
  * group and the syllable text are left alone — clef handling is a
@@ -39,4 +52,3 @@ export type TransposeOverflow = 'clamp' | 'null';
39
52
  export declare function transposeBy(shift: number, options?: {
40
53
  overflow?: TransposeOverflow;
41
54
  }): ScriptTransform;
42
- export {};
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Note-group anatomy shared by the layout pass and the exact
3
+ * transposition: the score markers and the split of a group into its
4
+ * leading markers, notes and trailing markers. Kept free of imports, so
5
+ * the transforms can use it without pulling in the style sets.
6
+ */
7
+ /**
8
+ * Score marker characters: they carry no pitch, and when they sit at the
9
+ * start or end of a note group (with the adjacent dash) they act as
10
+ * whitespace rather than note width.
11
+ */
12
+ export declare const SCORE_MARKERS = ",.:?'\"+!%/=()\u00D6\u00DC\u00D3;";
13
+ export declare const MARKER_CHARS: Set<string>;
14
+ export interface NoteGroupParts {
15
+ /** Marker run at the group's start (markers plus their adjacent dash) */
16
+ leading: string;
17
+ /** The notes between the marker runs */
18
+ core: string;
19
+ /** Marker run at the group's end (dash plus markers) */
20
+ trailing: string;
21
+ }
22
+ export declare function splitNoteMarkers(notes: string): NoteGroupParts;
@@ -1,5 +1,6 @@
1
1
  import { MaxWidth } from '../embed/core';
2
2
  import { StyleSetName } from '../guidoStyles';
3
+ import { EmptySlots, StanzaStack, StanzaWarning } from '../stanzas';
3
4
  export interface GuidexScoreProps {
4
5
  /** The score source in GuidoScript notation, e.g. `'(M)A(0) di(0q)e(qQ0)bus(1) an(3)ti(1e)quis(qQ0-:)'`. */
5
6
  script: string;
@@ -12,8 +13,25 @@ export interface GuidexScoreProps {
12
13
  textPx?: number;
13
14
  /** Transpose the score by N diatonic steps. */
14
15
  transpose?: number;
16
+ /** The stanza sung under the notes (1-based, default 1: the
17
+ * script's own text). */
18
+ stanza?: number;
19
+ /** Stacked stanzas under one staff (issue #84): `'all'`, or the
20
+ * stanza numbers in order. Replaces `stanza` when set. */
21
+ stack?: StanzaStack;
22
+ /** Stanzas 2, 3, … as stanza text, replacing the script's
23
+ * `(@stanza …)` statements. */
24
+ stanzas?: string[];
25
+ /** `'small'` draws the slots the stanza leaves empty while another
26
+ * stanza sings them with small heads (stacked: the slots no stacked
27
+ * stanza sings while another stanza does); `'partial'` every slot
28
+ * some stanza sings and another does not. */
29
+ emptySlots?: EmptySlots;
30
+ /** Called after each render with the rendered stanzas' count
31
+ * mismatches (empty when it fits). */
32
+ onWarnings?: (warnings: StanzaWarning[]) => void;
15
33
  /** Render measurement debug markers. */
16
34
  debug?: boolean;
17
35
  className?: string;
18
36
  }
19
- export declare function GuidexScore({ script, maxWidth, style, bold, textPx, transpose, debug, className, }: GuidexScoreProps): import('react').JSX.Element;
37
+ export declare function GuidexScore({ script, maxWidth, style, bold, textPx, transpose, stanza, stack, stanzas, emptySlots, onWarnings, debug, className, }: GuidexScoreProps): import('react').JSX.Element;
@@ -3,6 +3,7 @@
3
3
  * a re-export of the imperative embed API for advanced use.
4
4
  */
5
5
  export { GuidexScore, type GuidexScoreProps } from './GuidexScore';
6
- export { renderScore, renderAll, init, dispose, version, type EmbedOptions, type MaxWidth } from '../embed/core';
6
+ export { renderScore, renderAll, init, dispose, version, type EmbedOptions, type MaxWidth, type RenderResult, type RenderScoreOptions, } from '../embed/core';
7
7
  export { downloadScorePng, downloadScoreSvg, scorePngBlob, scoreSvg, type ScoreDownloadOptions, type ScoreImageOptions, } from '../scoreImage';
8
8
  export type { StyleSetName } from '../guidoStyles';
9
+ export type { EmptySlots, StanzaStack, StanzaWarning } from '../stanzas';
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { a as dispose, c as renderScore, i as scoreSvg, l as version, n as downloadScoreSvg, o as init, r as scorePngBlob, s as renderAll, t as downloadScorePng } from "../scoreImage-DnFIbtF_.js";
2
+ import { a as dispose, c as renderScore, i as scoreSvg, l as version, n as downloadScoreSvg, o as init, r as scorePngBlob, s as renderAll, t as downloadScorePng } from "../scoreImage-DSEupCru.js";
3
3
  import { useEffect, useRef } from "react";
4
4
  import { jsx } from "react/jsx-runtime";
5
5
  //#region src/react/GuidexScore.tsx
@@ -10,20 +10,36 @@ import { jsx } from "react/jsx-runtime";
10
10
  * client'` directive marks it for React Server Components (Next.js App
11
11
  * Router); in plain client-side React it is a no-op.
12
12
  */
13
- function GuidexScore({ script, maxWidth, style, bold, textPx, transpose, debug, className }) {
13
+ function GuidexScore({ script, maxWidth, style, bold, textPx, transpose, stanza, stack, stanzas, emptySlots, onWarnings, debug, className }) {
14
14
  const ref = useRef(null);
15
+ const onWarningsRef = useRef(onWarnings);
16
+ onWarningsRef.current = onWarnings;
17
+ const stanzasKey = stanzas === void 0 ? void 0 : JSON.stringify(stanzas);
18
+ const stackKey = stack === void 0 ? void 0 : JSON.stringify(stack);
15
19
  useEffect(() => {
16
20
  const el = ref.current;
17
21
  if (!el) return;
22
+ let live = true;
23
+ const stanzaTexts = stanzasKey === void 0 ? void 0 : JSON.parse(stanzasKey);
24
+ const stacked = stackKey === void 0 ? void 0 : JSON.parse(stackKey);
18
25
  renderScore(el, script, {
19
26
  maxWidth,
20
27
  style,
21
28
  bold,
22
29
  textPx,
23
30
  transpose,
31
+ stanza,
32
+ stack: stacked,
33
+ stanzas: stanzaTexts,
34
+ emptySlots,
24
35
  debug
36
+ }).then(({ warnings }) => {
37
+ if (live) onWarningsRef.current?.(warnings);
25
38
  });
26
- return () => dispose(el);
39
+ return () => {
40
+ live = false;
41
+ dispose(el);
42
+ };
27
43
  }, [
28
44
  script,
29
45
  maxWidth,
@@ -31,6 +47,10 @@ function GuidexScore({ script, maxWidth, style, bold, textPx, transpose, debug,
31
47
  bold,
32
48
  textPx,
33
49
  transpose,
50
+ stanza,
51
+ stackKey,
52
+ stanzasKey,
53
+ emptySlots,
34
54
  debug
35
55
  ]);
36
56
  return /* @__PURE__ */ jsx("div", {