tesseract  5.0.0-alpha-619-ge9db
ligature_table_test.cc
Go to the documentation of this file.
1 // (C) Copyright 2017, Google Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 // http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software
7 // distributed under the License is distributed on an "AS IS" BASIS,
8 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 // See the License for the specific language governing permissions and
10 // limitations under the License.
11 
12 #include "commandlineflags.h"
13 #include "fileio.h"
14 #include "include_gunit.h"
15 #include "ligature_table.h"
16 #include "pango_font_info.h"
17 
18 DECLARE_STRING_PARAM_FLAG(fonts_dir);
19 DECLARE_STRING_PARAM_FLAG(fontconfig_tmpdir);
20 
21 namespace {
22 
23 using tesseract::File;
26 
27 const char kEngNonLigatureText[] = "fidelity effigy ſteep";
28 // Same as above text, but with "fi" in the first word and "ffi" in the second
29 // word replaced with their respective ligatures.
30 const char kEngLigatureText[] = "fidelity effigy ſteep";
31 // Same as kEngLigatureText but with "fi" in both words replaced with their
32 // ligature. The test Verdana font does not support the "ffi" or "ſt" ligature.
33 const char kRenderableEngLigatureText[] = "fidelity effigy ſteep";
34 
35 class LigatureTableTest : public ::testing::Test {
36  protected:
37  void SetUp() override {
38  static std::locale system_locale("");
39  std::locale::global(system_locale);
40  lig_table_ = LigatureTable::Get();
41  }
42 
43  static void SetUpTestCase() {
44  FLAGS_fonts_dir = TESTING_DIR;
45  FLAGS_fontconfig_tmpdir = FLAGS_test_tmpdir;
46  }
47  LigatureTable* lig_table_;
48 };
49 
50 TEST_F(LigatureTableTest, DoesFillLigatureTables) {
51  EXPECT_GT(lig_table_->norm_to_lig_table().size(), 0);
52  EXPECT_GT(lig_table_->lig_to_norm_table().size(), 0);
53 }
54 
55 TEST_F(LigatureTableTest, DoesAddLigatures) {
56  EXPECT_STREQ(kEngLigatureText,
57  lig_table_->AddLigatures(kEngNonLigatureText, nullptr).c_str());
58 }
59 
60 TEST_F(LigatureTableTest, DoesAddLigaturesWithSupportedFont) {
61  PangoFontInfo font;
62  EXPECT_TRUE(font.ParseFontDescriptionName("Verdana"));
63 printf("1:%s\n", kRenderableEngLigatureText);
64 printf("2:%s\n", lig_table_->AddLigatures(kEngNonLigatureText, &font).c_str());
65  EXPECT_STREQ(kRenderableEngLigatureText,
66  lig_table_->AddLigatures(kEngNonLigatureText, &font).c_str());
67 }
68 
69 TEST_F(LigatureTableTest, DoesNotAddLigaturesWithUnsupportedFont) {
70  PangoFontInfo font;
71  EXPECT_TRUE(font.ParseFontDescriptionName("Lohit Hindi"));
72  EXPECT_STREQ(kEngNonLigatureText,
73  lig_table_->AddLigatures(kEngNonLigatureText, &font).c_str());
74 }
75 
76 TEST_F(LigatureTableTest, DoesRemoveLigatures) {
77  EXPECT_STREQ(kEngNonLigatureText,
78  lig_table_->RemoveLigatures(kEngLigatureText).c_str());
79 }
80 
81 TEST_F(LigatureTableTest, TestCustomLigatures) {
82  const char* kTestCases[] = {
83  "act", "a\uE003", "publiſh", "publi\uE006", "ſince",
84  "\uE007nce", "aſleep", "a\uE008eep", "neceſſary", "nece\uE009ary",
85  };
86  for (size_t i = 0; i < ARRAYSIZE(kTestCases); i += 2) {
87  EXPECT_STREQ(kTestCases[i + 1],
88  lig_table_->AddLigatures(kTestCases[i], nullptr).c_str());
89  EXPECT_STREQ(kTestCases[i],
90  lig_table_->RemoveLigatures(kTestCases[i + 1]).c_str());
91  EXPECT_STREQ(kTestCases[i],
92  lig_table_->RemoveCustomLigatures(kTestCases[i + 1]).c_str());
93  }
94 }
95 
96 TEST_F(LigatureTableTest, TestRemovesCustomLigatures) {
97  const char* kTestCases[] = {
98  "fiction",
99  "fi\uE003ion",
100  "fiction",
101  };
102  for (size_t i = 0; i < ARRAYSIZE(kTestCases); i += 3) {
103  EXPECT_STREQ(kTestCases[i + 1],
104  lig_table_->AddLigatures(kTestCases[i], nullptr).c_str());
105  EXPECT_STREQ(kTestCases[i + 2],
106  lig_table_->RemoveCustomLigatures(kTestCases[i + 1]).c_str());
107  }
108 }
109 } // namespace
DECLARE_STRING_PARAM_FLAG
DECLARE_STRING_PARAM_FLAG(fonts_dir)
tesseract::LigatureTable
Definition: ligature_table.h:55
ARRAYSIZE
#define ARRAYSIZE(arr)
Definition: include_gunit.h:53
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
fileio.h
tesseract::PangoFontInfo
Definition: pango_font_info.h:39
FLAGS_test_tmpdir
const char * FLAGS_test_tmpdir
Definition: include_gunit.h:20
pango_font_info.h
ligature_table.h
commandlineflags.h
tesseract::File
Definition: fileio.h:55