@tmhs/screencast-mcp 0.39.0 → 0.45.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/README.md +95 -17
- package/assets/icon.png +0 -0
- package/assets/ui/player.html +47 -302
- package/assets/ui/player.js +402 -0
- package/dist/context.js +0 -5
- package/dist/index.js +17 -21
- package/dist/prompts.js +18 -8
- package/dist/resources.js +2 -14
- package/dist/server.js +49 -11
- package/dist/tools/addMarker.js +89 -0
- package/dist/tools/assembleHighlights.js +12 -7
- package/dist/tools/backdrop.js +89 -0
- package/dist/tools/bugReport.js +266 -0
- package/dist/tools/burnSubtitles.js +12 -6
- package/dist/tools/callout.js +32 -16
- package/dist/tools/chapters.js +156 -0
- package/dist/tools/clip.js +12 -9
- package/dist/tools/compress.js +10 -5
- package/dist/tools/concat.js +16 -15
- package/dist/tools/contactSheet.js +39 -33
- package/dist/tools/convert.js +12 -6
- package/dist/tools/crop.js +12 -7
- package/dist/tools/cutIdle.js +15 -9
- package/dist/tools/denoise.js +12 -6
- package/dist/tools/doctor.js +133 -0
- package/dist/tools/exportGif.js +14 -10
- package/dist/tools/exportPreset.js +11 -8
- package/dist/tools/extractAudio.js +10 -8
- package/dist/tools/findText.js +52 -98
- package/dist/tools/getMediaInfo.js +17 -3
- package/dist/tools/getSession.js +15 -3
- package/dist/tools/keystrokeOverlay.js +131 -0
- package/dist/tools/listAudioDevices.js +14 -7
- package/dist/tools/listSessions.js +13 -3
- package/dist/tools/listTargets.js +76 -0
- package/dist/tools/musicBed.js +14 -10
- package/dist/tools/normalizeAudio.js +12 -8
- package/dist/tools/overlay.js +16 -12
- package/dist/tools/pipeline.js +86 -25
- package/dist/tools/readEvents.js +21 -12
- package/dist/tools/redactRegion.js +19 -17
- package/dist/tools/reframe.js +9 -5
- package/dist/tools/reviewRecording.js +75 -22
- package/dist/tools/sampleFrames.js +33 -40
- package/dist/tools/saveReplay.js +90 -0
- package/dist/tools/scale.js +9 -3
- package/dist/tools/screenDiff.js +23 -17
- package/dist/tools/screenshot.js +21 -17
- package/dist/tools/speed.js +15 -4
- package/dist/tools/startRecording.js +96 -59
- package/dist/tools/stopRecording.js +35 -10
- package/dist/tools/titleCard.js +19 -11
- package/dist/tools/transcribe.js +13 -7
- package/dist/tools/trim.js +11 -10
- package/dist/tools/waitForChange.js +37 -22
- package/dist/tools/xfadeTransition.js +12 -8
- package/dist/tools/zoom.js +26 -29
- package/dist/utils/anim.js +1 -21
- package/dist/utils/animExport.js +5 -10
- package/dist/utils/audio.js +4 -33
- package/dist/utils/audioDevices.js +1 -48
- package/dist/utils/backdrop.js +151 -0
- package/dist/utils/bugReport.js +246 -0
- package/dist/utils/callout.js +1 -19
- package/dist/utils/calloutAssets.js +21 -11
- package/dist/utils/capabilities.js +95 -0
- package/dist/utils/captions.js +9 -26
- package/dist/utils/chapters.js +222 -0
- package/dist/utils/consent.js +29 -0
- package/dist/utils/diff.js +0 -46
- package/dist/utils/errors.js +5 -13
- package/dist/utils/events.js +21 -77
- package/dist/utils/expr.js +11 -0
- package/dist/utils/ffmpeg.js +119 -48
- package/dist/utils/fontCoverage.js +177 -0
- package/dist/utils/fonts.js +139 -23
- package/dist/utils/idle.js +12 -24
- package/dist/utils/images.js +0 -17
- package/dist/utils/jobs.js +42 -41
- package/dist/utils/keystrokes.js +170 -0
- package/dist/utils/lifecycle.js +11 -31
- package/dist/utils/logging.js +75 -0
- package/dist/utils/media.js +16 -124
- package/dist/utils/monitors.js +7 -43
- package/dist/utils/ocr.js +53 -71
- package/dist/utils/ocrScan.js +77 -0
- package/dist/utils/paths.js +68 -31
- package/dist/utils/pipeline.js +39 -25
- package/dist/utils/player.js +50 -27
- package/dist/utils/produce.js +6 -66
- package/dist/utils/replay.js +278 -0
- package/dist/utils/resolveTarget.js +0 -19
- package/dist/utils/retention.js +1 -15
- package/dist/utils/roots.js +42 -0
- package/dist/utils/sessions.js +195 -160
- package/dist/utils/sheet.js +0 -19
- package/dist/utils/still.js +0 -15
- package/dist/utils/targetList.js +63 -0
- package/dist/utils/targets.js +5 -70
- package/dist/utils/toolSpec.js +25 -0
- package/dist/utils/toolsets.js +122 -0
- package/dist/utils/validate.js +0 -22
- package/dist/utils/wgc.js +0 -46
- package/dist/utils/windows.js +5 -50
- package/dist/utils/zoom.js +55 -85
- package/dist/wgcRecorder.js +1 -30
- package/package.json +7 -3
- package/dist/context.d.ts +0 -9
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/prompts.d.ts +0 -10
- package/dist/prompts.d.ts.map +0 -1
- package/dist/prompts.js.map +0 -1
- package/dist/resources.d.ts +0 -3
- package/dist/resources.d.ts.map +0 -1
- package/dist/resources.js.map +0 -1
- package/dist/server.d.ts +0 -3
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js.map +0 -1
- package/dist/tools/assembleHighlights.d.ts +0 -3
- package/dist/tools/assembleHighlights.d.ts.map +0 -1
- package/dist/tools/assembleHighlights.js.map +0 -1
- package/dist/tools/burnSubtitles.d.ts +0 -3
- package/dist/tools/burnSubtitles.d.ts.map +0 -1
- package/dist/tools/burnSubtitles.js.map +0 -1
- package/dist/tools/callout.d.ts +0 -37
- package/dist/tools/callout.d.ts.map +0 -1
- package/dist/tools/callout.js.map +0 -1
- package/dist/tools/clip.d.ts +0 -3
- package/dist/tools/clip.d.ts.map +0 -1
- package/dist/tools/clip.js.map +0 -1
- package/dist/tools/compress.d.ts +0 -3
- package/dist/tools/compress.d.ts.map +0 -1
- package/dist/tools/compress.js.map +0 -1
- package/dist/tools/concat.d.ts +0 -3
- package/dist/tools/concat.d.ts.map +0 -1
- package/dist/tools/concat.js.map +0 -1
- package/dist/tools/contactSheet.d.ts +0 -3
- package/dist/tools/contactSheet.d.ts.map +0 -1
- package/dist/tools/contactSheet.js.map +0 -1
- package/dist/tools/convert.d.ts +0 -3
- package/dist/tools/convert.d.ts.map +0 -1
- package/dist/tools/convert.js.map +0 -1
- package/dist/tools/crop.d.ts +0 -3
- package/dist/tools/crop.d.ts.map +0 -1
- package/dist/tools/crop.js.map +0 -1
- package/dist/tools/cutIdle.d.ts +0 -3
- package/dist/tools/cutIdle.d.ts.map +0 -1
- package/dist/tools/cutIdle.js.map +0 -1
- package/dist/tools/denoise.d.ts +0 -3
- package/dist/tools/denoise.d.ts.map +0 -1
- package/dist/tools/denoise.js.map +0 -1
- package/dist/tools/exportGif.d.ts +0 -3
- package/dist/tools/exportGif.d.ts.map +0 -1
- package/dist/tools/exportGif.js.map +0 -1
- package/dist/tools/exportPreset.d.ts +0 -3
- package/dist/tools/exportPreset.d.ts.map +0 -1
- package/dist/tools/exportPreset.js.map +0 -1
- package/dist/tools/extractAudio.d.ts +0 -3
- package/dist/tools/extractAudio.d.ts.map +0 -1
- package/dist/tools/extractAudio.js.map +0 -1
- package/dist/tools/findText.d.ts +0 -6
- package/dist/tools/findText.d.ts.map +0 -1
- package/dist/tools/findText.js.map +0 -1
- package/dist/tools/getMediaInfo.d.ts +0 -3
- package/dist/tools/getMediaInfo.d.ts.map +0 -1
- package/dist/tools/getMediaInfo.js.map +0 -1
- package/dist/tools/getSession.d.ts +0 -3
- package/dist/tools/getSession.d.ts.map +0 -1
- package/dist/tools/getSession.js.map +0 -1
- package/dist/tools/listAudioDevices.d.ts +0 -3
- package/dist/tools/listAudioDevices.d.ts.map +0 -1
- package/dist/tools/listAudioDevices.js.map +0 -1
- package/dist/tools/listSessions.d.ts +0 -3
- package/dist/tools/listSessions.d.ts.map +0 -1
- package/dist/tools/listSessions.js.map +0 -1
- package/dist/tools/musicBed.d.ts +0 -3
- package/dist/tools/musicBed.d.ts.map +0 -1
- package/dist/tools/musicBed.js.map +0 -1
- package/dist/tools/normalizeAudio.d.ts +0 -3
- package/dist/tools/normalizeAudio.d.ts.map +0 -1
- package/dist/tools/normalizeAudio.js.map +0 -1
- package/dist/tools/overlay.d.ts +0 -3
- package/dist/tools/overlay.d.ts.map +0 -1
- package/dist/tools/overlay.js.map +0 -1
- package/dist/tools/pipeline.d.ts +0 -3
- package/dist/tools/pipeline.d.ts.map +0 -1
- package/dist/tools/pipeline.js.map +0 -1
- package/dist/tools/readEvents.d.ts +0 -6
- package/dist/tools/readEvents.d.ts.map +0 -1
- package/dist/tools/readEvents.js.map +0 -1
- package/dist/tools/redactRegion.d.ts +0 -13
- package/dist/tools/redactRegion.d.ts.map +0 -1
- package/dist/tools/redactRegion.js.map +0 -1
- package/dist/tools/reframe.d.ts +0 -3
- package/dist/tools/reframe.d.ts.map +0 -1
- package/dist/tools/reframe.js.map +0 -1
- package/dist/tools/reviewRecording.d.ts +0 -5
- package/dist/tools/reviewRecording.d.ts.map +0 -1
- package/dist/tools/reviewRecording.js.map +0 -1
- package/dist/tools/sampleFrames.d.ts +0 -3
- package/dist/tools/sampleFrames.d.ts.map +0 -1
- package/dist/tools/sampleFrames.js.map +0 -1
- package/dist/tools/scale.d.ts +0 -3
- package/dist/tools/scale.d.ts.map +0 -1
- package/dist/tools/scale.js.map +0 -1
- package/dist/tools/screenDiff.d.ts +0 -3
- package/dist/tools/screenDiff.d.ts.map +0 -1
- package/dist/tools/screenDiff.js.map +0 -1
- package/dist/tools/screenshot.d.ts +0 -3
- package/dist/tools/screenshot.d.ts.map +0 -1
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/speed.d.ts +0 -3
- package/dist/tools/speed.d.ts.map +0 -1
- package/dist/tools/speed.js.map +0 -1
- package/dist/tools/startRecording.d.ts +0 -3
- package/dist/tools/startRecording.d.ts.map +0 -1
- package/dist/tools/startRecording.js.map +0 -1
- package/dist/tools/stopRecording.d.ts +0 -3
- package/dist/tools/stopRecording.d.ts.map +0 -1
- package/dist/tools/stopRecording.js.map +0 -1
- package/dist/tools/titleCard.d.ts +0 -3
- package/dist/tools/titleCard.d.ts.map +0 -1
- package/dist/tools/titleCard.js.map +0 -1
- package/dist/tools/transcribe.d.ts +0 -3
- package/dist/tools/transcribe.d.ts.map +0 -1
- package/dist/tools/transcribe.js.map +0 -1
- package/dist/tools/trim.d.ts +0 -3
- package/dist/tools/trim.d.ts.map +0 -1
- package/dist/tools/trim.js.map +0 -1
- package/dist/tools/waitForChange.d.ts +0 -3
- package/dist/tools/waitForChange.d.ts.map +0 -1
- package/dist/tools/waitForChange.js.map +0 -1
- package/dist/tools/xfadeTransition.d.ts +0 -3
- package/dist/tools/xfadeTransition.d.ts.map +0 -1
- package/dist/tools/xfadeTransition.js.map +0 -1
- package/dist/tools/zoom.d.ts +0 -15
- package/dist/tools/zoom.d.ts.map +0 -1
- package/dist/tools/zoom.js.map +0 -1
- package/dist/utils/anim.d.ts +0 -33
- package/dist/utils/anim.d.ts.map +0 -1
- package/dist/utils/anim.js.map +0 -1
- package/dist/utils/animExport.d.ts +0 -27
- package/dist/utils/animExport.d.ts.map +0 -1
- package/dist/utils/animExport.js.map +0 -1
- package/dist/utils/audio.d.ts +0 -49
- package/dist/utils/audio.d.ts.map +0 -1
- package/dist/utils/audio.js.map +0 -1
- package/dist/utils/audioDevices.d.ts +0 -33
- package/dist/utils/audioDevices.d.ts.map +0 -1
- package/dist/utils/audioDevices.js.map +0 -1
- package/dist/utils/callout.d.ts +0 -72
- package/dist/utils/callout.d.ts.map +0 -1
- package/dist/utils/callout.js.map +0 -1
- package/dist/utils/calloutAssets.d.ts +0 -3
- package/dist/utils/calloutAssets.d.ts.map +0 -1
- package/dist/utils/calloutAssets.js.map +0 -1
- package/dist/utils/captions.d.ts +0 -26
- package/dist/utils/captions.d.ts.map +0 -1
- package/dist/utils/captions.js.map +0 -1
- package/dist/utils/diff.d.ts +0 -112
- package/dist/utils/diff.d.ts.map +0 -1
- package/dist/utils/diff.js.map +0 -1
- package/dist/utils/errors.d.ts +0 -33
- package/dist/utils/errors.d.ts.map +0 -1
- package/dist/utils/errors.js.map +0 -1
- package/dist/utils/events.d.ts +0 -143
- package/dist/utils/events.d.ts.map +0 -1
- package/dist/utils/events.js.map +0 -1
- package/dist/utils/ffmpeg.d.ts +0 -50
- package/dist/utils/ffmpeg.d.ts.map +0 -1
- package/dist/utils/ffmpeg.js.map +0 -1
- package/dist/utils/fonts.d.ts +0 -17
- package/dist/utils/fonts.d.ts.map +0 -1
- package/dist/utils/fonts.js.map +0 -1
- package/dist/utils/idle.d.ts +0 -23
- package/dist/utils/idle.d.ts.map +0 -1
- package/dist/utils/idle.js.map +0 -1
- package/dist/utils/images.d.ts +0 -21
- package/dist/utils/images.d.ts.map +0 -1
- package/dist/utils/images.js.map +0 -1
- package/dist/utils/jobs.d.ts +0 -44
- package/dist/utils/jobs.d.ts.map +0 -1
- package/dist/utils/jobs.js.map +0 -1
- package/dist/utils/lifecycle.d.ts +0 -29
- package/dist/utils/lifecycle.d.ts.map +0 -1
- package/dist/utils/lifecycle.js.map +0 -1
- package/dist/utils/media.d.ts +0 -272
- package/dist/utils/media.d.ts.map +0 -1
- package/dist/utils/media.js.map +0 -1
- package/dist/utils/monitors.d.ts +0 -47
- package/dist/utils/monitors.d.ts.map +0 -1
- package/dist/utils/monitors.js.map +0 -1
- package/dist/utils/ocr.d.ts +0 -50
- package/dist/utils/ocr.d.ts.map +0 -1
- package/dist/utils/ocr.js.map +0 -1
- package/dist/utils/paths.d.ts +0 -28
- package/dist/utils/paths.d.ts.map +0 -1
- package/dist/utils/paths.js.map +0 -1
- package/dist/utils/pipeline.d.ts +0 -69
- package/dist/utils/pipeline.d.ts.map +0 -1
- package/dist/utils/pipeline.js.map +0 -1
- package/dist/utils/player.d.ts +0 -30
- package/dist/utils/player.d.ts.map +0 -1
- package/dist/utils/player.js.map +0 -1
- package/dist/utils/produce.d.ts +0 -126
- package/dist/utils/produce.d.ts.map +0 -1
- package/dist/utils/produce.js.map +0 -1
- package/dist/utils/resolveTarget.d.ts +0 -32
- package/dist/utils/resolveTarget.d.ts.map +0 -1
- package/dist/utils/resolveTarget.js.map +0 -1
- package/dist/utils/retention.d.ts +0 -10
- package/dist/utils/retention.d.ts.map +0 -1
- package/dist/utils/retention.js.map +0 -1
- package/dist/utils/sessions.d.ts +0 -154
- package/dist/utils/sessions.d.ts.map +0 -1
- package/dist/utils/sessions.js.map +0 -1
- package/dist/utils/sheet.d.ts +0 -26
- package/dist/utils/sheet.d.ts.map +0 -1
- package/dist/utils/sheet.js.map +0 -1
- package/dist/utils/still.d.ts +0 -7
- package/dist/utils/still.d.ts.map +0 -1
- package/dist/utils/still.js.map +0 -1
- package/dist/utils/targets.d.ts +0 -113
- package/dist/utils/targets.d.ts.map +0 -1
- package/dist/utils/targets.js.map +0 -1
- package/dist/utils/validate.d.ts +0 -15
- package/dist/utils/validate.d.ts.map +0 -1
- package/dist/utils/validate.js.map +0 -1
- package/dist/utils/wgc.d.ts +0 -46
- package/dist/utils/wgc.d.ts.map +0 -1
- package/dist/utils/wgc.js.map +0 -1
- package/dist/utils/windows.d.ts +0 -94
- package/dist/utils/windows.d.ts.map +0 -1
- package/dist/utils/windows.js.map +0 -1
- package/dist/utils/zoom.d.ts +0 -95
- package/dist/utils/zoom.d.ts.map +0 -1
- package/dist/utils/zoom.js.map +0 -1
- package/dist/wgcRecorder.d.ts +0 -2
- package/dist/wgcRecorder.d.ts.map +0 -1
- package/dist/wgcRecorder.js.map +0 -1
package/dist/utils/events.js
CHANGED
|
@@ -1,43 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Input event log for recordings (#118): mouse clicks, scroll, and sampled
|
|
3
|
-
* cursor positions, plus key presses, on the video's clock and in video pixels.
|
|
4
|
-
*
|
|
5
|
-
* A small helper (C# low-level mouse/keyboard hooks, compiled by PowerShell)
|
|
6
|
-
* runs for the life of one recording and appends raw lines to
|
|
7
|
-
* `<recording>.events.raw`:
|
|
8
|
-
*
|
|
9
|
-
* <epoch ms>,<type>,<x>,<y>,<data>
|
|
10
|
-
*
|
|
11
|
-
* When ffmpeg exits the raw file is converted to `<recording>.events.jsonl`:
|
|
12
|
-
* times become seconds from the first video frame (gdigrab reports that frame's
|
|
13
|
-
* wall-clock time as `start:`), and coordinates are shifted by the capture
|
|
14
|
-
* origin so they are video pixels.
|
|
15
|
-
*
|
|
16
|
-
* Keys are never logged as text, and typed keys are not logged one by one
|
|
17
|
-
* (#210): typing becomes `typing` bursts (start and length on a 0.5 s grid),
|
|
18
|
-
* because per-key timing alone leaks password lengths and can partly
|
|
19
|
-
* reconstruct text. "shortcuts" also names chords made with Ctrl, Alt, or Win
|
|
20
|
-
* (Ctrl+S, Alt+Tab) as timed `key` events, which is what a keystroke overlay
|
|
21
|
-
* shows; plain typing, Shift+letter, and AltGr characters (including
|
|
22
|
-
* Ctrl+Alt+key on AltGr layouts) stay bursts. There is no mode that records
|
|
23
|
-
* typed text: that would be a system-wide keylogger.
|
|
24
|
-
*
|
|
25
|
-
* The helper also writes a heartbeat every second and notes when Windows saw
|
|
26
|
-
* input its hooks did not. Those become `gap` events, so a log that stopped
|
|
27
|
-
* early says so instead of looking complete.
|
|
28
|
-
*
|
|
29
|
-
* Everything but `startEventLogger` is pure so it is unit-tested anywhere.
|
|
30
|
-
*/
|
|
31
1
|
import { spawn } from "node:child_process";
|
|
32
2
|
import { existsSync, readFileSync, writeFileSync, unlinkSync, rmSync } from "node:fs";
|
|
33
3
|
import { ScreencastError } from "./errors.js";
|
|
34
4
|
export const DEFAULT_KEY_MODE = "masked";
|
|
35
5
|
export const EVENT_TYPES = ["down", "up", "move", "wheel", "key", "typing", "gap"];
|
|
36
|
-
/** Cursor positions are sampled at most this often while the mouse moves. */
|
|
37
6
|
export const MOVE_SAMPLE_MS = 33;
|
|
38
|
-
/** The first input's start time in epoch ms from ffmpeg's stderr banner, or
|
|
39
|
-
* null if it has not been printed. gdigrab stamps frames with wall-clock time,
|
|
40
|
-
* so `start: 1792936190.232363` is when the first frame was grabbed. */
|
|
41
7
|
export function parseCaptureStart(stderr) {
|
|
42
8
|
const input = stderr.indexOf("Input #0");
|
|
43
9
|
if (input < 0)
|
|
@@ -45,11 +11,6 @@ export function parseCaptureStart(stderr) {
|
|
|
45
11
|
const m = /start: (\d{9,}\.\d+)/.exec(stderr.slice(input));
|
|
46
12
|
return m ? Math.round(Number(m[1]) * 1000) : null;
|
|
47
13
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Convert raw helper lines to events. Lines that do not parse are skipped (the
|
|
50
|
-
* last one can be cut short when the helper is killed). Events before the first
|
|
51
|
-
* frame or after `endSec` are dropped.
|
|
52
|
-
*/
|
|
53
14
|
export function convertEvents(raw, opts) {
|
|
54
15
|
const out = [];
|
|
55
16
|
const beats = [];
|
|
@@ -85,7 +46,7 @@ export function convertEvents(raw, opts) {
|
|
|
85
46
|
missFrom = null;
|
|
86
47
|
continue;
|
|
87
48
|
}
|
|
88
|
-
if (type === "gap" || !EVENT_TYPES.includes(type))
|
|
49
|
+
if (type === undefined || type === "gap" || !EVENT_TYPES.includes(type))
|
|
89
50
|
continue;
|
|
90
51
|
if (t < 0 || (opts.endSec !== undefined && t > opts.endSec))
|
|
91
52
|
continue;
|
|
@@ -114,21 +75,20 @@ export function convertEvents(raw, opts) {
|
|
|
114
75
|
const lastSec = opts.endSec ?? Math.max(0, ...out.map((e) => e.t), ...beats);
|
|
115
76
|
if (missFrom)
|
|
116
77
|
gap(missFrom.t, missFrom.x, missFrom.y, lastSec, "input_not_seen");
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (
|
|
122
|
-
gap(
|
|
123
|
-
|
|
124
|
-
|
|
78
|
+
beats.sort((a, b) => a - b);
|
|
79
|
+
const last = beats.at(-1);
|
|
80
|
+
if (last !== undefined) {
|
|
81
|
+
beats.reduce((prev, beat) => {
|
|
82
|
+
if (beat - prev > HEARTBEAT_GAP_SEC)
|
|
83
|
+
gap(prev, 0, 0, beat, "helper_stalled");
|
|
84
|
+
return beat;
|
|
85
|
+
});
|
|
125
86
|
if (opts.endSec !== undefined && opts.endSec - last > HEARTBEAT_GAP_SEC)
|
|
126
87
|
gap(last, 0, 0, opts.endSec, "helper_stopped");
|
|
127
88
|
}
|
|
128
89
|
return out.sort((a, b) => a.t - b.t);
|
|
129
90
|
}
|
|
130
91
|
const r3 = (n) => Math.round(n * 1000) / 1000;
|
|
131
|
-
/** The gaps in a log, for a summary: where events may be missing. */
|
|
132
92
|
export function eventGaps(events) {
|
|
133
93
|
return events
|
|
134
94
|
.filter((e) => e.type === "gap")
|
|
@@ -146,7 +106,6 @@ export function parseEventsJsonl(text) {
|
|
|
146
106
|
out.push(JSON.parse(line));
|
|
147
107
|
}
|
|
148
108
|
catch {
|
|
149
|
-
// A torn line: skip it rather than fail the whole read.
|
|
150
109
|
}
|
|
151
110
|
}
|
|
152
111
|
return out;
|
|
@@ -157,7 +116,6 @@ export function selectEvents(events, q) {
|
|
|
157
116
|
(!q.types || q.types.includes(e.type)) &&
|
|
158
117
|
(!q.inFrameOnly || e.inFrame));
|
|
159
118
|
}
|
|
160
|
-
/** Counts per type, for a one-line summary. */
|
|
161
119
|
export function summarizeEvents(events) {
|
|
162
120
|
const counts = {};
|
|
163
121
|
for (const e of events) {
|
|
@@ -173,20 +131,10 @@ export function summarizeEvents(events) {
|
|
|
173
131
|
}
|
|
174
132
|
return counts;
|
|
175
133
|
}
|
|
176
|
-
/** Typing is logged as bursts on this grid (ms), never as single keys (#210). */
|
|
177
134
|
export const TYPING_GRAIN_MS = 500;
|
|
178
|
-
/** A pause this long (ms) ends a typing burst. */
|
|
179
135
|
export const TYPING_GAP_MS = 1000;
|
|
180
|
-
/** The helper reports missed input when Windows saw input this long (ms)
|
|
181
|
-
* after the hooks last ran: the hook was removed, or the input went to an
|
|
182
|
-
* elevated window or the secure desktop. */
|
|
183
136
|
export const MISSED_INPUT_MS = 2000;
|
|
184
|
-
/** Helper heartbeats arrive every second; a gap longer than this (s) means
|
|
185
|
-
* the helper stalled or stopped. */
|
|
186
137
|
export const HEARTBEAT_GAP_SEC = 3;
|
|
187
|
-
// The hook helper. __PATH__ is the raw output file (base64 UTF-8), __MODE__ is
|
|
188
|
-
// 1 for shortcuts, __PARENT__ the server pid: the helper exits on its own if
|
|
189
|
-
// the server dies, so a crash cannot leave a hook running.
|
|
190
138
|
const PS_HOOK = `
|
|
191
139
|
$ErrorActionPreference='Stop'
|
|
192
140
|
$ProgressPreference='SilentlyContinue'
|
|
@@ -286,7 +234,6 @@ export function buildHookScript(rawPath, mode, parentPid) {
|
|
|
286
234
|
.replace("__MODE__", mode === "shortcuts" ? "1" : "0")
|
|
287
235
|
.replace("__PARENT__", String(parentPid));
|
|
288
236
|
}
|
|
289
|
-
/** PowerShell reports errors on a redirected stderr as CLIXML; keep the text. */
|
|
290
237
|
export function cleanPsError(stderr) {
|
|
291
238
|
if (!stderr.startsWith("#< CLIXML"))
|
|
292
239
|
return stderr.trim();
|
|
@@ -299,14 +246,12 @@ export function cleanPsError(stderr) {
|
|
|
299
246
|
.replace(/&/g, "&")
|
|
300
247
|
.trim();
|
|
301
248
|
}
|
|
302
|
-
/** How long the helper gets to compile its hooks and report ready. */
|
|
303
249
|
export const LOGGER_READY_TIMEOUT_MS = 20_000;
|
|
304
|
-
/** Start the hook helper and resolve once its hooks are installed. */
|
|
305
250
|
export async function startEventLogger(rawPath, mode) {
|
|
306
251
|
if (process.platform !== "win32") {
|
|
307
252
|
throw new ScreencastError("The input event log is implemented for Windows only.");
|
|
308
253
|
}
|
|
309
|
-
rmSync(`${rawPath}.stop`, { force: true });
|
|
254
|
+
rmSync(`${rawPath}.stop`, { force: true });
|
|
310
255
|
const encoded = Buffer.from(buildHookScript(rawPath, mode, process.pid), "utf16le").toString("base64");
|
|
311
256
|
const child = spawn("powershell", ["-NoProfile", "-NonInteractive", "-EncodedCommand", encoded], {
|
|
312
257
|
stdio: ["ignore", "pipe", "pipe"],
|
|
@@ -340,12 +285,8 @@ export async function startEventLogger(rawPath, mode) {
|
|
|
340
285
|
stopFiles.set(child, `${rawPath}.stop`);
|
|
341
286
|
return child;
|
|
342
287
|
}
|
|
343
|
-
/** Stop file per helper: when it appears, the helper writes out its buffer
|
|
344
|
-
* and any open typing burst, then exits. */
|
|
345
288
|
const stopFiles = new WeakMap();
|
|
346
|
-
/** How long a helper gets to flush and exit before it is killed. */
|
|
347
289
|
export const LOGGER_STOP_GRACE_MS = 2_000;
|
|
348
|
-
/** Stop the helper and wait for it to exit, so the raw file is complete. */
|
|
349
290
|
export function stopEventLogger(child) {
|
|
350
291
|
if (child.exitCode !== null || child.signalCode !== null)
|
|
351
292
|
return Promise.resolve();
|
|
@@ -355,7 +296,7 @@ export function stopEventLogger(child) {
|
|
|
355
296
|
writeFileSync(stop, "");
|
|
356
297
|
}
|
|
357
298
|
catch {
|
|
358
|
-
stop = undefined;
|
|
299
|
+
stop = undefined;
|
|
359
300
|
}
|
|
360
301
|
}
|
|
361
302
|
return new Promise((resolve) => {
|
|
@@ -371,13 +312,20 @@ export function stopEventLogger(child) {
|
|
|
371
312
|
rmSync(stop, { force: true });
|
|
372
313
|
});
|
|
373
314
|
}
|
|
374
|
-
/** Sibling paths for a recording's raw and converted event logs. */
|
|
375
315
|
export function eventPaths(recording) {
|
|
376
316
|
const base = recording.replace(/\.[^./\\]+$/, "");
|
|
377
317
|
return { raw: `${base}.events.raw`, jsonl: `${base}.events.jsonl` };
|
|
378
318
|
}
|
|
379
|
-
|
|
380
|
-
|
|
319
|
+
export function prepareEventPaths(paths, overwrite = false) {
|
|
320
|
+
if (!overwrite) {
|
|
321
|
+
const existing = [paths.jsonl, paths.raw].find((p) => existsSync(p));
|
|
322
|
+
if (existing) {
|
|
323
|
+
throw new ScreencastError(`Event log already exists: ${existing}. Pass overwrite: true to replace it, or choose a different output path.`);
|
|
324
|
+
}
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
rmSync(paths.raw, { force: true });
|
|
328
|
+
}
|
|
381
329
|
export function writeEventsFile(paths, opts) {
|
|
382
330
|
const raw = existsSync(paths.raw) ? readFileSync(paths.raw, "utf8") : "";
|
|
383
331
|
const events = convertEvents(raw, opts);
|
|
@@ -386,12 +334,9 @@ export function writeEventsFile(paths, opts) {
|
|
|
386
334
|
unlinkSync(paths.raw);
|
|
387
335
|
}
|
|
388
336
|
catch {
|
|
389
|
-
// Left behind if locked; harmless and gitignored.
|
|
390
337
|
}
|
|
391
338
|
return events;
|
|
392
339
|
}
|
|
393
|
-
// Event logs finish converting after ffmpeg exits. stopSession waits on this
|
|
394
|
-
// so stop_recording reports the finished log instead of racing it.
|
|
395
340
|
const finalizers = new Map();
|
|
396
341
|
export function trackFinalizer(sessionId, done) {
|
|
397
342
|
finalizers.set(sessionId, done);
|
|
@@ -406,4 +351,3 @@ export async function whenFinalized(sessionId, timeoutMs = 10_000) {
|
|
|
406
351
|
return;
|
|
407
352
|
await Promise.race([done, new Promise((r) => setTimeout(r, timeoutMs))]);
|
|
408
353
|
}
|
|
409
|
-
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const SUM_GROUP = 50;
|
|
2
|
+
export function sumExpr(terms) {
|
|
3
|
+
if (terms.length === 0)
|
|
4
|
+
return "0";
|
|
5
|
+
if (terms.length <= SUM_GROUP)
|
|
6
|
+
return terms.join("+");
|
|
7
|
+
const groups = [];
|
|
8
|
+
for (let i = 0; i < terms.length; i += SUM_GROUP)
|
|
9
|
+
groups.push(`(${terms.slice(i, i + SUM_GROUP).join("+")})`);
|
|
10
|
+
return sumExpr(groups);
|
|
11
|
+
}
|
package/dist/utils/ffmpeg.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ffmpeg / ffprobe discovery and invocation.
|
|
3
|
-
*
|
|
4
|
-
* ffmpeg and ffprobe are EXTERNAL dependencies. They are detected on PATH (or
|
|
5
|
-
* via the FFMPEG_PATH / FFPROBE_PATH env overrides) and a missing binary
|
|
6
|
-
* produces a clear ScreencastError with an install hint, rather than a cryptic
|
|
7
|
-
* spawn failure deep inside a tool call.
|
|
8
|
-
*/
|
|
9
1
|
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
10
5
|
import { ScreencastError } from "./errors.js";
|
|
11
6
|
import { buildProbeArgs, parseMediaInfo } from "./media.js";
|
|
7
|
+
import { logFfmpegWarnings } from "./logging.js";
|
|
12
8
|
const INSTALL_HINT = "ffmpeg and ffprobe must be installed and on PATH (or set FFMPEG_PATH / " +
|
|
13
9
|
"FFPROBE_PATH). Windows: `winget install Gyan.FFmpeg` or `choco install " +
|
|
14
10
|
"ffmpeg`. macOS: `brew install ffmpeg`. Linux: `apt install ffmpeg`.";
|
|
@@ -16,7 +12,6 @@ function resolveBinary(envVar, fallback) {
|
|
|
16
12
|
const override = process.env[envVar];
|
|
17
13
|
return override && override.trim().length > 0 ? override : fallback;
|
|
18
14
|
}
|
|
19
|
-
/** Return true if `bin -version` runs and exits 0. */
|
|
20
15
|
export function binaryWorks(bin) {
|
|
21
16
|
try {
|
|
22
17
|
const res = spawnSync(bin, ["-version"], {
|
|
@@ -29,10 +24,6 @@ export function binaryWorks(bin) {
|
|
|
29
24
|
return false;
|
|
30
25
|
}
|
|
31
26
|
}
|
|
32
|
-
/**
|
|
33
|
-
* Detect ffmpeg + ffprobe. Returns the resolved binary names/paths, or throws a
|
|
34
|
-
* ScreencastError with an install hint naming whichever binary is missing.
|
|
35
|
-
*/
|
|
36
27
|
export function detectFfmpeg() {
|
|
37
28
|
const ffmpeg = resolveBinary("FFMPEG_PATH", "ffmpeg");
|
|
38
29
|
const ffprobe = resolveBinary("FFPROBE_PATH", "ffprobe");
|
|
@@ -47,7 +38,6 @@ export function detectFfmpeg() {
|
|
|
47
38
|
return { ffmpeg, ffprobe };
|
|
48
39
|
}
|
|
49
40
|
let cached = null;
|
|
50
|
-
/** Cached variant for per-call guards. Re-detects only after a failure. */
|
|
51
41
|
export function requireFfmpeg() {
|
|
52
42
|
if (cached)
|
|
53
43
|
return cached;
|
|
@@ -55,8 +45,6 @@ export function requireFfmpeg() {
|
|
|
55
45
|
return cached;
|
|
56
46
|
}
|
|
57
47
|
let wgc = null;
|
|
58
|
-
/** True when this ffmpeg has the gfxcapture source (Windows Graphics Capture,
|
|
59
|
-
* FFmpeg 8+ on Windows). Probed once. */
|
|
60
48
|
export function wgcAvailable() {
|
|
61
49
|
if (wgc !== null)
|
|
62
50
|
return wgc;
|
|
@@ -76,8 +64,6 @@ export function wgcAvailable() {
|
|
|
76
64
|
return wgc;
|
|
77
65
|
}
|
|
78
66
|
const filterCache = new Map();
|
|
79
|
-
/** True when this ffmpeg build has the named filter (e.g. whisper needs
|
|
80
|
-
* --enable-whisper, subtitles needs libass). Probed once per name. */
|
|
81
67
|
export function hasFilter(name) {
|
|
82
68
|
const cached = filterCache.get(name);
|
|
83
69
|
if (cached !== undefined)
|
|
@@ -97,8 +83,6 @@ export function hasFilter(name) {
|
|
|
97
83
|
filterCache.set(name, found);
|
|
98
84
|
return found;
|
|
99
85
|
}
|
|
100
|
-
/** Run a binary to completion, capturing stdout/stderr. Used for ffprobe and
|
|
101
|
-
* short ffmpeg edit jobs (not for long-running captures). */
|
|
102
86
|
export function runCapture(bin, args, timeoutMs = 5 * 60 * 1000, opts = {}) {
|
|
103
87
|
return new Promise((resolve, reject) => {
|
|
104
88
|
const child = spawn(bin, args, { windowsHide: true });
|
|
@@ -106,10 +90,6 @@ export function runCapture(bin, args, timeoutMs = 5 * 60 * 1000, opts = {}) {
|
|
|
106
90
|
child.stdin?.end();
|
|
107
91
|
let stdout = "";
|
|
108
92
|
let stderr = "";
|
|
109
|
-
// On a timeout or cancel, settle only once the child has actually exited:
|
|
110
|
-
// on Windows a file the dying process still holds open cannot be deleted
|
|
111
|
-
// yet, so a caller cleaning up its output would hit EPERM. A short grace
|
|
112
|
-
// timer still settles if 'close' never arrives.
|
|
113
93
|
let killError = null;
|
|
114
94
|
let graceTimer;
|
|
115
95
|
const kill = (error) => {
|
|
@@ -149,47 +129,139 @@ export function runCapture(bin, args, timeoutMs = 5 * 60 * 1000, opts = {}) {
|
|
|
149
129
|
});
|
|
150
130
|
});
|
|
151
131
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
132
|
+
const SCRIPTABLE_OPTS = new Set(["-vf", "-af", "-filter_complex", "-lavfi", "-filter:v", "-filter:a"]);
|
|
133
|
+
export const MAX_INLINE_FILTER = 8_000;
|
|
134
|
+
const LEGACY_SCRIPT_OPTS = {
|
|
135
|
+
"-vf": "-filter_script:v",
|
|
136
|
+
"-filter:v": "-filter_script:v",
|
|
137
|
+
"-af": "-filter_script:a",
|
|
138
|
+
"-filter:a": "-filter_script:a",
|
|
139
|
+
"-filter_complex": "-filter_complex_script",
|
|
140
|
+
"-lavfi": "-filter_complex_script",
|
|
141
|
+
};
|
|
142
|
+
let modernScripts = null;
|
|
143
|
+
function supportsOptionFiles() {
|
|
144
|
+
if (modernScripts !== null)
|
|
145
|
+
return modernScripts;
|
|
146
|
+
try {
|
|
147
|
+
const res = spawnSync(requireFfmpeg().ffmpeg, ["-hide_banner", "-version"], {
|
|
148
|
+
encoding: "utf8",
|
|
149
|
+
windowsHide: true,
|
|
150
|
+
timeout: 10_000,
|
|
151
|
+
});
|
|
152
|
+
const avutil = /^\s*libavutil\s+(\d+)\./m.exec(res.stdout ?? "");
|
|
153
|
+
modernScripts = avutil?.[1] === undefined || Number(avutil[1]) >= 59;
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
modernScripts = true;
|
|
157
|
+
}
|
|
158
|
+
return modernScripts;
|
|
159
|
+
}
|
|
160
|
+
export function scriptLongFilters(args, modern) {
|
|
161
|
+
let dir = null;
|
|
162
|
+
const out = [...args];
|
|
163
|
+
for (const [i, a] of args.entries()) {
|
|
164
|
+
const opt = args[i - 1];
|
|
165
|
+
if (opt === undefined || !SCRIPTABLE_OPTS.has(opt) || a.length <= MAX_INLINE_FILTER)
|
|
166
|
+
continue;
|
|
167
|
+
dir ??= mkdtempSync(join(tmpdir(), "screencast-filter-"));
|
|
168
|
+
const file = join(dir, `filter${i}.txt`);
|
|
169
|
+
writeFileSync(file, a);
|
|
170
|
+
modern ??= supportsOptionFiles();
|
|
171
|
+
out[i - 1] = modern ? `-/${opt.slice(1)}` : (LEGACY_SCRIPT_OPTS[opt] ?? opt);
|
|
172
|
+
out[i] = file;
|
|
173
|
+
}
|
|
174
|
+
const d = dir;
|
|
175
|
+
return { args: out, cleanup: () => d && rmSync(d, { recursive: true, force: true }) };
|
|
176
|
+
}
|
|
177
|
+
export async function runFfmpegBuffer(args, timeoutMs = 30_000, opts = {}) {
|
|
178
|
+
const chunks = [];
|
|
179
|
+
await streamFfmpeg(args, timeoutMs, (d) => chunks.push(d), opts);
|
|
180
|
+
return Buffer.concat(chunks);
|
|
181
|
+
}
|
|
182
|
+
export async function runFfmpegFrames(args, frameBytes, onFrame, timeoutMs = 30_000, opts = {}) {
|
|
183
|
+
if (!(frameBytes > 0))
|
|
184
|
+
throw new ScreencastError("frameBytes must be positive.");
|
|
185
|
+
let pending = Buffer.alloc(0);
|
|
186
|
+
let count = 0;
|
|
187
|
+
await streamFfmpeg(args, timeoutMs, (d) => {
|
|
188
|
+
pending = pending.length ? Buffer.concat([pending, d]) : d;
|
|
189
|
+
let o = 0;
|
|
190
|
+
for (; o + frameBytes <= pending.length; o += frameBytes) {
|
|
191
|
+
onFrame(Buffer.from(pending.subarray(o, o + frameBytes)));
|
|
192
|
+
count++;
|
|
193
|
+
}
|
|
194
|
+
pending = pending.subarray(o);
|
|
195
|
+
}, opts);
|
|
196
|
+
return count;
|
|
197
|
+
}
|
|
198
|
+
function streamFfmpeg(rawArgs, timeoutMs, onData, opts) {
|
|
155
199
|
const { ffmpeg } = requireFfmpeg();
|
|
200
|
+
const { args, cleanup } = scriptLongFilters(rawArgs);
|
|
156
201
|
return new Promise((resolve, reject) => {
|
|
157
202
|
const child = spawn(ffmpeg, args, { windowsHide: true });
|
|
158
|
-
const chunks = [];
|
|
159
203
|
let stderr = "";
|
|
160
|
-
let
|
|
161
|
-
|
|
162
|
-
|
|
204
|
+
let killError = null;
|
|
205
|
+
let graceTimer;
|
|
206
|
+
const kill = (error) => {
|
|
207
|
+
if (killError)
|
|
208
|
+
return;
|
|
209
|
+
killError = error;
|
|
163
210
|
child.kill("SIGKILL");
|
|
164
|
-
|
|
165
|
-
|
|
211
|
+
graceTimer = setTimeout(() => reject(error), 5_000);
|
|
212
|
+
};
|
|
213
|
+
const onAbort = () => kill(new ScreencastError("ffmpeg was cancelled."));
|
|
214
|
+
const timer = setTimeout(() => kill(new ScreencastError(`ffmpeg timed out after ${Math.round(timeoutMs / 1000)}s.`)), timeoutMs);
|
|
215
|
+
const settle = () => {
|
|
216
|
+
clearTimeout(timer);
|
|
217
|
+
clearTimeout(graceTimer);
|
|
218
|
+
opts.signal?.removeEventListener("abort", onAbort);
|
|
219
|
+
};
|
|
220
|
+
if (opts.signal?.aborted)
|
|
221
|
+
onAbort();
|
|
222
|
+
else
|
|
223
|
+
opts.signal?.addEventListener("abort", onAbort, { once: true });
|
|
224
|
+
child.stdout?.on("data", (d) => {
|
|
225
|
+
if (killError)
|
|
226
|
+
return;
|
|
227
|
+
try {
|
|
228
|
+
onData(d);
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
kill(error);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
166
234
|
child.stderr?.on("data", (d) => (stderr += d.toString()));
|
|
167
235
|
child.on("error", (err) => {
|
|
168
|
-
|
|
236
|
+
settle();
|
|
169
237
|
reject(err);
|
|
170
238
|
});
|
|
171
239
|
child.on("close", (code) => {
|
|
172
|
-
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
else if (
|
|
176
|
-
|
|
240
|
+
settle();
|
|
241
|
+
if (killError)
|
|
242
|
+
reject(killError);
|
|
243
|
+
else if (code === 0)
|
|
244
|
+
resolve();
|
|
177
245
|
else
|
|
178
246
|
reject(new ScreencastError(`ffmpeg failed (exit ${code}):\n${stderr.trim().split("\n").slice(-6).join("\n")}`));
|
|
179
247
|
});
|
|
180
|
-
});
|
|
248
|
+
}).finally(cleanup);
|
|
181
249
|
}
|
|
182
|
-
/** Run an ffmpeg edit job and reject with the tail of stderr on non-zero exit. */
|
|
183
250
|
export async function runFfmpeg(args, timeoutMs, opts = {}) {
|
|
184
251
|
const { ffmpeg } = requireFfmpeg();
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
const
|
|
188
|
-
|
|
252
|
+
const scripted = scriptLongFilters(args);
|
|
253
|
+
try {
|
|
254
|
+
const res = await runCapture(ffmpeg, scripted.args, timeoutMs, opts);
|
|
255
|
+
if (res.code !== 0) {
|
|
256
|
+
const tail = res.stderr.trim().split("\n").slice(-6).join("\n");
|
|
257
|
+
throw new ScreencastError(`ffmpeg failed (exit ${res.code}):\n${tail}`);
|
|
258
|
+
}
|
|
259
|
+
logFfmpegWarnings(res.stderr);
|
|
260
|
+
}
|
|
261
|
+
finally {
|
|
262
|
+
scripted.cleanup();
|
|
189
263
|
}
|
|
190
264
|
}
|
|
191
|
-
/** Probe a media file with ffprobe and return its flattened MediaInfo. Shared by
|
|
192
|
-
* tools that need source dimensions or stream presence before building an edit. */
|
|
193
265
|
export async function probeMedia(input) {
|
|
194
266
|
const { ffprobe } = requireFfmpeg();
|
|
195
267
|
const res = await runCapture(ffprobe, buildProbeArgs(input), 30_000);
|
|
@@ -198,4 +270,3 @@ export async function probeMedia(input) {
|
|
|
198
270
|
}
|
|
199
271
|
return parseMediaInfo(JSON.parse(res.stdout));
|
|
200
272
|
}
|
|
201
|
-
//# sourceMappingURL=ffmpeg.js.map
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
export function bufferReader(buf) {
|
|
2
|
+
return (offset, length) => buf.subarray(offset, offset + length);
|
|
3
|
+
}
|
|
4
|
+
function need(b, n, what) {
|
|
5
|
+
if (b.length < n)
|
|
6
|
+
throw new Error(`Truncated font: ${what}.`);
|
|
7
|
+
return b;
|
|
8
|
+
}
|
|
9
|
+
function faceOffset(read, faceIndex) {
|
|
10
|
+
const head = need(read(0, 12), 12, "header");
|
|
11
|
+
if (head.toString("latin1", 0, 4) !== "ttcf") {
|
|
12
|
+
if (faceIndex !== 0)
|
|
13
|
+
throw new Error("Not a font collection; only face 0 exists.");
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
const count = head.readUInt32BE(8);
|
|
17
|
+
if (faceIndex < 0 || faceIndex >= count)
|
|
18
|
+
throw new Error(`Face ${faceIndex} is out of range (collection has ${count}).`);
|
|
19
|
+
return need(read(12 + faceIndex * 4, 4), 4, "collection offsets").readUInt32BE(0);
|
|
20
|
+
}
|
|
21
|
+
class RangeBuilder {
|
|
22
|
+
ranges = [];
|
|
23
|
+
add(first, last) {
|
|
24
|
+
const prev = this.ranges[this.ranges.length - 1];
|
|
25
|
+
if (prev && first <= prev[1] + 1)
|
|
26
|
+
prev[1] = Math.max(prev[1], last);
|
|
27
|
+
else
|
|
28
|
+
this.ranges.push([first, last]);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function parseFormat4(t) {
|
|
32
|
+
const segX2 = need(t, 14, "cmap format 4").readUInt16BE(6);
|
|
33
|
+
const ends = 14;
|
|
34
|
+
const starts = ends + segX2 + 2;
|
|
35
|
+
const deltas = starts + segX2;
|
|
36
|
+
const rangeOffsets = deltas + segX2;
|
|
37
|
+
need(t, rangeOffsets + segX2, "cmap format 4 segments");
|
|
38
|
+
const out = new RangeBuilder();
|
|
39
|
+
for (let s = 0; s < segX2 / 2; s++) {
|
|
40
|
+
const end = t.readUInt16BE(ends + s * 2);
|
|
41
|
+
const start = t.readUInt16BE(starts + s * 2);
|
|
42
|
+
const delta = t.readUInt16BE(deltas + s * 2);
|
|
43
|
+
const ro = t.readUInt16BE(rangeOffsets + s * 2);
|
|
44
|
+
if (start > end)
|
|
45
|
+
continue;
|
|
46
|
+
for (let c = start; c <= end && c !== 0xffff; c++) {
|
|
47
|
+
let glyph;
|
|
48
|
+
if (ro === 0)
|
|
49
|
+
glyph = (c + delta) & 0xffff;
|
|
50
|
+
else {
|
|
51
|
+
const at = rangeOffsets + s * 2 + ro + (c - start) * 2;
|
|
52
|
+
glyph = at + 2 <= t.length ? t.readUInt16BE(at) : 0;
|
|
53
|
+
if (glyph !== 0)
|
|
54
|
+
glyph = (glyph + delta) & 0xffff;
|
|
55
|
+
}
|
|
56
|
+
if (glyph !== 0)
|
|
57
|
+
out.add(c, c);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return out.ranges;
|
|
61
|
+
}
|
|
62
|
+
function parseGroups(t, format) {
|
|
63
|
+
const n = need(t, 16, `cmap format ${format}`).readUInt32BE(12);
|
|
64
|
+
need(t, 16 + n * 12, `cmap format ${format} groups`);
|
|
65
|
+
const groups = [];
|
|
66
|
+
for (let i = 0; i < n; i++) {
|
|
67
|
+
let first = t.readUInt32BE(16 + i * 12);
|
|
68
|
+
const last = Math.min(t.readUInt32BE(20 + i * 12), 0x10ffff);
|
|
69
|
+
const glyph = t.readUInt32BE(24 + i * 12);
|
|
70
|
+
if (glyph === 0) {
|
|
71
|
+
if (format === 13)
|
|
72
|
+
continue;
|
|
73
|
+
first++;
|
|
74
|
+
}
|
|
75
|
+
if (first <= last)
|
|
76
|
+
groups.push([first, last]);
|
|
77
|
+
}
|
|
78
|
+
groups.sort((a, b) => a[0] - b[0]);
|
|
79
|
+
const out = new RangeBuilder();
|
|
80
|
+
for (const [a, b] of groups)
|
|
81
|
+
out.add(a, b);
|
|
82
|
+
return out.ranges;
|
|
83
|
+
}
|
|
84
|
+
function subtableRank(platform, encoding) {
|
|
85
|
+
if (platform === 3 && encoding === 10)
|
|
86
|
+
return 0;
|
|
87
|
+
if (platform === 0 && (encoding === 4 || encoding === 6))
|
|
88
|
+
return 1;
|
|
89
|
+
if (platform === 3 && encoding === 1)
|
|
90
|
+
return 2;
|
|
91
|
+
if (platform === 0 && encoding <= 3)
|
|
92
|
+
return 3;
|
|
93
|
+
return -1;
|
|
94
|
+
}
|
|
95
|
+
export function parseCoverage(read, faceIndex = 0) {
|
|
96
|
+
const base = faceOffset(read, faceIndex);
|
|
97
|
+
const sfnt = need(read(base, 12), 12, "sfnt header");
|
|
98
|
+
const tag = sfnt.toString("latin1", 0, 4);
|
|
99
|
+
if (tag !== "\u0000\u0001\u0000\u0000" && tag !== "OTTO" && tag !== "true") {
|
|
100
|
+
throw new Error("Not a TrueType/OpenType font.");
|
|
101
|
+
}
|
|
102
|
+
const numTables = sfnt.readUInt16BE(4);
|
|
103
|
+
const dir = need(read(base + 12, numTables * 16), numTables * 16, "table directory");
|
|
104
|
+
let cmapAt = -1;
|
|
105
|
+
for (let i = 0; i < numTables; i++) {
|
|
106
|
+
if (dir.toString("latin1", i * 16, i * 16 + 4) === "cmap")
|
|
107
|
+
cmapAt = dir.readUInt32BE(i * 16 + 8);
|
|
108
|
+
}
|
|
109
|
+
if (cmapAt < 0)
|
|
110
|
+
throw new Error("Font has no cmap table.");
|
|
111
|
+
const cmapHead = need(read(cmapAt, 4), 4, "cmap header");
|
|
112
|
+
const n = cmapHead.readUInt16BE(2);
|
|
113
|
+
const recs = need(read(cmapAt + 4, n * 8), n * 8, "cmap records");
|
|
114
|
+
const candidates = [];
|
|
115
|
+
for (let i = 0; i < n; i++) {
|
|
116
|
+
const rank = subtableRank(recs.readUInt16BE(i * 8), recs.readUInt16BE(i * 8 + 2));
|
|
117
|
+
if (rank >= 0)
|
|
118
|
+
candidates.push({ rank, offset: cmapAt + recs.readUInt32BE(i * 8 + 4) });
|
|
119
|
+
}
|
|
120
|
+
candidates.sort((a, b) => a.rank - b.rank);
|
|
121
|
+
for (const { offset } of candidates) {
|
|
122
|
+
const fmtHead = read(offset, 8);
|
|
123
|
+
if (fmtHead.length < 8)
|
|
124
|
+
continue;
|
|
125
|
+
const format = fmtHead.readUInt16BE(0);
|
|
126
|
+
if (format === 4)
|
|
127
|
+
return parseFormat4(read(offset, fmtHead.readUInt16BE(2)));
|
|
128
|
+
if (format === 12 || format === 13)
|
|
129
|
+
return parseGroups(read(offset, fmtHead.readUInt32BE(4)), format);
|
|
130
|
+
}
|
|
131
|
+
throw new Error("Font has no Unicode cmap subtable this reader understands (formats 4, 12, 13).");
|
|
132
|
+
}
|
|
133
|
+
export function covers(cov, cp) {
|
|
134
|
+
let lo = 0;
|
|
135
|
+
let hi = cov.length - 1;
|
|
136
|
+
while (lo <= hi) {
|
|
137
|
+
const mid = (lo + hi) >> 1;
|
|
138
|
+
const [first, last] = cov[mid];
|
|
139
|
+
if (cp < first)
|
|
140
|
+
hi = mid - 1;
|
|
141
|
+
else if (cp > last)
|
|
142
|
+
lo = mid + 1;
|
|
143
|
+
else
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
export function needsGlyph(cp) {
|
|
149
|
+
if (cp < 0x20 || (cp >= 0x7f && cp < 0xa0))
|
|
150
|
+
return false;
|
|
151
|
+
if (cp === 0xad || cp === 0x34f || cp === 0x61c || cp === 0xfeff)
|
|
152
|
+
return false;
|
|
153
|
+
if (cp >= 0x200b && cp <= 0x200f)
|
|
154
|
+
return false;
|
|
155
|
+
if (cp >= 0x202a && cp <= 0x202e)
|
|
156
|
+
return false;
|
|
157
|
+
if (cp >= 0x2060 && cp <= 0x206f)
|
|
158
|
+
return false;
|
|
159
|
+
if (cp >= 0xfe00 && cp <= 0xfe0f)
|
|
160
|
+
return false;
|
|
161
|
+
if (cp >= 0xe0000 && cp <= 0xe0fff)
|
|
162
|
+
return false;
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
export function missingChars(text, cov) {
|
|
166
|
+
const seen = new Set();
|
|
167
|
+
const out = [];
|
|
168
|
+
for (const ch of text) {
|
|
169
|
+
const cp = ch.codePointAt(0);
|
|
170
|
+
if (seen.has(cp))
|
|
171
|
+
continue;
|
|
172
|
+
seen.add(cp);
|
|
173
|
+
if (needsGlyph(cp) && !covers(cov, cp))
|
|
174
|
+
out.push(ch);
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
}
|