Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_font_renderer.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_FONT_RENDERER_H
7 #define TB_FONT_RENDERER_H
8 
9 #include "tb_core.h"
10 #include "tb_bitmap_fragment.h"
11 #include "tb_renderer.h"
12 #include "tb_tempbuffer.h"
13 #include "tb_linklist.h"
14 #include "tb_font_desc.h"
15 #include "utf8/utf8.h"
16 
17 namespace tb {
18 
19 class TBBitmap;
20 class TBFontFace;
21 
25 {
26 public:
27  TBFontGlyphData() : data8(nullptr), data32(nullptr), w(0), h(0), stride(0), rgb(false) {}
28  ~TBFontGlyphData() {}
29 
30  uint8_t *data8;
31  uint32_t *data32;
32  int w, h, stride;
33  bool rgb;
34 };
35 
38 {
39 public:
40  TBGlyphMetrics() : advance(0), x(0), y(0) {}
41  int16_t advance, x, y;
42 };
43 
46 {
47 public:
48  TBFontMetrics() : ascent(0), descent(0), height(0) {}
49  int16_t ascent;
50  int16_t descent;
51  int16_t height;
52 };
53 
55 class TBFontRenderer : public TBLinkOf<TBFontRenderer>
56 {
57 public:
58  virtual ~TBFontRenderer() {}
59 
62  virtual TBFontFace *Create(TBFontManager *font_manager, const TBStr & filename,
63  const TBFontDescription &font_desc) = 0;
64 
65  virtual bool RenderGlyph(TBFontGlyphData *data, UCS4 cp) = 0;
66  virtual void GetGlyphMetrics(TBGlyphMetrics *metrics, UCS4 cp) = 0;
67  virtual TBFontMetrics GetMetrics() = 0;
68  //virtual int GetKernAdvance(UCS4 cp1, UCS4 cp2) = 0;
69 };
70 
74 class TBFontGlyph : public TBLinkOf<TBFontGlyph>
75 {
76 public:
77  TBFontGlyph(const TBID &hash_id, UCS4 cp);
78  TBID hash_id;
79  UCS4 cp;
82  bool has_rgb;
83 };
84 
88 {
89 public:
92 
94  TBFontGlyph *GetGlyph(const TBID &hash_id, UCS4 cp);
95 
97  TBFontGlyph *CreateAndCacheGlyph(const TBID &hash_id, UCS4 cp);
98 
101  TBBitmapFragment *CreateFragment(TBFontGlyph *glyph, int w, int h, int stride, uint32_t *data);
102 
103 #ifdef TB_RUNTIME_DEBUG_INFO
104 
105  void Debug();
106 #endif
107 
108  // Implementing TBRendererListener
109  virtual void OnContextLost();
110  virtual void OnContextRestored();
111 private:
112  void DropGlyphFragment(TBFontGlyph *glyph);
113  TBBitmapFragmentManager m_frag_manager;
115  TBLinkListOf<TBFontGlyph> m_all_rendered_glyphs;
116 };
117 
120 {
121 public:
122  TBFontEffect() : m_blur_radius(0) {}
123  ~TBFontEffect() {}
124 
126  void SetBlurRadius(int blur_radius);
127 
130  bool RendersInRGB() const { return false; }
131 
132  TBFontGlyphData *Render(TBGlyphMetrics *metrics, const TBFontGlyphData *src);
133 private:
134  // Blur data
135  int m_blur_radius;
136  TBTempBuffer m_kernel;
137  TBTempBuffer m_blur_temp;
138  TBTempBuffer m_data_dst;
139 };
140 
143 {
144 public:
145  TBFontFace(TBFontGlyphCache *glyph_cache, TBFontRenderer *renderer, const TBFontDescription &font_desc);
146  ~TBFontFace();
147 
149  bool RenderGlyphs(const char *glyph_str, int glyph_str_len = TB_ALL_TO_TERMINATION);
150 
153  int GetAscent() const { return m_metrics.ascent; }
154 
157  int GetDescent() const { return m_metrics.descent; }
158 
160  int GetHeight() const { return m_metrics.height; }
161 
163  TBFontDescription GetFontDescription() const { return m_font_desc; }
164 
167  TBFontEffect *GetEffect() { return &m_effect; }
168 
170  void DrawString(int x, int y, const TBColor &color, const char *str, int len = TB_ALL_TO_TERMINATION);
171 
174  int GetStringWidth(const char *str, int len = TB_ALL_TO_TERMINATION);
175 
176 #ifdef TB_RUNTIME_DEBUG_INFO
177 
178  void Debug();
179 #endif
180 
183  void SetBackgroundFont(TBFontFace *font, const TBColor &col, int xofs, int yofs);
184 private:
185  TBID GetHashId(UCS4 cp) const;
186  TBFontGlyph *GetGlyph(UCS4 cp, bool render_if_needed);
187  TBFontGlyph *CreateAndCacheGlyph(UCS4 cp);
188  void RenderGlyph(TBFontGlyph *glyph);
189  TBFontGlyphCache *m_glyph_cache;
190  TBFontRenderer *m_font_renderer;
191  TBFontDescription m_font_desc;
192  TBFontMetrics m_metrics;
193  TBFontEffect m_effect;
194  TBTempBuffer m_temp_buffer;
195 
196  TBFontFace *m_bgFont;
197  int m_bgX;
198  int m_bgY;
199  TBColor m_bgColor;
200 };
201 
204 {
205 public:
207  const TBStr & GetFilename() const { return m_filename; }
208 
210  const TBStr & GetName() const { return m_name; }
211 
214  TBID GetID() const { return m_id; }
215 
216 private:
217  friend class TBFontManager;
218  TBFontInfo(const TBStr & filename, const TBStr & name) : m_filename(filename), m_name(name), m_id(name) {}
219  TBStr m_filename;
220  TBStr m_name;
221  TBID m_id;
222 };
223 
237 {
238 public:
239  TBFontManager();
240  ~TBFontManager();
241 
244  void AddRenderer(TBFontRenderer *renderer) { m_font_renderers.AddLast(renderer); }
245  void RemoveRenderer(TBFontRenderer *renderer) { m_font_renderers.Remove(renderer); }
246 
249  TBFontInfo *AddFontInfo(const char *filename, const char *name);
250 
252  TBFontInfo *GetFontInfo(const TBID &id) const;
253 
255  bool HasFontFace(const TBFontDescription &font_desc) const;
256 
260  TBFontFace *GetFontFace(const TBFontDescription &font_desc);
261 
265  TBFontFace *CreateFontFace(const TBFontDescription &font_desc);
266 
269  void SetDefaultFontDescription(const TBFontDescription &font_desc) { m_default_font_desc = font_desc; }
270  TBFontDescription GetDefaultFontDescription() const { return m_default_font_desc; }
271 
273  TBFontGlyphCache *GetGlyphCache() { return &m_glyph_cache; }
274 private:
278  TBFontGlyphCache m_glyph_cache;
279  TBFontDescription m_default_font_desc;
280  TBFontDescription m_test_font_desc;
281 };
282 
283 } // namespace tb
284 
285 #endif // TB_FONT_RENDERER_H
TBFontGlyph * GetGlyph(const TBID &hash_id, UCS4 cp)
Get the glyph or nullptr if it is not in the cache.
Definition: tb_font_renderer.cpp:139
bool has_rgb
if true, drawing should ignore text color.
Definition: tb_font_renderer.h:82
TBBitmapFragmentManager manages loading bitmaps of arbitrary size, pack as many of them into as few T...
Definition: tb_bitmap_fragment.h:177
int16_t ascent
Ascent. See TBFontFace::GetAscent()
Definition: tb_font_renderer.h:49
TBFontFace * GetFontFace(const TBFontDescription &font_desc)
Get a loaded font matching the description, or the default font if there is no exact match...
Definition: tb_font_renderer.cpp:466
TBHashTableOf is a TBHashTable with the given class type as content.
Definition: tb_hashtable.h:112
TBFontFace represents a loaded font that can measure and render strings.
Definition: tb_font_renderer.h:142
TBFontGlyphData is rendering info used during glyph rendering by TBFontRenderer.
Definition: tb_font_renderer.h:24
void DrawString(int x, int y, const TBColor &color, const char *str, int len=TB_ALL_TO_TERMINATION)
Draw string at position x, y (marks the upper left corner of the text).
Definition: tb_font_renderer.cpp:366
int16_t height
Height. See TBFontFace::GetHeight()
Definition: tb_font_renderer.h:51
TBFontDescription GetFontDescription() const
Get the font description that was used to create this font.
Definition: tb_font_renderer.h:163
virtual void OnContextRestored()
Called when the context has been restored again, and new TBBitmaps can be created again...
Definition: tb_font_renderer.cpp:232
virtual TBFontFace * Create(TBFontManager *font_manager, const TBStr &filename, const TBFontDescription &font_desc)=0
Open the given font file with this renderer and return a new TBFontFace with it.
TBFontEffect applies an effect on each glyph that is rendered in a TBFontFace.
Definition: tb_font_renderer.h:119
TBFontInfo * GetFontInfo(const TBID &id) const
Get TBFontInfo for the given font id, or nullptr if there is no match.
Definition: tb_font_renderer.cpp:456
TBRendererListener is a listener for TBRenderer.
Definition: tb_renderer.h:19
void SetBlurRadius(int blur_radius)
Set blur radius.
Definition: tb_font_renderer.cpp:48
TBFontGlyphCache * GetGlyphCache()
Return the glyph cache used for fonts created by this font manager.
Definition: tb_font_renderer.h:273
TBID is a wrapper for a uint32_t to be used as ID.
Definition: tb_id.h:18
TBID GetID() const
Get the font ID that can be used to create this font from a TBFontDescription (See TBFontDescription:...
Definition: tb_font_renderer.h:214
TBBitmapFragment represents a sub part of a TBBitmap.
Definition: tb_bitmap_fragment.h:140
TBGlyphMetrics metrics
The glyph metrics.
Definition: tb_font_renderer.h:80
int GetStringWidth(const char *str, int len=TB_ALL_TO_TERMINATION)
Measure the width of the given string.
Definition: tb_font_renderer.cpp:404
TBFontEffect * GetEffect()
Get the effect object, so the effect can be changed.
Definition: tb_font_renderer.h:167
int GetAscent() const
Get the vertical distance (positive) from the horizontal baseline to the highest character coordinate...
Definition: tb_font_renderer.h:153
TBStr is a simple string class.
Definition: tb_str.h:62
TBBitmapFragment * frag
The bitmap fragment, or nullptr if missing.
Definition: tb_font_renderer.h:81
bool HasFontFace(const TBFontDescription &font_desc) const
Return true if there is a font loaded that match the given font description.
Definition: tb_font_renderer.cpp:461
int GetDescent() const
Get the vertical distance (positive) from the horizontal baseline to the lowest character coordinate ...
Definition: tb_font_renderer.h:157
TBFontFace * CreateFontFace(const TBFontDescription &font_desc)
Create and add a font with the given description.
Definition: tb_font_renderer.cpp:475
bool RendersInRGB() const
Returns true if the result is in RGB and should not be painted using the color parameter given to Dra...
Definition: tb_font_renderer.h:130
int16_t descent
Descent. See TBFontFace::GetDescent()
Definition: tb_font_renderer.h:50
void AddRenderer(TBFontRenderer *renderer)
Add a renderer so fonts supported by the renderer can be created.
Definition: tb_font_renderer.h:244
void SetDefaultFontDescription(const TBFontDescription &font_desc)
Set the default font description.
Definition: tb_font_renderer.h:269
TBColor contains a 32bit color.
Definition: tb_color.h:21
TBFontGlyph holds glyph metrics and bitmap fragment.
Definition: tb_font_renderer.h:74
int GetHeight() const
Get height of the font in pixels.
Definition: tb_font_renderer.h:160
TBFontRenderer renders glyphs from a font file.
Definition: tb_font_renderer.h:55
virtual void OnContextLost()
Called when the context has been lost and all TBBitmaps need to be deleted.
Definition: tb_font_renderer.cpp:227
TBFontDescription describes a font.
Definition: tb_font_desc.h:18
TBFontInfo provides information about a font file associated with a font id.
Definition: tb_font_renderer.h:203
const TBStr & GetName() const
Get the font name.
Definition: tb_font_renderer.h:210
TBFontMetrics contains metrics for a font face.
Definition: tb_font_renderer.h:45
TBGlyphMetrics contains metrics for a font glyph.
Definition: tb_font_renderer.h:37
const TBStr & GetFilename() const
Get the font filename.
Definition: tb_font_renderer.h:207
TBFontInfo * AddFontInfo(const char *filename, const char *name)
Add TBFontInfo for the given font filename, so it can be loaded and identified using the font id in a...
Definition: tb_font_renderer.cpp:445
TBBitmapFragment * CreateFragment(TBFontGlyph *glyph, int w, int h, int stride, uint32_t *data)
Create a bitmap fragment for the given glyph and render data.
Definition: tb_font_renderer.cpp:164
TBFontManager creates and owns font faces (TBFontFace) which are looked up from TBFontDescription usi...
Definition: tb_font_renderer.h:236
bool RenderGlyphs(const char *glyph_str, int glyph_str_len=TB_ALL_TO_TERMINATION)
Render all glyphs needed to display the string.
Definition: tb_font_renderer.cpp:270
TBFontGlyphCache caches glyphs for font faces.
Definition: tb_font_renderer.h:87
TBFontGlyph * CreateAndCacheGlyph(const TBID &hash_id, UCS4 cp)
Create the glyph and put it in the cache.
Definition: tb_font_renderer.cpp:154
void SetBackgroundFont(TBFontFace *font, const TBColor &col, int xofs, int yofs)
Set a background font which will always be rendered behind this one when calling DrawString.
Definition: tb_font_renderer.cpp:262
TBTempBuffer manages a buffer that will be deleted on destruction.
Definition: tb_tempbuffer.h:18