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