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" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 17 | #include "fpdfsdk/fsdk_define.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")); |
| 50 | int font_flags = font_desc->GetIntegerFor("Flags"); |
| 51 | if (bold) |
| 52 | EXPECT_TRUE(font_flags & FXFONT_BOLD); |
| 53 | else |
| 54 | EXPECT_FALSE(font_flags & FXFONT_BOLD); |
| 55 | if (italic) |
| 56 | EXPECT_TRUE(font_flags & FXFONT_ITALIC); |
| 57 | else |
| 58 | EXPECT_FALSE(font_flags & FXFONT_ITALIC); |
| 59 | EXPECT_TRUE(font_flags & FXFONT_NONSYMBOLIC); |
| 60 | ASSERT_TRUE(font_desc->KeyExist("FontBBox")); |
| 61 | EXPECT_EQ(4U, font_desc->GetArrayFor("FontBBox")->GetCount()); |
| 62 | EXPECT_TRUE(font_desc->KeyExist("ItalicAngle")); |
| 63 | EXPECT_TRUE(font_desc->KeyExist("Ascent")); |
| 64 | EXPECT_TRUE(font_desc->KeyExist("Descent")); |
| 65 | EXPECT_TRUE(font_desc->KeyExist("CapHeight")); |
| 66 | EXPECT_TRUE(font_desc->KeyExist("StemV")); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 67 | ByteString present("FontFile"); |
| 68 | ByteString absent("FontFile2"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 69 | if (font_type == FPDF_FONT_TRUETYPE) |
| 70 | std::swap(present, absent); |
| 71 | EXPECT_TRUE(font_desc->KeyExist(present)); |
| 72 | EXPECT_FALSE(font_desc->KeyExist(absent)); |
| 73 | |
| 74 | // Check that the font stream is the one that was provided |
| 75 | CPDF_Stream* font_stream = font_desc->GetStreamFor(present); |
| 76 | ASSERT_EQ(size, font_stream->GetRawSize()); |
| 77 | uint8_t* stream_data = font_stream->GetRawData(); |
| 78 | for (size_t j = 0; j < size; j++) |
| 79 | EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j; |
| 80 | } |
| 81 | |
| 82 | void CheckCompositeFontWidths(CPDF_Array* widths_array, |
| 83 | CPDF_Font* typed_font) { |
| 84 | // Check that W array is in a format that conforms to PDF spec 1.7 section |
| 85 | // "Glyph Metrics in CIDFonts" (these checks are not |
| 86 | // implementation-specific). |
| 87 | EXPECT_GT(widths_array->GetCount(), 1U); |
| 88 | int num_cids_checked = 0; |
| 89 | int cur_cid = 0; |
| 90 | for (size_t idx = 0; idx < widths_array->GetCount(); idx++) { |
| 91 | int cid = widths_array->GetNumberAt(idx); |
| 92 | EXPECT_GE(cid, cur_cid); |
| 93 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
| 94 | CPDF_Object* next = widths_array->GetObjectAt(idx); |
| 95 | if (next->IsArray()) { |
| 96 | // We are in the c [w1 w2 ...] case |
| 97 | CPDF_Array* arr = next->AsArray(); |
| 98 | int cnt = static_cast<int>(arr->GetCount()); |
| 99 | size_t inner_idx = 0; |
| 100 | for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) { |
| 101 | int width = arr->GetNumberAt(inner_idx++); |
| 102 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid " |
| 103 | << cur_cid; |
| 104 | } |
| 105 | num_cids_checked += cnt; |
| 106 | continue; |
| 107 | } |
| 108 | // Otherwise, are in the c_first c_last w case. |
| 109 | ASSERT_TRUE(next->IsNumber()); |
| 110 | int last_cid = next->AsNumber()->GetInteger(); |
| 111 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
| 112 | int width = widths_array->GetNumberAt(idx); |
| 113 | for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) { |
| 114 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid " |
| 115 | << cur_cid; |
| 116 | } |
| 117 | num_cids_checked += last_cid - cid + 1; |
| 118 | } |
| 119 | // Make sure we have a good amount of cids described |
| 120 | EXPECT_GT(num_cids_checked, 900); |
| 121 | } |
| 122 | CPDF_Document* cpdf_doc() { return cpdf_doc_; } |
| 123 | |
| 124 | private: |
| 125 | CPDF_Document* cpdf_doc_; |
| 126 | }; |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 127 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 128 | namespace { |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 129 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 130 | const char kExpectedPDF[] = |
| 131 | "%PDF-1.7\r\n" |
| 132 | "%\xA1\xB3\xC5\xD7\r\n" |
| 133 | "1 0 obj\r\n" |
| 134 | "<</Pages 2 0 R /Type/Catalog>>\r\n" |
| 135 | "endobj\r\n" |
| 136 | "2 0 obj\r\n" |
| 137 | "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n" |
| 138 | "endobj\r\n" |
| 139 | "3 0 obj\r\n" |
| 140 | "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n" |
| 141 | "endobj\r\n" |
| 142 | "4 0 obj\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 143 | "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R " |
| 144 | "/Resources<</ExtGState<</FXE1 5 0 R >>>>" |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 145 | "/Rotate 0/Type/Page" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 146 | ">>\r\n" |
| 147 | "endobj\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 148 | "5 0 obj\r\n" |
| 149 | "<</BM/Normal/CA 1/ca 1>>\r\n" |
| 150 | "endobj\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 151 | "xref\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 152 | "0 6\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 153 | "0000000000 65535 f\r\n" |
| 154 | "0000000017 00000 n\r\n" |
| 155 | "0000000066 00000 n\r\n" |
| 156 | "0000000122 00000 n\r\n" |
| 157 | "0000000192 00000 n\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 158 | "0000000311 00000 n\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 159 | "trailer\r\n" |
| 160 | "<<\r\n" |
| 161 | "/Root 1 0 R\r\n" |
| 162 | "/Info 3 0 R\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 163 | "/Size 6/ID\\[<.*><.*>\\]>>\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 164 | "startxref\r\n" |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 165 | "354\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 166 | "%%EOF\r\n"; |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 167 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 168 | } // namespace |
| 169 | |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 170 | TEST_F(FPDFEditEmbeddertest, EmptyCreation) { |
| 171 | EXPECT_TRUE(CreateEmptyDocument()); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 172 | FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 173 | EXPECT_NE(nullptr, page); |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 174 | // The FPDFPage_GenerateContent call should do nothing. |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 175 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 176 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 177 | |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 178 | EXPECT_THAT(GetString(), testing::MatchesRegex(std::string( |
| 179 | kExpectedPDF, sizeof(kExpectedPDF)))); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 180 | FPDF_ClosePage(page); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 181 | } |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 182 | |
| 183 | // Regression test for https://crbug.com/667012 |
| 184 | TEST_F(FPDFEditEmbeddertest, RasterizePDF) { |
| 185 | const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615"; |
| 186 | |
| 187 | // Get the bitmap for the original document/ |
| 188 | FPDF_BITMAP orig_bitmap; |
| 189 | { |
| 190 | EXPECT_TRUE(OpenDocument("black.pdf")); |
| 191 | FPDF_PAGE orig_page = LoadPage(0); |
| 192 | EXPECT_NE(nullptr, orig_page); |
| 193 | orig_bitmap = RenderPage(orig_page); |
| 194 | CompareBitmap(orig_bitmap, 612, 792, kAllBlackMd5sum); |
| 195 | UnloadPage(orig_page); |
| 196 | } |
| 197 | |
| 198 | // Create a new document from |orig_bitmap| and save it. |
| 199 | { |
| 200 | FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); |
| 201 | FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792); |
| 202 | |
| 203 | // Add the bitmap to an image object and add the image object to the output |
| 204 | // page. |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 205 | FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 206 | EXPECT_TRUE(FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap)); |
| 207 | EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0)); |
| 208 | FPDFPage_InsertObject(temp_page, temp_img); |
| 209 | EXPECT_TRUE(FPDFPage_GenerateContent(temp_page)); |
| 210 | EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0)); |
| 211 | FPDF_ClosePage(temp_page); |
| 212 | FPDF_CloseDocument(temp_doc); |
| 213 | } |
| 214 | FPDFBitmap_Destroy(orig_bitmap); |
| 215 | |
| 216 | // Get the generated content. Make sure it is at least as big as the original |
| 217 | // PDF. |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 218 | EXPECT_GT(GetString().size(), 923U); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 219 | TestAndCloseSaved(612, 792, kAllBlackMd5sum); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 220 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 221 | |
| 222 | TEST_F(FPDFEditEmbeddertest, AddPaths) { |
| 223 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 224 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 225 | |
| 226 | // We will first add a red rectangle |
| 227 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| 228 | ASSERT_NE(nullptr, red_rect); |
| 229 | // Expect false when trying to set colors out of range |
| 230 | EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300)); |
| 231 | EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0)); |
| 232 | |
| 233 | // Fill rectangle with red and insert to the page |
| 234 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 235 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 236 | FPDFPage_InsertObject(page, red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 237 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 238 | CompareBitmap(page_bitmap, 612, 792, "66d02eaa6181e2c069ce2ea99beda497"); |
| 239 | FPDFBitmap_Destroy(page_bitmap); |
| 240 | |
| 241 | // Now add to that a green rectangle with some medium alpha |
| 242 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40); |
| 243 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128)); |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 244 | |
Miklos Vajna | 1ef04c9 | 2017-05-08 18:14:19 +0200 | [diff] [blame] | 245 | // Make sure the type of the rectangle is a path. |
| 246 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect)); |
| 247 | |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 248 | // Make sure we get back the same color we set previously. |
| 249 | unsigned int R; |
| 250 | unsigned int G; |
| 251 | unsigned int B; |
| 252 | unsigned int A; |
| 253 | EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A)); |
| 254 | EXPECT_EQ(0U, R); |
| 255 | EXPECT_EQ(255U, G); |
| 256 | EXPECT_EQ(0U, B); |
| 257 | EXPECT_EQ(128U, A); |
| 258 | |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 259 | // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4 |
| 260 | // FXPT_TYPE::LineTo). |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 261 | ASSERT_EQ(5, FPDFPath_CountSegments(green_rect)); |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 262 | // Verify actual coordinates. |
| 263 | FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0); |
| 264 | float x; |
| 265 | float y; |
| 266 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 267 | EXPECT_EQ(100, x); |
| 268 | EXPECT_EQ(100, y); |
| 269 | EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| 270 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 271 | segment = FPDFPath_GetPathSegment(green_rect, 1); |
| 272 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 273 | EXPECT_EQ(100, x); |
| 274 | EXPECT_EQ(140, y); |
| 275 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 276 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 277 | segment = FPDFPath_GetPathSegment(green_rect, 2); |
| 278 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 279 | EXPECT_EQ(140, x); |
| 280 | EXPECT_EQ(140, y); |
| 281 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 282 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 283 | segment = FPDFPath_GetPathSegment(green_rect, 3); |
| 284 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 285 | EXPECT_EQ(140, x); |
| 286 | EXPECT_EQ(100, y); |
| 287 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 288 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 289 | segment = FPDFPath_GetPathSegment(green_rect, 4); |
| 290 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 291 | EXPECT_EQ(100, x); |
| 292 | EXPECT_EQ(100, y); |
| 293 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 294 | EXPECT_TRUE(FPDFPathSegment_GetClose(segment)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 295 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 296 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0)); |
| 297 | FPDFPage_InsertObject(page, green_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 298 | page_bitmap = RenderPage(page); |
| 299 | CompareBitmap(page_bitmap, 612, 792, "7b0b87604594e773add528fae567a558"); |
| 300 | FPDFBitmap_Destroy(page_bitmap); |
| 301 | |
| 302 | // Add a black triangle. |
| 303 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100); |
| 304 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200)); |
| 305 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 306 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200)); |
| 307 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100)); |
| 308 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 309 | |
| 310 | // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2 |
| 311 | // FXPT_TYPE::LineTo). |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 312 | ASSERT_EQ(3, FPDFPath_CountSegments(black_path)); |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 313 | // Verify actual coordinates. |
| 314 | segment = FPDFPath_GetPathSegment(black_path, 0); |
| 315 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 316 | EXPECT_EQ(400, x); |
| 317 | EXPECT_EQ(100, y); |
| 318 | EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment)); |
| 319 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 320 | segment = FPDFPath_GetPathSegment(black_path, 1); |
| 321 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 322 | EXPECT_EQ(400, x); |
| 323 | EXPECT_EQ(200, y); |
| 324 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 325 | EXPECT_FALSE(FPDFPathSegment_GetClose(segment)); |
| 326 | segment = FPDFPath_GetPathSegment(black_path, 2); |
| 327 | EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y)); |
| 328 | EXPECT_EQ(300, x); |
| 329 | EXPECT_EQ(100, y); |
| 330 | EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment)); |
| 331 | EXPECT_TRUE(FPDFPathSegment_GetClose(segment)); |
| 332 | // Make sure out of bounds index access fails properly. |
| 333 | EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 334 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 335 | FPDFPage_InsertObject(page, black_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 336 | page_bitmap = RenderPage(page); |
| 337 | CompareBitmap(page_bitmap, 612, 792, "eadc8020a14dfcf091da2688733d8806"); |
| 338 | FPDFBitmap_Destroy(page_bitmap); |
| 339 | |
| 340 | // Now add a more complex blue path. |
| 341 | FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200); |
| 342 | EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255)); |
| 343 | EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0)); |
| 344 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230)); |
| 345 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300)); |
| 346 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325)); |
| 347 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325)); |
| 348 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400)); |
| 349 | EXPECT_TRUE(FPDFPath_Close(blue_path)); |
| 350 | FPDFPage_InsertObject(page, blue_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 351 | page_bitmap = RenderPage(page); |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 352 | const char last_md5[] = "9823e1a21bd9b72b6a442ba4f12af946"; |
| 353 | CompareBitmap(page_bitmap, 612, 792, last_md5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 354 | FPDFBitmap_Destroy(page_bitmap); |
| 355 | |
| 356 | // Now save the result, closing the page and document |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 357 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 358 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 359 | FPDF_ClosePage(page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 360 | |
| 361 | // Render the saved result |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 362 | TestAndCloseSaved(612, 792, last_md5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 363 | } |
| 364 | |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 365 | TEST_F(FPDFEditEmbeddertest, PathsPoints) { |
| 366 | CreateNewDocument(); |
| 367 | FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_); |
| 368 | // This should fail gracefully, even if img is not a path. |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 369 | ASSERT_EQ(-1, FPDFPath_CountSegments(img)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 370 | |
| 371 | // This should fail gracefully, even if path is NULL. |
Miklos Vajna | 0150a54 | 2017-09-21 21:46:56 +0200 | [diff] [blame] | 372 | ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr)); |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 373 | |
Miklos Vajna | 36eed87 | 2017-09-20 22:52:43 +0200 | [diff] [blame] | 374 | // FPDFPath_GetPathSegment() with a non-path. |
| 375 | ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0)); |
| 376 | // FPDFPath_GetPathSegment() with a NULL path. |
| 377 | ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0)); |
| 378 | float x; |
| 379 | float y; |
| 380 | // FPDFPathSegment_GetPoint() with a NULL segment. |
| 381 | EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y)); |
| 382 | |
| 383 | // FPDFPathSegment_GetType() with a NULL segment. |
| 384 | ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr)); |
| 385 | |
| 386 | // FPDFPathSegment_GetClose() with a NULL segment. |
| 387 | EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr)); |
| 388 | |
Miklos Vajna | 12abfd0 | 2017-09-15 07:49:03 +0200 | [diff] [blame] | 389 | FPDFPageObj_Destroy(img); |
| 390 | } |
| 391 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 392 | TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) { |
| 393 | // Load document with some text |
| 394 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 395 | FPDF_PAGE page = LoadPage(0); |
| 396 | EXPECT_NE(nullptr, page); |
| 397 | |
| 398 | // Add an opaque rectangle on top of some of the text. |
| 399 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| 400 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 401 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 402 | FPDFPage_InsertObject(page, red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 403 | |
| 404 | // Add a transparent triangle on top of other part of the text. |
| 405 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50); |
| 406 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100)); |
| 407 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 408 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80)); |
| 409 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10)); |
| 410 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
| 411 | FPDFPage_InsertObject(page, black_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 412 | |
| 413 | // Render and check the result. Text is slightly different on Mac. |
| 414 | FPDF_BITMAP bitmap = RenderPage(page); |
| 415 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 416 | const char md5[] = "f9e6fa74230f234286bfcada9f7606d8"; |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 417 | #else |
Nicolas Pena | 00c3cfd | 2017-07-10 17:29:54 -0400 | [diff] [blame] | 418 | const char md5[] = "bc6e6eb50dda4695ba0fb4d04ed82ada"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 419 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 420 | CompareBitmap(bitmap, 200, 200, md5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 421 | FPDFBitmap_Destroy(bitmap); |
| 422 | UnloadPage(page); |
| 423 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 424 | |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 425 | TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) { |
| 426 | // Load document with existing content |
| 427 | EXPECT_TRUE(OpenDocument("bug_717.pdf")); |
| 428 | FPDF_PAGE page = LoadPage(0); |
| 429 | EXPECT_NE(nullptr, page); |
| 430 | |
| 431 | // Add a transparent rectangle on top of the existing content |
| 432 | FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50); |
| 433 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100)); |
| 434 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
| 435 | FPDFPage_InsertObject(page, red_rect2); |
| 436 | |
| 437 | // Add an opaque rectangle on top of the existing content |
| 438 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50); |
| 439 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 440 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 441 | FPDFPage_InsertObject(page, red_rect); |
| 442 | |
| 443 | FPDF_BITMAP bitmap = RenderPage(page); |
| 444 | CompareBitmap(bitmap, 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); |
| 445 | FPDFBitmap_Destroy(bitmap); |
| 446 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 447 | |
| 448 | // Now save the result, closing the page and document |
| 449 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 450 | UnloadPage(page); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 451 | |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 452 | // Render the saved result without closing the page and document |
| 453 | TestSaved(612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 454 | |
| 455 | ClearString(); |
| 456 | // Add another opaque rectangle on top of the existing content |
| 457 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50); |
| 458 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255)); |
| 459 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 460 | FPDFPage_InsertObject(m_SavedPage, green_rect); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 461 | |
| 462 | // Add another transparent rectangle on top of existing content |
| 463 | FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50); |
| 464 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100)); |
| 465 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 466 | FPDFPage_InsertObject(m_SavedPage, green_rect2); |
| 467 | FPDF_BITMAP new_bitmap = RenderPageWithFlags(m_SavedPage, m_SavedForm, 0); |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 468 | const char last_md5[] = "4b5b00f824620f8c9b8801ebb98e1cdd"; |
| 469 | CompareBitmap(new_bitmap, 612, 792, last_md5); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 470 | FPDFBitmap_Destroy(new_bitmap); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 471 | EXPECT_TRUE(FPDFPage_GenerateContent(m_SavedPage)); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 472 | |
| 473 | // Now save the result, closing the page and document |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 474 | EXPECT_TRUE(FPDF_SaveAsCopy(m_SavedDocument, this, 0)); |
| 475 | CloseSaved(); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 476 | |
| 477 | // Render the saved result |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 478 | TestAndCloseSaved(612, 792, last_md5); |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 479 | } |
| 480 | |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 481 | TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) { |
| 482 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 483 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 484 | |
| 485 | // Add a large stroked rectangle (fill color should not affect it). |
| 486 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400); |
| 487 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255)); |
| 488 | EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255)); |
| 489 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f)); |
| 490 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1)); |
| 491 | FPDFPage_InsertObject(page, rect); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 492 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 493 | CompareBitmap(page_bitmap, 612, 792, "64bd31f862a89e0a9e505a5af6efd506"); |
| 494 | FPDFBitmap_Destroy(page_bitmap); |
| 495 | |
| 496 | // Add crossed-checkmark |
| 497 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500); |
| 498 | EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400)); |
| 499 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600)); |
| 500 | EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600)); |
| 501 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400)); |
| 502 | EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180)); |
| 503 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f)); |
| 504 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 505 | FPDFPage_InsertObject(page, check); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 506 | page_bitmap = RenderPage(page); |
| 507 | CompareBitmap(page_bitmap, 612, 792, "4b6f3b9d25c4e194821217d5016c3724"); |
| 508 | FPDFBitmap_Destroy(page_bitmap); |
| 509 | |
| 510 | // Add stroked and filled oval-ish path. |
| 511 | FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100); |
| 512 | EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300)); |
| 513 | EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305)); |
| 514 | EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105)); |
| 515 | EXPECT_TRUE(FPDFPath_Close(path)); |
| 516 | EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100)); |
| 517 | EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150)); |
| 518 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f)); |
| 519 | EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1)); |
| 520 | FPDFPage_InsertObject(page, path); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 521 | page_bitmap = RenderPage(page); |
| 522 | CompareBitmap(page_bitmap, 612, 792, "ff3e6a22326754944cc6e56609acd73b"); |
| 523 | FPDFBitmap_Destroy(page_bitmap); |
| 524 | FPDF_ClosePage(page); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 525 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 526 | |
| 527 | TEST_F(FPDFEditEmbeddertest, AddStandardFontText) { |
| 528 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 529 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 530 | |
| 531 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 532 | FPDF_PAGEOBJECT text_object1 = |
| 533 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 534 | EXPECT_TRUE(text_object1); |
| 535 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 = |
| 536 | GetFPDFWideString(L"I'm at the bottom of the page"); |
| 537 | EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get())); |
| 538 | FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20); |
| 539 | FPDFPage_InsertObject(page, text_object1); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 540 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 541 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 542 | const char md5[] = "a4dddc1a3930fa694bbff9789dab4161"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 543 | #else |
Nicolas Pena | 00c3cfd | 2017-07-10 17:29:54 -0400 | [diff] [blame] | 544 | const char md5[] = "7a35771853a1cbba38f6775807878625"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 545 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 546 | CompareBitmap(page_bitmap, 612, 792, md5); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 547 | FPDFBitmap_Destroy(page_bitmap); |
| 548 | |
| 549 | // Try another font |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 550 | FPDF_PAGEOBJECT text_object2 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 551 | FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 552 | EXPECT_TRUE(text_object2); |
| 553 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 554 | GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold."); |
| 555 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 556 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600); |
| 557 | FPDFPage_InsertObject(page, text_object2); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 558 | page_bitmap = RenderPage(page); |
| 559 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 560 | const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c"; |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 561 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
Nicolas Pena | 00c3cfd | 2017-07-10 17:29:54 -0400 | [diff] [blame] | 562 | const char md5_2[] = "b231b329a4b566fb9b42bfc15fe59bb7"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 563 | #else |
Nicolas Pena | 00c3cfd | 2017-07-10 17:29:54 -0400 | [diff] [blame] | 564 | const char md5_2[] = "f85fae151851436072b7b3c6703e506a"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 565 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 566 | CompareBitmap(page_bitmap, 612, 792, md5_2); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 567 | FPDFBitmap_Destroy(page_bitmap); |
| 568 | |
| 569 | // And some randomly transformed text |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 570 | FPDF_PAGEOBJECT text_object3 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 571 | FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 572 | EXPECT_TRUE(text_object3); |
| 573 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 = |
| 574 | GetFPDFWideString(L"Can you read me? <:)>"); |
| 575 | EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get())); |
| 576 | FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200); |
| 577 | FPDFPage_InsertObject(page, text_object3); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 578 | page_bitmap = RenderPage(page); |
| 579 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 580 | const char md5_3[] = "40b3ef04f915ff4c4208948001763544"; |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 581 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
Nicolas Pena | 00c3cfd | 2017-07-10 17:29:54 -0400 | [diff] [blame] | 582 | const char md5_3[] = "ba874b3b137f984510c4e287ed4ba7ae"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 583 | #else |
Nicolas Pena | 00c3cfd | 2017-07-10 17:29:54 -0400 | [diff] [blame] | 584 | const char md5_3[] = "c5aed6a8ef05558c8c47d58c87cbcb46"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 585 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 586 | CompareBitmap(page_bitmap, 612, 792, md5_3); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 587 | FPDFBitmap_Destroy(page_bitmap); |
| 588 | |
| 589 | // TODO(npm): Why are there issues with text rotated by 90 degrees? |
| 590 | // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this. |
| 591 | FPDF_ClosePage(page); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 592 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 593 | |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 594 | TEST_F(FPDFEditEmbeddertest, GraphicsData) { |
| 595 | // New page |
| 596 | std::unique_ptr<void, FPDFPageDeleter> page( |
| 597 | FPDFPage_New(CreateNewDocument(), 0, 612, 792)); |
| 598 | |
| 599 | // Create a rect with nontrivial graphics |
| 600 | FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 601 | FPDFPageObj_SetBlendMode(rect1, "Color"); |
| 602 | FPDFPage_InsertObject(page.get(), rect1); |
| 603 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
| 604 | |
| 605 | // Check that the ExtGState was created |
| 606 | CPDF_Page* the_page = CPDFPageFromFPDFPage(page.get()); |
| 607 | CPDF_Dictionary* graphics_dict = |
| 608 | the_page->m_pResources->GetDictFor("ExtGState"); |
| 609 | ASSERT_TRUE(graphics_dict); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 610 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 611 | |
| 612 | // Add a text object causing no change to the graphics dictionary |
| 613 | FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 614 | // Only alpha, the last component, matters for the graphics dictionary. And |
| 615 | // the default value is 255. |
| 616 | EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255)); |
| 617 | FPDFPage_InsertObject(page.get(), text1); |
| 618 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 619 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 620 | |
| 621 | // Add a text object increasing the size of the graphics dictionary |
| 622 | FPDF_PAGEOBJECT text2 = |
| 623 | FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f); |
| 624 | FPDFPage_InsertObject(page.get(), text2); |
| 625 | FPDFPageObj_SetBlendMode(text2, "Darken"); |
| 626 | EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150)); |
| 627 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 628 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 629 | |
| 630 | // Add a path that should reuse graphics |
Nicolas Pena | ce67be4 | 2017-06-14 14:52:49 -0400 | [diff] [blame] | 631 | FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 632 | FPDFPageObj_SetBlendMode(path, "Darken"); |
| 633 | EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150)); |
| 634 | FPDFPage_InsertObject(page.get(), path); |
| 635 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 636 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 637 | |
| 638 | // Add a rect increasing the size of the graphics dictionary |
| 639 | FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 640 | FPDFPageObj_SetBlendMode(rect2, "Darken"); |
| 641 | EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150)); |
| 642 | EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200)); |
| 643 | FPDFPage_InsertObject(page.get(), rect2); |
| 644 | EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 645 | EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | 603a31d | 2017-06-14 11:41:18 -0400 | [diff] [blame] | 646 | } |
| 647 | |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 648 | TEST_F(FPDFEditEmbeddertest, DoubleGenerating) { |
| 649 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 650 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 651 | |
| 652 | // Add a red rectangle with some non-default alpha |
| 653 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 654 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128)); |
| 655 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0)); |
| 656 | FPDFPage_InsertObject(page, rect); |
| 657 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 658 | |
| 659 | // Check the ExtGState |
| 660 | CPDF_Page* the_page = CPDFPageFromFPDFPage(page); |
| 661 | CPDF_Dictionary* graphics_dict = |
| 662 | the_page->m_pResources->GetDictFor("ExtGState"); |
| 663 | ASSERT_TRUE(graphics_dict); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 664 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 665 | |
| 666 | // Check the bitmap |
| 667 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 668 | CompareBitmap(page_bitmap, 612, 792, "5384da3406d62360ffb5cac4476fff1c"); |
| 669 | FPDFBitmap_Destroy(page_bitmap); |
| 670 | |
| 671 | // Never mind, my new favorite color is blue, increase alpha |
| 672 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180)); |
| 673 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 674 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 675 | |
| 676 | // Check that bitmap displays changed content |
| 677 | page_bitmap = RenderPage(page); |
| 678 | CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c"); |
| 679 | FPDFBitmap_Destroy(page_bitmap); |
| 680 | |
| 681 | // And now generate, without changes |
| 682 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 683 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 684 | page_bitmap = RenderPage(page); |
| 685 | CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c"); |
| 686 | FPDFBitmap_Destroy(page_bitmap); |
| 687 | |
| 688 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 689 | FPDF_PAGEOBJECT text_object = |
| 690 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 691 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 692 | GetFPDFWideString(L"Something something #text# something"); |
| 693 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 694 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300); |
| 695 | FPDFPage_InsertObject(page, text_object); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 696 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 697 | CPDF_Dictionary* font_dict = the_page->m_pResources->GetDictFor("Font"); |
| 698 | ASSERT_TRUE(font_dict); |
| 699 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 700 | |
| 701 | // Generate yet again, check dicts are reasonably sized |
| 702 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 703 | EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount())); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 704 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 705 | FPDF_ClosePage(page); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 706 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 707 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 708 | TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) { |
| 709 | CreateNewDocument(); |
| 710 | // TODO(npm): use other fonts after disallowing loading any font as any type |
| 711 | const CPDF_Font* stock_font = |
| 712 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 713 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 714 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 715 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 716 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false)); |
| 717 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 718 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 719 | EXPECT_TRUE(typed_font->IsType1Font()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 720 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 721 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 722 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 723 | EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype")); |
| 724 | EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont")); |
| 725 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 726 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 727 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 728 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
| 729 | |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 730 | CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 731 | ASSERT_TRUE(widths_array); |
| 732 | ASSERT_EQ(224U, widths_array->GetCount()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 733 | EXPECT_EQ(250, widths_array->GetNumberAt(0)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 734 | EXPECT_EQ(569, widths_array->GetNumberAt(11)); |
| 735 | EXPECT_EQ(500, widths_array->GetNumberAt(223)); |
| 736 | CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data); |
| 737 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 738 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 739 | TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) { |
| 740 | CreateNewDocument(); |
| 741 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 742 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 743 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 744 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 745 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false)); |
| 746 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 747 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 748 | EXPECT_TRUE(typed_font->IsTrueTypeFont()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 749 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 750 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
| 751 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 752 | EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype")); |
| 753 | EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont")); |
| 754 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 755 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 756 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
| 757 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 758 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 759 | CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
| 760 | ASSERT_TRUE(widths_array); |
| 761 | ASSERT_EQ(224U, widths_array->GetCount()); |
| 762 | EXPECT_EQ(600, widths_array->GetNumberAt(33)); |
| 763 | EXPECT_EQ(600, widths_array->GetNumberAt(74)); |
| 764 | EXPECT_EQ(600, widths_array->GetNumberAt(223)); |
| 765 | CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data); |
| 766 | } |
| 767 | |
| 768 | TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) { |
| 769 | CreateNewDocument(); |
| 770 | const CPDF_Font* stock_font = |
| 771 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 772 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 773 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 774 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 775 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1)); |
| 776 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 777 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 778 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 779 | |
| 780 | // Check font dictionary entries |
| 781 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
| 782 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 783 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 784 | EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont")); |
| 785 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
| 786 | CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts"); |
| 787 | ASSERT_TRUE(descendant_array); |
| 788 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 789 | |
| 790 | // Check the CIDFontDict |
| 791 | CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
| 792 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 793 | EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype")); |
| 794 | EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont")); |
| 795 | CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo"); |
| 796 | ASSERT_TRUE(cidinfo_dict); |
| 797 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 798 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 799 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 800 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data); |
| 801 | |
| 802 | // Check widths |
| 803 | CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
| 804 | ASSERT_TRUE(widths_array); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 805 | EXPECT_GT(widths_array->GetCount(), 1U); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 806 | CheckCompositeFontWidths(widths_array, typed_font); |
| 807 | } |
| 808 | |
| 809 | TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) { |
| 810 | CreateNewDocument(); |
| 811 | const CPDF_Font* stock_font = |
| 812 | CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 813 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 814 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 815 | |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 816 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 817 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1)); |
| 818 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 819 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 820 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 821 | |
| 822 | // Check font dictionary entries |
| 823 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
| 824 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 825 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 826 | EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont")); |
| 827 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
| 828 | CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts"); |
| 829 | ASSERT_TRUE(descendant_array); |
| 830 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 831 | |
| 832 | // Check the CIDFontDict |
| 833 | CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
| 834 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 835 | EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype")); |
| 836 | EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont")); |
| 837 | CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo"); |
| 838 | ASSERT_TRUE(cidinfo_dict); |
| 839 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 840 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 841 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 842 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size, |
| 843 | data); |
| 844 | |
| 845 | // Check widths |
| 846 | CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
| 847 | ASSERT_TRUE(widths_array); |
| 848 | CheckCompositeFontWidths(widths_array, typed_font); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 849 | } |
rbpotter | ce8e51e | 2017-04-28 12:42:47 -0700 | [diff] [blame] | 850 | |
| 851 | TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) { |
| 852 | // Load document with a -90 degree rotation |
| 853 | EXPECT_TRUE(OpenDocument("bug_713197.pdf")); |
| 854 | FPDF_PAGE page = LoadPage(0); |
| 855 | EXPECT_NE(nullptr, page); |
| 856 | |
| 857 | EXPECT_EQ(3, FPDFPage_GetRotation(page)); |
| 858 | UnloadPage(page); |
| 859 | } |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 860 | |
| 861 | TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) { |
| 862 | // Start with a blank page |
| 863 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 864 | { |
| 865 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 866 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 867 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 868 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 869 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0)); |
| 870 | ASSERT_TRUE(font.get()); |
| 871 | |
| 872 | // Add some text to the page |
| 873 | FPDF_PAGEOBJECT text_object = |
| 874 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 875 | EXPECT_TRUE(text_object); |
| 876 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 877 | GetFPDFWideString(L"I am testing my loaded font, WEE."); |
| 878 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 879 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400); |
| 880 | FPDFPage_InsertObject(page, text_object); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 881 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 882 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 883 | const char md5[] = "17d2b6cd574cf66170b09c8927529a94"; |
| 884 | #else |
Nicolas Pena | 00c3cfd | 2017-07-10 17:29:54 -0400 | [diff] [blame] | 885 | const char md5[] = "1722c6a9deed953d730de9cd13dcbd55"; |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 886 | #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 887 | CompareBitmap(page_bitmap, 612, 792, md5); |
| 888 | FPDFBitmap_Destroy(page_bitmap); |
| 889 | |
| 890 | // Add some more text, same font |
| 891 | FPDF_PAGEOBJECT text_object2 = |
| 892 | FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f); |
| 893 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 894 | GetFPDFWideString(L"Bigger font size"); |
| 895 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 896 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200); |
| 897 | FPDFPage_InsertObject(page, text_object2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 898 | } |
| 899 | FPDF_BITMAP page_bitmap2 = RenderPage(page); |
| 900 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 901 | const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea"; |
| 902 | #else |
Nicolas Pena | 00c3cfd | 2017-07-10 17:29:54 -0400 | [diff] [blame] | 903 | const char md5_2[] = "9d7885072058f6c3e68ecaf32e917f30"; |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 904 | #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 905 | CompareBitmap(page_bitmap2, 612, 792, md5_2); |
| 906 | FPDFBitmap_Destroy(page_bitmap2); |
| 907 | |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 908 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 909 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 910 | FPDF_ClosePage(page); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 911 | TestAndCloseSaved(612, 792, md5_2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 912 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 913 | |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 914 | TEST_F(FPDFEditEmbeddertest, TransformAnnot) { |
| 915 | // Open a file with one annotation and load its first page. |
| 916 | ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf")); |
| 917 | FPDF_PAGE page = FPDF_LoadPage(document(), 0); |
| 918 | ASSERT_TRUE(page); |
| 919 | |
| 920 | // Add an underline annotation to the page without specifying its rectangle. |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 921 | FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE); |
| 922 | ASSERT_TRUE(annot); |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 923 | |
| 924 | // FPDFPage_TransformAnnots() should run without errors when modifying |
| 925 | // annotation rectangles. |
| 926 | FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6); |
| 927 | |
Jane Liu | d60e9ad | 2017-06-26 11:28:36 -0400 | [diff] [blame] | 928 | FPDFPage_CloseAnnot(annot); |
Jane Liu | eda6525 | 2017-06-07 11:31:27 -0400 | [diff] [blame] | 929 | UnloadPage(page); |
| 930 | } |
| 931 | |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 932 | // TODO(npm): Add tests using Japanese fonts in other OS. |
| 933 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ |
| 934 | TEST_F(FPDFEditEmbeddertest, AddCIDFontText) { |
| 935 | // Start with a blank page |
| 936 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 937 | CFX_Font CIDfont; |
| 938 | { |
| 939 | // First, get the data from the font |
| 940 | CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0); |
| 941 | EXPECT_EQ("IPAGothic", CIDfont.GetFaceName()); |
| 942 | const uint8_t* data = CIDfont.GetFontData(); |
| 943 | const uint32_t size = CIDfont.GetSize(); |
| 944 | |
| 945 | // Load the data into a FPDF_Font. |
| 946 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 947 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1)); |
| 948 | ASSERT_TRUE(font.get()); |
| 949 | |
| 950 | // Add some text to the page |
| 951 | FPDF_PAGEOBJECT text_object = |
| 952 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 953 | ASSERT_TRUE(text_object); |
| 954 | std::wstring wstr = L"ABCDEFGhijklmnop."; |
| 955 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 956 | GetFPDFWideString(wstr); |
| 957 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 958 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200); |
| 959 | FPDFPage_InsertObject(page, text_object); |
| 960 | |
| 961 | // And add some Japanese characters |
| 962 | FPDF_PAGEOBJECT text_object2 = |
| 963 | FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f); |
| 964 | ASSERT_TRUE(text_object2); |
| 965 | std::wstring wstr2 = |
| 966 | L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1" |
| 967 | L"\u756A"; |
| 968 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 969 | GetFPDFWideString(wstr2); |
| 970 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 971 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500); |
| 972 | FPDFPage_InsertObject(page, text_object2); |
| 973 | } |
| 974 | |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 975 | // Check that the text renders properly. |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 976 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 977 | const char md5[] = "2bc6c1aaa2252e73246a75775ccf38c2"; |
| 978 | CompareBitmap(page_bitmap, 612, 792, md5); |
| 979 | FPDFBitmap_Destroy(page_bitmap); |
| 980 | |
| 981 | // Save the document, close the page. |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 982 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 983 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 984 | FPDF_ClosePage(page); |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 985 | TestAndCloseSaved(612, 792, md5); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 986 | } |
| 987 | #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 988 | |
| 989 | TEST_F(FPDFEditEmbeddertest, SaveAndRender) { |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 990 | const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b"; |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 991 | { |
| 992 | EXPECT_TRUE(OpenDocument("bug_779.pdf")); |
| 993 | FPDF_PAGE page = LoadPage(0); |
| 994 | ASSERT_NE(nullptr, page); |
| 995 | |
| 996 | // Now add a more complex blue path. |
| 997 | FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20); |
| 998 | EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200)); |
| 999 | // TODO(npm): stroking will cause the MD5s to differ. |
| 1000 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0)); |
| 1001 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63)); |
| 1002 | EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90)); |
| 1003 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133)); |
| 1004 | EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33)); |
| 1005 | EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40)); |
| 1006 | EXPECT_TRUE(FPDFPath_Close(green_path)); |
| 1007 | FPDFPage_InsertObject(page, green_path); |
| 1008 | FPDF_BITMAP page_bitmap = RenderPage(page); |
Nicolas Pena | a0b48aa | 2017-06-29 11:01:46 -0400 | [diff] [blame] | 1009 | CompareBitmap(page_bitmap, 612, 792, md5); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1010 | FPDFBitmap_Destroy(page_bitmap); |
| 1011 | |
| 1012 | // Now save the result, closing the page and document |
| 1013 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 1014 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 1015 | UnloadPage(page); |
| 1016 | } |
Nicolas Pena | 3ff5400 | 2017-07-05 11:55:35 -0400 | [diff] [blame] | 1017 | TestAndCloseSaved(612, 792, md5); |
Nicolas Pena | 9ba8fbc | 2017-06-28 15:31:56 -0400 | [diff] [blame] | 1018 | } |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 1019 | |
| 1020 | TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) { |
| 1021 | ASSERT_TRUE(OpenDocument("embedded_images.pdf")); |
| 1022 | FPDF_PAGE page = LoadPage(0); |
| 1023 | ASSERT_TRUE(page); |
Miklos Vajna | 9262761 | 2017-09-25 12:59:29 +0200 | [diff] [blame^] | 1024 | ASSERT_EQ(39, FPDFPage_CountObjects(page)); |
Jane Liu | 28fb7ba | 2017-08-02 21:45:57 -0400 | [diff] [blame] | 1025 | |
| 1026 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32); |
| 1027 | EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1028 | EXPECT_FALSE(FPDFImageObj_GetBitmap(obj)); |
| 1029 | |
| 1030 | obj = FPDFPage_GetObject(page, 33); |
| 1031 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1032 | FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj); |
| 1033 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1034 | CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae"); |
| 1035 | FPDFBitmap_Destroy(bitmap); |
| 1036 | |
| 1037 | obj = FPDFPage_GetObject(page, 34); |
| 1038 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1039 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1040 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1041 | CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6"); |
| 1042 | FPDFBitmap_Destroy(bitmap); |
| 1043 | |
| 1044 | obj = FPDFPage_GetObject(page, 35); |
| 1045 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1046 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1047 | EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap)); |
| 1048 | CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3"); |
| 1049 | FPDFBitmap_Destroy(bitmap); |
| 1050 | |
| 1051 | obj = FPDFPage_GetObject(page, 36); |
| 1052 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1053 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1054 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1055 | CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a"); |
| 1056 | FPDFBitmap_Destroy(bitmap); |
| 1057 | |
| 1058 | obj = FPDFPage_GetObject(page, 37); |
| 1059 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1060 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1061 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1062 | CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534"); |
| 1063 | FPDFBitmap_Destroy(bitmap); |
| 1064 | |
| 1065 | obj = FPDFPage_GetObject(page, 38); |
| 1066 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1067 | bitmap = FPDFImageObj_GetBitmap(obj); |
| 1068 | EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap)); |
| 1069 | CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5"); |
| 1070 | FPDFBitmap_Destroy(bitmap); |
| 1071 | UnloadPage(page); |
| 1072 | } |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 1073 | |
| 1074 | TEST_F(FPDFEditEmbeddertest, GetImageData) { |
| 1075 | EXPECT_TRUE(OpenDocument("embedded_images.pdf")); |
| 1076 | FPDF_PAGE page = LoadPage(0); |
| 1077 | ASSERT_TRUE(page); |
Miklos Vajna | 9262761 | 2017-09-25 12:59:29 +0200 | [diff] [blame^] | 1078 | ASSERT_EQ(39, FPDFPage_CountObjects(page)); |
Jane Liu | 548334e | 2017-08-03 16:33:40 -0400 | [diff] [blame] | 1079 | |
| 1080 | // Retrieve an image object with flate-encoded data stream. |
| 1081 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33); |
| 1082 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1083 | |
| 1084 | // Check that the raw image data has the correct length and hash value. |
| 1085 | unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0); |
| 1086 | std::vector<char> buf(len); |
| 1087 | EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len)); |
| 1088 | EXPECT_EQ("f73802327d2e88e890f653961bcda81a", |
| 1089 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 1090 | |
| 1091 | // Check that the decoded image data has the correct length and hash value. |
| 1092 | len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0); |
| 1093 | buf.clear(); |
| 1094 | buf.resize(len); |
| 1095 | EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len)); |
| 1096 | EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e", |
| 1097 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 1098 | |
| 1099 | // Retrieve an image obejct with DCTDecode-encoded data stream. |
| 1100 | obj = FPDFPage_GetObject(page, 37); |
| 1101 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1102 | |
| 1103 | // Check that the raw image data has the correct length and hash value. |
| 1104 | len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0); |
| 1105 | buf.clear(); |
| 1106 | buf.resize(len); |
| 1107 | EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len)); |
| 1108 | EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", |
| 1109 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 1110 | |
| 1111 | // Check that the decoded image data has the correct length and hash value, |
| 1112 | // which should be the same as those of the raw data, since this image is |
| 1113 | // encoded by a single DCTDecode filter and decoding is a noop. |
| 1114 | len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0); |
| 1115 | buf.clear(); |
| 1116 | buf.resize(len); |
| 1117 | EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len)); |
| 1118 | EXPECT_EQ("6aae1f3710335023a9e12191be66b64b", |
| 1119 | GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len)); |
| 1120 | |
| 1121 | UnloadPage(page); |
| 1122 | } |
Jane Liu | 2e5f0ae | 2017-08-08 15:23:27 -0400 | [diff] [blame] | 1123 | |
| 1124 | TEST_F(FPDFEditEmbeddertest, DestroyPageObject) { |
| 1125 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| 1126 | ASSERT_TRUE(rect); |
| 1127 | |
| 1128 | // There should be no memory leaks with a call to FPDFPageObj_Destroy(). |
| 1129 | FPDFPageObj_Destroy(rect); |
| 1130 | } |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 1131 | |
| 1132 | TEST_F(FPDFEditEmbeddertest, GetImageFilters) { |
| 1133 | EXPECT_TRUE(OpenDocument("embedded_images.pdf")); |
| 1134 | FPDF_PAGE page = LoadPage(0); |
| 1135 | ASSERT_TRUE(page); |
| 1136 | |
| 1137 | // Verify that retrieving the filter of a non-image object would fail. |
| 1138 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32); |
| 1139 | ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1140 | ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj)); |
| 1141 | EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0)); |
| 1142 | |
| 1143 | // Verify the returned filter string for an image object with a single filter. |
| 1144 | obj = FPDFPage_GetObject(page, 33); |
| 1145 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1146 | ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj)); |
| 1147 | unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0); |
| 1148 | std::vector<char> buf(len); |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 1149 | static constexpr char kFlateDecode[] = "FlateDecode"; |
| 1150 | EXPECT_EQ(sizeof(kFlateDecode), |
| 1151 | FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len)); |
| 1152 | EXPECT_STREQ(kFlateDecode, buf.data()); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 1153 | EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0)); |
| 1154 | |
| 1155 | // Verify all the filters for an image object with a list of filters. |
| 1156 | obj = FPDFPage_GetObject(page, 38); |
| 1157 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1158 | ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj)); |
| 1159 | len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0); |
| 1160 | buf.clear(); |
| 1161 | buf.resize(len); |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 1162 | static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode"; |
| 1163 | EXPECT_EQ(sizeof(kASCIIHexDecode), |
| 1164 | FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len)); |
| 1165 | EXPECT_STREQ(kASCIIHexDecode, buf.data()); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 1166 | |
| 1167 | len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0); |
| 1168 | buf.clear(); |
| 1169 | buf.resize(len); |
Lei Zhang | 0733a1b | 2017-08-31 12:36:31 -0700 | [diff] [blame] | 1170 | static constexpr char kDCTDecode[] = "DCTDecode"; |
| 1171 | EXPECT_EQ(sizeof(kDCTDecode), |
| 1172 | FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len)); |
| 1173 | EXPECT_STREQ(kDCTDecode, buf.data()); |
Jane Liu | be63ab9 | 2017-08-09 14:09:34 -0400 | [diff] [blame] | 1174 | |
| 1175 | UnloadPage(page); |
| 1176 | } |
Jane Liu | ca89829 | 2017-08-16 11:25:35 -0400 | [diff] [blame] | 1177 | |
| 1178 | TEST_F(FPDFEditEmbeddertest, GetImageMetadata) { |
| 1179 | ASSERT_TRUE(OpenDocument("embedded_images.pdf")); |
| 1180 | FPDF_PAGE page = LoadPage(0); |
| 1181 | ASSERT_TRUE(page); |
| 1182 | |
| 1183 | // Check that getting the metadata of a null object would fail. |
| 1184 | FPDF_IMAGEOBJ_METADATA metadata; |
| 1185 | EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata)); |
| 1186 | |
| 1187 | // Check that receiving the metadata with a null metadata object would fail. |
| 1188 | FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35); |
| 1189 | EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr)); |
| 1190 | |
| 1191 | // Check that when retrieving an image object's metadata without passing in |
| 1192 | // |page|, all values are correct, with the last two being default values. |
| 1193 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1194 | ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata)); |
| 1195 | EXPECT_EQ(92u, metadata.width); |
| 1196 | EXPECT_EQ(68u, metadata.height); |
| 1197 | EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001); |
| 1198 | EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001); |
| 1199 | EXPECT_EQ(0u, metadata.bits_per_pixel); |
| 1200 | EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace); |
| 1201 | |
| 1202 | // Verify the metadata of a bitmap image with indexed colorspace. |
| 1203 | ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata)); |
| 1204 | EXPECT_EQ(92u, metadata.width); |
| 1205 | EXPECT_EQ(68u, metadata.height); |
| 1206 | EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001); |
| 1207 | EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001); |
| 1208 | EXPECT_EQ(1u, metadata.bits_per_pixel); |
| 1209 | EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace); |
| 1210 | |
| 1211 | // Verify the metadata of an image with RGB colorspace. |
| 1212 | obj = FPDFPage_GetObject(page, 37); |
| 1213 | ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj)); |
| 1214 | ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata)); |
| 1215 | EXPECT_EQ(126u, metadata.width); |
| 1216 | EXPECT_EQ(106u, metadata.height); |
| 1217 | EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001); |
| 1218 | EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001); |
| 1219 | EXPECT_EQ(24u, metadata.bits_per_pixel); |
| 1220 | EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace); |
| 1221 | |
| 1222 | UnloadPage(page); |
| 1223 | } |