@dsebastien/music-notation 0.11.0 → 0.12.0

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.
@@ -18,6 +18,41 @@ export declare function parseHand(text: string): Hand;
18
18
  export declare function layoutDefaults(value: unknown): LayoutDefaults;
19
19
  /** Width a title needs (14px semi-bold), so a narrow diagram doesn't clip it. Errs on the wide side. */
20
20
  export declare function titleWidth(title?: string): number;
21
+ /** Most lines a diagram's title takes (#74); a longer title squeezes its last line. */
22
+ export declare const MAX_TITLE_LINES = 3;
23
+ /** Distance between two lines of a title. */
24
+ export declare const TITLE_LINE = 17;
25
+ export interface TitleLine {
26
+ text: string;
27
+ /** Set when the line is wider than the diagram even alone: squeeze it to this length. */
28
+ textLength: number | null;
29
+ }
30
+ /**
31
+ * The width of a line of a title at 14px semibold, closer than `titleWidth` (spaces and punctuation
32
+ * are narrow), with the same 8 of padding; for line breaks (#74).
33
+ */
34
+ export declare function titleLineWidth(text: string): number;
35
+ /**
36
+ * A title's lines on a diagram `width` units wide (#74): the title never widens the diagram. It
37
+ * stays on one line when it fits (`titleLineWidth`); else it wraps into lines that fit, at most
38
+ * `MAX_TITLE_LINES`, breaking first after a comma or before a parenthesis, then at spaces; a word
39
+ * wider than the diagram, or the rest of a title that needs more lines, is squeezed (`textLength`,
40
+ * the padding left out).
41
+ */
42
+ export declare function titleLines(title: string, width: number): TitleLine[];
43
+ /**
44
+ * Draws a diagram's title centered at `x`, its first baseline at `y` (#74): one `<text class=
45
+ * "mn-title">`, a `<tspan>` per line when it wraps (lines after the first start with a space, so
46
+ * the text reads as the title). Sets `data-mn-title-lines` on `root` for `alignTitleLines`.
47
+ */
48
+ export declare function drawTitle(doc: Document, root: Element, lines: TitleLine[], x: number, y: number): Element;
49
+ /**
50
+ * Gives every diagram of a set the room of the set's longest title (#74), so their necks line up
51
+ * in a row: a diagram whose title takes fewer lines moves down by the missing lines (its content
52
+ * in a `<g class="mn-title-pad">`, its viewBox and height grown). Diagrams without
53
+ * `data-mn-title-lines` (no title) count as one line when another one has a title.
54
+ */
55
+ export declare function alignTitleLines(diagrams: Iterable<Element>): void;
21
56
  export interface NeckLayout {
22
57
  orientation: Orientation;
23
58
  hand: Hand;
@@ -144,6 +144,102 @@ function titleWidth(title) {
144
144
  }
145
145
  return Math.ceil(width);
146
146
  }
147
+ var MAX_TITLE_LINES = 3;
148
+ var TITLE_LINE = 17;
149
+ function titleLineWidth(text) {
150
+ if (!text)
151
+ return 0;
152
+ let width = 8;
153
+ for (const ch of text) {
154
+ const code = ch.codePointAt(0);
155
+ width += code > 591 ? 15 : ch === " " || ".,:;'!|".includes(ch) ? 4 : "()[]-/".includes(ch) || "iljfrt".includes(ch) ? 5 : "mwMW".includes(ch) ? 12 : /[A-Z0-9#♯♭]/.test(ch) ? 9.5 : 7.8;
156
+ }
157
+ return Math.ceil(width);
158
+ }
159
+ function titleLines(title, width) {
160
+ const fits = (text) => titleLineWidth(text) <= width;
161
+ const squeeze = (text) => ({
162
+ text,
163
+ textLength: fits(text) ? null : Math.max(1, width - 8)
164
+ });
165
+ if (fits(title))
166
+ return [{ text: title, textLength: null }];
167
+ const phrases = title.split(/(?<=,)\s+|\s+(?=\()/).flatMap((phrase) => fits(phrase) ? [phrase] : phrase.split(/\s+/)).filter(Boolean);
168
+ const lines = [];
169
+ let line = "";
170
+ for (const phrase of phrases) {
171
+ const next = line ? `${line} ${phrase}` : phrase;
172
+ if (!line || fits(next))
173
+ line = next;
174
+ else {
175
+ lines.push(line);
176
+ line = phrase;
177
+ }
178
+ }
179
+ if (line)
180
+ lines.push(line);
181
+ if (lines.length > MAX_TITLE_LINES) {
182
+ const kept = lines.slice(0, MAX_TITLE_LINES - 1);
183
+ kept.push(lines.slice(MAX_TITLE_LINES - 1).join(" "));
184
+ return kept.map(squeeze);
185
+ }
186
+ return lines.map(squeeze);
187
+ }
188
+ function drawTitle(doc, root, lines, x, y) {
189
+ const NS = "http://www.w3.org/2000/svg";
190
+ const text = doc.createElementNS(NS, "text");
191
+ text.setAttribute("class", "mn-title");
192
+ text.setAttribute("x", String(x));
193
+ text.setAttribute("y", String(y));
194
+ text.setAttribute("text-anchor", "middle");
195
+ const squeeze = (el, line) => {
196
+ if (line.textLength === null)
197
+ return;
198
+ el.setAttribute("textLength", String(line.textLength));
199
+ el.setAttribute("lengthAdjust", "spacingAndGlyphs");
200
+ };
201
+ if (lines.length === 1) {
202
+ text.textContent = lines[0].text;
203
+ squeeze(text, lines[0]);
204
+ } else {
205
+ lines.forEach((line, i) => {
206
+ const span = doc.createElementNS(NS, "tspan");
207
+ span.setAttribute("x", String(x));
208
+ if (i > 0)
209
+ span.setAttribute("dy", String(TITLE_LINE));
210
+ span.textContent = i > 0 ? ` ${line.text}` : line.text;
211
+ squeeze(span, line);
212
+ text.appendChild(span);
213
+ });
214
+ }
215
+ root.setAttribute("data-mn-title-lines", String(lines.length));
216
+ root.appendChild(text);
217
+ return text;
218
+ }
219
+ function alignTitleLines(diagrams) {
220
+ const list = [...diagrams].filter((d) => d.hasAttribute("data-mn-title-lines"));
221
+ const most = Math.max(0, ...list.map((d) => Number(d.getAttribute("data-mn-title-lines"))));
222
+ for (const d of list) {
223
+ const missing = most - Number(d.getAttribute("data-mn-title-lines"));
224
+ if (missing <= 0)
225
+ continue;
226
+ const shift = missing * TITLE_LINE;
227
+ const doc = d.ownerDocument;
228
+ const g = doc.createElementNS("http://www.w3.org/2000/svg", "g");
229
+ g.setAttribute("class", "mn-title-pad");
230
+ g.setAttribute("transform", `translate(0 ${shift})`);
231
+ for (const child of [...d.children]) {
232
+ if (child.tagName.toLowerCase() !== "defs")
233
+ g.appendChild(child);
234
+ }
235
+ d.appendChild(g);
236
+ const [x, y, w, h] = (d.getAttribute("viewBox") ?? "0 0 0 0").split(/\s+/).map(Number);
237
+ d.setAttribute("viewBox", `${x} ${y} ${w} ${h + shift}`);
238
+ if (d.hasAttribute("height"))
239
+ d.setAttribute("height", String(Number(d.getAttribute("height")) + shift));
240
+ d.setAttribute("data-mn-title-lines", String(most));
241
+ }
242
+ }
147
243
  function neckFrame(n) {
148
244
  const vertical = n.orientation === "vertical";
149
245
  const left = n.hand === "left";
@@ -295,7 +391,7 @@ function renderGuitarDiagram(doc, voicing, options = {}) {
295
391
  const fromNut = capo > 0 && options.fretsFrom === "nut";
296
392
  const capoFret = fromNut ? capo : null;
297
393
  const caption = capo && options.caption !== false ? capoCaption(capo, options.sounds) : undefined;
298
- const frame = neckFrame({
394
+ const layout = {
299
395
  orientation,
300
396
  hand: options.hand ?? "right",
301
397
  across: (strings - 1) * SPACING,
@@ -304,8 +400,13 @@ function renderGuitarDiagram(doc, voicing, options = {}) {
304
400
  after: 10,
305
401
  low: vertical || base > 1 ? BASE : 10,
306
402
  high: vertical ? 14 : 10,
307
- top: options.title ? TITLE : 4,
308
- minWidth: Math.max(titleWidth(options.title), titleWidth(caption))
403
+ top: 4,
404
+ minWidth: titleWidth(caption)
405
+ };
406
+ const lines = options.title ? titleLines(options.title, neckFrame(layout).width) : [];
407
+ const frame = neckFrame({
408
+ ...layout,
409
+ top: lines.length ? TITLE + (lines.length - 1) * TITLE_LINE : 4
309
410
  });
310
411
  const across = (string) => string * SPACING;
311
412
  const along = (row) => row * ROW;
@@ -328,14 +429,8 @@ function renderGuitarDiagram(doc, voicing, options = {}) {
328
429
  root.setAttribute("data-mn-capo", String(capo));
329
430
  root.setAttribute("data-mn-frets-from", fromNut ? "nut" : "capo");
330
431
  }
331
- if (options.title) {
332
- root.appendChild(svgText(doc, options.title, {
333
- class: "mn-title",
334
- x: frame.width / 2,
335
- y: 15,
336
- "text-anchor": "middle"
337
- }));
338
- }
432
+ if (lines.length)
433
+ drawTitle(doc, root, lines, frame.width / 2, 15);
339
434
  for (let row = 0;row <= rows; row++) {
340
435
  const cls = row === 0 && base === 1 && (!capo || fromNut) ? "mn-nut" : "mn-fret";
341
436
  root.appendChild(svgLine(doc, at(0, row), at(strings - 1, row), cls));
@@ -1613,5 +1708,5 @@ export {
1613
1708
  voicingCount
1614
1709
  };
1615
1710
 
1616
- //# debugId=0FDAEF7C01DCE45464756E2164756E21
1711
+ //# debugId=746B9A1294ADFAB664756E2164756E21
1617
1712
  //# sourceMappingURL=index.js.map