X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fvideo%2Ffont.cpp;h=7394441e721e0daa5bc208ff099a7021b287cab3;hb=67690e081c28b818e94796be284206326bc8a6b9;hp=bdcd541028df94c0e10a7994de08db1ca4f42178;hpb=60908c905544776c376421b8d3e12eeb936c068f;p=supertux.git diff --git a/src/video/font.cpp b/src/video/font.cpp index bdcd54102..7394441e7 100644 --- a/src/video/font.cpp +++ b/src/video/font.cpp @@ -23,45 +23,22 @@ #include #include -#include "lisp/parser.h" -#include "lisp/lisp.h" -#include "screen.h" -#include "font.h" -#include "drawing_context.h" - -Font::Font(const std::string& file, FontType ntype, int nw, int nh, - int nshadowsize) - : chars(0), shadow_chars(0), type(ntype), w(nw), h(nh), - shadowsize(nshadowsize) +#include "lisp/parser.hpp" +#include "lisp/lisp.hpp" +#include "screen.hpp" +#include "font.hpp" +#include "drawing_context.hpp" +#include "msg.hpp" + +Font::Font(const std::string& file, const std::string& shadowfile, + int w, int h, int shadowsize) + : chars(0), shadow_chars(0), w(w), h(h), shadowsize(shadowsize) { - chars = new Surface(file, true); + chars = new Surface(file); + shadow_chars = new Surface(shadowfile); - switch(type) { - case TEXT: - first_char = 32; - break; - case NUM: - 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; - - // Load shadow font. - if(shadowsize > 0) { - SDL_Surface* conv = SDL_DisplayFormatAlpha(chars->impl->get_sdl_surface()); - int pixels = conv->w * conv->h; - SDL_LockSurface(conv); - for(int i = 0; i < pixels; ++i) { - Uint32 *p = (Uint32 *)conv->pixels + i; - *p = *p & conv->format->Amask; - } - SDL_UnlockSurface(conv); - SDL_SetAlpha(conv, SDL_SRCALPHA, 128); - shadow_chars = new Surface(conv, true); - SDL_FreeSurface(conv); - } + first_char = 32; + char_count = ((int) chars->get_height() / h) * 16; } Font::~Font() @@ -88,6 +65,10 @@ Font::get_text_width(const std::string& text) const if(hl == 0) hl = text.size(); + for (unsigned int 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; } @@ -116,7 +97,7 @@ Font::get_height() const void Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment, - uint32_t drawing_effect, uint8_t alpha) const + DrawingEffect drawing_effect, float alpha) const { /* Cut lines changes into seperate strings, needed to support center/right text alignments with break lines. @@ -152,7 +133,7 @@ Font::draw(const std::string& text, const Vector& pos_, FontAlignment alignment, void Font::draw_text(const std::string& text, const Vector& pos, - uint32_t drawing_effect, uint8_t alpha) const + DrawingEffect drawing_effect, float alpha) const { if(shadowsize > 0) draw_chars(shadow_chars, text, pos + Vector(shadowsize, shadowsize), @@ -161,33 +142,89 @@ Font::draw_text(const std::string& text, const Vector& pos, 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 + DrawingEffect drawing_effect, float 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; - impl->draw_part(source_x, source_y, p.x, p.y, w, h, alpha, drawing_effect); + 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) { + msg_debug << "Unsupported utf-8 character '" << c << "' found" << std::endl; + font_index = 0; + } + } + + if(font_index < 0 || font_index >= (ssize_t) char_count) { + msg_debug << "Unsupported utf-8 character found" << std::endl; + font_index = 0; + } + + int source_x = (font_index % 16) * w; + int source_y = (font_index / 16) * h; + pchars->draw_part(source_x, source_y, p.x, p.y, w, h, alpha, + drawing_effect); p.x += w; } }