ScummVM API documentation
font.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 GRIM_FONT_H
23 #define GRIM_FONT_H
24 
25 #include "engines/grim/pool.h"
26 
27 #include "graphics/font.h"
28 #include "graphics/pixelformat.h"
29 
30 namespace Common {
31 class SeekableReadStream;
32 }
33 
34 namespace Grim {
35 
36 class SaveGame;
37 
38 class Font {
39 public:
40  virtual ~Font() {}
41 
42  virtual int32 getKernedHeight() const = 0;
43  virtual int32 getFontWidth() const = 0;
44  virtual int getKernedStringLength(const Common::String &text) const = 0;
45  virtual int32 getBaseOffsetY() const = 0;
46  virtual void render(Graphics::Surface &buf, const Common::String &currentLine, const Graphics::PixelFormat &pixelFormat, uint32 blackColor, uint32 color, uint32 colorKey) const = 0;
47  virtual int32 getCharKernedWidth(uint32 c) const = 0;
48  virtual int getPoolId() const = 0;
49  virtual int32 getPoolTag() const = 0;
50  virtual bool is8Bit() const = 0;
51  const Common::String &getFilename() const { return _filename; }
52 
53  // for Korean Translate
54  int32 getWCharKernedWidth(byte hi, byte lo) const { return getCharKernedWidth(hi) + getCharKernedWidth(lo); }
55  bool isKoreanChar(const byte hi, const byte lo) const { return (hi >= 0xB0 && hi <= 0xC8 && lo >= 0xA1 && lo <= 0xFE); }
56 
57  static Font *getByFileName(const Common::String& fileName);
58  static Font *getFirstFont();
59  static void save(const Font *font, SaveGame *state);
60  static Font *load(SaveGame *state);
61 
62 protected:
63  Common::String _filename;
64 };
65 
66 class BitmapFont : public Font, public PoolObject<BitmapFont> {
67 public:
68  BitmapFont();
69  ~BitmapFont();
70 
71  static int32 getStaticTag() { return MKTAG('F', 'O', 'N', 'T'); }
72  int getPoolId() const override { return getId(); }
73  int32 getPoolTag() const override { return getStaticTag(); }
74 
75  void load(const Common::String &filename, Common::SeekableReadStream *data);
76  void loadTGA(const Common::String &filename, Common::SeekableReadStream *index, Common::SeekableReadStream *image);
77 
78  const Common::String &getFilename() const { return _filename; }
79  int32 getKernedHeight() const override { return _kernedHeight; }
80  int32 getFontWidth() const override { return getCharKernedWidth('w'); }
81  int32 getBaseOffsetY() const override { return _baseOffsetY; }
82  void render(Graphics::Surface &buf, const Common::String &currentLine, const Graphics::PixelFormat &pixelFormat, uint32 blackColor, uint32 color, uint32 colorKey) const override;
83  int32 getCharBitmapWidth(uint32 c) const { return _charHeaders[getCharIndex(c)].bitmapWidth; }
84  int32 getCharBitmapPitch(uint32 c) const { return _charHeaders[getCharIndex(c)].bitmapPitch; }
85  int32 getCharBitmapHeight(uint32 c) const { return _charHeaders[getCharIndex(c)].bitmapHeight; }
86  int32 getCharKernedWidth(uint32 c) const override { return _charHeaders[getCharIndex(c)].kernedWidth; }
87  int32 getCharStartingCol(uint32 c) const { return _charHeaders[getCharIndex(c)].startingCol; }
88  int32 getCharStartingLine(uint32 c) const { return _charHeaders[getCharIndex(c)].startingLine; }
89  int32 getCharOffset(uint32 c) const { return _charHeaders[getCharIndex(c)].offset; }
90  const byte *getCharData(uint32 c) const { return _fontData + (_charHeaders[getCharIndex(c)].offset); }
91 
92  const byte *getFontData() const { return _fontData; }
93  uint32 getDataSize() const { return _dataSize; }
94  bool is8Bit() const override;
95 
96  int getKernedStringLength(const Common::String &text) const override;
97  int getBitmapStringLength(const Common::String &text) const;
98  int getStringHeight(const Common::String &text) const;
99 
100  const void *getUserData() const { return _userData; }
101  void setUserData(void *data) { _userData = data; }
102 
103  void saveState(SaveGame *state) const;
104  void restoreState(SaveGame *state);
105 
106  static const uint8 emerFont[][13];
107 private:
108  uint32 getNextChar(const Common::String &text, uint32 &i) const;
109  uint16 getCharIndex(uint32 c) const;
110  struct CharHeader {
111  int32 offset;
112  int8 kernedWidth;
113  int32 startingCol;
114  int32 startingLine;
115  int32 bitmapWidth;
116  int32 bitmapPitch;
117  int32 bitmapHeight;
118  };
119 
120  uint32 _numChars;
121  uint32 _dataSize;
122  uint32 _kernedHeight, _baseOffsetY;
123  uint32 _firstChar, _lastChar;
124  Common::Array<int> _fwdCharIndex;
125  CharHeader *_charHeaders;
126  byte *_fontData;
127  void *_userData;
128  bool _isDBCS, _isUnicode;
129 };
130 
131 class FontTTF : public Font, public PoolObject<FontTTF> {
132 public:
133  void loadTTF(const Common::String &filename, Common::SeekableReadStream *data, int size);
134  void loadTTFFromArchive(const Common::String &filename, int size);
135 
136  static int32 getStaticTag() { return MKTAG('T', 'T', 'F', ' '); }
137  int getPoolId() const override { return getId(); }
138  int32 getPoolTag() const override { return getStaticTag(); }
139 
140  int32 getKernedHeight() const override { return _font->getFontHeight(); }
141  int32 getBaseOffsetY() const override { return 0; }
142  int32 getCharKernedWidth(uint32 c) const override { return _font->getCharWidth(c); }
143  int32 getFontWidth() const override { return getCharKernedWidth('w'); }
144 
145  int getKernedStringLength(const Common::String &text) const override;
146  void render(Graphics::Surface &buf, const Common::String &currentLine, const Graphics::PixelFormat &pixelFormat, uint32 blackColor, uint32 color, uint32 colorKey) const override;
147  bool is8Bit() const override { return false; }
148 
149  void saveState(SaveGame *state) const;
150  void restoreState(SaveGame *state);
151 
152  // for Korean Translate
153  int32 getWCharKernedWidth(byte hi, byte lo) const { return _font->getCharWidth(Common::convertUHCToUCS(hi, lo)); }
154 
155 private:
156  Graphics::Font *_font;
157  bool _isUnicode;
158  int _size;
159 };
160 
161 } // end of namespace Grim
162 
163 #endif
Definition: font.h:131
Definition: str.h:59
Definition: font.h:83
Definition: surface.h:67
Definition: font.h:66
Definition: pixelformat.h:138
Definition: savegame.h:33
Definition: stream.h:745
Definition: actor.h:33
Definition: algorithm.h:29
#define MKTAG(a0, a1, a2, a3)
Definition: endian.h:188
Definition: pool.h:42
Definition: font.h:38