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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/*
* 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 StructuredContentGenerator {
constructor(templates, mediaLoader, document) {
this._templates = templates;
this._mediaLoader = mediaLoader;
this._document = document;
}
createStructuredContent(content, dictionary) {
if (typeof content === 'string') {
return this._createTextNode(content);
}
if (!(typeof content === 'object' && content !== null)) {
return null;
}
if (Array.isArray(content)) {
const fragment = this._createDocumentFragment();
for (const item of content) {
const child = this.createStructuredContent(item, dictionary);
if (child !== null) { fragment.appendChild(child); }
}
return fragment;
}
const {tag} = content;
switch (tag) {
case 'br':
return this._createStructuredContentElement(tag, content, dictionary, 'simple', false, false);
case 'ruby':
case 'rt':
case 'rp':
return this._createStructuredContentElement(tag, content, dictionary, 'simple', true, false);
case 'table':
return this._createStructuredContentTableElement(tag, content, dictionary);
case 'thead':
case 'tbody':
case 'tfoot':
case 'tr':
return this._createStructuredContentElement(tag, content, dictionary, 'table', true, false);
case 'th':
case 'td':
return this._createStructuredContentElement(tag, content, dictionary, 'table-cell', true, true);
case 'div':
case 'span':
return this._createStructuredContentElement(tag, content, dictionary, 'simple', true, true);
case 'img':
return this.createDefinitionImage(content, dictionary);
}
return null;
}
createDefinitionImage(data, dictionary) {
const {
path,
width,
height,
preferredWidth,
preferredHeight,
title,
pixelated,
imageRendering,
appearance,
background,
collapsed,
collapsible,
verticalAlign,
sizeUnits
} = data;
const hasPreferredWidth = (typeof preferredWidth === 'number');
const hasPreferredHeight = (typeof preferredHeight === 'number');
const aspectRatio = (
hasPreferredWidth && hasPreferredHeight ?
preferredWidth / preferredHeight :
width / height
);
const usedWidth = (
hasPreferredWidth ?
preferredWidth :
(hasPreferredHeight ? preferredHeight * aspectRatio : width)
);
const node = this._templates.instantiate('gloss-item-image');
const imageContainer = node.querySelector('.gloss-image-container');
const aspectRatioSizer = node.querySelector('.gloss-image-aspect-ratio-sizer');
const image = node.querySelector('.gloss-image');
const imageBackground = node.querySelector('.gloss-image-background');
node.dataset.path = path;
node.dataset.dictionary = dictionary;
node.dataset.imageLoadState = 'not-loaded';
node.dataset.hasAspectRatio = 'true';
node.dataset.imageRendering = typeof imageRendering === 'string' ? imageRendering : (pixelated ? 'pixelated' : 'auto');
node.dataset.appearance = typeof appearance === 'string' ? appearance : 'auto';
node.dataset.background = typeof background === 'boolean' ? `${background}` : 'true';
node.dataset.collapsed = typeof collapsed === 'boolean' ? `${collapsed}` : 'false';
node.dataset.collapsible = typeof collapsible === 'boolean' ? `${collapsible}` : 'true';
if (typeof verticalAlign === 'string') {
node.dataset.verticalAlign = verticalAlign;
}
if (typeof sizeUnits === 'string' && (hasPreferredWidth || hasPreferredHeight)) {
node.dataset.sizeUnits = sizeUnits;
}
imageContainer.style.width = `${usedWidth}em`;
if (typeof title === 'string') {
imageContainer.title = title;
}
aspectRatioSizer.style.paddingTop = `${aspectRatio * 100.0}%`;
if (this._mediaLoader !== null) {
this._mediaLoader.loadMedia(
path,
dictionary,
(url) => this._setImageData(node, image, imageBackground, url, false),
() => this._setImageData(node, image, imageBackground, null, true)
);
}
return node;
}
// Private
_createElement(tagName) {
return this._document.createElement(tagName);
}
_createTextNode(data) {
return this._document.createTextNode(data);
}
_createDocumentFragment() {
return this._document.createDocumentFragment();
}
_setImageData(node, image, imageBackground, url, unloaded) {
if (url !== null) {
image.src = url;
node.href = url;
node.dataset.imageLoadState = 'loaded';
imageBackground.style.setProperty('--image', `url("${url}")`);
} else {
image.removeAttribute('src');
node.removeAttribute('href');
node.dataset.imageLoadState = unloaded ? 'unloaded' : 'load-error';
imageBackground.style.removeProperty('--image');
}
}
_createStructuredContentTableElement(tag, content, dictionary) {
const container = this._createElement('div');
container.classList = 'gloss-sc-table-container';
const table = this._createStructuredContentElement(tag, content, dictionary, 'table', true, false);
container.appendChild(table);
return container;
}
_createStructuredContentElement(tag, content, dictionary, type, hasChildren, hasStyle) {
const node = this._createElement(tag);
node.className = `gloss-sc-${tag}`;
switch (type) {
case 'table-cell':
{
const {colSpan, rowSpan} = content;
if (typeof colSpan === 'number') { node.colSpan = colSpan; }
if (typeof rowSpan === 'number') { node.rowSpan = rowSpan; }
}
break;
}
if (hasStyle) {
const {style} = content;
if (typeof style === 'object' && style !== null) {
this._setStructuredContentElementStyle(node, style);
}
}
if (hasChildren) {
const child = this.createStructuredContent(content.content, dictionary);
if (child !== null) { node.appendChild(child); }
}
return node;
}
_setStructuredContentElementStyle(node, contentStyle) {
const {style} = node;
const {fontStyle, fontWeight, fontSize, textDecorationLine, verticalAlign} = contentStyle;
if (typeof fontStyle === 'string') { style.fontStyle = fontStyle; }
if (typeof fontWeight === 'string') { style.fontWeight = fontWeight; }
if (typeof fontSize === 'string') { style.fontSize = fontSize; }
if (typeof verticalAlign === 'string') { style.verticalAlign = verticalAlign; }
if (typeof textDecorationLine === 'string') {
style.textDecoration = textDecorationLine;
} else if (Array.isArray(textDecorationLine)) {
style.textDecoration = textDecorationLine.join(' ');
}
}
}
|