forked from od-contrib/commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_ttf_multifont.cpp
161 lines (144 loc) · 4.59 KB
/
sdl_ttf_multifont.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "sdl_ttf_multifont.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <vector>
namespace {
/* This function is incorrect as it only supports BMP but that's all SDL_ttf
* supports anyway. */
std::vector<std::uint16_t> DecodeUtf8(const std::string &utf8) {
std::vector<std::uint16_t> result;
for (std::size_t i = 0; i < utf8.size(); ++i) {
std::uint16_t ch = static_cast<unsigned char>(utf8[i]);
if (ch >= 0xF0) {
if (i + 4 > utf8.size()) {
ch = 0xFFFD;
++i;
continue;
}
ch = static_cast<std::uint16_t>(utf8[i] & 0x07) << 18;
ch |= static_cast<std::uint16_t>(utf8[++i] & 0x3F) << 12;
ch |= static_cast<std::uint16_t>(utf8[++i] & 0x3F) << 6;
ch |= static_cast<std::uint16_t>(utf8[++i] & 0x3F);
} else if (ch >= 0xE0) {
if (i + 3 > utf8.size()) {
ch = 0xFFFD;
++i;
continue;
}
ch = static_cast<std::uint16_t>(utf8[i] & 0x0F) << 12;
ch |= static_cast<std::uint16_t>(utf8[++i] & 0x3F) << 6;
ch |= static_cast<std::uint16_t>(utf8[++i] & 0x3F);
} else if (ch >= 0xC0) {
if (i + 2 > utf8.size()) {
ch = 0xFFFD;
++i;
continue;
}
ch = static_cast<std::uint16_t>(utf8[i] & 0x1F) << 6;
ch |= static_cast<std::uint16_t>(utf8[++i] & 0x3F);
}
result.push_back(ch);
}
result.push_back(0);
return result;
}
struct Slice {
std::uint16_t *text;
std::size_t text_size;
TTF_Font *font;
};
std::vector<Slice> SplitIntoSlices(
std::vector<std::uint16_t> &code_points,
const Fonts &fonts) {
TTF_Font *prev_font = nullptr;
std::vector<Slice> slices;
for (auto &cp : code_points) {
if (cp == 0) break;
TTF_Font *cur_font = fonts.GetFontForCodePoint(cp);
if (cur_font == prev_font) {
++slices.back().text_size;
} else {
slices.push_back(Slice{&cp, 1, cur_font});
prev_font = cur_font;
}
}
return slices;
}
} // namespace
Fonts::Fonts(std::vector<TTF_Font *> fonts)
: fonts_(std::move(fonts)),
glyph_index_cache_({0}) {}
TTF_Font *Fonts::GetFontForCodePoint(std::uint16_t cp) const {
auto &result = glyph_index_cache_[cp];
if (result == 0) {
std::size_t i = 1;
result = fonts_[0];
for (TTF_Font *font : fonts_) {
if (TTF_GlyphIsProvided(font, cp)) {
result = font;
break;
}
}
}
return result;
}
SDL_Surface *TTFMultiFont_RenderUTF8_Shaded(
const Fonts &fonts, const std::string &text, SDL_Color fg,
SDL_Color bg) {
if (fonts.IsSingle()) {
return TTF_RenderUTF8_Shaded(fonts.GetFirstFont(), text.c_str(), fg, bg);
}
auto code_points = DecodeUtf8(text);
const std::vector<Slice> slices = SplitIntoSlices(code_points, fonts);
if (slices.empty()) return nullptr;
if (slices.size() == 1)
return TTF_RenderUNICODE_Shaded(slices[0].font, slices[0].text, fg, bg);
std::vector<SDL_Surface *> surfaces;
surfaces.reserve(slices.size());
// SDL_ttf API requires a 0-terminated buffer.
// To avoid excessive copying, we iterate in-reverse and modify the buffer
// in-place.
for (std::size_t i = slices.size(); i > 0; --i) {
auto &slice = slices[i - 1];
slice.text[slice.text_size] = 0;
SDL_Surface *surface =
TTF_RenderUNICODE_Shaded(slice.font, slice.text, fg, bg);
if (surface == nullptr) {
std::cerr << "TTFMultiFont_RenderUTF8_Shaded error: " << SDL_GetError()
<< std::endl;
SDL_ClearError();
continue;
}
surfaces.push_back(surface);
}
int width = 0;
int height = 0;
for (auto *surface : surfaces) {
width += surface->w;
height = std::max(surface->h, height);
}
SDL_Surface *result = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
if (result == nullptr) return nullptr;
#ifdef USE_SDL2
SDL_SetPaletteColors(result->format->palette,
surfaces[0]->format->palette->colors, 0,
surfaces[0]->format->palette->ncolors);
#else
SDL_SetPalette(result, SDL_LOGPAL, surfaces[0]->format->palette->colors, 0,
surfaces[0]->format->palette->ncolors);
#endif
decltype(SDL_Rect().x) x = 0;
for (std::size_t i = surfaces.size(); i > 0; --i) {
auto *surface = surfaces[i - 1];
SDL_Rect dst_rect = {
x, static_cast<decltype(SDL_Rect().y)>(height - surface->h),
static_cast<decltype(SDL_Rect().w)>(surface->w),
static_cast<decltype(SDL_Rect().h)>(surface->h)};
SDL_BlitSurface(surface, nullptr, result, &dst_rect);
x += static_cast<decltype(SDL_Rect().w)>(surface->w);
}
for (auto *surface : surfaces) SDL_FreeSurface(surface);
return result;
}