2021-08-06 14:22:56 +02:00
|
|
|
#ifndef BWIDGETS_FONT_HPP
|
|
|
|
#define BWIDGETS_FONT_HPP
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-26 18:59:52 +02:00
|
|
|
#include <memory>
|
2021-07-18 18:26:12 +02:00
|
|
|
#include <string>
|
|
|
|
|
2021-08-23 00:00:57 +02:00
|
|
|
#include <SDL_ttf.h>
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
#include <basic_widgets/core/type/color.hpp>
|
2021-08-26 18:59:52 +02:00
|
|
|
#include <basic_widgets/core/type/deleter.hpp>
|
2021-07-18 18:26:12 +02:00
|
|
|
#include <basic_widgets/core/type/size.hpp>
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
namespace bwidgets
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-08-23 23:50:14 +02:00
|
|
|
// Wrap TTF_Font from SDL_ttf and provide a font finding function.
|
2021-08-26 18:59:52 +02:00
|
|
|
class Font final
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
2021-08-26 18:59:52 +02:00
|
|
|
const std::unique_ptr<TTF_Font, Deleter> _data;
|
|
|
|
|
2021-08-11 15:52:07 +02:00
|
|
|
public:
|
2021-07-29 16:06:03 +02:00
|
|
|
enum struct Hinting
|
|
|
|
{
|
2021-07-28 15:15:32 +02:00
|
|
|
LIGHT = TTF_HINTING_LIGHT,
|
|
|
|
MONO = TTF_HINTING_MONO,
|
|
|
|
NONE = TTF_HINTING_NONE,
|
|
|
|
NORMAL = TTF_HINTING_NORMAL
|
|
|
|
};
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-07-29 16:06:03 +02:00
|
|
|
enum struct RenderMode
|
|
|
|
{
|
|
|
|
SOLID,
|
|
|
|
SHADED,
|
|
|
|
BLENDED
|
2021-07-28 15:15:32 +02:00
|
|
|
};
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
enum struct Style
|
2021-07-29 16:06:03 +02:00
|
|
|
{
|
2021-08-06 14:22:56 +02:00
|
|
|
BOLD = TTF_STYLE_BOLD,
|
|
|
|
ITALIC = TTF_STYLE_ITALIC,
|
|
|
|
NORMAL = TTF_STYLE_NORMAL,
|
|
|
|
STRIKETHROUGH = TTF_STYLE_STRIKETHROUGH,
|
|
|
|
UNDERLINE = TTF_STYLE_UNDERLINE
|
2021-07-28 15:15:32 +02:00
|
|
|
};
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-12 16:04:02 +02:00
|
|
|
static const Color default_color_bg;
|
|
|
|
static const Color default_color_fg;
|
2021-08-06 14:22:56 +02:00
|
|
|
|
2021-07-28 15:15:32 +02:00
|
|
|
const int ascent;
|
|
|
|
const int descent;
|
2021-07-30 20:41:47 +02:00
|
|
|
const long faces;
|
2021-07-28 15:15:32 +02:00
|
|
|
const std::string family_name;
|
|
|
|
const bool fixed_width;
|
|
|
|
const int height;
|
|
|
|
const int line_skip;
|
|
|
|
const std::string style_name;
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-14 16:44:47 +02:00
|
|
|
explicit Font(TTF_Font*);
|
2021-08-27 01:21:25 +02:00
|
|
|
// Load font at given path and with given size.
|
2021-08-21 20:59:39 +02:00
|
|
|
Font(std::string_view, int);
|
2021-07-28 15:15:32 +02:00
|
|
|
Font(const Font&) = delete;
|
2021-08-12 15:29:25 +02:00
|
|
|
Font(Font&&) = delete;
|
|
|
|
~Font() noexcept = default;
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-12 23:50:22 +02:00
|
|
|
auto operator=(const Font&) = delete;
|
|
|
|
auto operator=(Font&&) = delete;
|
2021-07-19 23:50:50 +02:00
|
|
|
|
2021-08-08 16:00:22 +02:00
|
|
|
[[nodiscard]] auto hinting() const noexcept -> Hinting;
|
|
|
|
auto hinting(Hinting) noexcept -> Font*;
|
|
|
|
[[nodiscard]] auto kerning() const noexcept -> bool;
|
|
|
|
auto kerning(bool) noexcept -> Font*;
|
|
|
|
[[nodiscard]] auto outline() const noexcept -> int;
|
|
|
|
auto outline(int) noexcept -> Font*;
|
2021-08-21 20:59:39 +02:00
|
|
|
auto render(RenderMode, std::string_view, Color fg = default_color_fg,
|
2021-08-26 18:59:52 +02:00
|
|
|
Color bg = default_color_bg)
|
|
|
|
-> std::unique_ptr<SDL_Surface, Deleter>;
|
2021-08-08 16:00:22 +02:00
|
|
|
[[nodiscard]] auto style() const noexcept -> uint8_t;
|
|
|
|
auto style(uint8_t) noexcept -> Font*;
|
2021-08-21 20:59:39 +02:00
|
|
|
[[nodiscard]] auto text_size(std::string_view) const noexcept -> Size;
|
2021-07-18 18:26:12 +02:00
|
|
|
|
2021-08-23 23:50:14 +02:00
|
|
|
// Get file path of the font best matching a give fontconfig pattern.
|
2021-08-21 20:59:39 +02:00
|
|
|
[[nodiscard]] static auto find(std::string_view) -> std::string;
|
2021-07-18 18:26:12 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|