blob: 895b9fe3e7351d893b6dfb82628969ce030c887a [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"
Lei Zhang4c64e962019-02-05 19:24:12 +000029#include "testing/utils/hash.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080030
Lei Zhangab41f252018-12-23 03:10:50 +000031class FPDFEditEmbedderTest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050032 protected:
33 FPDF_DOCUMENT CreateNewDocument() {
34 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070035 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050036 return document_;
37 }
38
Lei Zhang710fa992018-05-25 16:24:48 +000039 void CheckFontDescriptor(const CPDF_Dictionary* font_dict,
Nicolas Penad03ca422017-03-06 13:54:33 -050040 int font_type,
41 bool bold,
42 bool italic,
Tom Sepez20c41a52018-08-29 23:53:53 +000043 pdfium::span<const uint8_t> span) {
Lei Zhangb1ec2802018-05-25 21:55:24 +000044 const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
Nicolas Penad03ca422017-03-06 13:54:33 -050045 ASSERT_TRUE(font_desc);
46 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
47 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
48 font_desc->GetStringFor("FontName"));
49
50 // Check that the font descriptor has the required keys according to spec
51 // 1.7 Table 5.19
52 ASSERT_TRUE(font_desc->KeyExist("Flags"));
Dan Sinclair10e1f052017-09-28 15:59:42 -040053
Nicolas Penad03ca422017-03-06 13:54:33 -050054 int font_flags = font_desc->GetIntegerFor("Flags");
Dan Sinclair10e1f052017-09-28 15:59:42 -040055 EXPECT_EQ(bold, FontStyleIsBold(font_flags));
56 EXPECT_EQ(italic, FontStyleIsItalic(font_flags));
57 EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags));
Nicolas Penad03ca422017-03-06 13:54:33 -050058 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
Nicolás Peña5f95f362017-09-28 13:00:45 +090059
Lei Zhangb1ec2802018-05-25 21:55:24 +000060 const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox");
Nicolás Peña5f95f362017-09-28 13:00:45 +090061 ASSERT_TRUE(fontBBox);
Lei Zhangf40380f2018-10-12 18:31:51 +000062 EXPECT_EQ(4u, fontBBox->size());
Nicolás Peña5f95f362017-09-28 13:00:45 +090063 // Check that the coordinates are in the preferred order according to spec
64 // 1.7 Section 3.8.4
65 EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2));
66 EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3));
67
Nicolas Penad03ca422017-03-06 13:54:33 -050068 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
69 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
70 EXPECT_TRUE(font_desc->KeyExist("Descent"));
71 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
72 EXPECT_TRUE(font_desc->KeyExist("StemV"));
Ryan Harrison275e2602017-09-18 14:23:18 -040073 ByteString present("FontFile");
74 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050075 if (font_type == FPDF_FONT_TRUETYPE)
76 std::swap(present, absent);
77 EXPECT_TRUE(font_desc->KeyExist(present));
78 EXPECT_FALSE(font_desc->KeyExist(absent));
79
Artem Strygineababa12018-06-06 12:31:18 +000080 auto streamAcc =
81 pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present));
82 streamAcc->LoadAllDataRaw();
83
Nicolas Penad03ca422017-03-06 13:54:33 -050084 // Check that the font stream is the one that was provided
Tom Sepez20c41a52018-08-29 23:53:53 +000085 ASSERT_EQ(span.size(), streamAcc->GetSize());
Nicolás Peña79eab232017-09-28 13:29:05 +090086 if (font_type == FPDF_FONT_TRUETYPE) {
Tom Sepez20c41a52018-08-29 23:53:53 +000087 ASSERT_EQ(static_cast<int>(span.size()),
Artem Strygineababa12018-06-06 12:31:18 +000088 streamAcc->GetDict()->GetIntegerFor("Length1"));
Nicolás Peña79eab232017-09-28 13:29:05 +090089 }
Artem Strygineababa12018-06-06 12:31:18 +000090
91 const uint8_t* stream_data = streamAcc->GetData();
Tom Sepez20c41a52018-08-29 23:53:53 +000092 for (size_t j = 0; j < span.size(); j++)
93 EXPECT_EQ(span[j], stream_data[j]) << " at byte " << j;
Nicolas Penad03ca422017-03-06 13:54:33 -050094 }
95
Lei Zhangde579ab2018-05-25 21:49:49 +000096 void CheckCompositeFontWidths(const CPDF_Array* widths_array,
Nicolas Penad03ca422017-03-06 13:54:33 -050097 CPDF_Font* typed_font) {
98 // Check that W array is in a format that conforms to PDF spec 1.7 section
99 // "Glyph Metrics in CIDFonts" (these checks are not
100 // implementation-specific).
Lei Zhangf40380f2018-10-12 18:31:51 +0000101 EXPECT_GT(widths_array->size(), 1u);
Nicolas Penad03ca422017-03-06 13:54:33 -0500102 int num_cids_checked = 0;
103 int cur_cid = 0;
Lei Zhangf40380f2018-10-12 18:31:51 +0000104 for (size_t idx = 0; idx < widths_array->size(); idx++) {
Nicolas Penad03ca422017-03-06 13:54:33 -0500105 int cid = widths_array->GetNumberAt(idx);
106 EXPECT_GE(cid, cur_cid);
Lei Zhangf40380f2018-10-12 18:31:51 +0000107 ASSERT_FALSE(++idx == widths_array->size());
Lei Zhangde579ab2018-05-25 21:49:49 +0000108 const CPDF_Object* next = widths_array->GetObjectAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500109 if (next->IsArray()) {
110 // We are in the c [w1 w2 ...] case
Lei Zhangde579ab2018-05-25 21:49:49 +0000111 const CPDF_Array* arr = next->AsArray();
Lei Zhangf40380f2018-10-12 18:31:51 +0000112 int cnt = static_cast<int>(arr->size());
Nicolas Penad03ca422017-03-06 13:54:33 -0500113 size_t inner_idx = 0;
114 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
Nicolas Pena23346602018-01-30 21:42:41 +0000115 uint32_t width = arr->GetNumberAt(inner_idx++);
Dan Sinclair971a6742018-03-28 19:23:25 +0000116 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
117 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500118 }
119 num_cids_checked += cnt;
120 continue;
121 }
122 // Otherwise, are in the c_first c_last w case.
123 ASSERT_TRUE(next->IsNumber());
124 int last_cid = next->AsNumber()->GetInteger();
Lei Zhangf40380f2018-10-12 18:31:51 +0000125 ASSERT_FALSE(++idx == widths_array->size());
Nicolas Pena23346602018-01-30 21:42:41 +0000126 uint32_t width = widths_array->GetNumberAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500127 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
Dan Sinclair971a6742018-03-28 19:23:25 +0000128 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
129 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500130 }
131 num_cids_checked += last_cid - cid + 1;
132 }
133 // Make sure we have a good amount of cids described
134 EXPECT_GT(num_cids_checked, 900);
135 }
136 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
137
138 private:
139 CPDF_Document* cpdf_doc_;
140};
Tom Sepezd483eb42016-01-06 10:03:59 -0800141
etienneb7712c262016-04-26 08:13:45 -0700142namespace {
thestigdc7ec032016-11-21 15:32:52 -0800143
etienneb7712c262016-04-26 08:13:45 -0700144const char kExpectedPDF[] =
145 "%PDF-1.7\r\n"
146 "%\xA1\xB3\xC5\xD7\r\n"
147 "1 0 obj\r\n"
148 "<</Pages 2 0 R /Type/Catalog>>\r\n"
149 "endobj\r\n"
150 "2 0 obj\r\n"
151 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
152 "endobj\r\n"
153 "3 0 obj\r\n"
154 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
155 "endobj\r\n"
156 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400157 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
158 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400159 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700160 ">>\r\n"
161 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400162 "5 0 obj\r\n"
163 "<</BM/Normal/CA 1/ca 1>>\r\n"
164 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700165 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400166 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700167 "0000000000 65535 f\r\n"
168 "0000000017 00000 n\r\n"
169 "0000000066 00000 n\r\n"
170 "0000000122 00000 n\r\n"
171 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400172 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700173 "trailer\r\n"
174 "<<\r\n"
175 "/Root 1 0 R\r\n"
176 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400177 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700178 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400179 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700180 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800181
etienneb7712c262016-04-26 08:13:45 -0700182} // namespace
183
Lei Zhangab41f252018-12-23 03:10:50 +0000184TEST_F(FPDFEditEmbedderTest, EmptyCreation) {
Tom Sepezd483eb42016-01-06 10:03:59 -0800185 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700186 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800187 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400188 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800189 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800190 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700191
Nicolas Penad9d6c292017-06-06 16:12:10 -0400192 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
193 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700194 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800195}
thestigdc7ec032016-11-21 15:32:52 -0800196
197// Regression test for https://crbug.com/667012
Lei Zhangab41f252018-12-23 03:10:50 +0000198TEST_F(FPDFEditEmbedderTest, RasterizePDF) {
thestigdc7ec032016-11-21 15:32:52 -0800199 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
200
Lei Zhangf0542892019-01-17 18:46:27 +0000201 // Get the bitmap for the original document.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000202 ScopedFPDFBitmap orig_bitmap;
thestigdc7ec032016-11-21 15:32:52 -0800203 {
204 EXPECT_TRUE(OpenDocument("black.pdf"));
205 FPDF_PAGE orig_page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000206 ASSERT_TRUE(orig_page);
207 orig_bitmap = RenderLoadedPage(orig_page);
208 CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800209 UnloadPage(orig_page);
210 }
211
212 // Create a new document from |orig_bitmap| and save it.
213 {
214 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
215 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
216
217 // Add the bitmap to an image object and add the image object to the output
218 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700219 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000220 EXPECT_TRUE(
221 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get()));
thestigdc7ec032016-11-21 15:32:52 -0800222 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
223 FPDFPage_InsertObject(temp_page, temp_img);
224 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
225 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
226 FPDF_ClosePage(temp_page);
227 FPDF_CloseDocument(temp_doc);
228 }
thestigdc7ec032016-11-21 15:32:52 -0800229
230 // Get the generated content. Make sure it is at least as big as the original
231 // PDF.
Lei Zhangd72fd582018-07-27 19:37:27 +0000232 EXPECT_GT(GetString().size(), 923u);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400233 VerifySavedDocument(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800234}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500235
Lei Zhangab41f252018-12-23 03:10:50 +0000236TEST_F(FPDFEditEmbedderTest, AddPaths) {
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500237 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500238 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000239 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500240
241 // We will first add a red rectangle
242 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000243 ASSERT_TRUE(red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500244 // Expect false when trying to set colors out of range
245 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
246 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
247
248 // Fill rectangle with red and insert to the page
249 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
250 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
Miklos Vajna491112b2018-05-30 13:30:10 +0000251
252 int fillmode = FPDF_FILLMODE_NONE;
253 FPDF_BOOL stroke = true;
254 EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke));
255 EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode);
256 EXPECT_FALSE(stroke);
257
Miklos Vajna97f4d672018-06-04 14:47:17 +0000258 double matrix_a = 1;
259 double matrix_b = 2;
260 double matrix_c = 3;
261 double matrix_d = 4;
262 double matrix_e = 5;
263 double matrix_f = 6;
264 EXPECT_FALSE(FPDFPath_SetMatrix(nullptr, matrix_a, matrix_b, matrix_c,
265 matrix_d, matrix_e, matrix_f));
266 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
267 matrix_d, matrix_e, matrix_f));
Miklos Vajnaac42dd22018-06-05 13:28:58 +0000268 // Set to 0 before FPDFPath_GetMatrix() to ensure they are actually set by
269 // the function.
270 matrix_a = 0;
271 matrix_b = 0;
272 matrix_c = 0;
273 matrix_d = 0;
274 matrix_e = 0;
275 matrix_f = 0;
Miklos Vajna97f4d672018-06-04 14:47:17 +0000276 EXPECT_FALSE(FPDFPath_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
277 &matrix_d, &matrix_e, &matrix_f));
278 EXPECT_TRUE(FPDFPath_GetMatrix(red_rect, &matrix_a, &matrix_b, &matrix_c,
279 &matrix_d, &matrix_e, &matrix_f));
280 EXPECT_EQ(1, static_cast<int>(matrix_a));
281 EXPECT_EQ(2, static_cast<int>(matrix_b));
282 EXPECT_EQ(3, static_cast<int>(matrix_c));
283 EXPECT_EQ(4, static_cast<int>(matrix_d));
284 EXPECT_EQ(5, static_cast<int>(matrix_e));
285 EXPECT_EQ(6, static_cast<int>(matrix_f));
286 // Set back the default
287 matrix_a = 1;
288 matrix_b = 0;
289 matrix_c = 0;
290 matrix_d = 1;
291 matrix_e = 0;
292 matrix_f = 0;
293 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
294 matrix_d, matrix_e, matrix_f));
295
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500296 FPDFPage_InsertObject(page, red_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000297 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000298 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000299 CompareBitmap(page_bitmap.get(), 612, 792,
300 "66d02eaa6181e2c069ce2ea99beda497");
301 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500302
303 // Now add to that a green rectangle with some medium alpha
304 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
305 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200306
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200307 // Make sure the type of the rectangle is a path.
308 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
309
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200310 // Make sure we get back the same color we set previously.
311 unsigned int R;
312 unsigned int G;
313 unsigned int B;
314 unsigned int A;
315 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
Lei Zhangd72fd582018-07-27 19:37:27 +0000316 EXPECT_EQ(0u, R);
317 EXPECT_EQ(255u, G);
318 EXPECT_EQ(0u, B);
319 EXPECT_EQ(128u, A);
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200320
Miklos Vajna12abfd02017-09-15 07:49:03 +0200321 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
322 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200323 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200324 // Verify actual coordinates.
325 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
326 float x;
327 float y;
328 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
329 EXPECT_EQ(100, x);
330 EXPECT_EQ(100, y);
331 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
332 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
333 segment = FPDFPath_GetPathSegment(green_rect, 1);
334 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
335 EXPECT_EQ(100, x);
336 EXPECT_EQ(140, y);
337 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
338 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
339 segment = FPDFPath_GetPathSegment(green_rect, 2);
340 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
341 EXPECT_EQ(140, x);
342 EXPECT_EQ(140, y);
343 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
344 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
345 segment = FPDFPath_GetPathSegment(green_rect, 3);
346 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
347 EXPECT_EQ(140, x);
348 EXPECT_EQ(100, y);
349 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
350 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
351 segment = FPDFPath_GetPathSegment(green_rect, 4);
352 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
353 EXPECT_EQ(100, x);
354 EXPECT_EQ(100, y);
355 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
356 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200357
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500358 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
359 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000360 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000361 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000362 CompareBitmap(page_bitmap.get(), 612, 792,
363 "7b0b87604594e773add528fae567a558");
364 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500365
366 // Add a black triangle.
367 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
368 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
369 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
370 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
371 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
372 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200373
374 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
375 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200376 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200377 // Verify actual coordinates.
378 segment = FPDFPath_GetPathSegment(black_path, 0);
379 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
380 EXPECT_EQ(400, x);
381 EXPECT_EQ(100, y);
382 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
383 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
384 segment = FPDFPath_GetPathSegment(black_path, 1);
385 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
386 EXPECT_EQ(400, x);
387 EXPECT_EQ(200, y);
388 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
389 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
390 segment = FPDFPath_GetPathSegment(black_path, 2);
391 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
392 EXPECT_EQ(300, x);
393 EXPECT_EQ(100, y);
394 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
395 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
396 // Make sure out of bounds index access fails properly.
397 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200398
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500399 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000400 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000401 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000402 CompareBitmap(page_bitmap.get(), 612, 792,
403 "eadc8020a14dfcf091da2688733d8806");
404 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500405
406 // Now add a more complex blue path.
407 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
408 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
409 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
410 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
411 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
412 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
413 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
414 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
415 EXPECT_TRUE(FPDFPath_Close(blue_path));
416 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000417 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
418 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000419 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000420 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
421 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500422
423 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400424 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500425 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500426 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500427
428 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000429 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500430}
431
Lei Zhangab41f252018-12-23 03:10:50 +0000432TEST_F(FPDFEditEmbedderTest, SetText) {
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000433 // Load document with some text.
434 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
435 FPDF_PAGE page = LoadPage(0);
436 ASSERT_TRUE(page);
437
438 // Get the "Hello, world!" text object and change it.
439 ASSERT_EQ(2, FPDFPage_CountObjects(page));
440 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
441 ASSERT_TRUE(page_object);
442 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
443 GetFPDFWideString(L"Changed for SetText test");
444 EXPECT_TRUE(FPDFText_SetText(page_object, text1.get()));
445
446 // Verify the "Hello, world!" text is gone and "Changed for SetText test" is
447 // now displayed.
448 ASSERT_EQ(2, FPDFPage_CountObjects(page));
449#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
450 const char kChangedMD5[] = "94c1e7a5af7dd9d77dc2223b1091acb7";
451#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000452 const char kChangedMD5[] = "3137fdb27962671f5c3963a5e965eff5";
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000453#else
454 const char kChangedMD5[] = "a0c4ea6620772991f66bf7130379b08a";
455#endif
456 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000457 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000458 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
459 }
460
461 // Now save the result.
462 EXPECT_TRUE(FPDFPage_GenerateContent(page));
463 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
464
465 UnloadPage(page);
466
467 // Re-open the file and check the changes were kept in the saved .pdf.
Lei Zhang0b494052019-01-31 21:41:15 +0000468 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000469 FPDF_PAGE saved_page = LoadSavedPage(0);
470 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
471 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000472 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000473 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
474 }
475
476 CloseSavedPage(saved_page);
477 CloseSavedDocument();
478}
479
Lei Zhangab41f252018-12-23 03:10:50 +0000480TEST_F(FPDFEditEmbedderTest, RemovePageObject) {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000481 // Load document with some text.
482 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
483 FPDF_PAGE page = LoadPage(0);
484 ASSERT_TRUE(page);
485
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000486 // Show what the original file looks like.
487 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000488#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000489 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000490#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000491 const char kOriginalMD5[] = "795b7ce1626931aa06af0fa23b7d80bb";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000492#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000493 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000494#endif
Lei Zhang30ff2532019-01-31 21:37:55 +0000495 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000496 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
497 }
498
499 // Get the "Hello, world!" text object and remove it.
500 ASSERT_EQ(2, FPDFPage_CountObjects(page));
501 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
502 ASSERT_TRUE(page_object);
503 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
504
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000505 // Verify the "Hello, world!" text is gone.
506 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000507#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000508 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000509#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000510 const char kRemovedMD5[] = "aae6c5334721f90ec30d3d59f4ef7deb";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000511#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000512 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000513#endif
Lei Zhang30ff2532019-01-31 21:37:55 +0000514 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000515 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
516 }
517 ASSERT_EQ(1, FPDFPage_CountObjects(page));
518
519 UnloadPage(page);
520 FPDFPageObj_Destroy(page_object);
521}
522
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000523void CheckMarkCounts(FPDF_PAGE page,
524 int start_from,
525 int expected_object_count,
526 size_t expected_prime_count,
527 size_t expected_square_count,
528 size_t expected_greater_than_ten_count,
529 size_t expected_bounds_count) {
530 int object_count = FPDFPage_CountObjects(page);
531 ASSERT_EQ(expected_object_count, object_count);
532
533 size_t prime_count = 0;
534 size_t square_count = 0;
535 size_t greater_than_ten_count = 0;
536 size_t bounds_count = 0;
537 for (int i = 0; i < object_count; ++i) {
538 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
539
540 int mark_count = FPDFPageObj_CountMarks(page_object);
541 for (int j = 0; j < mark_count; ++j) {
542 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
543
544 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000545 unsigned long name_len = 999u;
546 ASSERT_TRUE(
547 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
548 EXPECT_GT(name_len, 0u);
549 EXPECT_NE(999u, name_len);
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000550 std::wstring name =
551 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
552 if (name == L"Prime") {
553 prime_count++;
554 } else if (name == L"Square") {
555 square_count++;
556 int expected_square = start_from + i;
557 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
558
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000559 unsigned long get_param_key_return = 999u;
560 ASSERT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer),
561 &get_param_key_return));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000562 EXPECT_EQ((6u + 1u) * 2u, get_param_key_return);
563 std::wstring key =
564 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
565 EXPECT_EQ(L"Factor", key);
566
567 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000568 FPDFPageObjMark_GetParamValueType(mark, "Factor"));
Henrique Nakashima140dead2018-07-11 21:40:03 +0000569 int square_root;
Henrique Nakashima94230e52018-07-11 22:02:02 +0000570 EXPECT_TRUE(
571 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &square_root));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000572 EXPECT_EQ(expected_square, square_root * square_root);
573 } else if (name == L"GreaterThanTen") {
574 greater_than_ten_count++;
575 } else if (name == L"Bounds") {
576 bounds_count++;
577 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
578
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000579 unsigned long get_param_key_return = 999u;
580 ASSERT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer),
581 &get_param_key_return));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000582 EXPECT_EQ((8u + 1u) * 2u, get_param_key_return);
583 std::wstring key =
584 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
585 EXPECT_EQ(L"Position", key);
586
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000587 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +0000588 FPDFPageObjMark_GetParamValueType(mark, "Position"));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000589 unsigned long length;
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000590 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
591 mark, "Position", buffer, sizeof(buffer), &length));
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000592 ASSERT_GT(length, 0u);
Henrique Nakashima140dead2018-07-11 21:40:03 +0000593 std::wstring value =
594 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000595
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000596 // "Position" can be "First", "Last", or "End".
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000597 if (i == 0) {
598 EXPECT_EQ((5u + 1u) * 2u, length);
599 EXPECT_EQ(L"First", value);
600 } else if (i == object_count - 1) {
Henrique Nakashimaa3406772018-07-13 19:10:53 +0000601 if (length == (4u + 1u) * 2u) {
602 EXPECT_EQ(L"Last", value);
603 } else if (length == (3u + 1u) * 2u) {
604 EXPECT_EQ(L"End", value);
605 } else {
606 FAIL();
607 }
Henrique Nakashimad8df8c32018-07-12 22:15:09 +0000608 } else {
609 FAIL();
610 }
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000611 } else {
612 FAIL();
613 }
614 }
615 }
616
617 // Expect certain number of tagged objects. The test file contains strings
618 // from 1 to 19.
619 EXPECT_EQ(expected_prime_count, prime_count);
620 EXPECT_EQ(expected_square_count, square_count);
621 EXPECT_EQ(expected_greater_than_ten_count, greater_than_ten_count);
622 EXPECT_EQ(expected_bounds_count, bounds_count);
623}
624
Lei Zhangab41f252018-12-23 03:10:50 +0000625TEST_F(FPDFEditEmbedderTest, ReadMarkedObjectsIndirectDict) {
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000626 // Load document with some text marked with an indirect property.
627 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
628 FPDF_PAGE page = LoadPage(0);
629 ASSERT_TRUE(page);
630
631 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
632
633 UnloadPage(page);
634}
635
Lei Zhangab41f252018-12-23 03:10:50 +0000636TEST_F(FPDFEditEmbedderTest, RemoveMarkedObjectsPrime) {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000637 // Load document with some text.
638 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
639 FPDF_PAGE page = LoadPage(0);
640 ASSERT_TRUE(page);
641
642 // Show what the original file looks like.
643 {
644#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
645 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
646#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
647 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
648#else
649 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
650#endif
Lei Zhang30ff2532019-01-31 21:37:55 +0000651 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000652 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
653 }
654
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000655 constexpr int expected_object_count = 19;
656 CheckMarkCounts(page, 1, expected_object_count, 8, 4, 9, 1);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000657
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000658 // Get all objects marked with "Prime"
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000659 std::vector<FPDF_PAGEOBJECT> primes;
Henrique Nakashimacbed9492018-07-10 21:54:26 +0000660 for (int i = 0; i < expected_object_count; ++i) {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000661 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
662
663 int mark_count = FPDFPageObj_CountMarks(page_object);
664 for (int j = 0; j < mark_count; ++j) {
665 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
666
667 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000668 unsigned long name_len = 999u;
669 ASSERT_TRUE(
670 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
671 EXPECT_GT(name_len, 0u);
672 EXPECT_NE(999u, name_len);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000673 std::wstring name =
674 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
675 if (name == L"Prime") {
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000676 primes.push_back(page_object);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000677 }
678 }
679 }
680
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000681 // Remove all objects marked with "Prime".
682 for (FPDF_PAGEOBJECT page_object : primes) {
683 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
684 FPDFPageObj_Destroy(page_object);
685 }
686
687 EXPECT_EQ(11, FPDFPage_CountObjects(page));
688
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000689#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000690 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000691#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000692 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000693#else
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000694 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000695#endif
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000696 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000697 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000698 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
699 }
700
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000701 // Save the file.
702 EXPECT_TRUE(FPDFPage_GenerateContent(page));
703 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000704 UnloadPage(page);
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000705
706 // Re-open the file and check the prime marks are not there anymore.
Lei Zhang0b494052019-01-31 21:41:15 +0000707 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000708 FPDF_PAGE saved_page = LoadSavedPage(0);
709 EXPECT_EQ(11, FPDFPage_CountObjects(saved_page));
710
711 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000712 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashimaa5078b72018-09-10 17:09:42 +0000713 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
714 }
715
716 CloseSavedPage(saved_page);
717 CloseSavedDocument();
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000718}
719
Lei Zhangab41f252018-12-23 03:10:50 +0000720TEST_F(FPDFEditEmbedderTest, RemoveMarks) {
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000721 // Load document with some text.
722 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
723 FPDF_PAGE page = LoadPage(0);
724 ASSERT_TRUE(page);
725
726 constexpr int kExpectedObjectCount = 19;
727 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
728
729 // Remove all "Prime" content marks.
730 for (int i = 0; i < kExpectedObjectCount; ++i) {
731 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
732
733 int mark_count = FPDFPageObj_CountMarks(page_object);
734 for (int j = mark_count - 1; j >= 0; --j) {
735 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
736
737 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000738 unsigned long name_len = 999u;
739 ASSERT_TRUE(
740 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
741 EXPECT_GT(name_len, 0u);
742 EXPECT_NE(999u, name_len);
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000743 std::wstring name =
744 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
745 if (name == L"Prime") {
746 // Remove mark.
747 EXPECT_TRUE(FPDFPageObj_RemoveMark(page_object, mark));
748
749 // Verify there is now one fewer mark in the page object.
750 EXPECT_EQ(mark_count - 1, FPDFPageObj_CountMarks(page_object));
751 }
752 }
753 }
754
755 // Verify there are 0 "Prime" content marks now.
756 CheckMarkCounts(page, 1, kExpectedObjectCount, 0, 4, 9, 1);
757
758 // Save the file.
759 EXPECT_TRUE(FPDFPage_GenerateContent(page));
760 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
761 UnloadPage(page);
762
763 // Re-open the file and check the prime marks are not there anymore.
Lei Zhang0b494052019-01-31 21:41:15 +0000764 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimafed4adb2018-07-13 19:47:22 +0000765 FPDF_PAGE saved_page = LoadSavedPage(0);
766
767 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 0, 4, 9, 1);
768
769 CloseSavedPage(saved_page);
770 CloseSavedDocument();
771}
772
Lei Zhangab41f252018-12-23 03:10:50 +0000773TEST_F(FPDFEditEmbedderTest, RemoveMarkParam) {
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000774 // Load document with some text.
775 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
776 FPDF_PAGE page = LoadPage(0);
777 ASSERT_TRUE(page);
778
779 constexpr int kExpectedObjectCount = 19;
780 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
781
782 // Remove all "Square" content marks parameters.
783 for (int i = 0; i < kExpectedObjectCount; ++i) {
784 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
785
786 int mark_count = FPDFPageObj_CountMarks(page_object);
787 for (int j = 0; j < mark_count; ++j) {
788 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
789
790 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000791 unsigned long name_len = 999u;
792 ASSERT_TRUE(
793 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
794 EXPECT_GT(name_len, 0u);
795 EXPECT_NE(999u, name_len);
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000796 std::wstring name =
797 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
798 if (name == L"Square") {
799 // Show the mark has a "Factor" parameter.
800 int out_value;
801 EXPECT_TRUE(
802 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
803
804 // Remove parameter.
805 EXPECT_TRUE(FPDFPageObjMark_RemoveParam(page_object, mark, "Factor"));
806
807 // Verify the "Factor" parameter is gone.
808 EXPECT_FALSE(
809 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
810 }
811 }
812 }
813
814 // Save the file.
815 EXPECT_TRUE(FPDFPage_GenerateContent(page));
816 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
817 UnloadPage(page);
818
819 // Re-open the file and check the "Factor" parameters are still gone.
Lei Zhang0b494052019-01-31 21:41:15 +0000820 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000821 FPDF_PAGE saved_page = LoadSavedPage(0);
822
823 size_t square_count = 0;
824 for (int i = 0; i < kExpectedObjectCount; ++i) {
825 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
826
827 int mark_count = FPDFPageObj_CountMarks(page_object);
828 for (int j = 0; j < mark_count; ++j) {
829 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
830
831 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +0000832 unsigned long name_len = 999u;
833 ASSERT_TRUE(
834 FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
835 EXPECT_GT(name_len, 0u);
836 EXPECT_NE(999u, name_len);
Henrique Nakashimacf403ba2018-07-13 20:12:41 +0000837 std::wstring name =
838 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
839 if (name == L"Square") {
840 // Verify the "Factor" parameter is still gone.
841 int out_value;
842 EXPECT_FALSE(
843 FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
844
845 ++square_count;
846 }
847 }
848 }
849
850 // Verify the parameters are gone, but the marks are not.
851 EXPECT_EQ(4u, square_count);
852
853 CloseSavedPage(saved_page);
854 CloseSavedDocument();
855}
856
Lei Zhangab41f252018-12-23 03:10:50 +0000857TEST_F(FPDFEditEmbedderTest, MaintainMarkedObjects) {
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000858 // Load document with some text.
859 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
860 FPDF_PAGE page = LoadPage(0);
861 ASSERT_TRUE(page);
862
863 // Iterate over all objects, counting the number of times each content mark
864 // name appears.
865 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
866
867 // Remove first page object.
868 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
869 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
870 FPDFPageObj_Destroy(page_object);
871
872 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
873
874 EXPECT_TRUE(FPDFPage_GenerateContent(page));
875 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
876
877 UnloadPage(page);
878
Lei Zhang0b494052019-01-31 21:41:15 +0000879 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000880 FPDF_PAGE saved_page = LoadSavedPage(0);
881
882 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
883
884 CloseSavedPage(saved_page);
885 CloseSavedDocument();
886}
887
Lei Zhangab41f252018-12-23 03:10:50 +0000888TEST_F(FPDFEditEmbedderTest, MaintainIndirectMarkedObjects) {
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000889 // Load document with some text.
890 EXPECT_TRUE(OpenDocument("text_in_page_marked_indirect.pdf"));
891 FPDF_PAGE page = LoadPage(0);
892 ASSERT_TRUE(page);
893
894 // Iterate over all objects, counting the number of times each content mark
895 // name appears.
896 CheckMarkCounts(page, 1, 19, 8, 4, 9, 1);
897
898 // Remove first page object.
899 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
900 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
901 FPDFPageObj_Destroy(page_object);
902
903 CheckMarkCounts(page, 2, 18, 8, 3, 9, 1);
904
905 EXPECT_TRUE(FPDFPage_GenerateContent(page));
906 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
907
908 UnloadPage(page);
909
Lei Zhang0b494052019-01-31 21:41:15 +0000910 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimab4bcf692018-07-11 21:19:22 +0000911 FPDF_PAGE saved_page = LoadSavedPage(0);
912
913 CheckMarkCounts(saved_page, 2, 18, 8, 3, 9, 1);
914
915 CloseSavedPage(saved_page);
916 CloseSavedDocument();
917}
918
Lei Zhangab41f252018-12-23 03:10:50 +0000919TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObject) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000920 // Load document with some text.
921 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
922 FPDF_PAGE page = LoadPage(0);
923 ASSERT_TRUE(page);
924
925 // Get the "Hello, world!" text object and remove it.
926 ASSERT_EQ(2, FPDFPage_CountObjects(page));
927 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
928 ASSERT_TRUE(page_object);
929 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
930
931 // Verify the "Hello, world!" text is gone.
932 ASSERT_EQ(1, FPDFPage_CountObjects(page));
933
934 // Save the file
935 EXPECT_TRUE(FPDFPage_GenerateContent(page));
936 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
937 UnloadPage(page);
938 FPDFPageObj_Destroy(page_object);
939
940 // Re-open the file and check the page object count is still 1.
Lei Zhang0b494052019-01-31 21:41:15 +0000941 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000942 FPDF_PAGE saved_page = LoadSavedPage(0);
943 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
944 CloseSavedPage(saved_page);
945 CloseSavedDocument();
946}
947
Lei Zhangab41f252018-12-23 03:10:50 +0000948TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObjectSplitStreamsNotLonely) {
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000949 // Load document with some text.
950 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
951 FPDF_PAGE page = LoadPage(0);
952 ASSERT_TRUE(page);
953
954 // Get the "Hello, world!" text object and remove it. There is another object
955 // in the same stream that says "Goodbye, world!"
956 ASSERT_EQ(3, FPDFPage_CountObjects(page));
957 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
958 ASSERT_TRUE(page_object);
959 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
960
961 // Verify the "Hello, world!" text is gone.
962 ASSERT_EQ(2, FPDFPage_CountObjects(page));
963#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
964 const char kHelloRemovedMD5[] = "e07a62d412728fc4d6e3ff42f2dd0e11";
965#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +0000966 const char kHelloRemovedMD5[] = "a97d4c72c969ba373c2dce675d277e65";
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000967#else
968 const char kHelloRemovedMD5[] = "95b92950647a2190e1230911e7a1a0e9";
969#endif
970 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000971 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000972 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
973 }
974
975 // Save the file
976 EXPECT_TRUE(FPDFPage_GenerateContent(page));
977 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
978 UnloadPage(page);
979 FPDFPageObj_Destroy(page_object);
980
981 // Re-open the file and check the page object count is still 2.
Lei Zhang0b494052019-01-31 21:41:15 +0000982 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000983 FPDF_PAGE saved_page = LoadSavedPage(0);
984
985 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
986 {
Lei Zhang30ff2532019-01-31 21:37:55 +0000987 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000988 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
989 }
990
991 CloseSavedPage(saved_page);
992 CloseSavedDocument();
993}
994
Lei Zhangab41f252018-12-23 03:10:50 +0000995TEST_F(FPDFEditEmbedderTest, RemoveExistingPageObjectSplitStreamsLonely) {
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000996 // Load document with some text.
997 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
998 FPDF_PAGE page = LoadPage(0);
999 ASSERT_TRUE(page);
1000
1001 // Get the "Greetings, world!" text object and remove it. This is the only
1002 // object in the stream.
1003 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1004 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 2);
1005 ASSERT_TRUE(page_object);
1006 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
1007
1008 // Verify the "Greetings, world!" text is gone.
1009 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1010#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1011 const char kGreetingsRemovedMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
1012#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001013 const char kGreetingsRemovedMD5[] = "795b7ce1626931aa06af0fa23b7d80bb";
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001014#else
1015 const char kGreetingsRemovedMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
1016#endif
1017 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001018 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001019 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
1020 }
1021
1022 // Save the file
1023 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1024 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1025 UnloadPage(page);
1026 FPDFPageObj_Destroy(page_object);
1027
1028 // Re-open the file and check the page object count is still 2.
Lei Zhang0b494052019-01-31 21:41:15 +00001029 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001030 FPDF_PAGE saved_page = LoadSavedPage(0);
1031
1032 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
1033 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001034 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001035 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
1036 }
1037
1038 CloseSavedPage(saved_page);
1039 CloseSavedDocument();
1040}
1041
Lei Zhangab41f252018-12-23 03:10:50 +00001042TEST_F(FPDFEditEmbedderTest, GetContentStream) {
Henrique Nakashima6eb79392018-06-12 20:27:35 +00001043 // Load document with some text split across streams.
1044 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
1045 FPDF_PAGE page = LoadPage(0);
1046 ASSERT_TRUE(page);
1047
1048 // Content stream 0: page objects 0-14.
1049 // Content stream 1: page objects 15-17.
1050 // Content stream 2: page object 18.
1051 ASSERT_EQ(19, FPDFPage_CountObjects(page));
1052 for (int i = 0; i < 19; i++) {
1053 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1054 ASSERT_TRUE(page_object);
1055 CPDF_PageObject* cpdf_page_object =
1056 CPDFPageObjectFromFPDFPageObject(page_object);
1057 if (i < 15)
1058 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1059 else if (i < 18)
1060 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1061 else
1062 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
1063 }
1064
1065 UnloadPage(page);
1066}
1067
Lei Zhangab41f252018-12-23 03:10:50 +00001068TEST_F(FPDFEditEmbedderTest, RemoveAllFromStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001069 // Load document with some text split across streams.
1070 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
1071 FPDF_PAGE page = LoadPage(0);
1072 ASSERT_TRUE(page);
1073
1074 // Content stream 0: page objects 0-14.
1075 // Content stream 1: page objects 15-17.
1076 // Content stream 2: page object 18.
1077 ASSERT_EQ(19, FPDFPage_CountObjects(page));
1078
1079 // Loop backwards because objects will being removed, which shifts the indexes
1080 // after the removed position.
1081 for (int i = 18; i >= 0; i--) {
1082 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1083 ASSERT_TRUE(page_object);
1084 CPDF_PageObject* cpdf_page_object =
1085 CPDFPageObjectFromFPDFPageObject(page_object);
1086
1087 // Empty content stream 1.
1088 if (cpdf_page_object->GetContentStream() == 1) {
1089 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
1090 FPDFPageObj_Destroy(page_object);
1091 }
1092 }
1093
1094 // Content stream 0: page objects 0-14.
1095 // Content stream 2: page object 15.
1096 ASSERT_EQ(16, FPDFPage_CountObjects(page));
1097 for (int i = 0; i < 16; i++) {
1098 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1099 ASSERT_TRUE(page_object);
1100 CPDF_PageObject* cpdf_page_object =
1101 CPDFPageObjectFromFPDFPageObject(page_object);
1102 if (i < 15)
1103 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1104 else
1105 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
1106 }
1107
1108 // Generate contents should remove the empty stream and update the page
1109 // objects' contents stream indexes.
1110 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1111
1112 // Content stream 0: page objects 0-14.
1113 // Content stream 1: page object 15.
1114 ASSERT_EQ(16, FPDFPage_CountObjects(page));
1115 for (int i = 0; i < 16; i++) {
1116 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1117 ASSERT_TRUE(page_object);
1118 CPDF_PageObject* cpdf_page_object =
1119 CPDFPageObjectFromFPDFPageObject(page_object);
1120 if (i < 15)
1121 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1122 else
1123 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1124 }
1125
1126#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1127 const char kStream1RemovedMD5[] = "d2e21fbd5a6de563f619feeeb6163331";
1128#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1129 const char kStream1RemovedMD5[] = "b4140f203523e38793283a5943d8075b";
1130#else
1131 const char kStream1RemovedMD5[] = "e86a3efc160ede6cfcb1f59bcacf1105";
1132#endif
1133 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001134 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001135 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
1136 }
1137
1138 // Save the file
1139 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1140 UnloadPage(page);
1141
1142 // Re-open the file and check the page object count is still 16, and that
1143 // content stream 1 was removed.
Lei Zhang0b494052019-01-31 21:41:15 +00001144 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001145 FPDF_PAGE saved_page = LoadSavedPage(0);
1146
1147 // Content stream 0: page objects 0-14.
1148 // Content stream 1: page object 15.
1149 EXPECT_EQ(16, FPDFPage_CountObjects(saved_page));
1150 for (int i = 0; i < 16; i++) {
1151 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
1152 ASSERT_TRUE(page_object);
1153 CPDF_PageObject* cpdf_page_object =
1154 CPDFPageObjectFromFPDFPageObject(page_object);
1155 if (i < 15)
1156 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
1157 else
1158 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
1159 }
1160
1161 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001162 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001163 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
1164 }
1165
1166 CloseSavedPage(saved_page);
1167 CloseSavedDocument();
1168}
1169
Lei Zhangab41f252018-12-23 03:10:50 +00001170TEST_F(FPDFEditEmbedderTest, RemoveAllFromSingleStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001171 // Load document with a single stream.
1172 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1173 FPDF_PAGE page = LoadPage(0);
1174 ASSERT_TRUE(page);
1175
1176 // Content stream 0: page objects 0-1.
1177 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1178
1179 // Loop backwards because objects will being removed, which shifts the indexes
1180 // after the removed position.
1181 for (int i = 1; i >= 0; i--) {
1182 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1183 ASSERT_TRUE(page_object);
1184 CPDF_PageObject* cpdf_page_object =
1185 CPDFPageObjectFromFPDFPageObject(page_object);
1186 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1187 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1188 FPDFPageObj_Destroy(page_object);
1189 }
1190
1191 // No more objects in the stream
1192 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1193
1194 // Generate contents should remove the empty stream and update the page
1195 // objects' contents stream indexes.
1196 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1197
1198 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1199
1200 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1201 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001202 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001203 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1204 }
1205
1206 // Save the file
1207 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1208 UnloadPage(page);
1209
1210 // Re-open the file and check the page object count is still 0.
Lei Zhang0b494052019-01-31 21:41:15 +00001211 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001212 FPDF_PAGE saved_page = LoadSavedPage(0);
1213
1214 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1215 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001216 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001217 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1218 }
1219
1220 CloseSavedPage(saved_page);
1221 CloseSavedDocument();
1222}
1223
Lei Zhangab41f252018-12-23 03:10:50 +00001224TEST_F(FPDFEditEmbedderTest, RemoveFirstFromSingleStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001225 // Load document with a single stream.
1226 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1227 FPDF_PAGE page = LoadPage(0);
1228 ASSERT_TRUE(page);
1229
1230 // Content stream 0: page objects 0-1.
1231 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1232
1233 // Remove first object.
1234 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
1235 ASSERT_TRUE(page_object);
1236 CPDF_PageObject* cpdf_page_object =
1237 CPDFPageObjectFromFPDFPageObject(page_object);
1238 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1239 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1240 FPDFPageObj_Destroy(page_object);
1241
1242 // One object left in the stream.
1243 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1244 page_object = FPDFPage_GetObject(page, 0);
1245 ASSERT_TRUE(page_object);
1246 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1247 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1248
1249 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1250
1251 // Still one object left in the stream.
1252 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1253 page_object = FPDFPage_GetObject(page, 0);
1254 ASSERT_TRUE(page_object);
1255 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1256 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1257
1258#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1259 const char kFirstRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
1260#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001261 const char kFirstRemovedMD5[] = "aae6c5334721f90ec30d3d59f4ef7deb";
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001262#else
1263 const char kFirstRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
1264#endif
1265 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001266 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001267 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1268 }
1269
1270 // Save the file
1271 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1272 UnloadPage(page);
1273
1274 // Re-open the file and check the page object count is still 0.
Lei Zhang0b494052019-01-31 21:41:15 +00001275 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001276 FPDF_PAGE saved_page = LoadSavedPage(0);
1277
1278 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1279 page_object = FPDFPage_GetObject(saved_page, 0);
1280 ASSERT_TRUE(page_object);
1281 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1282 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1283 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001284 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001285 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1286 }
1287
1288 CloseSavedPage(saved_page);
1289 CloseSavedDocument();
1290}
1291
Lei Zhangab41f252018-12-23 03:10:50 +00001292TEST_F(FPDFEditEmbedderTest, RemoveLastFromSingleStream) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001293 // Load document with a single stream.
1294 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1295 FPDF_PAGE page = LoadPage(0);
1296 ASSERT_TRUE(page);
1297
1298 // Content stream 0: page objects 0-1.
1299 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1300
1301 // Remove last object
1302 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 1);
1303 ASSERT_TRUE(page_object);
1304 CPDF_PageObject* cpdf_page_object =
1305 CPDFPageObjectFromFPDFPageObject(page_object);
1306 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1307 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1308 FPDFPageObj_Destroy(page_object);
1309
1310 // One object left in the stream.
1311 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1312 page_object = FPDFPage_GetObject(page, 0);
1313 ASSERT_TRUE(page_object);
1314 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1315 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1316
1317 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1318
1319 // Still one object left in the stream.
1320 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1321 page_object = FPDFPage_GetObject(page, 0);
1322 ASSERT_TRUE(page_object);
1323 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1324 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1325
1326#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1327 const char kLastRemovedMD5[] = "f8fbd14a048b9e2ea8e5f059f22a910e";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001328#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1329 const char kLastRemovedMD5[] = "93db13099042bafefb3c22a165bad684";
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001330#else
1331 const char kLastRemovedMD5[] = "93dcc09055f87a2792c8e3065af99a1b";
1332#endif
1333 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001334 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001335 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1336 }
1337
1338 // Save the file
1339 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1340 UnloadPage(page);
1341
1342 // Re-open the file and check the page object count is still 0.
Lei Zhang0b494052019-01-31 21:41:15 +00001343 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001344 FPDF_PAGE saved_page = LoadSavedPage(0);
1345
1346 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1347 page_object = FPDFPage_GetObject(saved_page, 0);
1348 ASSERT_TRUE(page_object);
1349 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1350 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1351 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001352 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001353 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1354 }
1355
1356 CloseSavedPage(saved_page);
1357 CloseSavedDocument();
1358}
1359
Lei Zhangab41f252018-12-23 03:10:50 +00001360TEST_F(FPDFEditEmbedderTest, RemoveAllFromMultipleStreams) {
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001361 // Load document with some text.
1362 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
1363 FPDF_PAGE page = LoadPage(0);
1364 ASSERT_TRUE(page);
1365
1366 // Content stream 0: page objects 0-1.
1367 // Content stream 1: page object 2.
1368 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1369
1370 // Loop backwards because objects will being removed, which shifts the indexes
1371 // after the removed position.
1372 for (int i = 2; i >= 0; i--) {
1373 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1374 ASSERT_TRUE(page_object);
1375 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1376 FPDFPageObj_Destroy(page_object);
1377 }
1378
1379 // No more objects in the page.
1380 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1381
1382 // Generate contents should remove the empty streams and update the page
1383 // objects' contents stream indexes.
1384 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1385
1386 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1387
1388 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1389 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001390 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001391 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1392 }
1393
1394 // Save the file
1395 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1396 UnloadPage(page);
1397
1398 // Re-open the file and check the page object count is still 0.
Lei Zhang0b494052019-01-31 21:41:15 +00001399 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001400 FPDF_PAGE saved_page = LoadSavedPage(0);
1401
1402 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1403 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001404 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +00001405 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1406 }
1407
1408 CloseSavedPage(saved_page);
1409 CloseSavedDocument();
1410}
1411
Lei Zhangab41f252018-12-23 03:10:50 +00001412TEST_F(FPDFEditEmbedderTest, InsertPageObjectAndSave) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001413 // Load document with some text.
1414 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1415 FPDF_PAGE page = LoadPage(0);
1416 ASSERT_TRUE(page);
1417
1418 // Add a red rectangle.
1419 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1420 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1421 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1422 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1423 FPDFPage_InsertObject(page, red_rect);
1424
1425 // Verify the red rectangle was added.
1426 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1427
1428 // Save the file
1429 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1430 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1431 UnloadPage(page);
1432
1433 // Re-open the file and check the page object count is still 3.
Lei Zhang0b494052019-01-31 21:41:15 +00001434 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001435 FPDF_PAGE saved_page = LoadSavedPage(0);
1436 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1437 CloseSavedPage(saved_page);
1438 CloseSavedDocument();
1439}
1440
Lei Zhangab41f252018-12-23 03:10:50 +00001441TEST_F(FPDFEditEmbedderTest, InsertPageObjectEditAndSave) {
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001442 // Load document with some text.
1443 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1444 FPDF_PAGE page = LoadPage(0);
1445 ASSERT_TRUE(page);
1446
1447 // Add a red rectangle.
1448 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1449 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1450 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 100, 100, 255));
1451 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1452 FPDFPage_InsertObject(page, red_rect);
1453
1454 // Verify the red rectangle was added.
1455 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1456
1457 // Generate content but change it again
1458 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1459 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1460
1461 // Save the file
1462 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1463 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1464 UnloadPage(page);
1465
1466 // Re-open the file and check the page object count is still 3.
Lei Zhang0b494052019-01-31 21:41:15 +00001467 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001468 FPDF_PAGE saved_page = LoadSavedPage(0);
1469 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1470 CloseSavedPage(saved_page);
1471 CloseSavedDocument();
1472}
1473
Lei Zhangab41f252018-12-23 03:10:50 +00001474TEST_F(FPDFEditEmbedderTest, InsertAndRemoveLargeFile) {
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001475 const int kOriginalObjectCount = 600;
1476
1477 // Load document with many objects.
1478 EXPECT_TRUE(OpenDocument("many_rectangles.pdf"));
1479 FPDF_PAGE page = LoadPage(0);
1480 ASSERT_TRUE(page);
1481 const char kOriginalMD5[] = "b0170c575b65ecb93ebafada0ff0f038";
1482 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001483 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001484 CompareBitmap(page_bitmap.get(), 200, 300, kOriginalMD5);
1485 }
1486
1487 // Add a black rectangle.
1488 ASSERT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(page));
1489 FPDF_PAGEOBJECT black_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1490 EXPECT_TRUE(FPDFPath_SetFillColor(black_rect, 0, 0, 0, 255));
1491 EXPECT_TRUE(FPDFPath_SetDrawMode(black_rect, FPDF_FILLMODE_ALTERNATE, 0));
1492 FPDFPage_InsertObject(page, black_rect);
1493
1494 // Verify the black rectangle was added.
1495 ASSERT_EQ(kOriginalObjectCount + 1, FPDFPage_CountObjects(page));
1496 const char kPlusRectangleMD5[] = "6b9396ab570754b32b04ca629e902f77";
1497 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001498 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001499 CompareBitmap(page_bitmap.get(), 200, 300, kPlusRectangleMD5);
1500 }
1501
1502 // Save the file.
1503 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1504 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1505 UnloadPage(page);
1506
1507 // Re-open the file and check the rectangle added is still there.
Lei Zhang0b494052019-01-31 21:41:15 +00001508 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001509 FPDF_PAGE saved_page = LoadSavedPage(0);
1510 EXPECT_EQ(kOriginalObjectCount + 1, FPDFPage_CountObjects(saved_page));
1511 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001512 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001513 CompareBitmap(page_bitmap.get(), 200, 300, kPlusRectangleMD5);
1514 }
1515
1516 // Remove the added rectangle.
1517 FPDF_PAGEOBJECT added_object =
1518 FPDFPage_GetObject(saved_page, kOriginalObjectCount);
1519 EXPECT_TRUE(FPDFPage_RemoveObject(saved_page, added_object));
1520 FPDFPageObj_Destroy(added_object);
1521 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001522 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001523 CompareBitmap(page_bitmap.get(), 200, 300, kOriginalMD5);
1524 }
1525 EXPECT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(saved_page));
1526
1527 // Save the file again.
1528 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
1529 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
1530
1531 CloseSavedPage(saved_page);
1532 CloseSavedDocument();
1533
1534 // Re-open the file (again) and check the black rectangle was removed and the
1535 // rest is intact.
Lei Zhang0b494052019-01-31 21:41:15 +00001536 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001537 saved_page = LoadSavedPage(0);
1538 EXPECT_EQ(kOriginalObjectCount, FPDFPage_CountObjects(saved_page));
1539 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001540 ScopedFPDFBitmap page_bitmap = RenderPage(saved_page);
Henrique Nakashima657a1aa2018-09-12 16:19:22 +00001541 CompareBitmap(page_bitmap.get(), 200, 300, kOriginalMD5);
1542 }
1543
1544 CloseSavedPage(saved_page);
1545 CloseSavedDocument();
1546}
1547
Lei Zhangab41f252018-12-23 03:10:50 +00001548TEST_F(FPDFEditEmbedderTest, AddAndRemovePaths) {
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001549 // Start with a blank page.
1550 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1551 ASSERT_TRUE(page);
1552
1553 // Render the blank page and verify it's a blank bitmap.
1554 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
1555 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001556 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001557 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1558 }
1559 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1560
1561 // Add a red rectangle.
1562 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1563 ASSERT_TRUE(red_rect);
1564 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1565 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1566 FPDFPage_InsertObject(page, red_rect);
1567 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
1568 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001569 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001570 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
1571 }
1572 EXPECT_EQ(1, FPDFPage_CountObjects(page));
1573
1574 // Remove rectangle and verify it does not render anymore and the bitmap is
1575 // back to a blank one.
1576 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
1577 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001578 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001579 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1580 }
1581 EXPECT_EQ(0, FPDFPage_CountObjects(page));
1582
1583 // Trying to remove an object not in the page should return false.
1584 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
1585
1586 FPDF_ClosePage(page);
1587 FPDFPageObj_Destroy(red_rect);
1588}
1589
Lei Zhangab41f252018-12-23 03:10:50 +00001590TEST_F(FPDFEditEmbedderTest, PathsPoints) {
Miklos Vajna12abfd02017-09-15 07:49:03 +02001591 CreateNewDocument();
1592 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
1593 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +02001594 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001595
1596 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +02001597 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001598
Miklos Vajna36eed872017-09-20 22:52:43 +02001599 // FPDFPath_GetPathSegment() with a non-path.
1600 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
1601 // FPDFPath_GetPathSegment() with a NULL path.
1602 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
1603 float x;
1604 float y;
1605 // FPDFPathSegment_GetPoint() with a NULL segment.
1606 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
1607
1608 // FPDFPathSegment_GetType() with a NULL segment.
1609 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
1610
1611 // FPDFPathSegment_GetClose() with a NULL segment.
1612 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
1613
Miklos Vajna12abfd02017-09-15 07:49:03 +02001614 FPDFPageObj_Destroy(img);
1615}
1616
Lei Zhangab41f252018-12-23 03:10:50 +00001617TEST_F(FPDFEditEmbedderTest, PathOnTopOfText) {
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001618 // Load document with some text
1619 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1620 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001621 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001622
1623 // Add an opaque rectangle on top of some of the text.
1624 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1625 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1626 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1627 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001628
1629 // Add a transparent triangle on top of other part of the text.
1630 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
1631 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
1632 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
1633 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
1634 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
1635 EXPECT_TRUE(FPDFPath_Close(black_path));
1636 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001637
1638 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001639 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001640#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -07001641 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001642#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1643 const char md5[] = "74dd9c393b8b2578d2b7feb032b7daad";
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001644#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001645 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001646#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001647 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001648 UnloadPage(page);
1649}
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001650
Lei Zhangab41f252018-12-23 03:10:50 +00001651TEST_F(FPDFEditEmbedderTest, EditOverExistingContent) {
wileyryae858aa42017-05-31 14:49:05 -05001652 // Load document with existing content
1653 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
1654 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001655 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -05001656
1657 // Add a transparent rectangle on top of the existing content
1658 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
1659 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
1660 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
1661 FPDFPage_InsertObject(page, red_rect2);
1662
1663 // Add an opaque rectangle on top of the existing content
1664 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
1665 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1666 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1667 FPDFPage_InsertObject(page, red_rect);
1668
Tom Sepeze08d2b12018-04-25 18:49:32 +00001669 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001670 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001671 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1672
1673 // Now save the result, closing the page and document
1674 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -04001675 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -05001676
Lei Zhang0b494052019-01-31 21:41:15 +00001677 ASSERT_TRUE(OpenSavedDocument());
Lei Zhang107fa7b2018-02-09 21:48:15 +00001678 FPDF_PAGE saved_page = LoadSavedPage(0);
1679 VerifySavedRendering(saved_page, 612, 792,
1680 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001681
1682 ClearString();
1683 // Add another opaque rectangle on top of the existing content
1684 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
1685 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
1686 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001687 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -05001688
1689 // Add another transparent rectangle on top of existing content
1690 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
1691 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
1692 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001693 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001694 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
1695 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001696 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001697 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
1698 }
Lei Zhang107fa7b2018-02-09 21:48:15 +00001699 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -05001700
1701 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +00001702 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001703
Lei Zhang107fa7b2018-02-09 21:48:15 +00001704 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001705 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -05001706
1707 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +00001708 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -05001709}
1710
Lei Zhangab41f252018-12-23 03:10:50 +00001711TEST_F(FPDFEditEmbedderTest, AddStrokedPaths) {
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001712 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001713 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001714
1715 // Add a large stroked rectangle (fill color should not affect it).
1716 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
1717 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
1718 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
1719 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
Miklos Vajna366df7f2018-05-22 14:27:29 +00001720
1721 float width = 0;
1722 EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width));
1723 EXPECT_EQ(15.0f, width);
1724
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001725 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
1726 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001727 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001728 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001729 CompareBitmap(page_bitmap.get(), 612, 792,
1730 "64bd31f862a89e0a9e505a5af6efd506");
1731 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001732
1733 // Add crossed-checkmark
1734 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
1735 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
1736 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
1737 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
1738 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
1739 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
1740 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
1741 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1742 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001743 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001744 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001745 CompareBitmap(page_bitmap.get(), 612, 792,
1746 "4b6f3b9d25c4e194821217d5016c3724");
1747 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001748
1749 // Add stroked and filled oval-ish path.
1750 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
1751 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
1752 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
1753 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
1754 EXPECT_TRUE(FPDFPath_Close(path));
1755 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
1756 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
1757 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
1758 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
1759 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001760 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001761 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001762 CompareBitmap(page_bitmap.get(), 612, 792,
1763 "ff3e6a22326754944cc6e56609acd73b");
1764 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001765 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001766}
Nicolas Pena49058402017-02-14 18:26:20 -05001767
Nicolas Pena4c48b102018-06-13 18:23:46 +00001768// Tests adding text from standard font using FPDFPageObj_NewTextObj.
Lei Zhangab41f252018-12-23 03:10:50 +00001769TEST_F(FPDFEditEmbedderTest, AddStandardFontText) {
Nicolas Pena49058402017-02-14 18:26:20 -05001770 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001771 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -05001772
1773 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001774 FPDF_PAGEOBJECT text_object1 =
1775 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1776 EXPECT_TRUE(text_object1);
1777 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
1778 GetFPDFWideString(L"I'm at the bottom of the page");
1779 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
1780 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
1781 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001782 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001783 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001784#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001785 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001786#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1787 const char md5[] = "08d1ff3e5a42801bee6077fd366bef00";
Nicolas Pena49058402017-02-14 18:26:20 -05001788#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001789 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001790#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001791 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1792 }
Nicolas Pena49058402017-02-14 18:26:20 -05001793
1794 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -04001795 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001796 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001797 EXPECT_TRUE(text_object2);
1798 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1799 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
1800 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1801 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
1802 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001803 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001804 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001805#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +00001806 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -04001807#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001808 const char md5_2[] = "3755dd35abd4c605755369401ee85b2d";
Nicolas Pena49058402017-02-14 18:26:20 -05001809#else
Dan Sinclair971a6742018-03-28 19:23:25 +00001810 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001811#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001812 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001813 }
Nicolas Pena49058402017-02-14 18:26:20 -05001814
1815 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -04001816 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001817 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001818 EXPECT_TRUE(text_object3);
1819 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
1820 GetFPDFWideString(L"Can you read me? <:)>");
1821 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
1822 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
1823 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001824 {
Lei Zhang30ff2532019-01-31 21:37:55 +00001825 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001826#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001827 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -04001828#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001829 const char md5_3[] = "aba523a8110d01ed9bd7b7781ff74045";
Nicolas Pena49058402017-02-14 18:26:20 -05001830#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001831 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001832#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001833 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
1834 }
Nicolas Pena49058402017-02-14 18:26:20 -05001835
Miklos Vajnac765d2a2018-06-19 15:45:42 +00001836 double matrix_a = 0;
1837 double matrix_b = 0;
1838 double matrix_c = 0;
1839 double matrix_d = 0;
1840 double matrix_e = 0;
1841 double matrix_f = 0;
1842 EXPECT_FALSE(FPDFText_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1843 &matrix_d, &matrix_e, &matrix_f));
1844 EXPECT_TRUE(FPDFText_GetMatrix(text_object3, &matrix_a, &matrix_b, &matrix_c,
1845 &matrix_d, &matrix_e, &matrix_f));
1846 EXPECT_EQ(1., matrix_a);
1847 EXPECT_EQ(1.5, matrix_b);
1848 EXPECT_EQ(2., matrix_c);
1849 EXPECT_EQ(0.5, matrix_d);
1850 EXPECT_EQ(200., matrix_e);
1851 EXPECT_EQ(200., matrix_f);
1852
Miklos Vajna8625d3b2018-06-26 15:12:48 +00001853 EXPECT_EQ(0, FPDFTextObj_GetFontSize(nullptr));
1854 EXPECT_EQ(20, FPDFTextObj_GetFontSize(text_object3));
1855
Nicolas Pena49058402017-02-14 18:26:20 -05001856 // TODO(npm): Why are there issues with text rotated by 90 degrees?
1857 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
1858 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -05001859}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001860
Lei Zhangab41f252018-12-23 03:10:50 +00001861TEST_F(FPDFEditEmbedderTest, TestGetTextRenderMode) {
Miklos Vajna1448cc12018-07-03 13:52:33 +00001862 EXPECT_TRUE(OpenDocument("text_render_mode.pdf"));
1863 FPDF_PAGE page = LoadPage(0);
1864 ASSERT_TRUE(page);
1865 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1866
1867 ASSERT_EQ(-1, FPDFText_GetTextRenderMode(nullptr));
1868
1869 FPDF_PAGEOBJECT fill = FPDFPage_GetObject(page, 0);
1870 ASSERT_EQ(FPDF_TEXTRENDERMODE_FILL, FPDFText_GetTextRenderMode(fill));
1871
1872 FPDF_PAGEOBJECT stroke = FPDFPage_GetObject(page, 1);
1873 ASSERT_EQ(FPDF_TEXTRENDERMODE_STROKE, FPDFText_GetTextRenderMode(stroke));
1874
1875 UnloadPage(page);
1876}
1877
Lei Zhangab41f252018-12-23 03:10:50 +00001878TEST_F(FPDFEditEmbedderTest, TestGetTextFontName) {
Miklos Vajna53d4f0a2018-08-01 01:28:49 +00001879 EXPECT_TRUE(OpenDocument("text_font.pdf"));
1880 FPDF_PAGE page = LoadPage(0);
1881 ASSERT_TRUE(page);
1882 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1883
1884 // FPDFTextObj_GetFontName() positive testing.
1885 FPDF_PAGEOBJECT text = FPDFPage_GetObject(page, 0);
1886 unsigned long size = FPDFTextObj_GetFontName(text, nullptr, 0);
1887 const char kExpectedFontName[] = "Liberation Serif";
1888 ASSERT_EQ(sizeof(kExpectedFontName), size);
1889 std::vector<char> font_name(size);
1890 ASSERT_EQ(size, FPDFTextObj_GetFontName(text, font_name.data(), size));
1891 ASSERT_STREQ(kExpectedFontName, font_name.data());
1892
1893 // FPDFTextObj_GetFontName() negative testing.
1894 ASSERT_EQ(0U, FPDFTextObj_GetFontName(nullptr, nullptr, 0));
1895
1896 font_name.resize(2);
1897 font_name[0] = 'x';
1898 font_name[1] = '\0';
1899 size = FPDFTextObj_GetFontName(text, font_name.data(), font_name.size());
1900 ASSERT_EQ(sizeof(kExpectedFontName), size);
1901 ASSERT_EQ(std::string("x"), std::string(font_name.data()));
1902
1903 UnloadPage(page);
1904}
1905
Lei Zhangab41f252018-12-23 03:10:50 +00001906TEST_F(FPDFEditEmbedderTest, TestFormGetObjects) {
Miklos Vajnab66077d2018-07-11 13:25:02 +00001907 EXPECT_TRUE(OpenDocument("form_object.pdf"));
1908 FPDF_PAGE page = LoadPage(0);
1909 ASSERT_TRUE(page);
1910 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1911
1912 FPDF_PAGEOBJECT form = FPDFPage_GetObject(page, 0);
1913 EXPECT_EQ(FPDF_PAGEOBJ_FORM, FPDFPageObj_GetType(form));
1914 ASSERT_EQ(-1, FPDFFormObj_CountObjects(nullptr));
1915 ASSERT_EQ(2, FPDFFormObj_CountObjects(form));
1916
Miklos Vajna1d273f12018-07-16 19:20:36 +00001917 // FPDFFormObj_GetObject() positive testing.
1918 FPDF_PAGEOBJECT text1 = FPDFFormObj_GetObject(form, 0);
1919 ASSERT_TRUE(text1);
1920 float left = 0;
1921 float bottom = 0;
1922 float right = 0;
1923 float top = 0;
1924 ASSERT_TRUE(FPDFPageObj_GetBounds(text1, &left, &bottom, &right, &top));
1925 ASSERT_EQ(271, static_cast<int>(top));
1926
1927 FPDF_PAGEOBJECT text2 = FPDFFormObj_GetObject(form, 1);
1928 ASSERT_TRUE(text2);
1929 ASSERT_TRUE(FPDFPageObj_GetBounds(text2, &left, &bottom, &right, &top));
1930 ASSERT_EQ(221, static_cast<int>(top));
1931
1932 // FPDFFormObj_GetObject() negative testing.
1933 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(nullptr, 0));
1934 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(form, -1));
1935 ASSERT_EQ(nullptr, FPDFFormObj_GetObject(form, 2));
1936
Miklos Vajna46b43732018-08-14 19:15:43 +00001937 // Reset the form object matrix to identity.
1938 auto* pPageObj = CPDFPageObjectFromFPDFPageObject(form);
1939 CPDF_FormObject* pFormObj = pPageObj->AsForm();
1940 pFormObj->Transform(pFormObj->form_matrix().GetInverse());
1941
1942 // FPDFFormObj_GetMatrix() positive testing.
1943 static constexpr float kFloats[6] = {1.0, 1.5, 2.0, 2.5, 100.0, 200.0};
1944 CFX_Matrix matrix(kFloats);
1945 pFormObj->Transform(matrix);
1946
1947 double matrix_a = 0;
1948 double matrix_b = 0;
1949 double matrix_c = 0;
1950 double matrix_d = 0;
1951 double matrix_e = 0;
1952 double matrix_f = 0;
1953 EXPECT_TRUE(FPDFFormObj_GetMatrix(form, &matrix_a, &matrix_b, &matrix_c,
1954 &matrix_d, &matrix_e, &matrix_f));
1955 EXPECT_DOUBLE_EQ(kFloats[0], matrix_a);
1956 EXPECT_DOUBLE_EQ(kFloats[1], matrix_b);
1957 EXPECT_DOUBLE_EQ(kFloats[2], matrix_c);
1958 EXPECT_DOUBLE_EQ(kFloats[3], matrix_d);
1959 EXPECT_DOUBLE_EQ(kFloats[4], matrix_e);
1960 EXPECT_DOUBLE_EQ(kFloats[5], matrix_f);
1961
1962 // FPDFFormObj_GetMatrix() negative testing.
1963 EXPECT_FALSE(FPDFFormObj_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1964 &matrix_d, &matrix_e, &matrix_f));
1965 EXPECT_FALSE(FPDFFormObj_GetMatrix(form, nullptr, nullptr, nullptr, nullptr,
1966 nullptr, nullptr));
1967 EXPECT_FALSE(FPDFFormObj_GetMatrix(nullptr, nullptr, nullptr, nullptr,
1968 nullptr, nullptr, nullptr));
1969
Miklos Vajnab66077d2018-07-11 13:25:02 +00001970 UnloadPage(page);
1971}
1972
Nicolas Pena4c48b102018-06-13 18:23:46 +00001973// Tests adding text from standard font using FPDFText_LoadStandardFont.
Lei Zhangab41f252018-12-23 03:10:50 +00001974TEST_F(FPDFEditEmbedderTest, AddStandardFontText2) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00001975 // Start with a blank page
1976 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
1977
1978 // Load a standard font.
1979 FPDF_FONT font = FPDFText_LoadStandardFont(document(), "Helvetica");
1980 ASSERT_TRUE(font);
1981
1982 // Add some text to the page.
1983 FPDF_PAGEOBJECT text_object =
1984 FPDFPageObj_CreateTextObj(document(), font, 12.0f);
1985 EXPECT_TRUE(text_object);
1986 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1987 GetFPDFWideString(L"I'm at the bottom of the page");
1988 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1989 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20);
1990 FPDFPage_InsertObject(page.get(), text_object);
Lei Zhang30ff2532019-01-31 21:37:55 +00001991 ScopedFPDFBitmap page_bitmap = RenderPage(page.get());
Nicolas Pena4c48b102018-06-13 18:23:46 +00001992#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1993 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00001994#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
1995 const char md5[] = "08d1ff3e5a42801bee6077fd366bef00";
Nicolas Pena4c48b102018-06-13 18:23:46 +00001996#else
1997 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
1998#endif
1999 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2000}
2001
Lei Zhangab41f252018-12-23 03:10:50 +00002002TEST_F(FPDFEditEmbedderTest, LoadStandardFonts) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002003 CreateNewDocument();
Lei Zhangd72fd582018-07-27 19:37:27 +00002004 static constexpr const char* standard_font_names[] = {
2005 "Arial",
2006 "Arial-Bold",
2007 "Arial-BoldItalic",
2008 "Arial-Italic",
2009 "Courier",
2010 "Courier-BoldOblique",
2011 "Courier-Oblique",
2012 "Courier-Bold",
2013 "CourierNew",
2014 "CourierNew-Bold",
2015 "CourierNew-BoldItalic",
2016 "CourierNew-Italic",
2017 "Helvetica",
2018 "Helvetica-Bold",
2019 "Helvetica-BoldOblique",
2020 "Helvetica-Oblique",
2021 "Symbol",
2022 "TimesNewRoman",
2023 "TimesNewRoman-Bold",
2024 "TimesNewRoman-BoldItalic",
2025 "TimesNewRoman-Italic",
2026 "ZapfDingbats"};
2027 for (const char* font_name : standard_font_names) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002028 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
2029 EXPECT_TRUE(font) << font_name << " should be considered a standard font.";
2030 }
Lei Zhangd72fd582018-07-27 19:37:27 +00002031 static constexpr const char* not_standard_font_names[] = {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002032 "Abcdefg", "ArialB", "Arial-Style",
2033 "Font Name", "FontArial", "NotAStandardFontName",
2034 "TestFontName", "Quack", "Symbol-Italic",
2035 "Zapf"};
Lei Zhangd72fd582018-07-27 19:37:27 +00002036 for (const char* font_name : not_standard_font_names) {
Nicolas Pena4c48b102018-06-13 18:23:46 +00002037 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
2038 EXPECT_FALSE(font) << font_name
2039 << " should not be considered a standard font.";
2040 }
2041}
2042
Lei Zhangab41f252018-12-23 03:10:50 +00002043TEST_F(FPDFEditEmbedderTest, GraphicsData) {
Nicolas Pena603a31d2017-06-14 11:41:18 -04002044 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +00002045 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -04002046
2047 // Create a rect with nontrivial graphics
2048 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
2049 FPDFPageObj_SetBlendMode(rect1, "Color");
2050 FPDFPage_InsertObject(page.get(), rect1);
2051 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
2052
2053 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +00002054 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
2055 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -04002056 ASSERT_TRUE(graphics_dict);
Lei Zhangf40380f2018-10-12 18:31:51 +00002057 EXPECT_EQ(2u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002058
2059 // Add a text object causing no change to the graphics dictionary
2060 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
2061 // Only alpha, the last component, matters for the graphics dictionary. And
2062 // the default value is 255.
2063 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
2064 FPDFPage_InsertObject(page.get(), text1);
2065 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002066 EXPECT_EQ(2u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002067
2068 // Add a text object increasing the size of the graphics dictionary
2069 FPDF_PAGEOBJECT text2 =
2070 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
2071 FPDFPage_InsertObject(page.get(), text2);
2072 FPDFPageObj_SetBlendMode(text2, "Darken");
2073 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
2074 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002075 EXPECT_EQ(3u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002076
2077 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -04002078 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -04002079 FPDFPageObj_SetBlendMode(path, "Darken");
2080 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
2081 FPDFPage_InsertObject(page.get(), path);
2082 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002083 EXPECT_EQ(3u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002084
2085 // Add a rect increasing the size of the graphics dictionary
2086 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
2087 FPDFPageObj_SetBlendMode(rect2, "Darken");
2088 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
2089 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
2090 FPDFPage_InsertObject(page.get(), rect2);
2091 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Lei Zhangf40380f2018-10-12 18:31:51 +00002092 EXPECT_EQ(4u, graphics_dict->size());
Nicolas Pena603a31d2017-06-14 11:41:18 -04002093}
2094
Lei Zhangab41f252018-12-23 03:10:50 +00002095TEST_F(FPDFEditEmbedderTest, DoubleGenerating) {
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002096 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05002097 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002098
2099 // Add a red rectangle with some non-default alpha
2100 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
2101 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
2102 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
2103 FPDFPage_InsertObject(page, rect);
2104 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2105
2106 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +00002107 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
2108 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002109 ASSERT_TRUE(graphics_dict);
Lei Zhangf40380f2018-10-12 18:31:51 +00002110 EXPECT_EQ(2u, graphics_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002111
2112 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +00002113 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002114 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002115 CompareBitmap(page_bitmap.get(), 612, 792,
2116 "5384da3406d62360ffb5cac4476fff1c");
2117 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002118
2119 // Never mind, my new favorite color is blue, increase alpha
2120 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
2121 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhangf40380f2018-10-12 18:31:51 +00002122 EXPECT_EQ(3u, graphics_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002123
2124 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +00002125 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002126 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002127 CompareBitmap(page_bitmap.get(), 612, 792,
2128 "2e51656f5073b0bee611d9cd086aa09c");
2129 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002130
2131 // And now generate, without changes
2132 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhangf40380f2018-10-12 18:31:51 +00002133 EXPECT_EQ(3u, graphics_dict->size());
Lei Zhang107fa7b2018-02-09 21:48:15 +00002134 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002135 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002136 CompareBitmap(page_bitmap.get(), 612, 792,
2137 "2e51656f5073b0bee611d9cd086aa09c");
2138 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002139
2140 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04002141 FPDF_PAGEOBJECT text_object =
2142 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
2143 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2144 GetFPDFWideString(L"Something something #text# something");
2145 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2146 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
2147 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002148 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00002149 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002150 ASSERT_TRUE(font_dict);
Lei Zhangf40380f2018-10-12 18:31:51 +00002151 EXPECT_EQ(1u, font_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002152
2153 // Generate yet again, check dicts are reasonably sized
2154 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhangf40380f2018-10-12 18:31:51 +00002155 EXPECT_EQ(3u, graphics_dict->size());
2156 EXPECT_EQ(1u, font_dict->size());
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002157 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05002158}
Nicolas Penabe90aae2017-02-27 10:41:41 -05002159
Lei Zhangab41f252018-12-23 03:10:50 +00002160TEST_F(FPDFEditEmbedderTest, LoadSimpleType1Font) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002161 CreateNewDocument();
2162 // TODO(npm): use other fonts after disallowing loading any font as any type
2163 const CPDF_Font* stock_font =
2164 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Tom Sepez20c41a52018-08-29 23:53:53 +00002165 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2166 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2167 FPDF_FONT_TYPE1, false));
Nicolas Penab3161852017-05-02 14:12:50 -04002168 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002169 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002170 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002171
Lei Zhang710fa992018-05-25 16:24:48 +00002172 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05002173 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2174 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002175 EXPECT_EQ("TimesNewRomanPS-BoldMT", font_dict->GetStringFor("BaseFont"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05002176 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
2177 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
2178 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05002179 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
2180
Lei Zhangde579ab2018-05-25 21:49:49 +00002181 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05002182 ASSERT_TRUE(widths_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002183 ASSERT_EQ(224u, widths_array->size());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002184 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05002185 EXPECT_EQ(569, widths_array->GetNumberAt(11));
2186 EXPECT_EQ(500, widths_array->GetNumberAt(223));
Tom Sepez20c41a52018-08-29 23:53:53 +00002187 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002188}
Nicolas Penabe90aae2017-02-27 10:41:41 -05002189
Lei Zhangab41f252018-12-23 03:10:50 +00002190TEST_F(FPDFEditEmbedderTest, LoadSimpleTrueTypeFont) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002191 CreateNewDocument();
2192 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Tom Sepez20c41a52018-08-29 23:53:53 +00002193 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2194 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2195 FPDF_FONT_TRUETYPE, false));
Nicolas Penab3161852017-05-02 14:12:50 -04002196 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002197 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002198 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05002199
Lei Zhang710fa992018-05-25 16:24:48 +00002200 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002201 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2202 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002203 EXPECT_EQ("CourierNewPSMT", font_dict->GetStringFor("BaseFont"));
Nicolas Penad03ca422017-03-06 13:54:33 -05002204 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
2205 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
2206 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
2207 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05002208
Lei Zhangde579ab2018-05-25 21:49:49 +00002209 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05002210 ASSERT_TRUE(widths_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002211 ASSERT_EQ(224u, widths_array->size());
Nicolas Penad03ca422017-03-06 13:54:33 -05002212 EXPECT_EQ(600, widths_array->GetNumberAt(33));
2213 EXPECT_EQ(600, widths_array->GetNumberAt(74));
2214 EXPECT_EQ(600, widths_array->GetNumberAt(223));
Tom Sepez20c41a52018-08-29 23:53:53 +00002215 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002216}
2217
Lei Zhangab41f252018-12-23 03:10:50 +00002218TEST_F(FPDFEditEmbedderTest, LoadCIDType0Font) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002219 CreateNewDocument();
2220 const CPDF_Font* stock_font =
2221 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Tom Sepez20c41a52018-08-29 23:53:53 +00002222 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2223 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2224 FPDF_FONT_TYPE1, 1));
Nicolas Penab3161852017-05-02 14:12:50 -04002225 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002226 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002227 EXPECT_TRUE(typed_font->IsCIDFont());
2228
2229 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00002230 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002231 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2232 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002233 EXPECT_EQ("TimesNewRomanPSMT-Identity-H", font_dict->GetStringFor("BaseFont"));
Nicolas Penad03ca422017-03-06 13:54:33 -05002234 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00002235 const CPDF_Array* descendant_array =
2236 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05002237 ASSERT_TRUE(descendant_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002238 EXPECT_EQ(1u, descendant_array->size());
Nicolas Penad03ca422017-03-06 13:54:33 -05002239
2240 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00002241 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05002242 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
2243 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002244 EXPECT_EQ("TimesNewRomanPSMT", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00002245 const CPDF_Dictionary* cidinfo_dict =
2246 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05002247 ASSERT_TRUE(cidinfo_dict);
Lei Zhang9c950b12019-01-16 19:06:37 +00002248 const CPDF_Object* registry = cidinfo_dict->GetObjectFor("Registry");
2249 ASSERT_TRUE(registry);
KDr28da0e1b2019-01-17 03:44:29 +00002250 EXPECT_EQ(CPDF_Object::kString, registry->GetType());
Lei Zhang9c950b12019-01-16 19:06:37 +00002251 EXPECT_EQ("Adobe", registry->GetString());
2252 const CPDF_Object* ordering = cidinfo_dict->GetObjectFor("Ordering");
2253 ASSERT_TRUE(ordering);
KDr28da0e1b2019-01-17 03:44:29 +00002254 EXPECT_EQ(CPDF_Object::kString, ordering->GetType());
Lei Zhang9c950b12019-01-16 19:06:37 +00002255 EXPECT_EQ("Identity", ordering->GetString());
Nicolas Penad03ca422017-03-06 13:54:33 -05002256 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
Tom Sepez20c41a52018-08-29 23:53:53 +00002257 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002258
2259 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00002260 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05002261 ASSERT_TRUE(widths_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002262 EXPECT_GT(widths_array->size(), 1u);
Nicolas Penad03ca422017-03-06 13:54:33 -05002263 CheckCompositeFontWidths(widths_array, typed_font);
2264}
2265
Lei Zhangab41f252018-12-23 03:10:50 +00002266TEST_F(FPDFEditEmbedderTest, LoadCIDType2Font) {
Nicolas Penad03ca422017-03-06 13:54:33 -05002267 CreateNewDocument();
2268 const CPDF_Font* stock_font =
2269 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Tom Sepez20c41a52018-08-29 23:53:53 +00002270 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2271 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2272 FPDF_FONT_TRUETYPE, 1));
Nicolas Penab3161852017-05-02 14:12:50 -04002273 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00002274 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05002275 EXPECT_TRUE(typed_font->IsCIDFont());
2276
2277 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00002278 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05002279 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
2280 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002281 EXPECT_EQ("Arial-ItalicMT", font_dict->GetStringFor("BaseFont"));
Nicolas Penad03ca422017-03-06 13:54:33 -05002282 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00002283 const CPDF_Array* descendant_array =
2284 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05002285 ASSERT_TRUE(descendant_array);
Lei Zhangf40380f2018-10-12 18:31:51 +00002286 EXPECT_EQ(1u, descendant_array->size());
Nicolas Penad03ca422017-03-06 13:54:33 -05002287
2288 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00002289 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05002290 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
2291 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
KDr27a629882019-02-01 01:16:38 +00002292 EXPECT_EQ("Arial-ItalicMT", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00002293 const CPDF_Dictionary* cidinfo_dict =
2294 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05002295 ASSERT_TRUE(cidinfo_dict);
2296 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
2297 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
2298 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
Tom Sepez20c41a52018-08-29 23:53:53 +00002299 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, span);
Nicolas Penad03ca422017-03-06 13:54:33 -05002300
2301 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00002302 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05002303 ASSERT_TRUE(widths_array);
2304 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05002305}
rbpotterce8e51e2017-04-28 12:42:47 -07002306
Lei Zhangab41f252018-12-23 03:10:50 +00002307TEST_F(FPDFEditEmbedderTest, NormalizeNegativeRotation) {
rbpotterce8e51e2017-04-28 12:42:47 -07002308 // Load document with a -90 degree rotation
2309 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
2310 FPDF_PAGE page = LoadPage(0);
2311 EXPECT_NE(nullptr, page);
2312
2313 EXPECT_EQ(3, FPDFPage_GetRotation(page));
2314 UnloadPage(page);
2315}
Nicolas Penab3161852017-05-02 14:12:50 -04002316
Lei Zhangab41f252018-12-23 03:10:50 +00002317TEST_F(FPDFEditEmbedderTest, AddTrueTypeFontText) {
Nicolas Penab3161852017-05-02 14:12:50 -04002318 // Start with a blank page
2319 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2320 {
2321 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Tom Sepez20c41a52018-08-29 23:53:53 +00002322 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2323 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2324 FPDF_FONT_TRUETYPE, 0));
Nicolas Penab3161852017-05-02 14:12:50 -04002325 ASSERT_TRUE(font.get());
2326
2327 // Add some text to the page
2328 FPDF_PAGEOBJECT text_object =
2329 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2330 EXPECT_TRUE(text_object);
2331 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2332 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2333 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2334 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2335 FPDFPage_InsertObject(page, text_object);
Lei Zhang30ff2532019-01-31 21:37:55 +00002336 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04002337#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002338 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002339#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2340 const char md5[] = "d60ba39f9698e32360d99e727dd93165";
Nicolas Penab3161852017-05-02 14:12:50 -04002341#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002342 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04002343#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002344 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04002345
2346 // Add some more text, same font
2347 FPDF_PAGEOBJECT text_object2 =
2348 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
2349 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2350 GetFPDFWideString(L"Bigger font size");
2351 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2352 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
2353 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04002354 }
Lei Zhang30ff2532019-01-31 21:37:55 +00002355 ScopedFPDFBitmap page_bitmap2 = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04002356#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04002357 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002358#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2359 const char md5_2[] = "2199b579c49ab5f80c246a586a80ee90";
Nicolas Penab3161852017-05-02 14:12:50 -04002360#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00002361 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04002362#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00002363 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002364
Nicolas Pena207b7272017-05-26 17:37:06 -04002365 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04002366 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2367 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002368
2369 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04002370}
Nicolas Penaf45ade32017-05-03 10:23:49 -04002371
Lei Zhangab41f252018-12-23 03:10:50 +00002372TEST_F(FPDFEditEmbedderTest, TransformAnnot) {
Jane Liueda65252017-06-07 11:31:27 -04002373 // Open a file with one annotation and load its first page.
2374 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00002375 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04002376 ASSERT_TRUE(page);
2377
Lei Zhanga21d5932018-02-05 18:28:38 +00002378 {
2379 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00002380 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00002381 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
2382 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04002383
Lei Zhanga21d5932018-02-05 18:28:38 +00002384 // FPDFPage_TransformAnnots() should run without errors when modifying
2385 // annotation rectangles.
2386 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
2387 }
Jane Liueda65252017-06-07 11:31:27 -04002388 UnloadPage(page);
2389}
2390
Nicolas Penaf45ade32017-05-03 10:23:49 -04002391// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04002392#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Lei Zhangab41f252018-12-23 03:10:50 +00002393TEST_F(FPDFEditEmbedderTest, AddCIDFontText) {
Nicolas Penaf45ade32017-05-03 10:23:49 -04002394 // Start with a blank page
2395 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2396 CFX_Font CIDfont;
2397 {
2398 // First, get the data from the font
2399 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
2400 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
Tom Sepez20c41a52018-08-29 23:53:53 +00002401 pdfium::span<const uint8_t> span = CIDfont.GetFontSpan();
Nicolas Penaf45ade32017-05-03 10:23:49 -04002402
2403 // Load the data into a FPDF_Font.
Tom Sepez20c41a52018-08-29 23:53:53 +00002404 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2405 FPDF_FONT_TRUETYPE, 1));
Nicolas Penaf45ade32017-05-03 10:23:49 -04002406 ASSERT_TRUE(font.get());
2407
2408 // Add some text to the page
2409 FPDF_PAGEOBJECT text_object =
2410 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2411 ASSERT_TRUE(text_object);
2412 std::wstring wstr = L"ABCDEFGhijklmnop.";
2413 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
2414 GetFPDFWideString(wstr);
2415 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
2416 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
2417 FPDFPage_InsertObject(page, text_object);
2418
2419 // And add some Japanese characters
2420 FPDF_PAGEOBJECT text_object2 =
2421 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
2422 ASSERT_TRUE(text_object2);
2423 std::wstring wstr2 =
2424 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
2425 L"\u756A";
2426 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
2427 GetFPDFWideString(wstr2);
2428 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
2429 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
2430 FPDFPage_InsertObject(page, text_object2);
2431 }
2432
Nicolas Pena207b7272017-05-26 17:37:06 -04002433 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00002434 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00002435 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002436 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002437 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2438 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04002439
2440 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04002441 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04002442 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2443 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002444
2445 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04002446}
Dan Sinclair698aed72017-09-26 16:24:49 -04002447#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002448
Lei Zhangab41f252018-12-23 03:10:50 +00002449TEST_F(FPDFEditEmbedderTest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04002450 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002451 {
2452 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
2453 FPDF_PAGE page = LoadPage(0);
2454 ASSERT_NE(nullptr, page);
2455
2456 // Now add a more complex blue path.
2457 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
2458 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
2459 // TODO(npm): stroking will cause the MD5s to differ.
2460 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
2461 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
2462 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
2463 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
2464 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
2465 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
2466 EXPECT_TRUE(FPDFPath_Close(green_path));
2467 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002468 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002469 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002470
2471 // Now save the result, closing the page and document
2472 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2473 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2474 UnloadPage(page);
2475 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002476
2477 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002478}
Jane Liu28fb7ba2017-08-02 21:45:57 -04002479
Lei Zhangab41f252018-12-23 03:10:50 +00002480TEST_F(FPDFEditEmbedderTest, AddMark) {
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002481 // Load document with some text.
2482 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2483 FPDF_PAGE page = LoadPage(0);
2484 ASSERT_TRUE(page);
2485
2486 constexpr int kExpectedObjectCount = 19;
2487 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2488
2489 // Add to the first page object a "Bounds" mark with "Position": "First".
2490 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
2491 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(page_object, "Bounds");
2492 EXPECT_TRUE(mark);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002493 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2494 "Position", "First"));
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002495
2496 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2497
2498 // Save the file
2499 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2500 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2501 UnloadPage(page);
2502
2503 // Re-open the file and check the new mark is present.
Lei Zhang0b494052019-01-31 21:41:15 +00002504 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimad8df8c32018-07-12 22:15:09 +00002505 FPDF_PAGE saved_page = LoadSavedPage(0);
2506
2507 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 2);
2508
2509 CloseSavedPage(saved_page);
2510 CloseSavedDocument();
2511}
2512
Lei Zhangab41f252018-12-23 03:10:50 +00002513TEST_F(FPDFEditEmbedderTest, SetMarkParam) {
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002514 // Load document with some text.
2515 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2516 FPDF_PAGE page = LoadPage(0);
2517 ASSERT_TRUE(page);
2518
2519 constexpr int kExpectedObjectCount = 19;
2520 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2521
2522 // Check the "Bounds" mark's "Position" param is "Last".
2523 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2524 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2525 ASSERT_TRUE(mark);
2526 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002527 unsigned long name_len = 999u;
2528 ASSERT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
2529 EXPECT_EQ((6u + 1u) * 2u, name_len);
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002530 ASSERT_EQ(L"Bounds",
2531 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2532 unsigned long out_buffer_len;
2533 ASSERT_TRUE(FPDFPageObjMark_GetParamStringValue(
2534 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2535 ASSERT_EQ(L"Last",
2536 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2537
2538 // Set is to "End".
2539 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), page_object, mark,
2540 "Position", "End"));
2541
2542 // Verify the object passed must correspond to the mark passed.
2543 FPDF_PAGEOBJECT another_page_object = FPDFPage_GetObject(page, 17);
2544 EXPECT_FALSE(FPDFPageObjMark_SetStringParam(document(), another_page_object,
2545 mark, "Position", "End"));
2546
2547 // Verify nothing else changed.
2548 CheckMarkCounts(page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2549
2550 // Verify "Position" now maps to "End".
2551 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2552 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2553 EXPECT_EQ(L"End",
2554 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2555
2556 // Save the file
2557 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2558 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2559 UnloadPage(page);
2560
2561 // Re-open the file and cerify "Position" still maps to "End".
Lei Zhang0b494052019-01-31 21:41:15 +00002562 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002563 FPDF_PAGE saved_page = LoadSavedPage(0);
2564
2565 CheckMarkCounts(saved_page, 1, kExpectedObjectCount, 8, 4, 9, 1);
2566 page_object = FPDFPage_GetObject(saved_page, 18);
2567 mark = FPDFPageObj_GetMark(page_object, 1);
2568 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2569 mark, "Position", buffer, sizeof(buffer), &out_buffer_len));
2570 EXPECT_EQ(L"End",
2571 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2572
2573 CloseSavedPage(saved_page);
2574 CloseSavedDocument();
2575}
2576
Lei Zhangab41f252018-12-23 03:10:50 +00002577TEST_F(FPDFEditEmbedderTest, AddMarkedText) {
Henrique Nakashima144107d2018-07-10 21:04:05 +00002578 // Start with a blank page.
2579 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2580
2581 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Tom Sepez20c41a52018-08-29 23:53:53 +00002582 pdfium::span<const uint8_t> span = stock_font->GetFont()->GetFontSpan();
2583 ScopedFPDFFont font(FPDFText_LoadFont(document(), span.data(), span.size(),
2584 FPDF_FONT_TRUETYPE, 0));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002585 ASSERT_TRUE(font.get());
2586
2587 // Add some text to the page.
2588 FPDF_PAGEOBJECT text_object =
2589 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2590
2591 EXPECT_TRUE(text_object);
2592 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
2593 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2594 EXPECT_TRUE(FPDFText_SetText(text_object, text1.get()));
2595 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2596 FPDFPage_InsertObject(page, text_object);
2597
2598 // Add a mark with the tag "TestMarkName" to that text.
2599 EXPECT_EQ(0, FPDFPageObj_CountMarks(text_object));
Henrique Nakashima6fc8d872018-09-18 16:48:31 +00002600 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(text_object, "Test Mark Name");
Henrique Nakashima144107d2018-07-10 21:04:05 +00002601 EXPECT_TRUE(mark);
2602 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2603 EXPECT_EQ(mark, FPDFPageObj_GetMark(text_object, 0));
2604 char buffer[256];
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002605 unsigned long name_len = 999u;
2606 ASSERT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
2607 EXPECT_EQ((14u + 1u) * 2, name_len);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002608 std::wstring name =
2609 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashima6fc8d872018-09-18 16:48:31 +00002610 EXPECT_EQ(L"Test Mark Name", name);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002611
2612 // Add parameters:
2613 // - int "IntKey" : 42
2614 // - string "StringKey": "StringValue"
Henrique Nakashima07520f62018-07-12 19:45:29 +00002615 // - blob "BlobKey": "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0"
2616 constexpr const size_t kBlobLen = 28;
Lei Zhangd3b028b2018-11-30 22:22:00 +00002617 char block_value[kBlobLen];
2618 memcpy(block_value, "\x01\x02\x03\0BlobValue1\0\0\0BlobValue2\0", kBlobLen);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002619 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002620 EXPECT_TRUE(
2621 FPDFPageObjMark_SetIntParam(document(), text_object, mark, "IntKey", 42));
2622 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), text_object, mark,
2623 "StringKey", "StringValue"));
2624 EXPECT_TRUE(FPDFPageObjMark_SetBlobParam(document(), text_object, mark,
Lei Zhangd3b028b2018-11-30 22:22:00 +00002625 "BlobKey", block_value, kBlobLen));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002626 EXPECT_EQ(3, FPDFPageObjMark_CountParams(mark));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002627
2628 // Check the two parameters can be retrieved.
2629 EXPECT_EQ(FPDF_OBJECT_NUMBER,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002630 FPDFPageObjMark_GetParamValueType(mark, "IntKey"));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002631 int int_value;
Henrique Nakashima94230e52018-07-11 22:02:02 +00002632 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "IntKey", &int_value));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002633 EXPECT_EQ(42, int_value);
2634
2635 EXPECT_EQ(FPDF_OBJECT_STRING,
Henrique Nakashima94230e52018-07-11 22:02:02 +00002636 FPDFPageObjMark_GetParamValueType(mark, "StringKey"));
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002637 unsigned long out_buffer_len = 999u;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002638 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(
2639 mark, "StringKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima144107d2018-07-10 21:04:05 +00002640 EXPECT_GT(out_buffer_len, 0u);
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002641 EXPECT_NE(999u, out_buffer_len);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002642 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2643 EXPECT_EQ(L"StringValue", name);
2644
Henrique Nakashima07520f62018-07-12 19:45:29 +00002645 EXPECT_EQ(FPDF_OBJECT_STRING,
2646 FPDFPageObjMark_GetParamValueType(mark, "BlobKey"));
2647 out_buffer_len = 0;
Henrique Nakashimaa3406772018-07-13 19:10:53 +00002648 EXPECT_TRUE(FPDFPageObjMark_GetParamBlobValue(
2649 mark, "BlobKey", buffer, sizeof(buffer), &out_buffer_len));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002650 EXPECT_EQ(kBlobLen, out_buffer_len);
Lei Zhangd3b028b2018-11-30 22:22:00 +00002651 EXPECT_EQ(0, memcmp(block_value, buffer, kBlobLen));
Henrique Nakashima07520f62018-07-12 19:45:29 +00002652
Henrique Nakashima144107d2018-07-10 21:04:05 +00002653// Render and check the bitmap is the expected one.
2654#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
2655 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
Lei Zhang5e2c51c2018-07-27 22:33:34 +00002656#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
2657 const char md5[] = "d60ba39f9698e32360d99e727dd93165";
Henrique Nakashima144107d2018-07-10 21:04:05 +00002658#else
2659 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
2660#endif
2661 {
Lei Zhang30ff2532019-01-31 21:37:55 +00002662 ScopedFPDFBitmap page_bitmap = RenderPage(page);
Henrique Nakashima144107d2018-07-10 21:04:05 +00002663 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2664 }
2665
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002666 // Now save the result.
2667 EXPECT_EQ(1, FPDFPage_CountObjects(page));
2668 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2669 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2670
Henrique Nakashima144107d2018-07-10 21:04:05 +00002671 FPDF_ClosePage(page);
2672
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002673 // Re-open the file and check the changes were kept in the saved .pdf.
Lei Zhang0b494052019-01-31 21:41:15 +00002674 ASSERT_TRUE(OpenSavedDocument());
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002675 FPDF_PAGE saved_page = LoadSavedPage(0);
2676 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
2677
2678 text_object = FPDFPage_GetObject(saved_page, 0);
2679 EXPECT_TRUE(text_object);
2680 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2681 mark = FPDFPageObj_GetMark(text_object, 0);
2682 EXPECT_TRUE(mark);
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002683
2684 name_len = 999u;
2685 ASSERT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &name_len));
2686 EXPECT_EQ((14u + 1u) * 2, name_len);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002687 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
Henrique Nakashima6fc8d872018-09-18 16:48:31 +00002688 EXPECT_EQ(L"Test Mark Name", name);
Henrique Nakashimab4bcf692018-07-11 21:19:22 +00002689
2690 CloseSavedPage(saved_page);
2691 CloseSavedDocument();
Henrique Nakashima144107d2018-07-10 21:04:05 +00002692}
2693
Lei Zhangab41f252018-12-23 03:10:50 +00002694TEST_F(FPDFEditEmbedderTest, MarkGetName) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002695 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2696 FPDF_PAGE page = LoadPage(0);
2697 ASSERT_TRUE(page);
2698 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2699 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2700 ASSERT_TRUE(mark);
2701
2702 char buffer[256];
2703 unsigned long out_len;
2704
2705 // Show the positive cases of FPDFPageObjMark_GetName.
2706 out_len = 999u;
2707 EXPECT_TRUE(FPDFPageObjMark_GetName(mark, nullptr, 0, &out_len));
2708 EXPECT_EQ((6u + 1u) * 2u, out_len);
2709
2710 out_len = 999u;
2711 EXPECT_TRUE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), &out_len));
2712 EXPECT_EQ(L"Bounds",
2713 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2714 EXPECT_EQ((6u + 1u) * 2u, out_len);
2715
2716 // Show the negative cases of FPDFPageObjMark_GetName.
2717 out_len = 999u;
2718 EXPECT_FALSE(
2719 FPDFPageObjMark_GetName(nullptr, buffer, sizeof(buffer), &out_len));
2720 EXPECT_EQ(999u, out_len);
2721
2722 EXPECT_FALSE(FPDFPageObjMark_GetName(mark, buffer, sizeof(buffer), nullptr));
2723
2724 UnloadPage(page);
2725}
2726
Lei Zhangab41f252018-12-23 03:10:50 +00002727TEST_F(FPDFEditEmbedderTest, MarkGetParamKey) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002728 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2729 FPDF_PAGE page = LoadPage(0);
2730 ASSERT_TRUE(page);
2731 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2732 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2733 ASSERT_TRUE(mark);
2734
2735 char buffer[256];
2736 unsigned long out_len;
2737
2738 // Show the positive cases of FPDFPageObjMark_GetParamKey.
2739 out_len = 999u;
2740 EXPECT_TRUE(FPDFPageObjMark_GetParamKey(mark, 0, nullptr, 0, &out_len));
2741 EXPECT_EQ((8u + 1u) * 2u, out_len);
2742
2743 out_len = 999u;
2744 EXPECT_TRUE(
2745 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer), &out_len));
2746 EXPECT_EQ(L"Position",
2747 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2748 EXPECT_EQ((8u + 1u) * 2u, out_len);
2749
2750 // Show the negative cases of FPDFPageObjMark_GetParamKey.
2751 out_len = 999u;
2752 EXPECT_FALSE(FPDFPageObjMark_GetParamKey(nullptr, 0, buffer, sizeof(buffer),
2753 &out_len));
2754 EXPECT_EQ(999u, out_len);
2755
2756 out_len = 999u;
2757 EXPECT_FALSE(
2758 FPDFPageObjMark_GetParamKey(mark, 1, buffer, sizeof(buffer), &out_len));
2759 EXPECT_EQ(999u, out_len);
2760
2761 EXPECT_FALSE(
2762 FPDFPageObjMark_GetParamKey(mark, 0, buffer, sizeof(buffer), nullptr));
2763
2764 UnloadPage(page);
2765}
2766
Lei Zhangab41f252018-12-23 03:10:50 +00002767TEST_F(FPDFEditEmbedderTest, MarkGetIntParam) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002768 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2769 FPDF_PAGE page = LoadPage(0);
2770 ASSERT_TRUE(page);
2771 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 8);
2772 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 0);
2773 ASSERT_TRUE(mark);
2774
2775 int out_value;
2776
2777 // Show the positive cases of FPDFPageObjMark_GetParamIntValue.
2778 out_value = 999;
2779 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValue(mark, "Factor", &out_value));
2780 EXPECT_EQ(3, out_value);
2781
2782 // Show the negative cases of FPDFPageObjMark_GetParamIntValue.
2783 out_value = 999;
2784 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(nullptr, "Factor", &out_value));
2785 EXPECT_EQ(999, out_value);
2786
2787 out_value = 999;
2788 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(mark, "ParamThatDoesNotExist",
2789 &out_value));
2790 EXPECT_EQ(999, out_value);
2791
2792 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(mark, "Factor", nullptr));
2793
2794 page_object = FPDFPage_GetObject(page, 18);
2795 mark = FPDFPageObj_GetMark(page_object, 1);
2796 out_value = 999;
2797 EXPECT_FALSE(FPDFPageObjMark_GetParamIntValue(mark, "Position", &out_value));
2798 EXPECT_EQ(999, out_value);
2799
2800 UnloadPage(page);
2801}
2802
Lei Zhangab41f252018-12-23 03:10:50 +00002803TEST_F(FPDFEditEmbedderTest, MarkGetStringParam) {
Henrique Nakashimac3099d12018-09-18 18:08:15 +00002804 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
2805 FPDF_PAGE page = LoadPage(0);
2806 ASSERT_TRUE(page);
2807 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 18);
2808 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, 1);
2809 ASSERT_TRUE(mark);
2810
2811 char buffer[256];
2812 unsigned long out_len;
2813
2814 // Show the positive cases of FPDFPageObjMark_GetParamStringValue.
2815 out_len = 999u;
2816 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "Position", nullptr, 0,
2817 &out_len));
2818 EXPECT_EQ((4u + 1u) * 2u, out_len);
2819
2820 out_len = 999u;
2821 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValue(mark, "Position", buffer,
2822 sizeof(buffer), &out_len));
2823 EXPECT_EQ(L"Last",
2824 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer)));
2825 EXPECT_EQ((4u + 1u) * 2u, out_len);
2826
2827 // Show the negative cases of FPDFPageObjMark_GetParamStringValue.
2828 out_len = 999u;
2829 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(nullptr, "Position", buffer,
2830 sizeof(buffer), &out_len));
2831 EXPECT_EQ(999u, out_len);
2832
2833 out_len = 999u;
2834 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(
2835 mark, "ParamThatDoesNotExist", buffer, sizeof(buffer), &out_len));
2836 EXPECT_EQ(999u, out_len);
2837
2838 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(mark, "Position", buffer,
2839 sizeof(buffer), nullptr));
2840
2841 page_object = FPDFPage_GetObject(page, 8);
2842 mark = FPDFPageObj_GetMark(page_object, 0);
2843 out_len = 999u;
2844 EXPECT_FALSE(FPDFPageObjMark_GetParamStringValue(mark, "Factor", buffer,
2845 sizeof(buffer), &out_len));
2846 EXPECT_EQ(999u, out_len);
2847
2848 UnloadPage(page);
2849}
2850
Lei Zhangab41f252018-12-23 03:10:50 +00002851TEST_F(FPDFEditEmbedderTest, ExtractImageBitmap) {
Jane Liu28fb7ba2017-08-02 21:45:57 -04002852 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2853 FPDF_PAGE page = LoadPage(0);
2854 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002855 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04002856
2857 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2858 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2859 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
2860
2861 obj = FPDFPage_GetObject(page, 33);
2862 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2863 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
2864 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2865 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
2866 FPDFBitmap_Destroy(bitmap);
2867
2868 obj = FPDFPage_GetObject(page, 34);
2869 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2870 bitmap = FPDFImageObj_GetBitmap(obj);
2871 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2872 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
2873 FPDFBitmap_Destroy(bitmap);
2874
2875 obj = FPDFPage_GetObject(page, 35);
2876 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2877 bitmap = FPDFImageObj_GetBitmap(obj);
2878 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
2879 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
2880 FPDFBitmap_Destroy(bitmap);
2881
2882 obj = FPDFPage_GetObject(page, 36);
2883 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2884 bitmap = FPDFImageObj_GetBitmap(obj);
2885 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2886 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
2887 FPDFBitmap_Destroy(bitmap);
2888
2889 obj = FPDFPage_GetObject(page, 37);
2890 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2891 bitmap = FPDFImageObj_GetBitmap(obj);
2892 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2893 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
2894 FPDFBitmap_Destroy(bitmap);
2895
2896 obj = FPDFPage_GetObject(page, 38);
2897 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2898 bitmap = FPDFImageObj_GetBitmap(obj);
2899 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2900 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
2901 FPDFBitmap_Destroy(bitmap);
2902 UnloadPage(page);
2903}
Jane Liu548334e2017-08-03 16:33:40 -04002904
Lei Zhangab41f252018-12-23 03:10:50 +00002905TEST_F(FPDFEditEmbedderTest, ExtractJBigImageBitmap) {
Lei Zhang53341dd2018-03-01 15:42:47 +00002906 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
2907 FPDF_PAGE page = LoadPage(0);
2908 ASSERT_TRUE(page);
2909 ASSERT_EQ(1, FPDFPage_CountObjects(page));
2910
2911 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
2912 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2913 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002914 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00002915 ASSERT_TRUE(bitmap);
2916 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
2917 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00002918 }
2919
2920 UnloadPage(page);
2921}
2922
Lei Zhangab41f252018-12-23 03:10:50 +00002923TEST_F(FPDFEditEmbedderTest, GetImageData) {
Jane Liu548334e2017-08-03 16:33:40 -04002924 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2925 FPDF_PAGE page = LoadPage(0);
2926 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002927 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04002928
2929 // Retrieve an image object with flate-encoded data stream.
2930 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
2931 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2932
2933 // Check that the raw image data has the correct length and hash value.
2934 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2935 std::vector<char> buf(len);
2936 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2937 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
2938 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2939
2940 // Check that the decoded image data has the correct length and hash value.
2941 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2942 buf.clear();
2943 buf.resize(len);
2944 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2945 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
2946 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2947
Lei Zhang351e8b02018-12-20 01:10:06 +00002948 // Retrieve an image object with DCTDecode-encoded data stream.
Jane Liu548334e2017-08-03 16:33:40 -04002949 obj = FPDFPage_GetObject(page, 37);
2950 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2951
2952 // Check that the raw image data has the correct length and hash value.
2953 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2954 buf.clear();
2955 buf.resize(len);
2956 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2957 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2958 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2959
2960 // Check that the decoded image data has the correct length and hash value,
2961 // which should be the same as those of the raw data, since this image is
2962 // encoded by a single DCTDecode filter and decoding is a noop.
2963 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2964 buf.clear();
2965 buf.resize(len);
2966 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2967 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2968 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2969
2970 UnloadPage(page);
2971}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04002972
Lei Zhangab41f252018-12-23 03:10:50 +00002973TEST_F(FPDFEditEmbedderTest, GetImageMatrix) {
Lei Zhang866d6882018-10-24 17:31:01 +00002974 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2975 FPDF_PAGE page = LoadPage(0);
2976 ASSERT_TRUE(page);
2977 ASSERT_EQ(39, FPDFPage_CountObjects(page));
2978
2979 FPDF_PAGEOBJECT obj;
2980 double a;
2981 double b;
2982 double c;
2983 double d;
2984 double e;
2985 double f;
2986
2987 obj = FPDFPage_GetObject(page, 33);
2988 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2989 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
2990 EXPECT_DOUBLE_EQ(53.0, a);
2991 EXPECT_DOUBLE_EQ(0.0, b);
2992 EXPECT_DOUBLE_EQ(0.0, c);
2993 EXPECT_DOUBLE_EQ(43.0, d);
2994 EXPECT_DOUBLE_EQ(72.0, e);
2995 EXPECT_DOUBLE_EQ(646.510009765625, f);
2996
2997 obj = FPDFPage_GetObject(page, 34);
2998 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2999 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3000 EXPECT_DOUBLE_EQ(70.0, a);
3001 EXPECT_DOUBLE_EQ(0.0, b);
3002 EXPECT_DOUBLE_EQ(0.0, c);
3003 EXPECT_DOUBLE_EQ(51.0, d);
3004 EXPECT_DOUBLE_EQ(216.0, e);
3005 EXPECT_DOUBLE_EQ(646.510009765625, f);
3006
3007 obj = FPDFPage_GetObject(page, 35);
3008 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3009 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3010 EXPECT_DOUBLE_EQ(69.0, a);
3011 EXPECT_DOUBLE_EQ(0.0, b);
3012 EXPECT_DOUBLE_EQ(0.0, c);
3013 EXPECT_DOUBLE_EQ(51.0, d);
3014 EXPECT_DOUBLE_EQ(360.0, e);
3015 EXPECT_DOUBLE_EQ(646.510009765625, f);
3016
3017 obj = FPDFPage_GetObject(page, 36);
3018 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3019 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3020 EXPECT_DOUBLE_EQ(59.0, a);
3021 EXPECT_DOUBLE_EQ(0.0, b);
3022 EXPECT_DOUBLE_EQ(0.0, c);
3023 EXPECT_DOUBLE_EQ(45.0, d);
3024 EXPECT_DOUBLE_EQ(72.0, e);
3025 EXPECT_DOUBLE_EQ(553.510009765625, f);
3026
3027 obj = FPDFPage_GetObject(page, 37);
3028 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3029 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3030 EXPECT_DOUBLE_EQ(55.94000244140625, a);
3031 EXPECT_DOUBLE_EQ(0.0, b);
3032 EXPECT_DOUBLE_EQ(0.0, c);
3033 EXPECT_DOUBLE_EQ(46.950000762939453, d);
3034 EXPECT_DOUBLE_EQ(216.0, e);
3035 EXPECT_DOUBLE_EQ(552.510009765625, f);
3036
3037 obj = FPDFPage_GetObject(page, 38);
3038 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3039 EXPECT_TRUE(FPDFImageObj_GetMatrix(obj, &a, &b, &c, &d, &e, &f));
3040 EXPECT_DOUBLE_EQ(70.528999328613281, a);
3041 EXPECT_DOUBLE_EQ(0.0, b);
3042 EXPECT_DOUBLE_EQ(0.0, c);
3043 EXPECT_DOUBLE_EQ(43.149997711181641, d);
3044 EXPECT_DOUBLE_EQ(360.0, e);
3045 EXPECT_DOUBLE_EQ(553.3599853515625, f);
3046
3047 UnloadPage(page);
3048}
3049
Lei Zhangab41f252018-12-23 03:10:50 +00003050TEST_F(FPDFEditEmbedderTest, DestroyPageObject) {
Jane Liu2e5f0ae2017-08-08 15:23:27 -04003051 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
3052 ASSERT_TRUE(rect);
3053
3054 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
3055 FPDFPageObj_Destroy(rect);
3056}
Jane Liube63ab92017-08-09 14:09:34 -04003057
Lei Zhangab41f252018-12-23 03:10:50 +00003058TEST_F(FPDFEditEmbedderTest, GetImageFilters) {
Jane Liube63ab92017-08-09 14:09:34 -04003059 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
3060 FPDF_PAGE page = LoadPage(0);
3061 ASSERT_TRUE(page);
3062
3063 // Verify that retrieving the filter of a non-image object would fail.
3064 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
3065 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3066 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
3067 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
3068
3069 // Verify the returned filter string for an image object with a single filter.
3070 obj = FPDFPage_GetObject(page, 33);
3071 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3072 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
3073 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
3074 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07003075 static constexpr char kFlateDecode[] = "FlateDecode";
3076 EXPECT_EQ(sizeof(kFlateDecode),
3077 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
3078 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04003079 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
3080
3081 // Verify all the filters for an image object with a list of filters.
3082 obj = FPDFPage_GetObject(page, 38);
3083 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3084 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
3085 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
3086 buf.clear();
3087 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07003088 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
3089 EXPECT_EQ(sizeof(kASCIIHexDecode),
3090 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
3091 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04003092
3093 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
3094 buf.clear();
3095 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07003096 static constexpr char kDCTDecode[] = "DCTDecode";
3097 EXPECT_EQ(sizeof(kDCTDecode),
3098 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
3099 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04003100
3101 UnloadPage(page);
3102}
Jane Liuca898292017-08-16 11:25:35 -04003103
Lei Zhangab41f252018-12-23 03:10:50 +00003104TEST_F(FPDFEditEmbedderTest, GetImageMetadata) {
Jane Liuca898292017-08-16 11:25:35 -04003105 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
3106 FPDF_PAGE page = LoadPage(0);
3107 ASSERT_TRUE(page);
3108
3109 // Check that getting the metadata of a null object would fail.
3110 FPDF_IMAGEOBJ_METADATA metadata;
3111 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
3112
3113 // Check that receiving the metadata with a null metadata object would fail.
3114 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
3115 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
3116
3117 // Check that when retrieving an image object's metadata without passing in
3118 // |page|, all values are correct, with the last two being default values.
3119 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3120 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05003121 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04003122 EXPECT_EQ(92u, metadata.width);
3123 EXPECT_EQ(68u, metadata.height);
Lei Zhang351e8b02018-12-20 01:10:06 +00003124 EXPECT_FLOAT_EQ(96.0f, metadata.horizontal_dpi);
3125 EXPECT_FLOAT_EQ(96.0f, metadata.vertical_dpi);
Jane Liuca898292017-08-16 11:25:35 -04003126 EXPECT_EQ(0u, metadata.bits_per_pixel);
3127 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
3128
3129 // Verify the metadata of a bitmap image with indexed colorspace.
3130 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05003131 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04003132 EXPECT_EQ(92u, metadata.width);
3133 EXPECT_EQ(68u, metadata.height);
Lei Zhang351e8b02018-12-20 01:10:06 +00003134 EXPECT_FLOAT_EQ(96.0f, metadata.horizontal_dpi);
3135 EXPECT_FLOAT_EQ(96.0f, metadata.vertical_dpi);
Jane Liuca898292017-08-16 11:25:35 -04003136 EXPECT_EQ(1u, metadata.bits_per_pixel);
3137 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
3138
3139 // Verify the metadata of an image with RGB colorspace.
3140 obj = FPDFPage_GetObject(page, 37);
3141 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
3142 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05003143 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04003144 EXPECT_EQ(126u, metadata.width);
3145 EXPECT_EQ(106u, metadata.height);
Lei Zhang351e8b02018-12-20 01:10:06 +00003146 EXPECT_FLOAT_EQ(162.173752f, metadata.horizontal_dpi);
3147 EXPECT_FLOAT_EQ(162.555878f, metadata.vertical_dpi);
Jane Liuca898292017-08-16 11:25:35 -04003148 EXPECT_EQ(24u, metadata.bits_per_pixel);
3149 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
3150
3151 UnloadPage(page);
3152}