1 // Copyright 2022 The PDFium Authors
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/fpdfdoc/cpdf_bafontmap.h"
6
7 #include <utility>
8
9 #include "build/build_config.h"
10 #include "constants/annotation_common.h"
11 #include "core/fpdfapi/font/cpdf_font.h"
12 #include "core/fpdfapi/page/test_with_page_module.h"
13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
14 #include "core/fpdfapi/parser/cpdf_name.h"
15 #include "core/fpdfapi/parser/cpdf_string.h"
16 #include "core/fpdfapi/parser/cpdf_test_document.h"
17
18 using BAFontMapTest = TestWithPageModule;
19
TEST_F(BAFontMapTest,DefaultFont)20 TEST_F(BAFontMapTest, DefaultFont) {
21 // Without any font resources, CPDF_BAFontMap generates a default font.
22 CPDF_TestDocument doc;
23
24 auto annot_dict = pdfium::MakeRetain<CPDF_Dictionary>();
25 annot_dict->SetNewFor<CPDF_Name>(pdfium::annotation::kSubtype, "Widget");
26 annot_dict->SetNewFor<CPDF_String>("DA", "0 0 0 rg /F1 12 Tf");
27
28 CPDF_BAFontMap font_map(&doc, std::move(annot_dict), "N");
29 #if !BUILDFLAG(IS_WIN)
30 // TODO(thestig): Figure out why this does not work on Windows.
31 EXPECT_EQ(font_map.GetPDFFontAlias(0), "Helvetica_00");
32 #endif
33 RetainPtr<CPDF_Font> font = font_map.GetPDFFont(0);
34 ASSERT_TRUE(font);
35 EXPECT_TRUE(font->IsType1Font());
36 EXPECT_EQ(font->GetBaseFontName(), "Helvetica");
37 }
38
TEST_F(BAFontMapTest,Bug853238)39 TEST_F(BAFontMapTest, Bug853238) {
40 CPDF_TestDocument doc;
41 auto root_dict = pdfium::MakeRetain<CPDF_Dictionary>();
42 auto acroform_dict = root_dict->SetNewFor<CPDF_Dictionary>("AcroForm");
43 auto annot_dr_dict = acroform_dict->SetNewFor<CPDF_Dictionary>("DR");
44 auto annot_font_dict = annot_dr_dict->SetNewFor<CPDF_Dictionary>("Font");
45 auto annot_font_f1_dict = annot_font_dict->SetNewFor<CPDF_Dictionary>("F1");
46 annot_font_f1_dict->SetNewFor<CPDF_Name>("Type", "Font");
47 annot_font_f1_dict->SetNewFor<CPDF_Name>("Subtype", "Type1");
48 annot_font_f1_dict->SetNewFor<CPDF_Name>("BaseFont", "Times-Roman");
49 doc.SetRoot(root_dict);
50
51 auto annot_dict = pdfium::MakeRetain<CPDF_Dictionary>();
52 annot_dict->SetNewFor<CPDF_Name>(pdfium::annotation::kSubtype, "Widget");
53 annot_dict->SetNewFor<CPDF_String>("DA", "0 0 0 rg /F1 12 Tf");
54
55 CPDF_BAFontMap font_map(&doc, std::move(annot_dict), "N");
56 EXPECT_EQ(font_map.GetPDFFontAlias(0), "F1");
57 RetainPtr<CPDF_Font> font = font_map.GetPDFFont(0);
58 ASSERT_TRUE(font);
59 EXPECT_TRUE(font->IsType1Font());
60 EXPECT_EQ(font->GetBaseFontName(), "Times-Roman");
61 }
62