Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 1 | // Copyright 2016 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <string> |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 7 | #include <utility> |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 8 | #include <vector> |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 9 | |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 10 | #include "core/fpdfapi/font/cpdf_font.h" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 11 | #include "core/fpdfapi/page/cpdf_page.h" |
Henrique Nakashima | 6eb7939 | 2018-06-12 20:27:35 +0000 | [diff] [blame] | 12 | #include "core/fpdfapi/page/cpdf_pageobject.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_array.h" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 15 | #include "core/fpdfapi/parser/cpdf_number.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 16 | #include "core/fpdfapi/parser/cpdf_stream.h" |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 17 | #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 18 | #include "core/fxcrt/fx_system.h" |
Dan Sinclair | 00d47a6 | 2018-03-28 18:39:04 +0000 | [diff] [blame] | 19 | #include "fpdfsdk/cpdfsdk_helpers.h" |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 20 | #include "public/cpp/fpdf_scopers.h" |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 21 | #include "public/fpdf_annot.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 22 | #include "public/fpdf_edit.h" |
| 23 | #include "public/fpdfview.h" |
| 24 | #include "testing/embedder_test.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 25 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 27 | #include "testing/test_support.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 28 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 29 | class FPDFEditEmbeddertest : public EmbedderTest { |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 30 | protected: |
| 31 | FPDF_DOCUMENT CreateNewDocument() { |
| 32 | document_ = FPDF_CreateNewDocument(); |
Tom Sepez | 41066c1 | 2017-05-18 09:28:49 -0700 | [diff] [blame] | 33 | cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 34 | return document_; |
| 35 | } |
| 36 | |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 37 | void CheckFontDescriptor(const CPDF_Dictionary* font_dict, |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 38 | int font_type, |
| 39 | bool bold, |
| 40 | bool italic, |
| 41 | uint32_t size, |
| 42 | const uint8_t* data) { |
Lei Zhang | b1ec280 | 2018-05-25 21:55:24 +0000 | [diff] [blame] | 43 | const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 44 | ASSERT_TRUE(font_desc); |
| 45 | EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type")); |
| 46 | EXPECT_EQ(font_dict->GetStringFor("BaseFont"), |
| 47 | font_desc->GetStringFor("FontName")); |
| 48 | |
| 49 | // Check that the font descriptor has the required keys according to spec |
| 50 | // 1.7 Table 5.19 |
| 51 | ASSERT_TRUE(font_desc->KeyExist("Flags")); |
Dan Sinclair | 10e1f05 | 2017-09-28 15:59:42 -0400 | [diff] [blame] | 52 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 53 | int font_flags = font_desc->GetIntegerFor("Flags"); |
Dan Sinclair | 10e1f05 | 2017-09-28 15:59:42 -0400 | [diff] [blame] | 54 | EXPECT_EQ(bold, FontStyleIsBold(font_flags)); |
| 55 | EXPECT_EQ(italic, FontStyleIsItalic(font_flags)); |
| 56 | EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 57 | ASSERT_TRUE(font_desc->KeyExist("FontBBox")); |
Nicolás Peña | 5f95f36 | 2017-09-28 13:00:45 +0900 | [diff] [blame] | 58 | |
Lei Zhang | b1ec280 | 2018-05-25 21:55:24 +0000 | [diff] [blame] | 59 | const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox"); |
Nicolás Peña | 5f95f36 | 2017-09-28 13:00:45 +0900 | [diff] [blame] | 60 | ASSERT_TRUE(fontBBox); |
| 61 | EXPECT_EQ(4U, fontBBox->GetCount()); |
| 62 | // Check that the coordinates are in the preferred order according to spec |
| 63 | // 1.7 Section 3.8.4 |
| 64 | EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2)); |
| 65 | EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3)); |
| 66 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 67 | EXPECT_TRUE(font_desc->KeyExist("ItalicAngle")); |
| 68 | EXPECT_TRUE(font_desc->KeyExist("Ascent")); |
| 69 | EXPECT_TRUE(font_desc->KeyExist("Descent")); |
| 70 | EXPECT_TRUE(font_desc->KeyExist("CapHeight")); |
| 71 | EXPECT_TRUE(font_desc->KeyExist("StemV")); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 72 | ByteString present("FontFile"); |
| 73 | ByteString absent("FontFile2"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 74 | if (font_type == FPDF_FONT_TRUETYPE) |
| 75 | std::swap(present, absent); |
| 76 | EXPECT_TRUE(font_desc->KeyExist(present)); |
| 77 | EXPECT_FALSE(font_desc->KeyExist(absent)); |
| 78 | |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 79 | auto streamAcc = |
| 80 | pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present)); |
| 81 | streamAcc->LoadAllDataRaw(); |
| 82 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 83 | // Check that the font stream is the one that was provided |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 84 | ASSERT_EQ(size, streamAcc->GetSize()); |
Nicolás Peña | 79eab23 | 2017-09-28 13:29:05 +0900 | [diff] [blame] | 85 | if (font_type == FPDF_FONT_TRUETYPE) { |
| 86 | ASSERT_EQ(static_cast<int>(size), |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 87 | streamAcc->GetDict()->GetIntegerFor("Length1")); |
Nicolás Peña | 79eab23 | 2017-09-28 13:29:05 +0900 | [diff] [blame] | 88 | } |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 89 | |
| 90 | const uint8_t* stream_data = streamAcc->GetData(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 91 | for (size_t j = 0; j < size; j++) |
| 92 | EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j; |
| 93 | } |
| 94 | |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 95 | void CheckCompositeFontWidths(const CPDF_Array* widths_array, |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 96 | CPDF_Font* typed_font) { |
| 97 | // Check that W array is in a format that conforms to PDF spec 1.7 section |
| 98 | // "Glyph Metrics in CIDFonts" (these checks are not |
| 99 | // implementation-specific). |
| 100 | EXPECT_GT(widths_array->GetCount(), 1U); |
| 101 | int num_cids_checked = 0; |
| 102 | int cur_cid = 0; |
| 103 | for (size_t idx = 0; idx < widths_array->GetCount(); idx++) { |
| 104 | int cid = widths_array->GetNumberAt(idx); |
| 105 | EXPECT_GE(cid, cur_cid); |
| 106 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 107 | const CPDF_Object* next = widths_array->GetObjectAt(idx); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 108 | if (next->IsArray()) { |
| 109 | // We are in the c [w1 w2 ...] case |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 110 | const CPDF_Array* arr = next->AsArray(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 111 | int cnt = static_cast<int>(arr->GetCount()); |
| 112 | size_t inner_idx = 0; |
| 113 | for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) { |
Nicolas Pena | 2334660 | 2018-01-30 21:42:41 +0000 | [diff] [blame] | 114 | uint32_t width = arr->GetNumberAt(inner_idx++); |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 115 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) |
| 116 | << " at cid " << cur_cid; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 117 | } |
| 118 | num_cids_checked += cnt; |
| 119 | continue; |
| 120 | } |
| 121 | // Otherwise, are in the c_first c_last w case. |
| 122 | ASSERT_TRUE(next->IsNumber()); |
| 123 | int last_cid = next->AsNumber()->GetInteger(); |
| 124 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
Nicolas Pena | 2334660 | 2018-01-30 21:42:41 +0000 | [diff] [blame] | 125 | uint32_t width = widths_array->GetNumberAt(idx); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 126 | for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) { |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 127 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) |
| 128 | << " at cid " << cur_cid; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 129 | } |
| 130 | num_cids_checked += last_cid - cid + 1; |
| 131 | } |
| 132 | // Make sure we have a good amount of cids described |
| 133 | EXPECT_GT(num_cids_checked, 900); |
| 134 | } |
| 135 | CPDF_Document* cpdf_doc() { return cpdf_doc_; } |
| 136 | |
| 137 | private: |
| 138 | CPDF_Document* cpdf_doc_; |
| 139 | }; |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 140 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 141 | namespace { |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 142 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 143 | const char kExpectedPDF[] = |
| 144 | "%PDF-1.7\r\n" |
| 145 | "%\xA1\xB3\xC5\xD7\r\n" |
| 146 | "1 0 obj\r\n" |
| 147 | "<</Pages 2 0 R /Type/Catalog>>\r\n" |
| 148 | "endobj\r\n" |
| 149 | "2 0 obj\r\n" |
| 150 | "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n" |
| 151 | "endobj\r\n" |
| 152 | "3 0 obj\r\n" |
| 153 | "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n" |
| 154 | "endobj\r\n" |
| 155 | "4 0 obj\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 156 | "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R " |
| 157 | "/Resources<</ExtGState<</FXE1 5 0 R >>>>" |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 158 | "/Rotate 0/Type/Page" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 159 | ">>\r\n" |
| 160 | "endobj\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 161 | "5 0 obj\r\n" |
| 162 | "<</BM/Normal/CA 1/ca 1>>\r\n" |
| 163 | "endobj\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 164 | "xref\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 165 | "0 6\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 166 | "0000000000 65535 f\r\n" |
| 167 | "0000000017 00000 n\r\n" |
| 168 | "0000000066 00000 n\r\n" |
| 169 | "0000000122 00000 n\r\n" |
| 170 | "0000000192 00000 n\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 171 | "0000000311 00000 n\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 172 | "trailer\r\n" |
| 173 | "<<\r\n" |
| 174 | "/Root 1 0 R\r\n" |
| 175 | "/Info 3 0 R\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 176 | "/Size 6/ID\\[<.*><.*>\\]>>\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 177 | "startxref\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 178 | "354\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 179 | "%%EOF\r\n"; |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 180 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 181 | } // namespace |
| 182 | |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 183 | TEST_F(FPDFEditEmbeddertest, EmptyCreation) { |
| 184 | EXPECT_TRUE(CreateEmptyDocument()); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 185 | FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 186 | EXPECT_NE(nullptr, page); |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 187 | // The FPDFPage_GenerateContent call should do nothing. |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 188 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 189 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 190 | |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 191 | EXPECT_THAT(GetString(), testing::MatchesRegex(std::string( |
| 192 | kExpectedPDF, sizeof(kExpectedPDF)))); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 193 | FPDF_ClosePage(page); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 194 | } |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 195 | |
| 196 | // Regression test for https://crbug.com/667012 |
| 197 | TEST_F(FPDFEditEmbeddertest, RasterizePDF) { |
| 198 | const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615"; |
| 199 | |
| 200 | // Get the bitmap for the original document/ |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 201 | ScopedFPDFBitmap orig_bitmap; |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 202 | { |
| 203 | EXPECT_TRUE(OpenDocument("black.pdf")); |
| 204 | FPDF_PAGE orig_page = LoadPage(0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 205 | ASSERT_TRUE(orig_page); |
| 206 | orig_bitmap = RenderLoadedPage(orig_page); |
| 207 | CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 208 | UnloadPage(orig_page); |
| 209 | } |
| 210 | |
| 211 | // Create a new document from |orig_bitmap| and save it. |
| 212 | { |
| 213 | FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); |
| 214 | FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792); |
| 215 | |
| 216 | // Add the bitmap to an image object and add the image object to the output |
| 217 | // page. |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 218 | FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 219 | EXPECT_TRUE( |
| 220 | FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get())); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 221 | EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0)); |
| 222 | FPDFPage_InsertObject(temp_page, temp_img); |
| 223 | EXPECT_TRUE(FPDFPage_GenerateContent(temp_page)); |
| 224 | EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0)); |
| 225 | FPDF_ClosePage(temp_page); |
| 226 | FPDF_CloseDocument(temp_doc); |
| 227 | } |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 228 | |
| 229 | // Get the generated content. Make sure it is at least as big as the original |
| 230 | // PDF. |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 231 | EXPECT_GT(GetString().size(), 923U); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 232 | VerifySavedDocument(612, 792, kAllBlackMd5sum); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 233 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 234 | |
| 235 | TEST_F(FPDFEditEmbeddertest, AddPaths) { |
| 236 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 237 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 238 | ASSERT_TRUE(page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 239 | |
| 240 | // We will first add a red rectangle |
| 241 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 242 | ASSERT_TRUE(red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 243 | // Expect false when trying to set colors out of range |
| 244 | EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300)); |
| 245 | EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0)); |
| 246 | |
| 247 | // Fill rectangle with red and insert to the page |
| 248 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 249 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
Miklos Vajna | 491112b | 2018-05-30 13:30:10 +0000 | [diff] [blame] | 250 | |
| 251 | int fillmode = FPDF_FILLMODE_NONE; |
| 252 | FPDF_BOOL stroke = true; |
| 253 | EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke)); |
| 254 | EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode); |
| 255 | EXPECT_FALSE(stroke); |
| 256 | |
Miklos Vajna | 97f4d67 | 2018-06-04 14:47:17 +0000 | [diff] [blame] | 257 | double matrix_a = 1; |
| 258 | double matrix_b = 2; |
| 259 | double matrix_c = 3; |
| 260 | double matrix_d = 4; |
| 261 | double matrix_e = 5; |
| 262 | double matrix_f = 6; |
| 263 | EXPECT_FALSE(FPDFPath_SetMatrix(nullptr, matrix_a, matrix_b, matrix_c, |
| 264 | matrix_d, matrix_e, matrix_f)); |
| 265 | EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c, |
| 266 | matrix_d, matrix_e, matrix_f)); |
Miklos Vajna | ac42dd2 | 2018-06-05 13:28:58 +0000 | [diff] [blame] | 267 | // Set to 0 before FPDFPath_GetMatrix() to ensure they are actually set by |
| 268 | // the function. |
| 269 | matrix_a = 0; |
| 270 | matrix_b = 0; |
| 271 | matrix_c = 0; |
| 272 | matrix_d = 0; |
| 273 | matrix_e = 0; |
| 274 | matrix_f = 0; |
Miklos Vajna | 97f4d67 | 2018-06-04 14:47:17 +0000 | [diff] [blame] | 275 | EXPECT_FALSE(FPDFPath_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c, |
| 276 | &matrix_d, &matrix_e, &matrix_f)); |
| 277 | EXPECT_TRUE(FPDFPath_GetMatrix(red_rect, &matrix_a, &matrix_b, &matrix_c, |
| 278 | &matrix_d, &matrix_e, &matrix_f)); |
| 279 | EXPECT_EQ(1, static_cast<int>(matrix_a)); |
| 280 | EXPECT_EQ(2, static_cast<int>(matrix_b)); |
| 281 | EXPECT_EQ(3, static_cast<int>(matrix_c)); |
| 282 | EXPECT_EQ(4, static_cast<int>(matrix_d)); |
| 283 | EXPECT_EQ(5, static_cast<int>(matrix_e)); |
| 284 | EXPECT_EQ(6, static_cast<int>(matrix_f)); |
| 285 | // Set back the default |
| 286 | matrix_a = 1; |
| 287 | matrix_b = 0; |
| 288 | matrix_c = 0; |
| 289 | matrix_d = 1; |
| 290 | matrix_e = 0; |
| 291 | matrix_f = 0; |
| 292 | EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c, |
| 293 | matrix_d, matrix_e, matrix_f)); |
| 294 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 295 | FPDFPage_InsertObject(page, red_rect); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 296 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 297 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 298 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 299 | "66d02eaa6181e2c069ce2ea99beda497"); |
| 300 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 301 | |
| 302 | // Now add to that a green rectangle with some medium alpha |
| 303 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40); |
| 304 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128)); |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 305 | |
Miklos Vajna | 1ef04c9 | 2017-05-08 18:14:19 +0200 | [diff] [blame] | 306 | // Make sure the type of the rectangle is a path. |
| 307 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect)); |
| 308 | |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 309 | // Make sure we get back the same color we set previously. |
| 310 | unsigned int R; |
| 311 | unsigned int G; |
| 312 | unsigned int B; |
| 313 | unsigned int A; |
| 314 | EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A)); |
| 315 | EXPECT_EQ(0U, R); |
| 316 | EXPECT_EQ(255U, G); |
| 317 | EXPECT_EQ(0U, B); |
| 318 | EXPECT_EQ(128U, A); |
| 319 | |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 320 | // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4 |
| 321 | // FXPT_TYPE::LineTo). |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 322 | ASSERT_EQ(5, FPDFPath_CountSegments(green_rect)); |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 323 | // Verify actual coordinates. |
| 324 | FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0); |
| 325 | float x; |
| 326 | float y; |
| 327 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 328 | EXPECT_EQ(100, x); |
| 329 | EXPECT_EQ(100, y); |
| 330 | EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| 331 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 332 | segment = FPDFPath_GetPathSegment(green_rect, 1); |
| 333 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 334 | EXPECT_EQ(100, x); |
| 335 | EXPECT_EQ(140, y); |
| 336 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 337 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 338 | segment = FPDFPath_GetPathSegment(green_rect, 2); |
| 339 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 340 | EXPECT_EQ(140, x); |
| 341 | EXPECT_EQ(140, y); |
| 342 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 343 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 344 | segment = FPDFPath_GetPathSegment(green_rect, 3); |
| 345 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 346 | EXPECT_EQ(140, x); |
| 347 | EXPECT_EQ(100, y); |
| 348 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 349 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 350 | segment = FPDFPath_GetPathSegment(green_rect, 4); |
| 351 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 352 | EXPECT_EQ(100, x); |
| 353 | EXPECT_EQ(100, y); |
| 354 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 355 | EXPECT_TRUE(FPDFPathSegment_GetClose(segment)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 356 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 357 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0)); |
| 358 | FPDFPage_InsertObject(page, green_rect); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 359 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 360 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 361 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 362 | "7b0b87604594e773add528fae567a558"); |
| 363 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 364 | |
| 365 | // Add a black triangle. |
| 366 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100); |
| 367 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200)); |
| 368 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 369 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200)); |
| 370 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100)); |
| 371 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 372 | |
| 373 | // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2 |
| 374 | // FXPT_TYPE::LineTo). |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 375 | ASSERT_EQ(3, FPDFPath_CountSegments(black_path)); |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 376 | // Verify actual coordinates. |
| 377 | segment = FPDFPath_GetPathSegment(black_path, 0); |
| 378 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 379 | EXPECT_EQ(400, x); |
| 380 | EXPECT_EQ(100, y); |
| 381 | EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| 382 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 383 | segment = FPDFPath_GetPathSegment(black_path, 1); |
| 384 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 385 | EXPECT_EQ(400, x); |
| 386 | EXPECT_EQ(200, y); |
| 387 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 388 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 389 | segment = FPDFPath_GetPathSegment(black_path, 2); |
| 390 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 391 | EXPECT_EQ(300, x); |
| 392 | EXPECT_EQ(100, y); |
| 393 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 394 | EXPECT_TRUE(FPDFPathSegment_GetClose(segment)); |
| 395 | // Make sure out of bounds index access fails properly. |
| 396 | EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 397 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 398 | FPDFPage_InsertObject(page, black_path); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 399 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 400 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 401 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 402 | "eadc8020a14dfcf091da2688733d8806"); |
| 403 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 404 | |
| 405 | // Now add a more complex blue path. |
| 406 | FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200); |
| 407 | EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255)); |
| 408 | EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0)); |
| 409 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230)); |
| 410 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300)); |
| 411 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325)); |
| 412 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325)); |
| 413 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400)); |
| 414 | EXPECT_TRUE(FPDFPath_Close(blue_path)); |
| 415 | FPDFPage_InsertObject(page, blue_path); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 416 | const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946"; |
| 417 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 418 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 419 | CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5); |
| 420 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 421 | |
| 422 | // Now save the result, closing the page and document |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 423 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 424 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 425 | FPDF_ClosePage(page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 426 | |
| 427 | // Render the saved result |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 428 | VerifySavedDocument(612, 792, kLastMD5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Henrique Nakashima | 27cf78d | 2018-06-14 16:22:30 +0000 | [diff] [blame] | 431 | TEST_F(FPDFEditEmbeddertest, SetText) { |
Henrique Nakashima | 5ebfd64 | 2018-06-07 15:18:55 +0000 | [diff] [blame] | 432 | // Load document with some text. |
| 433 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 434 | FPDF_PAGE page = LoadPage(0); |
| 435 | ASSERT_TRUE(page); |
| 436 | |
| 437 | // Get the "Hello, world!" text object and change it. |
| 438 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 439 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| 440 | ASSERT_TRUE(page_object); |
| 441 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 = |
| 442 | GetFPDFWideString(L"Changed for SetText test"); |
| 443 | EXPECT_TRUE(FPDFText_SetText(page_object, text1.get())); |
| 444 | |
| 445 | // Verify the "Hello, world!" text is gone and "Changed for SetText test" is |
| 446 | // now displayed. |
| 447 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 448 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 449 | const char kChangedMD5[] = "94c1e7a5af7dd9d77dc2223b1091acb7"; |
| 450 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 451 | const char kChangedMD5[] = "9d31703c1d1a3e1e9a778b1e297c9cd2"; |
| 452 | #else |
| 453 | const char kChangedMD5[] = "a0c4ea6620772991f66bf7130379b08a"; |
| 454 | #endif |
| 455 | { |
| 456 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
| 457 | CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5); |
| 458 | } |
| 459 | |
| 460 | // Now save the result. |
| 461 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 462 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 463 | |
| 464 | UnloadPage(page); |
| 465 | |
| 466 | // Re-open the file and check the changes were kept in the saved .pdf. |
| 467 | OpenSavedDocument(); |
| 468 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 469 | EXPECT_EQ(2, FPDFPage_CountObjects(saved_page)); |
| 470 | { |
| 471 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0); |
| 472 | CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5); |
| 473 | } |
| 474 | |
| 475 | CloseSavedPage(saved_page); |
| 476 | CloseSavedDocument(); |
| 477 | } |
| 478 | |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 479 | TEST_F(FPDFEditEmbeddertest, RemovePageObject) { |
| 480 | // Load document with some text. |
| 481 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 482 | FPDF_PAGE page = LoadPage(0); |
| 483 | ASSERT_TRUE(page); |
| 484 | |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 485 | // Show what the original file looks like. |
| 486 | { |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 487 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 488 | const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a"; |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 489 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 490 | const char kOriginalMD5[] = "e5a6fa28298db07484cd922f3e210c88"; |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 491 | #else |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 492 | const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed"; |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 493 | #endif |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 494 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 495 | CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5); |
| 496 | } |
| 497 | |
| 498 | // Get the "Hello, world!" text object and remove it. |
| 499 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 500 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| 501 | ASSERT_TRUE(page_object); |
| 502 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 503 | |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 504 | // Verify the "Hello, world!" text is gone. |
| 505 | { |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 506 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 507 | const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa"; |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 508 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 509 | const char kRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433"; |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 510 | #else |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 511 | const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1"; |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 512 | #endif |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 513 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 514 | CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5); |
| 515 | } |
| 516 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 517 | |
| 518 | UnloadPage(page); |
| 519 | FPDFPageObj_Destroy(page_object); |
| 520 | } |
| 521 | |
Henrique Nakashima | cbed949 | 2018-07-10 21:54:26 +0000 | [diff] [blame] | 522 | void CheckMarkCounts(FPDF_PAGE page, |
| 523 | int start_from, |
| 524 | int expected_object_count, |
| 525 | size_t expected_prime_count, |
| 526 | size_t expected_square_count, |
| 527 | size_t expected_greater_than_ten_count, |
| 528 | size_t expected_bounds_count) { |
| 529 | int object_count = FPDFPage_CountObjects(page); |
| 530 | ASSERT_EQ(expected_object_count, object_count); |
| 531 | |
| 532 | size_t prime_count = 0; |
| 533 | size_t square_count = 0; |
| 534 | size_t greater_than_ten_count = 0; |
| 535 | size_t bounds_count = 0; |
| 536 | for (int i = 0; i < object_count; ++i) { |
| 537 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| 538 | |
| 539 | int mark_count = FPDFPageObj_CountMarks(page_object); |
| 540 | for (int j = 0; j < mark_count; ++j) { |
| 541 | FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j); |
| 542 | |
| 543 | char buffer[256]; |
| 544 | ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u); |
| 545 | std::wstring name = |
| 546 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 547 | if (name == L"Prime") { |
| 548 | prime_count++; |
| 549 | } else if (name == L"Square") { |
| 550 | square_count++; |
| 551 | int expected_square = start_from + i; |
| 552 | EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark)); |
| 553 | |
| 554 | unsigned long get_param_key_return = |
| 555 | FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256); |
| 556 | ASSERT_GT(get_param_key_return, 0u); |
| 557 | EXPECT_EQ((6u + 1u) * 2u, get_param_key_return); |
| 558 | std::wstring key = |
| 559 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 560 | EXPECT_EQ(L"Factor", key); |
| 561 | |
| 562 | EXPECT_EQ(FPDF_OBJECT_NUMBER, |
Henrique Nakashima | 94230e5 | 2018-07-11 22:02:02 +0000 | [diff] [blame^] | 563 | FPDFPageObjMark_GetParamValueType(mark, "Factor")); |
Henrique Nakashima | 140dead | 2018-07-11 21:40:03 +0000 | [diff] [blame] | 564 | int square_root; |
Henrique Nakashima | 94230e5 | 2018-07-11 22:02:02 +0000 | [diff] [blame^] | 565 | EXPECT_TRUE( |
| 566 | FPDFPageObjMark_GetParamIntValue(mark, "Factor", &square_root)); |
Henrique Nakashima | cbed949 | 2018-07-10 21:54:26 +0000 | [diff] [blame] | 567 | EXPECT_EQ(expected_square, square_root * square_root); |
| 568 | } else if (name == L"GreaterThanTen") { |
| 569 | greater_than_ten_count++; |
| 570 | } else if (name == L"Bounds") { |
| 571 | bounds_count++; |
| 572 | EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark)); |
| 573 | |
| 574 | unsigned long get_param_key_return = |
| 575 | FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256); |
| 576 | ASSERT_GT(get_param_key_return, 0u); |
| 577 | EXPECT_EQ((8u + 1u) * 2u, get_param_key_return); |
| 578 | std::wstring key = |
| 579 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 580 | EXPECT_EQ(L"Position", key); |
| 581 | |
Henrique Nakashima | cbed949 | 2018-07-10 21:54:26 +0000 | [diff] [blame] | 582 | // Should be the last object. |
| 583 | EXPECT_EQ(object_count - 1, i); |
| 584 | |
| 585 | EXPECT_EQ(FPDF_OBJECT_STRING, |
Henrique Nakashima | 94230e5 | 2018-07-11 22:02:02 +0000 | [diff] [blame^] | 586 | FPDFPageObjMark_GetParamValueType(mark, "Position")); |
Henrique Nakashima | cbed949 | 2018-07-10 21:54:26 +0000 | [diff] [blame] | 587 | unsigned long length; |
Henrique Nakashima | 94230e5 | 2018-07-11 22:02:02 +0000 | [diff] [blame^] | 588 | EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "Position", |
| 589 | buffer, 256, &length)); |
Henrique Nakashima | cbed949 | 2018-07-10 21:54:26 +0000 | [diff] [blame] | 590 | ASSERT_GT(length, 0u); |
| 591 | EXPECT_EQ((4u + 1u) * 2u, length); |
Henrique Nakashima | 140dead | 2018-07-11 21:40:03 +0000 | [diff] [blame] | 592 | std::wstring value = |
| 593 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
Henrique Nakashima | cbed949 | 2018-07-10 21:54:26 +0000 | [diff] [blame] | 594 | EXPECT_EQ(L"Last", value); |
| 595 | } else { |
| 596 | FAIL(); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // Expect certain number of tagged objects. The test file contains strings |
| 602 | // from 1 to 19. |
| 603 | EXPECT_EQ(expected_prime_count, prime_count); |
| 604 | EXPECT_EQ(expected_square_count, square_count); |
| 605 | EXPECT_EQ(expected_greater_than_ten_count, greater_than_ten_count); |
| 606 | EXPECT_EQ(expected_bounds_count, bounds_count); |
| 607 | } |
| 608 | |
| 609 | TEST_F(FPDFEditEmbeddertest, ReadMarkedObjectsIndirectDict) { |
| 610 | // Load document with some text marked with an indirect property. |
| 611 | EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf")); |
| 612 | FPDF_PAGE page = LoadPage(0); |
| 613 | ASSERT_TRUE(page); |
| 614 | |
| 615 | CheckMarkCounts(page, 1, 19, 8, 4, 9, 1); |
| 616 | |
| 617 | UnloadPage(page); |
| 618 | } |
| 619 | |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 620 | TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) { |
| 621 | // Load document with some text. |
| 622 | EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf")); |
| 623 | FPDF_PAGE page = LoadPage(0); |
| 624 | ASSERT_TRUE(page); |
| 625 | |
| 626 | // Show what the original file looks like. |
| 627 | { |
| 628 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 629 | const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df"; |
| 630 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 631 | const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655"; |
| 632 | #else |
| 633 | const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86"; |
| 634 | #endif |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 635 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 636 | CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5); |
| 637 | } |
| 638 | |
Henrique Nakashima | cbed949 | 2018-07-10 21:54:26 +0000 | [diff] [blame] | 639 | constexpr int expected_object_count = 19; |
| 640 | CheckMarkCounts(page, 1, expected_object_count, 8, 4, 9, 1); |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 641 | |
Henrique Nakashima | cbed949 | 2018-07-10 21:54:26 +0000 | [diff] [blame] | 642 | // Get all objects marked with "Prime" |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 643 | std::vector<FPDF_PAGEOBJECT> primes; |
Henrique Nakashima | cbed949 | 2018-07-10 21:54:26 +0000 | [diff] [blame] | 644 | for (int i = 0; i < expected_object_count; ++i) { |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 645 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| 646 | |
| 647 | int mark_count = FPDFPageObj_CountMarks(page_object); |
| 648 | for (int j = 0; j < mark_count; ++j) { |
| 649 | FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j); |
| 650 | |
| 651 | char buffer[256]; |
| 652 | ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u); |
| 653 | std::wstring name = |
| 654 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 655 | if (name == L"Prime") { |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 656 | primes.push_back(page_object); |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 661 | // Remove all objects marked with "Prime". |
| 662 | for (FPDF_PAGEOBJECT page_object : primes) { |
| 663 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 664 | FPDFPageObj_Destroy(page_object); |
| 665 | } |
| 666 | |
| 667 | EXPECT_EQ(11, FPDFPage_CountObjects(page)); |
| 668 | |
| 669 | { |
| 670 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 671 | const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65"; |
| 672 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 673 | const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c"; |
| 674 | #else |
| 675 | const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922"; |
| 676 | #endif |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 677 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 678 | CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5); |
| 679 | } |
| 680 | |
| 681 | UnloadPage(page); |
| 682 | } |
| 683 | |
Henrique Nakashima | b4bcf69 | 2018-07-11 21:19:22 +0000 | [diff] [blame] | 684 | TEST_F(FPDFEditEmbeddertest, MaintainMarkedObjects) { |
| 685 | // Load document with some text. |
| 686 | EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf")); |
| 687 | FPDF_PAGE page = LoadPage(0); |
| 688 | ASSERT_TRUE(page); |
| 689 | |
| 690 | // Iterate over all objects, counting the number of times each content mark |
| 691 | // name appears. |
| 692 | CheckMarkCounts(page, 1, 19, 8, 4, 9, 1); |
| 693 | |
| 694 | // Remove first page object. |
| 695 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| 696 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 697 | FPDFPageObj_Destroy(page_object); |
| 698 | |
| 699 | CheckMarkCounts(page, 2, 18, 8, 3, 9, 1); |
| 700 | |
| 701 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 702 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 703 | |
| 704 | UnloadPage(page); |
| 705 | |
| 706 | OpenSavedDocument(); |
| 707 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 708 | |
| 709 | CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1); |
| 710 | |
| 711 | CloseSavedPage(saved_page); |
| 712 | CloseSavedDocument(); |
| 713 | } |
| 714 | |
| 715 | TEST_F(FPDFEditEmbeddertest, MaintainIndirectMarkedObjects) { |
| 716 | // Load document with some text. |
| 717 | EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf")); |
| 718 | FPDF_PAGE page = LoadPage(0); |
| 719 | ASSERT_TRUE(page); |
| 720 | |
| 721 | // Iterate over all objects, counting the number of times each content mark |
| 722 | // name appears. |
| 723 | CheckMarkCounts(page, 1, 19, 8, 4, 9, 1); |
| 724 | |
| 725 | // Remove first page object. |
| 726 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| 727 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 728 | FPDFPageObj_Destroy(page_object); |
| 729 | |
| 730 | CheckMarkCounts(page, 2, 18, 8, 3, 9, 1); |
| 731 | |
| 732 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 733 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 734 | |
| 735 | UnloadPage(page); |
| 736 | |
| 737 | OpenSavedDocument(); |
| 738 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 739 | |
| 740 | CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1); |
| 741 | |
| 742 | CloseSavedPage(saved_page); |
| 743 | CloseSavedDocument(); |
| 744 | } |
| 745 | |
Henrique Nakashima | 27cf78d | 2018-06-14 16:22:30 +0000 | [diff] [blame] | 746 | TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObject) { |
Henrique Nakashima | c49e62e | 2018-04-16 20:58:47 +0000 | [diff] [blame] | 747 | // Load document with some text. |
| 748 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 749 | FPDF_PAGE page = LoadPage(0); |
| 750 | ASSERT_TRUE(page); |
| 751 | |
| 752 | // Get the "Hello, world!" text object and remove it. |
| 753 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 754 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| 755 | ASSERT_TRUE(page_object); |
| 756 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 757 | |
| 758 | // Verify the "Hello, world!" text is gone. |
| 759 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 760 | |
| 761 | // Save the file |
| 762 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 763 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 764 | UnloadPage(page); |
| 765 | FPDFPageObj_Destroy(page_object); |
| 766 | |
| 767 | // Re-open the file and check the page object count is still 1. |
| 768 | OpenSavedDocument(); |
| 769 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 770 | EXPECT_EQ(1, FPDFPage_CountObjects(saved_page)); |
| 771 | CloseSavedPage(saved_page); |
| 772 | CloseSavedDocument(); |
| 773 | } |
| 774 | |
Henrique Nakashima | 27cf78d | 2018-06-14 16:22:30 +0000 | [diff] [blame] | 775 | TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsNotLonely) { |
| 776 | // Load document with some text. |
| 777 | EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf")); |
| 778 | FPDF_PAGE page = LoadPage(0); |
| 779 | ASSERT_TRUE(page); |
| 780 | |
| 781 | // Get the "Hello, world!" text object and remove it. There is another object |
| 782 | // in the same stream that says "Goodbye, world!" |
| 783 | ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| 784 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| 785 | ASSERT_TRUE(page_object); |
| 786 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 787 | |
| 788 | // Verify the "Hello, world!" text is gone. |
| 789 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 790 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 791 | const char kHelloRemovedMD5[] = "e07a62d412728fc4d6e3ff42f2dd0e11"; |
| 792 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 793 | const char kHelloRemovedMD5[] = "de37b0bb7ff903c1068bae361844be50"; |
| 794 | #else |
| 795 | const char kHelloRemovedMD5[] = "95b92950647a2190e1230911e7a1a0e9"; |
| 796 | #endif |
| 797 | { |
| 798 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
| 799 | CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5); |
| 800 | } |
| 801 | |
| 802 | // Save the file |
| 803 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 804 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 805 | UnloadPage(page); |
| 806 | FPDFPageObj_Destroy(page_object); |
| 807 | |
| 808 | // Re-open the file and check the page object count is still 2. |
| 809 | OpenSavedDocument(); |
| 810 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 811 | |
| 812 | EXPECT_EQ(2, FPDFPage_CountObjects(saved_page)); |
| 813 | { |
| 814 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0); |
| 815 | CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5); |
| 816 | } |
| 817 | |
| 818 | CloseSavedPage(saved_page); |
| 819 | CloseSavedDocument(); |
| 820 | } |
| 821 | |
| 822 | TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsLonely) { |
| 823 | // Load document with some text. |
| 824 | EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf")); |
| 825 | FPDF_PAGE page = LoadPage(0); |
| 826 | ASSERT_TRUE(page); |
| 827 | |
| 828 | // Get the "Greetings, world!" text object and remove it. This is the only |
| 829 | // object in the stream. |
| 830 | ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| 831 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 2); |
| 832 | ASSERT_TRUE(page_object); |
| 833 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 834 | |
| 835 | // Verify the "Greetings, world!" text is gone. |
| 836 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 837 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 838 | const char kGreetingsRemovedMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a"; |
| 839 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 840 | const char kGreetingsRemovedMD5[] = "e5a6fa28298db07484cd922f3e210c88"; |
| 841 | #else |
| 842 | const char kGreetingsRemovedMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed"; |
| 843 | #endif |
| 844 | { |
| 845 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
| 846 | CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5); |
| 847 | } |
| 848 | |
| 849 | // Save the file |
| 850 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 851 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 852 | UnloadPage(page); |
| 853 | FPDFPageObj_Destroy(page_object); |
| 854 | |
| 855 | // Re-open the file and check the page object count is still 2. |
| 856 | OpenSavedDocument(); |
| 857 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 858 | |
| 859 | EXPECT_EQ(2, FPDFPage_CountObjects(saved_page)); |
| 860 | { |
| 861 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0); |
| 862 | CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5); |
| 863 | } |
| 864 | |
| 865 | CloseSavedPage(saved_page); |
| 866 | CloseSavedDocument(); |
| 867 | } |
| 868 | |
Henrique Nakashima | 6eb7939 | 2018-06-12 20:27:35 +0000 | [diff] [blame] | 869 | TEST_F(FPDFEditEmbeddertest, GetContentStream) { |
| 870 | // Load document with some text split across streams. |
| 871 | EXPECT_TRUE(OpenDocument("split_streams.pdf")); |
| 872 | FPDF_PAGE page = LoadPage(0); |
| 873 | ASSERT_TRUE(page); |
| 874 | |
| 875 | // Content stream 0: page objects 0-14. |
| 876 | // Content stream 1: page objects 15-17. |
| 877 | // Content stream 2: page object 18. |
| 878 | ASSERT_EQ(19, FPDFPage_CountObjects(page)); |
| 879 | for (int i = 0; i < 19; i++) { |
| 880 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| 881 | ASSERT_TRUE(page_object); |
| 882 | CPDF_PageObject* cpdf_page_object = |
| 883 | CPDFPageObjectFromFPDFPageObject(page_object); |
| 884 | if (i < 15) |
| 885 | EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i; |
| 886 | else if (i < 18) |
| 887 | EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i; |
| 888 | else |
| 889 | EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i; |
| 890 | } |
| 891 | |
| 892 | UnloadPage(page); |
| 893 | } |
| 894 | |
Henrique Nakashima | 0dcf1f4 | 2018-06-21 18:51:15 +0000 | [diff] [blame] | 895 | TEST_F(FPDFEditEmbeddertest, RemoveAllFromStream) { |
| 896 | // Load document with some text split across streams. |
| 897 | EXPECT_TRUE(OpenDocument("split_streams.pdf")); |
| 898 | FPDF_PAGE page = LoadPage(0); |
| 899 | ASSERT_TRUE(page); |
| 900 | |
| 901 | // Content stream 0: page objects 0-14. |
| 902 | // Content stream 1: page objects 15-17. |
| 903 | // Content stream 2: page object 18. |
| 904 | ASSERT_EQ(19, FPDFPage_CountObjects(page)); |
| 905 | |
| 906 | // Loop backwards because objects will being removed, which shifts the indexes |
| 907 | // after the removed position. |
| 908 | for (int i = 18; i >= 0; i--) { |
| 909 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| 910 | ASSERT_TRUE(page_object); |
| 911 | CPDF_PageObject* cpdf_page_object = |
| 912 | CPDFPageObjectFromFPDFPageObject(page_object); |
| 913 | |
| 914 | // Empty content stream 1. |
| 915 | if (cpdf_page_object->GetContentStream() == 1) { |
| 916 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 917 | FPDFPageObj_Destroy(page_object); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | // Content stream 0: page objects 0-14. |
| 922 | // Content stream 2: page object 15. |
| 923 | ASSERT_EQ(16, FPDFPage_CountObjects(page)); |
| 924 | for (int i = 0; i < 16; i++) { |
| 925 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| 926 | ASSERT_TRUE(page_object); |
| 927 | CPDF_PageObject* cpdf_page_object = |
| 928 | CPDFPageObjectFromFPDFPageObject(page_object); |
| 929 | if (i < 15) |
| 930 | EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i; |
| 931 | else |
| 932 | EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i; |
| 933 | } |
| 934 | |
| 935 | // Generate contents should remove the empty stream and update the page |
| 936 | // objects' contents stream indexes. |
| 937 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 938 | |
| 939 | // Content stream 0: page objects 0-14. |
| 940 | // Content stream 1: page object 15. |
| 941 | ASSERT_EQ(16, FPDFPage_CountObjects(page)); |
| 942 | for (int i = 0; i < 16; i++) { |
| 943 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| 944 | ASSERT_TRUE(page_object); |
| 945 | CPDF_PageObject* cpdf_page_object = |
| 946 | CPDFPageObjectFromFPDFPageObject(page_object); |
| 947 | if (i < 15) |
| 948 | EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i; |
| 949 | else |
| 950 | EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i; |
| 951 | } |
| 952 | |
| 953 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 954 | const char kStream1RemovedMD5[] = "d2e21fbd5a6de563f619feeeb6163331"; |
| 955 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 956 | const char kStream1RemovedMD5[] = "b4140f203523e38793283a5943d8075b"; |
| 957 | #else |
| 958 | const char kStream1RemovedMD5[] = "e86a3efc160ede6cfcb1f59bcacf1105"; |
| 959 | #endif |
| 960 | { |
| 961 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
| 962 | CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5); |
| 963 | } |
| 964 | |
| 965 | // Save the file |
| 966 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 967 | UnloadPage(page); |
| 968 | |
| 969 | // Re-open the file and check the page object count is still 16, and that |
| 970 | // content stream 1 was removed. |
| 971 | OpenSavedDocument(); |
| 972 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 973 | |
| 974 | // Content stream 0: page objects 0-14. |
| 975 | // Content stream 1: page object 15. |
| 976 | EXPECT_EQ(16, FPDFPage_CountObjects(saved_page)); |
| 977 | for (int i = 0; i < 16; i++) { |
| 978 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i); |
| 979 | ASSERT_TRUE(page_object); |
| 980 | CPDF_PageObject* cpdf_page_object = |
| 981 | CPDFPageObjectFromFPDFPageObject(page_object); |
| 982 | if (i < 15) |
| 983 | EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i; |
| 984 | else |
| 985 | EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i; |
| 986 | } |
| 987 | |
| 988 | { |
| 989 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0); |
| 990 | CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5); |
| 991 | } |
| 992 | |
| 993 | CloseSavedPage(saved_page); |
| 994 | CloseSavedDocument(); |
| 995 | } |
| 996 | |
| 997 | TEST_F(FPDFEditEmbeddertest, RemoveAllFromSingleStream) { |
| 998 | // Load document with a single stream. |
| 999 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 1000 | FPDF_PAGE page = LoadPage(0); |
| 1001 | ASSERT_TRUE(page); |
| 1002 | |
| 1003 | // Content stream 0: page objects 0-1. |
| 1004 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 1005 | |
| 1006 | // Loop backwards because objects will being removed, which shifts the indexes |
| 1007 | // after the removed position. |
| 1008 | for (int i = 1; i >= 0; i--) { |
| 1009 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| 1010 | ASSERT_TRUE(page_object); |
| 1011 | CPDF_PageObject* cpdf_page_object = |
| 1012 | CPDFPageObjectFromFPDFPageObject(page_object); |
| 1013 | ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| 1014 | ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 1015 | FPDFPageObj_Destroy(page_object); |
| 1016 | } |
| 1017 | |
| 1018 | // No more objects in the stream |
| 1019 | ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| 1020 | |
| 1021 | // Generate contents should remove the empty stream and update the page |
| 1022 | // objects' contents stream indexes. |
| 1023 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1024 | |
| 1025 | ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| 1026 | |
| 1027 | const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c"; |
| 1028 | { |
| 1029 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
| 1030 | CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5); |
| 1031 | } |
| 1032 | |
| 1033 | // Save the file |
| 1034 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1035 | UnloadPage(page); |
| 1036 | |
| 1037 | // Re-open the file and check the page object count is still 0. |
| 1038 | OpenSavedDocument(); |
| 1039 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1040 | |
| 1041 | EXPECT_EQ(0, FPDFPage_CountObjects(saved_page)); |
| 1042 | { |
| 1043 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0); |
| 1044 | CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5); |
| 1045 | } |
| 1046 | |
| 1047 | CloseSavedPage(saved_page); |
| 1048 | CloseSavedDocument(); |
| 1049 | } |
| 1050 | |
| 1051 | TEST_F(FPDFEditEmbeddertest, RemoveFirstFromSingleStream) { |
| 1052 | // Load document with a single stream. |
| 1053 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 1054 | FPDF_PAGE page = LoadPage(0); |
| 1055 | ASSERT_TRUE(page); |
| 1056 | |
| 1057 | // Content stream 0: page objects 0-1. |
| 1058 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 1059 | |
| 1060 | // Remove first object. |
| 1061 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| 1062 | ASSERT_TRUE(page_object); |
| 1063 | CPDF_PageObject* cpdf_page_object = |
| 1064 | CPDFPageObjectFromFPDFPageObject(page_object); |
| 1065 | ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| 1066 | ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 1067 | FPDFPageObj_Destroy(page_object); |
| 1068 | |
| 1069 | // One object left in the stream. |
| 1070 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 1071 | page_object = FPDFPage_GetObject(page, 0); |
| 1072 | ASSERT_TRUE(page_object); |
| 1073 | cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| 1074 | ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| 1075 | |
| 1076 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1077 | |
| 1078 | // Still one object left in the stream. |
| 1079 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 1080 | page_object = FPDFPage_GetObject(page, 0); |
| 1081 | ASSERT_TRUE(page_object); |
| 1082 | cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| 1083 | ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| 1084 | |
| 1085 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 1086 | const char kFirstRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa"; |
| 1087 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 1088 | const char kFirstRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433"; |
| 1089 | #else |
| 1090 | const char kFirstRemovedMD5[] = "b76df015fe88009c3c342395df96abf1"; |
| 1091 | #endif |
| 1092 | { |
| 1093 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
| 1094 | CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5); |
| 1095 | } |
| 1096 | |
| 1097 | // Save the file |
| 1098 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1099 | UnloadPage(page); |
| 1100 | |
| 1101 | // Re-open the file and check the page object count is still 0. |
| 1102 | OpenSavedDocument(); |
| 1103 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1104 | |
| 1105 | ASSERT_EQ(1, FPDFPage_CountObjects(saved_page)); |
| 1106 | page_object = FPDFPage_GetObject(saved_page, 0); |
| 1107 | ASSERT_TRUE(page_object); |
| 1108 | cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| 1109 | ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| 1110 | { |
| 1111 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0); |
| 1112 | CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5); |
| 1113 | } |
| 1114 | |
| 1115 | CloseSavedPage(saved_page); |
| 1116 | CloseSavedDocument(); |
| 1117 | } |
| 1118 | |
| 1119 | TEST_F(FPDFEditEmbeddertest, RemoveLastFromSingleStream) { |
| 1120 | // Load document with a single stream. |
| 1121 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 1122 | FPDF_PAGE page = LoadPage(0); |
| 1123 | ASSERT_TRUE(page); |
| 1124 | |
| 1125 | // Content stream 0: page objects 0-1. |
| 1126 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 1127 | |
| 1128 | // Remove last object |
| 1129 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 1); |
| 1130 | ASSERT_TRUE(page_object); |
| 1131 | CPDF_PageObject* cpdf_page_object = |
| 1132 | CPDFPageObjectFromFPDFPageObject(page_object); |
| 1133 | ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| 1134 | ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 1135 | FPDFPageObj_Destroy(page_object); |
| 1136 | |
| 1137 | // One object left in the stream. |
| 1138 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 1139 | page_object = FPDFPage_GetObject(page, 0); |
| 1140 | ASSERT_TRUE(page_object); |
| 1141 | cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| 1142 | ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| 1143 | |
| 1144 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1145 | |
| 1146 | // Still one object left in the stream. |
| 1147 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 1148 | page_object = FPDFPage_GetObject(page, 0); |
| 1149 | ASSERT_TRUE(page_object); |
| 1150 | cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| 1151 | ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| 1152 | |
| 1153 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 1154 | const char kLastRemovedMD5[] = "f8fbd14a048b9e2ea8e5f059f22a910e"; |
| 1155 | #else |
| 1156 | const char kLastRemovedMD5[] = "93dcc09055f87a2792c8e3065af99a1b"; |
| 1157 | #endif |
| 1158 | { |
| 1159 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
| 1160 | CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5); |
| 1161 | } |
| 1162 | |
| 1163 | // Save the file |
| 1164 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1165 | UnloadPage(page); |
| 1166 | |
| 1167 | // Re-open the file and check the page object count is still 0. |
| 1168 | OpenSavedDocument(); |
| 1169 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1170 | |
| 1171 | ASSERT_EQ(1, FPDFPage_CountObjects(saved_page)); |
| 1172 | page_object = FPDFPage_GetObject(saved_page, 0); |
| 1173 | ASSERT_TRUE(page_object); |
| 1174 | cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object); |
| 1175 | ASSERT_EQ(0, cpdf_page_object->GetContentStream()); |
| 1176 | { |
| 1177 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0); |
| 1178 | CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5); |
| 1179 | } |
| 1180 | |
| 1181 | CloseSavedPage(saved_page); |
| 1182 | CloseSavedDocument(); |
| 1183 | } |
| 1184 | |
| 1185 | TEST_F(FPDFEditEmbeddertest, RemoveAllFromMultipleStreams) { |
| 1186 | // Load document with some text. |
| 1187 | EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf")); |
| 1188 | FPDF_PAGE page = LoadPage(0); |
| 1189 | ASSERT_TRUE(page); |
| 1190 | |
| 1191 | // Content stream 0: page objects 0-1. |
| 1192 | // Content stream 1: page object 2. |
| 1193 | ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| 1194 | |
| 1195 | // Loop backwards because objects will being removed, which shifts the indexes |
| 1196 | // after the removed position. |
| 1197 | for (int i = 2; i >= 0; i--) { |
| 1198 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| 1199 | ASSERT_TRUE(page_object); |
| 1200 | ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 1201 | FPDFPageObj_Destroy(page_object); |
| 1202 | } |
| 1203 | |
| 1204 | // No more objects in the page. |
| 1205 | ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| 1206 | |
| 1207 | // Generate contents should remove the empty streams and update the page |
| 1208 | // objects' contents stream indexes. |
| 1209 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1210 | |
| 1211 | ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| 1212 | |
| 1213 | const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c"; |
| 1214 | { |
| 1215 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
| 1216 | CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5); |
| 1217 | } |
| 1218 | |
| 1219 | // Save the file |
| 1220 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1221 | UnloadPage(page); |
| 1222 | |
| 1223 | // Re-open the file and check the page object count is still 0. |
| 1224 | OpenSavedDocument(); |
| 1225 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1226 | |
| 1227 | EXPECT_EQ(0, FPDFPage_CountObjects(saved_page)); |
| 1228 | { |
| 1229 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0); |
| 1230 | CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5); |
| 1231 | } |
| 1232 | |
| 1233 | CloseSavedPage(saved_page); |
| 1234 | CloseSavedDocument(); |
| 1235 | } |
| 1236 | |
Henrique Nakashima | c49e62e | 2018-04-16 20:58:47 +0000 | [diff] [blame] | 1237 | TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) { |
| 1238 | // Load document with some text. |
| 1239 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 1240 | FPDF_PAGE page = LoadPage(0); |
| 1241 | ASSERT_TRUE(page); |
| 1242 | |
| 1243 | // Add a red rectangle. |
| 1244 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 1245 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| 1246 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 1247 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 1248 | FPDFPage_InsertObject(page, red_rect); |
| 1249 | |
| 1250 | // Verify the red rectangle was added. |
| 1251 | ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| 1252 | |
| 1253 | // Save the file |
| 1254 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1255 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1256 | UnloadPage(page); |
| 1257 | |
| 1258 | // Re-open the file and check the page object count is still 3. |
| 1259 | OpenSavedDocument(); |
| 1260 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1261 | EXPECT_EQ(3, FPDFPage_CountObjects(saved_page)); |
| 1262 | CloseSavedPage(saved_page); |
| 1263 | CloseSavedDocument(); |
| 1264 | } |
| 1265 | |
Henrique Nakashima | 27cf78d | 2018-06-14 16:22:30 +0000 | [diff] [blame] | 1266 | TEST_F(FPDFEditEmbeddertest, InsertPageObjectEditAndSave) { |
| 1267 | // Load document with some text. |
| 1268 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 1269 | FPDF_PAGE page = LoadPage(0); |
| 1270 | ASSERT_TRUE(page); |
| 1271 | |
| 1272 | // Add a red rectangle. |
| 1273 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 1274 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| 1275 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 100, 100, 255)); |
| 1276 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 1277 | FPDFPage_InsertObject(page, red_rect); |
| 1278 | |
| 1279 | // Verify the red rectangle was added. |
| 1280 | ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| 1281 | |
| 1282 | // Generate content but change it again |
| 1283 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1284 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 1285 | |
| 1286 | // Save the file |
| 1287 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1288 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1289 | UnloadPage(page); |
| 1290 | |
| 1291 | // Re-open the file and check the page object count is still 3. |
| 1292 | OpenSavedDocument(); |
| 1293 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1294 | EXPECT_EQ(3, FPDFPage_CountObjects(saved_page)); |
| 1295 | CloseSavedPage(saved_page); |
| 1296 | CloseSavedDocument(); |
| 1297 | } |
| 1298 | |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1299 | TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) { |
| 1300 | // Start with a blank page. |
| 1301 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 1302 | ASSERT_TRUE(page); |
| 1303 | |
| 1304 | // Render the blank page and verify it's a blank bitmap. |
| 1305 | const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3"; |
| 1306 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1307 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1308 | CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5); |
| 1309 | } |
| 1310 | ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| 1311 | |
| 1312 | // Add a red rectangle. |
| 1313 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| 1314 | ASSERT_TRUE(red_rect); |
| 1315 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 1316 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 1317 | FPDFPage_InsertObject(page, red_rect); |
| 1318 | const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497"; |
| 1319 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1320 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1321 | CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5); |
| 1322 | } |
| 1323 | EXPECT_EQ(1, FPDFPage_CountObjects(page)); |
| 1324 | |
| 1325 | // Remove rectangle and verify it does not render anymore and the bitmap is |
| 1326 | // back to a blank one. |
| 1327 | EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect)); |
| 1328 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1329 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 1330 | CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5); |
| 1331 | } |
| 1332 | EXPECT_EQ(0, FPDFPage_CountObjects(page)); |
| 1333 | |
| 1334 | // Trying to remove an object not in the page should return false. |
| 1335 | EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect)); |
| 1336 | |
| 1337 | FPDF_ClosePage(page); |
| 1338 | FPDFPageObj_Destroy(red_rect); |
| 1339 | } |
| 1340 | |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 1341 | TEST_F(FPDFEditEmbeddertest, PathsPoints) { |
| 1342 | CreateNewDocument(); |
| 1343 | FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_); |
| 1344 | // This should fail gracefully, even if img is not a path. |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 1345 | ASSERT_EQ(-1, FPDFPath_CountSegments(img)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 1346 | |
| 1347 | // This should fail gracefully, even if path is NULL. |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 1348 | ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 1349 | |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 1350 | // FPDFPath_GetPathSegment() with a non-path. |
| 1351 | ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0)); |
| 1352 | // FPDFPath_GetPathSegment() with a NULL path. |
| 1353 | ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0)); |
| 1354 | float x; |
| 1355 | float y; |
| 1356 | // FPDFPathSegment_GetPoint() with a NULL segment. |
| 1357 | EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y)); |
| 1358 | |
| 1359 | // FPDFPathSegment_GetType() with a NULL segment. |
| 1360 | ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr)); |
| 1361 | |
| 1362 | // FPDFPathSegment_GetClose() with a NULL segment. |
| 1363 | EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr)); |
| 1364 | |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 1365 | FPDFPageObj_Destroy(img); |
| 1366 | } |
| 1367 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 1368 | TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) { |
| 1369 | // Load document with some text |
| 1370 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 1371 | FPDF_PAGE page = LoadPage(0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1372 | ASSERT_TRUE(page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 1373 | |
| 1374 | // Add an opaque rectangle on top of some of the text. |
| 1375 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| 1376 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 1377 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 1378 | FPDFPage_InsertObject(page, red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 1379 | |
| 1380 | // Add a transparent triangle on top of other part of the text. |
| 1381 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50); |
| 1382 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100)); |
| 1383 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 1384 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80)); |
| 1385 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10)); |
| 1386 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
| 1387 | FPDFPage_InsertObject(page, black_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 1388 | |
| 1389 | // Render and check the result. Text is slightly different on Mac. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1390 | ScopedFPDFBitmap bitmap = RenderLoadedPage(page); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1391 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 1392 | const char md5[] = "f9e6fa74230f234286bfcada9f7606d8"; |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 1393 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 1394 | const char md5[] = "aa71b09b93b55f467f1290e5111babee"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 1395 | #endif |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1396 | CompareBitmap(bitmap.get(), 200, 200, md5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 1397 | UnloadPage(page); |
| 1398 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 1399 | |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 1400 | TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) { |
| 1401 | // Load document with existing content |
| 1402 | EXPECT_TRUE(OpenDocument("bug_717.pdf")); |
| 1403 | FPDF_PAGE page = LoadPage(0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1404 | ASSERT_TRUE(page); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 1405 | |
| 1406 | // Add a transparent rectangle on top of the existing content |
| 1407 | FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50); |
| 1408 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100)); |
| 1409 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
| 1410 | FPDFPage_InsertObject(page, red_rect2); |
| 1411 | |
| 1412 | // Add an opaque rectangle on top of the existing content |
| 1413 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50); |
| 1414 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 1415 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 1416 | FPDFPage_InsertObject(page, red_rect); |
| 1417 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1418 | ScopedFPDFBitmap bitmap = RenderLoadedPage(page); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1419 | CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 1420 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1421 | |
| 1422 | // Now save the result, closing the page and document |
| 1423 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 1424 | UnloadPage(page); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 1425 | |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1426 | OpenSavedDocument(); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1427 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 1428 | VerifySavedRendering(saved_page, 612, 792, |
| 1429 | "ad04e5bd0f471a9a564fb034bd0fb073"); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 1430 | |
| 1431 | ClearString(); |
| 1432 | // Add another opaque rectangle on top of the existing content |
| 1433 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50); |
| 1434 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255)); |
| 1435 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1436 | FPDFPage_InsertObject(saved_page, green_rect); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 1437 | |
| 1438 | // Add another transparent rectangle on top of existing content |
| 1439 | FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50); |
| 1440 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100)); |
| 1441 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1442 | FPDFPage_InsertObject(saved_page, green_rect2); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1443 | const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd"; |
| 1444 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1445 | ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1446 | CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5); |
| 1447 | } |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1448 | EXPECT_TRUE(FPDFPage_GenerateContent(saved_page)); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 1449 | |
| 1450 | // Now save the result, closing the page and document |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 1451 | EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0)); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1452 | |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1453 | CloseSavedPage(saved_page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1454 | CloseSavedDocument(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 1455 | |
| 1456 | // Render the saved result |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 1457 | VerifySavedDocument(612, 792, kLastMD5); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 1458 | } |
| 1459 | |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 1460 | TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) { |
| 1461 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1462 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 1463 | |
| 1464 | // Add a large stroked rectangle (fill color should not affect it). |
| 1465 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400); |
| 1466 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255)); |
| 1467 | EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255)); |
| 1468 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f)); |
Miklos Vajna | 366df7f | 2018-05-22 14:27:29 +0000 | [diff] [blame] | 1469 | |
| 1470 | float width = 0; |
| 1471 | EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width)); |
| 1472 | EXPECT_EQ(15.0f, width); |
| 1473 | |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 1474 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1)); |
| 1475 | FPDFPage_InsertObject(page, rect); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1476 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1477 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1478 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 1479 | "64bd31f862a89e0a9e505a5af6efd506"); |
| 1480 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 1481 | |
| 1482 | // Add crossed-checkmark |
| 1483 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500); |
| 1484 | EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400)); |
| 1485 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600)); |
| 1486 | EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600)); |
| 1487 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400)); |
| 1488 | EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180)); |
| 1489 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f)); |
| 1490 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 1491 | FPDFPage_InsertObject(page, check); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1492 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1493 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1494 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 1495 | "4b6f3b9d25c4e194821217d5016c3724"); |
| 1496 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 1497 | |
| 1498 | // Add stroked and filled oval-ish path. |
| 1499 | FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100); |
| 1500 | EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300)); |
| 1501 | EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305)); |
| 1502 | EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105)); |
| 1503 | EXPECT_TRUE(FPDFPath_Close(path)); |
| 1504 | EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100)); |
| 1505 | EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150)); |
| 1506 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f)); |
| 1507 | EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1)); |
| 1508 | FPDFPage_InsertObject(page, path); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1509 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1510 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1511 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 1512 | "ff3e6a22326754944cc6e56609acd73b"); |
| 1513 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 1514 | FPDF_ClosePage(page); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 1515 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1516 | |
Nicolas Pena | 4c48b10 | 2018-06-13 18:23:46 +0000 | [diff] [blame] | 1517 | // Tests adding text from standard font using FPDFPageObj_NewTextObj. |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1518 | TEST_F(FPDFEditEmbeddertest, AddStandardFontText) { |
| 1519 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1520 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1521 | |
| 1522 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1523 | FPDF_PAGEOBJECT text_object1 = |
| 1524 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 1525 | EXPECT_TRUE(text_object1); |
| 1526 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 = |
| 1527 | GetFPDFWideString(L"I'm at the bottom of the page"); |
| 1528 | EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get())); |
| 1529 | FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20); |
| 1530 | FPDFPage_InsertObject(page, text_object1); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1531 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1532 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1533 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1534 | const char md5[] = "a4dddc1a3930fa694bbff9789dab4161"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1535 | #else |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1536 | const char md5[] = "eacaa24573b8ce997b3882595f096f00"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 1537 | #endif |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1538 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
| 1539 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1540 | |
| 1541 | // Try another font |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1542 | FPDF_PAGEOBJECT text_object2 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1543 | FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1544 | EXPECT_TRUE(text_object2); |
| 1545 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 1546 | GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold."); |
| 1547 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 1548 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600); |
| 1549 | FPDFPage_InsertObject(page, text_object2); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1550 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1551 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1552 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 1553 | const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1554 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 1555 | const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1556 | #else |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 1557 | const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 1558 | #endif |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 1559 | CompareBitmap(page_bitmap.get(), 612, 792, md5_2); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1560 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1561 | |
| 1562 | // And some randomly transformed text |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1563 | FPDF_PAGEOBJECT text_object3 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1564 | FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1565 | EXPECT_TRUE(text_object3); |
| 1566 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 = |
| 1567 | GetFPDFWideString(L"Can you read me? <:)>"); |
| 1568 | EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get())); |
| 1569 | FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200); |
| 1570 | FPDFPage_InsertObject(page, text_object3); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1571 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1572 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1573 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1574 | const char md5_3[] = "40b3ef04f915ff4c4208948001763544"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1575 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1576 | const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1577 | #else |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1578 | const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 1579 | #endif |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1580 | CompareBitmap(page_bitmap.get(), 612, 792, md5_3); |
| 1581 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1582 | |
Miklos Vajna | c765d2a | 2018-06-19 15:45:42 +0000 | [diff] [blame] | 1583 | double matrix_a = 0; |
| 1584 | double matrix_b = 0; |
| 1585 | double matrix_c = 0; |
| 1586 | double matrix_d = 0; |
| 1587 | double matrix_e = 0; |
| 1588 | double matrix_f = 0; |
| 1589 | EXPECT_FALSE(FPDFText_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c, |
| 1590 | &matrix_d, &matrix_e, &matrix_f)); |
| 1591 | EXPECT_TRUE(FPDFText_GetMatrix(text_object3, &matrix_a, &matrix_b, &matrix_c, |
| 1592 | &matrix_d, &matrix_e, &matrix_f)); |
| 1593 | EXPECT_EQ(1., matrix_a); |
| 1594 | EXPECT_EQ(1.5, matrix_b); |
| 1595 | EXPECT_EQ(2., matrix_c); |
| 1596 | EXPECT_EQ(0.5, matrix_d); |
| 1597 | EXPECT_EQ(200., matrix_e); |
| 1598 | EXPECT_EQ(200., matrix_f); |
| 1599 | |
Miklos Vajna | 8625d3b | 2018-06-26 15:12:48 +0000 | [diff] [blame] | 1600 | EXPECT_EQ(0, FPDFTextObj_GetFontSize(nullptr)); |
| 1601 | EXPECT_EQ(20, FPDFTextObj_GetFontSize(text_object3)); |
| 1602 | |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1603 | // TODO(npm): Why are there issues with text rotated by 90 degrees? |
| 1604 | // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this. |
| 1605 | FPDF_ClosePage(page); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 1606 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1607 | |
Miklos Vajna | 1448cc1 | 2018-07-03 13:52:33 +0000 | [diff] [blame] | 1608 | TEST_F(FPDFEditEmbeddertest, TestGetTextRenderMode) { |
| 1609 | EXPECT_TRUE(OpenDocument("text_render_mode.pdf")); |
| 1610 | FPDF_PAGE page = LoadPage(0); |
| 1611 | ASSERT_TRUE(page); |
| 1612 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 1613 | |
| 1614 | ASSERT_EQ(-1, FPDFText_GetTextRenderMode(nullptr)); |
| 1615 | |
| 1616 | FPDF_PAGEOBJECT fill = FPDFPage_GetObject(page, 0); |
| 1617 | ASSERT_EQ(FPDF_TEXTRENDERMODE_FILL, FPDFText_GetTextRenderMode(fill)); |
| 1618 | |
| 1619 | FPDF_PAGEOBJECT stroke = FPDFPage_GetObject(page, 1); |
| 1620 | ASSERT_EQ(FPDF_TEXTRENDERMODE_STROKE, FPDFText_GetTextRenderMode(stroke)); |
| 1621 | |
| 1622 | UnloadPage(page); |
| 1623 | } |
| 1624 | |
Miklos Vajna | b66077d | 2018-07-11 13:25:02 +0000 | [diff] [blame] | 1625 | TEST_F(FPDFEditEmbeddertest, TestFormGetObjects) { |
| 1626 | EXPECT_TRUE(OpenDocument("form_object.pdf")); |
| 1627 | FPDF_PAGE page = LoadPage(0); |
| 1628 | ASSERT_TRUE(page); |
| 1629 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 1630 | |
| 1631 | FPDF_PAGEOBJECT form = FPDFPage_GetObject(page, 0); |
| 1632 | EXPECT_EQ(FPDF_PAGEOBJ_FORM, FPDFPageObj_GetType(form)); |
| 1633 | ASSERT_EQ(-1, FPDFFormObj_CountObjects(nullptr)); |
| 1634 | ASSERT_EQ(2, FPDFFormObj_CountObjects(form)); |
| 1635 | |
| 1636 | UnloadPage(page); |
| 1637 | } |
| 1638 | |
Nicolas Pena | 4c48b10 | 2018-06-13 18:23:46 +0000 | [diff] [blame] | 1639 | // Tests adding text from standard font using FPDFText_LoadStandardFont. |
| 1640 | TEST_F(FPDFEditEmbeddertest, AddStandardFontText2) { |
| 1641 | // Start with a blank page |
| 1642 | ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792)); |
| 1643 | |
| 1644 | // Load a standard font. |
| 1645 | FPDF_FONT font = FPDFText_LoadStandardFont(document(), "Helvetica"); |
| 1646 | ASSERT_TRUE(font); |
| 1647 | |
| 1648 | // Add some text to the page. |
| 1649 | FPDF_PAGEOBJECT text_object = |
| 1650 | FPDFPageObj_CreateTextObj(document(), font, 12.0f); |
| 1651 | EXPECT_TRUE(text_object); |
| 1652 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1653 | GetFPDFWideString(L"I'm at the bottom of the page"); |
| 1654 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 1655 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20); |
| 1656 | FPDFPage_InsertObject(page.get(), text_object); |
| 1657 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page.get(), nullptr, 0); |
| 1658 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 1659 | const char md5[] = "a4dddc1a3930fa694bbff9789dab4161"; |
| 1660 | #else |
| 1661 | const char md5[] = "eacaa24573b8ce997b3882595f096f00"; |
| 1662 | #endif |
| 1663 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
| 1664 | } |
| 1665 | |
| 1666 | TEST_F(FPDFEditEmbeddertest, LoadStandardFonts) { |
| 1667 | CreateNewDocument(); |
| 1668 | const char* standard_font_names[] = {"Arial", |
| 1669 | "Arial-Bold", |
| 1670 | "Arial-BoldItalic", |
| 1671 | "Arial-Italic", |
| 1672 | "Courier", |
| 1673 | "Courier-BoldOblique", |
| 1674 | "Courier-Oblique", |
| 1675 | "Courier-Bold", |
| 1676 | "CourierNew", |
| 1677 | "CourierNew-Bold", |
| 1678 | "CourierNew-BoldItalic", |
| 1679 | "CourierNew-Italic", |
| 1680 | "Helvetica", |
| 1681 | "Helvetica-Bold", |
| 1682 | "Helvetica-BoldOblique", |
| 1683 | "Helvetica-Oblique", |
| 1684 | "Symbol", |
| 1685 | "TimesNewRoman", |
| 1686 | "TimesNewRoman-Bold", |
| 1687 | "TimesNewRoman-BoldItalic", |
| 1688 | "TimesNewRoman-Italic", |
| 1689 | "ZapfDingbats"}; |
| 1690 | for (auto* const font_name : standard_font_names) { |
| 1691 | FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name); |
| 1692 | EXPECT_TRUE(font) << font_name << " should be considered a standard font."; |
| 1693 | } |
| 1694 | const char* not_standard_font_names[] = { |
| 1695 | "Abcdefg", "ArialB", "Arial-Style", |
| 1696 | "Font Name", "FontArial", "NotAStandardFontName", |
| 1697 | "TestFontName", "Quack", "Symbol-Italic", |
| 1698 | "Zapf"}; |
| 1699 | for (auto* const font_name : not_standard_font_names) { |
| 1700 | FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name); |
| 1701 | EXPECT_FALSE(font) << font_name |
| 1702 | << " should not be considered a standard font."; |
| 1703 | } |
| 1704 | } |
| 1705 | |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1706 | TEST_F(FPDFEditEmbeddertest, GraphicsData) { |
| 1707 | // New page |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1708 | ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792)); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1709 | |
| 1710 | // Create a rect with nontrivial graphics |
| 1711 | FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 1712 | FPDFPageObj_SetBlendMode(rect1, "Color"); |
| 1713 | FPDFPage_InsertObject(page.get(), rect1); |
| 1714 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| 1715 | |
| 1716 | // Check that the ExtGState was created |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1717 | CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get()); |
| 1718 | CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState"); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1719 | ASSERT_TRUE(graphics_dict); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1720 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1721 | |
| 1722 | // Add a text object causing no change to the graphics dictionary |
| 1723 | FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 1724 | // Only alpha, the last component, matters for the graphics dictionary. And |
| 1725 | // the default value is 255. |
| 1726 | EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255)); |
| 1727 | FPDFPage_InsertObject(page.get(), text1); |
| 1728 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1729 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1730 | |
| 1731 | // Add a text object increasing the size of the graphics dictionary |
| 1732 | FPDF_PAGEOBJECT text2 = |
| 1733 | FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f); |
| 1734 | FPDFPage_InsertObject(page.get(), text2); |
| 1735 | FPDFPageObj_SetBlendMode(text2, "Darken"); |
| 1736 | EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150)); |
| 1737 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1738 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1739 | |
| 1740 | // Add a path that should reuse graphics |
Nicolas Pena | ce67be4 | 2017-06-14 14:52:49 -0400 | [diff] [blame] | 1741 | FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1742 | FPDFPageObj_SetBlendMode(path, "Darken"); |
| 1743 | EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150)); |
| 1744 | FPDFPage_InsertObject(page.get(), path); |
| 1745 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1746 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1747 | |
| 1748 | // Add a rect increasing the size of the graphics dictionary |
| 1749 | FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 1750 | FPDFPageObj_SetBlendMode(rect2, "Darken"); |
| 1751 | EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150)); |
| 1752 | EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200)); |
| 1753 | FPDFPage_InsertObject(page.get(), rect2); |
| 1754 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1755 | EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1756 | } |
| 1757 | |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1758 | TEST_F(FPDFEditEmbeddertest, DoubleGenerating) { |
| 1759 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1760 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1761 | |
| 1762 | // Add a red rectangle with some non-default alpha |
| 1763 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 1764 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128)); |
| 1765 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0)); |
| 1766 | FPDFPage_InsertObject(page, rect); |
| 1767 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1768 | |
| 1769 | // Check the ExtGState |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1770 | CPDF_Page* cpage = CPDFPageFromFPDFPage(page); |
| 1771 | CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState"); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1772 | ASSERT_TRUE(graphics_dict); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1773 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1774 | |
| 1775 | // Check the bitmap |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1776 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1777 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1778 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 1779 | "5384da3406d62360ffb5cac4476fff1c"); |
| 1780 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1781 | |
| 1782 | // Never mind, my new favorite color is blue, increase alpha |
| 1783 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180)); |
| 1784 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1785 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1786 | |
| 1787 | // Check that bitmap displays changed content |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1788 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1789 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1790 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 1791 | "2e51656f5073b0bee611d9cd086aa09c"); |
| 1792 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1793 | |
| 1794 | // And now generate, without changes |
| 1795 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1796 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1797 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1798 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1799 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 1800 | "2e51656f5073b0bee611d9cd086aa09c"); |
| 1801 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1802 | |
| 1803 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1804 | FPDF_PAGEOBJECT text_object = |
| 1805 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 1806 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1807 | GetFPDFWideString(L"Something something #text# something"); |
| 1808 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 1809 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300); |
| 1810 | FPDFPage_InsertObject(page, text_object); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1811 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1812 | CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font"); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1813 | ASSERT_TRUE(font_dict); |
| 1814 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 1815 | |
| 1816 | // Generate yet again, check dicts are reasonably sized |
| 1817 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1818 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1819 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 1820 | FPDF_ClosePage(page); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1821 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1822 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1823 | TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) { |
| 1824 | CreateNewDocument(); |
| 1825 | // TODO(npm): use other fonts after disallowing loading any font as any type |
| 1826 | const CPDF_Font* stock_font = |
| 1827 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1828 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1829 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1830 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1831 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false)); |
| 1832 | ASSERT_TRUE(font.get()); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame] | 1833 | CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1834 | EXPECT_TRUE(typed_font->IsType1Font()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1835 | |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 1836 | const CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1837 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 1838 | EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype")); |
| 1839 | EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont")); |
| 1840 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 1841 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 1842 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1843 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
| 1844 | |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1845 | const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1846 | ASSERT_TRUE(widths_array); |
| 1847 | ASSERT_EQ(224U, widths_array->GetCount()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1848 | EXPECT_EQ(250, widths_array->GetNumberAt(0)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1849 | EXPECT_EQ(569, widths_array->GetNumberAt(11)); |
| 1850 | EXPECT_EQ(500, widths_array->GetNumberAt(223)); |
| 1851 | CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data); |
| 1852 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1853 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1854 | TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) { |
| 1855 | CreateNewDocument(); |
| 1856 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1857 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1858 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1859 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1860 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false)); |
| 1861 | ASSERT_TRUE(font.get()); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame] | 1862 | CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1863 | EXPECT_TRUE(typed_font->IsTrueTypeFont()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1864 | |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 1865 | const CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1866 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 1867 | EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype")); |
| 1868 | EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont")); |
| 1869 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 1870 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 1871 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
| 1872 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1873 | |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1874 | const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1875 | ASSERT_TRUE(widths_array); |
| 1876 | ASSERT_EQ(224U, widths_array->GetCount()); |
| 1877 | EXPECT_EQ(600, widths_array->GetNumberAt(33)); |
| 1878 | EXPECT_EQ(600, widths_array->GetNumberAt(74)); |
| 1879 | EXPECT_EQ(600, widths_array->GetNumberAt(223)); |
| 1880 | CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data); |
| 1881 | } |
| 1882 | |
| 1883 | TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) { |
| 1884 | CreateNewDocument(); |
| 1885 | const CPDF_Font* stock_font = |
| 1886 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1887 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1888 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1889 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1890 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1)); |
| 1891 | ASSERT_TRUE(font.get()); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame] | 1892 | CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1893 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 1894 | |
| 1895 | // Check font dictionary entries |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 1896 | const CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1897 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 1898 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 1899 | EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont")); |
| 1900 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1901 | const CPDF_Array* descendant_array = |
| 1902 | font_dict->GetArrayFor("DescendantFonts"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1903 | ASSERT_TRUE(descendant_array); |
| 1904 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 1905 | |
| 1906 | // Check the CIDFontDict |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1907 | const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1908 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 1909 | EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype")); |
| 1910 | EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont")); |
Lei Zhang | b1ec280 | 2018-05-25 21:55:24 +0000 | [diff] [blame] | 1911 | const CPDF_Dictionary* cidinfo_dict = |
| 1912 | cidfont_dict->GetDictFor("CIDSystemInfo"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1913 | ASSERT_TRUE(cidinfo_dict); |
| 1914 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 1915 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 1916 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 1917 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data); |
| 1918 | |
| 1919 | // Check widths |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1920 | const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1921 | ASSERT_TRUE(widths_array); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 1922 | EXPECT_GT(widths_array->GetCount(), 1U); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1923 | CheckCompositeFontWidths(widths_array, typed_font); |
| 1924 | } |
| 1925 | |
| 1926 | TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) { |
| 1927 | CreateNewDocument(); |
| 1928 | const CPDF_Font* stock_font = |
| 1929 | CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1930 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1931 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1932 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1933 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1934 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1)); |
| 1935 | ASSERT_TRUE(font.get()); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame] | 1936 | CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1937 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 1938 | |
| 1939 | // Check font dictionary entries |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 1940 | const CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1941 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 1942 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 1943 | EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont")); |
| 1944 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1945 | const CPDF_Array* descendant_array = |
| 1946 | font_dict->GetArrayFor("DescendantFonts"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1947 | ASSERT_TRUE(descendant_array); |
| 1948 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 1949 | |
| 1950 | // Check the CIDFontDict |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1951 | const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1952 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 1953 | EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype")); |
| 1954 | EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont")); |
Lei Zhang | b1ec280 | 2018-05-25 21:55:24 +0000 | [diff] [blame] | 1955 | const CPDF_Dictionary* cidinfo_dict = |
| 1956 | cidfont_dict->GetDictFor("CIDSystemInfo"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1957 | ASSERT_TRUE(cidinfo_dict); |
| 1958 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 1959 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 1960 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 1961 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size, |
| 1962 | data); |
| 1963 | |
| 1964 | // Check widths |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1965 | const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1966 | ASSERT_TRUE(widths_array); |
| 1967 | CheckCompositeFontWidths(widths_array, typed_font); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1968 | } |
rbpotter | ce8e51e | 2017-04-28 12:42:47 -0700 | [diff] [blame] | 1969 | |
| 1970 | TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) { |
| 1971 | // Load document with a -90 degree rotation |
| 1972 | EXPECT_TRUE(OpenDocument("bug_713197.pdf")); |
| 1973 | FPDF_PAGE page = LoadPage(0); |
| 1974 | EXPECT_NE(nullptr, page); |
| 1975 | |
| 1976 | EXPECT_EQ(3, FPDFPage_GetRotation(page)); |
| 1977 | UnloadPage(page); |
| 1978 | } |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1979 | |
| 1980 | TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) { |
| 1981 | // Start with a blank page |
| 1982 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 1983 | { |
| 1984 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1985 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1986 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1987 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1988 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0)); |
| 1989 | ASSERT_TRUE(font.get()); |
| 1990 | |
| 1991 | // Add some text to the page |
| 1992 | FPDF_PAGEOBJECT text_object = |
| 1993 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 1994 | EXPECT_TRUE(text_object); |
| 1995 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1996 | GetFPDFWideString(L"I am testing my loaded font, WEE."); |
| 1997 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 1998 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400); |
| 1999 | FPDFPage_InsertObject(page, text_object); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2000 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 2001 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 2002 | const char md5[] = "17d2b6cd574cf66170b09c8927529a94"; |
| 2003 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 2004 | const char md5[] = "70592859010ffbf532a2237b8118bcc4"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 2005 | #endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 2006 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 2007 | |
| 2008 | // Add some more text, same font |
| 2009 | FPDF_PAGEOBJECT text_object2 = |
| 2010 | FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f); |
| 2011 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 2012 | GetFPDFWideString(L"Bigger font size"); |
| 2013 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 2014 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200); |
| 2015 | FPDFPage_InsertObject(page, text_object2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 2016 | } |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2017 | ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 2018 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 2019 | const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea"; |
| 2020 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 2021 | const char md5_2[] = "c1d10cce1761c4a998a16b2562030568"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 2022 | #endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 2023 | CompareBitmap(page_bitmap2.get(), 612, 792, md5_2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 2024 | |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 2025 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 2026 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 2027 | FPDF_ClosePage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 2028 | |
| 2029 | VerifySavedDocument(612, 792, md5_2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 2030 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 2031 | |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 2032 | TEST_F(FPDFEditEmbeddertest, TransformAnnot) { |
| 2033 | // Open a file with one annotation and load its first page. |
| 2034 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 2035 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 2036 | ASSERT_TRUE(page); |
| 2037 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2038 | { |
| 2039 | // Add an underline annotation to the page without specifying its rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2040 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2041 | FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE)); |
| 2042 | ASSERT_TRUE(annot); |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 2043 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 2044 | // FPDFPage_TransformAnnots() should run without errors when modifying |
| 2045 | // annotation rectangles. |
| 2046 | FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6); |
| 2047 | } |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 2048 | UnloadPage(page); |
| 2049 | } |
| 2050 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 2051 | // TODO(npm): Add tests using Japanese fonts in other OS. |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 2052 | #if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_ |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 2053 | TEST_F(FPDFEditEmbeddertest, AddCIDFontText) { |
| 2054 | // Start with a blank page |
| 2055 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 2056 | CFX_Font CIDfont; |
| 2057 | { |
| 2058 | // First, get the data from the font |
| 2059 | CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0); |
| 2060 | EXPECT_EQ("IPAGothic", CIDfont.GetFaceName()); |
| 2061 | const uint8_t* data = CIDfont.GetFontData(); |
| 2062 | const uint32_t size = CIDfont.GetSize(); |
| 2063 | |
| 2064 | // Load the data into a FPDF_Font. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2065 | ScopedFPDFFont font( |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 2066 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1)); |
| 2067 | ASSERT_TRUE(font.get()); |
| 2068 | |
| 2069 | // Add some text to the page |
| 2070 | FPDF_PAGEOBJECT text_object = |
| 2071 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 2072 | ASSERT_TRUE(text_object); |
| 2073 | std::wstring wstr = L"ABCDEFGhijklmnop."; |
| 2074 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 2075 | GetFPDFWideString(wstr); |
| 2076 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 2077 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200); |
| 2078 | FPDFPage_InsertObject(page, text_object); |
| 2079 | |
| 2080 | // And add some Japanese characters |
| 2081 | FPDF_PAGEOBJECT text_object2 = |
| 2082 | FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f); |
| 2083 | ASSERT_TRUE(text_object2); |
| 2084 | std::wstring wstr2 = |
| 2085 | L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1" |
| 2086 | L"\u756A"; |
| 2087 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 2088 | GetFPDFWideString(wstr2); |
| 2089 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 2090 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500); |
| 2091 | FPDFPage_InsertObject(page, text_object2); |
| 2092 | } |
| 2093 | |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 2094 | // Check that the text renders properly. |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 2095 | const char md5[] = "c68cd79aa72bf83a7b25271370d46b21"; |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 2096 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2097 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 2098 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
| 2099 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 2100 | |
| 2101 | // Save the document, close the page. |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 2102 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 2103 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 2104 | FPDF_ClosePage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 2105 | |
| 2106 | VerifySavedDocument(612, 792, md5); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 2107 | } |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 2108 | #endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_ |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 2109 | |
| 2110 | TEST_F(FPDFEditEmbeddertest, SaveAndRender) { |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 2111 | const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b"; |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 2112 | { |
| 2113 | EXPECT_TRUE(OpenDocument("bug_779.pdf")); |
| 2114 | FPDF_PAGE page = LoadPage(0); |
| 2115 | ASSERT_NE(nullptr, page); |
| 2116 | |
| 2117 | // Now add a more complex blue path. |
| 2118 | FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20); |
| 2119 | EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200)); |
| 2120 | // TODO(npm): stroking will cause the MD5s to differ. |
| 2121 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0)); |
| 2122 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63)); |
| 2123 | EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90)); |
| 2124 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133)); |
| 2125 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33)); |
| 2126 | EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40)); |
| 2127 | EXPECT_TRUE(FPDFPath_Close(green_path)); |
| 2128 | FPDFPage_InsertObject(page, green_path); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2129 | ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 2130 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 2131 | |
| 2132 | // Now save the result, closing the page and document |
| 2133 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 2134 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 2135 | UnloadPage(page); |
| 2136 | } |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 2137 | |
| 2138 | VerifySavedDocument(612, 792, md5); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 2139 | } |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 2140 | |
Henrique Nakashima | 144107d | 2018-07-10 21:04:05 +0000 | [diff] [blame] | 2141 | TEST_F(FPDFEditEmbeddertest, AddMarkedText) { |
| 2142 | // Start with a blank page. |
| 2143 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 2144 | |
| 2145 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial"); |
| 2146 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 2147 | const uint32_t size = stock_font->GetFont()->GetSize(); |
| 2148 | ScopedFPDFFont font( |
| 2149 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0)); |
| 2150 | ASSERT_TRUE(font.get()); |
| 2151 | |
| 2152 | // Add some text to the page. |
| 2153 | FPDF_PAGEOBJECT text_object = |
| 2154 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 2155 | |
| 2156 | EXPECT_TRUE(text_object); |
| 2157 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 = |
| 2158 | GetFPDFWideString(L"I am testing my loaded font, WEE."); |
| 2159 | EXPECT_TRUE(FPDFText_SetText(text_object, text1.get())); |
| 2160 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400); |
| 2161 | FPDFPage_InsertObject(page, text_object); |
| 2162 | |
| 2163 | // Add a mark with the tag "TestMarkName" to that text. |
| 2164 | EXPECT_EQ(0, FPDFPageObj_CountMarks(text_object)); |
| 2165 | FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(text_object, "TestMarkName"); |
| 2166 | EXPECT_TRUE(mark); |
| 2167 | EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object)); |
| 2168 | EXPECT_EQ(mark, FPDFPageObj_GetMark(text_object, 0)); |
| 2169 | char buffer[256]; |
| 2170 | EXPECT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u); |
| 2171 | std::wstring name = |
| 2172 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 2173 | EXPECT_EQ(L"TestMarkName", name); |
| 2174 | |
| 2175 | // Add parameters: |
| 2176 | // - int "IntKey" : 42 |
| 2177 | // - string "StringKey": "StringValue" |
| 2178 | EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark)); |
| 2179 | EXPECT_TRUE(FPDFPageObjMark_SetIntParam(document(), mark, "IntKey", 42)); |
| 2180 | EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), mark, "StringKey", |
| 2181 | "StringValue")); |
| 2182 | EXPECT_EQ(2, FPDFPageObjMark_CountParams(mark)); |
| 2183 | |
| 2184 | // Check the two parameters can be retrieved. |
| 2185 | EXPECT_EQ(FPDF_OBJECT_NUMBER, |
Henrique Nakashima | 94230e5 | 2018-07-11 22:02:02 +0000 | [diff] [blame^] | 2186 | FPDFPageObjMark_GetParamValueType(mark, "IntKey")); |
Henrique Nakashima | 144107d | 2018-07-10 21:04:05 +0000 | [diff] [blame] | 2187 | int int_value; |
Henrique Nakashima | 94230e5 | 2018-07-11 22:02:02 +0000 | [diff] [blame^] | 2188 | EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "IntKey", &int_value)); |
Henrique Nakashima | 144107d | 2018-07-10 21:04:05 +0000 | [diff] [blame] | 2189 | EXPECT_EQ(42, int_value); |
| 2190 | |
| 2191 | EXPECT_EQ(FPDF_OBJECT_STRING, |
Henrique Nakashima | 94230e5 | 2018-07-11 22:02:02 +0000 | [diff] [blame^] | 2192 | FPDFPageObjMark_GetParamValueType(mark, "StringKey")); |
Henrique Nakashima | 144107d | 2018-07-10 21:04:05 +0000 | [diff] [blame] | 2193 | unsigned long out_buffer_len; |
Henrique Nakashima | 94230e5 | 2018-07-11 22:02:02 +0000 | [diff] [blame^] | 2194 | EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "StringKey", buffer, |
| 2195 | 256, &out_buffer_len)); |
Henrique Nakashima | 144107d | 2018-07-10 21:04:05 +0000 | [diff] [blame] | 2196 | EXPECT_GT(out_buffer_len, 0u); |
| 2197 | name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 2198 | EXPECT_EQ(L"StringValue", name); |
| 2199 | |
| 2200 | // Render and check the bitmap is the expected one. |
| 2201 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 2202 | const char md5[] = "17d2b6cd574cf66170b09c8927529a94"; |
| 2203 | #else |
| 2204 | const char md5[] = "70592859010ffbf532a2237b8118bcc4"; |
| 2205 | #endif |
| 2206 | { |
| 2207 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
| 2208 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
| 2209 | } |
| 2210 | |
Henrique Nakashima | b4bcf69 | 2018-07-11 21:19:22 +0000 | [diff] [blame] | 2211 | // Now save the result. |
| 2212 | EXPECT_EQ(1, FPDFPage_CountObjects(page)); |
| 2213 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 2214 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 2215 | |
Henrique Nakashima | 144107d | 2018-07-10 21:04:05 +0000 | [diff] [blame] | 2216 | FPDF_ClosePage(page); |
| 2217 | |
Henrique Nakashima | b4bcf69 | 2018-07-11 21:19:22 +0000 | [diff] [blame] | 2218 | // Re-open the file and check the changes were kept in the saved .pdf. |
| 2219 | OpenSavedDocument(); |
| 2220 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 2221 | EXPECT_EQ(1, FPDFPage_CountObjects(saved_page)); |
| 2222 | |
| 2223 | text_object = FPDFPage_GetObject(saved_page, 0); |
| 2224 | EXPECT_TRUE(text_object); |
| 2225 | EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object)); |
| 2226 | mark = FPDFPageObj_GetMark(text_object, 0); |
| 2227 | EXPECT_TRUE(mark); |
| 2228 | EXPECT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u); |
| 2229 | name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 2230 | EXPECT_EQ(L"TestMarkName", name); |
| 2231 | |
| 2232 | CloseSavedPage(saved_page); |
| 2233 | CloseSavedDocument(); |
Henrique Nakashima | 144107d | 2018-07-10 21:04:05 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 2236 | TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) { |
| 2237 | ASSERT_TRUE(OpenDocument("embedded_images.pdf")); |
| 2238 | FPDF_PAGE page = LoadPage(0); |
| 2239 | ASSERT_TRUE(page); |
Miklos Vajna | 9262761 | 2017-09-25 12:59:29 +0200 | [diff] [blame] | 2240 | ASSERT_EQ(39, FPDFPage_CountObjects(page)); |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 2241 | |
| 2242 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32); |
| 2243 | EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2244 | EXPECT_FALSE(FPDFImageObj_GetBitmap(obj)); |
| 2245 | |
| 2246 | obj = FPDFPage_GetObject(page, 33); |
| 2247 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2248 | FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj); |
| 2249 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 2250 | CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae"); |
| 2251 | FPDFBitmap_Destroy(bitmap); |
| 2252 | |
| 2253 | obj = FPDFPage_GetObject(page, 34); |
| 2254 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2255 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 2256 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 2257 | CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6"); |
| 2258 | FPDFBitmap_Destroy(bitmap); |
| 2259 | |
| 2260 | obj = FPDFPage_GetObject(page, 35); |
| 2261 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2262 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 2263 | EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap)); |
| 2264 | CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3"); |
| 2265 | FPDFBitmap_Destroy(bitmap); |
| 2266 | |
| 2267 | obj = FPDFPage_GetObject(page, 36); |
| 2268 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2269 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 2270 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 2271 | CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a"); |
| 2272 | FPDFBitmap_Destroy(bitmap); |
| 2273 | |
| 2274 | obj = FPDFPage_GetObject(page, 37); |
| 2275 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2276 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 2277 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 2278 | CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534"); |
| 2279 | FPDFBitmap_Destroy(bitmap); |
| 2280 | |
| 2281 | obj = FPDFPage_GetObject(page, 38); |
| 2282 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2283 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 2284 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 2285 | CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5"); |
| 2286 | FPDFBitmap_Destroy(bitmap); |
| 2287 | UnloadPage(page); |
| 2288 | } |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 2289 | |
Lei Zhang | 53341dd | 2018-03-01 15:42:47 +0000 | [diff] [blame] | 2290 | TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) { |
| 2291 | ASSERT_TRUE(OpenDocument("bug_631912.pdf")); |
| 2292 | FPDF_PAGE page = LoadPage(0); |
| 2293 | ASSERT_TRUE(page); |
| 2294 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 2295 | |
| 2296 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0); |
| 2297 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2298 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 2299 | ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj)); |
Lei Zhang | 1330ebb | 2018-03-05 15:16:37 +0000 | [diff] [blame] | 2300 | ASSERT_TRUE(bitmap); |
| 2301 | EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get())); |
| 2302 | CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de"); |
Lei Zhang | 53341dd | 2018-03-01 15:42:47 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | UnloadPage(page); |
| 2306 | } |
| 2307 | |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 2308 | TEST_F(FPDFEditEmbeddertest, GetImageData) { |
| 2309 | EXPECT_TRUE(OpenDocument("embedded_images.pdf")); |
| 2310 | FPDF_PAGE page = LoadPage(0); |
| 2311 | ASSERT_TRUE(page); |
Miklos Vajna | 9262761 | 2017-09-25 12:59:29 +0200 | [diff] [blame] | 2312 | ASSERT_EQ(39, FPDFPage_CountObjects(page)); |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 2313 | |
| 2314 | // Retrieve an image object with flate-encoded data stream. |
| 2315 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33); |
| 2316 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2317 | |
| 2318 | // Check that the raw image data has the correct length and hash value. |
| 2319 | unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0); |
| 2320 | std::vector<char> buf(len); |
| 2321 | EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len)); |
| 2322 | EXPECT_EQ("f73802327d2e88e890f653961bcda81a", |
| 2323 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 2324 | |
| 2325 | // Check that the decoded image data has the correct length and hash value. |
| 2326 | len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0); |
| 2327 | buf.clear(); |
| 2328 | buf.resize(len); |
| 2329 | EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len)); |
| 2330 | EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e", |
| 2331 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 2332 | |
| 2333 | // Retrieve an image obejct with DCTDecode-encoded data stream. |
| 2334 | obj = FPDFPage_GetObject(page, 37); |
| 2335 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2336 | |
| 2337 | // Check that the raw image data has the correct length and hash value. |
| 2338 | len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0); |
| 2339 | buf.clear(); |
| 2340 | buf.resize(len); |
| 2341 | EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len)); |
| 2342 | EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", |
| 2343 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 2344 | |
| 2345 | // Check that the decoded image data has the correct length and hash value, |
| 2346 | // which should be the same as those of the raw data, since this image is |
| 2347 | // encoded by a single DCTDecode filter and decoding is a noop. |
| 2348 | len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0); |
| 2349 | buf.clear(); |
| 2350 | buf.resize(len); |
| 2351 | EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len)); |
| 2352 | EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", |
| 2353 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 2354 | |
| 2355 | UnloadPage(page); |
| 2356 | } |
Jane Liu | 2e5f0ae | 2017-08-08 15:23:27 -0400 | [diff] [blame] | 2357 | |
| 2358 | TEST_F(FPDFEditEmbeddertest, DestroyPageObject) { |
| 2359 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| 2360 | ASSERT_TRUE(rect); |
| 2361 | |
| 2362 | // There should be no memory leaks with a call to FPDFPageObj_Destroy(). |
| 2363 | FPDFPageObj_Destroy(rect); |
| 2364 | } |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 2365 | |
| 2366 | TEST_F(FPDFEditEmbeddertest, GetImageFilters) { |
| 2367 | EXPECT_TRUE(OpenDocument("embedded_images.pdf")); |
| 2368 | FPDF_PAGE page = LoadPage(0); |
| 2369 | ASSERT_TRUE(page); |
| 2370 | |
| 2371 | // Verify that retrieving the filter of a non-image object would fail. |
| 2372 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32); |
| 2373 | ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2374 | ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj)); |
| 2375 | EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0)); |
| 2376 | |
| 2377 | // Verify the returned filter string for an image object with a single filter. |
| 2378 | obj = FPDFPage_GetObject(page, 33); |
| 2379 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2380 | ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj)); |
| 2381 | unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0); |
| 2382 | std::vector<char> buf(len); |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 2383 | static constexpr char kFlateDecode[] = "FlateDecode"; |
| 2384 | EXPECT_EQ(sizeof(kFlateDecode), |
| 2385 | FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len)); |
| 2386 | EXPECT_STREQ(kFlateDecode, buf.data()); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 2387 | EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0)); |
| 2388 | |
| 2389 | // Verify all the filters for an image object with a list of filters. |
| 2390 | obj = FPDFPage_GetObject(page, 38); |
| 2391 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2392 | ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj)); |
| 2393 | len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0); |
| 2394 | buf.clear(); |
| 2395 | buf.resize(len); |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 2396 | static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode"; |
| 2397 | EXPECT_EQ(sizeof(kASCIIHexDecode), |
| 2398 | FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len)); |
| 2399 | EXPECT_STREQ(kASCIIHexDecode, buf.data()); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 2400 | |
| 2401 | len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0); |
| 2402 | buf.clear(); |
| 2403 | buf.resize(len); |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 2404 | static constexpr char kDCTDecode[] = "DCTDecode"; |
| 2405 | EXPECT_EQ(sizeof(kDCTDecode), |
| 2406 | FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len)); |
| 2407 | EXPECT_STREQ(kDCTDecode, buf.data()); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 2408 | |
| 2409 | UnloadPage(page); |
| 2410 | } |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 2411 | |
| 2412 | TEST_F(FPDFEditEmbeddertest, GetImageMetadata) { |
| 2413 | ASSERT_TRUE(OpenDocument("embedded_images.pdf")); |
| 2414 | FPDF_PAGE page = LoadPage(0); |
| 2415 | ASSERT_TRUE(page); |
| 2416 | |
| 2417 | // Check that getting the metadata of a null object would fail. |
| 2418 | FPDF_IMAGEOBJ_METADATA metadata; |
| 2419 | EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata)); |
| 2420 | |
| 2421 | // Check that receiving the metadata with a null metadata object would fail. |
| 2422 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35); |
| 2423 | EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr)); |
| 2424 | |
| 2425 | // Check that when retrieving an image object's metadata without passing in |
| 2426 | // |page|, all values are correct, with the last two being default values. |
| 2427 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2428 | ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata)); |
Julian Lunger | ecd063e | 2017-12-27 10:18:50 -0500 | [diff] [blame] | 2429 | EXPECT_EQ(7, metadata.marked_content_id); |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 2430 | EXPECT_EQ(92u, metadata.width); |
| 2431 | EXPECT_EQ(68u, metadata.height); |
| 2432 | EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001); |
| 2433 | EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001); |
| 2434 | EXPECT_EQ(0u, metadata.bits_per_pixel); |
| 2435 | EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace); |
| 2436 | |
| 2437 | // Verify the metadata of a bitmap image with indexed colorspace. |
| 2438 | ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata)); |
Julian Lunger | ecd063e | 2017-12-27 10:18:50 -0500 | [diff] [blame] | 2439 | EXPECT_EQ(7, metadata.marked_content_id); |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 2440 | EXPECT_EQ(92u, metadata.width); |
| 2441 | EXPECT_EQ(68u, metadata.height); |
| 2442 | EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001); |
| 2443 | EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001); |
| 2444 | EXPECT_EQ(1u, metadata.bits_per_pixel); |
| 2445 | EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace); |
| 2446 | |
| 2447 | // Verify the metadata of an image with RGB colorspace. |
| 2448 | obj = FPDFPage_GetObject(page, 37); |
| 2449 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 2450 | ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata)); |
Julian Lunger | ecd063e | 2017-12-27 10:18:50 -0500 | [diff] [blame] | 2451 | EXPECT_EQ(9, metadata.marked_content_id); |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 2452 | EXPECT_EQ(126u, metadata.width); |
| 2453 | EXPECT_EQ(106u, metadata.height); |
| 2454 | EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001); |
| 2455 | EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001); |
| 2456 | EXPECT_EQ(24u, metadata.bits_per_pixel); |
| 2457 | EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace); |
| 2458 | |
| 2459 | UnloadPage(page); |
| 2460 | } |