Esempio n. 1
0
        /// <summary>
        /// Utility method to write style block into a buffer.
        /// </summary>
        /// <param name="style">Style block to write.</param>
        /// <param name="builder">String builder buffer to write into.</param>
        private static void WrteStyle(
            Style style,
            StringBuilder builder)
        {
            if (false == string.IsNullOrWhiteSpace(style.RawContent))
            {
                if (style.RawContent.Contains(Constants.ArrowToken))
                {
                    throw new ArgumentException(string.Format("Style cannot contain '{0}'.", Constants.ArrowToken));
                }

                builder.AppendLine();
                builder.Append(Constants.StyleToken);

                if (style.RawContent.IndexOfAny(WebVttSerializer.newLine) >= 0)
                {
                    builder.AppendLine();
                    WebVttSerializer.WriteString(style.RawContent.TrimEnd(WebVttSerializer.newLine), false, builder);
                }
                else
                {
                    builder.Append(" ");
                    builder.Append(style.RawContent);
                }

                builder.AppendLine();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Utility method to write region definition block into a buffer.
        /// </summary>
        /// <param name="region">Region definition to write.</param>
        /// <param name="builder">String builder buffer to write into.</param>
        private static void WriteRegion(
            RegionDefinition region,
            StringBuilder builder)
        {
            builder.AppendLine();
            builder.AppendLine(Constants.RegionToken);

            if (false == string.IsNullOrWhiteSpace(region.Id))
            {
                builder.Append(Constants.RegionIdName).Append(":");
                WebVttSerializer.WriteString(region.Id, true, builder);
                builder.AppendLine();
            }

            if (region.Lines.HasValue &&
                region.Lines.Value >= 0)
            {
                builder.Append(Constants.LinesName).Append(":").AppendLine(region.Lines.Value.ToString());
            }

            if (region.WidthPercent.HasValue)
            {
                builder
                .Append(Constants.WidthName)
                .Append(":")
                .AppendLine(WebVttSerializer.GetPercentValue(region.WidthPercent.Value));
            }

            if (region.RegionAnchor.HasValue)
            {
                builder
                .Append(Constants.RegionAnchorName).Append(":")
                .Append(WebVttSerializer.GetPercentValue(region.RegionAnchor.Value.XPercent))
                .Append(',')
                .AppendLine(WebVttSerializer.GetPercentValue(region.RegionAnchor.Value.YPercent));
            }

            if (region.ViewPortAnchor.HasValue)
            {
                builder
                .Append(Constants.ViewPortAnchorName).Append(":")
                .Append(WebVttSerializer.GetPercentValue(region.ViewPortAnchor.Value.XPercent))
                .Append(',')
                .AppendLine(WebVttSerializer.GetPercentValue(region.ViewPortAnchor.Value.YPercent));
            }

            if (region.Scroll.HasValue && region.Scroll.Value)
            {
                builder.Append(Constants.ScrollName).Append(':').AppendLine(Constants.ScrollUpValue);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Utility method to write a single cue span into a buffer.
        /// </summary>
        /// <param name="span">Cue span to write.</param>
        /// <param name="builder">String builder buffer to write into.</param>
        private static void WriteSpan(
            Span span,
            StringBuilder builder)
        {
            if (span.Type == SpanType.Text)
            {
                if (string.IsNullOrEmpty(span.Text))
                {
                    return;
                }

                if (span.Text.Contains(Constants.ArrowToken))
                {
                    throw new ArgumentException("Cue text cannot contain '{0}'.", Constants.ArrowToken);
                }

                WebVttSerializer.WriteString(span.Text, true, builder);
                return;
            }

            string tagName = WebVttSerializer.GetSpanTagName(span.Type);

            builder.Append('<').Append(tagName);

            if (span.Classes != null && span.Classes.Length > 0)
            {
                foreach (string cls in span.Classes)
                {
                    if (false == string.IsNullOrWhiteSpace(cls))
                    {
                        if (WebVttSerializer.HasWhiteSpace(cls))
                        {
                            throw new ArgumentException("White space characters are not allowed in span class name.");
                        }

                        builder.Append('.').Append(cls);
                    }
                }
            }

            if (false == string.IsNullOrWhiteSpace(span.Annotation))
            {
                builder.Append(' ');
                WebVttSerializer.WriteString(span.Annotation, true, builder);
            }

            builder.Append('>');

            if (span.Children != null && span.Children.Length > 0)
            {
                foreach (var child in span.Children)
                {
                    if (child != null)
                    {
                        WebVttSerializer.WriteSpan(child, builder);
                    }
                }
            }

            builder.Append("</").Append(tagName).Append('>');
        }