@@ -55,6 +55,39 @@ TEST(Freetype_Basic, success )
5555 EXPECT_NO_THROW ( ft2->putText (dst, " Basic,success" , Point ( 0 , 50 ), 50 , col, -1 , LINE_AA, true ) );
5656}
5757
58+ TEST (Freetype_Basic, in_memory_font )
59+ {
60+ const string root = cvtest::TS::ptr ()->get_data_path ();
61+ const string font_path = root + " freetype/mplus/Mplus1-Regular.ttf" ;
62+
63+ cv::Ptr<cv::freetype::FreeType2> ft2;
64+ EXPECT_NO_THROW ( ft2 = cv::freetype::createFreeType2 () );
65+ EXPECT_NO_THROW ( ft2->loadFontData ( font_path, 0 ) );
66+
67+ Mat dst (600 ,600 , CV_8UC3, Scalar::all (255 ) );
68+ Scalar col (128 ,64 ,255 ,192 );
69+ EXPECT_NO_THROW ( ft2->putText (dst, " Basic,success" , Point ( 0 , 50 ), 50 , col, -1 , LINE_AA, true ) );
70+
71+ FILE* fp = fopen (font_path.c_str (), " rb" );
72+ ASSERT_TRUE (fp != NULL );
73+ fseek (fp, 0 , SEEK_END);
74+ const size_t file_size = ftell (fp);
75+ fseek (fp, 0 , SEEK_SET);
76+
77+ std::vector<char > font_buffer (file_size);
78+ const size_t actual_read = fread (&font_buffer[0 ], 1 , file_size, fp);
79+ fclose (fp);
80+ ASSERT_EQ (file_size, actual_read);
81+
82+ cv::Ptr<cv::freetype::FreeType2> ft2_in_memory;
83+ EXPECT_NO_THROW ( ft2_in_memory = cv::freetype::createFreeType2 () );
84+ EXPECT_NO_THROW ( ft2_in_memory->loadFontData ( &font_buffer[0 ], file_size, 0 ) );
85+ Mat dst_in_memory (600 ,600 , CV_8UC3, Scalar::all (255 ) );
86+ EXPECT_NO_THROW ( ft2_in_memory->putText (dst_in_memory, " Basic,success" , Point ( 0 , 50 ), 50 , col, -1 , LINE_AA, true ) );
87+
88+ EXPECT_PRED_FORMAT2 (cvtest::MatComparator (0 , 0 ), dst, dst_in_memory);
89+ }
90+
5891/* *****************
5992 * loadFontData()
6093 *****************/
@@ -105,6 +138,37 @@ TEST(Freetype_loadFontData, call_multiple)
105138 EXPECT_NO_THROW ( ft2->putText (dst, " call_mutilple" , Point ( 0 , 50 ), 50 , col, -1 , LINE_AA, true ) );
106139}
107140
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+
108172typedef testing::TestWithParam<int > idx_range;
109173
110174TEST_P (idx_range, failed )
0 commit comments