コード例 #1
0
        private static WebVTTCue ParseCue(string block, TimeSpan timeOffset)
        {
            var cueReader = new StringReader(block);

            WebVTTCue result = new WebVTTCue();
            var       line   = cueReader.ReadLine();

            if (!line.Contains(cueIdentifier))
            {
                result.StyleClass = line;
                line = cueReader.ReadLine();
            }

            var parts = line.Split(cueHeaderSeparator, StringSplitOptions.RemoveEmptyEntries);

            if (parts[1] != cueIdentifier)
            {
                throw new Exception("Invalid WebVTT cue time separator");
            }
            result.Begin = ParseTimeStamp(parts[0]).Add(timeOffset);
            result.End   = ParseTimeStamp(parts[2]).Add(timeOffset);
            if (parts.Length >= 3)
            {
                var settingsDictionary = parts.Skip(3)
                                         .Select(p => p.Split(':'))
                                         .ToDictionary(i => i[0], i => i[1]);
                result.Settings = ParseCueSettings(settingsDictionary);
            }

            StringBuilder cueContent = new StringBuilder();

            do
            {
                line = cueReader.ReadLine();
                if (line == null)
                {
                    break;
                }
                if (line.Contains(cueIdentifier))
                {
                    throw new Exception("Invalid WebVTT cue content");
                }
                if (cueContent.Length > 0)
                {
                    cueContent.AppendLine();
                }
                cueContent.Append(line);
            } while (true);
            result.Content = ParseCueContent(cueContent.ToString(), timeOffset);

            return(result);
        }
コード例 #2
0
        public BoxElement GetRenderedCue(WebVTTCue cue)
        {
            var alignment = cue.Settings.Alignment;

            var result = new BoxElement();

            if (OutlineWidth > 0)
            {
                // create adjacent textblocks
                var textBlockOutline1 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.Left, 
                    new TranslateTransform() { X = -OutlineWidth, Y = 0 });
                textBlockOutline1.Margin = new Thickness(OutlineWidth);
                var textBlockOutline2 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.Top, 
                    new TranslateTransform { X = 0, Y = -OutlineWidth });
                textBlockOutline2.Margin = new Thickness(OutlineWidth);
                var textBlockOutline3 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.Right, 
                    new TranslateTransform { X = OutlineWidth, Y = 0 });
                textBlockOutline3.Margin = new Thickness(OutlineWidth);
                var textBlockOutline4 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.Bottom, 
                    new TranslateTransform { X = 0, Y = OutlineWidth });
                textBlockOutline4.Margin = new Thickness(OutlineWidth);

                // create diagonal textblocks
                var textBlockOutline5 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.TopLeft, 
                    new TranslateTransform { X = -OutlineWidth, Y = -OutlineWidth });
                textBlockOutline5.Margin = new Thickness(OutlineWidth);
                var textBlockOutline6 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.TopRight, 
                    new TranslateTransform { X = OutlineWidth, Y = -OutlineWidth });
                textBlockOutline6.Margin = new Thickness(OutlineWidth);
                var textBlockOutline7 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.BottomRight, 
                    new TranslateTransform { X = OutlineWidth, Y = OutlineWidth });
                textBlockOutline7.Margin = new Thickness(OutlineWidth);
                var textBlockOutline8 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.BottomLeft, 
                    new TranslateTransform { X = -OutlineWidth, Y = OutlineWidth });
                textBlockOutline8.Margin = new Thickness(OutlineWidth);

                result.AddBlock(textBlockOutline1);
                result.AddBlock(textBlockOutline2);
                result.AddBlock(textBlockOutline3);
                result.AddBlock(textBlockOutline4);
                result.AddBlock(textBlockOutline5);
                result.AddBlock(textBlockOutline6);
                result.AddBlock(textBlockOutline7);
                result.AddBlock(textBlockOutline8);
            }

            var textBlock = GetRenderedCueTextBlock(cue, alignment, InnerBrush, TextPosition.Center);
            textBlock.Margin = new Thickness(OutlineWidth);
            result.AddBlock(textBlock);

            return result;
        }
コード例 #3
0
        private TextBlock GetRenderedCueTextBlock(WebVTTCue cue, WebVTTAlignment alignment, Brush brush, TextPosition textPosition, Transform transform = null)
        {
            var content = cue.Content;

            var result = new TextBlock
            {
                TextWrapping    = TextWrapping.Wrap,
                RenderTransform = transform,
                Foreground      = brush
            };

            switch (alignment)
            {
            case WebVTTAlignment.Middle:
                result.TextAlignment = TextAlignment.Center;
                break;

            case WebVTTAlignment.Left:
                result.TextAlignment = TextAlignment.Left;
                break;

            case WebVTTAlignment.Right:
                result.TextAlignment = TextAlignment.Right;
                break;

            case WebVTTAlignment.Start:
                result.TextAlignment = TextAlignment.Left;
                break;

            case WebVTTAlignment.End:
                result.TextAlignment = TextAlignment.Right;
                break;
            }

            CreateInlines(cue, content, result.Inlines, brush);

            // If a Text Rendering event handler has been added, call it.
            if (this.TextRendering != null)
            {
                this.TextRendering(this, new CaptionTextEventArgs(result, textPosition));
            }

            return(result);
        }
コード例 #4
0
 private void CreateInlines(WebVTTCue cue, IWebVTTInternalNode node, InlineCollection inlines, Brush brush)
 {
     foreach (var child in node.Nodes)
     {
         var inline = CreateInline(child, brush);
         if (inline != null)
         {
             inlines.Add(inline);
             if (inline is Span && child is IWebVTTInternalNode)
             {
                 CreateInlines(cue, (IWebVTTInternalNode)child, ((Span)inline).Inlines, brush);
             }
             if (NodeRendering != null)
             {
                 NodeRendering(this, new NodeRenderingEventArgs(cue, child, inline));
             }
         }
     }
 }
コード例 #5
0
        private void AddCue(WebVTTCue cue)
        {
            BoxElement element = renderer.GetRenderedCue(cue);

            if (BoxStyle != null)
            {
                element.Style = BoxStyle;
            }
            element.FontSize = currentFontSize;
            WebVTTLayoutPanel.SetAlignment(element, GetPanelAlignment(cue.Settings.Alignment));
            WebVTTLayoutPanel.SetDirection(element, GetPanelAlignment(cue.Settings.WritingMode, cue.Content));
            WebVTTLayoutPanel.SetLinePosition(element, cue.Settings.LinePosition);
            WebVTTLayoutPanel.SetOrientation(element, GetPanelOrientation(cue.Settings.WritingMode));
            WebVTTLayoutPanel.SetPosition(element, cue.Settings.TextPosition);
            WebVTTLayoutPanel.SetSize(element, cue.Settings.Size);
            WebVTTLayoutPanel.SetSnapToLines(element, cue.Settings.SnapToLines);

            activeElements.Add(cue, element);
            captionPanel.Children.Add(element);
        }
コード例 #6
0
        private TextBlock GetRenderedCueTextBlock(WebVTTCue cue, WebVTTAlignment alignment, Brush brush, TextPosition textPosition, Transform transform = null)
        {
            var content = cue.Content;
            
            var result = new TextBlock
            {
                TextWrapping = TextWrapping.Wrap,
                RenderTransform = transform,
                Foreground = brush
            };

            switch (alignment)
            {
                case WebVTTAlignment.Middle:
                    result.TextAlignment = TextAlignment.Center;
                    break;
                case WebVTTAlignment.Left:
                    result.TextAlignment = TextAlignment.Left;
                    break;
                case WebVTTAlignment.Right:
                    result.TextAlignment = TextAlignment.Right;
                    break;
                case WebVTTAlignment.Start:
                    result.TextAlignment = TextAlignment.Left;
                    break;
                case WebVTTAlignment.End:
                    result.TextAlignment = TextAlignment.Right;
                    break;
            }

            CreateInlines(cue, content, result.Inlines, brush);

            // If a Text Rendering event handler has been added, call it.
            if (this.TextRendering != null)
            {
                this.TextRendering(this, new CaptionTextEventArgs(result, textPosition));
            }

            return result;
        }
コード例 #7
0
        private static WebVTTCue ParseCue(string block, TimeSpan timeOffset)
        {
            var cueReader = new StringReader(block);

            WebVTTCue result = new WebVTTCue();
            var line = cueReader.ReadLine();
            if (!line.Contains(cueIdentifier))
            {
                result.StyleClass = line;
                line = cueReader.ReadLine();
            }

            var parts = line.Split(cueHeaderSeparator, StringSplitOptions.RemoveEmptyEntries);
            if (parts[1] != cueIdentifier) throw new Exception("Invalid WebVTT cue time separator");
            result.Begin = ParseTimeStamp(parts[0]).Add(timeOffset);
            result.End = ParseTimeStamp(parts[2]).Add(timeOffset);
            if (parts.Length >= 3)
            {
                var settingsDictionary = parts.Skip(3)
                    .Select(p => p.Split(':'))
                    .ToDictionary(i => i[0], i => i[1]);
                result.Settings = ParseCueSettings(settingsDictionary);
            }

            StringBuilder cueContent = new StringBuilder();
            do
            {
                line = cueReader.ReadLine();
                if (line == null) break;
                if (line.Contains(cueIdentifier)) throw new Exception("Invalid WebVTT cue content");
                if (cueContent.Length > 0) cueContent.AppendLine();
                cueContent.Append(line);

            } while (true);
            result.Content = ParseCueContent(cueContent.ToString(), timeOffset);

            return result;
        }
コード例 #8
0
        private static WebVTTDocument ParseDocument(StringReader reader, TimeSpan timeOffset)
        {
            var blockReader = new BlockDocumentReader(reader);

            if (!blockReader.ReadBlock().StartsWith(bodyIdentifier))
            {
                throw new Exception("Invalid WebVTT start identifier");
            }

            var       result      = new WebVTTDocument();
            WebVTTCue previousCue = null;

            while (true)
            {
                var block = blockReader.ReadBlock();
                if (block == null)
                {
                    break;
                }
                else if (!block.StartsWith(noteIdentifier)) // ignore comments
                {
                    // cue found
                    var cue = ParseCue(block, timeOffset);
                    if (previousCue != null && cue.Begin < previousCue.Begin)
                    {
                        throw new Exception("Invalid WebVTT cue start time");
                    }
                    if (cue.End <= cue.Begin)
                    {
                        throw new Exception("Invalid WebVTT cue end time");
                    }
                    result.Cues.Add(cue);
                    previousCue = cue;
                }
            }
            return(result);
        }
コード例 #9
0
 public NodeRenderingEventArgs(WebVTTCue cue, IWebVTTNode node, Inline inline)
 {
     Cue    = cue;
     Node   = node;
     Inline = inline;
 }
コード例 #10
0
 public NodeRenderingEventArgs(WebVTTCue cue, IWebVTTNode node, Inline inline)
 {
     Cue = cue;
     Node = node;
     Inline = inline;
 }
コード例 #11
0
 private void CreateInlines(WebVTTCue cue, IWebVTTInternalNode node, InlineCollection inlines, Brush brush)
 {
     foreach (var child in node.Nodes)
     {
         var inline = CreateInline(child, brush);
         if (inline != null)
         {
             inlines.Add(inline);
             if (inline is Span && child is IWebVTTInternalNode)
             {
                 CreateInlines(cue, (IWebVTTInternalNode)child, ((Span)inline).Inlines, brush);
             }
             if (NodeRendering != null) NodeRendering(this, new NodeRenderingEventArgs(cue, child, inline));
         }
     }
 }
コード例 #12
0
 public Animation(WebVTTCue cue, TimeSpan timeStamp)
 {
     Cue = cue;
     TimeStamp = timeStamp;
 }
コード例 #13
0
        private void AddCue(WebVTTCue cue)
        {
            BoxElement element = renderer.GetRenderedCue(cue);
            if (BoxStyle != null) element.Style = BoxStyle;
            element.FontSize = currentFontSize;
            WebVTTLayoutPanel.SetAlignment(element, GetPanelAlignment(cue.Settings.Alignment));
            WebVTTLayoutPanel.SetDirection(element, GetPanelAlignment(cue.Settings.WritingMode, cue.Content));
            WebVTTLayoutPanel.SetLinePosition(element, cue.Settings.LinePosition);
            WebVTTLayoutPanel.SetOrientation(element, GetPanelOrientation(cue.Settings.WritingMode));
            WebVTTLayoutPanel.SetPosition(element, cue.Settings.TextPosition);
            WebVTTLayoutPanel.SetSize(element, cue.Settings.Size);
            WebVTTLayoutPanel.SetSnapToLines(element, cue.Settings.SnapToLines);

            activeElements.Add(cue, element);
            captionPanel.Children.Add(element);
        }
コード例 #14
0
 public Animation(WebVTTCue cue, TimeSpan timeStamp)
 {
     Cue       = cue;
     TimeStamp = timeStamp;
 }
コード例 #15
0
        public BoxElement GetRenderedCue(WebVTTCue cue)
        {
            var alignment = cue.Settings.Alignment;

            var result = new BoxElement();

            if (OutlineWidth > 0)
            {
                // create adjacent textblocks
                var textBlockOutline1 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.Left,
                                                                new TranslateTransform()
                {
                    X = -OutlineWidth, Y = 0
                });
                textBlockOutline1.Margin = new Thickness(OutlineWidth);
                var textBlockOutline2 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.Top,
                                                                new TranslateTransform {
                    X = 0, Y = -OutlineWidth
                });
                textBlockOutline2.Margin = new Thickness(OutlineWidth);
                var textBlockOutline3 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.Right,
                                                                new TranslateTransform {
                    X = OutlineWidth, Y = 0
                });
                textBlockOutline3.Margin = new Thickness(OutlineWidth);
                var textBlockOutline4 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.Bottom,
                                                                new TranslateTransform {
                    X = 0, Y = OutlineWidth
                });
                textBlockOutline4.Margin = new Thickness(OutlineWidth);

                // create diagonal textblocks
                var textBlockOutline5 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.TopLeft,
                                                                new TranslateTransform {
                    X = -OutlineWidth, Y = -OutlineWidth
                });
                textBlockOutline5.Margin = new Thickness(OutlineWidth);
                var textBlockOutline6 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.TopRight,
                                                                new TranslateTransform {
                    X = OutlineWidth, Y = -OutlineWidth
                });
                textBlockOutline6.Margin = new Thickness(OutlineWidth);
                var textBlockOutline7 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.BottomRight,
                                                                new TranslateTransform {
                    X = OutlineWidth, Y = OutlineWidth
                });
                textBlockOutline7.Margin = new Thickness(OutlineWidth);
                var textBlockOutline8 = GetRenderedCueTextBlock(cue, alignment, OutlineBrush, TextPosition.BottomLeft,
                                                                new TranslateTransform {
                    X = -OutlineWidth, Y = OutlineWidth
                });
                textBlockOutline8.Margin = new Thickness(OutlineWidth);

                result.AddBlock(textBlockOutline1);
                result.AddBlock(textBlockOutline2);
                result.AddBlock(textBlockOutline3);
                result.AddBlock(textBlockOutline4);
                result.AddBlock(textBlockOutline5);
                result.AddBlock(textBlockOutline6);
                result.AddBlock(textBlockOutline7);
                result.AddBlock(textBlockOutline8);
            }

            var textBlock = GetRenderedCueTextBlock(cue, alignment, InnerBrush, TextPosition.Center);

            textBlock.Margin = new Thickness(OutlineWidth);
            result.AddBlock(textBlock);

            return(result);
        }
コード例 #16
0
 void ShowCue(WebVTTCue cue)
 {
     captionsPanel.ActiveCues.Add(cue);
 }
コード例 #17
0
 void HideCue(WebVTTCue cue)
 {
     captionsPanel.ActiveCues.Remove(cue);
 }