Shaka Packager SDK
text_sample.h
1 // Copyright 2017 Google Inc. 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 <stdint.h>
11 
12 #include <string>
13 #include <vector>
14 
15 #include "packager/base/optional.h"
16 
17 namespace shaka {
18 namespace media {
19 
20 enum class TextUnitType {
22  kPixels,
24  kLines,
26  kPercent,
27 };
28 
29 enum class WritingDirection {
30  kHorizontal,
31  kVerticalGrowingLeft,
32  kVerticalGrowingRight,
33 };
34 
35 enum class TextAlignment {
37  kStart,
39  kCenter,
41  kEnd,
43  kLeft,
45  kRight,
46 };
47 
48 struct TextNumber {
49  TextNumber(float value, TextUnitType type) : value(value), type(type) {}
50 
51  float value;
52  TextUnitType type;
53 };
54 
55 struct TextSettings {
58  base::Optional<TextNumber> line;
61  base::Optional<TextNumber> position;
65  base::Optional<TextNumber> width;
69  base::Optional<TextNumber> height;
70 
72  std::string region;
73 
76  WritingDirection writing_direction = WritingDirection::kHorizontal;
78  TextAlignment text_alignment = TextAlignment::kCenter;
79 };
80 
82  base::Optional<bool> underline;
83  base::Optional<bool> bold;
84  base::Optional<bool> italic;
85 };
86 
89 struct TextFragment {
90  TextFragment() {}
91  TextFragment(const TextFragmentStyle& style,
92  const std::vector<TextFragment>& sub_fragments)
93  : style(style), sub_fragments(sub_fragments) {}
94  TextFragment(const TextFragmentStyle& style, const char* body)
95  : style(style), body(body) {}
96  TextFragment(const TextFragmentStyle& style, const std::string& body)
97  : style(style), body(body) {}
98  TextFragment(const TextFragmentStyle& style,
99  const std::vector<uint8_t>& image)
100  : style(style), image(image) {}
101  TextFragment(const TextFragmentStyle& style, bool newline)
102  : style(style), newline(newline) {}
103 
104  TextFragmentStyle style;
105 
106  std::vector<TextFragment> sub_fragments;
107  std::string body;
109  std::vector<uint8_t> image;
110  bool newline = false;
111 
112  bool is_empty() const;
113 };
114 
115 class TextSample {
116  public:
117  TextSample(const std::string& id,
118  int64_t start_time,
119  int64_t end_time,
120  const TextSettings& settings,
121  const TextFragment& body);
122 
123  const std::string& id() const { return id_; }
124  int64_t start_time() const { return start_time_; }
125  int64_t duration() const { return duration_; }
126  const TextSettings& settings() const { return settings_; }
127  const TextFragment& body() const { return body_; }
128  int64_t EndTime() const;
129 
130  int32_t sub_stream_index() const { return sub_stream_index_; }
131  void set_sub_stream_index(int32_t idx) { sub_stream_index_ = idx; }
132 
133  private:
134  // Allow the compiler generated copy constructor and assignment operator
135  // intentionally. Since the text data is typically small, the performance
136  // impact is minimal.
137 
138  const std::string id_;
139  const int64_t start_time_ = 0;
140  const int64_t duration_ = 0;
141  const TextSettings settings_;
142  const TextFragment body_;
143  int32_t sub_stream_index_ = -1;
144 };
145 
146 } // namespace media
147 } // namespace shaka
148 
149 #endif // PACKAGER_MEDIA_BASE_TEXT_SAMPLE_H_
All the methods that are virtual are virtual for mocking.
std::vector< uint8_t > image
PNG image data.
Definition: text_sample.h:109
base::Optional< TextNumber > line
Definition: text_sample.h:58
TextAlignment text_alignment
How to align the text within the cue box.
Definition: text_sample.h:78
std::string region
The region to draw the cue in.
Definition: text_sample.h:72
base::Optional< TextNumber > position
Definition: text_sample.h:61
base::Optional< TextNumber > height
Definition: text_sample.h:69
WritingDirection writing_direction
Definition: text_sample.h:76
base::Optional< TextNumber > width
Definition: text_sample.h:65