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