Work on WebVTT

This commit is contained in:
niksedk 2023-06-26 21:08:17 +02:00
parent 36b8a772b7
commit 2629e8be68
4 changed files with 154 additions and 33 deletions

View File

@ -5,6 +5,7 @@ using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Net.Mime.MediaTypeNames;
namespace Nikse.SubtitleEdit.Core.Common
{
@ -280,30 +281,11 @@ namespace Nikse.SubtitleEdit.Core.Common
}
}
public static WebVttStyle GetStyleFromColor(Color color, Subtitle webVttSubtitle)
{
foreach (var style in GetStyles(webVttSubtitle.Header))
{
if (style.Color.HasValue && style.Color.Value == color &&
style.BackgroundColor == null &&
style.Bold == null &&
style.Italic == null &&
style.FontName == null &&
style.FontSize == null &&
style.Underline == null)
{
return style;
}
}
return null;
}
public static WebVttStyle AddStyleFromColor(Color color)
{
return new WebVttStyle
{
Name = Utilities.ColorToHexWithTransparency(color).TrimStart('#'),
Name = "." + Utilities.ColorToHexWithTransparency(color).TrimStart('#'),
Color = color,
};
}
@ -317,7 +299,7 @@ namespace Nikse.SubtitleEdit.Core.Common
return "WEBVTT" + Environment.NewLine + Environment.NewLine + "STYLE" + Environment.NewLine + rawStyle;
}
if (header.Contains("::cue(." + style.Name + ")"))
if (header.Contains("::cue(." + style.Name.RemoveChar('.') + ")"))
{
return header;
}
@ -433,10 +415,10 @@ namespace Nikse.SubtitleEdit.Core.Common
public static string AddStyleToText(string input, WebVttStyle style)
{
var text = input;
var idx = text.IndexOf("<c." + style.Name + ">", StringComparison.Ordinal);
var idx = text.IndexOf("<c." + style.Name.TrimStart('.') + ">", StringComparison.Ordinal);
if (idx >= 0)
{
text = text.Replace("<c." + style.Name + ">", string.Empty);
text = text.Replace("<c." + style.Name.TrimStart('.') + ">", string.Empty);
idx = text.IndexOf("</c>", StringComparison.Ordinal);
if (idx >= 0)
{
@ -445,11 +427,17 @@ namespace Nikse.SubtitleEdit.Core.Common
}
else if (text.Contains("<c."))
{
text = text.Replace("." + style.Name, string.Empty);
var regex = new Regex(@"<c\.[\.a-zA-Z\d#_-]+>");
var match = regex.Match(text);
if (match.Success)
{
// todo: remove only color styles
text = text.Insert(match.Index + match.Length-1, "." + style.Name.TrimStart('.'));
}
}
else
{
text = "<c." + style.Name + ">" + text + "</c>";
text = "<c." + style.Name.TrimStart('.') + ">" + text + "</c>";
}
return text;
@ -507,5 +495,93 @@ namespace Nikse.SubtitleEdit.Core.Common
return prefix + text + "</c>";
}
public static string RemoveUnusedColorStylesFromText(string input, string header)
{
if (string.IsNullOrEmpty(header) || !header.Contains("WEBVTT"))
{
return input;
}
var styles = GetStyles(header);
if (styles.Count <= 1)
{
return input;
}
var regex = new Regex(@"<c\.[\.a-zA-Z\d#_-]+>");
var match = regex.Match(input);
if (!match.Success)
{
return input;
}
var text = input;
var styleNames = match.Value.Remove(0,3).Trim('>').Split('.');
var colorsOnly = new List<string>();
foreach (var styleName in styleNames)
{
var style = styles.FirstOrDefault(p => p.Name == "." + styleName);
if (style != null &&
style.Color.HasValue &&
style.Bold == null &&
style.Italic == null &&
style.FontName == null &&
style.FontSize == null &&
style.ShadowColor == null &&
style.BackgroundColor == null &&
style.Underline == null &&
style.StrikeThrough == null &&
style.StrikeThrough == null)
{
colorsOnly.Add(styleName);
}
}
while (colorsOnly.Count > 1)
{
var name = colorsOnly[0];
text = text.Replace("." + name + ".", ".");
text = text.Replace("." + name + ">", ">");
colorsOnly.RemoveAt(0);
}
return text;
}
public static WebVttStyle GetOnlyColorStyle(Color color, string header)
{
if (string.IsNullOrEmpty(header) || !header.Contains("WEBVTT"))
{
return null;
}
var styles = GetStyles(header);
if (styles.Count <= 1)
{
return null;
}
foreach (var style in styles)
{
if (style != null &&
style.Color.HasValue &&
style.Bold == null &&
style.Italic == null &&
style.FontName == null &&
style.FontSize == null &&
style.ShadowColor == null &&
style.BackgroundColor == null &&
style.Underline == null &&
style.StrikeThrough == null &&
style.StrikeThrough == null &&
style.Color == color)
{
return style;
}
}
return null;
}
}
}

View File

@ -14037,7 +14037,7 @@ namespace Nikse.SubtitleEdit.Forms
else if (format.Name == WebVTT.NameOfFormat || format.Name == WebVTTFileWithLineNumber.NameOfFormat)
{
var c = ColorTranslator.FromHtml(color);
WebVttStyle styleWithColor = WebVttHelper.GetStyleFromColor(c, _subtitle);
WebVttStyle styleWithColor = WebVttHelper.GetOnlyColorStyle(c, _subtitle.Header);
if (styleWithColor == null)
{
styleWithColor = WebVttHelper.AddStyleFromColor(c);
@ -14049,14 +14049,16 @@ namespace Nikse.SubtitleEdit.Forms
var indexOfEndTag = text.IndexOf('>');
if (indexOfEndTag > 0)
{
text = text.Insert(indexOfEndTag, "." + styleWithColor.Name);
text = text.Insert(indexOfEndTag, "." + styleWithColor.Name.RemoveChar('.'));
}
}
else
{
text = "<c." + styleWithColor.Name + ">" + text + "</c>";
text = "<c." + styleWithColor.Name.RemoveChar('.') + ">" + text + "</c>";
}
text = WebVttHelper.RemoveUnusedColorStylesFromText(text, _subtitle.Header);
tb.SelectedText = text;
tb.SelectionStart = selectionStart;
tb.SelectionLength = text.Length;
@ -14154,9 +14156,19 @@ namespace Nikse.SubtitleEdit.Forms
try
{
var c = ColorTranslator.FromHtml(color);
var styleWithColor = WebVttHelper.AddStyleFromColor(c);
subtitle.Header = WebVttHelper.AddStyleToHeader(_subtitle.Header, styleWithColor);
p.Text = WebVttHelper.AddStyleToText(p.Text, styleWithColor);
var existingStyle = WebVttHelper.GetOnlyColorStyle(c, _subtitle.Header);
if (existingStyle != null)
{
p.Text = WebVttHelper.AddStyleToText(p.Text, existingStyle);
p.Text = WebVttHelper.RemoveUnusedColorStylesFromText(p.Text, subtitle.Header);
}
else
{
var styleWithColor = WebVttHelper.AddStyleFromColor(c);
subtitle.Header = WebVttHelper.AddStyleToHeader(_subtitle.Header, styleWithColor);
p.Text = WebVttHelper.AddStyleToText(p.Text, styleWithColor);
p.Text = WebVttHelper.RemoveUnusedColorStylesFromText(p.Text, subtitle.Header);
}
}
catch
{
@ -27250,7 +27262,7 @@ namespace Nikse.SubtitleEdit.Forms
{
string color;
var formatType = GetCurrentSubtitleFormat().GetType();
if (formatType == typeof(AdvancedSubStationAlpha))
if (formatType == typeof(AdvancedSubStationAlpha) || formatType == typeof(WebVTT) || formatType == typeof(WebVTTFileWithLineNumber))
{
using (var form = new ColorChooser { Color = Configuration.Settings.General.LastColorPickerColor })
{

View File

@ -39,7 +39,9 @@
this.saveFileDialogStyle = new System.Windows.Forms.SaveFileDialog();
this.labelStyles = new System.Windows.Forms.Label();
this.groupBoxStyle = new System.Windows.Forms.GroupBox();
this.labelStyle = new System.Windows.Forms.Label();
this.contextMenuStrip1.SuspendLayout();
this.groupBoxStyle.SuspendLayout();
this.SuspendLayout();
//
// listViewExportStyles
@ -58,6 +60,7 @@
this.listViewExportStyles.TabIndex = 0;
this.listViewExportStyles.UseCompatibleStateImageBehavior = false;
this.listViewExportStyles.View = System.Windows.Forms.View.Details;
this.listViewExportStyles.SelectedIndexChanged += new System.EventHandler(this.listViewExportStyles_SelectedIndexChanged);
//
// contextMenuStrip1
//
@ -120,6 +123,7 @@
//
// groupBoxStyle
//
this.groupBoxStyle.Controls.Add(this.labelStyle);
this.groupBoxStyle.Location = new System.Drawing.Point(201, 38);
this.groupBoxStyle.Name = "groupBoxStyle";
this.groupBoxStyle.Size = new System.Drawing.Size(319, 187);
@ -127,7 +131,16 @@
this.groupBoxStyle.TabStop = false;
this.groupBoxStyle.Text = "Style";
//
// WebVttIStylePicker
// labelStyle
//
this.labelStyle.AutoSize = true;
this.labelStyle.Location = new System.Drawing.Point(7, 20);
this.labelStyle.Name = "labelStyle";
this.labelStyle.Size = new System.Drawing.Size(52, 13);
this.labelStyle.TabIndex = 0;
this.labelStyle.Text = "labelStyle";
//
// WebVttStylePicker
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
@ -148,6 +161,8 @@
this.Text = "SubStationAlphaStylesExport";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.WebVttImportExport_KeyDown);
this.contextMenuStrip1.ResumeLayout(false);
this.groupBoxStyle.ResumeLayout(false);
this.groupBoxStyle.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
@ -165,5 +180,6 @@
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemSelectAll;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemInverseSelection;
private System.Windows.Forms.GroupBox groupBoxStyle;
private System.Windows.Forms.Label labelStyle;
}
}

View File

@ -30,6 +30,8 @@ namespace Nikse.SubtitleEdit.Forms.VTT
toolStripMenuItemSelectAll.Text = LanguageSettings.Current.Main.Menu.ContextMenu.SelectAll;
buttonOK.Text = LanguageSettings.Current.General.Ok;
buttonCancel.Text = LanguageSettings.Current.General.Cancel;
labelStyle.Text = string.Empty;
}
private void buttonOK_Click(object sender, EventArgs e)
@ -68,5 +70,20 @@ namespace Nikse.SubtitleEdit.Forms.VTT
item.Checked = !item.Checked;
}
}
private void listViewExportStyles_SelectedIndexChanged(object sender, EventArgs e)
{
var x = listViewExportStyles.SelectedIndices;
if (x.Count != 1)
{
labelStyle.Text = string.Empty;
return;
}
var idx = x[0];
var style = (WebVttStyle)listViewExportStyles.Items[idx].Tag;
labelStyle.Text = style.ToString().Replace("; ", ";" + Environment.NewLine);
}
}
}