Esempio n. 1
0
        /// <summary>
        /// Even after taking months to fix a regression that completely broke the use of multiple formats
        /// in one subtitle, YouTube still hasn't got the Javascript code down. While changing the background
        /// color in the middle of a line now works again, changing it before or after a line break results
        /// in the rounded corners disappearing on one side. It doesn't look pretty.
        /// The only workaround seems to be to split the subtitle in two.
        /// </summary>
        private void SplitMultiBackgroundLine(int lineIndex)
        {
            Line line          = Lines[lineIndex];
            int  numLineBreaks = line.Sections.SelectMany(s => s.Text).Count(c => c == '\n');

            if (numLineBreaks != 1)
            {
                return;
            }

            int secondSubLineStartSectionIdx = -1;

            for (int i = 1; i < line.Sections.Count; i++)
            {
                Section prevSection = line.Sections[i - 1];
                Section section     = line.Sections[i];
                if ((prevSection.BackColor != section.BackColor || prevSection.Font != section.Font || prevSection.Scale != section.Scale) &&
                    (prevSection.Text.EndsWith("\r\n") || section.Text.StartsWith("\r\n")))
                {
                    secondSubLineStartSectionIdx = i;
                    break;
                }
            }

            if (secondSubLineStartSectionIdx < 0)
            {
                return;
            }

            Line secondLine = (Line)line.Clone();

            line.Sections.RemoveRange(secondSubLineStartSectionIdx, line.Sections.Count - secondSubLineStartSectionIdx);
            line.Sections[secondSubLineStartSectionIdx - 1].Text = line.Sections[secondSubLineStartSectionIdx - 1].Text.Replace("\r\n", "");
            secondLine.Sections.RemoveRange(0, secondSubLineStartSectionIdx);
            secondLine.Sections[0].Text = secondLine.Sections[0].Text.Replace("\r\n", "");

            PointF position = line.Position ?? GetDefaultPosition(line.AnchorPoint);

            if (AnchorPointUtil.IsTopAligned(line.AnchorPoint))
            {
                position.Y      += VideoDimensions.Height * 0.05f;
                line.AnchorPoint = AnchorPointUtil.GetVerticalOpposite(line.AnchorPoint);
            }
            else if (AnchorPointUtil.IsMiddleAligned(line.AnchorPoint))
            {
                line.AnchorPoint       = AnchorPointUtil.MakeBottomAligned(line.AnchorPoint);
                secondLine.AnchorPoint = AnchorPointUtil.MakeTopAligned(secondLine.AnchorPoint);
            }
            else
            {
                position.Y            -= VideoDimensions.Height * 0.05f;
                secondLine.AnchorPoint = AnchorPointUtil.GetVerticalOpposite(secondLine.AnchorPoint);
            }

            line.Position       = position;
            secondLine.Position = position;
            Lines.Insert(lineIndex + 1, secondLine);
        }
Esempio n. 2
0
        private static string GetVerticalAlign(AssStyle style, AssStyleOptions options)
        {
            if (!(options?.HasExistingBackgroundImage ?? false))
            {
                return("middle");
            }

            if (AnchorPointUtil.IsTopAligned(style.AnchorPoint))
            {
                return("top");
            }

            if (AnchorPointUtil.IsBottomAligned(style.AnchorPoint))
            {
                return("bottom");
            }

            return("middle");
        }