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 // Font management and font drawing header file
23 
24 #ifndef SAGA_FONT_H
25 #define SAGA_FONT_H
26 
27 #include "common/list.h"
28 #include "saga/gfx.h"
29 
30 namespace Graphics {
31  class FontSJIS;
32 }
33 
34 namespace Saga {
35 
36 #define FONT_SHOWUNDEFINED 1 // Define to draw undefined characters * as ?'s
37 
38 // The first defined character (!) is the only one that may
39 // have a valid offset of '0'
40 #define FONT_FIRSTCHAR 33
41 
42 #define FONT_CH_TAB 9
43 #define FONT_CH_SPACE 32
44 #define FONT_CH_QMARK 63
45 
46 // Minimum font header size without font data
47 // (6 + 512 + 256 + 256 + 256 )
48 #define FONT_DESCSIZE 1286
49 
50 #define FONT_CHARCOUNT 256
51 #define FONT_CHARMASK 0xFFU
52 
53 #define SAGA_FONT_HEADER_LEN 6
54 
55 #define TEXT_CENTERLIMIT 50
56 #define TEXT_MARGIN 10
57 #define TEXT_LINESPACING 2
58 
59 enum FontEffectFlags {
60  kFontNormal = 0,
61  kFontOutline = 1 << 0,
62  kFontShadow = 1 << 1,
63  kFontBold = 1 << 2,
64  kFontCentered = 1 << 3,
65  kFontDontmap = 1 << 4
66 };
67 
68 enum KnownFont {
69  kKnownFontSmall,
70  kKnownFontMedium,
71  kKnownFontBig,
72 
73  kKnownFontPause,
74  kKnownFontScript,
75  kKnownFontVerb
76 };
77 
78 struct TextListEntry {
79  bool display;
80  bool useRect;
81  Common::Point point;
82  Common::Rect rect;
83  KnownColor knownColor;
84  KnownColor effectKnownColor;
85  FontEffectFlags flags;
86  KnownFont font;
87  const char *text;
88 
89  TextListEntry() {
90  display = false;
91  useRect = false;
92  // point initialized by Common::Point constructor
93  // rect initialized by Common::Rect constructor
94  knownColor = kKnownColorTransparent;
95  effectKnownColor = kKnownColorTransparent;
96  flags = kFontNormal;
97  font = kKnownFontSmall;
98  text = nullptr;
99  }
100 
101  bool operator==(const TextListEntry &e) const {
102  return 0 == memcmp(this, &e, sizeof(*this));
103  }
104 };
105 
106 class TextList: public Common::List<TextListEntry> {
107 public:
108 
109  TextListEntry *addEntry(const TextListEntry &entry) {
112  }
113 };
114 
115 struct FontHeader {
116  int charHeight;
117  int charWidth;
118  int rowLength;
119 };
120 
122  int index;
123  int byteWidth;
124  int width;
125  int flag;
126  int tracking;
127 };
128 
129 struct FontStyle {
130  FontHeader header;
131  FontCharEntry fontCharEntry[256];
132 #ifndef __DS__
133  ByteArray font;
134 #else
135  byte* font;
136 #endif
137 };
138 
139 struct FontData {
140  FontStyle normal;
141  FontStyle outline;
142 };
143 
144 class Font {
145 public:
146  Font(SagaEngine *vm) : _vm(vm) {}
147  virtual ~Font() {}
148 
149  int getStringWidth(KnownFont font, const char *text, size_t count, FontEffectFlags flags) {
150  return getStringWidth(knownFont2FontIdx(font), text, count, flags);
151  }
152 
153  int getHeight(KnownFont font) {
154  return getHeight(knownFont2FontIdx(font));
155  }
156 
157  int getHeight(KnownFont font, const char *text, int width, FontEffectFlags flags) {
158  return getHeight(knownFont2FontIdx(font), text, width, flags);
159  }
160 
161  void textDraw(KnownFont font, const char *string, const Common::Point &point, int color, int effectColor, FontEffectFlags flags) {
162  textDraw(knownFont2FontIdx(font), string, point, color, effectColor, flags);
163  }
164 
165  void textDrawRect(KnownFont font, const char *text, const Common::Rect &rect, int color, int effectColor, FontEffectFlags flags) {
166  textDrawRect(knownFont2FontIdx(font), text, rect, color, effectColor, flags);
167  }
168 
169  virtual void setFontMapping(int) {}
170 
171 protected:
172  enum FontId {
173  kSmallFont,
174  kMediumFont,
175  kBigFont,
176  kIHNMUnknown,
177  kIHNMFont8,
178  kIHNMUnknown2,
179  kIHNMMainFont
180  };
181 
182  SagaEngine *_vm;
183 
184 private:
185  FontId knownFont2FontIdx(KnownFont font);
186  void textDraw(FontId fontId, const char *string, const Common::Point &point, int color, int effectColor, FontEffectFlags flags);
187 
188  virtual void textDrawRect(FontId fontId, const char *text, const Common::Rect &rect, int color, int effectColor, FontEffectFlags flags) = 0;
189  virtual int translateChar(int charId) = 0;
190  virtual int getStringLength(const char *text) = 0;
191  virtual int getStringWidth(FontId fontId, const char *text, size_t count, FontEffectFlags flags) = 0;
192  virtual int getHeight(FontId fontId, const char *text, int width, FontEffectFlags flags) = 0;
193  virtual int getHeight(FontId fontId) = 0;
194  virtual bool valid(FontId) = 0;
195  virtual void draw(FontId fontId, const char *text, size_t count, const Common::Point &point, int color, int effectColor, FontEffectFlags flags) = 0;
196 };
197 
198 class DefaultFont : public Font {
199  public:
200  DefaultFont(SagaEngine *vm);
201  ~DefaultFont() override;
202 
203  void setFontMapping(int mapping) override {
204  _fontMapping = mapping;
205  }
206 
207  private:
208  void textDrawRect(FontId fontId, const char *text, const Common::Rect &rect, int color, int effectColor, FontEffectFlags flags) override;
209  int translateChar(int charId) override;
210  int getStringLength(const char *text) override {
211  return strlen(text);
212  }
213 
214  int getStringWidth(FontId fontId, const char *text, size_t count, FontEffectFlags flags) override;
215  int getHeight(FontId fontId, const char *text, int width, FontEffectFlags flags) override;
216  int getHeight(FontId fontId) override {
217  return getFont(fontId)->normal.header.charHeight;
218  }
219 
220  int getHeight(FontId fontId, const char *text);
221 
222  void validate(FontId fontId) {
223  if (!valid(fontId)) {
224  error("Font::validate: Invalid font id");
225  }
226  }
227 
228  bool valid(FontId fontId) override {
229  return (uint(fontId) < _fonts.size());
230  }
231 
232  FontData *getFont(FontId fontId) {
233  validate(fontId);
234  return &_fonts[fontId];
235  }
236 
237  void draw(FontId fontId, const char *text, size_t count, const Common::Point &point, int color, int effectColor, FontEffectFlags flags) override;
238  void outFont(const FontStyle &drawFont, const char *text, size_t count, const Common::Point &point, int color, FontEffectFlags flags);
239  void loadFont(FontData *font, uint32 fontResourceId);
240  void loadFont(FontData *font, const ByteArray& fontResourceData, bool isBigEndian);
241  void loadChineseFontIHNM(FontData *font, uint32 fontResourceId);
242  void loadChineseFontITE(const Common::Path &fileName);
243  void loadKoreanFontIHNM(const Common::Path &fileName);
244  void saveBig5Index(byte head, byte tail, uint curIdx);
245  void createOutline(FontData *font);
246  void blitGlyph(const Common::Point &textPoint, const byte* bitmap, int charWidth, int charHeight, int rowLength, byte color);
247 
248  int getByteLen(int numBits) const {
249  int byteLength = numBits / 8;
250 
251  if (numBits % 8) {
252  byteLength++;
253  }
254 
255  return byteLength;
256  }
257 
258  static const int _charMap[128];
259 
261  int _fontMapping;
262 
263  byte *_chineseFont;
264  Common::Array<int> _chineseFontIndex;
265  int _cjkFontWidth;
266  int _cjkFontHeight;
267 
268  byte *_koreanFont;
269 
270  static const int kIHNMKoreanGlyphBytes = 32;
271  static const int kIHNMKoreanInitials = 20;
272  static const int kIHNMKoreanInitialVariants = 8;
273  static const int kIHNMKoreanMidOffset = kIHNMKoreanInitialVariants * kIHNMKoreanInitials;
274  static const int kIHNMKoreanMids = 22;
275  static const int kIHNMKoreanMidVariants = 4;
276  static const int kIHNMKoreanFinalsOffset = kIHNMKoreanMidOffset + kIHNMKoreanMids * kIHNMKoreanMidVariants;
277  static const int kIHNMKoreanFinals = 28;
278  static const int kIHNMKoreanFinalVariants = 4;
279  static const int kIHNMKoreanNonJamoOffset = kIHNMKoreanFinalsOffset + kIHNMKoreanFinals * kIHNMKoreanFinalVariants;
280  static const int kIHNMKoreanNonJamo = 126;
281 };
282 
283 class SJISFont : public Font {
284 public:
285  SJISFont(SagaEngine *vm);
286  ~SJISFont() override;
287 
288 private:
289  void textDrawRect(FontId fontId, const char *text, const Common::Rect &rect, int color, int effectColor, FontEffectFlags flags) override;
290  int translateChar(int charId) override { return charId; }
291  int getStringLength(const char *text) override;
292  int getStringWidth(FontId fontId, const char *text, size_t count, FontEffectFlags flags) override;
293  int getHeight(FontId fontId, const char *text, int width, FontEffectFlags flags) override;
294  int getHeight(FontId fontId) override;
295  bool valid(FontId fontId) override { return fontId != kBigFont; }
296 
297  void draw(FontId fontId, const char *text, size_t count, const Common::Point &point, int color, int effectColor, FontEffectFlags flags) override;
298 
299  uint16 fetchChar(const char *&s) const;
300  bool preventLineBreakForCharacter(uint16 ch) const;
301 
302  Graphics::FontSJIS *_font;
303 };
304 
305 } // End of namespace Saga
306 
307 #endif
Definition: font.h:78
Definition: saga.h:497
Definition: font.h:198
Definition: font.h:144
Definition: array.h:52
Definition: list.h:44
Definition: rect.h:144
Definition: font.h:139
Definition: path.h:52
Definition: font.h:283
Definition: saga.h:464
Definition: font.h:129
Definition: actor.h:34
iterator end()
Definition: list.h:240
Definition: formatinfo.h:28
Definition: rect.h:45
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: font.h:115
Definition: font.h:121
Definition: font.h:106
void push_back(const t_T &element)
Definition: list.h:140