blob: ba790de6549c2eb2d0dfe6c424269a05cd7fc663 [file] [log] [blame]
Tom Sepezd483eb42016-01-06 10:03:59 -08001// Copyright 2016 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05005#include <memory>
6#include <string>
Nicolas Penad03ca422017-03-06 13:54:33 -05007#include <utility>
Jane Liu548334e2017-08-03 16:33:40 -04008#include <vector>
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009
Nicolas Penabe90aae2017-02-27 10:41:41 -050010#include "core/fpdfapi/font/cpdf_font.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050011#include "core/fpdfapi/page/cpdf_page.h"
Henrique Nakashima6eb79392018-06-12 20:27:35 +000012#include "core/fpdfapi/page/cpdf_pageobject.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050013#include "core/fpdfapi/parser/cpdf_array.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050014#include "core/fpdfapi/parser/cpdf_dictionary.h"
Nicolas Penad03ca422017-03-06 13:54:33 -050015#include "core/fpdfapi/parser/cpdf_number.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050016#include "core/fpdfapi/parser/cpdf_stream.h"
Artem Strygineababa12018-06-06 12:31:18 +000017#include "core/fpdfapi/parser/cpdf_stream_acc.h"
Nicolas Pena0fc185e2017-02-08 12:13:20 -050018#include "core/fxcrt/fx_system.h"
Dan Sinclair00d47a62018-03-28 18:39:04 +000019#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000020#include "public/cpp/fpdf_scopers.h"
Jane Liueda65252017-06-07 11:31:27 -040021#include "public/fpdf_annot.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080022#include "public/fpdf_edit.h"
23#include "public/fpdfview.h"
24#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080025#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080026#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080027#include "testing/test_support.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080028
Nicolas Pena3ff54002017-07-05 11:55:35 -040029class FPDFEditEmbeddertest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050030 protected:
31 FPDF_DOCUMENT CreateNewDocument() {
32 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070033 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050034 return document_;
35 }
36
Lei Zhang710fa992018-05-25 16:24:48 +000037 void CheckFontDescriptor(const CPDF_Dictionary* font_dict,
Nicolas Penad03ca422017-03-06 13:54:33 -050038 int font_type,
39 bool bold,
40 bool italic,
41 uint32_t size,
42 const uint8_t* data) {
Lei Zhangb1ec2802018-05-25 21:55:24 +000043 const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
Nicolas Penad03ca422017-03-06 13:54:33 -050044 ASSERT_TRUE(font_desc);
45 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
46 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
47 font_desc->GetStringFor("FontName"));
48
49 // Check that the font descriptor has the required keys according to spec
50 // 1.7 Table 5.19
51 ASSERT_TRUE(font_desc->KeyExist("Flags"));
Dan Sinclair10e1f052017-09-28 15:59:42 -040052
Nicolas Penad03ca422017-03-06 13:54:33 -050053 int font_flags = font_desc->GetIntegerFor("Flags");
Dan Sinclair10e1f052017-09-28 15:59:42 -040054 EXPECT_EQ(bold, FontStyleIsBold(font_flags));
55 EXPECT_EQ(italic, FontStyleIsItalic(font_flags));
56 EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags));
Nicolas Penad03ca422017-03-06 13:54:33 -050057 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
Nicolás Peña5f95f362017-09-28 13:00:45 +090058
Lei Zhangb1ec2802018-05-25 21:55:24 +000059 const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox");
Nicolás Peña5f95f362017-09-28 13:00:45 +090060 ASSERT_TRUE(fontBBox);
61 EXPECT_EQ(4U, fontBBox->GetCount());
62 // Check that the coordinates are in the preferred order according to spec
63 // 1.7 Section 3.8.4
64 EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2));
65 EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3));
66
Nicolas Penad03ca422017-03-06 13:54:33 -050067 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
68 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
69 EXPECT_TRUE(font_desc->KeyExist("Descent"));
70 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
71 EXPECT_TRUE(font_desc->KeyExist("StemV"));
Ryan Harrison275e2602017-09-18 14:23:18 -040072 ByteString present("FontFile");
73 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050074 if (font_type == FPDF_FONT_TRUETYPE)
75 std::swap(present, absent);
76 EXPECT_TRUE(font_desc->KeyExist(present));
77 EXPECT_FALSE(font_desc->KeyExist(absent));
78
Artem Strygineababa12018-06-06 12:31:18 +000079 auto streamAcc =
80 pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present));
81 streamAcc->LoadAllDataRaw();
82
Nicolas Penad03ca422017-03-06 13:54:33 -050083 // Check that the font stream is the one that was provided
Artem Strygineababa12018-06-06 12:31:18 +000084 ASSERT_EQ(size, streamAcc->GetSize());
Nicolás Peña79eab232017-09-28 13:29:05 +090085 if (font_type == FPDF_FONT_TRUETYPE) {
86 ASSERT_EQ(static_cast<int>(size),
Artem Strygineababa12018-06-06 12:31:18 +000087 streamAcc->GetDict()->GetIntegerFor("Length1"));
Nicolás Peña79eab232017-09-28 13:29:05 +090088 }
Artem Strygineababa12018-06-06 12:31:18 +000089
90 const uint8_t* stream_data = streamAcc->GetData();
Nicolas Penad03ca422017-03-06 13:54:33 -050091 for (size_t j = 0; j < size; j++)
92 EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j;
93 }
94
Lei Zhangde579ab2018-05-25 21:49:49 +000095 void CheckCompositeFontWidths(const CPDF_Array* widths_array,
Nicolas Penad03ca422017-03-06 13:54:33 -050096 CPDF_Font* typed_font) {
97 // Check that W array is in a format that conforms to PDF spec 1.7 section
98 // "Glyph Metrics in CIDFonts" (these checks are not
99 // implementation-specific).
100 EXPECT_GT(widths_array->GetCount(), 1U);
101 int num_cids_checked = 0;
102 int cur_cid = 0;
103 for (size_t idx = 0; idx < widths_array->GetCount(); idx++) {
104 int cid = widths_array->GetNumberAt(idx);
105 EXPECT_GE(cid, cur_cid);
106 ASSERT_FALSE(++idx == widths_array->GetCount());
Lei Zhangde579ab2018-05-25 21:49:49 +0000107 const CPDF_Object* next = widths_array->GetObjectAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500108 if (next->IsArray()) {
109 // We are in the c [w1 w2 ...] case
Lei Zhangde579ab2018-05-25 21:49:49 +0000110 const CPDF_Array* arr = next->AsArray();
Nicolas Penad03ca422017-03-06 13:54:33 -0500111 int cnt = static_cast<int>(arr->GetCount());
112 size_t inner_idx = 0;
113 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
Nicolas Pena23346602018-01-30 21:42:41 +0000114 uint32_t width = arr->GetNumberAt(inner_idx++);
Dan Sinclair971a6742018-03-28 19:23:25 +0000115 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
116 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500117 }
118 num_cids_checked += cnt;
119 continue;
120 }
121 // Otherwise, are in the c_first c_last w case.
122 ASSERT_TRUE(next->IsNumber());
123 int last_cid = next->AsNumber()->GetInteger();
124 ASSERT_FALSE(++idx == widths_array->GetCount());
Nicolas Pena23346602018-01-30 21:42:41 +0000125 uint32_t width = widths_array->GetNumberAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500126 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
Dan Sinclair971a6742018-03-28 19:23:25 +0000127 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
128 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500129 }
130 num_cids_checked += last_cid - cid + 1;
131 }
132 // Make sure we have a good amount of cids described
133 EXPECT_GT(num_cids_checked, 900);
134 }
135 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
136
137 private:
138 CPDF_Document* cpdf_doc_;
139};
Tom Sepezd483eb42016-01-06 10:03:59 -0800140
etienneb7712c262016-04-26 08:13:45 -0700141namespace {
thestigdc7ec032016-11-21 15:32:52 -0800142
etienneb7712c262016-04-26 08:13:45 -0700143const char kExpectedPDF[] =
144 "%PDF-1.7\r\n"
145 "%\xA1\xB3\xC5\xD7\r\n"
146 "1 0 obj\r\n"
147 "<</Pages 2 0 R /Type/Catalog>>\r\n"
148 "endobj\r\n"
149 "2 0 obj\r\n"
150 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
151 "endobj\r\n"
152 "3 0 obj\r\n"
153 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
154 "endobj\r\n"
155 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400156 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
157 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400158 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700159 ">>\r\n"
160 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400161 "5 0 obj\r\n"
162 "<</BM/Normal/CA 1/ca 1>>\r\n"
163 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700164 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400165 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700166 "0000000000 65535 f\r\n"
167 "0000000017 00000 n\r\n"
168 "0000000066 00000 n\r\n"
169 "0000000122 00000 n\r\n"
170 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400171 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700172 "trailer\r\n"
173 "<<\r\n"
174 "/Root 1 0 R\r\n"
175 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400176 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700177 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400178 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700179 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800180
etienneb7712c262016-04-26 08:13:45 -0700181} // namespace
182
Tom Sepezd483eb42016-01-06 10:03:59 -0800183TEST_F(FPDFEditEmbeddertest, EmptyCreation) {
184 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700185 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800186 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400187 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800188 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800189 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700190
Nicolas Penad9d6c292017-06-06 16:12:10 -0400191 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
192 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700193 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800194}
thestigdc7ec032016-11-21 15:32:52 -0800195
196// Regression test for https://crbug.com/667012
197TEST_F(FPDFEditEmbeddertest, RasterizePDF) {
198 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
199
200 // Get the bitmap for the original document/
Tom Sepeze08d2b12018-04-25 18:49:32 +0000201 ScopedFPDFBitmap orig_bitmap;
thestigdc7ec032016-11-21 15:32:52 -0800202 {
203 EXPECT_TRUE(OpenDocument("black.pdf"));
204 FPDF_PAGE orig_page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000205 ASSERT_TRUE(orig_page);
206 orig_bitmap = RenderLoadedPage(orig_page);
207 CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800208 UnloadPage(orig_page);
209 }
210
211 // Create a new document from |orig_bitmap| and save it.
212 {
213 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
214 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
215
216 // Add the bitmap to an image object and add the image object to the output
217 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700218 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000219 EXPECT_TRUE(
220 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get()));
thestigdc7ec032016-11-21 15:32:52 -0800221 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
222 FPDFPage_InsertObject(temp_page, temp_img);
223 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
224 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
225 FPDF_ClosePage(temp_page);
226 FPDF_CloseDocument(temp_doc);
227 }
thestigdc7ec032016-11-21 15:32:52 -0800228
229 // Get the generated content. Make sure it is at least as big as the original
230 // PDF.
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400231 EXPECT_GT(GetString().size(), 923U);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400232 VerifySavedDocument(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800233}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500234
235TEST_F(FPDFEditEmbeddertest, AddPaths) {
236 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500237 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000238 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500239
240 // We will first add a red rectangle
241 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000242 ASSERT_TRUE(red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500243 // Expect false when trying to set colors out of range
244 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
245 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
246
247 // Fill rectangle with red and insert to the page
248 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
249 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
Miklos Vajna491112b2018-05-30 13:30:10 +0000250
251 int fillmode = FPDF_FILLMODE_NONE;
252 FPDF_BOOL stroke = true;
253 EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke));
254 EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode);
255 EXPECT_FALSE(stroke);
256
Miklos Vajna97f4d672018-06-04 14:47:17 +0000257 double matrix_a = 1;
258 double matrix_b = 2;
259 double matrix_c = 3;
260 double matrix_d = 4;
261 double matrix_e = 5;
262 double matrix_f = 6;
263 EXPECT_FALSE(FPDFPath_SetMatrix(nullptr, matrix_a, matrix_b, matrix_c,
264 matrix_d, matrix_e, matrix_f));
265 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
266 matrix_d, matrix_e, matrix_f));
Miklos Vajnaac42dd22018-06-05 13:28:58 +0000267 // Set to 0 before FPDFPath_GetMatrix() to ensure they are actually set by
268 // the function.
269 matrix_a = 0;
270 matrix_b = 0;
271 matrix_c = 0;
272 matrix_d = 0;
273 matrix_e = 0;
274 matrix_f = 0;
Miklos Vajna97f4d672018-06-04 14:47:17 +0000275 EXPECT_FALSE(FPDFPath_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
276 &matrix_d, &matrix_e, &matrix_f));
277 EXPECT_TRUE(FPDFPath_GetMatrix(red_rect, &matrix_a, &matrix_b, &matrix_c,
278 &matrix_d, &matrix_e, &matrix_f));
279 EXPECT_EQ(1, static_cast<int>(matrix_a));
280 EXPECT_EQ(2, static_cast<int>(matrix_b));
281 EXPECT_EQ(3, static_cast<int>(matrix_c));
282 EXPECT_EQ(4, static_cast<int>(matrix_d));
283 EXPECT_EQ(5, static_cast<int>(matrix_e));
284 EXPECT_EQ(6, static_cast<int>(matrix_f));
285 // Set back the default
286 matrix_a = 1;
287 matrix_b = 0;
288 matrix_c = 0;
289 matrix_d = 1;
290 matrix_e = 0;
291 matrix_f = 0;
292 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
293 matrix_d, matrix_e, matrix_f));
294
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500295 FPDFPage_InsertObject(page, red_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000296 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000297 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000298 CompareBitmap(page_bitmap.get(), 612, 792,
299 "66d02eaa6181e2c069ce2ea99beda497");
300 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500301
302 // Now add to that a green rectangle with some medium alpha
303 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
304 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200305
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200306 // Make sure the type of the rectangle is a path.
307 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
308
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200309 // Make sure we get back the same color we set previously.
310 unsigned int R;
311 unsigned int G;
312 unsigned int B;
313 unsigned int A;
314 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
315 EXPECT_EQ(0U, R);
316 EXPECT_EQ(255U, G);
317 EXPECT_EQ(0U, B);
318 EXPECT_EQ(128U, A);
319
Miklos Vajna12abfd02017-09-15 07:49:03 +0200320 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
321 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200322 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200323 // Verify actual coordinates.
324 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
325 float x;
326 float y;
327 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
328 EXPECT_EQ(100, x);
329 EXPECT_EQ(100, y);
330 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
331 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
332 segment = FPDFPath_GetPathSegment(green_rect, 1);
333 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
334 EXPECT_EQ(100, x);
335 EXPECT_EQ(140, y);
336 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
337 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
338 segment = FPDFPath_GetPathSegment(green_rect, 2);
339 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
340 EXPECT_EQ(140, x);
341 EXPECT_EQ(140, y);
342 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
343 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
344 segment = FPDFPath_GetPathSegment(green_rect, 3);
345 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
346 EXPECT_EQ(140, x);
347 EXPECT_EQ(100, y);
348 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
349 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
350 segment = FPDFPath_GetPathSegment(green_rect, 4);
351 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
352 EXPECT_EQ(100, x);
353 EXPECT_EQ(100, y);
354 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
355 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200356
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500357 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
358 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000359 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000360 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000361 CompareBitmap(page_bitmap.get(), 612, 792,
362 "7b0b87604594e773add528fae567a558");
363 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500364
365 // Add a black triangle.
366 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
367 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
368 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
369 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
370 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
371 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200372
373 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
374 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200375 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200376 // Verify actual coordinates.
377 segment = FPDFPath_GetPathSegment(black_path, 0);
378 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
379 EXPECT_EQ(400, x);
380 EXPECT_EQ(100, y);
381 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
382 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
383 segment = FPDFPath_GetPathSegment(black_path, 1);
384 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
385 EXPECT_EQ(400, x);
386 EXPECT_EQ(200, y);
387 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
388 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
389 segment = FPDFPath_GetPathSegment(black_path, 2);
390 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
391 EXPECT_EQ(300, x);
392 EXPECT_EQ(100, y);
393 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
394 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
395 // Make sure out of bounds index access fails properly.
396 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200397
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500398 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000399 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000400 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000401 CompareBitmap(page_bitmap.get(), 612, 792,
402 "eadc8020a14dfcf091da2688733d8806");
403 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500404
405 // Now add a more complex blue path.
406 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
407 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
408 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
409 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
410 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
411 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
412 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
413 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
414 EXPECT_TRUE(FPDFPath_Close(blue_path));
415 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000416 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
417 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000418 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000419 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
420 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500421
422 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400423 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500424 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500425 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500426
427 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000428 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500429}
430
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000431TEST_F(FPDFEditEmbeddertest, SetText) {
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000432 // Load document with some text.
433 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
434 FPDF_PAGE page = LoadPage(0);
435 ASSERT_TRUE(page);
436
437 // Get the "Hello, world!" text object and change it.
438 ASSERT_EQ(2, FPDFPage_CountObjects(page));
439 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
440 ASSERT_TRUE(page_object);
441 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
442 GetFPDFWideString(L"Changed for SetText test");
443 EXPECT_TRUE(FPDFText_SetText(page_object, text1.get()));
444
445 // Verify the "Hello, world!" text is gone and "Changed for SetText test" is
446 // now displayed.
447 ASSERT_EQ(2, FPDFPage_CountObjects(page));
448#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
449 const char kChangedMD5[] = "94c1e7a5af7dd9d77dc2223b1091acb7";
450#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
451 const char kChangedMD5[] = "9d31703c1d1a3e1e9a778b1e297c9cd2";
452#else
453 const char kChangedMD5[] = "a0c4ea6620772991f66bf7130379b08a";
454#endif
455 {
456 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
457 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
458 }
459
460 // Now save the result.
461 EXPECT_TRUE(FPDFPage_GenerateContent(page));
462 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
463
464 UnloadPage(page);
465
466 // Re-open the file and check the changes were kept in the saved .pdf.
467 OpenSavedDocument();
468 FPDF_PAGE saved_page = LoadSavedPage(0);
469 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
470 {
471 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
472 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
473 }
474
475 CloseSavedPage(saved_page);
476 CloseSavedDocument();
477}
478
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000479TEST_F(FPDFEditEmbeddertest, RemovePageObject) {
480 // Load document with some text.
481 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
482 FPDF_PAGE page = LoadPage(0);
483 ASSERT_TRUE(page);
484
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000485 // Show what the original file looks like.
486 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000487#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000488 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000489#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000490 const char kOriginalMD5[] = "e5a6fa28298db07484cd922f3e210c88";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000491#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000492 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000493#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000494 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000495 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
496 }
497
498 // Get the "Hello, world!" text object and remove it.
499 ASSERT_EQ(2, FPDFPage_CountObjects(page));
500 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
501 ASSERT_TRUE(page_object);
502 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
503
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000504 // Verify the "Hello, world!" text is gone.
505 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000506#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000507 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000508#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000509 const char kRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000510#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000511 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000512#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000513 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000514 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
515 }
516 ASSERT_EQ(1, FPDFPage_CountObjects(page));
517
518 UnloadPage(page);
519 FPDFPageObj_Destroy(page_object);
520}
521
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000522void CheckMarkCounts(FPDF_PAGE page,
523 int start_from,
524 int expected_object_count,
525 size_t expected_prime_count,
526 size_t expected_square_count,
527 size_t expected_greater_than_ten_count,
528 size_t expected_bounds_count) {
529 int object_count = FPDFPage_CountObjects(page);
530 ASSERT_EQ(expected_object_count, object_count);
531
532 size_t prime_count = 0;
533 size_t square_count = 0;
534 size_t greater_than_ten_count = 0;
535 size_t bounds_count = 0;
536 for (int i = 0; i < object_count; ++i) {
537 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
538
539 int mark_count = FPDFPageObj_CountMarks(page_object);
540 for (int j = 0; j < mark_count; ++j) {
541 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
542
543 char buffer[256];
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000544 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000545 std::wstring name =
546 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
547 if (name == L"Prime") {
548 prime_count++;
549 } else if (name == L"Square") {
550 square_count++;
551 int expected_square = start_from + i;
552 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
553
554 unsigned long get_param_key_return =
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000555 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000556 ASSERT_GT(get_param_key_return, 0u);
557 EXPECT_EQ((6u + 1u) * 2u, get_param_key_return);
558 std::wstring key =
559 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
560 EXPECT_EQ(L"Factor", key);
561
562 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000563 FPDFPageObjMark_GetParamValueType(mark, "Factor"));
Henrique Nakashima140dead2018-07-11 21:40:03 +0000564 int square_root;
Henrique Nakashima94230e52018-07-11 22:02:02 +0000565 EXPECT_TRUE(
566 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &square_root));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000567 EXPECT_EQ(expected_square, square_root * square_root);
568 } else if (name == L"GreaterThanTen") {
569 greater_than_ten_count++;
570 } else if (name == L"Bounds") {
571 bounds_count++;
572 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
573
574 unsigned long get_param_key_return =
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000575 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000576 ASSERT_GT(get_param_key_return, 0u);
577 EXPECT_EQ((8u + 1u) * 2u, get_param_key_return);
578 std::wstring key =
579 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
580 EXPECT_EQ(L"Position", key);
581
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000582 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000583 FPDFPageObjMark_GetParamValueType(mark, "Position"));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000584 unsigned long length;
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000585 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
586 mark, "Position", buffer, sizeof(buffer), &length));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000587 ASSERT_GT(length, 0u);
Henrique Nakashima140dead2018-07-11 21:40:03 +0000588 std::wstring value =
589 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000590
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000591 // "Position" can be "First", "Last", or "End".
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000592 if (i == 0) {
593 EXPECT_EQ((5u + 1u) * 2u, length);
594 EXPECT_EQ(L"First", value);
595 } else if (i == object_count - 1) {
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000596 if (length == (4u + 1u) * 2u) {
597 EXPECT_EQ(L"Last", value);
598 } else if (length == (3u + 1u) * 2u) {
599 EXPECT_EQ(L"End", value);
600 } else {
601 FAIL();
602 }
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000603 } else {
604 FAIL();
605 }
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000606 } else {
607 FAIL();
608 }
609 }
610 }
611
612 // Expect certain number of tagged objects. The test file contains strings
613 // from 1 to 19.
614 EXPECT_EQ(expected_prime_count, prime_count);
615 EXPECT_EQ(expected_square_count, square_count);
616 EXPECT_EQ(expected_greater_than_ten_count, greater_than_ten_count);
617 EXPECT_EQ(expected_bounds_count, bounds_count);
618}
619
620TEST_F(FPDFEditEmbeddertest, ReadMarkedObjectsIndirectDict) {
621 // Load document with some text marked with an indirect property.
622 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
623 FPDF_PAGE page = LoadPage(0);
624 ASSERT_TRUE(page);
625
626 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
627
628 UnloadPage(page);
629}
630
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000631TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) {
632 // Load document with some text.
633 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
634 FPDF_PAGE page = LoadPage(0);
635 ASSERT_TRUE(page);
636
637 // Show what the original file looks like.
638 {
639#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
640 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
641#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
642 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
643#else
644 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
645#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000646 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000647 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
648 }
649
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000650 constexpr int expected_object_count = 19;
651 CheckMarkCounts(page, 1, expected_object_count, 8, 4, 9, 1);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000652
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000653 // Get all objects marked with "Prime"
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000654 std::vector<FPDF_PAGEOBJECT> primes;
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000655 for (int i = 0; i < expected_object_count; ++i) {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000656 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
657
658 int mark_count = FPDFPageObj_CountMarks(page_object);
659 for (int j = 0; j < mark_count; ++j) {
660 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
661
662 char buffer[256];
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000663 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000664 std::wstring name =
665 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
666 if (name == L"Prime") {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000667 primes.push_back(page_object);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000668 }
669 }
670 }
671
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000672 // Remove all objects marked with "Prime".
673 for (FPDF_PAGEOBJECT page_object : primes) {
674 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
675 FPDFPageObj_Destroy(page_object);
676 }
677
678 EXPECT_EQ(11, FPDFPage_CountObjects(page));
679
680 {
681#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
682 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
683#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
684 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
685#else
686 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
687#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000688 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000689 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
690 }
691
692 UnloadPage(page);
693}
694
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000695TEST_F(FPDFEditEmbeddertest, MaintainMarkedObjects) {
696 // Load document with some text.
697 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
698 FPDF_PAGE page = LoadPage(0);
699 ASSERT_TRUE(page);
700
701 // Iterate over all objects, counting the number of times each content mark
702 // name appears.
703 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
704
705 // Remove first page object.
706 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
707 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
708 FPDFPageObj_Destroy(page_object);
709
710 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
711
712 EXPECT_TRUE(FPDFPage_GenerateContent(page));
713 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
714
715 UnloadPage(page);
716
717 OpenSavedDocument();
718 FPDF_PAGE saved_page = LoadSavedPage(0);
719
720 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
721
722 CloseSavedPage(saved_page);
723 CloseSavedDocument();
724}
725
726TEST_F(FPDFEditEmbeddertest, MaintainIndirectMarkedObjects) {
727 // Load document with some text.
728 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
729 FPDF_PAGE page = LoadPage(0);
730 ASSERT_TRUE(page);
731
732 // Iterate over all objects, counting the number of times each content mark
733 // name appears.
734 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
735
736 // Remove first page object.
737 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
738 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
739 FPDFPageObj_Destroy(page_object);
740
741 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
742
743 EXPECT_TRUE(FPDFPage_GenerateContent(page));
744 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
745
746 UnloadPage(page);
747
748 OpenSavedDocument();
749 FPDF_PAGE saved_page = LoadSavedPage(0);
750
751 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
752
753 CloseSavedPage(saved_page);
754 CloseSavedDocument();
755}
756
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000757TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObject) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000758 // Load document with some text.
759 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
760 FPDF_PAGE page = LoadPage(0);
761 ASSERT_TRUE(page);
762
763 // Get the "Hello, world!" text object and remove it.
764 ASSERT_EQ(2, FPDFPage_CountObjects(page));
765 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
766 ASSERT_TRUE(page_object);
767 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
768
769 // Verify the "Hello, world!" text is gone.
770 ASSERT_EQ(1, FPDFPage_CountObjects(page));
771
772 // Save the file
773 EXPECT_TRUE(FPDFPage_GenerateContent(page));
774 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
775 UnloadPage(page);
776 FPDFPageObj_Destroy(page_object);
777
778 // Re-open the file and check the page object count is still 1.
779 OpenSavedDocument();
780 FPDF_PAGE saved_page = LoadSavedPage(0);
781 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
782 CloseSavedPage(saved_page);
783 CloseSavedDocument();
784}
785
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000786TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsNotLonely) {
787 // Load document with some text.
788 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
789 FPDF_PAGE page = LoadPage(0);
790 ASSERT_TRUE(page);
791
792 // Get the "Hello, world!" text object and remove it. There is another object
793 // in the same stream that says "Goodbye, world!"
794 ASSERT_EQ(3, FPDFPage_CountObjects(page));
795 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
796 ASSERT_TRUE(page_object);
797 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
798
799 // Verify the "Hello, world!" text is gone.
800 ASSERT_EQ(2, FPDFPage_CountObjects(page));
801#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
802 const char kHelloRemovedMD5[] = "e07a62d412728fc4d6e3ff42f2dd0e11";
803#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
804 const char kHelloRemovedMD5[] = "de37b0bb7ff903c1068bae361844be50";
805#else
806 const char kHelloRemovedMD5[] = "95b92950647a2190e1230911e7a1a0e9";
807#endif
808 {
809 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
810 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
811 }
812
813 // Save the file
814 EXPECT_TRUE(FPDFPage_GenerateContent(page));
815 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
816 UnloadPage(page);
817 FPDFPageObj_Destroy(page_object);
818
819 // Re-open the file and check the page object count is still 2.
820 OpenSavedDocument();
821 FPDF_PAGE saved_page = LoadSavedPage(0);
822
823 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
824 {
825 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
826 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
827 }
828
829 CloseSavedPage(saved_page);
830 CloseSavedDocument();
831}
832
833TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsLonely) {
834 // Load document with some text.
835 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
836 FPDF_PAGE page = LoadPage(0);
837 ASSERT_TRUE(page);
838
839 // Get the "Greetings, world!" text object and remove it. This is the only
840 // object in the stream.
841 ASSERT_EQ(3, FPDFPage_CountObjects(page));
842 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 2);
843 ASSERT_TRUE(page_object);
844 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
845
846 // Verify the "Greetings, world!" text is gone.
847 ASSERT_EQ(2, FPDFPage_CountObjects(page));
848#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
849 const char kGreetingsRemovedMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
850#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
851 const char kGreetingsRemovedMD5[] = "e5a6fa28298db07484cd922f3e210c88";
852#else
853 const char kGreetingsRemovedMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
854#endif
855 {
856 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
857 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
858 }
859
860 // Save the file
861 EXPECT_TRUE(FPDFPage_GenerateContent(page));
862 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
863 UnloadPage(page);
864 FPDFPageObj_Destroy(page_object);
865
866 // Re-open the file and check the page object count is still 2.
867 OpenSavedDocument();
868 FPDF_PAGE saved_page = LoadSavedPage(0);
869
870 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
871 {
872 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
873 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
874 }
875
876 CloseSavedPage(saved_page);
877 CloseSavedDocument();
878}
879
Henrique Nakashima6eb79392018-06-12 20:27:35 +0000880TEST_F(FPDFEditEmbeddertest, GetContentStream) {
881 // Load document with some text split across streams.
882 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
883 FPDF_PAGE page = LoadPage(0);
884 ASSERT_TRUE(page);
885
886 // Content stream 0: page objects 0-14.
887 // Content stream 1: page objects 15-17.
888 // Content stream 2: page object 18.
889 ASSERT_EQ(19, FPDFPage_CountObjects(page));
890 for (int i = 0; i < 19; i++) {
891 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
892 ASSERT_TRUE(page_object);
893 CPDF_PageObject* cpdf_page_object =
894 CPDFPageObjectFromFPDFPageObject(page_object);
895 if (i < 15)
896 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
897 else if (i < 18)
898 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
899 else
900 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
901 }
902
903 UnloadPage(page);
904}
905
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +0000906TEST_F(FPDFEditEmbeddertest, RemoveAllFromStream) {
907 // Load document with some text split across streams.
908 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
909 FPDF_PAGE page = LoadPage(0);
910 ASSERT_TRUE(page);
911
912 // Content stream 0: page objects 0-14.
913 // Content stream 1: page objects 15-17.
914 // Content stream 2: page object 18.
915 ASSERT_EQ(19, FPDFPage_CountObjects(page));
916
917 // Loop backwards because objects will being removed, which shifts the indexes
918 // after the removed position.
919 for (int i = 18; i >= 0; i--) {
920 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
921 ASSERT_TRUE(page_object);
922 CPDF_PageObject* cpdf_page_object =
923 CPDFPageObjectFromFPDFPageObject(page_object);
924
925 // Empty content stream 1.
926 if (cpdf_page_object->GetContentStream() == 1) {
927 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
928 FPDFPageObj_Destroy(page_object);
929 }
930 }
931
932 // Content stream 0: page objects 0-14.
933 // Content stream 2: page object 15.
934 ASSERT_EQ(16, FPDFPage_CountObjects(page));
935 for (int i = 0; i < 16; i++) {
936 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
937 ASSERT_TRUE(page_object);
938 CPDF_PageObject* cpdf_page_object =
939 CPDFPageObjectFromFPDFPageObject(page_object);
940 if (i < 15)
941 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
942 else
943 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
944 }
945
946 // Generate contents should remove the empty stream and update the page
947 // objects' contents stream indexes.
948 EXPECT_TRUE(FPDFPage_GenerateContent(page));
949
950 // Content stream 0: page objects 0-14.
951 // Content stream 1: page object 15.
952 ASSERT_EQ(16, FPDFPage_CountObjects(page));
953 for (int i = 0; i < 16; i++) {
954 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
955 ASSERT_TRUE(page_object);
956 CPDF_PageObject* cpdf_page_object =
957 CPDFPageObjectFromFPDFPageObject(page_object);
958 if (i < 15)
959 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
960 else
961 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
962 }
963
964#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
965 const char kStream1RemovedMD5[] = "d2e21fbd5a6de563f619feeeb6163331";
966#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
967 const char kStream1RemovedMD5[] = "b4140f203523e38793283a5943d8075b";
968#else
969 const char kStream1RemovedMD5[] = "e86a3efc160ede6cfcb1f59bcacf1105";
970#endif
971 {
972 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
973 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
974 }
975
976 // Save the file
977 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
978 UnloadPage(page);
979
980 // Re-open the file and check the page object count is still 16, and that
981 // content stream 1 was removed.
982 OpenSavedDocument();
983 FPDF_PAGE saved_page = LoadSavedPage(0);
984
985 // Content stream 0: page objects 0-14.
986 // Content stream 1: page object 15.
987 EXPECT_EQ(16, FPDFPage_CountObjects(saved_page));
988 for (int i = 0; i < 16; i++) {
989 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
990 ASSERT_TRUE(page_object);
991 CPDF_PageObject* cpdf_page_object =
992 CPDFPageObjectFromFPDFPageObject(page_object);
993 if (i < 15)
994 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
995 else
996 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
997 }
998
999 {
1000 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1001 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
1002 }
1003
1004 CloseSavedPage(saved_page);
1005 CloseSavedDocument();
1006}
1007
1008TEST_F(FPDFEditEmbeddertest, RemoveAllFromSingleStream) {
1009 // Load document with a single stream.
1010 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1011 FPDF_PAGE page = LoadPage(0);
1012 ASSERT_TRUE(page);
1013
1014 // Content stream 0: page objects 0-1.
1015 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1016
1017 // Loop backwards because objects will being removed, which shifts the indexes
1018 // after the removed position.
1019 for (int i = 1; i >= 0; i--) {
1020 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1021 ASSERT_TRUE(page_object);
1022 CPDF_PageObject* cpdf_page_object =
1023 CPDFPageObjectFromFPDFPageObject(page_object);
1024 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1025 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1026 FPDFPageObj_Destroy(page_object);
1027 }
1028
1029 // No more objects in the stream
1030 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1031
1032 // Generate contents should remove the empty stream and update the page
1033 // objects' contents stream indexes.
1034 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1035
1036 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1037
1038 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1039 {
1040 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1041 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1042 }
1043
1044 // Save the file
1045 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1046 UnloadPage(page);
1047
1048 // Re-open the file and check the page object count is still 0.
1049 OpenSavedDocument();
1050 FPDF_PAGE saved_page = LoadSavedPage(0);
1051
1052 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1053 {
1054 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1055 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1056 }
1057
1058 CloseSavedPage(saved_page);
1059 CloseSavedDocument();
1060}
1061
1062TEST_F(FPDFEditEmbeddertest, RemoveFirstFromSingleStream) {
1063 // Load document with a single stream.
1064 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1065 FPDF_PAGE page = LoadPage(0);
1066 ASSERT_TRUE(page);
1067
1068 // Content stream 0: page objects 0-1.
1069 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1070
1071 // Remove first object.
1072 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
1073 ASSERT_TRUE(page_object);
1074 CPDF_PageObject* cpdf_page_object =
1075 CPDFPageObjectFromFPDFPageObject(page_object);
1076 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1077 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1078 FPDFPageObj_Destroy(page_object);
1079
1080 // One object left in the stream.
1081 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1082 page_object = FPDFPage_GetObject(page, 0);
1083 ASSERT_TRUE(page_object);
1084 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1085 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1086
1087 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1088
1089 // Still one object left in the stream.
1090 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1091 page_object = FPDFPage_GetObject(page, 0);
1092 ASSERT_TRUE(page_object);
1093 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1094 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1095
1096#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1097 const char kFirstRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
1098#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1099 const char kFirstRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
1100#else
1101 const char kFirstRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
1102#endif
1103 {
1104 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1105 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1106 }
1107
1108 // Save the file
1109 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1110 UnloadPage(page);
1111
1112 // Re-open the file and check the page object count is still 0.
1113 OpenSavedDocument();
1114 FPDF_PAGE saved_page = LoadSavedPage(0);
1115
1116 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1117 page_object = FPDFPage_GetObject(saved_page, 0);
1118 ASSERT_TRUE(page_object);
1119 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1120 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1121 {
1122 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1123 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1124 }
1125
1126 CloseSavedPage(saved_page);
1127 CloseSavedDocument();
1128}
1129
1130TEST_F(FPDFEditEmbeddertest, RemoveLastFromSingleStream) {
1131 // Load document with a single stream.
1132 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1133 FPDF_PAGE page = LoadPage(0);
1134 ASSERT_TRUE(page);
1135
1136 // Content stream 0: page objects 0-1.
1137 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1138
1139 // Remove last object
1140 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 1);
1141 ASSERT_TRUE(page_object);
1142 CPDF_PageObject* cpdf_page_object =
1143 CPDFPageObjectFromFPDFPageObject(page_object);
1144 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1145 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1146 FPDFPageObj_Destroy(page_object);
1147
1148 // One object left in the stream.
1149 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1150 page_object = FPDFPage_GetObject(page, 0);
1151 ASSERT_TRUE(page_object);
1152 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1153 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1154
1155 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1156
1157 // Still one object left in the stream.
1158 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1159 page_object = FPDFPage_GetObject(page, 0);
1160 ASSERT_TRUE(page_object);
1161 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1162 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1163
1164#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1165 const char kLastRemovedMD5[] = "f8fbd14a048b9e2ea8e5f059f22a910e";
1166#else
1167 const char kLastRemovedMD5[] = "93dcc09055f87a2792c8e3065af99a1b";
1168#endif
1169 {
1170 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1171 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1172 }
1173
1174 // Save the file
1175 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1176 UnloadPage(page);
1177
1178 // Re-open the file and check the page object count is still 0.
1179 OpenSavedDocument();
1180 FPDF_PAGE saved_page = LoadSavedPage(0);
1181
1182 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1183 page_object = FPDFPage_GetObject(saved_page, 0);
1184 ASSERT_TRUE(page_object);
1185 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1186 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1187 {
1188 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1189 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1190 }
1191
1192 CloseSavedPage(saved_page);
1193 CloseSavedDocument();
1194}
1195
1196TEST_F(FPDFEditEmbeddertest, RemoveAllFromMultipleStreams) {
1197 // Load document with some text.
1198 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
1199 FPDF_PAGE page = LoadPage(0);
1200 ASSERT_TRUE(page);
1201
1202 // Content stream 0: page objects 0-1.
1203 // Content stream 1: page object 2.
1204 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1205
1206 // Loop backwards because objects will being removed, which shifts the indexes
1207 // after the removed position.
1208 for (int i = 2; i >= 0; i--) {
1209 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1210 ASSERT_TRUE(page_object);
1211 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1212 FPDFPageObj_Destroy(page_object);
1213 }
1214
1215 // No more objects in the page.
1216 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1217
1218 // Generate contents should remove the empty streams and update the page
1219 // objects' contents stream indexes.
1220 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1221
1222 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1223
1224 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1225 {
1226 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1227 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1228 }
1229
1230 // Save the file
1231 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1232 UnloadPage(page);
1233
1234 // Re-open the file and check the page object count is still 0.
1235 OpenSavedDocument();
1236 FPDF_PAGE saved_page = LoadSavedPage(0);
1237
1238 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1239 {
1240 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1241 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1242 }
1243
1244 CloseSavedPage(saved_page);
1245 CloseSavedDocument();
1246}
1247
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001248TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) {
1249 // Load document with some text.
1250 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1251 FPDF_PAGE page = LoadPage(0);
1252 ASSERT_TRUE(page);
1253
1254 // Add a red rectangle.
1255 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1256 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1257 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1258 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1259 FPDFPage_InsertObject(page, red_rect);
1260
1261 // Verify the red rectangle was added.
1262 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1263
1264 // Save the file
1265 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1266 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1267 UnloadPage(page);
1268
1269 // Re-open the file and check the page object count is still 3.
1270 OpenSavedDocument();
1271 FPDF_PAGE saved_page = LoadSavedPage(0);
1272 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1273 CloseSavedPage(saved_page);
1274 CloseSavedDocument();
1275}
1276
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001277TEST_F(FPDFEditEmbeddertest, InsertPageObjectEditAndSave) {
1278 // Load document with some text.
1279 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1280 FPDF_PAGE page = LoadPage(0);
1281 ASSERT_TRUE(page);
1282
1283 // Add a red rectangle.
1284 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1285 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1286 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 100, 100, 255));
1287 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1288 FPDFPage_InsertObject(page, red_rect);
1289
1290 // Verify the red rectangle was added.
1291 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1292
1293 // Generate content but change it again
1294 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1295 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1296
1297 // Save the file
1298 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1299 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1300 UnloadPage(page);
1301
1302 // Re-open the file and check the page object count is still 3.
1303 OpenSavedDocument();
1304 FPDF_PAGE saved_page = LoadSavedPage(0);
1305 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1306 CloseSavedPage(saved_page);
1307 CloseSavedDocument();
1308}
1309
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001310TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) {
1311 // Start with a blank page.
1312 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1313 ASSERT_TRUE(page);
1314
1315 // Render the blank page and verify it's a blank bitmap.
1316 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
1317 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001318 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001319 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1320 }
1321 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1322
1323 // Add a red rectangle.
1324 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1325 ASSERT_TRUE(red_rect);
1326 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1327 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1328 FPDFPage_InsertObject(page, red_rect);
1329 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
1330 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001331 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001332 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
1333 }
1334 EXPECT_EQ(1, FPDFPage_CountObjects(page));
1335
1336 // Remove rectangle and verify it does not render anymore and the bitmap is
1337 // back to a blank one.
1338 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
1339 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001340 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001341 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1342 }
1343 EXPECT_EQ(0, FPDFPage_CountObjects(page));
1344
1345 // Trying to remove an object not in the page should return false.
1346 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
1347
1348 FPDF_ClosePage(page);
1349 FPDFPageObj_Destroy(red_rect);
1350}
1351
Miklos Vajna12abfd02017-09-15 07:49:03 +02001352TEST_F(FPDFEditEmbeddertest, PathsPoints) {
1353 CreateNewDocument();
1354 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
1355 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +02001356 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001357
1358 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +02001359 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001360
Miklos Vajna36eed872017-09-20 22:52:43 +02001361 // FPDFPath_GetPathSegment() with a non-path.
1362 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
1363 // FPDFPath_GetPathSegment() with a NULL path.
1364 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
1365 float x;
1366 float y;
1367 // FPDFPathSegment_GetPoint() with a NULL segment.
1368 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
1369
1370 // FPDFPathSegment_GetType() with a NULL segment.
1371 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
1372
1373 // FPDFPathSegment_GetClose() with a NULL segment.
1374 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
1375
Miklos Vajna12abfd02017-09-15 07:49:03 +02001376 FPDFPageObj_Destroy(img);
1377}
1378
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001379TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
1380 // Load document with some text
1381 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1382 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001383 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001384
1385 // Add an opaque rectangle on top of some of the text.
1386 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1387 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1388 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1389 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001390
1391 // Add a transparent triangle on top of other part of the text.
1392 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
1393 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
1394 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
1395 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
1396 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
1397 EXPECT_TRUE(FPDFPath_Close(black_path));
1398 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001399
1400 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001401 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001402#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -07001403 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001404#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001405 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001406#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001407 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001408 UnloadPage(page);
1409}
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001410
wileyryae858aa42017-05-31 14:49:05 -05001411TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
1412 // Load document with existing content
1413 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
1414 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001415 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -05001416
1417 // Add a transparent rectangle on top of the existing content
1418 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
1419 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
1420 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
1421 FPDFPage_InsertObject(page, red_rect2);
1422
1423 // Add an opaque rectangle on top of the existing content
1424 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
1425 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1426 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1427 FPDFPage_InsertObject(page, red_rect);
1428
Tom Sepeze08d2b12018-04-25 18:49:32 +00001429 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001430 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001431 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1432
1433 // Now save the result, closing the page and document
1434 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -04001435 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -05001436
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001437 OpenSavedDocument();
Lei Zhang107fa7b2018-02-09 21:48:15 +00001438 FPDF_PAGE saved_page = LoadSavedPage(0);
1439 VerifySavedRendering(saved_page, 612, 792,
1440 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001441
1442 ClearString();
1443 // Add another opaque rectangle on top of the existing content
1444 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
1445 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
1446 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001447 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -05001448
1449 // Add another transparent rectangle on top of existing content
1450 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
1451 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
1452 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001453 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001454 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
1455 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001456 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001457 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
1458 }
Lei Zhang107fa7b2018-02-09 21:48:15 +00001459 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -05001460
1461 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +00001462 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001463
Lei Zhang107fa7b2018-02-09 21:48:15 +00001464 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001465 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -05001466
1467 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +00001468 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -05001469}
1470
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001471TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
1472 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001473 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001474
1475 // Add a large stroked rectangle (fill color should not affect it).
1476 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
1477 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
1478 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
1479 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
Miklos Vajna366df7f2018-05-22 14:27:29 +00001480
1481 float width = 0;
1482 EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width));
1483 EXPECT_EQ(15.0f, width);
1484
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001485 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
1486 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001487 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001488 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001489 CompareBitmap(page_bitmap.get(), 612, 792,
1490 "64bd31f862a89e0a9e505a5af6efd506");
1491 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001492
1493 // Add crossed-checkmark
1494 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
1495 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
1496 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
1497 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
1498 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
1499 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
1500 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
1501 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1502 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001503 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001504 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001505 CompareBitmap(page_bitmap.get(), 612, 792,
1506 "4b6f3b9d25c4e194821217d5016c3724");
1507 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001508
1509 // Add stroked and filled oval-ish path.
1510 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
1511 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
1512 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
1513 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
1514 EXPECT_TRUE(FPDFPath_Close(path));
1515 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
1516 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
1517 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
1518 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
1519 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001520 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001521 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001522 CompareBitmap(page_bitmap.get(), 612, 792,
1523 "ff3e6a22326754944cc6e56609acd73b");
1524 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001525 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001526}
Nicolas Pena49058402017-02-14 18:26:20 -05001527
Nicolas Pena4c48b102018-06-13 18:23:46 +00001528// Tests adding text from standard font using FPDFPageObj_NewTextObj.
Nicolas Pena49058402017-02-14 18:26:20 -05001529TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
1530 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001531 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -05001532
1533 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001534 FPDF_PAGEOBJECT text_object1 =
1535 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1536 EXPECT_TRUE(text_object1);
1537 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
1538 GetFPDFWideString(L"I'm at the bottom of the page");
1539 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
1540 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
1541 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001542 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001543 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001544#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001545 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -05001546#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001547 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001548#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001549 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1550 }
Nicolas Pena49058402017-02-14 18:26:20 -05001551
1552 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -04001553 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001554 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001555 EXPECT_TRUE(text_object2);
1556 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1557 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
1558 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1559 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
1560 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001561 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001562 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001563#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +00001564 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -04001565#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +00001566 const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d";
Nicolas Pena49058402017-02-14 18:26:20 -05001567#else
Dan Sinclair971a6742018-03-28 19:23:25 +00001568 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001569#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001570 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001571 }
Nicolas Pena49058402017-02-14 18:26:20 -05001572
1573 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -04001574 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001575 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001576 EXPECT_TRUE(text_object3);
1577 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
1578 GetFPDFWideString(L"Can you read me? <:)>");
1579 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
1580 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
1581 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001582 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001583 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001584#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001585 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -04001586#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001587 const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2";
Nicolas Pena49058402017-02-14 18:26:20 -05001588#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001589 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001590#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001591 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
1592 }
Nicolas Pena49058402017-02-14 18:26:20 -05001593
Miklos Vajnac765d2a2018-06-19 15:45:42 +00001594 double matrix_a = 0;
1595 double matrix_b = 0;
1596 double matrix_c = 0;
1597 double matrix_d = 0;
1598 double matrix_e = 0;
1599 double matrix_f = 0;
1600 EXPECT_FALSE(FPDFText_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1601 &matrix_d, &matrix_e, &matrix_f));
1602 EXPECT_TRUE(FPDFText_GetMatrix(text_object3, &matrix_a, &matrix_b, &matrix_c,
1603 &matrix_d, &matrix_e, &matrix_f));
1604 EXPECT_EQ(1., matrix_a);
1605 EXPECT_EQ(1.5, matrix_b);
1606 EXPECT_EQ(2., matrix_c);
1607 EXPECT_EQ(0.5, matrix_d);
1608 EXPECT_EQ(200., matrix_e);
1609 EXPECT_EQ(200., matrix_f);
1610
Miklos Vajna8625d3b2018-06-26 15:12:48 +00001611 EXPECT_EQ(0, FPDFTextObj_GetFontSize(nullptr));
1612 EXPECT_EQ(20, FPDFTextObj_GetFontSize(text_object3));
1613
Nicolas Pena49058402017-02-14 18:26:20 -05001614 // TODO(npm): Why are there issues with text rotated by 90 degrees?
1615 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
1616 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -05001617}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001618
Miklos Vajna1448cc12018-07-03 13:52:33 +00001619TEST_F(FPDFEditEmbeddertest, TestGetTextRenderMode) {
1620 EXPECT_TRUE(OpenDocument("text_render_mode.pdf"));
1621 FPDF_PAGE page = LoadPage(0);
1622 ASSERT_TRUE(page);
1623 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1624
1625 ASSERT_EQ(-1, FPDFText_GetTextRenderMode(nullptr));
1626
1627 FPDF_PAGEOBJECT fill = FPDFPage_GetObject(page, 0);
1628 ASSERT_EQ(FPDF_TEXTRENDERMODE_FILL, FPDFText_GetTextRenderMode(fill));
1629
1630 FPDF_PAGEOBJECT stroke = FPDFPage_GetObject(page, 1);
1631 ASSERT_EQ(FPDF_TEXTRENDERMODE_STROKE, FPDFText_GetTextRenderMode(stroke));
1632
1633 UnloadPage(page);
1634}
1635
Miklos Vajnab66077d2018-07-11 13:25:02 +00001636TEST_F(FPDFEditEmbeddertest, TestFormGetObjects) {
1637 EXPECT_TRUE(OpenDocument("form_object.pdf"));
1638 FPDF_PAGE page = LoadPage(0);
1639 ASSERT_TRUE(page);
1640 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1641
1642 FPDF_PAGEOBJECT form = FPDFPage_GetObject(page, 0);
1643 EXPECT_EQ(FPDF_PAGEOBJ_FORM, FPDFPageObj_GetType(form));
1644 ASSERT_EQ(-1, FPDFFormObj_CountObjects(nullptr));
1645 ASSERT_EQ(2, FPDFFormObj_CountObjects(form));
1646
1647 UnloadPage(page);
1648}
1649
Nicolas Pena4c48b102018-06-13 18:23:46 +00001650// Tests adding text from standard font using FPDFText_LoadStandardFont.
1651TEST_F(FPDFEditEmbeddertest, AddStandardFontText2) {
1652 // Start with a blank page
1653 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
1654
1655 // Load a standard font.
1656 FPDF_FONT font = FPDFText_LoadStandardFont(document(), "Helvetica");
1657 ASSERT_TRUE(font);
1658
1659 // Add some text to the page.
1660 FPDF_PAGEOBJECT text_object =
1661 FPDFPageObj_CreateTextObj(document(), font, 12.0f);
1662 EXPECT_TRUE(text_object);
1663 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1664 GetFPDFWideString(L"I'm at the bottom of the page");
1665 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1666 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20);
1667 FPDFPage_InsertObject(page.get(), text_object);
1668 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page.get(), nullptr, 0);
1669#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1670 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
1671#else
1672 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
1673#endif
1674 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1675}
1676
1677TEST_F(FPDFEditEmbeddertest, LoadStandardFonts) {
1678 CreateNewDocument();
1679 const char* standard_font_names[] = {"Arial",
1680 "Arial-Bold",
1681 "Arial-BoldItalic",
1682 "Arial-Italic",
1683 "Courier",
1684 "Courier-BoldOblique",
1685 "Courier-Oblique",
1686 "Courier-Bold",
1687 "CourierNew",
1688 "CourierNew-Bold",
1689 "CourierNew-BoldItalic",
1690 "CourierNew-Italic",
1691 "Helvetica",
1692 "Helvetica-Bold",
1693 "Helvetica-BoldOblique",
1694 "Helvetica-Oblique",
1695 "Symbol",
1696 "TimesNewRoman",
1697 "TimesNewRoman-Bold",
1698 "TimesNewRoman-BoldItalic",
1699 "TimesNewRoman-Italic",
1700 "ZapfDingbats"};
1701 for (auto* const font_name : standard_font_names) {
1702 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
1703 EXPECT_TRUE(font) << font_name << " should be considered a standard font.";
1704 }
1705 const char* not_standard_font_names[] = {
1706 "Abcdefg", "ArialB", "Arial-Style",
1707 "Font Name", "FontArial", "NotAStandardFontName",
1708 "TestFontName", "Quack", "Symbol-Italic",
1709 "Zapf"};
1710 for (auto* const font_name : not_standard_font_names) {
1711 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
1712 EXPECT_FALSE(font) << font_name
1713 << " should not be considered a standard font.";
1714 }
1715}
1716
Nicolas Pena603a31d2017-06-14 11:41:18 -04001717TEST_F(FPDFEditEmbeddertest, GraphicsData) {
1718 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +00001719 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001720
1721 // Create a rect with nontrivial graphics
1722 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1723 FPDFPageObj_SetBlendMode(rect1, "Color");
1724 FPDFPage_InsertObject(page.get(), rect1);
1725 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
1726
1727 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +00001728 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
1729 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -04001730 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001731 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001732
1733 // Add a text object causing no change to the graphics dictionary
1734 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1735 // Only alpha, the last component, matters for the graphics dictionary. And
1736 // the default value is 255.
1737 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
1738 FPDFPage_InsertObject(page.get(), text1);
1739 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001740 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001741
1742 // Add a text object increasing the size of the graphics dictionary
1743 FPDF_PAGEOBJECT text2 =
1744 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
1745 FPDFPage_InsertObject(page.get(), text2);
1746 FPDFPageObj_SetBlendMode(text2, "Darken");
1747 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
1748 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001749 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001750
1751 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -04001752 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -04001753 FPDFPageObj_SetBlendMode(path, "Darken");
1754 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
1755 FPDFPage_InsertObject(page.get(), path);
1756 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001757 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001758
1759 // Add a rect increasing the size of the graphics dictionary
1760 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1761 FPDFPageObj_SetBlendMode(rect2, "Darken");
1762 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
1763 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
1764 FPDFPage_InsertObject(page.get(), rect2);
1765 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001766 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001767}
1768
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001769TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
1770 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001771 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001772
1773 // Add a red rectangle with some non-default alpha
1774 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1775 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
1776 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
1777 FPDFPage_InsertObject(page, rect);
1778 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1779
1780 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +00001781 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
1782 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001783 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001784 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001785
1786 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +00001787 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001788 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001789 CompareBitmap(page_bitmap.get(), 612, 792,
1790 "5384da3406d62360ffb5cac4476fff1c");
1791 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001792
1793 // Never mind, my new favorite color is blue, increase alpha
1794 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
1795 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001796 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001797
1798 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +00001799 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001800 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001801 CompareBitmap(page_bitmap.get(), 612, 792,
1802 "2e51656f5073b0bee611d9cd086aa09c");
1803 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001804
1805 // And now generate, without changes
1806 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001807 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001808 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001809 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001810 CompareBitmap(page_bitmap.get(), 612, 792,
1811 "2e51656f5073b0bee611d9cd086aa09c");
1812 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001813
1814 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001815 FPDF_PAGEOBJECT text_object =
1816 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1817 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1818 GetFPDFWideString(L"Something something #text# something");
1819 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1820 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
1821 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001822 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001823 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001824 ASSERT_TRUE(font_dict);
1825 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1826
1827 // Generate yet again, check dicts are reasonably sized
1828 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001829 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001830 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1831 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001832}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001833
Nicolas Penad03ca422017-03-06 13:54:33 -05001834TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
1835 CreateNewDocument();
1836 // TODO(npm): use other fonts after disallowing loading any font as any type
1837 const CPDF_Font* stock_font =
1838 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001839 const uint8_t* data = stock_font->GetFont()->GetFontData();
1840 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001841 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001842 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
1843 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001844 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001845 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001846
Lei Zhang710fa992018-05-25 16:24:48 +00001847 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05001848 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1849 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
1850 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
1851 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1852 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1853 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05001854 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
1855
Lei Zhangde579ab2018-05-25 21:49:49 +00001856 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001857 ASSERT_TRUE(widths_array);
1858 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001859 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05001860 EXPECT_EQ(569, widths_array->GetNumberAt(11));
1861 EXPECT_EQ(500, widths_array->GetNumberAt(223));
1862 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
1863}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001864
Nicolas Penad03ca422017-03-06 13:54:33 -05001865TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
1866 CreateNewDocument();
1867 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001868 const uint8_t* data = stock_font->GetFont()->GetFontData();
1869 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001870 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001871 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
1872 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001873 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001874 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001875
Lei Zhang710fa992018-05-25 16:24:48 +00001876 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001877 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1878 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
1879 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
1880 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1881 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1882 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
1883 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05001884
Lei Zhangde579ab2018-05-25 21:49:49 +00001885 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001886 ASSERT_TRUE(widths_array);
1887 ASSERT_EQ(224U, widths_array->GetCount());
1888 EXPECT_EQ(600, widths_array->GetNumberAt(33));
1889 EXPECT_EQ(600, widths_array->GetNumberAt(74));
1890 EXPECT_EQ(600, widths_array->GetNumberAt(223));
1891 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
1892}
1893
1894TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
1895 CreateNewDocument();
1896 const CPDF_Font* stock_font =
1897 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001898 const uint8_t* data = stock_font->GetFont()->GetFontData();
1899 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001900 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001901 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
1902 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001903 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001904 EXPECT_TRUE(typed_font->IsCIDFont());
1905
1906 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001907 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001908 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1909 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1910 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
1911 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001912 const CPDF_Array* descendant_array =
1913 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001914 ASSERT_TRUE(descendant_array);
1915 EXPECT_EQ(1U, descendant_array->GetCount());
1916
1917 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001918 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001919 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1920 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
1921 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001922 const CPDF_Dictionary* cidinfo_dict =
1923 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001924 ASSERT_TRUE(cidinfo_dict);
1925 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1926 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1927 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1928 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
1929
1930 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001931 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001932 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001933 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -05001934 CheckCompositeFontWidths(widths_array, typed_font);
1935}
1936
1937TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
1938 CreateNewDocument();
1939 const CPDF_Font* stock_font =
1940 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001941 const uint8_t* data = stock_font->GetFont()->GetFontData();
1942 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -05001943
Tom Sepeze08d2b12018-04-25 18:49:32 +00001944 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001945 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1946 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001947 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001948 EXPECT_TRUE(typed_font->IsCIDFont());
1949
1950 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001951 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001952 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1953 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1954 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
1955 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001956 const CPDF_Array* descendant_array =
1957 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001958 ASSERT_TRUE(descendant_array);
1959 EXPECT_EQ(1U, descendant_array->GetCount());
1960
1961 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001962 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001963 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1964 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
1965 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001966 const CPDF_Dictionary* cidinfo_dict =
1967 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001968 ASSERT_TRUE(cidinfo_dict);
1969 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1970 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1971 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1972 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
1973 data);
1974
1975 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001976 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001977 ASSERT_TRUE(widths_array);
1978 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05001979}
rbpotterce8e51e2017-04-28 12:42:47 -07001980
1981TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
1982 // Load document with a -90 degree rotation
1983 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
1984 FPDF_PAGE page = LoadPage(0);
1985 EXPECT_NE(nullptr, page);
1986
1987 EXPECT_EQ(3, FPDFPage_GetRotation(page));
1988 UnloadPage(page);
1989}
Nicolas Penab3161852017-05-02 14:12:50 -04001990
1991TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
1992 // Start with a blank page
1993 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1994 {
1995 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001996 const uint8_t* data = stock_font->GetFont()->GetFontData();
1997 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001998 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001999 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
2000 ASSERT_TRUE(font.get());
2001
2002 // Add some text to the page
2003 FPDF_PAGEOBJECT text_object =
2004 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2005 EXPECT_TRUE(text_object);
2006 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2007 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2008 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2009 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2010 FPDFPage_InsertObject(page, text_object);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002011 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04002012#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002013 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
2014#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002015 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04002016#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002017 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04002018
2019 // Add some more text, same font
2020 FPDF_PAGEOBJECT text_object2 =
2021 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
2022 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2023 GetFPDFWideString(L"Bigger font size");
2024 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2025 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
2026 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04002027 }
Tom Sepeze08d2b12018-04-25 18:49:32 +00002028 ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04002029#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002030 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
2031#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002032 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04002033#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002034 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002035
Nicolas Pena207b7272017-05-26 17:37:06 -04002036 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04002037 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2038 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002039
2040 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002041}
Nicolas Penaf45ade32017-05-03 10:23:49 -04002042
Jane Liueda65252017-06-07 11:31:27 -04002043TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
2044 // Open a file with one annotation and load its first page.
2045 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00002046 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04002047 ASSERT_TRUE(page);
2048
Lei Zhanga21d5932018-02-05 18:28:38 +00002049 {
2050 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00002051 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00002052 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
2053 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04002054
Lei Zhanga21d5932018-02-05 18:28:38 +00002055 // FPDFPage_TransformAnnots() should run without errors when modifying
2056 // annotation rectangles.
2057 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
2058 }
Jane Liueda65252017-06-07 11:31:27 -04002059 UnloadPage(page);
2060}
2061
Nicolas Penaf45ade32017-05-03 10:23:49 -04002062// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04002063#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -04002064TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
2065 // Start with a blank page
2066 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2067 CFX_Font CIDfont;
2068 {
2069 // First, get the data from the font
2070 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
2071 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
2072 const uint8_t* data = CIDfont.GetFontData();
2073 const uint32_t size = CIDfont.GetSize();
2074
2075 // Load the data into a FPDF_Font.
Tom Sepeze08d2b12018-04-25 18:49:32 +00002076 ScopedFPDFFont font(
Nicolas Penaf45ade32017-05-03 10:23:49 -04002077 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
2078 ASSERT_TRUE(font.get());
2079
2080 // Add some text to the page
2081 FPDF_PAGEOBJECT text_object =
2082 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2083 ASSERT_TRUE(text_object);
2084 std::wstring wstr = L"ABCDEFGhijklmnop.";
2085 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2086 GetFPDFWideString(wstr);
2087 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2088 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
2089 FPDFPage_InsertObject(page, text_object);
2090
2091 // And add some Japanese characters
2092 FPDF_PAGEOBJECT text_object2 =
2093 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
2094 ASSERT_TRUE(text_object2);
2095 std::wstring wstr2 =
2096 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
2097 L"\u756A";
2098 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2099 GetFPDFWideString(wstr2);
2100 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2101 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
2102 FPDFPage_InsertObject(page, text_object2);
2103 }
2104
Nicolas Pena207b7272017-05-26 17:37:06 -04002105 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00002106 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00002107 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002108 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002109 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2110 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04002111
2112 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04002113 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04002114 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2115 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002116
2117 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04002118}
Dan Sinclair698aed72017-09-26 16:24:49 -04002119#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002120
2121TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04002122 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002123 {
2124 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
2125 FPDF_PAGE page = LoadPage(0);
2126 ASSERT_NE(nullptr, page);
2127
2128 // Now add a more complex blue path.
2129 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
2130 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
2131 // TODO(npm): stroking will cause the MD5s to differ.
2132 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
2133 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
2134 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
2135 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
2136 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
2137 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
2138 EXPECT_TRUE(FPDFPath_Close(green_path));
2139 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002140 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002141 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002142
2143 // Now save the result, closing the page and document
2144 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2145 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2146 UnloadPage(page);
2147 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002148
2149 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002150}
Jane Liu28fb7ba2017-08-02 21:45:57 -04002151
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002152TEST_F(FPDFEditEmbeddertest, AddMark) {
2153 // Load document with some text.
2154 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2155 FPDF_PAGE page = LoadPage(0);
2156 ASSERT_TRUE(page);
2157
2158 constexpr int kExpectedObjectCount = 19;
2159 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2160
2161 // Add to the first page object a "Bounds" mark with "Position": "First".
2162 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
2163 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(page_object, "Bounds");
2164 EXPECT_TRUE(mark);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002165 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2166 "Position", "First"));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002167
2168 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2169
2170 // Save the file
2171 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2172 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2173 UnloadPage(page);
2174
2175 // Re-open the file and check the new mark is present.
2176 OpenSavedDocument();
2177 FPDF_PAGE saved_page = LoadSavedPage(0);
2178
2179 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2180
2181 CloseSavedPage(saved_page);
2182 CloseSavedDocument();
2183}
2184
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002185TEST_F(FPDFEditEmbeddertest, SetMarkParam) {
2186 // Load document with some text.
2187 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2188 FPDF_PAGE page = LoadPage(0);
2189 ASSERT_TRUE(page);
2190
2191 constexpr int kExpectedObjectCount = 19;
2192 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2193
2194 // Check the "Bounds" mark's "Position" param is "Last".
2195 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2196 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2197 ASSERT_TRUE(mark);
2198 char buffer[256];
2199 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
2200 ASSERT_EQ(L"Bounds",
2201 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2202 unsigned long out_buffer_len;
2203 ASSERT_TRUE(FPDFPageObjMark_GetParamStringValue(
2204 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2205 ASSERT_EQ(L"Last",
2206 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2207
2208 // Set is to "End".
2209 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2210 "Position", "End"));
2211
2212 // Verify the object passed must correspond to the mark passed.
2213 FPDF_PAGEOBJECT another_page_object = FPDFPage_GetObject(page, 17);
2214 EXPECT_FALSE(FPDFPageObjMark_SetStringParam(document(), another_page_object,
2215 mark, "Position", "End"));
2216
2217 // Verify nothing else changed.
2218 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2219
2220 // Verify "Position" now maps to "End".
2221 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2222 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2223 EXPECT_EQ(L"End",
2224 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2225
2226 // Save the file
2227 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2228 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2229 UnloadPage(page);
2230
2231 // Re-open the file and cerify "Position" still maps to "End".
2232 OpenSavedDocument();
2233 FPDF_PAGE saved_page = LoadSavedPage(0);
2234
2235 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2236 page_object = FPDFPage_GetObject(saved_page, 18);
2237 mark = FPDFPageObj_GetMark(page_object, 1);
2238 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2239 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2240 EXPECT_EQ(L"End",
2241 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2242
2243 CloseSavedPage(saved_page);
2244 CloseSavedDocument();
2245}
2246
Henrique Nakashima144107d2018-07-10 21:04:05 +00002247TEST_F(FPDFEditEmbeddertest, AddMarkedText) {
2248 // Start with a blank page.
2249 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2250
2251 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
2252 const uint8_t* data = stock_font->GetFont()->GetFontData();
2253 const uint32_t size = stock_font->GetFont()->GetSize();
2254 ScopedFPDFFont font(
2255 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
2256 ASSERT_TRUE(font.get());
2257
2258 // Add some text to the page.
2259 FPDF_PAGEOBJECT text_object =
2260 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2261
2262 EXPECT_TRUE(text_object);
2263 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
2264 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2265 EXPECT_TRUE(FPDFText_SetText(text_object, text1.get()));
2266 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2267 FPDFPage_InsertObject(page, text_object);
2268
2269 // Add a mark with the tag "TestMarkName" to that text.
2270 EXPECT_EQ(0, FPDFPageObj_CountMarks(text_object));
2271 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(text_object, "TestMarkName");
2272 EXPECT_TRUE(mark);
2273 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2274 EXPECT_EQ(mark, FPDFPageObj_GetMark(text_object, 0));
2275 char buffer[256];
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002276 EXPECT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002277 std::wstring name =
2278 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2279 EXPECT_EQ(L"TestMarkName", name);
2280
2281 // Add parameters:
2282 // - int "IntKey" : 42
2283 // - string "StringKey": "StringValue"
Henrique Nakashima07520f62018-07-12 19:45:29 +00002284 // - blob "BlobKey": "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0"
2285 constexpr const size_t kBlobLen = 28;
2286 char kBlobValue[kBlobLen];
2287 memcpy(kBlobValue, "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0", kBlobLen);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002288 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002289 EXPECT_TRUE(
2290 FPDFPageObjMark_SetIntParam(document(), text_object, mark, "IntKey", 42));
2291 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), text_object, mark,
2292 "StringKey", "StringValue"));
2293 EXPECT_TRUE(FPDFPageObjMark_SetBlobParam(document(), text_object, mark,
2294 "BlobKey", kBlobValue, kBlobLen));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002295 EXPECT_EQ(3, FPDFPageObjMark_CountParams(mark));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002296
2297 // Check the two parameters can be retrieved.
2298 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002299 FPDFPageObjMark_GetParamValueType(mark, "IntKey"));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002300 int int_value;
Henrique Nakashima94230e52018-07-11 22:02:02 +00002301 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "IntKey", &int_value));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002302 EXPECT_EQ(42, int_value);
2303
2304 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002305 FPDFPageObjMark_GetParamValueType(mark, "StringKey"));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002306 unsigned long out_buffer_len;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002307 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2308 mark, "StringKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002309 EXPECT_GT(out_buffer_len, 0u);
2310 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2311 EXPECT_EQ(L"StringValue", name);
2312
Henrique Nakashima07520f62018-07-12 19:45:29 +00002313 EXPECT_EQ(FPDF_OBJECT_STRING,
2314 FPDFPageObjMark_GetParamValueType(mark, "BlobKey"));
2315 out_buffer_len = 0;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002316 EXPECT_TRUE(FPDFPageObjMark_GetParamBlobValue(
2317 mark, "BlobKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002318 EXPECT_EQ(kBlobLen, out_buffer_len);
2319 EXPECT_EQ(0, memcmp(kBlobValue, buffer, kBlobLen));
2320
Henrique Nakashima144107d2018-07-10 21:04:05 +00002321// Render and check the bitmap is the expected one.
2322#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
2323 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
2324#else
2325 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
2326#endif
2327 {
2328 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
2329 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2330 }
2331
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002332 // Now save the result.
2333 EXPECT_EQ(1, FPDFPage_CountObjects(page));
2334 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2335 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2336
Henrique Nakashima144107d2018-07-10 21:04:05 +00002337 FPDF_ClosePage(page);
2338
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002339 // Re-open the file and check the changes were kept in the saved .pdf.
2340 OpenSavedDocument();
2341 FPDF_PAGE saved_page = LoadSavedPage(0);
2342 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
2343
2344 text_object = FPDFPage_GetObject(saved_page, 0);
2345 EXPECT_TRUE(text_object);
2346 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2347 mark = FPDFPageObj_GetMark(text_object, 0);
2348 EXPECT_TRUE(mark);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002349 EXPECT_GT(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer)), 0u);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002350 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2351 EXPECT_EQ(L"TestMarkName", name);
2352
2353 CloseSavedPage(saved_page);
2354 CloseSavedDocument();
Henrique Nakashima144107d2018-07-10 21:04:05 +00002355}
2356
Jane Liu28fb7ba2017-08-02 21:45:57 -04002357TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
2358 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2359 FPDF_PAGE page = LoadPage(0);
2360 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002361 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04002362
2363 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2364 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2365 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
2366
2367 obj = FPDFPage_GetObject(page, 33);
2368 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2369 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
2370 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2371 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
2372 FPDFBitmap_Destroy(bitmap);
2373
2374 obj = FPDFPage_GetObject(page, 34);
2375 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2376 bitmap = FPDFImageObj_GetBitmap(obj);
2377 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2378 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
2379 FPDFBitmap_Destroy(bitmap);
2380
2381 obj = FPDFPage_GetObject(page, 35);
2382 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2383 bitmap = FPDFImageObj_GetBitmap(obj);
2384 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
2385 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
2386 FPDFBitmap_Destroy(bitmap);
2387
2388 obj = FPDFPage_GetObject(page, 36);
2389 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2390 bitmap = FPDFImageObj_GetBitmap(obj);
2391 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2392 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
2393 FPDFBitmap_Destroy(bitmap);
2394
2395 obj = FPDFPage_GetObject(page, 37);
2396 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2397 bitmap = FPDFImageObj_GetBitmap(obj);
2398 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2399 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
2400 FPDFBitmap_Destroy(bitmap);
2401
2402 obj = FPDFPage_GetObject(page, 38);
2403 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2404 bitmap = FPDFImageObj_GetBitmap(obj);
2405 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2406 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
2407 FPDFBitmap_Destroy(bitmap);
2408 UnloadPage(page);
2409}
Jane Liu548334e2017-08-03 16:33:40 -04002410
Lei Zhang53341dd2018-03-01 15:42:47 +00002411TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) {
2412 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
2413 FPDF_PAGE page = LoadPage(0);
2414 ASSERT_TRUE(page);
2415 ASSERT_EQ(1, FPDFPage_CountObjects(page));
2416
2417 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
2418 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2419 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002420 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00002421 ASSERT_TRUE(bitmap);
2422 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
2423 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00002424 }
2425
2426 UnloadPage(page);
2427}
2428
Jane Liu548334e2017-08-03 16:33:40 -04002429TEST_F(FPDFEditEmbeddertest, GetImageData) {
2430 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2431 FPDF_PAGE page = LoadPage(0);
2432 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002433 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04002434
2435 // Retrieve an image object with flate-encoded data stream.
2436 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
2437 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2438
2439 // Check that the raw image data has the correct length and hash value.
2440 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2441 std::vector<char> buf(len);
2442 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2443 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
2444 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2445
2446 // Check that the decoded image data has the correct length and hash value.
2447 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2448 buf.clear();
2449 buf.resize(len);
2450 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2451 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
2452 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2453
2454 // Retrieve an image obejct with DCTDecode-encoded data stream.
2455 obj = FPDFPage_GetObject(page, 37);
2456 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2457
2458 // Check that the raw image data has the correct length and hash value.
2459 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2460 buf.clear();
2461 buf.resize(len);
2462 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2463 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2464 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2465
2466 // Check that the decoded image data has the correct length and hash value,
2467 // which should be the same as those of the raw data, since this image is
2468 // encoded by a single DCTDecode filter and decoding is a noop.
2469 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2470 buf.clear();
2471 buf.resize(len);
2472 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2473 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2474 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2475
2476 UnloadPage(page);
2477}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04002478
2479TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
2480 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
2481 ASSERT_TRUE(rect);
2482
2483 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
2484 FPDFPageObj_Destroy(rect);
2485}
Jane Liube63ab92017-08-09 14:09:34 -04002486
2487TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
2488 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2489 FPDF_PAGE page = LoadPage(0);
2490 ASSERT_TRUE(page);
2491
2492 // Verify that retrieving the filter of a non-image object would fail.
2493 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2494 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2495 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
2496 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
2497
2498 // Verify the returned filter string for an image object with a single filter.
2499 obj = FPDFPage_GetObject(page, 33);
2500 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2501 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
2502 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
2503 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002504 static constexpr char kFlateDecode[] = "FlateDecode";
2505 EXPECT_EQ(sizeof(kFlateDecode),
2506 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
2507 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002508 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
2509
2510 // Verify all the filters for an image object with a list of filters.
2511 obj = FPDFPage_GetObject(page, 38);
2512 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2513 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
2514 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
2515 buf.clear();
2516 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002517 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
2518 EXPECT_EQ(sizeof(kASCIIHexDecode),
2519 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
2520 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002521
2522 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
2523 buf.clear();
2524 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002525 static constexpr char kDCTDecode[] = "DCTDecode";
2526 EXPECT_EQ(sizeof(kDCTDecode),
2527 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
2528 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002529
2530 UnloadPage(page);
2531}
Jane Liuca898292017-08-16 11:25:35 -04002532
2533TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
2534 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2535 FPDF_PAGE page = LoadPage(0);
2536 ASSERT_TRUE(page);
2537
2538 // Check that getting the metadata of a null object would fail.
2539 FPDF_IMAGEOBJ_METADATA metadata;
2540 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
2541
2542 // Check that receiving the metadata with a null metadata object would fail.
2543 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
2544 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
2545
2546 // Check that when retrieving an image object's metadata without passing in
2547 // |page|, all values are correct, with the last two being default values.
2548 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2549 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002550 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002551 EXPECT_EQ(92u, metadata.width);
2552 EXPECT_EQ(68u, metadata.height);
2553 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
2554 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
2555 EXPECT_EQ(0u, metadata.bits_per_pixel);
2556 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
2557
2558 // Verify the metadata of a bitmap image with indexed colorspace.
2559 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002560 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002561 EXPECT_EQ(92u, metadata.width);
2562 EXPECT_EQ(68u, metadata.height);
2563 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
2564 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
2565 EXPECT_EQ(1u, metadata.bits_per_pixel);
2566 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
2567
2568 // Verify the metadata of an image with RGB colorspace.
2569 obj = FPDFPage_GetObject(page, 37);
2570 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2571 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002572 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002573 EXPECT_EQ(126u, metadata.width);
2574 EXPECT_EQ(106u, metadata.height);
2575 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
2576 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
2577 EXPECT_EQ(24u, metadata.bits_per_pixel);
2578 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
2579
2580 UnloadPage(page);
2581}