1 // $Id: font.cpp 2298 2005-03-30 12:01:02Z matzebraun $
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
26 #include "lisp/parser.h"
27 #include "lisp/lisp.h"
30 #include "drawing_context.h"
32 Font::Font(const std::string& file, FontType ntype, int nw, int nh,
34 : chars(0), shadow_chars(0), type(ntype), w(nw), h(nh),
35 shadowsize(nshadowsize)
37 chars = new Surface(file, true);
47 last_char = first_char + (chars->h / h) * 16;
48 if(last_char > 127) // we have left out some control chars at 128-159
53 SDL_Surface* conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface());
54 int pixels = conv->w * conv->h;
55 SDL_LockSurface(conv);
56 for(int i = 0; i < pixels; ++i) {
57 Uint32 *p = (Uint32 *)conv->pixels + i;
58 *p = *p & conv->format->Amask;
60 SDL_UnlockSurface(conv);
61 SDL_SetAlpha(conv, SDL_SRCALPHA, 128);
62 shadow_chars = new Surface(conv, true);
63 SDL_FreeSurface(conv);
74 Font::get_text_width(const std::string& text) const
76 /** Let's calculate the size of the biggest paragraph */
77 std::string::size_type l, hl, ol;
82 l = text.find("\n", l+1);
83 if(l == std::string::npos)
95 Font::get_text_height(const std::string& text) const
97 /** Let's calculate height of the text */
98 std::string::size_type l, hh;
102 l = text.find("\n", l+1);
103 if(l == std::string::npos)
112 Font::get_height() const
118 Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment,
119 uint32_t drawing_effect, uint8_t alpha) const
121 /* Cut lines changes into seperate strings, needed to support center/right text
122 alignments with break lines.
123 Feel free to replace this hack with a more elegant solution
126 std::string::size_type l, i, y;
131 l = text.find("\n", i);
132 if(l == std::string::npos) {
137 temp[text.copy(temp, l - i, i)] = '\0';
139 // calculate X positions based on the alignment type
140 Vector pos = Vector(pos_);
141 if(alignment == CENTER_ALLIGN)
142 pos.x -= get_text_width(temp) / 2;
143 else if(alignment == RIGHT_ALLIGN)
144 pos.x -= get_text_width(temp);
146 draw_text(temp, pos + Vector(0,y), drawing_effect, alpha);
154 Font::draw_text(const std::string& text, const Vector& pos,
155 uint32_t drawing_effect, uint8_t alpha) const
158 draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize),
159 drawing_effect, alpha);
161 draw_chars(chars, text, pos, drawing_effect, alpha);
165 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
166 uint32_t drawing_effect, uint8_t alpha) const
168 SurfaceImpl* impl = pchars->impl;
171 for(size_t i = 0; i < text.size(); ++i) {
172 int c = (unsigned char) text[i];
173 if(c > 127) // correct for the 32 controlchars at 128-159
175 // a non-printable character?
181 if(c == ' ' || c < first_char || c > last_char) {
186 int index = c - first_char;
187 int source_x = (index % 16) * w;
188 int source_y = (index / 16) * h;
190 impl->draw_part(source_x, source_y, p.x, p.y, w, h, alpha, drawing_effect);