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  bool isSfx() const;
73 
74 private:
76 };
77 
78 class Subtitles {
79 public:
80  enum FontStyle : int {
81  kFontStyleRegular = 0,
82  kFontStyleItalic,
83  };
84 
85  Subtitles();
86  virtual ~Subtitles();
87 
88  void loadSRTFile(const Common::Path &fname);
89  void close();
90  void setFont(const char *fontname, int height = 18, FontStyle type = kFontStyleRegular);
91  void setBBox(const Common::Rect &bbox);
92  void setColor(byte r, byte g, byte b);
93  void setPadding(uint16 horizontal, uint16 vertical);
94  bool drawSubtitle(uint32 timestamp, bool force = false, bool showSFX = false) const;
95  bool isSfx() const {
96  if (!_srtParser)
97  return false;
98  return _srtParser->isSfx();
99  }
100  bool isLoaded() const { return _loaded || _subtitleDev; }
101  virtual void clearSubtitle() const;
102 
103 protected:
104  bool recalculateBoundingBox() const;
105  void renderSubtitle() const;
106  void translateBBox(int16 dx, int16 dy) const { _realBBox.translate(dx, dy); }
107  virtual void updateSubtitleOverlay() const;
108  virtual bool shouldShowSubtitle() const { return true; }
109 
110  bool _loaded;
111  mutable const Common::Array<SubtitlePart> *_parts = nullptr;
112  mutable uint16 _splitPartCount = 0;
113 
114 private:
115  SRTParser *_srtParser = nullptr;
116  bool _subtitleDev;
117  bool _overlayHasAlpha;
118 
119  mutable Common::Array<SubtitlePart> _devParts;
120 
122  int _fontHeight;
123 
124  mutable Graphics::Surface _surface;
125 
126  mutable Common::Rect _drawRect;
127  Common::Rect _requestedBBox;
128  mutable Common::Rect _realBBox;
129  mutable int16 _lastOverlayWidth, _lastOverlayHeight;
130 
131  Common::Path _fname;
132  uint32 _color;
133  uint32 _blackColor;
134  uint32 _transparentColor;
135  uint16 _hPad;
136  uint16 _vPad;
137 };
138 
139 } // End of namespace Video
140 
141 #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:78
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