2019-06-11 17:42:22 +08:00
|
|
|
|
// (C) Copyright 2017, Google Inc.
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
|
#include "ligature_table.h"
|
2019-06-11 17:42:22 +08:00
|
|
|
|
#include "commandlineflags.h"
|
|
|
|
|
#include "fileio.h"
|
|
|
|
|
#include "include_gunit.h"
|
|
|
|
|
#include "pango_font_info.h"
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
2020-12-27 17:41:48 +08:00
|
|
|
|
namespace tesseract {
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
|
|
const char kEngNonLigatureText[] = "fidelity effigy ſteep";
|
|
|
|
|
// Same as above text, but with "fi" in the first word and "ffi" in the second
|
|
|
|
|
// word replaced with their respective ligatures.
|
|
|
|
|
const char kEngLigatureText[] = "fidelity effigy ſteep";
|
|
|
|
|
// Same as kEngLigatureText but with "fi" in both words replaced with their
|
|
|
|
|
// ligature. The test Verdana font does not support the "ffi" or "ſt" ligature.
|
|
|
|
|
const char kRenderableEngLigatureText[] = "fidelity effigy ſteep";
|
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
|
static PangoFontMap *font_map;
|
2020-12-23 19:59:18 +08:00
|
|
|
|
|
2018-08-24 21:07:48 +08:00
|
|
|
|
class LigatureTableTest : public ::testing::Test {
|
2021-03-13 05:06:34 +08:00
|
|
|
|
protected:
|
|
|
|
|
void SetUp() override {
|
2019-06-11 17:42:22 +08:00
|
|
|
|
lig_table_ = LigatureTable::Get();
|
2020-12-23 19:59:18 +08:00
|
|
|
|
if (!font_map) {
|
|
|
|
|
font_map = pango_cairo_font_map_new_for_font_type(CAIRO_FONT_TYPE_FT);
|
|
|
|
|
}
|
|
|
|
|
pango_cairo_font_map_set_default(PANGO_CAIRO_FONT_MAP(font_map));
|
2019-05-17 00:12:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 21:07:48 +08:00
|
|
|
|
static void SetUpTestCase() {
|
2021-01-11 06:21:02 +08:00
|
|
|
|
static std::locale system_locale("");
|
|
|
|
|
std::locale::global(system_locale);
|
|
|
|
|
|
2019-06-11 17:42:22 +08:00
|
|
|
|
FLAGS_fonts_dir = TESTING_DIR;
|
2018-08-24 21:07:48 +08:00
|
|
|
|
FLAGS_fontconfig_tmpdir = FLAGS_test_tmpdir;
|
2021-01-06 01:46:59 +08:00
|
|
|
|
file::MakeTmpdir();
|
|
|
|
|
PangoFontInfo::SoftInitFontConfig(); // init early
|
2018-08-24 21:07:48 +08:00
|
|
|
|
}
|
2021-03-13 05:06:34 +08:00
|
|
|
|
LigatureTable *lig_table_;
|
2018-08-24 21:07:48 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TEST_F(LigatureTableTest, DoesFillLigatureTables) {
|
|
|
|
|
EXPECT_GT(lig_table_->norm_to_lig_table().size(), 0);
|
|
|
|
|
EXPECT_GT(lig_table_->lig_to_norm_table().size(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(LigatureTableTest, DoesAddLigatures) {
|
2021-03-13 05:06:34 +08:00
|
|
|
|
EXPECT_STREQ(kEngLigatureText, lig_table_->AddLigatures(kEngNonLigatureText, nullptr).c_str());
|
2018-08-24 21:07:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(LigatureTableTest, DoesAddLigaturesWithSupportedFont) {
|
|
|
|
|
PangoFontInfo font;
|
|
|
|
|
EXPECT_TRUE(font.ParseFontDescriptionName("Verdana"));
|
2021-03-13 05:06:34 +08:00
|
|
|
|
printf("1:%s\n", kRenderableEngLigatureText);
|
|
|
|
|
printf("2:%s\n", lig_table_->AddLigatures(kEngNonLigatureText, &font).c_str());
|
2018-08-24 21:07:48 +08:00
|
|
|
|
EXPECT_STREQ(kRenderableEngLigatureText,
|
|
|
|
|
lig_table_->AddLigatures(kEngNonLigatureText, &font).c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(LigatureTableTest, DoesNotAddLigaturesWithUnsupportedFont) {
|
|
|
|
|
PangoFontInfo font;
|
|
|
|
|
EXPECT_TRUE(font.ParseFontDescriptionName("Lohit Hindi"));
|
2021-03-13 05:06:34 +08:00
|
|
|
|
EXPECT_STREQ(kEngNonLigatureText, lig_table_->AddLigatures(kEngNonLigatureText, &font).c_str());
|
2018-08-24 21:07:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(LigatureTableTest, DoesRemoveLigatures) {
|
2021-03-13 05:06:34 +08:00
|
|
|
|
EXPECT_STREQ(kEngNonLigatureText, lig_table_->RemoveLigatures(kEngLigatureText).c_str());
|
2018-08-24 21:07:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(LigatureTableTest, TestCustomLigatures) {
|
2021-03-13 05:06:34 +08:00
|
|
|
|
const char *kTestCases[] = {
|
2018-09-29 15:19:13 +08:00
|
|
|
|
"act", "a\uE003", "publiſh", "publi\uE006", "ſince",
|
|
|
|
|
"\uE007nce", "aſleep", "a\uE008eep", "neceſſary", "nece\uE009ary",
|
2018-08-24 21:07:48 +08:00
|
|
|
|
};
|
2021-02-24 03:19:52 +08:00
|
|
|
|
for (size_t i = 0; i < countof(kTestCases); i += 2) {
|
2021-03-13 05:06:34 +08:00
|
|
|
|
EXPECT_STREQ(kTestCases[i + 1], lig_table_->AddLigatures(kTestCases[i], nullptr).c_str());
|
|
|
|
|
EXPECT_STREQ(kTestCases[i], lig_table_->RemoveLigatures(kTestCases[i + 1]).c_str());
|
|
|
|
|
EXPECT_STREQ(kTestCases[i], lig_table_->RemoveCustomLigatures(kTestCases[i + 1]).c_str());
|
2018-08-24 21:07:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_F(LigatureTableTest, TestRemovesCustomLigatures) {
|
2021-03-13 05:06:34 +08:00
|
|
|
|
const char *kTestCases[] = {
|
2018-09-29 15:19:13 +08:00
|
|
|
|
"fiction",
|
|
|
|
|
"fi\uE003ion",
|
|
|
|
|
"fiction",
|
2018-08-24 21:07:48 +08:00
|
|
|
|
};
|
2021-02-24 03:19:52 +08:00
|
|
|
|
for (size_t i = 0; i < countof(kTestCases); i += 3) {
|
2021-03-13 05:06:34 +08:00
|
|
|
|
EXPECT_STREQ(kTestCases[i + 1], lig_table_->AddLigatures(kTestCases[i], nullptr).c_str());
|
|
|
|
|
EXPECT_STREQ(kTestCases[i + 2], lig_table_->RemoveCustomLigatures(kTestCases[i + 1]).c_str());
|
2018-08-24 21:07:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-13 05:06:34 +08:00
|
|
|
|
} // namespace tesseract
|