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
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "ultima/nuvie/misc/u6_misc.h"
24 #include "ultima/nuvie/files/utils.h"
25 #include "ultima/nuvie/gui/gui_font.h"
26 #include "ultima/nuvie/gui/gui_load_image.h"
27 #include "common/textconsole.h"
28
29 namespace Ultima {
30 namespace Nuvie {
31
32 /* use default 8x8 font */
GUI_Font(uint8 fontType)33 GUI_Font::GUI_Font(uint8 fontType) {
34 Graphics::ManagedSurface *temp;
35
36 _wData = NULL;
37
38 if (fontType == GUI_FONT_6X8)
39 temp = GUI_Font6x8();
40 else if (fontType == GUI_FONT_GUMP) {
41 temp = GUI_FontGump();
42 _wData = GUI_FontGumpWData();
43 } else
44 temp = GUI_DefaultFont();
45
46
47 _fontStore = SDL_ConvertSurface(temp, temp->format, SDL_SWSURFACE);
48 _charH = _fontStore->h / 16;
49 _charW = _fontStore->w / 16;
50 _disposeFont = DisposeAfterUse::YES;
51 setTransparency(true);
52 }
53
54 /* open named BMP file */
GUI_Font(char * name)55 GUI_Font::GUI_Font(char *name) {
56 _fontStore = SDL_LoadBMP(name);
57 if (_fontStore != NULL) {
58 _charH = _fontStore->h / 16;
59 _charW = _fontStore->w / 16;
60 _disposeFont = DisposeAfterUse::YES;
61 } else {
62 ::error("Could not load font");
63 }
64
65 setTransparency(true);
66 _wData = NULL;
67 }
68
69 /* use given YxY surface */
GUI_Font(Graphics::ManagedSurface * bitmap)70 GUI_Font::GUI_Font(Graphics::ManagedSurface *bitmap) {
71 if (bitmap == NULL)
72 _fontStore = GUI_DefaultFont();
73 else
74 _fontStore = bitmap;
75 _charH = _fontStore->h / 16;
76 _charW = _fontStore->w / 16;
77 _disposeFont = DisposeAfterUse::NO;
78 setTransparency(true);
79 _wData = NULL;
80 }
81
82 /* copy constructor */
GUI_Font(GUI_Font & font)83 GUI_Font::GUI_Font(GUI_Font &font) {
84 Graphics::ManagedSurface *temp = font._fontStore;
85 _fontStore = SDL_ConvertSurface(temp, temp->format, SDL_SWSURFACE);
86 _charH = _fontStore->h / 16;
87 _charW = _fontStore->w / 16;
88 _disposeFont = DisposeAfterUse::YES;
89 setTransparency(true);
90 _wData = NULL;
91 }
92
~GUI_Font()93 GUI_Font::~GUI_Font() {
94 if (_disposeFont == DisposeAfterUse::YES)
95 delete _fontStore;
96 }
97
98 /* determine drawing style */
setTransparency(bool on)99 void GUI_Font::setTransparency(bool on) {
100 _transparent = on;
101
102 if (_transparent)
103 _fontStore->setTransparentColor(0);
104 else
105 _fontStore->clearTransparentColor();
106 }
107
108 /* determine foreground and background color values RGB*/
setColoring(uint8 fr,uint8 fg,uint8 fb,uint8 br,uint8 bg,uint8 bb)109 void GUI_Font::setColoring(uint8 fr, uint8 fg, uint8 fb, uint8 br, uint8 bg, uint8 bb) {
110 const SDL_Color colors[2] = { MAKE_COLOR(br, bg, bb), MAKE_COLOR(fr, fg, fb) };
111 SDL_SetColors(_fontStore, colors, 0, 2);
112 }
113
setColoring(uint8 fr,uint8 fg,uint8 fb,uint8 fr1,uint8 fg1,uint8 fb1,uint8 br,uint8 bg,uint8 bb)114 void GUI_Font::setColoring(uint8 fr, uint8 fg, uint8 fb, uint8 fr1, uint8 fg1, uint8 fb1, uint8 br, uint8 bg, uint8 bb) {
115 const SDL_Color colors[3] = {
116 MAKE_COLOR(br, bg, bb), MAKE_COLOR(fr, fg, fb), MAKE_COLOR(fr1, fg1, fb1) };
117 SDL_SetColors(_fontStore, colors, 0, 3);
118 }
119
120 /* put the text onto the given surface using the preset mode and colors */
textOut(Graphics::ManagedSurface * context,int x,int y,const char * text,int line_wrap)121 void GUI_Font::textOut(Graphics::ManagedSurface *context, int x, int y, const char *text, int line_wrap) {
122 int i;
123 int j;
124 uint8 ch;
125 Common::Rect src(_charW, _charH - 1);
126 Common::Rect dst(_charW, _charH - 1);
127
128 i = 0;
129 j = 0;
130 while ((ch = text[i])) { // single "=" is correct!
131 if (line_wrap && j == line_wrap) {
132 j = 0;
133 y += _charH;
134 }
135
136 src.moveTo((ch % 16) * _charW, (ch / 16) * _charH);
137 if (_wData) {
138 dst.left = x;
139 dst.right = x + _wData[ch];
140 x += dst.width();
141
142 } else {
143 dst.moveTo(x + (j * _charW), dst.top);
144 }
145
146 dst.moveTo(dst.left, y);
147 SDL_BlitSurface(_fontStore, &src, context, &dst);
148 i++;
149 j++;
150 }
151 }
152
textExtent(const char * text,int * w,int * h,int line_wrap)153 void GUI_Font:: textExtent(const char *text, int *w, int *h, int line_wrap) {
154 int len = strlen(text);
155 if (_wData) { //variable width font.
156 //FIXME we're not handling line_wrap properly for variable width fonts!
157 *w = 0;
158 for (int i = 0; i < len; i++) {
159 *w += _wData[(byte)text[i]];
160 }
161 } else {
162 if (line_wrap && len > line_wrap)
163 *w = line_wrap * _charW;
164 else
165 *w = len * _charW;
166 }
167
168 if (line_wrap && len > line_wrap) {
169 *h = (int)ceil((float)len / (float)line_wrap);
170 *h *= _charH - 1;
171 } else
172 *h = _charH - 1;
173 return;
174 }
175
getCenter(const char * text,uint16 width)176 uint16 GUI_Font::getCenter(const char *text, uint16 width) {
177 int w, h;
178 textExtent(text, &w, &h);
179 if (w < width)
180 return ((width - w) / 2);
181 else
182 return 0;
183 }
184
185 } // End of namespace Nuvie
186 } // End of namespace Ultima
187