libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
subtitlerenderer.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 <promeki/color.h>
14#include <promeki/enums.h>
15#include <promeki/error.h>
16#include <promeki/fastfont.h>
17#include <promeki/namespace.h>
18#include <promeki/rect.h>
19#include <promeki/string.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
23class Subtitle;
24class UncompressedVideoPayload;
25
82class SubtitleRenderer {
83 public:
84 SubtitleRenderer();
85 ~SubtitleRenderer();
86
87 SubtitleRenderer(const SubtitleRenderer &) = delete;
88 SubtitleRenderer &operator=(const SubtitleRenderer &) = delete;
89
90 // ---- Configuration ------------------------------------------
91
93 void setFontFilename(const String &val);
94
97 void setFontSize(int val);
98
101 void setDefaultForeground(const Color &c);
102
105 void setDefaultBackground(const Color &c);
106
109 void setDrawBackground(bool v);
110
113 void setMargin(int v);
114
120 void setAnchorOverride(const SubtitleAnchor &v);
121
125 void setTopReserved(int lines);
126
129 void setBottomReserved(int lines);
130
131 // ---- Configuration accessors --------------------------------
132
133 const String &fontFilename() const { return _fontFilename; }
134 int fontSize() const { return _fontSize; }
135 const Color &defaultForeground() const { return _defaultFg; }
136 const Color &defaultBackground() const { return _defaultBg; }
137 bool drawBackground() const { return _drawBackground; }
138 int margin() const { return _margin; }
139 const SubtitleAnchor &anchorOverride() const { return _anchorOverride; }
140 int topReserved() const { return _topReserved; }
141 int bottomReserved() const { return _bottomReserved; }
142
143 // ---- Rendering ----------------------------------------------
144
158 Error render(const Subtitle &subtitle, UncompressedVideoPayload &target);
159
160 private:
161 struct StyledRun {
162 SubtitleSpan span;
163 int width = 0;
164 };
165
167 using StyledRunList = List<StyledRun>;
169 using StyledLineList = List<StyledRunList>;
170
173 SubtitleAnchor effectiveAnchor(const SubtitleAnchor &cueAnchor) const;
174
178 void layoutSpans(const SubtitleSpan::List &spans, StyledLineList &lines);
179
183 void computePosition(const SubtitleAnchor &anchor, const Rect2Di32 &bounds, int maxLineWidth,
184 int totalHeight, int ascender, int &outX, int &outBaselineY) const;
185
188 FastFont::DrawStyle styleFor(const SubtitleSpan &span) const;
189
190 String _fontFilename;
191 int _fontSize = 0; // 0 = auto.
192 Color _defaultFg = Color::White;
193 Color _defaultBg = Color::Black;
194 bool _drawBackground = true;
195 int _margin = 16;
196 SubtitleAnchor _anchorOverride; // Default = honour cue's anchor.
197 int _topReserved = 0;
198 int _bottomReserved = 0;
199
200 // Mutable state — updated from @ref render.
201 FastFont::UPtr _font;
202 int _effectiveFontSize = 0;
203 bool _fontDirty = true;
204};
205
206PROMEKI_NAMESPACE_END
207
208#endif // PROMEKI_ENABLE_PROAV