libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
subtitle.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_PROAV
13#include <cstdint>
14#include <promeki/color.h>
15#include <promeki/enums.h>
16#include <promeki/error.h>
17#include <promeki/list.h>
18#include <promeki/namespace.h>
19#include <promeki/rect.h>
20#include <promeki/sharedptr.h>
21#include <promeki/string.h>
22#include <promeki/timestamp.h>
23
24PROMEKI_NAMESPACE_BEGIN
25
26class DataStream;
27class FrameRate;
28class JsonObject;
29class Metadata;
30class SubtitleList;
31struct SubtitleImpl; // Pimpl — defined in subtitle.cpp.
32struct SubtitleSpanImpl; // Pimpl — defined in subtitle.cpp.
33
79class SubtitleSpan {
80 public:
81 // -- Construction / destruction (out-of-line for pimpl) ---
82
84 SubtitleSpan();
85
87 explicit SubtitleSpan(String text);
88
97 SubtitleSpan(String text, bool bold, bool italic, bool underline, Color color = Color());
98
99 SubtitleSpan(const SubtitleSpan &);
100 SubtitleSpan(SubtitleSpan &&) noexcept;
101 ~SubtitleSpan();
102 SubtitleSpan &operator=(const SubtitleSpan &);
103 SubtitleSpan &operator=(SubtitleSpan &&) noexcept;
104
105 // -- Read-only accessors ----------------------------------
106
108 const String &text() const;
109
111 bool bold() const;
112
114 bool italic() const;
115
117 bool underline() const;
118
127 const Color &color() const;
128
132 const Color &backgroundColor() const;
133
137 const Color &edgeColor() const;
138
141 const SubtitleEdgeStyle &edgeStyle() const;
142
146 const SubtitleFontFace &fontFace() const;
147
150 const SubtitleOpacity &foregroundOpacity() const;
151
153 const SubtitleOpacity &backgroundOpacity() const;
154
156 const SubtitleOpacity &edgeOpacity() const;
157
159 bool hasStyle() const;
160
162 bool isEmpty() const;
163
164 // -- CoW mutators -----------------------------------------
165
166 void setText(String v);
167 void setBold(bool v);
168 void setItalic(bool v);
169 void setUnderline(bool v);
170 void setColor(const Color &v);
171 void setBackgroundColor(const Color &v);
172 void setEdgeColor(const Color &v);
173 void setEdgeStyle(const SubtitleEdgeStyle &v);
174 void setFontFace(const SubtitleFontFace &v);
175 void setForegroundOpacity(const SubtitleOpacity &v);
176 void setBackgroundOpacity(const SubtitleOpacity &v);
177 void setEdgeOpacity(const SubtitleOpacity &v);
178
179 // -- Comparison + diagnostics -----------------------------
180
181 bool operator==(const SubtitleSpan &o) const;
182 bool operator!=(const SubtitleSpan &o) const { return !(*this == o); }
183
185 JsonObject toJson() const;
186
188 String toString() const;
189
191 using List = ::promeki::List<SubtitleSpan>;
192
193 private:
194 SharedPtr<SubtitleSpanImpl> _d;
195};
196
198DataStream &operator<<(DataStream &stream, const SubtitleSpan &span);
199
201DataStream &operator>>(DataStream &stream, SubtitleSpan &span);
202
270class Subtitle {
271 public:
272 PROMEKI_DATATYPE(Subtitle, DataTypeSubtitle, 1)
273
274 // -- Construction / destruction (out-of-line for pimpl) ---
275
276
277 Subtitle();
278
293 Subtitle(TimeStamp start, TimeStamp end, String text,
294 SubtitleAnchor anchor = SubtitleAnchor::Default);
295
305 Subtitle(TimeStamp start, TimeStamp end, String text, SubtitleAnchor anchor, Rect2Di32 region,
306 String speaker, Metadata metadata);
307
316 Subtitle(TimeStamp start, TimeStamp end, SubtitleSpan::List spans, SubtitleAnchor anchor,
317 Rect2Di32 region, String speaker, Metadata metadata);
318
319 Subtitle(const Subtitle &);
320 Subtitle(Subtitle &&) noexcept;
321 ~Subtitle();
322 Subtitle &operator=(const Subtitle &);
323 Subtitle &operator=(Subtitle &&) noexcept;
324
325 // -- Read-only accessors ----------------------------------
326
328 const TimeStamp &start() const;
329
331 const TimeStamp &end() const;
332
334 TimeStamp::Duration duration() const;
335
344 const String &text() const;
345
347 const SubtitleSpan::List &spans() const;
348
350 const SubtitleAnchor &anchor() const;
351
357 const CaptionMode &mode() const;
358
378 int rollUpRows() const;
379
381 const Rect2Di32 &region() const;
382
384 const String &speaker() const;
385
387 const Metadata &metadata() const;
388
389 // -- CoW mutators -----------------------------------------
390
391 void setStart(const TimeStamp &v);
392 void setEnd(const TimeStamp &v);
393
400 void setText(const String &v);
401
403 void setSpans(SubtitleSpan::List v);
404
405 void setAnchor(const SubtitleAnchor &v);
406 void setMode(const CaptionMode &v);
407 void setRollUpRows(int v);
408 void setRegion(const Rect2Di32 &v);
409 void setSpeaker(const String &v);
410 void setMetadata(const Metadata &v);
411
412 // -- Layout helpers ---------------------------------------
413
449 Subtitle wrapped(int maxCols, int maxRows) const;
450
451 // -- Predicates -------------------------------------------
452
455 bool isEmpty() const;
456
459 bool isActiveAt(const TimeStamp &t) const;
460
461 // -- Comparison + diagnostics -----------------------------
462
463 bool operator==(const Subtitle &o) const;
464 bool operator!=(const Subtitle &o) const { return !(*this == o); }
465
467 JsonObject toJson() const;
468
470 String toString() const;
471
481 Error writeToStream(DataStream &s) const;
482
487 template <uint32_t V> static Result<Subtitle> readFromStream(DataStream &s);
488
489 private:
490 SharedPtr<SubtitleImpl> _d;
491};
492
493// ============================================================================
494// SubtitleList
495// ============================================================================
496
497struct SubtitleListImpl; // Pimpl — defined in subtitle.cpp.
498
524class SubtitleList {
525 public:
526 // -- Construction / destruction (out-of-line for pimpl) ---
527
528 SubtitleList();
529 explicit SubtitleList(List<Subtitle> entries);
530 SubtitleList(const SubtitleList &);
531 SubtitleList(SubtitleList &&) noexcept;
532 ~SubtitleList();
533 SubtitleList &operator=(const SubtitleList &);
534 SubtitleList &operator=(SubtitleList &&) noexcept;
535
536 // -- Size / access ----------------------------------------
537
538 size_t size() const;
539 bool isEmpty() const;
540
542 const Subtitle &at(size_t i) const;
543
545 const Subtitle &operator[](size_t i) const;
546
547 // -- Mutators (CoW) ---------------------------------------
548
551 void append(const Subtitle &s);
552
554 void clear();
555
557 void sortByStart();
558
560 void reserve(size_t n);
561
562 // -- Search helpers ---------------------------------------
563
572 int64_t findActiveAt(const TimeStamp &t) const;
573
579 int64_t findNextAfter(const TimeStamp &t) const;
580
585 SubtitleList findInRange(const TimeStamp &start, const TimeStamp &end) const;
586
609 SubtitleList snapToFrames(const FrameRate &frameRate) const;
610
611 // -- Raw access -------------------------------------------
612
614 const List<Subtitle> &entries() const;
615
616 // -- Comparison + diagnostics -----------------------------
617
618 bool operator==(const SubtitleList &o) const;
619 bool operator!=(const SubtitleList &o) const { return !(*this == o); }
620
622 JsonObject toJson() const;
623
625 String toString() const;
626
627 private:
628 SharedPtr<SubtitleListImpl> _d;
629};
630
631PROMEKI_NAMESPACE_END
632
633#endif // PROMEKI_ENABLE_PROAV