Esempio n. 1
0
        public SsaStyle(SsaStyle ssaStyle)
        {
            Name     = ssaStyle.Name;
            FontName = ssaStyle.FontName;
            FontSize = ssaStyle.FontSize;

            Italic    = ssaStyle.Italic;
            Bold      = ssaStyle.Bold;
            Underline = ssaStyle.Underline;

            Primary    = ssaStyle.Primary;
            Secondary  = ssaStyle.Secondary;
            Tertiary   = ssaStyle.Tertiary;
            Outline    = ssaStyle.Outline;
            Background = ssaStyle.Background;

            ShadowWidth  = ssaStyle.ShadowWidth;
            OutlineWidth = ssaStyle.OutlineWidth;

            Alignment      = ssaStyle.Alignment;
            MarginLeft     = ssaStyle.MarginLeft;
            MarginRight    = ssaStyle.MarginRight;
            MarginVertical = ssaStyle.MarginVertical;

            BorderStyle      = ssaStyle.BorderStyle;
            RawLine          = ssaStyle.RawLine;
            LoadedFromHeader = ssaStyle.LoadedFromHeader;
        }
Esempio n. 2
0
        public SsaStyle(SsaStyle ssaStyle)
        {
            Name = ssaStyle.Name;
            FontName = ssaStyle.FontName;
            FontSize = ssaStyle.FontSize;

            Italic = ssaStyle.Italic;
            Bold = ssaStyle.Bold;
            Underline = ssaStyle.Underline;

            Primary = ssaStyle.Primary;
            Secondary = ssaStyle.Secondary;
            Tertiary = ssaStyle.Tertiary;
            Outline = ssaStyle.Outline;
            Background = ssaStyle.Background;

            ShadowWidth = ssaStyle.ShadowWidth;
            OutlineWidth = ssaStyle.OutlineWidth;

            Alignment = ssaStyle.Alignment;
            MarginLeft = ssaStyle.MarginLeft;
            MarginRight = ssaStyle.MarginRight;
            MarginVertical = ssaStyle.MarginVertical;

            BorderStyle = ssaStyle.BorderStyle;
            RawLine = ssaStyle.RawLine;
            LoadedFromHeader = ssaStyle.LoadedFromHeader;
        }
        /// <summary>
        /// The get ssa style.
        /// </summary>
        /// <param name="styleName">
        /// The style name.
        /// </param>
        /// <param name="header">
        /// The header.
        /// </param>
        /// <returns>
        /// The <see cref="SsaStyle"/>.
        /// </returns>
        public static SsaStyle GetSsaStyle(string styleName, string header)
        {
            SsaStyle 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)
                    {
                        string[] 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;
                        string[] 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 };
        }
        /// <summary>
        /// Add new style to ASS header
        /// </summary>
        /// <param name="style">
        /// The style.
        /// </param>
        /// <param name="header">
        /// The header.
        /// </param>
        /// <returns>
        /// Header with new style
        /// </returns>
        public static string AddSsaStyle(SsaStyle style, string header)
        {
            if (string.IsNullOrEmpty(header))
            {
                header = DefaultHeader;
            }

            StringBuilder 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.SplitToLines())
            {
                if (line.Equals("[V4+ Styles]", StringComparison.OrdinalIgnoreCase) || line.Equals("[V4 Styles]", StringComparison.OrdinalIgnoreCase))
                {
                    stylesStarted = true;
                }

                if (line.StartsWith("format:", StringComparison.OrdinalIgnoreCase))
                {
                    styleFormat = line;
                }

                if (!line.StartsWith("Style: " + style.Name + ",", StringComparison.Ordinal))
                {
                    // overwrite existing style
                    sb.AppendLine(line);
                }

                if (!styleAdded && stylesStarted && line.TrimStart().StartsWith("style:", StringComparison.OrdinalIgnoreCase))
                {
                    sb.AppendLine(style.ToRawAss(styleFormat));
                    styleAdded = true;
                }
            }

            return sb.ToString();
        }
        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;
            }
        }