#include <cstring>
#include <stdexcept>
-#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 && last_char < 160) // 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()
if(hl == 0)
hl = text.size();
- for (uint i = 0; i < text.size(); i++)
+ 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.
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.
l = text.size();
done = true;
}
+
+ if(l > sizeof(temp)-1)
+ l = sizeof(temp)-1;
temp[text.copy(temp, l - i, i)] = '\0';
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),
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;
- int utf8suppl = 0;
-
Vector p = pos;
- for(size_t i = 0; i < text.size(); ++i) {
- int c = (unsigned char) text[i];
- int d = 0;
- if(c > 127 && c < 160) // correct for the 32 controlchars at 128-159
- c -= 32;
- if (c > 0xC2 && text.size() == i+1) // string ends with control char
- {
- std::cerr << "String \"" << text << "\" is malformed.\n";
- return;
- }
- else
- d = (unsigned char) text[i+1];
-
- if (c == 0xC3 && d < 160) // first-byte identifier of U0080 ("C1 Control Characters and Latin-1 Supplement")
- { // iso-8859-1 equiv character is capital A with tilde above, signified as "C3 83" in utf-8
- utf8suppl = 64;
- continue;
- }
- else if (c == 0xC3 && d >= 160) // U0080 pt. 2
- {
- utf8suppl = 32;
- continue;
- }
- else if (c == 0xC4 && d < 160)
- {
- utf8suppl = 128;
- continue;
- }
- else if (c == 0xC4 && d >= 160)
- {
- utf8suppl = 96;
- continue;
- }
- else if (c == 0xC5 && d < 160) // first-byte identifier of U0100 ("Latin Extended-A")
- { // iso-8859-1 equiv character is capital A with ring above, signified as "C3 85" in utf-8
- utf8suppl = 192;
- continue;
- }
- else if (c == 0xC5 && d >= 160) // first-byte identifier of U0100 ("Latin Extended-A")
- { // iso-8859-1 equiv character is capital A with ring above, signified as "C3 85" in utf-8
- utf8suppl = 160;
- continue;
- }
- // insert more clauses here once somebody will need them
+ 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;
}
- c += utf8suppl;
- utf8suppl = 0;
-
- 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;
}
}