Show line lengths in the subtitle list, option configurable in the View menu (related...
[gn-sub.git] / src / SubLib / Core / Domain / SubtitleText.cs
blob9e8712d9148e41a2ff2f39340492a18250c25993
1 /*
2 * This file is part of SubLib.
3 * Copyright (C) 2005-2010 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
20 using System;
21 using System.Collections;
22 using System.Text;
23 using System.Text.RegularExpressions;
25 namespace SubLib.Core.Domain {
27 //TODO this can be optimized
28 /// <summary>Represents the text of a subtitle.</summary>
29 public class SubtitleText : ICloneable {
30 private ArrayList lines = new ArrayList();
32 /// <summary>Initializes a new instance of the <see cref="SubtitleText" /> class
33 /// with the specified text, line break and trimming option.</summary>
34 /// <param name="text">The subtitle text.</param>
35 /// <param name="lineBreak">The text substring that marks the end of lines.</param>
36 /// <param name="toTrimLines">Whether to trim every text line.</param>
37 public SubtitleText (string text, string lineBreak, bool toTrimLines) {
38 Set(text, lineBreak, toTrimLines);
41 /// <summary>Initializes a new instance of the <see cref="SubtitleText" /> class
42 /// with the specified text.</summary>
43 /// <remarks>Newline (\n) is used as the line break. The text lines are not trimmed.</remarks>
44 /// <param name="text">The subtitle text.</param>
45 public SubtitleText (string text) : this(text, "\n", false) {
48 /// <summary>Initializes a new instance of the <see cref="SubtitleText" /> class, with empty text.</summary>
49 public SubtitleText() {
53 /* Public properties */
55 /// <summary>Whether there is no text.</summary>
56 public bool IsEmpty {
57 get { return ((lines.Count == 0) || ((lines.Count == 1) && ((lines[0] as string).Length == 0))); }
60 public IEnumerator GetEnumerator () {
61 return lines.GetEnumerator();
65 /* Public methods */
67 /// <summary>Gets the specified text line.</summary>
68 /// <param name="index">The zero-based line number index.</param>
69 /// <returns>The specified text line.</returns>
70 public string GetLine (int index) {
71 if ((index >= 0) && (index < lines.Count))
72 return lines[index] as string;
73 else
74 return String.Empty;
77 /// <summary>Gets the text lines merged with the specified line break.</summary>
78 /// <param name="lineBreak">The line break used to merge the text.</param>
79 /// <returns>The subtitle text.</returns>
80 public string Get (string lineBreak) {
81 string text = String.Empty;
82 IEnumerator textLines = lines.GetEnumerator();
83 if (textLines.MoveNext()){
84 text = (textLines.Current as string);
85 while (textLines.MoveNext())
86 text += lineBreak + (textLines.Current as string);
88 return text;
91 /// <summary>Gets the subtitle text.</summary>
92 /// <remarks>The text lines end with the new line (\n) char.</remarks>
93 /// <returns>The subtitle text.</returns>
94 public string Get () {
95 return Get("\n");
98 /// <summary>Gets the text lines merged with the specified line break and replaces those that are empty.</summary>
99 /// <param name="replacement">The text to replace empty lines with.</param>
100 /// <param name="lineBreak">The line break used to merge the text.</param>
101 /// <remarks>A subtitle line is considered empty if it has no characters.</remarks>
102 /// <returns>The subtitle text, after replacement.</returns>
103 public string GetReplaceEmptyLines (string replacement, string lineBreak) {
104 if (this.IsEmpty)
105 return replacement;
107 string text = String.Empty;
108 IEnumerator textLines = lines.GetEnumerator();
109 if (textLines.MoveNext()){
110 string line = (textLines.Current as string);
111 text = ReplaceLineIfEmpty(line, replacement);
112 while (textLines.MoveNext()) {
113 line = (textLines.Current as string);
114 text += lineBreak + ReplaceLineIfEmpty(line, replacement);
117 return text;
120 /// <summary>Gets the text lines and replaces those that are empty.</summary>
121 /// <param name="replacement">The text to replace empty lines with.</param>
122 /// <remarks>The text lines are merged by the newline (\n) char. A subtitle line is considered empty
123 /// if it has no characters.</remarks>
124 /// <returns>The subtitle text, after replacement.</returns>
125 public string GetReplaceEmptyLines (string replacement) {
126 return GetReplaceEmptyLines(replacement, "\n");
129 /// <summary>Gets and trims the text lines merged with the specified line break.</summary>
130 /// <param name="lineBreak">The line break used to merge the text.</param>
131 /// <remarks>A subtitle line is considered blank if it has only white spaces.</remarks>
132 /// <returns>The subtitle text, after trimming.</returns>
133 public string GetTrimLines (string lineBreak) {
134 string text = String.Empty;
135 IEnumerator textLines = lines.GetEnumerator();
136 if (textLines.MoveNext()){
137 string line = (textLines.Current as string);
138 line = line.Trim();
139 if (line != String.Empty)
140 text += line;
142 while (textLines.MoveNext()) {
143 line = (textLines.Current as string);
144 line = line.Trim();
145 if (line != String.Empty)
146 text += lineBreak + line;
149 return text;
152 /// <summary>Gets and trims the text lines.</summary>
153 /// <remarks>The text lines are merged by the newline (\n) char. A subtitle line is
154 /// considered blank ifit has only white spaces.</remarks>
155 /// <returns>The subtitle text, after trimming.</returns>
156 public string GetTrimLines () {
157 return GetTrimLines("\n");
160 /// <summary>Sets the subtitle text using the specified line break and trimming option.</summary>
161 /// <param name="text">The subtitle text.</param>
162 /// <param name="lineBreak">The text substring used to split the text in lines.</param>
163 /// <param name="toTrimLines">Whether to trim every text line.</param>
164 public void Set (string text, string lineBreak, bool toTrimLines) {
165 if (toTrimLines)
166 text = text.Trim();
168 string escapedLineBreak = Regex.Escape(lineBreak);
169 string spaceDelimiter = (toTrimLines ? @"\s*" : String.Empty);
170 string regexPattern = spaceDelimiter + @escapedLineBreak + spaceDelimiter;
171 string[] textLines = Regex.Split(text, regexPattern);
172 lines.Clear();
173 foreach (string textLine in textLines)
174 lines.Add(textLine);
177 /// <summary>Sets the subtitle text.</summary>
178 /// <remarks>Newline (\n) is used as the line break. The text lines are not trimmed.</remarks>
179 /// <param name="text">The subtitle text.</param>
180 public void Set (string text) {
181 Set(text, "\n", false);
184 public override string ToString() {
185 string result = String.Empty;
186 int lineNumber = 1;
187 foreach (string line in lines){
188 result += "\t" + lineNumber + ". " + line + "\n";
189 lineNumber++;
191 return result;
194 public object Clone() {
195 SubtitleText clone = new SubtitleText();
196 foreach (string line in lines) {
197 clone.lines.Add(line);
199 return clone;
203 /* Private Methods */
205 private string ReplaceLineIfEmpty (string textLine, string replacement) {
206 if (textLine == String.Empty)
207 return replacement;
208 else
209 return textLine;