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> |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 8 | |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 9 | #include "core/fpdfapi/font/cpdf_font.h" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 10 | #include "core/fpdfapi/page/cpdf_page.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 11 | #include "core/fpdfapi/parser/cpdf_array.h" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_number.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_stream.h" |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 15 | #include "core/fxcrt/fx_system.h" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 16 | #include "fpdfsdk/fsdk_define.h" |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 17 | #include "public/cpp/fpdf_deleters.h" |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 18 | #include "public/fpdf_annot.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 19 | #include "public/fpdf_edit.h" |
| 20 | #include "public/fpdfview.h" |
| 21 | #include "testing/embedder_test.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 22 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 23 | #include "testing/gtest/include/gtest/gtest.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 24 | #include "testing/test_support.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 25 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 26 | class FPDFEditEmbeddertest : public EmbedderTest { |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 27 | protected: |
| 28 | FPDF_DOCUMENT CreateNewDocument() { |
| 29 | document_ = FPDF_CreateNewDocument(); |
Tom Sepez | 41066c1 | 2017-05-18 09:28:49 -0700 | [diff] [blame] | 30 | cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 31 | return document_; |
| 32 | } |
| 33 | |
| 34 | void CheckFontDescriptor(CPDF_Dictionary* font_dict, |
| 35 | int font_type, |
| 36 | bool bold, |
| 37 | bool italic, |
| 38 | uint32_t size, |
| 39 | const uint8_t* data) { |
| 40 | CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor"); |
| 41 | ASSERT_TRUE(font_desc); |
| 42 | EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type")); |
| 43 | EXPECT_EQ(font_dict->GetStringFor("BaseFont"), |
| 44 | font_desc->GetStringFor("FontName")); |
| 45 | |
| 46 | // Check that the font descriptor has the required keys according to spec |
| 47 | // 1.7 Table 5.19 |
| 48 | ASSERT_TRUE(font_desc->KeyExist("Flags")); |
| 49 | int font_flags = font_desc->GetIntegerFor("Flags"); |
| 50 | if (bold) |
| 51 | EXPECT_TRUE(font_flags & FXFONT_BOLD); |
| 52 | else |
| 53 | EXPECT_FALSE(font_flags & FXFONT_BOLD); |
| 54 | if (italic) |
| 55 | EXPECT_TRUE(font_flags & FXFONT_ITALIC); |
| 56 | else |
| 57 | EXPECT_FALSE(font_flags & FXFONT_ITALIC); |
| 58 | EXPECT_TRUE(font_flags & FXFONT_NONSYMBOLIC); |
| 59 | ASSERT_TRUE(font_desc->KeyExist("FontBBox")); |
| 60 | EXPECT_EQ(4U, font_desc->GetArrayFor("FontBBox")->GetCount()); |
| 61 | EXPECT_TRUE(font_desc->KeyExist("ItalicAngle")); |
| 62 | EXPECT_TRUE(font_desc->KeyExist("Ascent")); |
| 63 | EXPECT_TRUE(font_desc->KeyExist("Descent")); |
| 64 | EXPECT_TRUE(font_desc->KeyExist("CapHeight")); |
| 65 | EXPECT_TRUE(font_desc->KeyExist("StemV")); |
| 66 | CFX_ByteString present("FontFile"); |
| 67 | CFX_ByteString absent("FontFile2"); |
| 68 | if (font_type == FPDF_FONT_TRUETYPE) |
| 69 | std::swap(present, absent); |
| 70 | EXPECT_TRUE(font_desc->KeyExist(present)); |
| 71 | EXPECT_FALSE(font_desc->KeyExist(absent)); |
| 72 | |
| 73 | // Check that the font stream is the one that was provided |
| 74 | CPDF_Stream* font_stream = font_desc->GetStreamFor(present); |
| 75 | ASSERT_EQ(size, font_stream->GetRawSize()); |
| 76 | uint8_t* stream_data = font_stream->GetRawData(); |
| 77 | for (size_t j = 0; j < size; j++) |
| 78 | EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j; |
| 79 | } |
| 80 | |
| 81 | void CheckCompositeFontWidths(CPDF_Array* widths_array, |
| 82 | CPDF_Font* typed_font) { |
| 83 | // Check that W array is in a format that conforms to PDF spec 1.7 section |
| 84 | // "Glyph Metrics in CIDFonts" (these checks are not |
| 85 | // implementation-specific). |
| 86 | EXPECT_GT(widths_array->GetCount(), 1U); |
| 87 | int num_cids_checked = 0; |
| 88 | int cur_cid = 0; |
| 89 | for (size_t idx = 0; idx < widths_array->GetCount(); idx++) { |
| 90 | int cid = widths_array->GetNumberAt(idx); |
| 91 | EXPECT_GE(cid, cur_cid); |
| 92 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
| 93 | CPDF_Object* next = widths_array->GetObjectAt(idx); |
| 94 | if (next->IsArray()) { |
| 95 | // We are in the c [w1 w2 ...] case |
| 96 | CPDF_Array* arr = next->AsArray(); |
| 97 | int cnt = static_cast<int>(arr->GetCount()); |
| 98 | size_t inner_idx = 0; |
| 99 | for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) { |
| 100 | int width = arr->GetNumberAt(inner_idx++); |
| 101 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid " |
| 102 | << cur_cid; |
| 103 | } |
| 104 | num_cids_checked += cnt; |
| 105 | continue; |
| 106 | } |
| 107 | // Otherwise, are in the c_first c_last w case. |
| 108 | ASSERT_TRUE(next->IsNumber()); |
| 109 | int last_cid = next->AsNumber()->GetInteger(); |
| 110 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
| 111 | int width = widths_array->GetNumberAt(idx); |
| 112 | for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) { |
| 113 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid " |
| 114 | << cur_cid; |
| 115 | } |
| 116 | num_cids_checked += last_cid - cid + 1; |
| 117 | } |
| 118 | // Make sure we have a good amount of cids described |
| 119 | EXPECT_GT(num_cids_checked, 900); |
| 120 | } |
| 121 | CPDF_Document* cpdf_doc() { return cpdf_doc_; } |
| 122 | |
| 123 | private: |
| 124 | CPDF_Document* cpdf_doc_; |
| 125 | }; |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 126 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 127 | namespace { |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 128 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 129 | const char kExpectedPDF[] = |
| 130 | "%PDF-1.7\r\n" |
| 131 | "%\xA1\xB3\xC5\xD7\r\n" |
| 132 | "1 0 obj\r\n" |
| 133 | "<</Pages 2 0 R /Type/Catalog>>\r\n" |
| 134 | "endobj\r\n" |
| 135 | "2 0 obj\r\n" |
| 136 | "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n" |
| 137 | "endobj\r\n" |
| 138 | "3 0 obj\r\n" |
| 139 | "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n" |
| 140 | "endobj\r\n" |
| 141 | "4 0 obj\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 142 | "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R " |
| 143 | "/Resources<</ExtGState<</FXE1 5 0 R >>>>" |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 144 | "/Rotate 0/Type/Page" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 145 | ">>\r\n" |
| 146 | "endobj\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 147 | "5 0 obj\r\n" |
| 148 | "<</BM/Normal/CA 1/ca 1>>\r\n" |
| 149 | "endobj\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 150 | "xref\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 151 | "0 6\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 152 | "0000000000 65535 f\r\n" |
| 153 | "0000000017 00000 n\r\n" |
| 154 | "0000000066 00000 n\r\n" |
| 155 | "0000000122 00000 n\r\n" |
| 156 | "0000000192 00000 n\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 157 | "0000000311 00000 n\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 158 | "trailer\r\n" |
| 159 | "<<\r\n" |
| 160 | "/Root 1 0 R\r\n" |
| 161 | "/Info 3 0 R\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 162 | "/Size 6/ID\\[<.*><.*>\\]>>\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 163 | "startxref\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 164 | "354\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 165 | "%%EOF\r\n"; |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 166 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 167 | } // namespace |
| 168 | |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 169 | TEST_F(FPDFEditEmbeddertest, EmptyCreation) { |
| 170 | EXPECT_TRUE(CreateEmptyDocument()); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 171 | FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 172 | EXPECT_NE(nullptr, page); |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 173 | // The FPDFPage_GenerateContent call should do nothing. |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 174 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 175 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 176 | |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 177 | EXPECT_THAT(GetString(), testing::MatchesRegex(std::string( |
| 178 | kExpectedPDF, sizeof(kExpectedPDF)))); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 179 | FPDF_ClosePage(page); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 180 | } |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 181 | |
| 182 | // Regression test for https://crbug.com/667012 |
| 183 | TEST_F(FPDFEditEmbeddertest, RasterizePDF) { |
| 184 | const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615"; |
| 185 | |
| 186 | // Get the bitmap for the original document/ |
| 187 | FPDF_BITMAP orig_bitmap; |
| 188 | { |
| 189 | EXPECT_TRUE(OpenDocument("black.pdf")); |
| 190 | FPDF_PAGE orig_page = LoadPage(0); |
| 191 | EXPECT_NE(nullptr, orig_page); |
| 192 | orig_bitmap = RenderPage(orig_page); |
| 193 | CompareBitmap(orig_bitmap, 612, 792, kAllBlackMd5sum); |
| 194 | UnloadPage(orig_page); |
| 195 | } |
| 196 | |
| 197 | // Create a new document from |orig_bitmap| and save it. |
| 198 | { |
| 199 | FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); |
| 200 | FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792); |
| 201 | |
| 202 | // Add the bitmap to an image object and add the image object to the output |
| 203 | // page. |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 204 | FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 205 | EXPECT_TRUE(FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap)); |
| 206 | EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0)); |
| 207 | FPDFPage_InsertObject(temp_page, temp_img); |
| 208 | EXPECT_TRUE(FPDFPage_GenerateContent(temp_page)); |
| 209 | EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0)); |
| 210 | FPDF_ClosePage(temp_page); |
| 211 | FPDF_CloseDocument(temp_doc); |
| 212 | } |
| 213 | FPDFBitmap_Destroy(orig_bitmap); |
| 214 | |
| 215 | // Get the generated content. Make sure it is at least as big as the original |
| 216 | // PDF. |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 217 | EXPECT_GT(GetString().size(), 923U); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 218 | TestAndCloseSaved(612, 792, kAllBlackMd5sum); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 219 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 220 | |
| 221 | TEST_F(FPDFEditEmbeddertest, AddPaths) { |
| 222 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 223 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 224 | |
| 225 | // We will first add a red rectangle |
| 226 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| 227 | ASSERT_NE(nullptr, red_rect); |
| 228 | // Expect false when trying to set colors out of range |
| 229 | EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300)); |
| 230 | EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0)); |
| 231 | |
| 232 | // Fill rectangle with red and insert to the page |
| 233 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 234 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 235 | FPDFPage_InsertObject(page, red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 236 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 237 | CompareBitmap(page_bitmap, 612, 792, "66d02eaa6181e2c069ce2ea99beda497"); |
| 238 | FPDFBitmap_Destroy(page_bitmap); |
| 239 | |
| 240 | // Now add to that a green rectangle with some medium alpha |
| 241 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40); |
| 242 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128)); |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 243 | |
Miklos Vajna | 1ef04c9 | 2017-05-08 18:14:19 +0200 | [diff] [blame] | 244 | // Make sure the type of the rectangle is a path. |
| 245 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect)); |
| 246 | |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 247 | // Make sure we get back the same color we set previously. |
| 248 | unsigned int R; |
| 249 | unsigned int G; |
| 250 | unsigned int B; |
| 251 | unsigned int A; |
| 252 | EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A)); |
| 253 | EXPECT_EQ(0U, R); |
| 254 | EXPECT_EQ(255U, G); |
| 255 | EXPECT_EQ(0U, B); |
| 256 | EXPECT_EQ(128U, A); |
| 257 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 258 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0)); |
| 259 | FPDFPage_InsertObject(page, green_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 260 | page_bitmap = RenderPage(page); |
| 261 | CompareBitmap(page_bitmap, 612, 792, "7b0b87604594e773add528fae567a558"); |
| 262 | FPDFBitmap_Destroy(page_bitmap); |
| 263 | |
| 264 | // Add a black triangle. |
| 265 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100); |
| 266 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200)); |
| 267 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 268 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200)); |
| 269 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100)); |
| 270 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
| 271 | FPDFPage_InsertObject(page, black_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 272 | page_bitmap = RenderPage(page); |
| 273 | CompareBitmap(page_bitmap, 612, 792, "eadc8020a14dfcf091da2688733d8806"); |
| 274 | FPDFBitmap_Destroy(page_bitmap); |
| 275 | |
| 276 | // Now add a more complex blue path. |
| 277 | FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200); |
| 278 | EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255)); |
| 279 | EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0)); |
| 280 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230)); |
| 281 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300)); |
| 282 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325)); |
| 283 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325)); |
| 284 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400)); |
| 285 | EXPECT_TRUE(FPDFPath_Close(blue_path)); |
| 286 | FPDFPage_InsertObject(page, blue_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 287 | page_bitmap = RenderPage(page); |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 288 | const char last_md5[] = "9823e1a21bd9b72b6a442ba4f12af946"; |
| 289 | CompareBitmap(page_bitmap, 612, 792, last_md5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 290 | FPDFBitmap_Destroy(page_bitmap); |
| 291 | |
| 292 | // Now save the result, closing the page and document |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 293 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 294 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 295 | FPDF_ClosePage(page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 296 | |
| 297 | // Render the saved result |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 298 | TestAndCloseSaved(612, 792, last_md5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) { |
| 302 | // Load document with some text |
| 303 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 304 | FPDF_PAGE page = LoadPage(0); |
| 305 | EXPECT_NE(nullptr, page); |
| 306 | |
| 307 | // Add an opaque rectangle on top of some of the text. |
| 308 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| 309 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 310 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 311 | FPDFPage_InsertObject(page, red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 312 | |
| 313 | // Add a transparent triangle on top of other part of the text. |
| 314 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50); |
| 315 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100)); |
| 316 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 317 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80)); |
| 318 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10)); |
| 319 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
| 320 | FPDFPage_InsertObject(page, black_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 321 | |
| 322 | // Render and check the result. Text is slightly different on Mac. |
| 323 | FPDF_BITMAP bitmap = RenderPage(page); |
| 324 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 325 | const char md5[] = "f9e6fa74230f234286bfcada9f7606d8"; |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 326 | #else |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 327 | const char md5[] = "2fdfc5dda29374cfba4349362e38ebdb"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 328 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 329 | CompareBitmap(bitmap, 200, 200, md5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 330 | FPDFBitmap_Destroy(bitmap); |
| 331 | UnloadPage(page); |
| 332 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 333 | |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 334 | TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) { |
| 335 | // Load document with existing content |
| 336 | EXPECT_TRUE(OpenDocument("bug_717.pdf")); |
| 337 | FPDF_PAGE page = LoadPage(0); |
| 338 | EXPECT_NE(nullptr, page); |
| 339 | |
| 340 | // Add a transparent rectangle on top of the existing content |
| 341 | FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50); |
| 342 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100)); |
| 343 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
| 344 | FPDFPage_InsertObject(page, red_rect2); |
| 345 | |
| 346 | // Add an opaque rectangle on top of the existing content |
| 347 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50); |
| 348 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 349 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 350 | FPDFPage_InsertObject(page, red_rect); |
| 351 | |
| 352 | FPDF_BITMAP bitmap = RenderPage(page); |
| 353 | CompareBitmap(bitmap, 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); |
| 354 | FPDFBitmap_Destroy(bitmap); |
| 355 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 356 | |
| 357 | // Now save the result, closing the page and document |
| 358 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 359 | UnloadPage(page); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 360 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 361 | // Render the saved result without closing the page and document |
| 362 | TestSaved(612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 363 | |
| 364 | ClearString(); |
| 365 | // Add another opaque rectangle on top of the existing content |
| 366 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50); |
| 367 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255)); |
| 368 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 369 | FPDFPage_InsertObject(m_SavedPage, green_rect); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 370 | |
| 371 | // Add another transparent rectangle on top of existing content |
| 372 | FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50); |
| 373 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100)); |
| 374 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 375 | FPDFPage_InsertObject(m_SavedPage, green_rect2); |
| 376 | FPDF_BITMAP new_bitmap = RenderPageWithFlags(m_SavedPage, m_SavedForm, 0); |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 377 | const char last_md5[] = "4b5b00f824620f8c9b8801ebb98e1cdd"; |
| 378 | CompareBitmap(new_bitmap, 612, 792, last_md5); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 379 | FPDFBitmap_Destroy(new_bitmap); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 380 | EXPECT_TRUE(FPDFPage_GenerateContent(m_SavedPage)); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 381 | |
| 382 | // Now save the result, closing the page and document |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 383 | EXPECT_TRUE(FPDF_SaveAsCopy(m_SavedDocument, this, 0)); |
| 384 | CloseSaved(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 385 | |
| 386 | // Render the saved result |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 387 | TestAndCloseSaved(612, 792, last_md5); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 388 | } |
| 389 | |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 390 | TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) { |
| 391 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 392 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 393 | |
| 394 | // Add a large stroked rectangle (fill color should not affect it). |
| 395 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400); |
| 396 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255)); |
| 397 | EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255)); |
| 398 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f)); |
| 399 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1)); |
| 400 | FPDFPage_InsertObject(page, rect); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 401 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 402 | CompareBitmap(page_bitmap, 612, 792, "64bd31f862a89e0a9e505a5af6efd506"); |
| 403 | FPDFBitmap_Destroy(page_bitmap); |
| 404 | |
| 405 | // Add crossed-checkmark |
| 406 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500); |
| 407 | EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400)); |
| 408 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600)); |
| 409 | EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600)); |
| 410 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400)); |
| 411 | EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180)); |
| 412 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f)); |
| 413 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 414 | FPDFPage_InsertObject(page, check); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 415 | page_bitmap = RenderPage(page); |
| 416 | CompareBitmap(page_bitmap, 612, 792, "4b6f3b9d25c4e194821217d5016c3724"); |
| 417 | FPDFBitmap_Destroy(page_bitmap); |
| 418 | |
| 419 | // Add stroked and filled oval-ish path. |
| 420 | FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100); |
| 421 | EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300)); |
| 422 | EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305)); |
| 423 | EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105)); |
| 424 | EXPECT_TRUE(FPDFPath_Close(path)); |
| 425 | EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100)); |
| 426 | EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150)); |
| 427 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f)); |
| 428 | EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1)); |
| 429 | FPDFPage_InsertObject(page, path); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 430 | page_bitmap = RenderPage(page); |
| 431 | CompareBitmap(page_bitmap, 612, 792, "ff3e6a22326754944cc6e56609acd73b"); |
| 432 | FPDFBitmap_Destroy(page_bitmap); |
| 433 | FPDF_ClosePage(page); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 434 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 435 | |
| 436 | TEST_F(FPDFEditEmbeddertest, AddStandardFontText) { |
| 437 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 438 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 439 | |
| 440 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 441 | FPDF_PAGEOBJECT text_object1 = |
| 442 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 443 | EXPECT_TRUE(text_object1); |
| 444 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 = |
| 445 | GetFPDFWideString(L"I'm at the bottom of the page"); |
| 446 | EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get())); |
| 447 | FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20); |
| 448 | FPDFPage_InsertObject(page, text_object1); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 449 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 450 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 451 | const char md5[] = "a4dddc1a3930fa694bbff9789dab4161"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 452 | #else |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 453 | const char md5[] = "6e8a9b0682f60fd3ff1bf087b093d30d"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 454 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 455 | CompareBitmap(page_bitmap, 612, 792, md5); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 456 | FPDFBitmap_Destroy(page_bitmap); |
| 457 | |
| 458 | // Try another font |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 459 | FPDF_PAGEOBJECT text_object2 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 460 | FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 461 | EXPECT_TRUE(text_object2); |
| 462 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 463 | GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold."); |
| 464 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 465 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600); |
| 466 | FPDFPage_InsertObject(page, text_object2); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 467 | page_bitmap = RenderPage(page); |
| 468 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 469 | const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c"; |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 470 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 471 | const char md5_2[] = "56863642d4d8b418cfd810fdb5a5d92f"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 472 | #else |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 473 | const char md5_2[] = "0c83875429688bda45a55a692d5aa781"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 474 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 475 | CompareBitmap(page_bitmap, 612, 792, md5_2); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 476 | FPDFBitmap_Destroy(page_bitmap); |
| 477 | |
| 478 | // And some randomly transformed text |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 479 | FPDF_PAGEOBJECT text_object3 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 480 | FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 481 | EXPECT_TRUE(text_object3); |
| 482 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 = |
| 483 | GetFPDFWideString(L"Can you read me? <:)>"); |
| 484 | EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get())); |
| 485 | FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200); |
| 486 | FPDFPage_InsertObject(page, text_object3); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 487 | page_bitmap = RenderPage(page); |
| 488 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 489 | const char md5_3[] = "40b3ef04f915ff4c4208948001763544"; |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 490 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
Lei Zhang | b8e89e3 | 2017-05-02 11:53:08 -0700 | [diff] [blame] | 491 | const char md5_3[] = "d69d419def35a098d9c10f8317d6709b"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 492 | #else |
Lei Zhang | b8e89e3 | 2017-05-02 11:53:08 -0700 | [diff] [blame] | 493 | const char md5_3[] = "8b300d3c6dfc12fa9af97e12ed5bd80a"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 494 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 495 | CompareBitmap(page_bitmap, 612, 792, md5_3); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 496 | FPDFBitmap_Destroy(page_bitmap); |
| 497 | |
| 498 | // TODO(npm): Why are there issues with text rotated by 90 degrees? |
| 499 | // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this. |
| 500 | FPDF_ClosePage(page); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 501 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 502 | |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 503 | TEST_F(FPDFEditEmbeddertest, GraphicsData) { |
| 504 | // New page |
| 505 | std::unique_ptr<void, FPDFPageDeleter> page( |
| 506 | FPDFPage_New(CreateNewDocument(), 0, 612, 792)); |
| 507 | |
| 508 | // Create a rect with nontrivial graphics |
| 509 | FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 510 | FPDFPageObj_SetBlendMode(rect1, "Color"); |
| 511 | FPDFPage_InsertObject(page.get(), rect1); |
| 512 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| 513 | |
| 514 | // Check that the ExtGState was created |
| 515 | CPDF_Page* the_page = CPDFPageFromFPDFPage(page.get()); |
| 516 | CPDF_Dictionary* graphics_dict = |
| 517 | the_page->m_pResources->GetDictFor("ExtGState"); |
| 518 | ASSERT_TRUE(graphics_dict); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 519 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 520 | |
| 521 | // Add a text object causing no change to the graphics dictionary |
| 522 | FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 523 | // Only alpha, the last component, matters for the graphics dictionary. And |
| 524 | // the default value is 255. |
| 525 | EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255)); |
| 526 | FPDFPage_InsertObject(page.get(), text1); |
| 527 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 528 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 529 | |
| 530 | // Add a text object increasing the size of the graphics dictionary |
| 531 | FPDF_PAGEOBJECT text2 = |
| 532 | FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f); |
| 533 | FPDFPage_InsertObject(page.get(), text2); |
| 534 | FPDFPageObj_SetBlendMode(text2, "Darken"); |
| 535 | EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150)); |
| 536 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 537 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 538 | |
| 539 | // Add a path that should reuse graphics |
Nicolas Pena | ce67be4 | 2017-06-14 14:52:49 -0400 | [diff] [blame] | 540 | FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 541 | FPDFPageObj_SetBlendMode(path, "Darken"); |
| 542 | EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150)); |
| 543 | FPDFPage_InsertObject(page.get(), path); |
| 544 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 545 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 546 | |
| 547 | // Add a rect increasing the size of the graphics dictionary |
| 548 | FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 549 | FPDFPageObj_SetBlendMode(rect2, "Darken"); |
| 550 | EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150)); |
| 551 | EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200)); |
| 552 | FPDFPage_InsertObject(page.get(), rect2); |
| 553 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 554 | EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 555 | } |
| 556 | |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 557 | TEST_F(FPDFEditEmbeddertest, DoubleGenerating) { |
| 558 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 559 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 560 | |
| 561 | // Add a red rectangle with some non-default alpha |
| 562 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 563 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128)); |
| 564 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0)); |
| 565 | FPDFPage_InsertObject(page, rect); |
| 566 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 567 | |
| 568 | // Check the ExtGState |
| 569 | CPDF_Page* the_page = CPDFPageFromFPDFPage(page); |
| 570 | CPDF_Dictionary* graphics_dict = |
| 571 | the_page->m_pResources->GetDictFor("ExtGState"); |
| 572 | ASSERT_TRUE(graphics_dict); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 573 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 574 | |
| 575 | // Check the bitmap |
| 576 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 577 | CompareBitmap(page_bitmap, 612, 792, "5384da3406d62360ffb5cac4476fff1c"); |
| 578 | FPDFBitmap_Destroy(page_bitmap); |
| 579 | |
| 580 | // Never mind, my new favorite color is blue, increase alpha |
| 581 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180)); |
| 582 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 583 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 584 | |
| 585 | // Check that bitmap displays changed content |
| 586 | page_bitmap = RenderPage(page); |
| 587 | CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c"); |
| 588 | FPDFBitmap_Destroy(page_bitmap); |
| 589 | |
| 590 | // And now generate, without changes |
| 591 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 592 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 593 | page_bitmap = RenderPage(page); |
| 594 | CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c"); |
| 595 | FPDFBitmap_Destroy(page_bitmap); |
| 596 | |
| 597 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 598 | FPDF_PAGEOBJECT text_object = |
| 599 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 600 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 601 | GetFPDFWideString(L"Something something #text# something"); |
| 602 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 603 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300); |
| 604 | FPDFPage_InsertObject(page, text_object); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 605 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 606 | CPDF_Dictionary* font_dict = the_page->m_pResources->GetDictFor("Font"); |
| 607 | ASSERT_TRUE(font_dict); |
| 608 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 609 | |
| 610 | // Generate yet again, check dicts are reasonably sized |
| 611 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 612 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 613 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 614 | FPDF_ClosePage(page); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 615 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 616 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 617 | TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) { |
| 618 | CreateNewDocument(); |
| 619 | // TODO(npm): use other fonts after disallowing loading any font as any type |
| 620 | const CPDF_Font* stock_font = |
| 621 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 622 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 623 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 624 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 625 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false)); |
| 626 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 627 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 628 | EXPECT_TRUE(typed_font->IsType1Font()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 629 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 630 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 631 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 632 | EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype")); |
| 633 | EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont")); |
| 634 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 635 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 636 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 637 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
| 638 | |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 639 | CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 640 | ASSERT_TRUE(widths_array); |
| 641 | ASSERT_EQ(224U, widths_array->GetCount()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 642 | EXPECT_EQ(250, widths_array->GetNumberAt(0)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 643 | EXPECT_EQ(569, widths_array->GetNumberAt(11)); |
| 644 | EXPECT_EQ(500, widths_array->GetNumberAt(223)); |
| 645 | CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data); |
| 646 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 647 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 648 | TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) { |
| 649 | CreateNewDocument(); |
| 650 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 651 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 652 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 653 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 654 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false)); |
| 655 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 656 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 657 | EXPECT_TRUE(typed_font->IsTrueTypeFont()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 658 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 659 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
| 660 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 661 | EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype")); |
| 662 | EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont")); |
| 663 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 664 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 665 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
| 666 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 667 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 668 | CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
| 669 | ASSERT_TRUE(widths_array); |
| 670 | ASSERT_EQ(224U, widths_array->GetCount()); |
| 671 | EXPECT_EQ(600, widths_array->GetNumberAt(33)); |
| 672 | EXPECT_EQ(600, widths_array->GetNumberAt(74)); |
| 673 | EXPECT_EQ(600, widths_array->GetNumberAt(223)); |
| 674 | CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data); |
| 675 | } |
| 676 | |
| 677 | TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) { |
| 678 | CreateNewDocument(); |
| 679 | const CPDF_Font* stock_font = |
| 680 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 681 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 682 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 683 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 684 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1)); |
| 685 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 686 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 687 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 688 | |
| 689 | // Check font dictionary entries |
| 690 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
| 691 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 692 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 693 | EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont")); |
| 694 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
| 695 | CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts"); |
| 696 | ASSERT_TRUE(descendant_array); |
| 697 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 698 | |
| 699 | // Check the CIDFontDict |
| 700 | CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
| 701 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 702 | EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype")); |
| 703 | EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont")); |
| 704 | CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo"); |
| 705 | ASSERT_TRUE(cidinfo_dict); |
| 706 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 707 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 708 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 709 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data); |
| 710 | |
| 711 | // Check widths |
| 712 | CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
| 713 | ASSERT_TRUE(widths_array); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 714 | EXPECT_GT(widths_array->GetCount(), 1U); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 715 | CheckCompositeFontWidths(widths_array, typed_font); |
| 716 | } |
| 717 | |
| 718 | TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) { |
| 719 | CreateNewDocument(); |
| 720 | const CPDF_Font* stock_font = |
| 721 | CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 722 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 723 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 724 | |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 725 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 726 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1)); |
| 727 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 728 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 729 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 730 | |
| 731 | // Check font dictionary entries |
| 732 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
| 733 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 734 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 735 | EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont")); |
| 736 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
| 737 | CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts"); |
| 738 | ASSERT_TRUE(descendant_array); |
| 739 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 740 | |
| 741 | // Check the CIDFontDict |
| 742 | CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
| 743 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 744 | EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype")); |
| 745 | EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont")); |
| 746 | CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo"); |
| 747 | ASSERT_TRUE(cidinfo_dict); |
| 748 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 749 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 750 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 751 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size, |
| 752 | data); |
| 753 | |
| 754 | // Check widths |
| 755 | CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
| 756 | ASSERT_TRUE(widths_array); |
| 757 | CheckCompositeFontWidths(widths_array, typed_font); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 758 | } |
rbpotter | ce8e51e | 2017-04-28 12:42:47 -0700 | [diff] [blame] | 759 | |
| 760 | TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) { |
| 761 | // Load document with a -90 degree rotation |
| 762 | EXPECT_TRUE(OpenDocument("bug_713197.pdf")); |
| 763 | FPDF_PAGE page = LoadPage(0); |
| 764 | EXPECT_NE(nullptr, page); |
| 765 | |
| 766 | EXPECT_EQ(3, FPDFPage_GetRotation(page)); |
| 767 | UnloadPage(page); |
| 768 | } |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 769 | |
| 770 | TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) { |
| 771 | // Start with a blank page |
| 772 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 773 | { |
| 774 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 775 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 776 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 777 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 778 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0)); |
| 779 | ASSERT_TRUE(font.get()); |
| 780 | |
| 781 | // Add some text to the page |
| 782 | FPDF_PAGEOBJECT text_object = |
| 783 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 784 | EXPECT_TRUE(text_object); |
| 785 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 786 | GetFPDFWideString(L"I am testing my loaded font, WEE."); |
| 787 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 788 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400); |
| 789 | FPDFPage_InsertObject(page, text_object); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 790 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 791 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 792 | const char md5[] = "17d2b6cd574cf66170b09c8927529a94"; |
| 793 | #else |
| 794 | const char md5[] = "28e5b10743660dcdfd1618db47b39d32"; |
| 795 | #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 796 | CompareBitmap(page_bitmap, 612, 792, md5); |
| 797 | FPDFBitmap_Destroy(page_bitmap); |
| 798 | |
| 799 | // Add some more text, same font |
| 800 | FPDF_PAGEOBJECT text_object2 = |
| 801 | FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f); |
| 802 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 803 | GetFPDFWideString(L"Bigger font size"); |
| 804 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 805 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200); |
| 806 | FPDFPage_InsertObject(page, text_object2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 807 | } |
| 808 | FPDF_BITMAP page_bitmap2 = RenderPage(page); |
| 809 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 810 | const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea"; |
| 811 | #else |
| 812 | const char md5_2[] = "a068eef4110d607f77c87ea8340fa2a5"; |
| 813 | #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 814 | CompareBitmap(page_bitmap2, 612, 792, md5_2); |
| 815 | FPDFBitmap_Destroy(page_bitmap2); |
| 816 | |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 817 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 818 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 819 | FPDF_ClosePage(page); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 820 | TestAndCloseSaved(612, 792, md5_2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 821 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 822 | |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 823 | TEST_F(FPDFEditEmbeddertest, TransformAnnot) { |
| 824 | // Open a file with one annotation and load its first page. |
| 825 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 826 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 827 | ASSERT_TRUE(page); |
| 828 | |
| 829 | // Add an underline annotation to the page without specifying its rectangle. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 830 | FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE); |
| 831 | ASSERT_TRUE(annot); |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 832 | |
| 833 | // FPDFPage_TransformAnnots() should run without errors when modifying |
| 834 | // annotation rectangles. |
| 835 | FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6); |
| 836 | |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 837 | FPDFPage_CloseAnnot(annot); |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 838 | UnloadPage(page); |
| 839 | } |
| 840 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 841 | // TODO(npm): Add tests using Japanese fonts in other OS. |
| 842 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ |
| 843 | TEST_F(FPDFEditEmbeddertest, AddCIDFontText) { |
| 844 | // Start with a blank page |
| 845 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 846 | CFX_Font CIDfont; |
| 847 | { |
| 848 | // First, get the data from the font |
| 849 | CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0); |
| 850 | EXPECT_EQ("IPAGothic", CIDfont.GetFaceName()); |
| 851 | const uint8_t* data = CIDfont.GetFontData(); |
| 852 | const uint32_t size = CIDfont.GetSize(); |
| 853 | |
| 854 | // Load the data into a FPDF_Font. |
| 855 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 856 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1)); |
| 857 | ASSERT_TRUE(font.get()); |
| 858 | |
| 859 | // Add some text to the page |
| 860 | FPDF_PAGEOBJECT text_object = |
| 861 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 862 | ASSERT_TRUE(text_object); |
| 863 | std::wstring wstr = L"ABCDEFGhijklmnop."; |
| 864 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 865 | GetFPDFWideString(wstr); |
| 866 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 867 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200); |
| 868 | FPDFPage_InsertObject(page, text_object); |
| 869 | |
| 870 | // And add some Japanese characters |
| 871 | FPDF_PAGEOBJECT text_object2 = |
| 872 | FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f); |
| 873 | ASSERT_TRUE(text_object2); |
| 874 | std::wstring wstr2 = |
| 875 | L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1" |
| 876 | L"\u756A"; |
| 877 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 878 | GetFPDFWideString(wstr2); |
| 879 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 880 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500); |
| 881 | FPDFPage_InsertObject(page, text_object2); |
| 882 | } |
| 883 | |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 884 | // Check that the text renders properly. |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 885 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 886 | const char md5[] = "2bc6c1aaa2252e73246a75775ccf38c2"; |
| 887 | CompareBitmap(page_bitmap, 612, 792, md5); |
| 888 | FPDFBitmap_Destroy(page_bitmap); |
| 889 | |
| 890 | // Save the document, close the page. |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 891 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 892 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 893 | FPDF_ClosePage(page); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 894 | TestAndCloseSaved(612, 792, md5); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 895 | } |
| 896 | #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 897 | |
| 898 | TEST_F(FPDFEditEmbeddertest, SaveAndRender) { |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 899 | const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b"; |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 900 | { |
| 901 | EXPECT_TRUE(OpenDocument("bug_779.pdf")); |
| 902 | FPDF_PAGE page = LoadPage(0); |
| 903 | ASSERT_NE(nullptr, page); |
| 904 | |
| 905 | // Now add a more complex blue path. |
| 906 | FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20); |
| 907 | EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200)); |
| 908 | // TODO(npm): stroking will cause the MD5s to differ. |
| 909 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0)); |
| 910 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63)); |
| 911 | EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90)); |
| 912 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133)); |
| 913 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33)); |
| 914 | EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40)); |
| 915 | EXPECT_TRUE(FPDFPath_Close(green_path)); |
| 916 | FPDFPage_InsertObject(page, green_path); |
| 917 | FPDF_BITMAP page_bitmap = RenderPage(page); |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 918 | CompareBitmap(page_bitmap, 612, 792, md5); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 919 | FPDFBitmap_Destroy(page_bitmap); |
| 920 | |
| 921 | // Now save the result, closing the page and document |
| 922 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 923 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 924 | UnloadPage(page); |
| 925 | } |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 926 | TestAndCloseSaved(612, 792, md5); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 927 | } |