Line data Source code
1 : import 'dart:convert';
2 :
3 : import 'package:flutter/material.dart';
4 :
5 : import 'package:flutter_chrome_cast/common/text_track_font_style.dart';
6 : import 'package:flutter_chrome_cast/common/text_track_window_type.dart';
7 :
8 : import 'font_generic_family.dart';
9 :
10 : ///Describes style information for a text track. Colors
11 : ///are represented as strings “#RRGGBBAA” where XX are
12 : ///the two hexadecimal symbols that represent the
13 : /// 0-255 value for the specific channel/color.
14 : /// It follows CSS 8-digit hex color notation.
15 : /// (See http://dev.w3.org/csswg/css-color/#hex-notation).
16 : class TextTrackStyle {
17 : ///Background RGBA color, represented as "#RRGGBBAA".
18 : /// The alpha channel should be used for
19 : /// transparent backgrounds.
20 :
21 : final Color? backgroundColor;
22 :
23 : ///Custom application data.
24 : final Map<String, dynamic>? customData;
25 :
26 : ///RGBA color for the edge, represented as
27 : /// "#RRGGBBAA". This value will be
28 : /// ignored if edgeType is NONE.
29 :
30 : /// Edge color for the text track.
31 : final Color? edgeColor;
32 :
33 : /// Edge type for the text track.
34 : final TextTrackStyle? edgeType;
35 :
36 : /// If the font is not available in the receiver the fontGenericFamily will be used.
37 :
38 : final String? fontFamily;
39 :
40 : /// Generic font family for the text track.
41 : final TextTrackFontGenericFamily? fontGenericFamily;
42 :
43 : /// The font scaling factor for the text track (the default is 1.0).
44 : final int? fontScale;
45 :
46 : /// Font style for the text track.
47 : final TextTrackFontStyle? fontStyle;
48 :
49 : ///Foreground RGBA color, represented as "#RRGGBBAA".
50 : final Color? foregroundColor;
51 :
52 : ///RGBA color for the window, represented as
53 : ///"#RRGGBBAA". This value will be
54 : ///ignored if windowType is NONE.
55 : final Color? windowColor;
56 :
57 : ///Rounded corner radius absolute value in pixels (px).
58 : /// This value will be ignored if windowType
59 : /// is not ROUNDED_CORNERS.
60 : final double? windowRoundedCornerRadius;
61 :
62 : ///The window concept is defined in CEA-608 and CEA-708,
63 : /// See http://goo.gl/M3ea0X. In WebVTT is called a region.
64 :
65 : final TextTrackWindowType? windowType;
66 :
67 : /// Creates a new [TextTrackStyle].
68 2 : TextTrackStyle({
69 : this.backgroundColor,
70 : this.customData,
71 : this.edgeColor,
72 : this.edgeType,
73 : this.fontFamily,
74 : this.fontGenericFamily,
75 : this.fontScale,
76 : this.fontStyle,
77 : this.foregroundColor,
78 : this.windowColor,
79 : this.windowRoundedCornerRadius,
80 : this.windowType,
81 : });
82 :
83 : /// Converts this text track style to a map representation.
84 2 : Map<String, dynamic> toMap() {
85 2 : return {
86 3 : 'backgroundColor': backgroundColor?.hexColor,
87 2 : 'customData': customData,
88 2 : 'edgeColor': edgeColor?.hexColor,
89 2 : 'edgeType': edgeType?.toMap(),
90 2 : 'fontFamily': fontFamily,
91 2 : 'fontGenericFamily': fontGenericFamily?.name,
92 2 : 'fontScale': fontScale,
93 3 : 'fontStyle': fontStyle?.name,
94 3 : 'foregroundColor': foregroundColor?.hexColor,
95 2 : 'windowColor': windowColor?.hexColor,
96 2 : 'windowRoundedCornerRadius': windowRoundedCornerRadius,
97 3 : 'windowType': windowType?.name,
98 : };
99 : }
100 :
101 : /// Creates a [TextTrackStyle] from a map representation.
102 0 : factory TextTrackStyle.fromMap(Map<String, dynamic> map) {
103 0 : return TextTrackStyle(
104 0 : backgroundColor: map['backgroundColor'] != null
105 0 : ? HColor.fromHex(map['backgroundColor'])
106 : : null,
107 0 : customData: Map<String, dynamic>.from(map['customData']),
108 : edgeColor:
109 0 : map['edgeColor'] != null ? HColor.fromHex(map['edgeColor']) : null,
110 0 : edgeType: map['edgeType'] != null
111 0 : ? TextTrackStyle.fromMap(map['edgeType'])
112 : : null,
113 0 : fontFamily: map['fontFamily'],
114 0 : fontGenericFamily: map['fontGenericFamily'] != null
115 0 : ? TextTrackFontGenericFamily.fromMap(map['fontGenericFamily'])
116 : : null,
117 0 : fontScale: map['fontScale']?.toInt(),
118 0 : fontStyle: map['fontStyle'] != null
119 0 : ? TextTrackFontStyle.fromMap(map['fontStyle'])
120 : : null,
121 0 : foregroundColor: map['foregroundColor'] != null
122 0 : ? HColor.fromHex(map['foregroundColor'])
123 : : null,
124 0 : windowColor: map['windowColor'] != null
125 0 : ? HColor.fromHex(map['windowColor'])
126 : : null,
127 0 : windowRoundedCornerRadius: map['windowRoundedCornerRadius']?.toDouble(),
128 0 : windowType: map['windowType'] != null
129 0 : ? TextTrackWindowType.fromMap(map['windowType'])
130 : : null,
131 : );
132 : }
133 :
134 : /// Converts this text track style to a JSON string.
135 0 : String toJson() => json.encode(toMap());
136 :
137 : /// Creates a [TextTrackStyle] from a JSON string.
138 0 : factory TextTrackStyle.fromJson(String source) =>
139 0 : TextTrackStyle.fromMap(json.decode(source));
140 : }
141 :
142 : /// Extension on [Color] to provide hex color functionality.
143 : extension HColor on Color {
144 : /// Gets the hex color string representation.
145 1 : String get hexColor {
146 4 : return '#${toARGB32().toRadixString(16).padLeft(8, '0')}';
147 : }
148 :
149 : /// Creates a [Color] from a hex string.
150 0 : static Color fromHex(String hexString) {
151 0 : final buffer = StringBuffer();
152 0 : if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
153 0 : buffer.write(hexString.replaceFirst('#', ''));
154 0 : return Color(int.parse(buffer.toString(), radix: 16));
155 : }
156 : }
|