Root/src/font.cpp

1#include "font.h"
2
3#include "debug.h"
4#include "surface.h"
5#include "utilities.h"
6
7#include <SDL.h>
8#include <SDL_ttf.h>
9#include <vector>
10
11/* TODO: Let the theme choose the font and font size */
12#define TTF_FONT "/usr/share/fonts/truetype/dejavu/DejaVuSansCondensed.ttf"
13#define TTF_FONT_SIZE 12
14
15using namespace std;
16
17Font *Font::defaultFont()
18{
19    if (!TTF_WasInit() && TTF_Init() < 0) {
20        ERROR("Unable to init SDL_ttf library\n");
21        return nullptr;
22    }
23
24    TTF_Font *font = TTF_OpenFont(TTF_FONT, TTF_FONT_SIZE);
25    if (!font) {
26        ERROR("Unable to open font\n");
27        return nullptr;
28    }
29
30    return new Font(font);
31}
32
33Font::Font(TTF_Font *font)
34    : font(font)
35{
36    fontheight = TTF_FontHeight(font);
37}
38
39Font::~Font()
40{
41    TTF_CloseFont(font);
42    TTF_Quit();
43}
44
45int Font::getTextWidth(const char *text)
46{
47    int w, h;
48    TTF_SizeUTF8(font, text, &w, &h);
49    return w;
50}
51
52void Font::write(Surface *surface, const string &text,
53            int x, int y, HAlign halign, VAlign valign)
54{
55    if (text.find("\n", 0) == string::npos) {
56        writeLine(surface, text.c_str(), x, y, halign, valign);
57        return;
58    }
59
60    vector<string> v;
61    split(v, text, "\n");
62
63    for (vector<string>::const_iterator it = v.begin(); it != v.end(); it++) {
64        writeLine(surface, it->c_str(), x, y, halign, valign);
65        y += fontheight;
66    }
67}
68
69void Font::writeLine(Surface *surface, const char *text,
70                int x, int y, HAlign halign, VAlign valign)
71{
72    switch (halign) {
73    case HAlignLeft:
74        break;
75    case HAlignCenter:
76        x -= getTextWidth(text) / 2;
77        break;
78    case HAlignRight:
79        x -= getTextWidth(text);
80        break;
81    }
82
83    switch (valign) {
84    case VAlignTop:
85        break;
86    case VAlignMiddle:
87        y -= fontheight / 2;
88        break;
89    case VAlignBottom:
90        y -= fontheight;
91        break;
92    }
93
94    SDL_Color color = { 0, 0, 0, 0 };
95    SDL_Surface *s = TTF_RenderUTF8_Blended(font, text, color);
96
97    SDL_Rect rect = { (Sint16) x, (Sint16) (y - 1), 0, 0 };
98    SDL_BlitSurface(s, NULL, surface->raw, &rect);
99
100    /* Note: rect.x / rect.y are reset everytime because SDL_BlitSurface
101     * will modify them if negative */
102    rect.x = x;
103    rect.y = y + 1;
104    SDL_BlitSurface(s, NULL, surface->raw, &rect);
105
106    rect.x = x - 1;
107    rect.y = y;
108    SDL_BlitSurface(s, NULL, surface->raw, &rect);
109
110    rect.x = x + 1;
111    rect.y = y;
112    SDL_BlitSurface(s, NULL, surface->raw, &rect);
113    SDL_FreeSurface(s);
114
115    rect.x = x;
116    rect.y = y;
117    color.r = 0xff;
118    color.g = 0xff;
119    color.b = 0xff;
120
121    s = TTF_RenderUTF8_Blended(font, text, color);
122    SDL_BlitSurface(s, NULL, surface->raw, &rect);
123    SDL_FreeSurface(s);
124}
125

Archive Download this file



interactive