From 5e80fff82f92161b82dd2423fc3ac954c765f067 Mon Sep 17 00:00:00 2001 From: Jordan Bieder Date: Tue, 8 Aug 2023 21:09:16 +0200 Subject: [PATCH] Remove Freetype dep in header --- include/graphism/render.hpp | 66 +++++++++---------------------------- src/graphism/render.cpp | 34 ++++++++++++++++--- src/graphism/textrender.cpp | 2 +- src/window/window.cpp | 5 ++- 4 files changed, 47 insertions(+), 60 deletions(-) diff --git a/include/graphism/render.hpp b/include/graphism/render.hpp index d95c641..fe7fc25 100644 --- a/include/graphism/render.hpp +++ b/include/graphism/render.hpp @@ -38,44 +38,28 @@ #include #include -#ifdef HAVE_FREETYPE -#include -#include FT_FREETYPE_H -#endif #include "graphism/buffer.hpp" -/// @def RENDERGRAY -/// @brief Macro to render a text in gray mode (antialiasing). -/// @see render(); -#ifdef HAVE_FREETYPE -#define RENDERGRAY (FT_LOAD_RENDER) -#else -#define RENDERGRAY 0 -#endif - -/// @def RENDERMONO -/// @brief Macro to render a text in black and white mode. -/// @see render(); -#ifdef HAVE_FREETYPE -#define RENDERMONO (FT_LOAD_RENDER|FT_LOAD_MONOCHROME) -#else -#define RENDERMONO 1 -#endif +struct FT_LibraryRec_; +struct FT_FaceRec_; +struct FT_GlyphSlotRec_; /// Class that allows to render a string in pixel. class Render { + public: + enum class Mode {Mono,Gray}; private : #ifdef HAVE_FREETYPE - FT_Library _library; ///< handle to library. - FT_Face _face; ///< handle to face object. - FT_GlyphSlot _slot; ///< handle the buffer of the face render. - FT_Int _size; ///< size in pixel of the rendering (height). - FT_Int _pixelRatio; ///< Height/widht of a pixel (can be 2 if in terminal). + FT_LibraryRec_* _library; ///< handle to library. + FT_FaceRec_* _face; ///< handle to face object. + FT_GlyphSlotRec_* _slot; ///< handle the buffer of the face render. #endif + int _size; ///< size in pixel of the rendering (height). + int _pixelRatio; ///< Height/widht of a pixel (can be 2 if in terminal). std::string _fontfile; ///< Font file to use for rendering. - long _renderMode; ///< Render to load : gray or monochrome. + Mode _renderMode; ///< Render to load : gray or monochrome. unsigned char* _temp; ///< Temporary array containing all glyph for on font/size. /// Regenerate the _temp attribut that contains all pre rendered glyph. @@ -96,7 +80,7 @@ class Render { /// @param filename The font use for the rendering. /// @param size The size in pixel for the rendering /// @param renderMode Mode to render the text. - Render(const std::string& filename, const size_t size, const long renderMode = RENDERMONO); + Render(const std::string& filename, const size_t size, const Mode renderMode = Mode::Mono); /// Copy a rendere if needed. /// @param render The render to copy. @@ -133,16 +117,7 @@ class Render { /// Set the ratio height/width of one pixel. /// @param ratio The ratio height/width of one pixel. - void setPixelRatio(const int ratio){ -#ifdef HAVE_FREETYPE - _pixelRatio = ratio; - this->setSize(_size); // Force to regenerate. -#else - Exception e = EXCEPTION("PixelRation not used : no freetype support",ERRWAR); - std::clog << e.fullWhat() << std::endl; - (void) ratio; -#endif - } + void setPixelRatio(const int ratio); /// Increase or decrease the font size. /// @param toadd Number of pixels to add or remove from the font size @@ -150,12 +125,7 @@ class Render { /// Change render mode. /// @param mode New mode to use. - inline void setRender(const long mode) { - if ( (mode != RENDERGRAY) && (mode != RENDERMONO) ) - throw EXCEPTION("Error while setting new render mode",ERRDIV); - _renderMode = mode; - this->regenerate(); - } + void setRender(const Mode mode); /// Render a string to buffer that has a correct size value /// @param string The text to render @@ -173,15 +143,9 @@ class Render { /// Get the font size. /// @return the font size. -#ifdef HAVE_FREETYPE - inline FT_Int getSize() { - return _size; - } -#else inline int getSize() { - return 0; + return _size; } -#endif /// Allow to know if the render is set or not so it can be used. /// @return True if ready, false otherwise. diff --git a/src/graphism/render.cpp b/src/graphism/render.cpp index d245807..f4db450 100644 --- a/src/graphism/render.cpp +++ b/src/graphism/render.cpp @@ -32,6 +32,13 @@ #include #include +#ifdef HAVE_FREETYPE +#include +#include FT_FREETYPE_H +#define RENDERGRAY (FT_LOAD_RENDER) +#define RENDERMONO (FT_LOAD_RENDER|FT_LOAD_MONOCHROME) +#endif + // Render::Render() : @@ -43,7 +50,7 @@ Render::Render() : _pixelRatio(1), #endif _fontfile(""), - _renderMode(RENDERMONO), + _renderMode(Mode::Mono), _temp(nullptr) { #ifdef HAVE_FREETYPE @@ -58,7 +65,7 @@ Render::Render() : // -Render::Render(const std::string& filename, const size_t size, const long renderMode) : +Render::Render(const std::string& filename, const size_t size, const Mode renderMode) : #ifdef HAVE_FREETYPE _library(nullptr), _face(nullptr), @@ -293,6 +300,17 @@ void Render::setSize(const size_t size) { //_slot = _face->glyph; } +// +void Render::setPixelRatio(const int ratio){ +#ifdef HAVE_FREETYPE + _pixelRatio = ratio; + this->setSize(_size); // Force to regenerate. +#else + Exception e = EXCEPTION("PixelRation not used : no freetype support",ERRWAR); + std::clog << e.fullWhat() << std::endl; + (void) ratio; +#endif +} // void Render::setSizeR(const int toadd) { @@ -307,6 +325,12 @@ void Render::setSizeR(const int toadd) { #endif } +void Render::setRender(const Mode mode) { + if ( (mode != Mode::Gray) && (mode != Mode::Mono) ) + throw EXCEPTION("Error while setting new render mode",ERRDIV); + _renderMode = mode; + this->regenerate(); +} // void Render::render(const char* string, BufferRender& buffer, const unsigned char color[]) { @@ -370,7 +394,7 @@ void Render::regenerate() { using namespace std; try { /* load glyph image into the slot (erase previous one) */ - int error = FT_Load_Char( _face, C, _renderMode ); + int error = FT_Load_Char( _face, C, _renderMode==Mode::Mono?RENDERMONO:RENDERGRAY); if ( error ) { std::stringstream message ; message << "Error loading charachter " << C << "."; throw EXCEPTION(message.str(),ERRDIV); @@ -386,7 +410,7 @@ void Render::regenerate() { x_max = start_x + bitmap->width; switch (_renderMode) { - case RENDERMONO : { + case Mode::Mono: { //* for ( FT_Int i = start_y, q = 0; q < min((FT_Int)bitmap->rows,_size); i+=line, ++q ) { // each line for ( FT_Int j = start_x, p = 0; p < bitmap->pitch ; ++p ) { // each byte @@ -398,7 +422,7 @@ void Render::regenerate() { } break; } - case RENDERGRAY : { + case Mode::Gray: { for ( FT_Int i = start_y, q = 0; q < min((FT_Int)bitmap->rows,_size); i+=line, ++q ) for ( FT_Int j = start_x, p = 0; j < x_max; ++j, ++p ) _temp[i+j] |= bitmap->buffer[q * bitmap->width + p]; diff --git a/src/graphism/textrender.cpp b/src/graphism/textrender.cpp index 70f9310..f954962 100644 --- a/src/graphism/textrender.cpp +++ b/src/graphism/textrender.cpp @@ -59,7 +59,7 @@ TextRender::TextRender() : _doRender(false), _isOk(false), _color(), _render(), _buffer(Render::BufferRender(200,200) ) { try { - _render = Render(DEFAULT_FONT,20,RENDERGRAY); + _render = Render(DEFAULT_FONT,20,Render::Mode::Gray); _isOk = true; _doRender = true; _buffer.setType(Buffer::TYPE_TEXT); diff --git a/src/window/window.cpp b/src/window/window.cpp index e0d4360..e3fc3b2 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -163,7 +163,7 @@ Window::Window(pCanvas& canvas, const int width, const int height) _optioni["ntime"] = 0; _optioni["fontSize"] = 20; _render._render.setSize(_optioni["fontSize"]); - _render._render.setRender(RENDERGRAY); + _render._render.setRender(Render::Mode::Gray); #if defined(HAVE_GL) && defined(HAVE_GLEXT) std::clog << "OpenGL might use VBO" << std::endl; @@ -694,8 +694,7 @@ bool Window::userInput(std::stringstream& info) { case 'A': { msaa = !msaa; msaa ? glEnable(GL_MULTISAMPLE) : glDisable(GL_MULTISAMPLE); - msaa ? _render._render.setRender(RENDERGRAY) - : _render._render.setRender(RENDERMONO); + _render._render.setRender(msaa ? Render::Mode::Gray : Render::Mode::Mono); break; } #endif