- some win32 fixes
[supertux.git] / src / video / font.cpp
1 //  $Id: font.cpp 2298 2005-03-30 12:01:02Z matzebraun $
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
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.
10 //
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.
15 // 
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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include <cstdlib>
23 #include <cstring>
24 #include <stdexcept>
25
26 #include "lisp/parser.hpp"
27 #include "lisp/lisp.hpp"
28 #include "screen.hpp"
29 #include "font.hpp"
30 #include "drawing_context.hpp"
31
32 Font::Font(const std::string& file, const std::string& shadowfile,
33            int w, int h, int shadowsize)
34     : chars(0), shadow_chars(0), w(w), h(h), shadowsize(shadowsize)
35 {
36   chars = new Surface(file);
37   shadow_chars = new Surface(shadowfile);
38  
39   first_char = 32;
40   char_count = ((int) chars->get_height() / h) * 16;
41 }
42
43 Font::~Font()
44 {
45   delete chars;
46   delete shadow_chars;
47 }
48
49 float
50 Font::get_text_width(const std::string& text) const
51 {
52   /** Let's calculate the size of the biggest paragraph */
53   std::string::size_type l, hl, ol;
54   hl = 0; l = 0;
55   while(true)
56     {
57     ol = l;
58     l = text.find("\n", l+1);
59     if(l == std::string::npos)
60       break;
61     if(hl < l-ol)
62       hl = l-ol;
63     }
64   if(hl == 0)
65     hl = text.size();
66
67   for (unsigned int i = 0; i < text.size(); i++)
68     if ((unsigned char) text[i] > 0xC2 && (unsigned char) text[i] < 0xC6)
69       hl--;  // control characters are a WASTE.
70
71   return hl * w;
72 }
73
74 float
75 Font::get_text_height(const std::string& text) const
76 {
77   /** Let's calculate height of the text */
78   std::string::size_type l, hh;
79   hh = h; l = 0;
80   while(true)
81     {
82     l = text.find("\n", l+1);
83     if(l == std::string::npos)
84       break;
85     hh += h + 2;
86     }
87
88   return hh;
89 }
90
91 float
92 Font::get_height() const
93 {
94   return h;
95 }
96
97 void
98 Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment,
99            DrawingEffect drawing_effect, float alpha) const
100 {
101   /* Cut lines changes into seperate strings, needed to support center/right text
102      alignments with break lines.
103      Feel free to replace this hack with a more elegant solution
104   */
105   char temp[1024];
106   std::string::size_type l, i, y;
107   bool done = false;
108   i = y = 0;
109
110   while(!done) {
111     l = text.find("\n", i);
112     if(l == std::string::npos) {
113       l = text.size();
114       done = true;
115     }
116     
117     temp[text.copy(temp, l - i, i)] = '\0';
118     
119     // calculate X positions based on the alignment type
120     Vector pos = Vector(pos_);
121     if(alignment == CENTER_ALLIGN)
122       pos.x -= get_text_width(temp) / 2;
123     else if(alignment == RIGHT_ALLIGN)
124       pos.x -= get_text_width(temp);
125
126     draw_text(temp, pos + Vector(0,y), drawing_effect, alpha);
127
128     i = l+1;
129     y += h + 2;
130   }
131 }
132
133 void
134 Font::draw_text(const std::string& text, const Vector& pos, 
135                 DrawingEffect drawing_effect, float alpha) const
136 {
137   if(shadowsize > 0)
138     draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize),
139                drawing_effect, alpha);
140
141   draw_chars(chars, text, pos, drawing_effect, alpha);
142 }
143
144 /** decoding of a byte stream to a single unicode character.
145  * This should be correct for well formed utf-8 sequences but doesn't check for
146  * all forms of illegal sequences.
147  * (see unicode standard section 3.10 table 3-5 and 3-6 for details)
148  */
149 uint32_t decode_utf8(const std::string& text, size_t& p)
150 {
151   // 1 byte sequence
152   uint32_t c = (unsigned char) text[p++];
153   if(c <= 0x7F) {
154     return c;
155   }
156   
157   // 2 byte sequence
158   if(p >= text.size())
159     throw std::runtime_error("Malformed utf-8 sequence");
160   uint32_t c2 = (unsigned char) text[p++];
161   if(c <= 0xDF) {
162     if(c < 0xC2)
163       throw std::runtime_error("Malformed utf-8 sequence");
164     return (c & 0x1F) << 6 | (c2 & 0x3F);
165   }
166   
167   // 3 byte sequence
168   if(p >= text.size())
169     throw std::runtime_error("Malformed utf-8 sequence");
170   uint32_t c3 = (unsigned char) text[p++];
171   if(c <= 0xEF) {
172     return (c & 0x0F) << 12 | (c2 & 0x3F) << 6 | (c3 & 0x3F);
173   }
174   
175   // 4 byte sequence
176   if(p >= text.size())
177     throw std::runtime_error("Malformed utf-8 sequence");
178   uint32_t c4 = (unsigned char) text[p++];
179   if(c <= 0xF4) {
180     return (c & 0x07) << 18 | (c2 & 0x3F) << 12 | (c3 & 0x3F) << 6 
181       | (c4 & 0x3F);
182   }
183
184   throw std::runtime_error("Malformed utf-8 sequence");
185 }
186
187 void
188 Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
189                  DrawingEffect drawing_effect, float alpha) const
190 {
191   Vector p = pos;
192   size_t i = 0;
193   while(i < text.size()) {
194     uint32_t c = decode_utf8(text, i);
195     ssize_t font_index;
196
197     // a non-printable character?
198     if(c == '\n') {                                      
199       p.x = pos.x;
200       p.y += h + 2;
201       continue;
202     }
203     if(c == ' ') {
204       p.x += w;
205       continue;
206     }
207
208     font_index = c - first_char;
209     // we don't have the control chars 0x80-0xa0 in the font
210     if(c >= 0x80) {
211       font_index -= 32;
212       if(c <= 0xa0) {
213 #ifdef DEBUG
214         std::cout << "Unsupported utf-8 character '" << c << "' found\n";
215 #endif
216         font_index = 0;
217       }
218     }
219         
220     if(font_index < 0 || font_index >= (ssize_t) char_count) {
221 #ifdef DEBUG
222       std::cout << "Unsupported utf-8 character found\n";
223 #endif
224       font_index = 0;
225     }                   
226
227     int source_x = (font_index % 16) * w;
228     int source_y = (font_index / 16) * h;
229     pchars->draw_part(source_x, source_y, p.x, p.y, w, h, alpha,
230                       drawing_effect);
231     p.x += w;
232   }
233 }