Skip to content

fix: decode UTF-8 text previews as UTF-8 - #1566

Merged
CyanVoxel merged 1 commit into
TagStudioDev:mainfrom
kratos0718:fix-utf8-text-preview-encoding
Nov 13, 2026
Merged

CyanVoxel merged 1 commit into
TagStudioDev:mainfrom
kratos0718:fix-utf8-text-preview-encoding

Conversation

@kratos0718

Copy link
Copy Markdown
Contributor

Summary

Fixes #1564.

Text previews and thumbnails get their encoding from detect_char_encoding, which uses chardet. chardet gives UTF-8 a low confidence when a file has only a few multi-byte characters, so a short UTF-8 file with emoji comes back as Windows-1254 (Turkish) at 0.3-0.5 confidence, and each emoji is decoded as several Latin characters:

'Hello world 😀\n'          -> Windows-1254, confidence 0.30
'Grüße aus Köln 👋\n'        -> Windows-1254, confidence 0.41

detect_char_encoding now returns utf-8 whenever the file's bytes decode as UTF-8, and only asks chardet otherwise. Random bytes in a legacy single-byte encoding almost never form valid UTF-8, so files in Latin-1, Windows-125x and similar encodings still go to chardet as before. An empty file now returns utf-8 instead of None, which is what the text renderer already fell back to.

Tasks Completed

  • Platforms Tested:
    • macOS ARM
  • Tested For:
    • Basic functionality: new tests/core/test_encoding.py covers UTF-8 text with emoji (detected as utf-8 and round-trips) and a Latin-1 file (still detected by chardet and decodes correctly). I ran the detector against these cases with chardet 5.2; ruff format --check and ruff check pass on both files.

chardet gives UTF-8 a low confidence when a file has only a few multi-byte
characters, so a short file with emoji was detected as Windows-1254 and
its preview showed mojibake. Use UTF-8 whenever the bytes decode as
UTF-8, and fall back to chardet otherwise.
@TrigamDev TrigamDev added Type: UI/UX User interface and/or user experience Type: File System File system interactions TagStudio: Thumbs/Previews File thumbnails or previews Type: Fix A fix for a bug, typo, or other issue and removed Type: UI/UX User interface and/or user experience labels Nov 12, 2026
@TrigamDev

Copy link
Copy Markdown
Collaborator

Checked and this fixes my issue, the emojis are now encoded correctly (though they aren't able to be rendered, but that's a different issue).

@CyanVoxel CyanVoxel added this to the Alpha v9.7.0 milestone Nov 13, 2026

@CyanVoxel CyanVoxel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the fix!

@CyanVoxel
CyanVoxel merged commit 935cc8a into TagStudioDev:main Nov 13, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

TagStudio: Thumbs/Previews File thumbnails or previews Type: File System File system interactions Type: Fix A fix for a bug, typo, or other issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Emojis aren't encoded correctly in text file previews

3 participants