Simulant  21.12-1292
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
font.h
1 #pragma once
2 
3 #include <cstddef>
4 #include <map>
5 
6 #include "deps/stb_truetype/stb_truetype.h"
7 #include "types.h"
8 #include "generic/managed.h"
9 #include "generic/identifiable.h"
10 #include "loadable.h"
11 #include "asset.h"
12 #include "utils/limited_vector.h"
13 
14 #ifdef __DREAMCAST__
15 #undef page_count
16 #endif
17 
18 struct stbtt_fontinfo;
19 
20 namespace smlt {
21 
22 namespace loaders {
23  class TTFLoader;
24  class FNTLoader;
25 }
26 
27 enum CharacterSet {
28  CHARACTER_SET_LATIN
29 };
30 
31 struct CharInfo{
32  std::size_t page = 0;
33  Vec2 xy0;
34  Vec2 xy1;
35  Vec2 st0;
36  Vec2 st1;
37  Vec2 off;
38  float xadvance; // Offsets and advance
39 };
40 
41 enum FontStyle {
42  FONT_STYLE_NORMAL,
43  FONT_STYLE_ITALIC
44 };
45 
46 enum FontWeight {
47  FONT_WEIGHT_LIGHT,
48  FONT_WEIGHT_NORMAL,
49  FONT_WEIGHT_BOLD,
50  FONT_WEIGHT_BLACK
51 };
52 
53 constexpr const char* font_weight_name(FontWeight weight) {
54  return (weight == FONT_WEIGHT_NORMAL) ? "Regular": (weight == FONT_WEIGHT_BOLD) ? "Bold" : (weight == FONT_WEIGHT_BLACK) ? "Black" : "Light";
55 }
56 
57 constexpr const char* font_style_name(FontStyle style) {
58  return (style == FONT_STYLE_NORMAL) ? "Normal" : "Italic";
59 }
60 
61 
62 struct FontPage {
63  TexturePtr texture;
64  MaterialPtr material;
65  uint16_t width;
66  uint16_t height;
67 };
68 
69 class Font:
70  public RefCounted<Font>,
71  public Asset,
72  public Loadable,
73  public generic::Identifiable<AssetID>,
74  public ChainNameable<Font> {
75 
76 public:
77  const static std::size_t max_pages = 4;
78 
79  static std::string generate_name(const std::string& family, const uint16_t& size, FontWeight weight, FontStyle style) {
80  return family + "-" + font_weight_name(weight) + "-" + font_style_name(style) + "-" + smlt::to_string(size);
81  }
82 
83  Font(AssetID id, AssetManager* asset_manager);
84 
85  bool on_init() override;
86 
87  std::size_t page_count() const {
88  return pages_.size();
89  }
90 
91  const FontPage* page(std::size_t i) const;
92 
93  std::pair<Vec2, Vec2> char_texcoords(char32_t c) const;
94  std::pair<Vec2, Vec2> char_corners(char32_t c) const;
95 
96  uint16_t character_width(char32_t ch);
97  uint16_t character_height(char32_t ch);
98  float character_advance(char32_t ch, char32_t next);
99  Vec2 character_offset(char32_t ch);
100  uint16_t character_page(char32_t ch);
101 
102  uint16_t size() const { return font_size_; }
103 
104  int16_t ascent() const;
105  int16_t descent() const;
106  int16_t line_gap() const;
107 
108 private:
109  /* Given a character, return the width/height of the page it's on */
110  uint16_t page_width(char16_t ch) const;
111  uint16_t page_height(char16_t ch) const;
112 
113  uint16_t font_size_ = 0;
114  int16_t ascent_ = 0;
115  int16_t descent_ = 0;
116  int16_t line_gap_ = 0;
117  float scale_ = 0;
118 
119  std::unique_ptr<stbtt_fontinfo> info_;
120  std::map<char16_t, CharInfo> char_data_;
121 
123 
124  friend class ui::Widget;
125  friend class loaders::TTFLoader;
126  friend class loaders::FNTLoader;
127 };
128 
129 }
smlt::Font
Definition: font.h:74
smlt::RefCounted
Definition: managed.h:71
smlt
Definition: animation.cpp:25
smlt::loaders::TTFLoader
Definition: ttf_loader.h:8
smlt::ui::Widget
Definition: widget.h:130
smlt::generic::Identifiable
Definition: identifiable.h:28
smlt::FontPage
Definition: font.h:62
smlt::loaders::FNTLoader
Definition: fnt_loader.h:8
smlt::LimitedVector
Definition: limited_vector.h:13
smlt::Loadable
Definition: loadable.h:26
smlt::ChainNameable
Definition: nameable.h:34
smlt::CharInfo
Definition: font.h:31
smlt::Asset
Definition: asset.h:37
smlt::Vec2
Definition: vec2.h:16
smlt::AssetManager
Definition: asset_manager.h:92