ScummVM API documentation
subtitles.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef VIDEO_SUBTITLES_H
23 #define VIDEO_SUBTITLES_H
24 
25 #include "common/str.h"
26 #include "common/array.h"
27 #include "common/hashmap.h"
28 #include "common/rect.h"
29 
30 #include "graphics/surface.h"
31 
32 namespace Graphics {
33 class Font;
34 }
35 
36 namespace Video {
37 
38 struct SubtitlePart {
39  Common::String text;
40  Common::String tag;
41 
42  SubtitlePart(const Common::String &text_, const Common::String &tag_) : text(text_), tag(tag_) {}
43 };
44 
45 struct SRTEntry {
46  uint seq;
47  uint32 start;
48  uint32 end;
49 
51 
52  SRTEntry(uint seq_, uint32 start_, uint32 end_) {
53  seq = seq_; start = start_; end = end_;
54  }
55 
56  // Dummy constructor for bsearch
57  SRTEntry(uint seq_, uint32 start_, uint32 end_, const Common::String &text, const Common::String &tag = "") {
58  seq = seq_; start = start_; end = end_;
59  parts.push_back(SubtitlePart(text, tag));
60  }
61 };
62 
63 class SRTParser {
64 public:
65  SRTParser();
66  ~SRTParser();
67 
68  void cleanup();
69  bool parseFile(const Common::Path &fname);
70  void parseTextAndTags(const Common::String &text, Common::Array<SubtitlePart> &parts) const;
71  const Common::Array<SubtitlePart> *getSubtitleParts(uint32 timestamp) const;
72 
73 private:
75 };
76 
77 class Subtitles {
78 public:
79  enum FontStyle : int {
80  kFontStyleRegular = 0,
81  kFontStyleItalic,
82  };
83 
84  Subtitles();
85  ~Subtitles();
86 
87  void loadSRTFile(const Common::Path &fname);
88  void close() { _loaded = false; _parts = nullptr; _fname.clear(); _srtParser.cleanup(); }
89  void setFont(const char *fontname, int height = 18, FontStyle type = kFontStyleRegular);
90  void setBBox(const Common::Rect &bbox);
91  void setColor(byte r, byte g, byte b);
92  void setPadding(uint16 horizontal, uint16 vertical);
93  bool drawSubtitle(uint32 timestamp, bool force = false, bool showSFX = false) const;
94  bool isLoaded() const { return _loaded || _subtitleDev; }
95 
96 private:
97  void renderSubtitle() const;
98 
99  SRTParser _srtParser;
100  bool _loaded;
101  bool _subtitleDev;
102  bool _overlayHasAlpha;
103 
104  mutable Common::Array<SubtitlePart> _devParts;
105 
107  int _fontHeight;
108 
109  mutable Graphics::Surface _surface;
110 
111  mutable Common::Rect _drawRect;
112  Common::Rect _requestedBBox;
113  mutable Common::Rect _realBBox;
114  mutable int16 _lastOverlayWidth, _lastOverlayHeight;
115 
116  Common::Path _fname;
117  mutable const Common::Array<SubtitlePart> *_parts;
118  uint32 _color;
119  uint32 _blackColor;
120  uint32 _transparentColor;
121  uint16 _hPad;
122  uint16 _vPad;
123 };
124 
125 } // End of namespace Video
126 
127 #endif
Definition: str.h:59
Definition: surface.h:67
Definition: subtitles.h:38
Definition: array.h:52
Definition: rect.h:524
Definition: path.h:52
Definition: subtitles.h:77
void push_back(const T &element)
Definition: array.h:181
Definition: hashmap.h:85
Definition: formatinfo.h:28
Definition: subtitles.h:45
Definition: subtitles.h:63
Definition: animation.h:37