freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ft] Problem on render 'j' on non-monospace font face.


From: 昌霖
Subject: [ft] Problem on render 'j' on non-monospace font face.
Date: Thu, 6 Mar 2014 14:03:24 +0800

Hi everyone,

  I am new for freetype library, and trying to following the tutorial to render text into opencv mat.
  And I have some problem on non-monospace font face like Ubuntu Regular.
  Following is my testing code and it can also be downloaded from my dropbox page.
  https://www.dropbox.com/sh/qp93w5fzcu9hbcq/VSABZHiNV1
  That page include my testing code, render output, and test font face.
  You can compile it by using the following command:
  "g++ -o test_ubuntu_regular test_freetype.cpp `pkg-config opencv --cflags --libs` -I/usr/include/freetype2 -lfreetype"
  "g++ -o test_ubuntu_regular_mono test_freetype.cpp `pkg-config opencv --cflags --libs` -I/usr/include/freetype2 -lfreetype -D_USE_MONO_"
  I also try to use kerning to solve the problem but the delta vector always return (0,0).

Thanks for yours kindly help.
Changlin

==========================================================================================
#include <string>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <ft2build.h>
#include FT_FREETYPE_H

using namespace std;
using namespace cv;

int main(int arch, char* argv[])
{
    ///Setup test font, font size, and string
#ifndef _USE_MONO_
    string test_font = "Ubuntu-R.ttf";
#else
    string test_font = "UbuntuMono-R.ttf";
#endif
    string test_string = "jjj abcdefghijklmnopqrstuvwxyz jjj";
    int height = 32;

    FT_UInt status;
    ///Initialize library
    FT_Library library;
    status = FT_Init_FreeType(&library);
    if (status)
    {
        cout<<"Freetype library initialization failed"<<endl;
        return 1;
    }
    ///Load font face
    FT_Face face;
    status = FT_New_Face(library, test_font.c_str(), 0, &face);
    if (status)
    {
        cout<<"Freetype load font face failed"<<endl;
        return 1;
    }
    ///Set font size
    status = FT_Set_Pixel_Sizes(face, 0, height);
    if (status)
    {
        cout<<"Freetype set size failed"<<endl;
        return 1;
    }
    ///Render to text_image
    Mat text_image;
    int max_height=0;
    FT_GlyphSlot slot=face->glyph;
    for (size_t t=0; t<test_string.size(); t++)
    {
        ///Load and render character
        status = FT_Load_Char(face, test_string[t], FT_LOAD_RENDER );
        if (status)
        {
            cout<<"Freetype load char \""<<test_string[t]<<"\" failed"<<endl;
            continue;
        }
        ///Compute max height of character image for concat them
        max_height = max(max_height, height-slot->bitmap_top+slot->bitmap.rows);
        ///Create character image
        Mat char_image=Mat::zeros(max_height, slot->advance.x/64, CV_8UC1);
        for (int y=height-slot->bitmap_top, j=0; j<slot->bitmap.rows; y++, j++)
        {
            uchar* scanline = char_image.ptr<uchar>(y);
            for (int x=slot->bitmap_left, i=0; i<slot->bitmap.width; x++, i++)
            {
                if (x>=char_image.cols)
                {
                    continue;
                }
                scanline[x] |= slot->bitmap.buffer[j*slot->bitmap.pitch+i];
            }
        }
        ///Concat character image to text image
        if (text_image.empty())
        {
            text_image=char_image;
        }
        else
        {
            if (max_height>text_image.rows)
            {
                copyMakeBorder(text_image, text_image, 0, max_height-text_image.rows, 0, 0, BORDER_CONSTANT, Scalar(0));
            }
            hconcat(text_image, char_image, text_image);
        }
    }

#ifndef _USE_MONO_
    imwrite("ubuntu-regular.png", text_image);
#else
    imwrite("ubuntu-regular-mono.png", text_image);
#endif
}
==============================================================================================

Attachment: ubuntu-regular.png
Description: PNG image

Attachment: ubuntu-regular-mono.png
Description: PNG image


reply via email to

[Prev in Thread] Current Thread [Next in Thread]