summaryrefslogtreecommitdiff
path: root/ext/js/display/pronunciation-generator.js
blob: 13e3db3e5ba5a94c34801b949da39a37b54f1589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2021  Yomichan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

class PronunciationGenerator {
    constructor(japaneseUtil) {
        this._japaneseUtil = japaneseUtil;
    }

    createPitchAccentHtml(morae, downstepPosition, nasalPositions, devoicePositions) {
        const jp = this._japaneseUtil;
        const nasalPositionsSet = nasalPositions.length > 0 ? new Set(nasalPositions) : null;
        const devoicePositionsSet = devoicePositions.length > 0 ? new Set(devoicePositions) : null;
        const fragment = document.createDocumentFragment();
        for (let i = 0, ii = morae.length; i < ii; ++i) {
            const i1 = i + 1;
            const mora = morae[i];
            const highPitch = jp.isMoraPitchHigh(i, downstepPosition);
            const highPitchNext = jp.isMoraPitchHigh(i1, downstepPosition);
            const nasal = nasalPositionsSet !== null && nasalPositionsSet.has(i1);
            const devoice = devoicePositionsSet !== null && devoicePositionsSet.has(i1);

            const n1 = document.createElement('span');
            n1.className = 'pitch-accent-character';
            n1.dataset.position = `${i}`;
            n1.dataset.pitch = highPitch ? 'high' : 'low';
            n1.dataset.pitchNext = highPitchNext ? 'high' : 'low';

            const n2 = document.createElement('span');
            n2.className = 'pitch-accent-character-inner';
            n2.textContent = mora;
            n1.appendChild(n2);

            if (devoice) {
                n1.dataset.devoice = 'true';
                const n3 = document.createElement('span');
                n3.className = 'pitch-accent-character-devoice-indicator';
                n1.appendChild(n3);
            }
            if (nasal) {
                n1.dataset.nasal = 'true';
                n1.dataset.originalText = mora;
                n2.textContent = this._getPlainMora(mora);
                let n3 = document.createElement('span');
                n3.className = 'pitch-accent-character-nasal-diacritic';
                n3.textContent = '\u309a'; // Combining handakuten
                n1.appendChild(n3);
                n3 = document.createElement('span');
                n3.className = 'pitch-accent-character-nasal-indicator';
                n1.appendChild(n3);
            }

            fragment.appendChild(n1);
        }
        return fragment;
    }

    createPitchGraph(morae, downstepPosition) {
        const jp = this._japaneseUtil;
        const ii = morae.length;

        const svgns = 'http://www.w3.org/2000/svg';
        const svg = document.createElementNS(svgns, 'svg');
        svg.setAttribute('xmlns', svgns);
        svg.setAttribute('class', 'pitch-accent-graph');
        svg.setAttribute('focusable', 'false');
        svg.setAttribute('viewBox', `0 0 ${50 * (ii + 1)} 100`);

        if (ii <= 0) { return svg; }

        const path1 = document.createElementNS(svgns, 'path');
        svg.appendChild(path1);

        const path2 = document.createElementNS(svgns, 'path');
        svg.appendChild(path2);

        const pathPoints = [];
        for (let i = 0; i < ii; ++i) {
            const highPitch = jp.isMoraPitchHigh(i, downstepPosition);
            const highPitchNext = jp.isMoraPitchHigh(i + 1, downstepPosition);
            const x = i * 50 + 25;
            const y = highPitch ? 25 : 75;
            if (highPitch && !highPitchNext) {
                this._addGraphDotDownstep(svg, svgns, x, y);
            } else {
                this._addGraphDot(svg, svgns, x, y);
            }
            pathPoints.push(`${x} ${y}`);
        }

        path1.setAttribute('class', 'pitch-accent-graph-line');
        path1.setAttribute('d', `M${pathPoints.join(' L')}`);

        pathPoints.splice(0, ii - 1);
        {
            const highPitch = jp.isMoraPitchHigh(ii, downstepPosition);
            const x = ii * 50 + 25;
            const y = highPitch ? 25 : 75;
            this._addGraphTriangle(svg, svgns, x, y);
            pathPoints.push(`${x} ${y}`);
        }

        path2.setAttribute('class', 'pitch-accent-graph-line-tail');
        path2.setAttribute('d', `M${pathPoints.join(' L')}`);

        return svg;
    }

    // Private

    _addGraphDot(container, svgns, x, y) {
        container.appendChild(this._createGraphCircle(svgns, 'pitch-accent-graph-dot', x, y, '15'));
    }

    _addGraphDotDownstep(container, svgns, x, y) {
        container.appendChild(this._createGraphCircle(svgns, 'pitch-accent-graph-dot-downstep1', x, y, '15'));
        container.appendChild(this._createGraphCircle(svgns, 'pitch-accent-graph-dot-downstep2', x, y, '5'));
    }

    _addGraphTriangle(container, svgns, x, y) {
        const node = document.createElementNS(svgns, 'path');
        node.setAttribute('class', 'pitch-accent-graph-triangle');
        node.setAttribute('d', 'M0 13 L15 -13 L-15 -13 Z');
        node.setAttribute('transform', `translate(${x},${y})`);
        container.appendChild(node);
    }

    _createGraphCircle(svgns, className, x, y, radius) {
        const node = document.createElementNS(svgns, 'circle');
        node.setAttribute('class', className);
        node.setAttribute('cx', `${x}`);
        node.setAttribute('cy', `${y}`);
        node.setAttribute('r', radius);
        return node;
    }

    _getPlainMora(mora) {
        const first = mora[0];
        const info = this._japaneseUtil.getKanaDiacriticInfo(first);
        if (info === null) { return mora; }
        return `${info.character}${mora.substring(1)}`;
    }
}