blob: a9eca92a4edc17f97c4dbc3a1d62f38380a12b19 [file] [log] [blame]
Tom Sepezd483eb42016-01-06 10:03:59 -08001// Copyright 2016 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05005#include <memory>
6#include <string>
Nicolas Penad03ca422017-03-06 13:54:33 -05007#include <utility>
Jane Liu548334e2017-08-03 16:33:40 -04008#include <vector>
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009
Nicolas Penabe90aae2017-02-27 10:41:41 -050010#include "core/fpdfapi/font/cpdf_font.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050011#include "core/fpdfapi/page/cpdf_page.h"
Henrique Nakashima6eb79392018-06-12 20:27:35 +000012#include "core/fpdfapi/page/cpdf_pageobject.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050013#include "core/fpdfapi/parser/cpdf_array.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050014#include "core/fpdfapi/parser/cpdf_dictionary.h"
Nicolas Penad03ca422017-03-06 13:54:33 -050015#include "core/fpdfapi/parser/cpdf_number.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050016#include "core/fpdfapi/parser/cpdf_stream.h"
Artem Strygineababa12018-06-06 12:31:18 +000017#include "core/fpdfapi/parser/cpdf_stream_acc.h"
Nicolas Pena0fc185e2017-02-08 12:13:20 -050018#include "core/fxcrt/fx_system.h"
Dan Sinclair00d47a62018-03-28 18:39:04 +000019#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000020#include "public/cpp/fpdf_scopers.h"
Jane Liueda65252017-06-07 11:31:27 -040021#include "public/fpdf_annot.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080022#include "public/fpdf_edit.h"
23#include "public/fpdfview.h"
24#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080025#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080026#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080027#include "testing/test_support.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080028
Nicolas Pena3ff54002017-07-05 11:55:35 -040029class FPDFEditEmbeddertest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050030 protected:
31 FPDF_DOCUMENT CreateNewDocument() {
32 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070033 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050034 return document_;
35 }
36
Lei Zhang710fa992018-05-25 16:24:48 +000037 void CheckFontDescriptor(const CPDF_Dictionary* font_dict,
Nicolas Penad03ca422017-03-06 13:54:33 -050038 int font_type,
39 bool bold,
40 bool italic,
41 uint32_t size,
42 const uint8_t* data) {
Lei Zhangb1ec2802018-05-25 21:55:24 +000043 const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
Nicolas Penad03ca422017-03-06 13:54:33 -050044 ASSERT_TRUE(font_desc);
45 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
46 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
47 font_desc->GetStringFor("FontName"));
48
49 // Check that the font descriptor has the required keys according to spec
50 // 1.7 Table 5.19
51 ASSERT_TRUE(font_desc->KeyExist("Flags"));
Dan Sinclair10e1f052017-09-28 15:59:42 -040052
Nicolas Penad03ca422017-03-06 13:54:33 -050053 int font_flags = font_desc->GetIntegerFor("Flags");
Dan Sinclair10e1f052017-09-28 15:59:42 -040054 EXPECT_EQ(bold, FontStyleIsBold(font_flags));
55 EXPECT_EQ(italic, FontStyleIsItalic(font_flags));
56 EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags));
Nicolas Penad03ca422017-03-06 13:54:33 -050057 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
Nicolás Peña5f95f362017-09-28 13:00:45 +090058
Lei Zhangb1ec2802018-05-25 21:55:24 +000059 const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox");
Nicolás Peña5f95f362017-09-28 13:00:45 +090060 ASSERT_TRUE(fontBBox);
61 EXPECT_EQ(4U, fontBBox->GetCount());
62 // Check that the coordinates are in the preferred order according to spec
63 // 1.7 Section 3.8.4
64 EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2));
65 EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3));
66
Nicolas Penad03ca422017-03-06 13:54:33 -050067 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
68 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
69 EXPECT_TRUE(font_desc->KeyExist("Descent"));
70 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
71 EXPECT_TRUE(font_desc->KeyExist("StemV"));
Ryan Harrison275e2602017-09-18 14:23:18 -040072 ByteString present("FontFile");
73 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050074 if (font_type == FPDF_FONT_TRUETYPE)
75 std::swap(present, absent);
76 EXPECT_TRUE(font_desc->KeyExist(present));
77 EXPECT_FALSE(font_desc->KeyExist(absent));
78
Artem Strygineababa12018-06-06 12:31:18 +000079 auto streamAcc =
80 pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present));
81 streamAcc->LoadAllDataRaw();
82
Nicolas Penad03ca422017-03-06 13:54:33 -050083 // Check that the font stream is the one that was provided
Artem Strygineababa12018-06-06 12:31:18 +000084 ASSERT_EQ(size, streamAcc->GetSize());
Nicolás Peña79eab232017-09-28 13:29:05 +090085 if (font_type == FPDF_FONT_TRUETYPE) {
86 ASSERT_EQ(static_cast<int>(size),
Artem Strygineababa12018-06-06 12:31:18 +000087 streamAcc->GetDict()->GetIntegerFor("Length1"));
Nicolás Peña79eab232017-09-28 13:29:05 +090088 }
Artem Strygineababa12018-06-06 12:31:18 +000089
90 const uint8_t* stream_data = streamAcc->GetData();
Nicolas Penad03ca422017-03-06 13:54:33 -050091 for (size_t j = 0; j < size; j++)
92 EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j;
93 }
94
Lei Zhangde579ab2018-05-25 21:49:49 +000095 void CheckCompositeFontWidths(const CPDF_Array* widths_array,
Nicolas Penad03ca422017-03-06 13:54:33 -050096 CPDF_Font* typed_font) {
97 // Check that W array is in a format that conforms to PDF spec 1.7 section
98 // "Glyph Metrics in CIDFonts" (these checks are not
99 // implementation-specific).
100 EXPECT_GT(widths_array->GetCount(), 1U);
101 int num_cids_checked = 0;
102 int cur_cid = 0;
103 for (size_t idx = 0; idx < widths_array->GetCount(); idx++) {
104 int cid = widths_array->GetNumberAt(idx);
105 EXPECT_GE(cid, cur_cid);
106 ASSERT_FALSE(++idx == widths_array->GetCount());
Lei Zhangde579ab2018-05-25 21:49:49 +0000107 const CPDF_Object* next = widths_array->GetObjectAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500108 if (next->IsArray()) {
109 // We are in the c [w1 w2 ...] case
Lei Zhangde579ab2018-05-25 21:49:49 +0000110 const CPDF_Array* arr = next->AsArray();
Nicolas Penad03ca422017-03-06 13:54:33 -0500111 int cnt = static_cast<int>(arr->GetCount());
112 size_t inner_idx = 0;
113 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
Nicolas Pena23346602018-01-30 21:42:41 +0000114 uint32_t width = arr->GetNumberAt(inner_idx++);
Dan Sinclair971a6742018-03-28 19:23:25 +0000115 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
116 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500117 }
118 num_cids_checked += cnt;
119 continue;
120 }
121 // Otherwise, are in the c_first c_last w case.
122 ASSERT_TRUE(next->IsNumber());
123 int last_cid = next->AsNumber()->GetInteger();
124 ASSERT_FALSE(++idx == widths_array->GetCount());
Nicolas Pena23346602018-01-30 21:42:41 +0000125 uint32_t width = widths_array->GetNumberAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500126 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
Dan Sinclair971a6742018-03-28 19:23:25 +0000127 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
128 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500129 }
130 num_cids_checked += last_cid - cid + 1;
131 }
132 // Make sure we have a good amount of cids described
133 EXPECT_GT(num_cids_checked, 900);
134 }
135 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
136
137 private:
138 CPDF_Document* cpdf_doc_;
139};
Tom Sepezd483eb42016-01-06 10:03:59 -0800140
etienneb7712c262016-04-26 08:13:45 -0700141namespace {
thestigdc7ec032016-11-21 15:32:52 -0800142
etienneb7712c262016-04-26 08:13:45 -0700143const char kExpectedPDF[] =
144 "%PDF-1.7\r\n"
145 "%\xA1\xB3\xC5\xD7\r\n"
146 "1 0 obj\r\n"
147 "<</Pages 2 0 R /Type/Catalog>>\r\n"
148 "endobj\r\n"
149 "2 0 obj\r\n"
150 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
151 "endobj\r\n"
152 "3 0 obj\r\n"
153 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
154 "endobj\r\n"
155 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400156 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
157 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400158 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700159 ">>\r\n"
160 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400161 "5 0 obj\r\n"
162 "<</BM/Normal/CA 1/ca 1>>\r\n"
163 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700164 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400165 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700166 "0000000000 65535 f\r\n"
167 "0000000017 00000 n\r\n"
168 "0000000066 00000 n\r\n"
169 "0000000122 00000 n\r\n"
170 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400171 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700172 "trailer\r\n"
173 "<<\r\n"
174 "/Root 1 0 R\r\n"
175 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400176 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700177 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400178 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700179 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800180
etienneb7712c262016-04-26 08:13:45 -0700181} // namespace
182
Tom Sepezd483eb42016-01-06 10:03:59 -0800183TEST_F(FPDFEditEmbeddertest, EmptyCreation) {
184 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700185 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800186 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400187 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800188 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800189 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700190
Nicolas Penad9d6c292017-06-06 16:12:10 -0400191 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
192 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700193 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800194}
thestigdc7ec032016-11-21 15:32:52 -0800195
196// Regression test for https://crbug.com/667012
197TEST_F(FPDFEditEmbeddertest, RasterizePDF) {
198 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
199
200 // Get the bitmap for the original document/
Tom Sepeze08d2b12018-04-25 18:49:32 +0000201 ScopedFPDFBitmap orig_bitmap;
thestigdc7ec032016-11-21 15:32:52 -0800202 {
203 EXPECT_TRUE(OpenDocument("black.pdf"));
204 FPDF_PAGE orig_page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000205 ASSERT_TRUE(orig_page);
206 orig_bitmap = RenderLoadedPage(orig_page);
207 CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800208 UnloadPage(orig_page);
209 }
210
211 // Create a new document from |orig_bitmap| and save it.
212 {
213 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
214 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
215
216 // Add the bitmap to an image object and add the image object to the output
217 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700218 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000219 EXPECT_TRUE(
220 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get()));
thestigdc7ec032016-11-21 15:32:52 -0800221 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
222 FPDFPage_InsertObject(temp_page, temp_img);
223 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
224 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
225 FPDF_ClosePage(temp_page);
226 FPDF_CloseDocument(temp_doc);
227 }
thestigdc7ec032016-11-21 15:32:52 -0800228
229 // Get the generated content. Make sure it is at least as big as the original
230 // PDF.
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400231 EXPECT_GT(GetString().size(), 923U);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400232 VerifySavedDocument(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800233}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500234
235TEST_F(FPDFEditEmbeddertest, AddPaths) {
236 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500237 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000238 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500239
240 // We will first add a red rectangle
241 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000242 ASSERT_TRUE(red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500243 // Expect false when trying to set colors out of range
244 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
245 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
246
247 // Fill rectangle with red and insert to the page
248 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
249 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
Miklos Vajna491112b2018-05-30 13:30:10 +0000250
251 int fillmode = FPDF_FILLMODE_NONE;
252 FPDF_BOOL stroke = true;
253 EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke));
254 EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode);
255 EXPECT_FALSE(stroke);
256
Miklos Vajna97f4d672018-06-04 14:47:17 +0000257 double matrix_a = 1;
258 double matrix_b = 2;
259 double matrix_c = 3;
260 double matrix_d = 4;
261 double matrix_e = 5;
262 double matrix_f = 6;
263 EXPECT_FALSE(FPDFPath_SetMatrix(nullptr, matrix_a, matrix_b, matrix_c,
264 matrix_d, matrix_e, matrix_f));
265 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
266 matrix_d, matrix_e, matrix_f));
Miklos Vajnaac42dd22018-06-05 13:28:58 +0000267 // Set to 0 before FPDFPath_GetMatrix() to ensure they are actually set by
268 // the function.
269 matrix_a = 0;
270 matrix_b = 0;
271 matrix_c = 0;
272 matrix_d = 0;
273 matrix_e = 0;
274 matrix_f = 0;
Miklos Vajna97f4d672018-06-04 14:47:17 +0000275 EXPECT_FALSE(FPDFPath_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
276 &matrix_d, &matrix_e, &matrix_f));
277 EXPECT_TRUE(FPDFPath_GetMatrix(red_rect, &matrix_a, &matrix_b, &matrix_c,
278 &matrix_d, &matrix_e, &matrix_f));
279 EXPECT_EQ(1, static_cast<int>(matrix_a));
280 EXPECT_EQ(2, static_cast<int>(matrix_b));
281 EXPECT_EQ(3, static_cast<int>(matrix_c));
282 EXPECT_EQ(4, static_cast<int>(matrix_d));
283 EXPECT_EQ(5, static_cast<int>(matrix_e));
284 EXPECT_EQ(6, static_cast<int>(matrix_f));
285 // Set back the default
286 matrix_a = 1;
287 matrix_b = 0;
288 matrix_c = 0;
289 matrix_d = 1;
290 matrix_e = 0;
291 matrix_f = 0;
292 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
293 matrix_d, matrix_e, matrix_f));
294
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500295 FPDFPage_InsertObject(page, red_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000296 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000297 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000298 CompareBitmap(page_bitmap.get(), 612, 792,
299 "66d02eaa6181e2c069ce2ea99beda497");
300 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500301
302 // Now add to that a green rectangle with some medium alpha
303 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
304 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200305
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200306 // Make sure the type of the rectangle is a path.
307 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
308
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200309 // Make sure we get back the same color we set previously.
310 unsigned int R;
311 unsigned int G;
312 unsigned int B;
313 unsigned int A;
314 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
315 EXPECT_EQ(0U, R);
316 EXPECT_EQ(255U, G);
317 EXPECT_EQ(0U, B);
318 EXPECT_EQ(128U, A);
319
Miklos Vajna12abfd02017-09-15 07:49:03 +0200320 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
321 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200322 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200323 // Verify actual coordinates.
324 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
325 float x;
326 float y;
327 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
328 EXPECT_EQ(100, x);
329 EXPECT_EQ(100, y);
330 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
331 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
332 segment = FPDFPath_GetPathSegment(green_rect, 1);
333 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
334 EXPECT_EQ(100, x);
335 EXPECT_EQ(140, y);
336 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
337 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
338 segment = FPDFPath_GetPathSegment(green_rect, 2);
339 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
340 EXPECT_EQ(140, x);
341 EXPECT_EQ(140, y);
342 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
343 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
344 segment = FPDFPath_GetPathSegment(green_rect, 3);
345 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
346 EXPECT_EQ(140, x);
347 EXPECT_EQ(100, y);
348 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
349 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
350 segment = FPDFPath_GetPathSegment(green_rect, 4);
351 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
352 EXPECT_EQ(100, x);
353 EXPECT_EQ(100, y);
354 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
355 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200356
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500357 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
358 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000359 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000360 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000361 CompareBitmap(page_bitmap.get(), 612, 792,
362 "7b0b87604594e773add528fae567a558");
363 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500364
365 // Add a black triangle.
366 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
367 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
368 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
369 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
370 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
371 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200372
373 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
374 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200375 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200376 // Verify actual coordinates.
377 segment = FPDFPath_GetPathSegment(black_path, 0);
378 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
379 EXPECT_EQ(400, x);
380 EXPECT_EQ(100, y);
381 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
382 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
383 segment = FPDFPath_GetPathSegment(black_path, 1);
384 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
385 EXPECT_EQ(400, x);
386 EXPECT_EQ(200, y);
387 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
388 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
389 segment = FPDFPath_GetPathSegment(black_path, 2);
390 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
391 EXPECT_EQ(300, x);
392 EXPECT_EQ(100, y);
393 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
394 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
395 // Make sure out of bounds index access fails properly.
396 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200397
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500398 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000399 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000400 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000401 CompareBitmap(page_bitmap.get(), 612, 792,
402 "eadc8020a14dfcf091da2688733d8806");
403 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500404
405 // Now add a more complex blue path.
406 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
407 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
408 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
409 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
410 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
411 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
412 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
413 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
414 EXPECT_TRUE(FPDFPath_Close(blue_path));
415 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000416 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
417 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000418 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000419 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
420 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500421
422 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400423 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500424 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500425 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500426
427 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000428 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500429}
430
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000431TEST_F(FPDFEditEmbeddertest, SetText) {
Henrique Nakashima5ebfd642018-06-07 15:18:55 +0000432 // Load document with some text.
433 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
434 FPDF_PAGE page = LoadPage(0);
435 ASSERT_TRUE(page);
436
437 // Get the "Hello, world!" text object and change it.
438 ASSERT_EQ(2, FPDFPage_CountObjects(page));
439 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
440 ASSERT_TRUE(page_object);
441 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
442 GetFPDFWideString(L"Changed for SetText test");
443 EXPECT_TRUE(FPDFText_SetText(page_object, text1.get()));
444
445 // Verify the "Hello, world!" text is gone and "Changed for SetText test" is
446 // now displayed.
447 ASSERT_EQ(2, FPDFPage_CountObjects(page));
448#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
449 const char kChangedMD5[] = "94c1e7a5af7dd9d77dc2223b1091acb7";
450#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
451 const char kChangedMD5[] = "9d31703c1d1a3e1e9a778b1e297c9cd2";
452#else
453 const char kChangedMD5[] = "a0c4ea6620772991f66bf7130379b08a";
454#endif
455 {
456 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
457 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
458 }
459
460 // Now save the result.
461 EXPECT_TRUE(FPDFPage_GenerateContent(page));
462 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
463
464 UnloadPage(page);
465
466 // Re-open the file and check the changes were kept in the saved .pdf.
467 OpenSavedDocument();
468 FPDF_PAGE saved_page = LoadSavedPage(0);
469 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
470 {
471 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
472 CompareBitmap(page_bitmap.get(), 200, 200, kChangedMD5);
473 }
474
475 CloseSavedPage(saved_page);
476 CloseSavedDocument();
477}
478
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000479TEST_F(FPDFEditEmbeddertest, RemovePageObject) {
480 // Load document with some text.
481 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
482 FPDF_PAGE page = LoadPage(0);
483 ASSERT_TRUE(page);
484
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000485 // Show what the original file looks like.
486 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000487#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000488 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000489#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000490 const char kOriginalMD5[] = "e5a6fa28298db07484cd922f3e210c88";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000491#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000492 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000493#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000494 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000495 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
496 }
497
498 // Get the "Hello, world!" text object and remove it.
499 ASSERT_EQ(2, FPDFPage_CountObjects(page));
500 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
501 ASSERT_TRUE(page_object);
502 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
503
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000504 // Verify the "Hello, world!" text is gone.
505 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000506#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000507 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000508#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000509 const char kRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000510#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000511 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000512#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000513 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000514 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
515 }
516 ASSERT_EQ(1, FPDFPage_CountObjects(page));
517
518 UnloadPage(page);
519 FPDFPageObj_Destroy(page_object);
520}
521
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000522TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) {
523 // Load document with some text.
524 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
525 FPDF_PAGE page = LoadPage(0);
526 ASSERT_TRUE(page);
527
528 // Show what the original file looks like.
529 {
530#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
531 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
532#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
533 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
534#else
535 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
536#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000537 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000538 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
539 }
540
541 // Iterate over all objects, counting the number of times each content mark
542 // name appears.
543 int object_count = FPDFPage_CountObjects(page);
544 ASSERT_EQ(19, object_count);
545
546 unsigned long prime_count = 0;
547 unsigned long square_count = 0;
548 unsigned long greater_than_ten_count = 0;
549 std::vector<FPDF_PAGEOBJECT> primes;
550 for (int i = 0; i < object_count; ++i) {
551 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
552
553 int mark_count = FPDFPageObj_CountMarks(page_object);
554 for (int j = 0; j < mark_count; ++j) {
555 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
556
557 char buffer[256];
558 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
559 std::wstring name =
560 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
561 if (name == L"Prime") {
562 prime_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000563 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000564 primes.push_back(page_object);
565 } else if (name == L"Square") {
566 square_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000567 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
Henrique Nakashima132c38e2018-04-23 16:35:56 +0000568 ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u);
569 std::wstring key =
570 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
571 EXPECT_EQ(L"Factor", key);
572 EXPECT_EQ(FPDF_OBJECT_NUMBER,
573 FPDFPageObjMark_GetParamValueType(mark, 0));
574 int square_root = FPDFPageObjMark_GetParamIntValue(mark, 0);
575 EXPECT_EQ(i + 1, square_root * square_root);
Henrique Nakashima7007fd52018-07-05 18:04:19 +0000576
577 EXPECT_EQ(FPDF_OBJECT_NUMBER,
578 FPDFPageObjMark_GetParamValueTypeByKey(mark, "Factor"));
579 EXPECT_TRUE(FPDFPageObjMark_GetParamIntValueByKey(mark, "Factor",
580 &square_root));
581 EXPECT_EQ(i + 1, square_root * square_root);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000582 } else if (name == L"GreaterThanTen") {
583 greater_than_ten_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000584 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimab557bdc2018-04-23 18:04:26 +0000585 } else if (name == L"Bounds") {
586 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
587 ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u);
588 std::wstring key =
589 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
590 EXPECT_EQ(L"Position", key);
591 EXPECT_EQ(FPDF_OBJECT_STRING,
592 FPDFPageObjMark_GetParamValueType(mark, 0));
593 ASSERT_GT(FPDFPageObjMark_GetParamStringValue(mark, 0, buffer, 256),
594 0u);
595 std::wstring value =
596 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
597 EXPECT_EQ(L"Last", value);
598 EXPECT_EQ(18, i);
Henrique Nakashima7007fd52018-07-05 18:04:19 +0000599
600 EXPECT_EQ(FPDF_OBJECT_STRING,
601 FPDFPageObjMark_GetParamValueTypeByKey(mark, "Position"));
602 unsigned long length;
603 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValueByKey(
604 mark, "Position", buffer, 256, &length));
605 ASSERT_GT(length, 0u);
606 value = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
607 EXPECT_EQ(L"Last", value);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000608 } else {
609 FAIL();
610 }
611 }
612 }
613
614 // Expect certain number of tagged objects. The test file contains strings
615 // from 1 to 19.
616 EXPECT_EQ(8u, prime_count);
617 EXPECT_EQ(4u, square_count);
618 EXPECT_EQ(9u, greater_than_ten_count);
619
620 // Remove all objects marked with "Prime".
621 for (FPDF_PAGEOBJECT page_object : primes) {
622 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
623 FPDFPageObj_Destroy(page_object);
624 }
625
626 EXPECT_EQ(11, FPDFPage_CountObjects(page));
627
628 {
629#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
630 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
631#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
632 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
633#else
634 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
635#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000636 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000637 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
638 }
639
640 UnloadPage(page);
641}
642
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000643TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObject) {
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000644 // Load document with some text.
645 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
646 FPDF_PAGE page = LoadPage(0);
647 ASSERT_TRUE(page);
648
649 // Get the "Hello, world!" text object and remove it.
650 ASSERT_EQ(2, FPDFPage_CountObjects(page));
651 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
652 ASSERT_TRUE(page_object);
653 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
654
655 // Verify the "Hello, world!" text is gone.
656 ASSERT_EQ(1, FPDFPage_CountObjects(page));
657
658 // Save the file
659 EXPECT_TRUE(FPDFPage_GenerateContent(page));
660 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
661 UnloadPage(page);
662 FPDFPageObj_Destroy(page_object);
663
664 // Re-open the file and check the page object count is still 1.
665 OpenSavedDocument();
666 FPDF_PAGE saved_page = LoadSavedPage(0);
667 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
668 CloseSavedPage(saved_page);
669 CloseSavedDocument();
670}
671
Henrique Nakashima27cf78d2018-06-14 16:22:30 +0000672TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsNotLonely) {
673 // Load document with some text.
674 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
675 FPDF_PAGE page = LoadPage(0);
676 ASSERT_TRUE(page);
677
678 // Get the "Hello, world!" text object and remove it. There is another object
679 // in the same stream that says "Goodbye, world!"
680 ASSERT_EQ(3, FPDFPage_CountObjects(page));
681 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
682 ASSERT_TRUE(page_object);
683 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
684
685 // Verify the "Hello, world!" text is gone.
686 ASSERT_EQ(2, FPDFPage_CountObjects(page));
687#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
688 const char kHelloRemovedMD5[] = "e07a62d412728fc4d6e3ff42f2dd0e11";
689#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
690 const char kHelloRemovedMD5[] = "de37b0bb7ff903c1068bae361844be50";
691#else
692 const char kHelloRemovedMD5[] = "95b92950647a2190e1230911e7a1a0e9";
693#endif
694 {
695 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
696 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
697 }
698
699 // Save the file
700 EXPECT_TRUE(FPDFPage_GenerateContent(page));
701 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
702 UnloadPage(page);
703 FPDFPageObj_Destroy(page_object);
704
705 // Re-open the file and check the page object count is still 2.
706 OpenSavedDocument();
707 FPDF_PAGE saved_page = LoadSavedPage(0);
708
709 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
710 {
711 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
712 CompareBitmap(page_bitmap.get(), 200, 200, kHelloRemovedMD5);
713 }
714
715 CloseSavedPage(saved_page);
716 CloseSavedDocument();
717}
718
719TEST_F(FPDFEditEmbeddertest, RemoveExistingPageObjectSplitStreamsLonely) {
720 // Load document with some text.
721 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
722 FPDF_PAGE page = LoadPage(0);
723 ASSERT_TRUE(page);
724
725 // Get the "Greetings, world!" text object and remove it. This is the only
726 // object in the stream.
727 ASSERT_EQ(3, FPDFPage_CountObjects(page));
728 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 2);
729 ASSERT_TRUE(page_object);
730 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
731
732 // Verify the "Greetings, world!" text is gone.
733 ASSERT_EQ(2, FPDFPage_CountObjects(page));
734#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
735 const char kGreetingsRemovedMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
736#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
737 const char kGreetingsRemovedMD5[] = "e5a6fa28298db07484cd922f3e210c88";
738#else
739 const char kGreetingsRemovedMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
740#endif
741 {
742 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
743 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
744 }
745
746 // Save the file
747 EXPECT_TRUE(FPDFPage_GenerateContent(page));
748 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
749 UnloadPage(page);
750 FPDFPageObj_Destroy(page_object);
751
752 // Re-open the file and check the page object count is still 2.
753 OpenSavedDocument();
754 FPDF_PAGE saved_page = LoadSavedPage(0);
755
756 EXPECT_EQ(2, FPDFPage_CountObjects(saved_page));
757 {
758 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
759 CompareBitmap(page_bitmap.get(), 200, 200, kGreetingsRemovedMD5);
760 }
761
762 CloseSavedPage(saved_page);
763 CloseSavedDocument();
764}
765
Henrique Nakashima6eb79392018-06-12 20:27:35 +0000766TEST_F(FPDFEditEmbeddertest, GetContentStream) {
767 // Load document with some text split across streams.
768 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
769 FPDF_PAGE page = LoadPage(0);
770 ASSERT_TRUE(page);
771
772 // Content stream 0: page objects 0-14.
773 // Content stream 1: page objects 15-17.
774 // Content stream 2: page object 18.
775 ASSERT_EQ(19, FPDFPage_CountObjects(page));
776 for (int i = 0; i < 19; i++) {
777 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
778 ASSERT_TRUE(page_object);
779 CPDF_PageObject* cpdf_page_object =
780 CPDFPageObjectFromFPDFPageObject(page_object);
781 if (i < 15)
782 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
783 else if (i < 18)
784 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
785 else
786 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
787 }
788
789 UnloadPage(page);
790}
791
Henrique Nakashima0dcf1f42018-06-21 18:51:15 +0000792TEST_F(FPDFEditEmbeddertest, RemoveAllFromStream) {
793 // Load document with some text split across streams.
794 EXPECT_TRUE(OpenDocument("split_streams.pdf"));
795 FPDF_PAGE page = LoadPage(0);
796 ASSERT_TRUE(page);
797
798 // Content stream 0: page objects 0-14.
799 // Content stream 1: page objects 15-17.
800 // Content stream 2: page object 18.
801 ASSERT_EQ(19, FPDFPage_CountObjects(page));
802
803 // Loop backwards because objects will being removed, which shifts the indexes
804 // after the removed position.
805 for (int i = 18; i >= 0; i--) {
806 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
807 ASSERT_TRUE(page_object);
808 CPDF_PageObject* cpdf_page_object =
809 CPDFPageObjectFromFPDFPageObject(page_object);
810
811 // Empty content stream 1.
812 if (cpdf_page_object->GetContentStream() == 1) {
813 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
814 FPDFPageObj_Destroy(page_object);
815 }
816 }
817
818 // Content stream 0: page objects 0-14.
819 // Content stream 2: page object 15.
820 ASSERT_EQ(16, FPDFPage_CountObjects(page));
821 for (int i = 0; i < 16; i++) {
822 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
823 ASSERT_TRUE(page_object);
824 CPDF_PageObject* cpdf_page_object =
825 CPDFPageObjectFromFPDFPageObject(page_object);
826 if (i < 15)
827 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
828 else
829 EXPECT_EQ(2, cpdf_page_object->GetContentStream()) << i;
830 }
831
832 // Generate contents should remove the empty stream and update the page
833 // objects' contents stream indexes.
834 EXPECT_TRUE(FPDFPage_GenerateContent(page));
835
836 // Content stream 0: page objects 0-14.
837 // Content stream 1: page object 15.
838 ASSERT_EQ(16, FPDFPage_CountObjects(page));
839 for (int i = 0; i < 16; i++) {
840 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
841 ASSERT_TRUE(page_object);
842 CPDF_PageObject* cpdf_page_object =
843 CPDFPageObjectFromFPDFPageObject(page_object);
844 if (i < 15)
845 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
846 else
847 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
848 }
849
850#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
851 const char kStream1RemovedMD5[] = "d2e21fbd5a6de563f619feeeb6163331";
852#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
853 const char kStream1RemovedMD5[] = "b4140f203523e38793283a5943d8075b";
854#else
855 const char kStream1RemovedMD5[] = "e86a3efc160ede6cfcb1f59bcacf1105";
856#endif
857 {
858 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
859 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
860 }
861
862 // Save the file
863 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
864 UnloadPage(page);
865
866 // Re-open the file and check the page object count is still 16, and that
867 // content stream 1 was removed.
868 OpenSavedDocument();
869 FPDF_PAGE saved_page = LoadSavedPage(0);
870
871 // Content stream 0: page objects 0-14.
872 // Content stream 1: page object 15.
873 EXPECT_EQ(16, FPDFPage_CountObjects(saved_page));
874 for (int i = 0; i < 16; i++) {
875 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(saved_page, i);
876 ASSERT_TRUE(page_object);
877 CPDF_PageObject* cpdf_page_object =
878 CPDFPageObjectFromFPDFPageObject(page_object);
879 if (i < 15)
880 EXPECT_EQ(0, cpdf_page_object->GetContentStream()) << i;
881 else
882 EXPECT_EQ(1, cpdf_page_object->GetContentStream()) << i;
883 }
884
885 {
886 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
887 CompareBitmap(page_bitmap.get(), 200, 200, kStream1RemovedMD5);
888 }
889
890 CloseSavedPage(saved_page);
891 CloseSavedDocument();
892}
893
894TEST_F(FPDFEditEmbeddertest, RemoveAllFromSingleStream) {
895 // Load document with a single stream.
896 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
897 FPDF_PAGE page = LoadPage(0);
898 ASSERT_TRUE(page);
899
900 // Content stream 0: page objects 0-1.
901 ASSERT_EQ(2, FPDFPage_CountObjects(page));
902
903 // Loop backwards because objects will being removed, which shifts the indexes
904 // after the removed position.
905 for (int i = 1; i >= 0; i--) {
906 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
907 ASSERT_TRUE(page_object);
908 CPDF_PageObject* cpdf_page_object =
909 CPDFPageObjectFromFPDFPageObject(page_object);
910 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
911 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
912 FPDFPageObj_Destroy(page_object);
913 }
914
915 // No more objects in the stream
916 ASSERT_EQ(0, FPDFPage_CountObjects(page));
917
918 // Generate contents should remove the empty stream and update the page
919 // objects' contents stream indexes.
920 EXPECT_TRUE(FPDFPage_GenerateContent(page));
921
922 ASSERT_EQ(0, FPDFPage_CountObjects(page));
923
924 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
925 {
926 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
927 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
928 }
929
930 // Save the file
931 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
932 UnloadPage(page);
933
934 // Re-open the file and check the page object count is still 0.
935 OpenSavedDocument();
936 FPDF_PAGE saved_page = LoadSavedPage(0);
937
938 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
939 {
940 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
941 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
942 }
943
944 CloseSavedPage(saved_page);
945 CloseSavedDocument();
946}
947
948TEST_F(FPDFEditEmbeddertest, RemoveFirstFromSingleStream) {
949 // Load document with a single stream.
950 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
951 FPDF_PAGE page = LoadPage(0);
952 ASSERT_TRUE(page);
953
954 // Content stream 0: page objects 0-1.
955 ASSERT_EQ(2, FPDFPage_CountObjects(page));
956
957 // Remove first object.
958 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
959 ASSERT_TRUE(page_object);
960 CPDF_PageObject* cpdf_page_object =
961 CPDFPageObjectFromFPDFPageObject(page_object);
962 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
963 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
964 FPDFPageObj_Destroy(page_object);
965
966 // One object left in the stream.
967 ASSERT_EQ(1, FPDFPage_CountObjects(page));
968 page_object = FPDFPage_GetObject(page, 0);
969 ASSERT_TRUE(page_object);
970 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
971 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
972
973 EXPECT_TRUE(FPDFPage_GenerateContent(page));
974
975 // Still one object left in the stream.
976 ASSERT_EQ(1, FPDFPage_CountObjects(page));
977 page_object = FPDFPage_GetObject(page, 0);
978 ASSERT_TRUE(page_object);
979 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
980 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
981
982#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
983 const char kFirstRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
984#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
985 const char kFirstRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
986#else
987 const char kFirstRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
988#endif
989 {
990 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
991 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
992 }
993
994 // Save the file
995 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
996 UnloadPage(page);
997
998 // Re-open the file and check the page object count is still 0.
999 OpenSavedDocument();
1000 FPDF_PAGE saved_page = LoadSavedPage(0);
1001
1002 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1003 page_object = FPDFPage_GetObject(saved_page, 0);
1004 ASSERT_TRUE(page_object);
1005 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1006 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1007 {
1008 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1009 CompareBitmap(page_bitmap.get(), 200, 200, kFirstRemovedMD5);
1010 }
1011
1012 CloseSavedPage(saved_page);
1013 CloseSavedDocument();
1014}
1015
1016TEST_F(FPDFEditEmbeddertest, RemoveLastFromSingleStream) {
1017 // Load document with a single stream.
1018 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1019 FPDF_PAGE page = LoadPage(0);
1020 ASSERT_TRUE(page);
1021
1022 // Content stream 0: page objects 0-1.
1023 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1024
1025 // Remove last object
1026 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 1);
1027 ASSERT_TRUE(page_object);
1028 CPDF_PageObject* cpdf_page_object =
1029 CPDFPageObjectFromFPDFPageObject(page_object);
1030 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1031 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1032 FPDFPageObj_Destroy(page_object);
1033
1034 // One object left in the stream.
1035 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1036 page_object = FPDFPage_GetObject(page, 0);
1037 ASSERT_TRUE(page_object);
1038 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1039 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1040
1041 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1042
1043 // Still one object left in the stream.
1044 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1045 page_object = FPDFPage_GetObject(page, 0);
1046 ASSERT_TRUE(page_object);
1047 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1048 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1049
1050#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1051 const char kLastRemovedMD5[] = "f8fbd14a048b9e2ea8e5f059f22a910e";
1052#else
1053 const char kLastRemovedMD5[] = "93dcc09055f87a2792c8e3065af99a1b";
1054#endif
1055 {
1056 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1057 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1058 }
1059
1060 // Save the file
1061 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1062 UnloadPage(page);
1063
1064 // Re-open the file and check the page object count is still 0.
1065 OpenSavedDocument();
1066 FPDF_PAGE saved_page = LoadSavedPage(0);
1067
1068 ASSERT_EQ(1, FPDFPage_CountObjects(saved_page));
1069 page_object = FPDFPage_GetObject(saved_page, 0);
1070 ASSERT_TRUE(page_object);
1071 cpdf_page_object = CPDFPageObjectFromFPDFPageObject(page_object);
1072 ASSERT_EQ(0, cpdf_page_object->GetContentStream());
1073 {
1074 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1075 CompareBitmap(page_bitmap.get(), 200, 200, kLastRemovedMD5);
1076 }
1077
1078 CloseSavedPage(saved_page);
1079 CloseSavedDocument();
1080}
1081
1082TEST_F(FPDFEditEmbeddertest, RemoveAllFromMultipleStreams) {
1083 // Load document with some text.
1084 EXPECT_TRUE(OpenDocument("hello_world_split_streams.pdf"));
1085 FPDF_PAGE page = LoadPage(0);
1086 ASSERT_TRUE(page);
1087
1088 // Content stream 0: page objects 0-1.
1089 // Content stream 1: page object 2.
1090 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1091
1092 // Loop backwards because objects will being removed, which shifts the indexes
1093 // after the removed position.
1094 for (int i = 2; i >= 0; i--) {
1095 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
1096 ASSERT_TRUE(page_object);
1097 ASSERT_TRUE(FPDFPage_RemoveObject(page, page_object));
1098 FPDFPageObj_Destroy(page_object);
1099 }
1100
1101 // No more objects in the page.
1102 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1103
1104 // Generate contents should remove the empty streams and update the page
1105 // objects' contents stream indexes.
1106 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1107
1108 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1109
1110 const char kAllRemovedMD5[] = "eee4600ac08b458ac7ac2320e225674c";
1111 {
1112 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
1113 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1114 }
1115
1116 // Save the file
1117 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1118 UnloadPage(page);
1119
1120 // Re-open the file and check the page object count is still 0.
1121 OpenSavedDocument();
1122 FPDF_PAGE saved_page = LoadSavedPage(0);
1123
1124 EXPECT_EQ(0, FPDFPage_CountObjects(saved_page));
1125 {
1126 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(saved_page, nullptr, 0);
1127 CompareBitmap(page_bitmap.get(), 200, 200, kAllRemovedMD5);
1128 }
1129
1130 CloseSavedPage(saved_page);
1131 CloseSavedDocument();
1132}
1133
Henrique Nakashimac49e62e2018-04-16 20:58:47 +00001134TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) {
1135 // Load document with some text.
1136 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1137 FPDF_PAGE page = LoadPage(0);
1138 ASSERT_TRUE(page);
1139
1140 // Add a red rectangle.
1141 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1142 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1143 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1144 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1145 FPDFPage_InsertObject(page, red_rect);
1146
1147 // Verify the red rectangle was added.
1148 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1149
1150 // Save the file
1151 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1152 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1153 UnloadPage(page);
1154
1155 // Re-open the file and check the page object count is still 3.
1156 OpenSavedDocument();
1157 FPDF_PAGE saved_page = LoadSavedPage(0);
1158 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1159 CloseSavedPage(saved_page);
1160 CloseSavedDocument();
1161}
1162
Henrique Nakashima27cf78d2018-06-14 16:22:30 +00001163TEST_F(FPDFEditEmbeddertest, InsertPageObjectEditAndSave) {
1164 // Load document with some text.
1165 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1166 FPDF_PAGE page = LoadPage(0);
1167 ASSERT_TRUE(page);
1168
1169 // Add a red rectangle.
1170 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1171 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1172 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 100, 100, 255));
1173 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1174 FPDFPage_InsertObject(page, red_rect);
1175
1176 // Verify the red rectangle was added.
1177 ASSERT_EQ(3, FPDFPage_CountObjects(page));
1178
1179 // Generate content but change it again
1180 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1181 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1182
1183 // Save the file
1184 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1185 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1186 UnloadPage(page);
1187
1188 // Re-open the file and check the page object count is still 3.
1189 OpenSavedDocument();
1190 FPDF_PAGE saved_page = LoadSavedPage(0);
1191 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
1192 CloseSavedPage(saved_page);
1193 CloseSavedDocument();
1194}
1195
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001196TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) {
1197 // Start with a blank page.
1198 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1199 ASSERT_TRUE(page);
1200
1201 // Render the blank page and verify it's a blank bitmap.
1202 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
1203 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001204 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001205 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1206 }
1207 ASSERT_EQ(0, FPDFPage_CountObjects(page));
1208
1209 // Add a red rectangle.
1210 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1211 ASSERT_TRUE(red_rect);
1212 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1213 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1214 FPDFPage_InsertObject(page, red_rect);
1215 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
1216 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001217 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001218 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
1219 }
1220 EXPECT_EQ(1, FPDFPage_CountObjects(page));
1221
1222 // Remove rectangle and verify it does not render anymore and the bitmap is
1223 // back to a blank one.
1224 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
1225 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001226 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +00001227 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
1228 }
1229 EXPECT_EQ(0, FPDFPage_CountObjects(page));
1230
1231 // Trying to remove an object not in the page should return false.
1232 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
1233
1234 FPDF_ClosePage(page);
1235 FPDFPageObj_Destroy(red_rect);
1236}
1237
Miklos Vajna12abfd02017-09-15 07:49:03 +02001238TEST_F(FPDFEditEmbeddertest, PathsPoints) {
1239 CreateNewDocument();
1240 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
1241 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +02001242 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001243
1244 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +02001245 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +02001246
Miklos Vajna36eed872017-09-20 22:52:43 +02001247 // FPDFPath_GetPathSegment() with a non-path.
1248 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
1249 // FPDFPath_GetPathSegment() with a NULL path.
1250 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
1251 float x;
1252 float y;
1253 // FPDFPathSegment_GetPoint() with a NULL segment.
1254 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
1255
1256 // FPDFPathSegment_GetType() with a NULL segment.
1257 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
1258
1259 // FPDFPathSegment_GetClose() with a NULL segment.
1260 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
1261
Miklos Vajna12abfd02017-09-15 07:49:03 +02001262 FPDFPageObj_Destroy(img);
1263}
1264
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001265TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
1266 // Load document with some text
1267 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
1268 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001269 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001270
1271 // Add an opaque rectangle on top of some of the text.
1272 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
1273 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1274 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1275 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001276
1277 // Add a transparent triangle on top of other part of the text.
1278 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
1279 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
1280 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
1281 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
1282 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
1283 EXPECT_TRUE(FPDFPath_Close(black_path));
1284 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001285
1286 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001287 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -04001288#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -07001289 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001290#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001291 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001292#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001293 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -05001294 UnloadPage(page);
1295}
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001296
wileyryae858aa42017-05-31 14:49:05 -05001297TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
1298 // Load document with existing content
1299 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
1300 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001301 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -05001302
1303 // Add a transparent rectangle on top of the existing content
1304 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
1305 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
1306 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
1307 FPDFPage_InsertObject(page, red_rect2);
1308
1309 // Add an opaque rectangle on top of the existing content
1310 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
1311 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
1312 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
1313 FPDFPage_InsertObject(page, red_rect);
1314
Tom Sepeze08d2b12018-04-25 18:49:32 +00001315 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001316 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001317 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1318
1319 // Now save the result, closing the page and document
1320 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -04001321 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -05001322
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001323 OpenSavedDocument();
Lei Zhang107fa7b2018-02-09 21:48:15 +00001324 FPDF_PAGE saved_page = LoadSavedPage(0);
1325 VerifySavedRendering(saved_page, 612, 792,
1326 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -05001327
1328 ClearString();
1329 // Add another opaque rectangle on top of the existing content
1330 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
1331 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
1332 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001333 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -05001334
1335 // Add another transparent rectangle on top of existing content
1336 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
1337 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
1338 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001339 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001340 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
1341 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001342 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +00001343 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
1344 }
Lei Zhang107fa7b2018-02-09 21:48:15 +00001345 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -05001346
1347 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +00001348 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001349
Lei Zhang107fa7b2018-02-09 21:48:15 +00001350 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001351 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -05001352
1353 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +00001354 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -05001355}
1356
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001357TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
1358 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001359 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001360
1361 // Add a large stroked rectangle (fill color should not affect it).
1362 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
1363 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
1364 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
1365 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
Miklos Vajna366df7f2018-05-22 14:27:29 +00001366
1367 float width = 0;
1368 EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width));
1369 EXPECT_EQ(15.0f, width);
1370
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001371 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
1372 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001373 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001374 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001375 CompareBitmap(page_bitmap.get(), 612, 792,
1376 "64bd31f862a89e0a9e505a5af6efd506");
1377 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001378
1379 // Add crossed-checkmark
1380 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
1381 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
1382 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
1383 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
1384 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
1385 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
1386 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
1387 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
1388 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001389 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001390 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001391 CompareBitmap(page_bitmap.get(), 612, 792,
1392 "4b6f3b9d25c4e194821217d5016c3724");
1393 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001394
1395 // Add stroked and filled oval-ish path.
1396 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
1397 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
1398 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
1399 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
1400 EXPECT_TRUE(FPDFPath_Close(path));
1401 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
1402 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
1403 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
1404 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
1405 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001406 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001407 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001408 CompareBitmap(page_bitmap.get(), 612, 792,
1409 "ff3e6a22326754944cc6e56609acd73b");
1410 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001411 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -05001412}
Nicolas Pena49058402017-02-14 18:26:20 -05001413
Nicolas Pena4c48b102018-06-13 18:23:46 +00001414// Tests adding text from standard font using FPDFPageObj_NewTextObj.
Nicolas Pena49058402017-02-14 18:26:20 -05001415TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
1416 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001417 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -05001418
1419 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001420 FPDF_PAGEOBJECT text_object1 =
1421 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1422 EXPECT_TRUE(text_object1);
1423 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
1424 GetFPDFWideString(L"I'm at the bottom of the page");
1425 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
1426 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
1427 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001428 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001429 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001430#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001431 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -05001432#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001433 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001434#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001435 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1436 }
Nicolas Pena49058402017-02-14 18:26:20 -05001437
1438 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -04001439 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001440 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001441 EXPECT_TRUE(text_object2);
1442 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1443 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
1444 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1445 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
1446 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001447 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001448 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001449#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +00001450 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -04001451#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +00001452 const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d";
Nicolas Pena49058402017-02-14 18:26:20 -05001453#else
Dan Sinclair971a6742018-03-28 19:23:25 +00001454 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001455#endif
Dan Sinclair971a6742018-03-28 19:23:25 +00001456 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001457 }
Nicolas Pena49058402017-02-14 18:26:20 -05001458
1459 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -04001460 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -05001461 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -04001462 EXPECT_TRUE(text_object3);
1463 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
1464 GetFPDFWideString(L"Can you read me? <:)>");
1465 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
1466 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
1467 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001468 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001469 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001470#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001471 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -04001472#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001473 const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2";
Nicolas Pena49058402017-02-14 18:26:20 -05001474#else
Lei Zhang107fa7b2018-02-09 21:48:15 +00001475 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -04001476#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +00001477 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
1478 }
Nicolas Pena49058402017-02-14 18:26:20 -05001479
Miklos Vajnac765d2a2018-06-19 15:45:42 +00001480 double matrix_a = 0;
1481 double matrix_b = 0;
1482 double matrix_c = 0;
1483 double matrix_d = 0;
1484 double matrix_e = 0;
1485 double matrix_f = 0;
1486 EXPECT_FALSE(FPDFText_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
1487 &matrix_d, &matrix_e, &matrix_f));
1488 EXPECT_TRUE(FPDFText_GetMatrix(text_object3, &matrix_a, &matrix_b, &matrix_c,
1489 &matrix_d, &matrix_e, &matrix_f));
1490 EXPECT_EQ(1., matrix_a);
1491 EXPECT_EQ(1.5, matrix_b);
1492 EXPECT_EQ(2., matrix_c);
1493 EXPECT_EQ(0.5, matrix_d);
1494 EXPECT_EQ(200., matrix_e);
1495 EXPECT_EQ(200., matrix_f);
1496
Miklos Vajna8625d3b2018-06-26 15:12:48 +00001497 EXPECT_EQ(0, FPDFTextObj_GetFontSize(nullptr));
1498 EXPECT_EQ(20, FPDFTextObj_GetFontSize(text_object3));
1499
Nicolas Pena49058402017-02-14 18:26:20 -05001500 // TODO(npm): Why are there issues with text rotated by 90 degrees?
1501 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
1502 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -05001503}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001504
Miklos Vajna1448cc12018-07-03 13:52:33 +00001505TEST_F(FPDFEditEmbeddertest, TestGetTextRenderMode) {
1506 EXPECT_TRUE(OpenDocument("text_render_mode.pdf"));
1507 FPDF_PAGE page = LoadPage(0);
1508 ASSERT_TRUE(page);
1509 ASSERT_EQ(2, FPDFPage_CountObjects(page));
1510
1511 ASSERT_EQ(-1, FPDFText_GetTextRenderMode(nullptr));
1512
1513 FPDF_PAGEOBJECT fill = FPDFPage_GetObject(page, 0);
1514 ASSERT_EQ(FPDF_TEXTRENDERMODE_FILL, FPDFText_GetTextRenderMode(fill));
1515
1516 FPDF_PAGEOBJECT stroke = FPDFPage_GetObject(page, 1);
1517 ASSERT_EQ(FPDF_TEXTRENDERMODE_STROKE, FPDFText_GetTextRenderMode(stroke));
1518
1519 UnloadPage(page);
1520}
1521
Nicolas Pena4c48b102018-06-13 18:23:46 +00001522// Tests adding text from standard font using FPDFText_LoadStandardFont.
1523TEST_F(FPDFEditEmbeddertest, AddStandardFontText2) {
1524 // Start with a blank page
1525 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
1526
1527 // Load a standard font.
1528 FPDF_FONT font = FPDFText_LoadStandardFont(document(), "Helvetica");
1529 ASSERT_TRUE(font);
1530
1531 // Add some text to the page.
1532 FPDF_PAGEOBJECT text_object =
1533 FPDFPageObj_CreateTextObj(document(), font, 12.0f);
1534 EXPECT_TRUE(text_object);
1535 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1536 GetFPDFWideString(L"I'm at the bottom of the page");
1537 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1538 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 20, 20);
1539 FPDFPage_InsertObject(page.get(), text_object);
1540 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page.get(), nullptr, 0);
1541#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
1542 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
1543#else
1544 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
1545#endif
1546 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1547}
1548
1549TEST_F(FPDFEditEmbeddertest, LoadStandardFonts) {
1550 CreateNewDocument();
1551 const char* standard_font_names[] = {"Arial",
1552 "Arial-Bold",
1553 "Arial-BoldItalic",
1554 "Arial-Italic",
1555 "Courier",
1556 "Courier-BoldOblique",
1557 "Courier-Oblique",
1558 "Courier-Bold",
1559 "CourierNew",
1560 "CourierNew-Bold",
1561 "CourierNew-BoldItalic",
1562 "CourierNew-Italic",
1563 "Helvetica",
1564 "Helvetica-Bold",
1565 "Helvetica-BoldOblique",
1566 "Helvetica-Oblique",
1567 "Symbol",
1568 "TimesNewRoman",
1569 "TimesNewRoman-Bold",
1570 "TimesNewRoman-BoldItalic",
1571 "TimesNewRoman-Italic",
1572 "ZapfDingbats"};
1573 for (auto* const font_name : standard_font_names) {
1574 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
1575 EXPECT_TRUE(font) << font_name << " should be considered a standard font.";
1576 }
1577 const char* not_standard_font_names[] = {
1578 "Abcdefg", "ArialB", "Arial-Style",
1579 "Font Name", "FontArial", "NotAStandardFontName",
1580 "TestFontName", "Quack", "Symbol-Italic",
1581 "Zapf"};
1582 for (auto* const font_name : not_standard_font_names) {
1583 FPDF_FONT font = FPDFText_LoadStandardFont(document(), font_name);
1584 EXPECT_FALSE(font) << font_name
1585 << " should not be considered a standard font.";
1586 }
1587}
1588
Nicolas Pena603a31d2017-06-14 11:41:18 -04001589TEST_F(FPDFEditEmbeddertest, GraphicsData) {
1590 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +00001591 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001592
1593 // Create a rect with nontrivial graphics
1594 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1595 FPDFPageObj_SetBlendMode(rect1, "Color");
1596 FPDFPage_InsertObject(page.get(), rect1);
1597 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
1598
1599 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +00001600 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
1601 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -04001602 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001603 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001604
1605 // Add a text object causing no change to the graphics dictionary
1606 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1607 // Only alpha, the last component, matters for the graphics dictionary. And
1608 // the default value is 255.
1609 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
1610 FPDFPage_InsertObject(page.get(), text1);
1611 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001612 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001613
1614 // Add a text object increasing the size of the graphics dictionary
1615 FPDF_PAGEOBJECT text2 =
1616 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
1617 FPDFPage_InsertObject(page.get(), text2);
1618 FPDFPageObj_SetBlendMode(text2, "Darken");
1619 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
1620 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001621 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001622
1623 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -04001624 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -04001625 FPDFPageObj_SetBlendMode(path, "Darken");
1626 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
1627 FPDFPage_InsertObject(page.get(), path);
1628 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001629 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001630
1631 // Add a rect increasing the size of the graphics dictionary
1632 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1633 FPDFPageObj_SetBlendMode(rect2, "Darken");
1634 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
1635 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
1636 FPDFPage_InsertObject(page.get(), rect2);
1637 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001638 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -04001639}
1640
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001641TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
1642 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -05001643 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001644
1645 // Add a red rectangle with some non-default alpha
1646 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
1647 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
1648 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
1649 FPDFPage_InsertObject(page, rect);
1650 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1651
1652 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +00001653 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
1654 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001655 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001656 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001657
1658 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +00001659 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001660 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001661 CompareBitmap(page_bitmap.get(), 612, 792,
1662 "5384da3406d62360ffb5cac4476fff1c");
1663 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001664
1665 // Never mind, my new favorite color is blue, increase alpha
1666 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
1667 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001668 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001669
1670 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +00001671 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001672 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001673 CompareBitmap(page_bitmap.get(), 612, 792,
1674 "2e51656f5073b0bee611d9cd086aa09c");
1675 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001676
1677 // And now generate, without changes
1678 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001679 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001680 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001681 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001682 CompareBitmap(page_bitmap.get(), 612, 792,
1683 "2e51656f5073b0bee611d9cd086aa09c");
1684 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001685
1686 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001687 FPDF_PAGEOBJECT text_object =
1688 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1689 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1690 GetFPDFWideString(L"Something something #text# something");
1691 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1692 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
1693 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001694 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001695 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001696 ASSERT_TRUE(font_dict);
1697 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1698
1699 // Generate yet again, check dicts are reasonably sized
1700 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001701 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001702 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1703 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001704}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001705
Nicolas Penad03ca422017-03-06 13:54:33 -05001706TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
1707 CreateNewDocument();
1708 // TODO(npm): use other fonts after disallowing loading any font as any type
1709 const CPDF_Font* stock_font =
1710 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001711 const uint8_t* data = stock_font->GetFont()->GetFontData();
1712 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001713 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001714 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
1715 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001716 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001717 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001718
Lei Zhang710fa992018-05-25 16:24:48 +00001719 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05001720 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1721 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
1722 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
1723 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1724 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1725 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05001726 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
1727
Lei Zhangde579ab2018-05-25 21:49:49 +00001728 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001729 ASSERT_TRUE(widths_array);
1730 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001731 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05001732 EXPECT_EQ(569, widths_array->GetNumberAt(11));
1733 EXPECT_EQ(500, widths_array->GetNumberAt(223));
1734 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
1735}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001736
Nicolas Penad03ca422017-03-06 13:54:33 -05001737TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
1738 CreateNewDocument();
1739 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001740 const uint8_t* data = stock_font->GetFont()->GetFontData();
1741 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001742 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001743 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
1744 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001745 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001746 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001747
Lei Zhang710fa992018-05-25 16:24:48 +00001748 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001749 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1750 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
1751 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
1752 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1753 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1754 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
1755 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05001756
Lei Zhangde579ab2018-05-25 21:49:49 +00001757 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001758 ASSERT_TRUE(widths_array);
1759 ASSERT_EQ(224U, widths_array->GetCount());
1760 EXPECT_EQ(600, widths_array->GetNumberAt(33));
1761 EXPECT_EQ(600, widths_array->GetNumberAt(74));
1762 EXPECT_EQ(600, widths_array->GetNumberAt(223));
1763 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
1764}
1765
1766TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
1767 CreateNewDocument();
1768 const CPDF_Font* stock_font =
1769 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001770 const uint8_t* data = stock_font->GetFont()->GetFontData();
1771 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001772 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001773 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
1774 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001775 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001776 EXPECT_TRUE(typed_font->IsCIDFont());
1777
1778 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001779 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001780 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1781 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1782 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
1783 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001784 const CPDF_Array* descendant_array =
1785 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001786 ASSERT_TRUE(descendant_array);
1787 EXPECT_EQ(1U, descendant_array->GetCount());
1788
1789 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001790 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001791 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1792 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
1793 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001794 const CPDF_Dictionary* cidinfo_dict =
1795 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001796 ASSERT_TRUE(cidinfo_dict);
1797 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1798 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1799 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1800 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
1801
1802 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001803 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001804 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001805 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -05001806 CheckCompositeFontWidths(widths_array, typed_font);
1807}
1808
1809TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
1810 CreateNewDocument();
1811 const CPDF_Font* stock_font =
1812 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001813 const uint8_t* data = stock_font->GetFont()->GetFontData();
1814 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -05001815
Tom Sepeze08d2b12018-04-25 18:49:32 +00001816 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001817 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1818 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001819 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001820 EXPECT_TRUE(typed_font->IsCIDFont());
1821
1822 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001823 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001824 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1825 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1826 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
1827 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001828 const CPDF_Array* descendant_array =
1829 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001830 ASSERT_TRUE(descendant_array);
1831 EXPECT_EQ(1U, descendant_array->GetCount());
1832
1833 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001834 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001835 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1836 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
1837 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001838 const CPDF_Dictionary* cidinfo_dict =
1839 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001840 ASSERT_TRUE(cidinfo_dict);
1841 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1842 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1843 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1844 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
1845 data);
1846
1847 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001848 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001849 ASSERT_TRUE(widths_array);
1850 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05001851}
rbpotterce8e51e2017-04-28 12:42:47 -07001852
1853TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
1854 // Load document with a -90 degree rotation
1855 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
1856 FPDF_PAGE page = LoadPage(0);
1857 EXPECT_NE(nullptr, page);
1858
1859 EXPECT_EQ(3, FPDFPage_GetRotation(page));
1860 UnloadPage(page);
1861}
Nicolas Penab3161852017-05-02 14:12:50 -04001862
1863TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
1864 // Start with a blank page
1865 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1866 {
1867 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001868 const uint8_t* data = stock_font->GetFont()->GetFontData();
1869 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001870 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001871 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
1872 ASSERT_TRUE(font.get());
1873
1874 // Add some text to the page
1875 FPDF_PAGEOBJECT text_object =
1876 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1877 EXPECT_TRUE(text_object);
1878 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1879 GetFPDFWideString(L"I am testing my loaded font, WEE.");
1880 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1881 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
1882 FPDFPage_InsertObject(page, text_object);
Tom Sepeze08d2b12018-04-25 18:49:32 +00001883 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001884#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001885 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
1886#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001887 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04001888#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001889 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04001890
1891 // Add some more text, same font
1892 FPDF_PAGEOBJECT text_object2 =
1893 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
1894 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1895 GetFPDFWideString(L"Bigger font size");
1896 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1897 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
1898 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04001899 }
Tom Sepeze08d2b12018-04-25 18:49:32 +00001900 ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001901#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001902 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
1903#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001904 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04001905#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001906 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001907
Nicolas Pena207b7272017-05-26 17:37:06 -04001908 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04001909 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1910 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001911
1912 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001913}
Nicolas Penaf45ade32017-05-03 10:23:49 -04001914
Jane Liueda65252017-06-07 11:31:27 -04001915TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
1916 // Open a file with one annotation and load its first page.
1917 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001918 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04001919 ASSERT_TRUE(page);
1920
Lei Zhanga21d5932018-02-05 18:28:38 +00001921 {
1922 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001923 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001924 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
1925 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04001926
Lei Zhanga21d5932018-02-05 18:28:38 +00001927 // FPDFPage_TransformAnnots() should run without errors when modifying
1928 // annotation rectangles.
1929 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
1930 }
Jane Liueda65252017-06-07 11:31:27 -04001931 UnloadPage(page);
1932}
1933
Nicolas Penaf45ade32017-05-03 10:23:49 -04001934// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04001935#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -04001936TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
1937 // Start with a blank page
1938 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1939 CFX_Font CIDfont;
1940 {
1941 // First, get the data from the font
1942 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
1943 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
1944 const uint8_t* data = CIDfont.GetFontData();
1945 const uint32_t size = CIDfont.GetSize();
1946
1947 // Load the data into a FPDF_Font.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001948 ScopedFPDFFont font(
Nicolas Penaf45ade32017-05-03 10:23:49 -04001949 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1950 ASSERT_TRUE(font.get());
1951
1952 // Add some text to the page
1953 FPDF_PAGEOBJECT text_object =
1954 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1955 ASSERT_TRUE(text_object);
1956 std::wstring wstr = L"ABCDEFGhijklmnop.";
1957 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1958 GetFPDFWideString(wstr);
1959 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1960 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
1961 FPDFPage_InsertObject(page, text_object);
1962
1963 // And add some Japanese characters
1964 FPDF_PAGEOBJECT text_object2 =
1965 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
1966 ASSERT_TRUE(text_object2);
1967 std::wstring wstr2 =
1968 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
1969 L"\u756A";
1970 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1971 GetFPDFWideString(wstr2);
1972 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1973 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
1974 FPDFPage_InsertObject(page, text_object2);
1975 }
1976
Nicolas Pena207b7272017-05-26 17:37:06 -04001977 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00001978 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00001979 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001980 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001981 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1982 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04001983
1984 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04001985 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04001986 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1987 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001988
1989 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001990}
Dan Sinclair698aed72017-09-26 16:24:49 -04001991#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001992
1993TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04001994 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001995 {
1996 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
1997 FPDF_PAGE page = LoadPage(0);
1998 ASSERT_NE(nullptr, page);
1999
2000 // Now add a more complex blue path.
2001 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
2002 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
2003 // TODO(npm): stroking will cause the MD5s to differ.
2004 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
2005 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
2006 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
2007 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
2008 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
2009 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
2010 EXPECT_TRUE(FPDFPath_Close(green_path));
2011 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00002012 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00002013 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002014
2015 // Now save the result, closing the page and document
2016 EXPECT_TRUE(FPDFPage_GenerateContent(page));
2017 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
2018 UnloadPage(page);
2019 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04002020
2021 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04002022}
Jane Liu28fb7ba2017-08-02 21:45:57 -04002023
Henrique Nakashima144107d2018-07-10 21:04:05 +00002024TEST_F(FPDFEditEmbeddertest, AddMarkedText) {
2025 // Start with a blank page.
2026 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
2027
2028 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
2029 const uint8_t* data = stock_font->GetFont()->GetFontData();
2030 const uint32_t size = stock_font->GetFont()->GetSize();
2031 ScopedFPDFFont font(
2032 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
2033 ASSERT_TRUE(font.get());
2034
2035 // Add some text to the page.
2036 FPDF_PAGEOBJECT text_object =
2037 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
2038
2039 EXPECT_TRUE(text_object);
2040 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
2041 GetFPDFWideString(L"I am testing my loaded font, WEE.");
2042 EXPECT_TRUE(FPDFText_SetText(text_object, text1.get()));
2043 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
2044 FPDFPage_InsertObject(page, text_object);
2045
2046 // Add a mark with the tag "TestMarkName" to that text.
2047 EXPECT_EQ(0, FPDFPageObj_CountMarks(text_object));
2048 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_AddMark(text_object, "TestMarkName");
2049 EXPECT_TRUE(mark);
2050 EXPECT_EQ(1, FPDFPageObj_CountMarks(text_object));
2051 EXPECT_EQ(mark, FPDFPageObj_GetMark(text_object, 0));
2052 char buffer[256];
2053 EXPECT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
2054 std::wstring name =
2055 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2056 EXPECT_EQ(L"TestMarkName", name);
2057
2058 // Add parameters:
2059 // - int "IntKey" : 42
2060 // - string "StringKey": "StringValue"
2061 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
2062 EXPECT_TRUE(FPDFPageObjMark_SetIntParam(document(), mark, "IntKey", 42));
2063 EXPECT_TRUE(FPDFPageObjMark_SetStringParam(document(), mark, "StringKey",
2064 "StringValue"));
2065 EXPECT_EQ(2, FPDFPageObjMark_CountParams(mark));
2066
2067 // Check the two parameters can be retrieved.
2068 EXPECT_EQ(FPDF_OBJECT_NUMBER,
2069 FPDFPageObjMark_GetParamValueTypeByKey(mark, "IntKey"));
2070 int int_value;
2071 EXPECT_TRUE(
2072 FPDFPageObjMark_GetParamIntValueByKey(mark, "IntKey", &int_value));
2073 EXPECT_EQ(42, int_value);
2074
2075 EXPECT_EQ(FPDF_OBJECT_STRING,
2076 FPDFPageObjMark_GetParamValueTypeByKey(mark, "StringKey"));
2077 unsigned long out_buffer_len;
2078 EXPECT_TRUE(FPDFPageObjMark_GetParamStringValueByKey(
2079 mark, "StringKey", buffer, 256, &out_buffer_len));
2080 EXPECT_GT(out_buffer_len, 0u);
2081 name = GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
2082 EXPECT_EQ(L"StringValue", name);
2083
2084// Render and check the bitmap is the expected one.
2085#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
2086 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
2087#else
2088 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
2089#endif
2090 {
2091 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
2092 CompareBitmap(page_bitmap.get(), 612, 792, md5);
2093 }
2094
2095 FPDF_ClosePage(page);
2096
2097 // TODO(pdfium:1118): Save, then re-open the file and check the changes were
2098 // kept in the saved .pdf.
2099}
2100
Jane Liu28fb7ba2017-08-02 21:45:57 -04002101TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
2102 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2103 FPDF_PAGE page = LoadPage(0);
2104 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002105 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04002106
2107 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2108 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2109 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
2110
2111 obj = FPDFPage_GetObject(page, 33);
2112 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2113 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
2114 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2115 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
2116 FPDFBitmap_Destroy(bitmap);
2117
2118 obj = FPDFPage_GetObject(page, 34);
2119 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2120 bitmap = FPDFImageObj_GetBitmap(obj);
2121 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2122 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
2123 FPDFBitmap_Destroy(bitmap);
2124
2125 obj = FPDFPage_GetObject(page, 35);
2126 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2127 bitmap = FPDFImageObj_GetBitmap(obj);
2128 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
2129 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
2130 FPDFBitmap_Destroy(bitmap);
2131
2132 obj = FPDFPage_GetObject(page, 36);
2133 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2134 bitmap = FPDFImageObj_GetBitmap(obj);
2135 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2136 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
2137 FPDFBitmap_Destroy(bitmap);
2138
2139 obj = FPDFPage_GetObject(page, 37);
2140 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2141 bitmap = FPDFImageObj_GetBitmap(obj);
2142 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2143 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
2144 FPDFBitmap_Destroy(bitmap);
2145
2146 obj = FPDFPage_GetObject(page, 38);
2147 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2148 bitmap = FPDFImageObj_GetBitmap(obj);
2149 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
2150 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
2151 FPDFBitmap_Destroy(bitmap);
2152 UnloadPage(page);
2153}
Jane Liu548334e2017-08-03 16:33:40 -04002154
Lei Zhang53341dd2018-03-01 15:42:47 +00002155TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) {
2156 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
2157 FPDF_PAGE page = LoadPage(0);
2158 ASSERT_TRUE(page);
2159 ASSERT_EQ(1, FPDFPage_CountObjects(page));
2160
2161 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
2162 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2163 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00002164 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00002165 ASSERT_TRUE(bitmap);
2166 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
2167 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00002168 }
2169
2170 UnloadPage(page);
2171}
2172
Jane Liu548334e2017-08-03 16:33:40 -04002173TEST_F(FPDFEditEmbeddertest, GetImageData) {
2174 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2175 FPDF_PAGE page = LoadPage(0);
2176 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02002177 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04002178
2179 // Retrieve an image object with flate-encoded data stream.
2180 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
2181 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2182
2183 // Check that the raw image data has the correct length and hash value.
2184 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2185 std::vector<char> buf(len);
2186 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2187 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
2188 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2189
2190 // Check that the decoded image data has the correct length and hash value.
2191 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2192 buf.clear();
2193 buf.resize(len);
2194 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2195 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
2196 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2197
2198 // Retrieve an image obejct with DCTDecode-encoded data stream.
2199 obj = FPDFPage_GetObject(page, 37);
2200 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2201
2202 // Check that the raw image data has the correct length and hash value.
2203 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
2204 buf.clear();
2205 buf.resize(len);
2206 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
2207 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2208 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2209
2210 // Check that the decoded image data has the correct length and hash value,
2211 // which should be the same as those of the raw data, since this image is
2212 // encoded by a single DCTDecode filter and decoding is a noop.
2213 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
2214 buf.clear();
2215 buf.resize(len);
2216 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
2217 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
2218 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
2219
2220 UnloadPage(page);
2221}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04002222
2223TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
2224 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
2225 ASSERT_TRUE(rect);
2226
2227 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
2228 FPDFPageObj_Destroy(rect);
2229}
Jane Liube63ab92017-08-09 14:09:34 -04002230
2231TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
2232 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
2233 FPDF_PAGE page = LoadPage(0);
2234 ASSERT_TRUE(page);
2235
2236 // Verify that retrieving the filter of a non-image object would fail.
2237 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
2238 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2239 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
2240 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
2241
2242 // Verify the returned filter string for an image object with a single filter.
2243 obj = FPDFPage_GetObject(page, 33);
2244 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2245 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
2246 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
2247 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002248 static constexpr char kFlateDecode[] = "FlateDecode";
2249 EXPECT_EQ(sizeof(kFlateDecode),
2250 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
2251 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002252 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
2253
2254 // Verify all the filters for an image object with a list of filters.
2255 obj = FPDFPage_GetObject(page, 38);
2256 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2257 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
2258 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
2259 buf.clear();
2260 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002261 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
2262 EXPECT_EQ(sizeof(kASCIIHexDecode),
2263 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
2264 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002265
2266 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
2267 buf.clear();
2268 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07002269 static constexpr char kDCTDecode[] = "DCTDecode";
2270 EXPECT_EQ(sizeof(kDCTDecode),
2271 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
2272 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04002273
2274 UnloadPage(page);
2275}
Jane Liuca898292017-08-16 11:25:35 -04002276
2277TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
2278 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
2279 FPDF_PAGE page = LoadPage(0);
2280 ASSERT_TRUE(page);
2281
2282 // Check that getting the metadata of a null object would fail.
2283 FPDF_IMAGEOBJ_METADATA metadata;
2284 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
2285
2286 // Check that receiving the metadata with a null metadata object would fail.
2287 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
2288 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
2289
2290 // Check that when retrieving an image object's metadata without passing in
2291 // |page|, all values are correct, with the last two being default values.
2292 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2293 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002294 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002295 EXPECT_EQ(92u, metadata.width);
2296 EXPECT_EQ(68u, metadata.height);
2297 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
2298 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
2299 EXPECT_EQ(0u, metadata.bits_per_pixel);
2300 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
2301
2302 // Verify the metadata of a bitmap image with indexed colorspace.
2303 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002304 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002305 EXPECT_EQ(92u, metadata.width);
2306 EXPECT_EQ(68u, metadata.height);
2307 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
2308 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
2309 EXPECT_EQ(1u, metadata.bits_per_pixel);
2310 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
2311
2312 // Verify the metadata of an image with RGB colorspace.
2313 obj = FPDFPage_GetObject(page, 37);
2314 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
2315 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05002316 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04002317 EXPECT_EQ(126u, metadata.width);
2318 EXPECT_EQ(106u, metadata.height);
2319 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
2320 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
2321 EXPECT_EQ(24u, metadata.bits_per_pixel);
2322 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
2323
2324 UnloadPage(page);
2325}