private void buttonTrain_Click(object sender, EventArgs e) { if (!System.IO.File.Exists(textBoxInputFile.Text)) { return; } int numberOfCharactersLeaned = 0; int numberOfCharactersSkipped = 0; var nOcrD = new NOcrDb(textBoxNOcrDb.Text); var lines = new List<string>(); foreach (string line in System.IO.File.ReadAllLines(textBoxInputFile.Text)) lines.Add(line); var format = new SubRip(); var sub = new Subtitle(); format.LoadSubtitle(sub, lines, textBoxInputFile.Text); var charactersLearned = new List<string>(); foreach (ListViewItem item in listViewFonts.Items) { if (item.Checked) { _subtitleFontName = item.Text; _subtitleFontSize = Convert.ToInt32(comboBoxSubtitleFontSize.Items[comboBoxSubtitleFontSize.SelectedIndex].ToString()); charactersLearned = new List<string>(); foreach (Paragraph p in sub.Paragraphs) { foreach (char ch in p.Text) { string s = ch.ToString(); if (s.Trim().Length > 0) { if (!charactersLearned.Contains(s)) { TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, false); if (checkBoxBold.Checked) TrainLetter(ref numberOfCharactersLeaned, ref numberOfCharactersSkipped, nOcrD, charactersLearned, s, true); } } } } } } nOcrD.Save(); }
private void toolStripMenuItemPasteSpecial_Click(object sender, EventArgs e) { string text = Clipboard.GetText(); var tmp = new Subtitle(); var format = new SubRip(); var list = new List<string>(); foreach (string line in text.Replace(Environment.NewLine, "|").Split("|".ToCharArray(), StringSplitOptions.None)) list.Add(line); format.LoadSubtitle(tmp, list, null); if (SubtitleListview1.SelectedItems.Count == 1 && text.Length > 0) { var form = new ColumnPaste(SubtitleListview1.IsAlternateTextColumnVisible && _subtitleAlternate != null && Configuration.Settings.General.AllowEditOfOriginalSubtitle, tmp.Paragraphs.Count == 0); if (form.ShowDialog(this) == DialogResult.OK) { MakeHistoryForUndo(_language.BeforeColumnPaste); if (tmp.Paragraphs.Count == 0) { foreach (string line in text.Replace(Environment.NewLine, "|").Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) tmp.Paragraphs.Add(new Paragraph(0, 0, line)); } int index = FirstSelectedIndex; if (!form.PasteOverwrite) { for (int i = 0; i < tmp.Paragraphs.Count; i++) { if (form.PasteAll) { for (int k = _subtitle.Paragraphs.Count - 2; k > index; k--) { _subtitle.Paragraphs[k + 1] = new Paragraph(_subtitle.Paragraphs[k]); } if (index + i < _subtitle.Paragraphs.Count) _subtitle.Paragraphs[index + i].Text = string.Empty; } else if (form.PasteTimeCodesOnly) { for (int k = _subtitle.Paragraphs.Count - 2; k > index; k--) { _subtitle.Paragraphs[k + 1].StartTime.TotalMilliseconds = _subtitle.Paragraphs[k].StartTime.TotalMilliseconds; _subtitle.Paragraphs[k + 1].EndTime.TotalMilliseconds = _subtitle.Paragraphs[k].EndTime.TotalMilliseconds; _subtitle.Paragraphs[k + 1].StartFrame = _subtitle.Paragraphs[k].StartFrame; _subtitle.Paragraphs[k + 1].EndFrame = _subtitle.Paragraphs[k].EndFrame; } } else if (form.PasteTextOnly) { for (int k = _subtitle.Paragraphs.Count - 2; k > index; k--) { _subtitle.Paragraphs[k + 1].Text = _subtitle.Paragraphs[k].Text; } } else if (form.PasteOriginalTextOnly) { for (int k = _subtitle.Paragraphs.Count - 2; k > index; k--) { Paragraph original = Utilities.GetOriginalParagraph(index, _subtitle.Paragraphs[k], _subtitleAlternate.Paragraphs); Paragraph originalNext = Utilities.GetOriginalParagraph(index, _subtitle.Paragraphs[k + 1], _subtitleAlternate.Paragraphs); if (original != null) { originalNext.Text = original.Text; } } if (index + i < _subtitle.Paragraphs.Count) { Paragraph original = Utilities.GetOriginalParagraph(index, _subtitle.Paragraphs[index + i], _subtitleAlternate.Paragraphs); if (original != null) original.Text = string.Empty; } } } } if (form.PasteOverwrite) { for (int i = 0; i + index < _subtitle.Paragraphs.Count && i < tmp.Paragraphs.Count; i++) _subtitle.Paragraphs[index + i].Text = tmp.Paragraphs[i].Text; } else { for (int i = 0; i + index < _subtitle.Paragraphs.Count && i < tmp.Paragraphs.Count; i++) _subtitle.Paragraphs[index + i + 1].Text = tmp.Paragraphs[i].Text; } SubtitleListview1.Fill(_subtitle, _subtitleAlternate); SubtitleListview1.SelectIndexAndEnsureVisible(index, true); RefreshSelectedParagraph(); } } }
private void SubtitleListview1KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.C && e.Modifiers == Keys.Control) //Ctrl+c = Copy to clipboard { var tmp = new Subtitle(); foreach (int i in SubtitleListview1.SelectedIndices) { Paragraph p = _subtitle.GetParagraphOrDefault(i); if (p != null) tmp.Paragraphs.Add(new Paragraph(p)); } if (tmp.Paragraphs.Count > 0) { Clipboard.SetText(tmp.ToText(new SubRip())); } e.SuppressKeyPress = true; } else if (e.KeyData == _mainListViewCopyText) { StringBuilder sb = new StringBuilder(); foreach (int i in SubtitleListview1.SelectedIndices) { Paragraph p = _subtitle.GetParagraphOrDefault(i); if (p != null) sb.AppendLine(p.Text + Environment.NewLine); } if (sb.Length > 0) { Clipboard.SetText(sb.ToString().Trim()); } e.SuppressKeyPress = true; } else if (e.KeyData == _mainListViewAutoDuration) { MakeAutoDurationSelectedLines(); } else if (e.KeyData == _mainListViewFocusWaveform) { if (audioVisualizer.CanFocus) { audioVisualizer.Focus(); e.SuppressKeyPress = true; } } else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control) //Ctrl+vPaste from clipboard { if (Clipboard.ContainsText()) { string text = Clipboard.GetText(); var tmp = new Subtitle(); var format = new SubRip(); var list = new List<string>(); foreach (string line in text.Replace(Environment.NewLine, "|").Split("|".ToCharArray(), StringSplitOptions.None)) list.Add(line); format.LoadSubtitle(tmp, list, null); if (SubtitleListview1.SelectedItems.Count == 1 && tmp.Paragraphs.Count > 0) { MakeHistoryForUndo(_language.BeforeInsertLine); _makeHistoryPaused = true; Paragraph lastParagraph = null; Paragraph lastTempParagraph = null; foreach (Paragraph p in tmp.Paragraphs) { InsertAfter(); textBoxListViewText.Text = p.Text; if (lastParagraph != null && lastTempParagraph != null) { double millisecondsBetween = p.StartTime.TotalMilliseconds - lastTempParagraph.EndTime.TotalMilliseconds; timeUpDownStartTime.TimeCode = new TimeCode(TimeSpan.FromMilliseconds(lastParagraph.EndTime.TotalMilliseconds + millisecondsBetween)); } SetDurationInSeconds(p.Duration.TotalSeconds); lastParagraph = _subtitle.GetParagraphOrDefault(_subtitleListViewIndex); lastTempParagraph = p; } RestartHistory(); } else if (SubtitleListview1.Items.Count == 0 && tmp.Paragraphs.Count > 0) { // insert into empty subtitle MakeHistoryForUndo(_language.BeforeInsertLine); foreach (Paragraph p in tmp.Paragraphs) { _subtitle.Paragraphs.Add(p); } SubtitleListview1.Fill(_subtitle, _subtitleAlternate); SubtitleListview1.SelectIndexAndEnsureVisible(0, true); } else if (list.Count > 1 && list.Count < 2000) { MakeHistoryForUndo(_language.BeforeInsertLine); _makeHistoryPaused = true; foreach (string line in list) { if (line.Trim().Length > 0) { InsertAfter(); textBoxListViewText.Text = Utilities.AutoBreakLine(line); } } RestartHistory(); } } e.SuppressKeyPress = true; } else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control) //Ctrl+X = Cut to clipboard { var tmp = new Subtitle(); foreach (int i in SubtitleListview1.SelectedIndices) { Paragraph p = _subtitle.GetParagraphOrDefault(i); if (p != null) tmp.Paragraphs.Add(new Paragraph(p)); } e.SuppressKeyPress = true; _cutText = tmp.ToText(new SubRip()); ToolStripMenuItemDeleteClick(null, null); } else if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control) //SelectAll { foreach (ListViewItem item in SubtitleListview1.Items) item.Selected = true; e.SuppressKeyPress = true; } else if (e.KeyCode == Keys.D && e.Modifiers == Keys.Control) //SelectFirstSelectedItemOnly { if (SubtitleListview1.SelectedItems.Count > 0) { bool skipFirst = true; foreach (ListViewItem item in SubtitleListview1.SelectedItems) { if (skipFirst) skipFirst = false; else item.Selected = false; } e.SuppressKeyPress = true; } } else if (e.KeyCode == Keys.Delete && SubtitleListview1.SelectedItems.Count > 0) //Delete { ToolStripMenuItemDeleteClick(null, null); } else if (e.KeyData == _mainInsertBefore) { InsertBefore(); e.SuppressKeyPress = true; } else if (e.KeyData == _mainInsertAfter) { InsertAfter(); e.SuppressKeyPress = true; } else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Home) { SubtitleListview1.FirstVisibleIndex = -1; SubtitleListview1.SelectIndexAndEnsureVisible(0, true); e.SuppressKeyPress = true; } else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.End) { SubtitleListview1.SelectIndexAndEnsureVisible(SubtitleListview1.Items.Count - 1, true); e.SuppressKeyPress = true; } else if (e.Modifiers == Keys.None && e.KeyCode == Keys.Enter) { SubtitleListview1_MouseDoubleClick(null, null); } }
private void toolStripMenuItemPasteSpecial_Click(object sender, EventArgs e) { string text = Clipboard.GetText(); var tmp = new Subtitle(); var format = new SubRip(); var list = new List<string>(text.SplitToLines()); format.LoadSubtitle(tmp, list, null); if (this.SubtitleListview1.SelectedItems.Count == 1 && text.Length > 0) { var form = new ColumnPaste(this.SubtitleListview1.IsAlternateTextColumnVisible && this._subtitleAlternate != null && Configuration.Settings.General.AllowEditOfOriginalSubtitle, tmp.Paragraphs.Count == 0); if (form.ShowDialog(this) == DialogResult.OK) { this.MakeHistoryForUndo(this._language.BeforeColumnPaste); if (tmp.Paragraphs.Count == 0) { foreach (var line in text.SplitToLines()) { tmp.Paragraphs.Add(new Paragraph(0, 0, line)); } } int index = this.FirstSelectedIndex; if (!form.PasteOverwrite) { for (int i = 0; i < tmp.Paragraphs.Count; i++) { if (form.PasteAll) { for (int k = this._subtitle.Paragraphs.Count - 2; k > index; k--) { this._subtitle.Paragraphs[k + 1] = new Paragraph(this._subtitle.Paragraphs[k]); } if (index + i < this._subtitle.Paragraphs.Count) { this._subtitle.Paragraphs[index + i].Text = string.Empty; } } else if (form.PasteTimeCodesOnly) { for (int k = this._subtitle.Paragraphs.Count - 2; k > index; k--) { this._subtitle.Paragraphs[k + 1].StartTime.TotalMilliseconds = this._subtitle.Paragraphs[k].StartTime.TotalMilliseconds; this._subtitle.Paragraphs[k + 1].EndTime.TotalMilliseconds = this._subtitle.Paragraphs[k].EndTime.TotalMilliseconds; this._subtitle.Paragraphs[k + 1].StartFrame = this._subtitle.Paragraphs[k].StartFrame; this._subtitle.Paragraphs[k + 1].EndFrame = this._subtitle.Paragraphs[k].EndFrame; } } else if (form.PasteTextOnly) { for (int k = this._subtitle.Paragraphs.Count - 2; k > index; k--) { this._subtitle.Paragraphs[k + 1].Text = this._subtitle.Paragraphs[k].Text; } } else if (form.PasteOriginalTextOnly) { for (int k = this._subtitle.Paragraphs.Count - 2; k > index; k--) { var original = Utilities.GetOriginalParagraph(index, this._subtitle.Paragraphs[k], this._subtitleAlternate.Paragraphs); var originalNext = Utilities.GetOriginalParagraph(index, this._subtitle.Paragraphs[k + 1], this._subtitleAlternate.Paragraphs); if (original != null) { originalNext.Text = original.Text; } } if (index + i < this._subtitle.Paragraphs.Count) { var original = Utilities.GetOriginalParagraph(index, this._subtitle.Paragraphs[index + i], this._subtitleAlternate.Paragraphs); if (original != null) { original.Text = string.Empty; } } } } } if (form.PasteOverwrite) { for (int i = 0; i + index < this._subtitle.Paragraphs.Count && i < tmp.Paragraphs.Count; i++) { this._subtitle.Paragraphs[index + i].Text = tmp.Paragraphs[i].Text; } } else { for (int i = 0; i + index < this._subtitle.Paragraphs.Count && i < tmp.Paragraphs.Count; i++) { if (index + i + 1 < this._subtitle.Paragraphs.Count) { this._subtitle.Paragraphs[index + i + 1].Text = tmp.Paragraphs[i].Text; } } } this.SubtitleListview1.Fill(this._subtitle, this._subtitleAlternate); this.SubtitleListview1.SelectIndexAndEnsureVisible(index, true); this.RefreshSelectedParagraph(); } form.Dispose(); } }
private void SubtitleListview1KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.C && e.Modifiers == Keys.Control) { // Ctrl+c = Copy to clipboard var tmp = new Subtitle(); foreach (int i in this.SubtitleListview1.SelectedIndices) { var p = this._subtitle.GetParagraphOrDefault(i); if (p != null) { tmp.Paragraphs.Add(new Paragraph(p)); } } if (tmp.Paragraphs.Count > 0) { Clipboard.SetText(tmp.ToText(new SubRip())); } e.SuppressKeyPress = true; } else if (e.KeyData == this._mainListViewCopyText) { var sb = new StringBuilder(); foreach (int i in this.SubtitleListview1.SelectedIndices) { var p = this._subtitle.GetParagraphOrDefault(i); if (p != null) { sb.AppendLine(p.Text + Environment.NewLine); } } if (sb.Length > 0) { Clipboard.SetText(sb.ToString().Trim()); } e.SuppressKeyPress = true; } else if (e.KeyData == this._mainListViewAutoDuration) { this.MakeAutoDurationSelectedLines(); } else if (e.KeyData == this._mainListViewFocusWaveform) { if (this.audioVisualizer.CanFocus) { this.audioVisualizer.Focus(); e.SuppressKeyPress = true; } } else if (e.KeyData == this._mainListViewGoToNextError) { this.GoToNextSynaxError(); e.SuppressKeyPress = true; } else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control) { // Ctrl+vPaste from clipboard if (Clipboard.ContainsText()) { var text = Clipboard.GetText(); var tmp = new Subtitle(); var format = new SubRip(); var list = new List<string>(text.SplitToLines()); format.LoadSubtitle(tmp, list, null); if (this.SubtitleListview1.SelectedItems.Count == 1 && tmp.Paragraphs.Count > 0) { this.MakeHistoryForUndo(this._language.BeforeInsertLine); this._makeHistoryPaused = true; Paragraph lastParagraph = null; Paragraph lastTempParagraph = null; foreach (var p in tmp.Paragraphs) { this.InsertAfter(); this.textBoxListViewText.Text = p.Text; if (lastParagraph != null) { double millisecondsBetween = p.StartTime.TotalMilliseconds - lastTempParagraph.EndTime.TotalMilliseconds; this.timeUpDownStartTime.TimeCode = new TimeCode(lastParagraph.EndTime.TotalMilliseconds + millisecondsBetween); } this.SetDurationInSeconds(p.Duration.TotalSeconds); lastParagraph = this._subtitle.GetParagraphOrDefault(this._subtitleListViewIndex); lastTempParagraph = p; } this.RestartHistory(); } else if (this.SubtitleListview1.Items.Count == 0 && tmp.Paragraphs.Count > 0) { // insert into empty subtitle this.MakeHistoryForUndo(this._language.BeforeInsertLine); foreach (var p in tmp.Paragraphs) { this._subtitle.Paragraphs.Add(p); } this.SubtitleListview1.Fill(this._subtitle, this._subtitleAlternate); this.SubtitleListview1.SelectIndexAndEnsureVisible(0, true); } else if (this.SubtitleListview1.Items.Count > 1 && tmp.Paragraphs.Count > 0) { // multiple lines selected - first delete, then insert int firstIndex = this.FirstSelectedIndex; if (firstIndex >= 0) { this.MakeHistoryForUndo(this._language.BeforeInsertLine); this._makeHistoryPaused = true; this.DeleteSelectedLines(); foreach (var p in tmp.Paragraphs) { this._subtitle.Paragraphs.Insert(firstIndex, p); firstIndex++; } this.SubtitleListview1.Fill(this._subtitle, this._subtitleAlternate); this.SubtitleListview1.SelectIndexAndEnsureVisible(0, true); this.RestartHistory(); } } else if (list.Count >= 1 && list.Count < 4) { // less than 4 lines of text, just insert into first selected this.textBoxListViewText.Text = text.Trim(); } else if (list.Count > 1 && list.Count < 2000) { this.MakeHistoryForUndo(this._language.BeforeInsertLine); this._makeHistoryPaused = true; foreach (var line in list) { if (!string.IsNullOrWhiteSpace(line)) { this.InsertAfter(); this.textBoxListViewText.Text = Utilities.AutoBreakLine(line); } } this.RestartHistory(); } } e.SuppressKeyPress = true; } else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control) { // Ctrl+X = Cut to clipboard var tmp = new Subtitle(); foreach (int i in this.SubtitleListview1.SelectedIndices) { var p = this._subtitle.GetParagraphOrDefault(i); if (p != null) { tmp.Paragraphs.Add(new Paragraph(p)); } } e.SuppressKeyPress = true; this._cutText = tmp.ToText(new SubRip()); this.ToolStripMenuItemDeleteClick(null, null); } else if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control) { // SelectAll foreach (ListViewItem item in this.SubtitleListview1.Items) { item.Selected = true; } e.SuppressKeyPress = true; } else if (e.KeyCode == Keys.D && e.Modifiers == Keys.Control) { // SelectFirstSelectedItemOnly if (this.SubtitleListview1.SelectedItems.Count > 0) { bool skipFirst = true; foreach (ListViewItem item in this.SubtitleListview1.SelectedItems) { if (skipFirst) { skipFirst = false; } else { item.Selected = false; } } e.SuppressKeyPress = true; } } else if (e.KeyCode == Keys.Delete && this.SubtitleListview1.SelectedItems.Count > 0) { // Delete this.ToolStripMenuItemDeleteClick(null, null); } else if (e.KeyData == this._mainInsertBefore) { this.InsertBefore(); e.SuppressKeyPress = true; } else if (e.KeyData == this._mainInsertAfter) { this.InsertAfter(); e.SuppressKeyPress = true; } else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Home) { this.SubtitleListview1.FirstVisibleIndex = -1; this.SubtitleListview1.SelectIndexAndEnsureVisible(0, true); e.SuppressKeyPress = true; } else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.End) { this.SubtitleListview1.SelectIndexAndEnsureVisible(this.SubtitleListview1.Items.Count - 1, true); e.SuppressKeyPress = true; } else if (e.Modifiers == Keys.None && e.KeyCode == Keys.Enter) { this.SubtitleListview1_MouseDoubleClick(null, null); } }
public void SrtNoLineNumbers() { var target = new SubRip(); var subtitle = new Subtitle(); const string text = @"00:00:03,000 --> 00:00:08,000 Line1. 00:00:08,000 --> 00:00:09,920 Line 2."; target.LoadSubtitle(subtitle, GetSrtLines(text), null); string actual = subtitle.Paragraphs.Count.ToString(CultureInfo.InvariantCulture); const string expected = "2"; Assert.AreEqual(expected, actual); }
public void SrtCoordinates() { var target = new SubRip(); var subtitle = new Subtitle(); const string text = @"1 00:00:02,001 --> 00:00:16,001 X1:000 X2:000 Y1:050 Y2:100 Let us have some! Let us have some!"; target.LoadSubtitle(subtitle, GetSrtLines(text), null); string actual = subtitle.Paragraphs[0].Text; const string expected = "Let us have some! Let us have some!"; Assert.AreEqual(expected, actual); }
public void SrtThreeLiner() { var target = new SubRip(); var subtitle = new Subtitle(); const string text = @"2 00:00:04.501 --> 00:00:08.500 Line 1 Line 2 Line 3"; target.LoadSubtitle(subtitle, GetSrtLines(text), null); string actual = subtitle.Paragraphs[0].Text; string expected = "Line 1" + Environment.NewLine + "Line 2" + Environment.NewLine + "Line 3"; Assert.AreEqual(expected, actual); }
public void SrtDotsInsteadOfCommas() { var target = new SubRip(); var subtitle = new Subtitle(); const string text = @"2 00:00:04.501 --> 00:00:08.500 Dots instead of commas"; target.LoadSubtitle(subtitle, GetSrtLines(text), null); string actual = subtitle.Paragraphs[0].Text; const string expected = "Dots instead of commas"; Assert.AreEqual(expected, actual); }
public void SrtKeepBlankLines() { var target = new SubRip(); var subtitle = new Subtitle(); string subText = "Now go on!" + Environment.NewLine + Environment.NewLine + "Now go on!"; subtitle.Paragraphs.Add(new Paragraph(subText, 0, 999)); var text = target.ToText(subtitle, "title"); var outSubtitle = new Subtitle(); target.LoadSubtitle(outSubtitle, text.SplitToLines().ToList(), null); Assert.IsTrue(outSubtitle.Paragraphs[0].Text == subText); }