blob: a20faeba80003588aa89612139ab8b293990783c [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"
Nicolas Penabe90aae2017-02-27 10:41:41 -050012#include "core/fpdfapi/parser/cpdf_array.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050013#include "core/fpdfapi/parser/cpdf_dictionary.h"
Nicolas Penad03ca422017-03-06 13:54:33 -050014#include "core/fpdfapi/parser/cpdf_number.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050015#include "core/fpdfapi/parser/cpdf_stream.h"
Nicolas Pena0fc185e2017-02-08 12:13:20 -050016#include "core/fxcrt/fx_system.h"
Dan Sinclair00d47a62018-03-28 18:39:04 +000017#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000018#include "public/cpp/fpdf_scopers.h"
Jane Liueda65252017-06-07 11:31:27 -040019#include "public/fpdf_annot.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080020#include "public/fpdf_edit.h"
21#include "public/fpdfview.h"
22#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080023#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080024#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080025#include "testing/test_support.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080026
Nicolas Pena3ff54002017-07-05 11:55:35 -040027class FPDFEditEmbeddertest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050028 protected:
29 FPDF_DOCUMENT CreateNewDocument() {
30 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070031 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050032 return document_;
33 }
34
35 void CheckFontDescriptor(CPDF_Dictionary* font_dict,
36 int font_type,
37 bool bold,
38 bool italic,
39 uint32_t size,
40 const uint8_t* data) {
41 CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
42 ASSERT_TRUE(font_desc);
43 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
44 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
45 font_desc->GetStringFor("FontName"));
46
47 // Check that the font descriptor has the required keys according to spec
48 // 1.7 Table 5.19
49 ASSERT_TRUE(font_desc->KeyExist("Flags"));
Dan Sinclair10e1f052017-09-28 15:59:42 -040050
Nicolas Penad03ca422017-03-06 13:54:33 -050051 int font_flags = font_desc->GetIntegerFor("Flags");
Dan Sinclair10e1f052017-09-28 15:59:42 -040052 EXPECT_EQ(bold, FontStyleIsBold(font_flags));
53 EXPECT_EQ(italic, FontStyleIsItalic(font_flags));
54 EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags));
Nicolas Penad03ca422017-03-06 13:54:33 -050055 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
Nicolás Peña5f95f362017-09-28 13:00:45 +090056
57 CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox");
58 ASSERT_TRUE(fontBBox);
59 EXPECT_EQ(4U, fontBBox->GetCount());
60 // Check that the coordinates are in the preferred order according to spec
61 // 1.7 Section 3.8.4
62 EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2));
63 EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3));
64
Nicolas Penad03ca422017-03-06 13:54:33 -050065 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
66 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
67 EXPECT_TRUE(font_desc->KeyExist("Descent"));
68 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
69 EXPECT_TRUE(font_desc->KeyExist("StemV"));
Ryan Harrison275e2602017-09-18 14:23:18 -040070 ByteString present("FontFile");
71 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050072 if (font_type == FPDF_FONT_TRUETYPE)
73 std::swap(present, absent);
74 EXPECT_TRUE(font_desc->KeyExist(present));
75 EXPECT_FALSE(font_desc->KeyExist(absent));
76
77 // Check that the font stream is the one that was provided
78 CPDF_Stream* font_stream = font_desc->GetStreamFor(present);
79 ASSERT_EQ(size, font_stream->GetRawSize());
Nicolás Peña79eab232017-09-28 13:29:05 +090080 if (font_type == FPDF_FONT_TRUETYPE) {
81 ASSERT_EQ(static_cast<int>(size),
82 font_stream->GetDict()->GetIntegerFor("Length1"));
83 }
Nicolas Penad03ca422017-03-06 13:54:33 -050084 uint8_t* stream_data = font_stream->GetRawData();
85 for (size_t j = 0; j < size; j++)
86 EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j;
87 }
88
89 void CheckCompositeFontWidths(CPDF_Array* widths_array,
90 CPDF_Font* typed_font) {
91 // Check that W array is in a format that conforms to PDF spec 1.7 section
92 // "Glyph Metrics in CIDFonts" (these checks are not
93 // implementation-specific).
94 EXPECT_GT(widths_array->GetCount(), 1U);
95 int num_cids_checked = 0;
96 int cur_cid = 0;
97 for (size_t idx = 0; idx < widths_array->GetCount(); idx++) {
98 int cid = widths_array->GetNumberAt(idx);
99 EXPECT_GE(cid, cur_cid);
100 ASSERT_FALSE(++idx == widths_array->GetCount());
101 CPDF_Object* next = widths_array->GetObjectAt(idx);
102 if (next->IsArray()) {
103 // We are in the c [w1 w2 ...] case
104 CPDF_Array* arr = next->AsArray();
105 int cnt = static_cast<int>(arr->GetCount());
106 size_t inner_idx = 0;
107 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
Nicolas Pena23346602018-01-30 21:42:41 +0000108 uint32_t width = arr->GetNumberAt(inner_idx++);
Dan Sinclair971a6742018-03-28 19:23:25 +0000109 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
110 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500111 }
112 num_cids_checked += cnt;
113 continue;
114 }
115 // Otherwise, are in the c_first c_last w case.
116 ASSERT_TRUE(next->IsNumber());
117 int last_cid = next->AsNumber()->GetInteger();
118 ASSERT_FALSE(++idx == widths_array->GetCount());
Nicolas Pena23346602018-01-30 21:42:41 +0000119 uint32_t width = widths_array->GetNumberAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500120 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
Dan Sinclair971a6742018-03-28 19:23:25 +0000121 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
122 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500123 }
124 num_cids_checked += last_cid - cid + 1;
125 }
126 // Make sure we have a good amount of cids described
127 EXPECT_GT(num_cids_checked, 900);
128 }
129 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
130
131 private:
132 CPDF_Document* cpdf_doc_;
133};
Tom Sepezd483eb42016-01-06 10:03:59 -0800134
etienneb7712c262016-04-26 08:13:45 -0700135namespace {
thestigdc7ec032016-11-21 15:32:52 -0800136
etienneb7712c262016-04-26 08:13:45 -0700137const char kExpectedPDF[] =
138 "%PDF-1.7\r\n"
139 "%\xA1\xB3\xC5\xD7\r\n"
140 "1 0 obj\r\n"
141 "<</Pages 2 0 R /Type/Catalog>>\r\n"
142 "endobj\r\n"
143 "2 0 obj\r\n"
144 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
145 "endobj\r\n"
146 "3 0 obj\r\n"
147 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
148 "endobj\r\n"
149 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400150 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
151 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400152 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700153 ">>\r\n"
154 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400155 "5 0 obj\r\n"
156 "<</BM/Normal/CA 1/ca 1>>\r\n"
157 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700158 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400159 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700160 "0000000000 65535 f\r\n"
161 "0000000017 00000 n\r\n"
162 "0000000066 00000 n\r\n"
163 "0000000122 00000 n\r\n"
164 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400165 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700166 "trailer\r\n"
167 "<<\r\n"
168 "/Root 1 0 R\r\n"
169 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400170 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700171 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400172 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700173 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800174
etienneb7712c262016-04-26 08:13:45 -0700175} // namespace
176
Tom Sepezd483eb42016-01-06 10:03:59 -0800177TEST_F(FPDFEditEmbeddertest, EmptyCreation) {
178 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700179 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800180 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400181 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800182 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800183 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700184
Nicolas Penad9d6c292017-06-06 16:12:10 -0400185 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
186 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700187 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800188}
thestigdc7ec032016-11-21 15:32:52 -0800189
190// Regression test for https://crbug.com/667012
191TEST_F(FPDFEditEmbeddertest, RasterizePDF) {
192 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
193
194 // Get the bitmap for the original document/
Tom Sepeze08d2b12018-04-25 18:49:32 +0000195 ScopedFPDFBitmap orig_bitmap;
thestigdc7ec032016-11-21 15:32:52 -0800196 {
197 EXPECT_TRUE(OpenDocument("black.pdf"));
198 FPDF_PAGE orig_page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000199 ASSERT_TRUE(orig_page);
200 orig_bitmap = RenderLoadedPage(orig_page);
201 CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800202 UnloadPage(orig_page);
203 }
204
205 // Create a new document from |orig_bitmap| and save it.
206 {
207 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
208 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
209
210 // Add the bitmap to an image object and add the image object to the output
211 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700212 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000213 EXPECT_TRUE(
214 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get()));
thestigdc7ec032016-11-21 15:32:52 -0800215 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
216 FPDFPage_InsertObject(temp_page, temp_img);
217 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
218 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
219 FPDF_ClosePage(temp_page);
220 FPDF_CloseDocument(temp_doc);
221 }
thestigdc7ec032016-11-21 15:32:52 -0800222
223 // Get the generated content. Make sure it is at least as big as the original
224 // PDF.
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400225 EXPECT_GT(GetString().size(), 923U);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400226 VerifySavedDocument(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800227}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500228
229TEST_F(FPDFEditEmbeddertest, AddPaths) {
230 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500231 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000232 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500233
234 // We will first add a red rectangle
235 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000236 ASSERT_TRUE(red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500237 // Expect false when trying to set colors out of range
238 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
239 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
240
241 // Fill rectangle with red and insert to the page
242 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
243 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
244 FPDFPage_InsertObject(page, red_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000245 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000246 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000247 CompareBitmap(page_bitmap.get(), 612, 792,
248 "66d02eaa6181e2c069ce2ea99beda497");
249 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500250
251 // Now add to that a green rectangle with some medium alpha
252 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
253 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200254
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200255 // Make sure the type of the rectangle is a path.
256 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
257
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200258 // Make sure we get back the same color we set previously.
259 unsigned int R;
260 unsigned int G;
261 unsigned int B;
262 unsigned int A;
263 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
264 EXPECT_EQ(0U, R);
265 EXPECT_EQ(255U, G);
266 EXPECT_EQ(0U, B);
267 EXPECT_EQ(128U, A);
268
Miklos Vajna12abfd02017-09-15 07:49:03 +0200269 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
270 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200271 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200272 // Verify actual coordinates.
273 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
274 float x;
275 float y;
276 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
277 EXPECT_EQ(100, x);
278 EXPECT_EQ(100, y);
279 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
280 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
281 segment = FPDFPath_GetPathSegment(green_rect, 1);
282 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
283 EXPECT_EQ(100, x);
284 EXPECT_EQ(140, y);
285 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
286 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
287 segment = FPDFPath_GetPathSegment(green_rect, 2);
288 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
289 EXPECT_EQ(140, x);
290 EXPECT_EQ(140, y);
291 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
292 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
293 segment = FPDFPath_GetPathSegment(green_rect, 3);
294 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
295 EXPECT_EQ(140, x);
296 EXPECT_EQ(100, y);
297 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
298 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
299 segment = FPDFPath_GetPathSegment(green_rect, 4);
300 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
301 EXPECT_EQ(100, x);
302 EXPECT_EQ(100, y);
303 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
304 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200305
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500306 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
307 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000308 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000309 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000310 CompareBitmap(page_bitmap.get(), 612, 792,
311 "7b0b87604594e773add528fae567a558");
312 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500313
314 // Add a black triangle.
315 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
316 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
317 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
318 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
319 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
320 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200321
322 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
323 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200324 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200325 // Verify actual coordinates.
326 segment = FPDFPath_GetPathSegment(black_path, 0);
327 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
328 EXPECT_EQ(400, 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(black_path, 1);
333 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
334 EXPECT_EQ(400, x);
335 EXPECT_EQ(200, y);
336 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
337 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
338 segment = FPDFPath_GetPathSegment(black_path, 2);
339 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
340 EXPECT_EQ(300, x);
341 EXPECT_EQ(100, y);
342 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
343 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
344 // Make sure out of bounds index access fails properly.
345 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200346
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500347 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000348 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000349 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000350 CompareBitmap(page_bitmap.get(), 612, 792,
351 "eadc8020a14dfcf091da2688733d8806");
352 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500353
354 // Now add a more complex blue path.
355 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
356 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
357 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
358 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
359 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
360 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
361 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
362 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
363 EXPECT_TRUE(FPDFPath_Close(blue_path));
364 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000365 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
366 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000367 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000368 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
369 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500370
371 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400372 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500373 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500374 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500375
376 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000377 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500378}
379
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000380TEST_F(FPDFEditEmbeddertest, RemovePageObject) {
381 // Load document with some text.
382 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
383 FPDF_PAGE page = LoadPage(0);
384 ASSERT_TRUE(page);
385
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000386 // Show what the original file looks like.
387 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000388#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000389 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000390#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000391 const char kOriginalMD5[] = "e5a6fa28298db07484cd922f3e210c88";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000392#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000393 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000394#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000395 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000396 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
397 }
398
399 // Get the "Hello, world!" text object and remove it.
400 ASSERT_EQ(2, FPDFPage_CountObjects(page));
401 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
402 ASSERT_TRUE(page_object);
403 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
404
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000405 // Verify the "Hello, world!" text is gone.
406 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000407#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000408 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000409#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000410 const char kRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000411#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000412 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000413#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000414 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000415 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
416 }
417 ASSERT_EQ(1, FPDFPage_CountObjects(page));
418
419 UnloadPage(page);
420 FPDFPageObj_Destroy(page_object);
421}
422
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000423TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) {
424 // Load document with some text.
425 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
426 FPDF_PAGE page = LoadPage(0);
427 ASSERT_TRUE(page);
428
429 // Show what the original file looks like.
430 {
431#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
432 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
433#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
434 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
435#else
436 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
437#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000438 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000439 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
440 }
441
442 // Iterate over all objects, counting the number of times each content mark
443 // name appears.
444 int object_count = FPDFPage_CountObjects(page);
445 ASSERT_EQ(19, object_count);
446
447 unsigned long prime_count = 0;
448 unsigned long square_count = 0;
449 unsigned long greater_than_ten_count = 0;
450 std::vector<FPDF_PAGEOBJECT> primes;
451 for (int i = 0; i < object_count; ++i) {
452 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
453
454 int mark_count = FPDFPageObj_CountMarks(page_object);
455 for (int j = 0; j < mark_count; ++j) {
456 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
457
458 char buffer[256];
459 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
460 std::wstring name =
461 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
462 if (name == L"Prime") {
463 prime_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000464 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000465 primes.push_back(page_object);
466 } else if (name == L"Square") {
467 square_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000468 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
Henrique Nakashima132c38e2018-04-23 16:35:56 +0000469 ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u);
470 std::wstring key =
471 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
472 EXPECT_EQ(L"Factor", key);
473 EXPECT_EQ(FPDF_OBJECT_NUMBER,
474 FPDFPageObjMark_GetParamValueType(mark, 0));
475 int square_root = FPDFPageObjMark_GetParamIntValue(mark, 0);
476 EXPECT_EQ(i + 1, square_root * square_root);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000477 } else if (name == L"GreaterThanTen") {
478 greater_than_ten_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000479 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimab557bdc2018-04-23 18:04:26 +0000480 } else if (name == L"Bounds") {
481 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
482 ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u);
483 std::wstring key =
484 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
485 EXPECT_EQ(L"Position", key);
486 EXPECT_EQ(FPDF_OBJECT_STRING,
487 FPDFPageObjMark_GetParamValueType(mark, 0));
488 ASSERT_GT(FPDFPageObjMark_GetParamStringValue(mark, 0, buffer, 256),
489 0u);
490 std::wstring value =
491 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
492 EXPECT_EQ(L"Last", value);
493 EXPECT_EQ(18, i);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000494 } else {
495 FAIL();
496 }
497 }
498 }
499
500 // Expect certain number of tagged objects. The test file contains strings
501 // from 1 to 19.
502 EXPECT_EQ(8u, prime_count);
503 EXPECT_EQ(4u, square_count);
504 EXPECT_EQ(9u, greater_than_ten_count);
505
506 // Remove all objects marked with "Prime".
507 for (FPDF_PAGEOBJECT page_object : primes) {
508 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
509 FPDFPageObj_Destroy(page_object);
510 }
511
512 EXPECT_EQ(11, FPDFPage_CountObjects(page));
513
514 {
515#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
516 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
517#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
518 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
519#else
520 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
521#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000522 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000523 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
524 }
525
526 UnloadPage(page);
527}
528
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000529// Fails due to pdfium:1051.
530TEST_F(FPDFEditEmbeddertest, DISABLED_RemoveExistingPageObject) {
531 // Load document with some text.
532 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
533 FPDF_PAGE page = LoadPage(0);
534 ASSERT_TRUE(page);
535
536 // Get the "Hello, world!" text object and remove it.
537 ASSERT_EQ(2, FPDFPage_CountObjects(page));
538 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
539 ASSERT_TRUE(page_object);
540 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
541
542 // Verify the "Hello, world!" text is gone.
543 ASSERT_EQ(1, FPDFPage_CountObjects(page));
544
545 // Save the file
546 EXPECT_TRUE(FPDFPage_GenerateContent(page));
547 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
548 UnloadPage(page);
549 FPDFPageObj_Destroy(page_object);
550
551 // Re-open the file and check the page object count is still 1.
552 OpenSavedDocument();
553 FPDF_PAGE saved_page = LoadSavedPage(0);
554 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
555 CloseSavedPage(saved_page);
556 CloseSavedDocument();
557}
558
559TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) {
560 // Load document with some text.
561 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
562 FPDF_PAGE page = LoadPage(0);
563 ASSERT_TRUE(page);
564
565 // Add a red rectangle.
566 ASSERT_EQ(2, FPDFPage_CountObjects(page));
567 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
568 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
569 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
570 FPDFPage_InsertObject(page, red_rect);
571
572 // Verify the red rectangle was added.
573 ASSERT_EQ(3, FPDFPage_CountObjects(page));
574
575 // Save the file
576 EXPECT_TRUE(FPDFPage_GenerateContent(page));
577 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
578 UnloadPage(page);
579
580 // Re-open the file and check the page object count is still 3.
581 OpenSavedDocument();
582 FPDF_PAGE saved_page = LoadSavedPage(0);
583 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
584 CloseSavedPage(saved_page);
585 CloseSavedDocument();
586}
587
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000588TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) {
589 // Start with a blank page.
590 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
591 ASSERT_TRUE(page);
592
593 // Render the blank page and verify it's a blank bitmap.
594 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
595 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000596 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000597 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
598 }
599 ASSERT_EQ(0, FPDFPage_CountObjects(page));
600
601 // Add a red rectangle.
602 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
603 ASSERT_TRUE(red_rect);
604 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
605 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
606 FPDFPage_InsertObject(page, red_rect);
607 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
608 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000609 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000610 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
611 }
612 EXPECT_EQ(1, FPDFPage_CountObjects(page));
613
614 // Remove rectangle and verify it does not render anymore and the bitmap is
615 // back to a blank one.
616 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
617 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000618 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000619 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
620 }
621 EXPECT_EQ(0, FPDFPage_CountObjects(page));
622
623 // Trying to remove an object not in the page should return false.
624 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
625
626 FPDF_ClosePage(page);
627 FPDFPageObj_Destroy(red_rect);
628}
629
Miklos Vajna12abfd02017-09-15 07:49:03 +0200630TEST_F(FPDFEditEmbeddertest, PathsPoints) {
631 CreateNewDocument();
632 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
633 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +0200634 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200635
636 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +0200637 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200638
Miklos Vajna36eed872017-09-20 22:52:43 +0200639 // FPDFPath_GetPathSegment() with a non-path.
640 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
641 // FPDFPath_GetPathSegment() with a NULL path.
642 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
643 float x;
644 float y;
645 // FPDFPathSegment_GetPoint() with a NULL segment.
646 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
647
648 // FPDFPathSegment_GetType() with a NULL segment.
649 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
650
651 // FPDFPathSegment_GetClose() with a NULL segment.
652 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
653
Miklos Vajna12abfd02017-09-15 07:49:03 +0200654 FPDFPageObj_Destroy(img);
655}
656
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500657TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
658 // Load document with some text
659 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
660 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000661 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500662
663 // Add an opaque rectangle on top of some of the text.
664 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
665 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
666 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
667 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500668
669 // Add a transparent triangle on top of other part of the text.
670 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
671 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
672 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
673 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
674 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
675 EXPECT_TRUE(FPDFPath_Close(black_path));
676 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500677
678 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000679 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400680#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700681 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500682#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000683 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400684#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000685 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500686 UnloadPage(page);
687}
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500688
wileyryae858aa42017-05-31 14:49:05 -0500689TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
690 // Load document with existing content
691 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
692 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000693 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -0500694
695 // Add a transparent rectangle on top of the existing content
696 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
697 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
698 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
699 FPDFPage_InsertObject(page, red_rect2);
700
701 // Add an opaque rectangle on top of the existing content
702 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
703 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
704 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
705 FPDFPage_InsertObject(page, red_rect);
706
Tom Sepeze08d2b12018-04-25 18:49:32 +0000707 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000708 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500709 EXPECT_TRUE(FPDFPage_GenerateContent(page));
710
711 // Now save the result, closing the page and document
712 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400713 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -0500714
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400715 OpenSavedDocument();
Lei Zhang107fa7b2018-02-09 21:48:15 +0000716 FPDF_PAGE saved_page = LoadSavedPage(0);
717 VerifySavedRendering(saved_page, 612, 792,
718 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500719
720 ClearString();
721 // Add another opaque rectangle on top of the existing content
722 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
723 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
724 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000725 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -0500726
727 // Add another transparent rectangle on top of existing content
728 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
729 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
730 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000731 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000732 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
733 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000734 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000735 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
736 }
Lei Zhang107fa7b2018-02-09 21:48:15 +0000737 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -0500738
739 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +0000740 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400741
Lei Zhang107fa7b2018-02-09 21:48:15 +0000742 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400743 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -0500744
745 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +0000746 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -0500747}
748
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500749TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
750 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500751 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500752
753 // Add a large stroked rectangle (fill color should not affect it).
754 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
755 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
756 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
757 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
758 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
759 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000760 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000761 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000762 CompareBitmap(page_bitmap.get(), 612, 792,
763 "64bd31f862a89e0a9e505a5af6efd506");
764 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500765
766 // Add crossed-checkmark
767 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
768 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
769 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
770 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
771 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
772 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
773 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
774 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
775 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000776 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000777 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000778 CompareBitmap(page_bitmap.get(), 612, 792,
779 "4b6f3b9d25c4e194821217d5016c3724");
780 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500781
782 // Add stroked and filled oval-ish path.
783 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
784 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
785 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
786 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
787 EXPECT_TRUE(FPDFPath_Close(path));
788 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
789 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
790 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
791 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
792 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000793 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000794 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000795 CompareBitmap(page_bitmap.get(), 612, 792,
796 "ff3e6a22326754944cc6e56609acd73b");
797 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500798 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500799}
Nicolas Pena49058402017-02-14 18:26:20 -0500800
801TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
802 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500803 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -0500804
805 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400806 FPDF_PAGEOBJECT text_object1 =
807 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
808 EXPECT_TRUE(text_object1);
809 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
810 GetFPDFWideString(L"I'm at the bottom of the page");
811 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
812 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
813 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000814 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000815 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400816#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000817 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -0500818#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000819 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400820#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000821 CompareBitmap(page_bitmap.get(), 612, 792, md5);
822 }
Nicolas Pena49058402017-02-14 18:26:20 -0500823
824 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -0400825 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500826 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400827 EXPECT_TRUE(text_object2);
828 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
829 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
830 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
831 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
832 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000833 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000834 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400835#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000836 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -0400837#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000838 const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d";
Nicolas Pena49058402017-02-14 18:26:20 -0500839#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000840 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400841#endif
Dan Sinclair971a6742018-03-28 19:23:25 +0000842 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000843 }
Nicolas Pena49058402017-02-14 18:26:20 -0500844
845 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -0400846 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500847 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400848 EXPECT_TRUE(text_object3);
849 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
850 GetFPDFWideString(L"Can you read me? <:)>");
851 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
852 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
853 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000854 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000855 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400856#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000857 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -0400858#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000859 const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2";
Nicolas Pena49058402017-02-14 18:26:20 -0500860#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000861 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400862#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000863 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
864 }
Nicolas Pena49058402017-02-14 18:26:20 -0500865
866 // TODO(npm): Why are there issues with text rotated by 90 degrees?
867 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
868 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -0500869}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500870
Nicolas Pena603a31d2017-06-14 11:41:18 -0400871TEST_F(FPDFEditEmbeddertest, GraphicsData) {
872 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +0000873 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400874
875 // Create a rect with nontrivial graphics
876 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
877 FPDFPageObj_SetBlendMode(rect1, "Color");
878 FPDFPage_InsertObject(page.get(), rect1);
879 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
880
881 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +0000882 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
883 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -0400884 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400885 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400886
887 // Add a text object causing no change to the graphics dictionary
888 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
889 // Only alpha, the last component, matters for the graphics dictionary. And
890 // the default value is 255.
891 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
892 FPDFPage_InsertObject(page.get(), text1);
893 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400894 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400895
896 // Add a text object increasing the size of the graphics dictionary
897 FPDF_PAGEOBJECT text2 =
898 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
899 FPDFPage_InsertObject(page.get(), text2);
900 FPDFPageObj_SetBlendMode(text2, "Darken");
901 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
902 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400903 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400904
905 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -0400906 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -0400907 FPDFPageObj_SetBlendMode(path, "Darken");
908 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
909 FPDFPage_InsertObject(page.get(), path);
910 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400911 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400912
913 // Add a rect increasing the size of the graphics dictionary
914 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
915 FPDFPageObj_SetBlendMode(rect2, "Darken");
916 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
917 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
918 FPDFPage_InsertObject(page.get(), rect2);
919 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400920 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400921}
922
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500923TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
924 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500925 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500926
927 // Add a red rectangle with some non-default alpha
928 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
929 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
930 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
931 FPDFPage_InsertObject(page, rect);
932 EXPECT_TRUE(FPDFPage_GenerateContent(page));
933
934 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +0000935 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
936 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500937 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400938 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500939
940 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +0000941 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000942 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000943 CompareBitmap(page_bitmap.get(), 612, 792,
944 "5384da3406d62360ffb5cac4476fff1c");
945 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500946
947 // Never mind, my new favorite color is blue, increase alpha
948 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
949 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400950 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500951
952 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +0000953 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000954 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000955 CompareBitmap(page_bitmap.get(), 612, 792,
956 "2e51656f5073b0bee611d9cd086aa09c");
957 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500958
959 // And now generate, without changes
960 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400961 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000962 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000963 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000964 CompareBitmap(page_bitmap.get(), 612, 792,
965 "2e51656f5073b0bee611d9cd086aa09c");
966 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500967
968 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400969 FPDF_PAGEOBJECT text_object =
970 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
971 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
972 GetFPDFWideString(L"Something something #text# something");
973 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
974 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
975 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500976 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000977 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500978 ASSERT_TRUE(font_dict);
979 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
980
981 // Generate yet again, check dicts are reasonably sized
982 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400983 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500984 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
985 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500986}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500987
Nicolas Penad03ca422017-03-06 13:54:33 -0500988TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
989 CreateNewDocument();
990 // TODO(npm): use other fonts after disallowing loading any font as any type
991 const CPDF_Font* stock_font =
992 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700993 const uint8_t* data = stock_font->GetFont()->GetFontData();
994 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +0000995 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -0400996 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
997 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +0000998 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500999 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001000
Nicolas Penad03ca422017-03-06 13:54:33 -05001001 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05001002 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1003 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
1004 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
1005 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1006 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1007 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05001008 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
1009
Nicolas Penabe90aae2017-02-27 10:41:41 -05001010 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001011 ASSERT_TRUE(widths_array);
1012 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001013 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05001014 EXPECT_EQ(569, widths_array->GetNumberAt(11));
1015 EXPECT_EQ(500, widths_array->GetNumberAt(223));
1016 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
1017}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001018
Nicolas Penad03ca422017-03-06 13:54:33 -05001019TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
1020 CreateNewDocument();
1021 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001022 const uint8_t* data = stock_font->GetFont()->GetFontData();
1023 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001024 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001025 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
1026 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001027 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001028 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001029
Nicolas Penad03ca422017-03-06 13:54:33 -05001030 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
1031 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1032 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
1033 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
1034 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1035 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1036 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
1037 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05001038
Nicolas Penad03ca422017-03-06 13:54:33 -05001039 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
1040 ASSERT_TRUE(widths_array);
1041 ASSERT_EQ(224U, widths_array->GetCount());
1042 EXPECT_EQ(600, widths_array->GetNumberAt(33));
1043 EXPECT_EQ(600, widths_array->GetNumberAt(74));
1044 EXPECT_EQ(600, widths_array->GetNumberAt(223));
1045 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
1046}
1047
1048TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
1049 CreateNewDocument();
1050 const CPDF_Font* stock_font =
1051 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001052 const uint8_t* data = stock_font->GetFont()->GetFontData();
1053 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001054 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001055 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
1056 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001057 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001058 EXPECT_TRUE(typed_font->IsCIDFont());
1059
1060 // Check font dictionary entries
1061 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
1062 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1063 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1064 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
1065 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
1066 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
1067 ASSERT_TRUE(descendant_array);
1068 EXPECT_EQ(1U, descendant_array->GetCount());
1069
1070 // Check the CIDFontDict
1071 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
1072 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1073 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
1074 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
1075 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
1076 ASSERT_TRUE(cidinfo_dict);
1077 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1078 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1079 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1080 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
1081
1082 // Check widths
1083 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
1084 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001085 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -05001086 CheckCompositeFontWidths(widths_array, typed_font);
1087}
1088
1089TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
1090 CreateNewDocument();
1091 const CPDF_Font* stock_font =
1092 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001093 const uint8_t* data = stock_font->GetFont()->GetFontData();
1094 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -05001095
Tom Sepeze08d2b12018-04-25 18:49:32 +00001096 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001097 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1098 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001099 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001100 EXPECT_TRUE(typed_font->IsCIDFont());
1101
1102 // Check font dictionary entries
1103 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
1104 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1105 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1106 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
1107 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
1108 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
1109 ASSERT_TRUE(descendant_array);
1110 EXPECT_EQ(1U, descendant_array->GetCount());
1111
1112 // Check the CIDFontDict
1113 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
1114 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1115 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
1116 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
1117 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
1118 ASSERT_TRUE(cidinfo_dict);
1119 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1120 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1121 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1122 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
1123 data);
1124
1125 // Check widths
1126 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
1127 ASSERT_TRUE(widths_array);
1128 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05001129}
rbpotterce8e51e2017-04-28 12:42:47 -07001130
1131TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
1132 // Load document with a -90 degree rotation
1133 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
1134 FPDF_PAGE page = LoadPage(0);
1135 EXPECT_NE(nullptr, page);
1136
1137 EXPECT_EQ(3, FPDFPage_GetRotation(page));
1138 UnloadPage(page);
1139}
Nicolas Penab3161852017-05-02 14:12:50 -04001140
1141TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
1142 // Start with a blank page
1143 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1144 {
1145 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001146 const uint8_t* data = stock_font->GetFont()->GetFontData();
1147 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001148 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001149 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
1150 ASSERT_TRUE(font.get());
1151
1152 // Add some text to the page
1153 FPDF_PAGEOBJECT text_object =
1154 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1155 EXPECT_TRUE(text_object);
1156 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1157 GetFPDFWideString(L"I am testing my loaded font, WEE.");
1158 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1159 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
1160 FPDFPage_InsertObject(page, text_object);
Tom Sepeze08d2b12018-04-25 18:49:32 +00001161 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001162#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001163 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
1164#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001165 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04001166#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001167 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04001168
1169 // Add some more text, same font
1170 FPDF_PAGEOBJECT text_object2 =
1171 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
1172 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1173 GetFPDFWideString(L"Bigger font size");
1174 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1175 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
1176 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04001177 }
Tom Sepeze08d2b12018-04-25 18:49:32 +00001178 ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001179#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001180 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
1181#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001182 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04001183#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001184 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001185
Nicolas Pena207b7272017-05-26 17:37:06 -04001186 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04001187 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1188 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001189
1190 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001191}
Nicolas Penaf45ade32017-05-03 10:23:49 -04001192
Jane Liueda65252017-06-07 11:31:27 -04001193TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
1194 // Open a file with one annotation and load its first page.
1195 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001196 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04001197 ASSERT_TRUE(page);
1198
Lei Zhanga21d5932018-02-05 18:28:38 +00001199 {
1200 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001201 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001202 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
1203 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04001204
Lei Zhanga21d5932018-02-05 18:28:38 +00001205 // FPDFPage_TransformAnnots() should run without errors when modifying
1206 // annotation rectangles.
1207 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
1208 }
Jane Liueda65252017-06-07 11:31:27 -04001209 UnloadPage(page);
1210}
1211
Nicolas Penaf45ade32017-05-03 10:23:49 -04001212// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04001213#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -04001214TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
1215 // Start with a blank page
1216 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1217 CFX_Font CIDfont;
1218 {
1219 // First, get the data from the font
1220 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
1221 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
1222 const uint8_t* data = CIDfont.GetFontData();
1223 const uint32_t size = CIDfont.GetSize();
1224
1225 // Load the data into a FPDF_Font.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001226 ScopedFPDFFont font(
Nicolas Penaf45ade32017-05-03 10:23:49 -04001227 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1228 ASSERT_TRUE(font.get());
1229
1230 // Add some text to the page
1231 FPDF_PAGEOBJECT text_object =
1232 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1233 ASSERT_TRUE(text_object);
1234 std::wstring wstr = L"ABCDEFGhijklmnop.";
1235 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1236 GetFPDFWideString(wstr);
1237 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1238 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
1239 FPDFPage_InsertObject(page, text_object);
1240
1241 // And add some Japanese characters
1242 FPDF_PAGEOBJECT text_object2 =
1243 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
1244 ASSERT_TRUE(text_object2);
1245 std::wstring wstr2 =
1246 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
1247 L"\u756A";
1248 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1249 GetFPDFWideString(wstr2);
1250 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1251 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
1252 FPDFPage_InsertObject(page, text_object2);
1253 }
1254
Nicolas Pena207b7272017-05-26 17:37:06 -04001255 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00001256 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00001257 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001258 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001259 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1260 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04001261
1262 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04001263 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04001264 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1265 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001266
1267 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001268}
Dan Sinclair698aed72017-09-26 16:24:49 -04001269#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001270
1271TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04001272 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001273 {
1274 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
1275 FPDF_PAGE page = LoadPage(0);
1276 ASSERT_NE(nullptr, page);
1277
1278 // Now add a more complex blue path.
1279 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
1280 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
1281 // TODO(npm): stroking will cause the MD5s to differ.
1282 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
1283 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
1284 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
1285 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
1286 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
1287 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
1288 EXPECT_TRUE(FPDFPath_Close(green_path));
1289 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00001290 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001291 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001292
1293 // Now save the result, closing the page and document
1294 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1295 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1296 UnloadPage(page);
1297 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001298
1299 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001300}
Jane Liu28fb7ba2017-08-02 21:45:57 -04001301
1302TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
1303 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1304 FPDF_PAGE page = LoadPage(0);
1305 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001306 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04001307
1308 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1309 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1310 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
1311
1312 obj = FPDFPage_GetObject(page, 33);
1313 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1314 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
1315 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1316 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
1317 FPDFBitmap_Destroy(bitmap);
1318
1319 obj = FPDFPage_GetObject(page, 34);
1320 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1321 bitmap = FPDFImageObj_GetBitmap(obj);
1322 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1323 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
1324 FPDFBitmap_Destroy(bitmap);
1325
1326 obj = FPDFPage_GetObject(page, 35);
1327 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1328 bitmap = FPDFImageObj_GetBitmap(obj);
1329 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
1330 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
1331 FPDFBitmap_Destroy(bitmap);
1332
1333 obj = FPDFPage_GetObject(page, 36);
1334 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1335 bitmap = FPDFImageObj_GetBitmap(obj);
1336 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1337 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
1338 FPDFBitmap_Destroy(bitmap);
1339
1340 obj = FPDFPage_GetObject(page, 37);
1341 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1342 bitmap = FPDFImageObj_GetBitmap(obj);
1343 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1344 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
1345 FPDFBitmap_Destroy(bitmap);
1346
1347 obj = FPDFPage_GetObject(page, 38);
1348 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1349 bitmap = FPDFImageObj_GetBitmap(obj);
1350 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1351 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
1352 FPDFBitmap_Destroy(bitmap);
1353 UnloadPage(page);
1354}
Jane Liu548334e2017-08-03 16:33:40 -04001355
Lei Zhang53341dd2018-03-01 15:42:47 +00001356TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) {
1357 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
1358 FPDF_PAGE page = LoadPage(0);
1359 ASSERT_TRUE(page);
1360 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1361
1362 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
1363 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1364 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001365 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00001366 ASSERT_TRUE(bitmap);
1367 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
1368 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00001369 }
1370
1371 UnloadPage(page);
1372}
1373
Jane Liu548334e2017-08-03 16:33:40 -04001374TEST_F(FPDFEditEmbeddertest, GetImageData) {
1375 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1376 FPDF_PAGE page = LoadPage(0);
1377 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001378 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04001379
1380 // Retrieve an image object with flate-encoded data stream.
1381 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
1382 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1383
1384 // Check that the raw image data has the correct length and hash value.
1385 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1386 std::vector<char> buf(len);
1387 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1388 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
1389 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1390
1391 // Check that the decoded image data has the correct length and hash value.
1392 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1393 buf.clear();
1394 buf.resize(len);
1395 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1396 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
1397 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1398
1399 // Retrieve an image obejct with DCTDecode-encoded data stream.
1400 obj = FPDFPage_GetObject(page, 37);
1401 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1402
1403 // Check that the raw image data has the correct length and hash value.
1404 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1405 buf.clear();
1406 buf.resize(len);
1407 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1408 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1409 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1410
1411 // Check that the decoded image data has the correct length and hash value,
1412 // which should be the same as those of the raw data, since this image is
1413 // encoded by a single DCTDecode filter and decoding is a noop.
1414 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1415 buf.clear();
1416 buf.resize(len);
1417 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1418 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1419 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1420
1421 UnloadPage(page);
1422}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04001423
1424TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
1425 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1426 ASSERT_TRUE(rect);
1427
1428 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
1429 FPDFPageObj_Destroy(rect);
1430}
Jane Liube63ab92017-08-09 14:09:34 -04001431
1432TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
1433 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1434 FPDF_PAGE page = LoadPage(0);
1435 ASSERT_TRUE(page);
1436
1437 // Verify that retrieving the filter of a non-image object would fail.
1438 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1439 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1440 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
1441 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
1442
1443 // Verify the returned filter string for an image object with a single filter.
1444 obj = FPDFPage_GetObject(page, 33);
1445 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1446 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
1447 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1448 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001449 static constexpr char kFlateDecode[] = "FlateDecode";
1450 EXPECT_EQ(sizeof(kFlateDecode),
1451 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1452 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001453 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
1454
1455 // Verify all the filters for an image object with a list of filters.
1456 obj = FPDFPage_GetObject(page, 38);
1457 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1458 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
1459 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1460 buf.clear();
1461 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001462 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
1463 EXPECT_EQ(sizeof(kASCIIHexDecode),
1464 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1465 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001466
1467 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
1468 buf.clear();
1469 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001470 static constexpr char kDCTDecode[] = "DCTDecode";
1471 EXPECT_EQ(sizeof(kDCTDecode),
1472 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
1473 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001474
1475 UnloadPage(page);
1476}
Jane Liuca898292017-08-16 11:25:35 -04001477
1478TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
1479 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1480 FPDF_PAGE page = LoadPage(0);
1481 ASSERT_TRUE(page);
1482
1483 // Check that getting the metadata of a null object would fail.
1484 FPDF_IMAGEOBJ_METADATA metadata;
1485 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
1486
1487 // Check that receiving the metadata with a null metadata object would fail.
1488 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
1489 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
1490
1491 // Check that when retrieving an image object's metadata without passing in
1492 // |page|, all values are correct, with the last two being default values.
1493 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1494 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001495 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001496 EXPECT_EQ(92u, metadata.width);
1497 EXPECT_EQ(68u, metadata.height);
1498 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1499 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1500 EXPECT_EQ(0u, metadata.bits_per_pixel);
1501 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
1502
1503 // Verify the metadata of a bitmap image with indexed colorspace.
1504 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001505 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001506 EXPECT_EQ(92u, metadata.width);
1507 EXPECT_EQ(68u, metadata.height);
1508 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1509 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1510 EXPECT_EQ(1u, metadata.bits_per_pixel);
1511 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
1512
1513 // Verify the metadata of an image with RGB colorspace.
1514 obj = FPDFPage_GetObject(page, 37);
1515 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1516 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001517 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001518 EXPECT_EQ(126u, metadata.width);
1519 EXPECT_EQ(106u, metadata.height);
1520 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
1521 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
1522 EXPECT_EQ(24u, metadata.bits_per_pixel);
1523 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
1524
1525 UnloadPage(page);
1526}