~ubuntu-branches/ubuntu/lucid/gnome-subtitles/lucid

« back to all changes in this revision

Viewing changes to sublib-0.7/src/SubLib/Application/SubtitleFactory.cs

  • Committer: Bazaar Package Importer
  • Author(s): Tiago Bortoletto Vaz
  • Date: 2007-12-03 20:52:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071203205252-2y6uuv4gcw9mi9n5
Tags: 0.7-1
* New upstream release;
* Add libxml-parser-perl to Build-Depends-Indep. Thanks to Lucas Nussbaum.
  (Closes: #445799);
* Fixes manpage issue with dpatch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of SubLib.
3
 
 * Copyright (C) 2005-2007 Pedro Castro
4
 
 *
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.
9
 
 *
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.
14
 
 *
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
18
 
 */
19
 
 
20
 
using System;
21
 
using System.Text;
22
 
using System.Text.RegularExpressions;
23
 
 
24
 
namespace SubLib {
25
 
 
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;
31
 
        
32
 
        private bool includeIncompleteSubtitles = false;
33
 
        
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
36
 
        
37
 
        private SubtitleType subtitleType = SubtitleType.Unknown;
38
 
        
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; }
43
 
        }
44
 
        
45
 
        /// <summary>The properties of an opened file, after opening.</summary>
46
 
        public FileProperties FileProperties {
47
 
                 get { return fileProperties; }
48
 
        }
49
 
        
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>
53
 
        public bool Verbose {
54
 
                get { return VerboseConsole.Verbose; }
55
 
                set { VerboseConsole.Verbose = value; }
56
 
        }
57
 
        
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; }
63
 
        }
64
 
        
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; }
70
 
        }
71
 
        
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; }       
78
 
        }
79
 
        
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; }
86
 
        }
87
 
        
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);   
94
 
        }
95
 
        
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;   
105
 
                
106
 
                SubtitleInput input = new SubtitleInput(fallbackEncoding, subtitleType);
107
 
                if (encoding == null) {
108
 
                        text = input.Read(path, out fileEncoding, out format);
109
 
                }
110
 
                else {
111
 
                        text = input.Read(path, encoding, out format);
112
 
                        fileEncoding = encoding;
113
 
                }               
114
 
                
115
 
                if (IsTextEmpty(text))
116
 
                        return EmptySubtitles(path);
117
 
                else
118
 
                        return ParsedSubtitles(path, fileEncoding, format, text);
119
 
        }
120
 
        
121
 
        /* Private members */
122
 
                
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);
127
 
                
128
 
                SubtitleProperties subtitleProperties = new SubtitleProperties(parsingProperties);
129
 
                collection.SetPropertiesForAll(subtitleProperties);
130
 
                
131
 
                Subtitles subtitles = new Subtitles(collection, subtitleProperties);
132
 
                CompleteTimingsAfterParsing(subtitles, parsingProperties);
133
 
                
134
 
                fileProperties = new FileProperties(path, fileEncoding, format.Type , parsingProperties.TimingMode);
135
 
 
136
 
                VerboseConsole.WriteLine("[*] opened " + path + " with encoding " + fileEncoding + " and format " + format.Name);
137
 
                return subtitles;
138
 
        }
139
 
        
140
 
        private Subtitles EmptySubtitles (string path) {
141
 
                Subtitles subtitles = New();
142
 
                fileProperties = new FileProperties(path, Encoding.UTF8, SubtitleType.Unknown, TimingMode.Times);
143
 
                return subtitles;
144
 
        }
145
 
        
146
 
        private bool IsTextEmpty (string text) {
147
 
                Regex regex = new Regex(@"\s*");
148
 
                Match match = regex.Match(text);
149
 
                return (match.Length == text.Length);
150
 
        }
151
 
        
152
 
        private void CompleteTimingsAfterParsing(Subtitles subtitles, ParsingProperties parsingProperties){
153
 
                float originalFrameRate = subtitles.Properties.OriginalFrameRate;
154
 
                subtitles.Properties.SetCurrentFrameRate(originalFrameRate);
155
 
 
156
 
                if (parsingProperties.TimingMode == TimingMode.Times)
157
 
                        subtitles.UpdateFramesFromTimes(originalFrameRate);
158
 
                else
159
 
                        subtitles.UpdateTimesFromFrames(originalFrameRate);
160
 
        }
161
 
 
162
 
}
163
 
 
164
 
}
165