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  : text(t), m_format(formatting), parent(p), textFont(nullptr) {}
36  FormattedTextChunk(void) : parent(nullptr), textFont(nullptr) {}
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 text;
46  const SubtitleScreen *parent; // where fonts and sizes are kept
47 
48  // The following are calculated by PreRender().
49  QString bgShapeName;
50  QRect bgShapeRect;
52  QString textName;
53  QRect textRect;
54 };
55 
57 {
58 public:
59  FormattedTextLine(int x = -1, int y = -1, int o_x = -1, int o_y = -1)
60  : x_indent(x), y_indent(y), orig_x(o_x), orig_y(o_y) {}
61 
62  QSize CalcSize(float layoutSpacing = 0.0f) const;
63 
64  QList<FormattedTextChunk> chunks;
65  int x_indent; // -1 means TBD i.e. centered
66  int y_indent; // -1 means TBD i.e. relative to bottom
67  int orig_x, orig_y; // original, unscaled coordinates
68 };
69 
71 {
72 protected:
73  FormattedTextSubtitle(const QString &base, const QRect &safearea,
74  uint64_t start, uint64_t duration,
75  SubtitleScreen *p);
77 public:
78  virtual ~FormattedTextSubtitle() = default;
79  // These are the steps that can be done outside of the UI thread
80  // and the decoder thread.
81  virtual void WrapLongLines(void) {}
82  virtual void Layout(void);
83  virtual void PreRender(void);
84  // This is the step that can only be done in the UI thread.
85  virtual void Draw(void);
86  virtual int CacheNum(void) const { return -1; }
87  QStringList ToSRT(void) const;
88 
89 protected:
90  QString m_base;
91  QVector<FormattedTextLine> m_lines;
92  const QRect m_safeArea;
93  uint64_t m_start;
94  uint64_t m_duration;
95  SubtitleScreen *m_subScreen; // where fonts and sizes are kept
96  int m_xAnchorPoint; // 0=left, 1=center, 2=right
97  int m_yAnchorPoint; // 0=top, 1=center, 2=bottom
98  int m_xAnchor; // pixels from left
99  int m_yAnchor; // pixels from top
100  QRect m_bounds;
101 };
102 
104 {
105 public:
106  FormattedTextSubtitleSRT(const QString &base,
107  const QRect &safearea,
108  uint64_t start,
109  uint64_t duration,
110  SubtitleScreen *p,
111  const QStringList &subs) :
112  FormattedTextSubtitle(base, safearea, start, duration, p)
113  {
114  Init(subs);
115  }
116  void WrapLongLines(void) override; // FormattedTextSubtitle
117 private:
118  void Init(const QStringList &subs);
119 };
120 
122 {
123 public:
124  FormattedTextSubtitle608(const vector<CC608Text*> &buffers,
125  const QString &base = "",
126  const QRect &safearea = QRect(),
127  SubtitleScreen *p = nullptr) :
128  FormattedTextSubtitle(base, safearea, 0, 0, p)
129  {
130  Init(buffers);
131  }
132  void Layout(void) override; // FormattedTextSubtitle
133 private:
134  void Init(const vector<CC608Text*> &buffers);
135 };
136 
138 {
139 public:
141  int num,
142  const vector<CC708String*> &list,
143  const QString &base = "",
144  const QRect &safearea = QRect(),
145  SubtitleScreen *p = nullptr,
146  float aspect = 1.77777f) :
147  FormattedTextSubtitle(base, safearea, 0, 0, p),
148  m_num(num),
149  m_bgFillAlpha(win.GetFillAlpha()),
150  m_bgFillColor(win.GetFillColor())
151  {
152  Init(win, list, aspect);
153  }
154  void Draw(void) override; // FormattedTextSubtitle
155  int CacheNum(void) const override // FormattedTextSubtitle
156  { return m_num; }
157 private:
158  void Init(const CC708Window &win,
159  const vector<CC708String*> &list,
160  float aspect);
161  int m_num;
164 };
165 
167 {
168 public:
169  SubtitleScreen(MythPlayer *player, const char * name, int fontStretch);
170  virtual ~SubtitleScreen();
171 
172  void EnableSubtitles(int type, bool forced_only = false);
173  void DisableForcedSubtitles(void);
174  int EnabledSubtitleType(void) { return m_subtitleType; }
175 
176  void ClearAllSubtitles(void);
177  void ClearNonDisplayedSubtitles(void);
178  void ClearDisplayedSubtitles(void);
179  void DisplayDVDButton(AVSubtitle* dvdButton, QRect &buttonPos);
180 
181  void SetZoom(int percent);
182  int GetZoom(void) const;
183  void SetDelay(int ms);
184  int GetDelay(void) const;
185 
186  class SubtitleFormat *GetSubtitleFormat(void) { return m_format; }
187  void Clear708Cache(uint64_t mask);
188  void SetElementAdded(void);
189  void SetElementResized(void);
190  void SetElementDeleted(void);
191 
192  QSize CalcTextSize(const QString &text,
193  const CC708CharacterAttribute &format,
194  float layoutSpacing) const;
195  void CalcPadding(const CC708CharacterAttribute &format,
196  bool isFirst, bool isLast,
197  int &left, int &right) const;
199  void SetFontSize(int pixelSize) { m_fontSize = pixelSize; }
200 
201  // Temporary methods until teletextscreen.cpp is refactored into
202  // subtitlescreen.cpp
203  static QString GetTeletextFontName(void);
204  static int GetTeletextBackgroundAlpha(void);
205 
206  // MythScreenType methods
207  bool Create(void) override; // MythScreenType
208  void Pulse(void) override; // MythUIType
209 
210 private:
211  void ResetElementState(void);
212  void OptimiseDisplayedArea(void);
213  void DisplayAVSubtitles(void);
214  int DisplayScaledAVSubtitles(const AVSubtitleRect *rect, QRect &bbox,
215  bool top, QRect &display, int forced,
216  QString imagename,
217  long long displayuntil, long long late);
218  void DisplayTextSubtitles(void);
219  void DisplayRawTextSubtitles(void);
220  void DrawTextSubtitles(const QStringList &subs, uint64_t start,
221  uint64_t duration);
222  void DisplayCC608Subtitles(void);
223  void DisplayCC708Subtitles(void);
224  void AddScaledImage(QImage &img, QRect &pos);
225  void InitializeFonts(bool wasResized);
226 
231  QRect m_safeArea;
232  QRegExp m_removeHTML;
235  int m_textFontZoom; // valid for 708 & text subs
237  int m_textFontDelayMs; // valid for text subs
242  QString m_family; // 608, 708, text, teletext
243  // Subtitles initialized but still to be processed and drawn
244  QList<FormattedTextSubtitle *> m_qInited;
246 
247 #ifdef USING_LIBASS
248  bool InitialiseAssLibrary(void);
249  void LoadAssFonts(void);
250  void CleanupAssLibrary(void);
251  void InitialiseAssTrack(int tracknum);
252  void CleanupAssTrack(void);
253  void AddAssEvent(char *event);
254  void ResizeAssRenderer(void);
255  void RenderAssTrack(uint64_t timecode);
256 
257  ASS_Library *m_assLibrary;
258  ASS_Renderer *m_assRenderer;
260  ASS_Track *m_assTrack;
262 #endif // USING_LIBASS
263 };
264 
265 #endif // SUBTITLESCREEN_H
class SubtitleFormat * GetSubtitleFormat(void)
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)
void InitializeFonts(bool wasResized)
CC708Reader * m_708reader
int EnabledSubtitleType(void)
void InitialiseAssTrack(int tracknum)
void CleanupAssLibrary(void)
FormattedTextChunk(const QString &t, const CC708CharacterAttribute &formatting, 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)
virtual ~SubtitleScreen()
void SetFontSize(int pixelSize)
int CacheNum(void) const override
int DisplayScaledAVSubtitles(const AVSubtitleRect *rect, QRect &bbox, bool top, QRect &display, int forced, QString imagename, long long displayuntil, long long late)
SubtitleScreen(MythPlayer *player, const char *name, int fontStretch)
MythFontProperties * textFont
int GetZoom(void) const
void ClearNonDisplayedSubtitles(void)
void Init(const QStringList &subs)
void DisableForcedSubtitles(void)
QSize CalcSize(float layoutSpacing=0.0f) const
const SubtitleScreen * parent
void Init(const CC708Window &win, const vector< CC708String * > &list, float aspect)
void SetElementDeleted(void)
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:340
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:339
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
virtual ~FormattedTextSubtitle()=default
QSize CalcSize(float layoutSpacing=0.0f) const
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)