MythTV  master
subtitlescreen.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #ifndef SUBTITLESCREEN_H
4 #define SUBTITLESCREEN_H
5 
6 #include <utility>
7 #ifdef USING_LIBASS
8 extern "C" {
9 #include <ass/ass.h>
10 }
11 #endif
12 
13 // Qt headers
14 #include <QFont>
15 #include <QHash>
16 #include <QRect>
17 #include <QSize>
18 #include <QStringList>
19 #include <QVector>
20 
21 // MythTV headers
22 #include "mythscreentype.h"
23 #include "subtitlereader.h"
24 #include "mythplayer.h"
25 #include "mythuishape.h"
26 #include "mythuisimpletext.h"
27 #include "mythuiimage.h"
28 
29 class SubtitleScreen;
30 class TestSubtitleScreen;
31 
33 {
34 public:
36  CC708CharacterAttribute formatting,
38  // NOLINTNEXTLINE(performance-move-const-arg)
39  : m_text(std::move(t)), m_format(std::move(formatting)), m_parent(p) {}
40  FormattedTextChunk(void) = default;
41 
42  QSize CalcSize(float layoutSpacing = 0.0F) const;
43  void CalcPadding(bool isFirst, bool isLast, int &left, int &right) const;
44  bool Split(FormattedTextChunk &newChunk);
45  QString ToLogString(void) const;
46  bool PreRender(bool isFirst, bool isLast, int &x, int y, int height);
47 
48  QString m_text;
50  const SubtitleScreen *m_parent {nullptr}; // where fonts and sizes are kept
51 
52  // The following are calculated by PreRender().
53  QString m_bgShapeName;
56  QString m_textName;
57  QRect m_textRect;
58 };
59 
61 {
62 public:
63  explicit FormattedTextLine(int x = -1, int y = -1, int o_x = -1, int o_y = -1)
64  : m_xIndent(x), m_yIndent(y), m_origX(o_x), m_origY(o_y) {}
65 
66  QSize CalcSize(float layoutSpacing = 0.0F) const;
67 
68  QList<FormattedTextChunk> chunks;
69  int m_xIndent {-1}; // -1 means TBD i.e. centered
70  int m_yIndent {-1}; // -1 means TBD i.e. relative to bottom
71  int m_origX {-1}; // original, unscaled coordinates
72  int m_origY {-1}; // original, unscaled coordinates
73 };
74 
76 {
77  friend class TestSubtitleScreen;
78 
79 protected:
80  FormattedTextSubtitle(QString base, QRect safearea,
81  std::chrono::milliseconds start,
82  std::chrono::milliseconds duration,
83  SubtitleScreen *p) :
84  m_base(std::move(base)), m_safeArea(safearea),
85  m_start(start), m_duration(duration), m_subScreen(p) {}
86  FormattedTextSubtitle(void) = default;
87 public:
88  virtual ~FormattedTextSubtitle() = default;
89  // These are the steps that can be done outside of the UI thread
90  // and the decoder thread.
91  virtual void WrapLongLines(void) {}
92  virtual void Layout(void);
93  virtual void PreRender(void);
94  // This is the step that can only be done in the UI thread.
95  virtual void Draw(void);
96  virtual int CacheNum(void) const { return -1; }
97  QStringList ToSRT(void) const;
98 
99 protected:
100  QString m_base;
101  QVector<FormattedTextLine> m_lines;
102  const QRect m_safeArea;
103  std::chrono::milliseconds m_start {0ms};
104  std::chrono::milliseconds m_duration {0ms};
105  SubtitleScreen *m_subScreen {nullptr}; // where fonts and sizes are kept
106  int m_xAnchorPoint {0}; // 0=left, 1=center, 2=right
107  int m_yAnchorPoint {0}; // 0=top, 1=center, 2=bottom
108  int m_xAnchor {0}; // pixels from left
109  int m_yAnchor {0}; // pixels from top
110  QRect m_bounds;
111 };
112 
114 {
115 public:
116  FormattedTextSubtitleSRT(const QString &base,
117  QRect safearea,
118  std::chrono::milliseconds start,
119  std::chrono::milliseconds duration,
120  SubtitleScreen *p,
121  const QStringList &subs) :
122  FormattedTextSubtitle(base, safearea, start, duration, p)
123  {
124  Init(subs);
125  }
126  void WrapLongLines(void) override; // FormattedTextSubtitle
127 private:
128  void Init(const QStringList &subs);
129 };
130 
132 {
133 public:
134  explicit FormattedTextSubtitle608(const vector<CC608Text*> &buffers,
135  const QString &base = "",
136  QRect safearea = QRect(),
137  SubtitleScreen *p = nullptr) :
138  FormattedTextSubtitle(base, safearea, 0ms, 0ms, p)
139  {
140  Init(buffers);
141  }
142  void Layout(void) override; // FormattedTextSubtitle
143 private:
144  void Init(const vector<CC608Text*> &buffers);
145 };
146 
148 {
149 public:
151  int num,
152  const vector<CC708String*> &list,
153  const QString &base = "",
154  QRect safearea = QRect(),
155  SubtitleScreen *p = nullptr,
156  float aspect = 1.77777F) :
157  FormattedTextSubtitle(base, safearea, 0ms, 0ms, p),
158  m_num(num),
159  m_bgFillAlpha(win.GetFillAlpha()),
160  m_bgFillColor(win.GetFillColor())
161  {
162  Init(win, list, aspect);
163  }
164  void Draw(void) override; // FormattedTextSubtitle
165  int CacheNum(void) const override // FormattedTextSubtitle
166  { return m_num; }
167 private:
168  void Init(const CC708Window &win,
169  const vector<CC708String*> &list,
170  float aspect);
171  int m_num;
174 };
175 
177 {
178  Q_OBJECT
179 public:
180  SubtitleScreen(MythPlayer* Player, MythPainter* Painter, const QString& Name, int FontStretch);
181  ~SubtitleScreen() override;
182 
183  void EnableSubtitles(int type, bool forced_only = false);
184  void DisableForcedSubtitles(void);
185  int EnabledSubtitleType(void) const { return m_subtitleType; }
186 
187  void ClearAllSubtitles(void);
188  void ClearNonDisplayedSubtitles(void);
189  void ClearDisplayedSubtitles(void);
190  void DisplayDVDButton(AVSubtitle* dvdButton, QRect &buttonPos);
191 
192  void SetZoom(int percent);
193  int GetZoom(void) const;
194  void SetDelay(std::chrono::milliseconds ms);
195  std::chrono::milliseconds GetDelay(void) const;
196 
197  class SubtitleFormat *GetSubtitleFormat(void) { return m_format; }
198  void Clear708Cache(uint64_t mask);
199  void SetElementAdded(void);
200  void SetElementResized(void);
201  void SetElementDeleted(void);
202 
203  QSize CalcTextSize(const QString &text,
204  const CC708CharacterAttribute &format,
205  float layoutSpacing) const;
206  void CalcPadding(const CC708CharacterAttribute &format,
207  bool isFirst, bool isLast,
208  int &left, int &right) const;
210  void SetFontSize(int pixelSize) { m_fontSize = pixelSize; }
211 
212  // Temporary methods until teletextscreen.cpp is refactored into
213  // subtitlescreen.cpp
214  static QString GetTeletextFontName(void);
215  static int GetTeletextBackgroundAlpha(void);
216 
217  // MythScreenType methods
218  bool Create(void) override; // MythScreenType
219  void Pulse(void) override; // MythUIType
220 
221 private:
222  void ResetElementState(void);
223  void OptimiseDisplayedArea(void);
224  void DisplayAVSubtitles(void);
225  int DisplayScaledAVSubtitles(const AVSubtitleRect *rect, QRect &bbox,
226  bool top, QRect &display, int forced,
227  const QString& imagename,
228  std::chrono::milliseconds displayuntil,
229  std::chrono::milliseconds late);
230  void DisplayTextSubtitles(void);
231  void DisplayRawTextSubtitles(void);
232  void DrawTextSubtitles(const QStringList &subs, std::chrono::milliseconds start,
233  std::chrono::milliseconds duration);
234  void DisplayCC608Subtitles(void);
235  void DisplayCC708Subtitles(void);
236  void AddScaledImage(QImage &img, QRect &pos);
237  void InitializeFonts(bool wasResized);
238 
239  MythPlayer *m_player {nullptr};
240  SubtitleReader *m_subreader {nullptr};
241  CC608Reader *m_cc608reader {nullptr};
242  CC708Reader *m_cc708reader {nullptr};
243  QRect m_safeArea;
244  int m_subtitleType {kDisplayNone};
245  int m_fontSize {0};
246  int m_textFontZoom {100}; // valid for 708 & text subs
247  int m_textFontZoomPrev {100};
248  std::chrono::milliseconds m_textFontDelayMs {0ms}; // valid for text subs
249  std::chrono::milliseconds m_textFontDelayMsPrev {0ms};
250  std::chrono::milliseconds m_textFontMinDurationMs {50ms};
251  std::chrono::milliseconds m_textFontMinDurationMsPrev {50ms};
252  std::chrono::milliseconds m_textFontDurationExtensionMs {0ms};
253  std::chrono::milliseconds m_textFontDurationExtensionMsPrev {0ms};
254  bool m_refreshModified {false};
255  bool m_refreshDeleted {false};
257  QString m_family; // 608, 708, text, teletext
258  // Subtitles initialized but still to be processed and drawn
259  QList<FormattedTextSubtitle *> m_qInited;
260  class SubtitleFormat *m_format {nullptr};
261 
262 #ifdef USING_LIBASS
263  bool InitialiseAssLibrary(void);
264  void LoadAssFonts(void);
265  void CleanupAssLibrary(void);
266  void InitialiseAssTrack(int tracknum);
267  void CleanupAssTrack(void);
268  void AddAssEvent(char *event);
269  void ResizeAssRenderer(void);
270  void RenderAssTrack(std::chrono::milliseconds timecode);
271 
272  ASS_Library *m_assLibrary {nullptr};
273  ASS_Renderer *m_assRenderer {nullptr};
274  int m_assTrackNum {-1};
275  ASS_Track *m_assTrack {nullptr};
276  uint m_assFontCount {0};
277 #endif // USING_LIBASS
278 };
279 
280 #endif // SUBTITLESCREEN_H
FormattedTextSubtitle
Definition: subtitlescreen.h:75
FormattedTextSubtitle::CacheNum
virtual int CacheNum(void) const
Definition: subtitlescreen.h:96
SubtitleScreen::EnabledSubtitleType
int EnabledSubtitleType(void) const
Definition: subtitlescreen.h:185
FormattedTextSubtitle::Layout
virtual void Layout(void)
Definition: subtitlescreen.cpp:789
FormattedTextSubtitle::m_base
QString m_base
Definition: subtitlescreen.h:100
FormattedTextChunk::Split
bool Split(FormattedTextChunk &newChunk)
Definition: subtitlescreen.cpp:663
FormattedTextLine
Definition: subtitlescreen.h:60
FormattedTextSubtitle::FormattedTextSubtitle
FormattedTextSubtitle(QString base, QRect safearea, std::chrono::milliseconds start, std::chrono::milliseconds duration, SubtitleScreen *p)
Definition: subtitlescreen.h:80
kDisplayNone
@ kDisplayNone
Definition: videoouttypes.h:12
FormattedTextLine::m_xIndent
int m_xIndent
Definition: subtitlescreen.h:69
CC708Window
Definition: cc708window.h:198
FormattedTextChunk::FormattedTextChunk
FormattedTextChunk(QString t, CC708CharacterAttribute formatting, SubtitleScreen *p)
Definition: subtitlescreen.h:35
mythuisimpletext.h
FormattedTextSubtitle708::m_num
int m_num
Definition: subtitlescreen.h:171
FormattedTextSubtitleSRT
Definition: subtitlescreen.h:113
MythScreenType::Create
virtual bool Create(void)
Definition: mythscreentype.cpp:265
CC708CharacterAttribute
Definition: cc708window.h:87
SubtitleReader
Definition: subtitlereader.h:33
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:44
FormattedTextSubtitle708::m_bgFillAlpha
uint m_bgFillAlpha
Definition: subtitlescreen.h:172
mythplayer.h
FormattedTextLine::m_origY
int m_origY
Definition: subtitlescreen.h:72
subtitlereader.h
MythPlayer
Definition: mythplayer.h:86
FormattedTextChunk::m_bgShapeRect
QRect m_bgShapeRect
Definition: subtitlescreen.h:54
FormattedTextSubtitle608::FormattedTextSubtitle608
FormattedTextSubtitle608(const vector< CC608Text * > &buffers, const QString &base="", QRect safearea=QRect(), SubtitleScreen *p=nullptr)
Definition: subtitlescreen.h:134
FormattedTextLine::CalcSize
QSize CalcSize(float layoutSpacing=0.0F) const
Definition: subtitlescreen.cpp:756
MythUIType::Pulse
virtual void Pulse(void)
Pulse is called 70 times a second to trigger a single frame of an animation.
Definition: mythuitype.cpp:441
FormattedTextSubtitle708
Definition: subtitlescreen.h:147
mythuiimage.h
FormattedTextSubtitle608
Definition: subtitlescreen.h:131
FormattedTextSubtitle::m_safeArea
const QRect m_safeArea
Definition: subtitlescreen.h:102
SubtitleScreen::m_fontStretch
int m_fontStretch
Definition: subtitlescreen.h:256
SubtitleScreen::m_family
QString m_family
Definition: subtitlescreen.h:257
SubtitleScreen
Definition: subtitlescreen.h:176
FormattedTextChunk::CalcPadding
void CalcPadding(bool isFirst, bool isLast, int &left, int &right) const
Definition: subtitlescreen.cpp:657
FormattedTextChunk
Definition: subtitlescreen.h:32
FormattedTextChunk::FormattedTextChunk
FormattedTextChunk(void)=default
MythUIType::SetZoom
void SetZoom(float zoom)
Definition: mythuitype.cpp:950
FormattedTextLine::m_origX
int m_origX
Definition: subtitlescreen.h:71
hardwareprofile.config.p
p
Definition: config.py:33
hardwareprofile.i18n.t
t
Definition: i18n.py:36
FormattedTextSubtitle708::CacheNum
int CacheNum(void) const override
Definition: subtitlescreen.h:165
SubtitleScreen::m_qInited
QList< FormattedTextSubtitle * > m_qInited
Definition: subtitlescreen.h:259
MythFontProperties
Definition: mythfontproperties.h:13
FormattedTextChunk::m_parent
const SubtitleScreen * m_parent
Definition: subtitlescreen.h:50
FormattedTextChunk::m_textFont
MythFontProperties * m_textFont
Definition: subtitlescreen.h:55
FormattedTextChunk::m_textRect
QRect m_textRect
Definition: subtitlescreen.h:57
uint
unsigned int uint
Definition: compat.h:140
FormattedTextLine::FormattedTextLine
FormattedTextLine(int x=-1, int y=-1, int o_x=-1, int o_y=-1)
Definition: subtitlescreen.h:63
FormattedTextSubtitleSRT::FormattedTextSubtitleSRT
FormattedTextSubtitleSRT(const QString &base, QRect safearea, std::chrono::milliseconds start, std::chrono::milliseconds duration, SubtitleScreen *p, const QStringList &subs)
Definition: subtitlescreen.h:116
FormattedTextChunk::m_textName
QString m_textName
Definition: subtitlescreen.h:56
FormattedTextChunk::m_text
QString m_text
Definition: subtitlescreen.h:48
FormattedTextSubtitle708::FormattedTextSubtitle708
FormattedTextSubtitle708(const CC708Window &win, int num, const vector< CC708String * > &list, const QString &base="", QRect safearea=QRect(), SubtitleScreen *p=nullptr, float aspect=1.77777F)
Definition: subtitlescreen.h:150
FormattedTextSubtitle::m_lines
QVector< FormattedTextLine > m_lines
Definition: subtitlescreen.h:101
buffers
Definition: freesurround.cpp:49
MTV_PUBLIC
#define MTV_PUBLIC
Definition: mythtvexp.h:15
FormattedTextSubtitle708::m_bgFillColor
QColor m_bgFillColor
Definition: subtitlescreen.h:173
FormattedTextChunk::CalcSize
QSize CalcSize(float layoutSpacing=0.0F) const
Definition: subtitlescreen.cpp:652
FormattedTextChunk::ToLogString
QString ToLogString(void) const
Definition: subtitlescreen.cpp:683
MythPainter
Definition: mythpainter.h:32
SubtitleScreen::GetSubtitleFormat
class SubtitleFormat * GetSubtitleFormat(void)
Definition: subtitlescreen.h:197
Name
Definition: channelsettings.cpp:46
std
Definition: mythchrono.h:23
FormattedTextChunk::m_bgShapeName
QString m_bgShapeName
Definition: subtitlescreen.h:53
FormattedTextChunk::m_format
CC708CharacterAttribute m_format
Definition: subtitlescreen.h:49
MythUIType::GetFont
MythFontProperties * GetFont(const QString &text) const
Definition: mythuitype.cpp:1298
SubtitleScreen::m_safeArea
QRect m_safeArea
Definition: subtitlescreen.h:243
Player
Definition: zmliveplayer.h:34
FormattedTextChunk::PreRender
bool PreRender(bool isFirst, bool isLast, int &x, int y, int height)
Definition: subtitlescreen.cpp:707
CC708Reader
Definition: cc708reader.h:16
SubtitleFormat
Definition: subtitlescreen.cpp:130
CC608Reader
Definition: cc608reader.h:79
SubtitleScreen::SetFontSize
void SetFontSize(int pixelSize)
Definition: subtitlescreen.h:210
FormattedTextLine::chunks
QList< FormattedTextChunk > chunks
Definition: subtitlescreen.h:68
mythuishape.h
FormattedTextSubtitle::Draw
virtual void Draw(void)
Definition: subtitlescreen.cpp:881
FormattedTextSubtitle::m_bounds
QRect m_bounds
Definition: subtitlescreen.h:110
mythscreentype.h
FormattedTextSubtitle::WrapLongLines
virtual void WrapLongLines(void)
Definition: subtitlescreen.h:91
FormattedTextLine::m_yIndent
int m_yIndent
Definition: subtitlescreen.h:70