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 "IFontEngine.h"
|
||||||
|
|
||||||
|
#include "Image.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include <ft2build.h>
|
#include <ft2build.h>
|
||||||
#include FT_FREETYPE_H
|
#include FT_FREETYPE_H
|
||||||
|
@ -12,20 +15,24 @@ class FreeTypeFontEngine : public IFontEngine
|
||||||
public:
|
public:
|
||||||
FreeTypeFontEngine() = default;
|
FreeTypeFontEngine() = default;
|
||||||
|
|
||||||
void run()
|
void initialize()
|
||||||
{
|
{
|
||||||
FT_Library library;
|
if (mLibrary)
|
||||||
auto error = FT_Init_FreeType( &library );
|
|
||||||
if ( error )
|
|
||||||
{
|
{
|
||||||
std::cout << "Error initializing ft" << std::endl;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FT_Face face;
|
const auto error = FT_Init_FreeType(&mLibrary);
|
||||||
error = FT_New_Face( library,
|
if (error)
|
||||||
"/usr/share/fonts/truetype/tlwg/TlwgTypo-Bold.ttf",
|
{
|
||||||
0,
|
std::cout << "Error initializing FreeType" << std::endl;
|
||||||
&face );
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 )
|
if ( error == FT_Err_Unknown_File_Format )
|
||||||
{
|
{
|
||||||
std::cout << "Found file but format not supported" << std::endl;
|
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;
|
std::cout << "Failed to find or open file" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto charcode = 65;
|
int pensize = 16;
|
||||||
auto glyph_index = FT_Get_Char_Index( face, charcode );
|
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)
|
if (glyph_index == 0)
|
||||||
{
|
{
|
||||||
std::cout << "Got the null glyph" << std::endl;
|
std::cout << "Got the null glyph" << std::endl;
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
error = FT_Load_Glyph(
|
auto error = FT_Load_Glyph(mWorkingFontFace, glyph_index, FT_LOAD_DEFAULT );
|
||||||
face, /* handle to face object */
|
if (error == FT_Err_Invalid_Argument)
|
||||||
glyph_index, /* glyph index */
|
{
|
||||||
FT_LOAD_DEFAULT );
|
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;
|
std::cout << "It is bitmap format" << std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
error = FT_Render_Glyph( face->glyph, /* glyph slot */
|
error = FT_Render_Glyph( mWorkingFontFace->glyph, FT_RENDER_MODE_NORMAL );
|
||||||
FT_RENDER_MODE_NORMAL );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto rows = face->glyph->bitmap.rows;
|
auto rows = mWorkingFontFace->glyph->bitmap.rows;
|
||||||
auto columns = face->glyph->bitmap.width;
|
auto columns = mWorkingFontFace->glyph->bitmap.width;
|
||||||
std::cout << "We have a bitmap with rows " << rows << " and columns " << columns << std::endl;
|
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 height = double(surface->getHeight());
|
||||||
const auto num_mesh = context->getScene()->getNumMeshes();
|
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);
|
glViewport(0, 0, width, height);
|
||||||
glOrtho(0, width, 0, height, -1.0, 1.0);
|
glOrtho(0, width, 0, height, -1.0, 1.0);
|
||||||
|
glScissor(0, 0, width, height);
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
|
glClearColor(0.5, 0.5, 1.0, 0.0);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
//glMatrixMode(GL_MODELVIEW);
|
//glMatrixMode(GL_MODELVIEW);
|
||||||
//glLoadIdentity();
|
//glLoadIdentity();
|
||||||
|
|
||||||
|
|
|
@ -60,6 +60,8 @@ bool XcbGlWindowInterface::initialize(xcb_window_t window)
|
||||||
void XcbGlWindowInterface::resizeViewPort(unsigned width, unsigned height)
|
void XcbGlWindowInterface::resizeViewPort(unsigned width, unsigned height)
|
||||||
{
|
{
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
|
glScissor(0, 0, width, height);
|
||||||
|
|
||||||
glOrtho(0, width, 0, height, -1.0, 1.0);
|
glOrtho(0, width, 0, height, -1.0, 1.0);
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
|
@ -6,8 +6,12 @@
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
FreeTypeFontEngine engine;
|
FreeTypeFontEngine engine;
|
||||||
engine.run();
|
engine.initialize();
|
||||||
|
|
||||||
|
engine.loadFontFace("truetype/msttcorefonts/arial.ttf");
|
||||||
|
engine.loadGlyph('A');
|
||||||
|
engine.loadGlyph(66);
|
||||||
|
engine.loadGlyph(67);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue