Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 241 additions & 0 deletions core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,29 @@
#include "core/fpdfapi/page/cpdf_textobject.h"
#include "core/fpdfapi/page/cpdf_textstate.h"
#include "core/fpdfapi/page/test_with_page_module.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/parser/cpdf_name.h"
#include "core/fpdfapi/parser/cpdf_parser.h"
#include "core/fpdfapi/parser/cpdf_reference.h"
#include "core/fpdfapi/parser/cpdf_stream.h"
#include "core/fpdfapi/parser/cpdf_stream_acc.h"
#include "core/fpdfapi/parser/cpdf_test_document.h"
#include "core/fxcrt/data_vector.h"
#include "core/fxge/cfx_fillrenderoptions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

ByteString GetStreamData(RetainPtr<const CPDF_Stream> stream) {
auto acc = pdfium::MakeRetain<CPDF_StreamAcc>(std::move(stream));
acc->LoadAllDataFiltered();
return ByteString(ByteStringView(acc->GetSpan()));
}

} // namespace

class CPDFPageContentGeneratorTest : public TestWithPageModule {
protected:
void TestProcessPath(CPDF_PageContentGenerator* pGen,
Expand Down Expand Up @@ -524,3 +536,232 @@ TEST_F(CPDFPageContentGeneratorTest, ProcessContentMarksWithProperties) {

EXPECT_TRUE(content.Contains("/M1 /Property#20Name#20With#20Space BDC"));
}

TEST_F(CPDFPageContentGeneratorTest,
GenerateContentWrapsStreamWithoutLeadingSave) {
auto doc = std::make_unique<CPDF_TestDocument>();
doc->CreateNewDoc();

RetainPtr<CPDF_Dictionary> page_dict(doc->CreateNewPage(0));
static constexpr ByteStringView kOriginalContents =
"-100 Tz q 10 5 3 25 re f Q\n";
auto original_stream =
doc->NewIndirect<CPDF_Stream>(kOriginalContents.unsigned_span());
page_dict->SetNewFor<CPDF_Reference>("Contents", doc.get(),
original_stream->GetObjNum());

auto test_page = pdfium::MakeRetain<CPDF_Page>(doc.get(), page_dict);
test_page->ParseContent();
ASSERT_EQ(1u, test_page->GetPageObjectCount());
EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream());

auto path_obj = std::make_unique<CPDF_PathObject>();
path_obj->set_filltype(CFX_FillRenderOptions::FillType::kWinding);
path_obj->path().AppendRect(0, 0, 10, 10);
path_obj->SetDirty(true);
test_page->AppendPageObject(std::move(path_obj));

CPDF_PageContentGenerator generator(test_page.Get());
generator.GenerateContent();

RetainPtr<const CPDF_Array> contents_array =
page_dict->GetArrayFor("Contents");
ASSERT_TRUE(contents_array);
// TODO(crbug.com/578639452): GenerateContent() should wrap
// `kOriginalContents` in "q\n" and "Q\n" streams so bottom-most graphics
// state changes do not leak into the newly appended stream.
ASSERT_EQ(2u, contents_array->size());

EXPECT_EQ(kOriginalContents, GetStreamData(contents_array->GetStreamAt(0)));
// EmbedPDF(faithful-content-rewrite): no default black in the new stream's
// header; objects write their own colours.
EXPECT_EQ("q\n1 w 0 J 0 j\n/FXE1 gs q 0 0 10 10 re f Q\nQ\n",
GetStreamData(contents_array->GetStreamAt(1)));

ASSERT_EQ(2u, test_page->GetPageObjectCount());
EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream());
EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream());
}

TEST_F(CPDFPageContentGeneratorTest, GenerateContentBalancesUnmatchedSaves) {
auto doc = std::make_unique<CPDF_TestDocument>();
doc->CreateNewDoc();

RetainPtr<CPDF_Dictionary> page_dict(doc->CreateNewPage(0));
static constexpr ByteStringView kOriginalContents =
"q -100 Tz q 10 5 3 25 re f Q\n";
auto original_stream =
doc->NewIndirect<CPDF_Stream>(kOriginalContents.unsigned_span());
page_dict->SetNewFor<CPDF_Reference>("Contents", doc.get(),
original_stream->GetObjNum());

auto test_page = pdfium::MakeRetain<CPDF_Page>(doc.get(), page_dict);
test_page->ParseContent();
ASSERT_EQ(1u, test_page->GetPageObjectCount());
EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream());

auto path_obj = std::make_unique<CPDF_PathObject>();
path_obj->set_filltype(CFX_FillRenderOptions::FillType::kWinding);
path_obj->path().AppendRect(0, 0, 10, 10);
path_obj->SetDirty(true);
test_page->AppendPageObject(std::move(path_obj));

CPDF_PageContentGenerator generator(test_page.Get());
generator.GenerateContent();

RetainPtr<const CPDF_Array> contents_array =
page_dict->GetArrayFor("Contents");
ASSERT_TRUE(contents_array);
// TODO(crbug.com/578639452): GenerateContent() should append a "Q\n" stream
// before the new stream to close the unmatched "q" operator in
// `kOriginalContents`.
ASSERT_EQ(2u, contents_array->size());

EXPECT_EQ(kOriginalContents, GetStreamData(contents_array->GetStreamAt(0)));
// EmbedPDF(faithful-content-rewrite): no default black in the new stream's
// header; objects write their own colours.
EXPECT_EQ("q\n1 w 0 J 0 j\n/FXE1 gs q 0 0 10 10 re f Q\nQ\n",
GetStreamData(contents_array->GetStreamAt(1)));

ASSERT_EQ(2u, test_page->GetPageObjectCount());
EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream());
EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream());
}

TEST_F(CPDFPageContentGeneratorTest,
GenerateContentBalancesUnmatchedSavesWithCTM) {
auto doc = std::make_unique<CPDF_TestDocument>();
doc->CreateNewDoc();

RetainPtr<CPDF_Dictionary> page_dict(doc->CreateNewPage(0));
static constexpr ByteStringView kStream0 =
"q 1 0 0 1 50 50 cm 10 5 3 25 re f\n";
static constexpr ByteStringView kStream1 = "30 30 10 10 re f\n";
auto stream0 = doc->NewIndirect<CPDF_Stream>(kStream0.unsigned_span());
auto stream1 = doc->NewIndirect<CPDF_Stream>(kStream1.unsigned_span());
auto contents = doc->NewIndirect<CPDF_Array>();
contents->AppendNew<CPDF_Reference>(doc.get(), stream0->GetObjNum());
contents->AppendNew<CPDF_Reference>(doc.get(), stream1->GetObjNum());
page_dict->SetNewFor<CPDF_Reference>("Contents", doc.get(),
contents->GetObjNum());

auto test_page = pdfium::MakeRetain<CPDF_Page>(doc.get(), page_dict);
test_page->ParseContent();
ASSERT_EQ(2u, test_page->GetPageObjectCount());
EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream());
EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream());

auto path_obj = std::make_unique<CPDF_PathObject>();
path_obj->set_filltype(CFX_FillRenderOptions::FillType::kWinding);
path_obj->path().AppendRect(0, 0, 10, 10);
path_obj->SetDirty(true);
test_page->AppendPageObject(std::move(path_obj));

{
CPDF_PageContentGenerator generator(test_page.Get());
generator.GenerateContent();
}

RetainPtr<const CPDF_Array> contents_array =
page_dict->GetArrayFor("Contents");
ASSERT_TRUE(contents_array);
// TODO(crbug.com/578639452): GenerateContent() should append a "Q\n" stream
// at index 2 to close the unmatched "q" operator and restore the CTM to
// identity before appending the new stream.
ASSERT_EQ(3u, contents_array->size());

EXPECT_EQ(kStream0, GetStreamData(contents_array->GetStreamAt(0)));
EXPECT_EQ(kStream1, GetStreamData(contents_array->GetStreamAt(1)));
// EmbedPDF(faithful-content-rewrite): no default black in the new stream's
// header; objects write their own colours.
EXPECT_EQ("q\n1 0 0 1 -50 -50 cm\n1 w 0 J 0 j\n/FXE1 gs "
"q 0 0 10 10 re f Q\nQ\n",
GetStreamData(contents_array->GetStreamAt(2)));

// Stream 1 has no CTM operators of its own, so its end CTM must still match
// stream 0's CTM even after a "Q\n" stream is appended at index 2.
const CFX_Matrix expected_ctm(1, 0, 0, 1, 50, 50);
EXPECT_EQ(expected_ctm, test_page->GetCTMAtEndOfStream(0));
EXPECT_EQ(expected_ctm, test_page->GetCTMAtEndOfStream(1));
// TODO(crbug.com/578639452): Once a "Q\n" stream is appended at index 2, its
// end CTM should be the identity matrix instead of `expected_ctm`.
EXPECT_EQ(expected_ctm, test_page->GetCTMAtEndOfStream(2));

// Regenerating stream 1 must invert `expected_ctm` inside its own `q ... Q`,
// without emitting a trailing `cm` operator after `Q`.
test_page->GetPageObjectByIndex(1)->SetDirty(true);
{
CPDF_PageContentGenerator generator(test_page.Get());
generator.GenerateContent();
}

// EmbedPDF(faithful-content-rewrite): a change to existing content writes
// every stream again, each in its own `q ... Q` with no `cm` outside it, so
// nothing is left unbalanced and no stream needs an inverse `cm`. Colours
// are written in their own colour space.
ASSERT_EQ(3u, contents_array->size());
EXPECT_EQ(
"q\n1 w 0 J 0 j\n/FXE1 gs "
"q 0 g 0 G 1 0 0 1 50 50 cm 10 5 3 25 re f Q\nQ\n",
GetStreamData(contents_array->GetStreamAt(0)));
EXPECT_EQ(
"q\n1 w 0 J 0 j\n/FXE1 gs "
"q 0 g 0 G 1 0 0 1 50 50 cm 30 30 10 10 re f Q\nQ\n",
GetStreamData(contents_array->GetStreamAt(1)));
EXPECT_EQ("q\n1 w 0 J 0 j\n/FXE1 gs q 0 0 10 10 re f Q\nQ\n",
GetStreamData(contents_array->GetStreamAt(2)));
}

TEST_F(CPDFPageContentGeneratorTest,
GenerateContentRemovesStreamAndAddsNewObject) {
auto doc = std::make_unique<CPDF_TestDocument>();
doc->CreateNewDoc();

RetainPtr<CPDF_Dictionary> page_dict(doc->CreateNewPage(0));
static constexpr ByteStringView kStream0 = "q 10 5 3 25 re f Q\n";
static constexpr ByteStringView kStream1 = "-100 Tz 30 30 10 10 re f\n";
auto stream0 = doc->NewIndirect<CPDF_Stream>(kStream0.unsigned_span());
auto stream1 = doc->NewIndirect<CPDF_Stream>(kStream1.unsigned_span());
auto contents = doc->NewIndirect<CPDF_Array>();
contents->AppendNew<CPDF_Reference>(doc.get(), stream0->GetObjNum());
contents->AppendNew<CPDF_Reference>(doc.get(), stream1->GetObjNum());
page_dict->SetNewFor<CPDF_Reference>("Contents", doc.get(),
contents->GetObjNum());

auto test_page = pdfium::MakeRetain<CPDF_Page>(doc.get(), page_dict);
test_page->ParseContent();
ASSERT_EQ(2u, test_page->GetPageObjectCount());
EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream());
EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream());

// Remove the only object from stream 0 so stream 0 will be deleted, and add
// a new streamless object in the same GenerateContent() call.
ASSERT_TRUE(test_page->RemovePageObject(test_page->GetPageObjectByIndex(0)));

auto path_obj = std::make_unique<CPDF_PathObject>();
path_obj->set_filltype(CFX_FillRenderOptions::FillType::kWinding);
path_obj->path().AppendRect(0, 0, 10, 10);
path_obj->SetDirty(true);
test_page->AppendPageObject(std::move(path_obj));

CPDF_PageContentGenerator generator(test_page.Get());
generator.GenerateContent();

RetainPtr<const CPDF_Array> contents_array =
page_dict->GetArrayFor("Contents");
ASSERT_TRUE(contents_array);
// EmbedPDF(faithful-content-rewrite): removing stream 0 changes existing
// content, so `kStream1` is written again from its page object, in its own
// `q ... Q`, and nothing leaks into the new object's stream. Its `-100 Tz`
// belongs to no page object (no text follows it), so it is not written.
ASSERT_EQ(2u, contents_array->size());

EXPECT_EQ("q\n1 w 0 J 0 j\n/FXE1 gs q 0 g 0 G 30 30 10 10 re f Q\nQ\n",
GetStreamData(contents_array->GetStreamAt(0)));
EXPECT_EQ("q\n1 w 0 J 0 j\n/FXE1 gs q 0 0 10 10 re f Q\nQ\n",
GetStreamData(contents_array->GetStreamAt(1)));

ASSERT_EQ(2u, test_page->GetPageObjectCount());
EXPECT_EQ(0, test_page->GetPageObjectByIndex(0)->GetContentStream());
EXPECT_EQ(1, test_page->GetPageObjectByIndex(1)->GetContentStream());
}
3 changes: 2 additions & 1 deletion core/fxcrt/xml/cfx_xmlnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
#define CORE_FXCRT_XML_CFX_XMLNODE_H_

#include "core/fxcrt/fx_stream.h"
#include "core/fxcrt/observed_ptr.h"
#include "core/fxcrt/retain_ptr.h"
#include "core/fxcrt/tree_node.h"

class CFX_XMLDocument;

class CFX_XMLNode : public TreeNode<CFX_XMLNode> {
class CFX_XMLNode : public Observable, public TreeNode<CFX_XMLNode> {
public:
enum class Type {
kInstruction = 0,
Expand Down
8 changes: 5 additions & 3 deletions core/fxge/cfx_fontmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "core/fxge/cfx_face.h"
#include "core/fxge/cfx_font.h"
#include "core/fxge/cfx_fontmapper.h"
#include "core/fxge/cfx_gemodule.h"
#include "core/fxge/cfx_glyphcache.h"
#include "core/fxge/fx_font.h"
#include "core/fxge/systemfontinfo_iface.h"
Expand Down Expand Up @@ -73,8 +74,7 @@ CFX_FontMgr::CFX_FontMgr([[maybe_unused]] FontBackend backend)
ft_library_(InitializeFreeType()),
#endif
#if defined(PDF_USE_SKIA)
font_backend_(backend),
skia_fontmgr_(CreateSkiaFontManager(font_backend_)),
skia_fontmgr_(CreateSkiaFontManager(backend)),
#endif
builtin_mapper_(std::make_unique<CFX_FontMapper>())
#if defined(PDF_ENABLE_FREETYPE)
Expand Down Expand Up @@ -111,7 +111,9 @@ sk_sp<SkTypeface> CFX_FontMgr::MakeSkTypeface(
// If DirectWrite or CoreText didn't work, try a fallback font manager.
if (!result) {
if (!skia_fontmgr_fallback_) {
skia_fontmgr_fallback_ = CreateSkiaFontManagerFallback(font_backend_);
skia_fontmgr_fallback_ = CreateSkiaFontManagerFallback(
CFX_GEModule::IsFontations() ? FontBackend::kFontations
: FontBackend::kFreeType);
}
result = skia_fontmgr_fallback_->makeFromStream(
std::make_unique<SkMemoryStream>(font_span.data(), font_span.size()));
Expand Down
6 changes: 3 additions & 3 deletions core/fxge/cfx_fontmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class SkTypeface;

class CFX_FontMgr {
public:
enum class FontBackend { kFreeType, kFontations };
// This internal definition of font backends must stay updated with respect
// to the public definition of `FPDF_FONT_BACKEND_TYPE` in `fpdfview.h`.
enum class FontBackend { kFreeType = 0, kFontations = 1 };

explicit CFX_FontMgr(FontBackend backend);
~CFX_FontMgr();
Expand All @@ -49,7 +51,6 @@ class CFX_FontMgr {

#if defined(PDF_ENABLE_FREETYPE)
FXFT_LibraryRec* GetFTLibrary() const { return ft_library_.get(); }

bool FTLibrarySupportsHinting() const { return ft_library_supports_hinting_; }
#endif

Expand All @@ -63,7 +64,6 @@ class CFX_FontMgr {
ScopedFXFTLibraryRec const ft_library_;
#endif
#if defined(PDF_USE_SKIA)
const FontBackend font_backend_;
sk_sp<SkFontMgr> skia_fontmgr_;
sk_sp<SkFontMgr> skia_fontmgr_fallback_;
#endif
Expand Down
9 changes: 3 additions & 6 deletions core/fxge/cfx_gemodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ WindowsPrintMode CFX_GEModule::GetPrintMode() {
void CFX_GEModule::Create(
std::optional<pdfium::span<const char* const>> user_font_paths,
RendererType renderer_type,
CFX_FontMgr::FontBackend font_backend) {
FontBackend font_backend) {
DCHECK(!g_GEModule);
g_GEModule = new CFX_GEModule(user_font_paths, renderer_type, font_backend);
g_GEModule->platform_->Init();
Expand All @@ -64,14 +64,11 @@ CFX_GEModule* CFX_GEModule::Get() {
CFX_GEModule::CFX_GEModule(
std::optional<pdfium::span<const char* const>> user_font_paths,
RendererType renderer_type,
CFX_FontMgr::FontBackend font_backend)
FontBackend font_backend)
: renderer_type_(renderer_type),
#if defined(PDF_ENABLE_FONTATIONS)
font_backend_(font_backend),
#endif
platform_(PlatformIface::Create()),
font_mgr_(std::make_unique<CFX_FontMgr>(font_backend)),
user_font_paths_(user_font_paths) {
}
user_font_paths_(user_font_paths) {}

CFX_GEModule::~CFX_GEModule() = default;
Loading
Loading