blob: 02e7cc9a20dfa1d77cfbd698b17359a0cf2a8842 [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"
Dan Sinclair00d47a62018-03-28 18:39:04 +000020#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000021#include "public/cpp/fpdf_scopers.h"
Jane Liueda65252017-06-07 11:31:27 -040022#include "public/fpdf_annot.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080023#include "public/fpdf_edit.h"
24#include "public/fpdfview.h"
25#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080026#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080027#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080028#include "testing/test_support.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080029
Lei Zhangab41f252018-12-23 03:10:50 +000030class FPDFEditEmbedderTest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050031 protected:
32 FPDF_DOCUMENT CreateNewDocument() {
33 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070034 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050035 return document_;
36 }
37
Lei Zhang710fa992018-05-25 16:24:48 +000038 void CheckFontDescriptor(const CPDF_Dictionary* font_dict,
Nicolas Penad03ca422017-03-06 13:54:33 -050039 int font_type,
40 bool bold,
41 bool italic,
Tom Sepez20c41a52018-08-29 23:53:53 +000042 pdfium::span<const uint8_t> span) {
Lei Zhangb1ec2802018-05-25 21:55:24 +000043 const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
Nicolas Penad03ca422017-03-06 13:54:33 -050044 ASSERT_TRUE(font_desc);
45 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
46 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
47 font_desc->GetStringFor("FontName"));
48
49 // Check that the font descriptor has the required keys according to spec
50 // 1.7 Table 5.19
51 ASSERT_TRUE(font_desc->KeyExist("Flags"));
Dan Sinclair10e1f052017-09-28 15:59:42 -040052
Nicolas Penad03ca422017-03-06 13:54:33 -050053 int font_flags = font_desc->GetIntegerFor("Flags");
Dan Sinclair10e1f052017-09-28 15:59:42 -040054 EXPECT_EQ(bold, FontStyleIsBold(font_flags));
55 EXPECT_EQ(italic, FontStyleIsItalic(font_flags));
56 EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags));
Nicolas Penad03ca422017-03-06 13:54:33 -050057 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
Nicolás Peña5f95f362017-09-28 13:00:45 +090058
Lei Zhangb1ec2802018-05-25 21:55:24 +000059 const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox");
Nicolás Peña5f95f362017-09-28 13:00:45 +090060 ASSERT_TRUE(fontBBox);
Lei Zhangf40380f2018-10-12 18:31:51 +000061 EXPECT_EQ(4u, fontBBox->size());
Nicolás Peña5f95f362017-09-28 13:00:45 +090062 // Check that the coordinates are in the preferred order according to spec
63 // 1.7 Section 3.8.4
64 EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2));
65 EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3));
66
Nicolas Penad03ca422017-03-06 13:54:33 -050067 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
68 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
69 EXPECT_TRUE(font_desc->KeyExist("Descent"));
70 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
71 EXPECT_TRUE(font_desc->KeyExist("StemV"));
Ryan Harrison275e2602017-09-18 14:23:18 -040072 ByteString present("FontFile");
73 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050074 if (font_type == FPDF_FONT_TRUETYPE)
75 std::swap(present, absent);
76 EXPECT_TRUE(font_desc->KeyExist(present));
77 EXPECT_FALSE(font_desc->KeyExist(absent));
78
Artem Strygineababa12018-06-06 12:31:18 +000079 auto streamAcc =
80 pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present));
81 streamAcc->LoadAllDataRaw();
82
Nicolas Penad03ca422017-03-06 13:54:33 -050083 // Check that the font stream is the one that was provided
Tom Sepez20c41a52018-08-29 23:53:53 +000084 ASSERT_EQ(span.size(), streamAcc->GetSize());
Nicolás Peña79eab232017-09-28 13:29:05 +090085 if (font_type == FPDF_FONT_TRUETYPE) {
Tom Sepez20c41a52018-08-29 23:53:53 +000086 ASSERT_EQ(static_cast<int>(span.size()),
Artem Strygineababa12018-06-06 12:31:18 +000087 streamAcc->GetDict()->GetIntegerFor("Length1"));
Nicolás Peña79eab232017-09-28 13:29:05 +090088 }
Artem Strygineababa12018-06-06 12:31:18 +000089
90 const uint8_t* stream_data = streamAcc->GetData();
Tom Sepez20c41a52018-08-29 23:53:53 +000091 for (size_t j = 0; j < span.size(); j++)
92 EXPECT_EQ(span[j], stream_data[j]) << " at byte " << j;
Nicolas Penad03ca422017-03-06 13:54:33 -050093 }
94
Lei Zhangde579ab2018-05-25 21:49:49 +000095 void CheckCompositeFontWidths(const CPDF_Array* widths_array,
Nicolas Penad03ca422017-03-06 13:54:33 -050096 CPDF_Font* typed_font) {
97 // Check that W array is in a format that conforms to PDF spec 1.7 section
98 // "Glyph Metrics in CIDFonts" (these checks are not
99 // implementation-specific).
Lei Zhangf40380f2018-10-12 18:31:51 +0000100 EXPECT_GT(widths_array->size(), 1u);
Nicolas Penad03ca422017-03-06 13:54:33 -0500101 int num_cids_checked = 0;
102 int cur_cid = 0;
Lei Zhangf40380f2018-10-12 18:31:51 +0000103 for (size_t idx = 0; idx < widths_array->size(); idx++) {
Nicolas Penad03ca422017-03-06 13:54:33 -0500104 int cid = widths_array->GetNumberAt(idx);
105 EXPECT_GE(cid, cur_cid);
Lei Zhangf40380f2018-10-12 18:31:51 +0000106 ASSERT_FALSE(++idx == widths_array->size());
Lei Zhangde579ab2018-05-25 21:49:49 +0000107 const CPDF_Object* next = widths_array->GetObjectAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500108 if (next->IsArray()) {
109 // We are in the c [w1 w2 ...] case
Lei Zhangde579ab2018-05-25 21:49:49 +0000110 const CPDF_Array* arr = next->AsArray();
Lei Zhangf40380f2018-10-12 18:31:51 +0000111 int cnt = static_cast<int>(arr->size());
Nicolas Penad03ca422017-03-06 13:54:33 -0500112 size_t inner_idx = 0;
113 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
Nicolas Pena23346602018-01-30 21:42:41 +0000114 uint32_t width = arr->GetNumberAt(inner_idx++);
Dan Sinclair971a6742018-03-28 19:23:25 +0000115 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
116 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500117 }
118 num_cids_checked += cnt;
119 continue;
120 }
121 // Otherwise, are in the c_first c_last w case.
122 ASSERT_TRUE(next->IsNumber());
123 int last_cid = next->AsNumber()->GetInteger();
Lei Zhangf40380f2018-10-12 18:31:51 +0000124 ASSERT_FALSE(++idx == widths_array->size());
Nicolas Pena23346602018-01-30 21:42:41 +0000125 uint32_t width = widths_array->GetNumberAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500126 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
Dan Sinclair971a6742018-03-28 19:23:25 +0000127 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
128 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500129 }
130 num_cids_checked += last_cid - cid + 1;
131 }
132 // Make sure we have a good amount of cids described
133 EXPECT_GT(num_cids_checked, 900);
134 }
135 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
136
137 private:
138 CPDF_Document* cpdf_doc_;
139};
Tom Sepezd483eb42016-01-06 10:03:59 -0800140
etienneb7712c262016-04-26 08:13:45 -0700141namespace {
thestigdc7ec032016-11-21 15:32:52 -0800142
etienneb7712c262016-04-26 08:13:45 -0700143const char kExpectedPDF[] =
144 "%PDF-1.7\r\n"
145 "%\xA1\xB3\xC5\xD7\r\n"
146 "1 0 obj\r\n"
147 "<</Pages 2 0 R /Type/Catalog>>\r\n"
148 "endobj\r\n"
149 "2 0 obj\r\n"
150 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
151 "endobj\r\n"
152 "3 0 obj\r\n"
153 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
154 "endobj\r\n"
155 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400156 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
157 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400158 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700159 ">>\r\n"
160 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400161 "5 0 obj\r\n"
162 "<</BM/Normal/CA 1/ca 1>>\r\n"
163 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700164 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400165 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700166 "0000000000 65535 f\r\n"
167 "0000000017 00000 n\r\n"
168 "0000000066 00000 n\r\n"
169 "0000000122 00000 n\r\n"
170 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400171 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700172 "trailer\r\n"
173 "<<\r\n"
174 "/Root 1 0 R\r\n"
175 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400176 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700177 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400178 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700179 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800180
etienneb7712c262016-04-26 08:13:45 -0700181} // namespace
182
Lei Zhangab41f252018-12-23 03:10:50 +0000183TEST_F(FPDFEditEmbedderTest, EmptyCreation) {
Tom Sepezd483eb42016-01-06 10:03:59 -0800184 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700185 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800186 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400187 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800188 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800189 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700190
Nicolas Penad9d6c292017-06-06 16:12:10 -0400191 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
192 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700193 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800194}
thestigdc7ec032016-11-21 15:32:52 -0800195
196// Regression test for https://crbug.com/667012
Lei Zhangab41f252018-12-23 03:10:50 +0000197TEST_F(FPDFEditEmbedderTest, RasterizePDF) {
thestigdc7ec032016-11-21 15:32:52 -0800198 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
199
Lei Zhangf0542892019-01-17 18:46:27 +0000200 // Get the bitmap for the original document.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000201 ScopedFPDFBitmap orig_bitmap;
thestigdc7ec032016-11-21 15:32:52 -0800202 {
203 EXPECT_TRUE(OpenDocument("black.pdf"));
204 FPDF_PAGE orig_page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000205 ASSERT_TRUE(orig_page);
206 orig_bitmap = RenderLoadedPage(orig_page);
207 CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800208 UnloadPage(orig_page);
209 }
210
211 // Create a new document from |orig_bitmap| and save it.
212 {
213 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
214 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
215
216 // Add the bitmap to an image object and add the image object to the output
217 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700218 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000219 EXPECT_TRUE(
220 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get()));
thestigdc7ec032016-11-21 15:32:52 -0800221 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
222 FPDFPage_InsertObject(temp_page, temp_img);
223 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
224 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
225 FPDF_ClosePage(temp_page);
226 FPDF_CloseDocument(temp_doc);
227 }
thestigdc7ec032016-11-21 15:32:52 -0800228
229 // Get the generated content. Make sure it is at least as big as the original
230 // PDF.
Lei Zhangd72fd582018-07-27 19:37:27 +0000231 EXPECT_GT(GetString().size(), 923u);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400232 VerifySavedDocument(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800233}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500234
Lei Zhangab41f252018-12-23 03:10:50 +0000235TEST_F(FPDFEditEmbedderTest, AddPaths) {
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500236 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500237 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000238 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500239
240 // We will first add a red rectangle
241 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000242 ASSERT_TRUE(red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500243 // Expect false when trying to set colors out of range
244 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
245 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
246
247 // Fill rectangle with red and insert to the page
248 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
249 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
Miklos Vajna491112b2018-05-30 13:30:10 +0000250
251 int fillmode = FPDF_FILLMODE_NONE;
252 FPDF_BOOL stroke = true;
253 EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke));
254 EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode);
255 EXPECT_FALSE(stroke);
256
Miklos Vajna97f4d672018-06-04 14:47:17 +0000257 double matrix_a = 1;
258 double matrix_b = 2;
259 double matrix_c = 3;
260 double matrix_d = 4;
261 double matrix_e = 5;
262 double matrix_f = 6;
263 EXPECT_FALSE(FPDFPath_SetMatrix(nullptr, matrix_a, matrix_b, matrix_c,
264 matrix_d, matrix_e, matrix_f));
265 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
266 matrix_d, matrix_e, matrix_f));
Miklos Vajnaac42dd22018-06-05 13:28:58 +0000267 // Set to 0 before FPDFPath_GetMatrix() to ensure they are actually set by
268 // the function.
269 matrix_a = 0;
270 matrix_b = 0;
271 matrix_c = 0;
272 matrix_d = 0;
273 matrix_e = 0;
274 matrix_f = 0;
Miklos Vajna97f4d672018-06-04 14:47:17 +0000275 EXPECT_FALSE(FPDFPath_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
276 &matrix_d, &matrix_e, &matrix_f));
277 EXPECT_TRUE(FPDFPath_GetMatrix(red_rect, &matrix_a, &matrix_b, &matrix_c,
278 &matrix_d, &matrix_e, &matrix_f));
279 EXPECT_EQ(1, static_cast<int>(matrix_a));
280 EXPECT_EQ(2, static_cast<int>(matrix_b));
281 EXPECT_EQ(3, static_cast<int>(matrix_c));
282 EXPECT_EQ(4, static_cast<int>(matrix_d));
283 EXPECT_EQ(5, static_cast<int>(matrix_e));
284 EXPECT_EQ(6, static_cast<int>(matrix_f));
285 // Set back the default
286 matrix_a = 1;
287 matrix_b = 0;
288 matrix_c = 0;
289 matrix_d = 1;
290 matrix_e = 0;
291 matrix_f = 0;
292 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
293 matrix_d, matrix_e, matrix_f));
294
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500295 FPDFPage_InsertObject(page, red_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000296 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000297 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000298 CompareBitmap(page_bitmap.get(), 612, 792,
299 "66d02eaa6181e2c069ce2ea99beda497");
300 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500301
302 // Now add to that a green rectangle with some medium alpha
303 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
304 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200305
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200306 // Make sure the type of the rectangle is a path.
307 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
308
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200309 // Make sure we get back the same color we set previously.
310 unsigned int R;
311 unsigned int G;
312 unsigned int B;
313 unsigned int A;
314 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
Lei Zhangd72fd582018-07-27 19:37:27 +0000315 EXPECT_EQ(0u, R);
316 EXPECT_EQ(255u, G);
317 EXPECT_EQ(0u, B);
318 EXPECT_EQ(128u, A);
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200319
Miklos Vajna12abfd02017-09-15 07:49:03 +0200320 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
321 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200322 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200323 // Verify actual coordinates.
324 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
325 float x;
326 float y;
327 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
328 EXPECT_EQ(100, x);
329 EXPECT_EQ(100, y);
330 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
331 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
332 segment = FPDFPath_GetPathSegment(green_rect, 1);
333 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
334 EXPECT_EQ(100, x);
335 EXPECT_EQ(140, y);
336 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
337 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
338 segment = FPDFPath_GetPathSegment(green_rect, 2);
339 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
340 EXPECT_EQ(140, x);
341 EXPECT_EQ(140, y);
342 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
343 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
344 segment = FPDFPath_GetPathSegment(green_rect, 3);
345 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
346 EXPECT_EQ(140, x);
347 EXPECT_EQ(100, y);
348 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
349 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
350 segment = FPDFPath_GetPathSegment(green_rect, 4);
351 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
352 EXPECT_EQ(100, x);
353 EXPECT_EQ(100, y);
354 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
355 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200356
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500357 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
358 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000359 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000360 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000361 CompareBitmap(page_bitmap.get(), 612, 792,
362 "7b0b87604594e773add528fae567a558");
363 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500364
365 // Add a black triangle.
366 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
367 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
368 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
369 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
370 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
371 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200372
373 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
374 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200375 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200376 // Verify actual coordinates.
377 segment = FPDFPath_GetPathSegment(black_path, 0);
378 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
379 EXPECT_EQ(400, x);
380 EXPECT_EQ(100, y);
381 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
382 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
383 segment = FPDFPath_GetPathSegment(black_path, 1);
384 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
385 EXPECT_EQ(400, x);
386 EXPECT_EQ(200, y);
387 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
388 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
389 segment = FPDFPath_GetPathSegment(black_path, 2);
390 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
391 EXPECT_EQ(300, x);
392 EXPECT_EQ(100, y);
393 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
394 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
395 // Make sure out of bounds index access fails properly.
396 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200397
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500398 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000399 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000400 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000401 CompareBitmap(page_bitmap.get(), 612, 792,
402 "eadc8020a14dfcf091da2688733d8806");
403 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500404
405 // Now add a more complex blue path.
406 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
407 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
408 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
409 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
410 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
411 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
412 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
413 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
414 EXPECT_TRUE(FPDFPath_Close(blue_path));
415 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000416 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
417 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000418 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000419 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
420 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500421
422 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400423 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500424 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500425 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500426
427 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000428 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500429}
430
Lei Zhangab41f252018-12-23 03:10:50 +0000431TEST_F(FPDFEditEmbedderTest, SetText) {
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000432 // Load document with some text.
433 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
434 FPDF_PAGE page = LoadPage(0);
435 ASSERT_TRUE(page);
436
437 // Get the "Hello, world!" text object and change it.
438 ASSERT_EQ(2, FPDFPage_CountObjects(page));
439 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
440 ASSERT_TRUE(page_object);
441 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
442 GetFPDFWideString(L"Changed for SetText test");
443 EXPECT_TRUE(FPDFText_SetText(page_object, text1.get()));
444
445 // Verify the "Hello, world!" text is gone and "Changed for SetText test" is
446 // now displayed.
447 ASSERT_EQ(2, FPDFPage_CountObjects(page));
448#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
449 const char kChangedMD5[] = "94c1e7a5af7dd9d77dc2223b1091acb7";
450#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000451 const char kChangedMD5[] = "3137fdb27962671f5c3963a5e965eff5";
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000452#else
453 const char kChangedMD5[] = "a0c4ea6620772991f66bf7130379b08a";
454#endif
455 {
456 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
457 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
458 }
459
460 // Now save the result.
461 EXPECT_TRUE(FPDFPage_GenerateContent(page));
462 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
463
464 UnloadPage(page);
465
466 // Re-open the file and check the changes were kept in the saved .pdf.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000467 OpenSavedDocument(nullptr);
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000468 FPDF_PAGE saved_page = LoadSavedPage(0);
469 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
470 {
471 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
472 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
473 }
474
475 CloseSavedPage(saved_page);
476 CloseSavedDocument();
477}
478
Lei Zhangab41f252018-12-23 03:10:50 +0000479TEST_F(FPDFEditEmbedderTest, RemovePageObject) {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000480 // Load document with some text.
481 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
482 FPDF_PAGE page = LoadPage(0);
483 ASSERT_TRUE(page);
484
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000485 // Show what the original file looks like.
486 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000487#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000488 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000489#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000490 const char kOriginalMD5[] = "795b7ce1626931aa06af0fa23b7d80bb";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000491#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000492 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000493#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000494 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000495 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
496 }
497
498 // Get the "Hello, world!" text object and remove it.
499 ASSERT_EQ(2, FPDFPage_CountObjects(page));
500 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
501 ASSERT_TRUE(page_object);
502 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
503
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000504 // Verify the "Hello, world!" text is gone.
505 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000506#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000507 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000508#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000509 const char kRemovedMD5[] = "aae6c5334721f90ec30d3d59f4ef7deb";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000510#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000511 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000512#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000513 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000514 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
515 }
516 ASSERT_EQ(1, FPDFPage_CountObjects(page));
517
518 UnloadPage(page);
519 FPDFPageObj_Destroy(page_object);
520}
521
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000522void CheckMarkCounts(FPDF_PAGE page,
523 int start_from,
524 int expected_object_count,
525 size_t expected_prime_count,
526 size_t expected_square_count,
527 size_t expected_greater_than_ten_count,
528 size_t expected_bounds_count) {
529 int object_count = FPDFPage_CountObjects(page);
530 ASSERT_EQ(expected_object_count, object_count);
531
532 size_t prime_count = 0;
533 size_t square_count = 0;
534 size_t greater_than_ten_count = 0;
535 size_t bounds_count = 0;
536 for (int i = 0; i < object_count; ++i) {
537 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
538
539 int mark_count = FPDFPageObj_CountMarks(page_object);
540 for (int j = 0; j < mark_count; ++j) {
541 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
542
543 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000544 unsigned long name_len = 999u;
545 ASSERT_TRUE(
546 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
547 EXPECT_GT(name_len, 0u);
548 EXPECT_NE(999u, name_len);
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000549 std::wstring name =
550 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
551 if (name == L"Prime") {
552 prime_count++;
553 } else if (name == L"Square") {
554 square_count++;
555 int expected_square = start_from + i;
556 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
557
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000558 unsigned long get_param_key_return = 999u;
559 ASSERT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer),
560 &get_param_key_return));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000561 EXPECT_EQ((6u + 1u) * 2u, get_param_key_return);
562 std::wstring key =
563 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
564 EXPECT_EQ(L"Factor", key);
565
566 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000567 FPDFPageObjMark_GetParamValueType(mark, "Factor"));
Henrique Nakashima140dead2018-07-11 21:40:03 +0000568 int square_root;
Henrique Nakashima94230e52018-07-11 22:02:02 +0000569 EXPECT_TRUE(
570 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &square_root));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000571 EXPECT_EQ(expected_square, square_root * square_root);
572 } else if (name == L"GreaterThanTen") {
573 greater_than_ten_count++;
574 } else if (name == L"Bounds") {
575 bounds_count++;
576 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
577
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000578 unsigned long get_param_key_return = 999u;
579 ASSERT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer),
580 &get_param_key_return));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000581 EXPECT_EQ((8u + 1u) * 2u, get_param_key_return);
582 std::wstring key =
583 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
584 EXPECT_EQ(L"Position", key);
585
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000586 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000587 FPDFPageObjMark_GetParamValueType(mark, "Position"));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000588 unsigned long length;
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000589 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
590 mark, "Position", buffer, sizeof(buffer), &length));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000591 ASSERT_GT(length, 0u);
Henrique Nakashima140dead2018-07-11 21:40:03 +0000592 std::wstring value =
593 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000594
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000595 // "Position" can be "First", "Last", or "End".
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000596 if (i == 0) {
597 EXPECT_EQ((5u + 1u) * 2u, length);
598 EXPECT_EQ(L"First", value);
599 } else if (i == object_count - 1) {
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000600 if (length == (4u + 1u) * 2u) {
601 EXPECT_EQ(L"Last", value);
602 } else if (length == (3u + 1u) * 2u) {
603 EXPECT_EQ(L"End", value);
604 } else {
605 FAIL();
606 }
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000607 } else {
608 FAIL();
609 }
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000610 } else {
611 FAIL();
612 }
613 }
614 }
615
616 // Expect certain number of tagged objects. The test file contains strings
617 // from 1 to 19.
618 EXPECT_EQ(expected_prime_count, prime_count);
619 EXPECT_EQ(expected_square_count, square_count);
620 EXPECT_EQ(expected_greater_than_ten_count, greater_than_ten_count);
621 EXPECT_EQ(expected_bounds_count, bounds_count);
622}
623
Lei Zhangab41f252018-12-23 03:10:50 +0000624TEST_F(FPDFEditEmbedderTest, ReadMarkedObjectsIndirectDict) {
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000625 // Load document with some text marked with an indirect property.
626 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
627 FPDF_PAGE page = LoadPage(0);
628 ASSERT_TRUE(page);
629
630 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
631
632 UnloadPage(page);
633}
634
Lei Zhangab41f252018-12-23 03:10:50 +0000635TEST_F(FPDFEditEmbedderTest, RemoveMarkedObjectsPrime) {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000636 // Load document with some text.
637 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
638 FPDF_PAGE page = LoadPage(0);
639 ASSERT_TRUE(page);
640
641 // Show what the original file looks like.
642 {
643#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
644 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
645#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
646 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
647#else
648 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
649#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000650 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000651 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
652 }
653
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000654 constexpr int expected_object_count = 19;
655 CheckMarkCounts(page, 1, expected_object_count, 8, 4, 9, 1);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000656
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000657 // Get all objects marked with "Prime"
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000658 std::vector<FPDF_PAGEOBJECT> primes;
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000659 for (int i = 0; i < expected_object_count; ++i) {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000660 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
661
662 int mark_count = FPDFPageObj_CountMarks(page_object);
663 for (int j = 0; j < mark_count; ++j) {
664 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
665
666 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000667 unsigned long name_len = 999u;
668 ASSERT_TRUE(
669 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
670 EXPECT_GT(name_len, 0u);
671 EXPECT_NE(999u, name_len);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000672 std::wstring name =
673 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
674 if (name == L"Prime") {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000675 primes.push_back(page_object);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000676 }
677 }
678 }
679
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000680 // Remove all objects marked with "Prime".
681 for (FPDF_PAGEOBJECT page_object : primes) {
682 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
683 FPDFPageObj_Destroy(page_object);
684 }
685
686 EXPECT_EQ(11, FPDFPage_CountObjects(page));
687
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000688#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000689 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000690#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000691 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000692#else
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000693 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000694#endif
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000695 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000696 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000697 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
698 }
699
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000700 // Save the file.
701 EXPECT_TRUE(FPDFPage_GenerateContent(page));
702 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000703 UnloadPage(page);
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000704
705 // Re-open the file and check the prime marks are not there anymore.
706 OpenSavedDocument(nullptr);
707 FPDF_PAGE saved_page = LoadSavedPage(0);
708 EXPECT_EQ(11, FPDFPage_CountObjects(saved_page));
709
710 {
711 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
712 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
713 }
714
715 CloseSavedPage(saved_page);
716 CloseSavedDocument();
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000717}
718
Lei Zhangab41f252018-12-23 03:10:50 +0000719TEST_F(FPDFEditEmbedderTest, RemoveMarks) {
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000720 // Load document with some text.
721 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
722 FPDF_PAGE page = LoadPage(0);
723 ASSERT_TRUE(page);
724
725 constexpr int kExpectedObjectCount = 19;
726 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
727
728 // Remove all "Prime" content marks.
729 for (int i = 0; i < kExpectedObjectCount; ++i) {
730 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
731
732 int mark_count = FPDFPageObj_CountMarks(page_object);
733 for (int j = mark_count - 1; j >= 0; --j) {
734 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
735
736 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000737 unsigned long name_len = 999u;
738 ASSERT_TRUE(
739 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
740 EXPECT_GT(name_len, 0u);
741 EXPECT_NE(999u, name_len);
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000742 std::wstring name =
743 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
744 if (name == L"Prime") {
745 // Remove mark.
746 EXPECT_TRUE(FPDFPageObj_RemoveMark(page_object, mark));
747
748 // Verify there is now one fewer mark in the page object.
749 EXPECT_EQ(mark_count - 1, FPDFPageObj_CountMarks(page_object));
750 }
751 }
752 }
753
754 // Verify there are 0 "Prime" content marks now.
755 CheckMarkCounts(page, 1, kExpectedObjectCount, 0, 4, 9, 1);
756
757 // Save the file.
758 EXPECT_TRUE(FPDFPage_GenerateContent(page));
759 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
760 UnloadPage(page);
761
762 // Re-open the file and check the prime marks are not there anymore.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000763 OpenSavedDocument(nullptr);
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000764 FPDF_PAGE saved_page = LoadSavedPage(0);
765
766 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 0, 4, 9, 1);
767
768 CloseSavedPage(saved_page);
769 CloseSavedDocument();
770}
771
Lei Zhangab41f252018-12-23 03:10:50 +0000772TEST_F(FPDFEditEmbedderTest, RemoveMarkParam) {
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000773 // Load document with some text.
774 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
775 FPDF_PAGE page = LoadPage(0);
776 ASSERT_TRUE(page);
777
778 constexpr int kExpectedObjectCount = 19;
779 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
780
781 // Remove all "Square" content marks parameters.
782 for (int i = 0; i < kExpectedObjectCount; ++i) {
783 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
784
785 int mark_count = FPDFPageObj_CountMarks(page_object);
786 for (int j = 0; j < mark_count; ++j) {
787 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
788
789 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000790 unsigned long name_len = 999u;
791 ASSERT_TRUE(
792 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
793 EXPECT_GT(name_len, 0u);
794 EXPECT_NE(999u, name_len);
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000795 std::wstring name =
796 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
797 if (name == L"Square") {
798 // Show the mark has a "Factor" parameter.
799 int out_value;
800 EXPECT_TRUE(
801 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
802
803 // Remove parameter.
804 EXPECT_TRUE(FPDFPageObjMark_RemoveParam(page_object, mark, "Factor"));
805
806 // Verify the "Factor" parameter is gone.
807 EXPECT_FALSE(
808 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
809 }
810 }
811 }
812
813 // Save the file.
814 EXPECT_TRUE(FPDFPage_GenerateContent(page));
815 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
816 UnloadPage(page);
817
818 // Re-open the file and check the "Factor" parameters are still gone.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000819 OpenSavedDocument(nullptr);
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000820 FPDF_PAGE saved_page = LoadSavedPage(0);
821
822 size_t square_count = 0;
823 for (int i = 0; i < kExpectedObjectCount; ++i) {
824 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
825
826 int mark_count = FPDFPageObj_CountMarks(page_object);
827 for (int j = 0; j < mark_count; ++j) {
828 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
829
830 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000831 unsigned long name_len = 999u;
832 ASSERT_TRUE(
833 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
834 EXPECT_GT(name_len, 0u);
835 EXPECT_NE(999u, name_len);
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000836 std::wstring name =
837 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
838 if (name == L"Square") {
839 // Verify the "Factor" parameter is still gone.
840 int out_value;
841 EXPECT_FALSE(
842 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
843
844 ++square_count;
845 }
846 }
847 }
848
849 // Verify the parameters are gone, but the marks are not.
850 EXPECT_EQ(4u, square_count);
851
852 CloseSavedPage(saved_page);
853 CloseSavedDocument();
854}
855
Lei Zhangab41f252018-12-23 03:10:50 +0000856TEST_F(FPDFEditEmbedderTest, MaintainMarkedObjects) {
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000857 // Load document with some text.
858 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
859 FPDF_PAGE page = LoadPage(0);
860 ASSERT_TRUE(page);
861
862 // Iterate over all objects, counting the number of times each content mark
863 // name appears.
864 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
865
866 // Remove first page object.
867 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
868 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
869 FPDFPageObj_Destroy(page_object);
870
871 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
872
873 EXPECT_TRUE(FPDFPage_GenerateContent(page));
874 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
875
876 UnloadPage(page);
877
Tom Sepezb9c3e272018-08-14 18:22:06 +0000878 OpenSavedDocument(nullptr);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000879 FPDF_PAGE saved_page = LoadSavedPage(0);
880
881 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
882
883 CloseSavedPage(saved_page);
884 CloseSavedDocument();
885}
886
Lei Zhangab41f252018-12-23 03:10:50 +0000887TEST_F(FPDFEditEmbedderTest, MaintainIndirectMarkedObjects) {
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000888 // Load document with some text.
889 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
890 FPDF_PAGE page = LoadPage(0);
891 ASSERT_TRUE(page);
892
893 // Iterate over all objects, counting the number of times each content mark
894 // name appears.
895 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
896
897 // Remove first page object.
898 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
899 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
900 FPDFPageObj_Destroy(page_object);
901
902 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
903
904 EXPECT_TRUE(FPDFPage_GenerateContent(page));
905 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
906
907 UnloadPage(page);
908
Tom Sepezb9c3e272018-08-14 18:22:06 +0000909 OpenSavedDocument(nullptr);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000910 FPDF_PAGE saved_page = LoadSavedPage(0);
911
912 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
913
914 CloseSavedPage(saved_page);
915 CloseSavedDocument();
916}
917
Lei Zhangab41f252018-12-23 03:10:50 +0000918TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObject) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000919 // Load document with some text.
920 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
921 FPDF_PAGE page = LoadPage(0);
922 ASSERT_TRUE(page);
923
924 // Get the "Hello, world!" text object and remove it.
925 ASSERT_EQ(2, FPDFPage_CountObjects(page));
926 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
927 ASSERT_TRUE(page_object);
928 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
929
930 // Verify the "Hello, world!" text is gone.
931 ASSERT_EQ(1, FPDFPage_CountObjects(page));
932
933 // Save the file
934 EXPECT_TRUE(FPDFPage_GenerateContent(page));
935 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
936 UnloadPage(page);
937 FPDFPageObj_Destroy(page_object);
938
939 // Re-open the file and check the page object count is still 1.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000940 OpenSavedDocument(nullptr);
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000941 FPDF_PAGE saved_page = LoadSavedPage(0);
942 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
943 CloseSavedPage(saved_page);
944 CloseSavedDocument();
945}
946
Lei Zhangab41f252018-12-23 03:10:50 +0000947TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObjectSplitStreamsNotLonely) {
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000948 // Load document with some text.
949 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
950 FPDF_PAGE page = LoadPage(0);
951 ASSERT_TRUE(page);
952
953 // Get the "Hello, world!" text object and remove it. There is another object
954 // in the same stream that says "Goodbye, world!"
955 ASSERT_EQ(3, FPDFPage_CountObjects(page));
956 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
957 ASSERT_TRUE(page_object);
958 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
959
960 // Verify the "Hello, world!" text is gone.
961 ASSERT_EQ(2, FPDFPage_CountObjects(page));
962#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
963 const char kHelloRemovedMD5[] = "e07a62d412728fc4d6e3ff42f2dd0e11";
964#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000965 const char kHelloRemovedMD5[] = "a97d4c72c969ba373c2dce675d277e65";
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000966#else
967 const char kHelloRemovedMD5[] = "95b92950647a2190e1230911e7a1a0e9";
968#endif
969 {
970 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
971 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
972 }
973
974 // Save the file
975 EXPECT_TRUE(FPDFPage_GenerateContent(page));
976 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
977 UnloadPage(page);
978 FPDFPageObj_Destroy(page_object);
979
980 // Re-open the file and check the page object count is still 2.
Tom Sepezb9c3e272018-08-14 18:22:06 +0000981 OpenSavedDocument(nullptr);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000982 FPDF_PAGE saved_page = LoadSavedPage(0);
983
984 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
985 {
986 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
987 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
988 }
989
990 CloseSavedPage(saved_page);
991 CloseSavedDocument();
992}
993
Lei Zhangab41f252018-12-23 03:10:50 +0000994TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObjectSplitStreamsLonely) {
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000995 // Load document with some text.
996 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
997 FPDF_PAGE page = LoadPage(0);
998 ASSERT_TRUE(page);
999
1000 // Get the "Greetings, world!" text object and remove it. This is the only
1001 // object in the stream.
1002 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1003 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 2);
1004 ASSERT_TRUE(page_object);
1005 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
1006
1007 // Verify the "Greetings, world!" text is gone.
1008 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1009#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1010 const char kGreetingsRemovedMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
1011#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001012 const char kGreetingsRemovedMD5[] = "795b7ce1626931aa06af0fa23b7d80bb";
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001013#else
1014 const char kGreetingsRemovedMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
1015#endif
1016 {
1017 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1018 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
1019 }
1020
1021 // Save the file
1022 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1023 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1024 UnloadPage(page);
1025 FPDFPageObj_Destroy(page_object);
1026
1027 // Re-open the file and check the page object count is still 2.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001028 OpenSavedDocument(nullptr);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001029 FPDF_PAGE saved_page = LoadSavedPage(0);
1030
1031 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
1032 {
1033 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1034 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
1035 }
1036
1037 CloseSavedPage(saved_page);
1038 CloseSavedDocument();
1039}
1040
Lei Zhangab41f252018-12-23 03:10:50 +00001041TEST_F(FPDFEditEmbedderTest, GetContentStream) {
Henrique Nakashima6eb79392018-06-12 20:27:35 +00001042 // Load document with some text split across streams.
1043 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
1044 FPDF_PAGE page = LoadPage(0);
1045 ASSERT_TRUE(page);
1046
1047 // Content stream 0: page objects 0-14.
1048 // Content stream 1: page objects 15-17.
1049 // Content stream 2: page object 18.
1050 ASSERT_EQ(19, FPDFPage_CountObjects(page));
1051 for (int i = 0; i < 19; i++) {
1052 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1053 ASSERT_TRUE(page_object);
1054 CPDF_PageObject* cpdf_page_object =
1055 CPDFPageObjectFromFPDFPageObject(page_object);
1056 if (i < 15)
1057 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1058 else if (i < 18)
1059 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1060 else
1061 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
1062 }
1063
1064 UnloadPage(page);
1065}
1066
Lei Zhangab41f252018-12-23 03:10:50 +00001067TEST_F(FPDFEditEmbedderTest, RemoveAllFromStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001068 // Load document with some text split across streams.
1069 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
1070 FPDF_PAGE page = LoadPage(0);
1071 ASSERT_TRUE(page);
1072
1073 // Content stream 0: page objects 0-14.
1074 // Content stream 1: page objects 15-17.
1075 // Content stream 2: page object 18.
1076 ASSERT_EQ(19, FPDFPage_CountObjects(page));
1077
1078 // Loop backwards because objects will being removed, which shifts the indexes
1079 // after the removed position.
1080 for (int i = 18; i >= 0; i--) {
1081 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1082 ASSERT_TRUE(page_object);
1083 CPDF_PageObject* cpdf_page_object =
1084 CPDFPageObjectFromFPDFPageObject(page_object);
1085
1086 // Empty content stream 1.
1087 if (cpdf_page_object->GetContentStream() == 1) {
1088 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
1089 FPDFPageObj_Destroy(page_object);
1090 }
1091 }
1092
1093 // Content stream 0: page objects 0-14.
1094 // Content stream 2: page object 15.
1095 ASSERT_EQ(16, FPDFPage_CountObjects(page));
1096 for (int i = 0; i < 16; i++) {
1097 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1098 ASSERT_TRUE(page_object);
1099 CPDF_PageObject* cpdf_page_object =
1100 CPDFPageObjectFromFPDFPageObject(page_object);
1101 if (i < 15)
1102 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1103 else
1104 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
1105 }
1106
1107 // Generate contents should remove the empty stream and update the page
1108 // objects' contents stream indexes.
1109 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1110
1111 // Content stream 0: page objects 0-14.
1112 // Content stream 1: page object 15.
1113 ASSERT_EQ(16, FPDFPage_CountObjects(page));
1114 for (int i = 0; i < 16; i++) {
1115 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1116 ASSERT_TRUE(page_object);
1117 CPDF_PageObject* cpdf_page_object =
1118 CPDFPageObjectFromFPDFPageObject(page_object);
1119 if (i < 15)
1120 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1121 else
1122 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1123 }
1124
1125#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1126 const char kStream1RemovedMD5[] = "d2e21fbd5a6de563f619feeeb6163331";
1127#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1128 const char kStream1RemovedMD5[] = "b4140f203523e38793283a5943d8075b";
1129#else
1130 const char kStream1RemovedMD5[] = "e86a3efc160ede6cfcb1f59bcacf1105";
1131#endif
1132 {
1133 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1134 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
1135 }
1136
1137 // Save the file
1138 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1139 UnloadPage(page);
1140
1141 // Re-open the file and check the page object count is still 16, and that
1142 // content stream 1 was removed.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001143 OpenSavedDocument(nullptr);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001144 FPDF_PAGE saved_page = LoadSavedPage(0);
1145
1146 // Content stream 0: page objects 0-14.
1147 // Content stream 1: page object 15.
1148 EXPECT_EQ(16, FPDFPage_CountObjects(saved_page));
1149 for (int i = 0; i < 16; i++) {
1150 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
1151 ASSERT_TRUE(page_object);
1152 CPDF_PageObject* cpdf_page_object =
1153 CPDFPageObjectFromFPDFPageObject(page_object);
1154 if (i < 15)
1155 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1156 else
1157 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1158 }
1159
1160 {
1161 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1162 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
1163 }
1164
1165 CloseSavedPage(saved_page);
1166 CloseSavedDocument();
1167}
1168
Lei Zhangab41f252018-12-23 03:10:50 +00001169TEST_F(FPDFEditEmbedderTest, RemoveAllFromSingleStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001170 // Load document with a single stream.
1171 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1172 FPDF_PAGE page = LoadPage(0);
1173 ASSERT_TRUE(page);
1174
1175 // Content stream 0: page objects 0-1.
1176 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1177
1178 // Loop backwards because objects will being removed, which shifts the indexes
1179 // after the removed position.
1180 for (int i = 1; i >= 0; i--) {
1181 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1182 ASSERT_TRUE(page_object);
1183 CPDF_PageObject* cpdf_page_object =
1184 CPDFPageObjectFromFPDFPageObject(page_object);
1185 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1186 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1187 FPDFPageObj_Destroy(page_object);
1188 }
1189
1190 // No more objects in the stream
1191 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1192
1193 // Generate contents should remove the empty stream and update the page
1194 // objects' contents stream indexes.
1195 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1196
1197 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1198
1199 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1200 {
1201 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1202 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1203 }
1204
1205 // Save the file
1206 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1207 UnloadPage(page);
1208
1209 // Re-open the file and check the page object count is still 0.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001210 OpenSavedDocument(nullptr);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001211 FPDF_PAGE saved_page = LoadSavedPage(0);
1212
1213 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1214 {
1215 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1216 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1217 }
1218
1219 CloseSavedPage(saved_page);
1220 CloseSavedDocument();
1221}
1222
Lei Zhangab41f252018-12-23 03:10:50 +00001223TEST_F(FPDFEditEmbedderTest, RemoveFirstFromSingleStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001224 // Load document with a single stream.
1225 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1226 FPDF_PAGE page = LoadPage(0);
1227 ASSERT_TRUE(page);
1228
1229 // Content stream 0: page objects 0-1.
1230 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1231
1232 // Remove first object.
1233 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
1234 ASSERT_TRUE(page_object);
1235 CPDF_PageObject* cpdf_page_object =
1236 CPDFPageObjectFromFPDFPageObject(page_object);
1237 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1238 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1239 FPDFPageObj_Destroy(page_object);
1240
1241 // One object left in the stream.
1242 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1243 page_object = FPDFPage_GetObject(page, 0);
1244 ASSERT_TRUE(page_object);
1245 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1246 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1247
1248 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1249
1250 // Still one object left in the stream.
1251 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1252 page_object = FPDFPage_GetObject(page, 0);
1253 ASSERT_TRUE(page_object);
1254 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1255 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1256
1257#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1258 const char kFirstRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
1259#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001260 const char kFirstRemovedMD5[] = "aae6c5334721f90ec30d3d59f4ef7deb";
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001261#else
1262 const char kFirstRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
1263#endif
1264 {
1265 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1266 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1267 }
1268
1269 // Save the file
1270 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1271 UnloadPage(page);
1272
1273 // Re-open the file and check the page object count is still 0.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001274 OpenSavedDocument(nullptr);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001275 FPDF_PAGE saved_page = LoadSavedPage(0);
1276
1277 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1278 page_object = FPDFPage_GetObject(saved_page, 0);
1279 ASSERT_TRUE(page_object);
1280 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1281 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1282 {
1283 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1284 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1285 }
1286
1287 CloseSavedPage(saved_page);
1288 CloseSavedDocument();
1289}
1290
Lei Zhangab41f252018-12-23 03:10:50 +00001291TEST_F(FPDFEditEmbedderTest, RemoveLastFromSingleStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001292 // Load document with a single stream.
1293 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1294 FPDF_PAGE page = LoadPage(0);
1295 ASSERT_TRUE(page);
1296
1297 // Content stream 0: page objects 0-1.
1298 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1299
1300 // Remove last object
1301 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 1);
1302 ASSERT_TRUE(page_object);
1303 CPDF_PageObject* cpdf_page_object =
1304 CPDFPageObjectFromFPDFPageObject(page_object);
1305 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1306 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1307 FPDFPageObj_Destroy(page_object);
1308
1309 // One object left in the stream.
1310 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1311 page_object = FPDFPage_GetObject(page, 0);
1312 ASSERT_TRUE(page_object);
1313 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1314 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1315
1316 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1317
1318 // Still one object left in the stream.
1319 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1320 page_object = FPDFPage_GetObject(page, 0);
1321 ASSERT_TRUE(page_object);
1322 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1323 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1324
1325#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1326 const char kLastRemovedMD5[] = "f8fbd14a048b9e2ea8e5f059f22a910e";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001327#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1328 const char kLastRemovedMD5[] = "93db13099042bafefb3c22a165bad684";
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001329#else
1330 const char kLastRemovedMD5[] = "93dcc09055f87a2792c8e3065af99a1b";
1331#endif
1332 {
1333 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1334 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1335 }
1336
1337 // Save the file
1338 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1339 UnloadPage(page);
1340
1341 // Re-open the file and check the page object count is still 0.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001342 OpenSavedDocument(nullptr);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001343 FPDF_PAGE saved_page = LoadSavedPage(0);
1344
1345 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1346 page_object = FPDFPage_GetObject(saved_page, 0);
1347 ASSERT_TRUE(page_object);
1348 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1349 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1350 {
1351 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1352 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1353 }
1354
1355 CloseSavedPage(saved_page);
1356 CloseSavedDocument();
1357}
1358
Lei Zhangab41f252018-12-23 03:10:50 +00001359TEST_F(FPDFEditEmbedderTest, RemoveAllFromMultipleStreams) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001360 // Load document with some text.
1361 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
1362 FPDF_PAGE page = LoadPage(0);
1363 ASSERT_TRUE(page);
1364
1365 // Content stream 0: page objects 0-1.
1366 // Content stream 1: page object 2.
1367 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1368
1369 // Loop backwards because objects will being removed, which shifts the indexes
1370 // after the removed position.
1371 for (int i = 2; i >= 0; i--) {
1372 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1373 ASSERT_TRUE(page_object);
1374 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1375 FPDFPageObj_Destroy(page_object);
1376 }
1377
1378 // No more objects in the page.
1379 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1380
1381 // Generate contents should remove the empty streams and update the page
1382 // objects' contents stream indexes.
1383 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1384
1385 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1386
1387 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1388 {
1389 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1390 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1391 }
1392
1393 // Save the file
1394 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1395 UnloadPage(page);
1396
1397 // Re-open the file and check the page object count is still 0.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001398 OpenSavedDocument(nullptr);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001399 FPDF_PAGE saved_page = LoadSavedPage(0);
1400
1401 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1402 {
1403 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1404 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1405 }
1406
1407 CloseSavedPage(saved_page);
1408 CloseSavedDocument();
1409}
1410
Lei Zhangab41f252018-12-23 03:10:50 +00001411TEST_F(FPDFEditEmbedderTest, InsertPageObjectAndSave) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001412 // Load document with some text.
1413 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1414 FPDF_PAGE page = LoadPage(0);
1415 ASSERT_TRUE(page);
1416
1417 // Add a red rectangle.
1418 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1419 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1420 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1421 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1422 FPDFPage_InsertObject(page, red_rect);
1423
1424 // Verify the red rectangle was added.
1425 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1426
1427 // Save the file
1428 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1429 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1430 UnloadPage(page);
1431
1432 // Re-open the file and check the page object count is still 3.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001433 OpenSavedDocument(nullptr);
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001434 FPDF_PAGE saved_page = LoadSavedPage(0);
1435 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1436 CloseSavedPage(saved_page);
1437 CloseSavedDocument();
1438}
1439
Lei Zhangab41f252018-12-23 03:10:50 +00001440TEST_F(FPDFEditEmbedderTest, InsertPageObjectEditAndSave) {
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001441 // Load document with some text.
1442 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1443 FPDF_PAGE page = LoadPage(0);
1444 ASSERT_TRUE(page);
1445
1446 // Add a red rectangle.
1447 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1448 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1449 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 100, 100, 255));
1450 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1451 FPDFPage_InsertObject(page, red_rect);
1452
1453 // Verify the red rectangle was added.
1454 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1455
1456 // Generate content but change it again
1457 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1458 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1459
1460 // Save the file
1461 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1462 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1463 UnloadPage(page);
1464
1465 // Re-open the file and check the page object count is still 3.
Tom Sepezb9c3e272018-08-14 18:22:06 +00001466 OpenSavedDocument(nullptr);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001467 FPDF_PAGE saved_page = LoadSavedPage(0);
1468 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1469 CloseSavedPage(saved_page);
1470 CloseSavedDocument();
1471}
1472
Lei Zhangab41f252018-12-23 03:10:50 +00001473TEST_F(FPDFEditEmbedderTest, InsertAndRemoveLargeFile) {
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001474 const int kOriginalObjectCount = 600;
1475
1476 // Load document with many objects.
1477 EXPECT_TRUE(OpenDocument("many_rectangles.pdf"));
1478 FPDF_PAGE page = LoadPage(0);
1479 ASSERT_TRUE(page);
1480 const char kOriginalMD5[] = "b0170c575b65ecb93ebafada0ff0f038";
1481 {
1482 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1483 CompareBitmap(page_bitmap.get(), 200, 300, kOriginalMD5);
1484 }
1485
1486 // Add a black rectangle.
1487 ASSERT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(page));
1488 FPDF_PAGEOBJECT black_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1489 EXPECT_TRUE(FPDFPath_SetFillColor(black_rect, 0, 0, 0, 255));
1490 EXPECT_TRUE(FPDFPath_SetDrawMode(black_rect, FPDF_FILLMODE_ALTERNATE, 0));
1491 FPDFPage_InsertObject(page, black_rect);
1492
1493 // Verify the black rectangle was added.
1494 ASSERT_EQ(kOriginalObjectCount + 1, FPDFPage_CountObjects(page));
1495 const char kPlusRectangleMD5[] = "6b9396ab570754b32b04ca629e902f77";
1496 {
1497 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1498 CompareBitmap(page_bitmap.get(), 200, 300, kPlusRectangleMD5);
1499 }
1500
1501 // Save the file.
1502 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1503 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1504 UnloadPage(page);
1505
1506 // Re-open the file and check the rectangle added is still there.
1507 OpenSavedDocument(nullptr);
1508 FPDF_PAGE saved_page = LoadSavedPage(0);
1509 EXPECT_EQ(kOriginalObjectCount + 1, FPDFPage_CountObjects(saved_page));
1510 {
1511 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1512 CompareBitmap(page_bitmap.get(), 200, 300, kPlusRectangleMD5);
1513 }
1514
1515 // Remove the added rectangle.
1516 FPDF_PAGEOBJECT added_object =
1517 FPDFPage_GetObject(saved_page, kOriginalObjectCount);
1518 EXPECT_TRUE(FPDFPage_RemoveObject(saved_page, added_object));
1519 FPDFPageObj_Destroy(added_object);
1520 {
1521 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1522 CompareBitmap(page_bitmap.get(), 200, 300, kOriginalMD5);
1523 }
1524 EXPECT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(saved_page));
1525
1526 // Save the file again.
1527 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
1528 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
1529
1530 CloseSavedPage(saved_page);
1531 CloseSavedDocument();
1532
1533 // Re-open the file (again) and check the black rectangle was removed and the
1534 // rest is intact.
1535 OpenSavedDocument(nullptr);
1536 saved_page = LoadSavedPage(0);
1537 EXPECT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(saved_page));
1538 {
1539 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1540 CompareBitmap(page_bitmap.get(), 200, 300, kOriginalMD5);
1541 }
1542
1543 CloseSavedPage(saved_page);
1544 CloseSavedDocument();
1545}
1546
Lei Zhangab41f252018-12-23 03:10:50 +00001547TEST_F(FPDFEditEmbedderTest, AddAndRemovePaths) {
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001548 // Start with a blank page.
1549 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1550 ASSERT_TRUE(page);
1551
1552 // Render the blank page and verify it's a blank bitmap.
1553 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
1554 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001555 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001556 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1557 }
1558 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1559
1560 // Add a red rectangle.
1561 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1562 ASSERT_TRUE(red_rect);
1563 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1564 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1565 FPDFPage_InsertObject(page, red_rect);
1566 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
1567 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001568 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001569 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
1570 }
1571 EXPECT_EQ(1, FPDFPage_CountObjects(page));
1572
1573 // Remove rectangle and verify it does not render anymore and the bitmap is
1574 // back to a blank one.
1575 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
1576 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001577 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001578 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1579 }
1580 EXPECT_EQ(0, FPDFPage_CountObjects(page));
1581
1582 // Trying to remove an object not in the page should return false.
1583 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
1584
1585 FPDF_ClosePage(page);
1586 FPDFPageObj_Destroy(red_rect);
1587}
1588
Lei Zhangab41f252018-12-23 03:10:50 +00001589TEST_F(FPDFEditEmbedderTest, PathsPoints) {
Miklos Vajna12abfd02017-09-15 07:49:03 +02001590 CreateNewDocument();
1591 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
1592 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +02001593 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001594
1595 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +02001596 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001597
Miklos Vajna36eed872017-09-20 22:52:43 +02001598 // FPDFPath_GetPathSegment() with a non-path.
1599 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
1600 // FPDFPath_GetPathSegment() with a NULL path.
1601 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
1602 float x;
1603 float y;
1604 // FPDFPathSegment_GetPoint() with a NULL segment.
1605 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
1606
1607 // FPDFPathSegment_GetType() with a NULL segment.
1608 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
1609
1610 // FPDFPathSegment_GetClose() with a NULL segment.
1611 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
1612
Miklos Vajna12abfd02017-09-15 07:49:03 +02001613 FPDFPageObj_Destroy(img);
1614}
1615
Lei Zhangab41f252018-12-23 03:10:50 +00001616TEST_F(FPDFEditEmbedderTest, PathOnTopOfText) {
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001617 // Load document with some text
1618 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1619 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001620 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001621
1622 // Add an opaque rectangle on top of some of the text.
1623 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1624 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1625 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1626 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001627
1628 // Add a transparent triangle on top of other part of the text.
1629 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
1630 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
1631 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
1632 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
1633 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
1634 EXPECT_TRUE(FPDFPath_Close(black_path));
1635 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001636
1637 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001638 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001639#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -07001640 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001641#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1642 const char md5[] = "74dd9c393b8b2578d2b7feb032b7daad";
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001643#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001644 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001645#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001646 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001647 UnloadPage(page);
1648}
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001649
Lei Zhangab41f252018-12-23 03:10:50 +00001650TEST_F(FPDFEditEmbedderTest, EditOverExistingContent) {
wileyryae858aa42017-05-31 14:49:05 -05001651 // Load document with existing content
1652 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
1653 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001654 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -05001655
1656 // Add a transparent rectangle on top of the existing content
1657 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
1658 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
1659 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
1660 FPDFPage_InsertObject(page, red_rect2);
1661
1662 // Add an opaque rectangle on top of the existing content
1663 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
1664 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1665 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1666 FPDFPage_InsertObject(page, red_rect);
1667
Tom Sepeze08d2b12018-04-25 18:49:32 +00001668 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001669 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001670 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1671
1672 // Now save the result, closing the page and document
1673 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -04001674 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -05001675
Tom Sepezb9c3e272018-08-14 18:22:06 +00001676 OpenSavedDocument(nullptr);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001677 FPDF_PAGE saved_page = LoadSavedPage(0);
1678 VerifySavedRendering(saved_page, 612, 792,
1679 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001680
1681 ClearString();
1682 // Add another opaque rectangle on top of the existing content
1683 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
1684 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
1685 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001686 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -05001687
1688 // Add another transparent rectangle on top of existing content
1689 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
1690 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
1691 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001692 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001693 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
1694 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001695 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001696 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
1697 }
Lei Zhang107fa7b2018-02-09 21:48:15 +00001698 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -05001699
1700 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +00001701 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001702
Lei Zhang107fa7b2018-02-09 21:48:15 +00001703 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001704 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -05001705
1706 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +00001707 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -05001708}
1709
Lei Zhangab41f252018-12-23 03:10:50 +00001710TEST_F(FPDFEditEmbedderTest, AddStrokedPaths) {
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001711 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001712 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001713
1714 // Add a large stroked rectangle (fill color should not affect it).
1715 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
1716 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
1717 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
1718 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
Miklos Vajna366df7f2018-05-22 14:27:29 +00001719
1720 float width = 0;
1721 EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width));
1722 EXPECT_EQ(15.0f, width);
1723
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001724 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
1725 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001726 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001727 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001728 CompareBitmap(page_bitmap.get(), 612, 792,
1729 "64bd31f862a89e0a9e505a5af6efd506");
1730 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001731
1732 // Add crossed-checkmark
1733 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
1734 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
1735 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
1736 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
1737 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
1738 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
1739 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
1740 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1741 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001742 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001743 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001744 CompareBitmap(page_bitmap.get(), 612, 792,
1745 "4b6f3b9d25c4e194821217d5016c3724");
1746 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001747
1748 // Add stroked and filled oval-ish path.
1749 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
1750 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
1751 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
1752 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
1753 EXPECT_TRUE(FPDFPath_Close(path));
1754 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
1755 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
1756 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
1757 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
1758 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001759 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001760 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001761 CompareBitmap(page_bitmap.get(), 612, 792,
1762 "ff3e6a22326754944cc6e56609acd73b");
1763 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001764 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001765}
Nicolas Pena49058402017-02-14 18:26:20 -05001766
Nicolas Pena4c48b102018-06-13 18:23:46 +00001767// Tests adding text from standard font using FPDFPageObj_NewTextObj.
Lei Zhangab41f252018-12-23 03:10:50 +00001768TEST_F(FPDFEditEmbedderTest, AddStandardFontText) {
Nicolas Pena49058402017-02-14 18:26:20 -05001769 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001770 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -05001771
1772 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001773 FPDF_PAGEOBJECT text_object1 =
1774 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1775 EXPECT_TRUE(text_object1);
1776 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
1777 GetFPDFWideString(L"I'm at the bottom of the page");
1778 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
1779 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
1780 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001781 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001782 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001783#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001784 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001785#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1786 const char md5[] = "08d1ff3e5a42801bee6077fd366bef00";
Nicolas Pena49058402017-02-14 18:26:20 -05001787#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001788 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001789#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001790 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1791 }
Nicolas Pena49058402017-02-14 18:26:20 -05001792
1793 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -04001794 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001795 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001796 EXPECT_TRUE(text_object2);
1797 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1798 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
1799 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1800 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
1801 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001802 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001803 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001804#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +00001805 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -04001806#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001807 const char md5_2[] = "3755dd35abd4c605755369401ee85b2d";
Nicolas Pena49058402017-02-14 18:26:20 -05001808#else
Dan Sinclair971a6742018-03-28 19:23:25 +00001809 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001810#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001811 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001812 }
Nicolas Pena49058402017-02-14 18:26:20 -05001813
1814 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -04001815 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001816 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001817 EXPECT_TRUE(text_object3);
1818 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
1819 GetFPDFWideString(L"Can you read me? <:)>");
1820 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
1821 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
1822 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001823 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001824 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001825#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001826 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -04001827#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001828 const char md5_3[] = "aba523a8110d01ed9bd7b7781ff74045";
Nicolas Pena49058402017-02-14 18:26:20 -05001829#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001830 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001831#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001832 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
1833 }
Nicolas Pena49058402017-02-14 18:26:20 -05001834
Miklos Vajnac765d2a2018-06-19 15:45:42 +00001835 double matrix_a = 0;
1836 double matrix_b = 0;
1837 double matrix_c = 0;
1838 double matrix_d = 0;
1839 double matrix_e = 0;
1840 double matrix_f = 0;
1841 EXPECT_FALSE(FPDFText_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1842 &matrix_d, &matrix_e, &matrix_f));
1843 EXPECT_TRUE(FPDFText_GetMatrix(text_object3, &matrix_a, &matrix_b, &matrix_c,
1844 &matrix_d, &matrix_e, &matrix_f));
1845 EXPECT_EQ(1., matrix_a);
1846 EXPECT_EQ(1.5, matrix_b);
1847 EXPECT_EQ(2., matrix_c);
1848 EXPECT_EQ(0.5, matrix_d);
1849 EXPECT_EQ(200., matrix_e);
1850 EXPECT_EQ(200., matrix_f);
1851
Miklos Vajna8625d3b2018-06-26 15:12:48 +00001852 EXPECT_EQ(0, FPDFTextObj_GetFontSize(nullptr));
1853 EXPECT_EQ(20, FPDFTextObj_GetFontSize(text_object3));
1854
Nicolas Pena49058402017-02-14 18:26:20 -05001855 // TODO(npm): Why are there issues with text rotated by 90 degrees?
1856 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
1857 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -05001858}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001859
Lei Zhangab41f252018-12-23 03:10:50 +00001860TEST_F(FPDFEditEmbedderTest, TestGetTextRenderMode) {
Miklos Vajna1448cc12018-07-03 13:52:33 +00001861 EXPECT_TRUE(OpenDocument("text_render_mode.pdf"));
1862 FPDF_PAGE page = LoadPage(0);
1863 ASSERT_TRUE(page);
1864 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1865
1866 ASSERT_EQ(-1, FPDFText_GetTextRenderMode(nullptr));
1867
1868 FPDF_PAGEOBJECT fill = FPDFPage_GetObject(page, 0);
1869 ASSERT_EQ(FPDF_TEXTRENDERMODE_FILL, FPDFText_GetTextRenderMode(fill));
1870
1871 FPDF_PAGEOBJECT stroke = FPDFPage_GetObject(page, 1);
1872 ASSERT_EQ(FPDF_TEXTRENDERMODE_STROKE, FPDFText_GetTextRenderMode(stroke));
1873
1874 UnloadPage(page);
1875}
1876
Lei Zhangab41f252018-12-23 03:10:50 +00001877TEST_F(FPDFEditEmbedderTest, TestGetTextFontName) {
Miklos Vajna53d4f0a2018-08-01 01:28:49 +00001878 EXPECT_TRUE(OpenDocument("text_font.pdf"));
1879 FPDF_PAGE page = LoadPage(0);
1880 ASSERT_TRUE(page);
1881 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1882
1883 // FPDFTextObj_GetFontName() positive testing.
1884 FPDF_PAGEOBJECT text = FPDFPage_GetObject(page, 0);
1885 unsigned long size = FPDFTextObj_GetFontName(text, nullptr, 0);
1886 const char kExpectedFontName[] = "Liberation Serif";
1887 ASSERT_EQ(sizeof(kExpectedFontName), size);
1888 std::vector<char> font_name(size);
1889 ASSERT_EQ(size, FPDFTextObj_GetFontName(text, font_name.data(), size));
1890 ASSERT_STREQ(kExpectedFontName, font_name.data());
1891
1892 // FPDFTextObj_GetFontName() negative testing.
1893 ASSERT_EQ(0U, FPDFTextObj_GetFontName(nullptr, nullptr, 0));
1894
1895 font_name.resize(2);
1896 font_name[0] = 'x';
1897 font_name[1] = '\0';
1898 size = FPDFTextObj_GetFontName(text, font_name.data(), font_name.size());
1899 ASSERT_EQ(sizeof(kExpectedFontName), size);
1900 ASSERT_EQ(std::string("x"), std::string(font_name.data()));
1901
1902 UnloadPage(page);
1903}
1904
Lei Zhangab41f252018-12-23 03:10:50 +00001905TEST_F(FPDFEditEmbedderTest, TestFormGetObjects) {
Miklos Vajnab66077d2018-07-11 13:25:02 +00001906 EXPECT_TRUE(OpenDocument("form_object.pdf"));
1907 FPDF_PAGE page = LoadPage(0);
1908 ASSERT_TRUE(page);
1909 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1910
1911 FPDF_PAGEOBJECT form = FPDFPage_GetObject(page, 0);
1912 EXPECT_EQ(FPDF_PAGEOBJ_FORM, FPDFPageObj_GetType(form));
1913 ASSERT_EQ(-1, FPDFFormObj_CountObjects(nullptr));
1914 ASSERT_EQ(2, FPDFFormObj_CountObjects(form));
1915
Miklos Vajna1d273f12018-07-16 19:20:36 +00001916 // FPDFFormObj_GetObject() positive testing.
1917 FPDF_PAGEOBJECT text1 = FPDFFormObj_GetObject(form, 0);
1918 ASSERT_TRUE(text1);
1919 float left = 0;
1920 float bottom = 0;
1921 float right = 0;
1922 float top = 0;
1923 ASSERT_TRUE(FPDFPageObj_GetBounds(text1, &left, &bottom, &right, &top));
1924 ASSERT_EQ(271, static_cast<int>(top));
1925
1926 FPDF_PAGEOBJECT text2 = FPDFFormObj_GetObject(form, 1);
1927 ASSERT_TRUE(text2);
1928 ASSERT_TRUE(FPDFPageObj_GetBounds(text2, &left, &bottom, &right, &top));
1929 ASSERT_EQ(221, static_cast<int>(top));
1930
1931 // FPDFFormObj_GetObject() negative testing.
1932 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(nullptr, 0));
1933 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(form, -1));
1934 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(form, 2));
1935
Miklos Vajna46b43732018-08-14 19:15:43 +00001936 // Reset the form object matrix to identity.
1937 auto* pPageObj = CPDFPageObjectFromFPDFPageObject(form);
1938 CPDF_FormObject* pFormObj = pPageObj->AsForm();
1939 pFormObj->Transform(pFormObj->form_matrix().GetInverse());
1940
1941 // FPDFFormObj_GetMatrix() positive testing.
1942 static constexpr float kFloats[6] = {1.0, 1.5, 2.0, 2.5, 100.0, 200.0};
1943 CFX_Matrix matrix(kFloats);
1944 pFormObj->Transform(matrix);
1945
1946 double matrix_a = 0;
1947 double matrix_b = 0;
1948 double matrix_c = 0;
1949 double matrix_d = 0;
1950 double matrix_e = 0;
1951 double matrix_f = 0;
1952 EXPECT_TRUE(FPDFFormObj_GetMatrix(form, &matrix_a, &matrix_b, &matrix_c,
1953 &matrix_d, &matrix_e, &matrix_f));
1954 EXPECT_DOUBLE_EQ(kFloats[0], matrix_a);
1955 EXPECT_DOUBLE_EQ(kFloats[1], matrix_b);
1956 EXPECT_DOUBLE_EQ(kFloats[2], matrix_c);
1957 EXPECT_DOUBLE_EQ(kFloats[3], matrix_d);
1958 EXPECT_DOUBLE_EQ(kFloats[4], matrix_e);
1959 EXPECT_DOUBLE_EQ(kFloats[5], matrix_f);
1960
1961 // FPDFFormObj_GetMatrix() negative testing.
1962 EXPECT_FALSE(FPDFFormObj_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1963 &matrix_d, &matrix_e, &matrix_f));
1964 EXPECT_FALSE(FPDFFormObj_GetMatrix(form, nullptr, nullptr, nullptr, nullptr,
1965 nullptr, nullptr));
1966 EXPECT_FALSE(FPDFFormObj_GetMatrix(nullptr, nullptr, nullptr, nullptr,
1967 nullptr, nullptr, nullptr));
1968
Miklos Vajnab66077d2018-07-11 13:25:02 +00001969 UnloadPage(page);
1970}
1971
Nicolas Pena4c48b102018-06-13 18:23:46 +00001972// Tests adding text from standard font using FPDFText_LoadStandardFont.
Lei Zhangab41f252018-12-23 03:10:50 +00001973TEST_F(FPDFEditEmbedderTest, AddStandardFontText2) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00001974 // Start with a blank page
1975 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
1976
1977 // Load a standard font.
1978 FPDF_FONT font = FPDFText_LoadStandardFont(document(), "Helvetica");
1979 ASSERT_TRUE(font);
1980
1981 // Add some text to the page.
1982 FPDF_PAGEOBJECT text_object =
1983 FPDFPageObj_CreateTextObj(document(), font, 12.0f);
1984 EXPECT_TRUE(text_object);
1985 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1986 GetFPDFWideString(L"I'm at the bottom of the page");
1987 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1988 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20);
1989 FPDFPage_InsertObject(page.get(), text_object);
1990 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page.get(), nullptr, 0);
1991#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1992 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001993#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1994 const char md5[] = "08d1ff3e5a42801bee6077fd366bef00";
Nicolas Pena4c48b102018-06-13 18:23:46 +00001995#else
1996 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
1997#endif
1998 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1999}
2000
Lei Zhangab41f252018-12-23 03:10:50 +00002001TEST_F(FPDFEditEmbedderTest, LoadStandardFonts) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002002 CreateNewDocument();
Lei Zhangd72fd582018-07-27 19:37:27 +00002003 static constexpr const char* standard_font_names[] = {
2004 "Arial",
2005 "Arial-Bold",
2006 "Arial-BoldItalic",
2007 "Arial-Italic",
2008 "Courier",
2009 "Courier-BoldOblique",
2010 "Courier-Oblique",
2011 "Courier-Bold",
2012 "CourierNew",
2013 "CourierNew-Bold",
2014 "CourierNew-BoldItalic",
2015 "CourierNew-Italic",
2016 "Helvetica",
2017 "Helvetica-Bold",
2018 "Helvetica-BoldOblique",
2019 "Helvetica-Oblique",
2020 "Symbol",
2021 "TimesNewRoman",
2022 "TimesNewRoman-Bold",
2023 "TimesNewRoman-BoldItalic",
2024 "TimesNewRoman-Italic",
2025 "ZapfDingbats"};
2026 for (const char* font_name : standard_font_names) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002027 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
2028 EXPECT_TRUE(font) << font_name << " should be considered a standard font.";
2029 }
Lei Zhangd72fd582018-07-27 19:37:27 +00002030 static constexpr const char* not_standard_font_names[] = {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002031 "Abcdefg", "ArialB", "Arial-Style",
2032 "Font Name", "FontArial", "NotAStandardFontName",
2033 "TestFontName", "Quack", "Symbol-Italic",
2034 "Zapf"};
Lei Zhangd72fd582018-07-27 19:37:27 +00002035 for (const char* font_name : not_standard_font_names) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002036 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
2037 EXPECT_FALSE(font) << font_name
2038 << " should not be considered a standard font.";
2039 }
2040}
2041
Lei Zhangab41f252018-12-23 03:10:50 +00002042TEST_F(FPDFEditEmbedderTest, GraphicsData) {
Nicolas Pena603a31d2017-06-14 11:41:18 -04002043 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +00002044 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -04002045
2046 // Create a rect with nontrivial graphics
2047 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
2048 FPDFPageObj_SetBlendMode(rect1, "Color");
2049 FPDFPage_InsertObject(page.get(), rect1);
2050 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
2051
2052 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +00002053 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
2054 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -04002055 ASSERT_TRUE(graphics_dict);
Lei Zhangf40380f2018-10-12 18:31:51 +00002056 EXPECT_EQ(2u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002057
2058 // Add a text object causing no change to the graphics dictionary
2059 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
2060 // Only alpha, the last component, matters for the graphics dictionary. And
2061 // the default value is 255.
2062 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
2063 FPDFPage_InsertObject(page.get(), text1);
2064 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002065 EXPECT_EQ(2u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002066
2067 // Add a text object increasing the size of the graphics dictionary
2068 FPDF_PAGEOBJECT text2 =
2069 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
2070 FPDFPage_InsertObject(page.get(), text2);
2071 FPDFPageObj_SetBlendMode(text2, "Darken");
2072 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
2073 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002074 EXPECT_EQ(3u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002075
2076 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -04002077 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -04002078 FPDFPageObj_SetBlendMode(path, "Darken");
2079 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
2080 FPDFPage_InsertObject(page.get(), path);
2081 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002082 EXPECT_EQ(3u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002083
2084 // Add a rect increasing the size of the graphics dictionary
2085 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
2086 FPDFPageObj_SetBlendMode(rect2, "Darken");
2087 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
2088 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
2089 FPDFPage_InsertObject(page.get(), rect2);
2090 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002091 EXPECT_EQ(4u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002092}
2093
Lei Zhangab41f252018-12-23 03:10:50 +00002094TEST_F(FPDFEditEmbedderTest, DoubleGenerating) {
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002095 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05002096 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002097
2098 // Add a red rectangle with some non-default alpha
2099 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
2100 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
2101 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
2102 FPDFPage_InsertObject(page, rect);
2103 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2104
2105 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +00002106 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
2107 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002108 ASSERT_TRUE(graphics_dict);
Lei Zhangf40380f2018-10-12 18:31:51 +00002109 EXPECT_EQ(2u, graphics_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002110
2111 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +00002112 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002113 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002114 CompareBitmap(page_bitmap.get(), 612, 792,
2115 "5384da3406d62360ffb5cac4476fff1c");
2116 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002117
2118 // Never mind, my new favorite color is blue, increase alpha
2119 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
2120 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhangf40380f2018-10-12 18:31:51 +00002121 EXPECT_EQ(3u, graphics_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002122
2123 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +00002124 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002125 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002126 CompareBitmap(page_bitmap.get(), 612, 792,
2127 "2e51656f5073b0bee611d9cd086aa09c");
2128 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002129
2130 // And now generate, without changes
2131 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhangf40380f2018-10-12 18:31:51 +00002132 EXPECT_EQ(3u, graphics_dict->size());
Lei Zhang107fa7b2018-02-09 21:48:15 +00002133 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002134 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002135 CompareBitmap(page_bitmap.get(), 612, 792,
2136 "2e51656f5073b0bee611d9cd086aa09c");
2137 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002138
2139 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04002140 FPDF_PAGEOBJECT text_object =
2141 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
2142 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2143 GetFPDFWideString(L"Something something #text# something");
2144 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2145 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
2146 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002147 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00002148 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002149 ASSERT_TRUE(font_dict);
Lei Zhangf40380f2018-10-12 18:31:51 +00002150 EXPECT_EQ(1u, font_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002151
2152 // Generate yet again, check dicts are reasonably sized
2153 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhangf40380f2018-10-12 18:31:51 +00002154 EXPECT_EQ(3u, graphics_dict->size());
2155 EXPECT_EQ(1u, font_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002156 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002157}
Nicolas Penabe90aae2017-02-27 10:41:41 -05002158
Lei Zhangab41f252018-12-23 03:10:50 +00002159TEST_F(FPDFEditEmbedderTest, LoadSimpleType1Font) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002160 CreateNewDocument();
2161 // TODO(npm): use other fonts after disallowing loading any font as any type
2162 const CPDF_Font* stock_font =
2163 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Tom Sepez20c41a52018-08-29 23:53:53 +00002164 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2165 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2166 FPDF_FONT_TYPE1, false));
Nicolas Penab3161852017-05-02 14:12:50 -04002167 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002168 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002169 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002170
Lei Zhang710fa992018-05-25 16:24:48 +00002171 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05002172 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2173 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
2174 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
2175 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
2176 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
2177 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05002178 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
2179
Lei Zhangde579ab2018-05-25 21:49:49 +00002180 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05002181 ASSERT_TRUE(widths_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002182 ASSERT_EQ(224u, widths_array->size());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002183 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05002184 EXPECT_EQ(569, widths_array->GetNumberAt(11));
2185 EXPECT_EQ(500, widths_array->GetNumberAt(223));
Tom Sepez20c41a52018-08-29 23:53:53 +00002186 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002187}
Nicolas Penabe90aae2017-02-27 10:41:41 -05002188
Lei Zhangab41f252018-12-23 03:10:50 +00002189TEST_F(FPDFEditEmbedderTest, LoadSimpleTrueTypeFont) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002190 CreateNewDocument();
2191 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Tom Sepez20c41a52018-08-29 23:53:53 +00002192 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2193 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2194 FPDF_FONT_TRUETYPE, false));
Nicolas Penab3161852017-05-02 14:12:50 -04002195 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002196 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002197 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002198
Lei Zhang710fa992018-05-25 16:24:48 +00002199 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002200 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2201 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
2202 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
2203 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
2204 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
2205 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
2206 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05002207
Lei Zhangde579ab2018-05-25 21:49:49 +00002208 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05002209 ASSERT_TRUE(widths_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002210 ASSERT_EQ(224u, widths_array->size());
Nicolas Penad03ca422017-03-06 13:54:33 -05002211 EXPECT_EQ(600, widths_array->GetNumberAt(33));
2212 EXPECT_EQ(600, widths_array->GetNumberAt(74));
2213 EXPECT_EQ(600, widths_array->GetNumberAt(223));
Tom Sepez20c41a52018-08-29 23:53:53 +00002214 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002215}
2216
Lei Zhangab41f252018-12-23 03:10:50 +00002217TEST_F(FPDFEditEmbedderTest, LoadCIDType0Font) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002218 CreateNewDocument();
2219 const CPDF_Font* stock_font =
2220 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Tom Sepez20c41a52018-08-29 23:53:53 +00002221 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2222 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2223 FPDF_FONT_TYPE1, 1));
Nicolas Penab3161852017-05-02 14:12:50 -04002224 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002225 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002226 EXPECT_TRUE(typed_font->IsCIDFont());
2227
2228 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00002229 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002230 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2231 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
2232 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
2233 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00002234 const CPDF_Array* descendant_array =
2235 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05002236 ASSERT_TRUE(descendant_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002237 EXPECT_EQ(1u, descendant_array->size());
Nicolas Penad03ca422017-03-06 13:54:33 -05002238
2239 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00002240 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05002241 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
2242 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
2243 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00002244 const CPDF_Dictionary* cidinfo_dict =
2245 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05002246 ASSERT_TRUE(cidinfo_dict);
Lei Zhang9c950b12019-01-16 19:06:37 +00002247 const CPDF_Object* registry = cidinfo_dict->GetObjectFor("Registry");
2248 ASSERT_TRUE(registry);
KDr28da0e1b2019-01-17 03:44:29 +00002249 EXPECT_EQ(CPDF_Object::kString, registry->GetType());
Lei Zhang9c950b12019-01-16 19:06:37 +00002250 EXPECT_EQ("Adobe", registry->GetString());
2251 const CPDF_Object* ordering = cidinfo_dict->GetObjectFor("Ordering");
2252 ASSERT_TRUE(ordering);
KDr28da0e1b2019-01-17 03:44:29 +00002253 EXPECT_EQ(CPDF_Object::kString, ordering->GetType());
Lei Zhang9c950b12019-01-16 19:06:37 +00002254 EXPECT_EQ("Identity", ordering->GetString());
Nicolas Penad03ca422017-03-06 13:54:33 -05002255 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
Tom Sepez20c41a52018-08-29 23:53:53 +00002256 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002257
2258 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00002259 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05002260 ASSERT_TRUE(widths_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002261 EXPECT_GT(widths_array->size(), 1u);
Nicolas Penad03ca422017-03-06 13:54:33 -05002262 CheckCompositeFontWidths(widths_array, typed_font);
2263}
2264
Lei Zhangab41f252018-12-23 03:10:50 +00002265TEST_F(FPDFEditEmbedderTest, LoadCIDType2Font) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002266 CreateNewDocument();
2267 const CPDF_Font* stock_font =
2268 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Tom Sepez20c41a52018-08-29 23:53:53 +00002269 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2270 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2271 FPDF_FONT_TRUETYPE, 1));
Nicolas Penab3161852017-05-02 14:12:50 -04002272 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002273 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002274 EXPECT_TRUE(typed_font->IsCIDFont());
2275
2276 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00002277 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002278 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2279 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
2280 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
2281 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00002282 const CPDF_Array* descendant_array =
2283 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05002284 ASSERT_TRUE(descendant_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002285 EXPECT_EQ(1u, descendant_array->size());
Nicolas Penad03ca422017-03-06 13:54:33 -05002286
2287 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00002288 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05002289 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
2290 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
2291 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00002292 const CPDF_Dictionary* cidinfo_dict =
2293 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05002294 ASSERT_TRUE(cidinfo_dict);
2295 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
2296 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
2297 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
Tom Sepez20c41a52018-08-29 23:53:53 +00002298 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002299
2300 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00002301 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05002302 ASSERT_TRUE(widths_array);
2303 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05002304}
rbpotterce8e51e2017-04-28 12:42:47 -07002305
Lei Zhangab41f252018-12-23 03:10:50 +00002306TEST_F(FPDFEditEmbedderTest, NormalizeNegativeRotation) {
rbpotterce8e51e2017-04-28 12:42:47 -07002307 // Load document with a -90 degree rotation
2308 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
2309 FPDF_PAGE page = LoadPage(0);
2310 EXPECT_NE(nullptr, page);
2311
2312 EXPECT_EQ(3, FPDFPage_GetRotation(page));
2313 UnloadPage(page);
2314}
Nicolas Penab3161852017-05-02 14:12:50 -04002315
Lei Zhangab41f252018-12-23 03:10:50 +00002316TEST_F(FPDFEditEmbedderTest, AddTrueTypeFontText) {
Nicolas Penab3161852017-05-02 14:12:50 -04002317 // Start with a blank page
2318 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2319 {
2320 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Tom Sepez20c41a52018-08-29 23:53:53 +00002321 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2322 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2323 FPDF_FONT_TRUETYPE, 0));
Nicolas Penab3161852017-05-02 14:12:50 -04002324 ASSERT_TRUE(font.get());
2325
2326 // Add some text to the page
2327 FPDF_PAGEOBJECT text_object =
2328 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2329 EXPECT_TRUE(text_object);
2330 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2331 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2332 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2333 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2334 FPDFPage_InsertObject(page, text_object);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002335 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04002336#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002337 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002338#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2339 const char md5[] = "d60ba39f9698e32360d99e727dd93165";
Nicolas Penab3161852017-05-02 14:12:50 -04002340#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002341 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04002342#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002343 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04002344
2345 // Add some more text, same font
2346 FPDF_PAGEOBJECT text_object2 =
2347 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
2348 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2349 GetFPDFWideString(L"Bigger font size");
2350 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2351 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
2352 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04002353 }
Tom Sepeze08d2b12018-04-25 18:49:32 +00002354 ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04002355#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002356 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002357#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2358 const char md5_2[] = "2199b579c49ab5f80c246a586a80ee90";
Nicolas Penab3161852017-05-02 14:12:50 -04002359#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002360 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04002361#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002362 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002363
Nicolas Pena207b7272017-05-26 17:37:06 -04002364 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04002365 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2366 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002367
2368 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002369}
Nicolas Penaf45ade32017-05-03 10:23:49 -04002370
Lei Zhangab41f252018-12-23 03:10:50 +00002371TEST_F(FPDFEditEmbedderTest, TransformAnnot) {
Jane Liueda65252017-06-07 11:31:27 -04002372 // Open a file with one annotation and load its first page.
2373 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00002374 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04002375 ASSERT_TRUE(page);
2376
Lei Zhanga21d5932018-02-05 18:28:38 +00002377 {
2378 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00002379 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00002380 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
2381 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04002382
Lei Zhanga21d5932018-02-05 18:28:38 +00002383 // FPDFPage_TransformAnnots() should run without errors when modifying
2384 // annotation rectangles.
2385 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
2386 }
Jane Liueda65252017-06-07 11:31:27 -04002387 UnloadPage(page);
2388}
2389
Nicolas Penaf45ade32017-05-03 10:23:49 -04002390// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04002391#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Lei Zhangab41f252018-12-23 03:10:50 +00002392TEST_F(FPDFEditEmbedderTest, AddCIDFontText) {
Nicolas Penaf45ade32017-05-03 10:23:49 -04002393 // Start with a blank page
2394 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2395 CFX_Font CIDfont;
2396 {
2397 // First, get the data from the font
2398 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
2399 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
Tom Sepez20c41a52018-08-29 23:53:53 +00002400 pdfium::span<const uint8_t> span = CIDfont.GetFontSpan();
Nicolas Penaf45ade32017-05-03 10:23:49 -04002401
2402 // Load the data into a FPDF_Font.
Tom Sepez20c41a52018-08-29 23:53:53 +00002403 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2404 FPDF_FONT_TRUETYPE, 1));
Nicolas Penaf45ade32017-05-03 10:23:49 -04002405 ASSERT_TRUE(font.get());
2406
2407 // Add some text to the page
2408 FPDF_PAGEOBJECT text_object =
2409 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2410 ASSERT_TRUE(text_object);
2411 std::wstring wstr = L"ABCDEFGhijklmnop.";
2412 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2413 GetFPDFWideString(wstr);
2414 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2415 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
2416 FPDFPage_InsertObject(page, text_object);
2417
2418 // And add some Japanese characters
2419 FPDF_PAGEOBJECT text_object2 =
2420 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
2421 ASSERT_TRUE(text_object2);
2422 std::wstring wstr2 =
2423 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
2424 L"\u756A";
2425 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2426 GetFPDFWideString(wstr2);
2427 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2428 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
2429 FPDFPage_InsertObject(page, text_object2);
2430 }
2431
Nicolas Pena207b7272017-05-26 17:37:06 -04002432 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00002433 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00002434 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002435 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002436 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2437 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04002438
2439 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04002440 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04002441 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2442 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002443
2444 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04002445}
Dan Sinclair698aed72017-09-26 16:24:49 -04002446#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002447
Lei Zhangab41f252018-12-23 03:10:50 +00002448TEST_F(FPDFEditEmbedderTest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04002449 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002450 {
2451 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
2452 FPDF_PAGE page = LoadPage(0);
2453 ASSERT_NE(nullptr, page);
2454
2455 // Now add a more complex blue path.
2456 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
2457 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
2458 // TODO(npm): stroking will cause the MD5s to differ.
2459 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
2460 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
2461 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
2462 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
2463 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
2464 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
2465 EXPECT_TRUE(FPDFPath_Close(green_path));
2466 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002467 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002468 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002469
2470 // Now save the result, closing the page and document
2471 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2472 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2473 UnloadPage(page);
2474 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002475
2476 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002477}
Jane Liu28fb7ba2017-08-02 21:45:57 -04002478
Lei Zhangab41f252018-12-23 03:10:50 +00002479TEST_F(FPDFEditEmbedderTest, AddMark) {
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002480 // Load document with some text.
2481 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2482 FPDF_PAGE page = LoadPage(0);
2483 ASSERT_TRUE(page);
2484
2485 constexpr int kExpectedObjectCount = 19;
2486 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2487
2488 // Add to the first page object a "Bounds" mark with "Position": "First".
2489 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
2490 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(page_object, "Bounds");
2491 EXPECT_TRUE(mark);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002492 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2493 "Position", "First"));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002494
2495 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2496
2497 // Save the file
2498 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2499 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2500 UnloadPage(page);
2501
2502 // Re-open the file and check the new mark is present.
Tom Sepezb9c3e272018-08-14 18:22:06 +00002503 OpenSavedDocument(nullptr);
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002504 FPDF_PAGE saved_page = LoadSavedPage(0);
2505
2506 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2507
2508 CloseSavedPage(saved_page);
2509 CloseSavedDocument();
2510}
2511
Lei Zhangab41f252018-12-23 03:10:50 +00002512TEST_F(FPDFEditEmbedderTest, SetMarkParam) {
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002513 // Load document with some text.
2514 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2515 FPDF_PAGE page = LoadPage(0);
2516 ASSERT_TRUE(page);
2517
2518 constexpr int kExpectedObjectCount = 19;
2519 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2520
2521 // Check the "Bounds" mark's "Position" param is "Last".
2522 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2523 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2524 ASSERT_TRUE(mark);
2525 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002526 unsigned long name_len = 999u;
2527 ASSERT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
2528 EXPECT_EQ((6u + 1u) * 2u, name_len);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002529 ASSERT_EQ(L"Bounds",
2530 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2531 unsigned long out_buffer_len;
2532 ASSERT_TRUE(FPDFPageObjMark_GetParamStringValue(
2533 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2534 ASSERT_EQ(L"Last",
2535 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2536
2537 // Set is to "End".
2538 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2539 "Position", "End"));
2540
2541 // Verify the object passed must correspond to the mark passed.
2542 FPDF_PAGEOBJECT another_page_object = FPDFPage_GetObject(page, 17);
2543 EXPECT_FALSE(FPDFPageObjMark_SetStringParam(document(), another_page_object,
2544 mark, "Position", "End"));
2545
2546 // Verify nothing else changed.
2547 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2548
2549 // Verify "Position" now maps to "End".
2550 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2551 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2552 EXPECT_EQ(L"End",
2553 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2554
2555 // Save the file
2556 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2557 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2558 UnloadPage(page);
2559
2560 // Re-open the file and cerify "Position" still maps to "End".
Tom Sepezb9c3e272018-08-14 18:22:06 +00002561 OpenSavedDocument(nullptr);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002562 FPDF_PAGE saved_page = LoadSavedPage(0);
2563
2564 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2565 page_object = FPDFPage_GetObject(saved_page, 18);
2566 mark = FPDFPageObj_GetMark(page_object, 1);
2567 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2568 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2569 EXPECT_EQ(L"End",
2570 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2571
2572 CloseSavedPage(saved_page);
2573 CloseSavedDocument();
2574}
2575
Lei Zhangab41f252018-12-23 03:10:50 +00002576TEST_F(FPDFEditEmbedderTest, AddMarkedText) {
Henrique Nakashima144107d2018-07-10 21:04:05 +00002577 // Start with a blank page.
2578 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2579
2580 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Tom Sepez20c41a52018-08-29 23:53:53 +00002581 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2582 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2583 FPDF_FONT_TRUETYPE, 0));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002584 ASSERT_TRUE(font.get());
2585
2586 // Add some text to the page.
2587 FPDF_PAGEOBJECT text_object =
2588 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2589
2590 EXPECT_TRUE(text_object);
2591 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
2592 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2593 EXPECT_TRUE(FPDFText_SetText(text_object, text1.get()));
2594 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2595 FPDFPage_InsertObject(page, text_object);
2596
2597 // Add a mark with the tag "TestMarkName" to that text.
2598 EXPECT_EQ(0, FPDFPageObj_CountMarks(text_object));
Henrique Nakashima6fc8d872018-09-18 16:48:31 +00002599 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(text_object, "Test Mark Name");
Henrique Nakashima144107d2018-07-10 21:04:05 +00002600 EXPECT_TRUE(mark);
2601 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2602 EXPECT_EQ(mark, FPDFPageObj_GetMark(text_object, 0));
2603 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002604 unsigned long name_len = 999u;
2605 ASSERT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
2606 EXPECT_EQ((14u + 1u) * 2, name_len);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002607 std::wstring name =
2608 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashima6fc8d872018-09-18 16:48:31 +00002609 EXPECT_EQ(L"Test Mark Name", name);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002610
2611 // Add parameters:
2612 // - int "IntKey" : 42
2613 // - string "StringKey": "StringValue"
Henrique Nakashima07520f62018-07-12 19:45:29 +00002614 // - blob "BlobKey": "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0"
2615 constexpr const size_t kBlobLen = 28;
Lei Zhangd3b028b2018-11-30 22:22:00 +00002616 char block_value[kBlobLen];
2617 memcpy(block_value, "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0", kBlobLen);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002618 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002619 EXPECT_TRUE(
2620 FPDFPageObjMark_SetIntParam(document(), text_object, mark, "IntKey", 42));
2621 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), text_object, mark,
2622 "StringKey", "StringValue"));
2623 EXPECT_TRUE(FPDFPageObjMark_SetBlobParam(document(), text_object, mark,
Lei Zhangd3b028b2018-11-30 22:22:00 +00002624 "BlobKey", block_value, kBlobLen));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002625 EXPECT_EQ(3, FPDFPageObjMark_CountParams(mark));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002626
2627 // Check the two parameters can be retrieved.
2628 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002629 FPDFPageObjMark_GetParamValueType(mark, "IntKey"));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002630 int int_value;
Henrique Nakashima94230e52018-07-11 22:02:02 +00002631 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "IntKey", &int_value));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002632 EXPECT_EQ(42, int_value);
2633
2634 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002635 FPDFPageObjMark_GetParamValueType(mark, "StringKey"));
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002636 unsigned long out_buffer_len = 999u;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002637 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2638 mark, "StringKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002639 EXPECT_GT(out_buffer_len, 0u);
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002640 EXPECT_NE(999u, out_buffer_len);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002641 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2642 EXPECT_EQ(L"StringValue", name);
2643
Henrique Nakashima07520f62018-07-12 19:45:29 +00002644 EXPECT_EQ(FPDF_OBJECT_STRING,
2645 FPDFPageObjMark_GetParamValueType(mark, "BlobKey"));
2646 out_buffer_len = 0;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002647 EXPECT_TRUE(FPDFPageObjMark_GetParamBlobValue(
2648 mark, "BlobKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002649 EXPECT_EQ(kBlobLen, out_buffer_len);
Lei Zhangd3b028b2018-11-30 22:22:00 +00002650 EXPECT_EQ(0, memcmp(block_value, buffer, kBlobLen));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002651
Henrique Nakashima144107d2018-07-10 21:04:05 +00002652// Render and check the bitmap is the expected one.
2653#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
2654 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002655#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2656 const char md5[] = "d60ba39f9698e32360d99e727dd93165";
Henrique Nakashima144107d2018-07-10 21:04:05 +00002657#else
2658 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
2659#endif
2660 {
2661 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
2662 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2663 }
2664
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002665 // Now save the result.
2666 EXPECT_EQ(1, FPDFPage_CountObjects(page));
2667 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2668 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2669
Henrique Nakashima144107d2018-07-10 21:04:05 +00002670 FPDF_ClosePage(page);
2671
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002672 // Re-open the file and check the changes were kept in the saved .pdf.
Tom Sepezb9c3e272018-08-14 18:22:06 +00002673 OpenSavedDocument(nullptr);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002674 FPDF_PAGE saved_page = LoadSavedPage(0);
2675 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
2676
2677 text_object = FPDFPage_GetObject(saved_page, 0);
2678 EXPECT_TRUE(text_object);
2679 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2680 mark = FPDFPageObj_GetMark(text_object, 0);
2681 EXPECT_TRUE(mark);
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002682
2683 name_len = 999u;
2684 ASSERT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
2685 EXPECT_EQ((14u + 1u) * 2, name_len);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002686 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashima6fc8d872018-09-18 16:48:31 +00002687 EXPECT_EQ(L"Test Mark Name", name);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002688
2689 CloseSavedPage(saved_page);
2690 CloseSavedDocument();
Henrique Nakashima144107d2018-07-10 21:04:05 +00002691}
2692
Lei Zhangab41f252018-12-23 03:10:50 +00002693TEST_F(FPDFEditEmbedderTest, MarkGetName) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002694 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2695 FPDF_PAGE page = LoadPage(0);
2696 ASSERT_TRUE(page);
2697 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2698 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2699 ASSERT_TRUE(mark);
2700
2701 char buffer[256];
2702 unsigned long out_len;
2703
2704 // Show the positive cases of FPDFPageObjMark_GetName.
2705 out_len = 999u;
2706 EXPECT_TRUE(FPDFPageObjMark_GetName(mark, nullptr, 0, &out_len));
2707 EXPECT_EQ((6u + 1u) * 2u, out_len);
2708
2709 out_len = 999u;
2710 EXPECT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &out_len));
2711 EXPECT_EQ(L"Bounds",
2712 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2713 EXPECT_EQ((6u + 1u) * 2u, out_len);
2714
2715 // Show the negative cases of FPDFPageObjMark_GetName.
2716 out_len = 999u;
2717 EXPECT_FALSE(
2718 FPDFPageObjMark_GetName(nullptr, buffer, sizeof(buffer), &out_len));
2719 EXPECT_EQ(999u, out_len);
2720
2721 EXPECT_FALSE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), nullptr));
2722
2723 UnloadPage(page);
2724}
2725
Lei Zhangab41f252018-12-23 03:10:50 +00002726TEST_F(FPDFEditEmbedderTest, MarkGetParamKey) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002727 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2728 FPDF_PAGE page = LoadPage(0);
2729 ASSERT_TRUE(page);
2730 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2731 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2732 ASSERT_TRUE(mark);
2733
2734 char buffer[256];
2735 unsigned long out_len;
2736
2737 // Show the positive cases of FPDFPageObjMark_GetParamKey.
2738 out_len = 999u;
2739 EXPECT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, nullptr, 0, &out_len));
2740 EXPECT_EQ((8u + 1u) * 2u, out_len);
2741
2742 out_len = 999u;
2743 EXPECT_TRUE(
2744 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer), &out_len));
2745 EXPECT_EQ(L"Position",
2746 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2747 EXPECT_EQ((8u + 1u) * 2u, out_len);
2748
2749 // Show the negative cases of FPDFPageObjMark_GetParamKey.
2750 out_len = 999u;
2751 EXPECT_FALSE(FPDFPageObjMark_GetParamKey(nullptr, 0, buffer, sizeof(buffer),
2752 &out_len));
2753 EXPECT_EQ(999u, out_len);
2754
2755 out_len = 999u;
2756 EXPECT_FALSE(
2757 FPDFPageObjMark_GetParamKey(mark, 1, buffer, sizeof(buffer), &out_len));
2758 EXPECT_EQ(999u, out_len);
2759
2760 EXPECT_FALSE(
2761 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer), nullptr));
2762
2763 UnloadPage(page);
2764}
2765
Lei Zhangab41f252018-12-23 03:10:50 +00002766TEST_F(FPDFEditEmbedderTest, MarkGetIntParam) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002767 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2768 FPDF_PAGE page = LoadPage(0);
2769 ASSERT_TRUE(page);
2770 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 8);
2771 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 0);
2772 ASSERT_TRUE(mark);
2773
2774 int out_value;
2775
2776 // Show the positive cases of FPDFPageObjMark_GetParamIntValue.
2777 out_value = 999;
2778 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
2779 EXPECT_EQ(3, out_value);
2780
2781 // Show the negative cases of FPDFPageObjMark_GetParamIntValue.
2782 out_value = 999;
2783 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(nullptr, "Factor", &out_value));
2784 EXPECT_EQ(999, out_value);
2785
2786 out_value = 999;
2787 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(mark, "ParamThatDoesNotExist",
2788 &out_value));
2789 EXPECT_EQ(999, out_value);
2790
2791 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(mark, "Factor", nullptr));
2792
2793 page_object = FPDFPage_GetObject(page, 18);
2794 mark = FPDFPageObj_GetMark(page_object, 1);
2795 out_value = 999;
2796 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(mark, "Position", &out_value));
2797 EXPECT_EQ(999, out_value);
2798
2799 UnloadPage(page);
2800}
2801
Lei Zhangab41f252018-12-23 03:10:50 +00002802TEST_F(FPDFEditEmbedderTest, MarkGetStringParam) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002803 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2804 FPDF_PAGE page = LoadPage(0);
2805 ASSERT_TRUE(page);
2806 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2807 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2808 ASSERT_TRUE(mark);
2809
2810 char buffer[256];
2811 unsigned long out_len;
2812
2813 // Show the positive cases of FPDFPageObjMark_GetParamStringValue.
2814 out_len = 999u;
2815 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "Position", nullptr, 0,
2816 &out_len));
2817 EXPECT_EQ((4u + 1u) * 2u, out_len);
2818
2819 out_len = 999u;
2820 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "Position", buffer,
2821 sizeof(buffer), &out_len));
2822 EXPECT_EQ(L"Last",
2823 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2824 EXPECT_EQ((4u + 1u) * 2u, out_len);
2825
2826 // Show the negative cases of FPDFPageObjMark_GetParamStringValue.
2827 out_len = 999u;
2828 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(nullptr, "Position", buffer,
2829 sizeof(buffer), &out_len));
2830 EXPECT_EQ(999u, out_len);
2831
2832 out_len = 999u;
2833 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(
2834 mark, "ParamThatDoesNotExist", buffer, sizeof(buffer), &out_len));
2835 EXPECT_EQ(999u, out_len);
2836
2837 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(mark, "Position", buffer,
2838 sizeof(buffer), nullptr));
2839
2840 page_object = FPDFPage_GetObject(page, 8);
2841 mark = FPDFPageObj_GetMark(page_object, 0);
2842 out_len = 999u;
2843 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(mark, "Factor", buffer,
2844 sizeof(buffer), &out_len));
2845 EXPECT_EQ(999u, out_len);
2846
2847 UnloadPage(page);
2848}
2849
Lei Zhangab41f252018-12-23 03:10:50 +00002850TEST_F(FPDFEditEmbedderTest, ExtractImageBitmap) {
Jane Liu28fb7ba2017-08-02 21:45:57 -04002851 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2852 FPDF_PAGE page = LoadPage(0);
2853 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002854 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04002855
2856 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2857 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2858 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
2859
2860 obj = FPDFPage_GetObject(page, 33);
2861 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2862 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
2863 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2864 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
2865 FPDFBitmap_Destroy(bitmap);
2866
2867 obj = FPDFPage_GetObject(page, 34);
2868 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2869 bitmap = FPDFImageObj_GetBitmap(obj);
2870 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2871 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
2872 FPDFBitmap_Destroy(bitmap);
2873
2874 obj = FPDFPage_GetObject(page, 35);
2875 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2876 bitmap = FPDFImageObj_GetBitmap(obj);
2877 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
2878 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
2879 FPDFBitmap_Destroy(bitmap);
2880
2881 obj = FPDFPage_GetObject(page, 36);
2882 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2883 bitmap = FPDFImageObj_GetBitmap(obj);
2884 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2885 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
2886 FPDFBitmap_Destroy(bitmap);
2887
2888 obj = FPDFPage_GetObject(page, 37);
2889 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2890 bitmap = FPDFImageObj_GetBitmap(obj);
2891 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2892 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
2893 FPDFBitmap_Destroy(bitmap);
2894
2895 obj = FPDFPage_GetObject(page, 38);
2896 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2897 bitmap = FPDFImageObj_GetBitmap(obj);
2898 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2899 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
2900 FPDFBitmap_Destroy(bitmap);
2901 UnloadPage(page);
2902}
Jane Liu548334e2017-08-03 16:33:40 -04002903
Lei Zhangab41f252018-12-23 03:10:50 +00002904TEST_F(FPDFEditEmbedderTest, ExtractJBigImageBitmap) {
Lei Zhang53341dd2018-03-01 15:42:47 +00002905 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
2906 FPDF_PAGE page = LoadPage(0);
2907 ASSERT_TRUE(page);
2908 ASSERT_EQ(1, FPDFPage_CountObjects(page));
2909
2910 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
2911 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2912 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002913 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00002914 ASSERT_TRUE(bitmap);
2915 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
2916 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00002917 }
2918
2919 UnloadPage(page);
2920}
2921
Lei Zhangab41f252018-12-23 03:10:50 +00002922TEST_F(FPDFEditEmbedderTest, GetImageData) {
Jane Liu548334e2017-08-03 16:33:40 -04002923 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2924 FPDF_PAGE page = LoadPage(0);
2925 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002926 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04002927
2928 // Retrieve an image object with flate-encoded data stream.
2929 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
2930 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2931
2932 // Check that the raw image data has the correct length and hash value.
2933 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2934 std::vector<char> buf(len);
2935 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2936 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
2937 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2938
2939 // Check that the decoded image data has the correct length and hash value.
2940 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2941 buf.clear();
2942 buf.resize(len);
2943 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2944 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
2945 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2946
Lei Zhang351e8b02018-12-20 01:10:06 +00002947 // Retrieve an image object with DCTDecode-encoded data stream.
Jane Liu548334e2017-08-03 16:33:40 -04002948 obj = FPDFPage_GetObject(page, 37);
2949 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2950
2951 // Check that the raw image data has the correct length and hash value.
2952 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2953 buf.clear();
2954 buf.resize(len);
2955 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2956 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2957 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2958
2959 // Check that the decoded image data has the correct length and hash value,
2960 // which should be the same as those of the raw data, since this image is
2961 // encoded by a single DCTDecode filter and decoding is a noop.
2962 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2963 buf.clear();
2964 buf.resize(len);
2965 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2966 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2967 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2968
2969 UnloadPage(page);
2970}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04002971
Lei Zhangab41f252018-12-23 03:10:50 +00002972TEST_F(FPDFEditEmbedderTest, GetImageMatrix) {
Lei Zhang866d6882018-10-24 17:31:01 +00002973 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2974 FPDF_PAGE page = LoadPage(0);
2975 ASSERT_TRUE(page);
2976 ASSERT_EQ(39, FPDFPage_CountObjects(page));
2977
2978 FPDF_PAGEOBJECT obj;
2979 double a;
2980 double b;
2981 double c;
2982 double d;
2983 double e;
2984 double f;
2985
2986 obj = FPDFPage_GetObject(page, 33);
2987 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2988 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
2989 EXPECT_DOUBLE_EQ(53.0, a);
2990 EXPECT_DOUBLE_EQ(0.0, b);
2991 EXPECT_DOUBLE_EQ(0.0, c);
2992 EXPECT_DOUBLE_EQ(43.0, d);
2993 EXPECT_DOUBLE_EQ(72.0, e);
2994 EXPECT_DOUBLE_EQ(646.510009765625, f);
2995
2996 obj = FPDFPage_GetObject(page, 34);
2997 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2998 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
2999 EXPECT_DOUBLE_EQ(70.0, a);
3000 EXPECT_DOUBLE_EQ(0.0, b);
3001 EXPECT_DOUBLE_EQ(0.0, c);
3002 EXPECT_DOUBLE_EQ(51.0, d);
3003 EXPECT_DOUBLE_EQ(216.0, e);
3004 EXPECT_DOUBLE_EQ(646.510009765625, f);
3005
3006 obj = FPDFPage_GetObject(page, 35);
3007 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3008 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3009 EXPECT_DOUBLE_EQ(69.0, a);
3010 EXPECT_DOUBLE_EQ(0.0, b);
3011 EXPECT_DOUBLE_EQ(0.0, c);
3012 EXPECT_DOUBLE_EQ(51.0, d);
3013 EXPECT_DOUBLE_EQ(360.0, e);
3014 EXPECT_DOUBLE_EQ(646.510009765625, f);
3015
3016 obj = FPDFPage_GetObject(page, 36);
3017 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3018 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3019 EXPECT_DOUBLE_EQ(59.0, a);
3020 EXPECT_DOUBLE_EQ(0.0, b);
3021 EXPECT_DOUBLE_EQ(0.0, c);
3022 EXPECT_DOUBLE_EQ(45.0, d);
3023 EXPECT_DOUBLE_EQ(72.0, e);
3024 EXPECT_DOUBLE_EQ(553.510009765625, f);
3025
3026 obj = FPDFPage_GetObject(page, 37);
3027 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3028 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3029 EXPECT_DOUBLE_EQ(55.94000244140625, a);
3030 EXPECT_DOUBLE_EQ(0.0, b);
3031 EXPECT_DOUBLE_EQ(0.0, c);
3032 EXPECT_DOUBLE_EQ(46.950000762939453, d);
3033 EXPECT_DOUBLE_EQ(216.0, e);
3034 EXPECT_DOUBLE_EQ(552.510009765625, f);
3035
3036 obj = FPDFPage_GetObject(page, 38);
3037 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3038 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3039 EXPECT_DOUBLE_EQ(70.528999328613281, a);
3040 EXPECT_DOUBLE_EQ(0.0, b);
3041 EXPECT_DOUBLE_EQ(0.0, c);
3042 EXPECT_DOUBLE_EQ(43.149997711181641, d);
3043 EXPECT_DOUBLE_EQ(360.0, e);
3044 EXPECT_DOUBLE_EQ(553.3599853515625, f);
3045
3046 UnloadPage(page);
3047}
3048
Lei Zhangab41f252018-12-23 03:10:50 +00003049TEST_F(FPDFEditEmbedderTest, DestroyPageObject) {
Jane Liu2e5f0ae2017-08-08 15:23:27 -04003050 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
3051 ASSERT_TRUE(rect);
3052
3053 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
3054 FPDFPageObj_Destroy(rect);
3055}
Jane Liube63ab92017-08-09 14:09:34 -04003056
Lei Zhangab41f252018-12-23 03:10:50 +00003057TEST_F(FPDFEditEmbedderTest, GetImageFilters) {
Jane Liube63ab92017-08-09 14:09:34 -04003058 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
3059 FPDF_PAGE page = LoadPage(0);
3060 ASSERT_TRUE(page);
3061
3062 // Verify that retrieving the filter of a non-image object would fail.
3063 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
3064 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3065 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
3066 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
3067
3068 // Verify the returned filter string for an image object with a single filter.
3069 obj = FPDFPage_GetObject(page, 33);
3070 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3071 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
3072 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
3073 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07003074 static constexpr char kFlateDecode[] = "FlateDecode";
3075 EXPECT_EQ(sizeof(kFlateDecode),
3076 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
3077 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04003078 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
3079
3080 // Verify all the filters for an image object with a list of filters.
3081 obj = FPDFPage_GetObject(page, 38);
3082 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3083 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
3084 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
3085 buf.clear();
3086 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07003087 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
3088 EXPECT_EQ(sizeof(kASCIIHexDecode),
3089 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
3090 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04003091
3092 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
3093 buf.clear();
3094 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07003095 static constexpr char kDCTDecode[] = "DCTDecode";
3096 EXPECT_EQ(sizeof(kDCTDecode),
3097 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
3098 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04003099
3100 UnloadPage(page);
3101}
Jane Liuca898292017-08-16 11:25:35 -04003102
Lei Zhangab41f252018-12-23 03:10:50 +00003103TEST_F(FPDFEditEmbedderTest, GetImageMetadata) {
Jane Liuca898292017-08-16 11:25:35 -04003104 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
3105 FPDF_PAGE page = LoadPage(0);
3106 ASSERT_TRUE(page);
3107
3108 // Check that getting the metadata of a null object would fail.
3109 FPDF_IMAGEOBJ_METADATA metadata;
3110 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
3111
3112 // Check that receiving the metadata with a null metadata object would fail.
3113 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
3114 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
3115
3116 // Check that when retrieving an image object's metadata without passing in
3117 // |page|, all values are correct, with the last two being default values.
3118 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3119 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05003120 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04003121 EXPECT_EQ(92u, metadata.width);
3122 EXPECT_EQ(68u, metadata.height);
Lei Zhang351e8b02018-12-20 01:10:06 +00003123 EXPECT_FLOAT_EQ(96.0f, metadata.horizontal_dpi);
3124 EXPECT_FLOAT_EQ(96.0f, metadata.vertical_dpi);
Jane Liuca898292017-08-16 11:25:35 -04003125 EXPECT_EQ(0u, metadata.bits_per_pixel);
3126 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
3127
3128 // Verify the metadata of a bitmap image with indexed colorspace.
3129 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05003130 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04003131 EXPECT_EQ(92u, metadata.width);
3132 EXPECT_EQ(68u, metadata.height);
Lei Zhang351e8b02018-12-20 01:10:06 +00003133 EXPECT_FLOAT_EQ(96.0f, metadata.horizontal_dpi);
3134 EXPECT_FLOAT_EQ(96.0f, metadata.vertical_dpi);
Jane Liuca898292017-08-16 11:25:35 -04003135 EXPECT_EQ(1u, metadata.bits_per_pixel);
3136 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
3137
3138 // Verify the metadata of an image with RGB colorspace.
3139 obj = FPDFPage_GetObject(page, 37);
3140 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3141 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05003142 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04003143 EXPECT_EQ(126u, metadata.width);
3144 EXPECT_EQ(106u, metadata.height);
Lei Zhang351e8b02018-12-20 01:10:06 +00003145 EXPECT_FLOAT_EQ(162.173752f, metadata.horizontal_dpi);
3146 EXPECT_FLOAT_EQ(162.555878f, metadata.vertical_dpi);
Jane Liuca898292017-08-16 11:25:35 -04003147 EXPECT_EQ(24u, metadata.bits_per_pixel);
3148 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
3149
3150 UnloadPage(page);
3151}