Skip to content

Commit b2bb3d6

Browse files
committed
add function to load font from buffer
1 parent d51add3 commit b2bb3d6

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

modules/freetype/include/opencv2/freetype.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ The function loadFontData loads font data.
8484

8585
CV_WRAP virtual void loadFontData(String fontFileName, int idx) = 0;
8686

87+
/** @brief Load font data.
88+
89+
The function loadFontData loads font data.
90+
The data is not copied, the user needs to make sure the data lives at least as long as FreeType2.
91+
After the FreeType2 object is destroyed, the buffer can be safely deallocated.
92+
93+
@param pBuf pointer to buffer containing font data
94+
@param bufSize size of buffer
95+
@param idx face_index to select a font faces in a single file.
96+
*/
97+
98+
CV_WRAP virtual void loadFontData(uchar* pBuf, size_t bufSize, int idx) = 0;
99+
87100
/** @brief Set Split Number from Bezier-curve to line
88101
89102
The function setSplitNumber set the number of split points from bezier-curve to line.

modules/freetype/src/freetype.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class CV_EXPORTS_W FreeType2Impl CV_FINAL : public FreeType2
6767
FreeType2Impl();
6868
~FreeType2Impl();
6969
void loadFontData(String fontFileName, int idx) CV_OVERRIDE;
70+
void loadFontData(uchar* pBuf, size_t bufSize, int idx) CV_OVERRIDE;
7071
void setSplitNumber( int num ) CV_OVERRIDE;
7172
void putText(
7273
InputOutputArray img, const String& text, Point org,
@@ -181,16 +182,39 @@ FreeType2Impl::~FreeType2Impl()
181182
void FreeType2Impl::loadFontData(String fontFileName, int idx)
182183
{
183184
CV_Assert( idx >= 0 );
184-
if( mIsFaceAvailable == true )
185+
if ( mIsFaceAvailable == true )
185186
{
186-
hb_font_destroy (mHb_font);
187+
hb_font_destroy(mHb_font);
188+
CV_Assert(!FT_Done_Face(mFace));
189+
}
190+
191+
mIsFaceAvailable = false;
192+
CV_Assert( !FT_New_Face( mLibrary, fontFileName.c_str(), static_cast<FT_Long>(idx), &mFace ) );
193+
194+
mHb_font = hb_ft_font_create(mFace, NULL);
195+
if ( mHb_font == NULL )
196+
{
197+
CV_Assert(!FT_Done_Face(mFace));
198+
return;
199+
}
200+
CV_Assert( mHb_font != NULL );
201+
mIsFaceAvailable = true;
202+
}
203+
204+
void FreeType2Impl::loadFontData(uchar* pBuf, size_t bufSize, int idx)
205+
{
206+
CV_Assert( idx >= 0 );
207+
if ( mIsFaceAvailable == true )
208+
{
209+
hb_font_destroy(mHb_font);
187210
CV_Assert(!FT_Done_Face(mFace));
188211
}
189212

190213
mIsFaceAvailable = false;
191-
CV_Assert( !FT_New_Face( mLibrary, fontFileName.c_str(), static_cast<FT_Long>(idx), &(mFace) ) );
214+
FT_Open_Args args{ FT_OPEN_MEMORY, (FT_Byte*)pBuf, static_cast<FT_Long>(bufSize), nullptr, nullptr, nullptr, 0, nullptr };
215+
CV_Assert( !FT_Open_Face(mLibrary, &args, idx, &mFace) );
192216

193-
mHb_font = hb_ft_font_create (mFace, NULL);
217+
mHb_font = hb_ft_font_create(mFace, NULL);
194218
if ( mHb_font == NULL )
195219
{
196220
CV_Assert(!FT_Done_Face(mFace));

0 commit comments

Comments
 (0)