From d8388dc664539c7a0f43ec7ec9d5ba21b62ded6d Mon Sep 17 00:00:00 2001 From: Brian Matherly Date: Sat, 30 May 2026 22:57:26 -0500 Subject: [PATCH] Fix Full-width spaces replaced by half-width spaces when importing subtitles #1840 Some white space characters (like U+3000) were bring removed or converted by the html conversion and simplified() Simplified was replaced with trimmed(). Html conversion is avoided unless we are certain that the line has HTML. --- src/docks/subtitlesdock.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/docks/subtitlesdock.cpp b/src/docks/subtitlesdock.cpp index 5a0eb194fe..055d30fe9d 100644 --- a/src/docks/subtitlesdock.cpp +++ b/src/docks/subtitlesdock.cpp @@ -84,6 +84,11 @@ static QList readSrtFile(const QString &path, return items; } + static const QRegularExpression htmlTagPattern( + QStringLiteral("]*)?/?>")); + static const QRegularExpression htmlEntityPattern( + QStringLiteral("&(?:[A-Za-z][A-Za-z0-9]+|#[0-9]+|#x[0-9A-Fa-f]+);")); + // Convert the items to the return list Subtitles::SubtitleItem item; for (int i = 0; i < srtItems.size(); i++) { @@ -91,13 +96,18 @@ static QList readSrtFile(const QString &path, QStringList lines = QString::fromStdString(srtItems[i].text).split('\n'); QString text; for (int i = 0; i < lines.size(); i++) { + QString line = lines[i]; // Remove HTML - QString line = QTextDocumentFragment::fromHtml(lines[i]).toPlainText(); + bool hasHtmlSyntax = Qt::mightBeRichText(line) || htmlTagPattern.match(line).hasMatch() + || htmlEntityPattern.match(line).hasMatch(); + if (hasHtmlSyntax) { + line = QTextDocumentFragment::fromHtml(line).toPlainText(); + } // Remove unnecessary space - line = line.simplified(); + line = line.trimmed(); // Remove leading "-" if (line.startsWith("-")) { - line = line.remove(0, 1).simplified(); + line = line.remove(0, 1).trimmed(); } // Remove unspoken sounds if requested if (!includeNonspoken