blob: 6e03644ee452022b31ed98633c053ddc27cce140 [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"
Miklos Vajna46b43732018-08-14 19:15:43 +000011#include "core/fpdfapi/page/cpdf_formobject.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050012#include "core/fpdfapi/page/cpdf_page.h"
Henrique Nakashima6eb79392018-06-12 20:27:35 +000013#include "core/fpdfapi/page/cpdf_pageobject.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050014#include "core/fpdfapi/parser/cpdf_array.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050015#include "core/fpdfapi/parser/cpdf_dictionary.h"
Nicolas Penad03ca422017-03-06 13:54:33 -050016#include "core/fpdfapi/parser/cpdf_number.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050017#include "core/fpdfapi/parser/cpdf_stream.h"
Artem Strygineababa12018-06-06 12:31:18 +000018#include "core/fpdfapi/parser/cpdf_stream_acc.h"
Nicolas Pena0fc185e2017-02-08 12:13:20 -050019#include "core/fxcrt/fx_system.h"
Lei Zhang0dcb8fd2019-02-07 00:46:04 +000020#include "core/fxge/fx_font.h"
Dan Sinclair00d47a62018-03-28 18:39:04 +000021#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000022#include "public/cpp/fpdf_scopers.h"
Jane Liueda65252017-06-07 11:31:27 -040023#include "public/fpdf_annot.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080024#include "public/fpdf_edit.h"
25#include "public/fpdfview.h"
26#include "testing/embedder_test.h"
Lei Zhangb6992dd2019-02-05 23:30:20 +000027#include "testing/fx_string_testhelpers.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080028#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080029#include "testing/gtest/include/gtest/gtest.h"
Lei Zhang4c64e962019-02-05 19:24:12 +000030#include "testing/utils/hash.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080031
Lei Zhangab41f252018-12-23 03:10:50 +000032class FPDFEditEmbedderTest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050033 protected:
34 FPDF_DOCUMENT CreateNewDocument() {
35 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070036 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050037 return document_;
38 }
39
Lei Zhang710fa992018-05-25 16:24:48 +000040 void CheckFontDescriptor(const CPDF_Dictionary* font_dict,
Nicolas Penad03ca422017-03-06 13:54:33 -050041 int font_type,
42 bool bold,
43 bool italic,
Tom Sepez20c41a52018-08-29 23:53:53 +000044 pdfium::span<const uint8_t> span) {
Lei Zhangb1ec2802018-05-25 21:55:24 +000045 const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
Nicolas Penad03ca422017-03-06 13:54:33 -050046 ASSERT_TRUE(font_desc);
47 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
48 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
49 font_desc->GetStringFor("FontName"));
50
51 // Check that the font descriptor has the required keys according to spec
52 // 1.7 Table 5.19
53 ASSERT_TRUE(font_desc->KeyExist("Flags"));
Dan Sinclair10e1f052017-09-28 15:59:42 -040054
Nicolas Penad03ca422017-03-06 13:54:33 -050055 int font_flags = font_desc->GetIntegerFor("Flags");
Dan Sinclair10e1f052017-09-28 15:59:42 -040056 EXPECT_EQ(bold, FontStyleIsBold(font_flags));
57 EXPECT_EQ(italic, FontStyleIsItalic(font_flags));
58 EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags));
Nicolas Penad03ca422017-03-06 13:54:33 -050059 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
Nicolás Peña5f95f362017-09-28 13:00:45 +090060
Lei Zhangb1ec2802018-05-25 21:55:24 +000061 const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox");
Nicolás Peña5f95f362017-09-28 13:00:45 +090062 ASSERT_TRUE(fontBBox);
Lei Zhangf40380f2018-10-12 18:31:51 +000063 EXPECT_EQ(4u, fontBBox->size());
Nicolás Peña5f95f362017-09-28 13:00:45 +090064 // Check that the coordinates are in the preferred order according to spec
65 // 1.7 Section 3.8.4
66 EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2));
67 EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3));
68
Nicolas Penad03ca422017-03-06 13:54:33 -050069 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
70 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
71 EXPECT_TRUE(font_desc->KeyExist("Descent"));
72 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
73 EXPECT_TRUE(font_desc->KeyExist("StemV"));
Ryan Harrison275e2602017-09-18 14:23:18 -040074 ByteString present("FontFile");
75 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050076 if (font_type == FPDF_FONT_TRUETYPE)
77 std::swap(present, absent);
78 EXPECT_TRUE(font_desc->KeyExist(present));
79 EXPECT_FALSE(font_desc->KeyExist(absent));
80
Artem Strygineababa12018-06-06 12:31:18 +000081 auto streamAcc =
82 pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present));
83 streamAcc->LoadAllDataRaw();
84
Nicolas Penad03ca422017-03-06 13:54:33 -050085 // Check that the font stream is the one that was provided
Tom Sepez20c41a52018-08-29 23:53:53 +000086 ASSERT_EQ(span.size(), streamAcc->GetSize());
Nicolás Peña79eab232017-09-28 13:29:05 +090087 if (font_type == FPDF_FONT_TRUETYPE) {
Tom Sepez20c41a52018-08-29 23:53:53 +000088 ASSERT_EQ(static_cast<int>(span.size()),
Artem Strygineababa12018-06-06 12:31:18 +000089 streamAcc->GetDict()->GetIntegerFor("Length1"));
Nicolás Peña79eab232017-09-28 13:29:05 +090090 }
Artem Strygineababa12018-06-06 12:31:18 +000091
92 const uint8_t* stream_data = streamAcc->GetData();
Tom Sepez20c41a52018-08-29 23:53:53 +000093 for (size_t j = 0; j < span.size(); j++)
94 EXPECT_EQ(span[j], stream_data[j]) << " at byte " << j;
Nicolas Penad03ca422017-03-06 13:54:33 -050095 }
96
Lei Zhangde579ab2018-05-25 21:49:49 +000097 void CheckCompositeFontWidths(const CPDF_Array* widths_array,
Nicolas Penad03ca422017-03-06 13:54:33 -050098 CPDF_Font* typed_font) {
99 // Check that W array is in a format that conforms to PDF spec 1.7 section
100 // "Glyph Metrics in CIDFonts" (these checks are not
101 // implementation-specific).
Lei Zhangf40380f2018-10-12 18:31:51 +0000102 EXPECT_GT(widths_array->size(), 1u);
Nicolas Penad03ca422017-03-06 13:54:33 -0500103 int num_cids_checked = 0;
104 int cur_cid = 0;
Lei Zhangf40380f2018-10-12 18:31:51 +0000105 for (size_t idx = 0; idx < widths_array->size(); idx++) {
Nicolas Penad03ca422017-03-06 13:54:33 -0500106 int cid = widths_array->GetNumberAt(idx);
107 EXPECT_GE(cid, cur_cid);
Lei Zhangf40380f2018-10-12 18:31:51 +0000108 ASSERT_FALSE(++idx == widths_array->size());
Lei Zhangde579ab2018-05-25 21:49:49 +0000109 const CPDF_Object* next = widths_array->GetObjectAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500110 if (next->IsArray()) {
111 // We are in the c [w1 w2 ...] case
Lei Zhangde579ab2018-05-25 21:49:49 +0000112 const CPDF_Array* arr = next->AsArray();
Lei Zhangf40380f2018-10-12 18:31:51 +0000113 int cnt = static_cast<int>(arr->size());
Nicolas Penad03ca422017-03-06 13:54:33 -0500114 size_t inner_idx = 0;
115 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
Nicolas Pena23346602018-01-30 21:42:41 +0000116 uint32_t width = arr->GetNumberAt(inner_idx++);
Dan Sinclair971a6742018-03-28 19:23:25 +0000117 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
118 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500119 }
120 num_cids_checked += cnt;
121 continue;
122 }
123 // Otherwise, are in the c_first c_last w case.
124 ASSERT_TRUE(next->IsNumber());
125 int last_cid = next->AsNumber()->GetInteger();
Lei Zhangf40380f2018-10-12 18:31:51 +0000126 ASSERT_FALSE(++idx == widths_array->size());
Nicolas Pena23346602018-01-30 21:42:41 +0000127 uint32_t width = widths_array->GetNumberAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500128 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
Dan Sinclair971a6742018-03-28 19:23:25 +0000129 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
130 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500131 }
132 num_cids_checked += last_cid - cid + 1;
133 }
134 // Make sure we have a good amount of cids described
135 EXPECT_GT(num_cids_checked, 900);
136 }
137 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
138
139 private:
140 CPDF_Document* cpdf_doc_;
141};
Tom Sepezd483eb42016-01-06 10:03:59 -0800142
etienneb7712c262016-04-26 08:13:45 -0700143namespace {
thestigdc7ec032016-11-21 15:32:52 -0800144
etienneb7712c262016-04-26 08:13:45 -0700145const char kExpectedPDF[] =
146 "%PDF-1.7\r\n"
147 "%\xA1\xB3\xC5\xD7\r\n"
148 "1 0 obj\r\n"
149 "<</Pages 2 0 R /Type/Catalog>>\r\n"
150 "endobj\r\n"
151 "2 0 obj\r\n"
152 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
153 "endobj\r\n"
154 "3 0 obj\r\n"
155 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
156 "endobj\r\n"
157 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400158 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
159 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400160 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700161 ">>\r\n"
162 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400163 "5 0 obj\r\n"
164 "<</BM/Normal/CA 1/ca 1>>\r\n"
165 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700166 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400167 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700168 "0000000000 65535 f\r\n"
169 "0000000017 00000 n\r\n"
170 "0000000066 00000 n\r\n"
171 "0000000122 00000 n\r\n"
172 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400173 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700174 "trailer\r\n"
175 "<<\r\n"
176 "/Root 1 0 R\r\n"
177 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400178 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700179 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400180 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700181 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800182
etienneb7712c262016-04-26 08:13:45 -0700183} // namespace
184
Lei Zhangab41f252018-12-23 03:10:50 +0000185TEST_F(FPDFEditEmbedderTest, EmptyCreation) {
Tom Sepezd483eb42016-01-06 10:03:59 -0800186 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700187 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800188 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400189 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800190 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800191 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700192
Nicolas Penad9d6c292017-06-06 16:12:10 -0400193 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
194 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700195 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800196}
thestigdc7ec032016-11-21 15:32:52 -0800197
198// Regression test for https://crbug.com/667012
Lei Zhangab41f252018-12-23 03:10:50 +0000199TEST_F(FPDFEditEmbedderTest, RasterizePDF) {
thestigdc7ec032016-11-21 15:32:52 -0800200 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
201
Lei Zhangf0542892019-01-17 18:46:27 +0000202 // Get the bitmap for the original document.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000203 ScopedFPDFBitmap orig_bitmap;
thestigdc7ec032016-11-21 15:32:52 -0800204 {
205 EXPECT_TRUE(OpenDocument("black.pdf"));
206 FPDF_PAGE orig_page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000207 ASSERT_TRUE(orig_page);
208 orig_bitmap = RenderLoadedPage(orig_page);
209 CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800210 UnloadPage(orig_page);
211 }
212
213 // Create a new document from |orig_bitmap| and save it.
214 {
215 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
216 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
217
218 // Add the bitmap to an image object and add the image object to the output
219 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700220 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000221 EXPECT_TRUE(
222 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get()));
thestigdc7ec032016-11-21 15:32:52 -0800223 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
224 FPDFPage_InsertObject(temp_page, temp_img);
225 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
226 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
227 FPDF_ClosePage(temp_page);
228 FPDF_CloseDocument(temp_doc);
229 }
thestigdc7ec032016-11-21 15:32:52 -0800230
231 // Get the generated content. Make sure it is at least as big as the original
232 // PDF.
Lei Zhangd72fd582018-07-27 19:37:27 +0000233 EXPECT_GT(GetString().size(), 923u);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400234 VerifySavedDocument(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800235}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500236
Lei Zhangab41f252018-12-23 03:10:50 +0000237TEST_F(FPDFEditEmbedderTest, AddPaths) {
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500238 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500239 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000240 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500241
242 // We will first add a red rectangle
243 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000244 ASSERT_TRUE(red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500245 // Expect false when trying to set colors out of range
246 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
247 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
248
249 // Fill rectangle with red and insert to the page
250 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
251 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
Miklos Vajna491112b2018-05-30 13:30:10 +0000252
253 int fillmode = FPDF_FILLMODE_NONE;
254 FPDF_BOOL stroke = true;
255 EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke));
256 EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode);
257 EXPECT_FALSE(stroke);
258
Miklos Vajna97f4d672018-06-04 14:47:17 +0000259 double matrix_a = 1;
260 double matrix_b = 2;
261 double matrix_c = 3;
262 double matrix_d = 4;
263 double matrix_e = 5;
264 double matrix_f = 6;
265 EXPECT_FALSE(FPDFPath_SetMatrix(nullptr, matrix_a, matrix_b, matrix_c,
266 matrix_d, matrix_e, matrix_f));
267 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
268 matrix_d, matrix_e, matrix_f));
Miklos Vajnaac42dd22018-06-05 13:28:58 +0000269 // Set to 0 before FPDFPath_GetMatrix() to ensure they are actually set by
270 // the function.
271 matrix_a = 0;
272 matrix_b = 0;
273 matrix_c = 0;
274 matrix_d = 0;
275 matrix_e = 0;
276 matrix_f = 0;
Miklos Vajna97f4d672018-06-04 14:47:17 +0000277 EXPECT_FALSE(FPDFPath_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
278 &matrix_d, &matrix_e, &matrix_f));
279 EXPECT_TRUE(FPDFPath_GetMatrix(red_rect, &matrix_a, &matrix_b, &matrix_c,
280 &matrix_d, &matrix_e, &matrix_f));
281 EXPECT_EQ(1, static_cast<int>(matrix_a));
282 EXPECT_EQ(2, static_cast<int>(matrix_b));
283 EXPECT_EQ(3, static_cast<int>(matrix_c));
284 EXPECT_EQ(4, static_cast<int>(matrix_d));
285 EXPECT_EQ(5, static_cast<int>(matrix_e));
286 EXPECT_EQ(6, static_cast<int>(matrix_f));
287 // Set back the default
288 matrix_a = 1;
289 matrix_b = 0;
290 matrix_c = 0;
291 matrix_d = 1;
292 matrix_e = 0;
293 matrix_f = 0;
294 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
295 matrix_d, matrix_e, matrix_f));
296
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500297 FPDFPage_InsertObject(page, red_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000298 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000299 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000300 CompareBitmap(page_bitmap.get(), 612, 792,
301 "66d02eaa6181e2c069ce2ea99beda497");
302 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500303
304 // Now add to that a green rectangle with some medium alpha
305 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
306 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200307
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200308 // Make sure the type of the rectangle is a path.
309 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
310
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200311 // Make sure we get back the same color we set previously.
312 unsigned int R;
313 unsigned int G;
314 unsigned int B;
315 unsigned int A;
316 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
Lei Zhangd72fd582018-07-27 19:37:27 +0000317 EXPECT_EQ(0u, R);
318 EXPECT_EQ(255u, G);
319 EXPECT_EQ(0u, B);
320 EXPECT_EQ(128u, A);
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200321
Miklos Vajna12abfd02017-09-15 07:49:03 +0200322 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
323 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200324 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200325 // Verify actual coordinates.
326 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
327 float x;
328 float y;
329 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
330 EXPECT_EQ(100, x);
331 EXPECT_EQ(100, y);
332 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
333 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
334 segment = FPDFPath_GetPathSegment(green_rect, 1);
335 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
336 EXPECT_EQ(100, x);
337 EXPECT_EQ(140, y);
338 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
339 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
340 segment = FPDFPath_GetPathSegment(green_rect, 2);
341 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
342 EXPECT_EQ(140, x);
343 EXPECT_EQ(140, y);
344 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
345 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
346 segment = FPDFPath_GetPathSegment(green_rect, 3);
347 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
348 EXPECT_EQ(140, x);
349 EXPECT_EQ(100, y);
350 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
351 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
352 segment = FPDFPath_GetPathSegment(green_rect, 4);
353 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
354 EXPECT_EQ(100, x);
355 EXPECT_EQ(100, y);
356 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
357 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200358
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500359 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
360 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000361 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000362 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000363 CompareBitmap(page_bitmap.get(), 612, 792,
364 "7b0b87604594e773add528fae567a558");
365 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500366
367 // Add a black triangle.
368 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
369 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
370 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
371 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
372 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
373 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200374
375 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
376 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200377 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200378 // Verify actual coordinates.
379 segment = FPDFPath_GetPathSegment(black_path, 0);
380 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
381 EXPECT_EQ(400, x);
382 EXPECT_EQ(100, y);
383 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
384 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
385 segment = FPDFPath_GetPathSegment(black_path, 1);
386 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
387 EXPECT_EQ(400, x);
388 EXPECT_EQ(200, y);
389 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
390 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
391 segment = FPDFPath_GetPathSegment(black_path, 2);
392 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
393 EXPECT_EQ(300, x);
394 EXPECT_EQ(100, y);
395 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
396 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
397 // Make sure out of bounds index access fails properly.
398 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200399
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500400 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000401 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000402 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000403 CompareBitmap(page_bitmap.get(), 612, 792,
404 "eadc8020a14dfcf091da2688733d8806");
405 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500406
407 // Now add a more complex blue path.
408 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
409 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
410 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
411 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
412 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
413 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
414 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
415 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
416 EXPECT_TRUE(FPDFPath_Close(blue_path));
417 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000418 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
419 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000420 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000421 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
422 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500423
424 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400425 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500426 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500427 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500428
429 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000430 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500431}
432
Lei Zhangab41f252018-12-23 03:10:50 +0000433TEST_F(FPDFEditEmbedderTest, SetText) {
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000434 // Load document with some text.
435 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
436 FPDF_PAGE page = LoadPage(0);
437 ASSERT_TRUE(page);
438
439 // Get the "Hello, world!" text object and change it.
440 ASSERT_EQ(2, FPDFPage_CountObjects(page));
441 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
442 ASSERT_TRUE(page_object);
443 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
444 GetFPDFWideString(L"Changed for SetText test");
445 EXPECT_TRUE(FPDFText_SetText(page_object, text1.get()));
446
447 // Verify the "Hello, world!" text is gone and "Changed for SetText test" is
448 // now displayed.
449 ASSERT_EQ(2, FPDFPage_CountObjects(page));
450#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
451 const char kChangedMD5[] = "94c1e7a5af7dd9d77dc2223b1091acb7";
452#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000453 const char kChangedMD5[] = "3137fdb27962671f5c3963a5e965eff5";
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000454#else
455 const char kChangedMD5[] = "a0c4ea6620772991f66bf7130379b08a";
456#endif
457 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000458 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000459 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
460 }
461
462 // Now save the result.
463 EXPECT_TRUE(FPDFPage_GenerateContent(page));
464 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
465
466 UnloadPage(page);
467
468 // Re-open the file and check the changes were kept in the saved .pdf.
Lei Zhang0b494052019-01-31 21:41:15 +0000469 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000470 FPDF_PAGE saved_page = LoadSavedPage(0);
471 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
472 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000473 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000474 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
475 }
476
477 CloseSavedPage(saved_page);
478 CloseSavedDocument();
479}
480
Lei Zhangab41f252018-12-23 03:10:50 +0000481TEST_F(FPDFEditEmbedderTest, RemovePageObject) {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000482 // Load document with some text.
483 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
484 FPDF_PAGE page = LoadPage(0);
485 ASSERT_TRUE(page);
486
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000487 // Show what the original file looks like.
488 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000489#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000490 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000491#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000492 const char kOriginalMD5[] = "795b7ce1626931aa06af0fa23b7d80bb";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000493#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000494 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000495#endif
Lei Zhang30ff2532019-01-31 21:37:55 +0000496 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000497 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
498 }
499
500 // Get the "Hello, world!" text object and remove it.
501 ASSERT_EQ(2, FPDFPage_CountObjects(page));
502 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
503 ASSERT_TRUE(page_object);
504 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
505
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000506 // Verify the "Hello, world!" text is gone.
507 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000508#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000509 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000510#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000511 const char kRemovedMD5[] = "aae6c5334721f90ec30d3d59f4ef7deb";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000512#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000513 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000514#endif
Lei Zhang30ff2532019-01-31 21:37:55 +0000515 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000516 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
517 }
518 ASSERT_EQ(1, FPDFPage_CountObjects(page));
519
520 UnloadPage(page);
521 FPDFPageObj_Destroy(page_object);
522}
523
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000524void CheckMarkCounts(FPDF_PAGE page,
525 int start_from,
526 int expected_object_count,
527 size_t expected_prime_count,
528 size_t expected_square_count,
529 size_t expected_greater_than_ten_count,
530 size_t expected_bounds_count) {
531 int object_count = FPDFPage_CountObjects(page);
532 ASSERT_EQ(expected_object_count, object_count);
533
534 size_t prime_count = 0;
535 size_t square_count = 0;
536 size_t greater_than_ten_count = 0;
537 size_t bounds_count = 0;
538 for (int i = 0; i < object_count; ++i) {
539 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
540
541 int mark_count = FPDFPageObj_CountMarks(page_object);
542 for (int j = 0; j < mark_count; ++j) {
543 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
544
545 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000546 unsigned long name_len = 999u;
547 ASSERT_TRUE(
548 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
549 EXPECT_GT(name_len, 0u);
550 EXPECT_NE(999u, name_len);
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000551 std::wstring name =
552 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
553 if (name == L"Prime") {
554 prime_count++;
555 } else if (name == L"Square") {
556 square_count++;
557 int expected_square = start_from + i;
558 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
559
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000560 unsigned long get_param_key_return = 999u;
561 ASSERT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer),
562 &get_param_key_return));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000563 EXPECT_EQ((6u + 1u) * 2u, get_param_key_return);
564 std::wstring key =
565 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
566 EXPECT_EQ(L"Factor", key);
567
568 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000569 FPDFPageObjMark_GetParamValueType(mark, "Factor"));
Henrique Nakashima140dead2018-07-11 21:40:03 +0000570 int square_root;
Henrique Nakashima94230e52018-07-11 22:02:02 +0000571 EXPECT_TRUE(
572 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &square_root));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000573 EXPECT_EQ(expected_square, square_root * square_root);
574 } else if (name == L"GreaterThanTen") {
575 greater_than_ten_count++;
576 } else if (name == L"Bounds") {
577 bounds_count++;
578 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
579
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000580 unsigned long get_param_key_return = 999u;
581 ASSERT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer),
582 &get_param_key_return));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000583 EXPECT_EQ((8u + 1u) * 2u, get_param_key_return);
584 std::wstring key =
585 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
586 EXPECT_EQ(L"Position", key);
587
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000588 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000589 FPDFPageObjMark_GetParamValueType(mark, "Position"));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000590 unsigned long length;
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000591 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
592 mark, "Position", buffer, sizeof(buffer), &length));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000593 ASSERT_GT(length, 0u);
Henrique Nakashima140dead2018-07-11 21:40:03 +0000594 std::wstring value =
595 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000596
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000597 // "Position" can be "First", "Last", or "End".
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000598 if (i == 0) {
599 EXPECT_EQ((5u + 1u) * 2u, length);
600 EXPECT_EQ(L"First", value);
601 } else if (i == object_count - 1) {
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000602 if (length == (4u + 1u) * 2u) {
603 EXPECT_EQ(L"Last", value);
604 } else if (length == (3u + 1u) * 2u) {
605 EXPECT_EQ(L"End", value);
606 } else {
607 FAIL();
608 }
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000609 } else {
610 FAIL();
611 }
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000612 } else {
613 FAIL();
614 }
615 }
616 }
617
618 // Expect certain number of tagged objects. The test file contains strings
619 // from 1 to 19.
620 EXPECT_EQ(expected_prime_count, prime_count);
621 EXPECT_EQ(expected_square_count, square_count);
622 EXPECT_EQ(expected_greater_than_ten_count, greater_than_ten_count);
623 EXPECT_EQ(expected_bounds_count, bounds_count);
624}
625
Lei Zhangab41f252018-12-23 03:10:50 +0000626TEST_F(FPDFEditEmbedderTest, ReadMarkedObjectsIndirectDict) {
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000627 // Load document with some text marked with an indirect property.
628 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
629 FPDF_PAGE page = LoadPage(0);
630 ASSERT_TRUE(page);
631
632 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
633
634 UnloadPage(page);
635}
636
Lei Zhangab41f252018-12-23 03:10:50 +0000637TEST_F(FPDFEditEmbedderTest, RemoveMarkedObjectsPrime) {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000638 // Load document with some text.
639 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
640 FPDF_PAGE page = LoadPage(0);
641 ASSERT_TRUE(page);
642
643 // Show what the original file looks like.
644 {
645#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
646 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
647#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
648 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
649#else
650 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
651#endif
Lei Zhang30ff2532019-01-31 21:37:55 +0000652 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000653 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
654 }
655
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000656 constexpr int expected_object_count = 19;
657 CheckMarkCounts(page, 1, expected_object_count, 8, 4, 9, 1);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000658
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000659 // Get all objects marked with "Prime"
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000660 std::vector<FPDF_PAGEOBJECT> primes;
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000661 for (int i = 0; i < expected_object_count; ++i) {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000662 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
663
664 int mark_count = FPDFPageObj_CountMarks(page_object);
665 for (int j = 0; j < mark_count; ++j) {
666 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
667
668 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000669 unsigned long name_len = 999u;
670 ASSERT_TRUE(
671 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
672 EXPECT_GT(name_len, 0u);
673 EXPECT_NE(999u, name_len);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000674 std::wstring name =
675 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
676 if (name == L"Prime") {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000677 primes.push_back(page_object);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000678 }
679 }
680 }
681
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000682 // Remove all objects marked with "Prime".
683 for (FPDF_PAGEOBJECT page_object : primes) {
684 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
685 FPDFPageObj_Destroy(page_object);
686 }
687
688 EXPECT_EQ(11, FPDFPage_CountObjects(page));
689
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000690#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000691 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000692#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000693 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000694#else
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000695 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000696#endif
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000697 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000698 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000699 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
700 }
701
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000702 // Save the file.
703 EXPECT_TRUE(FPDFPage_GenerateContent(page));
704 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000705 UnloadPage(page);
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000706
707 // Re-open the file and check the prime marks are not there anymore.
Lei Zhang0b494052019-01-31 21:41:15 +0000708 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000709 FPDF_PAGE saved_page = LoadSavedPage(0);
710 EXPECT_EQ(11, FPDFPage_CountObjects(saved_page));
711
712 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000713 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000714 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
715 }
716
717 CloseSavedPage(saved_page);
718 CloseSavedDocument();
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000719}
720
Lei Zhangab41f252018-12-23 03:10:50 +0000721TEST_F(FPDFEditEmbedderTest, RemoveMarks) {
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000722 // Load document with some text.
723 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
724 FPDF_PAGE page = LoadPage(0);
725 ASSERT_TRUE(page);
726
727 constexpr int kExpectedObjectCount = 19;
728 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
729
730 // Remove all "Prime" content marks.
731 for (int i = 0; i < kExpectedObjectCount; ++i) {
732 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
733
734 int mark_count = FPDFPageObj_CountMarks(page_object);
735 for (int j = mark_count - 1; j >= 0; --j) {
736 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
737
738 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000739 unsigned long name_len = 999u;
740 ASSERT_TRUE(
741 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
742 EXPECT_GT(name_len, 0u);
743 EXPECT_NE(999u, name_len);
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000744 std::wstring name =
745 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
746 if (name == L"Prime") {
747 // Remove mark.
748 EXPECT_TRUE(FPDFPageObj_RemoveMark(page_object, mark));
749
750 // Verify there is now one fewer mark in the page object.
751 EXPECT_EQ(mark_count - 1, FPDFPageObj_CountMarks(page_object));
752 }
753 }
754 }
755
756 // Verify there are 0 "Prime" content marks now.
757 CheckMarkCounts(page, 1, kExpectedObjectCount, 0, 4, 9, 1);
758
759 // Save the file.
760 EXPECT_TRUE(FPDFPage_GenerateContent(page));
761 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
762 UnloadPage(page);
763
764 // Re-open the file and check the prime marks are not there anymore.
Lei Zhang0b494052019-01-31 21:41:15 +0000765 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000766 FPDF_PAGE saved_page = LoadSavedPage(0);
767
768 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 0, 4, 9, 1);
769
770 CloseSavedPage(saved_page);
771 CloseSavedDocument();
772}
773
Lei Zhangab41f252018-12-23 03:10:50 +0000774TEST_F(FPDFEditEmbedderTest, RemoveMarkParam) {
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000775 // Load document with some text.
776 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
777 FPDF_PAGE page = LoadPage(0);
778 ASSERT_TRUE(page);
779
780 constexpr int kExpectedObjectCount = 19;
781 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
782
783 // Remove all "Square" content marks parameters.
784 for (int i = 0; i < kExpectedObjectCount; ++i) {
785 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
786
787 int mark_count = FPDFPageObj_CountMarks(page_object);
788 for (int j = 0; j < mark_count; ++j) {
789 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
790
791 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000792 unsigned long name_len = 999u;
793 ASSERT_TRUE(
794 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
795 EXPECT_GT(name_len, 0u);
796 EXPECT_NE(999u, name_len);
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000797 std::wstring name =
798 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
799 if (name == L"Square") {
800 // Show the mark has a "Factor" parameter.
801 int out_value;
802 EXPECT_TRUE(
803 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
804
805 // Remove parameter.
806 EXPECT_TRUE(FPDFPageObjMark_RemoveParam(page_object, mark, "Factor"));
807
808 // Verify the "Factor" parameter is gone.
809 EXPECT_FALSE(
810 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
811 }
812 }
813 }
814
815 // Save the file.
816 EXPECT_TRUE(FPDFPage_GenerateContent(page));
817 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
818 UnloadPage(page);
819
820 // Re-open the file and check the "Factor" parameters are still gone.
Lei Zhang0b494052019-01-31 21:41:15 +0000821 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000822 FPDF_PAGE saved_page = LoadSavedPage(0);
823
824 size_t square_count = 0;
825 for (int i = 0; i < kExpectedObjectCount; ++i) {
826 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
827
828 int mark_count = FPDFPageObj_CountMarks(page_object);
829 for (int j = 0; j < mark_count; ++j) {
830 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
831
832 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000833 unsigned long name_len = 999u;
834 ASSERT_TRUE(
835 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
836 EXPECT_GT(name_len, 0u);
837 EXPECT_NE(999u, name_len);
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000838 std::wstring name =
839 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
840 if (name == L"Square") {
841 // Verify the "Factor" parameter is still gone.
842 int out_value;
843 EXPECT_FALSE(
844 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
845
846 ++square_count;
847 }
848 }
849 }
850
851 // Verify the parameters are gone, but the marks are not.
852 EXPECT_EQ(4u, square_count);
853
854 CloseSavedPage(saved_page);
855 CloseSavedDocument();
856}
857
Lei Zhangab41f252018-12-23 03:10:50 +0000858TEST_F(FPDFEditEmbedderTest, MaintainMarkedObjects) {
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000859 // Load document with some text.
860 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
861 FPDF_PAGE page = LoadPage(0);
862 ASSERT_TRUE(page);
863
864 // Iterate over all objects, counting the number of times each content mark
865 // name appears.
866 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
867
868 // Remove first page object.
869 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
870 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
871 FPDFPageObj_Destroy(page_object);
872
873 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
874
875 EXPECT_TRUE(FPDFPage_GenerateContent(page));
876 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
877
878 UnloadPage(page);
879
Lei Zhang0b494052019-01-31 21:41:15 +0000880 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000881 FPDF_PAGE saved_page = LoadSavedPage(0);
882
883 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
884
885 CloseSavedPage(saved_page);
886 CloseSavedDocument();
887}
888
Lei Zhangab41f252018-12-23 03:10:50 +0000889TEST_F(FPDFEditEmbedderTest, MaintainIndirectMarkedObjects) {
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000890 // Load document with some text.
891 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
892 FPDF_PAGE page = LoadPage(0);
893 ASSERT_TRUE(page);
894
895 // Iterate over all objects, counting the number of times each content mark
896 // name appears.
897 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
898
899 // Remove first page object.
900 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
901 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
902 FPDFPageObj_Destroy(page_object);
903
904 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
905
906 EXPECT_TRUE(FPDFPage_GenerateContent(page));
907 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
908
909 UnloadPage(page);
910
Lei Zhang0b494052019-01-31 21:41:15 +0000911 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000912 FPDF_PAGE saved_page = LoadSavedPage(0);
913
914 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
915
916 CloseSavedPage(saved_page);
917 CloseSavedDocument();
918}
919
Lei Zhangab41f252018-12-23 03:10:50 +0000920TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObject) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000921 // Load document with some text.
922 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
923 FPDF_PAGE page = LoadPage(0);
924 ASSERT_TRUE(page);
925
926 // Get the "Hello, world!" text object and remove it.
927 ASSERT_EQ(2, FPDFPage_CountObjects(page));
928 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
929 ASSERT_TRUE(page_object);
930 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
931
932 // Verify the "Hello, world!" text is gone.
933 ASSERT_EQ(1, FPDFPage_CountObjects(page));
934
935 // Save the file
936 EXPECT_TRUE(FPDFPage_GenerateContent(page));
937 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
938 UnloadPage(page);
939 FPDFPageObj_Destroy(page_object);
940
941 // Re-open the file and check the page object count is still 1.
Lei Zhang0b494052019-01-31 21:41:15 +0000942 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000943 FPDF_PAGE saved_page = LoadSavedPage(0);
944 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
945 CloseSavedPage(saved_page);
946 CloseSavedDocument();
947}
948
Lei Zhangab41f252018-12-23 03:10:50 +0000949TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObjectSplitStreamsNotLonely) {
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000950 // Load document with some text.
951 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
952 FPDF_PAGE page = LoadPage(0);
953 ASSERT_TRUE(page);
954
955 // Get the "Hello, world!" text object and remove it. There is another object
956 // in the same stream that says "Goodbye, world!"
957 ASSERT_EQ(3, FPDFPage_CountObjects(page));
958 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
959 ASSERT_TRUE(page_object);
960 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
961
962 // Verify the "Hello, world!" text is gone.
963 ASSERT_EQ(2, FPDFPage_CountObjects(page));
964#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
965 const char kHelloRemovedMD5[] = "e07a62d412728fc4d6e3ff42f2dd0e11";
966#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000967 const char kHelloRemovedMD5[] = "a97d4c72c969ba373c2dce675d277e65";
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000968#else
969 const char kHelloRemovedMD5[] = "95b92950647a2190e1230911e7a1a0e9";
970#endif
971 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000972 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000973 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
974 }
975
976 // Save the file
977 EXPECT_TRUE(FPDFPage_GenerateContent(page));
978 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
979 UnloadPage(page);
980 FPDFPageObj_Destroy(page_object);
981
982 // Re-open the file and check the page object count is still 2.
Lei Zhang0b494052019-01-31 21:41:15 +0000983 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000984 FPDF_PAGE saved_page = LoadSavedPage(0);
985
986 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
987 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000988 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000989 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
990 }
991
992 CloseSavedPage(saved_page);
993 CloseSavedDocument();
994}
995
Lei Zhangab41f252018-12-23 03:10:50 +0000996TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObjectSplitStreamsLonely) {
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000997 // Load document with some text.
998 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
999 FPDF_PAGE page = LoadPage(0);
1000 ASSERT_TRUE(page);
1001
1002 // Get the "Greetings, world!" text object and remove it. This is the only
1003 // object in the stream.
1004 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1005 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 2);
1006 ASSERT_TRUE(page_object);
1007 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
1008
1009 // Verify the "Greetings, world!" text is gone.
1010 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1011#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1012 const char kGreetingsRemovedMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
1013#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001014 const char kGreetingsRemovedMD5[] = "795b7ce1626931aa06af0fa23b7d80bb";
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001015#else
1016 const char kGreetingsRemovedMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
1017#endif
1018 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001019 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001020 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
1021 }
1022
1023 // Save the file
1024 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1025 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1026 UnloadPage(page);
1027 FPDFPageObj_Destroy(page_object);
1028
1029 // Re-open the file and check the page object count is still 2.
Lei Zhang0b494052019-01-31 21:41:15 +00001030 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001031 FPDF_PAGE saved_page = LoadSavedPage(0);
1032
1033 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
1034 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001035 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001036 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
1037 }
1038
1039 CloseSavedPage(saved_page);
1040 CloseSavedDocument();
1041}
1042
Lei Zhangab41f252018-12-23 03:10:50 +00001043TEST_F(FPDFEditEmbedderTest, GetContentStream) {
Henrique Nakashima6eb79392018-06-12 20:27:35 +00001044 // Load document with some text split across streams.
1045 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
1046 FPDF_PAGE page = LoadPage(0);
1047 ASSERT_TRUE(page);
1048
1049 // Content stream 0: page objects 0-14.
1050 // Content stream 1: page objects 15-17.
1051 // Content stream 2: page object 18.
1052 ASSERT_EQ(19, FPDFPage_CountObjects(page));
1053 for (int i = 0; i < 19; i++) {
1054 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1055 ASSERT_TRUE(page_object);
1056 CPDF_PageObject* cpdf_page_object =
1057 CPDFPageObjectFromFPDFPageObject(page_object);
1058 if (i < 15)
1059 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1060 else if (i < 18)
1061 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1062 else
1063 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
1064 }
1065
1066 UnloadPage(page);
1067}
1068
Lei Zhangab41f252018-12-23 03:10:50 +00001069TEST_F(FPDFEditEmbedderTest, RemoveAllFromStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001070 // Load document with some text split across streams.
1071 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
1072 FPDF_PAGE page = LoadPage(0);
1073 ASSERT_TRUE(page);
1074
1075 // Content stream 0: page objects 0-14.
1076 // Content stream 1: page objects 15-17.
1077 // Content stream 2: page object 18.
1078 ASSERT_EQ(19, FPDFPage_CountObjects(page));
1079
1080 // Loop backwards because objects will being removed, which shifts the indexes
1081 // after the removed position.
1082 for (int i = 18; i >= 0; i--) {
1083 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1084 ASSERT_TRUE(page_object);
1085 CPDF_PageObject* cpdf_page_object =
1086 CPDFPageObjectFromFPDFPageObject(page_object);
1087
1088 // Empty content stream 1.
1089 if (cpdf_page_object->GetContentStream() == 1) {
1090 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
1091 FPDFPageObj_Destroy(page_object);
1092 }
1093 }
1094
1095 // Content stream 0: page objects 0-14.
1096 // Content stream 2: page object 15.
1097 ASSERT_EQ(16, FPDFPage_CountObjects(page));
1098 for (int i = 0; i < 16; i++) {
1099 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1100 ASSERT_TRUE(page_object);
1101 CPDF_PageObject* cpdf_page_object =
1102 CPDFPageObjectFromFPDFPageObject(page_object);
1103 if (i < 15)
1104 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1105 else
1106 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
1107 }
1108
1109 // Generate contents should remove the empty stream and update the page
1110 // objects' contents stream indexes.
1111 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1112
1113 // Content stream 0: page objects 0-14.
1114 // Content stream 1: page object 15.
1115 ASSERT_EQ(16, FPDFPage_CountObjects(page));
1116 for (int i = 0; i < 16; i++) {
1117 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1118 ASSERT_TRUE(page_object);
1119 CPDF_PageObject* cpdf_page_object =
1120 CPDFPageObjectFromFPDFPageObject(page_object);
1121 if (i < 15)
1122 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1123 else
1124 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1125 }
1126
1127#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1128 const char kStream1RemovedMD5[] = "d2e21fbd5a6de563f619feeeb6163331";
1129#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1130 const char kStream1RemovedMD5[] = "b4140f203523e38793283a5943d8075b";
1131#else
1132 const char kStream1RemovedMD5[] = "e86a3efc160ede6cfcb1f59bcacf1105";
1133#endif
1134 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001135 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001136 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
1137 }
1138
1139 // Save the file
1140 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1141 UnloadPage(page);
1142
1143 // Re-open the file and check the page object count is still 16, and that
1144 // content stream 1 was removed.
Lei Zhang0b494052019-01-31 21:41:15 +00001145 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001146 FPDF_PAGE saved_page = LoadSavedPage(0);
1147
1148 // Content stream 0: page objects 0-14.
1149 // Content stream 1: page object 15.
1150 EXPECT_EQ(16, FPDFPage_CountObjects(saved_page));
1151 for (int i = 0; i < 16; i++) {
1152 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
1153 ASSERT_TRUE(page_object);
1154 CPDF_PageObject* cpdf_page_object =
1155 CPDFPageObjectFromFPDFPageObject(page_object);
1156 if (i < 15)
1157 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1158 else
1159 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1160 }
1161
1162 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001163 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001164 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
1165 }
1166
1167 CloseSavedPage(saved_page);
1168 CloseSavedDocument();
1169}
1170
Lei Zhangab41f252018-12-23 03:10:50 +00001171TEST_F(FPDFEditEmbedderTest, RemoveAllFromSingleStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001172 // Load document with a single stream.
1173 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1174 FPDF_PAGE page = LoadPage(0);
1175 ASSERT_TRUE(page);
1176
1177 // Content stream 0: page objects 0-1.
1178 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1179
1180 // Loop backwards because objects will being removed, which shifts the indexes
1181 // after the removed position.
1182 for (int i = 1; i >= 0; i--) {
1183 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1184 ASSERT_TRUE(page_object);
1185 CPDF_PageObject* cpdf_page_object =
1186 CPDFPageObjectFromFPDFPageObject(page_object);
1187 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1188 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1189 FPDFPageObj_Destroy(page_object);
1190 }
1191
1192 // No more objects in the stream
1193 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1194
1195 // Generate contents should remove the empty stream and update the page
1196 // objects' contents stream indexes.
1197 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1198
1199 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1200
1201 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1202 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001203 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001204 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1205 }
1206
1207 // Save the file
1208 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1209 UnloadPage(page);
1210
1211 // Re-open the file and check the page object count is still 0.
Lei Zhang0b494052019-01-31 21:41:15 +00001212 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001213 FPDF_PAGE saved_page = LoadSavedPage(0);
1214
1215 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1216 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001217 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001218 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1219 }
1220
1221 CloseSavedPage(saved_page);
1222 CloseSavedDocument();
1223}
1224
Lei Zhangab41f252018-12-23 03:10:50 +00001225TEST_F(FPDFEditEmbedderTest, RemoveFirstFromSingleStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001226 // Load document with a single stream.
1227 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1228 FPDF_PAGE page = LoadPage(0);
1229 ASSERT_TRUE(page);
1230
1231 // Content stream 0: page objects 0-1.
1232 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1233
1234 // Remove first object.
1235 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
1236 ASSERT_TRUE(page_object);
1237 CPDF_PageObject* cpdf_page_object =
1238 CPDFPageObjectFromFPDFPageObject(page_object);
1239 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1240 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1241 FPDFPageObj_Destroy(page_object);
1242
1243 // One object left in the stream.
1244 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1245 page_object = FPDFPage_GetObject(page, 0);
1246 ASSERT_TRUE(page_object);
1247 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1248 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1249
1250 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1251
1252 // Still one object left in the stream.
1253 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1254 page_object = FPDFPage_GetObject(page, 0);
1255 ASSERT_TRUE(page_object);
1256 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1257 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1258
1259#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1260 const char kFirstRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
1261#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001262 const char kFirstRemovedMD5[] = "aae6c5334721f90ec30d3d59f4ef7deb";
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001263#else
1264 const char kFirstRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
1265#endif
1266 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001267 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001268 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1269 }
1270
1271 // Save the file
1272 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1273 UnloadPage(page);
1274
1275 // Re-open the file and check the page object count is still 0.
Lei Zhang0b494052019-01-31 21:41:15 +00001276 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001277 FPDF_PAGE saved_page = LoadSavedPage(0);
1278
1279 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1280 page_object = FPDFPage_GetObject(saved_page, 0);
1281 ASSERT_TRUE(page_object);
1282 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1283 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1284 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001285 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001286 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1287 }
1288
1289 CloseSavedPage(saved_page);
1290 CloseSavedDocument();
1291}
1292
Lei Zhangab41f252018-12-23 03:10:50 +00001293TEST_F(FPDFEditEmbedderTest, RemoveLastFromSingleStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001294 // Load document with a single stream.
1295 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1296 FPDF_PAGE page = LoadPage(0);
1297 ASSERT_TRUE(page);
1298
1299 // Content stream 0: page objects 0-1.
1300 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1301
1302 // Remove last object
1303 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 1);
1304 ASSERT_TRUE(page_object);
1305 CPDF_PageObject* cpdf_page_object =
1306 CPDFPageObjectFromFPDFPageObject(page_object);
1307 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1308 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1309 FPDFPageObj_Destroy(page_object);
1310
1311 // One object left in the stream.
1312 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1313 page_object = FPDFPage_GetObject(page, 0);
1314 ASSERT_TRUE(page_object);
1315 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1316 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1317
1318 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1319
1320 // Still one object left in the stream.
1321 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1322 page_object = FPDFPage_GetObject(page, 0);
1323 ASSERT_TRUE(page_object);
1324 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1325 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1326
1327#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1328 const char kLastRemovedMD5[] = "f8fbd14a048b9e2ea8e5f059f22a910e";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001329#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1330 const char kLastRemovedMD5[] = "93db13099042bafefb3c22a165bad684";
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001331#else
1332 const char kLastRemovedMD5[] = "93dcc09055f87a2792c8e3065af99a1b";
1333#endif
1334 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001335 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001336 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1337 }
1338
1339 // Save the file
1340 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1341 UnloadPage(page);
1342
1343 // Re-open the file and check the page object count is still 0.
Lei Zhang0b494052019-01-31 21:41:15 +00001344 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001345 FPDF_PAGE saved_page = LoadSavedPage(0);
1346
1347 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1348 page_object = FPDFPage_GetObject(saved_page, 0);
1349 ASSERT_TRUE(page_object);
1350 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1351 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1352 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001353 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001354 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1355 }
1356
1357 CloseSavedPage(saved_page);
1358 CloseSavedDocument();
1359}
1360
Lei Zhangab41f252018-12-23 03:10:50 +00001361TEST_F(FPDFEditEmbedderTest, RemoveAllFromMultipleStreams) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001362 // Load document with some text.
1363 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
1364 FPDF_PAGE page = LoadPage(0);
1365 ASSERT_TRUE(page);
1366
1367 // Content stream 0: page objects 0-1.
1368 // Content stream 1: page object 2.
1369 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1370
1371 // Loop backwards because objects will being removed, which shifts the indexes
1372 // after the removed position.
1373 for (int i = 2; i >= 0; i--) {
1374 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1375 ASSERT_TRUE(page_object);
1376 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1377 FPDFPageObj_Destroy(page_object);
1378 }
1379
1380 // No more objects in the page.
1381 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1382
1383 // Generate contents should remove the empty streams and update the page
1384 // objects' contents stream indexes.
1385 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1386
1387 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1388
1389 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1390 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001391 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001392 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1393 }
1394
1395 // Save the file
1396 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1397 UnloadPage(page);
1398
1399 // Re-open the file and check the page object count is still 0.
Lei Zhang0b494052019-01-31 21:41:15 +00001400 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001401 FPDF_PAGE saved_page = LoadSavedPage(0);
1402
1403 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1404 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001405 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001406 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1407 }
1408
1409 CloseSavedPage(saved_page);
1410 CloseSavedDocument();
1411}
1412
Lei Zhangab41f252018-12-23 03:10:50 +00001413TEST_F(FPDFEditEmbedderTest, InsertPageObjectAndSave) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001414 // Load document with some text.
1415 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1416 FPDF_PAGE page = LoadPage(0);
1417 ASSERT_TRUE(page);
1418
1419 // Add a red rectangle.
1420 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1421 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1422 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1423 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1424 FPDFPage_InsertObject(page, red_rect);
1425
1426 // Verify the red rectangle was added.
1427 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1428
1429 // Save the file
1430 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1431 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1432 UnloadPage(page);
1433
1434 // Re-open the file and check the page object count is still 3.
Lei Zhang0b494052019-01-31 21:41:15 +00001435 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001436 FPDF_PAGE saved_page = LoadSavedPage(0);
1437 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1438 CloseSavedPage(saved_page);
1439 CloseSavedDocument();
1440}
1441
Lei Zhangab41f252018-12-23 03:10:50 +00001442TEST_F(FPDFEditEmbedderTest, InsertPageObjectEditAndSave) {
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001443 // Load document with some text.
1444 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1445 FPDF_PAGE page = LoadPage(0);
1446 ASSERT_TRUE(page);
1447
1448 // Add a red rectangle.
1449 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1450 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1451 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 100, 100, 255));
1452 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1453 FPDFPage_InsertObject(page, red_rect);
1454
1455 // Verify the red rectangle was added.
1456 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1457
1458 // Generate content but change it again
1459 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1460 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1461
1462 // Save the file
1463 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1464 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1465 UnloadPage(page);
1466
1467 // Re-open the file and check the page object count is still 3.
Lei Zhang0b494052019-01-31 21:41:15 +00001468 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001469 FPDF_PAGE saved_page = LoadSavedPage(0);
1470 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1471 CloseSavedPage(saved_page);
1472 CloseSavedDocument();
1473}
1474
Lei Zhangab41f252018-12-23 03:10:50 +00001475TEST_F(FPDFEditEmbedderTest, InsertAndRemoveLargeFile) {
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001476 const int kOriginalObjectCount = 600;
1477
1478 // Load document with many objects.
1479 EXPECT_TRUE(OpenDocument("many_rectangles.pdf"));
1480 FPDF_PAGE page = LoadPage(0);
1481 ASSERT_TRUE(page);
1482 const char kOriginalMD5[] = "b0170c575b65ecb93ebafada0ff0f038";
1483 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001484 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001485 CompareBitmap(page_bitmap.get(), 200, 300, kOriginalMD5);
1486 }
1487
1488 // Add a black rectangle.
1489 ASSERT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(page));
1490 FPDF_PAGEOBJECT black_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1491 EXPECT_TRUE(FPDFPath_SetFillColor(black_rect, 0, 0, 0, 255));
1492 EXPECT_TRUE(FPDFPath_SetDrawMode(black_rect, FPDF_FILLMODE_ALTERNATE, 0));
1493 FPDFPage_InsertObject(page, black_rect);
1494
1495 // Verify the black rectangle was added.
1496 ASSERT_EQ(kOriginalObjectCount + 1, FPDFPage_CountObjects(page));
1497 const char kPlusRectangleMD5[] = "6b9396ab570754b32b04ca629e902f77";
1498 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001499 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001500 CompareBitmap(page_bitmap.get(), 200, 300, kPlusRectangleMD5);
1501 }
1502
1503 // Save the file.
1504 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1505 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1506 UnloadPage(page);
1507
1508 // Re-open the file and check the rectangle added is still there.
Lei Zhang0b494052019-01-31 21:41:15 +00001509 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001510 FPDF_PAGE saved_page = LoadSavedPage(0);
1511 EXPECT_EQ(kOriginalObjectCount + 1, FPDFPage_CountObjects(saved_page));
1512 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001513 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001514 CompareBitmap(page_bitmap.get(), 200, 300, kPlusRectangleMD5);
1515 }
1516
1517 // Remove the added rectangle.
1518 FPDF_PAGEOBJECT added_object =
1519 FPDFPage_GetObject(saved_page, kOriginalObjectCount);
1520 EXPECT_TRUE(FPDFPage_RemoveObject(saved_page, added_object));
1521 FPDFPageObj_Destroy(added_object);
1522 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001523 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001524 CompareBitmap(page_bitmap.get(), 200, 300, kOriginalMD5);
1525 }
1526 EXPECT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(saved_page));
1527
1528 // Save the file again.
1529 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
1530 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
1531
1532 CloseSavedPage(saved_page);
1533 CloseSavedDocument();
1534
1535 // Re-open the file (again) and check the black rectangle was removed and the
1536 // rest is intact.
Lei Zhang0b494052019-01-31 21:41:15 +00001537 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001538 saved_page = LoadSavedPage(0);
1539 EXPECT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(saved_page));
1540 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001541 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001542 CompareBitmap(page_bitmap.get(), 200, 300, kOriginalMD5);
1543 }
1544
1545 CloseSavedPage(saved_page);
1546 CloseSavedDocument();
1547}
1548
Lei Zhangab41f252018-12-23 03:10:50 +00001549TEST_F(FPDFEditEmbedderTest, AddAndRemovePaths) {
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001550 // Start with a blank page.
1551 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1552 ASSERT_TRUE(page);
1553
1554 // Render the blank page and verify it's a blank bitmap.
1555 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
1556 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001557 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001558 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1559 }
1560 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1561
1562 // Add a red rectangle.
1563 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1564 ASSERT_TRUE(red_rect);
1565 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1566 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1567 FPDFPage_InsertObject(page, red_rect);
1568 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
1569 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001570 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001571 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
1572 }
1573 EXPECT_EQ(1, FPDFPage_CountObjects(page));
1574
1575 // Remove rectangle and verify it does not render anymore and the bitmap is
1576 // back to a blank one.
1577 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
1578 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001579 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001580 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1581 }
1582 EXPECT_EQ(0, FPDFPage_CountObjects(page));
1583
1584 // Trying to remove an object not in the page should return false.
1585 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
1586
1587 FPDF_ClosePage(page);
1588 FPDFPageObj_Destroy(red_rect);
1589}
1590
Lei Zhangab41f252018-12-23 03:10:50 +00001591TEST_F(FPDFEditEmbedderTest, PathsPoints) {
Miklos Vajna12abfd02017-09-15 07:49:03 +02001592 CreateNewDocument();
1593 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
1594 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +02001595 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001596
1597 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +02001598 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001599
Miklos Vajna36eed872017-09-20 22:52:43 +02001600 // FPDFPath_GetPathSegment() with a non-path.
1601 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
1602 // FPDFPath_GetPathSegment() with a NULL path.
1603 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
1604 float x;
1605 float y;
1606 // FPDFPathSegment_GetPoint() with a NULL segment.
1607 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
1608
1609 // FPDFPathSegment_GetType() with a NULL segment.
1610 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
1611
1612 // FPDFPathSegment_GetClose() with a NULL segment.
1613 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
1614
Miklos Vajna12abfd02017-09-15 07:49:03 +02001615 FPDFPageObj_Destroy(img);
1616}
1617
Lei Zhangab41f252018-12-23 03:10:50 +00001618TEST_F(FPDFEditEmbedderTest, PathOnTopOfText) {
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001619 // Load document with some text
1620 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1621 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001622 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001623
1624 // Add an opaque rectangle on top of some of the text.
1625 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1626 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1627 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1628 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001629
1630 // Add a transparent triangle on top of other part of the text.
1631 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
1632 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
1633 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
1634 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
1635 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
1636 EXPECT_TRUE(FPDFPath_Close(black_path));
1637 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001638
1639 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001640 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001641#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -07001642 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001643#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1644 const char md5[] = "74dd9c393b8b2578d2b7feb032b7daad";
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001645#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001646 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001647#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001648 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001649 UnloadPage(page);
1650}
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001651
Lei Zhangab41f252018-12-23 03:10:50 +00001652TEST_F(FPDFEditEmbedderTest, EditOverExistingContent) {
wileyryae858aa42017-05-31 14:49:05 -05001653 // Load document with existing content
1654 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
1655 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001656 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -05001657
1658 // Add a transparent rectangle on top of the existing content
1659 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
1660 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
1661 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
1662 FPDFPage_InsertObject(page, red_rect2);
1663
1664 // Add an opaque rectangle on top of the existing content
1665 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
1666 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1667 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1668 FPDFPage_InsertObject(page, red_rect);
1669
Tom Sepeze08d2b12018-04-25 18:49:32 +00001670 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001671 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001672 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1673
1674 // Now save the result, closing the page and document
1675 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -04001676 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -05001677
Lei Zhang0b494052019-01-31 21:41:15 +00001678 ASSERT_TRUE(OpenSavedDocument());
Lei Zhang107fa7b2018-02-09 21:48:15 +00001679 FPDF_PAGE saved_page = LoadSavedPage(0);
1680 VerifySavedRendering(saved_page, 612, 792,
1681 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001682
1683 ClearString();
1684 // Add another opaque rectangle on top of the existing content
1685 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
1686 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
1687 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001688 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -05001689
1690 // Add another transparent rectangle on top of existing content
1691 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
1692 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
1693 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001694 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001695 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
1696 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001697 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001698 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
1699 }
Lei Zhang107fa7b2018-02-09 21:48:15 +00001700 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -05001701
1702 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +00001703 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001704
Lei Zhang107fa7b2018-02-09 21:48:15 +00001705 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001706 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -05001707
1708 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +00001709 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -05001710}
1711
Lei Zhangab41f252018-12-23 03:10:50 +00001712TEST_F(FPDFEditEmbedderTest, AddStrokedPaths) {
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001713 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001714 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001715
1716 // Add a large stroked rectangle (fill color should not affect it).
1717 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
1718 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
1719 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
1720 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
Miklos Vajna366df7f2018-05-22 14:27:29 +00001721
1722 float width = 0;
1723 EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width));
1724 EXPECT_EQ(15.0f, width);
1725
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001726 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
1727 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001728 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001729 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001730 CompareBitmap(page_bitmap.get(), 612, 792,
1731 "64bd31f862a89e0a9e505a5af6efd506");
1732 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001733
1734 // Add crossed-checkmark
1735 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
1736 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
1737 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
1738 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
1739 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
1740 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
1741 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
1742 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1743 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001744 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001745 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001746 CompareBitmap(page_bitmap.get(), 612, 792,
1747 "4b6f3b9d25c4e194821217d5016c3724");
1748 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001749
1750 // Add stroked and filled oval-ish path.
1751 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
1752 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
1753 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
1754 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
1755 EXPECT_TRUE(FPDFPath_Close(path));
1756 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
1757 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
1758 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
1759 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
1760 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001761 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001762 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001763 CompareBitmap(page_bitmap.get(), 612, 792,
1764 "ff3e6a22326754944cc6e56609acd73b");
1765 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001766 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001767}
Nicolas Pena49058402017-02-14 18:26:20 -05001768
Nicolas Pena4c48b102018-06-13 18:23:46 +00001769// Tests adding text from standard font using FPDFPageObj_NewTextObj.
Lei Zhangab41f252018-12-23 03:10:50 +00001770TEST_F(FPDFEditEmbedderTest, AddStandardFontText) {
Nicolas Pena49058402017-02-14 18:26:20 -05001771 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001772 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -05001773
1774 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001775 FPDF_PAGEOBJECT text_object1 =
1776 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1777 EXPECT_TRUE(text_object1);
1778 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
1779 GetFPDFWideString(L"I'm at the bottom of the page");
1780 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
1781 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
1782 FPDFPage_InsertObject(page, text_object1);
Lei Zhange039bab2019-03-18 19:57:56 +00001783 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001784 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001785 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001786#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001787 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001788#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1789 const char md5[] = "08d1ff3e5a42801bee6077fd366bef00";
Nicolas Pena49058402017-02-14 18:26:20 -05001790#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001791 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001792#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001793 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Lei Zhange039bab2019-03-18 19:57:56 +00001794
1795 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1796 VerifySavedDocument(612, 792, md5);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001797 }
Nicolas Pena49058402017-02-14 18:26:20 -05001798
1799 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -04001800 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001801 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001802 EXPECT_TRUE(text_object2);
1803 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1804 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
1805 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1806 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
1807 FPDFPage_InsertObject(page, text_object2);
Lei Zhange039bab2019-03-18 19:57:56 +00001808 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001809 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001810 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001811#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhange039bab2019-03-18 19:57:56 +00001812 const char md5[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -04001813#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhange039bab2019-03-18 19:57:56 +00001814 const char md5[] = "3755dd35abd4c605755369401ee85b2d";
Nicolas Pena49058402017-02-14 18:26:20 -05001815#else
Lei Zhange039bab2019-03-18 19:57:56 +00001816 const char md5[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001817#endif
Lei Zhange039bab2019-03-18 19:57:56 +00001818 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1819
1820 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1821 VerifySavedDocument(612, 792, md5);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001822 }
Nicolas Pena49058402017-02-14 18:26:20 -05001823
1824 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -04001825 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001826 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001827 EXPECT_TRUE(text_object3);
1828 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
1829 GetFPDFWideString(L"Can you read me? <:)>");
1830 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
1831 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
1832 FPDFPage_InsertObject(page, text_object3);
Lei Zhange039bab2019-03-18 19:57:56 +00001833 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001834 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001835 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001836#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhange039bab2019-03-18 19:57:56 +00001837 const char md5[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -04001838#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhange039bab2019-03-18 19:57:56 +00001839 const char md5[] = "aba523a8110d01ed9bd7b7781ff74045";
Nicolas Pena49058402017-02-14 18:26:20 -05001840#else
Lei Zhange039bab2019-03-18 19:57:56 +00001841 const char md5[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001842#endif
Lei Zhange039bab2019-03-18 19:57:56 +00001843 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1844
1845 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1846 VerifySavedDocument(612, 792, md5);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001847 }
Nicolas Pena49058402017-02-14 18:26:20 -05001848
Miklos Vajnac765d2a2018-06-19 15:45:42 +00001849 double matrix_a = 0;
1850 double matrix_b = 0;
1851 double matrix_c = 0;
1852 double matrix_d = 0;
1853 double matrix_e = 0;
1854 double matrix_f = 0;
1855 EXPECT_FALSE(FPDFText_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1856 &matrix_d, &matrix_e, &matrix_f));
1857 EXPECT_TRUE(FPDFText_GetMatrix(text_object3, &matrix_a, &matrix_b, &matrix_c,
1858 &matrix_d, &matrix_e, &matrix_f));
1859 EXPECT_EQ(1., matrix_a);
1860 EXPECT_EQ(1.5, matrix_b);
1861 EXPECT_EQ(2., matrix_c);
1862 EXPECT_EQ(0.5, matrix_d);
1863 EXPECT_EQ(200., matrix_e);
1864 EXPECT_EQ(200., matrix_f);
1865
Miklos Vajna8625d3b2018-06-26 15:12:48 +00001866 EXPECT_EQ(0, FPDFTextObj_GetFontSize(nullptr));
1867 EXPECT_EQ(20, FPDFTextObj_GetFontSize(text_object3));
1868
Nicolas Pena49058402017-02-14 18:26:20 -05001869 // TODO(npm): Why are there issues with text rotated by 90 degrees?
1870 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
1871 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -05001872}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001873
Lei Zhangab41f252018-12-23 03:10:50 +00001874TEST_F(FPDFEditEmbedderTest, TestGetTextRenderMode) {
Miklos Vajna1448cc12018-07-03 13:52:33 +00001875 EXPECT_TRUE(OpenDocument("text_render_mode.pdf"));
1876 FPDF_PAGE page = LoadPage(0);
1877 ASSERT_TRUE(page);
1878 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1879
1880 ASSERT_EQ(-1, FPDFText_GetTextRenderMode(nullptr));
1881
1882 FPDF_PAGEOBJECT fill = FPDFPage_GetObject(page, 0);
1883 ASSERT_EQ(FPDF_TEXTRENDERMODE_FILL, FPDFText_GetTextRenderMode(fill));
1884
1885 FPDF_PAGEOBJECT stroke = FPDFPage_GetObject(page, 1);
1886 ASSERT_EQ(FPDF_TEXTRENDERMODE_STROKE, FPDFText_GetTextRenderMode(stroke));
1887
1888 UnloadPage(page);
1889}
1890
Lei Zhangab41f252018-12-23 03:10:50 +00001891TEST_F(FPDFEditEmbedderTest, TestGetTextFontName) {
Miklos Vajna53d4f0a2018-08-01 01:28:49 +00001892 EXPECT_TRUE(OpenDocument("text_font.pdf"));
1893 FPDF_PAGE page = LoadPage(0);
1894 ASSERT_TRUE(page);
1895 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1896
1897 // FPDFTextObj_GetFontName() positive testing.
1898 FPDF_PAGEOBJECT text = FPDFPage_GetObject(page, 0);
1899 unsigned long size = FPDFTextObj_GetFontName(text, nullptr, 0);
1900 const char kExpectedFontName[] = "Liberation Serif";
1901 ASSERT_EQ(sizeof(kExpectedFontName), size);
1902 std::vector<char> font_name(size);
1903 ASSERT_EQ(size, FPDFTextObj_GetFontName(text, font_name.data(), size));
1904 ASSERT_STREQ(kExpectedFontName, font_name.data());
1905
1906 // FPDFTextObj_GetFontName() negative testing.
1907 ASSERT_EQ(0U, FPDFTextObj_GetFontName(nullptr, nullptr, 0));
1908
1909 font_name.resize(2);
1910 font_name[0] = 'x';
1911 font_name[1] = '\0';
1912 size = FPDFTextObj_GetFontName(text, font_name.data(), font_name.size());
1913 ASSERT_EQ(sizeof(kExpectedFontName), size);
1914 ASSERT_EQ(std::string("x"), std::string(font_name.data()));
1915
1916 UnloadPage(page);
1917}
1918
Lei Zhangab41f252018-12-23 03:10:50 +00001919TEST_F(FPDFEditEmbedderTest, TestFormGetObjects) {
Miklos Vajnab66077d2018-07-11 13:25:02 +00001920 EXPECT_TRUE(OpenDocument("form_object.pdf"));
1921 FPDF_PAGE page = LoadPage(0);
1922 ASSERT_TRUE(page);
1923 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1924
1925 FPDF_PAGEOBJECT form = FPDFPage_GetObject(page, 0);
1926 EXPECT_EQ(FPDF_PAGEOBJ_FORM, FPDFPageObj_GetType(form));
1927 ASSERT_EQ(-1, FPDFFormObj_CountObjects(nullptr));
1928 ASSERT_EQ(2, FPDFFormObj_CountObjects(form));
1929
Miklos Vajna1d273f12018-07-16 19:20:36 +00001930 // FPDFFormObj_GetObject() positive testing.
1931 FPDF_PAGEOBJECT text1 = FPDFFormObj_GetObject(form, 0);
1932 ASSERT_TRUE(text1);
1933 float left = 0;
1934 float bottom = 0;
1935 float right = 0;
1936 float top = 0;
1937 ASSERT_TRUE(FPDFPageObj_GetBounds(text1, &left, &bottom, &right, &top));
1938 ASSERT_EQ(271, static_cast<int>(top));
1939
1940 FPDF_PAGEOBJECT text2 = FPDFFormObj_GetObject(form, 1);
1941 ASSERT_TRUE(text2);
1942 ASSERT_TRUE(FPDFPageObj_GetBounds(text2, &left, &bottom, &right, &top));
1943 ASSERT_EQ(221, static_cast<int>(top));
1944
1945 // FPDFFormObj_GetObject() negative testing.
1946 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(nullptr, 0));
1947 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(form, -1));
1948 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(form, 2));
1949
Miklos Vajna46b43732018-08-14 19:15:43 +00001950 // Reset the form object matrix to identity.
1951 auto* pPageObj = CPDFPageObjectFromFPDFPageObject(form);
1952 CPDF_FormObject* pFormObj = pPageObj->AsForm();
1953 pFormObj->Transform(pFormObj->form_matrix().GetInverse());
1954
1955 // FPDFFormObj_GetMatrix() positive testing.
1956 static constexpr float kFloats[6] = {1.0, 1.5, 2.0, 2.5, 100.0, 200.0};
1957 CFX_Matrix matrix(kFloats);
1958 pFormObj->Transform(matrix);
1959
1960 double matrix_a = 0;
1961 double matrix_b = 0;
1962 double matrix_c = 0;
1963 double matrix_d = 0;
1964 double matrix_e = 0;
1965 double matrix_f = 0;
1966 EXPECT_TRUE(FPDFFormObj_GetMatrix(form, &matrix_a, &matrix_b, &matrix_c,
1967 &matrix_d, &matrix_e, &matrix_f));
1968 EXPECT_DOUBLE_EQ(kFloats[0], matrix_a);
1969 EXPECT_DOUBLE_EQ(kFloats[1], matrix_b);
1970 EXPECT_DOUBLE_EQ(kFloats[2], matrix_c);
1971 EXPECT_DOUBLE_EQ(kFloats[3], matrix_d);
1972 EXPECT_DOUBLE_EQ(kFloats[4], matrix_e);
1973 EXPECT_DOUBLE_EQ(kFloats[5], matrix_f);
1974
1975 // FPDFFormObj_GetMatrix() negative testing.
1976 EXPECT_FALSE(FPDFFormObj_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1977 &matrix_d, &matrix_e, &matrix_f));
1978 EXPECT_FALSE(FPDFFormObj_GetMatrix(form, nullptr, nullptr, nullptr, nullptr,
1979 nullptr, nullptr));
1980 EXPECT_FALSE(FPDFFormObj_GetMatrix(nullptr, nullptr, nullptr, nullptr,
1981 nullptr, nullptr, nullptr));
1982
Miklos Vajnab66077d2018-07-11 13:25:02 +00001983 UnloadPage(page);
1984}
1985
Nicolas Pena4c48b102018-06-13 18:23:46 +00001986// Tests adding text from standard font using FPDFText_LoadStandardFont.
Lei Zhangab41f252018-12-23 03:10:50 +00001987TEST_F(FPDFEditEmbedderTest, AddStandardFontText2) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00001988 // Start with a blank page
1989 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
1990
1991 // Load a standard font.
1992 FPDF_FONT font = FPDFText_LoadStandardFont(document(), "Helvetica");
1993 ASSERT_TRUE(font);
1994
1995 // Add some text to the page.
1996 FPDF_PAGEOBJECT text_object =
1997 FPDFPageObj_CreateTextObj(document(), font, 12.0f);
1998 EXPECT_TRUE(text_object);
1999 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2000 GetFPDFWideString(L"I'm at the bottom of the page");
2001 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2002 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20);
2003 FPDFPage_InsertObject(page.get(), text_object);
Lei Zhang30ff2532019-01-31 21:37:55 +00002004 ScopedFPDFBitmap page_bitmap = RenderPage(page.get());
Nicolas Pena4c48b102018-06-13 18:23:46 +00002005#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
2006 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002007#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2008 const char md5[] = "08d1ff3e5a42801bee6077fd366bef00";
Nicolas Pena4c48b102018-06-13 18:23:46 +00002009#else
2010 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
2011#endif
2012 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2013}
2014
Lei Zhangab41f252018-12-23 03:10:50 +00002015TEST_F(FPDFEditEmbedderTest, LoadStandardFonts) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002016 CreateNewDocument();
Lei Zhangd72fd582018-07-27 19:37:27 +00002017 static constexpr const char* standard_font_names[] = {
2018 "Arial",
2019 "Arial-Bold",
2020 "Arial-BoldItalic",
2021 "Arial-Italic",
2022 "Courier",
2023 "Courier-BoldOblique",
2024 "Courier-Oblique",
2025 "Courier-Bold",
2026 "CourierNew",
2027 "CourierNew-Bold",
2028 "CourierNew-BoldItalic",
2029 "CourierNew-Italic",
2030 "Helvetica",
2031 "Helvetica-Bold",
2032 "Helvetica-BoldOblique",
2033 "Helvetica-Oblique",
2034 "Symbol",
2035 "TimesNewRoman",
2036 "TimesNewRoman-Bold",
2037 "TimesNewRoman-BoldItalic",
2038 "TimesNewRoman-Italic",
2039 "ZapfDingbats"};
2040 for (const char* font_name : standard_font_names) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002041 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
2042 EXPECT_TRUE(font) << font_name << " should be considered a standard font.";
2043 }
Lei Zhangd72fd582018-07-27 19:37:27 +00002044 static constexpr const char* not_standard_font_names[] = {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002045 "Abcdefg", "ArialB", "Arial-Style",
2046 "Font Name", "FontArial", "NotAStandardFontName",
2047 "TestFontName", "Quack", "Symbol-Italic",
2048 "Zapf"};
Lei Zhangd72fd582018-07-27 19:37:27 +00002049 for (const char* font_name : not_standard_font_names) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002050 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
2051 EXPECT_FALSE(font) << font_name
2052 << " should not be considered a standard font.";
2053 }
2054}
2055
Lei Zhangab41f252018-12-23 03:10:50 +00002056TEST_F(FPDFEditEmbedderTest, GraphicsData) {
Nicolas Pena603a31d2017-06-14 11:41:18 -04002057 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +00002058 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -04002059
2060 // Create a rect with nontrivial graphics
2061 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
2062 FPDFPageObj_SetBlendMode(rect1, "Color");
2063 FPDFPage_InsertObject(page.get(), rect1);
2064 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
2065
2066 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +00002067 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
2068 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -04002069 ASSERT_TRUE(graphics_dict);
Lei Zhangf40380f2018-10-12 18:31:51 +00002070 EXPECT_EQ(2u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002071
2072 // Add a text object causing no change to the graphics dictionary
2073 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
2074 // Only alpha, the last component, matters for the graphics dictionary. And
2075 // the default value is 255.
2076 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
2077 FPDFPage_InsertObject(page.get(), text1);
2078 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002079 EXPECT_EQ(2u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002080
2081 // Add a text object increasing the size of the graphics dictionary
2082 FPDF_PAGEOBJECT text2 =
2083 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
2084 FPDFPage_InsertObject(page.get(), text2);
2085 FPDFPageObj_SetBlendMode(text2, "Darken");
2086 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
2087 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002088 EXPECT_EQ(3u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002089
2090 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -04002091 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -04002092 FPDFPageObj_SetBlendMode(path, "Darken");
2093 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
2094 FPDFPage_InsertObject(page.get(), path);
2095 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002096 EXPECT_EQ(3u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002097
2098 // Add a rect increasing the size of the graphics dictionary
2099 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
2100 FPDFPageObj_SetBlendMode(rect2, "Darken");
2101 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
2102 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
2103 FPDFPage_InsertObject(page.get(), rect2);
2104 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002105 EXPECT_EQ(4u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002106}
2107
Lei Zhangab41f252018-12-23 03:10:50 +00002108TEST_F(FPDFEditEmbedderTest, DoubleGenerating) {
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002109 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05002110 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002111
2112 // Add a red rectangle with some non-default alpha
2113 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
2114 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
2115 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
2116 FPDFPage_InsertObject(page, rect);
2117 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2118
2119 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +00002120 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
2121 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002122 ASSERT_TRUE(graphics_dict);
Lei Zhangf40380f2018-10-12 18:31:51 +00002123 EXPECT_EQ(2u, graphics_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002124
2125 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +00002126 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002127 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002128 CompareBitmap(page_bitmap.get(), 612, 792,
2129 "5384da3406d62360ffb5cac4476fff1c");
2130 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002131
2132 // Never mind, my new favorite color is blue, increase alpha
2133 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
2134 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhangf40380f2018-10-12 18:31:51 +00002135 EXPECT_EQ(3u, graphics_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002136
2137 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +00002138 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002139 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002140 CompareBitmap(page_bitmap.get(), 612, 792,
2141 "2e51656f5073b0bee611d9cd086aa09c");
2142 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002143
2144 // And now generate, without changes
2145 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhangf40380f2018-10-12 18:31:51 +00002146 EXPECT_EQ(3u, graphics_dict->size());
Lei Zhang107fa7b2018-02-09 21:48:15 +00002147 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002148 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002149 CompareBitmap(page_bitmap.get(), 612, 792,
2150 "2e51656f5073b0bee611d9cd086aa09c");
2151 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002152
2153 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04002154 FPDF_PAGEOBJECT text_object =
2155 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
2156 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2157 GetFPDFWideString(L"Something something #text# something");
2158 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2159 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
2160 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002161 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00002162 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002163 ASSERT_TRUE(font_dict);
Lei Zhangf40380f2018-10-12 18:31:51 +00002164 EXPECT_EQ(1u, font_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002165
2166 // Generate yet again, check dicts are reasonably sized
2167 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhangf40380f2018-10-12 18:31:51 +00002168 EXPECT_EQ(3u, graphics_dict->size());
2169 EXPECT_EQ(1u, font_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002170 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002171}
Nicolas Penabe90aae2017-02-27 10:41:41 -05002172
Lei Zhangab41f252018-12-23 03:10:50 +00002173TEST_F(FPDFEditEmbedderTest, LoadSimpleType1Font) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002174 CreateNewDocument();
2175 // TODO(npm): use other fonts after disallowing loading any font as any type
2176 const CPDF_Font* stock_font =
2177 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Tom Sepez20c41a52018-08-29 23:53:53 +00002178 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2179 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2180 FPDF_FONT_TYPE1, false));
Nicolas Penab3161852017-05-02 14:12:50 -04002181 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002182 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002183 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002184
Lei Zhang710fa992018-05-25 16:24:48 +00002185 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05002186 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2187 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002188 EXPECT_EQ("TimesNewRomanPS-BoldMT", font_dict->GetStringFor("BaseFont"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05002189 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
2190 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
2191 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05002192 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
2193
Lei Zhangde579ab2018-05-25 21:49:49 +00002194 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05002195 ASSERT_TRUE(widths_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002196 ASSERT_EQ(224u, widths_array->size());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002197 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05002198 EXPECT_EQ(569, widths_array->GetNumberAt(11));
2199 EXPECT_EQ(500, widths_array->GetNumberAt(223));
Tom Sepez20c41a52018-08-29 23:53:53 +00002200 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002201}
Nicolas Penabe90aae2017-02-27 10:41:41 -05002202
Lei Zhangab41f252018-12-23 03:10:50 +00002203TEST_F(FPDFEditEmbedderTest, LoadSimpleTrueTypeFont) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002204 CreateNewDocument();
2205 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Tom Sepez20c41a52018-08-29 23:53:53 +00002206 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2207 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2208 FPDF_FONT_TRUETYPE, false));
Nicolas Penab3161852017-05-02 14:12:50 -04002209 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002210 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002211 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002212
Lei Zhang710fa992018-05-25 16:24:48 +00002213 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002214 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2215 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002216 EXPECT_EQ("CourierNewPSMT", font_dict->GetStringFor("BaseFont"));
Nicolas Penad03ca422017-03-06 13:54:33 -05002217 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
2218 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
2219 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
2220 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05002221
Lei Zhangde579ab2018-05-25 21:49:49 +00002222 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05002223 ASSERT_TRUE(widths_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002224 ASSERT_EQ(224u, widths_array->size());
Nicolas Penad03ca422017-03-06 13:54:33 -05002225 EXPECT_EQ(600, widths_array->GetNumberAt(33));
2226 EXPECT_EQ(600, widths_array->GetNumberAt(74));
2227 EXPECT_EQ(600, widths_array->GetNumberAt(223));
Tom Sepez20c41a52018-08-29 23:53:53 +00002228 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002229}
2230
Lei Zhangab41f252018-12-23 03:10:50 +00002231TEST_F(FPDFEditEmbedderTest, LoadCIDType0Font) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002232 CreateNewDocument();
2233 const CPDF_Font* stock_font =
2234 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Tom Sepez20c41a52018-08-29 23:53:53 +00002235 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2236 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2237 FPDF_FONT_TYPE1, 1));
Nicolas Penab3161852017-05-02 14:12:50 -04002238 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002239 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002240 EXPECT_TRUE(typed_font->IsCIDFont());
2241
2242 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00002243 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002244 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2245 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
Lei Zhangb6992dd2019-02-05 23:30:20 +00002246 EXPECT_EQ("TimesNewRomanPSMT-Identity-H",
2247 font_dict->GetStringFor("BaseFont"));
Nicolas Penad03ca422017-03-06 13:54:33 -05002248 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00002249 const CPDF_Array* descendant_array =
2250 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05002251 ASSERT_TRUE(descendant_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002252 EXPECT_EQ(1u, descendant_array->size());
Nicolas Penad03ca422017-03-06 13:54:33 -05002253
2254 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00002255 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05002256 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
2257 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002258 EXPECT_EQ("TimesNewRomanPSMT", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00002259 const CPDF_Dictionary* cidinfo_dict =
2260 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05002261 ASSERT_TRUE(cidinfo_dict);
Lei Zhang9c950b12019-01-16 19:06:37 +00002262 const CPDF_Object* registry = cidinfo_dict->GetObjectFor("Registry");
2263 ASSERT_TRUE(registry);
KDr28da0e1b2019-01-17 03:44:29 +00002264 EXPECT_EQ(CPDF_Object::kString, registry->GetType());
Lei Zhang9c950b12019-01-16 19:06:37 +00002265 EXPECT_EQ("Adobe", registry->GetString());
2266 const CPDF_Object* ordering = cidinfo_dict->GetObjectFor("Ordering");
2267 ASSERT_TRUE(ordering);
KDr28da0e1b2019-01-17 03:44:29 +00002268 EXPECT_EQ(CPDF_Object::kString, ordering->GetType());
Lei Zhang9c950b12019-01-16 19:06:37 +00002269 EXPECT_EQ("Identity", ordering->GetString());
Nicolas Penad03ca422017-03-06 13:54:33 -05002270 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
Tom Sepez20c41a52018-08-29 23:53:53 +00002271 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002272
2273 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00002274 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05002275 ASSERT_TRUE(widths_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002276 EXPECT_GT(widths_array->size(), 1u);
Nicolas Penad03ca422017-03-06 13:54:33 -05002277 CheckCompositeFontWidths(widths_array, typed_font);
2278}
2279
Lei Zhangab41f252018-12-23 03:10:50 +00002280TEST_F(FPDFEditEmbedderTest, LoadCIDType2Font) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002281 CreateNewDocument();
2282 const CPDF_Font* stock_font =
2283 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Tom Sepez20c41a52018-08-29 23:53:53 +00002284 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2285 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2286 FPDF_FONT_TRUETYPE, 1));
Nicolas Penab3161852017-05-02 14:12:50 -04002287 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002288 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002289 EXPECT_TRUE(typed_font->IsCIDFont());
2290
2291 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00002292 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002293 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2294 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002295 EXPECT_EQ("Arial-ItalicMT", font_dict->GetStringFor("BaseFont"));
Nicolas Penad03ca422017-03-06 13:54:33 -05002296 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00002297 const CPDF_Array* descendant_array =
2298 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05002299 ASSERT_TRUE(descendant_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002300 EXPECT_EQ(1u, descendant_array->size());
Nicolas Penad03ca422017-03-06 13:54:33 -05002301
2302 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00002303 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05002304 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
2305 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002306 EXPECT_EQ("Arial-ItalicMT", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00002307 const CPDF_Dictionary* cidinfo_dict =
2308 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05002309 ASSERT_TRUE(cidinfo_dict);
2310 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
2311 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
2312 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
Tom Sepez20c41a52018-08-29 23:53:53 +00002313 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002314
2315 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00002316 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05002317 ASSERT_TRUE(widths_array);
2318 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05002319}
rbpotterce8e51e2017-04-28 12:42:47 -07002320
Lei Zhangab41f252018-12-23 03:10:50 +00002321TEST_F(FPDFEditEmbedderTest, NormalizeNegativeRotation) {
rbpotterce8e51e2017-04-28 12:42:47 -07002322 // Load document with a -90 degree rotation
2323 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
2324 FPDF_PAGE page = LoadPage(0);
2325 EXPECT_NE(nullptr, page);
2326
2327 EXPECT_EQ(3, FPDFPage_GetRotation(page));
2328 UnloadPage(page);
2329}
Nicolas Penab3161852017-05-02 14:12:50 -04002330
Lei Zhangab41f252018-12-23 03:10:50 +00002331TEST_F(FPDFEditEmbedderTest, AddTrueTypeFontText) {
Nicolas Penab3161852017-05-02 14:12:50 -04002332 // Start with a blank page
2333 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2334 {
2335 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Tom Sepez20c41a52018-08-29 23:53:53 +00002336 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2337 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2338 FPDF_FONT_TRUETYPE, 0));
Nicolas Penab3161852017-05-02 14:12:50 -04002339 ASSERT_TRUE(font.get());
2340
2341 // Add some text to the page
2342 FPDF_PAGEOBJECT text_object =
2343 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2344 EXPECT_TRUE(text_object);
2345 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2346 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2347 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2348 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2349 FPDFPage_InsertObject(page, text_object);
Lei Zhang30ff2532019-01-31 21:37:55 +00002350 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04002351#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002352 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002353#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2354 const char md5[] = "d60ba39f9698e32360d99e727dd93165";
Nicolas Penab3161852017-05-02 14:12:50 -04002355#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002356 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04002357#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002358 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04002359
2360 // Add some more text, same font
2361 FPDF_PAGEOBJECT text_object2 =
2362 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
2363 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2364 GetFPDFWideString(L"Bigger font size");
2365 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2366 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
2367 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04002368 }
Lei Zhang30ff2532019-01-31 21:37:55 +00002369 ScopedFPDFBitmap page_bitmap2 = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04002370#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002371 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002372#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2373 const char md5_2[] = "2199b579c49ab5f80c246a586a80ee90";
Nicolas Penab3161852017-05-02 14:12:50 -04002374#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002375 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04002376#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002377 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002378
Nicolas Pena207b7272017-05-26 17:37:06 -04002379 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04002380 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2381 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002382
2383 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002384}
Nicolas Penaf45ade32017-05-03 10:23:49 -04002385
Lei Zhangab41f252018-12-23 03:10:50 +00002386TEST_F(FPDFEditEmbedderTest, TransformAnnot) {
Jane Liueda65252017-06-07 11:31:27 -04002387 // Open a file with one annotation and load its first page.
2388 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00002389 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04002390 ASSERT_TRUE(page);
2391
Lei Zhanga21d5932018-02-05 18:28:38 +00002392 {
2393 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00002394 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00002395 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
2396 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04002397
Lei Zhanga21d5932018-02-05 18:28:38 +00002398 // FPDFPage_TransformAnnots() should run without errors when modifying
2399 // annotation rectangles.
2400 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
2401 }
Jane Liueda65252017-06-07 11:31:27 -04002402 UnloadPage(page);
2403}
2404
Nicolas Penaf45ade32017-05-03 10:23:49 -04002405// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04002406#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Lei Zhangab41f252018-12-23 03:10:50 +00002407TEST_F(FPDFEditEmbedderTest, AddCIDFontText) {
Nicolas Penaf45ade32017-05-03 10:23:49 -04002408 // Start with a blank page
2409 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2410 CFX_Font CIDfont;
2411 {
2412 // First, get the data from the font
2413 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
2414 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
Tom Sepez20c41a52018-08-29 23:53:53 +00002415 pdfium::span<const uint8_t> span = CIDfont.GetFontSpan();
Nicolas Penaf45ade32017-05-03 10:23:49 -04002416
2417 // Load the data into a FPDF_Font.
Tom Sepez20c41a52018-08-29 23:53:53 +00002418 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2419 FPDF_FONT_TRUETYPE, 1));
Nicolas Penaf45ade32017-05-03 10:23:49 -04002420 ASSERT_TRUE(font.get());
2421
2422 // Add some text to the page
2423 FPDF_PAGEOBJECT text_object =
2424 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2425 ASSERT_TRUE(text_object);
2426 std::wstring wstr = L"ABCDEFGhijklmnop.";
2427 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2428 GetFPDFWideString(wstr);
2429 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2430 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
2431 FPDFPage_InsertObject(page, text_object);
2432
2433 // And add some Japanese characters
2434 FPDF_PAGEOBJECT text_object2 =
2435 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
2436 ASSERT_TRUE(text_object2);
2437 std::wstring wstr2 =
2438 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
2439 L"\u756A";
2440 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2441 GetFPDFWideString(wstr2);
2442 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2443 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
2444 FPDFPage_InsertObject(page, text_object2);
2445 }
2446
Nicolas Pena207b7272017-05-26 17:37:06 -04002447 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00002448 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00002449 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002450 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002451 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2452 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04002453
2454 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04002455 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04002456 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2457 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002458
2459 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04002460}
Dan Sinclair698aed72017-09-26 16:24:49 -04002461#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002462
Lei Zhangab41f252018-12-23 03:10:50 +00002463TEST_F(FPDFEditEmbedderTest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04002464 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002465 {
2466 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
2467 FPDF_PAGE page = LoadPage(0);
2468 ASSERT_NE(nullptr, page);
2469
2470 // Now add a more complex blue path.
2471 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
2472 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
2473 // TODO(npm): stroking will cause the MD5s to differ.
2474 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
2475 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
2476 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
2477 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
2478 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
2479 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
2480 EXPECT_TRUE(FPDFPath_Close(green_path));
2481 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002482 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002483 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002484
2485 // Now save the result, closing the page and document
2486 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2487 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2488 UnloadPage(page);
2489 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002490
2491 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002492}
Jane Liu28fb7ba2017-08-02 21:45:57 -04002493
Lei Zhangab41f252018-12-23 03:10:50 +00002494TEST_F(FPDFEditEmbedderTest, AddMark) {
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002495 // Load document with some text.
2496 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2497 FPDF_PAGE page = LoadPage(0);
2498 ASSERT_TRUE(page);
2499
2500 constexpr int kExpectedObjectCount = 19;
2501 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2502
2503 // Add to the first page object a "Bounds" mark with "Position": "First".
2504 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
2505 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(page_object, "Bounds");
2506 EXPECT_TRUE(mark);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002507 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2508 "Position", "First"));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002509
2510 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2511
2512 // Save the file
2513 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2514 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2515 UnloadPage(page);
2516
2517 // Re-open the file and check the new mark is present.
Lei Zhang0b494052019-01-31 21:41:15 +00002518 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002519 FPDF_PAGE saved_page = LoadSavedPage(0);
2520
2521 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2522
2523 CloseSavedPage(saved_page);
2524 CloseSavedDocument();
2525}
2526
Lei Zhangab41f252018-12-23 03:10:50 +00002527TEST_F(FPDFEditEmbedderTest, SetMarkParam) {
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002528 // Load document with some text.
2529 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2530 FPDF_PAGE page = LoadPage(0);
2531 ASSERT_TRUE(page);
2532
2533 constexpr int kExpectedObjectCount = 19;
2534 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2535
2536 // Check the "Bounds" mark's "Position" param is "Last".
2537 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2538 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2539 ASSERT_TRUE(mark);
2540 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002541 unsigned long name_len = 999u;
2542 ASSERT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
2543 EXPECT_EQ((6u + 1u) * 2u, name_len);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002544 ASSERT_EQ(L"Bounds",
2545 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2546 unsigned long out_buffer_len;
2547 ASSERT_TRUE(FPDFPageObjMark_GetParamStringValue(
2548 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2549 ASSERT_EQ(L"Last",
2550 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2551
2552 // Set is to "End".
2553 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2554 "Position", "End"));
2555
2556 // Verify the object passed must correspond to the mark passed.
2557 FPDF_PAGEOBJECT another_page_object = FPDFPage_GetObject(page, 17);
2558 EXPECT_FALSE(FPDFPageObjMark_SetStringParam(document(), another_page_object,
2559 mark, "Position", "End"));
2560
2561 // Verify nothing else changed.
2562 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2563
2564 // Verify "Position" now maps to "End".
2565 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2566 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2567 EXPECT_EQ(L"End",
2568 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2569
2570 // Save the file
2571 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2572 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2573 UnloadPage(page);
2574
2575 // Re-open the file and cerify "Position" still maps to "End".
Lei Zhang0b494052019-01-31 21:41:15 +00002576 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002577 FPDF_PAGE saved_page = LoadSavedPage(0);
2578
2579 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2580 page_object = FPDFPage_GetObject(saved_page, 18);
2581 mark = FPDFPageObj_GetMark(page_object, 1);
2582 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2583 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2584 EXPECT_EQ(L"End",
2585 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2586
2587 CloseSavedPage(saved_page);
2588 CloseSavedDocument();
2589}
2590
Lei Zhangab41f252018-12-23 03:10:50 +00002591TEST_F(FPDFEditEmbedderTest, AddMarkedText) {
Henrique Nakashima144107d2018-07-10 21:04:05 +00002592 // Start with a blank page.
2593 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2594
2595 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Tom Sepez20c41a52018-08-29 23:53:53 +00002596 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2597 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2598 FPDF_FONT_TRUETYPE, 0));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002599 ASSERT_TRUE(font.get());
2600
2601 // Add some text to the page.
2602 FPDF_PAGEOBJECT text_object =
2603 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2604
2605 EXPECT_TRUE(text_object);
2606 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
2607 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2608 EXPECT_TRUE(FPDFText_SetText(text_object, text1.get()));
2609 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2610 FPDFPage_InsertObject(page, text_object);
2611
2612 // Add a mark with the tag "TestMarkName" to that text.
2613 EXPECT_EQ(0, FPDFPageObj_CountMarks(text_object));
Henrique Nakashima6fc8d872018-09-18 16:48:31 +00002614 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(text_object, "Test Mark Name");
Henrique Nakashima144107d2018-07-10 21:04:05 +00002615 EXPECT_TRUE(mark);
2616 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2617 EXPECT_EQ(mark, FPDFPageObj_GetMark(text_object, 0));
2618 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002619 unsigned long name_len = 999u;
2620 ASSERT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
2621 EXPECT_EQ((14u + 1u) * 2, name_len);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002622 std::wstring name =
2623 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashima6fc8d872018-09-18 16:48:31 +00002624 EXPECT_EQ(L"Test Mark Name", name);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002625
2626 // Add parameters:
2627 // - int "IntKey" : 42
2628 // - string "StringKey": "StringValue"
Henrique Nakashima07520f62018-07-12 19:45:29 +00002629 // - blob "BlobKey": "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0"
2630 constexpr const size_t kBlobLen = 28;
Lei Zhangd3b028b2018-11-30 22:22:00 +00002631 char block_value[kBlobLen];
2632 memcpy(block_value, "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0", kBlobLen);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002633 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002634 EXPECT_TRUE(
2635 FPDFPageObjMark_SetIntParam(document(), text_object, mark, "IntKey", 42));
2636 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), text_object, mark,
2637 "StringKey", "StringValue"));
2638 EXPECT_TRUE(FPDFPageObjMark_SetBlobParam(document(), text_object, mark,
Lei Zhangd3b028b2018-11-30 22:22:00 +00002639 "BlobKey", block_value, kBlobLen));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002640 EXPECT_EQ(3, FPDFPageObjMark_CountParams(mark));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002641
2642 // Check the two parameters can be retrieved.
2643 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002644 FPDFPageObjMark_GetParamValueType(mark, "IntKey"));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002645 int int_value;
Henrique Nakashima94230e52018-07-11 22:02:02 +00002646 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "IntKey", &int_value));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002647 EXPECT_EQ(42, int_value);
2648
2649 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002650 FPDFPageObjMark_GetParamValueType(mark, "StringKey"));
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002651 unsigned long out_buffer_len = 999u;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002652 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2653 mark, "StringKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002654 EXPECT_GT(out_buffer_len, 0u);
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002655 EXPECT_NE(999u, out_buffer_len);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002656 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2657 EXPECT_EQ(L"StringValue", name);
2658
Henrique Nakashima07520f62018-07-12 19:45:29 +00002659 EXPECT_EQ(FPDF_OBJECT_STRING,
2660 FPDFPageObjMark_GetParamValueType(mark, "BlobKey"));
2661 out_buffer_len = 0;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002662 EXPECT_TRUE(FPDFPageObjMark_GetParamBlobValue(
2663 mark, "BlobKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002664 EXPECT_EQ(kBlobLen, out_buffer_len);
Lei Zhangd3b028b2018-11-30 22:22:00 +00002665 EXPECT_EQ(0, memcmp(block_value, buffer, kBlobLen));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002666
Henrique Nakashima144107d2018-07-10 21:04:05 +00002667// Render and check the bitmap is the expected one.
2668#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
2669 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002670#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2671 const char md5[] = "d60ba39f9698e32360d99e727dd93165";
Henrique Nakashima144107d2018-07-10 21:04:05 +00002672#else
2673 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
2674#endif
2675 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002676 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002677 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2678 }
2679
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002680 // Now save the result.
2681 EXPECT_EQ(1, FPDFPage_CountObjects(page));
2682 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2683 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2684
Henrique Nakashima144107d2018-07-10 21:04:05 +00002685 FPDF_ClosePage(page);
2686
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002687 // Re-open the file and check the changes were kept in the saved .pdf.
Lei Zhang0b494052019-01-31 21:41:15 +00002688 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002689 FPDF_PAGE saved_page = LoadSavedPage(0);
2690 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
2691
2692 text_object = FPDFPage_GetObject(saved_page, 0);
2693 EXPECT_TRUE(text_object);
2694 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2695 mark = FPDFPageObj_GetMark(text_object, 0);
2696 EXPECT_TRUE(mark);
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002697
2698 name_len = 999u;
2699 ASSERT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
2700 EXPECT_EQ((14u + 1u) * 2, name_len);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002701 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashima6fc8d872018-09-18 16:48:31 +00002702 EXPECT_EQ(L"Test Mark Name", name);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002703
2704 CloseSavedPage(saved_page);
2705 CloseSavedDocument();
Henrique Nakashima144107d2018-07-10 21:04:05 +00002706}
2707
Lei Zhangab41f252018-12-23 03:10:50 +00002708TEST_F(FPDFEditEmbedderTest, MarkGetName) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002709 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2710 FPDF_PAGE page = LoadPage(0);
2711 ASSERT_TRUE(page);
2712 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2713 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2714 ASSERT_TRUE(mark);
2715
2716 char buffer[256];
2717 unsigned long out_len;
2718
2719 // Show the positive cases of FPDFPageObjMark_GetName.
2720 out_len = 999u;
2721 EXPECT_TRUE(FPDFPageObjMark_GetName(mark, nullptr, 0, &out_len));
2722 EXPECT_EQ((6u + 1u) * 2u, out_len);
2723
2724 out_len = 999u;
2725 EXPECT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &out_len));
2726 EXPECT_EQ(L"Bounds",
2727 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2728 EXPECT_EQ((6u + 1u) * 2u, out_len);
2729
2730 // Show the negative cases of FPDFPageObjMark_GetName.
2731 out_len = 999u;
2732 EXPECT_FALSE(
2733 FPDFPageObjMark_GetName(nullptr, buffer, sizeof(buffer), &out_len));
2734 EXPECT_EQ(999u, out_len);
2735
2736 EXPECT_FALSE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), nullptr));
2737
2738 UnloadPage(page);
2739}
2740
Lei Zhangab41f252018-12-23 03:10:50 +00002741TEST_F(FPDFEditEmbedderTest, MarkGetParamKey) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002742 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2743 FPDF_PAGE page = LoadPage(0);
2744 ASSERT_TRUE(page);
2745 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2746 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2747 ASSERT_TRUE(mark);
2748
2749 char buffer[256];
2750 unsigned long out_len;
2751
2752 // Show the positive cases of FPDFPageObjMark_GetParamKey.
2753 out_len = 999u;
2754 EXPECT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, nullptr, 0, &out_len));
2755 EXPECT_EQ((8u + 1u) * 2u, out_len);
2756
2757 out_len = 999u;
2758 EXPECT_TRUE(
2759 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer), &out_len));
2760 EXPECT_EQ(L"Position",
2761 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2762 EXPECT_EQ((8u + 1u) * 2u, out_len);
2763
2764 // Show the negative cases of FPDFPageObjMark_GetParamKey.
2765 out_len = 999u;
2766 EXPECT_FALSE(FPDFPageObjMark_GetParamKey(nullptr, 0, buffer, sizeof(buffer),
2767 &out_len));
2768 EXPECT_EQ(999u, out_len);
2769
2770 out_len = 999u;
2771 EXPECT_FALSE(
2772 FPDFPageObjMark_GetParamKey(mark, 1, buffer, sizeof(buffer), &out_len));
2773 EXPECT_EQ(999u, out_len);
2774
2775 EXPECT_FALSE(
2776 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer), nullptr));
2777
2778 UnloadPage(page);
2779}
2780
Lei Zhangab41f252018-12-23 03:10:50 +00002781TEST_F(FPDFEditEmbedderTest, MarkGetIntParam) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002782 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2783 FPDF_PAGE page = LoadPage(0);
2784 ASSERT_TRUE(page);
2785 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 8);
2786 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 0);
2787 ASSERT_TRUE(mark);
2788
2789 int out_value;
2790
2791 // Show the positive cases of FPDFPageObjMark_GetParamIntValue.
2792 out_value = 999;
2793 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
2794 EXPECT_EQ(3, out_value);
2795
2796 // Show the negative cases of FPDFPageObjMark_GetParamIntValue.
2797 out_value = 999;
2798 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(nullptr, "Factor", &out_value));
2799 EXPECT_EQ(999, out_value);
2800
2801 out_value = 999;
2802 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(mark, "ParamThatDoesNotExist",
2803 &out_value));
2804 EXPECT_EQ(999, out_value);
2805
2806 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(mark, "Factor", nullptr));
2807
2808 page_object = FPDFPage_GetObject(page, 18);
2809 mark = FPDFPageObj_GetMark(page_object, 1);
2810 out_value = 999;
2811 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(mark, "Position", &out_value));
2812 EXPECT_EQ(999, out_value);
2813
2814 UnloadPage(page);
2815}
2816
Lei Zhangab41f252018-12-23 03:10:50 +00002817TEST_F(FPDFEditEmbedderTest, MarkGetStringParam) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002818 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2819 FPDF_PAGE page = LoadPage(0);
2820 ASSERT_TRUE(page);
2821 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2822 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2823 ASSERT_TRUE(mark);
2824
2825 char buffer[256];
2826 unsigned long out_len;
2827
2828 // Show the positive cases of FPDFPageObjMark_GetParamStringValue.
2829 out_len = 999u;
2830 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "Position", nullptr, 0,
2831 &out_len));
2832 EXPECT_EQ((4u + 1u) * 2u, out_len);
2833
2834 out_len = 999u;
2835 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "Position", buffer,
2836 sizeof(buffer), &out_len));
2837 EXPECT_EQ(L"Last",
2838 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2839 EXPECT_EQ((4u + 1u) * 2u, out_len);
2840
2841 // Show the negative cases of FPDFPageObjMark_GetParamStringValue.
2842 out_len = 999u;
2843 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(nullptr, "Position", buffer,
2844 sizeof(buffer), &out_len));
2845 EXPECT_EQ(999u, out_len);
2846
2847 out_len = 999u;
2848 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(
2849 mark, "ParamThatDoesNotExist", buffer, sizeof(buffer), &out_len));
2850 EXPECT_EQ(999u, out_len);
2851
2852 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(mark, "Position", buffer,
2853 sizeof(buffer), nullptr));
2854
2855 page_object = FPDFPage_GetObject(page, 8);
2856 mark = FPDFPageObj_GetMark(page_object, 0);
2857 out_len = 999u;
2858 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(mark, "Factor", buffer,
2859 sizeof(buffer), &out_len));
2860 EXPECT_EQ(999u, out_len);
2861
2862 UnloadPage(page);
2863}
2864
Lei Zhangab41f252018-12-23 03:10:50 +00002865TEST_F(FPDFEditEmbedderTest, ExtractImageBitmap) {
Jane Liu28fb7ba2017-08-02 21:45:57 -04002866 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2867 FPDF_PAGE page = LoadPage(0);
2868 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002869 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04002870
2871 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2872 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2873 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
2874
2875 obj = FPDFPage_GetObject(page, 33);
2876 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2877 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
2878 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2879 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
2880 FPDFBitmap_Destroy(bitmap);
2881
2882 obj = FPDFPage_GetObject(page, 34);
2883 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2884 bitmap = FPDFImageObj_GetBitmap(obj);
2885 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2886 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
2887 FPDFBitmap_Destroy(bitmap);
2888
2889 obj = FPDFPage_GetObject(page, 35);
2890 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2891 bitmap = FPDFImageObj_GetBitmap(obj);
2892 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
2893 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
2894 FPDFBitmap_Destroy(bitmap);
2895
2896 obj = FPDFPage_GetObject(page, 36);
2897 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2898 bitmap = FPDFImageObj_GetBitmap(obj);
2899 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2900 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
2901 FPDFBitmap_Destroy(bitmap);
2902
2903 obj = FPDFPage_GetObject(page, 37);
2904 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2905 bitmap = FPDFImageObj_GetBitmap(obj);
2906 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2907 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
2908 FPDFBitmap_Destroy(bitmap);
2909
2910 obj = FPDFPage_GetObject(page, 38);
2911 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2912 bitmap = FPDFImageObj_GetBitmap(obj);
2913 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2914 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
2915 FPDFBitmap_Destroy(bitmap);
2916 UnloadPage(page);
2917}
Jane Liu548334e2017-08-03 16:33:40 -04002918
Lei Zhangab41f252018-12-23 03:10:50 +00002919TEST_F(FPDFEditEmbedderTest, ExtractJBigImageBitmap) {
Lei Zhang53341dd2018-03-01 15:42:47 +00002920 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
2921 FPDF_PAGE page = LoadPage(0);
2922 ASSERT_TRUE(page);
2923 ASSERT_EQ(1, FPDFPage_CountObjects(page));
2924
2925 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
2926 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2927 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002928 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00002929 ASSERT_TRUE(bitmap);
2930 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
2931 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00002932 }
2933
2934 UnloadPage(page);
2935}
2936
Lei Zhangab41f252018-12-23 03:10:50 +00002937TEST_F(FPDFEditEmbedderTest, GetImageData) {
Jane Liu548334e2017-08-03 16:33:40 -04002938 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2939 FPDF_PAGE page = LoadPage(0);
2940 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002941 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04002942
2943 // Retrieve an image object with flate-encoded data stream.
2944 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
2945 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2946
2947 // Check that the raw image data has the correct length and hash value.
2948 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2949 std::vector<char> buf(len);
2950 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2951 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
2952 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2953
2954 // Check that the decoded image data has the correct length and hash value.
2955 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2956 buf.clear();
2957 buf.resize(len);
2958 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2959 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
2960 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2961
Lei Zhang351e8b02018-12-20 01:10:06 +00002962 // Retrieve an image object with DCTDecode-encoded data stream.
Jane Liu548334e2017-08-03 16:33:40 -04002963 obj = FPDFPage_GetObject(page, 37);
2964 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2965
2966 // Check that the raw image data has the correct length and hash value.
2967 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2968 buf.clear();
2969 buf.resize(len);
2970 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2971 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2972 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2973
2974 // Check that the decoded image data has the correct length and hash value,
2975 // which should be the same as those of the raw data, since this image is
2976 // encoded by a single DCTDecode filter and decoding is a noop.
2977 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2978 buf.clear();
2979 buf.resize(len);
2980 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2981 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2982 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2983
2984 UnloadPage(page);
2985}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04002986
Lei Zhangab41f252018-12-23 03:10:50 +00002987TEST_F(FPDFEditEmbedderTest, GetImageMatrix) {
Lei Zhang866d6882018-10-24 17:31:01 +00002988 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2989 FPDF_PAGE page = LoadPage(0);
2990 ASSERT_TRUE(page);
2991 ASSERT_EQ(39, FPDFPage_CountObjects(page));
2992
2993 FPDF_PAGEOBJECT obj;
2994 double a;
2995 double b;
2996 double c;
2997 double d;
2998 double e;
2999 double f;
3000
3001 obj = FPDFPage_GetObject(page, 33);
3002 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3003 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3004 EXPECT_DOUBLE_EQ(53.0, a);
3005 EXPECT_DOUBLE_EQ(0.0, b);
3006 EXPECT_DOUBLE_EQ(0.0, c);
3007 EXPECT_DOUBLE_EQ(43.0, d);
3008 EXPECT_DOUBLE_EQ(72.0, e);
3009 EXPECT_DOUBLE_EQ(646.510009765625, f);
3010
3011 obj = FPDFPage_GetObject(page, 34);
3012 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3013 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3014 EXPECT_DOUBLE_EQ(70.0, a);
3015 EXPECT_DOUBLE_EQ(0.0, b);
3016 EXPECT_DOUBLE_EQ(0.0, c);
3017 EXPECT_DOUBLE_EQ(51.0, d);
3018 EXPECT_DOUBLE_EQ(216.0, e);
3019 EXPECT_DOUBLE_EQ(646.510009765625, f);
3020
3021 obj = FPDFPage_GetObject(page, 35);
3022 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3023 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3024 EXPECT_DOUBLE_EQ(69.0, a);
3025 EXPECT_DOUBLE_EQ(0.0, b);
3026 EXPECT_DOUBLE_EQ(0.0, c);
3027 EXPECT_DOUBLE_EQ(51.0, d);
3028 EXPECT_DOUBLE_EQ(360.0, e);
3029 EXPECT_DOUBLE_EQ(646.510009765625, f);
3030
3031 obj = FPDFPage_GetObject(page, 36);
3032 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3033 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3034 EXPECT_DOUBLE_EQ(59.0, a);
3035 EXPECT_DOUBLE_EQ(0.0, b);
3036 EXPECT_DOUBLE_EQ(0.0, c);
3037 EXPECT_DOUBLE_EQ(45.0, d);
3038 EXPECT_DOUBLE_EQ(72.0, e);
3039 EXPECT_DOUBLE_EQ(553.510009765625, f);
3040
3041 obj = FPDFPage_GetObject(page, 37);
3042 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3043 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3044 EXPECT_DOUBLE_EQ(55.94000244140625, a);
3045 EXPECT_DOUBLE_EQ(0.0, b);
3046 EXPECT_DOUBLE_EQ(0.0, c);
3047 EXPECT_DOUBLE_EQ(46.950000762939453, d);
3048 EXPECT_DOUBLE_EQ(216.0, e);
3049 EXPECT_DOUBLE_EQ(552.510009765625, f);
3050
3051 obj = FPDFPage_GetObject(page, 38);
3052 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3053 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3054 EXPECT_DOUBLE_EQ(70.528999328613281, a);
3055 EXPECT_DOUBLE_EQ(0.0, b);
3056 EXPECT_DOUBLE_EQ(0.0, c);
3057 EXPECT_DOUBLE_EQ(43.149997711181641, d);
3058 EXPECT_DOUBLE_EQ(360.0, e);
3059 EXPECT_DOUBLE_EQ(553.3599853515625, f);
3060
3061 UnloadPage(page);
3062}
3063
Lei Zhangab41f252018-12-23 03:10:50 +00003064TEST_F(FPDFEditEmbedderTest, DestroyPageObject) {
Jane Liu2e5f0ae2017-08-08 15:23:27 -04003065 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
3066 ASSERT_TRUE(rect);
3067
3068 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
3069 FPDFPageObj_Destroy(rect);
3070}
Jane Liube63ab92017-08-09 14:09:34 -04003071
Lei Zhangab41f252018-12-23 03:10:50 +00003072TEST_F(FPDFEditEmbedderTest, GetImageFilters) {
Jane Liube63ab92017-08-09 14:09:34 -04003073 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
3074 FPDF_PAGE page = LoadPage(0);
3075 ASSERT_TRUE(page);
3076
3077 // Verify that retrieving the filter of a non-image object would fail.
3078 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
3079 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3080 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
3081 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
3082
3083 // Verify the returned filter string for an image object with a single filter.
3084 obj = FPDFPage_GetObject(page, 33);
3085 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3086 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
3087 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
3088 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07003089 static constexpr char kFlateDecode[] = "FlateDecode";
3090 EXPECT_EQ(sizeof(kFlateDecode),
3091 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
3092 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04003093 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
3094
3095 // Verify all the filters for an image object with a list of filters.
3096 obj = FPDFPage_GetObject(page, 38);
3097 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3098 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
3099 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
3100 buf.clear();
3101 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07003102 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
3103 EXPECT_EQ(sizeof(kASCIIHexDecode),
3104 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
3105 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04003106
3107 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
3108 buf.clear();
3109 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07003110 static constexpr char kDCTDecode[] = "DCTDecode";
3111 EXPECT_EQ(sizeof(kDCTDecode),
3112 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
3113 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04003114
3115 UnloadPage(page);
3116}
Jane Liuca898292017-08-16 11:25:35 -04003117
Lei Zhangab41f252018-12-23 03:10:50 +00003118TEST_F(FPDFEditEmbedderTest, GetImageMetadata) {
Jane Liuca898292017-08-16 11:25:35 -04003119 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
3120 FPDF_PAGE page = LoadPage(0);
3121 ASSERT_TRUE(page);
3122
3123 // Check that getting the metadata of a null object would fail.
3124 FPDF_IMAGEOBJ_METADATA metadata;
3125 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
3126
3127 // Check that receiving the metadata with a null metadata object would fail.
3128 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
3129 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
3130
3131 // Check that when retrieving an image object's metadata without passing in
3132 // |page|, all values are correct, with the last two being default values.
3133 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3134 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05003135 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04003136 EXPECT_EQ(92u, metadata.width);
3137 EXPECT_EQ(68u, metadata.height);
Lei Zhang351e8b02018-12-20 01:10:06 +00003138 EXPECT_FLOAT_EQ(96.0f, metadata.horizontal_dpi);
3139 EXPECT_FLOAT_EQ(96.0f, metadata.vertical_dpi);
Jane Liuca898292017-08-16 11:25:35 -04003140 EXPECT_EQ(0u, metadata.bits_per_pixel);
3141 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
3142
3143 // Verify the metadata of a bitmap image with indexed colorspace.
3144 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05003145 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04003146 EXPECT_EQ(92u, metadata.width);
3147 EXPECT_EQ(68u, metadata.height);
Lei Zhang351e8b02018-12-20 01:10:06 +00003148 EXPECT_FLOAT_EQ(96.0f, metadata.horizontal_dpi);
3149 EXPECT_FLOAT_EQ(96.0f, metadata.vertical_dpi);
Jane Liuca898292017-08-16 11:25:35 -04003150 EXPECT_EQ(1u, metadata.bits_per_pixel);
3151 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
3152
3153 // Verify the metadata of an image with RGB colorspace.
3154 obj = FPDFPage_GetObject(page, 37);
3155 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3156 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05003157 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04003158 EXPECT_EQ(126u, metadata.width);
3159 EXPECT_EQ(106u, metadata.height);
Lei Zhang351e8b02018-12-20 01:10:06 +00003160 EXPECT_FLOAT_EQ(162.173752f, metadata.horizontal_dpi);
3161 EXPECT_FLOAT_EQ(162.555878f, metadata.vertical_dpi);
Jane Liuca898292017-08-16 11:25:35 -04003162 EXPECT_EQ(24u, metadata.bits_per_pixel);
3163 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
3164
3165 UnloadPage(page);
3166}