From ce11c52ae5a6011dcdcfb31ffad17c8f81648455 Mon Sep 17 00:00:00 2001 From: James Grogan Date: Mon, 14 Nov 2022 17:27:24 +0000 Subject: [PATCH] Add some initial font support. --- src/fonts/FreeTypeFontEngine.h | 82 ++++++++++++++----- src/graphics/opengl/OpenGlPainter.cpp | 7 +- .../x11/XcbGlWindowInterface.cpp | 2 + test/fonts/TestFreeTypeFontEngine.cpp | 6 +- 4 files changed, 72 insertions(+), 25 deletions(-) diff --git a/src/fonts/FreeTypeFontEngine.h b/src/fonts/FreeTypeFontEngine.h index 0e4a86f..9bbf3b5 100644 --- a/src/fonts/FreeTypeFontEngine.h +++ b/src/fonts/FreeTypeFontEngine.h @@ -2,7 +2,10 @@ #include "IFontEngine.h" +#include "Image.h" + #include +#include #include #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 > 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(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> (columns, rows); + + unsigned counter = 0; + std::vector data(rows*columns); + for (unsigned idx=0; idxglyph->bitmap.buffer[counter]; + } + } + image->setData(data); + return image; } + +private: + std::filesystem::path mSystemFontLocation{"/usr/share/fonts/"}; + + FT_Face mWorkingFontFace{nullptr}; + FT_Library mLibrary{nullptr}; }; diff --git a/src/graphics/opengl/OpenGlPainter.cpp b/src/graphics/opengl/OpenGlPainter.cpp index df67c2a..02375aa 100644 --- a/src/graphics/opengl/OpenGlPainter.cpp +++ b/src/graphics/opengl/OpenGlPainter.cpp @@ -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(); diff --git a/src/windows/ui_interfaces/x11/XcbGlWindowInterface.cpp b/src/windows/ui_interfaces/x11/XcbGlWindowInterface.cpp index abc6d1e..c46e91f 100644 --- a/src/windows/ui_interfaces/x11/XcbGlWindowInterface.cpp +++ b/src/windows/ui_interfaces/x11/XcbGlWindowInterface.cpp @@ -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(); diff --git a/test/fonts/TestFreeTypeFontEngine.cpp b/test/fonts/TestFreeTypeFontEngine.cpp index b45fd00..998e7dd 100644 --- a/test/fonts/TestFreeTypeFontEngine.cpp +++ b/test/fonts/TestFreeTypeFontEngine.cpp @@ -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; }