blob: 80191556f3f71449d6542478ee6d219f3d2b3c2b [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"
Nicolas Penab3161852017-05-02 14:12:50 -040018#include "public/cpp/fpdf_deleters.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/
Lei Zhang107fa7b2018-02-09 21:48:15 +0000195 std::unique_ptr<void, FPDFBitmapDeleter> 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 {
246 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
247 RenderPageWithFlags(page, nullptr, 0);
248 CompareBitmap(page_bitmap.get(), 612, 792,
249 "66d02eaa6181e2c069ce2ea99beda497");
250 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500251
252 // Now add to that a green rectangle with some medium alpha
253 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
254 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200255
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200256 // Make sure the type of the rectangle is a path.
257 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
258
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200259 // Make sure we get back the same color we set previously.
260 unsigned int R;
261 unsigned int G;
262 unsigned int B;
263 unsigned int A;
264 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
265 EXPECT_EQ(0U, R);
266 EXPECT_EQ(255U, G);
267 EXPECT_EQ(0U, B);
268 EXPECT_EQ(128U, A);
269
Miklos Vajna12abfd02017-09-15 07:49:03 +0200270 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
271 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200272 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200273 // Verify actual coordinates.
274 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
275 float x;
276 float y;
277 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
278 EXPECT_EQ(100, x);
279 EXPECT_EQ(100, y);
280 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
281 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
282 segment = FPDFPath_GetPathSegment(green_rect, 1);
283 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
284 EXPECT_EQ(100, x);
285 EXPECT_EQ(140, y);
286 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
287 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
288 segment = FPDFPath_GetPathSegment(green_rect, 2);
289 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
290 EXPECT_EQ(140, x);
291 EXPECT_EQ(140, y);
292 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
293 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
294 segment = FPDFPath_GetPathSegment(green_rect, 3);
295 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
296 EXPECT_EQ(140, x);
297 EXPECT_EQ(100, y);
298 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
299 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
300 segment = FPDFPath_GetPathSegment(green_rect, 4);
301 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
302 EXPECT_EQ(100, x);
303 EXPECT_EQ(100, y);
304 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
305 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200306
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500307 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
308 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000309 {
310 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
311 RenderPageWithFlags(page, nullptr, 0);
312 CompareBitmap(page_bitmap.get(), 612, 792,
313 "7b0b87604594e773add528fae567a558");
314 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500315
316 // Add a black triangle.
317 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
318 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
319 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
320 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
321 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
322 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200323
324 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
325 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200326 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200327 // Verify actual coordinates.
328 segment = FPDFPath_GetPathSegment(black_path, 0);
329 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
330 EXPECT_EQ(400, x);
331 EXPECT_EQ(100, y);
332 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
333 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
334 segment = FPDFPath_GetPathSegment(black_path, 1);
335 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
336 EXPECT_EQ(400, x);
337 EXPECT_EQ(200, y);
338 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
339 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
340 segment = FPDFPath_GetPathSegment(black_path, 2);
341 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
342 EXPECT_EQ(300, x);
343 EXPECT_EQ(100, y);
344 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
345 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
346 // Make sure out of bounds index access fails properly.
347 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200348
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500349 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000350 {
351 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
352 RenderPageWithFlags(page, nullptr, 0);
353 CompareBitmap(page_bitmap.get(), 612, 792,
354 "eadc8020a14dfcf091da2688733d8806");
355 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500356
357 // Now add a more complex blue path.
358 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
359 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
360 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
361 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
362 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
363 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
364 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
365 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
366 EXPECT_TRUE(FPDFPath_Close(blue_path));
367 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000368 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
369 {
370 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
371 RenderPageWithFlags(page, nullptr, 0);
372 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
373 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500374
375 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400376 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500377 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500378 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500379
380 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000381 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500382}
383
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000384TEST_F(FPDFEditEmbeddertest, RemovePageObject) {
385 // Load document with some text.
386 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
387 FPDF_PAGE page = LoadPage(0);
388 ASSERT_TRUE(page);
389
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000390 // Show what the original file looks like.
391 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000392#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000393 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000394#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000395 const char kOriginalMD5[] = "e5a6fa28298db07484cd922f3e210c88";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000396#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000397 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000398#endif
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000399 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
400 RenderPageWithFlags(page, nullptr, 0);
401 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
402 }
403
404 // Get the "Hello, world!" text object and remove it.
405 ASSERT_EQ(2, FPDFPage_CountObjects(page));
406 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
407 ASSERT_TRUE(page_object);
408 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
409
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000410 // Verify the "Hello, world!" text is gone.
411 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000412#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000413 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000414#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000415 const char kRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000416#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000417 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000418#endif
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000419 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
420 RenderPageWithFlags(page, nullptr, 0);
421 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
422 }
423 ASSERT_EQ(1, FPDFPage_CountObjects(page));
424
425 UnloadPage(page);
426 FPDFPageObj_Destroy(page_object);
427}
428
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000429TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) {
430 // Load document with some text.
431 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
432 FPDF_PAGE page = LoadPage(0);
433 ASSERT_TRUE(page);
434
435 // Show what the original file looks like.
436 {
437#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
438 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
439#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
440 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
441#else
442 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
443#endif
444 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
445 RenderPageWithFlags(page, nullptr, 0);
446 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
447 }
448
449 // Iterate over all objects, counting the number of times each content mark
450 // name appears.
451 int object_count = FPDFPage_CountObjects(page);
452 ASSERT_EQ(19, object_count);
453
454 unsigned long prime_count = 0;
455 unsigned long square_count = 0;
456 unsigned long greater_than_ten_count = 0;
457 std::vector<FPDF_PAGEOBJECT> primes;
458 for (int i = 0; i < object_count; ++i) {
459 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
460
461 int mark_count = FPDFPageObj_CountMarks(page_object);
462 for (int j = 0; j < mark_count; ++j) {
463 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
464
465 char buffer[256];
466 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
467 std::wstring name =
468 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
469 if (name == L"Prime") {
470 prime_count++;
471 primes.push_back(page_object);
472 } else if (name == L"Square") {
473 square_count++;
474 } else if (name == L"GreaterThanTen") {
475 greater_than_ten_count++;
476 } else {
477 FAIL();
478 }
479 }
480 }
481
482 // Expect certain number of tagged objects. The test file contains strings
483 // from 1 to 19.
484 EXPECT_EQ(8u, prime_count);
485 EXPECT_EQ(4u, square_count);
486 EXPECT_EQ(9u, greater_than_ten_count);
487
488 // Remove all objects marked with "Prime".
489 for (FPDF_PAGEOBJECT page_object : primes) {
490 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
491 FPDFPageObj_Destroy(page_object);
492 }
493
494 EXPECT_EQ(11, FPDFPage_CountObjects(page));
495
496 {
497#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
498 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
499#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
500 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
501#else
502 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
503#endif
504 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
505 RenderPageWithFlags(page, nullptr, 0);
506 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
507 }
508
509 UnloadPage(page);
510}
511
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000512// Fails due to pdfium:1051.
513TEST_F(FPDFEditEmbeddertest, DISABLED_RemoveExistingPageObject) {
514 // Load document with some text.
515 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
516 FPDF_PAGE page = LoadPage(0);
517 ASSERT_TRUE(page);
518
519 // Get the "Hello, world!" text object and remove it.
520 ASSERT_EQ(2, FPDFPage_CountObjects(page));
521 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
522 ASSERT_TRUE(page_object);
523 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
524
525 // Verify the "Hello, world!" text is gone.
526 ASSERT_EQ(1, FPDFPage_CountObjects(page));
527
528 // Save the file
529 EXPECT_TRUE(FPDFPage_GenerateContent(page));
530 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
531 UnloadPage(page);
532 FPDFPageObj_Destroy(page_object);
533
534 // Re-open the file and check the page object count is still 1.
535 OpenSavedDocument();
536 FPDF_PAGE saved_page = LoadSavedPage(0);
537 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
538 CloseSavedPage(saved_page);
539 CloseSavedDocument();
540}
541
542TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) {
543 // Load document with some text.
544 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
545 FPDF_PAGE page = LoadPage(0);
546 ASSERT_TRUE(page);
547
548 // Add a red rectangle.
549 ASSERT_EQ(2, FPDFPage_CountObjects(page));
550 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
551 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
552 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
553 FPDFPage_InsertObject(page, red_rect);
554
555 // Verify the red rectangle was added.
556 ASSERT_EQ(3, FPDFPage_CountObjects(page));
557
558 // Save the file
559 EXPECT_TRUE(FPDFPage_GenerateContent(page));
560 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
561 UnloadPage(page);
562
563 // Re-open the file and check the page object count is still 3.
564 OpenSavedDocument();
565 FPDF_PAGE saved_page = LoadSavedPage(0);
566 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
567 CloseSavedPage(saved_page);
568 CloseSavedDocument();
569}
570
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000571TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) {
572 // Start with a blank page.
573 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
574 ASSERT_TRUE(page);
575
576 // Render the blank page and verify it's a blank bitmap.
577 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
578 {
579 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
580 RenderPageWithFlags(page, nullptr, 0);
581 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
582 }
583 ASSERT_EQ(0, FPDFPage_CountObjects(page));
584
585 // Add a red rectangle.
586 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
587 ASSERT_TRUE(red_rect);
588 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
589 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
590 FPDFPage_InsertObject(page, red_rect);
591 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
592 {
593 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
594 RenderPageWithFlags(page, nullptr, 0);
595 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
596 }
597 EXPECT_EQ(1, FPDFPage_CountObjects(page));
598
599 // Remove rectangle and verify it does not render anymore and the bitmap is
600 // back to a blank one.
601 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
602 {
603 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
604 RenderPageWithFlags(page, nullptr, 0);
605 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
606 }
607 EXPECT_EQ(0, FPDFPage_CountObjects(page));
608
609 // Trying to remove an object not in the page should return false.
610 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
611
612 FPDF_ClosePage(page);
613 FPDFPageObj_Destroy(red_rect);
614}
615
Miklos Vajna12abfd02017-09-15 07:49:03 +0200616TEST_F(FPDFEditEmbeddertest, PathsPoints) {
617 CreateNewDocument();
618 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
619 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +0200620 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200621
622 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +0200623 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200624
Miklos Vajna36eed872017-09-20 22:52:43 +0200625 // FPDFPath_GetPathSegment() with a non-path.
626 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
627 // FPDFPath_GetPathSegment() with a NULL path.
628 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
629 float x;
630 float y;
631 // FPDFPathSegment_GetPoint() with a NULL segment.
632 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
633
634 // FPDFPathSegment_GetType() with a NULL segment.
635 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
636
637 // FPDFPathSegment_GetClose() with a NULL segment.
638 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
639
Miklos Vajna12abfd02017-09-15 07:49:03 +0200640 FPDFPageObj_Destroy(img);
641}
642
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500643TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
644 // Load document with some text
645 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
646 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000647 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500648
649 // Add an opaque rectangle on top of some of the text.
650 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
651 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
652 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
653 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500654
655 // Add a transparent triangle on top of other part of the text.
656 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
657 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
658 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
659 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
660 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
661 EXPECT_TRUE(FPDFPath_Close(black_path));
662 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500663
664 // Render and check the result. Text is slightly different on Mac.
Lei Zhang107fa7b2018-02-09 21:48:15 +0000665 std::unique_ptr<void, FPDFBitmapDeleter> bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400666#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700667 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500668#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000669 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400670#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000671 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500672 UnloadPage(page);
673}
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500674
wileyryae858aa42017-05-31 14:49:05 -0500675TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
676 // Load document with existing content
677 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
678 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000679 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -0500680
681 // Add a transparent rectangle on top of the existing content
682 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
683 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
684 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
685 FPDFPage_InsertObject(page, red_rect2);
686
687 // Add an opaque rectangle on top of the existing content
688 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
689 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
690 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
691 FPDFPage_InsertObject(page, red_rect);
692
Lei Zhang107fa7b2018-02-09 21:48:15 +0000693 std::unique_ptr<void, FPDFBitmapDeleter> bitmap = RenderLoadedPage(page);
694 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500695 EXPECT_TRUE(FPDFPage_GenerateContent(page));
696
697 // Now save the result, closing the page and document
698 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400699 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -0500700
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400701 OpenSavedDocument();
Lei Zhang107fa7b2018-02-09 21:48:15 +0000702 FPDF_PAGE saved_page = LoadSavedPage(0);
703 VerifySavedRendering(saved_page, 612, 792,
704 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500705
706 ClearString();
707 // Add another opaque rectangle on top of the existing content
708 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
709 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
710 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000711 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -0500712
713 // Add another transparent rectangle on top of existing content
714 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
715 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
716 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000717 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000718 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
719 {
720 std::unique_ptr<void, FPDFBitmapDeleter> new_bitmap =
721 RenderSavedPage(saved_page);
722 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
723 }
Lei Zhang107fa7b2018-02-09 21:48:15 +0000724 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -0500725
726 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +0000727 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400728
Lei Zhang107fa7b2018-02-09 21:48:15 +0000729 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400730 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -0500731
732 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +0000733 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -0500734}
735
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500736TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
737 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500738 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500739
740 // Add a large stroked rectangle (fill color should not affect it).
741 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
742 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
743 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
744 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
745 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
746 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000747 {
748 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
749 RenderPageWithFlags(page, nullptr, 0);
750 CompareBitmap(page_bitmap.get(), 612, 792,
751 "64bd31f862a89e0a9e505a5af6efd506");
752 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500753
754 // Add crossed-checkmark
755 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
756 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
757 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
758 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
759 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
760 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
761 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
762 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
763 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000764 {
765 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
766 RenderPageWithFlags(page, nullptr, 0);
767 CompareBitmap(page_bitmap.get(), 612, 792,
768 "4b6f3b9d25c4e194821217d5016c3724");
769 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500770
771 // Add stroked and filled oval-ish path.
772 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
773 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
774 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
775 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
776 EXPECT_TRUE(FPDFPath_Close(path));
777 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
778 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
779 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
780 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
781 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000782 {
783 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
784 RenderPageWithFlags(page, nullptr, 0);
785 CompareBitmap(page_bitmap.get(), 612, 792,
786 "ff3e6a22326754944cc6e56609acd73b");
787 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500788 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500789}
Nicolas Pena49058402017-02-14 18:26:20 -0500790
791TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
792 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500793 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -0500794
795 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400796 FPDF_PAGEOBJECT text_object1 =
797 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
798 EXPECT_TRUE(text_object1);
799 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
800 GetFPDFWideString(L"I'm at the bottom of the page");
801 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
802 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
803 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000804 {
805 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
806 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400807#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000808 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -0500809#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000810 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400811#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000812 CompareBitmap(page_bitmap.get(), 612, 792, md5);
813 }
Nicolas Pena49058402017-02-14 18:26:20 -0500814
815 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -0400816 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500817 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400818 EXPECT_TRUE(text_object2);
819 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
820 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
821 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
822 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
823 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000824 {
825 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
826 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400827#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000828 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -0400829#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000830 const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d";
Nicolas Pena49058402017-02-14 18:26:20 -0500831#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000832 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400833#endif
Dan Sinclair971a6742018-03-28 19:23:25 +0000834 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000835 }
Nicolas Pena49058402017-02-14 18:26:20 -0500836
837 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -0400838 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500839 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400840 EXPECT_TRUE(text_object3);
841 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
842 GetFPDFWideString(L"Can you read me? <:)>");
843 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
844 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
845 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000846 {
847 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
848 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400849#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000850 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -0400851#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000852 const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2";
Nicolas Pena49058402017-02-14 18:26:20 -0500853#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000854 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400855#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000856 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
857 }
Nicolas Pena49058402017-02-14 18:26:20 -0500858
859 // TODO(npm): Why are there issues with text rotated by 90 degrees?
860 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
861 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -0500862}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500863
Nicolas Pena603a31d2017-06-14 11:41:18 -0400864TEST_F(FPDFEditEmbeddertest, GraphicsData) {
865 // New page
866 std::unique_ptr<void, FPDFPageDeleter> page(
867 FPDFPage_New(CreateNewDocument(), 0, 612, 792));
868
869 // Create a rect with nontrivial graphics
870 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
871 FPDFPageObj_SetBlendMode(rect1, "Color");
872 FPDFPage_InsertObject(page.get(), rect1);
873 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
874
875 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +0000876 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
877 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -0400878 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400879 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400880
881 // Add a text object causing no change to the graphics dictionary
882 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
883 // Only alpha, the last component, matters for the graphics dictionary. And
884 // the default value is 255.
885 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
886 FPDFPage_InsertObject(page.get(), text1);
887 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400888 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400889
890 // Add a text object increasing the size of the graphics dictionary
891 FPDF_PAGEOBJECT text2 =
892 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
893 FPDFPage_InsertObject(page.get(), text2);
894 FPDFPageObj_SetBlendMode(text2, "Darken");
895 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
896 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400897 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400898
899 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -0400900 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -0400901 FPDFPageObj_SetBlendMode(path, "Darken");
902 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
903 FPDFPage_InsertObject(page.get(), path);
904 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400905 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400906
907 // Add a rect increasing the size of the graphics dictionary
908 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
909 FPDFPageObj_SetBlendMode(rect2, "Darken");
910 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
911 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
912 FPDFPage_InsertObject(page.get(), rect2);
913 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400914 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400915}
916
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500917TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
918 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500919 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500920
921 // Add a red rectangle with some non-default alpha
922 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
923 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
924 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
925 FPDFPage_InsertObject(page, rect);
926 EXPECT_TRUE(FPDFPage_GenerateContent(page));
927
928 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +0000929 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
930 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500931 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400932 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500933
934 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +0000935 {
936 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
937 RenderPageWithFlags(page, nullptr, 0);
938 CompareBitmap(page_bitmap.get(), 612, 792,
939 "5384da3406d62360ffb5cac4476fff1c");
940 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500941
942 // Never mind, my new favorite color is blue, increase alpha
943 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
944 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400945 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500946
947 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +0000948 {
949 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
950 RenderPageWithFlags(page, nullptr, 0);
951 CompareBitmap(page_bitmap.get(), 612, 792,
952 "2e51656f5073b0bee611d9cd086aa09c");
953 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500954
955 // And now generate, without changes
956 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400957 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000958 {
959 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
960 RenderPageWithFlags(page, nullptr, 0);
961 CompareBitmap(page_bitmap.get(), 612, 792,
962 "2e51656f5073b0bee611d9cd086aa09c");
963 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500964
965 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400966 FPDF_PAGEOBJECT text_object =
967 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
968 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
969 GetFPDFWideString(L"Something something #text# something");
970 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
971 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
972 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500973 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000974 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500975 ASSERT_TRUE(font_dict);
976 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
977
978 // Generate yet again, check dicts are reasonably sized
979 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400980 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500981 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
982 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500983}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500984
Nicolas Penad03ca422017-03-06 13:54:33 -0500985TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
986 CreateNewDocument();
987 // TODO(npm): use other fonts after disallowing loading any font as any type
988 const CPDF_Font* stock_font =
989 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700990 const uint8_t* data = stock_font->GetFont()->GetFontData();
991 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400992 std::unique_ptr<void, FPDFFontDeleter> font(
993 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
994 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400995 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500996 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500997
Nicolas Penad03ca422017-03-06 13:54:33 -0500998 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -0500999 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1000 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
1001 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
1002 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1003 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1004 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05001005 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
1006
Nicolas Penabe90aae2017-02-27 10:41:41 -05001007 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001008 ASSERT_TRUE(widths_array);
1009 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001010 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05001011 EXPECT_EQ(569, widths_array->GetNumberAt(11));
1012 EXPECT_EQ(500, widths_array->GetNumberAt(223));
1013 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
1014}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001015
Nicolas Penad03ca422017-03-06 13:54:33 -05001016TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
1017 CreateNewDocument();
1018 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001019 const uint8_t* data = stock_font->GetFont()->GetFontData();
1020 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -04001021 std::unique_ptr<void, FPDFFontDeleter> font(
1022 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
1023 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -04001024 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001025 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001026
Nicolas Penad03ca422017-03-06 13:54:33 -05001027 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
1028 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1029 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
1030 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
1031 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1032 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1033 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
1034 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05001035
Nicolas Penad03ca422017-03-06 13:54:33 -05001036 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
1037 ASSERT_TRUE(widths_array);
1038 ASSERT_EQ(224U, widths_array->GetCount());
1039 EXPECT_EQ(600, widths_array->GetNumberAt(33));
1040 EXPECT_EQ(600, widths_array->GetNumberAt(74));
1041 EXPECT_EQ(600, widths_array->GetNumberAt(223));
1042 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
1043}
1044
1045TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
1046 CreateNewDocument();
1047 const CPDF_Font* stock_font =
1048 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001049 const uint8_t* data = stock_font->GetFont()->GetFontData();
1050 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -04001051 std::unique_ptr<void, FPDFFontDeleter> font(
1052 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
1053 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -04001054 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001055 EXPECT_TRUE(typed_font->IsCIDFont());
1056
1057 // Check font dictionary entries
1058 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
1059 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1060 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1061 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
1062 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
1063 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
1064 ASSERT_TRUE(descendant_array);
1065 EXPECT_EQ(1U, descendant_array->GetCount());
1066
1067 // Check the CIDFontDict
1068 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
1069 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1070 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
1071 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
1072 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
1073 ASSERT_TRUE(cidinfo_dict);
1074 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1075 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1076 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1077 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
1078
1079 // Check widths
1080 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
1081 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001082 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -05001083 CheckCompositeFontWidths(widths_array, typed_font);
1084}
1085
1086TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
1087 CreateNewDocument();
1088 const CPDF_Font* stock_font =
1089 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001090 const uint8_t* data = stock_font->GetFont()->GetFontData();
1091 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -05001092
Nicolas Penab3161852017-05-02 14:12:50 -04001093 std::unique_ptr<void, FPDFFontDeleter> font(
1094 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1095 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -04001096 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001097 EXPECT_TRUE(typed_font->IsCIDFont());
1098
1099 // Check font dictionary entries
1100 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
1101 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1102 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1103 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
1104 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
1105 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
1106 ASSERT_TRUE(descendant_array);
1107 EXPECT_EQ(1U, descendant_array->GetCount());
1108
1109 // Check the CIDFontDict
1110 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
1111 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1112 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
1113 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
1114 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
1115 ASSERT_TRUE(cidinfo_dict);
1116 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1117 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1118 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1119 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
1120 data);
1121
1122 // Check widths
1123 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
1124 ASSERT_TRUE(widths_array);
1125 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05001126}
rbpotterce8e51e2017-04-28 12:42:47 -07001127
1128TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
1129 // Load document with a -90 degree rotation
1130 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
1131 FPDF_PAGE page = LoadPage(0);
1132 EXPECT_NE(nullptr, page);
1133
1134 EXPECT_EQ(3, FPDFPage_GetRotation(page));
1135 UnloadPage(page);
1136}
Nicolas Penab3161852017-05-02 14:12:50 -04001137
1138TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
1139 // Start with a blank page
1140 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1141 {
1142 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001143 const uint8_t* data = stock_font->GetFont()->GetFontData();
1144 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -04001145 std::unique_ptr<void, FPDFFontDeleter> font(
1146 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
1147 ASSERT_TRUE(font.get());
1148
1149 // Add some text to the page
1150 FPDF_PAGEOBJECT text_object =
1151 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1152 EXPECT_TRUE(text_object);
1153 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1154 GetFPDFWideString(L"I am testing my loaded font, WEE.");
1155 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1156 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
1157 FPDFPage_InsertObject(page, text_object);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001158 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
1159 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001160#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001161 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
1162#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001163 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04001164#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001165 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04001166
1167 // Add some more text, same font
1168 FPDF_PAGEOBJECT text_object2 =
1169 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
1170 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1171 GetFPDFWideString(L"Bigger font size");
1172 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1173 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
1174 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04001175 }
Lei Zhang107fa7b2018-02-09 21:48:15 +00001176 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap2 =
1177 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001178#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001179 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
1180#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001181 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04001182#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001183 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001184
Nicolas Pena207b7272017-05-26 17:37:06 -04001185 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04001186 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1187 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001188
1189 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001190}
Nicolas Penaf45ade32017-05-03 10:23:49 -04001191
Jane Liueda65252017-06-07 11:31:27 -04001192TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
1193 // Open a file with one annotation and load its first page.
1194 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001195 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04001196 ASSERT_TRUE(page);
1197
Lei Zhanga21d5932018-02-05 18:28:38 +00001198 {
1199 // Add an underline annotation to the page without specifying its rectangle.
1200 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1201 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
1202 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04001203
Lei Zhanga21d5932018-02-05 18:28:38 +00001204 // FPDFPage_TransformAnnots() should run without errors when modifying
1205 // annotation rectangles.
1206 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
1207 }
Jane Liueda65252017-06-07 11:31:27 -04001208 UnloadPage(page);
1209}
1210
Nicolas Penaf45ade32017-05-03 10:23:49 -04001211// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04001212#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -04001213TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
1214 // Start with a blank page
1215 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1216 CFX_Font CIDfont;
1217 {
1218 // First, get the data from the font
1219 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
1220 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
1221 const uint8_t* data = CIDfont.GetFontData();
1222 const uint32_t size = CIDfont.GetSize();
1223
1224 // Load the data into a FPDF_Font.
1225 std::unique_ptr<void, FPDFFontDeleter> font(
1226 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1227 ASSERT_TRUE(font.get());
1228
1229 // Add some text to the page
1230 FPDF_PAGEOBJECT text_object =
1231 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1232 ASSERT_TRUE(text_object);
1233 std::wstring wstr = L"ABCDEFGhijklmnop.";
1234 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1235 GetFPDFWideString(wstr);
1236 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1237 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
1238 FPDFPage_InsertObject(page, text_object);
1239
1240 // And add some Japanese characters
1241 FPDF_PAGEOBJECT text_object2 =
1242 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
1243 ASSERT_TRUE(text_object2);
1244 std::wstring wstr2 =
1245 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
1246 L"\u756A";
1247 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1248 GetFPDFWideString(wstr2);
1249 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1250 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
1251 FPDFPage_InsertObject(page, text_object2);
1252 }
1253
Nicolas Pena207b7272017-05-26 17:37:06 -04001254 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00001255 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00001256 {
1257 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
1258 RenderPageWithFlags(page, nullptr, 0);
1259 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);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001290 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
1291 RenderLoadedPage(page);
1292 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001293
1294 // Now save the result, closing the page and document
1295 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1296 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1297 UnloadPage(page);
1298 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001299
1300 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001301}
Jane Liu28fb7ba2017-08-02 21:45:57 -04001302
1303TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
1304 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1305 FPDF_PAGE page = LoadPage(0);
1306 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001307 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04001308
1309 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1310 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1311 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
1312
1313 obj = FPDFPage_GetObject(page, 33);
1314 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1315 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
1316 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1317 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
1318 FPDFBitmap_Destroy(bitmap);
1319
1320 obj = FPDFPage_GetObject(page, 34);
1321 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1322 bitmap = FPDFImageObj_GetBitmap(obj);
1323 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1324 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
1325 FPDFBitmap_Destroy(bitmap);
1326
1327 obj = FPDFPage_GetObject(page, 35);
1328 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1329 bitmap = FPDFImageObj_GetBitmap(obj);
1330 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
1331 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
1332 FPDFBitmap_Destroy(bitmap);
1333
1334 obj = FPDFPage_GetObject(page, 36);
1335 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1336 bitmap = FPDFImageObj_GetBitmap(obj);
1337 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1338 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
1339 FPDFBitmap_Destroy(bitmap);
1340
1341 obj = FPDFPage_GetObject(page, 37);
1342 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1343 bitmap = FPDFImageObj_GetBitmap(obj);
1344 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1345 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
1346 FPDFBitmap_Destroy(bitmap);
1347
1348 obj = FPDFPage_GetObject(page, 38);
1349 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1350 bitmap = FPDFImageObj_GetBitmap(obj);
1351 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1352 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
1353 FPDFBitmap_Destroy(bitmap);
1354 UnloadPage(page);
1355}
Jane Liu548334e2017-08-03 16:33:40 -04001356
Lei Zhang53341dd2018-03-01 15:42:47 +00001357TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) {
1358 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
1359 FPDF_PAGE page = LoadPage(0);
1360 ASSERT_TRUE(page);
1361 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1362
1363 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
1364 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1365 {
Lei Zhang53341dd2018-03-01 15:42:47 +00001366 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
1367 FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00001368 ASSERT_TRUE(bitmap);
1369 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
1370 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00001371 }
1372
1373 UnloadPage(page);
1374}
1375
Jane Liu548334e2017-08-03 16:33:40 -04001376TEST_F(FPDFEditEmbeddertest, GetImageData) {
1377 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1378 FPDF_PAGE page = LoadPage(0);
1379 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001380 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04001381
1382 // Retrieve an image object with flate-encoded data stream.
1383 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
1384 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1385
1386 // Check that the raw image data has the correct length and hash value.
1387 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1388 std::vector<char> buf(len);
1389 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1390 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
1391 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1392
1393 // Check that the decoded image data has the correct length and hash value.
1394 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1395 buf.clear();
1396 buf.resize(len);
1397 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1398 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
1399 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1400
1401 // Retrieve an image obejct with DCTDecode-encoded data stream.
1402 obj = FPDFPage_GetObject(page, 37);
1403 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1404
1405 // Check that the raw image data has the correct length and hash value.
1406 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1407 buf.clear();
1408 buf.resize(len);
1409 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1410 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1411 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1412
1413 // Check that the decoded image data has the correct length and hash value,
1414 // which should be the same as those of the raw data, since this image is
1415 // encoded by a single DCTDecode filter and decoding is a noop.
1416 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1417 buf.clear();
1418 buf.resize(len);
1419 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1420 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1421 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1422
1423 UnloadPage(page);
1424}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04001425
1426TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
1427 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1428 ASSERT_TRUE(rect);
1429
1430 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
1431 FPDFPageObj_Destroy(rect);
1432}
Jane Liube63ab92017-08-09 14:09:34 -04001433
1434TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
1435 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1436 FPDF_PAGE page = LoadPage(0);
1437 ASSERT_TRUE(page);
1438
1439 // Verify that retrieving the filter of a non-image object would fail.
1440 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1441 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1442 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
1443 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
1444
1445 // Verify the returned filter string for an image object with a single filter.
1446 obj = FPDFPage_GetObject(page, 33);
1447 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1448 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
1449 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1450 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001451 static constexpr char kFlateDecode[] = "FlateDecode";
1452 EXPECT_EQ(sizeof(kFlateDecode),
1453 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1454 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001455 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
1456
1457 // Verify all the filters for an image object with a list of filters.
1458 obj = FPDFPage_GetObject(page, 38);
1459 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1460 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
1461 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1462 buf.clear();
1463 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001464 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
1465 EXPECT_EQ(sizeof(kASCIIHexDecode),
1466 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1467 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001468
1469 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
1470 buf.clear();
1471 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001472 static constexpr char kDCTDecode[] = "DCTDecode";
1473 EXPECT_EQ(sizeof(kDCTDecode),
1474 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
1475 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001476
1477 UnloadPage(page);
1478}
Jane Liuca898292017-08-16 11:25:35 -04001479
1480TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
1481 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1482 FPDF_PAGE page = LoadPage(0);
1483 ASSERT_TRUE(page);
1484
1485 // Check that getting the metadata of a null object would fail.
1486 FPDF_IMAGEOBJ_METADATA metadata;
1487 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
1488
1489 // Check that receiving the metadata with a null metadata object would fail.
1490 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
1491 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
1492
1493 // Check that when retrieving an image object's metadata without passing in
1494 // |page|, all values are correct, with the last two being default values.
1495 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1496 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001497 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001498 EXPECT_EQ(92u, metadata.width);
1499 EXPECT_EQ(68u, metadata.height);
1500 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1501 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1502 EXPECT_EQ(0u, metadata.bits_per_pixel);
1503 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
1504
1505 // Verify the metadata of a bitmap image with indexed colorspace.
1506 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001507 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001508 EXPECT_EQ(92u, metadata.width);
1509 EXPECT_EQ(68u, metadata.height);
1510 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1511 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1512 EXPECT_EQ(1u, metadata.bits_per_pixel);
1513 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
1514
1515 // Verify the metadata of an image with RGB colorspace.
1516 obj = FPDFPage_GetObject(page, 37);
1517 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1518 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001519 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001520 EXPECT_EQ(126u, metadata.width);
1521 EXPECT_EQ(106u, metadata.height);
1522 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
1523 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
1524 EXPECT_EQ(24u, metadata.bits_per_pixel);
1525 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
1526
1527 UnloadPage(page);
1528}