dvb-cc 0.2.0

DVB closed-caption carriage — cc_data() from picture user_data (ETSI TS 101 154 Table B.9), CEA-608/708 triplet demux.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Shared caption display model — colour / opacity / edge / font enumerations
//! and pen attributes, per the CEA-708 conformance model (47 CFR §79.102 (h)–(q),
//! ANSI/CTA-708-E §8.5, §8.8; `dvb-cc/docs/decode/cea708-conformance.md`,
//! `cea708-decode.md`).

/// A CEA-708 colour: 2 bits per RGB component (R, G, B each 0–3), 64 colours
/// total (§8.8, Tables 30/31).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Color {
    /// Red component, 0–3.
    pub r: u8,
    /// Green component, 0–3.
    pub g: u8,
    /// Blue component, 0–3.
    pub b: u8,
}

impl Color {
    /// Black `(0,0,0)`.
    pub const BLACK: Color = Color { r: 0, g: 0, b: 0 };
    /// White `(2,2,2)`.
    pub const WHITE: Color = Color { r: 2, g: 2, b: 2 };

    /// Construct from three 2-bit components (masked to 0–3).
    #[must_use]
    pub const fn new(r: u8, g: u8, b: u8) -> Self {
        Color {
            r: r & 0x03,
            g: g & 0x03,
            b: b & 0x03,
        }
    }

    /// Map this colour onto the minimum 8-colour palette (§8.8, p.92): component
    /// `1 → 0`, `2 → 2`, `3 → 2` (and `0 → 0`).
    #[must_use]
    pub const fn to_8_color(self) -> Color {
        const fn q(c: u8) -> u8 {
            match c {
                0 | 1 => 0,
                _ => 2,
            }
        }
        Color {
            r: q(self.r),
            g: q(self.g),
            b: q(self.b),
        }
    }
}

/// Opacity of a foreground / background / fill (§8.8; SPC/SWA opacity field,
/// `cea708-decode.md`). 2-bit field.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum Opacity {
    /// Fully opaque.
    #[default]
    Solid,
    /// Alternates opaque / transparent.
    Flash,
    /// Partially transparent (background shows through).
    Translucent,
    /// Fully transparent (not rendered).
    Transparent,
}

impl Opacity {
    /// From the 2-bit wire value.
    #[must_use]
    pub fn from_bits(v: u8) -> Self {
        match v & 0x03 {
            0 => Self::Solid,
            1 => Self::Flash,
            2 => Self::Translucent,
            _ => Self::Transparent,
        }
    }
    /// Label per the project's `name()` convention.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::Solid => "solid",
            Self::Flash => "flash",
            Self::Translucent => "translucent",
            Self::Transparent => "transparent",
        }
    }
}
dvb_common::impl_spec_display!(Opacity);

/// Character edge type (§79.102 (p) / SPA edge-type field; 3-bit). 0–5 defined.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum EdgeType {
    /// No edge.
    #[default]
    None,
    /// Raised edge.
    Raised,
    /// Depressed edge.
    Depressed,
    /// Uniform outline.
    Uniform,
    /// Left drop shadow.
    LeftDropShadow,
    /// Right drop shadow.
    RightDropShadow,
}

impl EdgeType {
    /// From the 3-bit wire value (values 6–7 fold to `None`).
    #[must_use]
    pub fn from_bits(v: u8) -> Self {
        match v & 0x07 {
            0 => Self::None,
            1 => Self::Raised,
            2 => Self::Depressed,
            3 => Self::Uniform,
            4 => Self::LeftDropShadow,
            5 => Self::RightDropShadow,
            _ => Self::None,
        }
    }
    /// Label per the project's `name()` convention.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Raised => "raised",
            Self::Depressed => "depressed",
            Self::Uniform => "uniform",
            Self::LeftDropShadow => "left_drop_shadow",
            Self::RightDropShadow => "right_drop_shadow",
        }
    }
}
dvb_common::impl_spec_display!(EdgeType);

/// Pen size (§79.102 (j) / SPA pen-size field; 2-bit). Value 3 is reserved.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum PenSize {
    /// Small.
    Small,
    /// Standard (default).
    #[default]
    Standard,
    /// Large.
    Large,
}

impl PenSize {
    /// From the 2-bit wire value (3 folds to `Standard`).
    #[must_use]
    pub fn from_bits(v: u8) -> Self {
        match v & 0x03 {
            0 => Self::Small,
            2 => Self::Large,
            _ => Self::Standard,
        }
    }
    /// Label per the project's `name()` convention.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::Small => "small",
            Self::Standard => "standard",
            Self::Large => "large",
        }
    }
}
dvb_common::impl_spec_display!(PenSize);

/// Pen vertical offset (§79.102 (l) / SPA offset field; 2-bit).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum PenOffset {
    /// Subscript.
    Subscript,
    /// Normal (default).
    #[default]
    Normal,
    /// Superscript.
    Superscript,
}

impl PenOffset {
    /// From the 2-bit wire value (3 folds to `Normal`).
    #[must_use]
    pub fn from_bits(v: u8) -> Self {
        match v & 0x03 {
            0 => Self::Subscript,
            2 => Self::Superscript,
            _ => Self::Normal,
        }
    }
    /// Label per the project's `name()` convention.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::Subscript => "subscript",
            Self::Normal => "normal",
            Self::Superscript => "superscript",
        }
    }
}
dvb_common::impl_spec_display!(PenOffset);

/// Font style (§79.102 (k) / SPA font-style field; 3-bit, 0–7).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum FontStyle {
    /// Default / undefined.
    #[default]
    Default,
    /// Monospaced with serifs.
    MonospacedSerif,
    /// Proportionally spaced with serifs.
    ProportionalSerif,
    /// Monospaced without serifs.
    MonospacedSansSerif,
    /// Proportionally spaced without serifs.
    ProportionalSansSerif,
    /// Casual.
    Casual,
    /// Cursive.
    Cursive,
    /// Small capitals.
    SmallCapitals,
}

impl FontStyle {
    /// From the 3-bit wire value.
    #[must_use]
    pub fn from_bits(v: u8) -> Self {
        match v & 0x07 {
            0 => Self::Default,
            1 => Self::MonospacedSerif,
            2 => Self::ProportionalSerif,
            3 => Self::MonospacedSansSerif,
            4 => Self::ProportionalSansSerif,
            5 => Self::Casual,
            6 => Self::Cursive,
            _ => Self::SmallCapitals,
        }
    }
    /// Label per the project's `name()` convention.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::MonospacedSerif => "monospaced_serif",
            Self::ProportionalSerif => "proportional_serif",
            Self::MonospacedSansSerif => "monospaced_sans_serif",
            Self::ProportionalSansSerif => "proportional_sans_serif",
            Self::Casual => "casual",
            Self::Cursive => "cursive",
            Self::SmallCapitals => "small_capitals",
        }
    }
}
dvb_common::impl_spec_display!(FontStyle);

/// Text justification (SWA justify field; 2-bit).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum Justify {
    /// Left-justified (default).
    #[default]
    Left,
    /// Right-justified.
    Right,
    /// Centred.
    Center,
    /// Fully justified.
    Full,
}

impl Justify {
    /// From the 2-bit wire value.
    #[must_use]
    pub fn from_bits(v: u8) -> Self {
        match v & 0x03 {
            0 => Self::Left,
            1 => Self::Right,
            2 => Self::Center,
            _ => Self::Full,
        }
    }
    /// Label per the project's `name()` convention.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::Left => "left",
            Self::Right => "right",
            Self::Center => "center",
            Self::Full => "full",
        }
    }
}
dvb_common::impl_spec_display!(Justify);

/// Print direction (SWA print-direction field; 2-bit).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum PrintDirection {
    /// Left to right (default).
    #[default]
    LeftToRight,
    /// Right to left.
    RightToLeft,
    /// Top to bottom.
    TopToBottom,
    /// Bottom to top.
    BottomToTop,
}

impl PrintDirection {
    /// From the 2-bit wire value.
    #[must_use]
    pub fn from_bits(v: u8) -> Self {
        match v & 0x03 {
            0 => Self::LeftToRight,
            1 => Self::RightToLeft,
            2 => Self::TopToBottom,
            _ => Self::BottomToTop,
        }
    }
    /// Label per the project's `name()` convention.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::LeftToRight => "left_to_right",
            Self::RightToLeft => "right_to_left",
            Self::TopToBottom => "top_to_bottom",
            Self::BottomToTop => "bottom_to_top",
        }
    }
}
dvb_common::impl_spec_display!(PrintDirection);

/// Scroll direction (SWA scroll-direction field; 2-bit). Same wire mapping as
/// [`PrintDirection`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum ScrollDirection {
    /// Left to right.
    LeftToRight,
    /// Right to left.
    RightToLeft,
    /// Top to bottom.
    TopToBottom,
    /// Bottom to top (default — NTSC roll-up).
    #[default]
    BottomToTop,
}

impl ScrollDirection {
    /// From the 2-bit wire value.
    #[must_use]
    pub fn from_bits(v: u8) -> Self {
        match v & 0x03 {
            0 => Self::LeftToRight,
            1 => Self::RightToLeft,
            2 => Self::TopToBottom,
            _ => Self::BottomToTop,
        }
    }
    /// Label per the project's `name()` convention.
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::LeftToRight => "left_to_right",
            Self::RightToLeft => "right_to_left",
            Self::TopToBottom => "top_to_bottom",
            Self::BottomToTop => "bottom_to_top",
        }
    }
}
dvb_common::impl_spec_display!(ScrollDirection);

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::ToString;

    #[test]
    fn color_8_color_mapping() {
        // (1,2,3) → (0,2,2); (3,3,3) → (2,2,2); (1,1,1) → (0,0,0)
        assert_eq!(Color::new(1, 2, 3).to_8_color(), Color::new(0, 2, 2));
        assert_eq!(Color::new(3, 3, 3).to_8_color(), Color::WHITE);
        assert_eq!(Color::new(1, 1, 1).to_8_color(), Color::BLACK);
    }

    #[test]
    fn opacity_bits() {
        assert_eq!(Opacity::from_bits(0), Opacity::Solid);
        assert_eq!(Opacity::from_bits(3), Opacity::Transparent);
        assert_eq!(Opacity::Translucent.to_string(), "translucent");
    }

    #[test]
    fn edge_bits() {
        assert_eq!(EdgeType::from_bits(5), EdgeType::RightDropShadow);
        assert_eq!(EdgeType::from_bits(7), EdgeType::None);
    }

    #[test]
    fn font_bits() {
        assert_eq!(FontStyle::from_bits(7), FontStyle::SmallCapitals);
        assert_eq!(FontStyle::from_bits(1).to_string(), "monospaced_serif");
    }
}