优化osd绘制代码

serial
BlueMatthew 1 year ago
parent 4de8b2ec79
commit 22fd597163

@ -620,7 +620,7 @@ namespace cv {
// Update current position ( in FreeType coordinates ) // Update current position ( in FreeType coordinates )
float advance = mFace->glyph->advance.x >> 6; float advance = mFace->glyph->advance.x >> 6;
currentPos.x += mFace->glyph->advance.x + mFace->glyph->bitmap_left; currentPos.x += mFace->glyph->advance.x + mFace->glyph->metrics.horiBearingX;
currentPos.y += mFace->glyph->advance.y; currentPos.y += mFace->glyph->advance.y;
// currentPos.x += mFace->glyph->metrics.horiBearingX >> 6; // currentPos.x += mFace->glyph->metrics.horiBearingX >> 6;
// currentPos.y += mFace->glyph->metrics.horiBearingY >> 6; // currentPos.y += mFace->glyph->metrics.horiBearingY >> 6;
@ -628,7 +628,7 @@ namespace cv {
// currentPos.x += mFace->glyph->metrics.horiBearingX; // currentPos.x += mFace->glyph->metrics.horiBearingX;
// currentPos.y += mFace->glyph->metrics.horiBearingY; // currentPos.y += mFace->glyph->metrics.horiBearingY;
// break; // break;
_org.x += (mFace->glyph->advance.x + mFace->glyph->bitmap_left) >> 6; _org.x += (mFace->glyph->advance.x + mFace->glyph->metrics.horiBearingX) >> 6;
_org.y += mFace->glyph->advance.y >> 6; _org.y += mFace->glyph->advance.y >> 6;
} }
@ -657,6 +657,13 @@ namespace cv {
CV_Assert(!FT_Set_Pixel_Sizes(mFace, _fontHeight, _fontHeight)); CV_Assert(!FT_Set_Pixel_Sizes(mFace, _fontHeight, _fontHeight));
FT_UInt glyph_index;
FT_Bool use_kerning;
FT_UInt previous;
use_kerning = FT_HAS_KERNING(mFace);
previous = 0;
FT_Vector currentPos = { 0,0 }; FT_Vector currentPos = { 0,0 };
FT_Set_Transform(mFace, 0, &currentPos); FT_Set_Transform(mFace, 0, &currentPos);
#if defined(USING_HB) #if defined(USING_HB)
@ -681,7 +688,8 @@ namespace cv {
#if defined(USING_HB) #if defined(USING_HB)
for (unsigned int i = 0; i < textLen; i++) { for (unsigned int i = 0; i < textLen; i++) {
CV_Assert(!FT_Load_Glyph(mFace, info[i].codepoint, 0)); glyph_index = info[i].codepoint;
CV_Assert(!FT_Load_Glyph(mFace, glyph_index, 0));
#else #else
for (unsigned int i = 0; i < wstr.size(); i++) { for (unsigned int i = 0; i < wstr.size(); i++) {
if (wstr[i] == '\r' || wstr[i] == '\n') if (wstr[i] == '\r' || wstr[i] == '\n')
@ -692,8 +700,17 @@ namespace cv {
// yMax = cv::max(yMax, currentPos.y + (mFace->glyph->advance.y)); // yMax = cv::max(yMax, currentPos.y + (mFace->glyph->advance.y));
continue; continue;
} }
CV_Assert(!FT_Load_Glyph(mFace, FT_Get_Char_Index(mFace, wstr[i]), 0)); glyph_index = FT_Get_Char_Index(mFace, wstr[i]);
CV_Assert(!FT_Load_Glyph(mFace, glyph_index, 0));
#endif #endif
/* retrieve kerning distance and move pen position */
if (use_kerning && previous)
{
FT_Vector delta;
FT_Get_Kerning(mFace, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
currentPos.x += delta.x >> 6;
}
FT_GlyphSlot slot = mFace->glyph; FT_GlyphSlot slot = mFace->glyph;
FT_Outline outline = slot->outline; FT_Outline outline = slot->outline;
FT_BBox bbox; FT_BBox bbox;
@ -716,7 +733,7 @@ namespace cv {
) )
{ {
bbox.xMin = currentPos.x; bbox.xMin = currentPos.x;
bbox.xMax = currentPos.x + (mFace->glyph->advance.x); bbox.xMax = currentPos.x + (mFace->glyph->advance.x + mFace->glyph->bitmap_left);
bbox.yMin = yMin; bbox.yMin = yMin;
bbox.yMax = yMax; bbox.yMax = yMax;
} }
@ -730,6 +747,8 @@ namespace cv {
xMax = cv::max(xMax, ftd(bbox.xMax)); xMax = cv::max(xMax, ftd(bbox.xMax));
yMin = cv::min(yMin, ftd(bbox.yMin)); yMin = cv::min(yMin, ftd(bbox.yMin));
yMax = cv::max(yMax, ftd(bbox.yMax)); yMax = cv::max(yMax, ftd(bbox.yMax));
previous = glyph_index;
} }
#if defined(USING_HB) #if defined(USING_HB)
@ -756,6 +775,7 @@ namespace cv {
return Size(width, height); return Size(width, height);
} }
void FreeType2Impl::putPixel_8UC1_mono(Mat& _dst, const int _py, const int _px, const uint8_t *_col) void FreeType2Impl::putPixel_8UC1_mono(Mat& _dst, const int _py, const int _px, const uint8_t *_col)
{ {
uint8_t* ptr = _dst.ptr<uint8_t>(_py, _px); uint8_t* ptr = _dst.ptr<uint8_t>(_py, _px);
@ -1038,6 +1058,7 @@ namespace cv {
#endif #endif
} }
int FreeType2Impl::mvFn(const FT_Vector *to, void * user) int FreeType2Impl::mvFn(const FT_Vector *to, void * user)
{ {
if (user == NULL) { return 1; } if (user == NULL) { return 1; }

@ -942,16 +942,6 @@ void DrawOutlineText(cv::Ptr<cv::ft::FreeType2> ft2, cv::Mat& mat, const std::st
lineHeight = std::max(fontSize, textSize.height); lineHeight = std::max(fontSize, textSize.height);
ft2->putText(mat, *it, pt, fontSize, clr, thickness, cv::LINE_AA, false, true); ft2->putText(mat, *it, pt, fontSize, clr, thickness, cv::LINE_AA, false, true);
#ifdef _DEBUG
if (pt.y > (mat.rows / 2))
{
ft2->putText(mat, *it, cv::Point(pt.x, pt.y - fontSize * 3), fontSize, clr, thickness, cv::LINE_AA, false);
}
else
{
ft2->putText(mat, *it, cv::Point(pt.x, pt.y + fontSize * 3), fontSize, clr, thickness, cv::LINE_AA, false);
}
#endif
pt.x = startPoint.x; pt.x = startPoint.x;
pt.y += lineHeight + (lineHeight >> 2); // 125% pt.y += lineHeight + (lineHeight >> 2); // 125%
@ -1123,8 +1113,8 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat)
pt.y = height - it->y * ratio - textSize.height; pt.y = height - it->y * ratio - textSize.height;
} }
cv::Rect rc(pt.x, pt.y, textSize.width, textSize.height); // cv::Rect rc(pt.x, pt.y, textSize.width, textSize.height);
cv::rectangle(mat, rc, cv::Scalar(0,255,255), 2); // cv::rectangle(mat, rc, cv::Scalar(0,255,255), 2);
DrawOutlineText(ft2, mat, it->text, pt, fontSize, scalar, thickness); DrawOutlineText(ft2, mat, it->text, pt, fontSize, scalar, thickness);
} }

Loading…
Cancel
Save