Add some initial font support.
This commit is contained in:
parent
71b5e8d4b1
commit
ce11c52ae5
4 changed files with 72 additions and 25 deletions
|
@ -2,7 +2,10 @@
|
|||
|
||||
#include "IFontEngine.h"
|
||||
|
||||
#include "Image.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
|
||||
#include <ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
|
@ -12,20 +15,24 @@ class FreeTypeFontEngine : public IFontEngine
|
|||
public:
|
||||
FreeTypeFontEngine() = default;
|
||||
|
||||
void run()
|
||||
void initialize()
|
||||
{
|
||||
FT_Library library;
|
||||
auto error = FT_Init_FreeType( &library );
|
||||
if ( error )
|
||||
if (mLibrary)
|
||||
{
|
||||
std::cout << "Error initializing ft" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
FT_Face face;
|
||||
error = FT_New_Face( library,
|
||||
"/usr/share/fonts/truetype/tlwg/TlwgTypo-Bold.ttf",
|
||||
0,
|
||||
&face );
|
||||
const auto error = FT_Init_FreeType(&mLibrary);
|
||||
if (error)
|
||||
{
|
||||
std::cout << "Error initializing FreeType" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void loadFontFace(const std::filesystem::path& fontFile)
|
||||
{
|
||||
const auto full_path = mSystemFontLocation / fontFile;
|
||||
const auto error = FT_New_Face(mLibrary, full_path.c_str(), 0, &mWorkingFontFace );
|
||||
if ( error == FT_Err_Unknown_File_Format )
|
||||
{
|
||||
std::cout << "Found file but format not supported" << std::endl;
|
||||
|
@ -35,30 +42,63 @@ public:
|
|||
std::cout << "Failed to find or open file" << std::endl;
|
||||
}
|
||||
|
||||
auto charcode = 65;
|
||||
auto glyph_index = FT_Get_Char_Index( face, charcode );
|
||||
int pensize = 16;
|
||||
FT_Set_Pixel_Sizes(mWorkingFontFace, pensize, 0);
|
||||
}
|
||||
|
||||
std::unique_ptr<Image<unsigned char> > loadGlyph(unsigned charCode)
|
||||
{
|
||||
auto glyph_index = FT_Get_Char_Index( mWorkingFontFace, charCode );
|
||||
if (glyph_index == 0)
|
||||
{
|
||||
std::cout << "Got the null glyph" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
error = FT_Load_Glyph(
|
||||
face, /* handle to face object */
|
||||
glyph_index, /* glyph index */
|
||||
FT_LOAD_DEFAULT );
|
||||
auto error = FT_Load_Glyph(mWorkingFontFace, glyph_index, FT_LOAD_DEFAULT );
|
||||
if (error == FT_Err_Invalid_Argument)
|
||||
{
|
||||
std::cout << "Error loading glyph - bad arg" << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
else if(error)
|
||||
{
|
||||
auto code = static_cast<int>(error);
|
||||
std::cout << "Error loading glyph - somethign else " << code << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (face->glyph->format == FT_GLYPH_FORMAT_BITMAP)
|
||||
if (mWorkingFontFace->glyph->format == FT_GLYPH_FORMAT_BITMAP)
|
||||
{
|
||||
std::cout << "It is bitmap format" << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
error = FT_Render_Glyph( face->glyph, /* glyph slot */
|
||||
FT_RENDER_MODE_NORMAL );
|
||||
error = FT_Render_Glyph( mWorkingFontFace->glyph, FT_RENDER_MODE_NORMAL );
|
||||
}
|
||||
|
||||
auto rows = face->glyph->bitmap.rows;
|
||||
auto columns = face->glyph->bitmap.width;
|
||||
auto rows = mWorkingFontFace->glyph->bitmap.rows;
|
||||
auto columns = mWorkingFontFace->glyph->bitmap.width;
|
||||
std::cout << "We have a bitmap with rows " << rows << " and columns " << columns << std::endl;
|
||||
|
||||
auto image = std::make_unique<Image<unsigned char>> (columns, rows);
|
||||
|
||||
unsigned counter = 0;
|
||||
std::vector<unsigned char> data(rows*columns);
|
||||
for (unsigned idx=0; idx<rows; idx++)
|
||||
{
|
||||
for (unsigned jdx=0; jdx<columns; jdx++)
|
||||
{
|
||||
data[counter] = mWorkingFontFace->glyph->bitmap.buffer[counter];
|
||||
}
|
||||
}
|
||||
image->setData(data);
|
||||
return image;
|
||||
}
|
||||
|
||||
private:
|
||||
std::filesystem::path mSystemFontLocation{"/usr/share/fonts/"};
|
||||
|
||||
FT_Face mWorkingFontFace{nullptr};
|
||||
FT_Library mLibrary{nullptr};
|
||||
};
|
||||
|
|
|
@ -20,14 +20,15 @@ void OpenGlPainter::paint(DrawingContext* context)
|
|||
const auto height = double(surface->getHeight());
|
||||
const auto num_mesh = context->getScene()->getNumMeshes();
|
||||
|
||||
glClearColor(1.0, 1.0, 1.0, 0.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
glOrtho(0, width, 0, height, -1.0, 1.0);
|
||||
glScissor(0, 0, width, height);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
glClearColor(0.5, 0.5, 1.0, 0.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
//glMatrixMode(GL_MODELVIEW);
|
||||
//glLoadIdentity();
|
||||
|
||||
|
|
|
@ -60,6 +60,8 @@ bool XcbGlWindowInterface::initialize(xcb_window_t window)
|
|||
void XcbGlWindowInterface::resizeViewPort(unsigned width, unsigned height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
glScissor(0, 0, width, height);
|
||||
|
||||
glOrtho(0, width, 0, height, -1.0, 1.0);
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
|
|
@ -6,8 +6,12 @@
|
|||
int main()
|
||||
{
|
||||
FreeTypeFontEngine engine;
|
||||
engine.run();
|
||||
engine.initialize();
|
||||
|
||||
engine.loadFontFace("truetype/msttcorefonts/arial.ttf");
|
||||
engine.loadGlyph('A');
|
||||
engine.loadGlyph(66);
|
||||
engine.loadGlyph(67);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue