blob: 800fc70076a2b763a2f2c9c8f8ce7b2ae61fc655 [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];
544 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
545 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 =
555 FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256);
556 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 =
575 FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256);
576 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 // Should be the last object.
583 EXPECT_EQ(object_count - 1, i);
584
585 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000586 FPDFPageObjMark_GetParamValueType(mark, "Position"));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000587 unsigned long length;
Henrique Nakashima94230e52018-07-11 22:02:02 +0000588 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "Position",
589 buffer, 256, &length));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000590 ASSERT_GT(length, 0u);
591 EXPECT_EQ((4u + 1u) * 2u, length);
Henrique Nakashima140dead2018-07-11 21:40:03 +0000592 std::wstring value =
593 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000594 EXPECT_EQ(L"Last", value);
595 } else {
596 FAIL();
597 }
598 }
599 }
600
601 // Expect certain number of tagged objects. The test file contains strings
602 // from 1 to 19.
603 EXPECT_EQ(expected_prime_count, prime_count);
604 EXPECT_EQ(expected_square_count, square_count);
605 EXPECT_EQ(expected_greater_than_ten_count, greater_than_ten_count);
606 EXPECT_EQ(expected_bounds_count, bounds_count);
607}
608
609TEST_F(FPDFEditEmbeddertest, ReadMarkedObjectsIndirectDict) {
610 // Load document with some text marked with an indirect property.
611 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
612 FPDF_PAGE page = LoadPage(0);
613 ASSERT_TRUE(page);
614
615 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
616
617 UnloadPage(page);
618}
619
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000620TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) {
621 // Load document with some text.
622 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
623 FPDF_PAGE page = LoadPage(0);
624 ASSERT_TRUE(page);
625
626 // Show what the original file looks like.
627 {
628#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
629 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
630#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
631 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
632#else
633 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
634#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000635 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000636 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
637 }
638
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000639 constexpr int expected_object_count = 19;
640 CheckMarkCounts(page, 1, expected_object_count, 8, 4, 9, 1);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000641
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000642 // Get all objects marked with "Prime"
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000643 std::vector<FPDF_PAGEOBJECT> primes;
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000644 for (int i = 0; i < expected_object_count; ++i) {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000645 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
646
647 int mark_count = FPDFPageObj_CountMarks(page_object);
648 for (int j = 0; j < mark_count; ++j) {
649 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
650
651 char buffer[256];
652 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
653 std::wstring name =
654 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
655 if (name == L"Prime") {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000656 primes.push_back(page_object);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000657 }
658 }
659 }
660
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000661 // Remove all objects marked with "Prime".
662 for (FPDF_PAGEOBJECT page_object : primes) {
663 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
664 FPDFPageObj_Destroy(page_object);
665 }
666
667 EXPECT_EQ(11, FPDFPage_CountObjects(page));
668
669 {
670#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
671 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
672#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
673 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
674#else
675 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
676#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000677 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000678 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
679 }
680
681 UnloadPage(page);
682}
683
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000684TEST_F(FPDFEditEmbeddertest, MaintainMarkedObjects) {
685 // Load document with some text.
686 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
687 FPDF_PAGE page = LoadPage(0);
688 ASSERT_TRUE(page);
689
690 // Iterate over all objects, counting the number of times each content mark
691 // name appears.
692 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
693
694 // Remove first page object.
695 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
696 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
697 FPDFPageObj_Destroy(page_object);
698
699 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
700
701 EXPECT_TRUE(FPDFPage_GenerateContent(page));
702 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
703
704 UnloadPage(page);
705
706 OpenSavedDocument();
707 FPDF_PAGE saved_page = LoadSavedPage(0);
708
709 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
710
711 CloseSavedPage(saved_page);
712 CloseSavedDocument();
713}
714
715TEST_F(FPDFEditEmbeddertest, MaintainIndirectMarkedObjects) {
716 // Load document with some text.
717 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
718 FPDF_PAGE page = LoadPage(0);
719 ASSERT_TRUE(page);
720
721 // Iterate over all objects, counting the number of times each content mark
722 // name appears.
723 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
724
725 // Remove first page object.
726 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
727 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
728 FPDFPageObj_Destroy(page_object);
729
730 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
731
732 EXPECT_TRUE(FPDFPage_GenerateContent(page));
733 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
734
735 UnloadPage(page);
736
737 OpenSavedDocument();
738 FPDF_PAGE saved_page = LoadSavedPage(0);
739
740 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
741
742 CloseSavedPage(saved_page);
743 CloseSavedDocument();
744}
745
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000746TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObject) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000747 // Load document with some text.
748 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
749 FPDF_PAGE page = LoadPage(0);
750 ASSERT_TRUE(page);
751
752 // Get the "Hello, world!" text object and remove it.
753 ASSERT_EQ(2, FPDFPage_CountObjects(page));
754 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
755 ASSERT_TRUE(page_object);
756 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
757
758 // Verify the "Hello, world!" text is gone.
759 ASSERT_EQ(1, FPDFPage_CountObjects(page));
760
761 // Save the file
762 EXPECT_TRUE(FPDFPage_GenerateContent(page));
763 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
764 UnloadPage(page);
765 FPDFPageObj_Destroy(page_object);
766
767 // Re-open the file and check the page object count is still 1.
768 OpenSavedDocument();
769 FPDF_PAGE saved_page = LoadSavedPage(0);
770 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
771 CloseSavedPage(saved_page);
772 CloseSavedDocument();
773}
774
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000775TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsNotLonely) {
776 // Load document with some text.
777 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
778 FPDF_PAGE page = LoadPage(0);
779 ASSERT_TRUE(page);
780
781 // Get the "Hello, world!" text object and remove it. There is another object
782 // in the same stream that says "Goodbye, world!"
783 ASSERT_EQ(3, FPDFPage_CountObjects(page));
784 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
785 ASSERT_TRUE(page_object);
786 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
787
788 // Verify the "Hello, world!" text is gone.
789 ASSERT_EQ(2, FPDFPage_CountObjects(page));
790#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
791 const char kHelloRemovedMD5[] = "e07a62d412728fc4d6e3ff42f2dd0e11";
792#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
793 const char kHelloRemovedMD5[] = "de37b0bb7ff903c1068bae361844be50";
794#else
795 const char kHelloRemovedMD5[] = "95b92950647a2190e1230911e7a1a0e9";
796#endif
797 {
798 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
799 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
800 }
801
802 // Save the file
803 EXPECT_TRUE(FPDFPage_GenerateContent(page));
804 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
805 UnloadPage(page);
806 FPDFPageObj_Destroy(page_object);
807
808 // Re-open the file and check the page object count is still 2.
809 OpenSavedDocument();
810 FPDF_PAGE saved_page = LoadSavedPage(0);
811
812 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
813 {
814 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
815 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
816 }
817
818 CloseSavedPage(saved_page);
819 CloseSavedDocument();
820}
821
822TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsLonely) {
823 // Load document with some text.
824 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
825 FPDF_PAGE page = LoadPage(0);
826 ASSERT_TRUE(page);
827
828 // Get the "Greetings, world!" text object and remove it. This is the only
829 // object in the stream.
830 ASSERT_EQ(3, FPDFPage_CountObjects(page));
831 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 2);
832 ASSERT_TRUE(page_object);
833 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
834
835 // Verify the "Greetings, world!" text is gone.
836 ASSERT_EQ(2, FPDFPage_CountObjects(page));
837#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
838 const char kGreetingsRemovedMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
839#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
840 const char kGreetingsRemovedMD5[] = "e5a6fa28298db07484cd922f3e210c88";
841#else
842 const char kGreetingsRemovedMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
843#endif
844 {
845 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
846 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
847 }
848
849 // Save the file
850 EXPECT_TRUE(FPDFPage_GenerateContent(page));
851 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
852 UnloadPage(page);
853 FPDFPageObj_Destroy(page_object);
854
855 // Re-open the file and check the page object count is still 2.
856 OpenSavedDocument();
857 FPDF_PAGE saved_page = LoadSavedPage(0);
858
859 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
860 {
861 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
862 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
863 }
864
865 CloseSavedPage(saved_page);
866 CloseSavedDocument();
867}
868
Henrique Nakashima6eb79392018-06-12 20:27:35 +0000869TEST_F(FPDFEditEmbeddertest, GetContentStream) {
870 // Load document with some text split across streams.
871 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
872 FPDF_PAGE page = LoadPage(0);
873 ASSERT_TRUE(page);
874
875 // Content stream 0: page objects 0-14.
876 // Content stream 1: page objects 15-17.
877 // Content stream 2: page object 18.
878 ASSERT_EQ(19, FPDFPage_CountObjects(page));
879 for (int i = 0; i < 19; i++) {
880 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
881 ASSERT_TRUE(page_object);
882 CPDF_PageObject* cpdf_page_object =
883 CPDFPageObjectFromFPDFPageObject(page_object);
884 if (i < 15)
885 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
886 else if (i < 18)
887 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
888 else
889 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
890 }
891
892 UnloadPage(page);
893}
894
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +0000895TEST_F(FPDFEditEmbeddertest, RemoveAllFromStream) {
896 // Load document with some text split across streams.
897 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
898 FPDF_PAGE page = LoadPage(0);
899 ASSERT_TRUE(page);
900
901 // Content stream 0: page objects 0-14.
902 // Content stream 1: page objects 15-17.
903 // Content stream 2: page object 18.
904 ASSERT_EQ(19, FPDFPage_CountObjects(page));
905
906 // Loop backwards because objects will being removed, which shifts the indexes
907 // after the removed position.
908 for (int i = 18; i >= 0; i--) {
909 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
910 ASSERT_TRUE(page_object);
911 CPDF_PageObject* cpdf_page_object =
912 CPDFPageObjectFromFPDFPageObject(page_object);
913
914 // Empty content stream 1.
915 if (cpdf_page_object->GetContentStream() == 1) {
916 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
917 FPDFPageObj_Destroy(page_object);
918 }
919 }
920
921 // Content stream 0: page objects 0-14.
922 // Content stream 2: page object 15.
923 ASSERT_EQ(16, FPDFPage_CountObjects(page));
924 for (int i = 0; i < 16; i++) {
925 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
926 ASSERT_TRUE(page_object);
927 CPDF_PageObject* cpdf_page_object =
928 CPDFPageObjectFromFPDFPageObject(page_object);
929 if (i < 15)
930 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
931 else
932 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
933 }
934
935 // Generate contents should remove the empty stream and update the page
936 // objects' contents stream indexes.
937 EXPECT_TRUE(FPDFPage_GenerateContent(page));
938
939 // Content stream 0: page objects 0-14.
940 // Content stream 1: page object 15.
941 ASSERT_EQ(16, FPDFPage_CountObjects(page));
942 for (int i = 0; i < 16; i++) {
943 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
944 ASSERT_TRUE(page_object);
945 CPDF_PageObject* cpdf_page_object =
946 CPDFPageObjectFromFPDFPageObject(page_object);
947 if (i < 15)
948 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
949 else
950 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
951 }
952
953#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
954 const char kStream1RemovedMD5[] = "d2e21fbd5a6de563f619feeeb6163331";
955#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
956 const char kStream1RemovedMD5[] = "b4140f203523e38793283a5943d8075b";
957#else
958 const char kStream1RemovedMD5[] = "e86a3efc160ede6cfcb1f59bcacf1105";
959#endif
960 {
961 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
962 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
963 }
964
965 // Save the file
966 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
967 UnloadPage(page);
968
969 // Re-open the file and check the page object count is still 16, and that
970 // content stream 1 was removed.
971 OpenSavedDocument();
972 FPDF_PAGE saved_page = LoadSavedPage(0);
973
974 // Content stream 0: page objects 0-14.
975 // Content stream 1: page object 15.
976 EXPECT_EQ(16, FPDFPage_CountObjects(saved_page));
977 for (int i = 0; i < 16; i++) {
978 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
979 ASSERT_TRUE(page_object);
980 CPDF_PageObject* cpdf_page_object =
981 CPDFPageObjectFromFPDFPageObject(page_object);
982 if (i < 15)
983 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
984 else
985 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
986 }
987
988 {
989 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
990 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
991 }
992
993 CloseSavedPage(saved_page);
994 CloseSavedDocument();
995}
996
997TEST_F(FPDFEditEmbeddertest, RemoveAllFromSingleStream) {
998 // Load document with a single stream.
999 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1000 FPDF_PAGE page = LoadPage(0);
1001 ASSERT_TRUE(page);
1002
1003 // Content stream 0: page objects 0-1.
1004 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1005
1006 // Loop backwards because objects will being removed, which shifts the indexes
1007 // after the removed position.
1008 for (int i = 1; i >= 0; i--) {
1009 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1010 ASSERT_TRUE(page_object);
1011 CPDF_PageObject* cpdf_page_object =
1012 CPDFPageObjectFromFPDFPageObject(page_object);
1013 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1014 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1015 FPDFPageObj_Destroy(page_object);
1016 }
1017
1018 // No more objects in the stream
1019 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1020
1021 // Generate contents should remove the empty stream and update the page
1022 // objects' contents stream indexes.
1023 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1024
1025 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1026
1027 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1028 {
1029 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1030 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1031 }
1032
1033 // Save the file
1034 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1035 UnloadPage(page);
1036
1037 // Re-open the file and check the page object count is still 0.
1038 OpenSavedDocument();
1039 FPDF_PAGE saved_page = LoadSavedPage(0);
1040
1041 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1042 {
1043 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1044 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1045 }
1046
1047 CloseSavedPage(saved_page);
1048 CloseSavedDocument();
1049}
1050
1051TEST_F(FPDFEditEmbeddertest, RemoveFirstFromSingleStream) {
1052 // Load document with a single stream.
1053 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1054 FPDF_PAGE page = LoadPage(0);
1055 ASSERT_TRUE(page);
1056
1057 // Content stream 0: page objects 0-1.
1058 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1059
1060 // Remove first object.
1061 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
1062 ASSERT_TRUE(page_object);
1063 CPDF_PageObject* cpdf_page_object =
1064 CPDFPageObjectFromFPDFPageObject(page_object);
1065 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1066 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1067 FPDFPageObj_Destroy(page_object);
1068
1069 // One object left in the stream.
1070 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1071 page_object = FPDFPage_GetObject(page, 0);
1072 ASSERT_TRUE(page_object);
1073 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1074 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1075
1076 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1077
1078 // Still one object left in the stream.
1079 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1080 page_object = FPDFPage_GetObject(page, 0);
1081 ASSERT_TRUE(page_object);
1082 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1083 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1084
1085#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1086 const char kFirstRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
1087#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1088 const char kFirstRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
1089#else
1090 const char kFirstRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
1091#endif
1092 {
1093 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1094 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1095 }
1096
1097 // Save the file
1098 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1099 UnloadPage(page);
1100
1101 // Re-open the file and check the page object count is still 0.
1102 OpenSavedDocument();
1103 FPDF_PAGE saved_page = LoadSavedPage(0);
1104
1105 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1106 page_object = FPDFPage_GetObject(saved_page, 0);
1107 ASSERT_TRUE(page_object);
1108 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1109 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1110 {
1111 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1112 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1113 }
1114
1115 CloseSavedPage(saved_page);
1116 CloseSavedDocument();
1117}
1118
1119TEST_F(FPDFEditEmbeddertest, RemoveLastFromSingleStream) {
1120 // Load document with a single stream.
1121 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1122 FPDF_PAGE page = LoadPage(0);
1123 ASSERT_TRUE(page);
1124
1125 // Content stream 0: page objects 0-1.
1126 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1127
1128 // Remove last object
1129 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 1);
1130 ASSERT_TRUE(page_object);
1131 CPDF_PageObject* cpdf_page_object =
1132 CPDFPageObjectFromFPDFPageObject(page_object);
1133 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1134 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1135 FPDFPageObj_Destroy(page_object);
1136
1137 // One object left in the stream.
1138 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1139 page_object = FPDFPage_GetObject(page, 0);
1140 ASSERT_TRUE(page_object);
1141 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1142 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1143
1144 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1145
1146 // Still one object left in the stream.
1147 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1148 page_object = FPDFPage_GetObject(page, 0);
1149 ASSERT_TRUE(page_object);
1150 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1151 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1152
1153#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1154 const char kLastRemovedMD5[] = "f8fbd14a048b9e2ea8e5f059f22a910e";
1155#else
1156 const char kLastRemovedMD5[] = "93dcc09055f87a2792c8e3065af99a1b";
1157#endif
1158 {
1159 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1160 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1161 }
1162
1163 // Save the file
1164 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1165 UnloadPage(page);
1166
1167 // Re-open the file and check the page object count is still 0.
1168 OpenSavedDocument();
1169 FPDF_PAGE saved_page = LoadSavedPage(0);
1170
1171 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1172 page_object = FPDFPage_GetObject(saved_page, 0);
1173 ASSERT_TRUE(page_object);
1174 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1175 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1176 {
1177 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1178 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1179 }
1180
1181 CloseSavedPage(saved_page);
1182 CloseSavedDocument();
1183}
1184
1185TEST_F(FPDFEditEmbeddertest, RemoveAllFromMultipleStreams) {
1186 // Load document with some text.
1187 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
1188 FPDF_PAGE page = LoadPage(0);
1189 ASSERT_TRUE(page);
1190
1191 // Content stream 0: page objects 0-1.
1192 // Content stream 1: page object 2.
1193 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1194
1195 // Loop backwards because objects will being removed, which shifts the indexes
1196 // after the removed position.
1197 for (int i = 2; i >= 0; i--) {
1198 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1199 ASSERT_TRUE(page_object);
1200 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1201 FPDFPageObj_Destroy(page_object);
1202 }
1203
1204 // No more objects in the page.
1205 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1206
1207 // Generate contents should remove the empty streams and update the page
1208 // objects' contents stream indexes.
1209 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1210
1211 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1212
1213 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1214 {
1215 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1216 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1217 }
1218
1219 // Save the file
1220 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1221 UnloadPage(page);
1222
1223 // Re-open the file and check the page object count is still 0.
1224 OpenSavedDocument();
1225 FPDF_PAGE saved_page = LoadSavedPage(0);
1226
1227 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1228 {
1229 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1230 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1231 }
1232
1233 CloseSavedPage(saved_page);
1234 CloseSavedDocument();
1235}
1236
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001237TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) {
1238 // Load document with some text.
1239 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1240 FPDF_PAGE page = LoadPage(0);
1241 ASSERT_TRUE(page);
1242
1243 // Add a red rectangle.
1244 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1245 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1246 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1247 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1248 FPDFPage_InsertObject(page, red_rect);
1249
1250 // Verify the red rectangle was added.
1251 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1252
1253 // Save the file
1254 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1255 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1256 UnloadPage(page);
1257
1258 // Re-open the file and check the page object count is still 3.
1259 OpenSavedDocument();
1260 FPDF_PAGE saved_page = LoadSavedPage(0);
1261 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1262 CloseSavedPage(saved_page);
1263 CloseSavedDocument();
1264}
1265
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001266TEST_F(FPDFEditEmbeddertest, InsertPageObjectEditAndSave) {
1267 // Load document with some text.
1268 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1269 FPDF_PAGE page = LoadPage(0);
1270 ASSERT_TRUE(page);
1271
1272 // Add a red rectangle.
1273 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1274 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1275 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 100, 100, 255));
1276 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1277 FPDFPage_InsertObject(page, red_rect);
1278
1279 // Verify the red rectangle was added.
1280 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1281
1282 // Generate content but change it again
1283 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1284 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1285
1286 // Save the file
1287 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1288 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1289 UnloadPage(page);
1290
1291 // Re-open the file and check the page object count is still 3.
1292 OpenSavedDocument();
1293 FPDF_PAGE saved_page = LoadSavedPage(0);
1294 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1295 CloseSavedPage(saved_page);
1296 CloseSavedDocument();
1297}
1298
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001299TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) {
1300 // Start with a blank page.
1301 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1302 ASSERT_TRUE(page);
1303
1304 // Render the blank page and verify it's a blank bitmap.
1305 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
1306 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001307 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001308 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1309 }
1310 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1311
1312 // Add a red rectangle.
1313 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1314 ASSERT_TRUE(red_rect);
1315 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1316 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1317 FPDFPage_InsertObject(page, red_rect);
1318 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
1319 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001320 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001321 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
1322 }
1323 EXPECT_EQ(1, FPDFPage_CountObjects(page));
1324
1325 // Remove rectangle and verify it does not render anymore and the bitmap is
1326 // back to a blank one.
1327 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
1328 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001329 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001330 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1331 }
1332 EXPECT_EQ(0, FPDFPage_CountObjects(page));
1333
1334 // Trying to remove an object not in the page should return false.
1335 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
1336
1337 FPDF_ClosePage(page);
1338 FPDFPageObj_Destroy(red_rect);
1339}
1340
Miklos Vajna12abfd02017-09-15 07:49:03 +02001341TEST_F(FPDFEditEmbeddertest, PathsPoints) {
1342 CreateNewDocument();
1343 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
1344 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +02001345 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001346
1347 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +02001348 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001349
Miklos Vajna36eed872017-09-20 22:52:43 +02001350 // FPDFPath_GetPathSegment() with a non-path.
1351 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
1352 // FPDFPath_GetPathSegment() with a NULL path.
1353 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
1354 float x;
1355 float y;
1356 // FPDFPathSegment_GetPoint() with a NULL segment.
1357 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
1358
1359 // FPDFPathSegment_GetType() with a NULL segment.
1360 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
1361
1362 // FPDFPathSegment_GetClose() with a NULL segment.
1363 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
1364
Miklos Vajna12abfd02017-09-15 07:49:03 +02001365 FPDFPageObj_Destroy(img);
1366}
1367
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001368TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
1369 // Load document with some text
1370 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1371 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001372 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001373
1374 // Add an opaque rectangle on top of some of the text.
1375 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1376 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1377 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1378 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001379
1380 // Add a transparent triangle on top of other part of the text.
1381 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
1382 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
1383 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
1384 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
1385 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
1386 EXPECT_TRUE(FPDFPath_Close(black_path));
1387 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001388
1389 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001390 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001391#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -07001392 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001393#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001394 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001395#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001396 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001397 UnloadPage(page);
1398}
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001399
wileyryae858aa42017-05-31 14:49:05 -05001400TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
1401 // Load document with existing content
1402 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
1403 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001404 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -05001405
1406 // Add a transparent rectangle on top of the existing content
1407 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
1408 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
1409 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
1410 FPDFPage_InsertObject(page, red_rect2);
1411
1412 // Add an opaque rectangle on top of the existing content
1413 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
1414 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1415 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1416 FPDFPage_InsertObject(page, red_rect);
1417
Tom Sepeze08d2b12018-04-25 18:49:32 +00001418 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001419 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001420 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1421
1422 // Now save the result, closing the page and document
1423 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -04001424 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -05001425
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001426 OpenSavedDocument();
Lei Zhang107fa7b2018-02-09 21:48:15 +00001427 FPDF_PAGE saved_page = LoadSavedPage(0);
1428 VerifySavedRendering(saved_page, 612, 792,
1429 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001430
1431 ClearString();
1432 // Add another opaque rectangle on top of the existing content
1433 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
1434 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
1435 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001436 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -05001437
1438 // Add another transparent rectangle on top of existing content
1439 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
1440 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
1441 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001442 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001443 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
1444 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001445 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001446 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
1447 }
Lei Zhang107fa7b2018-02-09 21:48:15 +00001448 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -05001449
1450 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +00001451 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001452
Lei Zhang107fa7b2018-02-09 21:48:15 +00001453 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001454 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -05001455
1456 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +00001457 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -05001458}
1459
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001460TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
1461 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001462 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001463
1464 // Add a large stroked rectangle (fill color should not affect it).
1465 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
1466 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
1467 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
1468 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
Miklos Vajna366df7f2018-05-22 14:27:29 +00001469
1470 float width = 0;
1471 EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width));
1472 EXPECT_EQ(15.0f, width);
1473
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001474 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
1475 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001476 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001477 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001478 CompareBitmap(page_bitmap.get(), 612, 792,
1479 "64bd31f862a89e0a9e505a5af6efd506");
1480 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001481
1482 // Add crossed-checkmark
1483 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
1484 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
1485 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
1486 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
1487 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
1488 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
1489 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
1490 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1491 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001492 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001493 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001494 CompareBitmap(page_bitmap.get(), 612, 792,
1495 "4b6f3b9d25c4e194821217d5016c3724");
1496 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001497
1498 // Add stroked and filled oval-ish path.
1499 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
1500 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
1501 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
1502 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
1503 EXPECT_TRUE(FPDFPath_Close(path));
1504 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
1505 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
1506 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
1507 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
1508 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001509 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001510 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001511 CompareBitmap(page_bitmap.get(), 612, 792,
1512 "ff3e6a22326754944cc6e56609acd73b");
1513 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001514 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001515}
Nicolas Pena49058402017-02-14 18:26:20 -05001516
Nicolas Pena4c48b102018-06-13 18:23:46 +00001517// Tests adding text from standard font using FPDFPageObj_NewTextObj.
Nicolas Pena49058402017-02-14 18:26:20 -05001518TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
1519 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001520 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -05001521
1522 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001523 FPDF_PAGEOBJECT text_object1 =
1524 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1525 EXPECT_TRUE(text_object1);
1526 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
1527 GetFPDFWideString(L"I'm at the bottom of the page");
1528 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
1529 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
1530 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001531 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001532 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001533#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001534 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -05001535#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001536 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001537#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001538 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1539 }
Nicolas Pena49058402017-02-14 18:26:20 -05001540
1541 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -04001542 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001543 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001544 EXPECT_TRUE(text_object2);
1545 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1546 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
1547 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1548 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
1549 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001550 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001551 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001552#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +00001553 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -04001554#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +00001555 const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d";
Nicolas Pena49058402017-02-14 18:26:20 -05001556#else
Dan Sinclair971a6742018-03-28 19:23:25 +00001557 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001558#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001559 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001560 }
Nicolas Pena49058402017-02-14 18:26:20 -05001561
1562 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -04001563 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001564 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001565 EXPECT_TRUE(text_object3);
1566 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
1567 GetFPDFWideString(L"Can you read me? <:)>");
1568 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
1569 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
1570 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001571 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001572 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001573#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001574 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -04001575#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001576 const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2";
Nicolas Pena49058402017-02-14 18:26:20 -05001577#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001578 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001579#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001580 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
1581 }
Nicolas Pena49058402017-02-14 18:26:20 -05001582
Miklos Vajnac765d2a2018-06-19 15:45:42 +00001583 double matrix_a = 0;
1584 double matrix_b = 0;
1585 double matrix_c = 0;
1586 double matrix_d = 0;
1587 double matrix_e = 0;
1588 double matrix_f = 0;
1589 EXPECT_FALSE(FPDFText_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1590 &matrix_d, &matrix_e, &matrix_f));
1591 EXPECT_TRUE(FPDFText_GetMatrix(text_object3, &matrix_a, &matrix_b, &matrix_c,
1592 &matrix_d, &matrix_e, &matrix_f));
1593 EXPECT_EQ(1., matrix_a);
1594 EXPECT_EQ(1.5, matrix_b);
1595 EXPECT_EQ(2., matrix_c);
1596 EXPECT_EQ(0.5, matrix_d);
1597 EXPECT_EQ(200., matrix_e);
1598 EXPECT_EQ(200., matrix_f);
1599
Miklos Vajna8625d3b2018-06-26 15:12:48 +00001600 EXPECT_EQ(0, FPDFTextObj_GetFontSize(nullptr));
1601 EXPECT_EQ(20, FPDFTextObj_GetFontSize(text_object3));
1602
Nicolas Pena49058402017-02-14 18:26:20 -05001603 // TODO(npm): Why are there issues with text rotated by 90 degrees?
1604 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
1605 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -05001606}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001607
Miklos Vajna1448cc12018-07-03 13:52:33 +00001608TEST_F(FPDFEditEmbeddertest, TestGetTextRenderMode) {
1609 EXPECT_TRUE(OpenDocument("text_render_mode.pdf"));
1610 FPDF_PAGE page = LoadPage(0);
1611 ASSERT_TRUE(page);
1612 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1613
1614 ASSERT_EQ(-1, FPDFText_GetTextRenderMode(nullptr));
1615
1616 FPDF_PAGEOBJECT fill = FPDFPage_GetObject(page, 0);
1617 ASSERT_EQ(FPDF_TEXTRENDERMODE_FILL, FPDFText_GetTextRenderMode(fill));
1618
1619 FPDF_PAGEOBJECT stroke = FPDFPage_GetObject(page, 1);
1620 ASSERT_EQ(FPDF_TEXTRENDERMODE_STROKE, FPDFText_GetTextRenderMode(stroke));
1621
1622 UnloadPage(page);
1623}
1624
Miklos Vajnab66077d2018-07-11 13:25:02 +00001625TEST_F(FPDFEditEmbeddertest, TestFormGetObjects) {
1626 EXPECT_TRUE(OpenDocument("form_object.pdf"));
1627 FPDF_PAGE page = LoadPage(0);
1628 ASSERT_TRUE(page);
1629 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1630
1631 FPDF_PAGEOBJECT form = FPDFPage_GetObject(page, 0);
1632 EXPECT_EQ(FPDF_PAGEOBJ_FORM, FPDFPageObj_GetType(form));
1633 ASSERT_EQ(-1, FPDFFormObj_CountObjects(nullptr));
1634 ASSERT_EQ(2, FPDFFormObj_CountObjects(form));
1635
1636 UnloadPage(page);
1637}
1638
Nicolas Pena4c48b102018-06-13 18:23:46 +00001639// Tests adding text from standard font using FPDFText_LoadStandardFont.
1640TEST_F(FPDFEditEmbeddertest, AddStandardFontText2) {
1641 // Start with a blank page
1642 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
1643
1644 // Load a standard font.
1645 FPDF_FONT font = FPDFText_LoadStandardFont(document(), "Helvetica");
1646 ASSERT_TRUE(font);
1647
1648 // Add some text to the page.
1649 FPDF_PAGEOBJECT text_object =
1650 FPDFPageObj_CreateTextObj(document(), font, 12.0f);
1651 EXPECT_TRUE(text_object);
1652 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1653 GetFPDFWideString(L"I'm at the bottom of the page");
1654 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1655 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20);
1656 FPDFPage_InsertObject(page.get(), text_object);
1657 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page.get(), nullptr, 0);
1658#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1659 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
1660#else
1661 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
1662#endif
1663 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1664}
1665
1666TEST_F(FPDFEditEmbeddertest, LoadStandardFonts) {
1667 CreateNewDocument();
1668 const char* standard_font_names[] = {"Arial",
1669 "Arial-Bold",
1670 "Arial-BoldItalic",
1671 "Arial-Italic",
1672 "Courier",
1673 "Courier-BoldOblique",
1674 "Courier-Oblique",
1675 "Courier-Bold",
1676 "CourierNew",
1677 "CourierNew-Bold",
1678 "CourierNew-BoldItalic",
1679 "CourierNew-Italic",
1680 "Helvetica",
1681 "Helvetica-Bold",
1682 "Helvetica-BoldOblique",
1683 "Helvetica-Oblique",
1684 "Symbol",
1685 "TimesNewRoman",
1686 "TimesNewRoman-Bold",
1687 "TimesNewRoman-BoldItalic",
1688 "TimesNewRoman-Italic",
1689 "ZapfDingbats"};
1690 for (auto* const font_name : standard_font_names) {
1691 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
1692 EXPECT_TRUE(font) << font_name << " should be considered a standard font.";
1693 }
1694 const char* not_standard_font_names[] = {
1695 "Abcdefg", "ArialB", "Arial-Style",
1696 "Font Name", "FontArial", "NotAStandardFontName",
1697 "TestFontName", "Quack", "Symbol-Italic",
1698 "Zapf"};
1699 for (auto* const font_name : not_standard_font_names) {
1700 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
1701 EXPECT_FALSE(font) << font_name
1702 << " should not be considered a standard font.";
1703 }
1704}
1705
Nicolas Pena603a31d2017-06-14 11:41:18 -04001706TEST_F(FPDFEditEmbeddertest, GraphicsData) {
1707 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +00001708 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001709
1710 // Create a rect with nontrivial graphics
1711 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1712 FPDFPageObj_SetBlendMode(rect1, "Color");
1713 FPDFPage_InsertObject(page.get(), rect1);
1714 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
1715
1716 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +00001717 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
1718 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -04001719 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001720 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001721
1722 // Add a text object causing no change to the graphics dictionary
1723 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1724 // Only alpha, the last component, matters for the graphics dictionary. And
1725 // the default value is 255.
1726 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
1727 FPDFPage_InsertObject(page.get(), text1);
1728 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001729 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001730
1731 // Add a text object increasing the size of the graphics dictionary
1732 FPDF_PAGEOBJECT text2 =
1733 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
1734 FPDFPage_InsertObject(page.get(), text2);
1735 FPDFPageObj_SetBlendMode(text2, "Darken");
1736 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
1737 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001738 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001739
1740 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -04001741 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -04001742 FPDFPageObj_SetBlendMode(path, "Darken");
1743 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
1744 FPDFPage_InsertObject(page.get(), path);
1745 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001746 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001747
1748 // Add a rect increasing the size of the graphics dictionary
1749 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1750 FPDFPageObj_SetBlendMode(rect2, "Darken");
1751 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
1752 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
1753 FPDFPage_InsertObject(page.get(), rect2);
1754 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001755 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001756}
1757
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001758TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
1759 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001760 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001761
1762 // Add a red rectangle with some non-default alpha
1763 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1764 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
1765 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
1766 FPDFPage_InsertObject(page, rect);
1767 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1768
1769 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +00001770 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
1771 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001772 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001773 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001774
1775 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +00001776 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001777 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001778 CompareBitmap(page_bitmap.get(), 612, 792,
1779 "5384da3406d62360ffb5cac4476fff1c");
1780 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001781
1782 // Never mind, my new favorite color is blue, increase alpha
1783 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
1784 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001785 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001786
1787 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +00001788 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001789 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001790 CompareBitmap(page_bitmap.get(), 612, 792,
1791 "2e51656f5073b0bee611d9cd086aa09c");
1792 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001793
1794 // And now generate, without changes
1795 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001796 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001797 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001798 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001799 CompareBitmap(page_bitmap.get(), 612, 792,
1800 "2e51656f5073b0bee611d9cd086aa09c");
1801 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001802
1803 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001804 FPDF_PAGEOBJECT text_object =
1805 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1806 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1807 GetFPDFWideString(L"Something something #text# something");
1808 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1809 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
1810 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001811 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001812 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001813 ASSERT_TRUE(font_dict);
1814 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1815
1816 // Generate yet again, check dicts are reasonably sized
1817 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001818 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001819 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1820 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001821}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001822
Nicolas Penad03ca422017-03-06 13:54:33 -05001823TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
1824 CreateNewDocument();
1825 // TODO(npm): use other fonts after disallowing loading any font as any type
1826 const CPDF_Font* stock_font =
1827 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001828 const uint8_t* data = stock_font->GetFont()->GetFontData();
1829 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001830 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001831 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
1832 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001833 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001834 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001835
Lei Zhang710fa992018-05-25 16:24:48 +00001836 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05001837 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1838 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
1839 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
1840 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1841 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1842 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05001843 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
1844
Lei Zhangde579ab2018-05-25 21:49:49 +00001845 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001846 ASSERT_TRUE(widths_array);
1847 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001848 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05001849 EXPECT_EQ(569, widths_array->GetNumberAt(11));
1850 EXPECT_EQ(500, widths_array->GetNumberAt(223));
1851 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
1852}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001853
Nicolas Penad03ca422017-03-06 13:54:33 -05001854TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
1855 CreateNewDocument();
1856 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001857 const uint8_t* data = stock_font->GetFont()->GetFontData();
1858 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001859 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001860 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
1861 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001862 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001863 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001864
Lei Zhang710fa992018-05-25 16:24:48 +00001865 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001866 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1867 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
1868 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
1869 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1870 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1871 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
1872 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05001873
Lei Zhangde579ab2018-05-25 21:49:49 +00001874 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001875 ASSERT_TRUE(widths_array);
1876 ASSERT_EQ(224U, widths_array->GetCount());
1877 EXPECT_EQ(600, widths_array->GetNumberAt(33));
1878 EXPECT_EQ(600, widths_array->GetNumberAt(74));
1879 EXPECT_EQ(600, widths_array->GetNumberAt(223));
1880 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
1881}
1882
1883TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
1884 CreateNewDocument();
1885 const CPDF_Font* stock_font =
1886 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001887 const uint8_t* data = stock_font->GetFont()->GetFontData();
1888 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001889 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001890 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
1891 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001892 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001893 EXPECT_TRUE(typed_font->IsCIDFont());
1894
1895 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001896 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001897 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1898 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1899 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
1900 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001901 const CPDF_Array* descendant_array =
1902 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001903 ASSERT_TRUE(descendant_array);
1904 EXPECT_EQ(1U, descendant_array->GetCount());
1905
1906 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001907 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001908 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1909 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
1910 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001911 const CPDF_Dictionary* cidinfo_dict =
1912 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001913 ASSERT_TRUE(cidinfo_dict);
1914 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1915 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1916 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1917 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
1918
1919 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001920 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001921 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001922 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -05001923 CheckCompositeFontWidths(widths_array, typed_font);
1924}
1925
1926TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
1927 CreateNewDocument();
1928 const CPDF_Font* stock_font =
1929 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001930 const uint8_t* data = stock_font->GetFont()->GetFontData();
1931 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -05001932
Tom Sepeze08d2b12018-04-25 18:49:32 +00001933 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001934 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1935 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001936 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001937 EXPECT_TRUE(typed_font->IsCIDFont());
1938
1939 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001940 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001941 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1942 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1943 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
1944 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001945 const CPDF_Array* descendant_array =
1946 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001947 ASSERT_TRUE(descendant_array);
1948 EXPECT_EQ(1U, descendant_array->GetCount());
1949
1950 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001951 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001952 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1953 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
1954 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001955 const CPDF_Dictionary* cidinfo_dict =
1956 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001957 ASSERT_TRUE(cidinfo_dict);
1958 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1959 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1960 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1961 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
1962 data);
1963
1964 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001965 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001966 ASSERT_TRUE(widths_array);
1967 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05001968}
rbpotterce8e51e2017-04-28 12:42:47 -07001969
1970TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
1971 // Load document with a -90 degree rotation
1972 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
1973 FPDF_PAGE page = LoadPage(0);
1974 EXPECT_NE(nullptr, page);
1975
1976 EXPECT_EQ(3, FPDFPage_GetRotation(page));
1977 UnloadPage(page);
1978}
Nicolas Penab3161852017-05-02 14:12:50 -04001979
1980TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
1981 // Start with a blank page
1982 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1983 {
1984 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001985 const uint8_t* data = stock_font->GetFont()->GetFontData();
1986 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001987 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001988 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
1989 ASSERT_TRUE(font.get());
1990
1991 // Add some text to the page
1992 FPDF_PAGEOBJECT text_object =
1993 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1994 EXPECT_TRUE(text_object);
1995 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1996 GetFPDFWideString(L"I am testing my loaded font, WEE.");
1997 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1998 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
1999 FPDFPage_InsertObject(page, text_object);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002000 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04002001#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002002 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
2003#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002004 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04002005#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002006 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04002007
2008 // Add some more text, same font
2009 FPDF_PAGEOBJECT text_object2 =
2010 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
2011 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2012 GetFPDFWideString(L"Bigger font size");
2013 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2014 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
2015 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04002016 }
Tom Sepeze08d2b12018-04-25 18:49:32 +00002017 ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04002018#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002019 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
2020#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002021 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04002022#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002023 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002024
Nicolas Pena207b7272017-05-26 17:37:06 -04002025 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04002026 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2027 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002028
2029 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002030}
Nicolas Penaf45ade32017-05-03 10:23:49 -04002031
Jane Liueda65252017-06-07 11:31:27 -04002032TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
2033 // Open a file with one annotation and load its first page.
2034 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00002035 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04002036 ASSERT_TRUE(page);
2037
Lei Zhanga21d5932018-02-05 18:28:38 +00002038 {
2039 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00002040 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00002041 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
2042 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04002043
Lei Zhanga21d5932018-02-05 18:28:38 +00002044 // FPDFPage_TransformAnnots() should run without errors when modifying
2045 // annotation rectangles.
2046 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
2047 }
Jane Liueda65252017-06-07 11:31:27 -04002048 UnloadPage(page);
2049}
2050
Nicolas Penaf45ade32017-05-03 10:23:49 -04002051// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04002052#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -04002053TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
2054 // Start with a blank page
2055 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2056 CFX_Font CIDfont;
2057 {
2058 // First, get the data from the font
2059 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
2060 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
2061 const uint8_t* data = CIDfont.GetFontData();
2062 const uint32_t size = CIDfont.GetSize();
2063
2064 // Load the data into a FPDF_Font.
Tom Sepeze08d2b12018-04-25 18:49:32 +00002065 ScopedFPDFFont font(
Nicolas Penaf45ade32017-05-03 10:23:49 -04002066 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
2067 ASSERT_TRUE(font.get());
2068
2069 // Add some text to the page
2070 FPDF_PAGEOBJECT text_object =
2071 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2072 ASSERT_TRUE(text_object);
2073 std::wstring wstr = L"ABCDEFGhijklmnop.";
2074 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2075 GetFPDFWideString(wstr);
2076 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2077 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
2078 FPDFPage_InsertObject(page, text_object);
2079
2080 // And add some Japanese characters
2081 FPDF_PAGEOBJECT text_object2 =
2082 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
2083 ASSERT_TRUE(text_object2);
2084 std::wstring wstr2 =
2085 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
2086 L"\u756A";
2087 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2088 GetFPDFWideString(wstr2);
2089 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2090 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
2091 FPDFPage_InsertObject(page, text_object2);
2092 }
2093
Nicolas Pena207b7272017-05-26 17:37:06 -04002094 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00002095 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00002096 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002097 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002098 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2099 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04002100
2101 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04002102 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04002103 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2104 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002105
2106 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04002107}
Dan Sinclair698aed72017-09-26 16:24:49 -04002108#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002109
2110TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04002111 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002112 {
2113 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
2114 FPDF_PAGE page = LoadPage(0);
2115 ASSERT_NE(nullptr, page);
2116
2117 // Now add a more complex blue path.
2118 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
2119 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
2120 // TODO(npm): stroking will cause the MD5s to differ.
2121 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
2122 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
2123 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
2124 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
2125 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
2126 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
2127 EXPECT_TRUE(FPDFPath_Close(green_path));
2128 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002129 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002130 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002131
2132 // Now save the result, closing the page and document
2133 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2134 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2135 UnloadPage(page);
2136 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002137
2138 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002139}
Jane Liu28fb7ba2017-08-02 21:45:57 -04002140
Henrique Nakashima144107d2018-07-10 21:04:05 +00002141TEST_F(FPDFEditEmbeddertest, AddMarkedText) {
2142 // Start with a blank page.
2143 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2144
2145 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
2146 const uint8_t* data = stock_font->GetFont()->GetFontData();
2147 const uint32_t size = stock_font->GetFont()->GetSize();
2148 ScopedFPDFFont font(
2149 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
2150 ASSERT_TRUE(font.get());
2151
2152 // Add some text to the page.
2153 FPDF_PAGEOBJECT text_object =
2154 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2155
2156 EXPECT_TRUE(text_object);
2157 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
2158 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2159 EXPECT_TRUE(FPDFText_SetText(text_object, text1.get()));
2160 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2161 FPDFPage_InsertObject(page, text_object);
2162
2163 // Add a mark with the tag "TestMarkName" to that text.
2164 EXPECT_EQ(0, FPDFPageObj_CountMarks(text_object));
2165 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(text_object, "TestMarkName");
2166 EXPECT_TRUE(mark);
2167 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2168 EXPECT_EQ(mark, FPDFPageObj_GetMark(text_object, 0));
2169 char buffer[256];
2170 EXPECT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
2171 std::wstring name =
2172 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2173 EXPECT_EQ(L"TestMarkName", name);
2174
2175 // Add parameters:
2176 // - int "IntKey" : 42
2177 // - string "StringKey": "StringValue"
2178 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
2179 EXPECT_TRUE(FPDFPageObjMark_SetIntParam(document(), mark, "IntKey", 42));
2180 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), mark, "StringKey",
2181 "StringValue"));
2182 EXPECT_EQ(2, FPDFPageObjMark_CountParams(mark));
2183
2184 // Check the two parameters can be retrieved.
2185 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002186 FPDFPageObjMark_GetParamValueType(mark, "IntKey"));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002187 int int_value;
Henrique Nakashima94230e52018-07-11 22:02:02 +00002188 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "IntKey", &int_value));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002189 EXPECT_EQ(42, int_value);
2190
2191 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002192 FPDFPageObjMark_GetParamValueType(mark, "StringKey"));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002193 unsigned long out_buffer_len;
Henrique Nakashima94230e52018-07-11 22:02:02 +00002194 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "StringKey", buffer,
2195 256, &out_buffer_len));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002196 EXPECT_GT(out_buffer_len, 0u);
2197 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2198 EXPECT_EQ(L"StringValue", name);
2199
2200// Render and check the bitmap is the expected one.
2201#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
2202 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
2203#else
2204 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
2205#endif
2206 {
2207 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
2208 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2209 }
2210
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002211 // Now save the result.
2212 EXPECT_EQ(1, FPDFPage_CountObjects(page));
2213 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2214 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2215
Henrique Nakashima144107d2018-07-10 21:04:05 +00002216 FPDF_ClosePage(page);
2217
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002218 // Re-open the file and check the changes were kept in the saved .pdf.
2219 OpenSavedDocument();
2220 FPDF_PAGE saved_page = LoadSavedPage(0);
2221 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
2222
2223 text_object = FPDFPage_GetObject(saved_page, 0);
2224 EXPECT_TRUE(text_object);
2225 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2226 mark = FPDFPageObj_GetMark(text_object, 0);
2227 EXPECT_TRUE(mark);
2228 EXPECT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
2229 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2230 EXPECT_EQ(L"TestMarkName", name);
2231
2232 CloseSavedPage(saved_page);
2233 CloseSavedDocument();
Henrique Nakashima144107d2018-07-10 21:04:05 +00002234}
2235
Jane Liu28fb7ba2017-08-02 21:45:57 -04002236TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
2237 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2238 FPDF_PAGE page = LoadPage(0);
2239 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002240 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04002241
2242 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2243 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2244 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
2245
2246 obj = FPDFPage_GetObject(page, 33);
2247 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2248 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
2249 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2250 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
2251 FPDFBitmap_Destroy(bitmap);
2252
2253 obj = FPDFPage_GetObject(page, 34);
2254 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2255 bitmap = FPDFImageObj_GetBitmap(obj);
2256 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2257 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
2258 FPDFBitmap_Destroy(bitmap);
2259
2260 obj = FPDFPage_GetObject(page, 35);
2261 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2262 bitmap = FPDFImageObj_GetBitmap(obj);
2263 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
2264 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
2265 FPDFBitmap_Destroy(bitmap);
2266
2267 obj = FPDFPage_GetObject(page, 36);
2268 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2269 bitmap = FPDFImageObj_GetBitmap(obj);
2270 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2271 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
2272 FPDFBitmap_Destroy(bitmap);
2273
2274 obj = FPDFPage_GetObject(page, 37);
2275 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2276 bitmap = FPDFImageObj_GetBitmap(obj);
2277 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2278 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
2279 FPDFBitmap_Destroy(bitmap);
2280
2281 obj = FPDFPage_GetObject(page, 38);
2282 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2283 bitmap = FPDFImageObj_GetBitmap(obj);
2284 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2285 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
2286 FPDFBitmap_Destroy(bitmap);
2287 UnloadPage(page);
2288}
Jane Liu548334e2017-08-03 16:33:40 -04002289
Lei Zhang53341dd2018-03-01 15:42:47 +00002290TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) {
2291 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
2292 FPDF_PAGE page = LoadPage(0);
2293 ASSERT_TRUE(page);
2294 ASSERT_EQ(1, FPDFPage_CountObjects(page));
2295
2296 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
2297 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2298 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002299 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00002300 ASSERT_TRUE(bitmap);
2301 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
2302 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00002303 }
2304
2305 UnloadPage(page);
2306}
2307
Jane Liu548334e2017-08-03 16:33:40 -04002308TEST_F(FPDFEditEmbeddertest, GetImageData) {
2309 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2310 FPDF_PAGE page = LoadPage(0);
2311 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002312 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04002313
2314 // Retrieve an image object with flate-encoded data stream.
2315 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
2316 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2317
2318 // Check that the raw image data has the correct length and hash value.
2319 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2320 std::vector<char> buf(len);
2321 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2322 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
2323 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2324
2325 // Check that the decoded image data has the correct length and hash value.
2326 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2327 buf.clear();
2328 buf.resize(len);
2329 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2330 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
2331 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2332
2333 // Retrieve an image obejct with DCTDecode-encoded data stream.
2334 obj = FPDFPage_GetObject(page, 37);
2335 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2336
2337 // Check that the raw image data has the correct length and hash value.
2338 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2339 buf.clear();
2340 buf.resize(len);
2341 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2342 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2343 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2344
2345 // Check that the decoded image data has the correct length and hash value,
2346 // which should be the same as those of the raw data, since this image is
2347 // encoded by a single DCTDecode filter and decoding is a noop.
2348 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2349 buf.clear();
2350 buf.resize(len);
2351 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2352 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2353 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2354
2355 UnloadPage(page);
2356}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04002357
2358TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
2359 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
2360 ASSERT_TRUE(rect);
2361
2362 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
2363 FPDFPageObj_Destroy(rect);
2364}
Jane Liube63ab92017-08-09 14:09:34 -04002365
2366TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
2367 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2368 FPDF_PAGE page = LoadPage(0);
2369 ASSERT_TRUE(page);
2370
2371 // Verify that retrieving the filter of a non-image object would fail.
2372 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2373 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2374 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
2375 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
2376
2377 // Verify the returned filter string for an image object with a single filter.
2378 obj = FPDFPage_GetObject(page, 33);
2379 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2380 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
2381 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
2382 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002383 static constexpr char kFlateDecode[] = "FlateDecode";
2384 EXPECT_EQ(sizeof(kFlateDecode),
2385 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
2386 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002387 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
2388
2389 // Verify all the filters for an image object with a list of filters.
2390 obj = FPDFPage_GetObject(page, 38);
2391 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2392 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
2393 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
2394 buf.clear();
2395 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002396 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
2397 EXPECT_EQ(sizeof(kASCIIHexDecode),
2398 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
2399 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002400
2401 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
2402 buf.clear();
2403 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002404 static constexpr char kDCTDecode[] = "DCTDecode";
2405 EXPECT_EQ(sizeof(kDCTDecode),
2406 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
2407 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002408
2409 UnloadPage(page);
2410}
Jane Liuca898292017-08-16 11:25:35 -04002411
2412TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
2413 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2414 FPDF_PAGE page = LoadPage(0);
2415 ASSERT_TRUE(page);
2416
2417 // Check that getting the metadata of a null object would fail.
2418 FPDF_IMAGEOBJ_METADATA metadata;
2419 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
2420
2421 // Check that receiving the metadata with a null metadata object would fail.
2422 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
2423 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
2424
2425 // Check that when retrieving an image object's metadata without passing in
2426 // |page|, all values are correct, with the last two being default values.
2427 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2428 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002429 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002430 EXPECT_EQ(92u, metadata.width);
2431 EXPECT_EQ(68u, metadata.height);
2432 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
2433 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
2434 EXPECT_EQ(0u, metadata.bits_per_pixel);
2435 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
2436
2437 // Verify the metadata of a bitmap image with indexed colorspace.
2438 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002439 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002440 EXPECT_EQ(92u, metadata.width);
2441 EXPECT_EQ(68u, metadata.height);
2442 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
2443 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
2444 EXPECT_EQ(1u, metadata.bits_per_pixel);
2445 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
2446
2447 // Verify the metadata of an image with RGB colorspace.
2448 obj = FPDFPage_GetObject(page, 37);
2449 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2450 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002451 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002452 EXPECT_EQ(126u, metadata.width);
2453 EXPECT_EQ(106u, metadata.height);
2454 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
2455 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
2456 EXPECT_EQ(24u, metadata.bits_per_pixel);
2457 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
2458
2459 UnloadPage(page);
2460}