Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
rendering_engine::TextRenderer Class Reference

#include <text_renderer.hpp>

Public Member Functions

 TextRenderer (std::string const pathToFont, unsigned int const size)
 ~TextRenderer ()
ImageData CreateGlyphBitmap (char const character)
ImageData CreateStringBitmap (std::string text)

Static Public Member Functions

static ImageData CreateGlyphBitmap (std::string const pathToFont, char const character)

Protected Attributes

FT_Error mErrorResult = FT_Err_Ok
FT_Library mLibrary = 0
FT_Face mFace = 0
std::map< char, ImageDatamGlyphBitmaps
ImageData mGlyphTextureAtlas
std::map< char, std::pair< unsigned int, unsigned int > > mTexAtlasData

Detailed Description

Definition at line 23 of file text_renderer.hpp.

Constructor & Destructor Documentation

◆ TextRenderer()

rendering_engine::TextRenderer::TextRenderer ( std::string const pathToFont,
unsigned int const size )

Definition at line 7 of file text_renderer.cpp.

8 :
9 mErrorResult(FT_Err_Ok),
10 mLibrary(0),
11 mFace(0)
12{
13 boost::filesystem::path path{ pathToFont };
14 if( boost::filesystem::is_regular_file(pathToFont) )
15 {
16 //Debug level
17 std::cout << "Font file found." << std::endl;
18 }
19 else
20 {
21 //Error level
22 std::cout << "Font file not found." << std::endl;
23 }
24
25 mErrorResult = FT_Init_FreeType(&mLibrary);
26 if( mErrorResult )
27 {
28 throw std::runtime_error{ "Failed to initialize FreeType library!" };
29 }
30
31 mErrorResult = FT_New_Face(mLibrary, pathToFont.c_str(), 0, &mFace);
32 if( mErrorResult )
33 {
34 throw std::runtime_error{ "Failed to create new face!" };
35 }
36
37 mErrorResult = FT_Set_Char_Size(mFace, size << 6, size << 6, 90, 90);
38 if( mErrorResult )
39 {
40 throw std::runtime_error{ "Failed to set char size!" };
41 }
42
43 for( int i = 32; i < 128; i++ )
44 {
45 auto glyphBitmap = CreateGlyphBitmap((char)(i));
46 mGlyphBitmaps.emplace((char)(i), glyphBitmap);
47 }
48
49 TextureAtlasMaker texAtlasMaker(mGlyphBitmaps);
50 texAtlasMaker.CreateTextureAtlas(mTexAtlasData, mGlyphTextureAtlas);
51
52 mGlyphTextureAtlas.WritePngFile("GlyphTextureAtlas.png");
53}
std::map< char, ImageData > mGlyphBitmaps
std::map< char, std::pair< unsigned int, unsigned int > > mTexAtlasData
ImageData CreateGlyphBitmap(char const character)

◆ ~TextRenderer()

rendering_engine::TextRenderer::~TextRenderer ( )

Definition at line 55 of file text_renderer.cpp.

56{
57 FT_Done_Face(mFace);
58 FT_Done_FreeType(mLibrary);
59}

Member Function Documentation

◆ CreateGlyphBitmap() [1/2]

ImageData rendering_engine::TextRenderer::CreateGlyphBitmap ( char const character)

Definition at line 61 of file text_renderer.cpp.

62{
63 FT_UInt glyph_index = 0;
64 glyph_index = FT_Get_Char_Index(mFace, (std::uint16_t)(character));
65 mErrorResult = FT_Load_Glyph(mFace, glyph_index, FT_LOAD_DEFAULT);
66 if( mErrorResult )
67 {
68 throw std::runtime_error{ "Failed to load glyph!" };
69 }
70
71 mErrorResult = FT_Render_Glyph(mFace->glyph, FT_RENDER_MODE_NORMAL);
72 if( mErrorResult )
73 {
74 throw std::runtime_error{ "Failed to render glyph!" };
75 }
76
77 std::vector<unsigned int> buffer;
78 for( int i = 0; i < (mFace->glyph->bitmap.width * mFace->glyph->bitmap.rows); ++i )
79 {
80 buffer.push_back((unsigned int)(255));
81 buffer.push_back((unsigned int)(255));
82 buffer.push_back((unsigned int)(255));
83 buffer.push_back((unsigned int)(mFace->glyph->bitmap.buffer[i]));
84 }
85
86 ImageData imageData(mFace->glyph->bitmap.width, mFace->glyph->bitmap.rows, buffer);
87
88 return imageData;
89}

◆ CreateGlyphBitmap() [2/2]

ImageData rendering_engine::TextRenderer::CreateGlyphBitmap ( std::string const pathToFont,
char const character )
static

Definition at line 120 of file text_renderer.cpp.

121{
122 boost::filesystem::path path{ pathToFont };
123 if( boost::filesystem::is_regular_file(pathToFont) )
124 {
125 std::cout << "Font file found." << std::endl;
126 }
127 else
128 {
129 std::cout << "Font file not found." << std::endl;
130 }
131
132 FT_Error error = FT_Err_Ok;
133 FT_Library library = 0;
134 FT_Face face = 0;
135
136 error = FT_Init_FreeType( &library );
137 if( error )
138 {
139 throw std::runtime_error{ "Failed to initialize FreeType library!" };
140 }
141
142 error = FT_New_Face( library, pathToFont.c_str(), 0, &face);
143 if( error )
144 {
145 throw std::runtime_error{ "Failed to create new face!" };
146 }
147
148 error = FT_Set_Char_Size(face, 10 << 6, 10 << 6, 90, 90);
149 if( error )
150 {
151 throw std::runtime_error{ "Failed to set char size!" };
152 }
153
154 FT_UInt glyph_index = 0;
155 glyph_index = FT_Get_Char_Index(face, (std::uint16_t)(character));
156 error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
157 if( error )
158 {
159 throw std::runtime_error{"Failed to load glyph!"};
160 }
161
162 error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
163 if( error )
164 {
165 throw std::runtime_error{ "Failed to render glyph!" };
166 }
167
168 std::vector<unsigned int> buffer;
169 for( int i = 0; i < (face->glyph->bitmap.width * face->glyph->bitmap.rows); ++i )
170 {
171 buffer.push_back((unsigned int)(255));
172 buffer.push_back((unsigned int)(255));
173 buffer.push_back((unsigned int)(255));
174 buffer.push_back((unsigned int)(face->glyph->bitmap.buffer[i]));
175 }
176
177 ImageData imageData(face->glyph->bitmap.width, face->glyph->bitmap.rows, buffer);
178
179 FT_Done_Face(face);
180 FT_Done_FreeType(library);
181 return imageData;
182}

◆ CreateStringBitmap()

ImageData rendering_engine::TextRenderer::CreateStringBitmap ( std::string text)

Definition at line 91 of file text_renderer.cpp.

92{
93 //Store image data and x offset for each letter in the string.
94 std::vector<std::pair<unsigned int, ImageData>> glyphBitmaps;
95 unsigned int stringWidthPx = 0U;
96 unsigned int stringHeighPx = 0U;
97 for( auto glypth : text )
98 {
99 auto glBitmap = CreateGlyphBitmap(glypth);
100 auto const offsetX = stringWidthPx;
101 stringWidthPx += glBitmap.GetWidth();
102 if( glBitmap.GetHeight() > stringHeighPx )
103 {
104 stringHeighPx = glBitmap.GetHeight();
105 }
106 glyphBitmaps.push_back(std::pair<unsigned int, ImageData>(offsetX, glBitmap));
107 }
108
109 ImageData stringBitmap(stringWidthPx, stringHeighPx);
110 for( auto it : glyphBitmaps )
111 {
112 unsigned int const xPos = it.first;
113 unsigned int const yPos = stringHeighPx - it.second.GetHeight();
114 ImageData::DrawImageOnImageAtPos( xPos, yPos, stringBitmap, it.second);
115 }
116
117 return stringBitmap;
118}
static void DrawImageOnImageAtPos(unsigned int const x, unsigned int const y, ImageData &toImage, ImageData &fromImage)
Overlays one image on top of another at a given position.

Member Data Documentation

◆ mErrorResult

FT_Error rendering_engine::TextRenderer::mErrorResult = FT_Err_Ok
protected

Definition at line 40 of file text_renderer.hpp.

◆ mFace

FT_Face rendering_engine::TextRenderer::mFace = 0
protected

Definition at line 42 of file text_renderer.hpp.

◆ mGlyphBitmaps

std::map<char, ImageData> rendering_engine::TextRenderer::mGlyphBitmaps
protected

Definition at line 44 of file text_renderer.hpp.

◆ mGlyphTextureAtlas

ImageData rendering_engine::TextRenderer::mGlyphTextureAtlas
protected

Definition at line 45 of file text_renderer.hpp.

◆ mLibrary

FT_Library rendering_engine::TextRenderer::mLibrary = 0
protected

Definition at line 41 of file text_renderer.hpp.

◆ mTexAtlasData

std::map<char, std::pair<unsigned int, unsigned int> > rendering_engine::TextRenderer::mTexAtlasData
protected

Definition at line 48 of file text_renderer.hpp.


The documentation for this class was generated from the following files: