Skip to content

Commit

Permalink
Remove Freetype dep in header
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Bieder committed Aug 8, 2023
1 parent 4a14528 commit 5e80fff
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 60 deletions.
66 changes: 15 additions & 51 deletions include/graphism/render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,28 @@

#include <cstddef>
#include <string>
#ifdef HAVE_FREETYPE
#include <ft2build.h>
#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.
Expand All @@ -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.
Expand Down Expand Up @@ -133,29 +117,15 @@ 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
void setSizeR(const int toadd);

/// 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
Expand All @@ -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.
Expand Down
34 changes: 29 additions & 5 deletions src/graphism/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
#include <cmath>
#include <bitset>

#ifdef HAVE_FREETYPE
#include <ft2build.h>
#include FT_FREETYPE_H
#define RENDERGRAY (FT_LOAD_RENDER)
#define RENDERMONO (FT_LOAD_RENDER|FT_LOAD_MONOCHROME)
#endif


//
Render::Render() :
Expand All @@ -43,7 +50,7 @@ Render::Render() :
_pixelRatio(1),
#endif
_fontfile(""),
_renderMode(RENDERMONO),
_renderMode(Mode::Mono),
_temp(nullptr)
{
#ifdef HAVE_FREETYPE
Expand All @@ -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),
Expand Down Expand Up @@ -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) {
Expand All @@ -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[]) {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/graphism/textrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned int>::TYPE_TEXT);
Expand Down
5 changes: 2 additions & 3 deletions src/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5e80fff

Please sign in to comment.