getTextSize method
Calculates the width and height of a text string.
The function getTextSize calculates and returns the approximate size of a box that contains the specified text. That is, the following code renders some text, the tight box surrounding it, and the baseline: :
String text = "Funny text inside the box";
int fontHeight = 60;
int thickness = -1;
int linestyle = LINE_8;
Mat img(600, 800, CV_8UC3, Scalar::all(0));
int baseline=0;
cv::Ptr<cv::freetype::FreeType2> ft2;
ft2 = cv::freetype::createFreeType2();
ft2->loadFontData( "./mplus-1p-regular.ttf", 0 );
Size textSize = ft2->getTextSize(text,
fontHeight,
thickness,
&baseline);
if(thickness > 0){
baseline += thickness;
}
// center the text
Point textOrg((img.cols - textSize.width) / 2,
(img.rows + textSize.height) / 2);
// draw the box
rectangle(img, textOrg + Point(0, baseline),
textOrg + Point(textSize.width, -textSize.height),
Scalar(0,255,0),1,8);
// ... and the baseline first
line(img, textOrg + Point(0, thickness),
textOrg + Point(textSize.width, thickness),
Scalar(0, 0, 255),1,8);
// then put the text itself
ft2->putText(img, text, textOrg, fontHeight,
Scalar::all(255), thickness, linestyle, true );
text
Input text string.
fontHeight
Drawing font size by pixel unit.
thickness
Thickness of lines used to render the text. See putText for details.
Return:
- Size size, The size of a box that contains the specified text.
- int baseLine y-coordinate of the baseline relative to the bottom-most text point.
Also see putText
Implementation
(Size size, int baseline) getTextSize(String text, int fontHeight, int thickness) {
final pBaseline = calloc<ffi.Int>();
final pSize = calloc<cvg.CvSize>();
final textPtr = text.toNativeUtf8().cast<ffi.Char>();
cvRun(
() => ccontrib.cv_freetype_FreeType2_getTextSize(
ref,
textPtr,
fontHeight,
thickness,
pBaseline,
pSize,
ffi.nullptr,
),
);
final rval = (Size.fromPointer(pSize), pBaseline.value);
calloc.free(pBaseline);
return rval;
}