Example #1
0
        private void ButtonSetDefaultCategory_Click(object sender, EventArgs e)
        {
            var oldDefaultCategory             = _assaCategories.Single(category => category.IsDefault);
            var oldDefaultCategoryListViewItem = listViewCategories.Items[_assaCategories.IndexOf(oldDefaultCategory)];

            oldDefaultCategory.IsDefault = false;
            oldDefaultCategoryListViewItem.SubItems[columnHeaderForDefault.Index].Text = oldDefaultCategory.IsDefault.ToString();
            SetDefaultSubItemColor(oldDefaultCategoryListViewItem, oldDefaultCategory.IsDefault);

            var newDefault = SelectedCategory;
            var newDefaultCategoryListViewItem = listViewCategories.Items[_assaCategories.IndexOf(newDefault)];

            newDefault.IsDefault = true;
            if (newDefault.Styles.Count == 0)
            {
                var defaultStyle = new SsaStyle();
                SelectedCategory.Styles.Add(defaultStyle);
            }
            newDefaultCategoryListViewItem.SubItems[columnHeaderForStyles.Index].Text  = newDefault.Styles.Count.ToString();
            newDefaultCategoryListViewItem.SubItems[columnHeaderForDefault.Index].Text = newDefault.IsDefault.ToString();
            SetDefaultSubItemColor(newDefaultCategoryListViewItem, newDefault.IsDefault);

            buttonSetDefaultCategory.Enabled = false;
            buttonRemoveCategory.Enabled     = false;

            listViewCategories.Focus();
        }
        /// <summary>
        /// Add new style to ASS header
        /// </summary>
        /// <returns>Header with new style</returns>
        public static string AddSsaStyle(SsaStyle style, string header)
        {
            if (string.IsNullOrEmpty(header))
                header = DefaultHeader;

            var sb = new StringBuilder();
            bool stylesStarted = false;
            bool styleAdded = false;
            string styleFormat = "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding";
            foreach (string line in header.Replace(Environment.NewLine, "\n").Split('\n'))
            {
                if (line.ToLower() == "[V4+ Styles]".ToLower() || line.ToLower() == "[V4 Styles]".ToLower())
                    stylesStarted = true;
                if (line.ToLower().StartsWith("format:"))
                    styleFormat = line;
                if (!line.StartsWith("Style: " + style.Name + ",")) // overwrite existing style
                    sb.AppendLine(line);
                if (!styleAdded && stylesStarted && line.ToLower().Trim().StartsWith("style:"))
                {
                    sb.AppendLine(style.ToRawAss(styleFormat));
                    styleAdded = true;
                }
            }
            return sb.ToString();
        }
        private void textBoxRawHeader_Validated(object sender, EventArgs e)
        {
            _doUpdate = false;
            SsaStyle style = GetSsaStyle(CurrentStyleName);

            SetControlsFromStyle(style);
            _doUpdate = true;
            GeneratePreview();
        }
        private void SubStationAlphaStylesBatchConvert_Shown(object sender, EventArgs e)
        {
            string   styleName = CurrentStyleName;
            SsaStyle style     = GetSsaStyle(styleName);

            SetControlsFromStyle(style);
            SetControlsFromHeader();
            _doUpdate = true;
            groupBoxProperties.Enabled = true;
            GeneratePreviewAndUpdateRawHeader();
        }
Example #5
0
        private void InitializeListView()
        {
            var styles = AdvancedSubStationAlpha.GetStylesFromHeader(_subtitle.Header);

            listViewStyles.Items.Clear();
            foreach (string style in styles)
            {
                SsaStyle ssaStyle = AdvancedSubStationAlpha.GetSsaStyle(style, _subtitle.Header);
                SubStationAlphaStyles.AddStyle(listViewStyles, ssaStyle, _subtitle, _isSubStationAlpha);
            }
            if (listViewStyles.Items.Count > 0)
            {
                listViewStyles.Items[0].Selected = true;
            }
        }
Example #6
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            ExportedStyles = new List <string>();
            foreach (ListViewItem item in listViewExportStyles.Items)
            {
                if (item.Checked)
                {
                    ExportedStyles.Add(item.Text);
                }
            }
            if (ExportedStyles.Count == 0)
            {
                return;
            }

            saveFileDialogStyle.Title            = LanguageSettings.Current.SubStationAlphaStyles.ExportStyleToFile;
            saveFileDialogStyle.InitialDirectory = Configuration.DataDirectory;
            if (_isSubStationAlpha)
            {
                saveFileDialogStyle.Filter   = SubStationAlpha.NameOfFormat + "|*.ssa|" + LanguageSettings.Current.General.AllFiles + "|*.*";
                saveFileDialogStyle.FileName = "my_styles.ssa";
            }
            else
            {
                saveFileDialogStyle.Filter   = AdvancedSubStationAlpha.NameOfFormat + "|*.ass|" + LanguageSettings.Current.General.AllFiles + "|*.*";
                saveFileDialogStyle.FileName = "my_styles.ass";
            }

            if (saveFileDialogStyle.ShowDialog(this) == DialogResult.OK)
            {
                if (File.Exists(saveFileDialogStyle.FileName))
                {
                    var s      = new Subtitle();
                    var format = s.LoadSubtitle(saveFileDialogStyle.FileName, out _, null);
                    if (format == null)
                    {
                        MessageBox.Show("Not subtitle format: " + _format.Name);
                        return;
                    }

                    if (format.Name != _format.Name)
                    {
                        MessageBox.Show(string.Format("Cannot save {1} style in {0} file!", format.Name, _format.Name));
                    }
                    else
                    {
                        var    sb          = new StringBuilder();
                        bool   stylesOn    = false;
                        bool   done        = false;
                        string styleFormat = SsaStyle.DefaultAssStyleFormat;
                        foreach (string line in File.ReadAllLines(saveFileDialogStyle.FileName))
                        {
                            if (line.StartsWith("format:", StringComparison.OrdinalIgnoreCase))
                            {
                                styleFormat = line;
                            }
                            else if (line.StartsWith("style:", StringComparison.OrdinalIgnoreCase))
                            {
                                stylesOn = true;
                            }
                            else if (stylesOn && !done)
                            {
                                done = true;
                                foreach (var styleName in ExportedStyles)
                                {
                                    SsaStyle style = AdvancedSubStationAlpha.GetSsaStyle(styleName, _header);
                                    if (_isSubStationAlpha)
                                    {
                                        sb.AppendLine(style.ToRawSsa(styleFormat));
                                    }
                                    else
                                    {
                                        sb.AppendLine(style.ToRawAss(styleFormat));
                                    }
                                }
                            }
                            sb.AppendLine(line);
                            foreach (var styleName in ExportedStyles)
                            {
                                var toLower = line.Trim().ToLowerInvariant();
                                while (toLower.Contains(": "))
                                {
                                    toLower = toLower.Replace(": ", ":");
                                }

                                while (toLower.Contains(" :"))
                                {
                                    toLower = toLower.Replace(" :", ":");
                                }

                                if (stylesOn && toLower.StartsWith("style:" + styleName.Trim() + ",", StringComparison.OrdinalIgnoreCase))
                                {
                                    MessageBox.Show(string.Format(LanguageSettings.Current.SubStationAlphaStyles.StyleAlreadyExits, styleName));
                                    return;
                                }
                            }
                        }
                        File.WriteAllText(saveFileDialogStyle.FileName, sb.ToString(), Encoding.UTF8);
                    }
                }
                else
                {
                    var sb = new StringBuilder();
                    foreach (var line in _header.Replace(Environment.NewLine, "\n").Split('\n'))
                    {
                        if (line.StartsWith("style:", StringComparison.OrdinalIgnoreCase))
                        {
                            foreach (var styleName in ExportedStyles)
                            {
                                var toLower = line.Trim().ToLowerInvariant();
                                while (toLower.Contains(": "))
                                {
                                    toLower = toLower.Replace(": ", ":");
                                }

                                while (toLower.Contains(" :"))
                                {
                                    toLower = toLower.Replace(" :", ":");
                                }

                                if (toLower.StartsWith("style:" + styleName.ToLowerInvariant().Trim(), StringComparison.Ordinal))
                                {
                                    sb.AppendLine(line);
                                }
                            }
                        }
                        else
                        {
                            sb.AppendLine(line);
                        }
                    }
                    string content = sb.ToString();
                    if (content.TrimEnd().EndsWith("[Events]", StringComparison.Ordinal))
                    {
                        content = content.Trim() + Environment.NewLine +
                                  "Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text" + Environment.NewLine +
                                  "Dialogue: 0,0:00:31.91,0:00:33.91,Default,,0,0,0,,My Styles :)";
                    }
                    else if (!content.Contains("[Events]", StringComparison.OrdinalIgnoreCase))
                    {
                        content = content.Trim() + Environment.NewLine +
                                  Environment.NewLine +
                                  "[Events]" + Environment.NewLine +
                                  "Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text" + Environment.NewLine +
                                  "Dialogue: 0,0:00:31.91,0:00:33.91,Default,,0,0,0,,My Styles :)";
                    }
                    File.WriteAllText(saveFileDialogStyle.FileName, content, Encoding.UTF8);
                    DialogResult = DialogResult.OK;
                }
            }
        }
        private void SetControlsFromStyle(SsaStyle style)
        {
            comboBoxFontName.Text         = style.FontName;
            checkBoxFontItalic.Checked    = style.Italic;
            checkBoxFontBold.Checked      = style.Bold;
            checkBoxFontUnderline.Checked = style.Underline;

            if (style.FontSize > 0 && style.FontSize <= (float)numericUpDownFontSize.Maximum)
            {
                numericUpDownFontSize.Value = (decimal)style.FontSize;
            }
            else
            {
                numericUpDownFontSize.Value = 20;
            }

            panelPrimaryColor.BackColor   = style.Primary;
            panelSecondaryColor.BackColor = style.Secondary;
            if (_isSubStationAlpha)
            {
                panelOutlineColor.BackColor = style.Tertiary;
            }
            else
            {
                panelOutlineColor.BackColor = style.Outline;
            }

            panelBackColor.BackColor = style.Background;

            if (style.OutlineWidth >= 0 && style.OutlineWidth <= numericUpDownOutline.Maximum)
            {
                numericUpDownOutline.Value = style.OutlineWidth;
            }
            else
            {
                numericUpDownOutline.Value = 2;
            }

            if (style.ShadowWidth >= 0 && style.ShadowWidth <= numericUpDownShadowWidth.Maximum)
            {
                numericUpDownShadowWidth.Value = style.ShadowWidth;
            }
            else
            {
                numericUpDownShadowWidth.Value = 1;
            }

            if (_isSubStationAlpha)
            {
                switch (style.Alignment)
                {
                case "1":
                    radioButtonBottomLeft.Checked = true;
                    break;

                case "3":
                    radioButtonBottomRight.Checked = true;
                    break;

                case "9":
                    radioButtonMiddleLeft.Checked = true;
                    break;

                case "10":
                    radioButtonMiddleCenter.Checked = true;
                    break;

                case "11":
                    radioButtonMiddleRight.Checked = true;
                    break;

                case "5":
                    radioButtonTopLeft.Checked = true;
                    break;

                case "6":
                    radioButtonTopCenter.Checked = true;
                    break;

                case "7":
                    radioButtonTopRight.Checked = true;
                    break;

                default:
                    radioButtonBottomCenter.Checked = true;
                    break;
                }
            }
            else
            {
                switch (style.Alignment)
                {
                case "1":
                    radioButtonBottomLeft.Checked = true;
                    break;

                case "3":
                    radioButtonBottomRight.Checked = true;
                    break;

                case "4":
                    radioButtonMiddleLeft.Checked = true;
                    break;

                case "5":
                    radioButtonMiddleCenter.Checked = true;
                    break;

                case "6":
                    radioButtonMiddleRight.Checked = true;
                    break;

                case "7":
                    radioButtonTopLeft.Checked = true;
                    break;

                case "8":
                    radioButtonTopCenter.Checked = true;
                    break;

                case "9":
                    radioButtonTopRight.Checked = true;
                    break;

                default:
                    radioButtonBottomCenter.Checked = true;
                    break;
                }
            }

            if (style.MarginLeft >= 0 && style.MarginLeft <= numericUpDownMarginLeft.Maximum)
            {
                numericUpDownMarginLeft.Value = style.MarginLeft;
            }
            else
            {
                numericUpDownMarginLeft.Value = 10;
            }

            if (style.MarginRight >= 0 && style.MarginRight <= numericUpDownMarginRight.Maximum)
            {
                numericUpDownMarginRight.Value = style.MarginRight;
            }
            else
            {
                numericUpDownMarginRight.Value = 10;
            }

            if (style.MarginVertical >= 0 && style.MarginVertical <= numericUpDownMarginVertical.Maximum)
            {
                numericUpDownMarginVertical.Value = style.MarginVertical;
            }
            else
            {
                numericUpDownMarginVertical.Value = 10;
            }

            if (style.BorderStyle == "3")
            {
                radioButtonOpaqueBox.Checked = true;
            }
            else
            {
                radioButtonOutline.Checked = true;
            }
        }
        public static SsaStyle GetSsaStyle(string styleName, string header)
        {
            var style = new SsaStyle { Name = styleName };

            int nameIndex = -1;
            int fontNameIndex = -1;
            int fontsizeIndex = -1;
            int primaryColourIndex = -1;
            int secondaryColourIndex = -1;
            int tertiaryColourIndex = -1;
            int outlineColourIndex = -1;
            int backColourIndex = -1;
            int boldIndex = -1;
            int italicIndex = -1;
            int underlineIndex = -1;
            int outlineIndex = -1;
            int shadowIndex = -1;
            int alignmentIndex = -1;
            int marginLIndex = -1;
            int marginRIndex = -1;
            int marginVIndex = -1;
            int borderStyleIndex = -1;

            if (header == null)
                header = DefaultHeader;

            foreach (string line in header.SplitToLines())
            {
                string s = line.Trim().ToLower();
                if (s.StartsWith("format:", StringComparison.Ordinal))
                {
                    if (line.Length > 10)
                    {
                        var format = line.ToLower().Substring(8).Split(',');
                        for (int i = 0; i < format.Length; i++)
                        {
                            string f = format[i].Trim().ToLower();
                            if (f == "name")
                                nameIndex = i;
                            else if (f == "fontname")
                                fontNameIndex = i;
                            else if (f == "fontsize")
                                fontsizeIndex = i;
                            else if (f == "primarycolour")
                                primaryColourIndex = i;
                            else if (f == "secondarycolour")
                                secondaryColourIndex = i;
                            else if (f == "tertiarycolour")
                                tertiaryColourIndex = i;
                            else if (f == "outlinecolour")
                                outlineColourIndex = i;
                            else if (f == "backcolour")
                                backColourIndex = i;
                            else if (f == "bold")
                                boldIndex = i;
                            else if (f == "italic")
                                italicIndex = i;
                            else if (f == "underline")
                                underlineIndex = i;
                            else if (f == "outline")
                                outlineIndex = i;
                            else if (f == "shadow")
                                shadowIndex = i;
                            else if (f == "alignment")
                                alignmentIndex = i;
                            else if (f == "marginl")
                                marginLIndex = i;
                            else if (f == "marginr")
                                marginRIndex = i;
                            else if (f == "marginv")
                                marginVIndex = i;
                            else if (f == "borderstyle")
                                borderStyleIndex = i;
                        }
                    }
                }
                else if (s.Replace(" ", string.Empty).StartsWith("style:", StringComparison.Ordinal))
                {
                    if (line.Length > 10)
                    {
                        style.RawLine = line;
                        var format = line.Substring(6).Split(',');
                        for (int i = 0; i < format.Length; i++)
                        {
                            string f = format[i].Trim().ToLower();
                            if (i == nameIndex)
                            {
                                style.Name = format[i].Trim();
                            }
                            else if (i == fontNameIndex)
                            {
                                style.FontName = f;
                            }
                            else if (i == fontsizeIndex)
                            {
                                int number;
                                if (int.TryParse(f, out number))
                                    style.FontSize = number;
                            }
                            else if (i == primaryColourIndex)
                            {
                                style.Primary = GetSsaColor(f, Color.White);
                            }
                            else if (i == secondaryColourIndex)
                            {
                                style.Secondary = GetSsaColor(f, Color.Yellow);
                            }
                            else if (i == tertiaryColourIndex)
                            {
                                style.Tertiary = GetSsaColor(f, Color.Yellow);
                            }
                            else if (i == outlineColourIndex)
                            {
                                style.Outline = GetSsaColor(f, Color.Black);
                            }
                            else if (i == backColourIndex)
                            {
                                style.Background = GetSsaColor(f, Color.Black);
                            }
                            else if (i == boldIndex)
                            {
                                style.Bold = f == "1";
                            }
                            else if (i == italicIndex)
                            {
                                style.Italic = f == "1";
                            }
                            else if (i == underlineIndex)
                            {
                                style.Underline = f == "1";
                            }
                            else if (i == outlineIndex)
                            {
                                int number;
                                if (int.TryParse(f, out number))
                                    style.OutlineWidth = number;
                            }
                            else if (i == shadowIndex)
                            {
                                int number;
                                if (int.TryParse(f, out number))
                                    style.ShadowWidth = number;
                            }
                            else if (i == alignmentIndex)
                            {
                                style.Alignment = f;
                            }
                            else if (i == marginLIndex)
                            {
                                int number;
                                if (int.TryParse(f, out number))
                                    style.MarginLeft = number;
                            }
                            else if (i == marginRIndex)
                            {
                                int number;
                                if (int.TryParse(f, out number))
                                    style.MarginRight = number;
                            }
                            else if (i == marginVIndex)
                            {
                                int number;
                                if (int.TryParse(f, out number))
                                    style.MarginVertical = number;
                            }
                            else if (i == borderStyleIndex)
                            {
                                style.BorderStyle = f;
                            }
                        }
                    }
                    if (styleName != null && style.Name != null && styleName.Equals(style.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        style.LoadedFromHeader = true;
                        return style;
                    }
                }
            }
            return new SsaStyle { Name = styleName };
        }
        private void buttonCopy_Click(object sender, EventArgs e)
        {
            if (listViewStyles.SelectedItems.Count == 1)
            {
                string styleName = listViewStyles.SelectedItems[0].Text;
                SsaStyle oldStyle = GetSsaStyle(styleName);
                var style = new SsaStyle(oldStyle); // Copy contructor
                style.Name = string.Format(Configuration.Settings.Language.SubStationAlphaStyles.CopyOfY, styleName);

                if (GetSsaStyle(style.Name).LoadedFromHeader)
                {
                    int count = 2;
                    bool doRepeat = true;
                    while (doRepeat)
                    {
                        style.Name = string.Format(Configuration.Settings.Language.SubStationAlphaStyles.CopyXOfY, count, styleName);
                        doRepeat = GetSsaStyle(style.Name).LoadedFromHeader;
                        count++;
                    }
                }

                _doUpdate = false;
                AddStyle(listViewStyles, style, Subtitle, _isSubStationAlpha);
                listViewStyles.Items[listViewStyles.Items.Count - 1].Selected = true;
                listViewStyles.Items[listViewStyles.Items.Count - 1].EnsureVisible();
                listViewStyles.Items[listViewStyles.Items.Count - 1].Focused = true;
                textBoxStyleName.Text = style.Name;
                textBoxStyleName.Focus();
                AddStyleToHeader(style, oldStyle);
                _doUpdate = true;
                listViewStyles_SelectedIndexChanged(null, null);
            }
        }
        private void AddStyleToHeader(SsaStyle newStyle, SsaStyle oldStyle)
        {
            if (listViewStyles.SelectedItems.Count == 1)
            {
                string newLine = oldStyle.RawLine;
                newLine = newLine.Replace(oldStyle.Name + ",", newStyle.Name + ",");

                int indexOfEvents = _header.IndexOf("[Events]", StringComparison.Ordinal);
                if (indexOfEvents > 0)
                {
                    int i = indexOfEvents - 1;
                    while (i > 0 && Environment.NewLine.Contains(_header[i]))
                        i--;
                    _header = _header.Insert(i + 1, Environment.NewLine + newLine);
                }
                else
                {
                    _header += Environment.NewLine + newLine;
                }
            }
        }
        public static void AddStyle(ListView lv, SsaStyle ssaStyle, Subtitle subtitle, bool isSubstationAlpha)
        {
            var item = new ListViewItem(ssaStyle.Name.Trim());
            item.UseItemStyleForSubItems = false;

            var subItem = new ListViewItem.ListViewSubItem(item, ssaStyle.FontName);
            item.SubItems.Add(subItem);

            subItem = new ListViewItem.ListViewSubItem(item, ssaStyle.FontSize.ToString());
            item.SubItems.Add(subItem);

            int count = 0;
            foreach (Paragraph p in subtitle.Paragraphs)
            {
                if (string.IsNullOrEmpty(p.Extra) && ssaStyle.Name.TrimStart('*') == "Default")
                    count++;
                else if (p.Extra != null && ssaStyle.Name.TrimStart('*').Equals(p.Extra.TrimStart('*'), StringComparison.OrdinalIgnoreCase))
                    count++;
            }
            subItem = new ListViewItem.ListViewSubItem(item, count.ToString());
            item.SubItems.Add(subItem);

            subItem = new ListViewItem.ListViewSubItem(item, string.Empty);
            subItem.BackColor = ssaStyle.Primary;
            item.SubItems.Add(subItem);

            subItem = new ListViewItem.ListViewSubItem(item, string.Empty);
            if (isSubstationAlpha)
                subItem.BackColor = ssaStyle.Background;
            else
                subItem.BackColor = ssaStyle.Outline;
            subItem.Text = Configuration.Settings.Language.General.Text;
            subItem.ForeColor = ssaStyle.Primary;
            try
            {
                if (ssaStyle.Bold || ssaStyle.Italic)
                    subItem.Font = new Font(ssaStyle.FontName, subItem.Font.Size, FontStyle.Bold | FontStyle.Italic);
                else if (ssaStyle.Bold)
                    subItem.Font = new Font(ssaStyle.FontName, subItem.Font.Size, FontStyle.Bold);
                else if (ssaStyle.Italic)
                    subItem.Font = new Font(ssaStyle.FontName, subItem.Font.Size, FontStyle.Italic);
                else if (ssaStyle.Italic)
                    subItem.Font = new Font(ssaStyle.FontName, subItem.Font.Size, FontStyle.Regular);
            }
            catch
            {
            }
            item.SubItems.Add(subItem);

            lv.Items.Add(item);
        }
        private void SetControlsFromStyle(SsaStyle style)
        {
            textBoxStyleName.Text = style.Name;
            textBoxStyleName.BackColor = listViewStyles.BackColor;
            comboBoxFontName.Text = style.FontName;
            checkBoxFontItalic.Checked = style.Italic;
            checkBoxFontBold.Checked = style.Bold;
            checkBoxFontUnderline.Checked = style.Underline;

            if (style.FontSize > 0 && style.FontSize <= numericUpDownFontSize.Maximum)
                numericUpDownFontSize.Value = style.FontSize;
            else
                numericUpDownFontSize.Value = 20;

            panelPrimaryColor.BackColor = style.Primary;
            panelSecondaryColor.BackColor = style.Secondary;
            if (_isSubStationAlpha)
                panelOutlineColor.BackColor = style.Tertiary;
            else
                panelOutlineColor.BackColor = style.Outline;
            panelBackColor.BackColor = style.Background;
            numericUpDownOutline.Value = style.OutlineWidth;
            numericUpDownShadowWidth.Value = style.ShadowWidth;

            if (_isSubStationAlpha)
            {
                switch (style.Alignment)
                {
                    case "1":
                        radioButtonBottomLeft.Checked = true;
                        break;
                    case "3":
                        radioButtonBottomRight.Checked = true;
                        break;
                    case "9":
                        radioButtonMiddleLeft.Checked = true;
                        break;
                    case "10":
                        radioButtonMiddleCenter.Checked = true;
                        break;
                    case "11":
                        radioButtonMiddleRight.Checked = true;
                        break;
                    case "5":
                        radioButtonTopLeft.Checked = true;
                        break;
                    case "6":
                        radioButtonTopCenter.Checked = true;
                        break;
                    case "7":
                        radioButtonTopRight.Checked = true;
                        break;
                    default:
                        radioButtonBottomCenter.Checked = true;
                        break;
                }
            }
            else
            {
                switch (style.Alignment)
                {
                    case "1":
                        radioButtonBottomLeft.Checked = true;
                        break;
                    case "3":
                        radioButtonBottomRight.Checked = true;
                        break;
                    case "4":
                        radioButtonMiddleLeft.Checked = true;
                        break;
                    case "5":
                        radioButtonMiddleCenter.Checked = true;
                        break;
                    case "6":
                        radioButtonMiddleRight.Checked = true;
                        break;
                    case "7":
                        radioButtonTopLeft.Checked = true;
                        break;
                    case "8":
                        radioButtonTopCenter.Checked = true;
                        break;
                    case "9":
                        radioButtonTopRight.Checked = true;
                        break;
                    default:
                        radioButtonBottomCenter.Checked = true;
                        break;
                }
            }

            if (style.MarginLeft >= 0 && style.MarginLeft <= numericUpDownMarginLeft.Maximum)
                numericUpDownMarginLeft.Value = style.MarginLeft;
            else
                numericUpDownMarginLeft.Value = 10;

            if (style.MarginRight >= 0 && style.MarginRight <= numericUpDownMarginRight.Maximum)
                numericUpDownMarginRight.Value = style.MarginRight;
            else
                numericUpDownMarginRight.Value = 10;

            if (style.MarginVertical >= 0 && style.MarginVertical <= numericUpDownMarginVertical.Maximum)
                numericUpDownMarginVertical.Value = style.MarginVertical;
            else
                numericUpDownMarginVertical.Value = 10;

            if (style.BorderStyle == "3")
            {
                radioButtonOpaqueBox.Checked = true;
            }
            else
            {
                radioButtonOutline.Checked = true;
            }
        }
Example #13
0
        public override string ToText(Subtitle subtitle, string title)
        {
            const string header =
                @"[Script Info]
; This is a Sub Station Alpha v4 script.
Title: {0}
ScriptType: v4.00
Collisions: Normal
PlayDepth: 0

[V4 Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding
Style: Default,{1},{2},{3},65535,65535,-2147483640,{9},0,1,{4},{5},2,{6},{7},{8},0,1

[Events]
Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text";



            const string timeCodeFormat       = "{0}:{1:00}:{2:00}.{3:00}"; // h:mm:ss.cc
            const string paragraphWriteFormat = "Dialogue: Marked={4},{0},{1},{3},{5},{6},{7},{8},{9},{2}";
            const string commentWriteFormat   = "Comment: Marked={4},{0},{1},{3},{5},{6},{7},{8},{9},{2}";

            var sb = new StringBuilder();
            var isValidSsaHeader = !string.IsNullOrEmpty(subtitle.Header) && subtitle.Header.Contains("[V4 Styles]");
            var styles           = new List <string>();

            if (isValidSsaHeader)
            {
                sb.AppendLine(subtitle.Header.Trim());
                const string formatLine = "Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text";
                if (!subtitle.Header.Contains(formatLine))
                {
                    sb.AppendLine(formatLine);
                }

                styles = AdvancedSubStationAlpha.GetStylesFromHeader(subtitle.Header);
            }
            else if (!string.IsNullOrEmpty(subtitle.Header) && subtitle.Header.Contains("[V4+ Styles]"))
            {
                LoadStylesFromAdvancedSubstationAlpha(subtitle, title, subtitle.Header, HeaderNoStyles, sb);
                isValidSsaHeader = true;
                styles           = AdvancedSubStationAlpha.GetStylesFromHeader(subtitle.Header);
            }
            else if (subtitle.Header != null && subtitle.Header.Contains("http://www.w3.org/ns/ttml"))
            {
                LoadStylesFromTimedText10(subtitle, title, header, HeaderNoStyles, sb);
            }
            else
            {
                SsaStyle style             = null;
                var      storageCategories = Configuration.Settings.SubtitleSettings.AssaStyleStorageCategories;
                if (storageCategories != null && storageCategories.Count > 0 && storageCategories.Exists(x => x.IsDefault))
                {
                    var defaultStyle = storageCategories.FirstOrDefault(x => x.IsDefault)?.Styles.FirstOrDefault(x => x.Name.ToLowerInvariant() == "default");
                    style = defaultStyle ?? storageCategories.FirstOrDefault(x => x.IsDefault)?.Styles[0];
                }

                style = style ?? new SsaStyle();

                var boldStyle = "0"; // 0=regular
                if (style.Bold)
                {
                    boldStyle = "-1"; // -1 = true, 0 is false
                }

                sb.AppendLine(string.Format(header,
                                            title,
                                            style.FontName,
                                            style.FontSize,
                                            ColorTranslator.ToWin32(style.Primary),
                                            style.OutlineWidth,
                                            style.ShadowWidth,
                                            style.MarginLeft,
                                            style.MarginRight,
                                            style.MarginVertical,
                                            boldStyle
                                            ));
            }
            foreach (var p in subtitle.Paragraphs)
            {
                var start = string.Format(timeCodeFormat, p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds / 10);
                var end   = string.Format(timeCodeFormat, p.EndTime.Hours, p.EndTime.Minutes, p.EndTime.Seconds, p.EndTime.Milliseconds / 10);
                var style = "Default";
                var actor = "NTP";
                if (!string.IsNullOrEmpty(p.Actor))
                {
                    actor = p.Actor;
                }

                var marginL = "0000";
                if (!string.IsNullOrEmpty(p.MarginL) && Utilities.IsInteger(p.MarginL))
                {
                    marginL = p.MarginL.PadLeft(4, '0');
                }

                var marginR = "0000";
                if (!string.IsNullOrEmpty(p.MarginR) && Utilities.IsInteger(p.MarginR))
                {
                    marginR = p.MarginR.PadLeft(4, '0');
                }

                string marginV = "0000";
                if (!string.IsNullOrEmpty(p.MarginV) && Utilities.IsInteger(p.MarginV))
                {
                    marginV = p.MarginV.PadLeft(4, '0');
                }

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

                if (!string.IsNullOrEmpty(p.Extra) && isValidSsaHeader && styles.Contains(p.Extra))
                {
                    style = p.Extra;
                }

                if (style == "Default")
                {
                    style = "*Default";
                }

                if (p.IsComment)
                {
                    sb.AppendLine(string.Format(commentWriteFormat, start, end, AdvancedSubStationAlpha.FormatText(p), style, p.Layer, actor, marginL, marginR, marginV, effect));
                }
                else
                {
                    sb.AppendLine(string.Format(paragraphWriteFormat, start, end, AdvancedSubStationAlpha.FormatText(p), style, p.Layer, actor, marginL, marginR, marginV, effect));
                }
            }
            return(sb.ToString().Trim() + Environment.NewLine);
        }
Example #14
0
        private List <AssaStorageCategory> ImportGroupsFile(string fileName)
        {
            var list = new List <AssaStorageCategory>();
            var doc  = new XmlDocument {
                XmlResolver = null
            };

            doc.Load(fileName);
            var categories = doc.DocumentElement?.SelectNodes($"//{Category}");

            if (categories != null)
            {
                foreach (XmlNode categoryNode in categories)
                {
                    var category      = new AssaStorageCategory();
                    var nameNode      = categoryNode.SelectSingleNode(CategoryName);
                    var isDefaultNode = categoryNode.SelectSingleNode(CategoryIsDefault);

                    category.Name      = nameNode?.InnerText ?? "Untitled";
                    category.IsDefault = isDefaultNode != null && Convert.ToBoolean(isDefaultNode.InnerText);

                    category.Styles = new List <SsaStyle>();
                    list.Add(category);

                    var styles = categoryNode.SelectNodes("Style");
                    if (styles != null)
                    {
                        foreach (XmlNode styleNode in styles)
                        {
                            var style   = new SsaStyle();
                            var subNode = styleNode.SelectSingleNode("Name");
                            if (subNode != null)
                            {
                                style.Name = subNode.InnerText;
                            }

                            subNode = styleNode.SelectSingleNode(StyleFontName);
                            if (subNode != null)
                            {
                                style.FontName = subNode.InnerText;
                            }

                            subNode = styleNode.SelectSingleNode("FontSize");
                            if (subNode != null)
                            {
                                style.FontSize = Convert.ToSingle(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("Primary");
                            if (subNode != null)
                            {
                                style.Primary = ColorTranslator.FromHtml(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("Secondary");
                            if (subNode != null)
                            {
                                style.Secondary = ColorTranslator.FromHtml(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("Outline");
                            if (subNode != null)
                            {
                                style.Outline = ColorTranslator.FromHtml(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("Background");
                            if (subNode != null)
                            {
                                style.Background = ColorTranslator.FromHtml(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("ShadowWidth");
                            if (subNode != null)
                            {
                                style.ShadowWidth = Convert.ToDecimal(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("OutlineWidth");
                            if (subNode != null)
                            {
                                style.OutlineWidth = Convert.ToDecimal(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("Alignment");
                            if (subNode != null)
                            {
                                style.Alignment = subNode.InnerText;
                            }

                            subNode = styleNode.SelectSingleNode("MarginLeft");
                            if (subNode != null)
                            {
                                style.MarginLeft = Convert.ToInt32(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("MarginRight");
                            if (subNode != null)
                            {
                                style.MarginRight = Convert.ToInt32(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("MarginVertical");
                            if (subNode != null)
                            {
                                style.MarginVertical = Convert.ToInt32(subNode.InnerText);
                            }

                            subNode = styleNode.SelectSingleNode("BorderStyle");
                            if (subNode != null)
                            {
                                style.BorderStyle = subNode.InnerText;
                            }

                            category.Styles.Add(style);
                        }
                    }
                }
            }

            return(list);
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            var exportNames        = new List <string>();
            var occursMoreThanOnce = new List <string>();

            ExportedStyles = new List <string>();
            foreach (ListViewItem item in listViewExportStyles.Items)
            {
                if (item.Checked)
                {
                    ExportedStyles.Add(item.Text);
                    if (exportNames.Contains(item.Text))
                    {
                        occursMoreThanOnce.Add(item.Text);
                    }
                    else
                    {
                        exportNames.Add(item.Text);
                    }
                }
            }

            if (occursMoreThanOnce.Count > 0)
            {
                MessageBox.Show("Style name must be unique - can only export one style with name: " + string.Join(", ", occursMoreThanOnce));
                return;
            }

            if (ExportedStyles.Count == 0)
            {
                return;
            }

            saveFileDialogStyle.Title            = LanguageSettings.Current.SubStationAlphaStyles.ExportStyleToFile;
            saveFileDialogStyle.InitialDirectory = Configuration.DataDirectory;
            if (_isSubStationAlpha)
            {
                saveFileDialogStyle.Filter   = SubStationAlpha.NameOfFormat + "|*.ssa|" + LanguageSettings.Current.General.AllFiles + "|*.*";
                saveFileDialogStyle.FileName = "my_styles.ssa";
            }
            else
            {
                saveFileDialogStyle.Filter   = AdvancedSubStationAlpha.NameOfFormat + "|*.ass|" + LanguageSettings.Current.General.AllFiles + "|*.*";
                saveFileDialogStyle.FileName = "my_styles.ass";
            }

            if (saveFileDialogStyle.ShowDialog(this) == DialogResult.OK)
            {
                if (File.Exists(saveFileDialogStyle.FileName))
                {
                    var s      = new Subtitle();
                    var format = s.LoadSubtitle(saveFileDialogStyle.FileName, out _, null);
                    if (format == null)
                    {
                        MessageBox.Show("Not subtitle format: " + _format.Name);
                        return;
                    }

                    if (format.Name != _format.Name)
                    {
                        MessageBox.Show(string.Format("Cannot save {1} style in {0} file!", format.Name, _format.Name));
                    }
                    else
                    {
                        var    sb          = new StringBuilder();
                        bool   stylesOn    = false;
                        bool   done        = false;
                        string styleFormat = SsaStyle.DefaultAssStyleFormat;
                        foreach (string line in File.ReadAllLines(saveFileDialogStyle.FileName))
                        {
                            if (line.StartsWith("format:", StringComparison.OrdinalIgnoreCase))
                            {
                                styleFormat = line;
                            }
                            else if (line.StartsWith("style:", StringComparison.OrdinalIgnoreCase))
                            {
                                stylesOn = true;
                            }
                            else if (stylesOn && !done)
                            {
                                done = true;
                                foreach (var styleName in ExportedStyles)
                                {
                                    SsaStyle style = _stylesToExport.Single(styleItem => styleItem.Name == styleName);
                                    if (_isSubStationAlpha)
                                    {
                                        sb.AppendLine(style.ToRawSsa(styleFormat));
                                    }
                                    else
                                    {
                                        sb.AppendLine(style.ToRawAss(styleFormat));
                                    }
                                }
                            }
                            sb.AppendLine(line);
                            foreach (var styleName in ExportedStyles)
                            {
                                var toLower = line.Trim().ToLowerInvariant();
                                while (toLower.Contains(": "))
                                {
                                    toLower = toLower.Replace(": ", ":");
                                }

                                while (toLower.Contains(" :"))
                                {
                                    toLower = toLower.Replace(" :", ":");
                                }

                                if (stylesOn && toLower.StartsWith("style:" + styleName.Trim() + ",", StringComparison.OrdinalIgnoreCase))
                                {
                                    MessageBox.Show(string.Format(LanguageSettings.Current.SubStationAlphaStyles.StyleAlreadyExits, styleName));
                                    return;
                                }
                            }
                        }
                        File.WriteAllText(saveFileDialogStyle.FileName, sb.ToString(), Encoding.UTF8);
                    }
                }
                else
                {
                    var sb = new StringBuilder();
                    foreach (var styleName in ExportedStyles)
                    {
                        var style = _stylesToExport.First(styleItem => styleItem.Name == styleName);
                        sb.Append(style.ToRawAss());
                        sb.Append(Environment.NewLine);
                    }

                    string content = string.Format(AdvancedSubStationAlpha.HeaderNoStyles, "Subtitle Edit styles file", sb.ToString().Trim());
                    content = content.Trim() + Environment.NewLine + "Dialogue: 0,0:00:31.91,0:00:33.91,Default,,0,0,0,,My Styles :)";
                    File.WriteAllText(saveFileDialogStyle.FileName, content, Encoding.UTF8);
                    DialogResult = DialogResult.OK;
                }
            }
        }