SubtitleFormat.cs 5.36 KB
Newer Older
1 2
/*
 * This file is part of SubLib.
3
 * Copyright (C) 2005-2010 Pedro Castro
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
 *
 * SubLib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * SubLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using SubLib.Core.Domain;
using SubLib.IO.Input;
using System;
using System.Text.RegularExpressions;

namespace SubLib.IO.SubtitleFormats {

internal abstract class SubtitleFormat {

	/* Required members */
	protected string name;			//the name of the format.
	protected SubtitleType type;	//the subtitle type
	protected SubtitleMode mode;	//the subtitle mode
	protected string[] extensions;	//the extensions used
	
	protected string lineBreak;		//used to split the subtitle's text lines
	protected string format;		//regex used to detect the file's format		
	protected string subtitleIn;	//regex used to read a subtitle
	protected string subtitleOut = null;	//expression with tags used to write a subtitle; GetDynamicSubtitleOut can be used instead if the expression is dynamic

	/* Optional members */
	protected string[] headers = null;				//used to read the subtitles' headers
	protected string comments = String.Empty;		//used to discard comments
	protected string bodyBeginOut = String.Empty;	//used to write the beginning of the subtitles' body
	protected string bodyEndIn = String.Empty;		//used to detect the end of the subtitles' body
	protected string bodyEndOut = String.Empty;		//used to write the end of the subtitles' body
	
	/* Required members for subtitles that support both frames and times */
	//The following two are used instead of subtitleIn
	protected string subtitleInTimesMode = String.Empty;	//regex used to read a subtitle in times mode
	protected string subtitleInFramesMode = String.Empty;	//regex used to read a subtitle in frames mode
	//The following two are used instead of subtitleOut
	protected string subtitleOutTimesMode = String.Empty;   //regex used to output a subtitle in times mode
	protected string subtitleOutFramesMode = String.Empty;  //regex used to output a subtitle in times mode
	
	internal string Name {
		get { return name; }
	}
		
	internal SubtitleType Type {
		get { return type; }
	}
	
	internal SubtitleMode Mode {
		get { return mode; }
	}

	/// <remarks>Only use this when the Mode is not Both. This is provided as a convenience
	/// for when the Mode is either Frames or Times.</remarks>
	internal TimingMode ModeAsTimingMode {
		get {
			if (mode == SubtitleMode.Frames)
				return TimingMode.Frames;
			else
				return TimingMode.Times;
		}
	}
	
	internal string[] Extensions {
		get { return extensions; }
	}
	
	internal string LineBreak {
		get { return lineBreak; }
	}
	
	internal string Format {
		get { return format; }
	}
	
	internal string SubtitleIn {
		get { return subtitleIn; }
	}
	
	internal string SubtitleOut {
		get { return subtitleOut; }
	}

	internal bool HasHeaders {
		get { return headers != null; }
	}
	
	internal string[] Headers {
		get { return headers; }
	}
	
	internal bool HasComments {
		get { return comments != String.Empty; }
	}
	
	internal string Comments {
		get { return comments; }
	}
	
	internal bool HasBodyBegin {
		get { return bodyBeginOut != String.Empty; }
	}
	
	internal string BodyBeginOut {
		get { return bodyBeginOut; }
	}
	
	internal bool HasBodyEnd {
		get { return bodyEndOut != String.Empty; }
	}
	
	internal string BodyEndIn {
		get { return bodyEndIn; }
	}
	
	internal string BodyEndOut {
		get { return bodyEndOut; }
	}

	internal string SubtitleInTimesMode {
		get { return subtitleInTimesMode; }
	}
	
	internal string SubtitleInFramesMode {
		get { return subtitleInFramesMode; }
	}
	
	internal string SubtitleOutTimesMode {
		get { return subtitleOutTimesMode; }
	}
	
	internal string SubtitleOutFramesMode {
		get { return subtitleOutFramesMode; }
	}
	
	internal virtual Style StringToStyle (string styleText) {
		Style style = new Style();
		foreach (char character in styleText) {
			if ((character == 'u') || (character == 'U'))
				style.Underline = true;
			else if ((character == 'b') || (character == 'B'))
				style.Bold = true;
			else if ((character == 'i') || (character == 'I'))
				style.Italic = true;					
		}
		return style;
	}
	
	internal virtual string StyleToString (Style style) {
		return String.Empty;
	}
	
	internal virtual string EndOfStyleToString (Style style) {
		return String.Empty;
	}
		
	internal virtual void SubtitleInputPostProcess (Subtitle subtitle) {
		return;
	}
	
	internal virtual void GlobalInputGetProperties (string text, ParsingProperties properties) {
		return;
	}
	
	internal virtual string GetDynamicSubtitleOut (SubtitleProperties properties) {
		return null;
	}

    internal virtual string HeadersToString (SubtitleProperties subtitleProperties, FileProperties fileProperties) {
        return String.Empty;
    }
    
	/* Public members */
	
	public override string ToString(){
		return Enum.GetName(typeof(SubtitleType), Type);
	}
}

}