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>
15#include <promeki/error.h>
16#if PROMEKI_ENABLE_FREETYPE
17#include <promeki/fastfont.h>
18#endif
19#include <promeki/namespace.h>
20#include <promeki/rect.h>
21#include <promeki/string.h>
22
23PROMEKI_NAMESPACE_BEGIN
24
25class Subtitle;
26class UncompressedVideoPayload;
27
84class SubtitleRenderer {
85 public:
86 SubtitleRenderer();
87 ~SubtitleRenderer();
88
89 SubtitleRenderer(const SubtitleRenderer &) = delete;
90 SubtitleRenderer &operator=(const SubtitleRenderer &) = delete;
91
92 // ---- Configuration ------------------------------------------
93
95 void setFontFilename(const String &val);
96
99 void setFontSize(int val);
100
103 void setDefaultForeground(const Color &c);
104
107 void setDefaultBackground(const Color &c);
108
111 void setDrawBackground(bool v);
112
115 void setMargin(int v);
116
122 void setAnchorOverride(const SubtitleAnchor &v);
123
127 void setTopReserved(int lines);
128
131 void setBottomReserved(int lines);
132
133 // ---- Configuration accessors --------------------------------
134
135 const String &fontFilename() const { return _fontFilename; }
136 int fontSize() const { return _fontSize; }
137 const Color &defaultForeground() const { return _defaultFg; }
138 const Color &defaultBackground() const { return _defaultBg; }
139 bool drawBackground() const { return _drawBackground; }
140 int margin() const { return _margin; }
141 const SubtitleAnchor &anchorOverride() const { return _anchorOverride; }
142 int topReserved() const { return _topReserved; }
143 int bottomReserved() const { return _bottomReserved; }
144
145 // ---- Rendering ----------------------------------------------
146
160 Error render(const Subtitle &subtitle, UncompressedVideoPayload &target);
161
162 private:
163 struct StyledRun {
164 SubtitleSpan span;
165 int width = 0;
166 };
167
169 using StyledRunList = List<StyledRun>;
171 using StyledLineList = List<StyledRunList>;
172
175 SubtitleAnchor effectiveAnchor(const SubtitleAnchor &cueAnchor) const;
176
180 void layoutSpans(const SubtitleSpan::List &spans, StyledLineList &lines);
181
185 void computePosition(const SubtitleAnchor &anchor, const Rect2Di32 &bounds, int maxLineWidth,
186 int totalHeight, int ascender, int &outX, int &outBaselineY) const;
187
188#if PROMEKI_ENABLE_FREETYPE
191 FastFont::DrawStyle styleFor(const SubtitleSpan &span) const;
192#endif
193
194 String _fontFilename;
195 int _fontSize = 0; // 0 = auto.
196 Color _defaultFg = Color::White;
197 Color _defaultBg = Color::Black;
198 bool _drawBackground = true;
199 int _margin = 16;
200 SubtitleAnchor _anchorOverride; // Default = honour cue's anchor.
201 int _topReserved = 0;
202 int _bottomReserved = 0;
203
204 // Mutable state — updated from @ref render. The glyph
205 // renderer is only present when FreeType is compiled in;
206 // without it @ref render returns @c Error::FontUnavailable.
207#if PROMEKI_ENABLE_FREETYPE
208 FastFont::UPtr _font;
209#endif
210 int _effectiveFontSize = 0;
211 bool _fontDirty = true;
212};
213
214PROMEKI_NAMESPACE_END
215
216#endif // PROMEKI_ENABLE_PROAV