| 1 |
// { dg-do compile } |
| 2 |
// { dg-require-effective-target c++17 } |
| 3 |
// { dg-options "-O2 -Wall" } |
| 4 |
// { dg-skip-if "requires hosted libstdc++ for cwchar" { ! hostedlib } } |
| 5 |
|
| 6 |
#include <stdint.h> |
| 7 |
#include <optional> |
| 8 |
#include <string_view> |
| 9 |
|
| 10 |
using utf8 = char; |
| 11 |
|
| 12 |
enum |
| 13 |
{ |
| 14 |
FONT_SIZE_TINY = 2, |
| 15 |
FONT_SIZE_SMALL = 0, |
| 16 |
FONT_SIZE_MEDIUM = 1, |
| 17 |
FONT_SIZE_COUNT = 3 |
| 18 |
}; |
| 19 |
|
| 20 |
constexpr const uint16_t FONT_SPRITE_GLYPH_COUNT = 224; |
| 21 |
|
| 22 |
enum class FontSpriteBase : int16_t |
| 23 |
{ |
| 24 |
MEDIUM_EXTRA_DARK = -2, |
| 25 |
MEDIUM_DARK = -1, |
| 26 |
|
| 27 |
TINY = FONT_SIZE_TINY * FONT_SPRITE_GLYPH_COUNT, |
| 28 |
SMALL = FONT_SIZE_SMALL * FONT_SPRITE_GLYPH_COUNT, |
| 29 |
MEDIUM = FONT_SIZE_MEDIUM * FONT_SPRITE_GLYPH_COUNT, |
| 30 |
}; |
| 31 |
|
| 32 |
struct TTFSurface; |
| 33 |
|
| 34 |
class CodepointView |
| 35 |
{ |
| 36 |
private: |
| 37 |
std::string_view _str; |
| 38 |
|
| 39 |
public: |
| 40 |
class iterator |
| 41 |
{ |
| 42 |
private: |
| 43 |
std::string_view _str; |
| 44 |
size_t _index; |
| 45 |
|
| 46 |
public: |
| 47 |
iterator(std::string_view str, size_t index) |
| 48 |
: _str(str) |
| 49 |
, _index(index) |
| 50 |
{ |
| 51 |
} |
| 52 |
|
| 53 |
bool operator==(const iterator& rhs) const |
| 54 |
{ |
| 55 |
return _index == rhs._index; |
| 56 |
} |
| 57 |
bool operator!=(const iterator& rhs) const |
| 58 |
{ |
| 59 |
return _index != rhs._index; |
| 60 |
} |
| 61 |
char32_t operator*() const |
| 62 |
{ |
| 63 |
return GetNextCodepoint(&_str[_index], nullptr); |
| 64 |
} |
| 65 |
iterator& operator++() |
| 66 |
{ |
| 67 |
return *this; |
| 68 |
} |
| 69 |
iterator operator++(int) |
| 70 |
{ |
| 71 |
auto result = *this; |
| 72 |
if (_index < _str.size()) |
| 73 |
{ |
| 74 |
const utf8* nextch; |
| 75 |
GetNextCodepoint(&_str[_index], &nextch); |
| 76 |
_index = nextch - _str.data(); |
| 77 |
} |
| 78 |
return result; |
| 79 |
} |
| 80 |
|
| 81 |
size_t GetIndex() const |
| 82 |
{ |
| 83 |
return _index; |
| 84 |
} |
| 85 |
|
| 86 |
static char32_t GetNextCodepoint(const char* ch, const char** next); |
| 87 |
}; |
| 88 |
|
| 89 |
CodepointView(std::string_view str) |
| 90 |
: _str(str) |
| 91 |
{ |
| 92 |
} |
| 93 |
|
| 94 |
iterator begin() const |
| 95 |
{ |
| 96 |
return iterator(_str, 0); |
| 97 |
} |
| 98 |
|
| 99 |
iterator end() const |
| 100 |
{ |
| 101 |
return iterator(_str, _str.size()); |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
struct InternalTTFFont; |
| 106 |
using TTF_Font = InternalTTFFont; |
| 107 |
struct TTFFontDescriptor |
| 108 |
{ |
| 109 |
const utf8* filename; |
| 110 |
const utf8* font_name; |
| 111 |
int32_t ptSize; |
| 112 |
int32_t offset_x; |
| 113 |
int32_t offset_y; |
| 114 |
int32_t line_height; |
| 115 |
int32_t hinting_threshold; |
| 116 |
TTF_Font* font; |
| 117 |
}; |
| 118 |
using codepoint_t = uint32_t; |
| 119 |
|
| 120 |
#define abstract = 0 |
| 121 |
|
| 122 |
struct ITTF |
| 123 |
{ |
| 124 |
virtual ~ITTF() = default; |
| 125 |
virtual TTFFontDescriptor* ttf_get_font_from_sprite_base(FontSpriteBase spriteBase) abstract; |
| 126 |
virtual TTFSurface* ttf_surface_cache_get_or_add(TTF_Font* font, std::string_view text) abstract; |
| 127 |
}; |
| 128 |
|
| 129 |
namespace OpenRCT2 { |
| 130 |
struct IContext |
| 131 |
{ |
| 132 |
virtual ~IContext() = default; |
| 133 |
|
| 134 |
virtual ITTF* GetTTF() abstract; |
| 135 |
}; |
| 136 |
} |
| 137 |
|
| 138 |
static void ttf_draw_string_raw_ttf(OpenRCT2::IContext* context, std::string_view text) |
| 139 |
{ |
| 140 |
TTFFontDescriptor* fontDesc = context->GetTTF()->ttf_get_font_from_sprite_base(FontSpriteBase::MEDIUM_EXTRA_DARK); |
| 141 |
if (fontDesc->font == nullptr) |
| 142 |
{ |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
TTFSurface* surface = context->GetTTF()->ttf_surface_cache_get_or_add(fontDesc->font, text); |
| 147 |
if (surface == nullptr) |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
namespace UnicodeChar |
| 152 |
{ |
| 153 |
// Punctuation |
| 154 |
constexpr char32_t leftguillemet = 0xAB; |
| 155 |
constexpr char32_t rightguillemet = 0xBB; |
| 156 |
constexpr char32_t german_quote_open = 0x201E; |
| 157 |
constexpr char32_t quote_open = 0x201C; |
| 158 |
constexpr char32_t quote_close = 0x201D; |
| 159 |
|
| 160 |
// Dingbats |
| 161 |
constexpr char32_t up = 0x25B2; |
| 162 |
constexpr char32_t small_up = 0x25B4; |
| 163 |
constexpr char32_t right = 0x25B6; |
| 164 |
constexpr char32_t down = 0x25BC; |
| 165 |
constexpr char32_t small_down = 0x25BE; |
| 166 |
constexpr char32_t left = 0x25C0; |
| 167 |
constexpr char32_t tick = 0x2713; |
| 168 |
constexpr char32_t plus = 0x2795; |
| 169 |
constexpr char32_t minus = 0x2796; |
| 170 |
|
| 171 |
// Emoji |
| 172 |
constexpr char32_t cross = 0x274C; |
| 173 |
constexpr char32_t variation_selector = 0xFE0F; |
| 174 |
constexpr char32_t eye = 0x1F441; |
| 175 |
constexpr char32_t road = 0x1F6E3; |
| 176 |
constexpr char32_t railway = 0x1F6E4; |
| 177 |
}; // namespace UnicodeChar |
| 178 |
|
| 179 |
|
| 180 |
static bool ShouldUseSpriteForCodepoint(char32_t codepoint) |
| 181 |
{ |
| 182 |
switch (codepoint) |
| 183 |
{ |
| 184 |
case UnicodeChar::up: |
| 185 |
case UnicodeChar::down: |
| 186 |
case UnicodeChar::leftguillemet: |
| 187 |
case UnicodeChar::tick: |
| 188 |
case UnicodeChar::cross: |
| 189 |
case UnicodeChar::right: |
| 190 |
case UnicodeChar::rightguillemet: |
| 191 |
case UnicodeChar::small_up: |
| 192 |
case UnicodeChar::small_down: |
| 193 |
case UnicodeChar::left: |
| 194 |
case UnicodeChar::quote_open: |
| 195 |
case UnicodeChar::quote_close: |
| 196 |
case UnicodeChar::german_quote_open: |
| 197 |
case UnicodeChar::plus: |
| 198 |
case UnicodeChar::minus: |
| 199 |
case UnicodeChar::variation_selector: |
| 200 |
case UnicodeChar::eye: |
| 201 |
case UnicodeChar::road: |
| 202 |
case UnicodeChar::railway: |
| 203 |
return true; |
| 204 |
default: |
| 205 |
return false; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void ttf_process_string_literal(OpenRCT2::IContext* context, std::string_view text) |
| 210 |
{ |
| 211 |
CodepointView codepoints(text); |
| 212 |
std::optional<size_t> ttfRunIndex; |
| 213 |
for (auto it = codepoints.begin(); it != codepoints.end(); it++) |
| 214 |
{ |
| 215 |
auto codepoint = *it; |
| 216 |
if (ShouldUseSpriteForCodepoint(codepoint)) |
| 217 |
{ |
| 218 |
if (ttfRunIndex.has_value()) |
| 219 |
{ |
| 220 |
// Draw the TTF run |
| 221 |
auto len = it.GetIndex() - ttfRunIndex.value(); // { dg-bogus "may be used uninitialized" } |
| 222 |
ttf_draw_string_raw_ttf(context, text.substr(ttfRunIndex.value(), len)); |
| 223 |
ttfRunIndex = std::nullopt; |
| 224 |
} |
| 225 |
|
| 226 |
// Draw the sprite font glyph |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
if (!ttfRunIndex.has_value()) |
| 231 |
{ |
| 232 |
ttfRunIndex = it.GetIndex(); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |