2
* This file is part of SubLib.
3
* Copyright (C) 2005-2007 Pedro Castro
5
* SubLib is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* SubLib is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
using System.Text.RegularExpressions;
26
/// <summary>Represents the main mechanism for creating new <see cref="Subtitles" />.</summary>
27
/// <remarks>A <see cref="SubtitleFactory" /> is no longer needed after the subtitles have been created.</remarks>
28
public class SubtitleFactory {
29
private IncompleteSubtitleCollection incompleteSubtitles = null;
30
private FileProperties fileProperties = null;
32
private bool includeIncompleteSubtitles = false;
34
private Encoding encoding = null; //The encoding to be used to open a file
35
private Encoding fallbackEncoding = Encoding.GetEncoding(1252); //The encoding to fall back to when no encoding is detected
37
private SubtitleType subtitleType = SubtitleType.Unknown;
39
/// <summary>The incomplete subtitles that were found when opening a file.</summary>
40
/// <remarks>This is only used when <see cref="IncludeIncompleteSubtitles" /> is set.</remarks>
41
public IncompleteSubtitleCollection IncompleteSubtitles {
42
get { return incompleteSubtitles; }
45
/// <summary>The properties of an opened file, after opening.</summary>
46
public FileProperties FileProperties {
47
get { return fileProperties; }
50
/// <summary>Whether to enable the library to print messages to the console.</summary>
51
/// <remarks>Messages will be shown along with the main methods of <see cref="SubtitleFactory" />
52
/// and <see cref="Subtitles" />. The default value is false.</remarks>
54
get { return VerboseConsole.Verbose; }
55
set { VerboseConsole.Verbose = value; }
58
/// <summary>Whether to detect and store incomplete subtitles found upon open.</summary>
59
/// <remarks>The default value is false.</remarks>
60
public bool IncludeIncompleteSubtitles {
61
get { return includeIncompleteSubtitles; }
62
set { includeIncompleteSubtitles = value; }
65
/// <summary>The encoding to be used upon open.</summary>
66
/// <remarks>When set to null, encoding auto-detection is used. The default value is null (use auto-detection).</remarks>
67
public Encoding Encoding {
68
get { return encoding; }
69
set { encoding = value; }
72
/// <summary>The encoding to fallback to when using encoding auto-detection.</summary>
73
/// <remarks>When using encoding auto-detection, this encoding will be used if no encoding could be auto-detected.
74
/// Defaults to Windows-1252.</remarks>
75
public Encoding FallbackEncoding {
76
get { return fallbackEncoding; }
77
set { fallbackEncoding = value; }
80
/// <summary>The type of the subtitle being opened.</summary>
81
/// <remarks>When set to <see cref="SubtitleType.Unknown" />, subtitle type auto-detection is used.
82
/// The default value is <see cref="SubtitleType.Unknown" /> (auto-detection).</remarks>
83
public SubtitleType SubtitleType {
84
get { return subtitleType; }
85
set { subtitleType = value; }
88
/// <summary>Creates new empty <see cref="Subtitles" />.</summary>
89
/// <returns>The newly created subtitles.</returns>
90
public Subtitles New () {
91
SubtitleCollection collection = new SubtitleCollection();
92
SubtitleProperties properties = new SubtitleProperties();
93
return new Subtitles(collection, properties);
96
/// <summary>Creates <see cref="Subtitles" /> by opening the file at the specified path.</summary>
97
/// <remarks>The properties of the opened file are accessible with <see cref="FileProperties" />, after opening.</remarks>
98
/// <returns>The opened subtitles.</returns>
99
/// <exception cref="EncodingNotSupportedException">Thrown if a detected encoding is not supported by the platform.</exception>
100
/// <exception cref="UnknownSubtitleFormatException">Thrown if a subtitle format could not be detected.</exception>
101
public Subtitles Open (string path){
102
SubtitleFormat format = null;
103
string text = String.Empty;
104
Encoding fileEncoding = null;
106
SubtitleInput input = new SubtitleInput(fallbackEncoding, subtitleType);
107
if (encoding == null) {
108
text = input.Read(path, out fileEncoding, out format);
111
text = input.Read(path, encoding, out format);
112
fileEncoding = encoding;
115
if (IsTextEmpty(text))
116
return EmptySubtitles(path);
118
return ParsedSubtitles(path, fileEncoding, format, text);
121
/* Private members */
123
private Subtitles ParsedSubtitles (string path, Encoding fileEncoding, SubtitleFormat format, string text) {
124
SubtitleCollection collection = null;
125
SubtitleParser subtitleParser = new SubtitleParser(includeIncompleteSubtitles);
126
ParsingProperties parsingProperties = subtitleParser.Parse(text, format, out collection, out incompleteSubtitles);
128
SubtitleProperties subtitleProperties = new SubtitleProperties(parsingProperties);
129
collection.SetPropertiesForAll(subtitleProperties);
131
Subtitles subtitles = new Subtitles(collection, subtitleProperties);
132
CompleteTimingsAfterParsing(subtitles, parsingProperties);
134
fileProperties = new FileProperties(path, fileEncoding, format.Type , parsingProperties.TimingMode);
136
VerboseConsole.WriteLine("[*] opened " + path + " with encoding " + fileEncoding + " and format " + format.Name);
140
private Subtitles EmptySubtitles (string path) {
141
Subtitles subtitles = New();
142
fileProperties = new FileProperties(path, Encoding.UTF8, SubtitleType.Unknown, TimingMode.Times);
146
private bool IsTextEmpty (string text) {
147
Regex regex = new Regex(@"\s*");
148
Match match = regex.Match(text);
149
return (match.Length == text.Length);
152
private void CompleteTimingsAfterParsing(Subtitles subtitles, ParsingProperties parsingProperties){
153
float originalFrameRate = subtitles.Properties.OriginalFrameRate;
154
subtitles.Properties.SetCurrentFrameRate(originalFrameRate);
156
if (parsingProperties.TimingMode == TimingMode.Times)
157
subtitles.UpdateFramesFromTimes(originalFrameRate);
159
subtitles.UpdateTimesFromFrames(originalFrameRate);