1 // Copyright 2019 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "core/fpdfapi/font/cpdf_cidfont.h"
6 
7 #include <utility>
8 
9 #include "core/fpdfapi/page/cpdf_docpagedata.h"
10 #include "core/fpdfapi/page/cpdf_pagemodule.h"
11 #include "core/fpdfapi/parser/cpdf_array.h"
12 #include "core/fpdfapi/parser/cpdf_dictionary.h"
13 #include "core/fpdfapi/parser/cpdf_document.h"
14 #include "core/fpdfapi/parser/cpdf_name.h"
15 #include "core/fpdfapi/render/cpdf_docrenderdata.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 class CPDF_CIDFontTest : public testing::Test {
19  protected:
SetUp()20   void SetUp() override { CPDF_PageModule::Create(); }
TearDown()21   void TearDown() override { CPDF_PageModule::Destroy(); }
22 };
23 
TEST_F(CPDF_CIDFontTest,BUG_920636)24 TEST_F(CPDF_CIDFontTest, BUG_920636) {
25   CPDF_Document doc(std::make_unique<CPDF_DocRenderData>(),
26                     std::make_unique<CPDF_DocPageData>());
27   auto font_dict = pdfium::MakeRetain<CPDF_Dictionary>();
28   font_dict->SetNewFor<CPDF_Name>("Encoding", "Identity−H");
29 
30   {
31     auto descendant_fonts = pdfium::MakeRetain<CPDF_Array>();
32     {
33       auto descendant_font = pdfium::MakeRetain<CPDF_Dictionary>();
34       descendant_font->SetNewFor<CPDF_Name>("BaseFont", "CourierStd");
35       descendant_fonts->Append(std::move(descendant_font));
36     }
37     font_dict->SetFor("DescendantFonts", std::move(descendant_fonts));
38   }
39 
40   auto font = pdfium::MakeRetain<CPDF_CIDFont>(&doc, font_dict.Get());
41   ASSERT_TRUE(font->Load());
42 
43   // It would be nice if we can test more values here. However, the glyph
44   // indices are sometimes machine dependent.
45   struct {
46     uint32_t charcode;
47     int glyph;
48   } static constexpr kTestCases[] = {
49       {0, 31},
50       {256, 287},
51       {34661, 34692},
52   };
53 
54   for (const auto& test_case : kTestCases) {
55     EXPECT_EQ(test_case.glyph,
56               font->GlyphFromCharCode(test_case.charcode, nullptr));
57   }
58 }
59