nomlib
Loading...
Searching...
No Matches
TrueTypeFont.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SDL2_TRUETYPE_FONT_HEADERS
30#define NOMLIB_SDL2_TRUETYPE_FONT_HEADERS
31
32#include <iostream>
33#include <string>
34#include <memory>
35
36#include "nomlib/config.hpp"
37#include "nomlib/graphics/fonts/IFont.hpp"
38#include "nomlib/graphics/fonts/FontMetrics.hpp"
39#include "nomlib/graphics/fonts/FontPage.hpp"
40
41// Dump glyph bounding coordinates & save source, destination sheets as PNG
42// files
43//#define NOM_DEBUG_SDL2_TRUE_TYPE_FONT_GLYPHS
44
45// Dump the individual glyph bitmaps as PNG files
46//#define NOM_DEBUG_SDL2_TRUE_TYPE_FONT_GLYPHS_PNG
47
48// Forward declarations (Third-party)
49typedef struct _TTF_Font TTF_Font;
50
51namespace nom {
52
53// Forward declarations
54class Image;
55
57class TrueTypeFont: public IFont
58{
59 public:
60 typedef TrueTypeFont self_type;
61 typedef self_type* RawPtr;
62 typedef std::shared_ptr<self_type> SharedPtr;
63
67 TrueTypeFont ( void );
68
72 ~TrueTypeFont ( void );
73
75 TrueTypeFont ( const TrueTypeFont& copy );
76
78 IFont::raw_ptr clone( void ) const override;
79
81 bool valid ( void ) const override;
82
83 enum IFont::FontType type ( void ) const override;
84
85 TTF_Font* font ( void ) const;
86
91 const Image* image(uint32 character_size) const override;
92
97 sint spacing ( uint32 character_size ) const override;
98
99 int point_size() const;
100
106 int newline( uint32 character_size ) const override;
107
115 int kerning( uint32 first_char, uint32 second_char, uint32 character_size ) const override;
116
123 int hinting( void ) const override;
124
131 const Glyph& glyph ( uint32 codepoint, uint32 character_size ) const override;
132
138 int outline ( /*uint32 character_size*/void ) /*const*/;
139
145 uint32 font_style( void ) const override;
146
159 bool set_point_size( int point_size ) override;
160
170 bool set_hinting( int type ) override;
171
177 bool set_outline( int outline ) override;
178
184 void set_font_style( uint32 style ) override;
185
187 void set_font_kerning( bool state ) override;
188
193 bool load( const std::string& filename ) override;
194
198 const FontMetrics& metrics( void ) const override;
199
200 private:
205 bool build ( uint32 character_size );
206
207 const GlyphPage& pages ( void ) const;
208
209 sint sheet_width ( void ) const;
210 sint sheet_height ( void ) const;
211
223 const IntRect glyph_rect ( FontPage& page, int width, int height ) const;
224
226 const enum IFont::FontType type_;
227
230
233
235 std::shared_ptr<TTF_Font> font_;
236
239 mutable GlyphPage pages_;
240
243
249 std::string filename_;
250
258
263};
264
265} // namespace nom
266
267#endif // include guard defined
268
Bitmap storage container.
Definition Image.hpp:48
std::shared_ptr< TTF_Font > font_
Font file data, used by SDL_ttf extension.
int outline(void)
Obtain font's outline size.
uint32 font_style(void) const override
Get the rendering style of the font.
int hinting(void) const override
Get the font's hinting style.
bool set_hinting(int type) override
Set the requested font hinting style.
void set_font_style(uint32 style) override
Set the rendering style of the font.
bool set_point_size(int point_size) override
Set a new font point size.
bool set_outline(int outline) override
Set font's outline size.
TrueTypeFont(const TrueTypeFont &copy)
Copy constructor.
bool build(uint32 character_size)
Trigger a rebuild of the font metrics from the current font; this recalculates character sizes,...
FontMetrics metrics_
General font metric data, such as the proper value for newline spacing.
sint sheet_width_
Width – in pixels – of overall texture atlas sheet.
bool load(const std::string &filename) override
Load a new TrueType font from a file.
~TrueTypeFont(void)
Destructor.
const Glyph & glyph(uint32 codepoint, uint32 character_size) const override
Obtain a glyph.
const IntRect glyph_rect(FontPage &page, int width, int height) const
Find a suitable rectangle within the texture for a glyph.
std::string filename_
TrueTypeFont(void)
Default constructor; initialize the object to sane defaults.
void set_font_kerning(bool state) override
Set the use of kerning for the font.
sint spacing(uint32 character_size) const override
Obtain text character spacing width in pixels.
const FontMetrics & metrics(void) const override
Obtain information about the loaded font.
const Image * image(uint32 character_size) const override
sint sheet_height_
Height – in pixels – of overall texture atlas sheet.
bool valid(void) const override
Validity check.
bool use_kerning_
Whether or not to use font kerning.
IFont::raw_ptr clone(void) const override
Construct a clone of the existing instance.
int kerning(uint32 first_char, uint32 second_char, uint32 character_size) const override
Obtain the kerning pair offsets between two glyphs.
enum IFont::FontType type_
The type of font we are.
int newline(uint32 character_size) const override
Obtain font's line spacing.
Container structure for global font metric data.
Container structure for font data.
Definition FontPage.hpp:45
Container structure for font data.
Definition Glyph.hpp:44