Exemplo n.º 1
0
        public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName)
        {
            _errorCount = 0;
            Errors      = null;
            bool eventsStarted = false;

            subtitle.Paragraphs.Clear();
            // "Marked", " Start", " End", " Style", " Name", " MarginL", " MarginR", " MarginV", " Effect", " Text"
            int       indexLayer   = 0;
            int       indexStart   = 1;
            int       indexEnd     = 2;
            int       indexStyle   = 3;
            const int indexName    = 4;
            int       indexMarginL = 5;
            int       indexMarginR = 6;
            int       indexMarginV = 7;
            int       indexEffect  = 8;
            int       indexText    = 9;
            var       errors       = new StringBuilder();
            int       lineNumber   = 0;

            var header = new StringBuilder();

            for (int i1 = 0; i1 < lines.Count; i1++)
            {
                string line = lines[i1];
                lineNumber++;
                if (!eventsStarted)
                {
                    header.AppendLine(line);
                }

                if (!string.IsNullOrEmpty(line) && line.TrimStart().StartsWith(';'))
                {
                    // skip comment lines
                }
                else if (line.Trim().Equals("[events]", StringComparison.OrdinalIgnoreCase))
                {
                    eventsStarted = true;
                }
                else if (eventsStarted && !string.IsNullOrWhiteSpace(line))
                {
                    string s = line.Trim().ToLowerInvariant();
                    if (line.Length > 10 && s.StartsWith("format:", StringComparison.Ordinal))
                    {
                        var format = s.Substring(8).Split(',');
                        for (int i = 0; i < format.Length; i++)
                        {
                            var formatTrimmed = format[i].Trim();
                            if (formatTrimmed.Equals("layer", StringComparison.Ordinal))
                            {
                                indexLayer = i;
                            }
                            else if (formatTrimmed.Equals("start", StringComparison.Ordinal))
                            {
                                indexStart = i;
                            }
                            else if (formatTrimmed.Equals("end", StringComparison.Ordinal))
                            {
                                indexEnd = i;
                            }
                            else if (formatTrimmed.Equals("text", StringComparison.Ordinal))
                            {
                                indexText = i;
                            }
                            else if (formatTrimmed.Equals("effect", StringComparison.Ordinal))
                            {
                                indexEffect = i;
                            }
                            else if (formatTrimmed.Equals("style", StringComparison.Ordinal))
                            {
                                indexStyle = i;
                            }
                            else if (formatTrimmed.Equals("marginl", StringComparison.Ordinal))
                            {
                                indexMarginL = i;
                            }
                            else if (formatTrimmed.Equals("marginr", StringComparison.Ordinal))
                            {
                                indexMarginR = i;
                            }
                            else if (formatTrimmed.Equals("marginv", StringComparison.Ordinal))
                            {
                                indexMarginV = i;
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(s))
                    {
                        var text    = string.Empty;
                        var start   = string.Empty;
                        var end     = string.Empty;
                        var style   = string.Empty;
                        var marginL = string.Empty;
                        var marginR = string.Empty;
                        var marginV = string.Empty;
                        var layer   = 0;
                        var effect  = string.Empty;
                        var name    = string.Empty;

                        string[] splitLine;
                        if (s.StartsWith("dialog:", StringComparison.Ordinal))
                        {
                            var dialog = line.Remove(0, 7);
                            if (dialog.StartsWith(' '))
                            {
                                dialog = dialog.Remove(0, 1);
                            }
                            splitLine = dialog.Split(',');
                        }
                        else if (s.StartsWith("dialogue:", StringComparison.Ordinal))
                        {
                            var dialog = line.Remove(0, 9);
                            if (dialog.StartsWith(' '))
                            {
                                dialog = dialog.Remove(0, 1);
                            }
                            splitLine = dialog.Split(',');
                        }
                        else
                        {
                            splitLine = line.Split(',');
                        }

                        for (int i = 0; i < splitLine.Length; i++)
                        {
                            if (i == indexStart)
                            {
                                start = splitLine[i].Trim();
                            }
                            else if (i == indexEnd)
                            {
                                end = splitLine[i].Trim();
                            }
                            else if (i == indexLayer)
                            {
                                int.TryParse(splitLine[i], out layer);
                            }
                            else if (i == indexEffect)
                            {
                                effect = splitLine[i];
                            }
                            else if (i == indexText)
                            {
                                text = splitLine[i];
                            }
                            else if (i == indexStyle)
                            {
                                style = splitLine[i];
                            }
                            else if (i == indexMarginL)
                            {
                                marginL = splitLine[i].Trim();
                            }
                            else if (i == indexMarginR)
                            {
                                marginR = splitLine[i].Trim();
                            }
                            else if (i == indexMarginV)
                            {
                                marginV = splitLine[i].Trim();
                            }
                            else if (i == indexName)
                            {
                                name = splitLine[i];
                            }
                            else if (i > indexText)
                            {
                                text += "," + splitLine[i];
                            }
                        }

                        try
                        {
                            var p = new Paragraph
                            {
                                StartTime = GetTimeCodeFromString(start),
                                EndTime   = GetTimeCodeFromString(end),
                                Text      = AdvancedSubStationAlpha.GetFormattedText(text)
                            };

                            if (!string.IsNullOrEmpty(style))
                            {
                                p.Extra = style;
                            }

                            if (!string.IsNullOrEmpty(marginL))
                            {
                                p.MarginL = marginL;
                            }

                            if (!string.IsNullOrEmpty(marginR))
                            {
                                p.MarginR = marginR;
                            }

                            if (!string.IsNullOrEmpty(marginV))
                            {
                                p.MarginV = marginV;
                            }

                            if (!string.IsNullOrEmpty(effect))
                            {
                                p.Effect = effect;
                            }

                            p.Layer = layer;
                            if (!string.IsNullOrEmpty(name))
                            {
                                p.Actor = name;
                            }

                            p.IsComment = s.StartsWith("comment:", StringComparison.Ordinal);
                            subtitle.Paragraphs.Add(p);
                        }
                        catch
                        {
                            _errorCount++;
                            if (errors.Length < 2000)
                            {
                                errors.AppendLine(string.Format(FormatLanguage.LineNumberXErrorReadingTimeCodeFromSourceLineY, lineNumber, line));
                            }
                            else if (subtitle.Paragraphs.Count == 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            if (header.Length > 0)
            {
                subtitle.Header = header.ToString();
            }

            subtitle.Renumber();
            Errors = errors.ToString();
        }
Exemplo n.º 2
0
        public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName)
        {
            _errorCount = 0;
            Errors      = null;
            bool eventsStarted = false;

            subtitle.Paragraphs.Clear();
            string[]  format      = { "Marked", " Start", " End", " Style", " Name", " MarginL", " MarginR", " MarginV", " Effect", " Text" };
            int       indexLayer  = 0;
            int       indexStart  = 1;
            int       indexEnd    = 2;
            int       indexStyle  = 3;
            const int indexName   = 4;
            int       indexEffect = 8;
            int       indexText   = 9;
            var       errors      = new StringBuilder();
            int       lineNumber  = 0;

            var header = new StringBuilder();

            foreach (string line in lines)
            {
                lineNumber++;
                if (!eventsStarted)
                {
                    header.AppendLine(line);
                }

                if (line.Trim().Equals("[events]", StringComparison.OrdinalIgnoreCase))
                {
                    eventsStarted = true;
                }
                else if (!string.IsNullOrEmpty(line) && line.TrimStart().StartsWith(';'))
                {
                    // skip comment lines
                }
                else if (eventsStarted && !string.IsNullOrWhiteSpace(line))
                {
                    string s = line.Trim().ToLower();
                    if (s.StartsWith("format:", StringComparison.Ordinal))
                    {
                        if (line.Length > 10)
                        {
                            format = line.ToLower().Substring(8).Split(',');
                            for (int i = 0; i < format.Length; i++)
                            {
                                if (format[i].Trim().Equals("layer", StringComparison.OrdinalIgnoreCase))
                                {
                                    indexLayer = i;
                                }
                                else if (format[i].Trim().Equals("start", StringComparison.OrdinalIgnoreCase))
                                {
                                    indexStart = i;
                                }
                                else if (format[i].Trim().Equals("end", StringComparison.OrdinalIgnoreCase))
                                {
                                    indexEnd = i;
                                }
                                else if (format[i].Trim().Equals("text", StringComparison.OrdinalIgnoreCase))
                                {
                                    indexText = i;
                                }
                                else if (format[i].Trim().Equals("effect", StringComparison.OrdinalIgnoreCase))
                                {
                                    indexEffect = i;
                                }
                                else if (format[i].Trim().Equals("style", StringComparison.OrdinalIgnoreCase))
                                {
                                    indexStyle = i;
                                }
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(s))
                    {
                        string text   = string.Empty;
                        string start  = string.Empty;
                        string end    = string.Empty;
                        string style  = string.Empty;
                        var    layer  = 0;
                        string effect = string.Empty;
                        string name   = string.Empty;

                        string[] splittedLine;
                        if (s.StartsWith("dialog:", StringComparison.Ordinal))
                        {
                            splittedLine = line.Remove(0, 7).Split(',');
                        }
                        else if (s.StartsWith("dialogue:", StringComparison.Ordinal))
                        {
                            splittedLine = line.Remove(0, 9).Split(',');
                        }
                        else
                        {
                            splittedLine = line.Split(',');
                        }

                        for (int i = 0; i < splittedLine.Length; i++)
                        {
                            if (i == indexStart)
                            {
                                start = splittedLine[i].Trim();
                            }
                            else if (i == indexEnd)
                            {
                                end = splittedLine[i].Trim();
                            }
                            else if (i == indexLayer)
                            {
                                int.TryParse(splittedLine[i], out layer);
                            }
                            else if (i == indexEffect)
                            {
                                effect = splittedLine[i];
                            }
                            else if (i == indexText)
                            {
                                text = splittedLine[i];
                            }
                            else if (i == indexStyle)
                            {
                                style = splittedLine[i];
                            }
                            else if (i == indexName)
                            {
                                name = splittedLine[i];
                            }
                            else if (i > indexText)
                            {
                                text += "," + splittedLine[i];
                            }
                        }

                        try
                        {
                            var p = new Paragraph
                            {
                                StartTime = GetTimeCodeFromString(start),
                                EndTime   = GetTimeCodeFromString(end),
                                Text      = AdvancedSubStationAlpha.GetFormattedText(text)
                            };

                            if (!string.IsNullOrEmpty(style))
                            {
                                p.Extra = style;
                            }
                            if (!string.IsNullOrEmpty(effect))
                            {
                                p.Effect = effect;
                            }
                            p.Layer = layer;
                            if (!string.IsNullOrEmpty(name))
                            {
                                p.Actor = name;
                            }
                            p.IsComment = s.StartsWith("comment:", StringComparison.Ordinal);
                            subtitle.Paragraphs.Add(p);
                        }
                        catch
                        {
                            _errorCount++;
                        }
                    }
                }
            }
            if (header.Length > 0)
            {
                subtitle.Header = header.ToString();
            }
            subtitle.Renumber(1);
            Errors = errors.ToString();
        }
Exemplo n.º 3
0
        public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName)
        {
            _errorCount = 0;
            Errors      = null;
            bool eventsStarted = false;

            subtitle.Paragraphs.Clear();
            string[] format      = "Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text".Split(',');
            int      indexLayer  = 0;
            int      indexStart  = 1;
            int      indexEnd    = 2;
            int      indexStyle  = 3;
            int      indexName   = 4;
            int      indexEffect = 8;
            int      indexText   = 9;
            var      errors      = new StringBuilder();
            int      lineNumber  = 0;

            var header = new StringBuilder();

            foreach (string line in lines)
            {
                lineNumber++;
                if (!eventsStarted)
                {
                    header.AppendLine(line);
                }

                if (line.Trim().ToLower() == "[events]")
                {
                    eventsStarted = true;
                }
                else if (!string.IsNullOrEmpty(line) && line.Trim().StartsWith(";"))
                {
                    // skip comment lines
                }
                else if (eventsStarted && line.Trim().Length > 0)
                {
                    string s = line.Trim().ToLower();
                    if (s.StartsWith("format:"))
                    {
                        if (line.Length > 10)
                        {
                            format = line.ToLower().Substring(8).Split(',');
                            for (int i = 0; i < format.Length; i++)
                            {
                                if (format[i].Trim().ToLower() == "layer")
                                {
                                    indexLayer = i;
                                }
                                else if (format[i].Trim().ToLower() == "start")
                                {
                                    indexStart = i;
                                }
                                else if (format[i].Trim().ToLower() == "end")
                                {
                                    indexEnd = i;
                                }
                                else if (format[i].Trim().ToLower() == "text")
                                {
                                    indexText = i;
                                }
                                else if (format[i].Trim().ToLower() == "effect")
                                {
                                    indexEffect = i;
                                }
                                else if (format[i].Trim().ToLower() == "style")
                                {
                                    indexStyle = i;
                                }
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(s))
                    {
                        string text   = string.Empty;
                        string start  = string.Empty;
                        string end    = string.Empty;
                        string style  = string.Empty;
                        string layer  = string.Empty;
                        string effect = string.Empty;
                        string name   = string.Empty;

                        string[] splittedLine;

                        if (s.StartsWith("dialogue:"))
                        {
                            splittedLine = line.Substring(10).Split(',');
                        }
                        else
                        {
                            splittedLine = line.Split(',');
                        }

                        for (int i = 0; i < splittedLine.Length; i++)
                        {
                            if (i == indexStart)
                            {
                                start = splittedLine[i].Trim();
                            }
                            else if (i == indexEnd)
                            {
                                end = splittedLine[i].Trim();
                            }
                            else if (i == indexLayer)
                            {
                                layer = splittedLine[i];
                            }
                            else if (i == indexEffect)
                            {
                                effect = splittedLine[i];
                            }
                            else if (i == indexText)
                            {
                                text = splittedLine[i];
                            }
                            else if (i == indexStyle)
                            {
                                style = splittedLine[i];
                            }
                            else if (i == indexName)
                            {
                                name = splittedLine[i];
                            }
                            else if (i > indexText)
                            {
                                text += "," + splittedLine[i];
                            }
                        }

                        try
                        {
                            var p = new Paragraph();

                            p.StartTime = GetTimeCodeFromString(start);
                            p.EndTime   = GetTimeCodeFromString(end);
                            p.Text      = AdvancedSubStationAlpha.GetFormattedText(text);
                            if (!string.IsNullOrEmpty(style))
                            {
                                p.Extra = style;
                            }
                            if (!string.IsNullOrEmpty(effect))
                            {
                                p.Effect = effect;
                            }
                            if (!string.IsNullOrEmpty(layer))
                            {
                                p.Layer = layer;
                            }
                            if (!string.IsNullOrEmpty(name))
                            {
                                p.Actor = name;
                            }
                            p.IsComment = s.StartsWith("comment:");
                            subtitle.Paragraphs.Add(p);
                        }
                        catch
                        {
                            _errorCount++;
                            if (errors.Length < 2000)
                            {
                                errors.AppendLine(string.Format(Configuration.Settings.Language.Main.LineNumberXErrorReadingTimeCodeFromSourceLineY, lineNumber, line));
                            }
                        }
                    }
                }
            }
            if (header.Length > 0)
            {
                subtitle.Header = header.ToString();
            }
            subtitle.Renumber(1);
            Errors = errors.ToString();
        }