Display pronunciation refactor (#1835)

* Simplify svg and container styles

* Rename pitch-accent-details to pitch-accent-graph-container

* Rename pitch-accent-graph* to pronunciation-graph*

* Remove unused template

* Omit quotes

* Rename pitch-accent-character to pronunciation-mora

* Simplify names

* Replace pronunciation-mora-inner with pronunciation-character

* Improve grouping of nasal characters

* Simplify character replacement

* Replace pseudo-elements with real elements

* Use an element rather than a fragment

* Rename functions

* Add wrapper

* Rename pitch-accent-characters to pronunciation-text-container

* Update test data

* Update downstep notation generation

* Rename pitch-accent-position to pronunciation-downstep-notation-container

* Space

* Wrap pronunciations in a container node

* Update preview

* Embed pitch accent styles directly
This commit is contained in:
toasted-nutbread 2021-07-17 14:44:36 -04:00 committed by GitHub
parent a933cfdc07
commit 3e938f1a1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 164 additions and 204 deletions

View File

@ -22,15 +22,21 @@
--pitch-accent-annotation-color: #ffffff;
}
.pitch-accent-character {
.pronunciation-downstep-notation {
display: inline;
}
.pronunciation-text {
display: inline;
}
.pronunciation-mora {
display: inline-block;
position: relative;
}
.pitch-accent-character::before {
.pronunciation-mora-line {
border-color: var(--pitch-accent-annotation-color);
}
.pitch-accent-character[data-pitch='high']::before {
content: '';
.pronunciation-mora[data-pitch=high]>.pronunciation-mora-line {
display: block;
user-select: none;
pointer-events: none;
@ -42,17 +48,17 @@
border-top-width: 0.1em;
border-top-style: solid;
}
.pitch-accent-character[data-pitch='high'][data-pitch-next='low']::before {
.pronunciation-mora[data-pitch=high][data-pitch-next=low]>.pronunciation-mora-line {
right: -0.1em;
height: 0.4em;
border-right-width: 0.1em;
border-right-style: solid;
}
.pitch-accent-character[data-pitch='high'][data-pitch-next='low'] {
.pronunciation-mora[data-pitch=high][data-pitch-next=low] {
padding-right: 0.1em;
margin-right: 0.1em;
}
.pitch-accent-character-devoice-indicator {
.pronunciation-devoice-indicator {
display: block;
position: absolute;
left: 50%;
@ -65,7 +71,7 @@
z-index: 1;
transform: translate(-50%, -50%);
}
.pitch-accent-character-nasal-indicator {
.pronunciation-nasal-indicator {
display: block;
position: absolute;
right: -0.125em;
@ -77,48 +83,48 @@
box-sizing: border-box;
z-index: 1;
}
.pitch-accent-character-nasal-diacritic {
.pronunciation-nasal-diacritic {
position: absolute;
width: 0;
height: 0;
opacity: 0;
}
.pitch-accent-position::before {
content: ' [';
.pronunciation-character {
display: inline;
}
.pitch-accent-position::after {
content: ']';
.pronunciation-character-group {
display: inline-block;
position: relative;
}
.pitch-accent-graph {
display: block;
.pronunciation-graph {
display: inline-block;
vertical-align: middle;
height: 1.5em;
transform: translateY(-0.875em);
}
.pitch-accent-graph-line,
.pitch-accent-graph-line-tail {
.pronunciation-graph-line,
.pronunciation-graph-line-tail {
fill: none;
stroke: var(--pitch-accent-annotation-color);
stroke-width: 5;
}
.pitch-accent-graph-line-tail {
.pronunciation-graph-line-tail {
stroke-dasharray: 5 5;
}
.pitch-accent-graph-dot {
.pronunciation-graph-dot {
fill: var(--pitch-accent-annotation-color);
stroke: var(--pitch-accent-annotation-color);
stroke-width: 5;
}
.pitch-accent-graph-dot-downstep1 {
.pronunciation-graph-dot-downstep1 {
fill: none;
stroke: var(--pitch-accent-annotation-color);
stroke-width: 5;
}
.pitch-accent-graph-dot-downstep2 {
.pronunciation-graph-dot-downstep2 {
fill: var(--pitch-accent-annotation-color);
}
.pitch-accent-graph-triangle {
.pronunciation-graph-triangle {
fill: none;
stroke: var(--pitch-accent-annotation-color);
stroke-width: 5;

View File

@ -1478,11 +1478,11 @@ button.definition-item-expansion-button:focus:focus-visible+.definition-item-con
.pitch-accent-tag-list:not([data-count='0']) {
margin-right: 0.375em;
}
.pitch-accent-details {
display: inline-block;
height: 0;
padding: 0 0.25em;
vertical-align: middle;
.pronunciation-downstep-notation-container {
margin-left: 0.25em;
}
.pronunciation-graph-container {
margin-left: 0.25em;
}
@ -1947,13 +1947,13 @@ button.footer-notification-close-button {
:root[data-show-pitch-accent-downstep-notation=true] .pitch-accent-disambiguation[data-type=reading] {
display: none;
}
:root[data-show-pitch-accent-downstep-notation=false] .pitch-accent-characters {
:root[data-show-pitch-accent-downstep-notation=false] .pronunciation-text-container {
display: none;
}
:root[data-show-pitch-accent-position-notation=false] .pitch-accent-position {
:root[data-show-pitch-accent-position-notation=false] .pronunciation-downstep-notation-container {
display: none;
}
:root[data-show-pitch-accent-graph=false] .pitch-accent-details {
:root[data-show-pitch-accent-graph=false] .pronunciation-graph-container {
display: none;
}

View File

@ -1,65 +0,0 @@
/*
* Copyright (C) 2020-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/>.
*/
:root {
--font-size-no-units: 14;
--font-size: calc(1px * var(--font-size-no-units));
--line-height-no-units: 20;
--line-height: calc(var(--line-height-no-units) / var(--font-size-no-units));
}
html {
background-color: transparent;
color: #333333;
}
html,
body {
margin: 0;
padding: 0;
border: 0;
overflow: hidden;
width: 100%;
height: 100%;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: var(--font-size);
line-height: var(--line-height);
}
ul {
margin: 0;
padding: 0 0 0 1.5em;
}
li {
margin: 0.425em 0;
}
.content {
display: flex;
min-width: 100%;
min-height: 100%;
box-sizing: border-box;
padding: 0;
flex-flow: column nowrap;
align-items: center;
justify-content: center;
}
.content-body {
max-width: 100%;
width: 700px;
flex: 0 0 auto;
height: auto;
}

View File

@ -1768,14 +1768,6 @@ code.anki-field-marker {
align-items: flex-start;
}
#pitch-accents-preview-frame {
border: none;
margin: 0;
padding: 0;
width: 100%;
height: calc(0.425em * 4 + 1em * var(--line-height-default) * 3);
}
.translation-text-replacement-entry {
display: grid;
grid-template-columns: auto auto 1fr auto;

View File

@ -84,8 +84,7 @@
<!-- Pitch accent templates -->
<template id="pitch-accent-group-template"><li class="pitch-accent-group"><span class="pitch-accent-group-tag-list tag-list"></span><ul class="pitch-accent-list"></ul></li></template>
<template id="pitch-accent-disambiguation-template"><span class="pitch-accent-disambiguation"></span></template>
<template id="pitch-accent-template"><li class="pitch-accent"><span class="pitch-accent-tag-list tag-list"></span><span class="pitch-accent-disambiguation-list"></span><span class="pitch-accent-characters"></span><span class="pitch-accent-position"></span><span class="pitch-accent-details"></span></li></template>
<template id="pitch-accent-character-template"><span class="pitch-accent-character"><span class="pitch-accent-character-inner"></span></span></template>
<template id="pitch-accent-template"><li class="pitch-accent"><span class="pitch-accent-tag-list tag-list"></span><span class="pitch-accent-disambiguation-list"></span><span class="pronunciation-representation-list"><span class="pronunciation-text-container"></span><span class="pronunciation-downstep-notation-container"></span><span class="pronunciation-graph-container"></span></span></li></template>
<!-- Kanji entry templates -->
<template id="kanji-entry-template" data-remove-whitespace-text="true"><div class="entry" data-type="kanji">

View File

@ -470,20 +470,20 @@ class DisplayGenerator {
if (devoicePositions.length > 0) { node.dataset.devoiceMoraPosition = devoicePositions.join(' '); }
node.dataset.tagCount = `${tags.length}`;
let n = node.querySelector('.pitch-accent-position');
this._setTextContent(n, `${position}`, '');
n = node.querySelector('.pitch-accent-tag-list');
let n = node.querySelector('.pitch-accent-tag-list');
this._appendMultiple(n, this._createTag.bind(this), tags);
n = node.querySelector('.pitch-accent-disambiguation-list');
this._createPitchAccentDisambiguations(n, exclusiveTerms, exclusiveReadings);
n = node.querySelector('.pitch-accent-characters');
n.lang = 'ja';
n.appendChild(this._pronunciationGenerator.createPitchAccentHtml(morae, position, nasalPositions, devoicePositions));
n = node.querySelector('.pronunciation-downstep-notation-container');
n.appendChild(this._pronunciationGenerator.createPronunciationDownstepNotation(position));
node.querySelector('.pitch-accent-details').appendChild(this._pronunciationGenerator.createPitchGraph(morae, position));
n = node.querySelector('.pronunciation-text-container');
n.lang = 'ja';
n.appendChild(this._pronunciationGenerator.createPronunciationText(morae, position, nasalPositions, devoicePositions));
node.querySelector('.pronunciation-graph-container').appendChild(this._pronunciationGenerator.createPronunciationGraph(morae, position));
return node;
}

View File

@ -20,11 +20,12 @@ class PronunciationGenerator {
this._japaneseUtil = japaneseUtil;
}
createPitchAccentHtml(morae, downstepPosition, nasalPositions, devoicePositions) {
createPronunciationText(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();
const container = document.createElement('span');
container.className = 'pronunciation-text';
for (let i = 0, ii = morae.length; i < ii; ++i) {
const i1 = i + 1;
const mora = morae[i];
@ -34,48 +35,72 @@ class PronunciationGenerator {
const devoice = devoicePositionsSet !== null && devoicePositionsSet.has(i1);
const n1 = document.createElement('span');
n1.className = 'pitch-accent-character';
n1.className = 'pronunciation-mora';
n1.dataset.position = `${i}`;
n1.dataset.pitch = highPitch ? 'high' : 'low';
n1.dataset.pitchNext = highPitchNext ? 'high' : 'low';
const characterNodes = [];
for (const character of mora) {
const n2 = document.createElement('span');
n2.className = 'pitch-accent-character-inner';
n2.textContent = mora;
n2.className = 'pronunciation-character';
n2.textContent = character;
n1.appendChild(n2);
characterNodes.push(n2);
}
if (devoice) {
n1.dataset.devoice = 'true';
const n3 = document.createElement('span');
n3.className = 'pitch-accent-character-devoice-indicator';
n3.className = 'pronunciation-devoice-indicator';
n1.appendChild(n3);
}
if (nasal) {
if (nasal && characterNodes.length > 0) {
n1.dataset.nasal = 'true';
const group = document.createElement('span');
group.className = 'pronunciation-character-group';
const n2 = characterNodes[0];
const character = n2.textContent;
const characterInfo = jp.getKanaDiacriticInfo(character);
if (characterInfo !== null) {
n1.dataset.originalText = mora;
n2.textContent = this._getPlainMora(mora);
n2.dataset.originalText = character;
n2.textContent = characterInfo.character;
}
let n3 = document.createElement('span');
n3.className = 'pitch-accent-character-nasal-diacritic';
n3.className = 'pronunciation-nasal-diacritic';
n3.textContent = '\u309a'; // Combining handakuten
n1.appendChild(n3);
group.appendChild(n3);
n3 = document.createElement('span');
n3.className = 'pitch-accent-character-nasal-indicator';
n1.appendChild(n3);
n3.className = 'pronunciation-nasal-indicator';
group.appendChild(n3);
n2.parentNode.replaceChild(group, n2);
group.insertBefore(n2, group.firstChild);
}
fragment.appendChild(n1);
const line = document.createElement('span');
line.className = 'pronunciation-mora-line';
n1.appendChild(line);
container.appendChild(n1);
}
return fragment;
return container;
}
createPitchGraph(morae, downstepPosition) {
createPronunciationGraph(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('class', 'pronunciation-graph');
svg.setAttribute('focusable', 'false');
svg.setAttribute('viewBox', `0 0 ${50 * (ii + 1)} 100`);
@ -101,7 +126,7 @@ class PronunciationGenerator {
pathPoints.push(`${x} ${y}`);
}
path1.setAttribute('class', 'pitch-accent-graph-line');
path1.setAttribute('class', 'pronunciation-graph-line');
path1.setAttribute('d', `M${pathPoints.join(' L')}`);
pathPoints.splice(0, ii - 1);
@ -113,26 +138,51 @@ class PronunciationGenerator {
pathPoints.push(`${x} ${y}`);
}
path2.setAttribute('class', 'pitch-accent-graph-line-tail');
path2.setAttribute('class', 'pronunciation-graph-line-tail');
path2.setAttribute('d', `M${pathPoints.join(' L')}`);
return svg;
}
createPronunciationDownstepNotation(downstepPosition) {
downstepPosition = `${downstepPosition}`;
const n1 = document.createElement('span');
n1.className = 'pronunciation-downstep-notation';
n1.dataset.downstepPosition = downstepPosition;
let n2 = document.createElement('span');
n2.className = 'pronunciation-downstep-notation-prefix';
n2.textContent = '[';
n1.appendChild(n2);
n2 = document.createElement('span');
n2.className = 'pronunciation-downstep-notation-number';
n2.textContent = downstepPosition;
n1.appendChild(n2);
n2 = document.createElement('span');
n2.className = 'pronunciation-downstep-notation-suffix';
n2.textContent = ']';
n1.appendChild(n2);
return n1;
}
// Private
_addGraphDot(container, svgns, x, y) {
container.appendChild(this._createGraphCircle(svgns, 'pitch-accent-graph-dot', x, y, '15'));
container.appendChild(this._createGraphCircle(svgns, 'pronunciation-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'));
container.appendChild(this._createGraphCircle(svgns, 'pronunciation-graph-dot-downstep1', x, y, '15'));
container.appendChild(this._createGraphCircle(svgns, 'pronunciation-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('class', 'pronunciation-graph-triangle');
node.setAttribute('d', 'M0 13 L15 -13 L-15 -13 Z');
node.setAttribute('transform', `translate(${x},${y})`);
container.appendChild(node);
@ -146,11 +196,4 @@ class PronunciationGenerator {
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)}`;
}
}

View File

@ -1,54 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Yomichan Pitch Accents Preview</title>
<link rel="icon" type="image/png" href="/images/icon16.png" sizes="16x16">
<link rel="icon" type="image/png" href="/images/icon19.png" sizes="19x19">
<link rel="icon" type="image/png" href="/images/icon32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/images/icon38.png" sizes="38x38">
<link rel="icon" type="image/png" href="/images/icon48.png" sizes="48x48">
<link rel="icon" type="image/png" href="/images/icon64.png" sizes="64x64">
<link rel="icon" type="image/png" href="/images/icon128.png" sizes="128x128">
<link rel="stylesheet" type="text/css" href="/css/display-pronunciation.css">
<link rel="stylesheet" type="text/css" href="/css/pitch-accents-preview.css">
</head>
<body>
<div class="content"><div class="content-body">
<ul>
<li>
<strong>Downstep notation</strong> -
<span class="format-preview" lang="ja">
<span class="pitch-accent-characters"><span class="pitch-accent-character" data-position="0" data-pitch="high" data-pitch-next="low"><span class="pitch-accent-character-inner"></span></span><span class="pitch-accent-character" data-position="1" data-pitch="low" data-pitch-next="low"><span class="pitch-accent-character-inner"></span></span></span>
</span>
</li>
<li>
<strong>Downstep position</strong> -
<span class="format-preview">
<span class="pitch-accent-position">1</span>
</span>
</li>
<li>
<strong>Graph</strong> -
<span class="format-preview">
<span class="pitch-accent-details">
<svg xmlns="http://www.w3.org/2000/svg" class="pitch-accent-graph" focusable="false" viewBox="0 0 150 100">
<path class="pitch-accent-graph-line" d="M25 25 L75 75"></path>
<path class="pitch-accent-graph-line-tail" d="M75 75 L125 75"></path>
<circle class="pitch-accent-graph-dot-downstep1" cx="25" cy="25" r="15"></circle>
<circle class="pitch-accent-graph-dot-downstep2" cx="25" cy="25" r="5"></circle>
<circle class="pitch-accent-graph-dot" cx="75" cy="75" r="15"></circle>
<path class="pitch-accent-graph-triangle" d="M0 13 L15 -13 L-15 -13 Z" transform="translate(125,75)"></path>
</svg>
</span>
</span>
</li>
</ul>
</div></div>
</body>
</html>

View File

@ -13,6 +13,7 @@
<link rel="icon" type="image/png" href="/images/icon128.png" sizes="128x128">
<link rel="stylesheet" type="text/css" href="/css/material.css">
<link rel="stylesheet" type="text/css" href="/css/settings.css">
<link rel="stylesheet" type="text/css" href="/css/display-pronunciation.css">
</head>
<body>
@ -747,7 +748,35 @@
Pitch accents for terms and expressions can be shown if a dictionary supporting pitch accents is installed.
There are currently three different ways that pitch accents can be presented:
</p>
<iframe data-src="/pitch-accents-preview.html" id="pitch-accents-preview-frame" class="defer-load-iframe" data-parent-distance="1"></iframe>
<ul>
<li>
<strong>Downstep notation</strong> -
<span class="format-preview" lang="ja">
<span class="pronunciation-text-container"><span class="pronunciation-text"><span class="pronunciation-mora" data-position="0" data-pitch="high" data-pitch-next="low"><span class="pronunciation-character"></span><span class="pronunciation-mora-line"></span></span><span class="pronunciation-mora" data-position="1" data-pitch="low" data-pitch-next="low"><span class="pronunciation-character"></span><span class="pronunciation-mora-line"></span></span></span></span>
</span>
</li>
<li>
<strong>Downstep position</strong> -
<span class="format-preview">
<span class="pronunciation-downstep-notation-container"><span class="pronunciation-downstep-notation" data-downstep-position="1"><span class="pronunciation-downstep-notation-prefix">[</span><span class="pronunciation-downstep-notation-number">1</span><span class="pronunciation-downstep-notation-suffix">]</span></span></span>
</span>
</li>
<li>
<strong>Graph</strong> -
<span class="format-preview">
<span class="pronunciation-graph-container">
<svg xmlns="http://www.w3.org/2000/svg" class="pronunciation-graph" focusable="false" viewBox="0 0 150 100">
<path class="pronunciation-graph-line" d="M25 25 L75 75"></path>
<path class="pronunciation-graph-line-tail" d="M75 75 L125 75"></path>
<circle class="pronunciation-graph-dot-downstep1" cx="25" cy="25" r="15"></circle>
<circle class="pronunciation-graph-dot-downstep2" cx="25" cy="25" r="5"></circle>
<circle class="pronunciation-graph-dot" cx="75" cy="75" r="15"></circle>
<path class="pronunciation-graph-triangle" d="M0 13 L15 -13 L-15 -13 Z" transform="translate(125,75)"></path>
</svg>
</span>
</span>
</li>
</ul>
<p>
<a class="more-toggle" data-parent-distance="3">Less&hellip;</a>
</p>

View File

@ -62,6 +62,16 @@
]
}
],
[
"所業",
"pitch",
{
"reading": "しょぎょう",
"pitches": [
{"position": 0, "nasal": 2}
]
}
],
[
"土木工事",
"pitch",

View File

@ -162,8 +162,8 @@ async function testDatabase1() {
true
);
vm.assert.deepStrictEqual(counts, {
counts: [{kanji: 2, kanjiMeta: 2, terms: 15, termMeta: 15, tagMeta: 15, media: 2}],
total: {kanji: 2, kanjiMeta: 2, terms: 15, termMeta: 15, tagMeta: 15, media: 2}
counts: [{kanji: 2, kanjiMeta: 2, terms: 15, termMeta: 16, tagMeta: 15, media: 2}],
total: {kanji: 2, kanjiMeta: 2, terms: 15, termMeta: 16, tagMeta: 15, media: 2}
});
// Test find* functions