@willwade/aac-processors 0.2.20 → 0.3.1
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/README.md +1 -0
- package/dist/browser/index.browser.js +17 -1
- package/dist/browser/processors/gotalkNow/nsKeyed.js +108 -0
- package/dist/browser/processors/gotalkNowProcessor.js +538 -0
- package/dist/browser/processors/gridsetProcessor.js +2 -2
- package/dist/browser/processors/obfProcessor.js +17 -6
- package/dist/browser/validation/gridsetValidator.js +54 -198
- package/dist/cli/index.js +3 -0
- package/dist/core/analyze.js +5 -0
- package/dist/core/treeStructure.d.ts +12 -0
- package/dist/gotalkNow.d.ts +6 -0
- package/dist/gotalkNow.js +11 -0
- package/dist/index.browser.d.ts +1 -0
- package/dist/index.browser.js +19 -2
- package/dist/index.node.d.ts +1 -0
- package/dist/index.node.js +6 -1
- package/dist/processors/gotalkNow/nsKeyed.d.ts +27 -0
- package/dist/processors/gotalkNow/nsKeyed.js +112 -0
- package/dist/processors/gotalkNowProcessor.d.ts +51 -0
- package/dist/processors/gotalkNowProcessor.js +544 -0
- package/dist/processors/gridsetProcessor.js +2 -2
- package/dist/processors/index.d.ts +1 -0
- package/dist/processors/index.js +3 -1
- package/dist/processors/obfProcessor.js +17 -6
- package/dist/validation/gridsetValidator.d.ts +8 -15
- package/dist/validation/gridsetValidator.js +54 -198
- package/package.json +5 -1
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GoTalk NOW Processor
|
|
3
|
+
*
|
|
4
|
+
* Reads, translates, and converts Attainment Company's GoTalk NOW
|
|
5
|
+
* communication-board files (`.gtbz`).
|
|
6
|
+
*
|
|
7
|
+
* A `.gtbz` file is a ZIP archive containing Apple property lists:
|
|
8
|
+
* - BookInfo.plist – format version (GoTalkBookFormatVersion)
|
|
9
|
+
* - PageData.plist – pages dict keyed by page id (positive = content,
|
|
10
|
+
* negative = system/express-bar pages)
|
|
11
|
+
* - PageOrder.plist – ordered array of page ids
|
|
12
|
+
* - PageHistory.plist – navigation history (usually empty)
|
|
13
|
+
* - SettingsWriteOut.plist– app-level settings (TTS, scanning, …)
|
|
14
|
+
* - IconSource-<id>.png – page icons
|
|
15
|
+
* - Source-<page>-<btn>-<guid>-…png – button images
|
|
16
|
+
* - <page>-<btn>.caf – audio recordings (Core Audio Format)
|
|
17
|
+
* - Backup-<n>.zip – rolling backups
|
|
18
|
+
* - REQUIRESREGEN – marker file
|
|
19
|
+
*
|
|
20
|
+
* Colours and fonts in PageData are NSKeyedArchiver-encoded binary plists
|
|
21
|
+
* carried inside `<data>` tags. They are treated as opaque blobs and preserved
|
|
22
|
+
* verbatim so that only text fields are touched during translation.
|
|
23
|
+
*/
|
|
24
|
+
import { BaseProcessor, } from '../core/baseProcessor';
|
|
25
|
+
import { AACTree, AACPage, AACButton, AACSemanticCategory, AACSemanticIntent, } from '../core/treeStructure';
|
|
26
|
+
import plist from 'plist';
|
|
27
|
+
import { ValidationFailureError, buildValidationResultFromMessage, } from '../validation/validationTypes';
|
|
28
|
+
import { getBasename } from '../utils/io';
|
|
29
|
+
import { decodeNsColor, decodeNsFont } from './gotalkNow/nsKeyed';
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
// GoTalk NOW button type → semantic-action mapping
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
function buildSemanticAction(rawButton, pageId, buttonIndex) {
|
|
34
|
+
const buttonType = rawButton.ButtonType || 'TTS';
|
|
35
|
+
const actionData = rawButton.ActionData || {};
|
|
36
|
+
const label = rawButton.ButtonText ?? '';
|
|
37
|
+
const ttsText = actionData.TTSText;
|
|
38
|
+
const spoken = ttsText && ttsText.length > 0 ? ttsText : label;
|
|
39
|
+
switch (buttonType) {
|
|
40
|
+
case 'Jump': {
|
|
41
|
+
const jumpTo = actionData.JumpTo;
|
|
42
|
+
const target = jumpTo !== undefined ? String(jumpTo) : undefined;
|
|
43
|
+
return {
|
|
44
|
+
semanticAction: {
|
|
45
|
+
category: AACSemanticCategory.NAVIGATION,
|
|
46
|
+
intent: AACSemanticIntent.NAVIGATE_TO,
|
|
47
|
+
targetId: target,
|
|
48
|
+
platformData: {
|
|
49
|
+
gotalkNow: { buttonType: 'Jump', jumpTo, jumpBook: actionData.JumpBook },
|
|
50
|
+
},
|
|
51
|
+
fallback: { type: 'NAVIGATE', targetPageId: target },
|
|
52
|
+
},
|
|
53
|
+
message: '',
|
|
54
|
+
targetPageId: target,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
case 'Audio':
|
|
58
|
+
case 'Recorded': {
|
|
59
|
+
// Recorded audio: plays a .caf file named <pageId>-<buttonIndex>.caf.
|
|
60
|
+
// Recorded buttons may also have TrimStart/TrimEnd for audio trimming.
|
|
61
|
+
const audioLocation = `${pageId}-${buttonIndex}.caf`;
|
|
62
|
+
return {
|
|
63
|
+
semanticAction: {
|
|
64
|
+
category: AACSemanticCategory.MEDIA,
|
|
65
|
+
intent: AACSemanticIntent.PLAY_SOUND,
|
|
66
|
+
platformData: {
|
|
67
|
+
gotalkNow: {
|
|
68
|
+
buttonType,
|
|
69
|
+
audioLocation,
|
|
70
|
+
trimStart: actionData.TrimStart,
|
|
71
|
+
trimEnd: actionData.TrimEnd,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
fallback: { type: 'ACTION' },
|
|
75
|
+
},
|
|
76
|
+
message: spoken,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
case 'URL': {
|
|
80
|
+
return {
|
|
81
|
+
semanticAction: {
|
|
82
|
+
category: AACSemanticCategory.SYSTEM_CONTROL,
|
|
83
|
+
intent: AACSemanticIntent.WEB_NAVIGATE,
|
|
84
|
+
text: actionData.URL,
|
|
85
|
+
platformData: {
|
|
86
|
+
gotalkNow: {
|
|
87
|
+
buttonType: 'URL',
|
|
88
|
+
url: actionData.URL,
|
|
89
|
+
openInternally: actionData.OpenInternally,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
fallback: { type: 'ACTION' },
|
|
93
|
+
},
|
|
94
|
+
message: spoken,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
case 'GoVisual': {
|
|
98
|
+
return {
|
|
99
|
+
semanticAction: {
|
|
100
|
+
category: AACSemanticCategory.SYSTEM_CONTROL,
|
|
101
|
+
intent: AACSemanticIntent.PLATFORM_SPECIFIC,
|
|
102
|
+
platformData: {
|
|
103
|
+
gotalkNow: {
|
|
104
|
+
buttonType: 'GoVisual',
|
|
105
|
+
goVisual: actionData.GoVisual,
|
|
106
|
+
goVisualLink: actionData.GoVisualLink,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
fallback: { type: 'ACTION' },
|
|
110
|
+
},
|
|
111
|
+
message: spoken,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
case 'Video': {
|
|
115
|
+
return {
|
|
116
|
+
semanticAction: {
|
|
117
|
+
category: AACSemanticCategory.MEDIA,
|
|
118
|
+
intent: AACSemanticIntent.PLAY_VIDEO,
|
|
119
|
+
platformData: {
|
|
120
|
+
gotalkNow: { buttonType: 'Video' },
|
|
121
|
+
},
|
|
122
|
+
fallback: { type: 'ACTION' },
|
|
123
|
+
},
|
|
124
|
+
message: spoken,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
case 'Express':
|
|
128
|
+
case 'Bookmark': {
|
|
129
|
+
return {
|
|
130
|
+
semanticAction: {
|
|
131
|
+
category: AACSemanticCategory.SYSTEM_CONTROL,
|
|
132
|
+
intent: AACSemanticIntent.PLATFORM_SPECIFIC,
|
|
133
|
+
platformData: {
|
|
134
|
+
gotalkNow: { buttonType },
|
|
135
|
+
},
|
|
136
|
+
fallback: { type: 'ACTION' },
|
|
137
|
+
},
|
|
138
|
+
message: spoken,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
case 'TTS':
|
|
142
|
+
default: {
|
|
143
|
+
// Empty/placeholder cells (no ButtonType or no label) get a SPEAK with empty text.
|
|
144
|
+
return {
|
|
145
|
+
semanticAction: {
|
|
146
|
+
category: AACSemanticCategory.COMMUNICATION,
|
|
147
|
+
intent: AACSemanticIntent.SPEAK_TEXT,
|
|
148
|
+
text: spoken,
|
|
149
|
+
platformData: {
|
|
150
|
+
gotalkNow: { buttonType: buttonType === 'TTS' ? 'TTS' : 'Empty' },
|
|
151
|
+
},
|
|
152
|
+
fallback: { type: 'SPEAK', message: spoken },
|
|
153
|
+
},
|
|
154
|
+
message: spoken,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Derive a square-ish grid layout from the declared button count.
|
|
161
|
+
* GoTalk NOW uses fixed layouts (1, 4, 9, 16, 25, 36 …).
|
|
162
|
+
*/
|
|
163
|
+
function gridDimensionsFromButtonCount(count) {
|
|
164
|
+
const side = Math.max(1, Math.ceil(Math.sqrt(Math.max(count, 1))));
|
|
165
|
+
return { rows: side, cols: side };
|
|
166
|
+
}
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
// Processor
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
class GotalkNowProcessor extends BaseProcessor {
|
|
171
|
+
constructor(options) {
|
|
172
|
+
super(options);
|
|
173
|
+
this.capabilities = {
|
|
174
|
+
wordList: 'none',
|
|
175
|
+
preservesAssetsOnSave: true,
|
|
176
|
+
newCellCreation: 'allowed',
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
// ---- Zip access ---------------------------------------------------------
|
|
180
|
+
async openZip(filePathOrBuffer) {
|
|
181
|
+
const zip = await this.options.zipAdapter(filePathOrBuffer, this.options.fileAdapter);
|
|
182
|
+
return zip;
|
|
183
|
+
}
|
|
184
|
+
async readPlistFromZip(zip, name, fallback) {
|
|
185
|
+
const files = zip.listFiles();
|
|
186
|
+
if (!files.includes(name)) {
|
|
187
|
+
if (fallback)
|
|
188
|
+
return fallback();
|
|
189
|
+
throw new Error(`GoTalk NOW archive is missing ${name}`);
|
|
190
|
+
}
|
|
191
|
+
const bytes = await zip.readFile(name);
|
|
192
|
+
const text = typeof Buffer !== 'undefined' && Buffer.isBuffer(bytes)
|
|
193
|
+
? bytes.toString('utf8')
|
|
194
|
+
: new TextDecoder().decode(bytes);
|
|
195
|
+
return plist.parse(text);
|
|
196
|
+
}
|
|
197
|
+
// ---- extractTexts -------------------------------------------------------
|
|
198
|
+
async extractTexts(filePathOrBuffer) {
|
|
199
|
+
const tree = await this.loadIntoTree(filePathOrBuffer);
|
|
200
|
+
const texts = [];
|
|
201
|
+
for (const pageId in tree.pages) {
|
|
202
|
+
const page = tree.pages[pageId];
|
|
203
|
+
if (page.name)
|
|
204
|
+
texts.push(page.name);
|
|
205
|
+
page.buttons.forEach((btn) => {
|
|
206
|
+
if (btn.label)
|
|
207
|
+
texts.push(btn.label);
|
|
208
|
+
if (btn.message && btn.message !== btn.label)
|
|
209
|
+
texts.push(btn.message);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return texts;
|
|
213
|
+
}
|
|
214
|
+
// ---- loadIntoTree -------------------------------------------------------
|
|
215
|
+
async loadIntoTree(filePathOrBuffer) {
|
|
216
|
+
const { getFileSize, pathExists } = this.options.fileAdapter;
|
|
217
|
+
const filename = typeof filePathOrBuffer === 'string' ? getBasename(filePathOrBuffer) : 'upload.gtbz';
|
|
218
|
+
let zip;
|
|
219
|
+
let pageDataValue;
|
|
220
|
+
let pageOrderValue;
|
|
221
|
+
let bookInfoValue;
|
|
222
|
+
try {
|
|
223
|
+
zip = await this.openZip(filePathOrBuffer);
|
|
224
|
+
pageDataValue = await this.readPlistFromZip(zip, 'PageData.plist');
|
|
225
|
+
pageOrderValue = await this.readPlistFromZip(zip, 'PageOrder.plist', () => []);
|
|
226
|
+
bookInfoValue = await this.readPlistFromZip(zip, 'BookInfo.plist', () => ({}));
|
|
227
|
+
}
|
|
228
|
+
catch (err) {
|
|
229
|
+
if (err instanceof ValidationFailureError)
|
|
230
|
+
throw err;
|
|
231
|
+
const validation = buildValidationResultFromMessage({
|
|
232
|
+
filename,
|
|
233
|
+
filesize: typeof filePathOrBuffer === 'string'
|
|
234
|
+
? (await pathExists(filePathOrBuffer))
|
|
235
|
+
? await getFileSize(filePathOrBuffer)
|
|
236
|
+
: 0
|
|
237
|
+
: 0,
|
|
238
|
+
format: 'gotalknow',
|
|
239
|
+
message: err?.message || 'Failed to open GoTalk NOW archive',
|
|
240
|
+
type: 'structure',
|
|
241
|
+
description: 'gtbz archive',
|
|
242
|
+
});
|
|
243
|
+
throw new ValidationFailureError('Failed to load GoTalk NOW file', validation, err);
|
|
244
|
+
}
|
|
245
|
+
const pageData = pageDataValue || {};
|
|
246
|
+
const pageOrderRaw = Array.isArray(pageOrderValue) ? pageOrderValue : [];
|
|
247
|
+
const tree = new AACTree();
|
|
248
|
+
tree.metadata.format = 'gotalknow';
|
|
249
|
+
// Format version
|
|
250
|
+
const bookInfo = bookInfoValue || {};
|
|
251
|
+
if (bookInfo.GoTalkBookFormatVersion !== undefined) {
|
|
252
|
+
const version = bookInfo.GoTalkBookFormatVersion;
|
|
253
|
+
tree.metadata.version =
|
|
254
|
+
typeof version === 'number' || typeof version === 'string' ? String(version) : '1';
|
|
255
|
+
}
|
|
256
|
+
// Preserve the ordered list of page ids for round-trip fidelity.
|
|
257
|
+
const orderedPageIds = pageOrderRaw.map((id) => String(id));
|
|
258
|
+
tree.metadata.pageOrder = orderedPageIds;
|
|
259
|
+
// The first page in PageOrder (if any) is treated as the home page.
|
|
260
|
+
if (orderedPageIds.length > 0) {
|
|
261
|
+
tree.metadata.defaultHomePageId = orderedPageIds[0];
|
|
262
|
+
}
|
|
263
|
+
// Track available image files so we can resolve ButtonImages locations.
|
|
264
|
+
const zipFiles = new Set(zip.listFiles());
|
|
265
|
+
// Build pages. Use the union of PageData keys and PageOrder ids.
|
|
266
|
+
const allPageIds = new Set([...Object.keys(pageData), ...orderedPageIds]);
|
|
267
|
+
// Deterministic ordering: ordered pages first (in order), then any extras sorted numerically.
|
|
268
|
+
const sortedPageIds = [
|
|
269
|
+
...orderedPageIds.filter((id) => allPageIds.has(id)),
|
|
270
|
+
...[...allPageIds]
|
|
271
|
+
.filter((id) => !orderedPageIds.includes(id))
|
|
272
|
+
.sort((a, b) => Number(a) - Number(b)),
|
|
273
|
+
];
|
|
274
|
+
if (sortedPageIds.length === 0) {
|
|
275
|
+
const validation = buildValidationResultFromMessage({
|
|
276
|
+
filename,
|
|
277
|
+
filesize: 0,
|
|
278
|
+
format: 'gotalknow',
|
|
279
|
+
message: 'No pages found in GoTalk NOW PageData.plist',
|
|
280
|
+
type: 'structure',
|
|
281
|
+
description: 'PageData.plist',
|
|
282
|
+
});
|
|
283
|
+
throw new ValidationFailureError('GoTalk NOW file has no pages', validation);
|
|
284
|
+
}
|
|
285
|
+
for (const pageId of sortedPageIds) {
|
|
286
|
+
const rawPage = pageData[pageId] || { Buttons: {} };
|
|
287
|
+
const pageTitle = typeof rawPage.PageTitle === 'string' && rawPage.PageTitle.trim().length > 0
|
|
288
|
+
? rawPage.PageTitle
|
|
289
|
+
: `Page ${pageId}`;
|
|
290
|
+
const page = new AACPage({
|
|
291
|
+
id: pageId,
|
|
292
|
+
name: pageTitle,
|
|
293
|
+
grid: [],
|
|
294
|
+
buttons: [],
|
|
295
|
+
parentId: null,
|
|
296
|
+
});
|
|
297
|
+
// Page-level style: background colour and title font.
|
|
298
|
+
const pageBg = decodeNsColor(rawPage.BackgroundColor);
|
|
299
|
+
const pageFont = decodeNsFont(rawPage.TitleFont);
|
|
300
|
+
if (pageBg || pageFont.fontFamily) {
|
|
301
|
+
page.style = {
|
|
302
|
+
...(page.style || {}),
|
|
303
|
+
...(pageBg ? { backgroundColor: pageBg } : {}),
|
|
304
|
+
...(pageFont.fontFamily ? { fontFamily: pageFont.fontFamily } : {}),
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
if (rawPage.Enabled === false) {
|
|
308
|
+
// Disabled pages are retained but flagged for fidelity.
|
|
309
|
+
page.enabled = false;
|
|
310
|
+
}
|
|
311
|
+
const buttonsById = rawPage.Buttons || {};
|
|
312
|
+
// Numeric sort of button indices ("0","1",…,"10",…)
|
|
313
|
+
const buttonIndices = Object.keys(buttonsById).sort((a, b) => Number(a) - Number(b));
|
|
314
|
+
const buttonCount = rawPage.ButtonCount ?? buttonIndices.length;
|
|
315
|
+
const { rows, cols } = gridDimensionsFromButtonCount(buttonCount);
|
|
316
|
+
const gridLayout = Array.from({ length: rows }, () => Array.from({ length: cols }, () => null));
|
|
317
|
+
buttonIndices.forEach((btnIndex, i) => {
|
|
318
|
+
const rawButton = buttonsById[btnIndex];
|
|
319
|
+
if (!rawButton)
|
|
320
|
+
return;
|
|
321
|
+
const { semanticAction, message, targetPageId } = buildSemanticAction(rawButton, pageId, btnIndex);
|
|
322
|
+
const button = new AACButton({
|
|
323
|
+
id: `${pageId}_btn_${btnIndex}`,
|
|
324
|
+
label: rawButton.ButtonText ?? '',
|
|
325
|
+
message,
|
|
326
|
+
targetPageId,
|
|
327
|
+
semanticAction,
|
|
328
|
+
});
|
|
329
|
+
// Decode style from NSKeyedArchiver blobs.
|
|
330
|
+
const backgroundColor = decodeNsColor(rawButton.BackgroundColor);
|
|
331
|
+
const borderColor = decodeNsColor(rawButton.BorderColor);
|
|
332
|
+
const fontColor = decodeNsColor(rawButton.TextColor);
|
|
333
|
+
const font = decodeNsFont(rawButton.TextFont);
|
|
334
|
+
if (backgroundColor || borderColor || fontColor || font.fontFamily) {
|
|
335
|
+
button.style = {
|
|
336
|
+
...(button.style || {}),
|
|
337
|
+
...(backgroundColor ? { backgroundColor } : {}),
|
|
338
|
+
...(borderColor ? { borderColor } : {}),
|
|
339
|
+
...(fontColor ? { fontColor } : {}),
|
|
340
|
+
...(font.fontFamily ? { fontFamily: font.fontFamily } : {}),
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
// Resolve image metadata.
|
|
344
|
+
const images = rawButton.ButtonImages;
|
|
345
|
+
if (Array.isArray(images) && images.length > 0) {
|
|
346
|
+
const first = images[0];
|
|
347
|
+
if (first?.Location) {
|
|
348
|
+
button.image = first.Location;
|
|
349
|
+
if (zipFiles.has(first.Location)) {
|
|
350
|
+
button.resolvedImageEntry = first.Location;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (first?.SourceLibrary)
|
|
354
|
+
button.symbolLibrary = first.SourceLibrary;
|
|
355
|
+
if (first?.SourceImageName)
|
|
356
|
+
button.symbolPath = first.SourceImageName;
|
|
357
|
+
}
|
|
358
|
+
// Visibility: GoTalk buttons can be disabled via either field.
|
|
359
|
+
if (rawButton.Enabled === false || rawButton.Disabled === true) {
|
|
360
|
+
button.visibility = 'Disabled';
|
|
361
|
+
}
|
|
362
|
+
// Auditory cue (played when scanning/highlighting).
|
|
363
|
+
if (rawButton.CueType && rawButton.CueType !== 'None') {
|
|
364
|
+
const cueText = rawButton.CueData?.TTSText;
|
|
365
|
+
if (cueText && cueText.length > 0) {
|
|
366
|
+
button.audioDescription = cueText;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// Recorded audio reference.
|
|
370
|
+
const btnType = rawButton.ButtonType;
|
|
371
|
+
if (btnType === 'Audio' || btnType === 'Recorded') {
|
|
372
|
+
button.audioRecording = {
|
|
373
|
+
identifier: `${pageId}-${btnIndex}.caf`,
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
// AfterAction: motor-planning "speak and return" behaviour.
|
|
377
|
+
// AfterAction is an enum (0=stay, 2=..., 50=jump-back) and
|
|
378
|
+
// AfterActionData has the same shape as ActionData (e.g. {JumpTo: 0}).
|
|
379
|
+
const afterActionJumpTo = rawButton.AfterActionData?.JumpTo;
|
|
380
|
+
// Preserve raw style + extended data opaquely for round-trip styling.
|
|
381
|
+
button.parameters = {
|
|
382
|
+
...(button.parameters || {}),
|
|
383
|
+
__gotalkNow: {
|
|
384
|
+
buttonType: rawButton.ButtonType,
|
|
385
|
+
textShadow: rawButton.TextShadow,
|
|
386
|
+
textCenter: rawButton.TextCenter,
|
|
387
|
+
cueType: rawButton.CueType,
|
|
388
|
+
afterAction: rawButton.AfterAction,
|
|
389
|
+
afterActionJumpTo: afterActionJumpTo !== undefined ? String(afterActionJumpTo) : undefined,
|
|
390
|
+
jumpBook: rawButton.ActionData?.JumpBook,
|
|
391
|
+
},
|
|
392
|
+
};
|
|
393
|
+
page._loadButton(button);
|
|
394
|
+
// Place into grid by index (row-major).
|
|
395
|
+
const row = Math.floor(i / cols);
|
|
396
|
+
const col = i % cols;
|
|
397
|
+
if (row < rows && col < cols) {
|
|
398
|
+
gridLayout[row][col] = button;
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
page.grid = gridLayout;
|
|
402
|
+
tree.addPage(page);
|
|
403
|
+
}
|
|
404
|
+
return tree;
|
|
405
|
+
}
|
|
406
|
+
// ---- processTexts (asset-preserving round-trip) -------------------------
|
|
407
|
+
async processTexts(filePathOrBuffer, translations, outputPath) {
|
|
408
|
+
const { writeBinaryToPath } = this.options.fileAdapter;
|
|
409
|
+
const originalZip = await this.openZip(filePathOrBuffer);
|
|
410
|
+
// Parse PageData and apply translations in place.
|
|
411
|
+
const pageDataBytes = await originalZip.readFile('PageData.plist');
|
|
412
|
+
const pageDataText = typeof Buffer !== 'undefined' && Buffer.isBuffer(pageDataBytes)
|
|
413
|
+
? pageDataBytes.toString('utf8')
|
|
414
|
+
: new TextDecoder().decode(pageDataBytes);
|
|
415
|
+
const pageData = plist.parse(pageDataText);
|
|
416
|
+
for (const pageId of Object.keys(pageData)) {
|
|
417
|
+
const rawPage = pageData[pageId];
|
|
418
|
+
if (!rawPage || !rawPage.Buttons)
|
|
419
|
+
continue;
|
|
420
|
+
for (const btnIndex of Object.keys(rawPage.Buttons)) {
|
|
421
|
+
const rawButton = rawPage.Buttons[btnIndex];
|
|
422
|
+
if (!rawButton)
|
|
423
|
+
continue;
|
|
424
|
+
// Translate the visible label.
|
|
425
|
+
if (rawButton.ButtonText && translations.has(rawButton.ButtonText)) {
|
|
426
|
+
rawButton.ButtonText = translations.get(rawButton.ButtonText);
|
|
427
|
+
}
|
|
428
|
+
// Translate the spoken text (only when explicitly set).
|
|
429
|
+
const actionData = rawButton.ActionData;
|
|
430
|
+
if (actionData && actionData.TTSText && translations.has(actionData.TTSText)) {
|
|
431
|
+
actionData.TTSText = translations.get(actionData.TTSText);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const rebuiltPageData = plist.build(pageData);
|
|
436
|
+
// Repack: copy every original entry verbatim, replacing only PageData.plist.
|
|
437
|
+
const outputZip = await this.options.zipAdapter(undefined, this.options.fileAdapter);
|
|
438
|
+
const files = [];
|
|
439
|
+
for (const name of originalZip.listFiles()) {
|
|
440
|
+
if (name === 'PageData.plist')
|
|
441
|
+
continue;
|
|
442
|
+
files.push({ name, data: await originalZip.readFile(name) });
|
|
443
|
+
}
|
|
444
|
+
files.push({ name: 'PageData.plist', data: rebuiltPageData });
|
|
445
|
+
const outputBuffer = await outputZip.writeFiles(files);
|
|
446
|
+
await writeBinaryToPath(outputPath, outputBuffer);
|
|
447
|
+
return outputBuffer;
|
|
448
|
+
}
|
|
449
|
+
// ---- saveFromTree (build from tree) -------------------------------------
|
|
450
|
+
async saveFromTree(tree, outputPath) {
|
|
451
|
+
const { writeBinaryToPath } = this.options.fileAdapter;
|
|
452
|
+
const pageData = {};
|
|
453
|
+
const pageOrder = [];
|
|
454
|
+
const imageFiles = [];
|
|
455
|
+
const pageIds = Object.keys(tree.pages);
|
|
456
|
+
pageIds.forEach((pageId, pageIdx) => {
|
|
457
|
+
const page = tree.pages[pageId];
|
|
458
|
+
const numericId = Number(pageId);
|
|
459
|
+
if (!Number.isNaN(numericId))
|
|
460
|
+
pageOrder.push(numericId);
|
|
461
|
+
const buttons = {};
|
|
462
|
+
const buttonList = page.buttons;
|
|
463
|
+
buttonList.forEach((btn, i) => {
|
|
464
|
+
const btnIndex = String(i);
|
|
465
|
+
const buttonType = btn.parameters?.__gotalkNow?.buttonType ||
|
|
466
|
+
(btn.targetPageId ? 'Jump' : 'TTS');
|
|
467
|
+
const actionData = {};
|
|
468
|
+
if (buttonType === 'Jump' && btn.targetPageId) {
|
|
469
|
+
actionData.JumpTo = Number(btn.targetPageId);
|
|
470
|
+
actionData.TTSText = '';
|
|
471
|
+
}
|
|
472
|
+
else if (buttonType === 'TTS') {
|
|
473
|
+
// Only store TTSText when it differs from the label (matches GoTalk's convention).
|
|
474
|
+
actionData.TTSText = btn.message && btn.message !== btn.label ? btn.message : '';
|
|
475
|
+
}
|
|
476
|
+
else {
|
|
477
|
+
actionData.TTSText = btn.message || '';
|
|
478
|
+
}
|
|
479
|
+
const rawButton = {
|
|
480
|
+
ActionData: actionData,
|
|
481
|
+
ButtonType: buttonType,
|
|
482
|
+
ButtonText: btn.label || '',
|
|
483
|
+
};
|
|
484
|
+
if (btn.resolvedImageEntry || btn.image) {
|
|
485
|
+
rawButton.ButtonImages = [
|
|
486
|
+
{
|
|
487
|
+
Location: btn.resolvedImageEntry || btn.image,
|
|
488
|
+
Type: 'Saved',
|
|
489
|
+
},
|
|
490
|
+
];
|
|
491
|
+
}
|
|
492
|
+
buttons[btnIndex] = rawButton;
|
|
493
|
+
});
|
|
494
|
+
pageData[pageId] = {
|
|
495
|
+
ButtonCount: buttonList.length,
|
|
496
|
+
Buttons: buttons,
|
|
497
|
+
};
|
|
498
|
+
// Collect referenced images so we can include them in the archive.
|
|
499
|
+
buttonList.forEach((btn) => {
|
|
500
|
+
if (btn.resolvedImageEntry) {
|
|
501
|
+
// Image bytes are not available from the tree alone; skip if absent.
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
void pageIdx;
|
|
505
|
+
});
|
|
506
|
+
const bookInfo = {
|
|
507
|
+
GoTalkBookFormatVersion: Number(tree.metadata.version) || 1,
|
|
508
|
+
};
|
|
509
|
+
const zip = await this.options.zipAdapter(undefined, this.options.fileAdapter);
|
|
510
|
+
const files = [
|
|
511
|
+
{ name: 'BookInfo.plist', data: plist.build(bookInfo) },
|
|
512
|
+
{ name: 'PageData.plist', data: plist.build(pageData) },
|
|
513
|
+
{
|
|
514
|
+
name: 'PageOrder.plist',
|
|
515
|
+
data: plist.build(pageOrder),
|
|
516
|
+
},
|
|
517
|
+
{ name: 'PageHistory.plist', data: plist.build([]) },
|
|
518
|
+
...imageFiles,
|
|
519
|
+
];
|
|
520
|
+
const outputBuffer = await zip.writeFiles(files);
|
|
521
|
+
await writeBinaryToPath(outputPath, outputBuffer);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Extract strings with metadata for aac-tools-platform compatibility.
|
|
525
|
+
* Uses the generic implementation from BaseProcessor.
|
|
526
|
+
*/
|
|
527
|
+
extractStringsWithMetadata(filePath) {
|
|
528
|
+
return this.extractStringsWithMetadataGeneric(filePath);
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Generate translated download for aac-tools-platform compatibility.
|
|
532
|
+
* Uses the generic implementation from BaseProcessor.
|
|
533
|
+
*/
|
|
534
|
+
generateTranslatedDownload(filePath, translatedStrings, sourceStrings) {
|
|
535
|
+
return this.generateTranslatedDownloadGeneric(filePath, translatedStrings, sourceStrings);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
export { GotalkNowProcessor };
|
|
@@ -2960,8 +2960,8 @@ class GridsetProcessor extends BaseProcessor {
|
|
|
2960
2960
|
cellContent.CaptionAndImage = captionAndImage;
|
|
2961
2961
|
}
|
|
2962
2962
|
const cellData = {
|
|
2963
|
-
'@_X': position.x
|
|
2964
|
-
'@_Y': position.y + yOffset
|
|
2963
|
+
'@_X': position.x, // Grid 3 coordinates are 0-based (matching loader)
|
|
2964
|
+
'@_Y': position.y + yOffset,
|
|
2965
2965
|
'@_ColumnSpan': position.columnSpan,
|
|
2966
2966
|
'@_RowSpan': position.rowSpan,
|
|
2967
2967
|
Content: cellContent,
|
|
@@ -144,14 +144,21 @@ class ObfProcessor extends BaseProcessor {
|
|
|
144
144
|
const pageId = boardData?.id ? String(boardData.id) : _boardPath?.split(/[/\\]/).pop() || '';
|
|
145
145
|
const images = boardData.images;
|
|
146
146
|
const buttons = await Promise.all(sourceButtons.map(async (btn) => {
|
|
147
|
+
// Resolve load_board.path to a board ID (strip .obf suffix if present).
|
|
148
|
+
// The OBF spec says load_board.path is a file path, but internally we
|
|
149
|
+
// store the board ID as targetPageId.
|
|
150
|
+
const rawPath = btn.load_board?.path;
|
|
151
|
+
const boardRef = rawPath
|
|
152
|
+
? rawPath.replace(/\.obf$/i, '').replace(/\.obz$/i, '')
|
|
153
|
+
: undefined;
|
|
147
154
|
const semanticAction = btn.load_board
|
|
148
155
|
? {
|
|
149
156
|
category: AACSemanticCategory.NAVIGATION,
|
|
150
157
|
intent: AACSemanticIntent.NAVIGATE_TO,
|
|
151
|
-
targetId:
|
|
158
|
+
targetId: boardRef,
|
|
152
159
|
fallback: {
|
|
153
160
|
type: 'NAVIGATE',
|
|
154
|
-
targetPageId:
|
|
161
|
+
targetPageId: boardRef,
|
|
155
162
|
},
|
|
156
163
|
}
|
|
157
164
|
: {
|
|
@@ -199,7 +206,7 @@ class ObfProcessor extends BaseProcessor {
|
|
|
199
206
|
resolvedImageEntry: resolvedImage,
|
|
200
207
|
parameters: Object.keys(buttonParameters).length > 0 ? buttonParameters : undefined,
|
|
201
208
|
semanticAction,
|
|
202
|
-
targetPageId:
|
|
209
|
+
targetPageId: boardRef,
|
|
203
210
|
semantic_id: btn.semantic_id, // Extract semantic_id if present
|
|
204
211
|
});
|
|
205
212
|
}));
|
|
@@ -618,7 +625,7 @@ class ObfProcessor extends BaseProcessor {
|
|
|
618
625
|
vocalization: button.message || button.label,
|
|
619
626
|
load_board: button.semanticAction?.intent === AACSemanticIntent.NAVIGATE_TO && button.targetPageId
|
|
620
627
|
? {
|
|
621
|
-
path: button.targetPageId,
|
|
628
|
+
path: this.getPageFilename(button.targetPageId, metadata),
|
|
622
629
|
}
|
|
623
630
|
: undefined,
|
|
624
631
|
background_color: button.style?.backgroundColor,
|
|
@@ -688,7 +695,9 @@ class ObfProcessor extends BaseProcessor {
|
|
|
688
695
|
});
|
|
689
696
|
const manifest = {
|
|
690
697
|
format: OBF_FORMAT_VERSION,
|
|
691
|
-
root: tree.metadata.defaultHomePageId
|
|
698
|
+
root: tree.metadata.defaultHomePageId
|
|
699
|
+
? this.getPageFilename(tree.metadata.defaultHomePageId, tree.metadata)
|
|
700
|
+
: undefined,
|
|
692
701
|
paths: {
|
|
693
702
|
boards: Object.fromEntries(Object.entries(tree.pages).map(([id, page]) => [
|
|
694
703
|
id,
|
|
@@ -755,7 +764,9 @@ class ObfProcessor extends BaseProcessor {
|
|
|
755
764
|
if (Object.keys(tree.pages).length > 0) {
|
|
756
765
|
const manifest = {
|
|
757
766
|
format: OBF_FORMAT_VERSION,
|
|
758
|
-
root: tree.metadata.defaultHomePageId
|
|
767
|
+
root: tree.metadata.defaultHomePageId
|
|
768
|
+
? this.getPageFilename(tree.metadata.defaultHomePageId, tree.metadata)
|
|
769
|
+
: undefined,
|
|
759
770
|
paths: {
|
|
760
771
|
boards: Object.fromEntries(Object.entries(tree.pages).map(([id, page]) => [
|
|
761
772
|
id,
|