Shaka Packager SDK
text_sample.h
1 // Copyright 2017 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef PACKAGER_MEDIA_BASE_TEXT_SAMPLE_H_
8 #define PACKAGER_MEDIA_BASE_TEXT_SAMPLE_H_
9 
10 #include <cstdint>
11 #include <optional>
12 #include <string>
13 #include <vector>
14 
15 namespace shaka {
16 namespace media {
17 
18 enum class TextUnitType {
20  kPixels,
22  kLines,
24  kPercent,
25 };
26 
27 enum class WritingDirection {
28  kHorizontal,
29  kVerticalGrowingLeft,
30  kVerticalGrowingRight,
31 };
32 
33 enum class TextAlignment {
35  kStart,
37  kCenter,
39  kEnd,
41  kLeft,
43  kRight,
44 };
45 
46 struct TextNumber {
47  TextNumber(float value, TextUnitType type) : value(value), type(type) {}
48 
49  float value;
50  TextUnitType type;
51 };
52 
53 struct TextSettings {
56  std::optional<TextNumber> line;
59  std::optional<TextNumber> position;
63  std::optional<TextNumber> width;
67  std::optional<TextNumber> height;
68 
70  std::string region;
71 
74  WritingDirection writing_direction = WritingDirection::kHorizontal;
76  TextAlignment text_alignment = TextAlignment::kCenter;
77 };
78 
80  std::optional<bool> underline;
81  std::optional<bool> bold;
82  std::optional<bool> italic;
83  // The colors could be any string that can be interpreted as
84  // a color in TTML (or WebVTT). As a start, the 8 teletext colors are used,
85  // i.e. black, red, green, yellow, blue, magenta, cyan, and white
86  std::string color;
87  std::string backgroundColor;
88 };
89 
92 struct TextFragment {
93  TextFragment() {}
94  TextFragment(const TextFragmentStyle& style,
95  const std::vector<TextFragment>& sub_fragments)
96  : style(style), sub_fragments(sub_fragments) {}
97  TextFragment(const TextFragmentStyle& style, const char* body)
98  : style(style), body(body) {}
99  TextFragment(const TextFragmentStyle& style, const std::string& body)
100  : style(style), body(body) {}
101  TextFragment(const TextFragmentStyle& style,
102  const std::vector<uint8_t>& image)
103  : style(style), image(image) {}
104  TextFragment(const TextFragmentStyle& style, bool newline)
105  : style(style), newline(newline) {}
106 
107  TextFragmentStyle style;
108 
109  std::vector<TextFragment> sub_fragments;
110  std::string body;
112  std::vector<uint8_t> image;
113  bool newline = false;
114 
115  bool is_empty() const;
116 };
117 
118 class TextSample {
119  public:
120  TextSample(const std::string& id,
121  int64_t start_time,
122  int64_t end_time,
123  const TextSettings& settings,
124  const TextFragment& body);
125 
126  const std::string& id() const { return id_; }
127  int64_t start_time() const { return start_time_; }
128  int64_t duration() const { return duration_; }
129  const TextSettings& settings() const { return settings_; }
130  const TextFragment& body() const { return body_; }
131  int64_t EndTime() const;
132 
133  int32_t sub_stream_index() const { return sub_stream_index_; }
134  void set_sub_stream_index(int32_t idx) { sub_stream_index_ = idx; }
135 
136  private:
137  // Allow the compiler generated copy constructor and assignment operator
138  // intentionally. Since the text data is typically small, the performance
139  // impact is minimal.
140 
141  const std::string id_;
142  const int64_t start_time_ = 0;
143  const int64_t duration_ = 0;
144  const TextSettings settings_;
145  const TextFragment body_;
146  int32_t sub_stream_index_ = -1;
147 };
148 
149 } // namespace media
150 } // namespace shaka
151 
152 #endif // PACKAGER_MEDIA_BASE_TEXT_SAMPLE_H_
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66
std::vector< uint8_t > image
PNG image data.
Definition: text_sample.h:112
TextAlignment text_alignment
How to align the text within the cue box.
Definition: text_sample.h:76
std::string region
The region to draw the cue in.
Definition: text_sample.h:70
std::optional< TextNumber > height
Definition: text_sample.h:67
WritingDirection writing_direction
Definition: text_sample.h:74
std::optional< TextNumber > width
Definition: text_sample.h:63
std::optional< TextNumber > position
Definition: text_sample.h:59
std::optional< TextNumber > line
Definition: text_sample.h:56