diff --git a/changes/character-dictionary-known-names.md b/changes/character-dictionary-known-names.md new file mode 100644 index 00000000..8c6de281 --- /dev/null +++ b/changes/character-dictionary-known-names.md @@ -0,0 +1,4 @@ +type: changed +area: dictionary + +- Keep character dictionary lookup entries scoped to generated Japanese name aliases instead of surfacing raw romanized/English aliases as separate results, and refresh cached v15 snapshots so old English-name entries are regenerated. diff --git a/changes/character-dictionary-manual-search.md b/changes/character-dictionary-manual-search.md new file mode 100644 index 00000000..469cf1aa --- /dev/null +++ b/changes/character-dictionary-manual-search.md @@ -0,0 +1,4 @@ +type: changed +area: character-dictionary + +- **Character Dictionary:** Changed the in-app AniList selector to wait for an explicit title search. The search box is prefilled from the current filename guess, so you can edit it before choosing an override. diff --git a/changes/character-name-inline-images.md b/changes/character-name-inline-images.md new file mode 100644 index 00000000..3e41ee2d --- /dev/null +++ b/changes/character-name-inline-images.md @@ -0,0 +1,7 @@ +type: added +area: subtitles + +- Added optional inline AniList portraits for character-name subtitle matches, including automatic refresh of cached character dictionary snapshots that do not contain portrait data. +- Scoped manual AniList overrides by parent media directory, so separate season folders can keep separate character dictionary selections. +- Fixed large character dictionary imports by serving the merged ZIP through a local URL when supported, with a base64 fallback for older bundled Yomitan builds. +- Allowed subtitle overlay data image sources so inline character portraits render instead of showing a broken image icon. diff --git a/config.example.jsonc b/config.example.jsonc index 98e3e720..83da260f 100644 --- a/config.example.jsonc +++ b/config.example.jsonc @@ -384,6 +384,7 @@ "autoPauseVideoOnHover": true, // Automatically pause mpv playback while hovering subtitle text, then resume on leave. Values: true | false "autoPauseVideoOnYomitanPopup": true, // Automatically pause mpv playback while Yomitan popup is open, then resume when popup closes. Values: true | false "nameMatchEnabled": false, // Enable subtitle token coloring for matches from the SubMiner character dictionary. Values: true | false + "nameMatchImagesEnabled": false, // Show small character portraits beside subtitle tokens matched from the SubMiner character dictionary. Values: true | false "nameMatchColor": "#f5bde6", // Hex color used when a subtitle token matches an entry from the SubMiner character dictionary. "nPlusOneColor": "#c6a0f6", // Color used for the single N+1 target token subtitle highlight. "knownWordColor": "#a6da95", // Color used for known-word subtitle highlights. diff --git a/docs-site/character-dictionary.md b/docs-site/character-dictionary.md index b3a731a3..7cc4d491 100644 --- a/docs-site/character-dictionary.md +++ b/docs-site/character-dictionary.md @@ -91,21 +91,26 @@ Name matching runs inside Yomitan's scanning pipeline during subtitle tokenizati 2. Entries from "SubMiner Character Dictionary" are checked with exact primary-source matching — the token must match the entry's `originalText` with `isPrimary: true` and `matchType: 'exact'`. 3. Matched tokens are flagged `isNameMatch: true` and forwarded to the renderer. 4. If `subtitleStyle.nameMatchEnabled` is enabled, the renderer applies the name-match highlight color (default: `#f5bde6`). +5. If `subtitleStyle.nameMatchImagesEnabled` is enabled, the renderer also injects a small circular AniList portrait from the cached snapshot image data. + +Older snapshot schema versions are regenerated automatically. Current-version snapshots are normally reused, but when `subtitleStyle.nameMatchImagesEnabled` is enabled SubMiner also checks whether the cached snapshot contains usable character portrait data. If it does not, the snapshot is refreshed so the merged dictionary can include images. Name matches are visually distinct from [N+1 targeting, frequency highlighting, and JLPT tags](/subtitle-annotations) so you can tell at a glance whether a highlighted word is a character name or a vocabulary target. **Key settings:** -| Option | Default | Description | -| -------------------------------- | --------- | ---------------------------------- | -| `subtitleStyle.nameMatchEnabled` | `false` | Toggle character-name highlighting | -| `subtitleStyle.nameMatchColor` | `#f5bde6` | Highlight color for matched names | +| Option | Default | Description | +| -------------------------------------- | --------- | ----------------------------------------- | +| `subtitleStyle.nameMatchEnabled` | `false` | Toggle character-name highlighting | +| `subtitleStyle.nameMatchImagesEnabled` | `false` | Show small AniList portraits beside names | +| `subtitleStyle.nameMatchColor` | `#f5bde6` | Highlight color for matched names | ## Dictionary Entries Each character entry in the Yomitan dictionary includes structured content: -- **Name** — native (Japanese) and romanized forms +- **Name** — the matched Japanese name form +- **Known names** — generated non-honorific Japanese aliases for that character, excluding raw romanized/English aliases from lookup results - **Role badge** — color-coded by role: main (score 100), supporting (90), side (80), background (70) - **Portrait** — character image from AniList, embedded in the ZIP - **Description** — biography text from AniList (collapsible) @@ -169,10 +174,13 @@ This creates a standalone dictionary ZIP for the target media and saves it along ## Correcting AniList Matches -SubMiner uses `guessit` to infer the anime title from the active filename, then searches AniList. Some filenames can still resolve to the wrong title. For example, `Re - ZERO, Starting Life in Another World (2016)` can be misread as a different `Re...` series. +SubMiner uses `guessit` to infer the anime title from the active filename before searching AniList. Some filenames can still resolve to the wrong title. For example, `Re - ZERO, Starting Life in Another World (2016)` can be misread as a different `Re...` series. Use the in-app selector or CLI to pin the correct AniList media for the whole series: +- In-app: open the selector with `Ctrl/Cmd+Alt+A` or `--open-character-dictionary`, edit the prefilled title if needed, then search and choose the correct result. +- CLI: `--dictionary-candidates` still lists matches for the current filename guess. + ```bash # List candidate AniList matches for a file subminer dictionary --candidates "/path/to/episode.mkv" @@ -188,7 +196,7 @@ SubMiner.AppImage --dictionary-select --dictionary-anilist-id 21355 --dictionary subminer app --open-character-dictionary ``` -Manual selections are stored in `character-dictionaries/anilist-overrides.json` using a series key derived from the filename guess. Later episodes with the same series key use the selected AniList ID automatically. When the override replaces a previous wrong match, SubMiner removes that stale media ID from the merged dictionary's active set and rebuilds/imports the merged character dictionary. +Manual selections are stored in `character-dictionaries/anilist-overrides.json` using a series key derived from the episode's parent directory plus the filename guess. Later episodes in the same directory use the selected AniList ID automatically, while separate season directories can keep separate overrides and character dictionaries. When the override replaces a previous wrong match, SubMiner removes that stale media ID from the merged dictionary's active set and rebuilds/imports the merged character dictionary. ## File Structure @@ -207,7 +215,7 @@ character-dictionaries/ m170942-va67890.jpg # Voice actor portrait ``` -**Snapshot format** (v15): each snapshot contains the media ID, title, entry count, timestamp, an array of Yomitan term entries, and base64-encoded images. +**Snapshot format** (v16): each snapshot contains the media ID, title, entry count, timestamp, an array of Yomitan term entries, and base64-encoded images. **ZIP structure** follows the Yomitan dictionary format: @@ -231,6 +239,7 @@ merged.zip | `anilist.characterDictionary.collapsibleSections.characterInformation` | `false` | Start Character Information section expanded | | `anilist.characterDictionary.collapsibleSections.voicedBy` | `false` | Start Voiced By section expanded | | `subtitleStyle.nameMatchEnabled` | `false` | Toggle character-name highlighting in subtitles | +| `subtitleStyle.nameMatchImagesEnabled` | `false` | Show small AniList portraits beside matched names | | `subtitleStyle.nameMatchColor` | `#f5bde6` | Highlight color for character-name matches | ## Reference Implementation @@ -253,8 +262,9 @@ If you work with visual novels or want a standalone dictionary generator indepen ## Troubleshooting - **Names not highlighting:** Confirm `anilist.characterDictionary.enabled` is `true` and `subtitleStyle.nameMatchEnabled` is `true`. Check that the current media has an AniList entry — SubMiner needs a media ID to fetch characters. +- **Inline portraits missing:** Confirm `subtitleStyle.nameMatchImagesEnabled` is `true`. On the next character dictionary sync, SubMiner refreshes current-version snapshots that do not contain usable cached character portrait data. Portraits still require AniList to return an image and the image download to succeed. - **Sync seems stuck:** The auto-sync debounces for 800ms after media changes and throttles image downloads at 250ms per image. Large casts (50+ characters) take longer. Check the status bar for the current sync phase. -- **Wrong characters showing:** Open the in-app character dictionary selector (`--open-character-dictionary`) or run `--dictionary-candidates`, then save the correct media with `--dictionary-select --dictionary-anilist-id `. This replaces stale wrong-title entries for that series. If names are only from an older unrelated show, they'll rotate out once you watch enough new titles to push it past `maxLoaded`. +- **Wrong characters showing:** Open the in-app character dictionary selector (`--open-character-dictionary`), edit the search title, and select the right AniList entry. You can also run `--dictionary-candidates`, then save the correct media with `--dictionary-select --dictionary-anilist-id `. This replaces stale wrong-title entries for that series. If names are only from an older unrelated show, they'll rotate out once you watch enough new titles to push it past `maxLoaded`. - **Yomitan import fails:** SubMiner waits up to 7 seconds for Yomitan to be ready for mutations. If Yomitan is still loading dictionaries or performing another import, the operation may time out. Restarting the overlay typically resolves this. - **Portraits missing:** Images are downloaded from AniList CDN during snapshot generation. If the network was unavailable during the initial sync, delete the snapshot file from `character-dictionaries/snapshots/` and let it regenerate. diff --git a/docs-site/configuration.md b/docs-site/configuration.md index d5eb7643..a2756fc8 100644 --- a/docs-site/configuration.md +++ b/docs-site/configuration.md @@ -371,34 +371,35 @@ See `config.example.jsonc` for detailed configuration options. } ``` -| Option | Values | Description | -| ---------------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------- | -| `fontFamily` | string | CSS font-family value (default: `"Hiragino Sans, M PLUS 1, Source Han Sans JP, Noto Sans CJK JP"`) | -| `fontSize` | number (px) | Font size in pixels (default: `35`) | -| `fontColor` | string | Any CSS color value (default: `"#cad3f5"`) | -| `css` | object | CSS declarations applied to subtitles after normal style defaults; the settings window writes textbox edits here | -| `fontWeight` | string | CSS font-weight, e.g. `"bold"`, `"normal"`, `"600"` (default: `"600"`) | -| `fontStyle` | string | `"normal"` or `"italic"` (default: `"normal"`) | -| `backgroundColor` | string | Any CSS color, including `"transparent"` (default: `"transparent"`) | -| `enableJlpt` | boolean | Enable JLPT level underline styling (`false` by default) | -| `preserveLineBreaks` | boolean | Preserve line breaks in visible overlay subtitle rendering (`false` by default). Enable to mirror mpv line layout. | -| `autoPauseVideoOnHover` | boolean | Pause playback while mouse hovers subtitle text, then resume on leave (`true` by default). | -| `autoPauseVideoOnYomitanPopup` | boolean | Pause playback while the Yomitan popup is open, then resume when the popup closes (`true` by default). | -| `hoverTokenColor` | string | Hex color used for hovered subtitle token highlight in mpv (default: catppuccin mauve) | +| Option | Values | Description | +| ---------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------ | +| `fontFamily` | string | CSS font-family value (default: `"Hiragino Sans, M PLUS 1, Source Han Sans JP, Noto Sans CJK JP"`) | +| `fontSize` | number (px) | Font size in pixels (default: `35`) | +| `fontColor` | string | Any CSS color value (default: `"#cad3f5"`) | +| `css` | object | CSS declarations applied to subtitles after normal style defaults; the settings window writes textbox edits here | +| `fontWeight` | string | CSS font-weight, e.g. `"bold"`, `"normal"`, `"600"` (default: `"600"`) | +| `fontStyle` | string | `"normal"` or `"italic"` (default: `"normal"`) | +| `backgroundColor` | string | Any CSS color, including `"transparent"` (default: `"transparent"`) | +| `enableJlpt` | boolean | Enable JLPT level underline styling (`false` by default) | +| `preserveLineBreaks` | boolean | Preserve line breaks in visible overlay subtitle rendering (`false` by default). Enable to mirror mpv line layout. | +| `autoPauseVideoOnHover` | boolean | Pause playback while mouse hovers subtitle text, then resume on leave (`true` by default). | +| `autoPauseVideoOnYomitanPopup` | boolean | Pause playback while the Yomitan popup is open, then resume when the popup closes (`true` by default). | +| `hoverTokenColor` | string | Hex color used for hovered subtitle token highlight in mpv (default: catppuccin mauve) | | `hoverTokenBackgroundColor` | string | CSS color used for hovered subtitle token background highlight (default: `"transparent"`); `hoverBackground` is accepted as an alias | -| `nameMatchEnabled` | boolean | Enable subtitle token coloring for matches from the SubMiner character dictionary (`false` by default) | -| `nameMatchColor` | string | Hex color used for subtitle tokens matched from the SubMiner character dictionary (default: `#f5bde6`) | -| `knownWordColor` | string | Hex color used for known-word subtitle highlights (default: `#a6da95`) | -| `nPlusOneColor` | string | Hex color used for the single N+1 target subtitle highlight (default: `#c6a0f6`) | -| `frequencyDictionary.enabled` | boolean | Enable frequency highlighting from dictionary lookups (`false` by default) | -| `frequencyDictionary.sourcePath` | string | Path to a local frequency dictionary root. Leave empty or omit to use installed/default frequency-dictionary search paths. | -| `frequencyDictionary.topX` | number | Only color tokens whose frequency rank is `<= topX` (`1000` by default) | -| `frequencyDictionary.mode` | string | `"single"` or `"banded"` (`"single"` by default) | -| `frequencyDictionary.matchMode` | string | `"headword"` or `"surface"` (`"headword"` by default) | -| `frequencyDictionary.singleColor` | string | Color used for all highlighted tokens in single mode | -| `frequencyDictionary.bandedColors` | string[] | Array of five hex colors used for ranked bands in banded mode | -| `jlptColors` | object | JLPT level underline colors object (`N1`..`N5`) | -| `secondary` | object | Override any of the above for secondary subtitles (optional), including `secondary.css` declarations | +| `nameMatchEnabled` | boolean | Enable subtitle token coloring for matches from the SubMiner character dictionary (`false` by default) | +| `nameMatchImagesEnabled` | boolean | Show small cached AniList character portraits beside matched character-name tokens (`false` by default) | +| `nameMatchColor` | string | Hex color used for subtitle tokens matched from the SubMiner character dictionary (default: `#f5bde6`) | +| `knownWordColor` | string | Hex color used for known-word subtitle highlights (default: `#a6da95`) | +| `nPlusOneColor` | string | Hex color used for the single N+1 target subtitle highlight (default: `#c6a0f6`) | +| `frequencyDictionary.enabled` | boolean | Enable frequency highlighting from dictionary lookups (`false` by default) | +| `frequencyDictionary.sourcePath` | string | Path to a local frequency dictionary root. Leave empty or omit to use installed/default frequency-dictionary search paths. | +| `frequencyDictionary.topX` | number | Only color tokens whose frequency rank is `<= topX` (`1000` by default) | +| `frequencyDictionary.mode` | string | `"single"` or `"banded"` (`"single"` by default) | +| `frequencyDictionary.matchMode` | string | `"headword"` or `"surface"` (`"headword"` by default) | +| `frequencyDictionary.singleColor` | string | Color used for all highlighted tokens in single mode | +| `frequencyDictionary.bandedColors` | string[] | Array of five hex colors used for ranked bands in banded mode | +| `jlptColors` | object | JLPT level underline colors object (`N1`..`N5`) | +| `secondary` | object | Override any of the above for secondary subtitles (optional), including `secondary.css` declarations | The Settings window keeps subtitle color controls separate, then saves CSS textboxes to `subtitleStyle.css`, `subtitleStyle.secondary.css`, and `subtitleSidebar.css`. The generated example @@ -420,6 +421,7 @@ In `single` mode all highlights use `singleColor`; in `banded` mode tokens map t Character-name highlighting is separate from N+1 and frequency highlighting: - `nameMatchEnabled` controls whether SubMiner includes character-dictionary name matches in subtitle token metadata and renderer styling. +- `nameMatchImagesEnabled` adds small circular portraits beside matched names using the AniList images already cached with character dictionary snapshots. - `nameMatchColor` sets the highlight color for those matched character names. - Matches come from the bundled SubMiner character dictionary, including AniList-synced merged dictionaries when enabled. @@ -865,15 +867,15 @@ This is the single, shared connection to an OpenAI-compatible LLM endpoint. Conf } ``` -| Option | Values | Description | -| ------------------ | -------------------- | ---------------------------------------------------------------------------------- | -| `enabled` | `true`, `false` | Enable shared AI provider features (default: `false`) | -| `apiKey` | string | Static API key for the shared provider | -| `apiKeyCommand` | string | Shell command used to resolve the API key (preferred over a plaintext `apiKey`) | +| Option | Values | Description | +| ------------------ | -------------------- | ------------------------------------------------------------------------------------ | +| `enabled` | `true`, `false` | Enable shared AI provider features (default: `false`) | +| `apiKey` | string | Static API key for the shared provider | +| `apiKeyCommand` | string | Shell command used to resolve the API key (preferred over a plaintext `apiKey`) | | `model` | string | Default model identifier requested from the provider (default: `openai/gpt-4o-mini`) | -| `baseUrl` | string (URL) | OpenAI-compatible base URL (default: `https://openrouter.ai/api`) | -| `systemPrompt` | string | Default system prompt sent with requests (default: a translation-engine prompt) | -| `requestTimeoutMs` | integer milliseconds | Shared request timeout (default: `15000`) | +| `baseUrl` | string (URL) | OpenAI-compatible base URL (default: `https://openrouter.ai/api`) | +| `systemPrompt` | string | Default system prompt sent with requests (default: a translation-engine prompt) | +| `requestTimeoutMs` | integer milliseconds | Shared request timeout (default: `15000`) | SubMiner uses the shared provider for: @@ -1125,12 +1127,12 @@ Sync the active subtitle track from the overlay picker using `alass` or `ffsubsy } ``` -| Option | Values | Description | -| ---------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------- | -| `alass_path` | string path | Path to `alass` executable. Empty or `null` resolves from `PATH`. `alass` must be installed separately. | -| `ffsubsync_path` | string path | Path to `ffsubsync` executable. Empty or `null` resolves from `PATH`. `ffsubsync` must be installed separately. | -| `ffmpeg_path` | string path | Path to `ffmpeg` (used for internal subtitle extraction). Empty or `null` falls back to `/usr/bin/ffmpeg`. | -| `replace` | `true`, `false` | When `true` (default), overwrite the active subtitle file on successful sync. When `false`, write `_retimed.`. | +| Option | Values | Description | +| ---------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------- | +| `alass_path` | string path | Path to `alass` executable. Empty or `null` resolves from `PATH`. `alass` must be installed separately. | +| `ffsubsync_path` | string path | Path to `ffsubsync` executable. Empty or `null` resolves from `PATH`. `ffsubsync` must be installed separately. | +| `ffmpeg_path` | string path | Path to `ffmpeg` (used for internal subtitle extraction). Empty or `null` falls back to `/usr/bin/ffmpeg`. | +| `replace` | `true`, `false` | When `true` (default), overwrite the active subtitle file on successful sync. When `false`, write `_retimed.`. | Default trigger is `Ctrl+Alt+S` via `shortcuts.triggerSubsync`. Customize it there, or set it to `null` to disable. diff --git a/docs-site/public/config.example.jsonc b/docs-site/public/config.example.jsonc index 98e3e720..83da260f 100644 --- a/docs-site/public/config.example.jsonc +++ b/docs-site/public/config.example.jsonc @@ -384,6 +384,7 @@ "autoPauseVideoOnHover": true, // Automatically pause mpv playback while hovering subtitle text, then resume on leave. Values: true | false "autoPauseVideoOnYomitanPopup": true, // Automatically pause mpv playback while Yomitan popup is open, then resume when popup closes. Values: true | false "nameMatchEnabled": false, // Enable subtitle token coloring for matches from the SubMiner character dictionary. Values: true | false + "nameMatchImagesEnabled": false, // Show small character portraits beside subtitle tokens matched from the SubMiner character dictionary. Values: true | false "nameMatchColor": "#f5bde6", // Hex color used when a subtitle token matches an entry from the SubMiner character dictionary. "nPlusOneColor": "#c6a0f6", // Color used for the single N+1 target token subtitle highlight. "knownWordColor": "#a6da95", // Color used for known-word subtitle highlights. diff --git a/docs-site/subtitle-annotations.md b/docs-site/subtitle-annotations.md index d8e9fe14..72fedf10 100644 --- a/docs-site/subtitle-annotations.md +++ b/docs-site/subtitle-annotations.md @@ -44,13 +44,15 @@ Character-name matches are built from the active merged SubMiner character dicti 1. Subtitles are tokenized, then candidate name tokens are matched against the character dictionary via Yomitan's scanning pipeline. 2. Matching tokens receive a dedicated style distinct from N+1 and frequency layers. 3. This layer can be independently toggled with `subtitleStyle.nameMatchEnabled`. +4. When `subtitleStyle.nameMatchImagesEnabled` is also enabled, SubMiner shows the cached AniList portrait beside matched names. **Key settings:** -| Option | Default | Description | -| -------------------------------- | --------- | ---------------------------------------- | -| `subtitleStyle.nameMatchEnabled` | `false` | Enable character-name token highlighting | -| `subtitleStyle.nameMatchColor` | `#f5bde6` | Color used for character-name matches | +| Option | Default | Description | +| -------------------------------------- | --------- | ------------------------------------------------ | +| `subtitleStyle.nameMatchEnabled` | `false` | Enable character-name token highlighting | +| `subtitleStyle.nameMatchImagesEnabled` | `false` | Show small AniList portraits next to name tokens | +| `subtitleStyle.nameMatchColor` | `#f5bde6` | Color used for character-name matches | For full details on dictionary generation, name variant expansion, auto-sync lifecycle, and configuration, see the dedicated [Character Dictionary](/character-dictionary) page. @@ -67,14 +69,14 @@ SubMiner looks up each token's `frequencyRank` from `term_meta_bank_*.json` file **Key settings:** -| Option | Default | Description | -| ------------------------------------------------ | ------------ | ---------------------------------------- | -| `subtitleStyle.frequencyDictionary.enabled` | `false` | Enable frequency highlighting | -| `subtitleStyle.frequencyDictionary.topX` | `1000` | Max frequency rank to highlight | -| `subtitleStyle.frequencyDictionary.mode` | `"single"` | `"single"` or `"banded"` | -| `subtitleStyle.frequencyDictionary.matchMode` | `"headword"` | `"headword"` or `"surface"` | -| `subtitleStyle.frequencyDictionary.singleColor` | `#f5a97f` | Color for single mode | -| `subtitleStyle.frequencyDictionary.bandedColors` | 5 colors[^1] | Array of five hex colors for banded mode | +| Option | Default | Description | +| ------------------------------------------------ | ------------ | ---------------------------------------------------------------- | +| `subtitleStyle.frequencyDictionary.enabled` | `false` | Enable frequency highlighting | +| `subtitleStyle.frequencyDictionary.topX` | `1000` | Max frequency rank to highlight | +| `subtitleStyle.frequencyDictionary.mode` | `"single"` | `"single"` or `"banded"` | +| `subtitleStyle.frequencyDictionary.matchMode` | `"headword"` | `"headword"` or `"surface"` | +| `subtitleStyle.frequencyDictionary.singleColor` | `#f5a97f` | Color for single mode | +| `subtitleStyle.frequencyDictionary.bandedColors` | 5 colors[^1] | Array of five hex colors for banded mode | | `subtitleStyle.frequencyDictionary.sourcePath` | `""` | Custom path to frequency dictionary root (empty = auto-discover) | [^1]: Default banded palette (most common → least common): `#ed8796`, `#f5a97f`, `#f9e2af`, `#8bd5ca`, `#8aadf4`. @@ -122,6 +124,7 @@ All annotation layers can be toggled at runtime via the mpv command menu without - `ankiConnect.knownWords.highlightEnabled` (`On` / `Off`) - `subtitleStyle.nameMatchEnabled` (`On` / `Off`) +- `subtitleStyle.nameMatchImagesEnabled` (`On` / `Off`) - `subtitleStyle.enableJlpt` (`On` / `Off`) - `subtitleStyle.frequencyDictionary.enabled` (`On` / `Off`) diff --git a/src/config/config.test.ts b/src/config/config.test.ts index 35fef738..68ff68c9 100644 --- a/src/config/config.test.ts +++ b/src/config/config.test.ts @@ -64,6 +64,7 @@ test('loads defaults when config is missing', () => { assert.equal(config.ankiConnect.media.audioPadding, 0); assert.equal(config.anilist.enabled, false); assert.equal(config.anilist.characterDictionary.enabled, false); + assert.equal(config.subtitleStyle.nameMatchImagesEnabled, false); assert.equal(config.anilist.characterDictionary.refreshTtlHours, 168); assert.equal(config.anilist.characterDictionary.maxLoaded, 3); assert.equal(config.anilist.characterDictionary.evictionPolicy, 'delete'); @@ -740,6 +741,44 @@ test('parses subtitleStyle.nameMatchEnabled and warns on invalid values', () => ); }); +test('parses subtitleStyle.nameMatchImagesEnabled and warns on invalid values', () => { + const validDir = makeTempDir(); + fs.writeFileSync( + path.join(validDir, 'config.jsonc'), + `{ + "subtitleStyle": { + "nameMatchImagesEnabled": true + } + }`, + 'utf-8', + ); + + const validService = new ConfigService(validDir); + assert.equal(validService.getConfig().subtitleStyle.nameMatchImagesEnabled, true); + + const invalidDir = makeTempDir(); + fs.writeFileSync( + path.join(invalidDir, 'config.jsonc'), + `{ + "subtitleStyle": { + "nameMatchImagesEnabled": "yes" + } + }`, + 'utf-8', + ); + + const invalidService = new ConfigService(invalidDir); + assert.equal( + invalidService.getConfig().subtitleStyle.nameMatchImagesEnabled, + DEFAULT_CONFIG.subtitleStyle.nameMatchImagesEnabled, + ); + assert.ok( + invalidService + .getWarnings() + .some((warning) => warning.path === 'subtitleStyle.nameMatchImagesEnabled'), + ); +}); + test('parses anilist.enabled and warns for invalid value', () => { const dir = makeTempDir(); fs.writeFileSync( diff --git a/src/config/definitions/defaults-subtitle.ts b/src/config/definitions/defaults-subtitle.ts index 94a487c6..b3392fbc 100644 --- a/src/config/definitions/defaults-subtitle.ts +++ b/src/config/definitions/defaults-subtitle.ts @@ -11,6 +11,7 @@ export const SUBTITLE_DEFAULT_CONFIG: Pick { ); }); +test('subtitleStyle nameMatchImagesEnabled accepts boolean and warns on invalid', () => { + const valid = createResolveContext({ + subtitleStyle: { + nameMatchImagesEnabled: true, + }, + }); + applySubtitleDomainConfig(valid.context); + assert.equal(valid.context.resolved.subtitleStyle.nameMatchImagesEnabled, true); + + const invalid = createResolveContext({ + subtitleStyle: { + nameMatchImagesEnabled: 'yes' as unknown as boolean, + }, + }); + applySubtitleDomainConfig(invalid.context); + assert.equal(invalid.context.resolved.subtitleStyle.nameMatchImagesEnabled, false); + assert.ok( + invalid.warnings.some( + (warning) => + warning.path === 'subtitleStyle.nameMatchImagesEnabled' && + warning.message === 'Expected boolean.', + ), + ); +}); + test('subtitleStyle frequencyDictionary defaults to the teal fourth band color', () => { const { context } = createResolveContext({}); diff --git a/src/config/settings/registry.test.ts b/src/config/settings/registry.test.ts index e9daf06b..4f9ece6b 100644 --- a/src/config/settings/registry.test.ts +++ b/src/config/settings/registry.test.ts @@ -173,6 +173,7 @@ test('settings registry exposes css declaration editor for primary and secondary assert.equal(field('subtitleStyle.WebkitTextStroke').settingsHidden, true); assert.equal(field('subtitleStyle.knownWordColor').settingsHidden, false); assert.equal(field('subtitleStyle.nPlusOneColor').settingsHidden, false); + assert.equal(field('subtitleStyle.nameMatchImagesEnabled').settingsHidden, false); assert.equal(field('subtitleStyle.nameMatchColor').settingsHidden, false); assert.equal(field('subtitleStyle.jlptColors.N1').settingsHidden, false); assert.equal(field('subtitleStyle.frequencyDictionary.singleColor').settingsHidden, false); diff --git a/src/config/settings/registry.ts b/src/config/settings/registry.ts index 60faed9c..e9da6c20 100644 --- a/src/config/settings/registry.ts +++ b/src/config/settings/registry.ts @@ -345,6 +345,7 @@ function categoryAndSection(path: string): { category: ConfigSettingsCategory; s path === 'subtitleStyle.knownWordColor' || path === 'subtitleStyle.nPlusOneColor' || path === 'subtitleStyle.nameMatchEnabled' || + path === 'subtitleStyle.nameMatchImagesEnabled' || path === 'subtitleStyle.nameMatchColor' ) { return { category: 'appearance', section: 'Annotation Display' }; @@ -524,7 +525,11 @@ function subsectionForPath(path: string): string | undefined { ) { return 'Frequency Highlighting'; } - if (path === 'subtitleStyle.nameMatchEnabled' || path === 'subtitleStyle.nameMatchColor') { + if ( + path === 'subtitleStyle.nameMatchEnabled' || + path === 'subtitleStyle.nameMatchImagesEnabled' || + path === 'subtitleStyle.nameMatchColor' + ) { return 'Character Names'; } if (path === 'anilist.characterDictionary.collapsibleSections.description') { diff --git a/src/core/services/ipc.test.ts b/src/core/services/ipc.test.ts index 70d11c07..f9429a63 100644 --- a/src/core/services/ipc.test.ts +++ b/src/core/services/ipc.test.ts @@ -1191,18 +1191,22 @@ test('registerIpcHandlers rejects malformed controller preference payloads', asy test('registerIpcHandlers exposes character dictionary selection handlers', async () => { const { registrar, handlers } = createFakeIpcRegistrar(); const calls: number[] = []; + const searches: Array = []; registerIpcHandlers( createRegisterIpcDeps({ - getCharacterDictionarySelection: async () => ({ - seriesKey: 're-zero-starting-life-in-another-world-2016', - guessTitle: 'Re ZERO, Starting Life in Another World', - current: { id: 10607, title: 'Rerere no Tensai Bakabon', episodes: 24 }, - override: null, - candidates: [ - { id: 21355, title: 'Re:ZERO -Starting Life in Another World-', episodes: 25 }, - ], - }), + getCharacterDictionarySelection: async (searchTitle) => { + searches.push(searchTitle); + return { + seriesKey: 're-zero-starting-life-in-another-world-2016', + guessTitle: 'Re ZERO, Starting Life in Another World', + current: { id: 10607, title: 'Rerere no Tensai Bakabon', episodes: 24 }, + override: null, + candidates: [ + { id: 21355, title: 'Re:ZERO -Starting Life in Another World-', episodes: 25 }, + ], + }; + }, setCharacterDictionarySelection: async (mediaId) => { calls.push(mediaId); return { @@ -1223,7 +1227,7 @@ test('registerIpcHandlers exposes character dictionary selection handlers', asyn const getHandler = handlers.handle.get(IPC_CHANNELS.request.getCharacterDictionarySelection); const setHandler = handlers.handle.get(IPC_CHANNELS.request.setCharacterDictionarySelection); - assert.deepEqual(await getHandler!({}), { + assert.deepEqual(await getHandler!({}, ' Re:ZERO '), { seriesKey: 're-zero-starting-life-in-another-world-2016', guessTitle: 'Re ZERO, Starting Life in Another World', current: { id: 10607, title: 'Rerere no Tensai Bakabon', episodes: 24 }, @@ -1241,4 +1245,5 @@ test('registerIpcHandlers exposes character dictionary selection handlers', asyn staleMediaIds: [10607], }); assert.deepEqual(calls, [21355]); + assert.deepEqual(searches, ['Re:ZERO']); }); diff --git a/src/core/services/ipc.ts b/src/core/services/ipc.ts index f6b0cf58..c5cd354a 100644 --- a/src/core/services/ipc.ts +++ b/src/core/services/ipc.ts @@ -95,7 +95,7 @@ export interface IpcServiceDeps { getAnilistQueueStatus: () => unknown; retryAnilistQueueNow: () => Promise<{ ok: boolean; message: string }>; runAnilistPostWatchUpdateOnManualMark?: () => Promise; - getCharacterDictionarySelection?: () => Promise; + getCharacterDictionarySelection?: (searchTitle?: string) => Promise; setCharacterDictionarySelection?: (mediaId: number) => Promise; appendClipboardVideoToQueue: () => { ok: boolean; message: string }; getPlaylistBrowserSnapshot: () => Promise; @@ -223,7 +223,7 @@ export interface IpcDepsRuntimeOptions { getAnilistQueueStatus: () => unknown; retryAnilistQueueNow: () => Promise<{ ok: boolean; message: string }>; runAnilistPostWatchUpdateOnManualMark?: () => Promise; - getCharacterDictionarySelection?: () => Promise; + getCharacterDictionarySelection?: (searchTitle?: string) => Promise; setCharacterDictionarySelection?: (mediaId: number) => Promise; appendClipboardVideoToQueue: () => { ok: boolean; message: string }; getPlaylistBrowserSnapshot: () => Promise; @@ -615,8 +615,9 @@ export function registerIpcHandlers(deps: IpcServiceDeps, ipc: IpcMainRegistrar return await deps.retryAnilistQueueNow(); }); - ipc.handle(IPC_CHANNELS.request.getCharacterDictionarySelection, async () => { - return await (deps.getCharacterDictionarySelection?.() ?? + ipc.handle(IPC_CHANNELS.request.getCharacterDictionarySelection, async (_event, searchTitle) => { + const normalizedSearchTitle = typeof searchTitle === 'string' ? searchTitle.trim() : undefined; + return await (deps.getCharacterDictionarySelection?.(normalizedSearchTitle) ?? Promise.resolve({ seriesKey: '', guessTitle: null, diff --git a/src/core/services/tokenizer.test.ts b/src/core/services/tokenizer.test.ts index 71c31811..edc28217 100644 --- a/src/core/services/tokenizer.test.ts +++ b/src/core/services/tokenizer.test.ts @@ -149,6 +149,70 @@ test('tokenizeSubtitle preserves Yomitan name-match metadata on tokens', async ( assert.equal((result.tokens?.[1] as { isNameMatch?: boolean } | undefined)?.isNameMatch, false); }); +test('tokenizeSubtitle attaches character image metadata to name matches when enabled', async () => { + const result = await tokenizeSubtitle( + 'アクアです', + makeDepsFromYomitanTokens( + [ + { surface: 'アクア', reading: 'あくあ', headword: 'アクア', isNameMatch: true }, + { surface: 'です', reading: 'です', headword: 'です' }, + ], + { + getNameMatchImagesEnabled: () => true, + getCharacterNameImage: (term) => + term === 'アクア' + ? { + src: 'data:image/png;base64,AAAA', + alt: 'アクア', + } + : null, + } as Partial, + ), + ); + + assert.deepEqual(result.tokens?.[0]?.characterImage, { + src: 'data:image/png;base64,AAAA', + alt: 'アクア', + }); + assert.equal(result.tokens?.[1]?.characterImage, undefined); +}); + +test('tokenizeSubtitle keeps tokens when character image lookup throws', async () => { + const result = await tokenizeSubtitle( + 'アクア', + makeDepsFromYomitanTokens( + [{ surface: 'アクア', reading: 'あくあ', headword: 'アクア', isNameMatch: true }], + { + getNameMatchImagesEnabled: () => true, + getCharacterNameImage: () => { + throw new Error('image lookup failed'); + }, + } as Partial, + ), + ); + + assert.equal(result.tokens?.[0]?.surface, 'アクア'); + assert.equal(result.tokens?.[0]?.characterImage, undefined); +}); + +test('tokenizeSubtitle omits character image metadata when name-match images are disabled', async () => { + const result = await tokenizeSubtitle( + 'アクア', + makeDepsFromYomitanTokens( + [{ surface: 'アクア', reading: 'あくあ', headword: 'アクア', isNameMatch: true }], + { + getNameMatchImagesEnabled: () => false, + getCharacterNameImage: () => ({ + src: 'data:image/png;base64,AAAA', + alt: 'アクア', + }), + } as Partial, + ), + ); + + assert.equal(result.tokens?.[0]?.characterImage, undefined); +}); + test('tokenizeSubtitle caches JLPT lookups across repeated tokens', async () => { let lookupCalls = 0; const result = await tokenizeSubtitle( diff --git a/src/core/services/tokenizer.ts b/src/core/services/tokenizer.ts index 6b93bcfe..eafbf423 100644 --- a/src/core/services/tokenizer.ts +++ b/src/core/services/tokenizer.ts @@ -3,6 +3,7 @@ import { mergeTokens } from '../../token-merger'; import { createLogger } from '../../logger'; import { FrequencyDictionaryMatchMode, + CharacterNameImage, MergedToken, NPlusOneMatchMode, SubtitleData, @@ -48,6 +49,8 @@ export interface TokenizerServiceDeps { getNPlusOneEnabled?: () => boolean; getJlptEnabled?: () => boolean; getNameMatchEnabled?: () => boolean; + getNameMatchImagesEnabled?: () => boolean; + getCharacterNameImage?: (term: string) => CharacterNameImage | null; getFrequencyDictionaryEnabled?: () => boolean; getFrequencyDictionaryMatchMode?: () => FrequencyDictionaryMatchMode; getFrequencyRank?: FrequencyDictionaryLookup; @@ -80,6 +83,8 @@ export interface TokenizerDepsRuntimeOptions { getNPlusOneEnabled?: () => boolean; getJlptEnabled?: () => boolean; getNameMatchEnabled?: () => boolean; + getNameMatchImagesEnabled?: () => boolean; + getCharacterNameImage?: (term: string) => CharacterNameImage | null; getFrequencyDictionaryEnabled?: () => boolean; getFrequencyDictionaryMatchMode?: () => FrequencyDictionaryMatchMode; getFrequencyRank?: FrequencyDictionaryLookup; @@ -94,6 +99,7 @@ interface TokenizerAnnotationOptions { nPlusOneEnabled: boolean; jlptEnabled: boolean; nameMatchEnabled: boolean; + nameMatchImagesEnabled: boolean; frequencyEnabled: boolean; frequencyMatchMode: FrequencyDictionaryMatchMode; minSentenceWordsForNPlusOne: number | undefined; @@ -229,6 +235,8 @@ export function createTokenizerDepsRuntime( getNPlusOneEnabled: options.getNPlusOneEnabled, getJlptEnabled: options.getJlptEnabled, getNameMatchEnabled: options.getNameMatchEnabled, + getNameMatchImagesEnabled: options.getNameMatchImagesEnabled, + getCharacterNameImage: options.getCharacterNameImage, getFrequencyDictionaryEnabled: options.getFrequencyDictionaryEnabled, getFrequencyDictionaryMatchMode: options.getFrequencyDictionaryMatchMode ?? (() => 'headword'), getFrequencyRank: options.getFrequencyRank, @@ -684,6 +692,7 @@ function getAnnotationOptions(deps: TokenizerServiceDeps): TokenizerAnnotationOp nPlusOneEnabled, jlptEnabled: deps.getJlptEnabled?.() !== false, nameMatchEnabled: deps.getNameMatchEnabled?.() !== false, + nameMatchImagesEnabled: deps.getNameMatchImagesEnabled?.() === true, frequencyEnabled: deps.getFrequencyDictionaryEnabled?.() !== false, frequencyMatchMode: deps.getFrequencyDictionaryMatchMode?.() ?? 'headword', minSentenceWordsForNPlusOne: deps.getMinSentenceWordsForNPlusOne?.(), @@ -780,6 +789,53 @@ async function parseWithYomitanInternalParser( return enrichedTokens; } +function resolveCharacterNameImageForToken( + token: MergedToken, + getCharacterNameImage: (term: string) => CharacterNameImage | null, +): CharacterNameImage | null { + const terms = [token.headword, token.surface] + .map((term) => term.trim()) + .filter((term, index, list) => term.length > 0 && list.indexOf(term) === index); + for (const term of terms) { + const image = getCharacterNameImage(term); + if (image) { + return image; + } + } + return null; +} + +function applyCharacterNameImages( + tokens: MergedToken[], + deps: TokenizerServiceDeps, + options: TokenizerAnnotationOptions, +): MergedToken[] { + if ( + !options.nameMatchEnabled || + !options.nameMatchImagesEnabled || + typeof deps.getCharacterNameImage !== 'function' + ) { + return tokens.map((token) => ({ ...token, characterImage: undefined })); + } + + const getCharacterNameImage = deps.getCharacterNameImage; + return tokens.map((token) => { + if (token.isNameMatch !== true) { + return { ...token, characterImage: undefined }; + } + let characterImage: CharacterNameImage | undefined; + try { + characterImage = resolveCharacterNameImageForToken(token, getCharacterNameImage) ?? undefined; + } catch (err) { + logger.warn('Failed to resolve character name image:', (err as Error).message); + } + return { + ...token, + characterImage, + }; + }); +} + export async function tokenizeSubtitle( text: string, deps: TokenizerServiceDeps, @@ -805,9 +861,10 @@ export async function tokenizeSubtitle( const yomitanTokens = await parseWithYomitanInternalParser(tokenizeText, deps, annotationOptions); if (yomitanTokens && yomitanTokens.length > 0) { const annotatedTokens = await applyAnnotationStage(yomitanTokens, deps, annotationOptions); + const renderedTokens = applyCharacterNameImages(annotatedTokens, deps, annotationOptions); return { text: displayText, - tokens: annotatedTokens.length > 0 ? annotatedTokens : null, + tokens: renderedTokens.length > 0 ? renderedTokens : null, }; } diff --git a/src/core/services/tokenizer/annotation-stage.test.ts b/src/core/services/tokenizer/annotation-stage.test.ts index 6dde5087..46f73448 100644 --- a/src/core/services/tokenizer/annotation-stage.test.ts +++ b/src/core/services/tokenizer/annotation-stage.test.ts @@ -788,6 +788,30 @@ test('stripSubtitleAnnotationMetadata keeps known hover data while clearing non- }); }); +test('stripSubtitleAnnotationMetadata clears character image metadata from excluded name matches', () => { + const token = makeToken({ + surface: 'は', + headword: 'は', + reading: 'ハ', + partOfSpeech: PartOfSpeech.particle, + pos1: '助詞', + isNameMatch: true, + }); + token.characterImage = { + src: 'data:image/png;base64,AAAA', + alt: 'は', + }; + + assert.deepEqual(stripSubtitleAnnotationMetadata(token), { + ...token, + isNPlusOneTarget: false, + isNameMatch: false, + characterImage: undefined, + jlptLevel: undefined, + frequencyRank: undefined, + }); +}); + test('stripSubtitleAnnotationMetadata leaves content tokens unchanged', () => { const token = makeToken({ surface: '猫', diff --git a/src/core/services/tokenizer/subtitle-annotation-filter.ts b/src/core/services/tokenizer/subtitle-annotation-filter.ts index 927a0364..3e8d3b08 100644 --- a/src/core/services/tokenizer/subtitle-annotation-filter.ts +++ b/src/core/services/tokenizer/subtitle-annotation-filter.ts @@ -508,11 +508,17 @@ export function stripSubtitleAnnotationMetadata( return token; } - return { + const strippedToken = { ...token, isNPlusOneTarget: false, isNameMatch: false, jlptLevel: undefined, frequencyRank: undefined, }; + + if ('characterImage' in strippedToken) { + strippedToken.characterImage = undefined; + } + + return strippedToken; } diff --git a/src/core/services/tokenizer/yomitan-parser-runtime.test.ts b/src/core/services/tokenizer/yomitan-parser-runtime.test.ts index 15b04c9f..5fc414b9 100644 --- a/src/core/services/tokenizer/yomitan-parser-runtime.test.ts +++ b/src/core/services/tokenizer/yomitan-parser-runtime.test.ts @@ -1577,18 +1577,24 @@ test('dictionary settings helpers upsert and remove dictionary entries without r assert.match(upsertScript ?? '', /"enabled":true/); }); -test('importYomitanDictionaryFromZip uses settings automation bridge instead of custom backend action', async () => { +test('importYomitanDictionaryFromZip imports via localhost URL instead of embedding archive bytes in script', async () => { const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-yomitan-import-')); const zipPath = path.join(tempDir, 'dict.zip'); fs.writeFileSync(zipPath, Buffer.from('zip-bytes')); const scripts: string[] = []; + const servedArchives: string[] = []; const settingsWindow = { isDestroyed: () => false, destroy: () => undefined, webContents: { executeJavaScript: async (script: string) => { scripts.push(script); + const urlMatch = script.match(/importDictionaryArchiveUrl\(\s*"([^"]+)"/); + if (urlMatch) { + const response = await fetch(JSON.parse(`"${urlMatch[1]}"`) as string); + servedArchives.push(await response.text()); + } return true; }, }, @@ -1611,15 +1617,103 @@ test('importYomitanDictionaryFromZip uses settings automation bridge instead of true, ); assert.equal( - scripts.some((script) => script.includes('importDictionaryArchiveBase64')), + scripts.some((script) => script.includes('importDictionaryArchiveUrl')), true, ); + assert.deepEqual(servedArchives, ['zip-bytes']); + assert.equal( + scripts.some((script) => script.includes('emlwLWJ5dGVz')), + false, + ); assert.equal( scripts.some((script) => script.includes('subminerImportDictionary')), false, ); }); +test('importYomitanDictionaryFromZip falls back to base64 import for older Yomitan bridge', async () => { + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-yomitan-import-')); + const zipPath = path.join(tempDir, 'dict.zip'); + fs.writeFileSync(zipPath, Buffer.from('zip-bytes')); + + const scripts: string[] = []; + const settingsWindow = { + isDestroyed: () => false, + destroy: () => undefined, + webContents: { + executeJavaScript: async (script: string) => { + scripts.push(script); + if ( + script.includes( + 'typeof globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveUrl', + ) + ) { + return false; + } + return true; + }, + }, + }; + + const deps = createDeps(async () => true, { + createYomitanExtensionWindow: async (pageName: string) => { + assert.equal(pageName, 'settings.html'); + return settingsWindow; + }, + }); + + const imported = await importYomitanDictionaryFromZip(zipPath, deps, { + error: () => undefined, + }); + + assert.equal(imported, true); + assert.equal( + scripts.some((script) => script.includes('importDictionaryArchiveBase64')), + true, + ); + assert.equal( + scripts.some((script) => script.includes('importDictionaryArchiveUrl(')), + false, + ); + assert.equal( + scripts.some((script) => script.includes('emlwLWJ5dGVz')), + true, + ); +}); + +test('importYomitanDictionaryFromZip returns false when served archive cannot be read', async () => { + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-yomitan-import-')); + const zipPath = path.join(tempDir, 'dict.zip'); + fs.writeFileSync(zipPath, Buffer.from('zip-bytes')); + + const settingsWindow = { + isDestroyed: () => false, + destroy: () => undefined, + webContents: { + executeJavaScript: async (script: string) => { + const urlMatch = script.match(/importDictionaryArchiveUrl\(\s*"([^"]+)"/); + if (!urlMatch) return true; + fs.unlinkSync(zipPath); + const response = await fetch(JSON.parse(`"${urlMatch[1]}"`) as string); + return response.ok; + }, + }, + }; + + const deps = createDeps(async () => true, { + createYomitanExtensionWindow: async (pageName: string) => { + assert.equal(pageName, 'settings.html'); + return settingsWindow; + }, + }); + + const imported = await importYomitanDictionaryFromZip(zipPath, deps, { + error: () => undefined, + }); + + assert.equal(imported, false); +}); + test('deleteYomitanDictionaryByTitle uses settings automation bridge instead of custom backend action', async () => { const scripts: string[] = []; const settingsWindow = { diff --git a/src/core/services/tokenizer/yomitan-parser-runtime.ts b/src/core/services/tokenizer/yomitan-parser-runtime.ts index b5591dda..c5fd0835 100644 --- a/src/core/services/tokenizer/yomitan-parser-runtime.ts +++ b/src/core/services/tokenizer/yomitan-parser-runtime.ts @@ -1,5 +1,6 @@ import type { BrowserWindow, Extension, Session } from 'electron'; import * as fs from 'fs'; +import * as http from 'http'; import * as path from 'path'; import { selectYomitanParseTokens } from './parser-selection-stage'; @@ -705,6 +706,70 @@ async function invokeYomitanSettingsAutomation( } } +async function serveDictionaryZipOnce( + zipPath: string, + callback: (url: string) => Promise, +): Promise { + const fileName = path.basename(zipPath); + const token = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`; + const requestPath = `/${token}/${encodeURIComponent(fileName)}`; + let served = false; + const server = http.createServer((request, response) => { + if (request.method === 'OPTIONS') { + response.writeHead(204, { + 'access-control-allow-origin': '*', + 'access-control-allow-methods': 'GET, OPTIONS', + }); + response.end(); + return; + } + if (request.method !== 'GET' || request.url !== requestPath || served) { + response.writeHead(404, { 'access-control-allow-origin': '*' }); + response.end(); + return; + } + served = true; + let size = 0; + try { + size = fs.statSync(zipPath).size; + } catch { + response.writeHead(500, { 'access-control-allow-origin': '*' }); + response.end(); + return; + } + response.writeHead(200, { + 'access-control-allow-origin': '*', + 'content-length': String(size), + 'content-type': 'application/zip', + }); + const stream = fs.createReadStream(zipPath); + stream.on('error', () => { + if (!response.headersSent) { + response.writeHead(500, { 'access-control-allow-origin': '*' }); + response.end(); + return; + } + response.destroy(); + }); + stream.pipe(response); + }); + + await new Promise((resolve, reject) => { + server.once('error', reject); + server.listen(0, '127.0.0.1', () => resolve()); + }); + + try { + const address = server.address(); + if (!address || typeof address === 'string') { + throw new Error('Dictionary import server did not bind to a TCP port.'); + } + return await callback(`http://127.0.0.1:${address.port}${requestPath}`); + } finally { + await new Promise((resolve) => server.close(() => resolve())); + } +} + const YOMITAN_SCANNING_HELPERS = String.raw` const HIRAGANA_CONVERSION_RANGE = [0x3041, 0x3096]; const KATAKANA_CONVERSION_RANGE = [0x30a1, 0x30f6]; @@ -1863,17 +1928,43 @@ export async function importYomitanDictionaryFromZip( return false; } - const archiveBase64 = fs.readFileSync(normalizedZipPath).toString('base64'); - const script = ` - (async () => { - await globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveBase64( - ${JSON.stringify(archiveBase64)}, - ${JSON.stringify(path.basename(normalizedZipPath))} - ); - return true; - })(); - `; - const result = await invokeYomitanSettingsAutomation(script, deps, logger); + const supportsUrlImport = await invokeYomitanSettingsAutomation( + ` + (() => typeof globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveUrl === "function")(); + `, + deps, + logger, + ); + + const result = + supportsUrlImport === true + ? await serveDictionaryZipOnce(normalizedZipPath, async (archiveUrl) => + invokeYomitanSettingsAutomation( + ` + (async () => { + await globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveUrl( + ${JSON.stringify(archiveUrl)} + ); + return true; + })(); + `, + deps, + logger, + ), + ) + : await invokeYomitanSettingsAutomation( + ` + (async () => { + await globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveBase64( + ${JSON.stringify(fs.readFileSync(normalizedZipPath).toString('base64'))}, + ${JSON.stringify(path.basename(normalizedZipPath))} + ); + return true; + })(); + `, + deps, + logger, + ); return result === true; } diff --git a/src/main.ts b/src/main.ts index 30600421..19920485 100644 --- a/src/main.ts +++ b/src/main.ts @@ -518,6 +518,7 @@ import { createOverlayVisibilityRuntimeService } from './main/overlay-visibility import { createStatsOverlayVisibilityChangeHandler } from './main/runtime/stats-overlay-visibility'; import { createDiscordPresenceRuntime } from './main/runtime/discord-presence-runtime'; import { createCharacterDictionaryRuntimeService } from './main/character-dictionary-runtime'; +import { createCharacterDictionaryImageLookup } from './main/character-dictionary-runtime/image-lookup'; import { createCharacterDictionaryAutoSyncRuntimeService } from './main/runtime/character-dictionary-auto-sync'; import { handleCharacterDictionaryAutoSyncComplete } from './main/runtime/character-dictionary-auto-sync-completion'; import { notifyCharacterDictionaryAutoSyncStatus } from './main/runtime/character-dictionary-auto-sync-notifications'; @@ -2178,6 +2179,7 @@ const characterDictionaryRuntime = createCharacterDictionaryRuntimeService({ getCurrentMediaTitle: () => appState.currentMediaTitle, resolveMediaPathForJimaku: (mediaPath) => mediaRuntime.resolveMediaPathForJimaku(mediaPath), guessAnilistMediaInfo: (mediaPath, mediaTitle) => guessAnilistMediaInfo(mediaPath, mediaTitle), + getNameMatchImagesEnabled: () => getResolvedConfig().subtitleStyle.nameMatchImagesEnabled, getCollapsibleSectionOpenState: (section) => getResolvedConfig().anilist.characterDictionary.collapsibleSections[section], now: () => Date.now(), @@ -2185,6 +2187,10 @@ const characterDictionaryRuntime = createCharacterDictionaryRuntimeService({ logWarn: (message) => logger.warn(message), }); +const characterDictionaryImageLookup = createCharacterDictionaryImageLookup({ + userDataPath: USER_DATA_PATH, +}); + const characterDictionaryAutoSyncRuntime = createCharacterDictionaryAutoSyncRuntimeService({ userDataPath: USER_DATA_PATH, getConfig: () => { @@ -4728,6 +4734,8 @@ const { yomitanProfilePolicy.isCharacterDictionaryEnabled() && !isYoutubePlaybackActiveNow(), getNameMatchEnabled: () => getResolvedConfig().subtitleStyle.nameMatchEnabled, + getNameMatchImagesEnabled: () => getResolvedConfig().subtitleStyle.nameMatchImagesEnabled, + getCharacterNameImage: (term) => characterDictionaryImageLookup.get(term), getFrequencyDictionaryEnabled: () => getRuntimeBooleanOption( 'subtitle.annotation.frequency', @@ -5967,8 +5975,8 @@ const { registerIpcRuntimeHandlers } = composeIpcRuntimeHandlers({ getAnilistQueueStatus: () => anilistStateRuntime.getQueueStatusSnapshot(), retryAnilistQueueNow: () => processNextAnilistRetryUpdate(), runAnilistPostWatchUpdateOnManualMark: () => maybeRunAnilistPostWatchUpdate({ force: true }), - getCharacterDictionarySelection: () => - characterDictionaryRuntime.getManualSelectionSnapshot(), + getCharacterDictionarySelection: (searchTitle?: string) => + characterDictionaryRuntime.getManualSelectionSnapshot(undefined, searchTitle), setCharacterDictionarySelection: async (mediaId: number) => applyCharacterDictionarySelection( { mediaId }, diff --git a/src/main/character-dictionary-runtime.test.ts b/src/main/character-dictionary-runtime.test.ts index 13f365d7..a3166513 100644 --- a/src/main/character-dictionary-runtime.test.ts +++ b/src/main/character-dictionary-runtime.test.ts @@ -195,22 +195,45 @@ test('generateForCurrentMedia emits structured-content glossary so image stays w assert.equal(nameDiv.tag, 'div'); assert.equal(nameDiv.content, 'アレクシア・ミドガル'); - const secondaryNameDiv = children[1] as { tag: string; content: string }; - assert.equal(secondaryNameDiv.tag, 'div'); - assert.equal(secondaryNameDiv.content, 'Alexia Midgar'); + assert.equal( + children.some((child) => (child as { content?: unknown }).content === 'Alexia Midgar'), + false, + ); - const imageWrap = children[2] as { tag: string; content: Record }; + const imageWrap = children.find((child) => { + const content = (child as { content?: unknown }).content; + return ( + content && + typeof content === 'object' && + !Array.isArray(content) && + (content as { path?: unknown }).path === 'img/m130298-c123.png' + ); + }) as { tag: string; content: Record } | undefined; + assert.ok(imageWrap); assert.equal(imageWrap.tag, 'div'); const image = imageWrap.content as Record; assert.equal(image.tag, 'img'); assert.equal(image.path, 'img/m130298-c123.png'); assert.equal(image.sizeUnits, 'em'); - const sourceDiv = children[3] as { tag: string; content: string }; + const sourceDiv = children.find((child) => { + const content = (child as { content?: unknown }).content; + return typeof content === 'string' && content.includes('The Eminence in Shadow'); + }) as { tag: string; content: string } | undefined; + assert.ok(sourceDiv); assert.equal(sourceDiv.tag, 'div'); assert.ok(sourceDiv.content.includes('The Eminence in Shadow')); - const roleBadgeDiv = children[4] as { tag: string; content: Record }; + const roleBadgeDiv = children.find((child) => { + const content = (child as { content?: unknown }).content; + return ( + content && + typeof content === 'object' && + !Array.isArray(content) && + (content as { content?: unknown }).content === 'Main Character' + ); + }) as { tag: string; content: Record } | undefined; + assert.ok(roleBadgeDiv); assert.equal(roleBadgeDiv.tag, 'div'); const badge = roleBadgeDiv.content as { tag: string; content: string }; assert.equal(badge.tag, 'span'); @@ -1882,9 +1905,9 @@ test('generateForCurrentMedia logs progress while resolving and rebuilding snaps '[dictionary] snapshot miss for AniList 130298, fetching characters', '[dictionary] downloaded AniList character page 1 for AniList 130298', '[dictionary] downloading 1 images for AniList 130298', - '[dictionary] stored snapshot for AniList 130298: 32 terms', + '[dictionary] stored snapshot for AniList 130298: 16 terms', '[dictionary] building ZIP for AniList 130298', - '[dictionary] generated AniList 130298: 32 terms -> ' + + '[dictionary] generated AniList 130298: 16 terms -> ' + path.join(userDataPath, 'character-dictionaries', 'anilist-130298.zip'), ]); } finally { diff --git a/src/main/character-dictionary-runtime.ts b/src/main/character-dictionary-runtime.ts index 408ccb8c..8ed153e2 100644 --- a/src/main/character-dictionary-runtime.ts +++ b/src/main/character-dictionary-runtime.ts @@ -37,6 +37,7 @@ import { buildCharacterDictionarySeriesKey, createCharacterDictionaryManualSelectionStore, } from './character-dictionary-runtime/manual-selection'; +import { snapshotHasCharacterNameImages } from './character-dictionary-runtime/image-lookup'; import type { AniListMediaCandidate, CharacterDictionaryBuildResult, @@ -151,6 +152,7 @@ export function createCharacterDictionaryRuntimeService(deps: CharacterDictionar buildMergedDictionary: (mediaIds: number[]) => Promise; getManualSelectionSnapshot: ( targetPath?: string, + searchTitle?: string, ) => Promise; setManualSelection: (request: { targetPath?: string; @@ -168,6 +170,13 @@ export function createCharacterDictionaryRuntimeService(deps: CharacterDictionar userDataPath: deps.userDataPath, }); + const shouldRefreshCachedSnapshot = (snapshot: CharacterDictionarySnapshot): boolean => { + if (deps.getNameMatchImagesEnabled?.() !== true) { + return false; + } + return !snapshotHasCharacterNameImages(snapshot); + }; + const createAniListRequestSlot = (): (() => Promise) => { let hasAniListRequest = false; return async (): Promise => { @@ -205,12 +214,19 @@ export function createCharacterDictionaryRuntimeService(deps: CharacterDictionar mediaTitle: guessInput.mediaTitle, guess: guessed, }), + unscopedSeriesKey: buildCharacterDictionarySeriesKey({ + mediaPath: null, + mediaTitle: guessInput.mediaTitle, + guess: guessed, + }), }; }; const findCachedSnapshotForSeriesKey = ( seriesKey: string, + fallbackSeriesKey?: string, ): CharacterDictionarySnapshot | null => { + const acceptedKeys = new Set([seriesKey, fallbackSeriesKey].filter(Boolean)); return ( readCachedSnapshots(outputDir).find((snapshot) => { const snapshotSeriesKey = buildCharacterDictionarySeriesKey({ @@ -223,7 +239,7 @@ export function createCharacterDictionaryRuntimeService(deps: CharacterDictionar source: 'fallback', }, }); - return snapshotSeriesKey === seriesKey; + return acceptedKeys.has(snapshotSeriesKey); }) ?? null ); }; @@ -233,7 +249,7 @@ export function createCharacterDictionaryRuntimeService(deps: CharacterDictionar beforeRequest?: () => Promise, ): Promise => { deps.logInfo?.('[dictionary] resolving current anime for character dictionary generation'); - const { guessed, seriesKey } = await guessCurrentMedia(targetPath); + const { guessed, seriesKey, unscopedSeriesKey } = await guessCurrentMedia(targetPath); deps.logInfo?.( `[dictionary] current anime guess: ${guessed.title.trim()}${ typeof guessed.episode === 'number' && guessed.episode > 0 @@ -267,7 +283,7 @@ export function createCharacterDictionaryRuntimeService(deps: CharacterDictionar } } - const cachedSnapshot = findCachedSnapshotForSeriesKey(seriesKey); + const cachedSnapshot = findCachedSnapshotForSeriesKey(seriesKey, unscopedSeriesKey); if (cachedSnapshot) { writeCachedMediaResolution(outputDir, { seriesKey, @@ -301,7 +317,7 @@ export function createCharacterDictionaryRuntimeService(deps: CharacterDictionar ): Promise => { const snapshotPath = getSnapshotPath(outputDir, mediaId); const cachedSnapshot = readSnapshot(snapshotPath); - if (cachedSnapshot) { + if (cachedSnapshot && !shouldRefreshCachedSnapshot(cachedSnapshot)) { deps.logInfo?.(`[dictionary] snapshot hit for AniList ${mediaId}`); return { mediaId: cachedSnapshot.mediaId, @@ -311,6 +327,11 @@ export function createCharacterDictionaryRuntimeService(deps: CharacterDictionar updatedAt: cachedSnapshot.updatedAt, }; } + if (cachedSnapshot) { + deps.logInfo?.( + `[dictionary] snapshot stale for AniList ${mediaId}: missing cached character images`, + ); + } progress?.onGenerating?.({ mediaId, @@ -455,28 +476,43 @@ export function createCharacterDictionaryRuntimeService(deps: CharacterDictionar entryCount, }; }, - getManualSelectionSnapshot: async (targetPath?: string) => { + getManualSelectionSnapshot: async (targetPath?: string, searchTitle?: string) => { const waitForAniListRequestSlot = createAniListRequestSlot(); const { guessed, seriesKey } = await guessCurrentMedia(targetPath); - const [candidates, override] = await Promise.all([ - searchAniListMediaCandidates(guessed.title, waitForAniListRequestSlot), + const normalizedSearchTitle = searchTitle?.trim(); + const shouldUseExplicitSearch = searchTitle !== undefined; + const candidateSearchTitle = shouldUseExplicitSearch ? normalizedSearchTitle : guessed.title; + const candidates = candidateSearchTitle + ? await searchAniListMediaCandidates(candidateSearchTitle, waitForAniListRequestSlot) + : []; + const [override, current] = await Promise.all([ manualSelectionStore.getOverride(seriesKey), + shouldUseExplicitSearch + ? Promise.resolve(null) + : resolveAniListMediaIdFromGuess(guessed, waitForAniListRequestSlot) + .then( + (entry): AniListMediaCandidate => ({ + id: entry.id, + title: entry.title, + episodes: + candidates.find((candidate) => candidate.id === entry.id)?.episodes ?? null, + }), + ) + .catch(() => null), ]); - const current = await resolveAniListMediaIdFromGuess(guessed, waitForAniListRequestSlot) - .then( - (entry): AniListMediaCandidate => ({ - id: entry.id, - title: entry.title, - episodes: candidates.find((candidate) => candidate.id === entry.id)?.episodes ?? null, - }), - ) - .catch(() => null); + const overrideCandidate = override + ? candidates.find((candidate) => candidate.id === override.mediaId) + : null; return { seriesKey, guessTitle: guessed.title, current, override: override - ? { id: override.mediaId, title: override.mediaTitle, episodes: null } + ? { + id: override.mediaId, + title: override.mediaTitle, + episodes: overrideCandidate?.episodes ?? null, + } : null, candidates, }; diff --git a/src/main/character-dictionary-runtime/build.test.ts b/src/main/character-dictionary-runtime/build.test.ts index caf0755d..974e6023 100644 --- a/src/main/character-dictionary-runtime/build.test.ts +++ b/src/main/character-dictionary-runtime/build.test.ts @@ -1,7 +1,7 @@ import assert from 'node:assert/strict'; import test from 'node:test'; -import { applyCollapsibleOpenStatesToTermEntries } from './build'; -import type { CharacterDictionaryTermEntry } from './types'; +import { applyCollapsibleOpenStatesToTermEntries, buildSnapshotFromCharacters } from './build'; +import type { CharacterDictionaryTermEntry, CharacterRecord } from './types'; test('applyCollapsibleOpenStatesToTermEntries reapplies configured details open states', () => { const termEntries: CharacterDictionaryTermEntry[] = [ @@ -56,3 +56,66 @@ test('applyCollapsibleOpenStatesToTermEntries reapplies configured details open assert.equal(glossaryEntry.content.content[0]?.open, true); assert.equal(glossaryEntry.content.content[1]?.open, false); }); + +test('buildSnapshotFromCharacters shows Japanese aliases without adding romanized names as lookup entries', () => { + const character: CharacterRecord = { + id: 1, + role: 'main', + firstNameHint: '', + fullName: 'Aqua', + lastNameHint: '', + nativeName: 'アクア', + alternativeNames: ['阿久亜'], + bloodType: '', + birthday: null, + description: '', + imageUrl: null, + age: '', + sex: '', + voiceActors: [], + }; + + const snapshot = buildSnapshotFromCharacters( + 100, + 'KonoSuba', + [character], + new Map(), + new Map(), + 1_700_000_000_000, + () => false, + ); + + const aquaEntry = snapshot.termEntries.find(([term]) => term === 'アクア'); + assert.ok(aquaEntry); + const glossaryEntry = aquaEntry[5][0] as { + content: { + content: Array<{ content?: unknown }>; + }; + }; + const wholeGlossary = JSON.stringify(glossaryEntry); + + const knownNames = glossaryEntry.content.content.find((node) => { + const content = node.content; + return ( + Array.isArray(content) && + content.some( + (child) => + child && + typeof child === 'object' && + (child as { content?: unknown }).content === 'Known names', + ) + ); + }) as { content: Array<{ content?: unknown }> } | undefined; + assert.ok(knownNames, 'expected a Known names block in the character glossary'); + const knownNameItems = JSON.stringify(knownNames.content); + const terms = snapshot.termEntries.map(([term]) => term); + + assert.match(knownNameItems, /アクア/); + assert.match(knownNameItems, /阿久亜/); + assert.doesNotMatch(wholeGlossary, /Aqua/); + assert.doesNotMatch(knownNameItems, /Aqua/); + assert.doesNotMatch(knownNameItems, /アクア様/); + assert.equal(terms.includes('Aqua'), false); + assert.equal(terms.includes('アクア'), true); + assert.equal(terms.includes('阿久亜'), true); +}); diff --git a/src/main/character-dictionary-runtime/cache.test.ts b/src/main/character-dictionary-runtime/cache.test.ts index 6f739b0f..9b1b20bb 100644 --- a/src/main/character-dictionary-runtime/cache.test.ts +++ b/src/main/character-dictionary-runtime/cache.test.ts @@ -52,3 +52,18 @@ test('readSnapshot ignores snapshots written with an older format version', () = assert.equal(readSnapshot(snapshotPath), null); }); + +test('readSnapshot ignores v15 snapshots with stale romanized character-name entries', () => { + const outputDir = makeTempDir(); + const snapshotPath = getSnapshotPath(outputDir, 130298); + const staleSnapshot = { + ...createSnapshot(), + formatVersion: 15, + termEntries: [['Vanir', 'ばにる', 'name primary', '', 75, ['Vanir'], 0, '']], + }; + + fs.mkdirSync(path.dirname(snapshotPath), { recursive: true }); + fs.writeFileSync(snapshotPath, JSON.stringify(staleSnapshot), 'utf8'); + + assert.equal(readSnapshot(snapshotPath), null); +}); diff --git a/src/main/character-dictionary-runtime/constants.ts b/src/main/character-dictionary-runtime/constants.ts index c4bef988..c214d9c1 100644 --- a/src/main/character-dictionary-runtime/constants.ts +++ b/src/main/character-dictionary-runtime/constants.ts @@ -1,7 +1,7 @@ export const ANILIST_GRAPHQL_URL = 'https://graphql.anilist.co'; export const ANILIST_REQUEST_DELAY_MS = 2000; export const CHARACTER_IMAGE_DOWNLOAD_DELAY_MS = 250; -export const CHARACTER_DICTIONARY_FORMAT_VERSION = 15; +export const CHARACTER_DICTIONARY_FORMAT_VERSION = 16; export const CHARACTER_DICTIONARY_MERGED_TITLE = 'SubMiner Character Dictionary'; export const HONORIFIC_SUFFIXES = [ diff --git a/src/main/character-dictionary-runtime/fetch.ts b/src/main/character-dictionary-runtime/fetch.ts index 17e02565..c1fd8bbe 100644 --- a/src/main/character-dictionary-runtime/fetch.ts +++ b/src/main/character-dictionary-runtime/fetch.ts @@ -191,11 +191,51 @@ function mapRole(input: string | null | undefined): CharacterDictionaryRole { return 'side'; } -function inferImageExt(contentType: string | null): string { +function inferImageExtFromBytes(bytes: Buffer): string | null { + if ( + bytes.length >= 8 && + bytes[0] === 0x89 && + bytes[1] === 0x50 && + bytes[2] === 0x4e && + bytes[3] === 0x47 + ) { + return 'png'; + } + if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) { + return 'jpg'; + } + if ( + bytes.length >= 12 && + bytes.subarray(0, 4).toString('ascii') === 'RIFF' && + bytes.subarray(8, 12).toString('ascii') === 'WEBP' + ) { + return 'webp'; + } + if (bytes.length >= 6 && bytes.subarray(0, 6).toString('ascii') === 'GIF89a') { + return 'gif'; + } + if (bytes.length >= 6 && bytes.subarray(0, 6).toString('ascii') === 'GIF87a') { + return 'gif'; + } + if ( + bytes.length >= 12 && + bytes.subarray(4, 8).toString('ascii') === 'ftyp' && + bytes.subarray(8, 12).toString('ascii') === 'avif' + ) { + return 'avif'; + } + return null; +} + +function inferImageExt(contentType: string | null, bytes: Buffer): string { + const extFromBytes = inferImageExtFromBytes(bytes); + if (extFromBytes) return extFromBytes; + const normalized = (contentType || '').toLowerCase(); if (normalized.includes('png')) return 'png'; if (normalized.includes('gif')) return 'gif'; if (normalized.includes('webp')) return 'webp'; + if (normalized.includes('avif')) return 'avif'; return 'jpg'; } @@ -462,7 +502,7 @@ export async function downloadCharacterImage( if (!response.ok) return null; const bytes = Buffer.from(await response.arrayBuffer()); if (bytes.length === 0) return null; - const ext = inferImageExt(response.headers.get('content-type')); + const ext = inferImageExt(response.headers.get('content-type'), bytes); return { filename: `c${charId}.${ext}`, ext, diff --git a/src/main/character-dictionary-runtime/glossary.ts b/src/main/character-dictionary-runtime/glossary.ts index c042b395..c02197ca 100644 --- a/src/main/character-dictionary-runtime/glossary.ts +++ b/src/main/character-dictionary-runtime/glossary.ts @@ -117,20 +117,44 @@ function buildVoicedByContent( return { tag: 'ul', style: { marginTop: '0.15em' }, content: items }; } +function buildKnownNamesBlock(nameTerms: string[]): Record | null { + const visibleTerms = [...new Set(nameTerms.map((term) => term.trim()).filter(Boolean))]; + if (visibleTerms.length <= 1) { + return null; + } + + return { + tag: 'div', + style: { fontSize: '0.85em', marginBottom: '0.25em' }, + content: [ + { + tag: 'div', + style: { fontWeight: 'bold', color: '#d0d0d0', marginBottom: '0.1em' }, + content: 'Known names', + }, + { + tag: 'ul', + style: { marginTop: '0', marginBottom: '0', paddingLeft: '1.2em' }, + content: visibleTerms.map((term) => ({ + tag: 'li', + content: term, + })), + }, + ], + }; +} + export function createDefinitionGlossary( character: CharacterRecord, mediaTitle: string, imagePath: string | null, vaImagePaths: Map, + nameTerms: string[], getCollapsibleSectionOpenState: ( section: AnilistCharacterDictionaryCollapsibleSectionKey, ) => boolean, ): CharacterDictionaryGlossaryEntry[] { const displayName = character.nativeName || character.fullName || `Character ${character.id}`; - const secondaryName = - character.nativeName && character.fullName && character.fullName !== character.nativeName - ? character.fullName - : null; const { fields, text: descriptionText } = parseCharacterDescription(character.description); const content: Array> = [ @@ -141,12 +165,9 @@ export function createDefinitionGlossary( }, ]; - if (secondaryName) { - content.push({ - tag: 'div', - style: { fontSize: '0.85em', fontStyle: 'italic', color: '#b0b0b0', marginBottom: '0.2em' }, - content: secondaryName, - }); + const knownNamesBlock = buildKnownNamesBlock(nameTerms); + if (knownNamesBlock) { + content.push(knownNamesBlock); } if (imagePath) { diff --git a/src/main/character-dictionary-runtime/image-lookup.test.ts b/src/main/character-dictionary-runtime/image-lookup.test.ts new file mode 100644 index 00000000..8a05b95b --- /dev/null +++ b/src/main/character-dictionary-runtime/image-lookup.test.ts @@ -0,0 +1,121 @@ +import assert from 'node:assert/strict'; +import * as fs from 'fs'; +import * as os from 'os'; +import * as path from 'path'; +import test from 'node:test'; +import { getSnapshotPath, writeSnapshot } from './cache'; +import { CHARACTER_DICTIONARY_FORMAT_VERSION } from './constants'; +import { buildCharacterNameImageIndexFromSnapshots } from './image-lookup'; +import type { CharacterDictionarySnapshot } from './types'; + +const PNG_1X1_BASE64 = + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+nmX8AAAAASUVORK5CYII='; + +function makeTempDir(): string { + return fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-character-image-lookup-')); +} + +test('buildCharacterNameImageIndexFromSnapshots maps name terms to character portrait data URLs', () => { + const outputDir = makeTempDir(); + const snapshot: CharacterDictionarySnapshot = { + formatVersion: CHARACTER_DICTIONARY_FORMAT_VERSION, + mediaId: 130298, + mediaTitle: 'The Eminence in Shadow', + entryCount: 1, + updatedAt: 1_700_000_000_000, + termEntries: [ + [ + 'アレクシア', + 'あれくしあ', + 'name primary', + '', + 75, + [ + { + type: 'structured-content', + content: { + tag: 'div', + content: [ + { tag: 'div', content: 'アレクシア・ミドガル' }, + { + tag: 'div', + content: { + tag: 'img', + path: 'img/m130298-c123.png', + alt: 'アレクシア・ミドガル', + }, + }, + { + tag: 'details', + content: [ + { tag: 'summary', content: 'Voiced by' }, + { + tag: 'div', + content: { + tag: 'img', + path: 'img/m130298-va456.png', + alt: 'VA', + }, + }, + ], + }, + ], + }, + }, + ], + 0, + '', + ], + ], + images: [ + { path: 'img/m130298-c123.png', dataBase64: 'AAAA' }, + { path: 'img/m130298-va456.png', dataBase64: 'BBBB' }, + ], + }; + writeSnapshot(getSnapshotPath(outputDir, snapshot.mediaId), snapshot); + + const index = buildCharacterNameImageIndexFromSnapshots(outputDir); + + assert.deepEqual(index.get('アレクシア'), { + src: 'data:image/png;base64,AAAA', + alt: 'アレクシア・ミドガル', + }); +}); + +test('buildCharacterNameImageIndexFromSnapshots sniffs image MIME from bytes before path extension', () => { + const outputDir = makeTempDir(); + const snapshot: CharacterDictionarySnapshot = { + formatVersion: CHARACTER_DICTIONARY_FORMAT_VERSION, + mediaId: 130298, + mediaTitle: 'The Eminence in Shadow', + entryCount: 1, + updatedAt: 1_700_000_000_000, + termEntries: [ + [ + 'アレクシア', + 'あれくしあ', + 'name primary', + '', + 75, + [ + { + type: 'structured-content', + content: { + tag: 'img', + path: 'img/m130298-c123.jpg', + alt: 'アレクシア・ミドガル', + }, + }, + ], + 0, + '', + ], + ], + images: [{ path: 'img/m130298-c123.jpg', dataBase64: PNG_1X1_BASE64 }], + }; + writeSnapshot(getSnapshotPath(outputDir, snapshot.mediaId), snapshot); + + const index = buildCharacterNameImageIndexFromSnapshots(outputDir); + + assert.equal(index.get('アレクシア')?.src, `data:image/png;base64,${PNG_1X1_BASE64}`); +}); diff --git a/src/main/character-dictionary-runtime/image-lookup.ts b/src/main/character-dictionary-runtime/image-lookup.ts new file mode 100644 index 00000000..42432365 --- /dev/null +++ b/src/main/character-dictionary-runtime/image-lookup.ts @@ -0,0 +1,249 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import type { CharacterNameImage } from '../../types'; +import { readCachedSnapshots } from './cache'; +import type { + CharacterDictionaryGlossaryEntry, + CharacterDictionarySnapshot, + CharacterDictionarySnapshotImage, + CharacterDictionaryTermEntry, +} from './types'; + +const CHARACTER_IMAGE_PATH_PATTERN = /^img\/m\d+-c\d+\.[a-z0-9]+$/i; + +type StructuredContentNode = { + tag?: unknown; + path?: unknown; + alt?: unknown; + title?: unknown; + content?: unknown; +}; + +function normalizeLookupTerm(term: string): string { + return term.trim(); +} + +function getSnapshotsDir(outputDir: string): string { + return path.join(outputDir, 'snapshots'); +} + +function getImageMimeType(imagePath: string, dataBase64: string): string { + const signature = Buffer.from(dataBase64.slice(0, 64), 'base64'); + if ( + signature.length >= 8 && + signature[0] === 0x89 && + signature[1] === 0x50 && + signature[2] === 0x4e && + signature[3] === 0x47 + ) { + return 'image/png'; + } + if ( + signature.length >= 12 && + signature.subarray(0, 4).toString('ascii') === 'RIFF' && + signature.subarray(8, 12).toString('ascii') === 'WEBP' + ) { + return 'image/webp'; + } + if ( + signature.length >= 6 && + (signature.subarray(0, 6).toString('ascii') === 'GIF89a' || + signature.subarray(0, 6).toString('ascii') === 'GIF87a') + ) { + return 'image/gif'; + } + if (signature.length >= 3 && signature[0] === 0xff && signature[1] === 0xd8) { + return 'image/jpeg'; + } + if ( + signature.length >= 12 && + signature.subarray(4, 8).toString('ascii') === 'ftyp' && + signature.subarray(8, 12).toString('ascii') === 'avif' + ) { + return 'image/avif'; + } + + const ext = path.extname(imagePath).toLowerCase(); + if (ext === '.jpg' || ext === '.jpeg') return 'image/jpeg'; + if (ext === '.png') return 'image/png'; + if (ext === '.webp') return 'image/webp'; + if (ext === '.gif') return 'image/gif'; + if (ext === '.avif') return 'image/avif'; + return 'image/jpeg'; +} + +function buildImageByPath( + images: ReadonlyArray, +): Map { + const imageByPath = new Map(); + for (const image of images) { + if (image.path && image.dataBase64) { + imageByPath.set(image.path, image); + } + } + return imageByPath; +} + +function findCharacterImageNode(value: unknown): StructuredContentNode | null { + if (Array.isArray(value)) { + for (const item of value) { + const found = findCharacterImageNode(item); + if (found) return found; + } + return null; + } + + if (!value || typeof value !== 'object') { + return null; + } + + const node = value as StructuredContentNode; + if ( + node.tag === 'img' && + typeof node.path === 'string' && + CHARACTER_IMAGE_PATH_PATTERN.test(node.path) + ) { + return node; + } + + return findCharacterImageNode(node.content); +} + +function findCharacterImageNodeInGlossary( + glossary: ReadonlyArray, +): StructuredContentNode | null { + for (const entry of glossary) { + const found = findCharacterImageNode(entry); + if (found) return found; + } + return null; +} + +function createCharacterNameImage( + entry: CharacterDictionaryTermEntry, + imageByPath: ReadonlyMap, +): CharacterNameImage | null { + const term = normalizeLookupTerm(entry[0]); + if (!term) { + return null; + } + + const imageNode = findCharacterImageNodeInGlossary(entry[5]); + const imagePath = typeof imageNode?.path === 'string' ? imageNode.path : ''; + const image = imageByPath.get(imagePath); + if (!image) { + return null; + } + + const rawAlt = + typeof imageNode?.alt === 'string' + ? imageNode.alt + : typeof imageNode?.title === 'string' + ? imageNode.title + : term; + const alt = rawAlt.trim() || term; + return { + src: `data:${getImageMimeType(image.path, image.dataBase64)};base64,${image.dataBase64}`, + alt, + }; +} + +function appendSnapshotImages( + index: Map, + snapshot: CharacterDictionarySnapshot, +): void { + const imageByPath = buildImageByPath(snapshot.images); + for (const entry of snapshot.termEntries) { + const term = normalizeLookupTerm(entry[0]); + if (!term || index.has(term)) { + continue; + } + const image = createCharacterNameImage(entry, imageByPath); + if (image) { + index.set(term, image); + } + } +} + +export function snapshotHasCharacterNameImages(snapshot: CharacterDictionarySnapshot): boolean { + const imageByPath = buildImageByPath(snapshot.images); + return snapshot.termEntries.some( + (entry) => createCharacterNameImage(entry, imageByPath) !== null, + ); +} + +function getSnapshotDirectorySignature(outputDir: string): string { + let entries: fs.Dirent[] = []; + try { + entries = fs.readdirSync(getSnapshotsDir(outputDir), { withFileTypes: true }); + } catch { + return ''; + } + + const parts: string[] = []; + for (const entry of entries) { + if (!entry.isFile() || !/^anilist-\d+\.json$/.test(entry.name)) { + continue; + } + const snapshotPath = path.join(getSnapshotsDir(outputDir), entry.name); + try { + const stat = fs.statSync(snapshotPath); + parts.push(`${entry.name}:${stat.mtimeMs}:${stat.size}`); + } catch { + // Ignore files that disappear during refresh; next lookup will rebuild. + } + } + return parts.sort().join('|'); +} + +export function buildCharacterNameImageIndexFromSnapshots( + outputDir: string, +): Map { + const index = new Map(); + for (const snapshot of readCachedSnapshots(outputDir)) { + appendSnapshotImages(index, snapshot); + } + return index; +} + +export function createCharacterDictionaryImageLookup(deps: { + userDataPath?: string; + outputDir?: string; +}): { + get: (term: string) => CharacterNameImage | null; + invalidate: () => void; +} { + const outputDir = + deps.outputDir ?? + (deps.userDataPath ? path.join(deps.userDataPath, 'character-dictionaries') : ''); + let signature: string | null = null; + let index = new Map(); + + function refreshIfNeeded(): void { + if (!outputDir) { + index = new Map(); + signature = ''; + return; + } + const nextSignature = getSnapshotDirectorySignature(outputDir); + if (nextSignature === signature) { + return; + } + signature = nextSignature; + index = buildCharacterNameImageIndexFromSnapshots(outputDir); + } + + return { + get(term: string): CharacterNameImage | null { + const normalizedTerm = normalizeLookupTerm(term); + if (!normalizedTerm) { + return null; + } + refreshIfNeeded(); + return index.get(normalizedTerm) ?? null; + }, + invalidate(): void { + signature = null; + }, + }; +} diff --git a/src/main/character-dictionary-runtime/manual-selection-snapshot.test.ts b/src/main/character-dictionary-runtime/manual-selection-snapshot.test.ts new file mode 100644 index 00000000..3a213dd7 --- /dev/null +++ b/src/main/character-dictionary-runtime/manual-selection-snapshot.test.ts @@ -0,0 +1,162 @@ +import assert from 'node:assert/strict'; +import * as fs from 'fs'; +import * as os from 'os'; +import * as path from 'path'; +import test from 'node:test'; + +import { createCharacterDictionaryRuntimeService } from '../character-dictionary-runtime'; +import { buildCharacterDictionarySeriesKey } from './manual-selection'; + +function makeTempDir(): string { + return fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-character-dictionary-')); +} + +test('getManualSelectionSnapshot waits for explicit search text before fetching candidates', async () => { + const userDataPath = makeTempDir(); + const originalFetch = globalThis.fetch; + const searchTerms: string[] = []; + + globalThis.fetch = (async (_input: string | URL | Request, init?: RequestInit) => { + const body = JSON.parse(String(init?.body ?? '{}')) as { + variables?: { search?: string }; + }; + searchTerms.push(String(body.variables?.search ?? '')); + return new Response( + JSON.stringify({ + data: { + Page: { + media: [ + { + id: 154587, + episodes: 28, + title: { + romaji: 'Sousou no Frieren', + english: 'Frieren: Beyond Journey’s End', + native: '葬送のフリーレン', + }, + }, + ], + }, + }, + }), + { + status: 200, + headers: { 'content-type': 'application/json' }, + }, + ); + }) as typeof globalThis.fetch; + + try { + const runtime = createCharacterDictionaryRuntimeService({ + userDataPath, + getCurrentMediaPath: () => '/tmp/[SubsPlease] Kage no Jitsuryokusha - 05.mkv', + getCurrentMediaTitle: () => '[SubsPlease] Kage no Jitsuryokusha - 05.mkv', + resolveMediaPathForJimaku: (mediaPath) => mediaPath, + guessAnilistMediaInfo: async () => ({ + title: 'Kage no Jitsuryokusha ni Naritakute!', + season: null, + episode: 5, + source: 'guessit', + }), + now: () => 1_700_000_000_000, + }); + + const initial = await runtime.getManualSelectionSnapshot(undefined, ''); + assert.equal(initial.guessTitle, 'Kage no Jitsuryokusha ni Naritakute!'); + assert.deepEqual(initial.candidates, []); + assert.deepEqual(searchTerms, []); + + const searched = await runtime.getManualSelectionSnapshot(undefined, 'Frieren'); + assert.deepEqual(searchTerms, ['Frieren']); + assert.deepEqual(searched.candidates, [ + { id: 154587, title: 'Frieren: Beyond Journey’s End', episodes: 28 }, + ]); + } finally { + globalThis.fetch = originalFetch; + } +}); + +test('getManualSelectionSnapshot hydrates override episode count from searched candidates', async () => { + const userDataPath = makeTempDir(); + const overrideSeriesKey = buildCharacterDictionarySeriesKey({ + mediaPath: '/tmp/KonoSuba - 01.mkv', + mediaTitle: 'KonoSuba - 01.mkv', + guess: { + title: "KonoSuba - God's blessing on this wonderful world!", + year: 2016, + season: null, + episode: 1, + source: 'guessit', + }, + }); + const overrideDir = path.join(userDataPath, 'character-dictionaries'); + fs.mkdirSync(overrideDir, { recursive: true }); + fs.writeFileSync( + path.join(overrideDir, 'anilist-overrides.json'), + JSON.stringify({ + overrides: [ + { + seriesKey: overrideSeriesKey, + mediaId: 21202, + mediaTitle: "KONOSUBA -God's blessing on this wonderful world!", + staleMediaIds: [], + }, + ], + }), + 'utf8', + ); + const originalFetch = globalThis.fetch; + + globalThis.fetch = (async (_input: string | URL | Request) => { + return new Response( + JSON.stringify({ + data: { + Page: { + media: [ + { + id: 21202, + episodes: 10, + title: { + romaji: 'Kono Subarashii Sekai ni Shukufuku wo!', + english: "KONOSUBA -God's blessing on this wonderful world!", + native: 'この素晴らしい世界に祝福を!', + }, + }, + ], + }, + }, + }), + { + status: 200, + headers: { 'content-type': 'application/json' }, + }, + ); + }) as typeof globalThis.fetch; + + try { + const runtime = createCharacterDictionaryRuntimeService({ + userDataPath, + getCurrentMediaPath: () => '/tmp/KonoSuba - 01.mkv', + getCurrentMediaTitle: () => 'KonoSuba - 01.mkv', + resolveMediaPathForJimaku: (mediaPath) => mediaPath, + guessAnilistMediaInfo: async () => ({ + title: "KonoSuba - God's blessing on this wonderful world!", + year: 2016, + season: null, + episode: 1, + source: 'guessit', + }), + now: () => 1_700_000_000_000, + }); + + const snapshot = await runtime.getManualSelectionSnapshot(undefined, 'KonoSuba'); + + assert.deepEqual(snapshot.override, { + id: 21202, + title: "KONOSUBA -God's blessing on this wonderful world!", + episodes: 10, + }); + } finally { + globalThis.fetch = originalFetch; + } +}); diff --git a/src/main/character-dictionary-runtime/manual-selection.test.ts b/src/main/character-dictionary-runtime/manual-selection.test.ts index e7901b40..fd9b066d 100644 --- a/src/main/character-dictionary-runtime/manual-selection.test.ts +++ b/src/main/character-dictionary-runtime/manual-selection.test.ts @@ -10,15 +10,17 @@ import { } from './manual-selection'; const REZERO_EP1 = - '/anime/Re - ZERO, Starting Life in Another World (2016) - S01E01 - - The End of the Beginning and the Beginning of the End [v2 Bluray-1080p Proper][10bit][x265][FLAC 2.0][EN+JA]-SCY.mkv'; + '/anime/ReZERO/Season 1/Re - ZERO, Starting Life in Another World (2016) - S01E01 - - The End of the Beginning and the Beginning of the End [v2 Bluray-1080p Proper][10bit][x265][FLAC 2.0][EN+JA]-SCY.mkv'; const REZERO_EP2 = - '/anime/Re - ZERO, Starting Life in Another World (2016) - S01E02 - Reunion with the Witch [Bluray-1080p][x265][JA]-SCY.mkv'; + '/anime/ReZERO/Season 1/Re - ZERO, Starting Life in Another World (2016) - S01E02 - Reunion with the Witch [Bluray-1080p][x265][JA]-SCY.mkv'; +const REZERO_S2_EP1 = + '/anime/ReZERO/Season 2/Re - ZERO, Starting Life in Another World (2016) - S02E01 - Each Ones Promise [Bluray-1080p][x265][JA]-SCY.mkv'; function makeTempDir(): string { return fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-manual-selection-')); } -test('buildCharacterDictionarySeriesKey uses guessit title, alternative title, and year for Re ZERO series scope', () => { +test('buildCharacterDictionarySeriesKey scopes guessit title and year by media directory', () => { const key = buildCharacterDictionarySeriesKey({ mediaPath: REZERO_EP1, mediaTitle: null, @@ -32,10 +34,10 @@ test('buildCharacterDictionarySeriesKey uses guessit title, alternative title, a }, }); - assert.equal(key, 're-zero-starting-life-in-another-world-2016'); + assert.equal(key, 'anime-rezero-season-1--re-zero-starting-life-in-another-world-2016'); }); -test('manual selection store persists overrides and matches later episodes in the same series', async () => { +test('manual selection store persists overrides and matches later episodes in the same directory', async () => { const userDataPath = makeTempDir(); const store = createCharacterDictionaryManualSelectionStore({ userDataPath }); const firstKey = buildCharacterDictionarySeriesKey({ @@ -79,3 +81,131 @@ test('manual selection store persists overrides and matches later episodes in th staleMediaIds: [10607], }); }); + +test('manual selection store resolves legacy unscoped override keys', async () => { + const userDataPath = makeTempDir(); + const overrideDir = path.join(userDataPath, 'character-dictionaries'); + fs.mkdirSync(overrideDir, { recursive: true }); + fs.writeFileSync( + path.join(overrideDir, 'anilist-overrides.json'), + JSON.stringify({ + overrides: [ + { + seriesKey: 're-zero-starting-life-in-another-world-2016', + mediaId: 21355, + mediaTitle: 'Re:ZERO -Starting Life in Another World-', + staleMediaIds: [10607], + }, + ], + }), + 'utf8', + ); + + const scopedKey = buildCharacterDictionarySeriesKey({ + mediaPath: REZERO_EP1, + mediaTitle: null, + guess: { + title: 'Re ZERO, Starting Life in Another World', + alternativeTitle: 'ZERO, Starting Life in Another World', + year: 2016, + season: 1, + episode: 1, + source: 'guessit', + }, + }); + + const store = createCharacterDictionaryManualSelectionStore({ userDataPath }); + + assert.deepEqual(await store.getOverride(scopedKey), { + seriesKey: 're-zero-starting-life-in-another-world-2016', + mediaId: 21355, + mediaTitle: 'Re:ZERO -Starting Life in Another World-', + staleMediaIds: [10607], + }); +}); + +test('manual selection store prefers exact scoped override over legacy fallback', async () => { + const userDataPath = makeTempDir(); + const overrideDir = path.join(userDataPath, 'character-dictionaries'); + fs.mkdirSync(overrideDir, { recursive: true }); + const scopedKey = buildCharacterDictionarySeriesKey({ + mediaPath: REZERO_EP1, + mediaTitle: null, + guess: { + title: 'Re ZERO, Starting Life in Another World', + alternativeTitle: 'ZERO, Starting Life in Another World', + year: 2016, + season: 1, + episode: 1, + source: 'guessit', + }, + }); + fs.writeFileSync( + path.join(overrideDir, 'anilist-overrides.json'), + JSON.stringify({ + overrides: [ + { + seriesKey: 're-zero-starting-life-in-another-world-2016', + mediaId: 10607, + mediaTitle: 'Legacy Re:ZERO', + staleMediaIds: [], + }, + { + seriesKey: scopedKey, + mediaId: 21355, + mediaTitle: 'Re:ZERO -Starting Life in Another World-', + staleMediaIds: [10607], + }, + ], + }), + 'utf8', + ); + + const store = createCharacterDictionaryManualSelectionStore({ userDataPath }); + + assert.deepEqual(await store.getOverride(scopedKey), { + seriesKey: scopedKey, + mediaId: 21355, + mediaTitle: 'Re:ZERO -Starting Life in Another World-', + staleMediaIds: [10607], + }); +}); + +test('manual selection store keeps overrides separate for different season directories', async () => { + const userDataPath = makeTempDir(); + const store = createCharacterDictionaryManualSelectionStore({ userDataPath }); + const firstSeasonKey = buildCharacterDictionarySeriesKey({ + mediaPath: REZERO_EP1, + mediaTitle: null, + guess: { + title: 'Re ZERO, Starting Life in Another World', + alternativeTitle: 'ZERO, Starting Life in Another World', + year: 2016, + season: 1, + episode: 1, + source: 'guessit', + }, + }); + await store.setOverride({ + seriesKey: firstSeasonKey, + mediaId: 21355, + mediaTitle: 'Re:ZERO -Starting Life in Another World-', + staleMediaIds: [], + }); + + const secondSeasonKey = buildCharacterDictionarySeriesKey({ + mediaPath: REZERO_S2_EP1, + mediaTitle: null, + guess: { + title: 'Re ZERO, Starting Life in Another World', + alternativeTitle: 'ZERO, Starting Life in Another World', + year: 2016, + season: 2, + episode: 1, + source: 'guessit', + }, + }); + + assert.notEqual(secondSeasonKey, firstSeasonKey); + assert.equal(await store.getOverride(secondSeasonKey), null); +}); diff --git a/src/main/character-dictionary-runtime/manual-selection.ts b/src/main/character-dictionary-runtime/manual-selection.ts index c2d29587..d401eb73 100644 --- a/src/main/character-dictionary-runtime/manual-selection.ts +++ b/src/main/character-dictionary-runtime/manual-selection.ts @@ -31,6 +31,29 @@ function normalizeSeriesKeyPart(value: string): string { .toLowerCase(); } +function getMediaDirectoryKey(mediaPath: string | null): string { + const rawPath = mediaPath?.trim(); + if (!rawPath) return ''; + + if (/^[a-zA-Z][a-zA-Z\d+.-]*:\/\//.test(rawPath) || rawPath.startsWith('file:')) { + try { + const url = new URL(rawPath); + const directoryPath = path.posix.dirname( + decodeURIComponent(url.pathname).replace(/\\/g, '/'), + ); + const scopedPath = `${url.hostname}${directoryPath === '/' ? '' : directoryPath}`; + return normalizeSeriesKeyPart(scopedPath); + } catch { + return ''; + } + } + + const normalizedPath = rawPath.replace(/\\/g, '/'); + const directoryPath = path.posix.dirname(normalizedPath); + if (!directoryPath || directoryPath === '.') return ''; + return normalizeSeriesKeyPart(directoryPath); +} + function dedupeNumbers(values: number[]): number[] { const seen = new Set(); const result: number[] = []; @@ -78,6 +101,12 @@ function writeOverrides(filePath: string, overrides: CharacterDictionaryManualSe fs.writeFileSync(filePath, JSON.stringify({ overrides }, null, 2), 'utf8'); } +function getLegacySeriesKeyCandidates(seriesKey: string): string[] { + const scopedSeparatorIndex = seriesKey.indexOf('--'); + if (scopedSeparatorIndex < 0) return [seriesKey]; + return [seriesKey, seriesKey.slice(scopedSeparatorIndex + 2)]; +} + export function buildCharacterDictionarySeriesKey(input: { mediaPath: string | null; mediaTitle: string | null; @@ -94,7 +123,9 @@ export function buildCharacterDictionarySeriesKey(input: { .replace(/\bepisode\s+\d+\b/gi, ' ') .trim(); const base = normalizeSeriesKeyPart(withoutEpisode) || 'unknown'; - return input.guess?.year ? `${base}-${input.guess.year}` : base; + const directoryKey = getMediaDirectoryKey(input.mediaPath); + const scopedBase = directoryKey ? `${directoryKey}--${base}` : base; + return input.guess?.year ? `${scopedBase}-${input.guess.year}` : scopedBase; } export function createCharacterDictionaryManualSelectionStore(deps: { userDataPath: string }) { @@ -102,7 +133,13 @@ export function createCharacterDictionaryManualSelectionStore(deps: { userDataPa return { getOverride: async (seriesKey: string): Promise => { - return readOverrides(filePath).find((entry) => entry.seriesKey === seriesKey) ?? null; + const candidates = getLegacySeriesKeyCandidates(seriesKey); + const overrides = readOverrides(filePath); + for (const candidate of candidates) { + const match = overrides.find((entry) => entry.seriesKey === candidate); + if (match) return match; + } + return null; }, setOverride: async (selection: CharacterDictionaryManualSelection): Promise => { const normalized = normalizeOverride(selection); diff --git a/src/main/character-dictionary-runtime/snapshot-refresh.test.ts b/src/main/character-dictionary-runtime/snapshot-refresh.test.ts new file mode 100644 index 00000000..bf488d3a --- /dev/null +++ b/src/main/character-dictionary-runtime/snapshot-refresh.test.ts @@ -0,0 +1,157 @@ +import assert from 'node:assert/strict'; +import * as fs from 'fs'; +import * as os from 'os'; +import * as path from 'path'; +import test from 'node:test'; + +import { createCharacterDictionaryRuntimeService } from '../character-dictionary-runtime'; +import { getSnapshotPath, writeSnapshot } from './cache'; +import { CHARACTER_DICTIONARY_FORMAT_VERSION } from './constants'; +import type { CharacterDictionarySnapshot } from './types'; + +const GRAPHQL_URL = 'https://graphql.anilist.co'; +const PNG_1X1 = Buffer.from( + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+nmX8AAAAASUVORK5CYII=', + 'base64', +); + +function makeTempDir(): string { + return fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-character-dictionary-')); +} + +function createSnapshotWithoutImages(): CharacterDictionarySnapshot { + return { + formatVersion: CHARACTER_DICTIONARY_FORMAT_VERSION, + mediaId: 130298, + mediaTitle: 'The Eminence in Shadow', + entryCount: 1, + updatedAt: 1_700_000_000_000, + termEntries: [['アレクシア', 'あれくしあ', 'name primary', '', 75, ['Alexia'], 0, '']], + images: [], + }; +} + +test('generateForCurrentMedia refreshes same-version snapshots missing images when inline images are enabled', async () => { + const userDataPath = makeTempDir(); + const outputDir = path.join(userDataPath, 'character-dictionaries'); + writeSnapshot(getSnapshotPath(outputDir, 130298), createSnapshotWithoutImages()); + const originalFetch = globalThis.fetch; + const fetchUrls: string[] = []; + + globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => { + const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; + fetchUrls.push(url); + + if (url === GRAPHQL_URL) { + const body = JSON.parse(String(init?.body ?? '{}')) as { + query?: string; + }; + if (body.query?.includes('characters(page: $page')) { + return new Response( + JSON.stringify({ + data: { + Media: { + title: { + english: 'The Eminence in Shadow', + }, + characters: { + pageInfo: { hasNextPage: false }, + edges: [ + { + role: 'SUPPORTING', + node: { + id: 123, + description: 'Alexia Midgar.', + image: { + large: 'https://cdn.example.com/character-123.png', + medium: null, + }, + name: { + full: 'Alexia Midgar', + native: 'アレクシア・ミドガル', + }, + }, + }, + ], + }, + }, + }, + }), + { status: 200, headers: { 'content-type': 'application/json' } }, + ); + } + } + + if (url === 'https://cdn.example.com/character-123.png') { + return new Response(PNG_1X1, { + status: 200, + headers: { 'content-type': 'image/png' }, + }); + } + + throw new Error(`Unexpected fetch URL: ${url}`); + }) as typeof globalThis.fetch; + + try { + const runtime = createCharacterDictionaryRuntimeService({ + userDataPath, + getCurrentMediaPath: () => '/tmp/eminence-s01e05.mkv', + getCurrentMediaTitle: () => 'The Eminence in Shadow - S01E05', + resolveMediaPathForJimaku: (mediaPath) => mediaPath, + guessAnilistMediaInfo: async () => ({ + title: 'The Eminence in Shadow', + season: null, + episode: 5, + source: 'fallback', + }), + getNameMatchImagesEnabled: () => true, + now: () => 1_700_000_000_500, + }); + + const result = await runtime.generateForCurrentMedia(); + const refreshedSnapshot = JSON.parse( + fs.readFileSync(getSnapshotPath(outputDir, 130298), 'utf8'), + ) as CharacterDictionarySnapshot; + + assert.equal(result.fromCache, false); + assert.ok(fetchUrls.includes(GRAPHQL_URL)); + assert.ok(refreshedSnapshot.images.some((image) => image.path === 'img/m130298-c123.png')); + } finally { + globalThis.fetch = originalFetch; + } +}); + +test('generateForCurrentMedia keeps same-version snapshots without images when inline images are disabled', async () => { + const userDataPath = makeTempDir(); + const outputDir = path.join(userDataPath, 'character-dictionaries'); + writeSnapshot(getSnapshotPath(outputDir, 130298), createSnapshotWithoutImages()); + const originalFetch = globalThis.fetch; + + globalThis.fetch = (async (input: string | URL | Request) => { + const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url; + throw new Error(`Unexpected fetch URL: ${url}`); + }) as typeof globalThis.fetch; + + try { + const runtime = createCharacterDictionaryRuntimeService({ + userDataPath, + getCurrentMediaPath: () => '/tmp/eminence-s01e05.mkv', + getCurrentMediaTitle: () => 'The Eminence in Shadow - S01E05', + resolveMediaPathForJimaku: (mediaPath) => mediaPath, + guessAnilistMediaInfo: async () => ({ + title: 'The Eminence in Shadow', + season: null, + episode: 5, + source: 'fallback', + }), + getNameMatchImagesEnabled: () => false, + now: () => 1_700_000_000_500, + }); + + const result = await runtime.generateForCurrentMedia(); + + assert.equal(result.fromCache, true); + } finally { + globalThis.fetch = originalFetch; + } +}); diff --git a/src/main/character-dictionary-runtime/snapshot.ts b/src/main/character-dictionary-runtime/snapshot.ts index a2e6a5ab..b4d2f62a 100644 --- a/src/main/character-dictionary-runtime/snapshot.ts +++ b/src/main/character-dictionary-runtime/snapshot.ts @@ -2,7 +2,12 @@ import type { AnilistCharacterDictionaryCollapsibleSectionKey } from '../../type import { CHARACTER_DICTIONARY_FORMAT_VERSION } from './constants'; import { createDefinitionGlossary } from './glossary'; import { generateNameReadings, splitJapaneseName } from './name-reading'; -import { buildNameTerms, buildReadingForTerm, buildTermEntry } from './term-building'; +import { + buildNameTerms, + buildReadingForTerm, + buildTermEntry, + buildVisibleNameTerms, +} from './term-building'; import type { CharacterDictionaryGlossaryEntry, CharacterDictionarySnapshot, @@ -40,14 +45,15 @@ export function buildSnapshotFromCharacters( const vaImg = imagesByVaId.get(va.id); if (vaImg) vaImagePaths.set(va.id, vaImg.path); } + const candidateTerms = buildNameTerms(character); const glossary = createDefinitionGlossary( character, mediaTitle, imagePath, vaImagePaths, + buildVisibleNameTerms(candidateTerms), getCollapsibleSectionOpenState, ); - const candidateTerms = buildNameTerms(character); const nameParts = splitJapaneseName( character.nativeName, character.firstNameHint, diff --git a/src/main/character-dictionary-runtime/term-building.ts b/src/main/character-dictionary-runtime/term-building.ts index 8b776c5d..b46ecdc9 100644 --- a/src/main/character-dictionary-runtime/term-building.ts +++ b/src/main/character-dictionary-runtime/term-building.ts @@ -41,25 +41,27 @@ function expandRawNameVariants(rawName: string): string[] { export function buildNameTerms(character: CharacterRecord): string[] { const base = new Set(); + const romanizedBase = new Set(); const rawNames = [character.nativeName, character.fullName, ...character.alternativeNames]; for (const rawName of rawNames) { for (const name of expandRawNameVariants(rawName)) { - base.add(name); + const target = isRomanizedName(name) ? romanizedBase : base; + target.add(name); const compact = name.replace(/[\s\u3000]+/g, ''); if (compact && compact !== name) { - base.add(compact); + target.add(compact); } const noMiddleDots = compact.replace(/[・・·•]/g, ''); if (noMiddleDots && noMiddleDots !== compact) { - base.add(noMiddleDots); + target.add(noMiddleDots); } const split = name.split(/[\s\u3000]+/).filter((part) => part.trim().length > 0); if (split.length === 2) { - base.add(split[0]!); - base.add(split[1]!); + target.add(split[0]!); + target.add(split[1]!); } const splitByMiddleDot = name @@ -68,12 +70,16 @@ export function buildNameTerms(character: CharacterRecord): string[] { .filter((part) => part.length > 0); if (splitByMiddleDot.length >= 2) { for (const part of splitByMiddleDot) { - base.add(part); + target.add(part); } } } } + for (const alias of addRomanizedKanaAliases(romanizedBase)) { + base.add(alias); + } + const nativeParts = splitJapaneseName( character.nativeName, character.firstNameHint, @@ -94,16 +100,24 @@ export function buildNameTerms(character: CharacterRecord): string[] { } } - for (const alias of addRomanizedKanaAliases(withHonorifics)) { - withHonorifics.add(alias); - for (const suffix of HONORIFIC_SUFFIXES) { - withHonorifics.add(`${alias}${suffix.term}`); - } - } - return [...withHonorifics].filter((entry) => entry.trim().length > 0); } +export function buildVisibleNameTerms(nameTerms: string[]): string[] { + const allTerms = new Set(nameTerms); + return nameTerms.filter((term) => { + for (const suffix of HONORIFIC_SUFFIXES) { + if (!term.endsWith(suffix.term) || term.length <= suffix.term.length) { + continue; + } + if (allTerms.has(term.slice(0, -suffix.term.length))) { + return false; + } + } + return true; + }); +} + export function buildReadingForTerm( term: string, character: CharacterRecord, diff --git a/src/main/character-dictionary-runtime/types.ts b/src/main/character-dictionary-runtime/types.ts index a12e20b2..0385c966 100644 --- a/src/main/character-dictionary-runtime/types.ts +++ b/src/main/character-dictionary-runtime/types.ts @@ -147,6 +147,7 @@ export interface CharacterDictionaryRuntimeDeps { sleep?: (ms: number) => Promise; logInfo?: (message: string) => void; logWarn?: (message: string) => void; + getNameMatchImagesEnabled?: () => boolean; getCollapsibleSectionOpenState?: ( section: AnilistCharacterDictionaryCollapsibleSectionKey, ) => boolean; diff --git a/src/main/runtime/config-hot-reload-handlers.ts b/src/main/runtime/config-hot-reload-handlers.ts index be0e5677..1d30313b 100644 --- a/src/main/runtime/config-hot-reload-handlers.ts +++ b/src/main/runtime/config-hot-reload-handlers.ts @@ -124,6 +124,8 @@ function hasAnnotationRuntimeHotReload(diff: ConfigHotReloadDiff): boolean { 'ankiConnect.knownWords', 'ankiConnect.nPlusOne', 'ankiConnect.fields.word', + 'subtitleStyle.nameMatchEnabled', + 'subtitleStyle.nameMatchImagesEnabled', ]); } diff --git a/src/main/runtime/subtitle-tokenization-main-deps.test.ts b/src/main/runtime/subtitle-tokenization-main-deps.test.ts index 7cfafe12..24b19b09 100644 --- a/src/main/runtime/subtitle-tokenization-main-deps.test.ts +++ b/src/main/runtime/subtitle-tokenization-main-deps.test.ts @@ -36,6 +36,9 @@ test('tokenizer deps builder records known-word lookups and maps readers', () => getJlptLevel: () => 'N2', getJlptEnabled: () => true, getNameMatchEnabled: () => false, + getNameMatchImagesEnabled: () => true, + getCharacterNameImage: (term) => + term === 'name' ? { src: 'data:image/png;base64,AAAA', alt: 'Name' } : null, getFrequencyDictionaryEnabled: () => true, getFrequencyDictionaryMatchMode: () => 'surface', getFrequencyRank: () => 5, @@ -52,6 +55,11 @@ test('tokenizer deps builder records known-word lookups and maps readers', () => assert.equal(deps.getNPlusOneEnabled?.(), true); assert.equal(deps.getMinSentenceWordsForNPlusOne?.(), 3); assert.equal(deps.getNameMatchEnabled?.(), false); + assert.equal(deps.getNameMatchImagesEnabled?.(), true); + assert.deepEqual(deps.getCharacterNameImage?.('name'), { + src: 'data:image/png;base64,AAAA', + alt: 'Name', + }); assert.equal(deps.getFrequencyDictionaryMatchMode?.(), 'surface'); assert.deepEqual(calls, ['lookup:true', 'lookup:false', 'set-window', 'set-ready', 'set-init']); }); @@ -74,6 +82,7 @@ test('tokenizer deps builder disables name matching when character dictionary is getJlptEnabled: () => true, getCharacterDictionaryEnabled: () => false, getNameMatchEnabled: () => true, + getNameMatchImagesEnabled: () => true, getFrequencyDictionaryEnabled: () => true, getFrequencyDictionaryMatchMode: () => 'surface', getFrequencyRank: () => 5, @@ -82,6 +91,7 @@ test('tokenizer deps builder disables name matching when character dictionary is })(); assert.equal(deps.getNameMatchEnabled?.(), false); + assert.equal(deps.getNameMatchImagesEnabled?.(), false); }); test('mecab tokenizer check creates tokenizer once and runs availability check', async () => { diff --git a/src/main/runtime/subtitle-tokenization-main-deps.ts b/src/main/runtime/subtitle-tokenization-main-deps.ts index a6dbd86f..faf536e9 100644 --- a/src/main/runtime/subtitle-tokenization-main-deps.ts +++ b/src/main/runtime/subtitle-tokenization-main-deps.ts @@ -4,6 +4,8 @@ type TokenizerMainDeps = TokenizerDepsRuntimeOptions & { getJlptEnabled: NonNullable; getCharacterDictionaryEnabled?: () => boolean; getNameMatchEnabled?: NonNullable; + getNameMatchImagesEnabled?: NonNullable; + getCharacterNameImage?: NonNullable; getFrequencyDictionaryEnabled: NonNullable< TokenizerDepsRuntimeOptions['getFrequencyDictionaryEnabled'] >; @@ -57,6 +59,17 @@ export function createBuildTokenizerDepsMainHandler(deps: TokenizerMainDeps) { deps.getCharacterDictionaryEnabled?.() !== false && deps.getNameMatchEnabled!(), } : {}), + ...(deps.getNameMatchImagesEnabled + ? { + getNameMatchImagesEnabled: () => + deps.getCharacterDictionaryEnabled?.() !== false && deps.getNameMatchImagesEnabled!(), + } + : {}), + ...(deps.getCharacterNameImage + ? { + getCharacterNameImage: (term: string) => deps.getCharacterNameImage!(term), + } + : {}), getFrequencyDictionaryEnabled: () => deps.getFrequencyDictionaryEnabled(), getFrequencyDictionaryMatchMode: () => deps.getFrequencyDictionaryMatchMode(), getFrequencyRank: (text: string) => deps.getFrequencyRank(text), diff --git a/src/preload.ts b/src/preload.ts index 3622c31d..51a9bd5a 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -413,8 +413,8 @@ const electronAPI: ElectronAPI = { request: YoutubePickerResolveRequest, ): Promise => ipcRenderer.invoke(IPC_CHANNELS.request.youtubePickerResolve, request), - getCharacterDictionarySelection: () => - ipcRenderer.invoke(IPC_CHANNELS.request.getCharacterDictionarySelection), + getCharacterDictionarySelection: (searchTitle?: string) => + ipcRenderer.invoke(IPC_CHANNELS.request.getCharacterDictionarySelection, searchTitle), setCharacterDictionarySelection: (mediaId: number) => ipcRenderer.invoke(IPC_CHANNELS.request.setCharacterDictionarySelection, mediaId), notifyOverlayModalClosed: (modal) => { diff --git a/src/renderer/handlers/keyboard.test.ts b/src/renderer/handlers/keyboard.test.ts index 443e2171..879422fd 100644 --- a/src/renderer/handlers/keyboard.test.ts +++ b/src/renderer/handlers/keyboard.test.ts @@ -681,6 +681,7 @@ test('numeric selection start focuses overlay for follow-up digit keys', async ( assert.equal(testGlobals.windowFocusCalls() > 0, true); assert.equal(testGlobals.overlayFocusCalls.length > 0, true); } finally { + testGlobals.dispatchKeydown({ key: 'Escape', code: 'Escape' }); testGlobals.restore(); } }); diff --git a/src/renderer/index.html b/src/renderer/index.html index 1bb59ba6..685f74f6 100644 --- a/src/renderer/index.html +++ b/src/renderer/index.html @@ -22,7 +22,7 @@ SubMiner @@ -205,6 +205,22 @@