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" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_array.h" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_number.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 15 | #include "core/fpdfapi/parser/cpdf_stream.h" |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 16 | #include "core/fpdfapi/parser/cpdf_stream_acc.h" |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 17 | #include "core/fxcrt/fx_system.h" |
Dan Sinclair | 00d47a6 | 2018-03-28 18:39:04 +0000 | [diff] [blame] | 18 | #include "fpdfsdk/cpdfsdk_helpers.h" |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 19 | #include "public/cpp/fpdf_scopers.h" |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 20 | #include "public/fpdf_annot.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 21 | #include "public/fpdf_edit.h" |
| 22 | #include "public/fpdfview.h" |
| 23 | #include "testing/embedder_test.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 24 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 25 | #include "testing/gtest/include/gtest/gtest.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 26 | #include "testing/test_support.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 27 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 28 | class FPDFEditEmbeddertest : public EmbedderTest { |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 29 | protected: |
| 30 | FPDF_DOCUMENT CreateNewDocument() { |
| 31 | document_ = FPDF_CreateNewDocument(); |
Tom Sepez | 41066c1 | 2017-05-18 09:28:49 -0700 | [diff] [blame] | 32 | cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 33 | return document_; |
| 34 | } |
| 35 | |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 36 | void CheckFontDescriptor(const CPDF_Dictionary* font_dict, |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 37 | int font_type, |
| 38 | bool bold, |
| 39 | bool italic, |
| 40 | uint32_t size, |
| 41 | const uint8_t* data) { |
Lei Zhang | b1ec280 | 2018-05-25 21:55:24 +0000 | [diff] [blame] | 42 | const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 43 | ASSERT_TRUE(font_desc); |
| 44 | EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type")); |
| 45 | EXPECT_EQ(font_dict->GetStringFor("BaseFont"), |
| 46 | font_desc->GetStringFor("FontName")); |
| 47 | |
| 48 | // Check that the font descriptor has the required keys according to spec |
| 49 | // 1.7 Table 5.19 |
| 50 | ASSERT_TRUE(font_desc->KeyExist("Flags")); |
Dan Sinclair | 10e1f05 | 2017-09-28 15:59:42 -0400 | [diff] [blame] | 51 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 52 | int font_flags = font_desc->GetIntegerFor("Flags"); |
Dan Sinclair | 10e1f05 | 2017-09-28 15:59:42 -0400 | [diff] [blame] | 53 | EXPECT_EQ(bold, FontStyleIsBold(font_flags)); |
| 54 | EXPECT_EQ(italic, FontStyleIsItalic(font_flags)); |
| 55 | EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 56 | ASSERT_TRUE(font_desc->KeyExist("FontBBox")); |
Nicolás Peña | 5f95f36 | 2017-09-28 13:00:45 +0900 | [diff] [blame] | 57 | |
Lei Zhang | b1ec280 | 2018-05-25 21:55:24 +0000 | [diff] [blame] | 58 | const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox"); |
Nicolás Peña | 5f95f36 | 2017-09-28 13:00:45 +0900 | [diff] [blame] | 59 | ASSERT_TRUE(fontBBox); |
| 60 | EXPECT_EQ(4U, fontBBox->GetCount()); |
| 61 | // Check that the coordinates are in the preferred order according to spec |
| 62 | // 1.7 Section 3.8.4 |
| 63 | EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2)); |
| 64 | EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3)); |
| 65 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 66 | EXPECT_TRUE(font_desc->KeyExist("ItalicAngle")); |
| 67 | EXPECT_TRUE(font_desc->KeyExist("Ascent")); |
| 68 | EXPECT_TRUE(font_desc->KeyExist("Descent")); |
| 69 | EXPECT_TRUE(font_desc->KeyExist("CapHeight")); |
| 70 | EXPECT_TRUE(font_desc->KeyExist("StemV")); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 71 | ByteString present("FontFile"); |
| 72 | ByteString absent("FontFile2"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 73 | if (font_type == FPDF_FONT_TRUETYPE) |
| 74 | std::swap(present, absent); |
| 75 | EXPECT_TRUE(font_desc->KeyExist(present)); |
| 76 | EXPECT_FALSE(font_desc->KeyExist(absent)); |
| 77 | |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 78 | auto streamAcc = |
| 79 | pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present)); |
| 80 | streamAcc->LoadAllDataRaw(); |
| 81 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 82 | // Check that the font stream is the one that was provided |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 83 | ASSERT_EQ(size, streamAcc->GetSize()); |
Nicolás Peña | 79eab23 | 2017-09-28 13:29:05 +0900 | [diff] [blame] | 84 | if (font_type == FPDF_FONT_TRUETYPE) { |
| 85 | ASSERT_EQ(static_cast<int>(size), |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 86 | streamAcc->GetDict()->GetIntegerFor("Length1")); |
Nicolás Peña | 79eab23 | 2017-09-28 13:29:05 +0900 | [diff] [blame] | 87 | } |
Artem Strygin | eababa1 | 2018-06-06 12:31:18 +0000 | [diff] [blame] | 88 | |
| 89 | const uint8_t* stream_data = streamAcc->GetData(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 90 | for (size_t j = 0; j < size; j++) |
| 91 | EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j; |
| 92 | } |
| 93 | |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 94 | void CheckCompositeFontWidths(const CPDF_Array* widths_array, |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 95 | CPDF_Font* typed_font) { |
| 96 | // Check that W array is in a format that conforms to PDF spec 1.7 section |
| 97 | // "Glyph Metrics in CIDFonts" (these checks are not |
| 98 | // implementation-specific). |
| 99 | EXPECT_GT(widths_array->GetCount(), 1U); |
| 100 | int num_cids_checked = 0; |
| 101 | int cur_cid = 0; |
| 102 | for (size_t idx = 0; idx < widths_array->GetCount(); idx++) { |
| 103 | int cid = widths_array->GetNumberAt(idx); |
| 104 | EXPECT_GE(cid, cur_cid); |
| 105 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 106 | const CPDF_Object* next = widths_array->GetObjectAt(idx); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 107 | if (next->IsArray()) { |
| 108 | // We are in the c [w1 w2 ...] case |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 109 | const CPDF_Array* arr = next->AsArray(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 110 | int cnt = static_cast<int>(arr->GetCount()); |
| 111 | size_t inner_idx = 0; |
| 112 | for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) { |
Nicolas Pena | 2334660 | 2018-01-30 21:42:41 +0000 | [diff] [blame] | 113 | uint32_t width = arr->GetNumberAt(inner_idx++); |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 114 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) |
| 115 | << " at cid " << cur_cid; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 116 | } |
| 117 | num_cids_checked += cnt; |
| 118 | continue; |
| 119 | } |
| 120 | // Otherwise, are in the c_first c_last w case. |
| 121 | ASSERT_TRUE(next->IsNumber()); |
| 122 | int last_cid = next->AsNumber()->GetInteger(); |
| 123 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
Nicolas Pena | 2334660 | 2018-01-30 21:42:41 +0000 | [diff] [blame] | 124 | uint32_t width = widths_array->GetNumberAt(idx); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 125 | for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) { |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 126 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) |
| 127 | << " at cid " << cur_cid; |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 128 | } |
| 129 | num_cids_checked += last_cid - cid + 1; |
| 130 | } |
| 131 | // Make sure we have a good amount of cids described |
| 132 | EXPECT_GT(num_cids_checked, 900); |
| 133 | } |
| 134 | CPDF_Document* cpdf_doc() { return cpdf_doc_; } |
| 135 | |
| 136 | private: |
| 137 | CPDF_Document* cpdf_doc_; |
| 138 | }; |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 139 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 140 | namespace { |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 141 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 142 | const char kExpectedPDF[] = |
| 143 | "%PDF-1.7\r\n" |
| 144 | "%\xA1\xB3\xC5\xD7\r\n" |
| 145 | "1 0 obj\r\n" |
| 146 | "<</Pages 2 0 R /Type/Catalog>>\r\n" |
| 147 | "endobj\r\n" |
| 148 | "2 0 obj\r\n" |
| 149 | "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n" |
| 150 | "endobj\r\n" |
| 151 | "3 0 obj\r\n" |
| 152 | "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n" |
| 153 | "endobj\r\n" |
| 154 | "4 0 obj\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 155 | "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R " |
| 156 | "/Resources<</ExtGState<</FXE1 5 0 R >>>>" |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 157 | "/Rotate 0/Type/Page" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 158 | ">>\r\n" |
| 159 | "endobj\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 160 | "5 0 obj\r\n" |
| 161 | "<</BM/Normal/CA 1/ca 1>>\r\n" |
| 162 | "endobj\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 163 | "xref\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 164 | "0 6\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 165 | "0000000000 65535 f\r\n" |
| 166 | "0000000017 00000 n\r\n" |
| 167 | "0000000066 00000 n\r\n" |
| 168 | "0000000122 00000 n\r\n" |
| 169 | "0000000192 00000 n\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 170 | "0000000311 00000 n\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 171 | "trailer\r\n" |
| 172 | "<<\r\n" |
| 173 | "/Root 1 0 R\r\n" |
| 174 | "/Info 3 0 R\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 175 | "/Size 6/ID\\[<.*><.*>\\]>>\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 176 | "startxref\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 177 | "354\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 178 | "%%EOF\r\n"; |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 179 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 180 | } // namespace |
| 181 | |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 182 | TEST_F(FPDFEditEmbeddertest, EmptyCreation) { |
| 183 | EXPECT_TRUE(CreateEmptyDocument()); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 184 | FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 185 | EXPECT_NE(nullptr, page); |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 186 | // The FPDFPage_GenerateContent call should do nothing. |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 187 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 188 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 189 | |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 190 | EXPECT_THAT(GetString(), testing::MatchesRegex(std::string( |
| 191 | kExpectedPDF, sizeof(kExpectedPDF)))); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 192 | FPDF_ClosePage(page); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 193 | } |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 194 | |
| 195 | // Regression test for https://crbug.com/667012 |
| 196 | TEST_F(FPDFEditEmbeddertest, RasterizePDF) { |
| 197 | const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615"; |
| 198 | |
| 199 | // Get the bitmap for the original document/ |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 200 | ScopedFPDFBitmap orig_bitmap; |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 201 | { |
| 202 | EXPECT_TRUE(OpenDocument("black.pdf")); |
| 203 | FPDF_PAGE orig_page = LoadPage(0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 204 | ASSERT_TRUE(orig_page); |
| 205 | orig_bitmap = RenderLoadedPage(orig_page); |
| 206 | CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 207 | UnloadPage(orig_page); |
| 208 | } |
| 209 | |
| 210 | // Create a new document from |orig_bitmap| and save it. |
| 211 | { |
| 212 | FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); |
| 213 | FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792); |
| 214 | |
| 215 | // Add the bitmap to an image object and add the image object to the output |
| 216 | // page. |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 217 | FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 218 | EXPECT_TRUE( |
| 219 | FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get())); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 220 | EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0)); |
| 221 | FPDFPage_InsertObject(temp_page, temp_img); |
| 222 | EXPECT_TRUE(FPDFPage_GenerateContent(temp_page)); |
| 223 | EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0)); |
| 224 | FPDF_ClosePage(temp_page); |
| 225 | FPDF_CloseDocument(temp_doc); |
| 226 | } |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 227 | |
| 228 | // Get the generated content. Make sure it is at least as big as the original |
| 229 | // PDF. |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 230 | EXPECT_GT(GetString().size(), 923U); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 231 | VerifySavedDocument(612, 792, kAllBlackMd5sum); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 232 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 233 | |
| 234 | TEST_F(FPDFEditEmbeddertest, AddPaths) { |
| 235 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 236 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 237 | ASSERT_TRUE(page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 238 | |
| 239 | // We will first add a red rectangle |
| 240 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 241 | ASSERT_TRUE(red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 242 | // Expect false when trying to set colors out of range |
| 243 | EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300)); |
| 244 | EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0)); |
| 245 | |
| 246 | // Fill rectangle with red and insert to the page |
| 247 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 248 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
Miklos Vajna | 491112b | 2018-05-30 13:30:10 +0000 | [diff] [blame] | 249 | |
| 250 | int fillmode = FPDF_FILLMODE_NONE; |
| 251 | FPDF_BOOL stroke = true; |
| 252 | EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke)); |
| 253 | EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode); |
| 254 | EXPECT_FALSE(stroke); |
| 255 | |
Miklos Vajna | 97f4d67 | 2018-06-04 14:47:17 +0000 | [diff] [blame] | 256 | double matrix_a = 1; |
| 257 | double matrix_b = 2; |
| 258 | double matrix_c = 3; |
| 259 | double matrix_d = 4; |
| 260 | double matrix_e = 5; |
| 261 | double matrix_f = 6; |
| 262 | EXPECT_FALSE(FPDFPath_SetMatrix(nullptr, matrix_a, matrix_b, matrix_c, |
| 263 | matrix_d, matrix_e, matrix_f)); |
| 264 | EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c, |
| 265 | matrix_d, matrix_e, matrix_f)); |
Miklos Vajna | ac42dd2 | 2018-06-05 13:28:58 +0000 | [diff] [blame] | 266 | // Set to 0 before FPDFPath_GetMatrix() to ensure they are actually set by |
| 267 | // the function. |
| 268 | matrix_a = 0; |
| 269 | matrix_b = 0; |
| 270 | matrix_c = 0; |
| 271 | matrix_d = 0; |
| 272 | matrix_e = 0; |
| 273 | matrix_f = 0; |
Miklos Vajna | 97f4d67 | 2018-06-04 14:47:17 +0000 | [diff] [blame] | 274 | EXPECT_FALSE(FPDFPath_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c, |
| 275 | &matrix_d, &matrix_e, &matrix_f)); |
| 276 | EXPECT_TRUE(FPDFPath_GetMatrix(red_rect, &matrix_a, &matrix_b, &matrix_c, |
| 277 | &matrix_d, &matrix_e, &matrix_f)); |
| 278 | EXPECT_EQ(1, static_cast<int>(matrix_a)); |
| 279 | EXPECT_EQ(2, static_cast<int>(matrix_b)); |
| 280 | EXPECT_EQ(3, static_cast<int>(matrix_c)); |
| 281 | EXPECT_EQ(4, static_cast<int>(matrix_d)); |
| 282 | EXPECT_EQ(5, static_cast<int>(matrix_e)); |
| 283 | EXPECT_EQ(6, static_cast<int>(matrix_f)); |
| 284 | // Set back the default |
| 285 | matrix_a = 1; |
| 286 | matrix_b = 0; |
| 287 | matrix_c = 0; |
| 288 | matrix_d = 1; |
| 289 | matrix_e = 0; |
| 290 | matrix_f = 0; |
| 291 | EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c, |
| 292 | matrix_d, matrix_e, matrix_f)); |
| 293 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 294 | FPDFPage_InsertObject(page, red_rect); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 295 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 296 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 297 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 298 | "66d02eaa6181e2c069ce2ea99beda497"); |
| 299 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 300 | |
| 301 | // Now add to that a green rectangle with some medium alpha |
| 302 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40); |
| 303 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128)); |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 304 | |
Miklos Vajna | 1ef04c9 | 2017-05-08 18:14:19 +0200 | [diff] [blame] | 305 | // Make sure the type of the rectangle is a path. |
| 306 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect)); |
| 307 | |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 308 | // Make sure we get back the same color we set previously. |
| 309 | unsigned int R; |
| 310 | unsigned int G; |
| 311 | unsigned int B; |
| 312 | unsigned int A; |
| 313 | EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A)); |
| 314 | EXPECT_EQ(0U, R); |
| 315 | EXPECT_EQ(255U, G); |
| 316 | EXPECT_EQ(0U, B); |
| 317 | EXPECT_EQ(128U, A); |
| 318 | |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 319 | // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4 |
| 320 | // FXPT_TYPE::LineTo). |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 321 | ASSERT_EQ(5, FPDFPath_CountSegments(green_rect)); |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 322 | // Verify actual coordinates. |
| 323 | FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0); |
| 324 | float x; |
| 325 | float y; |
| 326 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 327 | EXPECT_EQ(100, x); |
| 328 | EXPECT_EQ(100, y); |
| 329 | EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| 330 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 331 | segment = FPDFPath_GetPathSegment(green_rect, 1); |
| 332 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 333 | EXPECT_EQ(100, x); |
| 334 | EXPECT_EQ(140, y); |
| 335 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 336 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 337 | segment = FPDFPath_GetPathSegment(green_rect, 2); |
| 338 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 339 | EXPECT_EQ(140, x); |
| 340 | EXPECT_EQ(140, y); |
| 341 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 342 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 343 | segment = FPDFPath_GetPathSegment(green_rect, 3); |
| 344 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 345 | EXPECT_EQ(140, x); |
| 346 | EXPECT_EQ(100, y); |
| 347 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 348 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 349 | segment = FPDFPath_GetPathSegment(green_rect, 4); |
| 350 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 351 | EXPECT_EQ(100, x); |
| 352 | EXPECT_EQ(100, y); |
| 353 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 354 | EXPECT_TRUE(FPDFPathSegment_GetClose(segment)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 355 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 356 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0)); |
| 357 | FPDFPage_InsertObject(page, green_rect); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 358 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 359 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 360 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 361 | "7b0b87604594e773add528fae567a558"); |
| 362 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 363 | |
| 364 | // Add a black triangle. |
| 365 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100); |
| 366 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200)); |
| 367 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 368 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200)); |
| 369 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100)); |
| 370 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 371 | |
| 372 | // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2 |
| 373 | // FXPT_TYPE::LineTo). |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 374 | ASSERT_EQ(3, FPDFPath_CountSegments(black_path)); |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 375 | // Verify actual coordinates. |
| 376 | segment = FPDFPath_GetPathSegment(black_path, 0); |
| 377 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 378 | EXPECT_EQ(400, x); |
| 379 | EXPECT_EQ(100, y); |
| 380 | EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| 381 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 382 | segment = FPDFPath_GetPathSegment(black_path, 1); |
| 383 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 384 | EXPECT_EQ(400, x); |
| 385 | EXPECT_EQ(200, y); |
| 386 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 387 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 388 | segment = FPDFPath_GetPathSegment(black_path, 2); |
| 389 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 390 | EXPECT_EQ(300, x); |
| 391 | EXPECT_EQ(100, y); |
| 392 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 393 | EXPECT_TRUE(FPDFPathSegment_GetClose(segment)); |
| 394 | // Make sure out of bounds index access fails properly. |
| 395 | EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 396 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 397 | FPDFPage_InsertObject(page, black_path); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 398 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 399 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 400 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 401 | "eadc8020a14dfcf091da2688733d8806"); |
| 402 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 403 | |
| 404 | // Now add a more complex blue path. |
| 405 | FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200); |
| 406 | EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255)); |
| 407 | EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0)); |
| 408 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230)); |
| 409 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300)); |
| 410 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325)); |
| 411 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325)); |
| 412 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400)); |
| 413 | EXPECT_TRUE(FPDFPath_Close(blue_path)); |
| 414 | FPDFPage_InsertObject(page, blue_path); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 415 | const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946"; |
| 416 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 417 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 418 | CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5); |
| 419 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 420 | |
| 421 | // Now save the result, closing the page and document |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 422 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 423 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 424 | FPDF_ClosePage(page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 425 | |
| 426 | // Render the saved result |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 427 | VerifySavedDocument(612, 792, kLastMD5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 428 | } |
| 429 | |
Henrique Nakashima | 5ebfd64 | 2018-06-07 15:18:55 +0000 | [diff] [blame^] | 430 | // Fails due to pdfium:1051. |
| 431 | TEST_F(FPDFEditEmbeddertest, DISABLED_SetText) { |
| 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 | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 522 | TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) { |
| 523 | // Load document with some text. |
| 524 | EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf")); |
| 525 | FPDF_PAGE page = LoadPage(0); |
| 526 | ASSERT_TRUE(page); |
| 527 | |
| 528 | // Show what the original file looks like. |
| 529 | { |
| 530 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 531 | const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df"; |
| 532 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 533 | const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655"; |
| 534 | #else |
| 535 | const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86"; |
| 536 | #endif |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 537 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 538 | CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5); |
| 539 | } |
| 540 | |
| 541 | // Iterate over all objects, counting the number of times each content mark |
| 542 | // name appears. |
| 543 | int object_count = FPDFPage_CountObjects(page); |
| 544 | ASSERT_EQ(19, object_count); |
| 545 | |
| 546 | unsigned long prime_count = 0; |
| 547 | unsigned long square_count = 0; |
| 548 | unsigned long greater_than_ten_count = 0; |
| 549 | std::vector<FPDF_PAGEOBJECT> primes; |
| 550 | for (int i = 0; i < object_count; ++i) { |
| 551 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i); |
| 552 | |
| 553 | int mark_count = FPDFPageObj_CountMarks(page_object); |
| 554 | for (int j = 0; j < mark_count; ++j) { |
| 555 | FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j); |
| 556 | |
| 557 | char buffer[256]; |
| 558 | ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u); |
| 559 | std::wstring name = |
| 560 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 561 | if (name == L"Prime") { |
| 562 | prime_count++; |
Henrique Nakashima | aed6253 | 2018-04-17 20:34:38 +0000 | [diff] [blame] | 563 | EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark)); |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 564 | primes.push_back(page_object); |
| 565 | } else if (name == L"Square") { |
| 566 | square_count++; |
Henrique Nakashima | aed6253 | 2018-04-17 20:34:38 +0000 | [diff] [blame] | 567 | EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark)); |
Henrique Nakashima | 132c38e | 2018-04-23 16:35:56 +0000 | [diff] [blame] | 568 | ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u); |
| 569 | std::wstring key = |
| 570 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 571 | EXPECT_EQ(L"Factor", key); |
| 572 | EXPECT_EQ(FPDF_OBJECT_NUMBER, |
| 573 | FPDFPageObjMark_GetParamValueType(mark, 0)); |
| 574 | int square_root = FPDFPageObjMark_GetParamIntValue(mark, 0); |
| 575 | EXPECT_EQ(i + 1, square_root * square_root); |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 576 | } else if (name == L"GreaterThanTen") { |
| 577 | greater_than_ten_count++; |
Henrique Nakashima | aed6253 | 2018-04-17 20:34:38 +0000 | [diff] [blame] | 578 | EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark)); |
Henrique Nakashima | b557bdc | 2018-04-23 18:04:26 +0000 | [diff] [blame] | 579 | } else if (name == L"Bounds") { |
| 580 | EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark)); |
| 581 | ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u); |
| 582 | std::wstring key = |
| 583 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 584 | EXPECT_EQ(L"Position", key); |
| 585 | EXPECT_EQ(FPDF_OBJECT_STRING, |
| 586 | FPDFPageObjMark_GetParamValueType(mark, 0)); |
| 587 | ASSERT_GT(FPDFPageObjMark_GetParamStringValue(mark, 0, buffer, 256), |
| 588 | 0u); |
| 589 | std::wstring value = |
| 590 | GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)); |
| 591 | EXPECT_EQ(L"Last", value); |
| 592 | EXPECT_EQ(18, i); |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 593 | } else { |
| 594 | FAIL(); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | // Expect certain number of tagged objects. The test file contains strings |
| 600 | // from 1 to 19. |
| 601 | EXPECT_EQ(8u, prime_count); |
| 602 | EXPECT_EQ(4u, square_count); |
| 603 | EXPECT_EQ(9u, greater_than_ten_count); |
| 604 | |
| 605 | // Remove all objects marked with "Prime". |
| 606 | for (FPDF_PAGEOBJECT page_object : primes) { |
| 607 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 608 | FPDFPageObj_Destroy(page_object); |
| 609 | } |
| 610 | |
| 611 | EXPECT_EQ(11, FPDFPage_CountObjects(page)); |
| 612 | |
| 613 | { |
| 614 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
| 615 | const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65"; |
| 616 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
| 617 | const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c"; |
| 618 | #else |
| 619 | const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922"; |
| 620 | #endif |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 621 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | c90adc5 | 2018-03-27 16:26:44 +0000 | [diff] [blame] | 622 | CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5); |
| 623 | } |
| 624 | |
| 625 | UnloadPage(page); |
| 626 | } |
| 627 | |
Henrique Nakashima | c49e62e | 2018-04-16 20:58:47 +0000 | [diff] [blame] | 628 | // Fails due to pdfium:1051. |
| 629 | TEST_F(FPDFEditEmbeddertest, DISABLED_RemoveExistingPageObject) { |
| 630 | // Load document with some text. |
| 631 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 632 | FPDF_PAGE page = LoadPage(0); |
| 633 | ASSERT_TRUE(page); |
| 634 | |
| 635 | // Get the "Hello, world!" text object and remove it. |
| 636 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 637 | FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0); |
| 638 | ASSERT_TRUE(page_object); |
| 639 | EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object)); |
| 640 | |
| 641 | // Verify the "Hello, world!" text is gone. |
| 642 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 643 | |
| 644 | // Save the file |
| 645 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 646 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 647 | UnloadPage(page); |
| 648 | FPDFPageObj_Destroy(page_object); |
| 649 | |
| 650 | // Re-open the file and check the page object count is still 1. |
| 651 | OpenSavedDocument(); |
| 652 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 653 | EXPECT_EQ(1, FPDFPage_CountObjects(saved_page)); |
| 654 | CloseSavedPage(saved_page); |
| 655 | CloseSavedDocument(); |
| 656 | } |
| 657 | |
| 658 | TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) { |
| 659 | // Load document with some text. |
| 660 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 661 | FPDF_PAGE page = LoadPage(0); |
| 662 | ASSERT_TRUE(page); |
| 663 | |
| 664 | // Add a red rectangle. |
| 665 | ASSERT_EQ(2, FPDFPage_CountObjects(page)); |
| 666 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| 667 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 668 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 669 | FPDFPage_InsertObject(page, red_rect); |
| 670 | |
| 671 | // Verify the red rectangle was added. |
| 672 | ASSERT_EQ(3, FPDFPage_CountObjects(page)); |
| 673 | |
| 674 | // Save the file |
| 675 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 676 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 677 | UnloadPage(page); |
| 678 | |
| 679 | // Re-open the file and check the page object count is still 3. |
| 680 | OpenSavedDocument(); |
| 681 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 682 | EXPECT_EQ(3, FPDFPage_CountObjects(saved_page)); |
| 683 | CloseSavedPage(saved_page); |
| 684 | CloseSavedDocument(); |
| 685 | } |
| 686 | |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 687 | TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) { |
| 688 | // Start with a blank page. |
| 689 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 690 | ASSERT_TRUE(page); |
| 691 | |
| 692 | // Render the blank page and verify it's a blank bitmap. |
| 693 | const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3"; |
| 694 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 695 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 696 | CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5); |
| 697 | } |
| 698 | ASSERT_EQ(0, FPDFPage_CountObjects(page)); |
| 699 | |
| 700 | // Add a red rectangle. |
| 701 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| 702 | ASSERT_TRUE(red_rect); |
| 703 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 704 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 705 | FPDFPage_InsertObject(page, red_rect); |
| 706 | const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497"; |
| 707 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 708 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 709 | CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5); |
| 710 | } |
| 711 | EXPECT_EQ(1, FPDFPage_CountObjects(page)); |
| 712 | |
| 713 | // Remove rectangle and verify it does not render anymore and the bitmap is |
| 714 | // back to a blank one. |
| 715 | EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect)); |
| 716 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 717 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Henrique Nakashima | 35841fa | 2018-03-15 15:25:16 +0000 | [diff] [blame] | 718 | CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5); |
| 719 | } |
| 720 | EXPECT_EQ(0, FPDFPage_CountObjects(page)); |
| 721 | |
| 722 | // Trying to remove an object not in the page should return false. |
| 723 | EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect)); |
| 724 | |
| 725 | FPDF_ClosePage(page); |
| 726 | FPDFPageObj_Destroy(red_rect); |
| 727 | } |
| 728 | |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 729 | TEST_F(FPDFEditEmbeddertest, PathsPoints) { |
| 730 | CreateNewDocument(); |
| 731 | FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_); |
| 732 | // This should fail gracefully, even if img is not a path. |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 733 | ASSERT_EQ(-1, FPDFPath_CountSegments(img)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 734 | |
| 735 | // This should fail gracefully, even if path is NULL. |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 736 | ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 737 | |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 738 | // FPDFPath_GetPathSegment() with a non-path. |
| 739 | ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0)); |
| 740 | // FPDFPath_GetPathSegment() with a NULL path. |
| 741 | ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0)); |
| 742 | float x; |
| 743 | float y; |
| 744 | // FPDFPathSegment_GetPoint() with a NULL segment. |
| 745 | EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y)); |
| 746 | |
| 747 | // FPDFPathSegment_GetType() with a NULL segment. |
| 748 | ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr)); |
| 749 | |
| 750 | // FPDFPathSegment_GetClose() with a NULL segment. |
| 751 | EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr)); |
| 752 | |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 753 | FPDFPageObj_Destroy(img); |
| 754 | } |
| 755 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 756 | TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) { |
| 757 | // Load document with some text |
| 758 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 759 | FPDF_PAGE page = LoadPage(0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 760 | ASSERT_TRUE(page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 761 | |
| 762 | // Add an opaque rectangle on top of some of the text. |
| 763 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| 764 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 765 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 766 | FPDFPage_InsertObject(page, red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 767 | |
| 768 | // Add a transparent triangle on top of other part of the text. |
| 769 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50); |
| 770 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100)); |
| 771 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 772 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80)); |
| 773 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10)); |
| 774 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
| 775 | FPDFPage_InsertObject(page, black_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 776 | |
| 777 | // Render and check the result. Text is slightly different on Mac. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 778 | ScopedFPDFBitmap bitmap = RenderLoadedPage(page); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 779 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 780 | const char md5[] = "f9e6fa74230f234286bfcada9f7606d8"; |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 781 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 782 | const char md5[] = "aa71b09b93b55f467f1290e5111babee"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 783 | #endif |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 784 | CompareBitmap(bitmap.get(), 200, 200, md5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 785 | UnloadPage(page); |
| 786 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 787 | |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 788 | TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) { |
| 789 | // Load document with existing content |
| 790 | EXPECT_TRUE(OpenDocument("bug_717.pdf")); |
| 791 | FPDF_PAGE page = LoadPage(0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 792 | ASSERT_TRUE(page); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 793 | |
| 794 | // Add a transparent rectangle on top of the existing content |
| 795 | FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50); |
| 796 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100)); |
| 797 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
| 798 | FPDFPage_InsertObject(page, red_rect2); |
| 799 | |
| 800 | // Add an opaque rectangle on top of the existing content |
| 801 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50); |
| 802 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 803 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 804 | FPDFPage_InsertObject(page, red_rect); |
| 805 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 806 | ScopedFPDFBitmap bitmap = RenderLoadedPage(page); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 807 | CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 808 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 809 | |
| 810 | // Now save the result, closing the page and document |
| 811 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 812 | UnloadPage(page); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 813 | |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 814 | OpenSavedDocument(); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 815 | FPDF_PAGE saved_page = LoadSavedPage(0); |
| 816 | VerifySavedRendering(saved_page, 612, 792, |
| 817 | "ad04e5bd0f471a9a564fb034bd0fb073"); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 818 | |
| 819 | ClearString(); |
| 820 | // Add another opaque rectangle on top of the existing content |
| 821 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50); |
| 822 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255)); |
| 823 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 824 | FPDFPage_InsertObject(saved_page, green_rect); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 825 | |
| 826 | // Add another transparent rectangle on top of existing content |
| 827 | FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50); |
| 828 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100)); |
| 829 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 830 | FPDFPage_InsertObject(saved_page, green_rect2); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 831 | const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd"; |
| 832 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 833 | ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page); |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 834 | CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5); |
| 835 | } |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 836 | EXPECT_TRUE(FPDFPage_GenerateContent(saved_page)); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 837 | |
| 838 | // Now save the result, closing the page and document |
Lei Zhang | 0729be2 | 2018-02-05 21:13:51 +0000 | [diff] [blame] | 839 | EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0)); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 840 | |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 841 | CloseSavedPage(saved_page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 842 | CloseSavedDocument(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 843 | |
| 844 | // Render the saved result |
Lei Zhang | c113c7a | 2018-02-12 14:58:44 +0000 | [diff] [blame] | 845 | VerifySavedDocument(612, 792, kLastMD5); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 846 | } |
| 847 | |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 848 | TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) { |
| 849 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 850 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 851 | |
| 852 | // Add a large stroked rectangle (fill color should not affect it). |
| 853 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400); |
| 854 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255)); |
| 855 | EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255)); |
| 856 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f)); |
Miklos Vajna | 366df7f | 2018-05-22 14:27:29 +0000 | [diff] [blame] | 857 | |
| 858 | float width = 0; |
| 859 | EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width)); |
| 860 | EXPECT_EQ(15.0f, width); |
| 861 | |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 862 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1)); |
| 863 | FPDFPage_InsertObject(page, rect); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 864 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 865 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 866 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 867 | "64bd31f862a89e0a9e505a5af6efd506"); |
| 868 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 869 | |
| 870 | // Add crossed-checkmark |
| 871 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500); |
| 872 | EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400)); |
| 873 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600)); |
| 874 | EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600)); |
| 875 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400)); |
| 876 | EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180)); |
| 877 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f)); |
| 878 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 879 | FPDFPage_InsertObject(page, check); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 880 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 881 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 882 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 883 | "4b6f3b9d25c4e194821217d5016c3724"); |
| 884 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 885 | |
| 886 | // Add stroked and filled oval-ish path. |
| 887 | FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100); |
| 888 | EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300)); |
| 889 | EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305)); |
| 890 | EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105)); |
| 891 | EXPECT_TRUE(FPDFPath_Close(path)); |
| 892 | EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100)); |
| 893 | EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150)); |
| 894 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f)); |
| 895 | EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1)); |
| 896 | FPDFPage_InsertObject(page, path); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 897 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 898 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 899 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 900 | "ff3e6a22326754944cc6e56609acd73b"); |
| 901 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 902 | FPDF_ClosePage(page); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 903 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 904 | |
| 905 | TEST_F(FPDFEditEmbeddertest, AddStandardFontText) { |
| 906 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 907 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 908 | |
| 909 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 910 | FPDF_PAGEOBJECT text_object1 = |
| 911 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 912 | EXPECT_TRUE(text_object1); |
| 913 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 = |
| 914 | GetFPDFWideString(L"I'm at the bottom of the page"); |
| 915 | EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get())); |
| 916 | FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20); |
| 917 | FPDFPage_InsertObject(page, text_object1); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 918 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 919 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 920 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 921 | const char md5[] = "a4dddc1a3930fa694bbff9789dab4161"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 922 | #else |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 923 | const char md5[] = "eacaa24573b8ce997b3882595f096f00"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 924 | #endif |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 925 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
| 926 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 927 | |
| 928 | // Try another font |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 929 | FPDF_PAGEOBJECT text_object2 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 930 | FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 931 | EXPECT_TRUE(text_object2); |
| 932 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 933 | GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold."); |
| 934 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 935 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600); |
| 936 | FPDFPage_InsertObject(page, text_object2); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 937 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 938 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 939 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 940 | const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 941 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 942 | const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 943 | #else |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 944 | const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 945 | #endif |
Dan Sinclair | 971a674 | 2018-03-28 19:23:25 +0000 | [diff] [blame] | 946 | CompareBitmap(page_bitmap.get(), 612, 792, md5_2); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 947 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 948 | |
| 949 | // And some randomly transformed text |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 950 | FPDF_PAGEOBJECT text_object3 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 951 | FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 952 | EXPECT_TRUE(text_object3); |
| 953 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 = |
| 954 | GetFPDFWideString(L"Can you read me? <:)>"); |
| 955 | EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get())); |
| 956 | FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200); |
| 957 | FPDFPage_InsertObject(page, text_object3); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 958 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 959 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 960 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 961 | const char md5_3[] = "40b3ef04f915ff4c4208948001763544"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 962 | #elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 963 | const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 964 | #else |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 965 | const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 966 | #endif |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 967 | CompareBitmap(page_bitmap.get(), 612, 792, md5_3); |
| 968 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 969 | |
| 970 | // TODO(npm): Why are there issues with text rotated by 90 degrees? |
| 971 | // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this. |
| 972 | FPDF_ClosePage(page); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 973 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 974 | |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 975 | TEST_F(FPDFEditEmbeddertest, GraphicsData) { |
| 976 | // New page |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 977 | ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792)); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 978 | |
| 979 | // Create a rect with nontrivial graphics |
| 980 | FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 981 | FPDFPageObj_SetBlendMode(rect1, "Color"); |
| 982 | FPDFPage_InsertObject(page.get(), rect1); |
| 983 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| 984 | |
| 985 | // Check that the ExtGState was created |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 986 | CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get()); |
| 987 | CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState"); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 988 | ASSERT_TRUE(graphics_dict); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 989 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 990 | |
| 991 | // Add a text object causing no change to the graphics dictionary |
| 992 | FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 993 | // Only alpha, the last component, matters for the graphics dictionary. And |
| 994 | // the default value is 255. |
| 995 | EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255)); |
| 996 | FPDFPage_InsertObject(page.get(), text1); |
| 997 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 998 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 999 | |
| 1000 | // Add a text object increasing the size of the graphics dictionary |
| 1001 | FPDF_PAGEOBJECT text2 = |
| 1002 | FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f); |
| 1003 | FPDFPage_InsertObject(page.get(), text2); |
| 1004 | FPDFPageObj_SetBlendMode(text2, "Darken"); |
| 1005 | EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150)); |
| 1006 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1007 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1008 | |
| 1009 | // Add a path that should reuse graphics |
Nicolas Pena | ce67be4 | 2017-06-14 14:52:49 -0400 | [diff] [blame] | 1010 | FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1011 | FPDFPageObj_SetBlendMode(path, "Darken"); |
| 1012 | EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150)); |
| 1013 | FPDFPage_InsertObject(page.get(), path); |
| 1014 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1015 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1016 | |
| 1017 | // Add a rect increasing the size of the graphics dictionary |
| 1018 | FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 1019 | FPDFPageObj_SetBlendMode(rect2, "Darken"); |
| 1020 | EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150)); |
| 1021 | EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200)); |
| 1022 | FPDFPage_InsertObject(page.get(), rect2); |
| 1023 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1024 | EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 1025 | } |
| 1026 | |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1027 | TEST_F(FPDFEditEmbeddertest, DoubleGenerating) { |
| 1028 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1029 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1030 | |
| 1031 | // Add a red rectangle with some non-default alpha |
| 1032 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 1033 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128)); |
| 1034 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0)); |
| 1035 | FPDFPage_InsertObject(page, rect); |
| 1036 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1037 | |
| 1038 | // Check the ExtGState |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1039 | CPDF_Page* cpage = CPDFPageFromFPDFPage(page); |
| 1040 | CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState"); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1041 | ASSERT_TRUE(graphics_dict); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1042 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1043 | |
| 1044 | // Check the bitmap |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1045 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1046 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1047 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 1048 | "5384da3406d62360ffb5cac4476fff1c"); |
| 1049 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1050 | |
| 1051 | // Never mind, my new favorite color is blue, increase alpha |
| 1052 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180)); |
| 1053 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1054 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1055 | |
| 1056 | // Check that bitmap displays changed content |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1057 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1058 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1059 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 1060 | "2e51656f5073b0bee611d9cd086aa09c"); |
| 1061 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1062 | |
| 1063 | // And now generate, without changes |
| 1064 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1065 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1066 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1067 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1068 | CompareBitmap(page_bitmap.get(), 612, 792, |
| 1069 | "2e51656f5073b0bee611d9cd086aa09c"); |
| 1070 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1071 | |
| 1072 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1073 | FPDF_PAGEOBJECT text_object = |
| 1074 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 1075 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1076 | GetFPDFWideString(L"Something something #text# something"); |
| 1077 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 1078 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300); |
| 1079 | FPDFPage_InsertObject(page, text_object); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1080 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1081 | CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font"); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1082 | ASSERT_TRUE(font_dict); |
| 1083 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 1084 | |
| 1085 | // Generate yet again, check dicts are reasonably sized |
| 1086 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1087 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1088 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 1089 | FPDF_ClosePage(page); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 1090 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1091 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1092 | TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) { |
| 1093 | CreateNewDocument(); |
| 1094 | // TODO(npm): use other fonts after disallowing loading any font as any type |
| 1095 | const CPDF_Font* stock_font = |
| 1096 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1097 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1098 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1099 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1100 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false)); |
| 1101 | ASSERT_TRUE(font.get()); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame] | 1102 | CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1103 | EXPECT_TRUE(typed_font->IsType1Font()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1104 | |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 1105 | const CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1106 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 1107 | EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype")); |
| 1108 | EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont")); |
| 1109 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 1110 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 1111 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1112 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
| 1113 | |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1114 | const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1115 | ASSERT_TRUE(widths_array); |
| 1116 | ASSERT_EQ(224U, widths_array->GetCount()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1117 | EXPECT_EQ(250, widths_array->GetNumberAt(0)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1118 | EXPECT_EQ(569, widths_array->GetNumberAt(11)); |
| 1119 | EXPECT_EQ(500, widths_array->GetNumberAt(223)); |
| 1120 | CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data); |
| 1121 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1122 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1123 | TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) { |
| 1124 | CreateNewDocument(); |
| 1125 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1126 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1127 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1128 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1129 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false)); |
| 1130 | ASSERT_TRUE(font.get()); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame] | 1131 | CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1132 | EXPECT_TRUE(typed_font->IsTrueTypeFont()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1133 | |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 1134 | const CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1135 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 1136 | EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype")); |
| 1137 | EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont")); |
| 1138 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 1139 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 1140 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
| 1141 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1142 | |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1143 | const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1144 | ASSERT_TRUE(widths_array); |
| 1145 | ASSERT_EQ(224U, widths_array->GetCount()); |
| 1146 | EXPECT_EQ(600, widths_array->GetNumberAt(33)); |
| 1147 | EXPECT_EQ(600, widths_array->GetNumberAt(74)); |
| 1148 | EXPECT_EQ(600, widths_array->GetNumberAt(223)); |
| 1149 | CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data); |
| 1150 | } |
| 1151 | |
| 1152 | TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) { |
| 1153 | CreateNewDocument(); |
| 1154 | const CPDF_Font* stock_font = |
| 1155 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1156 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1157 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1158 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1159 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1)); |
| 1160 | ASSERT_TRUE(font.get()); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame] | 1161 | CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1162 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 1163 | |
| 1164 | // Check font dictionary entries |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 1165 | const CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1166 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 1167 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 1168 | EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont")); |
| 1169 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1170 | const CPDF_Array* descendant_array = |
| 1171 | font_dict->GetArrayFor("DescendantFonts"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1172 | ASSERT_TRUE(descendant_array); |
| 1173 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 1174 | |
| 1175 | // Check the CIDFontDict |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1176 | const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1177 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 1178 | EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype")); |
| 1179 | EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont")); |
Lei Zhang | b1ec280 | 2018-05-25 21:55:24 +0000 | [diff] [blame] | 1180 | const CPDF_Dictionary* cidinfo_dict = |
| 1181 | cidfont_dict->GetDictFor("CIDSystemInfo"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1182 | ASSERT_TRUE(cidinfo_dict); |
| 1183 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 1184 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 1185 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 1186 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data); |
| 1187 | |
| 1188 | // Check widths |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1189 | const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1190 | ASSERT_TRUE(widths_array); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 1191 | EXPECT_GT(widths_array->GetCount(), 1U); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1192 | CheckCompositeFontWidths(widths_array, typed_font); |
| 1193 | } |
| 1194 | |
| 1195 | TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) { |
| 1196 | CreateNewDocument(); |
| 1197 | const CPDF_Font* stock_font = |
| 1198 | CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1199 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1200 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1201 | |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1202 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1203 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1)); |
| 1204 | ASSERT_TRUE(font.get()); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame] | 1205 | CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1206 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 1207 | |
| 1208 | // Check font dictionary entries |
Lei Zhang | 710fa99 | 2018-05-25 16:24:48 +0000 | [diff] [blame] | 1209 | const CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1210 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 1211 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 1212 | EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont")); |
| 1213 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1214 | const CPDF_Array* descendant_array = |
| 1215 | font_dict->GetArrayFor("DescendantFonts"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1216 | ASSERT_TRUE(descendant_array); |
| 1217 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 1218 | |
| 1219 | // Check the CIDFontDict |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1220 | const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1221 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 1222 | EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype")); |
| 1223 | EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont")); |
Lei Zhang | b1ec280 | 2018-05-25 21:55:24 +0000 | [diff] [blame] | 1224 | const CPDF_Dictionary* cidinfo_dict = |
| 1225 | cidfont_dict->GetDictFor("CIDSystemInfo"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1226 | ASSERT_TRUE(cidinfo_dict); |
| 1227 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 1228 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 1229 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 1230 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size, |
| 1231 | data); |
| 1232 | |
| 1233 | // Check widths |
Lei Zhang | de579ab | 2018-05-25 21:49:49 +0000 | [diff] [blame] | 1234 | const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 1235 | ASSERT_TRUE(widths_array); |
| 1236 | CheckCompositeFontWidths(widths_array, typed_font); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 1237 | } |
rbpotter | ce8e51e | 2017-04-28 12:42:47 -0700 | [diff] [blame] | 1238 | |
| 1239 | TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) { |
| 1240 | // Load document with a -90 degree rotation |
| 1241 | EXPECT_TRUE(OpenDocument("bug_713197.pdf")); |
| 1242 | FPDF_PAGE page = LoadPage(0); |
| 1243 | EXPECT_NE(nullptr, page); |
| 1244 | |
| 1245 | EXPECT_EQ(3, FPDFPage_GetRotation(page)); |
| 1246 | UnloadPage(page); |
| 1247 | } |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1248 | |
| 1249 | TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) { |
| 1250 | // Start with a blank page |
| 1251 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 1252 | { |
| 1253 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 1254 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 1255 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1256 | ScopedFPDFFont font( |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1257 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0)); |
| 1258 | ASSERT_TRUE(font.get()); |
| 1259 | |
| 1260 | // Add some text to the page |
| 1261 | FPDF_PAGEOBJECT text_object = |
| 1262 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 1263 | EXPECT_TRUE(text_object); |
| 1264 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1265 | GetFPDFWideString(L"I am testing my loaded font, WEE."); |
| 1266 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 1267 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400); |
| 1268 | FPDFPage_InsertObject(page, text_object); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1269 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1270 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1271 | const char md5[] = "17d2b6cd574cf66170b09c8927529a94"; |
| 1272 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 1273 | const char md5[] = "70592859010ffbf532a2237b8118bcc4"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1274 | #endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1275 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1276 | |
| 1277 | // Add some more text, same font |
| 1278 | FPDF_PAGEOBJECT text_object2 = |
| 1279 | FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f); |
| 1280 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 1281 | GetFPDFWideString(L"Bigger font size"); |
| 1282 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 1283 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200); |
| 1284 | FPDFPage_InsertObject(page, text_object2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1285 | } |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1286 | ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0); |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1287 | #if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1288 | const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea"; |
| 1289 | #else |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 1290 | const char md5_2[] = "c1d10cce1761c4a998a16b2562030568"; |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1291 | #endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1292 | CompareBitmap(page_bitmap2.get(), 612, 792, md5_2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1293 | |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 1294 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1295 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1296 | FPDF_ClosePage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1297 | |
| 1298 | VerifySavedDocument(612, 792, md5_2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 1299 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 1300 | |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 1301 | TEST_F(FPDFEditEmbeddertest, TransformAnnot) { |
| 1302 | // Open a file with one annotation and load its first page. |
| 1303 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
Lei Zhang | 75c8171 | 2018-02-08 17:22:39 +0000 | [diff] [blame] | 1304 | FPDF_PAGE page = LoadPage(0); |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 1305 | ASSERT_TRUE(page); |
| 1306 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1307 | { |
| 1308 | // Add an underline annotation to the page without specifying its rectangle. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1309 | ScopedFPDFAnnotation annot( |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1310 | FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE)); |
| 1311 | ASSERT_TRUE(annot); |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 1312 | |
Lei Zhang | a21d593 | 2018-02-05 18:28:38 +0000 | [diff] [blame] | 1313 | // FPDFPage_TransformAnnots() should run without errors when modifying |
| 1314 | // annotation rectangles. |
| 1315 | FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6); |
| 1316 | } |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 1317 | UnloadPage(page); |
| 1318 | } |
| 1319 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 1320 | // TODO(npm): Add tests using Japanese fonts in other OS. |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1321 | #if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_ |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 1322 | TEST_F(FPDFEditEmbeddertest, AddCIDFontText) { |
| 1323 | // Start with a blank page |
| 1324 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 1325 | CFX_Font CIDfont; |
| 1326 | { |
| 1327 | // First, get the data from the font |
| 1328 | CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0); |
| 1329 | EXPECT_EQ("IPAGothic", CIDfont.GetFaceName()); |
| 1330 | const uint8_t* data = CIDfont.GetFontData(); |
| 1331 | const uint32_t size = CIDfont.GetSize(); |
| 1332 | |
| 1333 | // Load the data into a FPDF_Font. |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1334 | ScopedFPDFFont font( |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 1335 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1)); |
| 1336 | ASSERT_TRUE(font.get()); |
| 1337 | |
| 1338 | // Add some text to the page |
| 1339 | FPDF_PAGEOBJECT text_object = |
| 1340 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 1341 | ASSERT_TRUE(text_object); |
| 1342 | std::wstring wstr = L"ABCDEFGhijklmnop."; |
| 1343 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 1344 | GetFPDFWideString(wstr); |
| 1345 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 1346 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200); |
| 1347 | FPDFPage_InsertObject(page, text_object); |
| 1348 | |
| 1349 | // And add some Japanese characters |
| 1350 | FPDF_PAGEOBJECT text_object2 = |
| 1351 | FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f); |
| 1352 | ASSERT_TRUE(text_object2); |
| 1353 | std::wstring wstr2 = |
| 1354 | L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1" |
| 1355 | L"\u756A"; |
| 1356 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 1357 | GetFPDFWideString(wstr2); |
| 1358 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 1359 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500); |
| 1360 | FPDFPage_InsertObject(page, text_object2); |
| 1361 | } |
| 1362 | |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 1363 | // Check that the text renders properly. |
Henrique Nakashima | 09b4192 | 2017-10-27 20:39:29 +0000 | [diff] [blame] | 1364 | const char md5[] = "c68cd79aa72bf83a7b25271370d46b21"; |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1365 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1366 | ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1367 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
| 1368 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 1369 | |
| 1370 | // Save the document, close the page. |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 1371 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 1372 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1373 | FPDF_ClosePage(page); |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1374 | |
| 1375 | VerifySavedDocument(612, 792, md5); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 1376 | } |
Dan Sinclair | 698aed7 | 2017-09-26 16:24:49 -0400 | [diff] [blame] | 1377 | #endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_ |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1378 | |
| 1379 | TEST_F(FPDFEditEmbeddertest, SaveAndRender) { |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 1380 | const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b"; |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1381 | { |
| 1382 | EXPECT_TRUE(OpenDocument("bug_779.pdf")); |
| 1383 | FPDF_PAGE page = LoadPage(0); |
| 1384 | ASSERT_NE(nullptr, page); |
| 1385 | |
| 1386 | // Now add a more complex blue path. |
| 1387 | FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20); |
| 1388 | EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200)); |
| 1389 | // TODO(npm): stroking will cause the MD5s to differ. |
| 1390 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0)); |
| 1391 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63)); |
| 1392 | EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90)); |
| 1393 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133)); |
| 1394 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33)); |
| 1395 | EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40)); |
| 1396 | EXPECT_TRUE(FPDFPath_Close(green_path)); |
| 1397 | FPDFPage_InsertObject(page, green_path); |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1398 | ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page); |
Lei Zhang | 107fa7b | 2018-02-09 21:48:15 +0000 | [diff] [blame] | 1399 | CompareBitmap(page_bitmap.get(), 612, 792, md5); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1400 | |
| 1401 | // Now save the result, closing the page and document |
| 1402 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1403 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1404 | UnloadPage(page); |
| 1405 | } |
Dan Sinclair | 04e4dc8 | 2017-10-18 12:17:14 -0400 | [diff] [blame] | 1406 | |
| 1407 | VerifySavedDocument(612, 792, md5); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1408 | } |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 1409 | |
| 1410 | TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) { |
| 1411 | ASSERT_TRUE(OpenDocument("embedded_images.pdf")); |
| 1412 | FPDF_PAGE page = LoadPage(0); |
| 1413 | ASSERT_TRUE(page); |
Miklos Vajna | 9262761 | 2017-09-25 12:59:29 +0200 | [diff] [blame] | 1414 | ASSERT_EQ(39, FPDFPage_CountObjects(page)); |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 1415 | |
| 1416 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32); |
| 1417 | EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1418 | EXPECT_FALSE(FPDFImageObj_GetBitmap(obj)); |
| 1419 | |
| 1420 | obj = FPDFPage_GetObject(page, 33); |
| 1421 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1422 | FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj); |
| 1423 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1424 | CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae"); |
| 1425 | FPDFBitmap_Destroy(bitmap); |
| 1426 | |
| 1427 | obj = FPDFPage_GetObject(page, 34); |
| 1428 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1429 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1430 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1431 | CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6"); |
| 1432 | FPDFBitmap_Destroy(bitmap); |
| 1433 | |
| 1434 | obj = FPDFPage_GetObject(page, 35); |
| 1435 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1436 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1437 | EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap)); |
| 1438 | CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3"); |
| 1439 | FPDFBitmap_Destroy(bitmap); |
| 1440 | |
| 1441 | obj = FPDFPage_GetObject(page, 36); |
| 1442 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1443 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1444 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1445 | CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a"); |
| 1446 | FPDFBitmap_Destroy(bitmap); |
| 1447 | |
| 1448 | obj = FPDFPage_GetObject(page, 37); |
| 1449 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1450 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1451 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1452 | CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534"); |
| 1453 | FPDFBitmap_Destroy(bitmap); |
| 1454 | |
| 1455 | obj = FPDFPage_GetObject(page, 38); |
| 1456 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1457 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1458 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1459 | CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5"); |
| 1460 | FPDFBitmap_Destroy(bitmap); |
| 1461 | UnloadPage(page); |
| 1462 | } |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 1463 | |
Lei Zhang | 53341dd | 2018-03-01 15:42:47 +0000 | [diff] [blame] | 1464 | TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) { |
| 1465 | ASSERT_TRUE(OpenDocument("bug_631912.pdf")); |
| 1466 | FPDF_PAGE page = LoadPage(0); |
| 1467 | ASSERT_TRUE(page); |
| 1468 | ASSERT_EQ(1, FPDFPage_CountObjects(page)); |
| 1469 | |
| 1470 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0); |
| 1471 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1472 | { |
Tom Sepez | e08d2b1 | 2018-04-25 18:49:32 +0000 | [diff] [blame] | 1473 | ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj)); |
Lei Zhang | 1330ebb | 2018-03-05 15:16:37 +0000 | [diff] [blame] | 1474 | ASSERT_TRUE(bitmap); |
| 1475 | EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get())); |
| 1476 | CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de"); |
Lei Zhang | 53341dd | 2018-03-01 15:42:47 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | UnloadPage(page); |
| 1480 | } |
| 1481 | |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 1482 | TEST_F(FPDFEditEmbeddertest, GetImageData) { |
| 1483 | EXPECT_TRUE(OpenDocument("embedded_images.pdf")); |
| 1484 | FPDF_PAGE page = LoadPage(0); |
| 1485 | ASSERT_TRUE(page); |
Miklos Vajna | 9262761 | 2017-09-25 12:59:29 +0200 | [diff] [blame] | 1486 | ASSERT_EQ(39, FPDFPage_CountObjects(page)); |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 1487 | |
| 1488 | // Retrieve an image object with flate-encoded data stream. |
| 1489 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33); |
| 1490 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1491 | |
| 1492 | // Check that the raw image data has the correct length and hash value. |
| 1493 | unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0); |
| 1494 | std::vector<char> buf(len); |
| 1495 | EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len)); |
| 1496 | EXPECT_EQ("f73802327d2e88e890f653961bcda81a", |
| 1497 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 1498 | |
| 1499 | // Check that the decoded image data has the correct length and hash value. |
| 1500 | len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0); |
| 1501 | buf.clear(); |
| 1502 | buf.resize(len); |
| 1503 | EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len)); |
| 1504 | EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e", |
| 1505 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 1506 | |
| 1507 | // Retrieve an image obejct with DCTDecode-encoded data stream. |
| 1508 | obj = FPDFPage_GetObject(page, 37); |
| 1509 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1510 | |
| 1511 | // Check that the raw image data has the correct length and hash value. |
| 1512 | len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0); |
| 1513 | buf.clear(); |
| 1514 | buf.resize(len); |
| 1515 | EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len)); |
| 1516 | EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", |
| 1517 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 1518 | |
| 1519 | // Check that the decoded image data has the correct length and hash value, |
| 1520 | // which should be the same as those of the raw data, since this image is |
| 1521 | // encoded by a single DCTDecode filter and decoding is a noop. |
| 1522 | len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0); |
| 1523 | buf.clear(); |
| 1524 | buf.resize(len); |
| 1525 | EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len)); |
| 1526 | EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", |
| 1527 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 1528 | |
| 1529 | UnloadPage(page); |
| 1530 | } |
Jane Liu | 2e5f0ae | 2017-08-08 15:23:27 -0400 | [diff] [blame] | 1531 | |
| 1532 | TEST_F(FPDFEditEmbeddertest, DestroyPageObject) { |
| 1533 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| 1534 | ASSERT_TRUE(rect); |
| 1535 | |
| 1536 | // There should be no memory leaks with a call to FPDFPageObj_Destroy(). |
| 1537 | FPDFPageObj_Destroy(rect); |
| 1538 | } |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 1539 | |
| 1540 | TEST_F(FPDFEditEmbeddertest, GetImageFilters) { |
| 1541 | EXPECT_TRUE(OpenDocument("embedded_images.pdf")); |
| 1542 | FPDF_PAGE page = LoadPage(0); |
| 1543 | ASSERT_TRUE(page); |
| 1544 | |
| 1545 | // Verify that retrieving the filter of a non-image object would fail. |
| 1546 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32); |
| 1547 | ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1548 | ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj)); |
| 1549 | EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0)); |
| 1550 | |
| 1551 | // Verify the returned filter string for an image object with a single filter. |
| 1552 | obj = FPDFPage_GetObject(page, 33); |
| 1553 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1554 | ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj)); |
| 1555 | unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0); |
| 1556 | std::vector<char> buf(len); |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 1557 | static constexpr char kFlateDecode[] = "FlateDecode"; |
| 1558 | EXPECT_EQ(sizeof(kFlateDecode), |
| 1559 | FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len)); |
| 1560 | EXPECT_STREQ(kFlateDecode, buf.data()); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 1561 | EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0)); |
| 1562 | |
| 1563 | // Verify all the filters for an image object with a list of filters. |
| 1564 | obj = FPDFPage_GetObject(page, 38); |
| 1565 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1566 | ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj)); |
| 1567 | len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0); |
| 1568 | buf.clear(); |
| 1569 | buf.resize(len); |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 1570 | static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode"; |
| 1571 | EXPECT_EQ(sizeof(kASCIIHexDecode), |
| 1572 | FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len)); |
| 1573 | EXPECT_STREQ(kASCIIHexDecode, buf.data()); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 1574 | |
| 1575 | len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0); |
| 1576 | buf.clear(); |
| 1577 | buf.resize(len); |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 1578 | static constexpr char kDCTDecode[] = "DCTDecode"; |
| 1579 | EXPECT_EQ(sizeof(kDCTDecode), |
| 1580 | FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len)); |
| 1581 | EXPECT_STREQ(kDCTDecode, buf.data()); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 1582 | |
| 1583 | UnloadPage(page); |
| 1584 | } |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 1585 | |
| 1586 | TEST_F(FPDFEditEmbeddertest, GetImageMetadata) { |
| 1587 | ASSERT_TRUE(OpenDocument("embedded_images.pdf")); |
| 1588 | FPDF_PAGE page = LoadPage(0); |
| 1589 | ASSERT_TRUE(page); |
| 1590 | |
| 1591 | // Check that getting the metadata of a null object would fail. |
| 1592 | FPDF_IMAGEOBJ_METADATA metadata; |
| 1593 | EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata)); |
| 1594 | |
| 1595 | // Check that receiving the metadata with a null metadata object would fail. |
| 1596 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35); |
| 1597 | EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr)); |
| 1598 | |
| 1599 | // Check that when retrieving an image object's metadata without passing in |
| 1600 | // |page|, all values are correct, with the last two being default values. |
| 1601 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1602 | ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata)); |
Julian Lunger | ecd063e | 2017-12-27 10:18:50 -0500 | [diff] [blame] | 1603 | EXPECT_EQ(7, metadata.marked_content_id); |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 1604 | EXPECT_EQ(92u, metadata.width); |
| 1605 | EXPECT_EQ(68u, metadata.height); |
| 1606 | EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001); |
| 1607 | EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001); |
| 1608 | EXPECT_EQ(0u, metadata.bits_per_pixel); |
| 1609 | EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace); |
| 1610 | |
| 1611 | // Verify the metadata of a bitmap image with indexed colorspace. |
| 1612 | ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata)); |
Julian Lunger | ecd063e | 2017-12-27 10:18:50 -0500 | [diff] [blame] | 1613 | EXPECT_EQ(7, metadata.marked_content_id); |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 1614 | EXPECT_EQ(92u, metadata.width); |
| 1615 | EXPECT_EQ(68u, metadata.height); |
| 1616 | EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001); |
| 1617 | EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001); |
| 1618 | EXPECT_EQ(1u, metadata.bits_per_pixel); |
| 1619 | EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace); |
| 1620 | |
| 1621 | // Verify the metadata of an image with RGB colorspace. |
| 1622 | obj = FPDFPage_GetObject(page, 37); |
| 1623 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1624 | ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata)); |
Julian Lunger | ecd063e | 2017-12-27 10:18:50 -0500 | [diff] [blame] | 1625 | EXPECT_EQ(9, metadata.marked_content_id); |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 1626 | EXPECT_EQ(126u, metadata.width); |
| 1627 | EXPECT_EQ(106u, metadata.height); |
| 1628 | EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001); |
| 1629 | EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001); |
| 1630 | EXPECT_EQ(24u, metadata.bits_per_pixel); |
| 1631 | EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace); |
| 1632 | |
| 1633 | UnloadPage(page); |
| 1634 | } |