Skip to content

Commit 9b32dd9

Browse files
committed
fix for review
1 parent 9e71621 commit 9b32dd9

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

modules/freetype/include/opencv2/freetype.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class CV_EXPORTS_W FreeType2 : public Algorithm
7676
public:
7777
/** @brief Load font data.
7878
79-
The function loadFontData loads font data.
79+
The function loadFontData loads font data from file.
8080
8181
@param fontFileName FontFile Name
8282
@param idx face_index to select a font faces in a single file.
@@ -86,7 +86,7 @@ The function loadFontData loads font data.
8686

8787
/** @brief Load font data.
8888
89-
The function loadFontData loads font data.
89+
The function loadFontData loads font data from memory.
9090
The data is not copied, the user needs to make sure the data lives at least as long as FreeType2.
9191
After the FreeType2 object is destroyed, the buffer can be safely deallocated.
9292
@@ -95,7 +95,7 @@ After the FreeType2 object is destroyed, the buffer can be safely deallocated.
9595
@param idx face_index to select a font faces in a single file.
9696
*/
9797

98-
CV_WRAP virtual void loadFontData(uchar* pBuf, size_t bufSize, int idx) = 0;
98+
CV_WRAP virtual void loadFontData(char* pBuf, size_t bufSize, int idx) = 0;
9999

100100
/** @brief Set Split Number from Bezier-curve to line
101101

modules/freetype/src/freetype.cpp

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +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;
70+
void loadFontData(char* pBuf, size_t bufSize, int idx) CV_OVERRIDE;
7171
void setSplitNumber( int num ) CV_OVERRIDE;
7272
void putText(
7373
InputOutputArray img, const String& text, Point org,
@@ -88,6 +88,8 @@ class CV_EXPORTS_W FreeType2Impl CV_FINAL : public FreeType2
8888
int mCtoL;
8989
hb_font_t *mHb_font;
9090

91+
void loadFontData(FT_Open_Args &args, int idx);
92+
9193
void putTextBitmapMono(
9294
InputOutputArray img, const String& text, Point org,
9395
int fontHeight, Scalar color,
@@ -181,27 +183,41 @@ FreeType2Impl::~FreeType2Impl()
181183

182184
void FreeType2Impl::loadFontData(String fontFileName, int idx)
183185
{
184-
CV_Assert( idx >= 0 );
185-
if ( mIsFaceAvailable == true )
186+
FT_Open_Args args
186187
{
187-
hb_font_destroy(mHb_font);
188-
CV_Assert(!FT_Done_Face(mFace));
189-
}
188+
FT_OPEN_PATHNAME,
189+
nullptr, // memory_base
190+
0, // memory_size
191+
const_cast<FT_String*>(fontFileName.c_str()),
192+
nullptr, // stream
193+
nullptr, // driver
194+
0, // num_params
195+
nullptr // params
196+
};
190197

191-
mIsFaceAvailable = false;
192-
CV_Assert( !FT_New_Face( mLibrary, fontFileName.c_str(), static_cast<FT_Long>(idx), &mFace ) );
198+
this->loadFontData(args, idx);
199+
}
193200

194-
mHb_font = hb_ft_font_create(mFace, NULL);
195-
if ( mHb_font == NULL )
201+
void FreeType2Impl::loadFontData(char* pBuf, size_t bufSize, int idx)
202+
{
203+
CV_Assert( pBuf != nullptr );
204+
205+
FT_Open_Args args
196206
{
197-
CV_Assert(!FT_Done_Face(mFace));
198-
return;
199-
}
200-
CV_Assert( mHb_font != NULL );
201-
mIsFaceAvailable = true;
207+
FT_OPEN_MEMORY,
208+
reinterpret_cast<FT_Byte*>(pBuf),
209+
static_cast<FT_Long>(bufSize),
210+
nullptr, // pathname
211+
nullptr, // stream
212+
nullptr, // driver
213+
0, // num_params
214+
nullptr // params
215+
};
216+
217+
this->loadFontData(args, idx);
202218
}
203219

204-
void FreeType2Impl::loadFontData(uchar* pBuf, size_t bufSize, int idx)
220+
void FreeType2Impl::loadFontData(FT_Open_Args &args, int idx)
205221
{
206222
CV_Assert( idx >= 0 );
207223
if ( mIsFaceAvailable == true )
@@ -211,7 +227,6 @@ void FreeType2Impl::loadFontData(uchar* pBuf, size_t bufSize, int idx)
211227
}
212228

213229
mIsFaceAvailable = false;
214-
FT_Open_Args args{ FT_OPEN_MEMORY, (FT_Byte*)pBuf, static_cast<FT_Long>(bufSize), nullptr, nullptr, nullptr, 0, nullptr };
215230
CV_Assert( !FT_Open_Face(mLibrary, &args, idx, &mFace) );
216231

217232
mHb_font = hb_ft_font_create(mFace, NULL);

modules/freetype/test/test_basic.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ TEST(Freetype_Basic, in_memory_font )
7474
const size_t file_size = ftell(fp);
7575
fseek(fp, 0, SEEK_SET);
7676

77-
std::vector<uchar> font_buffer(file_size);
77+
std::vector<char> font_buffer(file_size);
7878
const size_t actual_read = fread(&font_buffer[0], 1, file_size, fp);
7979
fclose(fp);
8080
ASSERT_EQ(file_size, actual_read);
@@ -138,6 +138,37 @@ TEST(Freetype_loadFontData, call_multiple)
138138
EXPECT_NO_THROW( ft2->putText(dst, "call_mutilple", Point( 0, 50), 50, col, -1, LINE_AA, true ) );
139139
}
140140

141+
TEST(Freetype_loadFontDataMemory, nullptr )
142+
{
143+
cv::Ptr<cv::freetype::FreeType2> ft2;
144+
EXPECT_NO_THROW( ft2 = cv::freetype::createFreeType2() );
145+
EXPECT_ANY_THROW( ft2->loadFontData( nullptr, 0, 0 ) );
146+
}
147+
148+
TEST(Freetype_loadFontDataMemory, broken_data )
149+
{
150+
const string root = cvtest::TS::ptr()->get_data_path();
151+
const string font_path = root + "freetype/mplus/Mplus1-Regular.ttf";
152+
153+
FILE* fp = fopen(font_path.c_str(), "rb");
154+
ASSERT_TRUE(fp != NULL);
155+
fseek(fp, 0, SEEK_END);
156+
const size_t file_size = ftell(fp);
157+
fseek(fp, 0, SEEK_SET);
158+
159+
std::vector<char> font_buffer(file_size);
160+
const size_t actual_read = fread(&font_buffer[0], 1, file_size, fp);
161+
fclose(fp);
162+
ASSERT_EQ(file_size, actual_read);
163+
164+
cv::Ptr<cv::freetype::FreeType2> ft2_in_memory;
165+
EXPECT_NO_THROW( ft2_in_memory = cv::freetype::createFreeType2() );
166+
167+
font_buffer[0] = ~font_buffer[0]; // font buffer was broken.
168+
169+
EXPECT_ANY_THROW( ft2_in_memory->loadFontData( &font_buffer[0], file_size, 0 ) );
170+
}
171+
141172
typedef testing::TestWithParam<int> idx_range;
142173

143174
TEST_P(idx_range, failed )

0 commit comments

Comments
 (0)