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