Search code examples
c++opengl-esfreetype2

FreeType2 add outline to text


I'm trying to add border/outline to text's drawn using freetype. So far I've found freetype's docs about it but they are a bit inconsistent. Some functions/structs are visible on my project but some are not. For example I can create an outline object like this FT_Outline border; but I can't call the function I need to call in order to apply that outline like the docs says me to do like this: FT_Outline_New( FT_Library library, FT_UInt numPoints, FT_Int numContours, FT_Outline *anoutline );. This question is of no help either, almost all of the functions/structs are not present on my setup.

I'm a bit lost, how am I supposed to implement outlines to my text? Are these methods outdated if so what is the modern method? Am I linking/importing/compiling freetype wrong? I have no errors with freetype whatsoever so I don't think there are any errors with my freetype implementation.

Here is how my text-rendering process looks like:

Text.cpp


Text::Text(text_attributes&& atrib, int gl_width, int gl_height) 
{
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    shader = Shader("./src/opengl/shaders/text.vs", "./src/opengl/shaders/text.fs");

    glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(SCR_WIDTH), 0.0f, static_cast<float>(SCR_HEIGHT));
    shader.use();
    glUniformMatrix4fv(glGetUniformLocation(shader.ID, "projection"), 1, GL_FALSE, glm::value_ptr(projection));

    if (FT_Init_FreeType(&ft))
    {
        exit(0);
    }

    std::string font_name = "./src/opengl/fonts/" + *atrib.__font_name +  ".ttf";
    
    FT_Face face;
    if (FT_New_Face(ft, font_name.c_str(), 0, &face)) {
        VI_ERROR("ERROR::FREETYPE: Failed to load font");
        exit(0) ;
    } else {
        FT_Outline border;
        
        FT_Set_Pixel_Sizes(face, 0, atrib.__font_size);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

        for (unsigned char c = 0; c < 128; c++)
        {
            // Load character glyph 
            if (FT_Load_Char(face, c, FT_LOAD_RENDER))
            {
                VI_ERROR("ERROR::FREETYTPE: Failed to load Glyph");
                continue;
            }

            // generate texture
            GLuint texture;
            glGenTextures(1, &texture);
            glBindTexture(GL_TEXTURE_2D, texture);
            glTexImage2D(
                GL_TEXTURE_2D,
                0,
                GL_RED,
                face->glyph->bitmap.width,
                face->glyph->bitmap.rows,
                0,
                GL_RED,
                GL_UNSIGNED_BYTE,
                face->glyph->bitmap.buffer
            );
            // set texture options
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            // now store character for later use
            Character character = {
                texture,
                glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
                glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
                static_cast<unsigned int>(face->glyph->advance.x)
            };
            Characters.insert(std::pair<char, Character>(c, character));
        }
        glBindTexture(GL_TEXTURE_2D, 0);
    }
    FT_Done_Face(face);
    FT_Done_FreeType(ft);

    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}
void Text::render_text(std::string text, float x, float y, float z, std::string hex_color, float angle_rad, bool bg)
{   
    static const int scale = 1;

    shader.use();
    glUniform3f(glGetUniformLocation(shader.ID, "textColor"), 0.1, 0.1, 0.1);//color.get_color_float(Utils::RED), color.get_color_float(Utils::GREEN), color.get_color_float(Utils::BLUE));
    glActiveTexture(GL_TEXTURE0);
    glBindVertexArray(VAO);

    GLfloat vertices[6][4] = {
        { 0.0,  1.0,   0.0, 0.0 },
        { 0.0,  0.0,   0.0, 1.0 },
        { 1.0,  0.0,   1.0, 1.0 },

        { 0.0,  1.0,   0.0, 0.0 },
        { 1.0,  0.0,   1.0, 1.0 },
        { 1.0,  1.0,   1.0, 0.0 }
    };

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); // Be sure to use glBufferSubData and not glBufferData
    glBindBuffer(GL_ARRAY_BUFFER, 0);


    glm::mat4 rotateM = glm::rotate(glm::mat4(1.0f), glm::radians(angle_rad), glm::vec3(0.0f, 0.0f, 1.0f));
    glm::mat4 transOriginM = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z));

    std::string::const_iterator c;
    GLfloat char_x = 0.0f;
    for (c = text.begin(); c != text.end(); c++)
    {
        Character ch = Characters[*c];

        GLfloat w = ch.Size.x * scale;
        GLfloat h = ch.Size.y * scale;
        GLfloat xrel = char_x + ch.Bearing.x * scale;
        GLfloat yrel = y - (ch.Size.y - ch.Bearing.y) * scale;

        char_x += (ch.Advance >> 6) * scale; // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount of 1/64th pixels by 64 to get amount of pixels))

        glm::mat4 scaleM = glm::scale(glm::mat4(1.0f), glm::vec3(w, h, 1.0f));
        glm::mat4 transRelM = glm::translate(glm::mat4(1.0f), glm::vec3(xrel, yrel, z));

        glm::mat4 modelM = transOriginM * rotateM * transRelM * scaleM;

        GLint model_loc = glGetUniformLocation(shader.ID, "model");
        glUniformMatrix4fv(model_loc, 1, GL_FALSE, glm::value_ptr(modelM));

        glBindTexture(GL_TEXTURE_2D, ch.TextureID);

        glDrawArrays(GL_TRIANGLES, 0, 6);
    }
    glBindVertexArray(0);
    glBindTexture(GL_TEXTURE_2D, 0);
}

Text.h

extern "C"{
    #include <ft2build.h>
    #include FT_FREETYPE_H
}

class Text{
   Text(text_attributes&& atrib, int w, int h);
   render_text(std::string text, float x, float y, float z, std::string hex_color, float angle_rad, bool bg);
};


Solution

  • You should also include:

    #include FT_GLYPH_H   //optional glyph management component
    #include FT_OUTLINE_H //scalable outline management
    #include FT_STROKER_H //functions to stroke outline paths
    

    See also:


    If you want to render an outline (in the specified order):

    • FT_Stroker_New
    • FT_Stroker_Set
    • FT_Get_Glyph
    • FT_Glyph_Stroke
    • if glyph->format == FT_GLYPH_FORMAT_OUTLINE
      • FT_Raster_Params params
        • params.flags = FT_RASTER_FLAG_AA | FT_RASTER_FLAG_DIRECT
        • params.gray_spans = outlineRenderCallback
        • params.user = user_data
      • FT_Outline_Render