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