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