@yassimba/pi-loom-mermaid 0.2.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.
- package/LICENSE +21 -0
- package/README.md +28 -0
- package/index.ts +1 -0
- package/package.json +57 -0
- package/src/index.ts +103 -0
- package/src/loom-mermaid/ansi.ts +65 -0
- package/src/loom-mermaid/canvas.ts +466 -0
- package/src/loom-mermaid/class-style.ts +65 -0
- package/src/loom-mermaid/css-colors.ts +153 -0
- package/src/loom-mermaid/diagrams/class.ts +297 -0
- package/src/loom-mermaid/diagrams/er.ts +189 -0
- package/src/loom-mermaid/diagrams/flowchart.ts +519 -0
- package/src/loom-mermaid/diagrams/gitgraph.ts +230 -0
- package/src/loom-mermaid/diagrams/mindmap.ts +107 -0
- package/src/loom-mermaid/diagrams/pie.ts +121 -0
- package/src/loom-mermaid/diagrams/sequence.ts +305 -0
- package/src/loom-mermaid/diagrams/state.ts +279 -0
- package/src/loom-mermaid/diagrams/timeline.ts +117 -0
- package/src/loom-mermaid/graph-render.ts +195 -0
- package/src/loom-mermaid/graph.ts +205 -0
- package/src/loom-mermaid/index.ts +78 -0
- package/src/loom-mermaid/labels.ts +353 -0
- package/src/loom-mermaid/layout-seq.ts +234 -0
- package/src/loom-mermaid/layout.ts +1749 -0
- package/src/loom-mermaid/paint.ts +325 -0
- package/src/loom-mermaid/placement.ts +255 -0
- package/src/loom-mermaid/registry.ts +88 -0
- package/src/loom-mermaid/source-box.ts +111 -0
- package/src/loom-mermaid/statements.ts +228 -0
- package/src/loom-mermaid/types.ts +64 -0
- package/src/loom-mermaid/width-data.ts +993 -0
- package/src/loom-mermaid/width.ts +69 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { WIDTHS } from './width-data.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Display width, measured in grapheme clusters.
|
|
5
|
+
*
|
|
6
|
+
* A cluster is the unit both of measuring and of painting, so a box is always
|
|
7
|
+
* sized for exactly what gets drawn into it. Splitting those two — sizing by
|
|
8
|
+
* cluster but painting by code point — is what makes `👨👩👧` overflow its
|
|
9
|
+
* border in the Rust original.
|
|
10
|
+
*
|
|
11
|
+
* Clustering comes from `Intl.Segmenter` (UAX #29), which already handles ZWJ
|
|
12
|
+
* sequences, skin-tone modifiers, variation selectors, keycaps, flags and
|
|
13
|
+
* Hangul. Per-code-point widths are generated from the `unicode-width` crate.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' })
|
|
17
|
+
|
|
18
|
+
const VS16 = 0xfe0f
|
|
19
|
+
const isRegionalIndicator = (cp: number): boolean => cp >= 0x1f1e6 && cp <= 0x1f1ff
|
|
20
|
+
|
|
21
|
+
/** Width of one code point; the table covers the whole code point space. */
|
|
22
|
+
function codePointWidth(cp: number): number {
|
|
23
|
+
let lo = 0
|
|
24
|
+
let hi = WIDTHS.length - 1
|
|
25
|
+
while (lo <= hi) {
|
|
26
|
+
const mid = (lo + hi) >> 1
|
|
27
|
+
const run = WIDTHS[mid]
|
|
28
|
+
if (cp < run[0]) hi = mid - 1
|
|
29
|
+
else if (cp > run[1]) lo = mid + 1
|
|
30
|
+
else return run[2]
|
|
31
|
+
}
|
|
32
|
+
return 1
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Columns occupied by one grapheme cluster.
|
|
37
|
+
*
|
|
38
|
+
* The widest code point wins, so a base plus its combining marks measures as
|
|
39
|
+
* the base. Two adjustments: a variation selector requesting emoji
|
|
40
|
+
* presentation forces two columns, as does a regional indicator pair (a flag).
|
|
41
|
+
*
|
|
42
|
+
* Zero is a real answer — a soft hyphen or zero-width space occupies nothing,
|
|
43
|
+
* and callers skip painting such a cluster rather than reserving a cell.
|
|
44
|
+
*/
|
|
45
|
+
function clusterWidth(cluster: string): number {
|
|
46
|
+
let w = 0
|
|
47
|
+
let vs16 = false
|
|
48
|
+
let regional = 0
|
|
49
|
+
for (const ch of cluster) {
|
|
50
|
+
const cp = ch.codePointAt(0) as number
|
|
51
|
+
if (cp === VS16) vs16 = true
|
|
52
|
+
if (isRegionalIndicator(cp)) regional++
|
|
53
|
+
const cw = codePointWidth(cp)
|
|
54
|
+
if (cw > w) w = cw
|
|
55
|
+
}
|
|
56
|
+
return vs16 || regional >= 2 ? 2 : w
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Iterate clusters paired with their display width. */
|
|
60
|
+
export function* measured(s: string): Generator<[string, number]> {
|
|
61
|
+
for (const { segment } of segmenter.segment(s)) yield [segment, clusterWidth(segment)]
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Display columns of a string. */
|
|
65
|
+
export function stringWidth(s: string): number {
|
|
66
|
+
let w = 0
|
|
67
|
+
for (const { segment } of segmenter.segment(s)) w += clusterWidth(segment)
|
|
68
|
+
return w
|
|
69
|
+
}
|