CorsixTH engine (the C++ part)
Open source implementation of Theme Hospital
Loading...
Searching...
No Matches
th_gfx_font.h
Go to the documentation of this file.
1/*
2Copyright (c) 2010 Peter "Corsix" Cawley
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of
5this software and associated documentation files (the "Software"), to deal in
6the Software without restriction, including without limitation the rights to
7use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8of the Software, and to permit persons to whom the Software is furnished to do
9so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22
23#ifndef CORSIX_TH_TH_GFX_FONT_H_
24#define CORSIX_TH_TH_GFX_FONT_H_
25#include "config.h"
26
27#include <SDL_render.h>
28#include <ft2build.h> // IWYU pragma: keep
29// IWYU pragma: no_include "freetype/config/ftheader.h"
30
31#include <climits>
32
33#include "th_gfx_sdl.h"
34#include FT_FREETYPE_H
35#include FT_IMAGE_H
36#include FT_TYPES_H
37
38enum class text_alignment {
39 left = 0,
40 center = 1,
41 right = 2,
42};
43
45
50
53
55 int end_x;
56
59
61 int end_y;
62
64 int width;
65};
66
69 bool enabled{false};
70
72 int offset_x{1};
73
75 int offset_y{1};
76
78 argb_colour color{0xFF000000u};
79};
80
81class font {
82 public:
83 virtual ~font() = default;
84
86
97 size_t iMessageLength,
98 int iMaxWidth = INT_MAX) const = 0;
99
101
112 virtual void draw_text(render_target* pCanvas, const char* sMessage,
113 size_t iMessageLength, int iX, int iY) const = 0;
114
116
134 render_target* pCanvas, const char* sMessage, size_t iMessageLength,
135 int iX, int iY, int iWidth, int iMaxRows = INT_MAX, int iSkipRows = 0,
137};
138
139class bitmap_font final : public font {
140 public:
142
144
153 bitmap_font_character_set character_set);
154
155 void set_scale_factor(int factor);
156
157 sprite_sheet* get_sprite_sheet() { return sheet; }
158
160
164 void set_separation(int iCharSep, int iLineSep);
165
167 int iMaxWidth = INT_MAX) const override;
168
169 void draw_text(render_target* pCanvas, const char* sMessage,
170 size_t iMessageLength, int iX, int iY) const override;
171
173 render_target* pCanvas, const char* sMessage, size_t iMessageLength,
174 int iX, int iY, int iWidth, int iMaxRows = INT_MAX, int iSkipRows = 0,
176
177 private:
178 sprite_sheet* sheet{nullptr};
179 int letter_spacing{};
180 int line_spacing{};
181 int scale_factor{1};
183};
184
186
198class freetype_font final : public font {
199 public:
201 ~freetype_font() override;
202
204
209 static const char* get_copyright_notice();
210
212
216
218 void clear_cache();
219
221
227 FT_Error set_face(const uint8_t* pData, size_t iLength);
228
230
241 int* width, int* height);
242
244
250
251 void set_font_color(argb_colour color);
252
254
256 int iMaxWidth = INT_MAX) const override;
257
258 void draw_text(render_target* pCanvas, const char* sMessage,
259 size_t iMessageLength, int iX, int iY) const override;
260
262 render_target* pCanvas, const char* sMessage, size_t iMessageLength,
263 int iX, int iY, int iWidth, int iMaxRows = INT_MAX, int iSkipRows = 0,
265
266 private:
267 struct cached_text {
269 char* message;
270
272 uint8_t* data;
273
275 SDL_Texture* texture;
276
278 size_t message_length;
279
281 size_t message_buffer_length;
282
284 int width;
285
287 int height;
288
290 int widest_line_width;
291
293 int last_x;
294
296 int row_count;
297
299 text_alignment alignment;
300
302 bool is_valid;
303 };
304
306 void render_mono(cached_text* pCacheEntry, FT_Bitmap* pBitmap, FT_Pos x,
307 FT_Pos y) const;
308
310 void render_gray(cached_text* pCacheEntry, FT_Bitmap* pBitmap, FT_Pos x,
311 FT_Pos y) const;
312
313 static FT_Library freetype_library;
314 static int freetype_init_count;
315 static constexpr int cache_size_log2{7};
316 FT_Face font_face{nullptr};
317 argb_colour font_color{0};
318 font_shadow_options shadow_opts{};
319 bool is_done_freetype_init{false};
320 mutable cached_text cache[1 << cache_size_log2]{};
321
322 // The following five methods are implemented by the rendering engine.
323
325
330 bool is_monochrome() const;
331
333
341 void make_texture(const render_target* pEventualCanvas,
342 cached_text* pCacheEntry) const;
343
344 static void copy_pixel_data(const cached_text* cacheEntry, argb_colour color,
345 int offsetX, int offsetY,
346 std::vector<argb_colour>::iterator outIter);
347
349
355 void free_texture(cached_text* pCacheEntry) const;
356
358
366 void draw_texture(render_target* pCanvas, cached_text* pCacheEntry, int iX,
367 int iY) const;
368};
369
370#endif // CORSIX_TH_TH_GFX_FONT_H_
Definition th_gfx_font.h:139
void draw_text(render_target *pCanvas, const char *sMessage, size_t iMessageLength, int iX, int iY) const override
Draw a single line of text.
Definition th_gfx_font.cpp:82
text_layout draw_text_wrapped(render_target *pCanvas, const char *sMessage, size_t iMessageLength, int iX, int iY, int iWidth, int iMaxRows=INT_MAX, int iSkipRows=0, text_alignment eAlign=text_alignment::left) const override
Draw a single line of text, splitting it at word boundaries.
Definition th_gfx_font.cpp:111
sprite_sheet * get_sprite_sheet()
Definition th_gfx_font.h:157
text_layout get_text_dimensions(const char *sMessage, size_t iMessageLength, int iMaxWidth=INT_MAX) const override
Get the size of drawn text.
Definition th_gfx_font.cpp:75
void set_scale_factor(int factor)
Definition th_gfx_font.cpp:73
void set_sprite_sheet(sprite_sheet *pSpriteSheet, bitmap_font_character_set character_set)
Set the character glyph sprite sheet.
Definition th_gfx_font.cpp:62
void set_separation(int iCharSep, int iLineSep)
Set the separation between characters and between lines.
Definition th_gfx_font.cpp:68
Definition th_gfx_font.h:81
virtual ~font()=default
virtual text_layout draw_text_wrapped(render_target *pCanvas, const char *sMessage, size_t iMessageLength, int iX, int iY, int iWidth, int iMaxRows=INT_MAX, int iSkipRows=0, text_alignment eAlign=text_alignment::left) const =0
Draw a single line of text, splitting it at word boundaries.
virtual text_layout get_text_dimensions(const char *sMessage, size_t iMessageLength, int iMaxWidth=INT_MAX) const =0
Get the size of drawn text.
virtual void draw_text(render_target *pCanvas, const char *sMessage, size_t iMessageLength, int iX, int iY) const =0
Draw a single line of text.
Adaptor around the FreeType2 library to a THFont.
Definition th_gfx_font.h:198
void draw_text(render_target *pCanvas, const char *sMessage, size_t iMessageLength, int iX, int iY) const override
Draw a single line of text.
Definition th_gfx_font.cpp:377
void set_shadow_options(const font_shadow_options &options)
Definition th_gfx_font.cpp:366
FT_Error set_ideal_character_size(int iWidth, int iHeight)
Set the ideal character size using pixel values.
Definition th_gfx_font.cpp:327
freetype_font()
Definition th_gfx_font.cpp:216
text_layout draw_text_wrapped(render_target *pCanvas, const char *sMessage, size_t iMessageLength, int iX, int iY, int iWidth, int iMaxRows=INT_MAX, int iSkipRows=0, text_alignment eAlign=text_alignment::left) const override
Draw a single line of text, splitting it at word boundaries.
Definition th_gfx_font.cpp:416
~freetype_font() override
Definition th_gfx_font.cpp:233
void clear_cache()
Remove all cached strings, as our graphics context has changed.
Definition th_gfx_font.cpp:265
void set_font_color(argb_colour color)
Definition th_gfx_font.cpp:364
FT_Error match_bitmap_font(sprite_sheet *font_spritesheet, argb_colour *color, int *width, int *height)
Find colour and size to best match a bitmap font.
Definition th_gfx_font.cpp:289
static const char * get_copyright_notice()
Get the copyright notice which should be displayed for FreeType2.
Definition th_gfx_font.cpp:249
FT_Error initialise()
Initialise the FreeType2 library.
Definition th_gfx_font.cpp:254
text_layout get_text_dimensions(const char *sMessage, size_t iMessageLength, int iMaxWidth=INT_MAX) const override
Get the size of drawn text.
Definition th_gfx_font.cpp:370
FT_Error set_face(const uint8_t *pData, size_t iLength)
Set the font face to be used.
Definition th_gfx_font.cpp:273
Definition th_gfx_sdl.h:247
Sheet of sprites.
Definition th_gfx_sdl.h:504
Definition th_gfx_font.h:67
int offset_y
Vertical offset of the shadow, in pixels.
Definition th_gfx_font.h:75
int offset_x
Horizontal offset of the shadow, in pixels.
Definition th_gfx_font.h:72
argb_colour color
Colour of the shadow.
Definition th_gfx_font.h:78
bool enabled
Whether to draw a shadow.
Definition th_gfx_font.h:69
Definition th_gfx_font.h:47
int start_y
Top Y-coordinate for the start of the text.
Definition th_gfx_font.h:58
int width
Width of the widest line in the text.
Definition th_gfx_font.h:64
int end_x
Right X-coordinate for the right part of the last letter rendered.
Definition th_gfx_font.h:55
int start_x
Left X-coordinate for the start of the text.
Definition th_gfx_font.h:52
int end_y
Bottom Y-coordinate for the end of the text.
Definition th_gfx_font.h:61
int row_count
Number of rows the rendered text spans.
Definition th_gfx_font.h:49
text_alignment
Definition th_gfx_font.h:38
bitmap_font_character_set
Definition th_gfx_font.h:44
uint32_t argb_colour
32bpp ARGB colour. See palette::pack_argb
Definition th_gfx_sdl.h:65