2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #ifndef FONTMANAGER_HPP_INCLUDED
27 #define FONTMANAGER_HPP_INCLUDED
34 #include "cave/colors.hpp"
35 #include "gfx/pixmapstorage.hpp"
42 /* these can't be larger than 31, or they mess up utf8 coding or are the same as some ascii letter */
43 #define GD_DOWN_CHAR char(1)
44 #define GD_LEFT_CHAR char(2)
45 #define GD_UP_CHAR char(3)
46 #define GD_RIGHT_CHAR char(4)
48 #define GD_BALL_CHAR char(5)
49 #define GD_UNCHECKED_BOX_CHAR char(6)
50 #define GD_CHECKED_BOX_CHAR char(7)
52 #define GD_PLAYER_CHAR char(8)
53 #define GD_TAB_CHAR char(9) // cannot be used, as it is the tabulator
54 #define GD_NEWLINE_CHAR char(10) // this cannot be used as a separate character, as it is the newline char
55 #define GD_SKELETON_CHAR char(11)
56 #define GD_KEY_CHAR char(12)
57 #define GD_FULL_BOX_CHAR char(13)
58 #define GD_DIAMOND_CHAR char(14)
61 #define GD_ACUTE_CHAR char(16)
62 #define GD_UMLAUT_CHAR char(17)
63 #define GD_DOUBLE_ACUTE_CHAR char(18)
65 #define GD_UNKNOWN_CHAR char(127)
72 * @brief The class which manages font rendering and drawing.
74 * After creating a font manager, its blittext functions
75 * can be used to draw text on the screen using C64 fonts.
76 * The blittext function draws wide (16x8), the blittext_n
77 * function draws narrow (8x8) characters. (Of course,
78 * these sizes are enlarged by the pixbuf factory.)
81 class FontManager
: public PixmapStorage
{
83 /// Raw font data. Its format is very similar
84 /// to the one used by cells.
85 /// A font map consists of 128 chars, each usually 8x8 pixel.
86 /// The 128 chars are in a table, 16 horizontally x 8 vertically.
87 std::vector
<unsigned char> font
;
89 /// The size of the font pixbufs, in pixels. This may be sized up when drawing.
90 unsigned int font_size
;
92 /// The currently selected color
93 GdColor current_color
;
95 /// The Screen on which this FontManager is working.
98 /// Rendered fonts are stored in a list for caching.
99 typedef std::list
<RenderedFont
*> container
;
100 /// Cached fonts for narrow and wide letters.
101 container _narrow
, _wide
;
103 /// @brief Return with the narrow rendered font.
104 /// If it does not exist yet, create. If too many
105 /// rendered characters are in the cache, delete the
106 /// oldest (least recently used) one.
107 /// @param c The color to draw the font.
108 /// @return The font created for the color.
109 RenderedFont
*narrow(const GdColor
&c
);
111 /// @brief Return with the wide rendered font.
112 /// If it does not exist yet, create. If too many
113 /// rendered characters are in the cache, delete the
114 /// oldest (least recently used) one.
115 /// @param c The color to draw the font.
116 /// @return The font created for the color.
117 RenderedFont
*wide(const GdColor
&c
);
119 /// @brief Draw a piece of text with wide or narrow font.
120 /// @param x The x coordinate to start drawing at. If -1 is given,
121 /// the text will be centered on the screen.
122 /// @param y The y coordinate to draw at.
123 /// @param text The UTF8 text to draw. Can contain more lines.
124 /// Can contain accented characters, most of which will be drawn
125 /// without an accent.
126 /// @param widefont True for the wide font, false for the narrow one.
127 /// @return The x coordinate at which the drawing was ended.
128 /// By this, more text can be written with successive blittext calls.
129 int blittext_internal(int x
, int y
, char const *text
, bool widefont
);
131 FontManager(const FontManager
&); // not implemented
132 FontManager
&operator=(const FontManager
&); // not implemented
134 bool loadfont_image(Pixbuf
const &loadcells_image
);
135 bool loadfont_file(const std::string
&filename
);
138 /// @brief Creates a font manager.
139 FontManager(Screen
&screen
, const std::string
&theme_file
);
141 /// @brief Destructor.
142 virtual ~FontManager();
144 /// @brief Set the color for the next piece of text drawn.
145 void set_color(GdColor
const &color
) {
146 current_color
= color
;
149 /// @brief Write text on the screen with the wide font.
150 /// For more info, look at blittext_internal.
151 int blittext(int x
, int y
, char const *text
) {
152 return blittext_internal(x
, y
, text
, true);
155 /// @brief Write text on the screen with the wide font.
156 /// For more info, look at blittext_internal.
157 int blittext(int x
, int y
, const GdColor
&color
, char const *text
) {
159 return blittext_internal(x
, y
, text
, true);
162 /// @brief Write text on the screen with the narrow font.
163 /// For more info, look at blittext_internal.
164 int blittext_n(int x
, int y
, char const *text
) {
165 return blittext_internal(x
, y
, text
, false);
168 /// @brief Write text on the screen with the narrow font.
169 /// For more info, look at blittext_internal.
170 int blittext_n(int x
, int y
, const GdColor
&color
, char const *text
) {
172 return blittext_internal(x
, y
, text
, false);
175 /// @brief Set font of the manager.
176 /// Destroys cache and starts drawing with a different font.
177 void load_theme(const std::string
&theme_file
);
179 /// @brief Destroy cache.
182 /// @brief Implement PixmapStorage.
183 virtual void release_pixmaps();
185 /// @brief Get height of font in pixels.
186 int get_font_height() const;
188 /// @brief Get line height in pixels - a bit taller than font height.
189 int get_line_height() const;
191 /// @brief Get width of wide font in pixels.
192 int get_font_width_wide() const;
194 /// @brief Get width of narrow font in pixels.
195 int get_font_width_narrow() const;
197 /// @brief This function checks if a file is suitable to be used as a GDash font theme.
198 static bool is_image_ok_for_theme(PixbufFactory
&pixbuf_factory
, const char *image
);
199 static bool is_pixbuf_ok_for_theme(const Pixbuf
&surface
);