blob: 227e5c9ef50bc3e7553a657a979542e62f96a1b0 [file] [log] [blame]
Tom Sepezd483eb42016-01-06 10:03:59 -08001// 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 Sinclair85c8e7f2016-11-21 13:50:32 -05005#include <memory>
6#include <string>
Nicolas Penad03ca422017-03-06 13:54:33 -05007#include <utility>
Jane Liu548334e2017-08-03 16:33:40 -04008#include <vector>
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009
Nicolas Penabe90aae2017-02-27 10:41:41 -050010#include "core/fpdfapi/font/cpdf_font.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050011#include "core/fpdfapi/page/cpdf_page.h"
Henrique Nakashima6eb79392018-06-12 20:27:35 +000012#include "core/fpdfapi/page/cpdf_pageobject.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050013#include "core/fpdfapi/parser/cpdf_array.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050014#include "core/fpdfapi/parser/cpdf_dictionary.h"
Nicolas Penad03ca422017-03-06 13:54:33 -050015#include "core/fpdfapi/parser/cpdf_number.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050016#include "core/fpdfapi/parser/cpdf_stream.h"
Artem Strygineababa12018-06-06 12:31:18 +000017#include "core/fpdfapi/parser/cpdf_stream_acc.h"
Nicolas Pena0fc185e2017-02-08 12:13:20 -050018#include "core/fxcrt/fx_system.h"
Dan Sinclair00d47a62018-03-28 18:39:04 +000019#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000020#include "public/cpp/fpdf_scopers.h"
Jane Liueda65252017-06-07 11:31:27 -040021#include "public/fpdf_annot.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080022#include "public/fpdf_edit.h"
23#include "public/fpdfview.h"
24#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080025#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080026#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080027#include "testing/test_support.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080028
Nicolas Pena3ff54002017-07-05 11:55:35 -040029class FPDFEditEmbeddertest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050030 protected:
31 FPDF_DOCUMENT CreateNewDocument() {
32 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070033 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050034 return document_;
35 }
36
Lei Zhang710fa992018-05-25 16:24:48 +000037 void CheckFontDescriptor(const CPDF_Dictionary* font_dict,
Nicolas Penad03ca422017-03-06 13:54:33 -050038 int font_type,
39 bool bold,
40 bool italic,
41 uint32_t size,
42 const uint8_t* data) {
Lei Zhangb1ec2802018-05-25 21:55:24 +000043 const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
Nicolas Penad03ca422017-03-06 13:54:33 -050044 ASSERT_TRUE(font_desc);
45 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
46 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
47 font_desc->GetStringFor("FontName"));
48
49 // Check that the font descriptor has the required keys according to spec
50 // 1.7 Table 5.19
51 ASSERT_TRUE(font_desc->KeyExist("Flags"));
Dan Sinclair10e1f052017-09-28 15:59:42 -040052
Nicolas Penad03ca422017-03-06 13:54:33 -050053 int font_flags = font_desc->GetIntegerFor("Flags");
Dan Sinclair10e1f052017-09-28 15:59:42 -040054 EXPECT_EQ(bold, FontStyleIsBold(font_flags));
55 EXPECT_EQ(italic, FontStyleIsItalic(font_flags));
56 EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags));
Nicolas Penad03ca422017-03-06 13:54:33 -050057 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
Nicolás Peña5f95f362017-09-28 13:00:45 +090058
Lei Zhangb1ec2802018-05-25 21:55:24 +000059 const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox");
Nicolás Peña5f95f362017-09-28 13:00:45 +090060 ASSERT_TRUE(fontBBox);
61 EXPECT_EQ(4U, fontBBox->GetCount());
62 // Check that the coordinates are in the preferred order according to spec
63 // 1.7 Section 3.8.4
64 EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2));
65 EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3));
66
Nicolas Penad03ca422017-03-06 13:54:33 -050067 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
68 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
69 EXPECT_TRUE(font_desc->KeyExist("Descent"));
70 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
71 EXPECT_TRUE(font_desc->KeyExist("StemV"));
Ryan Harrison275e2602017-09-18 14:23:18 -040072 ByteString present("FontFile");
73 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050074 if (font_type == FPDF_FONT_TRUETYPE)
75 std::swap(present, absent);
76 EXPECT_TRUE(font_desc->KeyExist(present));
77 EXPECT_FALSE(font_desc->KeyExist(absent));
78
Artem Strygineababa12018-06-06 12:31:18 +000079 auto streamAcc =
80 pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present));
81 streamAcc->LoadAllDataRaw();
82
Nicolas Penad03ca422017-03-06 13:54:33 -050083 // Check that the font stream is the one that was provided
Artem Strygineababa12018-06-06 12:31:18 +000084 ASSERT_EQ(size, streamAcc->GetSize());
Nicolás Peña79eab232017-09-28 13:29:05 +090085 if (font_type == FPDF_FONT_TRUETYPE) {
86 ASSERT_EQ(static_cast<int>(size),
Artem Strygineababa12018-06-06 12:31:18 +000087 streamAcc->GetDict()->GetIntegerFor("Length1"));
Nicolás Peña79eab232017-09-28 13:29:05 +090088 }
Artem Strygineababa12018-06-06 12:31:18 +000089
90 const uint8_t* stream_data = streamAcc->GetData();
Nicolas Penad03ca422017-03-06 13:54:33 -050091 for (size_t j = 0; j < size; j++)
92 EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j;
93 }
94
Lei Zhangde579ab2018-05-25 21:49:49 +000095 void CheckCompositeFontWidths(const CPDF_Array* widths_array,
Nicolas Penad03ca422017-03-06 13:54:33 -050096 CPDF_Font* typed_font) {
97 // Check that W array is in a format that conforms to PDF spec 1.7 section
98 // "Glyph Metrics in CIDFonts" (these checks are not
99 // implementation-specific).
100 EXPECT_GT(widths_array->GetCount(), 1U);
101 int num_cids_checked = 0;
102 int cur_cid = 0;
103 for (size_t idx = 0; idx < widths_array->GetCount(); idx++) {
104 int cid = widths_array->GetNumberAt(idx);
105 EXPECT_GE(cid, cur_cid);
106 ASSERT_FALSE(++idx == widths_array->GetCount());
Lei Zhangde579ab2018-05-25 21:49:49 +0000107 const CPDF_Object* next = widths_array->GetObjectAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500108 if (next->IsArray()) {
109 // We are in the c [w1 w2 ...] case
Lei Zhangde579ab2018-05-25 21:49:49 +0000110 const CPDF_Array* arr = next->AsArray();
Nicolas Penad03ca422017-03-06 13:54:33 -0500111 int cnt = static_cast<int>(arr->GetCount());
112 size_t inner_idx = 0;
113 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
Nicolas Pena23346602018-01-30 21:42:41 +0000114 uint32_t width = arr->GetNumberAt(inner_idx++);
Dan Sinclair971a6742018-03-28 19:23:25 +0000115 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
116 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500117 }
118 num_cids_checked += cnt;
119 continue;
120 }
121 // Otherwise, are in the c_first c_last w case.
122 ASSERT_TRUE(next->IsNumber());
123 int last_cid = next->AsNumber()->GetInteger();
124 ASSERT_FALSE(++idx == widths_array->GetCount());
Nicolas Pena23346602018-01-30 21:42:41 +0000125 uint32_t width = widths_array->GetNumberAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500126 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
Dan Sinclair971a6742018-03-28 19:23:25 +0000127 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
128 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500129 }
130 num_cids_checked += last_cid - cid + 1;
131 }
132 // Make sure we have a good amount of cids described
133 EXPECT_GT(num_cids_checked, 900);
134 }
135 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
136
137 private:
138 CPDF_Document* cpdf_doc_;
139};
Tom Sepezd483eb42016-01-06 10:03:59 -0800140
etienneb7712c262016-04-26 08:13:45 -0700141namespace {
thestigdc7ec032016-11-21 15:32:52 -0800142
etienneb7712c262016-04-26 08:13:45 -0700143const char kExpectedPDF[] =
144 "%PDF-1.7\r\n"
145 "%\xA1\xB3\xC5\xD7\r\n"
146 "1 0 obj\r\n"
147 "<</Pages 2 0 R /Type/Catalog>>\r\n"
148 "endobj\r\n"
149 "2 0 obj\r\n"
150 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
151 "endobj\r\n"
152 "3 0 obj\r\n"
153 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
154 "endobj\r\n"
155 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400156 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
157 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400158 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700159 ">>\r\n"
160 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400161 "5 0 obj\r\n"
162 "<</BM/Normal/CA 1/ca 1>>\r\n"
163 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700164 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400165 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700166 "0000000000 65535 f\r\n"
167 "0000000017 00000 n\r\n"
168 "0000000066 00000 n\r\n"
169 "0000000122 00000 n\r\n"
170 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400171 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700172 "trailer\r\n"
173 "<<\r\n"
174 "/Root 1 0 R\r\n"
175 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400176 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700177 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400178 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700179 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800180
etienneb7712c262016-04-26 08:13:45 -0700181} // namespace
182
Tom Sepezd483eb42016-01-06 10:03:59 -0800183TEST_F(FPDFEditEmbeddertest, EmptyCreation) {
184 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700185 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800186 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400187 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800188 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800189 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700190
Nicolas Penad9d6c292017-06-06 16:12:10 -0400191 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
192 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700193 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800194}
thestigdc7ec032016-11-21 15:32:52 -0800195
196// Regression test for https://crbug.com/667012
197TEST_F(FPDFEditEmbeddertest, RasterizePDF) {
198 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
199
200 // Get the bitmap for the original document/
Tom Sepeze08d2b12018-04-25 18:49:32 +0000201 ScopedFPDFBitmap orig_bitmap;
thestigdc7ec032016-11-21 15:32:52 -0800202 {
203 EXPECT_TRUE(OpenDocument("black.pdf"));
204 FPDF_PAGE orig_page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000205 ASSERT_TRUE(orig_page);
206 orig_bitmap = RenderLoadedPage(orig_page);
207 CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800208 UnloadPage(orig_page);
209 }
210
211 // Create a new document from |orig_bitmap| and save it.
212 {
213 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
214 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
215
216 // Add the bitmap to an image object and add the image object to the output
217 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700218 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000219 EXPECT_TRUE(
220 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get()));
thestigdc7ec032016-11-21 15:32:52 -0800221 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
222 FPDFPage_InsertObject(temp_page, temp_img);
223 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
224 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
225 FPDF_ClosePage(temp_page);
226 FPDF_CloseDocument(temp_doc);
227 }
thestigdc7ec032016-11-21 15:32:52 -0800228
229 // Get the generated content. Make sure it is at least as big as the original
230 // PDF.
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400231 EXPECT_GT(GetString().size(), 923U);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400232 VerifySavedDocument(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800233}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500234
235TEST_F(FPDFEditEmbeddertest, AddPaths) {
236 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500237 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000238 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500239
240 // We will first add a red rectangle
241 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000242 ASSERT_TRUE(red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500243 // Expect false when trying to set colors out of range
244 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
245 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
246
247 // Fill rectangle with red and insert to the page
248 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
249 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
Miklos Vajna491112b2018-05-30 13:30:10 +0000250
251 int fillmode = FPDF_FILLMODE_NONE;
252 FPDF_BOOL stroke = true;
253 EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke));
254 EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode);
255 EXPECT_FALSE(stroke);
256
Miklos Vajna97f4d672018-06-04 14:47:17 +0000257 double matrix_a = 1;
258 double matrix_b = 2;
259 double matrix_c = 3;
260 double matrix_d = 4;
261 double matrix_e = 5;
262 double matrix_f = 6;
263 EXPECT_FALSE(FPDFPath_SetMatrix(nullptr, matrix_a, matrix_b, matrix_c,
264 matrix_d, matrix_e, matrix_f));
265 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
266 matrix_d, matrix_e, matrix_f));
Miklos Vajnaac42dd22018-06-05 13:28:58 +0000267 // Set to 0 before FPDFPath_GetMatrix() to ensure they are actually set by
268 // the function.
269 matrix_a = 0;
270 matrix_b = 0;
271 matrix_c = 0;
272 matrix_d = 0;
273 matrix_e = 0;
274 matrix_f = 0;
Miklos Vajna97f4d672018-06-04 14:47:17 +0000275 EXPECT_FALSE(FPDFPath_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
276 &matrix_d, &matrix_e, &matrix_f));
277 EXPECT_TRUE(FPDFPath_GetMatrix(red_rect, &matrix_a, &matrix_b, &matrix_c,
278 &matrix_d, &matrix_e, &matrix_f));
279 EXPECT_EQ(1, static_cast<int>(matrix_a));
280 EXPECT_EQ(2, static_cast<int>(matrix_b));
281 EXPECT_EQ(3, static_cast<int>(matrix_c));
282 EXPECT_EQ(4, static_cast<int>(matrix_d));
283 EXPECT_EQ(5, static_cast<int>(matrix_e));
284 EXPECT_EQ(6, static_cast<int>(matrix_f));
285 // Set back the default
286 matrix_a = 1;
287 matrix_b = 0;
288 matrix_c = 0;
289 matrix_d = 1;
290 matrix_e = 0;
291 matrix_f = 0;
292 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
293 matrix_d, matrix_e, matrix_f));
294
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500295 FPDFPage_InsertObject(page, red_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000296 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000297 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000298 CompareBitmap(page_bitmap.get(), 612, 792,
299 "66d02eaa6181e2c069ce2ea99beda497");
300 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500301
302 // Now add to that a green rectangle with some medium alpha
303 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
304 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200305
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200306 // Make sure the type of the rectangle is a path.
307 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
308
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200309 // Make sure we get back the same color we set previously.
310 unsigned int R;
311 unsigned int G;
312 unsigned int B;
313 unsigned int A;
314 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
315 EXPECT_EQ(0U, R);
316 EXPECT_EQ(255U, G);
317 EXPECT_EQ(0U, B);
318 EXPECT_EQ(128U, A);
319
Miklos Vajna12abfd02017-09-15 07:49:03 +0200320 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
321 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200322 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200323 // Verify actual coordinates.
324 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
325 float x;
326 float y;
327 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
328 EXPECT_EQ(100, x);
329 EXPECT_EQ(100, y);
330 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
331 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
332 segment = FPDFPath_GetPathSegment(green_rect, 1);
333 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
334 EXPECT_EQ(100, x);
335 EXPECT_EQ(140, y);
336 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
337 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
338 segment = FPDFPath_GetPathSegment(green_rect, 2);
339 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
340 EXPECT_EQ(140, x);
341 EXPECT_EQ(140, y);
342 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
343 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
344 segment = FPDFPath_GetPathSegment(green_rect, 3);
345 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
346 EXPECT_EQ(140, x);
347 EXPECT_EQ(100, y);
348 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
349 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
350 segment = FPDFPath_GetPathSegment(green_rect, 4);
351 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
352 EXPECT_EQ(100, x);
353 EXPECT_EQ(100, y);
354 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
355 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200356
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500357 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
358 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000359 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000360 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000361 CompareBitmap(page_bitmap.get(), 612, 792,
362 "7b0b87604594e773add528fae567a558");
363 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500364
365 // Add a black triangle.
366 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
367 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
368 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
369 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
370 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
371 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200372
373 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
374 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200375 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200376 // Verify actual coordinates.
377 segment = FPDFPath_GetPathSegment(black_path, 0);
378 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
379 EXPECT_EQ(400, x);
380 EXPECT_EQ(100, y);
381 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
382 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
383 segment = FPDFPath_GetPathSegment(black_path, 1);
384 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
385 EXPECT_EQ(400, x);
386 EXPECT_EQ(200, y);
387 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
388 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
389 segment = FPDFPath_GetPathSegment(black_path, 2);
390 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
391 EXPECT_EQ(300, x);
392 EXPECT_EQ(100, y);
393 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
394 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
395 // Make sure out of bounds index access fails properly.
396 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200397
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500398 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000399 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000400 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000401 CompareBitmap(page_bitmap.get(), 612, 792,
402 "eadc8020a14dfcf091da2688733d8806");
403 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500404
405 // Now add a more complex blue path.
406 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
407 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
408 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
409 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
410 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
411 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
412 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
413 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
414 EXPECT_TRUE(FPDFPath_Close(blue_path));
415 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000416 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
417 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000418 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000419 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
420 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500421
422 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400423 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500424 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500425 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500426
427 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000428 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500429}
430
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000431TEST_F(FPDFEditEmbeddertest, SetText) {
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000432 // Load document with some text.
433 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
434 FPDF_PAGE page = LoadPage(0);
435 ASSERT_TRUE(page);
436
437 // Get the "Hello, world!" text object and change it.
438 ASSERT_EQ(2, FPDFPage_CountObjects(page));
439 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
440 ASSERT_TRUE(page_object);
441 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
442 GetFPDFWideString(L"Changed for SetText test");
443 EXPECT_TRUE(FPDFText_SetText(page_object, text1.get()));
444
445 // Verify the "Hello, world!" text is gone and "Changed for SetText test" is
446 // now displayed.
447 ASSERT_EQ(2, FPDFPage_CountObjects(page));
448#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
449 const char kChangedMD5[] = "94c1e7a5af7dd9d77dc2223b1091acb7";
450#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
451 const char kChangedMD5[] = "9d31703c1d1a3e1e9a778b1e297c9cd2";
452#else
453 const char kChangedMD5[] = "a0c4ea6620772991f66bf7130379b08a";
454#endif
455 {
456 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
457 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
458 }
459
460 // Now save the result.
461 EXPECT_TRUE(FPDFPage_GenerateContent(page));
462 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
463
464 UnloadPage(page);
465
466 // Re-open the file and check the changes were kept in the saved .pdf.
467 OpenSavedDocument();
468 FPDF_PAGE saved_page = LoadSavedPage(0);
469 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
470 {
471 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
472 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
473 }
474
475 CloseSavedPage(saved_page);
476 CloseSavedDocument();
477}
478
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000479TEST_F(FPDFEditEmbeddertest, RemovePageObject) {
480 // Load document with some text.
481 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
482 FPDF_PAGE page = LoadPage(0);
483 ASSERT_TRUE(page);
484
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000485 // Show what the original file looks like.
486 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000487#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000488 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000489#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000490 const char kOriginalMD5[] = "e5a6fa28298db07484cd922f3e210c88";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000491#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000492 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000493#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000494 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000495 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
496 }
497
498 // Get the "Hello, world!" text object and remove it.
499 ASSERT_EQ(2, FPDFPage_CountObjects(page));
500 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
501 ASSERT_TRUE(page_object);
502 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
503
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000504 // Verify the "Hello, world!" text is gone.
505 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000506#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000507 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000508#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000509 const char kRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000510#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000511 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000512#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000513 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000514 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
515 }
516 ASSERT_EQ(1, FPDFPage_CountObjects(page));
517
518 UnloadPage(page);
519 FPDFPageObj_Destroy(page_object);
520}
521
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000522void CheckMarkCounts(FPDF_PAGE page,
523 int start_from,
524 int expected_object_count,
525 size_t expected_prime_count,
526 size_t expected_square_count,
527 size_t expected_greater_than_ten_count,
528 size_t expected_bounds_count) {
529 int object_count = FPDFPage_CountObjects(page);
530 ASSERT_EQ(expected_object_count, object_count);
531
532 size_t prime_count = 0;
533 size_t square_count = 0;
534 size_t greater_than_ten_count = 0;
535 size_t bounds_count = 0;
536 for (int i = 0; i < object_count; ++i) {
537 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
538
539 int mark_count = FPDFPageObj_CountMarks(page_object);
540 for (int j = 0; j < mark_count; ++j) {
541 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
542
543 char buffer[256];
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000544 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000545 std::wstring name =
546 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
547 if (name == L"Prime") {
548 prime_count++;
549 } else if (name == L"Square") {
550 square_count++;
551 int expected_square = start_from + i;
552 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
553
554 unsigned long get_param_key_return =
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000555 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000556 ASSERT_GT(get_param_key_return, 0u);
557 EXPECT_EQ((6u + 1u) * 2u, get_param_key_return);
558 std::wstring key =
559 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
560 EXPECT_EQ(L"Factor", key);
561
562 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000563 FPDFPageObjMark_GetParamValueType(mark, "Factor"));
Henrique Nakashima140dead2018-07-11 21:40:03 +0000564 int square_root;
Henrique Nakashima94230e52018-07-11 22:02:02 +0000565 EXPECT_TRUE(
566 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &square_root));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000567 EXPECT_EQ(expected_square, square_root * square_root);
568 } else if (name == L"GreaterThanTen") {
569 greater_than_ten_count++;
570 } else if (name == L"Bounds") {
571 bounds_count++;
572 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
573
574 unsigned long get_param_key_return =
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000575 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000576 ASSERT_GT(get_param_key_return, 0u);
577 EXPECT_EQ((8u + 1u) * 2u, get_param_key_return);
578 std::wstring key =
579 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
580 EXPECT_EQ(L"Position", key);
581
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000582 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000583 FPDFPageObjMark_GetParamValueType(mark, "Position"));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000584 unsigned long length;
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000585 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
586 mark, "Position", buffer, sizeof(buffer), &length));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000587 ASSERT_GT(length, 0u);
Henrique Nakashima140dead2018-07-11 21:40:03 +0000588 std::wstring value =
589 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000590
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000591 // "Position" can be "First", "Last", or "End".
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000592 if (i == 0) {
593 EXPECT_EQ((5u + 1u) * 2u, length);
594 EXPECT_EQ(L"First", value);
595 } else if (i == object_count - 1) {
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000596 if (length == (4u + 1u) * 2u) {
597 EXPECT_EQ(L"Last", value);
598 } else if (length == (3u + 1u) * 2u) {
599 EXPECT_EQ(L"End", value);
600 } else {
601 FAIL();
602 }
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000603 } else {
604 FAIL();
605 }
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000606 } else {
607 FAIL();
608 }
609 }
610 }
611
612 // Expect certain number of tagged objects. The test file contains strings
613 // from 1 to 19.
614 EXPECT_EQ(expected_prime_count, prime_count);
615 EXPECT_EQ(expected_square_count, square_count);
616 EXPECT_EQ(expected_greater_than_ten_count, greater_than_ten_count);
617 EXPECT_EQ(expected_bounds_count, bounds_count);
618}
619
620TEST_F(FPDFEditEmbeddertest, ReadMarkedObjectsIndirectDict) {
621 // Load document with some text marked with an indirect property.
622 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
623 FPDF_PAGE page = LoadPage(0);
624 ASSERT_TRUE(page);
625
626 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
627
628 UnloadPage(page);
629}
630
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000631TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) {
632 // Load document with some text.
633 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
634 FPDF_PAGE page = LoadPage(0);
635 ASSERT_TRUE(page);
636
637 // Show what the original file looks like.
638 {
639#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
640 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
641#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
642 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
643#else
644 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
645#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000646 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000647 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
648 }
649
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000650 constexpr int expected_object_count = 19;
651 CheckMarkCounts(page, 1, expected_object_count, 8, 4, 9, 1);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000652
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000653 // Get all objects marked with "Prime"
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000654 std::vector<FPDF_PAGEOBJECT> primes;
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000655 for (int i = 0; i < expected_object_count; ++i) {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000656 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
657
658 int mark_count = FPDFPageObj_CountMarks(page_object);
659 for (int j = 0; j < mark_count; ++j) {
660 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
661
662 char buffer[256];
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000663 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000664 std::wstring name =
665 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
666 if (name == L"Prime") {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000667 primes.push_back(page_object);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000668 }
669 }
670 }
671
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000672 // Remove all objects marked with "Prime".
673 for (FPDF_PAGEOBJECT page_object : primes) {
674 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
675 FPDFPageObj_Destroy(page_object);
676 }
677
678 EXPECT_EQ(11, FPDFPage_CountObjects(page));
679
680 {
681#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
682 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
683#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
684 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
685#else
686 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
687#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000688 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000689 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
690 }
691
692 UnloadPage(page);
693}
694
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000695TEST_F(FPDFEditEmbeddertest, RemoveMarks) {
696 // Load document with some text.
697 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
698 FPDF_PAGE page = LoadPage(0);
699 ASSERT_TRUE(page);
700
701 constexpr int kExpectedObjectCount = 19;
702 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
703
704 // Remove all "Prime" content marks.
705 for (int i = 0; i < kExpectedObjectCount; ++i) {
706 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
707
708 int mark_count = FPDFPageObj_CountMarks(page_object);
709 for (int j = mark_count - 1; j >= 0; --j) {
710 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
711
712 char buffer[256];
713 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
714 std::wstring name =
715 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
716 if (name == L"Prime") {
717 // Remove mark.
718 EXPECT_TRUE(FPDFPageObj_RemoveMark(page_object, mark));
719
720 // Verify there is now one fewer mark in the page object.
721 EXPECT_EQ(mark_count - 1, FPDFPageObj_CountMarks(page_object));
722 }
723 }
724 }
725
726 // Verify there are 0 "Prime" content marks now.
727 CheckMarkCounts(page, 1, kExpectedObjectCount, 0, 4, 9, 1);
728
729 // Save the file.
730 EXPECT_TRUE(FPDFPage_GenerateContent(page));
731 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
732 UnloadPage(page);
733
734 // Re-open the file and check the prime marks are not there anymore.
735 OpenSavedDocument();
736 FPDF_PAGE saved_page = LoadSavedPage(0);
737
738 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 0, 4, 9, 1);
739
740 CloseSavedPage(saved_page);
741 CloseSavedDocument();
742}
743
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000744TEST_F(FPDFEditEmbeddertest, RemoveMarkParam) {
745 // Load document with some text.
746 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
747 FPDF_PAGE page = LoadPage(0);
748 ASSERT_TRUE(page);
749
750 constexpr int kExpectedObjectCount = 19;
751 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
752
753 // Remove all "Square" content marks parameters.
754 for (int i = 0; i < kExpectedObjectCount; ++i) {
755 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
756
757 int mark_count = FPDFPageObj_CountMarks(page_object);
758 for (int j = 0; j < mark_count; ++j) {
759 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
760
761 char buffer[256];
762 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
763 std::wstring name =
764 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
765 if (name == L"Square") {
766 // Show the mark has a "Factor" parameter.
767 int out_value;
768 EXPECT_TRUE(
769 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
770
771 // Remove parameter.
772 EXPECT_TRUE(FPDFPageObjMark_RemoveParam(page_object, mark, "Factor"));
773
774 // Verify the "Factor" parameter is gone.
775 EXPECT_FALSE(
776 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
777 }
778 }
779 }
780
781 // Save the file.
782 EXPECT_TRUE(FPDFPage_GenerateContent(page));
783 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
784 UnloadPage(page);
785
786 // Re-open the file and check the "Factor" parameters are still gone.
787 OpenSavedDocument();
788 FPDF_PAGE saved_page = LoadSavedPage(0);
789
790 size_t square_count = 0;
791 for (int i = 0; i < kExpectedObjectCount; ++i) {
792 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
793
794 int mark_count = FPDFPageObj_CountMarks(page_object);
795 for (int j = 0; j < mark_count; ++j) {
796 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
797
798 char buffer[256];
799 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
800 std::wstring name =
801 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
802 if (name == L"Square") {
803 // Verify the "Factor" parameter is still gone.
804 int out_value;
805 EXPECT_FALSE(
806 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
807
808 ++square_count;
809 }
810 }
811 }
812
813 // Verify the parameters are gone, but the marks are not.
814 EXPECT_EQ(4u, square_count);
815
816 CloseSavedPage(saved_page);
817 CloseSavedDocument();
818}
819
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000820TEST_F(FPDFEditEmbeddertest, MaintainMarkedObjects) {
821 // Load document with some text.
822 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
823 FPDF_PAGE page = LoadPage(0);
824 ASSERT_TRUE(page);
825
826 // Iterate over all objects, counting the number of times each content mark
827 // name appears.
828 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
829
830 // Remove first page object.
831 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
832 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
833 FPDFPageObj_Destroy(page_object);
834
835 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
836
837 EXPECT_TRUE(FPDFPage_GenerateContent(page));
838 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
839
840 UnloadPage(page);
841
842 OpenSavedDocument();
843 FPDF_PAGE saved_page = LoadSavedPage(0);
844
845 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
846
847 CloseSavedPage(saved_page);
848 CloseSavedDocument();
849}
850
851TEST_F(FPDFEditEmbeddertest, MaintainIndirectMarkedObjects) {
852 // Load document with some text.
853 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
854 FPDF_PAGE page = LoadPage(0);
855 ASSERT_TRUE(page);
856
857 // Iterate over all objects, counting the number of times each content mark
858 // name appears.
859 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
860
861 // Remove first page object.
862 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
863 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
864 FPDFPageObj_Destroy(page_object);
865
866 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
867
868 EXPECT_TRUE(FPDFPage_GenerateContent(page));
869 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
870
871 UnloadPage(page);
872
873 OpenSavedDocument();
874 FPDF_PAGE saved_page = LoadSavedPage(0);
875
876 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
877
878 CloseSavedPage(saved_page);
879 CloseSavedDocument();
880}
881
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000882TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObject) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000883 // Load document with some text.
884 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
885 FPDF_PAGE page = LoadPage(0);
886 ASSERT_TRUE(page);
887
888 // Get the "Hello, world!" text object and remove it.
889 ASSERT_EQ(2, FPDFPage_CountObjects(page));
890 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
891 ASSERT_TRUE(page_object);
892 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
893
894 // Verify the "Hello, world!" text is gone.
895 ASSERT_EQ(1, FPDFPage_CountObjects(page));
896
897 // Save the file
898 EXPECT_TRUE(FPDFPage_GenerateContent(page));
899 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
900 UnloadPage(page);
901 FPDFPageObj_Destroy(page_object);
902
903 // Re-open the file and check the page object count is still 1.
904 OpenSavedDocument();
905 FPDF_PAGE saved_page = LoadSavedPage(0);
906 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
907 CloseSavedPage(saved_page);
908 CloseSavedDocument();
909}
910
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000911TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsNotLonely) {
912 // Load document with some text.
913 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
914 FPDF_PAGE page = LoadPage(0);
915 ASSERT_TRUE(page);
916
917 // Get the "Hello, world!" text object and remove it. There is another object
918 // in the same stream that says "Goodbye, world!"
919 ASSERT_EQ(3, FPDFPage_CountObjects(page));
920 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
921 ASSERT_TRUE(page_object);
922 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
923
924 // Verify the "Hello, world!" text is gone.
925 ASSERT_EQ(2, FPDFPage_CountObjects(page));
926#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
927 const char kHelloRemovedMD5[] = "e07a62d412728fc4d6e3ff42f2dd0e11";
928#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
929 const char kHelloRemovedMD5[] = "de37b0bb7ff903c1068bae361844be50";
930#else
931 const char kHelloRemovedMD5[] = "95b92950647a2190e1230911e7a1a0e9";
932#endif
933 {
934 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
935 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
936 }
937
938 // Save the file
939 EXPECT_TRUE(FPDFPage_GenerateContent(page));
940 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
941 UnloadPage(page);
942 FPDFPageObj_Destroy(page_object);
943
944 // Re-open the file and check the page object count is still 2.
945 OpenSavedDocument();
946 FPDF_PAGE saved_page = LoadSavedPage(0);
947
948 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
949 {
950 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
951 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
952 }
953
954 CloseSavedPage(saved_page);
955 CloseSavedDocument();
956}
957
958TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsLonely) {
959 // Load document with some text.
960 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
961 FPDF_PAGE page = LoadPage(0);
962 ASSERT_TRUE(page);
963
964 // Get the "Greetings, world!" text object and remove it. This is the only
965 // object in the stream.
966 ASSERT_EQ(3, FPDFPage_CountObjects(page));
967 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 2);
968 ASSERT_TRUE(page_object);
969 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
970
971 // Verify the "Greetings, world!" text is gone.
972 ASSERT_EQ(2, FPDFPage_CountObjects(page));
973#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
974 const char kGreetingsRemovedMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
975#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
976 const char kGreetingsRemovedMD5[] = "e5a6fa28298db07484cd922f3e210c88";
977#else
978 const char kGreetingsRemovedMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
979#endif
980 {
981 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
982 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
983 }
984
985 // Save the file
986 EXPECT_TRUE(FPDFPage_GenerateContent(page));
987 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
988 UnloadPage(page);
989 FPDFPageObj_Destroy(page_object);
990
991 // Re-open the file and check the page object count is still 2.
992 OpenSavedDocument();
993 FPDF_PAGE saved_page = LoadSavedPage(0);
994
995 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
996 {
997 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
998 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
999 }
1000
1001 CloseSavedPage(saved_page);
1002 CloseSavedDocument();
1003}
1004
Henrique Nakashima6eb79392018-06-12 20:27:35 +00001005TEST_F(FPDFEditEmbeddertest, GetContentStream) {
1006 // Load document with some text split across streams.
1007 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
1008 FPDF_PAGE page = LoadPage(0);
1009 ASSERT_TRUE(page);
1010
1011 // Content stream 0: page objects 0-14.
1012 // Content stream 1: page objects 15-17.
1013 // Content stream 2: page object 18.
1014 ASSERT_EQ(19, FPDFPage_CountObjects(page));
1015 for (int i = 0; i < 19; i++) {
1016 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1017 ASSERT_TRUE(page_object);
1018 CPDF_PageObject* cpdf_page_object =
1019 CPDFPageObjectFromFPDFPageObject(page_object);
1020 if (i < 15)
1021 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1022 else if (i < 18)
1023 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1024 else
1025 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
1026 }
1027
1028 UnloadPage(page);
1029}
1030
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001031TEST_F(FPDFEditEmbeddertest, RemoveAllFromStream) {
1032 // Load document with some text split across streams.
1033 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
1034 FPDF_PAGE page = LoadPage(0);
1035 ASSERT_TRUE(page);
1036
1037 // Content stream 0: page objects 0-14.
1038 // Content stream 1: page objects 15-17.
1039 // Content stream 2: page object 18.
1040 ASSERT_EQ(19, FPDFPage_CountObjects(page));
1041
1042 // Loop backwards because objects will being removed, which shifts the indexes
1043 // after the removed position.
1044 for (int i = 18; i >= 0; i--) {
1045 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1046 ASSERT_TRUE(page_object);
1047 CPDF_PageObject* cpdf_page_object =
1048 CPDFPageObjectFromFPDFPageObject(page_object);
1049
1050 // Empty content stream 1.
1051 if (cpdf_page_object->GetContentStream() == 1) {
1052 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
1053 FPDFPageObj_Destroy(page_object);
1054 }
1055 }
1056
1057 // Content stream 0: page objects 0-14.
1058 // Content stream 2: page object 15.
1059 ASSERT_EQ(16, FPDFPage_CountObjects(page));
1060 for (int i = 0; i < 16; i++) {
1061 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1062 ASSERT_TRUE(page_object);
1063 CPDF_PageObject* cpdf_page_object =
1064 CPDFPageObjectFromFPDFPageObject(page_object);
1065 if (i < 15)
1066 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1067 else
1068 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
1069 }
1070
1071 // Generate contents should remove the empty stream and update the page
1072 // objects' contents stream indexes.
1073 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1074
1075 // Content stream 0: page objects 0-14.
1076 // Content stream 1: page object 15.
1077 ASSERT_EQ(16, FPDFPage_CountObjects(page));
1078 for (int i = 0; i < 16; i++) {
1079 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1080 ASSERT_TRUE(page_object);
1081 CPDF_PageObject* cpdf_page_object =
1082 CPDFPageObjectFromFPDFPageObject(page_object);
1083 if (i < 15)
1084 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1085 else
1086 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1087 }
1088
1089#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1090 const char kStream1RemovedMD5[] = "d2e21fbd5a6de563f619feeeb6163331";
1091#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1092 const char kStream1RemovedMD5[] = "b4140f203523e38793283a5943d8075b";
1093#else
1094 const char kStream1RemovedMD5[] = "e86a3efc160ede6cfcb1f59bcacf1105";
1095#endif
1096 {
1097 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1098 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
1099 }
1100
1101 // Save the file
1102 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1103 UnloadPage(page);
1104
1105 // Re-open the file and check the page object count is still 16, and that
1106 // content stream 1 was removed.
1107 OpenSavedDocument();
1108 FPDF_PAGE saved_page = LoadSavedPage(0);
1109
1110 // Content stream 0: page objects 0-14.
1111 // Content stream 1: page object 15.
1112 EXPECT_EQ(16, FPDFPage_CountObjects(saved_page));
1113 for (int i = 0; i < 16; i++) {
1114 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
1115 ASSERT_TRUE(page_object);
1116 CPDF_PageObject* cpdf_page_object =
1117 CPDFPageObjectFromFPDFPageObject(page_object);
1118 if (i < 15)
1119 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1120 else
1121 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1122 }
1123
1124 {
1125 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1126 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
1127 }
1128
1129 CloseSavedPage(saved_page);
1130 CloseSavedDocument();
1131}
1132
1133TEST_F(FPDFEditEmbeddertest, RemoveAllFromSingleStream) {
1134 // Load document with a single stream.
1135 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1136 FPDF_PAGE page = LoadPage(0);
1137 ASSERT_TRUE(page);
1138
1139 // Content stream 0: page objects 0-1.
1140 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1141
1142 // Loop backwards because objects will being removed, which shifts the indexes
1143 // after the removed position.
1144 for (int i = 1; i >= 0; i--) {
1145 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1146 ASSERT_TRUE(page_object);
1147 CPDF_PageObject* cpdf_page_object =
1148 CPDFPageObjectFromFPDFPageObject(page_object);
1149 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1150 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1151 FPDFPageObj_Destroy(page_object);
1152 }
1153
1154 // No more objects in the stream
1155 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1156
1157 // Generate contents should remove the empty stream and update the page
1158 // objects' contents stream indexes.
1159 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1160
1161 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1162
1163 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1164 {
1165 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1166 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1167 }
1168
1169 // Save the file
1170 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1171 UnloadPage(page);
1172
1173 // Re-open the file and check the page object count is still 0.
1174 OpenSavedDocument();
1175 FPDF_PAGE saved_page = LoadSavedPage(0);
1176
1177 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1178 {
1179 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1180 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1181 }
1182
1183 CloseSavedPage(saved_page);
1184 CloseSavedDocument();
1185}
1186
1187TEST_F(FPDFEditEmbeddertest, RemoveFirstFromSingleStream) {
1188 // Load document with a single stream.
1189 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1190 FPDF_PAGE page = LoadPage(0);
1191 ASSERT_TRUE(page);
1192
1193 // Content stream 0: page objects 0-1.
1194 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1195
1196 // Remove first object.
1197 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
1198 ASSERT_TRUE(page_object);
1199 CPDF_PageObject* cpdf_page_object =
1200 CPDFPageObjectFromFPDFPageObject(page_object);
1201 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1202 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1203 FPDFPageObj_Destroy(page_object);
1204
1205 // One object left in the stream.
1206 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1207 page_object = FPDFPage_GetObject(page, 0);
1208 ASSERT_TRUE(page_object);
1209 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1210 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1211
1212 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1213
1214 // Still one object left in the stream.
1215 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1216 page_object = FPDFPage_GetObject(page, 0);
1217 ASSERT_TRUE(page_object);
1218 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1219 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1220
1221#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1222 const char kFirstRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
1223#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1224 const char kFirstRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
1225#else
1226 const char kFirstRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
1227#endif
1228 {
1229 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1230 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1231 }
1232
1233 // Save the file
1234 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1235 UnloadPage(page);
1236
1237 // Re-open the file and check the page object count is still 0.
1238 OpenSavedDocument();
1239 FPDF_PAGE saved_page = LoadSavedPage(0);
1240
1241 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1242 page_object = FPDFPage_GetObject(saved_page, 0);
1243 ASSERT_TRUE(page_object);
1244 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1245 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1246 {
1247 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1248 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1249 }
1250
1251 CloseSavedPage(saved_page);
1252 CloseSavedDocument();
1253}
1254
1255TEST_F(FPDFEditEmbeddertest, RemoveLastFromSingleStream) {
1256 // Load document with a single stream.
1257 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1258 FPDF_PAGE page = LoadPage(0);
1259 ASSERT_TRUE(page);
1260
1261 // Content stream 0: page objects 0-1.
1262 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1263
1264 // Remove last object
1265 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 1);
1266 ASSERT_TRUE(page_object);
1267 CPDF_PageObject* cpdf_page_object =
1268 CPDFPageObjectFromFPDFPageObject(page_object);
1269 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1270 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1271 FPDFPageObj_Destroy(page_object);
1272
1273 // One object left in the stream.
1274 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1275 page_object = FPDFPage_GetObject(page, 0);
1276 ASSERT_TRUE(page_object);
1277 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1278 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1279
1280 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1281
1282 // Still one object left in the stream.
1283 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1284 page_object = FPDFPage_GetObject(page, 0);
1285 ASSERT_TRUE(page_object);
1286 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1287 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1288
1289#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1290 const char kLastRemovedMD5[] = "f8fbd14a048b9e2ea8e5f059f22a910e";
1291#else
1292 const char kLastRemovedMD5[] = "93dcc09055f87a2792c8e3065af99a1b";
1293#endif
1294 {
1295 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1296 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1297 }
1298
1299 // Save the file
1300 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1301 UnloadPage(page);
1302
1303 // Re-open the file and check the page object count is still 0.
1304 OpenSavedDocument();
1305 FPDF_PAGE saved_page = LoadSavedPage(0);
1306
1307 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1308 page_object = FPDFPage_GetObject(saved_page, 0);
1309 ASSERT_TRUE(page_object);
1310 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1311 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1312 {
1313 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1314 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1315 }
1316
1317 CloseSavedPage(saved_page);
1318 CloseSavedDocument();
1319}
1320
1321TEST_F(FPDFEditEmbeddertest, RemoveAllFromMultipleStreams) {
1322 // Load document with some text.
1323 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
1324 FPDF_PAGE page = LoadPage(0);
1325 ASSERT_TRUE(page);
1326
1327 // Content stream 0: page objects 0-1.
1328 // Content stream 1: page object 2.
1329 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1330
1331 // Loop backwards because objects will being removed, which shifts the indexes
1332 // after the removed position.
1333 for (int i = 2; i >= 0; i--) {
1334 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1335 ASSERT_TRUE(page_object);
1336 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1337 FPDFPageObj_Destroy(page_object);
1338 }
1339
1340 // No more objects in the page.
1341 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1342
1343 // Generate contents should remove the empty streams and update the page
1344 // objects' contents stream indexes.
1345 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1346
1347 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1348
1349 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1350 {
1351 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1352 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1353 }
1354
1355 // Save the file
1356 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1357 UnloadPage(page);
1358
1359 // Re-open the file and check the page object count is still 0.
1360 OpenSavedDocument();
1361 FPDF_PAGE saved_page = LoadSavedPage(0);
1362
1363 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1364 {
1365 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1366 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1367 }
1368
1369 CloseSavedPage(saved_page);
1370 CloseSavedDocument();
1371}
1372
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001373TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) {
1374 // Load document with some text.
1375 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1376 FPDF_PAGE page = LoadPage(0);
1377 ASSERT_TRUE(page);
1378
1379 // Add a red rectangle.
1380 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1381 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1382 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1383 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1384 FPDFPage_InsertObject(page, red_rect);
1385
1386 // Verify the red rectangle was added.
1387 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1388
1389 // Save the file
1390 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1391 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1392 UnloadPage(page);
1393
1394 // Re-open the file and check the page object count is still 3.
1395 OpenSavedDocument();
1396 FPDF_PAGE saved_page = LoadSavedPage(0);
1397 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1398 CloseSavedPage(saved_page);
1399 CloseSavedDocument();
1400}
1401
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001402TEST_F(FPDFEditEmbeddertest, InsertPageObjectEditAndSave) {
1403 // Load document with some text.
1404 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1405 FPDF_PAGE page = LoadPage(0);
1406 ASSERT_TRUE(page);
1407
1408 // Add a red rectangle.
1409 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1410 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1411 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 100, 100, 255));
1412 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1413 FPDFPage_InsertObject(page, red_rect);
1414
1415 // Verify the red rectangle was added.
1416 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1417
1418 // Generate content but change it again
1419 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1420 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1421
1422 // Save the file
1423 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1424 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1425 UnloadPage(page);
1426
1427 // Re-open the file and check the page object count is still 3.
1428 OpenSavedDocument();
1429 FPDF_PAGE saved_page = LoadSavedPage(0);
1430 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1431 CloseSavedPage(saved_page);
1432 CloseSavedDocument();
1433}
1434
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001435TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) {
1436 // Start with a blank page.
1437 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1438 ASSERT_TRUE(page);
1439
1440 // Render the blank page and verify it's a blank bitmap.
1441 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
1442 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001443 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001444 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1445 }
1446 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1447
1448 // Add a red rectangle.
1449 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1450 ASSERT_TRUE(red_rect);
1451 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1452 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1453 FPDFPage_InsertObject(page, red_rect);
1454 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
1455 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001456 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001457 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
1458 }
1459 EXPECT_EQ(1, FPDFPage_CountObjects(page));
1460
1461 // Remove rectangle and verify it does not render anymore and the bitmap is
1462 // back to a blank one.
1463 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
1464 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001465 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001466 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1467 }
1468 EXPECT_EQ(0, FPDFPage_CountObjects(page));
1469
1470 // Trying to remove an object not in the page should return false.
1471 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
1472
1473 FPDF_ClosePage(page);
1474 FPDFPageObj_Destroy(red_rect);
1475}
1476
Miklos Vajna12abfd02017-09-15 07:49:03 +02001477TEST_F(FPDFEditEmbeddertest, PathsPoints) {
1478 CreateNewDocument();
1479 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
1480 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +02001481 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001482
1483 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +02001484 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001485
Miklos Vajna36eed872017-09-20 22:52:43 +02001486 // FPDFPath_GetPathSegment() with a non-path.
1487 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
1488 // FPDFPath_GetPathSegment() with a NULL path.
1489 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
1490 float x;
1491 float y;
1492 // FPDFPathSegment_GetPoint() with a NULL segment.
1493 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
1494
1495 // FPDFPathSegment_GetType() with a NULL segment.
1496 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
1497
1498 // FPDFPathSegment_GetClose() with a NULL segment.
1499 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
1500
Miklos Vajna12abfd02017-09-15 07:49:03 +02001501 FPDFPageObj_Destroy(img);
1502}
1503
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001504TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
1505 // Load document with some text
1506 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1507 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001508 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001509
1510 // Add an opaque rectangle on top of some of the text.
1511 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1512 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1513 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1514 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001515
1516 // Add a transparent triangle on top of other part of the text.
1517 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
1518 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
1519 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
1520 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
1521 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
1522 EXPECT_TRUE(FPDFPath_Close(black_path));
1523 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001524
1525 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001526 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001527#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -07001528 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001529#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001530 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001531#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001532 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001533 UnloadPage(page);
1534}
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001535
wileyryae858aa42017-05-31 14:49:05 -05001536TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
1537 // Load document with existing content
1538 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
1539 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001540 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -05001541
1542 // Add a transparent rectangle on top of the existing content
1543 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
1544 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
1545 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
1546 FPDFPage_InsertObject(page, red_rect2);
1547
1548 // Add an opaque rectangle on top of the existing content
1549 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
1550 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1551 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1552 FPDFPage_InsertObject(page, red_rect);
1553
Tom Sepeze08d2b12018-04-25 18:49:32 +00001554 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001555 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001556 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1557
1558 // Now save the result, closing the page and document
1559 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -04001560 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -05001561
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001562 OpenSavedDocument();
Lei Zhang107fa7b2018-02-09 21:48:15 +00001563 FPDF_PAGE saved_page = LoadSavedPage(0);
1564 VerifySavedRendering(saved_page, 612, 792,
1565 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001566
1567 ClearString();
1568 // Add another opaque rectangle on top of the existing content
1569 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
1570 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
1571 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001572 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -05001573
1574 // Add another transparent rectangle on top of existing content
1575 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
1576 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
1577 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001578 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001579 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
1580 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001581 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001582 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
1583 }
Lei Zhang107fa7b2018-02-09 21:48:15 +00001584 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -05001585
1586 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +00001587 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001588
Lei Zhang107fa7b2018-02-09 21:48:15 +00001589 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001590 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -05001591
1592 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +00001593 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -05001594}
1595
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001596TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
1597 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001598 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001599
1600 // Add a large stroked rectangle (fill color should not affect it).
1601 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
1602 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
1603 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
1604 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
Miklos Vajna366df7f2018-05-22 14:27:29 +00001605
1606 float width = 0;
1607 EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width));
1608 EXPECT_EQ(15.0f, width);
1609
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001610 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
1611 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001612 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001613 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001614 CompareBitmap(page_bitmap.get(), 612, 792,
1615 "64bd31f862a89e0a9e505a5af6efd506");
1616 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001617
1618 // Add crossed-checkmark
1619 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
1620 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
1621 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
1622 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
1623 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
1624 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
1625 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
1626 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1627 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001628 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001629 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001630 CompareBitmap(page_bitmap.get(), 612, 792,
1631 "4b6f3b9d25c4e194821217d5016c3724");
1632 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001633
1634 // Add stroked and filled oval-ish path.
1635 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
1636 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
1637 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
1638 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
1639 EXPECT_TRUE(FPDFPath_Close(path));
1640 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
1641 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
1642 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
1643 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
1644 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001645 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001646 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001647 CompareBitmap(page_bitmap.get(), 612, 792,
1648 "ff3e6a22326754944cc6e56609acd73b");
1649 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001650 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001651}
Nicolas Pena49058402017-02-14 18:26:20 -05001652
Nicolas Pena4c48b102018-06-13 18:23:46 +00001653// Tests adding text from standard font using FPDFPageObj_NewTextObj.
Nicolas Pena49058402017-02-14 18:26:20 -05001654TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
1655 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001656 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -05001657
1658 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001659 FPDF_PAGEOBJECT text_object1 =
1660 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1661 EXPECT_TRUE(text_object1);
1662 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
1663 GetFPDFWideString(L"I'm at the bottom of the page");
1664 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
1665 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
1666 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001667 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001668 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001669#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001670 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -05001671#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001672 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001673#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001674 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1675 }
Nicolas Pena49058402017-02-14 18:26:20 -05001676
1677 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -04001678 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001679 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001680 EXPECT_TRUE(text_object2);
1681 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1682 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
1683 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1684 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
1685 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001686 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001687 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001688#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +00001689 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -04001690#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +00001691 const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d";
Nicolas Pena49058402017-02-14 18:26:20 -05001692#else
Dan Sinclair971a6742018-03-28 19:23:25 +00001693 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001694#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001695 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001696 }
Nicolas Pena49058402017-02-14 18:26:20 -05001697
1698 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -04001699 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001700 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001701 EXPECT_TRUE(text_object3);
1702 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
1703 GetFPDFWideString(L"Can you read me? <:)>");
1704 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
1705 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
1706 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001707 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001708 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001709#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001710 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -04001711#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001712 const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2";
Nicolas Pena49058402017-02-14 18:26:20 -05001713#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001714 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001715#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001716 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
1717 }
Nicolas Pena49058402017-02-14 18:26:20 -05001718
Miklos Vajnac765d2a2018-06-19 15:45:42 +00001719 double matrix_a = 0;
1720 double matrix_b = 0;
1721 double matrix_c = 0;
1722 double matrix_d = 0;
1723 double matrix_e = 0;
1724 double matrix_f = 0;
1725 EXPECT_FALSE(FPDFText_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1726 &matrix_d, &matrix_e, &matrix_f));
1727 EXPECT_TRUE(FPDFText_GetMatrix(text_object3, &matrix_a, &matrix_b, &matrix_c,
1728 &matrix_d, &matrix_e, &matrix_f));
1729 EXPECT_EQ(1., matrix_a);
1730 EXPECT_EQ(1.5, matrix_b);
1731 EXPECT_EQ(2., matrix_c);
1732 EXPECT_EQ(0.5, matrix_d);
1733 EXPECT_EQ(200., matrix_e);
1734 EXPECT_EQ(200., matrix_f);
1735
Miklos Vajna8625d3b2018-06-26 15:12:48 +00001736 EXPECT_EQ(0, FPDFTextObj_GetFontSize(nullptr));
1737 EXPECT_EQ(20, FPDFTextObj_GetFontSize(text_object3));
1738
Nicolas Pena49058402017-02-14 18:26:20 -05001739 // TODO(npm): Why are there issues with text rotated by 90 degrees?
1740 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
1741 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -05001742}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001743
Miklos Vajna1448cc12018-07-03 13:52:33 +00001744TEST_F(FPDFEditEmbeddertest, TestGetTextRenderMode) {
1745 EXPECT_TRUE(OpenDocument("text_render_mode.pdf"));
1746 FPDF_PAGE page = LoadPage(0);
1747 ASSERT_TRUE(page);
1748 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1749
1750 ASSERT_EQ(-1, FPDFText_GetTextRenderMode(nullptr));
1751
1752 FPDF_PAGEOBJECT fill = FPDFPage_GetObject(page, 0);
1753 ASSERT_EQ(FPDF_TEXTRENDERMODE_FILL, FPDFText_GetTextRenderMode(fill));
1754
1755 FPDF_PAGEOBJECT stroke = FPDFPage_GetObject(page, 1);
1756 ASSERT_EQ(FPDF_TEXTRENDERMODE_STROKE, FPDFText_GetTextRenderMode(stroke));
1757
1758 UnloadPage(page);
1759}
1760
Miklos Vajnab66077d2018-07-11 13:25:02 +00001761TEST_F(FPDFEditEmbeddertest, TestFormGetObjects) {
1762 EXPECT_TRUE(OpenDocument("form_object.pdf"));
1763 FPDF_PAGE page = LoadPage(0);
1764 ASSERT_TRUE(page);
1765 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1766
1767 FPDF_PAGEOBJECT form = FPDFPage_GetObject(page, 0);
1768 EXPECT_EQ(FPDF_PAGEOBJ_FORM, FPDFPageObj_GetType(form));
1769 ASSERT_EQ(-1, FPDFFormObj_CountObjects(nullptr));
1770 ASSERT_EQ(2, FPDFFormObj_CountObjects(form));
1771
Miklos Vajna1d273f12018-07-16 19:20:36 +00001772 // FPDFFormObj_GetObject() positive testing.
1773 FPDF_PAGEOBJECT text1 = FPDFFormObj_GetObject(form, 0);
1774 ASSERT_TRUE(text1);
1775 float left = 0;
1776 float bottom = 0;
1777 float right = 0;
1778 float top = 0;
1779 ASSERT_TRUE(FPDFPageObj_GetBounds(text1, &left, &bottom, &right, &top));
1780 ASSERT_EQ(271, static_cast<int>(top));
1781
1782 FPDF_PAGEOBJECT text2 = FPDFFormObj_GetObject(form, 1);
1783 ASSERT_TRUE(text2);
1784 ASSERT_TRUE(FPDFPageObj_GetBounds(text2, &left, &bottom, &right, &top));
1785 ASSERT_EQ(221, static_cast<int>(top));
1786
1787 // FPDFFormObj_GetObject() negative testing.
1788 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(nullptr, 0));
1789 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(form, -1));
1790 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(form, 2));
1791
Miklos Vajnab66077d2018-07-11 13:25:02 +00001792 UnloadPage(page);
1793}
1794
Nicolas Pena4c48b102018-06-13 18:23:46 +00001795// Tests adding text from standard font using FPDFText_LoadStandardFont.
1796TEST_F(FPDFEditEmbeddertest, AddStandardFontText2) {
1797 // Start with a blank page
1798 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
1799
1800 // Load a standard font.
1801 FPDF_FONT font = FPDFText_LoadStandardFont(document(), "Helvetica");
1802 ASSERT_TRUE(font);
1803
1804 // Add some text to the page.
1805 FPDF_PAGEOBJECT text_object =
1806 FPDFPageObj_CreateTextObj(document(), font, 12.0f);
1807 EXPECT_TRUE(text_object);
1808 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1809 GetFPDFWideString(L"I'm at the bottom of the page");
1810 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1811 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20);
1812 FPDFPage_InsertObject(page.get(), text_object);
1813 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page.get(), nullptr, 0);
1814#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1815 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
1816#else
1817 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
1818#endif
1819 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1820}
1821
1822TEST_F(FPDFEditEmbeddertest, LoadStandardFonts) {
1823 CreateNewDocument();
1824 const char* standard_font_names[] = {"Arial",
1825 "Arial-Bold",
1826 "Arial-BoldItalic",
1827 "Arial-Italic",
1828 "Courier",
1829 "Courier-BoldOblique",
1830 "Courier-Oblique",
1831 "Courier-Bold",
1832 "CourierNew",
1833 "CourierNew-Bold",
1834 "CourierNew-BoldItalic",
1835 "CourierNew-Italic",
1836 "Helvetica",
1837 "Helvetica-Bold",
1838 "Helvetica-BoldOblique",
1839 "Helvetica-Oblique",
1840 "Symbol",
1841 "TimesNewRoman",
1842 "TimesNewRoman-Bold",
1843 "TimesNewRoman-BoldItalic",
1844 "TimesNewRoman-Italic",
1845 "ZapfDingbats"};
1846 for (auto* const font_name : standard_font_names) {
1847 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
1848 EXPECT_TRUE(font) << font_name << " should be considered a standard font.";
1849 }
1850 const char* not_standard_font_names[] = {
1851 "Abcdefg", "ArialB", "Arial-Style",
1852 "Font Name", "FontArial", "NotAStandardFontName",
1853 "TestFontName", "Quack", "Symbol-Italic",
1854 "Zapf"};
1855 for (auto* const font_name : not_standard_font_names) {
1856 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
1857 EXPECT_FALSE(font) << font_name
1858 << " should not be considered a standard font.";
1859 }
1860}
1861
Nicolas Pena603a31d2017-06-14 11:41:18 -04001862TEST_F(FPDFEditEmbeddertest, GraphicsData) {
1863 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +00001864 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001865
1866 // Create a rect with nontrivial graphics
1867 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1868 FPDFPageObj_SetBlendMode(rect1, "Color");
1869 FPDFPage_InsertObject(page.get(), rect1);
1870 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
1871
1872 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +00001873 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
1874 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -04001875 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001876 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001877
1878 // Add a text object causing no change to the graphics dictionary
1879 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1880 // Only alpha, the last component, matters for the graphics dictionary. And
1881 // the default value is 255.
1882 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
1883 FPDFPage_InsertObject(page.get(), text1);
1884 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001885 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001886
1887 // Add a text object increasing the size of the graphics dictionary
1888 FPDF_PAGEOBJECT text2 =
1889 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
1890 FPDFPage_InsertObject(page.get(), text2);
1891 FPDFPageObj_SetBlendMode(text2, "Darken");
1892 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
1893 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001894 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001895
1896 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -04001897 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -04001898 FPDFPageObj_SetBlendMode(path, "Darken");
1899 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
1900 FPDFPage_InsertObject(page.get(), path);
1901 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001902 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001903
1904 // Add a rect increasing the size of the graphics dictionary
1905 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1906 FPDFPageObj_SetBlendMode(rect2, "Darken");
1907 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
1908 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
1909 FPDFPage_InsertObject(page.get(), rect2);
1910 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001911 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001912}
1913
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001914TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
1915 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001916 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001917
1918 // Add a red rectangle with some non-default alpha
1919 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1920 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
1921 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
1922 FPDFPage_InsertObject(page, rect);
1923 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1924
1925 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +00001926 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
1927 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001928 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001929 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001930
1931 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +00001932 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001933 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001934 CompareBitmap(page_bitmap.get(), 612, 792,
1935 "5384da3406d62360ffb5cac4476fff1c");
1936 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001937
1938 // Never mind, my new favorite color is blue, increase alpha
1939 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
1940 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001941 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001942
1943 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +00001944 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001945 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001946 CompareBitmap(page_bitmap.get(), 612, 792,
1947 "2e51656f5073b0bee611d9cd086aa09c");
1948 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001949
1950 // And now generate, without changes
1951 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001952 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001953 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001954 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001955 CompareBitmap(page_bitmap.get(), 612, 792,
1956 "2e51656f5073b0bee611d9cd086aa09c");
1957 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001958
1959 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001960 FPDF_PAGEOBJECT text_object =
1961 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1962 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1963 GetFPDFWideString(L"Something something #text# something");
1964 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1965 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
1966 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001967 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001968 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001969 ASSERT_TRUE(font_dict);
1970 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1971
1972 // Generate yet again, check dicts are reasonably sized
1973 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001974 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001975 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1976 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001977}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001978
Nicolas Penad03ca422017-03-06 13:54:33 -05001979TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
1980 CreateNewDocument();
1981 // TODO(npm): use other fonts after disallowing loading any font as any type
1982 const CPDF_Font* stock_font =
1983 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001984 const uint8_t* data = stock_font->GetFont()->GetFontData();
1985 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001986 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001987 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
1988 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001989 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001990 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001991
Lei Zhang710fa992018-05-25 16:24:48 +00001992 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05001993 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1994 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
1995 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
1996 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1997 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1998 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05001999 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
2000
Lei Zhangde579ab2018-05-25 21:49:49 +00002001 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05002002 ASSERT_TRUE(widths_array);
2003 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002004 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05002005 EXPECT_EQ(569, widths_array->GetNumberAt(11));
2006 EXPECT_EQ(500, widths_array->GetNumberAt(223));
2007 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
2008}
Nicolas Penabe90aae2017-02-27 10:41:41 -05002009
Nicolas Penad03ca422017-03-06 13:54:33 -05002010TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
2011 CreateNewDocument();
2012 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -07002013 const uint8_t* data = stock_font->GetFont()->GetFontData();
2014 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00002015 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04002016 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
2017 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002018 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002019 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002020
Lei Zhang710fa992018-05-25 16:24:48 +00002021 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002022 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2023 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
2024 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
2025 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
2026 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
2027 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
2028 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05002029
Lei Zhangde579ab2018-05-25 21:49:49 +00002030 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05002031 ASSERT_TRUE(widths_array);
2032 ASSERT_EQ(224U, widths_array->GetCount());
2033 EXPECT_EQ(600, widths_array->GetNumberAt(33));
2034 EXPECT_EQ(600, widths_array->GetNumberAt(74));
2035 EXPECT_EQ(600, widths_array->GetNumberAt(223));
2036 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
2037}
2038
2039TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
2040 CreateNewDocument();
2041 const CPDF_Font* stock_font =
2042 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -07002043 const uint8_t* data = stock_font->GetFont()->GetFontData();
2044 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00002045 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04002046 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
2047 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002048 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002049 EXPECT_TRUE(typed_font->IsCIDFont());
2050
2051 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00002052 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002053 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2054 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
2055 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
2056 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00002057 const CPDF_Array* descendant_array =
2058 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05002059 ASSERT_TRUE(descendant_array);
2060 EXPECT_EQ(1U, descendant_array->GetCount());
2061
2062 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00002063 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05002064 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
2065 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
2066 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00002067 const CPDF_Dictionary* cidinfo_dict =
2068 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05002069 ASSERT_TRUE(cidinfo_dict);
2070 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
2071 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
2072 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
2073 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
2074
2075 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00002076 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05002077 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -04002078 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -05002079 CheckCompositeFontWidths(widths_array, typed_font);
2080}
2081
2082TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
2083 CreateNewDocument();
2084 const CPDF_Font* stock_font =
2085 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -07002086 const uint8_t* data = stock_font->GetFont()->GetFontData();
2087 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -05002088
Tom Sepeze08d2b12018-04-25 18:49:32 +00002089 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04002090 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
2091 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002092 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002093 EXPECT_TRUE(typed_font->IsCIDFont());
2094
2095 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00002096 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002097 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2098 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
2099 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
2100 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00002101 const CPDF_Array* descendant_array =
2102 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05002103 ASSERT_TRUE(descendant_array);
2104 EXPECT_EQ(1U, descendant_array->GetCount());
2105
2106 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00002107 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05002108 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
2109 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
2110 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00002111 const CPDF_Dictionary* cidinfo_dict =
2112 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05002113 ASSERT_TRUE(cidinfo_dict);
2114 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
2115 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
2116 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
2117 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
2118 data);
2119
2120 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00002121 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05002122 ASSERT_TRUE(widths_array);
2123 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05002124}
rbpotterce8e51e2017-04-28 12:42:47 -07002125
2126TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
2127 // Load document with a -90 degree rotation
2128 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
2129 FPDF_PAGE page = LoadPage(0);
2130 EXPECT_NE(nullptr, page);
2131
2132 EXPECT_EQ(3, FPDFPage_GetRotation(page));
2133 UnloadPage(page);
2134}
Nicolas Penab3161852017-05-02 14:12:50 -04002135
2136TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
2137 // Start with a blank page
2138 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2139 {
2140 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -07002141 const uint8_t* data = stock_font->GetFont()->GetFontData();
2142 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00002143 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04002144 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
2145 ASSERT_TRUE(font.get());
2146
2147 // Add some text to the page
2148 FPDF_PAGEOBJECT text_object =
2149 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2150 EXPECT_TRUE(text_object);
2151 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2152 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2153 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2154 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2155 FPDFPage_InsertObject(page, text_object);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002156 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04002157#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002158 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
2159#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002160 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04002161#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002162 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04002163
2164 // Add some more text, same font
2165 FPDF_PAGEOBJECT text_object2 =
2166 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
2167 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2168 GetFPDFWideString(L"Bigger font size");
2169 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2170 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
2171 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04002172 }
Tom Sepeze08d2b12018-04-25 18:49:32 +00002173 ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04002174#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002175 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
2176#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002177 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04002178#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002179 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002180
Nicolas Pena207b7272017-05-26 17:37:06 -04002181 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04002182 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2183 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002184
2185 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002186}
Nicolas Penaf45ade32017-05-03 10:23:49 -04002187
Jane Liueda65252017-06-07 11:31:27 -04002188TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
2189 // Open a file with one annotation and load its first page.
2190 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00002191 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04002192 ASSERT_TRUE(page);
2193
Lei Zhanga21d5932018-02-05 18:28:38 +00002194 {
2195 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00002196 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00002197 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
2198 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04002199
Lei Zhanga21d5932018-02-05 18:28:38 +00002200 // FPDFPage_TransformAnnots() should run without errors when modifying
2201 // annotation rectangles.
2202 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
2203 }
Jane Liueda65252017-06-07 11:31:27 -04002204 UnloadPage(page);
2205}
2206
Nicolas Penaf45ade32017-05-03 10:23:49 -04002207// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04002208#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -04002209TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
2210 // Start with a blank page
2211 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2212 CFX_Font CIDfont;
2213 {
2214 // First, get the data from the font
2215 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
2216 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
2217 const uint8_t* data = CIDfont.GetFontData();
2218 const uint32_t size = CIDfont.GetSize();
2219
2220 // Load the data into a FPDF_Font.
Tom Sepeze08d2b12018-04-25 18:49:32 +00002221 ScopedFPDFFont font(
Nicolas Penaf45ade32017-05-03 10:23:49 -04002222 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
2223 ASSERT_TRUE(font.get());
2224
2225 // Add some text to the page
2226 FPDF_PAGEOBJECT text_object =
2227 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2228 ASSERT_TRUE(text_object);
2229 std::wstring wstr = L"ABCDEFGhijklmnop.";
2230 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2231 GetFPDFWideString(wstr);
2232 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2233 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
2234 FPDFPage_InsertObject(page, text_object);
2235
2236 // And add some Japanese characters
2237 FPDF_PAGEOBJECT text_object2 =
2238 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
2239 ASSERT_TRUE(text_object2);
2240 std::wstring wstr2 =
2241 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
2242 L"\u756A";
2243 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2244 GetFPDFWideString(wstr2);
2245 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2246 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
2247 FPDFPage_InsertObject(page, text_object2);
2248 }
2249
Nicolas Pena207b7272017-05-26 17:37:06 -04002250 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00002251 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00002252 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002253 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002254 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2255 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04002256
2257 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04002258 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04002259 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2260 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002261
2262 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04002263}
Dan Sinclair698aed72017-09-26 16:24:49 -04002264#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002265
2266TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04002267 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002268 {
2269 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
2270 FPDF_PAGE page = LoadPage(0);
2271 ASSERT_NE(nullptr, page);
2272
2273 // Now add a more complex blue path.
2274 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
2275 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
2276 // TODO(npm): stroking will cause the MD5s to differ.
2277 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
2278 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
2279 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
2280 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
2281 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
2282 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
2283 EXPECT_TRUE(FPDFPath_Close(green_path));
2284 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002285 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002286 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002287
2288 // Now save the result, closing the page and document
2289 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2290 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2291 UnloadPage(page);
2292 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002293
2294 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002295}
Jane Liu28fb7ba2017-08-02 21:45:57 -04002296
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002297TEST_F(FPDFEditEmbeddertest, AddMark) {
2298 // Load document with some text.
2299 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2300 FPDF_PAGE page = LoadPage(0);
2301 ASSERT_TRUE(page);
2302
2303 constexpr int kExpectedObjectCount = 19;
2304 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2305
2306 // Add to the first page object a "Bounds" mark with "Position": "First".
2307 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
2308 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(page_object, "Bounds");
2309 EXPECT_TRUE(mark);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002310 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2311 "Position", "First"));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002312
2313 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2314
2315 // Save the file
2316 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2317 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2318 UnloadPage(page);
2319
2320 // Re-open the file and check the new mark is present.
2321 OpenSavedDocument();
2322 FPDF_PAGE saved_page = LoadSavedPage(0);
2323
2324 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2325
2326 CloseSavedPage(saved_page);
2327 CloseSavedDocument();
2328}
2329
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002330TEST_F(FPDFEditEmbeddertest, SetMarkParam) {
2331 // Load document with some text.
2332 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2333 FPDF_PAGE page = LoadPage(0);
2334 ASSERT_TRUE(page);
2335
2336 constexpr int kExpectedObjectCount = 19;
2337 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2338
2339 // Check the "Bounds" mark's "Position" param is "Last".
2340 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2341 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2342 ASSERT_TRUE(mark);
2343 char buffer[256];
2344 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
2345 ASSERT_EQ(L"Bounds",
2346 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2347 unsigned long out_buffer_len;
2348 ASSERT_TRUE(FPDFPageObjMark_GetParamStringValue(
2349 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2350 ASSERT_EQ(L"Last",
2351 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2352
2353 // Set is to "End".
2354 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2355 "Position", "End"));
2356
2357 // Verify the object passed must correspond to the mark passed.
2358 FPDF_PAGEOBJECT another_page_object = FPDFPage_GetObject(page, 17);
2359 EXPECT_FALSE(FPDFPageObjMark_SetStringParam(document(), another_page_object,
2360 mark, "Position", "End"));
2361
2362 // Verify nothing else changed.
2363 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2364
2365 // Verify "Position" now maps to "End".
2366 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2367 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2368 EXPECT_EQ(L"End",
2369 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2370
2371 // Save the file
2372 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2373 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2374 UnloadPage(page);
2375
2376 // Re-open the file and cerify "Position" still maps to "End".
2377 OpenSavedDocument();
2378 FPDF_PAGE saved_page = LoadSavedPage(0);
2379
2380 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2381 page_object = FPDFPage_GetObject(saved_page, 18);
2382 mark = FPDFPageObj_GetMark(page_object, 1);
2383 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2384 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2385 EXPECT_EQ(L"End",
2386 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2387
2388 CloseSavedPage(saved_page);
2389 CloseSavedDocument();
2390}
2391
Henrique Nakashima144107d2018-07-10 21:04:05 +00002392TEST_F(FPDFEditEmbeddertest, AddMarkedText) {
2393 // Start with a blank page.
2394 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2395
2396 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
2397 const uint8_t* data = stock_font->GetFont()->GetFontData();
2398 const uint32_t size = stock_font->GetFont()->GetSize();
2399 ScopedFPDFFont font(
2400 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
2401 ASSERT_TRUE(font.get());
2402
2403 // Add some text to the page.
2404 FPDF_PAGEOBJECT text_object =
2405 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2406
2407 EXPECT_TRUE(text_object);
2408 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
2409 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2410 EXPECT_TRUE(FPDFText_SetText(text_object, text1.get()));
2411 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2412 FPDFPage_InsertObject(page, text_object);
2413
2414 // Add a mark with the tag "TestMarkName" to that text.
2415 EXPECT_EQ(0, FPDFPageObj_CountMarks(text_object));
2416 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(text_object, "TestMarkName");
2417 EXPECT_TRUE(mark);
2418 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2419 EXPECT_EQ(mark, FPDFPageObj_GetMark(text_object, 0));
2420 char buffer[256];
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002421 EXPECT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002422 std::wstring name =
2423 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2424 EXPECT_EQ(L"TestMarkName", name);
2425
2426 // Add parameters:
2427 // - int "IntKey" : 42
2428 // - string "StringKey": "StringValue"
Henrique Nakashima07520f62018-07-12 19:45:29 +00002429 // - blob "BlobKey": "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0"
2430 constexpr const size_t kBlobLen = 28;
2431 char kBlobValue[kBlobLen];
2432 memcpy(kBlobValue, "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0", kBlobLen);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002433 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002434 EXPECT_TRUE(
2435 FPDFPageObjMark_SetIntParam(document(), text_object, mark, "IntKey", 42));
2436 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), text_object, mark,
2437 "StringKey", "StringValue"));
2438 EXPECT_TRUE(FPDFPageObjMark_SetBlobParam(document(), text_object, mark,
2439 "BlobKey", kBlobValue, kBlobLen));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002440 EXPECT_EQ(3, FPDFPageObjMark_CountParams(mark));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002441
2442 // Check the two parameters can be retrieved.
2443 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002444 FPDFPageObjMark_GetParamValueType(mark, "IntKey"));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002445 int int_value;
Henrique Nakashima94230e52018-07-11 22:02:02 +00002446 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "IntKey", &int_value));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002447 EXPECT_EQ(42, int_value);
2448
2449 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002450 FPDFPageObjMark_GetParamValueType(mark, "StringKey"));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002451 unsigned long out_buffer_len;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002452 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2453 mark, "StringKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002454 EXPECT_GT(out_buffer_len, 0u);
2455 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2456 EXPECT_EQ(L"StringValue", name);
2457
Henrique Nakashima07520f62018-07-12 19:45:29 +00002458 EXPECT_EQ(FPDF_OBJECT_STRING,
2459 FPDFPageObjMark_GetParamValueType(mark, "BlobKey"));
2460 out_buffer_len = 0;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002461 EXPECT_TRUE(FPDFPageObjMark_GetParamBlobValue(
2462 mark, "BlobKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002463 EXPECT_EQ(kBlobLen, out_buffer_len);
2464 EXPECT_EQ(0, memcmp(kBlobValue, buffer, kBlobLen));
2465
Henrique Nakashima144107d2018-07-10 21:04:05 +00002466// Render and check the bitmap is the expected one.
2467#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
2468 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
2469#else
2470 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
2471#endif
2472 {
2473 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
2474 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2475 }
2476
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002477 // Now save the result.
2478 EXPECT_EQ(1, FPDFPage_CountObjects(page));
2479 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2480 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2481
Henrique Nakashima144107d2018-07-10 21:04:05 +00002482 FPDF_ClosePage(page);
2483
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002484 // Re-open the file and check the changes were kept in the saved .pdf.
2485 OpenSavedDocument();
2486 FPDF_PAGE saved_page = LoadSavedPage(0);
2487 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
2488
2489 text_object = FPDFPage_GetObject(saved_page, 0);
2490 EXPECT_TRUE(text_object);
2491 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2492 mark = FPDFPageObj_GetMark(text_object, 0);
2493 EXPECT_TRUE(mark);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002494 EXPECT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002495 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2496 EXPECT_EQ(L"TestMarkName", name);
2497
2498 CloseSavedPage(saved_page);
2499 CloseSavedDocument();
Henrique Nakashima144107d2018-07-10 21:04:05 +00002500}
2501
Jane Liu28fb7ba2017-08-02 21:45:57 -04002502TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
2503 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2504 FPDF_PAGE page = LoadPage(0);
2505 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002506 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04002507
2508 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2509 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2510 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
2511
2512 obj = FPDFPage_GetObject(page, 33);
2513 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2514 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
2515 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2516 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
2517 FPDFBitmap_Destroy(bitmap);
2518
2519 obj = FPDFPage_GetObject(page, 34);
2520 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2521 bitmap = FPDFImageObj_GetBitmap(obj);
2522 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2523 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
2524 FPDFBitmap_Destroy(bitmap);
2525
2526 obj = FPDFPage_GetObject(page, 35);
2527 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2528 bitmap = FPDFImageObj_GetBitmap(obj);
2529 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
2530 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
2531 FPDFBitmap_Destroy(bitmap);
2532
2533 obj = FPDFPage_GetObject(page, 36);
2534 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2535 bitmap = FPDFImageObj_GetBitmap(obj);
2536 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2537 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
2538 FPDFBitmap_Destroy(bitmap);
2539
2540 obj = FPDFPage_GetObject(page, 37);
2541 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2542 bitmap = FPDFImageObj_GetBitmap(obj);
2543 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2544 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
2545 FPDFBitmap_Destroy(bitmap);
2546
2547 obj = FPDFPage_GetObject(page, 38);
2548 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2549 bitmap = FPDFImageObj_GetBitmap(obj);
2550 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2551 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
2552 FPDFBitmap_Destroy(bitmap);
2553 UnloadPage(page);
2554}
Jane Liu548334e2017-08-03 16:33:40 -04002555
Lei Zhang53341dd2018-03-01 15:42:47 +00002556TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) {
2557 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
2558 FPDF_PAGE page = LoadPage(0);
2559 ASSERT_TRUE(page);
2560 ASSERT_EQ(1, FPDFPage_CountObjects(page));
2561
2562 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
2563 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2564 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002565 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00002566 ASSERT_TRUE(bitmap);
2567 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
2568 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00002569 }
2570
2571 UnloadPage(page);
2572}
2573
Jane Liu548334e2017-08-03 16:33:40 -04002574TEST_F(FPDFEditEmbeddertest, GetImageData) {
2575 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2576 FPDF_PAGE page = LoadPage(0);
2577 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002578 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04002579
2580 // Retrieve an image object with flate-encoded data stream.
2581 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
2582 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2583
2584 // Check that the raw image data has the correct length and hash value.
2585 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2586 std::vector<char> buf(len);
2587 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2588 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
2589 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2590
2591 // Check that the decoded image data has the correct length and hash value.
2592 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2593 buf.clear();
2594 buf.resize(len);
2595 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2596 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
2597 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2598
2599 // Retrieve an image obejct with DCTDecode-encoded data stream.
2600 obj = FPDFPage_GetObject(page, 37);
2601 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2602
2603 // Check that the raw image data has the correct length and hash value.
2604 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2605 buf.clear();
2606 buf.resize(len);
2607 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2608 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2609 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2610
2611 // Check that the decoded image data has the correct length and hash value,
2612 // which should be the same as those of the raw data, since this image is
2613 // encoded by a single DCTDecode filter and decoding is a noop.
2614 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2615 buf.clear();
2616 buf.resize(len);
2617 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2618 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2619 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2620
2621 UnloadPage(page);
2622}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04002623
2624TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
2625 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
2626 ASSERT_TRUE(rect);
2627
2628 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
2629 FPDFPageObj_Destroy(rect);
2630}
Jane Liube63ab92017-08-09 14:09:34 -04002631
2632TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
2633 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2634 FPDF_PAGE page = LoadPage(0);
2635 ASSERT_TRUE(page);
2636
2637 // Verify that retrieving the filter of a non-image object would fail.
2638 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2639 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2640 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
2641 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
2642
2643 // Verify the returned filter string for an image object with a single filter.
2644 obj = FPDFPage_GetObject(page, 33);
2645 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2646 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
2647 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
2648 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002649 static constexpr char kFlateDecode[] = "FlateDecode";
2650 EXPECT_EQ(sizeof(kFlateDecode),
2651 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
2652 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002653 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
2654
2655 // Verify all the filters for an image object with a list of filters.
2656 obj = FPDFPage_GetObject(page, 38);
2657 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2658 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
2659 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
2660 buf.clear();
2661 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002662 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
2663 EXPECT_EQ(sizeof(kASCIIHexDecode),
2664 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
2665 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002666
2667 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
2668 buf.clear();
2669 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002670 static constexpr char kDCTDecode[] = "DCTDecode";
2671 EXPECT_EQ(sizeof(kDCTDecode),
2672 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
2673 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002674
2675 UnloadPage(page);
2676}
Jane Liuca898292017-08-16 11:25:35 -04002677
2678TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
2679 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2680 FPDF_PAGE page = LoadPage(0);
2681 ASSERT_TRUE(page);
2682
2683 // Check that getting the metadata of a null object would fail.
2684 FPDF_IMAGEOBJ_METADATA metadata;
2685 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
2686
2687 // Check that receiving the metadata with a null metadata object would fail.
2688 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
2689 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
2690
2691 // Check that when retrieving an image object's metadata without passing in
2692 // |page|, all values are correct, with the last two being default values.
2693 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2694 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002695 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002696 EXPECT_EQ(92u, metadata.width);
2697 EXPECT_EQ(68u, metadata.height);
2698 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
2699 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
2700 EXPECT_EQ(0u, metadata.bits_per_pixel);
2701 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
2702
2703 // Verify the metadata of a bitmap image with indexed colorspace.
2704 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002705 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002706 EXPECT_EQ(92u, metadata.width);
2707 EXPECT_EQ(68u, metadata.height);
2708 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
2709 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
2710 EXPECT_EQ(1u, metadata.bits_per_pixel);
2711 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
2712
2713 // Verify the metadata of an image with RGB colorspace.
2714 obj = FPDFPage_GetObject(page, 37);
2715 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2716 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002717 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002718 EXPECT_EQ(126u, metadata.width);
2719 EXPECT_EQ(106u, metadata.height);
2720 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
2721 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
2722 EXPECT_EQ(24u, metadata.bits_per_pixel);
2723 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
2724
2725 UnloadPage(page);
2726}