00001
00002
00003 #ifndef SUBTITLESCREEN_H
00004 #define SUBTITLESCREEN_H
00005
00006 #include <QStringList>
00007 #include <QRegExp>
00008 #include <QVector>
00009 #include <QFont>
00010 #include <QHash>
00011 #include <QRect>
00012 #include <QSize>
00013
00014 #ifdef USING_LIBASS
00015 extern "C" {
00016 #include <ass/ass.h>
00017 }
00018 #endif
00019
00020 #include "mythscreentype.h"
00021 #include "subtitlereader.h"
00022 #include "mythplayer.h"
00023
00024 class SubtitleScreen : public MythScreenType
00025 {
00026 friend class FormattedTextSubtitle;
00027
00028 public:
00029 SubtitleScreen(MythPlayer *player, const char * name, int fontStretch);
00030 virtual ~SubtitleScreen();
00031
00032 void EnableSubtitles(int type, bool forced_only = false);
00033 void DisableForcedSubtitles(void);
00034 int EnabledSubtitleType(void) { return m_subtitleType; }
00035
00036 void ClearAllSubtitles(void);
00037 void ClearNonDisplayedSubtitles(void);
00038 void ClearDisplayedSubtitles(void);
00039 void ExpireSubtitles(void);
00040 void DisplayDVDButton(AVSubtitle* dvdButton, QRect &buttonPos);
00041
00042 void SetZoom(int percent);
00043 int GetZoom(void);
00044
00045 QSize CalcTextSize(const QString &text,
00046 const CC708CharacterAttribute &format,
00047 bool teletext,
00048 float layoutSpacing) const;
00049 int CalcPadding(const CC708CharacterAttribute &format,
00050 bool teletext, bool isLeft) const;
00051
00052 void RegisterExpiration(MythUIType *shape, long long endTime)
00053 {
00054 m_expireTimes.insert(shape, endTime);
00055 }
00056
00057
00058
00059 static QString GetTeletextFontName(void);
00060
00061
00062 virtual bool Create(void);
00063 virtual void Pulse(void);
00064
00065 private:
00066 void OptimiseDisplayedArea(void);
00067 void DisplayAVSubtitles(void);
00068 int DisplayScaledAVSubtitles(const AVSubtitleRect *rect, QRect &bbox,
00069 bool top, QRect &display, int forced,
00070 QString imagename,
00071 long long displayuntil, long long late);
00072 void DisplayTextSubtitles(void);
00073 void DisplayRawTextSubtitles(void);
00074 void DrawTextSubtitles(QStringList &wrappedsubs, uint64_t start,
00075 uint64_t duration);
00076 void DisplayCC608Subtitles(void);
00077 void DisplayCC708Subtitles(void);
00078 void AddScaledImage(QImage &img, QRect &pos);
00079 void Clear708Cache(int num);
00080 void InitializeFonts(bool wasResized);
00081 MythFontProperties* GetFont(CC708CharacterAttribute attr,
00082 bool teletext) const;
00083 void SetFontSize(int pixelSize) { m_fontSize = pixelSize; }
00084
00085 MythPlayer *m_player;
00086 SubtitleReader *m_subreader;
00087 CC608Reader *m_608reader;
00088 CC708Reader *m_708reader;
00089 QRect m_safeArea;
00090 QRegExp m_removeHTML;
00091 int m_subtitleType;
00092 QHash<MythUIType*, long long> m_expireTimes;
00093 QHash<MythUIType*, MythImage*> m_avsubCache;
00094 int m_fontSize;
00095 int m_textFontZoom;
00096 int m_textFontZoomPrev;
00097 bool m_refreshArea;
00098 QHash<int,QList<MythUIType*> > m_708imageCache;
00099 int m_fontStretch;
00100 QString m_family;
00101 class SubtitleFormat *m_format;
00102
00103 #ifdef USING_LIBASS
00104 bool InitialiseAssLibrary(void);
00105 void LoadAssFonts(void);
00106 void CleanupAssLibrary(void);
00107 void InitialiseAssTrack(int tracknum);
00108 void CleanupAssTrack(void);
00109 void AddAssEvent(char *event);
00110 void ResizeAssRenderer(void);
00111 void RenderAssTrack(uint64_t timecode);
00112
00113 ASS_Library *m_assLibrary;
00114 ASS_Renderer *m_assRenderer;
00115 int m_assTrackNum;
00116 ASS_Track *m_assTrack;
00117 uint m_assFontCount;
00118 #endif // USING_LIBASS
00119 };
00120
00121 class FormattedTextChunk
00122 {
00123 public:
00124 FormattedTextChunk(const QString &t, CC708CharacterAttribute formatting,
00125 SubtitleScreen *p, bool teletext = false)
00126 : text(t), format(formatting), parent(p), isTeletext(teletext)
00127 {
00128 }
00129 FormattedTextChunk(void) : parent(NULL), isTeletext(false) {}
00130
00131 QSize CalcSize(float layoutSpacing = 0.0f) const
00132 {
00133 return parent->CalcTextSize(text, format, isTeletext, layoutSpacing);
00134 }
00135 int CalcPadding(bool isLeft) const
00136 {
00137 return parent->CalcPadding(format, isTeletext, isLeft);
00138 }
00139 bool Split(FormattedTextChunk &newChunk);
00140 QString ToLogString(void) const;
00141
00142 QString text;
00143 CC708CharacterAttribute format;
00144 SubtitleScreen *parent;
00145 bool isTeletext;
00146 };
00147
00148 class FormattedTextLine
00149 {
00150 public:
00151 FormattedTextLine(int x = -1, int y = -1, int o_x = -1, int o_y = -1)
00152 : x_indent(x), y_indent(y), orig_x(o_x), orig_y(o_y) {}
00153
00154 QSize CalcSize(float layoutSpacing = 0.0f) const
00155 {
00156 int height = 0, width = 0;
00157 int leftPadding = 0, rightPadding = 0;
00158 QList<FormattedTextChunk>::const_iterator it;
00159 for (it = chunks.constBegin(); it != chunks.constEnd(); ++it)
00160 {
00161 QSize tmp = (*it).CalcSize(layoutSpacing);
00162 height = max(height, tmp.height());
00163 width += tmp.width();
00164 leftPadding = (*it).CalcPadding(true);
00165 rightPadding = (*it).CalcPadding(false);
00166 if (it == chunks.constBegin())
00167 width += leftPadding;
00168 }
00169 return QSize(width + rightPadding, height);
00170 }
00171
00172 QList<FormattedTextChunk> chunks;
00173 int x_indent;
00174 int y_indent;
00175 int orig_x, orig_y;
00176 };
00177
00178 class FormattedTextSubtitle
00179 {
00180 public:
00181 FormattedTextSubtitle(const QRect &safearea, SubtitleScreen *p)
00182 : m_safeArea(safearea), parent(p)
00183 {
00184
00185 m_xAnchorPoint = 0;
00186 m_yAnchorPoint = 0;
00187 m_xAnchor = 0;
00188 m_yAnchor = 0;
00189 }
00190 FormattedTextSubtitle(void)
00191 : m_safeArea(QRect()), parent(NULL)
00192 {
00193
00194 m_xAnchorPoint = 0;
00195 m_yAnchorPoint = 0;
00196 m_xAnchor = 0;
00197 m_yAnchor = 0;
00198 }
00199 void InitFromCC608(vector<CC608Text*> &buffers, int textFontZoom = 100);
00200 void InitFromCC708(const CC708Window &win, int num,
00201 const vector<CC708String*> &list,
00202 float aspect = 1.77777f,
00203 int textFontZoom = 100);
00204 void InitFromSRT(QStringList &subs, int textFontZoom);
00205 void WrapLongLines(void);
00206 void Layout(void);
00207 void Layout608(void);
00208 bool Draw(const QString &base, QList<MythUIType*> *imageCache = NULL,
00209 uint64_t start = 0, uint64_t duration = 0) const;
00210 QStringList ToSRT(void) const;
00211 QRect m_bounds;
00212
00213 private:
00214 QVector<FormattedTextLine> m_lines;
00215 const QRect m_safeArea;
00216 SubtitleScreen *parent;
00217 int m_xAnchorPoint;
00218 int m_yAnchorPoint;
00219 int m_xAnchor;
00220 int m_yAnchor;
00221 };
00222
00223 #endif // SUBTITLESCREEN_H