#include <cstring>
#include <stdexcept>
-#include "app/globals.h"
-#include "lisp/parser.h"
-#include "lisp/lisp.h"
-#include "screen.h"
-#include "font.h"
-#include "drawing_context.h"
-
-using namespace SuperTux;
+#include "lisp/parser.hpp"
+#include "lisp/lisp.hpp"
+#include "screen.hpp"
+#include "font.hpp"
+#include "drawing_context.hpp"
Font::Font(const std::string& file, FontType ntype, int nw, int nh,
int nshadowsize)
first_char = 48;
break;
}
- last_char = first_char + (chars->h / h) * 16;
- if(last_char > 127) // we have left out some control chars at 128-159
- last_char += 32;
+ char_count = (chars->h / h) * 16;
// Load shadow font.
if(shadowsize > 0) {
if(hl == 0)
hl = text.size();
+ for (uint i = 0; i < text.size(); i++)
+ if ((unsigned char) text[i] > 0xC2 && (unsigned char) text[i] < 0xC6)
+ hl--; // control characters are a WASTE.
+
return hl * w;
}
draw_chars(chars, text, pos, drawing_effect, alpha);
}
+/** decoding of a byte stream to a single unicode character.
+ * This should be correct for well formed utf-8 sequences but doesn't check for
+ * all forms of illegal sequences.
+ * (see unicode standard section 3.10 table 3-5 and 3-6 for details)
+ */
+uint32_t decode_utf8(const std::string& text, size_t& p)
+{
+ // 1 byte sequence
+ uint32_t c = (unsigned char) text[p++];
+ if(c <= 0x7F) {
+ return c;
+ }
+
+ // 2 byte sequence
+ if(p >= text.size())
+ throw std::runtime_error("Malformed utf-8 sequence");
+ uint32_t c2 = (unsigned char) text[p++];
+ if(c <= 0xDF) {
+ if(c < 0xC2)
+ throw std::runtime_error("Malformed utf-8 sequence");
+ return (c & 0x1F) << 6 | (c2 & 0x3F);
+ }
+
+ // 3 byte sequence
+ if(p >= text.size())
+ throw std::runtime_error("Malformed utf-8 sequence");
+ uint32_t c3 = (unsigned char) text[p++];
+ if(c <= 0xEF) {
+ return (c & 0x0F) << 12 | (c2 & 0x3F) << 6 | (c3 & 0x3F);
+ }
+
+ // 4 byte sequence
+ if(p >= text.size())
+ throw std::runtime_error("Malformed utf-8 sequence");
+ uint32_t c4 = (unsigned char) text[p++];
+ if(c <= 0xF4) {
+ return (c & 0x07) << 18 | (c2 & 0x3F) << 12 | (c3 & 0x3F) << 6
+ | (c4 & 0x3F);
+ }
+
+ throw std::runtime_error("Malformed utf-8 sequence");
+}
+
void
Font::draw_chars(Surface* pchars, const std::string& text, const Vector& pos,
uint32_t drawing_effect, uint8_t alpha) const
SurfaceImpl* impl = pchars->impl;
Vector p = pos;
- for(size_t i = 0; i < text.size(); ++i) {
- int c = (unsigned char) text[i];
- if(c > 127) // correct for the 32 controlchars at 128-159
- c -= 32;
+ size_t i = 0;
+ while(i < text.size()) {
+ uint32_t c = decode_utf8(text, i);
+ ssize_t font_index;
+
// a non-printable character?
- if(c == '\n') {
+ if(c == '\n') {
p.x = pos.x;
p.y += h + 2;
continue;
}
- if(c == ' ' || c < first_char || c > last_char) {
+ if(c == ' ') {
p.x += w;
continue;
}
-
- int index = c - first_char;
- int source_x = (index % 16) * w;
- int source_y = (index / 16) * h;
+ font_index = c - first_char;
+ // we don't have the control chars 0x80-0xa0 in the font
+ if(c >= 0x80) {
+ font_index -= 32;
+ if(c <= 0xa0) {
+#ifdef DEBUG
+ std::cout << "Unsupported utf-8 character '" << c << "' found\n";
+#endif
+ font_index = 0;
+ }
+ }
+
+ if(font_index < 0 || font_index >= (ssize_t) char_count) {
+#ifdef DEBUG
+ std::cout << "Unsupported utf-8 character found\n";
+#endif
+ font_index = 0;
+ }
+
+ int source_x = (font_index % 16) * w;
+ int source_y = (font_index / 16) * h;
impl->draw_part(source_x, source_y, p.x, p.y, w, h, alpha, drawing_effect);
p.x += w;
}