1 /************************************************************************
2  *                                                                      *
3  *  FreeSynd - a remake of the classic Bullfrog game "Syndicate".       *
4  *                                                                      *
5  *   Copyright (C) 2005  Stuart Binge  <skbinge@gmail.com>              *
6  *   Copyright (C) 2005  Joost Peters  <joostp@users.sourceforge.net>   *
7  *   Copyright (C) 2006  Trent Waddington <qg@biodome.org>              *
8  *   Copyright (C) 2011  Joey Parrish  <joey.parrish@gmail.com>         *
9  *                                                                      *
10  *    This program is free software;  you can redistribute it and / or  *
11  *  modify it  under the  terms of the  GNU General  Public License as  *
12  *  published by the Free Software Foundation; either version 2 of the  *
13  *  License, or (at your option) any later version.                     *
14  *                                                                      *
15  *    This program is  distributed in the hope that it will be useful,  *
16  *  but WITHOUT  ANY WARRANTY;  without even  the implied  warranty of  *
17  *  MERCHANTABILITY  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
18  *  General Public License for more details.                            *
19  *                                                                      *
20  *    You can view the GNU  General Public License, online, at the GNU  *
21  *  project's  web  site;  see <http://www.gnu.org/licenses/gpl.html>.  *
22  *  The full text of the license is also included in the file COPYING.  *
23  *                                                                      *
24  ************************************************************************/
25 
26 #ifndef FONT_H
27 #define FONT_H
28 
29 #include "common.h"
30 #include "spritemanager.h"
31 #include <map>
32 
33 /*!
34  * Font range description for 8-bit character sets.
35  */
36 class FontRange {
37 public:
38     FontRange();
39     FontRange(const std::string& valid_chars);
40 
in_range(unsigned char c)41     inline bool in_range(unsigned char c) {
42         return (char_present_[c / 32] & (1 << (c % 32))) != 0;
43     }
44 
45 private:
46     unsigned int char_present_[8]; // 256 bits
47 };
48 
49 /*!
50  * Font class.
51  */
52 class Font {
53 public:
Font()54     Font() {}
~Font()55     virtual ~Font() {}
56 
57     void setSpriteManager(SpriteManager *sprites, int offset, char base,
58             const std::string& valid_chars);
59     void setSpriteManager(SpriteManager *sprites, int offset, char base,
60             const FontRange& range);
61     // If dos is true, the text is in cp437, otherwise it's utf-8.
62     void drawText(int x, int y, const char *text, bool dos, bool x2 = true);
63     int textWidth(const char *text, bool dos, bool x2 = true);
64     int textHeight(bool x2 = true);
65 
66     // returns true if given code point is printable with the font
67     bool isPrintable(uint16 unicode);
68 
69 protected:
70     static unsigned char decode(const unsigned char * &c, bool dos);
71     static int decodeUTF8(const unsigned char * &c);
72     virtual Sprite *getSprite(unsigned char dos_char);
73 
74     SpriteManager *sprites_;
75     int offset_;
76     FontRange range_;
77 };
78 
79 /*!
80  * Font used in menu and widget. Text can be highlighted or dark.
81  * Text can be in UTF-8 or Cp437.
82  */
83 class MenuFont : public Font {
84 public:
85     MenuFont();
86 
87     void setSpriteManager(SpriteManager *sprites, int darkOffset, int lightOffset, char base,
88             const std::string& valid_chars);
89 
90     //! draws a UTF-8 text at the given position
91     void drawText(int x, int y, const char *text, bool lighted, bool x2 = true) {
92         drawText(x, y, false, text, lighted, x2);
93     };
94     //! draws a Cp437 text at the given position
95     void drawTextCp437(int x, int y, const char *text, bool lighted, bool x2 = true) {
96         drawText(x, y, true, text, lighted, x2);
97     };
98 
99 protected:
100     //! returns the sprite which can be highlighted or not
101     virtual Sprite *getSprite(unsigned char dos_char, bool highlighted);
102     //! draws a text at the given position
103     void drawText(int x, int y, bool dos, const char *text, bool lighted, bool x2 = true);
104 
105 protected:
106     int lightOffset_;
107 };
108 
109 /*!
110  * GameFont is the font used during gameplay (for displaying hints).
111  * It uses the same sprites as the menu fonts but only in size 3 and it
112  * changes its color to a specified color.
113  * It uses only UTF-8 text in input.
114  */
115 class GameFont : public Font {
116 public:
117     GameFont();
118 
119     //! draw a UTF-8 text at the given position with the given color
120     void drawText(int x, int y, const char *text, uint8 toColor);
121 };
122 
123 class HChar {
124 public:
125     HChar();
126     ~HChar();
127 
128     void set(int w, int h, uint8 *data);
129 
130     int draw(int x, int y, uint8 color);
131 
132 protected:
133     int width_;
134     int height_;
135     bool *bits_;
136 };
137 
138 class HFont {
139 public:
140     HFont();
141     ~HFont();
142 
143     void load();
144 
145     void drawText(int x, int y, const char *str, uint8 color);
146 
147 protected:
148     std::map<char, HChar> characters;
149 };
150 
151 #endif
152