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