Example #1
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('>');
        }
Example #2
0
        /// <summary>
        /// Utility method to write a caption cue into a buffer.
        /// </summary>
        /// <param name="cue">Cue to write.</param>
        /// <param name="builder">String builder buffer to write into.</param>
        private static void WriteCue(
            Cue cue,
            StringBuilder builder)
        {
            if (cue.End <= cue.Start)
            {
                throw new ArgumentException(string.Format("Cue start time '{0}' must be less than cue end time '{1}'.", cue.Start, cue.End));
            }

            builder.AppendLine();
            if (false == string.IsNullOrWhiteSpace(cue.Id))
            {
                builder.AppendLine(cue.Id);
            }

            WebVttSerializer.WriteTimeSpanValue(cue.Start, builder);
            builder
            .Append(' ')
            .Append(Constants.ArrowToken)
            .Append(' ');
            WebVttSerializer.WriteTimeSpanValue(cue.End, builder);

            if (false == string.IsNullOrWhiteSpace(cue.Region))
            {
                if (WebVttSerializer.HasWhiteSpace(cue.Region))
                {
                    throw new ArgumentException("White space characters are not allowed in cue region id.");
                }

                builder
                .Append(' ')
                .Append(Constants.RegionName).Append(':').Append(cue.Region);
            }

            if (cue.Alignment.HasValue)
            {
                builder
                .Append(' ')
                .Append(Constants.AlignName).Append(':').Append(WebVttSerializer.GetAlignmentValue(cue.Alignment.Value));
            }

            if (cue.Line.HasValue)
            {
                builder
                .Append(' ')
                .Append(Constants.LineName).Append(':');

                if (cue.Line.Value.Percent.HasValue)
                {
                    builder.Append(WebVttSerializer.GetPercentValue(cue.Line.Value.Percent.Value));
                }
                else if (cue.Line.Value.LineNumber.HasValue)
                {
                    builder.Append(cue.Line.Value.LineNumber.Value);
                }
                else
                {
                    throw new ArgumentException("Cue line setting must specify either percent or line number value.");
                }

                if (cue.Line.Value.Alignment.HasValue)
                {
                    builder.Append(',').Append(WebVttSerializer.GetLineAlignmentValue(cue.Line.Value.Alignment.Value));
                }
            }

            if (cue.Position.HasValue)
            {
                builder
                .Append(' ')
                .Append(Constants.PositionName).Append(':')
                .Append(WebVttSerializer.GetPercentValue(
                            cue.Position.Value.PositionPercent.HasValue
                        ? cue.Position.Value.PositionPercent.Value
                        : 0.0));

                if (cue.Position.Value.Alignment.HasValue)
                {
                    builder
                    .Append(',')
                    .Append(WebVttSerializer.GetPositionAlignmentValue(cue.Position.Value.Alignment.Value));
                }
            }

            if (cue.SizePercent.HasValue)
            {
                builder
                .Append(' ')
                .Append(Constants.SizeName)
                .Append(":")
                .Append(WebVttSerializer.GetPercentValue(cue.SizePercent.Value));
            }

            if (cue.Vertical.HasValue)
            {
                builder
                .Append(' ')
                .Append(Constants.VerticalName)
                .Append(":")
                .Append(WebVttSerializer.GetVerticalAlignmentValue(cue.Vertical.Value));
            }

            builder.AppendLine();

            if (cue.Content != null && cue.Content.Length > 0)
            {
                Span previousSpan = null;
                foreach (var span in cue.Content)
                {
                    if (span == null)
                    {
                        throw new ArgumentException("Cue content cannot be null.");
                    }

                    if (WebVttSerializer.NeedNewLine(previousSpan, span))
                    {
                        builder.AppendLine();
                    }

                    WebVttSerializer.WriteSpan(span, builder);
                    previousSpan = span;
                }

                builder.AppendLine();
            }
        }