internal string Build (SubtitleCollection collection, SubtitleProperties subtitleProperties, FileProperties fileProperties) { this.subtitleProperties = subtitleProperties; StringBuilder output = new StringBuilder(); if (format.HasHeaders) output.Append(format.HeadersToString(subtitleProperties, fileProperties)); if (format.HasBodyBegin) output.Append(format.BodyBeginOut); string subtitleExpression = GetSubtitleExpression(format, subtitleProperties, fileProperties); Regex fieldExpression = new Regex(@"<<(?<Field>\w+)(,(?<Width>\d+))?>>"); MatchEvaluator matchEvaluator = new MatchEvaluator(this.FieldEvaluator); foreach (Subtitle currentSubtitle in collection) { subtitle = currentSubtitle; string outputSubtitle = fieldExpression.Replace(subtitleExpression, matchEvaluator); output.Append(outputSubtitle); output.Append("\n"); subtitleNumber++; previousSubtitle = subtitle; } if (format.HasBodyEnd) output.Append(format.BodyEndOut); subtitle = null; previousSubtitle = null; subtitleNumber = 1; ConvertNewlines(output, fileProperties); return output.ToString(); }
internal override string HeadersToString(SubtitleProperties subtitleProperties, FileProperties fileProperties) { Headers headers = subtitleProperties.Headers; return "# <HEAD>\n" + "# FRAME RATE=" + headers.FrameRate + "\n" + "# CREATOR=" + headers.Author + "\n" + "# VIDEO SOURCE=" + headers.VideoSource + "\n" + "# DATE=" + headers.Date + "\n" + "# </HEAD>\n#\n"; }
internal override string HeadersToString (SubtitleProperties subtitleProperties, FileProperties fileProperties) { Headers headers = subtitleProperties.Headers; string format = (fileProperties.TimingMode == TimingMode.Times ? "TIME" : subtitleProperties.CurrentFrameRate.ToString()); return "TITLE=" + headers.Title + "\n" + "FILE=" + headers.FileProperties + "\n" + "AUTHOR=" + headers.Author + "\n" + "TYPE=" + headers.MediaType + "\n" + "FORMAT=" + format + "\n" + "NOTE=" + headers.Comment + "\n\n"; }
/* Public methods */ /// <summary>Saves subtitles to the file with the specified properties.</summary> /// <param name="subtitles">The subtitles to save.</param> /// <param name="properties">The properties of the file to save the subtitles to. Its <see cref="TimingMode" /> property is used to /// choose the timing mode for subtitle formats that support both time and frame modes.</param> /// <param name="textType">The type of text content to save.</param> /// <remarks>An updated <see cref="SubLib.FileProperties" /> object can be accessed with <see cref="FileProperties" /> after saving.</remarks> public void Save (Subtitles subtitles, FileProperties properties, SubtitleTextType textType) { SubtitleFormat format = BuiltInSubtitleFormats.GetFormat(properties.SubtitleType); SubtitleOutput output = new SubtitleOutput(format, textType); string text = output.Build(subtitles.Collection, subtitles.Properties, properties); FileInputOutput.WriteFile(properties.Path, text, properties.Encoding); fileProperties = GetUpdatedFileProperties(properties); VerboseConsole.WriteLine("[*] Saved " + textType + " \"" + properties.Path + "\" with encoding \"" + properties.Encoding + "\", format \"" + format.Name + "\" and frame rate \"" + subtitles.Properties.CurrentFrameRate + "\""); }
internal override string HeadersToString (SubtitleProperties subtitleProperties, FileProperties fileProperties) { Headers headers = subtitleProperties.Headers; return "[ti: " + headers.Title + "]\n" + "[au:" + headers.MovieAuthor + "]\n" + "[ar:" + headers.Artist + "]\n" + "[al:" + headers.Album + "]\n" + "[by:" + headers.Author + "]\n" + "[ve:" + headers.Version + "]\n" + "[re:" + headers.Program + "]\n"; }
internal override string HeadersToString (SubtitleProperties subtitleProperties, FileProperties fileProperties) { Headers headers = subtitleProperties.Headers; return "[TITLE]\n" + headers.Title + "\n" + "[AUTHOR]\n" + headers.Author + "\n" + "[SOURCE]\n" + headers.VideoSource + "\n" + "[PRG]\n" + headers.Program + "\n" + "[FILEPATH]\n" + headers.SubtitlesSource + "\n" + "[DELAY]\n" + headers.Delay + "\n" + "[CD TRACK]\n" + headers.CDTrack + "\n"; }
internal override string HeadersToString (SubtitleProperties subtitleProperties, FileProperties fileProperties) { Headers headers = subtitleProperties.Headers; return "[INFORMATION]\n" + "[TITLE]" + headers.Title + "\n" + "[AUTHOR]" + headers.Author + "\n" + "[SOURCE]" + headers.VideoSource + "\n" + "[PRG]" + headers.Program + "\n" + "[FILEPATH]" + headers.SubtitlesSource + "\n" + "[DELAY]" + headers.Delay + "\n" + "[CD TRACK]" + headers.CDTrack + "\n" + "[COMMENT]" + headers.Comment + "\n" + "[END INFORMATION]\n" + "[SUBTITLE]\n" + "[COLF]" + headers.FontColor + ",[STYLE]" + headers.FontStyle + ",[SIZE]" + headers.FontSize + ",[FONT]" + headers.FontName + "\n"; }
internal override string HeadersToString (SubtitleProperties subtitleProperties, FileProperties fileProperties) { Headers headers = subtitleProperties.Headers; return "[Script Info]\n" + Header("Title:", headers.Title, "<untitled>") + Header("Original Script:", headers.OriginalScript, "<unknown>") + "Script Type: " + ScriptType + "\n" + Header("Original Translation:", headers.OriginalTranslation) + Header("Original Editing:", headers.OriginalEditing) + Header("Original Timing:", headers.OriginalTiming) + Header("Original Script Checking:", headers.OriginalScriptChecking) + Header("Script Updated By:", headers.ScriptUpdatedBy) + Header("Collisions:", headers.Collisions) + Header("PlayResX:", headers.PlayResX) + Header("PlayResY:", headers.PlayResY) + Header("PlayDepth:", headers.PlayDepth) + Header("Timer:", headers.Timer) + "\n" + StyleSection;
private bool SaveFile (SubLib.Core.Domain.Subtitles subtitles, FileProperties properties, SubtitleTextType textType) { try { SubtitleSaver saver = new SubtitleSaver(); saver.Save(subtitles, properties, textType); return true; } catch (Exception e) { Console.Error.WriteLine("Caught exception saving backup file: " + e); return false; } }
public bool SaveTranslation (FileProperties newFileProperties) { SubtitleSaver saver = new SubtitleSaver(); saver.Save(subtitles, newFileProperties, SubtitleTextType.Translation); translationFile = saver.FileProperties; canTranslationBeSaved = true; ClearTranslationModified(); return true; }
private void SaveTranslation (FileProperties fileProperties) { try { Base.Document.SaveTranslation(fileProperties); } catch (Exception exception) { FileSaveErrorDialog errorDialog = new FileSaveErrorDialog(fileProperties.Path, exception); errorDialog.Show(); bool toSaveAgain = errorDialog.WaitForResponse(); if (toSaveAgain) TranslationSaveAs(); } }
/* Private methods */ /* Used in the object construction. Path is used when running from the command line specifying an inexistent file. */ private void New (string path) { New(); textFile = new FileProperties(path); }
private string GetSubtitleExpression (SubtitleFormat format, SubtitleProperties subtitleProperties, FileProperties fileProperties) { if (format.Mode == SubtitleMode.Both) { if (fileProperties.TimingMode == TimingMode.Times) return format.SubtitleOutTimesMode; else return format.SubtitleOutFramesMode; } else { if (format.SubtitleOut != null) return format.SubtitleOut; else return format.GetDynamicSubtitleOut(subtitleProperties); } }
private void ConvertNewlines (StringBuilder builder, FileProperties properties) { NewlineType type = properties.NewlineType; if ((type == NewlineType.Unknown) || (type == NewlineType.Unix)) return; string newline = (type == NewlineType.Windows ? "\r\n" : "\r"); //Windows : Macintosh builder.Replace("\n", newline); }
public void OpenTranslation (string path, Encoding encoding) { if (this.IsTranslationLoaded) CloseTranslation(); SubtitleFactory factory = new SubtitleFactory(); factory.Verbose = true; factory.Encoding = encoding; factory.FallbackEncoding = GetFallbackEncoding(); SubLib.Core.Domain.Subtitles openedTranslation = factory.Open(path); FileProperties newTranslationFile = factory.FileProperties; AddExtraSubtitles(openedTranslation); Translations translations = new Translations(); translations.Import(subtitles, openedTranslation, Base.Config.PrefsTimingsTimeBetweenSubtitles); if (newTranslationFile.SubtitleType != SubtitleType.Unknown) canTranslationBeSaved = true; this.translationFile = newTranslationFile; this.isTranslationLoaded = true; }
/* Private methods */ private FileProperties GetUpdatedFileProperties (FileProperties properties) { SubtitleFormat format = BuiltInSubtitleFormats.GetFormat(properties.SubtitleType); TimingMode newTimingMode = (format.Mode == SubtitleMode.Both) ? properties.TimingMode : format.ModeAsTimingMode; return new FileProperties(properties.Path, properties.Encoding, properties.SubtitleType, newTimingMode, properties.NewlineType); }
private void ClearTranslationStatus () { this.isTranslationLoaded = false; this.wasTranslationModified = false; this.translationFile = null; this.canTranslationBeSaved = false; ClearTranslationModified(); }
/* Used in the object construction */ private void Open (string path, Encoding encoding) { SubtitleFactory factory = new SubtitleFactory(); factory.Verbose = true; factory.Encoding = encoding; factory.FallbackEncoding = GetFallbackEncoding(); factory.InputFrameRate = Base.Ui.Menus.TimingsInputFrameRateActive; SubLib.Core.Domain.Subtitles openedSubtitles = null; try { openedSubtitles = factory.Open(path); } catch (FileNotFoundException) { New(path); return; } subtitles = new Ui.View.Subtitles(openedSubtitles); textFile = factory.FileProperties; if (textFile.SubtitleType != SubtitleType.Unknown) canTextBeSaved = true; }
internal virtual string HeadersToString (SubtitleProperties subtitleProperties, FileProperties fileProperties) { return String.Empty; }
/* Public methods */ public bool Save (FileProperties newFileProperties) { SubtitleSaver saver = new SubtitleSaver(); saver.Save(subtitles, newFileProperties, SubtitleTextType.Text); textFile = saver.FileProperties; canTextBeSaved = true; ClearTextModified(); return true; }