| // Copyright 2016 The PDFium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include <limits> |
| #include <memory> |
| #include <string> |
| #include <utility> |
| #include <vector> |
| |
| #include "build/build_config.h" |
| #include "core/fpdfapi/font/cpdf_font.h" |
| #include "core/fpdfapi/page/cpdf_page.h" |
| #include "core/fpdfapi/page/cpdf_pageobject.h" |
| #include "core/fpdfapi/parser/cpdf_array.h" |
| #include "core/fpdfapi/parser/cpdf_dictionary.h" |
| #include "core/fpdfapi/parser/cpdf_number.h" |
| #include "core/fpdfapi/parser/cpdf_stream.h" |
| #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
| #include "core/fxcrt/fx_codepage.h" |
| #include "core/fxcrt/fx_system.h" |
| #include "core/fxge/cfx_defaultrenderdevice.h" |
| #include "core/fxge/fx_font.h" |
| #include "fpdfsdk/cpdfsdk_helpers.h" |
| #include "public/cpp/fpdf_scopers.h" |
| #include "public/fpdf_annot.h" |
| #include "public/fpdf_edit.h" |
| #include "public/fpdfview.h" |
| #include "testing/embedder_test.h" |
| #include "testing/embedder_test_constants.h" |
| #include "testing/fx_string_testhelpers.h" |
| #include "testing/gmock/include/gmock/gmock-matchers.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "testing/utils/file_util.h" |
| #include "testing/utils/hash.h" |
| #include "testing/utils/path_service.h" |
| #include "third_party/base/check.h" |
| |
| using pdfium::HelloWorldChecksum; |
| using testing::HasSubstr; |
| using testing::Not; |
| using testing::UnorderedElementsAreArray; |
| |
| namespace { |
| |
| const char kAllRemovedChecksum[] = "eee4600ac08b458ac7ac2320e225674c"; |
| |
| const wchar_t kBottomText[] = L"I'm at the bottom of the page"; |
| |
| const char* BottomTextChecksum() { |
| #if BUILDFLAG(IS_APPLE) |
| if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "81636489006a31fcb00cf29efcdf7909"; |
| #endif |
| return "891dcb6e914c8360998055f1f47c9727"; |
| } |
| |
| const char* FirstRemovedChecksum() { |
| #if BUILDFLAG(IS_APPLE) |
| if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "a1dc2812692fcc7ee4f01ca77435df9d"; |
| #endif |
| return "e1477dc3b5b3b9c560814c4d1135a02b"; |
| } |
| |
| const wchar_t kLoadedFontText[] = L"I am testing my loaded font, WEE."; |
| |
| const char* LoadedFontTextChecksum() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "d58570cc045dfb818b92cbabbd1a364c"; |
| #if BUILDFLAG(IS_APPLE) |
| return "0f3e4a7d71f9e7eb8a1a0d69403b9848"; |
| #else |
| return "d58570cc045dfb818b92cbabbd1a364c"; |
| #endif |
| } |
| |
| const char kRedRectangleChecksum[] = "66d02eaa6181e2c069ce2ea99beda497"; |
| |
| // In embedded_images.pdf. |
| const char kEmbeddedImage33Checksum[] = "cb3637934bb3b95a6e4ae1ea9eb9e56e"; |
| |
| } // namespace |
| |
| class FPDFEditEmbedderTest : public EmbedderTest { |
| protected: |
| FPDF_DOCUMENT CreateNewDocument() { |
| CreateEmptyDocumentWithoutFormFillEnvironment(); |
| cpdf_doc_ = CPDFDocumentFromFPDFDocument(document()); |
| return document(); |
| } |
| |
| void CheckFontDescriptor(const CPDF_Dictionary* font_dict, |
| int font_type, |
| bool bold, |
| bool italic, |
| pdfium::span<const uint8_t> span) { |
| RetainPtr<const CPDF_Dictionary> font_desc = |
| font_dict->GetDictFor("FontDescriptor"); |
| ASSERT_TRUE(font_desc); |
| EXPECT_EQ("FontDescriptor", font_desc->GetNameFor("Type")); |
| ByteString font_name = font_desc->GetNameFor("FontName"); |
| EXPECT_FALSE(font_name.IsEmpty()); |
| EXPECT_EQ(font_dict->GetNameFor("BaseFont"), font_name); |
| |
| // Check that the font descriptor has the required keys according to spec |
| // 1.7 Table 5.19 |
| ASSERT_TRUE(font_desc->KeyExist("Flags")); |
| |
| int font_flags = font_desc->GetIntegerFor("Flags"); |
| EXPECT_EQ(bold, FontStyleIsForceBold(font_flags)); |
| EXPECT_EQ(italic, FontStyleIsItalic(font_flags)); |
| EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags)); |
| ASSERT_TRUE(font_desc->KeyExist("FontBBox")); |
| |
| RetainPtr<const CPDF_Array> fontBBox = font_desc->GetArrayFor("FontBBox"); |
| ASSERT_TRUE(fontBBox); |
| EXPECT_EQ(4u, fontBBox->size()); |
| // Check that the coordinates are in the preferred order according to spec |
| // 1.7 Section 3.8.4 |
| EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2)); |
| EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3)); |
| |
| EXPECT_TRUE(font_desc->KeyExist("ItalicAngle")); |
| EXPECT_TRUE(font_desc->KeyExist("Ascent")); |
| EXPECT_TRUE(font_desc->KeyExist("Descent")); |
| EXPECT_TRUE(font_desc->KeyExist("CapHeight")); |
| EXPECT_TRUE(font_desc->KeyExist("StemV")); |
| ByteString present("FontFile"); |
| ByteString absent("FontFile2"); |
| if (font_type == FPDF_FONT_TRUETYPE) |
| std::swap(present, absent); |
| EXPECT_TRUE(font_desc->KeyExist(present)); |
| EXPECT_FALSE(font_desc->KeyExist(absent)); |
| |
| auto streamAcc = |
| pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present)); |
| streamAcc->LoadAllDataRaw(); |
| |
| // Check that the font stream is the one that was provided |
| ASSERT_EQ(span.size(), streamAcc->GetSize()); |
| if (font_type == FPDF_FONT_TRUETYPE) { |
| ASSERT_EQ(static_cast<int>(span.size()), streamAcc->GetLength1ForTest()); |
| } |
| |
| pdfium::span<const uint8_t> stream_data = streamAcc->GetSpan(); |
| for (size_t j = 0; j < span.size(); j++) |
| EXPECT_EQ(span[j], stream_data[j]) << " at byte " << j; |
| } |
| |
| void CheckCompositeFontWidths(const CPDF_Array* widths_array, |
| CPDF_Font* typed_font) { |
| // Check that W array is in a format that conforms to PDF spec 1.7 section |
| // "Glyph Metrics in CIDFonts" (these checks are not |
| // implementation-specific). |
| EXPECT_GT(widths_array->size(), 1u); |
| int num_cids_checked = 0; |
| int cur_cid = 0; |
| for (size_t idx = 0; idx < widths_array->size(); idx++) { |
| int cid = widths_array->GetFloatAt(idx); |
| EXPECT_GE(cid, cur_cid); |
| ASSERT_FALSE(++idx == widths_array->size()); |
| RetainPtr<const CPDF_Object> next = widths_array->GetObjectAt(idx); |
| if (next->IsArray()) { |
| // We are in the c [w1 w2 ...] case |
| const CPDF_Array* arr = next->AsArray(); |
| int cnt = static_cast<int>(arr->size()); |
| size_t inner_idx = 0; |
| for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) { |
| int width = arr->GetFloatAt(inner_idx++); |
| EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) |
| << " at cid " << cur_cid; |
| } |
| num_cids_checked += cnt; |
| continue; |
| } |
| // Otherwise, are in the c_first c_last w case. |
| ASSERT_TRUE(next->IsNumber()); |
| int last_cid = next->AsNumber()->GetInteger(); |
| ASSERT_FALSE(++idx == widths_array->size()); |
| int width = widths_array->GetFloatAt(idx); |
| for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) { |
| EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) |
| << " at cid " << cur_cid; |
| } |
| num_cids_checked += last_cid - cid + 1; |
| } |
| // Make sure we have a good amount of cids described |
| EXPECT_GT(num_cids_checked, 200); |
| } |
| CPDF_Document* cpdf_doc() { return cpdf_doc_; } |
| |
| private: |
| CPDF_Document* cpdf_doc_; |
| }; |
| |
| namespace { |
| |
| const char kExpectedPDF[] = |
| "%PDF-1.7\r\n" |
| "%\xA1\xB3\xC5\xD7\r\n" |
| "1 0 obj\r\n" |
| "<</Pages 2 0 R /Type/Catalog>>\r\n" |
| "endobj\r\n" |
| "2 0 obj\r\n" |
| "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n" |
| "endobj\r\n" |
| "3 0 obj\r\n" |
| "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n" |
| "endobj\r\n" |
| "4 0 obj\r\n" |
| "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R " |
| "/Resources<<>>" |
| "/Rotate 0/Type/Page" |
| ">>\r\n" |
| "endobj\r\n" |
| "xref\r\n" |
| "0 5\r\n" |
| "0000000000 65535 f\r\n" |
| "0000000017 00000 n\r\n" |
| "0000000066 00000 n\r\n" |
| "0000000122 00000 n\r\n" |
| "0000000192 00000 n\r\n" |
| "trailer\r\n" |
| "<<\r\n" |
| "/Root 1 0 R\r\n" |
| "/Info 3 0 R\r\n" |
| "/Size 5/ID\\[<.*><.*>\\]>>\r\n" |
| "startxref\r\n" |
| "285\r\n" |
| "%%EOF\r\n"; |
| |
| } // namespace |
| |
| TEST_F(FPDFEditEmbedderTest, EmbedNotoSansSCFont) { |
| CreateEmptyDocument(); |
| ScopedFPDFPage page(FPDFPage_New(document(), 0, 400, 400)); |
| std::string font_path; |
| ASSERT_TRUE(PathService::GetThirdPartyFilePath( |
| "NotoSansCJK/NotoSansSC-Regular.subset.otf", &font_path)); |
| |
| size_t file_length = 0; |
| std::unique_ptr<char, pdfium::FreeDeleter> font_data = |
| GetFileContents(font_path.c_str(), &file_length); |
| ASSERT_TRUE(font_data); |
| |
| ScopedFPDFFont font(FPDFText_LoadFont( |
| document(), reinterpret_cast<const uint8_t*>(font_data.get()), |
| file_length, FPDF_FONT_TRUETYPE, /*cid=*/true)); |
| FPDF_PAGEOBJECT text_object = |
| FPDFPageObj_CreateTextObj(document(), font.get(), 20.0f); |
| EXPECT_TRUE(text_object); |
| |
| // Test the characters which are either mapped to one single unicode or |
| // multiple unicodes in the embedded font. |
| ScopedFPDFWideString text = GetFPDFWideString(L"这是第一句。 这是第二行。"); |
| EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| |
| FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 50, 200); |
| FPDFPage_InsertObject(page.get(), text_object); |
| EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| |
| const char kChecksum[] = "9a31fb87d1c6d2346bba22d1196041cd"; |
| ScopedFPDFBitmap page_bitmap = RenderPage(page.get()); |
| CompareBitmap(page_bitmap.get(), 400, 400, kChecksum); |
| |
| ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| VerifySavedDocument(400, 400, kChecksum); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, EmbedNotoSansSCFontWithCharcodes) { |
| CreateEmptyDocument(); |
| ScopedFPDFPage page(FPDFPage_New(document(), 0, 400, 400)); |
| std::string font_path; |
| ASSERT_TRUE(PathService::GetThirdPartyFilePath( |
| "NotoSansCJK/NotoSansSC-Regular.subset.otf", &font_path)); |
| |
| size_t file_length = 0; |
| std::unique_ptr<char, pdfium::FreeDeleter> font_data = |
| GetFileContents(font_path.c_str(), &file_length); |
| ASSERT_TRUE(font_data); |
| |
| ScopedFPDFFont font(FPDFText_LoadFont( |
| document(), reinterpret_cast<const uint8_t*>(font_data.get()), |
| file_length, FPDF_FONT_TRUETYPE, /*cid=*/true)); |
| FPDF_PAGEOBJECT text_object = |
| FPDFPageObj_CreateTextObj(document(), font.get(), 20.0f); |
| EXPECT_TRUE(text_object); |
| |
| // Same as `text` in the EmbedNotoSansSCFont test case above. |
| const std::vector<uint32_t> charcodes = {9, 6, 7, 3, 5, 2, 1, |
| 9, 6, 7, 4, 8, 2}; |
| EXPECT_TRUE( |
| FPDFText_SetCharcodes(text_object, charcodes.data(), charcodes.size())); |
| |
| FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 50, 200); |
| FPDFPage_InsertObject(page.get(), text_object); |
| EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| |
| const char kChecksum[] = "9a31fb87d1c6d2346bba22d1196041cd"; |
| ScopedFPDFBitmap page_bitmap = RenderPage(page.get()); |
| CompareBitmap(page_bitmap.get(), 400, 400, kChecksum); |
| |
| ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| VerifySavedDocument(400, 400, kChecksum); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, EmptyCreation) { |
| CreateEmptyDocument(); |
| FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0); |
| EXPECT_TRUE(page); |
| // The FPDFPage_GenerateContent call should do nothing. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| |
| EXPECT_THAT(GetString(), testing::MatchesRegex(std::string( |
| kExpectedPDF, sizeof(kExpectedPDF)))); |
| FPDF_ClosePage(page); |
| } |
| |
| // Regression test for https://crbug.com/667012 |
| TEST_F(FPDFEditEmbedderTest, RasterizePDF) { |
| const char kAllBlackChecksum[] = "5708fc5c4a8bd0abde99c8e8f0390615"; |
| |
| // Get the bitmap for the original document. |
| ScopedFPDFBitmap orig_bitmap; |
| { |
| ASSERT_TRUE(OpenDocument("black.pdf")); |
| FPDF_PAGE orig_page = LoadPage(0); |
| ASSERT_TRUE(orig_page); |
| orig_bitmap = RenderLoadedPage(orig_page); |
| CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackChecksum); |
| UnloadPage(orig_page); |
| } |
| |
| // Create a new document from |orig_bitmap| and save it. |
| { |
| FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); |
| FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792); |
| |
| // Add the bitmap to an image object and add the image object to the output |
| // page. |
| FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc); |
| EXPECT_TRUE( |
| FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get())); |
| static constexpr FS_MATRIX kLetterScaleMatrix{612, 0, 0, 792, 0, 0}; |
| EXPECT_TRUE(FPDFPageObj_SetMatrix(temp_img, &kLetterScaleMatrix)); |
| FPDFPage_InsertObject(temp_page, temp_img); |
| EXPECT_TRUE(FPDFPage_GenerateContent(temp_page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0)); |
| FPDF_ClosePage(temp_page); |
| FPDF_CloseDocument(temp_doc); |
| } |
| |
| // Get the generated content. Make sure it is at least as big as the original |
| // PDF. |
| EXPECT_GT(GetString().size(), 923u); |
| VerifySavedDocument(612, 792, kAllBlackChecksum); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, AddPaths) { |
| // Start with a blank page |
| FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| ASSERT_TRUE(page); |
| |
| // We will first add a red rectangle |
| FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| ASSERT_TRUE(red_rect); |
| // Expect false when trying to set colors out of range |
| EXPECT_FALSE(FPDFPageObj_SetStrokeColor(red_rect, 100, 100, 100, 300)); |
| EXPECT_FALSE(FPDFPageObj_SetFillColor(red_rect, 200, 256, 200, 0)); |
| |
| // Fill rectangle with red and insert to the page |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(red_rect, 255, 0, 0, 255)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| |
| int fillmode = FPDF_FILLMODE_NONE; |
| FPDF_BOOL stroke = true; |
| EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke)); |
| EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode); |
| EXPECT_FALSE(stroke); |
| |
| static constexpr FS_MATRIX kMatrix = {1, 2, 3, 4, 5, 6}; |
| EXPECT_TRUE(FPDFPageObj_SetMatrix(red_rect, &kMatrix)); |
| |
| FS_MATRIX matrix; |
| EXPECT_TRUE(FPDFPageObj_GetMatrix(red_rect, &matrix)); |
| EXPECT_FLOAT_EQ(1.0f, matrix.a); |
| EXPECT_FLOAT_EQ(2.0f, matrix.b); |
| EXPECT_FLOAT_EQ(3.0f, matrix.c); |
| EXPECT_FLOAT_EQ(4.0f, matrix.d); |
| EXPECT_FLOAT_EQ(5.0f, matrix.e); |
| EXPECT_FLOAT_EQ(6.0f, matrix.f); |
| |
| // Set back the identity matrix. |
| matrix = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}; |
| EXPECT_TRUE(FPDFPageObj_SetMatrix(red_rect, &matrix)); |
| |
| FPDFPage_InsertObject(page, red_rect); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleChecksum); |
| } |
| |
| // Now add to that a green rectangle with some medium alpha |
| FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(green_rect, 0, 255, 0, 128)); |
| |
| // Make sure the type of the rectangle is a path. |
| EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect)); |
| |
| // Make sure we get back the same color we set previously. |
| unsigned int R; |
| unsigned int G; |
| unsigned int B; |
| unsigned int A; |
| EXPECT_TRUE(FPDFPageObj_GetFillColor(green_rect, &R, &G, &B, &A)); |
| EXPECT_EQ(0u, R); |
| EXPECT_EQ(255u, G); |
| EXPECT_EQ(0u, B); |
| EXPECT_EQ(128u, A); |
| |
| // Make sure the path has 5 points (1 CFX_Path::Point::Type::kMove and 4 |
| // CFX_Path::Point::Type::kLine). |
| ASSERT_EQ(5, FPDFPath_CountSegments(green_rect)); |
| // Verify actual coordinates. |
| FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0); |
| float x; |
| float y; |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(100, x); |
| EXPECT_EQ(100, y); |
| EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| segment = FPDFPath_GetPathSegment(green_rect, 1); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(100, x); |
| EXPECT_EQ(140, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| segment = FPDFPath_GetPathSegment(green_rect, 2); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(140, x); |
| EXPECT_EQ(140, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| segment = FPDFPath_GetPathSegment(green_rect, 3); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(140, x); |
| EXPECT_EQ(100, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| segment = FPDFPath_GetPathSegment(green_rect, 4); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(100, x); |
| EXPECT_EQ(100, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_TRUE(FPDFPathSegment_GetClose(segment)); |
| |
| EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0)); |
| FPDFPage_InsertObject(page, green_rect); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 612, 792, |
| "7b0b87604594e773add528fae567a558"); |
| } |
| |
| // Add a black triangle. |
| FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(black_path, 0, 0, 0, 200)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200)); |
| EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100)); |
| EXPECT_TRUE(FPDFPath_Close(black_path)); |
| |
| // Make sure the path has 3 points (1 CFX_Path::Point::Type::kMove and 2 |
| // CFX_Path::Point::Type::kLine). |
| ASSERT_EQ(3, FPDFPath_CountSegments(black_path)); |
| // Verify actual coordinates. |
| segment = FPDFPath_GetPathSegment(black_path, 0); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(400, x); |
| EXPECT_EQ(100, y); |
| EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| segment = FPDFPath_GetPathSegment(black_path, 1); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(400, x); |
| EXPECT_EQ(200, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| segment = FPDFPath_GetPathSegment(black_path, 2); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(300, x); |
| EXPECT_EQ(100, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_TRUE(FPDFPathSegment_GetClose(segment)); |
| // Make sure out of bounds index access fails properly. |
| EXPECT_FALSE(FPDFPath_GetPathSegment(black_path, 3)); |
| |
| FPDFPage_InsertObject(page, black_path); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 612, 792, |
| "eadc8020a14dfcf091da2688733d8806"); |
| } |
| |
| // Now add a more complex blue path. |
| FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(blue_path, 0, 0, 255, 255)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0)); |
| EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230)); |
| EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300)); |
| EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325)); |
| EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325)); |
| EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400)); |
| EXPECT_TRUE(FPDFPath_Close(blue_path)); |
| FPDFPage_InsertObject(page, blue_path); |
| const char* last_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "ed14c60702b1489c597c7d46ece7f86d"; |
| return "9823e1a21bd9b72b6a442ba4f12af946"; |
| }(); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 612, 792, last_checksum); |
| } |
| |
| // Now save the result, closing the page and document |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| FPDF_ClosePage(page); |
| |
| // Render the saved result |
| VerifySavedDocument(612, 792, last_checksum); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, ClipPath) { |
| // Load document with a clipped rectangle. |
| ASSERT_TRUE(OpenDocument("clip_path.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| |
| FPDF_PAGEOBJECT triangle = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(triangle); |
| |
| // Test that we got the expected triangle. |
| ASSERT_EQ(4, FPDFPath_CountSegments(triangle)); |
| |
| FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(triangle, 0); |
| float x; |
| float y; |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(10, x); |
| EXPECT_EQ(10, y); |
| EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| |
| segment = FPDFPath_GetPathSegment(triangle, 1); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(25, x); |
| EXPECT_EQ(40, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| |
| segment = FPDFPath_GetPathSegment(triangle, 2); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(40, x); |
| EXPECT_EQ(10, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| |
| segment = FPDFPath_GetPathSegment(triangle, 3); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_TRUE(FPDFPathSegment_GetClose(segment)); |
| |
| // Test FPDFPageObj_GetClipPath(). |
| ASSERT_EQ(nullptr, FPDFPageObj_GetClipPath(nullptr)); |
| |
| FPDF_CLIPPATH clip_path = FPDFPageObj_GetClipPath(triangle); |
| ASSERT_TRUE(clip_path); |
| |
| // Test FPDFClipPath_CountPaths(). |
| ASSERT_EQ(-1, FPDFClipPath_CountPaths(nullptr)); |
| ASSERT_EQ(1, FPDFClipPath_CountPaths(clip_path)); |
| |
| // Test FPDFClipPath_CountPathSegments(). |
| ASSERT_EQ(-1, FPDFClipPath_CountPathSegments(nullptr, 0)); |
| ASSERT_EQ(-1, FPDFClipPath_CountPathSegments(clip_path, -1)); |
| ASSERT_EQ(-1, FPDFClipPath_CountPathSegments(clip_path, 1)); |
| ASSERT_EQ(4, FPDFClipPath_CountPathSegments(clip_path, 0)); |
| |
| // FPDFClipPath_GetPathSegment() negative testing. |
| ASSERT_EQ(nullptr, FPDFClipPath_GetPathSegment(nullptr, 0, 0)); |
| ASSERT_EQ(nullptr, FPDFClipPath_GetPathSegment(clip_path, -1, 0)); |
| ASSERT_EQ(nullptr, FPDFClipPath_GetPathSegment(clip_path, 1, 0)); |
| ASSERT_EQ(nullptr, FPDFClipPath_GetPathSegment(clip_path, 0, -1)); |
| ASSERT_EQ(nullptr, FPDFClipPath_GetPathSegment(clip_path, 0, 4)); |
| |
| // FPDFClipPath_GetPathSegment() positive testing. |
| segment = FPDFClipPath_GetPathSegment(clip_path, 0, 0); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(10, x); |
| EXPECT_EQ(15, y); |
| EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| |
| segment = FPDFClipPath_GetPathSegment(clip_path, 0, 1); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(40, x); |
| EXPECT_EQ(15, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| |
| segment = FPDFClipPath_GetPathSegment(clip_path, 0, 2); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(40, x); |
| EXPECT_EQ(35, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| |
| segment = FPDFClipPath_GetPathSegment(clip_path, 0, 3); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_EQ(10, x); |
| EXPECT_EQ(35, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, BUG_1399) { |
| // Load document with a clipped rectangle. |
| ASSERT_TRUE(OpenDocument("bug_1399.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| ASSERT_EQ(7, FPDFPage_CountObjects(page)); |
| |
| FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(obj); |
| |
| ASSERT_EQ(2, FPDFPath_CountSegments(obj)); |
| |
| FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(obj, 0); |
| float x; |
| float y; |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_FLOAT_EQ(107.718f, x); |
| EXPECT_FLOAT_EQ(719.922f, y); |
| EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| |
| segment = FPDFPath_GetPathSegment(obj, 1); |
| EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| EXPECT_FLOAT_EQ(394.718f, x); |
| EXPECT_FLOAT_EQ(719.922f, y); |
| EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| |
| FPDF_CLIPPATH clip_path = FPDFPageObj_GetClipPath(obj); |
| ASSERT_TRUE(clip_path); |
| |
| EXPECT_EQ(-1, FPDFClipPath_CountPaths(clip_path)); |
| EXPECT_EQ(-1, FPDFClipPath_CountPathSegments(clip_path, 0)); |
| EXPECT_FALSE(FPDFClipPath_GetPathSegment(clip_path, 0, 0)); |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, BUG_1549) { |
| static const char kOriginalChecksum[] = "126366fb95e6caf8ea196780075b22b2"; |
| static const char kRemovedChecksum[] = "6ec2f27531927882624b37bc7d8e12f4"; |
| |
| ASSERT_TRUE(OpenDocument("bug_1549.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| |
| { |
| ScopedFPDFBitmap bitmap = RenderLoadedPage(page); |
| CompareBitmap(bitmap.get(), 100, 150, kOriginalChecksum); |
| |
| ScopedFPDFPageObject obj(FPDFPage_GetObject(page, 0)); |
| ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj.get())); |
| ASSERT_TRUE(FPDFPage_RemoveObject(page, obj.get())); |
| } |
| |
| ASSERT_TRUE(FPDFPage_GenerateContent(page)); |
| |
| { |
| ScopedFPDFBitmap bitmap = RenderLoadedPage(page); |
| CompareBitmap(bitmap.get(), 100, 150, kRemovedChecksum); |
| } |
| |
| ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // TODO(crbug.com/pdfium/1549): Should be `kRemovedChecksum`. |
| VerifySavedDocument(100, 150, "4f9889cd5993db20f1ab37d677ac8d26"); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, SetText) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Get the "Hello, world!" text object and change it. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| ScopedFPDFWideString text1 = GetFPDFWideString(L"Changed for SetText test"); |
| EXPECT_TRUE(FPDFText_SetText(page_object, text1.get())); |
| |
| // Verify the "Hello, world!" text is gone and "Changed for SetText test" is |
| // now displayed. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| |
| const char* changed_checksum = []() { |
| #if BUILDFLAG(IS_APPLE) |
| if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "b720e83476fd6819d47c533f1f43c728"; |
| #endif |
| return "9a85b9354a69c61772ed24151c140f46"; |
| }(); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, changed_checksum); |
| } |
| |
| // Now save the result. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| |
| UnloadPage(page); |
| |
| // Re-open the file and check the changes were kept in the saved .pdf. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| EXPECT_EQ(2, FPDFPage_CountObjects(saved_page)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 200, changed_checksum); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, SetCharcodesBadParams) { |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| |
| const uint32_t kDummyValue = 42; |
| EXPECT_FALSE(FPDFText_SetCharcodes(nullptr, nullptr, 0)); |
| EXPECT_FALSE(FPDFText_SetCharcodes(nullptr, nullptr, 1)); |
| EXPECT_FALSE(FPDFText_SetCharcodes(nullptr, &kDummyValue, 0)); |
| EXPECT_FALSE(FPDFText_SetCharcodes(nullptr, &kDummyValue, 1)); |
| EXPECT_FALSE(FPDFText_SetCharcodes(page_object, nullptr, 1)); |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, SetTextKeepClippingPath) { |
| // Load document with some text, with parts clipped. |
| ASSERT_TRUE(OpenDocument("bug_1558.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| const char* original_checksum = []() { |
| #if BUILDFLAG(IS_APPLE) |
| if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "ae7a25c85e0e2dd0c5cb9dd5cd37f6df"; |
| #endif |
| return "7af7fe5b281298261eb66ac2d22f5054"; |
| }(); |
| { |
| // When opened before any editing and saving, the clipping path is rendered. |
| ScopedFPDFBitmap original_bitmap = RenderPage(page); |
| CompareBitmap(original_bitmap.get(), 200, 200, original_checksum); |
| } |
| |
| // "Change" the text in the objects to their current values to force them to |
| // regenerate when saving. |
| { |
| ScopedFPDFTextPage text_page(FPDFText_LoadPage(page)); |
| ASSERT_TRUE(text_page); |
| const int obj_count = FPDFPage_CountObjects(page); |
| ASSERT_EQ(2, obj_count); |
| for (int i = 0; i < obj_count; ++i) { |
| FPDF_PAGEOBJECT text_obj = FPDFPage_GetObject(page, i); |
| ASSERT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_obj)); |
| unsigned long size = |
| FPDFTextObj_GetText(text_obj, text_page.get(), |
| /*buffer=*/nullptr, /*length=*/0); |
| ASSERT_GT(size, 0u); |
| std::vector<FPDF_WCHAR> buffer = GetFPDFWideStringBuffer(size); |
| ASSERT_EQ(size, FPDFTextObj_GetText(text_obj, text_page.get(), |
| buffer.data(), size)); |
| EXPECT_TRUE(FPDFText_SetText(text_obj, buffer.data())); |
| } |
| } |
| |
| { |
| // After editing but before saving, the clipping path is retained. |
| ScopedFPDFBitmap edited_bitmap = RenderPage(page); |
| CompareBitmap(edited_bitmap.get(), 200, 200, original_checksum); |
| } |
| |
| // Save the file. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Open the saved copy and render it. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| { |
| ScopedFPDFBitmap saved_bitmap = RenderSavedPage(saved_page); |
| CompareBitmap(saved_bitmap.get(), 200, 200, original_checksum); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, BUG_1574) { |
| // Load document with some text within a clipping path. |
| ASSERT_TRUE(OpenDocument("bug_1574.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| const char* original_checksum = []() { |
| #if BUILDFLAG(IS_APPLE) |
| if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "1226bc2b8072622eb28f52321876e814"; |
| #endif |
| return "c5241eef60b9eac68ed1f2a5fd002703"; |
| }(); |
| { |
| // When opened before any editing and saving, the text object is rendered. |
| ScopedFPDFBitmap original_bitmap = RenderPage(page); |
| CompareBitmap(original_bitmap.get(), 200, 300, original_checksum); |
| } |
| |
| // "Change" the text in the objects to their current values to force them to |
| // regenerate when saving. |
| { |
| ScopedFPDFTextPage text_page(FPDFText_LoadPage(page)); |
| ASSERT_TRUE(text_page); |
| |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT text_obj = FPDFPage_GetObject(page, 1); |
| ASSERT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(text_obj)); |
| |
| unsigned long size = FPDFTextObj_GetText(text_obj, text_page.get(), |
| /*buffer=*/nullptr, /*length=*/0); |
| ASSERT_GT(size, 0u); |
| std::vector<FPDF_WCHAR> buffer = GetFPDFWideStringBuffer(size); |
| ASSERT_EQ(size, FPDFTextObj_GetText(text_obj, text_page.get(), |
| buffer.data(), size)); |
| EXPECT_TRUE(FPDFText_SetText(text_obj, buffer.data())); |
| } |
| |
| // Save the file. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Open the saved copy and render it. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| { |
| ScopedFPDFBitmap saved_bitmap = RenderSavedPage(saved_page); |
| CompareBitmap(saved_bitmap.get(), 200, 300, original_checksum); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveTextObject) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Show what the original file looks like. |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Get the "Hello, world!" text object and remove it. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| { |
| ScopedFPDFPageObject page_object(FPDFPage_GetObject(page, 0)); |
| ASSERT_TRUE(page_object); |
| ASSERT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(page_object.get())); |
| EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object.get())); |
| } |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| |
| // Verify the "Hello, world!" text is gone. |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| } |
| |
| // Verify the rendering again after calling FPDFPage_GenerateContent(). |
| ASSERT_TRUE(FPDFPage_GenerateContent(page)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| } |
| |
| // Save the document and verify it after reloading. |
| ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| VerifySavedDocument(200, 200, FirstRemovedChecksum()); |
| |
| // Verify removed/renamed resources are no longer there. |
| EXPECT_THAT(GetString(), Not(HasSubstr("/F1"))); |
| EXPECT_THAT(GetString(), Not(HasSubstr("/F2"))); |
| EXPECT_THAT(GetString(), Not(HasSubstr("/Times-Roman"))); |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, |
| RemoveTextObjectWithTwoPagesSharingContentStreamAndResources) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world_2_pages.pdf")); |
| FPDF_PAGE page1 = LoadPage(0); |
| ASSERT_TRUE(page1); |
| FPDF_PAGE page2 = LoadPage(1); |
| ASSERT_TRUE(page2); |
| |
| // Show what the original file looks like. |
| { |
| ScopedFPDFBitmap page1_bitmap = RenderPage(page1); |
| CompareBitmap(page1_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| ScopedFPDFBitmap page2_bitmap = RenderPage(page2); |
| CompareBitmap(page2_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Get the "Hello, world!" text object from page 1 and remove it. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page1)); |
| { |
| ScopedFPDFPageObject page_object(FPDFPage_GetObject(page1, 0)); |
| ASSERT_TRUE(page_object); |
| ASSERT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(page_object.get())); |
| EXPECT_TRUE(FPDFPage_RemoveObject(page1, page_object.get())); |
| } |
| ASSERT_EQ(1, FPDFPage_CountObjects(page1)); |
| |
| // Verify the "Hello, world!" text is gone from page 1. |
| { |
| ScopedFPDFBitmap page1_bitmap = RenderPage(page1); |
| CompareBitmap(page1_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| ScopedFPDFBitmap page2_bitmap = RenderPage(page2); |
| CompareBitmap(page2_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Verify the rendering again after calling FPDFPage_GenerateContent(). |
| ASSERT_TRUE(FPDFPage_GenerateContent(page1)); |
| { |
| ScopedFPDFBitmap page1_bitmap = RenderPage(page1); |
| CompareBitmap(page1_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| ScopedFPDFBitmap page2_bitmap = RenderPage(page2); |
| CompareBitmap(page2_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Save the document and verify it after reloading. |
| ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page1 = LoadSavedPage(0); |
| VerifySavedRendering(saved_page1, 200, 200, FirstRemovedChecksum()); |
| CloseSavedPage(saved_page1); |
| FPDF_PAGE saved_page2 = LoadSavedPage(1); |
| VerifySavedRendering(saved_page2, 200, 200, HelloWorldChecksum()); |
| CloseSavedPage(saved_page2); |
| CloseSavedDocument(); |
| |
| std::vector<std::string> split_saved_data = StringSplit(GetString(), '\n'); |
| // Verify removed/renamed resources are in the save PDF the correct number of |
| // times. |
| EXPECT_THAT(split_saved_data, Contains(HasSubstr("/F1")).Times(1)); |
| EXPECT_THAT(split_saved_data, Contains(HasSubstr("/F2")).Times(1)); |
| EXPECT_THAT(split_saved_data, Contains(HasSubstr("/Times-Roman")).Times(1)); |
| |
| UnloadPage(page1); |
| UnloadPage(page2); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, |
| RemoveTextObjectWithTwoPagesSharingContentArrayAndResources) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world_2_pages_split_streams.pdf")); |
| FPDF_PAGE page1 = LoadPage(0); |
| ASSERT_TRUE(page1); |
| FPDF_PAGE page2 = LoadPage(1); |
| ASSERT_TRUE(page2); |
| |
| // Show what the original file looks like. |
| { |
| ScopedFPDFBitmap page1_bitmap = RenderPage(page1); |
| CompareBitmap(page1_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| ScopedFPDFBitmap page2_bitmap = RenderPage(page2); |
| CompareBitmap(page2_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Get the "Hello, world!" text object from page 1 and remove it. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page1)); |
| { |
| ScopedFPDFPageObject page_object(FPDFPage_GetObject(page1, 0)); |
| ASSERT_TRUE(page_object); |
| ASSERT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(page_object.get())); |
| EXPECT_TRUE(FPDFPage_RemoveObject(page1, page_object.get())); |
| } |
| ASSERT_EQ(1, FPDFPage_CountObjects(page1)); |
| |
| // Verify the "Hello, world!" text is gone from page 1. |
| { |
| ScopedFPDFBitmap page1_bitmap = RenderPage(page1); |
| CompareBitmap(page1_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| ScopedFPDFBitmap page2_bitmap = RenderPage(page2); |
| CompareBitmap(page2_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Verify the rendering again after calling FPDFPage_GenerateContent(). |
| ASSERT_TRUE(FPDFPage_GenerateContent(page1)); |
| { |
| ScopedFPDFBitmap page1_bitmap = RenderPage(page1); |
| CompareBitmap(page1_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| ScopedFPDFBitmap page2_bitmap = RenderPage(page2); |
| CompareBitmap(page2_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Save the document and verify it after reloading. |
| ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page1 = LoadSavedPage(0); |
| VerifySavedRendering(saved_page1, 200, 200, FirstRemovedChecksum()); |
| CloseSavedPage(saved_page1); |
| FPDF_PAGE saved_page2 = LoadSavedPage(1); |
| VerifySavedRendering(saved_page2, 200, 200, HelloWorldChecksum()); |
| CloseSavedPage(saved_page2); |
| CloseSavedDocument(); |
| |
| UnloadPage(page1); |
| UnloadPage(page2); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveTextObjectWithTwoPagesSharingResourcesDict) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world_2_pages_shared_resources_dict.pdf")); |
| FPDF_PAGE page1 = LoadPage(0); |
| ASSERT_TRUE(page1); |
| FPDF_PAGE page2 = LoadPage(1); |
| ASSERT_TRUE(page2); |
| |
| // Show what the original file looks like. |
| { |
| ScopedFPDFBitmap page1_bitmap = RenderPage(page1); |
| CompareBitmap(page1_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| ScopedFPDFBitmap page2_bitmap = RenderPage(page2); |
| CompareBitmap(page2_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Get the "Hello, world!" text object from page 1 and remove it. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page1)); |
| { |
| ScopedFPDFPageObject page_object(FPDFPage_GetObject(page1, 0)); |
| ASSERT_TRUE(page_object); |
| ASSERT_EQ(FPDF_PAGEOBJ_TEXT, FPDFPageObj_GetType(page_object.get())); |
| EXPECT_TRUE(FPDFPage_RemoveObject(page1, page_object.get())); |
| } |
| ASSERT_EQ(1, FPDFPage_CountObjects(page1)); |
| |
| // Verify the "Hello, world!" text is gone from page 1 |
| { |
| ScopedFPDFBitmap page1_bitmap = RenderPage(page1); |
| CompareBitmap(page1_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| ScopedFPDFBitmap page2_bitmap = RenderPage(page2); |
| CompareBitmap(page2_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Verify the rendering again after calling FPDFPage_GenerateContent(). |
| ASSERT_TRUE(FPDFPage_GenerateContent(page1)); |
| { |
| ScopedFPDFBitmap page1_bitmap = RenderPage(page1); |
| CompareBitmap(page1_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| ScopedFPDFBitmap page2_bitmap = RenderPage(page2); |
| CompareBitmap(page2_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Save the document and verify it after reloading. |
| ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page1 = LoadSavedPage(0); |
| VerifySavedRendering(saved_page1, 200, 200, FirstRemovedChecksum()); |
| CloseSavedPage(saved_page1); |
| FPDF_PAGE saved_page2 = LoadSavedPage(1); |
| VerifySavedRendering(saved_page2, 200, 200, HelloWorldChecksum()); |
| CloseSavedPage(saved_page2); |
| CloseSavedDocument(); |
| |
| UnloadPage(page1); |
| UnloadPage(page2); |
| } |
| |
| void CheckMarkCounts(FPDF_PAGE page, |
| int start_from, |
| int expected_object_count, |
| size_t expected_prime_count, |
| size_t expected_square_count, |
| size_t expected_greater_than_ten_count, |
| size_t expected_bounds_count) { |
| int object_count = FPDFPage_CountObjects(page); |
| ASSERT_EQ(expected_object_count, object_count); |
| |
| size_t prime_count = 0; |
| size_t square_count = 0; |
| size_t greater_than_ten_count = 0; |
| size_t bounds_count = 0; |
| for (int i = 0; i < object_count; ++i) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| |
| int mark_count = FPDFPageObj_CountMarks(page_object); |
| for (int j = 0; j < mark_count; ++j) { |
| FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j); |
| |
| char buffer[256]; |
| unsigned long name_len = 999u; |
| ASSERT_TRUE( |
| FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len)); |
| EXPECT_GT(name_len, 0u); |
| EXPECT_NE(999u, name_len); |
| std::wstring name = |
| GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| if (name == L"Prime") { |
| prime_count++; |
| } else if (name == L"Square") { |
| square_count++; |
| int expected_square = start_from + i; |
| EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark)); |
| |
| unsigned long get_param_key_return = 999u; |
| ASSERT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer), |
| &get_param_key_return)); |
| EXPECT_EQ((6u + 1u) * 2u, get_param_key_return); |
| std::wstring key = |
| GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| EXPECT_EQ(L"Factor", key); |
| |
| EXPECT_EQ(FPDF_OBJECT_NUMBER, |
| FPDFPageObjMark_GetParamValueType(mark, "Factor")); |
| int square_root; |
| EXPECT_TRUE( |
| FPDFPageObjMark_GetParamIntValue(mark, "Factor", &square_root)); |
| EXPECT_EQ(expected_square, square_root * square_root); |
| } else if (name == L"GreaterThanTen") { |
| greater_than_ten_count++; |
| } else if (name == L"Bounds") { |
| bounds_count++; |
| EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark)); |
| |
| unsigned long get_param_key_return = 999u; |
| ASSERT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer), |
| &get_param_key_return)); |
| EXPECT_EQ((8u + 1u) * 2u, get_param_key_return); |
| std::wstring key = |
| GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| EXPECT_EQ(L"Position", key); |
| |
| EXPECT_EQ(FPDF_OBJECT_STRING, |
| FPDFPageObjMark_GetParamValueType(mark, "Position")); |
| unsigned long length; |
| EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue( |
| mark, "Position", buffer, sizeof(buffer), &length)); |
| ASSERT_GT(length, 0u); |
| std::wstring value = |
| GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| |
| // "Position" can be "First", "Last", or "End". |
| if (i == 0) { |
| EXPECT_EQ((5u + 1u) * 2u, length); |
| EXPECT_EQ(L"First", value); |
| } else if (i == object_count - 1) { |
| if (length == (4u + 1u) * 2u) { |
| EXPECT_EQ(L"Last", value); |
| } else if (length == (3u + 1u) * 2u) { |
| EXPECT_EQ(L"End", value); |
| } else { |
| FAIL(); |
| } |
| } else { |
| FAIL(); |
| } |
| } else { |
| FAIL(); |
| } |
| } |
| } |
| |
| // Expect certain number of tagged objects. The test file contains strings |
| // from 1 to 19. |
| EXPECT_EQ(expected_prime_count, prime_count); |
| EXPECT_EQ(expected_square_count, square_count); |
| EXPECT_EQ(expected_greater_than_ten_count, greater_than_ten_count); |
| EXPECT_EQ(expected_bounds_count, bounds_count); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, ReadMarkedObjectsIndirectDict) { |
| // Load document with some text marked with an indirect property. |
| ASSERT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| CheckMarkCounts(page, 1, 19, 8, 4, 9, 1); |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveMarkedObjectsPrime) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("text_in_page_marked.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Show what the original file looks like. |
| { |
| const char* original_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "34ac4e0f3ba510760be09d0e19c1b43e"; |
| #if BUILDFLAG(IS_APPLE) |
| #ifdef ARCH_CPU_ARM64 |
| return "cdc8e22cf1e7e06999dc456288672a3b"; |
| #else |
| return "966579fb98206858ce2f0a1f94a74d05"; |
| #endif // ARCH_CPU_ARM64 |
| #else |
| return "3d5a3de53d5866044c2b6bf339742c97"; |
| #endif // BUILDFLAG(IS_APPLE) |
| }(); |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, original_checksum); |
| } |
| |
| constexpr int expected_object_count = 19; |
| CheckMarkCounts(page, 1, expected_object_count, 8, 4, 9, 1); |
| |
| // Get all objects marked with "Prime" |
| std::vector<FPDF_PAGEOBJECT> primes; |
| for (int i = 0; i < expected_object_count; ++i) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| |
| int mark_count = FPDFPageObj_CountMarks(page_object); |
| for (int j = 0; j < mark_count; ++j) { |
| FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j); |
| |
| char buffer[256]; |
| unsigned long name_len = 999u; |
| ASSERT_TRUE( |
| FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len)); |
| EXPECT_GT(name_len, 0u); |
| EXPECT_NE(999u, name_len); |
| std::wstring name = |
| GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| if (name == L"Prime") { |
| primes.push_back(page_object); |
| } |
| } |
| } |
| |
| // Remove all objects marked with "Prime". |
| for (FPDF_PAGEOBJECT page_object : primes) { |
| EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| FPDFPageObj_Destroy(page_object); |
| } |
| |
| EXPECT_EQ(11, FPDFPage_CountObjects(page)); |
| const char* non_primes_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "c671fa07b63e85c4201b9926e880fda8"; |
| #if BUILDFLAG(IS_APPLE) |
| #ifdef ARCH_CPU_ARM64 |
| return "23c4aec321547f51591fe7363a9ea2d6"; |
| #else |
| return "6e19a4dd674b522cd39cf41956559bd6"; |
| #endif // ARCH_CPU_ARM64 |
| #else |
| return "bc8623c052f12376c3d8dd09a6cd27df"; |
| #endif // BUILDFLAG(IS_APPLE) |
| }(); |
| const char* non_primes_after_save_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "c671fa07b63e85c4201b9926e880fda8"; |
| #if BUILDFLAG(IS_APPLE) |
| #ifdef ARCH_CPU_ARM64 |
| return "6bb1ea0d0a512f29edabda33064a0725"; |
| #else |
| return "3cb35c681f8fb5a43a49146ac7caa818"; |
| #endif // ARCH_CPU_ARM64 |
| #else |
| return "bc8623c052f12376c3d8dd09a6cd27df"; |
| #endif // BUILDFLAG(IS_APPLE) |
| }(); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, non_primes_checksum); |
| } |
| |
| // Save the file. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the prime marks are not there anymore. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| EXPECT_EQ(11, FPDFPage_CountObjects(saved_page)); |
| |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 200, non_primes_after_save_checksum); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveMarks) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("text_in_page_marked.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| constexpr int kExpectedObjectCount = 19; |
| CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1); |
| |
| // Remove all "Prime" content marks. |
| for (int i = 0; i < kExpectedObjectCount; ++i) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| |
| int mark_count = FPDFPageObj_CountMarks(page_object); |
| for (int j = mark_count - 1; j >= 0; --j) { |
| FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j); |
| |
| char buffer[256]; |
| unsigned long name_len = 999u; |
| ASSERT_TRUE( |
| FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len)); |
| EXPECT_GT(name_len, 0u); |
| EXPECT_NE(999u, name_len); |
| std::wstring name = |
| GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| if (name == L"Prime") { |
| // Remove mark. |
| EXPECT_TRUE(FPDFPageObj_RemoveMark(page_object, mark)); |
| |
| // Verify there is now one fewer mark in the page object. |
| EXPECT_EQ(mark_count - 1, FPDFPageObj_CountMarks(page_object)); |
| } |
| } |
| } |
| |
| // Verify there are 0 "Prime" content marks now. |
| CheckMarkCounts(page, 1, kExpectedObjectCount, 0, 4, 9, 1); |
| |
| // Save the file. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the prime marks are not there anymore. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 0, 4, 9, 1); |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveMarkParam) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("text_in_page_marked.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| constexpr int kExpectedObjectCount = 19; |
| CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1); |
| |
| // Remove all "Square" content marks parameters. |
| for (int i = 0; i < kExpectedObjectCount; ++i) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| |
| int mark_count = FPDFPageObj_CountMarks(page_object); |
| for (int j = 0; j < mark_count; ++j) { |
| FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j); |
| |
| char buffer[256]; |
| unsigned long name_len = 999u; |
| ASSERT_TRUE( |
| FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len)); |
| EXPECT_GT(name_len, 0u); |
| EXPECT_NE(999u, name_len); |
| std::wstring name = |
| GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| if (name == L"Square") { |
| // Show the mark has a "Factor" parameter. |
| int out_value; |
| EXPECT_TRUE( |
| FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value)); |
| |
| // Remove parameter. |
| EXPECT_TRUE(FPDFPageObjMark_RemoveParam(page_object, mark, "Factor")); |
| |
| // Verify the "Factor" parameter is gone. |
| EXPECT_FALSE( |
| FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value)); |
| } |
| } |
| } |
| |
| // Save the file. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the "Factor" parameters are still gone. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| size_t square_count = 0; |
| for (int i = 0; i < kExpectedObjectCount; ++i) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i); |
| |
| int mark_count = FPDFPageObj_CountMarks(page_object); |
| for (int j = 0; j < mark_count; ++j) { |
| FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j); |
| |
| char buffer[256]; |
| unsigned long name_len = 999u; |
| ASSERT_TRUE( |
| FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len)); |
| EXPECT_GT(name_len, 0u); |
| EXPECT_NE(999u, name_len); |
| std::wstring name = |
| GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| if (name == L"Square") { |
| // Verify the "Factor" parameter is still gone. |
| int out_value; |
| EXPECT_FALSE( |
| FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value)); |
| |
| ++square_count; |
| } |
| } |
| } |
| |
| // Verify the parameters are gone, but the marks are not. |
| EXPECT_EQ(4u, square_count); |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, MaintainMarkedObjects) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("text_in_page_marked.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Iterate over all objects, counting the number of times each content mark |
| // name appears. |
| CheckMarkCounts(page, 1, 19, 8, 4, 9, 1); |
| |
| // Remove first page object. |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| FPDFPageObj_Destroy(page_object); |
| |
| CheckMarkCounts(page, 2, 18, 8, 3, 9, 1); |
| |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| |
| UnloadPage(page); |
| |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1); |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, MaintainIndirectMarkedObjects) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Iterate over all objects, counting the number of times each content mark |
| // name appears. |
| CheckMarkCounts(page, 1, 19, 8, 4, 9, 1); |
| |
| // Remove first page object. |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| FPDFPageObj_Destroy(page_object); |
| |
| CheckMarkCounts(page, 2, 18, 8, 3, 9, 1); |
| |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| |
| UnloadPage(page); |
| |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1); |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObject) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Get the "Hello, world!" text object and remove it. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| |
| // Verify the "Hello, world!" text is gone. |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| |
| // Save the file |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| FPDFPageObj_Destroy(page_object); |
| |
| // Re-open the file and check the page object count is still 1. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| EXPECT_EQ(1, FPDFPage_CountObjects(saved_page)); |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObjectSplitStreamsNotLonely) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world_split_streams.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Get the "Hello, world!" text object and remove it. There is another object |
| // in the same stream that says "Goodbye, world!" |
| ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| |
| // Verify the "Hello, world!" text is gone. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| const char* hello_removed_checksum = []() { |
| #if BUILDFLAG(IS_APPLE) |
| if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "5508c2f06d104050f74f655693e38c2c"; |
| #endif |
| return "a8cd82499cf744e0862ca468c9d4ceb8"; |
| }(); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, hello_removed_checksum); |
| } |
| |
| // Save the file |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| FPDFPageObj_Destroy(page_object); |
| |
| // Re-open the file and check the page object count is still 2. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| EXPECT_EQ(2, FPDFPage_CountObjects(saved_page)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 200, hello_removed_checksum); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObjectSplitStreamsLonely) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world_split_streams.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Get the "Greetings, world!" text object and remove it. This is the only |
| // object in the stream. |
| ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 2); |
| ASSERT_TRUE(page_object); |
| EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| |
| // Verify the "Greetings, world!" text is gone. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| // Save the file |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| FPDFPageObj_Destroy(page_object); |
| |
| // Re-open the file and check the page object count is still 2. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| EXPECT_EQ(2, FPDFPage_CountObjects(saved_page)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 200, HelloWorldChecksum()); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, GetContentStream) { |
| // Load document with some text split across streams. |
| ASSERT_TRUE(OpenDocument("split_streams.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Content stream 0: page objects 0-14. |
| // Content stream 1: page objects 15-17. |
| // Content stream 2: page object 18. |
| ASSERT_EQ(19, FPDFPage_CountObjects(page)); |
| for (int i = 0; i < 19; i++) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| ASSERT_TRUE(page_object); |
| CPDF_PageObject* cpdf_page_object = |
| CPDFPageObjectFromFPDFPageObject(page_object); |
| if (i < 15) |
| EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i; |
| else if (i < 18) |
| EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i; |
| else |
| EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i; |
| } |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveAllFromStream) { |
| // Load document with some text split across streams. |
| ASSERT_TRUE(OpenDocument("split_streams.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Content stream 0: page objects 0-14. |
| // Content stream 1: page objects 15-17. |
| // Content stream 2: page object 18. |
| ASSERT_EQ(19, FPDFPage_CountObjects(page)); |
| |
| // Loop backwards because objects will being removed, which shifts the indexes |
| // after the removed position. |
| for (int i = 18; i >= 0; i--) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| ASSERT_TRUE(page_object); |
| CPDF_PageObject* cpdf_page_object = |
| CPDFPageObjectFromFPDFPageObject(page_object); |
| |
| // Empty content stream 1. |
| if (cpdf_page_object->GetContentStream() == 1) { |
| EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| FPDFPageObj_Destroy(page_object); |
| } |
| } |
| |
| // Content stream 0: page objects 0-14. |
| // Content stream 2: page object 15. |
| ASSERT_EQ(16, FPDFPage_CountObjects(page)); |
| for (int i = 0; i < 16; i++) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| ASSERT_TRUE(page_object); |
| CPDF_PageObject* cpdf_page_object = |
| CPDFPageObjectFromFPDFPageObject(page_object); |
| if (i < 15) |
| EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i; |
| else |
| EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i; |
| } |
| |
| // Generate contents should remove the empty stream and update the page |
| // objects' contents stream indexes. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| |
| // Content stream 0: page objects 0-14. |
| // Content stream 1: page object 15. |
| ASSERT_EQ(16, FPDFPage_CountObjects(page)); |
| for (int i = 0; i < 16; i++) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| ASSERT_TRUE(page_object); |
| CPDF_PageObject* cpdf_page_object = |
| CPDFPageObjectFromFPDFPageObject(page_object); |
| if (i < 15) |
| EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i; |
| else |
| EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i; |
| } |
| |
| const char* stream1_removed_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "b9bb0acfdba4bb5d2e578e7b73341baf"; |
| #if BUILDFLAG(IS_APPLE) |
| #if ARCH_CPU_ARM64 |
| return "08505db7b598f7397a2260ecb1f6d86d"; |
| #else |
| return "3cdc75af44c15bed80998facd6e674c9"; |
| #endif // ARCH_CPU_ARM64 |
| #else |
| return "b474826df1acedb05c7b82e1e49e64a6"; |
| #endif // BUILDFLAG(IS_APPLE) |
| }(); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, stream1_removed_checksum); |
| } |
| |
| // Save the file |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the page object count is still 16, and that |
| // content stream 1 was removed. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| // Content stream 0: page objects 0-14. |
| // Content stream 1: page object 15. |
| EXPECT_EQ(16, FPDFPage_CountObjects(saved_page)); |
| for (int i = 0; i < 16; i++) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i); |
| ASSERT_TRUE(page_object); |
| CPDF_PageObject* cpdf_page_object = |
| CPDFPageObjectFromFPDFPageObject(page_object); |
| if (i < 15) |
| EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i; |
| else |
| EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i; |
| } |
| |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 200, stream1_removed_checksum); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveAllFromSingleStream) { |
| // Load document with a single stream. |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Content stream 0: page objects 0-1. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| |
| // Loop backwards because objects will being removed, which shifts the indexes |
| // after the removed position. |
| for (int i = 1; i >= 0; i--) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| ASSERT_TRUE(page_object); |
| CPDF_PageObject* cpdf_page_object = |
| CPDFPageObjectFromFPDFPageObject(page_object); |
| ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| FPDFPageObj_Destroy(page_object); |
| } |
| |
| // No more objects in the stream |
| ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| |
| // Generate contents should remove the empty stream and update the page |
| // objects' contents stream indexes. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| |
| ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedChecksum); |
| } |
| |
| // Save the file |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the page object count is still 0. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| EXPECT_EQ(0, FPDFPage_CountObjects(saved_page)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedChecksum); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveFirstFromSingleStream) { |
| // Load document with a single stream. |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Content stream 0: page objects 0-1. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| |
| // Remove first object. |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| CPDF_PageObject* cpdf_page_object = |
| CPDFPageObjectFromFPDFPageObject(page_object); |
| ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| FPDFPageObj_Destroy(page_object); |
| |
| // One object left in the stream. |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| |
| // Still one object left in the stream. |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| } |
| |
| // Save the file |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the page object count is still 0. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| ASSERT_EQ(1, FPDFPage_CountObjects(saved_page)); |
| page_object = FPDFPage_GetObject(saved_page, 0); |
| ASSERT_TRUE(page_object); |
| cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 200, FirstRemovedChecksum()); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveLastFromSingleStream) { |
| // Load document with a single stream. |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Content stream 0: page objects 0-1. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| |
| // Remove last object |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 1); |
| ASSERT_TRUE(page_object); |
| CPDF_PageObject* cpdf_page_object = |
| CPDFPageObjectFromFPDFPageObject(page_object); |
| ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| FPDFPageObj_Destroy(page_object); |
| |
| // One object left in the stream. |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| |
| // Still one object left in the stream. |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| |
| using pdfium::HelloWorldRemovedChecksum; |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, HelloWorldRemovedChecksum()); |
| } |
| |
| // Save the file |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the page object count is still 0. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| ASSERT_EQ(1, FPDFPage_CountObjects(saved_page)); |
| page_object = FPDFPage_GetObject(saved_page, 0); |
| ASSERT_TRUE(page_object); |
| cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 200, HelloWorldRemovedChecksum()); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, RemoveAllFromMultipleStreams) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world_split_streams.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Content stream 0: page objects 0-1. |
| // Content stream 1: page object 2. |
| ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| |
| // Loop backwards because objects will being removed, which shifts the indexes |
| // after the removed position. |
| for (int i = 2; i >= 0; i--) { |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| ASSERT_TRUE(page_object); |
| ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| FPDFPageObj_Destroy(page_object); |
| } |
| |
| // No more objects in the page. |
| ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| |
| // Generate contents should remove the empty streams and update the page |
| // objects' contents stream indexes. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| |
| ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedChecksum); |
| } |
| |
| // Save the file |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the page object count is still 0. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| EXPECT_EQ(0, FPDFPage_CountObjects(saved_page)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedChecksum); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, InsertPageObjectAndSave) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Add a red rectangle. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(red_rect, 255, 0, 0, 255)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| FPDFPage_InsertObject(page, red_rect); |
| |
| // Verify the red rectangle was added. |
| ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| |
| // Save the file |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the page object count is still 3. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| EXPECT_EQ(3, FPDFPage_CountObjects(saved_page)); |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, InsertPageObjectEditAndSave) { |
| // Load document with some text. |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Add a red rectangle. |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(red_rect, 255, 100, 100, 255)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| FPDFPage_InsertObject(page, red_rect); |
| |
| // Verify the red rectangle was added. |
| ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| |
| // Generate content but change it again |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(red_rect, 255, 0, 0, 255)); |
| |
| // Save the file |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the page object count is still 3. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| EXPECT_EQ(3, FPDFPage_CountObjects(saved_page)); |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, InsertAndRemoveLargeFile) { |
| const int kOriginalObjectCount = 600; |
| |
| // Load document with many objects. |
| ASSERT_TRUE(OpenDocument("many_rectangles.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| using pdfium::ManyRectanglesChecksum; |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 300, ManyRectanglesChecksum()); |
| } |
| |
| // Add a black rectangle. |
| ASSERT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT black_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(black_rect, 0, 0, 0, 255)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(black_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| FPDFPage_InsertObject(page, black_rect); |
| |
| // Verify the black rectangle was added. |
| ASSERT_EQ(kOriginalObjectCount + 1, FPDFPage_CountObjects(page)); |
| const char* plus_rectangle_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "0d3715fcfb9bd0dd25dcce60800bff47"; |
| return "6b9396ab570754b32b04ca629e902f77"; |
| }(); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 200, 300, plus_rectangle_checksum); |
| } |
| |
| // Save the file. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| // Re-open the file and check the rectangle added is still there. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| EXPECT_EQ(kOriginalObjectCount + 1, FPDFPage_CountObjects(saved_page)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 300, plus_rectangle_checksum); |
| } |
| |
| // Remove the added rectangle. |
| FPDF_PAGEOBJECT added_object = |
| FPDFPage_GetObject(saved_page, kOriginalObjectCount); |
| EXPECT_TRUE(FPDFPage_RemoveObject(saved_page, added_object)); |
| FPDFPageObj_Destroy(added_object); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 300, ManyRectanglesChecksum()); |
| } |
| EXPECT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(saved_page)); |
| |
| // Save the file again. |
| EXPECT_TRUE(FPDFPage_GenerateContent(saved_page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(saved_document(), this, 0)); |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| |
| // Re-open the file (again) and check the black rectangle was removed and the |
| // rest is intact. |
| ASSERT_TRUE(OpenSavedDocument()); |
| saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| EXPECT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(saved_page)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(saved_page); |
| CompareBitmap(page_bitmap.get(), 200, 300, ManyRectanglesChecksum()); |
| } |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, AddAndRemovePaths) { |
| // Start with a blank page. |
| FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| ASSERT_TRUE(page); |
| |
| // Render the blank page and verify it's a blank bitmap. |
| using pdfium::kBlankPage612By792Checksum; |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 612, 792, kBlankPage612By792Checksum); |
| } |
| ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| |
| // Add a red rectangle. |
| FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| ASSERT_TRUE(red_rect); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(red_rect, 255, 0, 0, 255)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| FPDFPage_InsertObject(page, red_rect); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleChecksum); |
| } |
| EXPECT_EQ(1, FPDFPage_CountObjects(page)); |
| |
| // Remove rectangle and verify it does not render anymore and the bitmap is |
| // back to a blank one. |
| EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect)); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 612, 792, kBlankPage612By792Checksum); |
| } |
| EXPECT_EQ(0, FPDFPage_CountObjects(page)); |
| |
| // Trying to remove an object not in the page should return false. |
| EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect)); |
| |
| FPDF_ClosePage(page); |
| FPDFPageObj_Destroy(red_rect); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, PathsPoints) { |
| CreateNewDocument(); |
| FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document()); |
| // This should fail gracefully, even if img is not a path. |
| ASSERT_EQ(-1, FPDFPath_CountSegments(img)); |
| |
| // This should fail gracefully, even if path is NULL. |
| ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr)); |
| |
| // FPDFPath_GetPathSegment() with a non-path. |
| ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0)); |
| // FPDFPath_GetPathSegment() with a NULL path. |
| ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0)); |
| float x; |
| float y; |
| // FPDFPathSegment_GetPoint() with a NULL segment. |
| EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y)); |
| |
| // FPDFPathSegment_GetType() with a NULL segment. |
| ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr)); |
| |
| // FPDFPathSegment_GetClose() with a NULL segment. |
| EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr)); |
| |
| FPDFPageObj_Destroy(img); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, PathOnTopOfText) { |
| // Load document with some text |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Add an opaque rectangle on top of some of the text. |
| FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(red_rect, 255, 0, 0, 255)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| FPDFPage_InsertObject(page, red_rect); |
| |
| // Add a transparent triangle on top of other part of the text. |
| FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(black_path, 0, 0, 0, 100)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80)); |
| EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10)); |
| EXPECT_TRUE(FPDFPath_Close(black_path)); |
| FPDFPage_InsertObject(page, black_path); |
| |
| // Render and check the result. |
| ScopedFPDFBitmap bitmap = RenderLoadedPage(page); |
| const char* checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "8a48b019826492331454f2809990aba8"; |
| #if BUILDFLAG(IS_APPLE) |
| return "279693baca9f48da2d75a8e289aed58e"; |
| #else |
| return "fe415d47945c10b9cc8e9ca08887369e"; |
| #endif |
| }(); |
| CompareBitmap(bitmap.get(), 200, 200, checksum); |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, EditOverExistingContent) { |
| // Load document with existing content |
| ASSERT_TRUE(OpenDocument("bug_717.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| |
| // Add a transparent rectangle on top of the existing content |
| FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(red_rect2, 255, 0, 0, 100)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
| FPDFPage_InsertObject(page, red_rect2); |
| |
| // Add an opaque rectangle on top of the existing content |
| FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(red_rect, 255, 0, 0, 255)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| FPDFPage_InsertObject(page, red_rect); |
| |
| const char* original_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "1e82fbdd21490cee9d3479fe6125af67"; |
| return "ad04e5bd0f471a9a564fb034bd0fb073"; |
| }(); |
| ScopedFPDFBitmap bitmap = RenderLoadedPage(page); |
| CompareBitmap(bitmap.get(), 612, 792, original_checksum); |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| |
| // Now save the result, closing the page and document |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| UnloadPage(page); |
| |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| VerifySavedRendering(saved_page, 612, 792, original_checksum); |
| |
| ClearString(); |
| // Add another opaque rectangle on top of the existing content |
| FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(green_rect, 0, 255, 0, 255)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| FPDFPage_InsertObject(saved_page, green_rect); |
| |
| // Add another transparent rectangle on top of existing content |
| FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(green_rect2, 0, 255, 0, 100)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
| FPDFPage_InsertObject(saved_page, green_rect2); |
| const char* last_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "8705d023e5fec3499d1e30cf2bcc5dc1"; |
| return "4b5b00f824620f8c9b8801ebb98e1cdd"; |
| }(); |
| { |
| ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page); |
| CompareBitmap(new_bitmap.get(), 612, 792, last_checksum); |
| } |
| EXPECT_TRUE(FPDFPage_GenerateContent(saved_page)); |
| |
| // Now save the result, closing the page and document |
| EXPECT_TRUE(FPDF_SaveAsCopy(saved_document(), this, 0)); |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| |
| // Render the saved result |
| VerifySavedDocument(612, 792, last_checksum); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, AddStrokedPaths) { |
| // Start with a blank page |
| FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| |
| // Add a large stroked rectangle (fill color should not affect it). |
| FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(rect, 255, 0, 0, 255)); |
| EXPECT_TRUE(FPDFPageObj_SetStrokeColor(rect, 0, 255, 0, 255)); |
| EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(rect, 15.0f)); |
| |
| float width = 0; |
| EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width)); |
| EXPECT_EQ(15.0f, width); |
| |
| EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1)); |
| FPDFPage_InsertObject(page, rect); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| const char* checksum_1 = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "1469acf60e7647ebeb8e1fb08c5d6c7a"; |
| return "64bd31f862a89e0a9e505a5af6efd506"; |
| }(); |
| CompareBitmap(page_bitmap.get(), 612, 792, checksum_1); |
| } |
| |
| // Add crossed-checkmark |
| FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500); |
| EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400)); |
| EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600)); |
| EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600)); |
| EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400)); |
| EXPECT_TRUE(FPDFPageObj_SetStrokeColor(check, 128, 128, 128, 180)); |
| EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(check, 8.35f)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| FPDFPage_InsertObject(page, check); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| const char* checksum_2 = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "68b3194f74abd9d471695ce1415be43f"; |
| return "4b6f3b9d25c4e194821217d5016c3724"; |
| }(); |
| CompareBitmap(page_bitmap.get(), 612, 792, checksum_2); |
| } |
| |
| // Add stroked and filled oval-ish path. |
| FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100); |
| EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300)); |
| EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305)); |
| EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105)); |
| EXPECT_TRUE(FPDFPath_Close(path)); |
| EXPECT_TRUE(FPDFPageObj_SetFillColor(path, 200, 128, 128, 100)); |
| EXPECT_TRUE(FPDFPageObj_SetStrokeColor(path, 128, 200, 128, 150)); |
| EXPECT_TRUE(FPDFPageObj_SetStrokeWidth(path, 10.5f)); |
| EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1)); |
| FPDFPage_InsertObject(page, path); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| const char* checksum_3 = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "ea784068651df2b9ba132ce9215e6780"; |
| return "ff3e6a22326754944cc6e56609acd73b"; |
| }(); |
| CompareBitmap(page_bitmap.get(), 612, 792, checksum_3); |
| } |
| FPDF_ClosePage(page); |
| } |
| |
| // Tests adding text from standard font using FPDFPageObj_NewTextObj. |
| TEST_F(FPDFEditEmbedderTest, AddStandardFontText) { |
| // Start with a blank page |
| ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792)); |
| |
| // Add some text to the page |
| FPDF_PAGEOBJECT text_object1 = |
| FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| EXPECT_TRUE(text_object1); |
| ScopedFPDFWideString text1 = GetFPDFWideString(kBottomText); |
| EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get())); |
| static constexpr FS_MATRIX kMatrix1{1, 0, 0, 1, 20, 20}; |
| EXPECT_TRUE(FPDFPageObj_SetMatrix(text_object1, &kMatrix1)); |
| FPDFPage_InsertObject(page.get(), text_object1); |
| EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page.get()); |
| CompareBitmap(page_bitmap.get(), 612, 792, BottomTextChecksum()); |
| |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| VerifySavedDocument(612, 792, BottomTextChecksum()); |
| } |
| |
| // Try another font |
| FPDF_PAGEOBJECT text_object2 = |
| FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f); |
| EXPECT_TRUE(text_object2); |
| ScopedFPDFWideString text2 = |
| GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold."); |
| EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600); |
| FPDFPage_InsertObject(page.get(), text_object2); |
| EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page.get()); |
| const char* checksum = []() { |
| #if BUILDFLAG(IS_APPLE) |
| if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "983baaa1f688eff7a14b1bf91c171a1a"; |
| #endif |
| return "161523e196eb5341604cd73e12c97922"; |
| }(); |
| CompareBitmap(page_bitmap.get(), 612, 792, checksum); |
| |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| VerifySavedDocument(612, 792, checksum); |
| } |
| |
| // And some randomly transformed text |
| FPDF_PAGEOBJECT text_object3 = |
| FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f); |
| EXPECT_TRUE(text_object3); |
| ScopedFPDFWideString text3 = GetFPDFWideString(L"Can you read me? <:)>"); |
| EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get())); |
| FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200); |
| FPDFPage_InsertObject(page.get(), text_object3); |
| EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page.get()); |
| const char* checksum = []() { |
| #if BUILDFLAG(IS_APPLE) |
| if (!CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "e0b3493c5c16e41d0d892ffb48e63fba"; |
| #endif |
| return "1fbf772dca8d82b960631e6683934964"; |
| }(); |
| CompareBitmap(page_bitmap.get(), 612, 792, checksum); |
| |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| VerifySavedDocument(612, 792, checksum); |
| } |
| |
| FS_MATRIX matrix; |
| EXPECT_TRUE(FPDFPageObj_GetMatrix(text_object3, &matrix)); |
| EXPECT_FLOAT_EQ(1.0f, matrix.a); |
| EXPECT_FLOAT_EQ(1.5f, matrix.b); |
| EXPECT_FLOAT_EQ(2.0f, matrix.c); |
| EXPECT_FLOAT_EQ(0.5f, matrix.d); |
| EXPECT_FLOAT_EQ(200.0f, matrix.e); |
| EXPECT_FLOAT_EQ(200.0f, matrix.f); |
| |
| EXPECT_FALSE(FPDFTextObj_GetFontSize(nullptr, nullptr)); |
| float size = 55; |
| EXPECT_FALSE(FPDFTextObj_GetFontSize(nullptr, &size)); |
| EXPECT_EQ(55, size); |
| EXPECT_TRUE(FPDFTextObj_GetFontSize(text_object3, &size)); |
| EXPECT_EQ(20, size); |
| |
| // TODO(npm): Why are there issues with text rotated by 90 degrees? |
| // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this. |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, AddStandardFontTextOfSizeZero) { |
| // Start with a blank page |
| ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792)); |
| |
| // Add some text of size 0 to the page. |
| FPDF_PAGEOBJECT text_object = |
| FPDFPageObj_NewTextObj(document(), "Arial", 0.0f); |
| EXPECT_TRUE(text_object); |
| ScopedFPDFWideString text = GetFPDFWideString(kBottomText); |
| EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20); |
| |
| float size = -1; // Make sure 'size' gets changed. |
| EXPECT_TRUE(FPDFTextObj_GetFontSize(text_object, &size)); |
| EXPECT_EQ(0.0f, size); |
| |
| FPDFPage_InsertObject(page.get(), text_object); |
| EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page.get()); |
| CompareBitmap(page_bitmap.get(), 612, 792, |
| pdfium::kBlankPage612By792Checksum); |
| |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| VerifySavedDocument(612, 792, pdfium::kBlankPage612By792Checksum); |
| } |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, GetTextRenderMode) { |
| ASSERT_TRUE(OpenDocument("text_render_mode.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| |
| EXPECT_EQ(FPDF_TEXTRENDERMODE_UNKNOWN, |
| FPDFTextObj_GetTextRenderMode(nullptr)); |
| |
| FPDF_PAGEOBJECT fill = FPDFPage_GetObject(page, 0); |
| EXPECT_EQ(FPDF_TEXTRENDERMODE_FILL, FPDFTextObj_GetTextRenderMode(fill)); |
| |
| FPDF_PAGEOBJECT stroke = FPDFPage_GetObject(page, 1); |
| EXPECT_EQ(FPDF_TEXTRENDERMODE_STROKE, FPDFTextObj_GetTextRenderMode(stroke)); |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, SetTextRenderMode) { |
| const char* original_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "39a4ac8f1fdc6653edd3b91862ea7b75"; |
| #if BUILDFLAG(IS_APPLE) |
| return "c488514ce0fc949069ff560407edacd2"; |
| #else |
| return "97a4fcf3c9581e19917895631af31d41"; |
| #endif |
| }(); |
| const char* stroke_checksum = []() { |
| if (CFX_DefaultRenderDevice::SkiaIsDefaultRenderer()) |
| return "d16eb1bb4748eeb5fb801594da70d519"; |
| return "e06ee84aeebe926e8c980b7822027e8a"; |
| }(); |
| |
| { |
| ASSERT_TRUE(OpenDocument("text_render_mode.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| |
| // Check the bitmap |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 612, 446, original_checksum); |
| } |
| |
| // Cannot set on a null object. |
| EXPECT_FALSE( |
| FPDFTextObj_SetTextRenderMode(nullptr, FPDF_TEXTRENDERMODE_UNKNOWN)); |
| EXPECT_FALSE( |
| FPDFTextObj_SetTextRenderMode(nullptr, FPDF_TEXTRENDERMODE_INVISIBLE)); |
| |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(page_object); |
| EXPECT_EQ(FPDF_TEXTRENDERMODE_FILL, |
| FPDFTextObj_GetTextRenderMode(page_object)); |
| |
| // Cannot set UNKNOWN as a render mode. |
| EXPECT_FALSE(FPDFTextObj_SetTextRenderMode(page_object, |
| FPDF_TEXTRENDERMODE_UNKNOWN)); |
| |
| EXPECT_TRUE( |
| FPDFTextObj_SetTextRenderMode(page_object, FPDF_TEXTRENDERMODE_STROKE)); |
| EXPECT_EQ(FPDF_TEXTRENDERMODE_STROKE, |
| FPDFTextObj_GetTextRenderMode(page_object)); |
| |
| // Check that bitmap displays changed content |
| { |
| ScopedFPDFBitmap page_bitmap = RenderPage(page); |
| CompareBitmap(page_bitmap.get(), 612, 446, stroke_checksum); |
| } |
| |
| // Save a copy. |
| EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| |
| UnloadPage(page); |
| } |
| |
| { |
| // Open the saved copy and render it. Check that the changed text render |
| // mode is kept in the saved copy. |
| ASSERT_TRUE(OpenSavedDocument()); |
| FPDF_PAGE saved_page = LoadSavedPage(0); |
| ASSERT_TRUE(saved_page); |
| |
| FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, 0); |
| EXPECT_TRUE(page_object); |
| EXPECT_EQ(FPDF_TEXTRENDERMODE_STROKE, |
| FPDFTextObj_GetTextRenderMode(page_object)); |
| |
| ScopedFPDFBitmap bitmap = RenderSavedPage(saved_page); |
| CompareBitmap(bitmap.get(), 612, 446, stroke_checksum); |
| |
| CloseSavedPage(saved_page); |
| CloseSavedDocument(); |
| } |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, TextFontProperties) { |
| // bad object tests |
| EXPECT_FALSE(FPDFTextObj_GetFont(nullptr)); |
| EXPECT_EQ(0U, FPDFFont_GetFontName(nullptr, nullptr, 5)); |
| EXPECT_EQ(-1, FPDFFont_GetFlags(nullptr)); |
| EXPECT_EQ(-1, FPDFFont_GetWeight(nullptr)); |
| EXPECT_FALSE(FPDFFont_GetItalicAngle(nullptr, nullptr)); |
| EXPECT_FALSE(FPDFFont_GetAscent(nullptr, 12.f, nullptr)); |
| EXPECT_FALSE(FPDFFont_GetDescent(nullptr, 12.f, nullptr)); |
| EXPECT_FALSE(FPDFFont_GetGlyphWidth(nullptr, 's', 12.f, nullptr)); |
| EXPECT_FALSE(FPDFFont_GetGlyphPath(nullptr, 's', 12.f)); |
| |
| // good object tests |
| ASSERT_TRUE(OpenDocument("text_font.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT text = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(text); |
| float font_size; |
| ASSERT_TRUE(FPDFTextObj_GetFontSize(text, &font_size)); |
| FPDF_FONT font = FPDFTextObj_GetFont(text); |
| ASSERT_TRUE(font); |
| |
| // null return pointer tests |
| EXPECT_FALSE(FPDFFont_GetItalicAngle(font, nullptr)); |
| EXPECT_FALSE(FPDFFont_GetAscent(font, font_size, nullptr)); |
| EXPECT_FALSE(FPDFFont_GetDescent(font, font_size, nullptr)); |
| EXPECT_FALSE(FPDFFont_GetGlyphWidth(font, 's', font_size, nullptr)); |
| |
| // correct property tests |
| { |
| EXPECT_EQ(4, FPDFFont_GetFlags(font)); |
| EXPECT_EQ(400, FPDFFont_GetWeight(font)); |
| |
| int angle; |
| EXPECT_TRUE(FPDFFont_GetItalicAngle(font, &angle)); |
| EXPECT_EQ(0, angle); |
| |
| float ascent; |
| EXPECT_TRUE(FPDFFont_GetAscent(font, font_size, &ascent)); |
| EXPECT_FLOAT_EQ(891 * font_size / 1000.0f, ascent); |
| |
| float descent; |
| EXPECT_TRUE(FPDFFont_GetDescent(font, font_size, &descent)); |
| EXPECT_FLOAT_EQ(-216 * font_size / 1000.0f, descent); |
| |
| float a12; |
| float a24; |
| EXPECT_TRUE(FPDFFont_GetGlyphWidth(font, 'a', 12.0f, &a12)); |
| EXPECT_FLOAT_EQ(a12, 5.316f); |
| EXPECT_TRUE(FPDFFont_GetGlyphWidth(font, 'a', 24.0f, &a24)); |
| EXPECT_FLOAT_EQ(a24, 10.632f); |
| } |
| |
| { |
| // FPDFFont_GetFontName() positive testing. |
| unsigned long size = FPDFFont_GetFontName(font, nullptr, 0); |
| const char kExpectedFontName[] = "Liberation Serif"; |
| ASSERT_EQ(sizeof(kExpectedFontName), size); |
| std::vector<char> font_name(size); |
| ASSERT_EQ(size, FPDFFont_GetFontName(font, font_name.data(), size)); |
| ASSERT_STREQ(kExpectedFontName, font_name.data()); |
| |
| // FPDFFont_GetFontName() negative testing. |
| ASSERT_EQ(0U, FPDFFont_GetFontName(nullptr, nullptr, 0)); |
| |
| font_name.resize(2); |
| font_name[0] = 'x'; |
| font_name[1] = '\0'; |
| size = FPDFFont_GetFontName(font, font_name.data(), font_name.size()); |
| ASSERT_EQ(sizeof(kExpectedFontName), size); |
| ASSERT_STREQ("x", font_name.data()); |
| } |
| |
| { |
| // FPDFFont_GetFontData() positive testing. |
| constexpr size_t kExpectedSize = 8268; |
| std::vector<uint8_t> buf; |
| size_t buf_bytes_required = 123; |
| ASSERT_TRUE(FPDFFont_GetFontData(font, nullptr, 0, &buf_bytes_required)); |
| ASSERT_EQ(kExpectedSize, buf_bytes_required); |
| |
| buf.resize(kExpectedSize); |
| EXPECT_EQ("495800b8e56e2d37f3bc48a1b52db952", GenerateMD5Base16(buf)); |
| buf_bytes_required = 234; |
| // Test with buffer that is too small. Make sure `buf` is unchanged. |
| EXPECT_TRUE(FPDFFont_GetFontData(font, buf.data(), buf.size() - 1, |
| &buf_bytes_required)); |
| EXPECT_EQ("495800b8e56e2d37f3bc48a1b52db952", GenerateMD5Base16(buf)); |
| EXPECT_EQ(kExpectedSize, buf_bytes_required); |
| |
| // Test with buffer of the correct size. |
| buf_bytes_required = 234; |
| EXPECT_TRUE(FPDFFont_GetFontData(font, buf.data(), buf.size(), |
| &buf_bytes_required)); |
| EXPECT_EQ("1a67be75f719b6c476804d85bb9e4844", GenerateMD5Base16(buf)); |
| EXPECT_EQ(kExpectedSize, buf_bytes_required); |
| |
| // FPDFFont_GetFontData() negative testing. |
| EXPECT_FALSE(FPDFFont_GetFontData(nullptr, nullptr, 0, nullptr)); |
| EXPECT_FALSE(FPDFFont_GetFontData(font, nullptr, 0, nullptr)); |
| |
| buf_bytes_required = 345; |
| EXPECT_FALSE( |
| FPDFFont_GetFontData(nullptr, nullptr, 0, &buf_bytes_required)); |
| EXPECT_EQ(345u, buf_bytes_required); |
| |
| EXPECT_FALSE( |
| FPDFFont_GetFontData(nullptr, buf.data(), buf.size(), nullptr)); |
| EXPECT_FALSE(FPDFFont_GetFontData(font, buf.data(), buf.size(), nullptr)); |
| |
| buf_bytes_required = 345; |
| EXPECT_FALSE(FPDFFont_GetFontData(nullptr, buf.data(), buf.size(), |
| &buf_bytes_required)); |
| EXPECT_EQ(345u, buf_bytes_required); |
| } |
| { |
| ASSERT_EQ(1, FPDFFont_GetIsEmbedded(font)); |
| ASSERT_EQ(-1, FPDFFont_GetIsEmbedded(nullptr)); |
| } |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, NoEmbeddedFontData) { |
| ASSERT_TRUE(OpenDocument("hello_world.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| |
| // Since hello_world.pdf does not embed any font data, FPDFFont_GetFontData() |
| // will return the substitution font data. Since pdfium_embeddertest is |
| // hermetic, this first object consistently maps to Tinos-Regular.ttf. |
| constexpr size_t kTinosRegularSize = 469968; |
| FPDF_PAGEOBJECT text = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(text); |
| FPDF_FONT font = FPDFTextObj_GetFont(text); |
| ASSERT_TRUE(font); |
| std::vector<uint8_t> buf; |
| buf.resize(kTinosRegularSize); |
| size_t buf_bytes_required; |
| ASSERT_TRUE( |
| FPDFFont_GetFontData(font, buf.data(), buf.size(), &buf_bytes_required)); |
| EXPECT_EQ(kTinosRegularSize, buf_bytes_required); |
| EXPECT_EQ("2b019558f2c2de0b7cbc0a6e64b20599", GenerateMD5Base16(buf)); |
| EXPECT_EQ(0, FPDFFont_GetIsEmbedded(font)); |
| |
| // Similarly, the second object consistently maps to Arimo-Regular.ttf. |
| constexpr size_t kArimoRegularSize = 436180; |
| text = FPDFPage_GetObject(page, 1); |
| ASSERT_TRUE(text); |
| font = FPDFTextObj_GetFont(text); |
| ASSERT_TRUE(font); |
| buf.resize(kArimoRegularSize); |
| ASSERT_TRUE( |
| FPDFFont_GetFontData(font, buf.data(), buf.size(), &buf_bytes_required)); |
| EXPECT_EQ(kArimoRegularSize, buf_bytes_required); |
| EXPECT_EQ("7ac02a544211773d9636e056e9da6c35", GenerateMD5Base16(buf)); |
| EXPECT_EQ(0, FPDFFont_GetIsEmbedded(font)); |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, GlyphPaths) { |
| // bad glyphpath |
| EXPECT_EQ(-1, FPDFGlyphPath_CountGlyphSegments(nullptr)); |
| EXPECT_FALSE(FPDFGlyphPath_GetGlyphPathSegment(nullptr, 1)); |
| |
| ASSERT_TRUE(OpenDocument("text_font.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| FPDF_PAGEOBJECT text = FPDFPage_GetObject(page, 0); |
| ASSERT_TRUE(text); |
| FPDF_FONT font = FPDFTextObj_GetFont(text); |
| ASSERT_TRUE(font); |
| |
| // bad glyph argument. |
| ASSERT_FALSE(FPDFFont_GetGlyphPath(font, 1, 12.0f)); |
| |
| // good glyphpath |
| FPDF_GLYPHPATH gpath = FPDFFont_GetGlyphPath(font, 's', 12.0f); |
| ASSERT_TRUE(gpath); |
| |
| int count = FPDFGlyphPath_CountGlyphSegments(gpath); |
| ASSERT_GT(count, 0); |
| EXPECT_FALSE(FPDFGlyphPath_GetGlyphPathSegment(gpath, -1)); |
| EXPECT_FALSE(FPDFGlyphPath_GetGlyphPathSegment(gpath, count)); |
| |
| FPDF_PATHSEGMENT segment = FPDFGlyphPath_GetGlyphPathSegment(gpath, 1); |
| ASSERT_TRUE(segment); |
| EXPECT_EQ(FPDF_SEGMENT_BEZIERTO, FPDFPathSegment_GetType(segment)); |
| |
| UnloadPage(page); |
| } |
| |
| TEST_F(FPDFEditEmbedderTest, FormGetObjects) { |
| ASSERT_TRUE(OpenDocument("form_object.pdf")); |
| FPDF_PAGE page = LoadPage(0); |
| ASSERT_TRUE(page); |
| ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| |
| FPDF_PAGEOBJECT form = FPDFP
|