private static TimedTextElement BuildTimedTextElements(TimedTextElementBase element, RegionElement region) { TimedTextElement timedTextElement = CreateTimedTextElement(element, region); foreach (TimedTextElementBase c in element.Children) { TimedTextElement child = BuildTimedTextElements(c, region); if (child is TimedTextAnimation) { #if HACK_XAMLTYPEINFO var children = timedTextElement.Animations as MediaMarkerCollection <TimedTextAnimation>; #else var children = timedTextElement.Animations; #endif children.Add((TimedTextAnimation)child); } else if (timedTextElement is CaptionElement && child is CaptionElement) { #if HACK_XAMLTYPEINFO var children = timedTextElement.Children as MediaMarkerCollection <TimedTextElement>; #else var children = timedTextElement.Children; #endif ((CaptionElement)child).Index = children.Count; children.Add((CaptionElement)child); } } return(timedTextElement); }
private static TtElement BuildDocument(XDocument xml, TimeSpan timeOffset, TimeSpan defaultEndTime) { TtElement parsetree = null; try { parsetree = (TtElement)TimedTextElementBase.Parse(xml.Root); } catch (TimedTextException) { } if (parsetree == null) { throw new TimedTextException("No Parse tree returned"); } if (!parsetree.Valid()) { throw new TimedTextException("Document is Well formed XML, but invalid Timed Text"); } var startTime = new TimeCode(timeOffset, TimeExpression.CurrentSmpteFrameRate); var endTime = new TimeCode(defaultEndTime, TimeExpression.CurrentSmpteFrameRate); parsetree.ComputeTimeIntervals(TimeContainer.Par, startTime, endTime); return(parsetree); }
private static TimedTextElement CreateTimedTextElement(TimedTextElementBase element, RegionElement region) { var captionElement = element is SetElement ? (TimedTextElement)BuildCaptionAnimationElement(element) : new CaptionElement(); var endTime = element.End.TotalSeconds >= TimeSpan.MaxValue.TotalSeconds ? TimeSpan.MaxValue : TimeSpan.FromSeconds(element.End.TotalSeconds); captionElement.End = endTime; captionElement.Begin = TimeSpan.FromSeconds(element.Begin.TotalSeconds); if (element is BrElement) { captionElement.CaptionElementType = TimedTextElementType.LineBreak; } else if (element is AnonymousSpanElement) { var span = element as AnonymousSpanElement; captionElement.CaptionElementType = TimedTextElementType.Text; captionElement.Content = HttpUtility.HtmlDecode(span.Text); captionElement.Style = TimedTextStyleParser.MapStyle(element, region); } else if (!(element is SetElement)) { captionElement.CaptionElementType = TimedTextElementType.Container; captionElement.Style = TimedTextStyleParser.MapStyle(element, region); } return(captionElement); }
private static void BuildCaptions(TimedTextElementBase timedTextElement, IDictionary <string, RegionElement> regionElementsHash, IDictionary <string, CaptionRegion> captionRegionsHash) { var pElement = timedTextElement as PElement; if (pElement != null) { // region inheritence per spec: http://www.w3.org/TR/ttaf1-dfxp/#semantics-region-layout-step-1 string regionName = GetInheritedAttribute(timedTextElement, "region") ?? GetDescendentAttribute(timedTextElement, "region") ?? RegionElement.DefaultRegionName ?? string.Empty; RegionElement regionElement; if (regionElementsHash.TryGetValue(regionName, out regionElement)) { var captionElement = MapToCaption(pElement, regionElement); CaptionRegion captionRegion; if (captionRegionsHash.TryGetValue(regionName, out captionRegion)) { captionElement.Index = captionRegion.Children.Count; captionRegion.Children.Add(captionElement); } } } else if (timedTextElement.Children != null) { timedTextElement.Children .Cast <TimedTextElementBase>() .ForEach(i => BuildCaptions(i, regionElementsHash, captionRegionsHash)); } }
private static void BuildCaptions(TimedTextElementBase timedTextElement, IDictionary<string, RegionElement> regionElementsHash, IDictionary<string, CaptionRegion> captionRegionsHash) { var pElement = timedTextElement as PElement; if (pElement != null) { var regionNameAttribute = pElement.Attributes.FirstOrDefault(i => i.LocalName == "region"); var regionName = regionNameAttribute != null ? regionNameAttribute.Value : string.Empty; RegionElement regionElement; if (regionElementsHash.TryGetValue(regionName, out regionElement)) { var captionElement = MapToCaption(pElement, regionElement); CaptionRegion captionRegion; if (captionRegionsHash.TryGetValue(regionName, out captionRegion)) { captionElement.Index = captionRegion.Children.Count; captionRegion.Children.Add(captionElement); } } } else if (timedTextElement.Children != null) { timedTextElement.Children .Cast<TimedTextElementBase>() .ForEach(i => BuildCaptions(i, regionElementsHash, captionRegionsHash)); } }
private static TimedTextAnimation BuildCaptionAnimationElement(TimedTextElementBase element) { var propertyName = element.Attributes .Where(i => TimedTextStyleParser.IsValidAnimationPropertyName(i.LocalName)) .Select(i => i.LocalName) .FirstOrDefault(); return(!propertyName.IsNullOrWhiteSpace() ? new TimedTextAnimation { CaptionElementType = TimedTextElementType.Animation, PropertyName = propertyName, Style = TimedTextStyleParser.MapStyle(element, null) } : null); }
private static TimedTextAnimation BuildCaptionAnimationElement(TimedTextElementBase element) { var propertyName = element.Attributes .Where(i => TimedTextStyleParser.IsValidAnimationPropertyName(i.LocalName)) .Select(i => i.LocalName) .FirstOrDefault(); return !propertyName.IsNullOrWhiteSpace() ? new TimedTextAnimation { CaptionElementType = TimedTextElementType.Animation, PropertyName = propertyName, Style = TimedTextStyleParser.MapStyle(element, null) } : null; }
internal static bool UnitTests() { #region string preamble string preamble = @"<tt xml:lang ='' ttp:profile='http://www.w3.org/2006/10/ttaf1#profile-dfxp' xmlns='http://www.w3.org/2006/10/ttaf1' xmlns:ttm='http://www.w3.org/2006/10/ttaf1#metadata' xmlns:ttp='http://www.w3.org/2006/10/ttaf1#parameter' xmlns:tts='http://www.w3.org/2006/10/ttaf1#style' > <head> <ttm:title>Unit test</ttm:title> </head> "; #endregion XElement ttData1 = XElement.Parse(preamble + @" <body timeContainer ='par' > <div timeContainer ='seq' end='5s'> <p dur='2s'>One<span begin='0.5s' dur='1s' > and a bit</span></p> <p dur='2s'>Two</p> </div> <div timeContainer ='seq' dur='5s'> <p begin='0.1s' dur='2s'>One</p> <p dur='2s'>Two</p> </div> </body> </tt> "); TimedTextElementBase tt1 = TimedTextElementBase.Parse(ttData1); bool pass = tt1.Valid(); TimeCode startTime = new TimeCode("00:00:00:00", TimeExpression.CurrentSmpteFrameRate); TimeCode endTime = new TimeCode("01:00:00:00", TimeExpression.CurrentSmpteFrameRate); tt1.ComputeTimeIntervals(TimeContainer.Par, startTime, endTime); ReadOnlyCollection <TimeCode> ev = tt1.Events; // some basic tests, try to come up with some more devilish ones. pass &= ev.Count == 10; return(pass); }
private static TimedTextElement BuildTimedTextElements(TimedTextElementBase element, RegionElement region) { TimedTextElement timedTextElement = CreateTimedTextElement(element, region); foreach (TimedTextElementBase c in element.Children) { TimedTextElement child = BuildTimedTextElements(c, region); if (child is TimedTextAnimation) { timedTextElement.Animations.Add((TimedTextAnimation)child); } else if (timedTextElement is CaptionElement && child is CaptionElement) { ((CaptionElement)child).Index = timedTextElement.Children.Count; timedTextElement.Children.Add((CaptionElement)child); } } return(timedTextElement); }
internal static Style MapStyle(TimedTextElementBase styleElement, RegionElement region) { var style = new Style(); if (styleElement.Id != null) { style.Id = styleElement.Id; } var backgroundColor = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.BackgroundColor.LocalName, region) as Color?; style.BackgroundColor = backgroundColor.GetValueOrDefault(DefaultStyle.BackgroundColor); var color = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Color.LocalName, region) as Color?; style.Color = color.GetValueOrDefault(DefaultStyle.Color); var extent = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Extent.LocalName, region) as TimedTextExtent; if (extent != null) { Length height = new Length { Value = extent.Height, Unit = FromUnit(extent.UnitMeasureVertical) }; Length width = new Length { Value = extent.Width, Unit = FromUnit(extent.UnitMeasureHorizontal) }; style.Extent = new Extent { Height = height, Width = width }; } else { style.Extent = DefaultStyle.Extent; } var fontFamily = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.FontFamily.LocalName, region) as string; style.FontFamily = !fontFamily.IsNullOrWhiteSpace() ? new FontFamily(fontFamily) : DefaultStyle.FontFamily; object oFontSize = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.FontSize.LocalName, region); var fontSize = oFontSize as string; if (!fontSize.IsNullOrWhiteSpace()) { var parsedFontSize = GetNumberPair(fontSize); style.FontSize = new Length { Unit = FromUnit(parsedFontSize.UnitMeasureHorizontal), Value = parsedFontSize.First }; } var fontStyle = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.FontStyle.LocalName, region) as FontStyleAttributeValue?; style.FontStyle = fontStyle.HasValue && (fontStyle.Value == FontStyleAttributeValue.Italic || fontStyle.Value == FontStyleAttributeValue.Oblique || fontStyle.Value == FontStyleAttributeValue.ReverseOblique) ? FontStyles.Italic : DefaultStyle.FontStyle; var fontWeight = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.FontWeight.LocalName, region) as FontWeightAttributeValue?; style.FontWeight = fontWeight.HasValue && fontWeight.Value == FontWeightAttributeValue.Bold ? FontWeights.Bold : DefaultStyle.FontWeight; var lineHeight = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.LineHeight.LocalName, region) as LineHeight; style.LineHeight = lineHeight != null && !(lineHeight is NormalHeight) ? new Length { Unit = FromUnit(lineHeight.UnitMeasureVertical), Value = lineHeight.Height } : null; var textOutline = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.TextOutline.LocalName, region) as TextOutline; style.OutlineBlur = new Length { Unit = FromUnit(textOutline.UnitMeasureBlur), Value = textOutline.Blur }; style.OutlineWidth = new Length { Unit = FromUnit(textOutline.UnitMeasureWidth), Value = textOutline.Width }; style.OutlineColor = textOutline.StrokeColor; var opacity = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Opacity.LocalName, region) as double?; style.Opacity = opacity.HasValue ? opacity.Value : DefaultStyle.Opacity; var origin = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Origin.LocalName, region) as TimedTextOrigin; style.Origin = origin != null ? new Origin { Left = new Length { Unit = FromUnit(origin.UnitMeasureHorizontal), Value = origin.X }, Top = new Length { Unit = FromUnit(origin.UnitMeasureVertical), Value = origin.Y } } : DefaultStyle.Origin; var overflow = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Overflow.LocalName, region) as string; Overflow parsedOverflow; style.Overflow = overflow.EnumTryParse(true, out parsedOverflow) ? parsedOverflow : DefaultStyle.Overflow; var padding = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Padding.LocalName, region) as PaddingThickness; style.Padding = padding != null ? new Padding { Left = new Length { Unit = FromUnit(padding.WidthStartUnit), Value = padding.WidthStart }, Right = new Length { Unit = FromUnit(padding.WidthEndUnit), Value = padding.WidthEnd }, Top = new Length { Unit = FromUnit(padding.WidthBeforeUnit), Value = padding.WidthBefore }, Bottom = new Length { Unit = FromUnit(padding.WidthAfterUnit), Value = padding.WidthAfter } } : DefaultStyle.Padding; var textAlign = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.TextAlign.LocalName, region) as string; TextAlignment parsedTextAlign; if (textAlign == "start") { textAlign = "left"; } else if (textAlign == "end") { textAlign = "right"; } style.TextAlign = textAlign.EnumTryParse(true, out parsedTextAlign) ? parsedTextAlign : DefaultStyle.TextAlign; var direction = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Direction.LocalName, region) as string; style.Direction = direction == "rtl" ? Direction.RightToLeft : Direction.LeftToRight; var displayAlign = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.DisplayAlign.LocalName, region) as string; DisplayAlign parsedDisplayAlign; style.DisplayAlign = displayAlign.EnumTryParse(true, out parsedDisplayAlign) ? parsedDisplayAlign : DefaultStyle.DisplayAlign; var visibility = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Visibility.LocalName, region) as string; style.Visibility = !visibility.IsNullOrWhiteSpace() && visibility.Equals("hidden", StringComparison.CurrentCultureIgnoreCase) ? Visibility.Collapsed : DefaultStyle.Visibility; var display = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.Display.LocalName, region) as string; style.Display = !display.IsNullOrWhiteSpace() && display.Equals("none", StringComparison.CurrentCultureIgnoreCase) ? Visibility.Collapsed : DefaultStyle.Display; var wrapOption = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.WrapOption.LocalName, region) as string; TextWrapping parsedWrapOption; style.WrapOption = wrapOption.EnumTryParse(true, out parsedWrapOption) ? parsedWrapOption : DefaultStyle.WrapOption; var showBackground = styleElement.GetComputedStyle(TimedTextVocabulary.Attributes.Styling.ShowBackground.LocalName, region) as string; ShowBackground parsedShowBackground; style.ShowBackground = showBackground.EnumTryParse(true, out parsedShowBackground) ? parsedShowBackground : DefaultStyle.ShowBackground; object zindex = styleElement.GetComputedStyle("zIndex", null); try { if (zindex is string == false) { var tmp = (double)zindex; style.ZIndex = (int)tmp; } else { style.ZIndex = 0; } } catch { style.ZIndex = 0; } return(style); }
private static TimedTextElement CreateTimedTextElement(TimedTextElementBase element, RegionElement region) { var captionElement = element is SetElement ? (TimedTextElement)BuildCaptionAnimationElement(element) : new CaptionElement(); var endTime = element.End.TotalSeconds >= TimeSpan.MaxValue.TotalSeconds ? TimeSpan.MaxValue : TimeSpan.FromSeconds(element.End.TotalSeconds); captionElement.End = endTime; captionElement.Begin = TimeSpan.FromSeconds(element.Begin.TotalSeconds); if (element is BrElement) { captionElement.CaptionElementType = TimedTextElementType.LineBreak; } else if (element is AnonymousSpanElement) { var span = element as AnonymousSpanElement; captionElement.CaptionElementType = TimedTextElementType.Text; captionElement.Content = HttpUtility.HtmlDecode(span.Text); captionElement.Style = TimedTextStyleParser.MapStyle(element, region); } else if (!(element is SetElement)) { captionElement.CaptionElementType = TimedTextElementType.Container; captionElement.Style = TimedTextStyleParser.MapStyle(element, region); } return captionElement; }
private static TimedTextElement BuildTimedTextElements(TimedTextElementBase element, RegionElement region) { TimedTextElement timedTextElement = CreateTimedTextElement(element, region); foreach (TimedTextElementBase c in element.Children) { TimedTextElement child = BuildTimedTextElements(c, region); if (child is TimedTextAnimation) { #if HACK_XAMLTYPEINFO var children = timedTextElement.Animations as MediaMarkerCollection<TimedTextAnimation>; #else var children = timedTextElement.Animations; #endif children.Add((TimedTextAnimation)child); } else if (timedTextElement is CaptionElement && child is CaptionElement) { #if HACK_XAMLTYPEINFO var children = timedTextElement.Children as MediaMarkerCollection<TimedTextElement>; #else var children = timedTextElement.Children; #endif ((CaptionElement)child).Index = children.Count; children.Add((CaptionElement)child); } } return timedTextElement; }
private static void BuildCaptions(TimedTextElementBase timedTextElement, IDictionary<string, RegionElement> regionElementsHash, IDictionary<string, CaptionRegion> captionRegionsHash) { var pElement = timedTextElement as PElement; if (pElement != null) { // region inheritence per spec: http://www.w3.org/TR/ttaf1-dfxp/#semantics-region-layout-step-1 string regionName = GetInheritedAttribute(timedTextElement, "region") ?? GetDescendentAttribute(timedTextElement, "region") ?? RegionElement.DefaultRegionName ?? string.Empty; RegionElement regionElement; if (regionElementsHash.TryGetValue(regionName, out regionElement)) { var captionElement = MapToCaption(pElement, regionElement); CaptionRegion captionRegion; if (captionRegionsHash.TryGetValue(regionName, out captionRegion)) { #if HACK_XAMLTYPEINFO var children = captionRegion.Children as MediaMarkerCollection<TimedTextElement>; #else var children = captionRegion.Children; #endif captionElement.Index = children.Count; children.Add(captionElement); } } } else if (timedTextElement.Children != null) { timedTextElement.Children .Cast<TimedTextElementBase>() .ForEach(i => BuildCaptions(i, regionElementsHash, captionRegionsHash)); } }
private static TimedTextElement BuildTimedTextElements(TimedTextElementBase element, RegionElement region) { TimedTextElement timedTextElement = CreateTimedTextElement(element, region); foreach (TimedTextElementBase c in element.Children) { TimedTextElement child = BuildTimedTextElements(c, region); if (child is TimedTextAnimation) { timedTextElement.Animations.Add((TimedTextAnimation)child); } else if (timedTextElement is CaptionElement && child is CaptionElement) { ((CaptionElement)child).Index = timedTextElement.Children.Count; timedTextElement.Children.Add((CaptionElement)child); } } return timedTextElement; }