13#include <pango/pango.h>
18#include "gmock/gmock-matchers.h"
20#ifdef INCLUDE_TENSORFLOW
29 "DejaVu Sans Ultra-Light",
31#if PANGO_VERSION <= 12005
40const char kArabicText[] =
"والفكر والصراع 1234,\nوالفكر والصراع";
41const char kEngText[] =
"the quick brown fox jumps over the lazy dog";
42const char kHinText[] =
"पिताने विवाह की | हो गई उद्विग्न वह सोचा";
46#if PANGO_VERSION <= 12005
47 "उपयोक्ताो",
"नहीें",
"कहीअे",
"पत्रिाका",
"छह्णाीस",
52static PangoFontMap *font_map;
58 font_map = pango_cairo_font_map_new_for_font_type(CAIRO_FONT_TYPE_FT);
60 pango_cairo_font_map_set_default(PANGO_CAIRO_FONT_MAP(font_map));
66 static std::locale system_locale(
"");
67 std::locale::global(system_locale);
69 FLAGS_fonts_dir = TESTING_DIR;
70 FLAGS_fontconfig_tmpdir = FLAGS_test_tmpdir;
85 EXPECT_TRUE(font_info_.ParseFontDescriptionName(
"Arial Bold Italic 12"));
86 EXPECT_EQ(12, font_info_.font_size());
87 EXPECT_EQ(
"Arial", font_info_.family_name());
89 EXPECT_TRUE(font_info_.ParseFontDescriptionName(
"Verdana 10"));
90 EXPECT_EQ(10, font_info_.font_size());
91 EXPECT_EQ(
"Verdana", font_info_.family_name());
93 EXPECT_TRUE(font_info_.ParseFontDescriptionName(
"DejaVu Sans Ultra-Light"));
94 EXPECT_EQ(
"DejaVu Sans", font_info_.family_name());
99 EXPECT_TRUE(font_info_.ParseFontDescriptionName(
"Arial Italic 12"));
100 EXPECT_EQ(12, font_info_.font_size());
101 EXPECT_EQ(
"Arial", font_info_.family_name());
106 EXPECT_TRUE(font_info_.ParseFontDescriptionName(
"Georgia 10"));
107 EXPECT_EQ(10, font_info_.font_size());
108 EXPECT_EQ(
"Georgia", font_info_.family_name());
112 EXPECT_TRUE(font_info_.ParseFontDescriptionName(
"Arial Italic 12"));
113 int x_bearing, x_advance;
114 EXPECT_TRUE(font_info_.GetSpacingProperties(
"A", &x_bearing, &x_advance));
115 EXPECT_GT(x_advance, 0);
116 EXPECT_TRUE(font_info_.GetSpacingProperties(
"a", &x_bearing, &x_advance));
117 EXPECT_GT(x_advance, 0);
121 font_info_.ParseFontDescriptionName(
"Verdana 12");
124 font_info_.ParseFontDescriptionName(
"UnBatang 12");
127 font_info_.ParseFontDescriptionName(
"Lohit Hindi 12");
132 font_info_.ParseFontDescriptionName(
"Arab 12");
133 const char kArabicLigature[] =
"لا";
134 EXPECT_TRUE(font_info_.CanRenderString(kArabicLigature, strlen(kArabicLigature)));
136 printf(
"Next word\n");
141 font_info_.ParseFontDescriptionName(
"Verdana 12");
146 font_info_.ParseFontDescriptionName(
"Lohit Hindi 12");
155 font_info_.ParseFontDescriptionName(
"Verdana 12");
157 std::string word =
"office";
158 EXPECT_EQ(1, font_info_.DropUncoveredChars(&word));
159 EXPECT_EQ(
"oice", word);
162 const char *kJoiners[] = {
167 for (
auto &kJoiner : kJoiners) {
169 EXPECT_EQ(0, font_info_.DropUncoveredChars(&word));
170 EXPECT_STREQ(kJoiner, word.c_str());
184 FLAGS_fonts_dir = TESTING_DIR;
185 FLAGS_fontconfig_tmpdir = FLAGS_test_tmpdir;
187 font_map = pango_cairo_font_map_new_for_font_type(CAIRO_FONT_TYPE_FT);
189 pango_cairo_font_map_set_default(PANGO_CAIRO_FONT_MAP(font_map));
192#ifdef INCLUDE_TENSORFLOW
193 void CountUnicodeChars(
const char *utf8_text, std::unordered_map<char32, int64_t> *ch_map) {
199 if (UnicodeProps::IsWhitespace(*it))
continue;
201 if (std::isspace(*it))
235 for (
auto &font : fonts) {
241#ifdef INCLUDE_TENSORFLOW
242TEST_F(FontUtilsTest, DoesFindBestFonts) {
243 std::string fonts_list;
244 std::unordered_map<char32, int64_t> ch_map;
245 CountUnicodeChars(
kEngText, &ch_map);
246 EXPECT_EQ(26, ch_map.size());
247 std::vector<std::pair<const char *, std::vector<bool> > > font_flags;
249 EXPECT_TRUE(best_list.size());
253 CountUnicodeChars(
kKorText, &ch_map);
255 EXPECT_TRUE(best_list.size());
257 EXPECT_EQ(1, font_flags.size());
258 EXPECT_STREQ(
"UnBatang", font_flags[0].first);
264 const char *kLangNames[] = {
"Arabic",
"English",
"Hindi",
"Korean",
nullptr};
265 for (
int i = 0; kLangText[i] !=
nullptr; ++i) {
266 SCOPED_TRACE(kLangNames[i]);
267 std::vector<std::string> graphemes;
268 std::string selected_font;
271 EXPECT_TRUE(selected_font.size());
272 EXPECT_TRUE(graphemes.size());
277 const char kMixedScriptText[] =
"पिताने विवाह की | والفكر والصراع";
278 std::vector<std::string> graphemes;
279 std::string selected_font;
287TEST_F(FontUtilsTest, GetAllRenderableCharacters) {
288 const int32_t kHindiChar = 0x0905;
289 const int32_t kArabicChar = 0x0623;
290 const int32_t kMongolianChar = 0x180E;
291 const int32_t kOghamChar = 0x1680;
292 std::vector<bool> unicode_mask;
293 FontUtils::GetAllRenderableCharacters(&unicode_mask);
294 EXPECT_TRUE(unicode_mask[
'A']);
295 EXPECT_TRUE(unicode_mask[
'1']);
296 EXPECT_TRUE(unicode_mask[kHindiChar]);
297 EXPECT_TRUE(unicode_mask[kArabicChar]);
298 EXPECT_FALSE(unicode_mask[kMongolianChar]);
300 EXPECT_FALSE(unicode_mask[kOghamChar]);
302 unicode_mask.clear();
304 std::vector<std::string> selected_fonts;
305 selected_fonts.push_back(
"Lohit Hindi");
306 FontUtils::GetAllRenderableCharacters(selected_fonts, &unicode_mask);
307 EXPECT_TRUE(unicode_mask[
'1']);
308 EXPECT_TRUE(unicode_mask[kHindiChar]);
309 EXPECT_FALSE(unicode_mask[
'A']);
310 EXPECT_FALSE(unicode_mask[kArabicChar]);
311 EXPECT_FALSE(unicode_mask[kMongolianChar]);
312 EXPECT_FALSE(unicode_mask[kOghamChar]);
313 unicode_mask.clear();
319 SCOPED_TRACE(tracestring);
322 EXPECT_FALSE(unicode_mask[kOghamChar]);
324 EXPECT_FALSE(unicode_mask[kMongolianChar]);
325 unicode_mask.clear();
constexpr size_t countof(T const (&)[N]) noexcept
const char * kBadlyFormedHinWords[]
const char * kExpectedFontNames[]
TEST_F(EuroText, FastLatinOCR)
bool ParseFontDescriptionName(const std::string &name)
const std::string & family_name() const
static void SoftInitFontConfig()
static std::string BestFonts(const std::unordered_map< char32, int64_t > &ch_map, std::vector< std::pair< const char *, std::vector< bool > > > *font_flag)
static bool SelectFont(const char *utf8_word, const int utf8_len, std::string *font_name, std::vector< std::string > *graphemes)
static bool IsAvailableFont(const char *font_desc)
static const std::vector< std::string > & ListAvailableFonts()
static void SetUpTestCase()
static void SetUpTestCase()
const_iterator end() const
UnicodeText & PointToUTF8(const char *utf8_buffer, int byte_length)
const_iterator begin() const