4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "app/globals.h"
28 #include "lisp/parser.h"
29 #include "lisp/lisp.h"
32 #include "drawing_context.h"
34 using namespace SuperTux;
36 Font::Font(const std::string& file, FontType ntype, int nw, int nh,
38 : chars(0), shadow_chars(0), type(ntype), w(nw), h(nh),
39 shadowsize(nshadowsize)
41 chars = new Surface(file, true);
51 last_char = first_char + (chars->h / h) * 16;
52 if(last_char > 127) // we have left out some control chars at 128-159
57 SDL_Surface* conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface());
58 int pixels = conv->w * conv->h;
59 SDL_LockSurface(conv);
60 for(int i = 0; i < pixels; ++i) {
61 Uint32 *p = (Uint32 *)conv->pixels + i;
62 *p = *p & conv->format->Amask;
64 SDL_UnlockSurface(conv);
65 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
66 shadow_chars = new Surface(conv, true);
67 SDL_FreeSurface(conv);
78 Font::get_text_width(const std::string& text) const
80 /** Let's calculate the size of the biggest paragraph */
81 std::string::size_type l, hl, ol;
86 l = text.find("\n", l+1);
87 if(l == std::string::npos)
99 Font::get_text_height(const std::string& text) const
101 /** Let's calculate height of the text */
102 std::string::size_type l, hh;
106 l = text.find("\n", l+1);
107 if(l == std::string::npos)
116 Font::get_height() const
122 Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment,
123 uint32_t drawing_effect, uint8_t alpha) const
125 /* Cut lines changes into seperate strings, needed to support center/right text
126 alignments with break lines.
127 Feel free to replace this hack with a more elegant solution
130 std::string::size_type l, i, y;
136 l = text.find("\n", i);
137 if(l == std::string::npos)
143 temp[text.copy(temp, l - i, i)] = '\0';
145 // calculate X positions based on the alignment type
146 Vector pos = Vector(pos_);
147 if(alignment == CENTER_ALLIGN)
148 pos.x -= get_text_width(temp) / 2;
149 else if(alignment == RIGHT_ALLIGN)
150 pos.x -= get_text_width(temp);
152 draw_text(temp, pos + Vector(0,y), drawing_effect, alpha);
160 Font::draw_text(const std::string& text, const Vector& pos,
161 uint32_t drawing_effect, uint8_t alpha) const
164 draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize),
165 drawing_effect, alpha);
167 draw_chars(chars, text, pos, drawing_effect, alpha);
171 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
172 uint32_t drawing_effect, uint8_t alpha) const
174 SurfaceImpl* impl = pchars->impl;
177 for(size_t i = 0; i < text.size(); ++i)
179 int c = (unsigned char) text[i];
180 if(c > 127) // correct for the 32 controlchars at 128-159
182 // a non-printable character?
188 if(c == ' ' || c < first_char || c > last_char) {
193 int index = c - first_char;
194 int source_x = (index % 16) * w;
195 int source_y = (index / 16) * h;
197 impl->draw_part(source_x, source_y, p.x, p.y, w, h, alpha, drawing_effect);