Skip to content

Commit 36943cc

Browse files
committed
Update handling of subtitle tags
1 parent d2793f8 commit 36943cc

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

Assets/USharpVideoSubtitles/Scripts/SubtitleManager.asset

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,12 @@ MonoBehaviour:
509509
Data: 32|UnityEngine.TooltipAttribute, UnityEngine.CoreModule
510510
- Name: tooltip
511511
Entry: 1
512-
Data: 'Removes unsupported HTML tags as well as {\anX} and {\aX} tags
512+
Data: 'Removes "{\" tags, unsupported HTML tags and replaces "\n" with actual
513+
new lines
513514
514-
Disabling
515-
this will speed up processing of huge files - you should only disable this
516-
if you''re building custom integration and not allowing people to load their
517-
own subtitles'
515+
Disabling this will speed up processing of huge files
516+
- you should only disable this if you''re building custom integration and
517+
not allowing people to load their own subtitles'
518518
- Name:
519519
Entry: 8
520520
Data:

Assets/USharpVideoSubtitles/Scripts/SubtitleManager.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class SubtitleManager : UdonSharpBehaviour
5050
[SerializeField, Tooltip("When false then only the master can manage the subtitles\nThis setting does nothing when using USharpVideo as the lock state is inherited from it")]
5151
private bool defaultUnlocked = true;
5252

53-
[Tooltip("Removes unsupported HTML tags as well as {\\anX} and {\\aX} tags\nDisabling this will speed up processing of huge files - you should only disable this if you're building custom integration and not allowing people to load their own subtitles")]
53+
[Tooltip("Removes \"{\\\" tags, unsupported HTML tags and replaces \"\\n\" with actual new lines\nDisabling this will speed up processing of huge files - you should only disable this if you're building custom integration and not allowing people to load their own subtitles")]
5454
public bool filterSubtitles = true;
5555

5656
[UdonSynced]
@@ -478,12 +478,12 @@ private bool ParseSubtitles(string text)
478478
}
479479
else if (parserState == 1 && line != "")
480480
{
481-
_dataText[currentIndex] = filterSubtitles ? FilterSubtitle(line) : line;
481+
_dataText[currentIndex] = HandleTextNewLine(filterSubtitles ? FilterSubtitle(line) : line);
482482
parserState = 2;
483483
}
484484
else if (parserState == 2 && line != "")
485485
{
486-
_dataText[currentIndex] += "\n" + (filterSubtitles ? FilterSubtitle(line) : line);
486+
_dataText[currentIndex] += "\n" + HandleTextNewLine(filterSubtitles ? FilterSubtitle(line) : line);
487487
}
488488
else if (parserState != 0 && line == "")
489489
{
@@ -541,54 +541,61 @@ private float ParseTimestamp(string timestamp)
541541
}
542542

543543
private string FilterSubtitle(string text)
544-
{
545-
if (text.Contains(@"{\an"))
546-
text = text.Substring(text.IndexOf(@"{\an") + 6).TrimStart(' ');
547-
548-
if (text.Contains(@"{\a"))
549-
text = text.Substring(text.IndexOf(@"{\a") + 5).TrimStart(' ');
550-
551-
text = FilterHTMLTags(text);
552-
553-
return text;
554-
}
555-
556-
private string FilterHTMLTags(string text)
557544
{
558545
char[] textArray = text.ToCharArray();
559546
text = "";
560547

561-
char[] allowedShortTags = { 'b', 'i', 'u' };
548+
char[] allowedShortHTMLTags = { 'b', 'i', 'u' };
562549

563550
bool inHtmlTag = false;
551+
bool inTag = false;
564552
for (int i = 0; i < textArray.Length; i++)
565553
{
566-
if (textArray[i] == '<' && i + 2 < textArray.Length)
554+
if (!inHtmlTag && textArray[i] == '{' && i + 1 < textArray.Length && textArray[i + 1] == '\\')
555+
{
556+
inTag = true;
557+
continue;
558+
}
559+
else if (!inHtmlTag && inTag)
560+
{
561+
if (textArray[i] == '}')
562+
inTag = false;
563+
564+
continue;
565+
}
566+
else if (!inTag && textArray[i] == '<' && i + 1 < textArray.Length)
567567
{
568568
bool isEndingTag = textArray[i + 1] == '/';
569569
bool isShortTag = isEndingTag ? textArray[i + 3] == '>' : textArray[i + 2] == '>';
570570
char shortTagValue = isShortTag ? (isEndingTag ? textArray[i + 2] : textArray[i + 1]) : ' ';
571571

572-
if (!isShortTag || Array.IndexOf(allowedShortTags, shortTagValue) == -1)
572+
if (!isShortTag || Array.IndexOf(allowedShortHTMLTags, shortTagValue) == -1)
573573
{
574574
inHtmlTag = true;
575575
continue;
576576
}
577577
}
578-
else if (inHtmlTag)
578+
else if (!inTag && inHtmlTag)
579579
{
580580
if (textArray[i] == '>')
581581
inHtmlTag = false;
582582

583583
continue;
584-
}
584+
}
585585

586586
text += textArray[i];
587587
}
588588

589589
return text;
590590
}
591591

592+
private string HandleTextNewLine(string text)
593+
{
594+
text = text.Replace("\\n", "\n").Replace("\\N", "\n");
595+
596+
return text;
597+
}
598+
592599
public void ProcessInput(string input)
593600
{
594601
if (!_isLocal && !CanControlSubtitles())

0 commit comments

Comments
 (0)