|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
#include "ft_render.hpp"
|
|
|
|
|
|
#ifdef HAVE_FREETYPE
|
|
|
|
|
|
#include "ft_render_priv.hpp"
|
|
|
|
|
|
#include <opencv2/gapi/util/throw.hpp>
|
|
|
#include <opencv2/gapi/own/assert.hpp>
|
|
|
|
|
|
cv::gapi::wip::draw::FTTextRender::Priv::Priv(const std::string& path)
|
|
|
{
|
|
|
if (FT_Init_FreeType(&m_library) != 0)
|
|
|
{
|
|
|
cv::util::throw_error(std::runtime_error("Failed to initialize FT"));
|
|
|
}
|
|
|
|
|
|
if (FT_New_Face(m_library, path.c_str(), 0, &m_face))
|
|
|
{
|
|
|
FT_Done_FreeType(m_library);
|
|
|
cv::util::throw_error(std::runtime_error("Failed to create a font face"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
cv::Size cv::gapi::wip::draw::FTTextRender::Priv::getTextSize(const std::wstring& text, int fh, int* baseline)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GAPI_Assert(!FT_Set_Pixel_Sizes(m_face, fh, fh) &&
|
|
|
"Failed to set pixel size");
|
|
|
|
|
|
cv::Point pen(0, 0);
|
|
|
|
|
|
int max_bot = 0;
|
|
|
int max_top = 0;
|
|
|
int last_advance = 0;
|
|
|
int last_width = 0;
|
|
|
int last_left = 0;
|
|
|
|
|
|
for (const auto& wc : text)
|
|
|
{
|
|
|
GAPI_Assert(!FT_Load_Char(m_face, wc, FT_LOAD_RENDER) &&
|
|
|
"Failed to load char");
|
|
|
|
|
|
FT_Bitmap *bitmap = &(m_face->glyph->bitmap);
|
|
|
|
|
|
int left = m_face->glyph->bitmap_left;
|
|
|
int advance = (m_face->glyph->advance.x >> 6);
|
|
|
int width = bitmap->width;
|
|
|
|
|
|
|
|
|
if (pen.x + left < 0)
|
|
|
{
|
|
|
left = 0;
|
|
|
}
|
|
|
|
|
|
int bot = (m_face->glyph->metrics.height - m_face->glyph->metrics.horiBearingY) >> 6;
|
|
|
max_bot = std::max(max_bot, bot);
|
|
|
max_top = std::max(max_top, m_face->glyph->bitmap_top);
|
|
|
|
|
|
|
|
|
if (width != 0)
|
|
|
{
|
|
|
last_width = width;
|
|
|
last_advance = advance;
|
|
|
last_left = left;
|
|
|
}
|
|
|
|
|
|
pen.x += advance;
|
|
|
}
|
|
|
|
|
|
|
|
|
int diff = (last_width + last_left) - last_advance;
|
|
|
pen.x += (diff > 0) ? diff : 0;
|
|
|
|
|
|
if (baseline)
|
|
|
{
|
|
|
*baseline = max_bot;
|
|
|
}
|
|
|
|
|
|
return {pen.x, max_bot + max_top};
|
|
|
}
|
|
|
|
|
|
void cv::gapi::wip::draw::FTTextRender::Priv::putText(cv::Mat& mat,
|
|
|
const std::wstring& text,
|
|
|
const cv::Point& org,
|
|
|
int fh)
|
|
|
{
|
|
|
GAPI_Assert(!FT_Set_Pixel_Sizes(m_face, fh, fh) &&
|
|
|
"Failed to set pixel size");
|
|
|
|
|
|
cv::Point pen = org;
|
|
|
for (const auto& wc : text)
|
|
|
{
|
|
|
GAPI_Assert(!FT_Load_Char(m_face, wc, FT_LOAD_RENDER) &&
|
|
|
"Failed to load char");
|
|
|
FT_Bitmap *bitmap = &(m_face->glyph->bitmap);
|
|
|
|
|
|
|
|
|
if (bitmap->rows == 0 || bitmap->width == 0) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
cv::Mat glyph(bitmap->rows, bitmap->width, CV_8UC1, bitmap->buffer, bitmap->pitch);
|
|
|
|
|
|
int left = m_face->glyph->bitmap_left;
|
|
|
int top = m_face->glyph->bitmap_top;
|
|
|
int advance = (m_face->glyph->advance.x >> 6);
|
|
|
|
|
|
if (pen.x + left < 0)
|
|
|
{
|
|
|
left = 0;
|
|
|
}
|
|
|
|
|
|
cv::Rect rect(pen.x + left, org.y - top, glyph.cols, glyph.rows);
|
|
|
|
|
|
auto roi = mat(rect);
|
|
|
roi += glyph;
|
|
|
pen.x += advance;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
cv::gapi::wip::draw::FTTextRender::Priv::~Priv()
|
|
|
{
|
|
|
FT_Done_Face(m_face);
|
|
|
FT_Done_FreeType(m_library);
|
|
|
}
|
|
|
|
|
|
cv::gapi::wip::draw::FTTextRender::FTTextRender(const std::string& path)
|
|
|
: m_priv(new Priv(path))
|
|
|
{
|
|
|
}
|
|
|
|
|
|
cv::Size cv::gapi::wip::draw::FTTextRender::getTextSize(const std::wstring& text,
|
|
|
int fh,
|
|
|
int* baseline)
|
|
|
{
|
|
|
return m_priv->getTextSize(text, fh, baseline);
|
|
|
}
|
|
|
|
|
|
void cv::gapi::wip::draw::FTTextRender::putText(cv::Mat& mat,
|
|
|
const std::wstring& text,
|
|
|
const cv::Point& org,
|
|
|
int fh)
|
|
|
{
|
|
|
m_priv->putText(mat, text, org, fh);
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
|
|
|
cv::Size cv::gapi::wip::draw::FTTextRender::getTextSize(const std::wstring&, int, int*)
|
|
|
{
|
|
|
cv::util::throw_error(std::runtime_error("Freetype not found"));
|
|
|
}
|
|
|
|
|
|
void cv::gapi::wip::draw::FTTextRender::putText(cv::Mat&, const std::wstring&, const cv::Point&, int)
|
|
|
{
|
|
|
cv::util::throw_error(std::runtime_error("Freetype not found"));
|
|
|
}
|
|
|
|
|
|
cv::gapi::wip::draw::FTTextRender::FTTextRender(const std::string&)
|
|
|
{
|
|
|
cv::util::throw_error(std::runtime_error("Freetype not found"));
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|