blob: ec403c74b3a4e6518f88b45f4594040d1852e5cd [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"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050017#include "fpdfsdk/fsdk_define.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++);
Nicolas Penad03ca422017-03-06 13:54:33 -0500109 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
110 << cur_cid;
111 }
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++) {
121 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
122 << cur_cid;
123 }
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
390// Show how the original file looks like.
391#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
392 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
393#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
394 const char kOriginalMD5[] = "e5a6fa28298db07484cd922f3e210c88";
395#else
396 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
397#endif
398 {
399 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
410// Verify the "Hello, world!" text is gone.
411#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
412 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
413#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
414 const char kRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
415#else
416 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
417#endif
418 {
419 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
429TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) {
430 // Start with a blank page.
431 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
432 ASSERT_TRUE(page);
433
434 // Render the blank page and verify it's a blank bitmap.
435 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
436 {
437 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
438 RenderPageWithFlags(page, nullptr, 0);
439 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
440 }
441 ASSERT_EQ(0, FPDFPage_CountObjects(page));
442
443 // Add a red rectangle.
444 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
445 ASSERT_TRUE(red_rect);
446 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
447 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
448 FPDFPage_InsertObject(page, red_rect);
449 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
450 {
451 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
452 RenderPageWithFlags(page, nullptr, 0);
453 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
454 }
455 EXPECT_EQ(1, FPDFPage_CountObjects(page));
456
457 // Remove rectangle and verify it does not render anymore and the bitmap is
458 // back to a blank one.
459 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
460 {
461 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
462 RenderPageWithFlags(page, nullptr, 0);
463 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
464 }
465 EXPECT_EQ(0, FPDFPage_CountObjects(page));
466
467 // Trying to remove an object not in the page should return false.
468 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
469
470 FPDF_ClosePage(page);
471 FPDFPageObj_Destroy(red_rect);
472}
473
Miklos Vajna12abfd02017-09-15 07:49:03 +0200474TEST_F(FPDFEditEmbeddertest, PathsPoints) {
475 CreateNewDocument();
476 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
477 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +0200478 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200479
480 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +0200481 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200482
Miklos Vajna36eed872017-09-20 22:52:43 +0200483 // FPDFPath_GetPathSegment() with a non-path.
484 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
485 // FPDFPath_GetPathSegment() with a NULL path.
486 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
487 float x;
488 float y;
489 // FPDFPathSegment_GetPoint() with a NULL segment.
490 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
491
492 // FPDFPathSegment_GetType() with a NULL segment.
493 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
494
495 // FPDFPathSegment_GetClose() with a NULL segment.
496 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
497
Miklos Vajna12abfd02017-09-15 07:49:03 +0200498 FPDFPageObj_Destroy(img);
499}
500
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500501TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
502 // Load document with some text
503 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
504 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000505 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500506
507 // Add an opaque rectangle on top of some of the text.
508 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
509 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
510 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
511 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500512
513 // Add a transparent triangle on top of other part of the text.
514 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
515 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
516 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
517 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
518 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
519 EXPECT_TRUE(FPDFPath_Close(black_path));
520 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500521
522 // Render and check the result. Text is slightly different on Mac.
Lei Zhang107fa7b2018-02-09 21:48:15 +0000523 std::unique_ptr<void, FPDFBitmapDeleter> bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400524#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700525 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500526#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000527 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400528#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000529 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500530 UnloadPage(page);
531}
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500532
wileyryae858aa42017-05-31 14:49:05 -0500533TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
534 // Load document with existing content
535 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
536 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000537 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -0500538
539 // Add a transparent rectangle on top of the existing content
540 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
541 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
542 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
543 FPDFPage_InsertObject(page, red_rect2);
544
545 // Add an opaque rectangle on top of the existing content
546 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
547 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
548 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
549 FPDFPage_InsertObject(page, red_rect);
550
Lei Zhang107fa7b2018-02-09 21:48:15 +0000551 std::unique_ptr<void, FPDFBitmapDeleter> bitmap = RenderLoadedPage(page);
552 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500553 EXPECT_TRUE(FPDFPage_GenerateContent(page));
554
555 // Now save the result, closing the page and document
556 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400557 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -0500558
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400559 OpenSavedDocument();
Lei Zhang107fa7b2018-02-09 21:48:15 +0000560 FPDF_PAGE saved_page = LoadSavedPage(0);
561 VerifySavedRendering(saved_page, 612, 792,
562 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500563
564 ClearString();
565 // Add another opaque rectangle on top of the existing content
566 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
567 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
568 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000569 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -0500570
571 // Add another transparent rectangle on top of existing content
572 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
573 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
574 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000575 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000576 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
577 {
578 std::unique_ptr<void, FPDFBitmapDeleter> new_bitmap =
579 RenderSavedPage(saved_page);
580 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
581 }
Lei Zhang107fa7b2018-02-09 21:48:15 +0000582 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -0500583
584 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +0000585 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400586
Lei Zhang107fa7b2018-02-09 21:48:15 +0000587 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400588 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -0500589
590 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +0000591 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -0500592}
593
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500594TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
595 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500596 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500597
598 // Add a large stroked rectangle (fill color should not affect it).
599 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
600 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
601 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
602 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
603 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
604 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000605 {
606 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
607 RenderPageWithFlags(page, nullptr, 0);
608 CompareBitmap(page_bitmap.get(), 612, 792,
609 "64bd31f862a89e0a9e505a5af6efd506");
610 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500611
612 // Add crossed-checkmark
613 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
614 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
615 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
616 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
617 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
618 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
619 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
620 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
621 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000622 {
623 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
624 RenderPageWithFlags(page, nullptr, 0);
625 CompareBitmap(page_bitmap.get(), 612, 792,
626 "4b6f3b9d25c4e194821217d5016c3724");
627 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500628
629 // Add stroked and filled oval-ish path.
630 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
631 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
632 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
633 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
634 EXPECT_TRUE(FPDFPath_Close(path));
635 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
636 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
637 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
638 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
639 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000640 {
641 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
642 RenderPageWithFlags(page, nullptr, 0);
643 CompareBitmap(page_bitmap.get(), 612, 792,
644 "ff3e6a22326754944cc6e56609acd73b");
645 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500646 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500647}
Nicolas Pena49058402017-02-14 18:26:20 -0500648
649TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
650 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500651 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -0500652
653 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400654 FPDF_PAGEOBJECT text_object1 =
655 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
656 EXPECT_TRUE(text_object1);
657 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
658 GetFPDFWideString(L"I'm at the bottom of the page");
659 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
660 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
661 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000662 {
663 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
664 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400665#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000666 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -0500667#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000668 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400669#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000670 CompareBitmap(page_bitmap.get(), 612, 792, md5);
671 }
Nicolas Pena49058402017-02-14 18:26:20 -0500672
673 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -0400674 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500675 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400676 EXPECT_TRUE(text_object2);
677 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
678 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
679 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
680 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
681 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000682 {
683 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
684 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400685#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700686 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -0400687#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Henrique Nakashima09b41922017-10-27 20:39:29 +0000688 const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d";
Nicolas Pena49058402017-02-14 18:26:20 -0500689#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000690 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400691#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000692 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
693 }
Nicolas Pena49058402017-02-14 18:26:20 -0500694
695 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -0400696 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500697 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400698 EXPECT_TRUE(text_object3);
699 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
700 GetFPDFWideString(L"Can you read me? <:)>");
701 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
702 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
703 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000704 {
705 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
706 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400707#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000708 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -0400709#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000710 const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2";
Nicolas Pena49058402017-02-14 18:26:20 -0500711#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000712 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400713#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000714 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
715 }
Nicolas Pena49058402017-02-14 18:26:20 -0500716
717 // TODO(npm): Why are there issues with text rotated by 90 degrees?
718 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
719 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -0500720}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500721
Nicolas Pena603a31d2017-06-14 11:41:18 -0400722TEST_F(FPDFEditEmbeddertest, GraphicsData) {
723 // New page
724 std::unique_ptr<void, FPDFPageDeleter> page(
725 FPDFPage_New(CreateNewDocument(), 0, 612, 792));
726
727 // Create a rect with nontrivial graphics
728 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
729 FPDFPageObj_SetBlendMode(rect1, "Color");
730 FPDFPage_InsertObject(page.get(), rect1);
731 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
732
733 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +0000734 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
735 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -0400736 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400737 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400738
739 // Add a text object causing no change to the graphics dictionary
740 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
741 // Only alpha, the last component, matters for the graphics dictionary. And
742 // the default value is 255.
743 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
744 FPDFPage_InsertObject(page.get(), text1);
745 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400746 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400747
748 // Add a text object increasing the size of the graphics dictionary
749 FPDF_PAGEOBJECT text2 =
750 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
751 FPDFPage_InsertObject(page.get(), text2);
752 FPDFPageObj_SetBlendMode(text2, "Darken");
753 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
754 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400755 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400756
757 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -0400758 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -0400759 FPDFPageObj_SetBlendMode(path, "Darken");
760 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
761 FPDFPage_InsertObject(page.get(), path);
762 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400763 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400764
765 // Add a rect increasing the size of the graphics dictionary
766 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
767 FPDFPageObj_SetBlendMode(rect2, "Darken");
768 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
769 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
770 FPDFPage_InsertObject(page.get(), rect2);
771 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400772 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400773}
774
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500775TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
776 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500777 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500778
779 // Add a red rectangle with some non-default alpha
780 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
781 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
782 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
783 FPDFPage_InsertObject(page, rect);
784 EXPECT_TRUE(FPDFPage_GenerateContent(page));
785
786 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +0000787 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
788 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500789 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400790 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500791
792 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +0000793 {
794 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
795 RenderPageWithFlags(page, nullptr, 0);
796 CompareBitmap(page_bitmap.get(), 612, 792,
797 "5384da3406d62360ffb5cac4476fff1c");
798 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500799
800 // Never mind, my new favorite color is blue, increase alpha
801 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
802 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400803 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500804
805 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +0000806 {
807 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
808 RenderPageWithFlags(page, nullptr, 0);
809 CompareBitmap(page_bitmap.get(), 612, 792,
810 "2e51656f5073b0bee611d9cd086aa09c");
811 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500812
813 // And now generate, without changes
814 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400815 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000816 {
817 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
818 RenderPageWithFlags(page, nullptr, 0);
819 CompareBitmap(page_bitmap.get(), 612, 792,
820 "2e51656f5073b0bee611d9cd086aa09c");
821 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500822
823 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400824 FPDF_PAGEOBJECT text_object =
825 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
826 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
827 GetFPDFWideString(L"Something something #text# something");
828 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
829 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
830 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500831 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000832 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500833 ASSERT_TRUE(font_dict);
834 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
835
836 // Generate yet again, check dicts are reasonably sized
837 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400838 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500839 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
840 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500841}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500842
Nicolas Penad03ca422017-03-06 13:54:33 -0500843TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
844 CreateNewDocument();
845 // TODO(npm): use other fonts after disallowing loading any font as any type
846 const CPDF_Font* stock_font =
847 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700848 const uint8_t* data = stock_font->GetFont()->GetFontData();
849 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400850 std::unique_ptr<void, FPDFFontDeleter> font(
851 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
852 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400853 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500854 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500855
Nicolas Penad03ca422017-03-06 13:54:33 -0500856 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -0500857 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
858 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
859 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
860 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
861 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
862 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -0500863 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
864
Nicolas Penabe90aae2017-02-27 10:41:41 -0500865 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -0500866 ASSERT_TRUE(widths_array);
867 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500868 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -0500869 EXPECT_EQ(569, widths_array->GetNumberAt(11));
870 EXPECT_EQ(500, widths_array->GetNumberAt(223));
871 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
872}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500873
Nicolas Penad03ca422017-03-06 13:54:33 -0500874TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
875 CreateNewDocument();
876 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700877 const uint8_t* data = stock_font->GetFont()->GetFontData();
878 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400879 std::unique_ptr<void, FPDFFontDeleter> font(
880 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
881 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400882 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500883 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500884
Nicolas Penad03ca422017-03-06 13:54:33 -0500885 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
886 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
887 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
888 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
889 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
890 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
891 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
892 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -0500893
Nicolas Penad03ca422017-03-06 13:54:33 -0500894 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
895 ASSERT_TRUE(widths_array);
896 ASSERT_EQ(224U, widths_array->GetCount());
897 EXPECT_EQ(600, widths_array->GetNumberAt(33));
898 EXPECT_EQ(600, widths_array->GetNumberAt(74));
899 EXPECT_EQ(600, widths_array->GetNumberAt(223));
900 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
901}
902
903TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
904 CreateNewDocument();
905 const CPDF_Font* stock_font =
906 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700907 const uint8_t* data = stock_font->GetFont()->GetFontData();
908 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400909 std::unique_ptr<void, FPDFFontDeleter> font(
910 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
911 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400912 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500913 EXPECT_TRUE(typed_font->IsCIDFont());
914
915 // Check font dictionary entries
916 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
917 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
918 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
919 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
920 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
921 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
922 ASSERT_TRUE(descendant_array);
923 EXPECT_EQ(1U, descendant_array->GetCount());
924
925 // Check the CIDFontDict
926 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
927 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
928 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
929 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
930 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
931 ASSERT_TRUE(cidinfo_dict);
932 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
933 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
934 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
935 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
936
937 // Check widths
938 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
939 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -0400940 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -0500941 CheckCompositeFontWidths(widths_array, typed_font);
942}
943
944TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
945 CreateNewDocument();
946 const CPDF_Font* stock_font =
947 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700948 const uint8_t* data = stock_font->GetFont()->GetFontData();
949 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -0500950
Nicolas Penab3161852017-05-02 14:12:50 -0400951 std::unique_ptr<void, FPDFFontDeleter> font(
952 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
953 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400954 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500955 EXPECT_TRUE(typed_font->IsCIDFont());
956
957 // Check font dictionary entries
958 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
959 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
960 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
961 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
962 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
963 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
964 ASSERT_TRUE(descendant_array);
965 EXPECT_EQ(1U, descendant_array->GetCount());
966
967 // Check the CIDFontDict
968 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
969 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
970 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
971 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
972 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
973 ASSERT_TRUE(cidinfo_dict);
974 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
975 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
976 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
977 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
978 data);
979
980 // Check widths
981 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
982 ASSERT_TRUE(widths_array);
983 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -0500984}
rbpotterce8e51e2017-04-28 12:42:47 -0700985
986TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
987 // Load document with a -90 degree rotation
988 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
989 FPDF_PAGE page = LoadPage(0);
990 EXPECT_NE(nullptr, page);
991
992 EXPECT_EQ(3, FPDFPage_GetRotation(page));
993 UnloadPage(page);
994}
Nicolas Penab3161852017-05-02 14:12:50 -0400995
996TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
997 // Start with a blank page
998 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
999 {
1000 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001001 const uint8_t* data = stock_font->GetFont()->GetFontData();
1002 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -04001003 std::unique_ptr<void, FPDFFontDeleter> font(
1004 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
1005 ASSERT_TRUE(font.get());
1006
1007 // Add some text to the page
1008 FPDF_PAGEOBJECT text_object =
1009 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1010 EXPECT_TRUE(text_object);
1011 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1012 GetFPDFWideString(L"I am testing my loaded font, WEE.");
1013 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1014 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
1015 FPDFPage_InsertObject(page, text_object);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001016 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
1017 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001018#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001019 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
1020#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001021 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04001022#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001023 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04001024
1025 // Add some more text, same font
1026 FPDF_PAGEOBJECT text_object2 =
1027 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
1028 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1029 GetFPDFWideString(L"Bigger font size");
1030 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1031 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
1032 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04001033 }
Lei Zhang107fa7b2018-02-09 21:48:15 +00001034 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap2 =
1035 RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001036#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001037 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
1038#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001039 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04001040#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001041 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001042
Nicolas Pena207b7272017-05-26 17:37:06 -04001043 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04001044 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1045 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001046
1047 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001048}
Nicolas Penaf45ade32017-05-03 10:23:49 -04001049
Jane Liueda65252017-06-07 11:31:27 -04001050TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
1051 // Open a file with one annotation and load its first page.
1052 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001053 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04001054 ASSERT_TRUE(page);
1055
Lei Zhanga21d5932018-02-05 18:28:38 +00001056 {
1057 // Add an underline annotation to the page without specifying its rectangle.
1058 std::unique_ptr<void, FPDFAnnotationDeleter> annot(
1059 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
1060 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04001061
Lei Zhanga21d5932018-02-05 18:28:38 +00001062 // FPDFPage_TransformAnnots() should run without errors when modifying
1063 // annotation rectangles.
1064 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
1065 }
Jane Liueda65252017-06-07 11:31:27 -04001066 UnloadPage(page);
1067}
1068
Nicolas Penaf45ade32017-05-03 10:23:49 -04001069// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04001070#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -04001071TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
1072 // Start with a blank page
1073 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1074 CFX_Font CIDfont;
1075 {
1076 // First, get the data from the font
1077 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
1078 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
1079 const uint8_t* data = CIDfont.GetFontData();
1080 const uint32_t size = CIDfont.GetSize();
1081
1082 // Load the data into a FPDF_Font.
1083 std::unique_ptr<void, FPDFFontDeleter> font(
1084 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1085 ASSERT_TRUE(font.get());
1086
1087 // Add some text to the page
1088 FPDF_PAGEOBJECT text_object =
1089 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1090 ASSERT_TRUE(text_object);
1091 std::wstring wstr = L"ABCDEFGhijklmnop.";
1092 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1093 GetFPDFWideString(wstr);
1094 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1095 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
1096 FPDFPage_InsertObject(page, text_object);
1097
1098 // And add some Japanese characters
1099 FPDF_PAGEOBJECT text_object2 =
1100 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
1101 ASSERT_TRUE(text_object2);
1102 std::wstring wstr2 =
1103 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
1104 L"\u756A";
1105 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1106 GetFPDFWideString(wstr2);
1107 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1108 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
1109 FPDFPage_InsertObject(page, text_object2);
1110 }
1111
Nicolas Pena207b7272017-05-26 17:37:06 -04001112 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00001113 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00001114 {
1115 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
1116 RenderPageWithFlags(page, nullptr, 0);
1117 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1118 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04001119
1120 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04001121 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04001122 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1123 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001124
1125 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001126}
Dan Sinclair698aed72017-09-26 16:24:49 -04001127#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001128
1129TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04001130 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001131 {
1132 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
1133 FPDF_PAGE page = LoadPage(0);
1134 ASSERT_NE(nullptr, page);
1135
1136 // Now add a more complex blue path.
1137 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
1138 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
1139 // TODO(npm): stroking will cause the MD5s to differ.
1140 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
1141 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
1142 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
1143 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
1144 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
1145 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
1146 EXPECT_TRUE(FPDFPath_Close(green_path));
1147 FPDFPage_InsertObject(page, green_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001148 std::unique_ptr<void, FPDFBitmapDeleter> page_bitmap =
1149 RenderLoadedPage(page);
1150 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001151
1152 // Now save the result, closing the page and document
1153 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1154 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1155 UnloadPage(page);
1156 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001157
1158 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001159}
Jane Liu28fb7ba2017-08-02 21:45:57 -04001160
1161TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
1162 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1163 FPDF_PAGE page = LoadPage(0);
1164 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001165 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04001166
1167 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1168 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1169 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
1170
1171 obj = FPDFPage_GetObject(page, 33);
1172 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1173 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
1174 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1175 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
1176 FPDFBitmap_Destroy(bitmap);
1177
1178 obj = FPDFPage_GetObject(page, 34);
1179 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1180 bitmap = FPDFImageObj_GetBitmap(obj);
1181 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1182 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
1183 FPDFBitmap_Destroy(bitmap);
1184
1185 obj = FPDFPage_GetObject(page, 35);
1186 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1187 bitmap = FPDFImageObj_GetBitmap(obj);
1188 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
1189 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
1190 FPDFBitmap_Destroy(bitmap);
1191
1192 obj = FPDFPage_GetObject(page, 36);
1193 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1194 bitmap = FPDFImageObj_GetBitmap(obj);
1195 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1196 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
1197 FPDFBitmap_Destroy(bitmap);
1198
1199 obj = FPDFPage_GetObject(page, 37);
1200 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1201 bitmap = FPDFImageObj_GetBitmap(obj);
1202 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1203 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
1204 FPDFBitmap_Destroy(bitmap);
1205
1206 obj = FPDFPage_GetObject(page, 38);
1207 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1208 bitmap = FPDFImageObj_GetBitmap(obj);
1209 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1210 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
1211 FPDFBitmap_Destroy(bitmap);
1212 UnloadPage(page);
1213}
Jane Liu548334e2017-08-03 16:33:40 -04001214
Lei Zhang53341dd2018-03-01 15:42:47 +00001215TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) {
1216 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
1217 FPDF_PAGE page = LoadPage(0);
1218 ASSERT_TRUE(page);
1219 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1220
1221 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
1222 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1223 {
Lei Zhang53341dd2018-03-01 15:42:47 +00001224 std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
1225 FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00001226 ASSERT_TRUE(bitmap);
1227 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
1228 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00001229 }
1230
1231 UnloadPage(page);
1232}
1233
Jane Liu548334e2017-08-03 16:33:40 -04001234TEST_F(FPDFEditEmbeddertest, GetImageData) {
1235 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1236 FPDF_PAGE page = LoadPage(0);
1237 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001238 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04001239
1240 // Retrieve an image object with flate-encoded data stream.
1241 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
1242 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1243
1244 // Check that the raw image data has the correct length and hash value.
1245 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1246 std::vector<char> buf(len);
1247 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1248 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
1249 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1250
1251 // Check that the decoded image data has the correct length and hash value.
1252 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1253 buf.clear();
1254 buf.resize(len);
1255 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1256 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
1257 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1258
1259 // Retrieve an image obejct with DCTDecode-encoded data stream.
1260 obj = FPDFPage_GetObject(page, 37);
1261 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1262
1263 // Check that the raw image data has the correct length and hash value.
1264 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1265 buf.clear();
1266 buf.resize(len);
1267 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1268 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1269 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1270
1271 // Check that the decoded image data has the correct length and hash value,
1272 // which should be the same as those of the raw data, since this image is
1273 // encoded by a single DCTDecode filter and decoding is a noop.
1274 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1275 buf.clear();
1276 buf.resize(len);
1277 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1278 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1279 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1280
1281 UnloadPage(page);
1282}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04001283
1284TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
1285 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1286 ASSERT_TRUE(rect);
1287
1288 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
1289 FPDFPageObj_Destroy(rect);
1290}
Jane Liube63ab92017-08-09 14:09:34 -04001291
1292TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
1293 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1294 FPDF_PAGE page = LoadPage(0);
1295 ASSERT_TRUE(page);
1296
1297 // Verify that retrieving the filter of a non-image object would fail.
1298 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1299 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1300 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
1301 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
1302
1303 // Verify the returned filter string for an image object with a single filter.
1304 obj = FPDFPage_GetObject(page, 33);
1305 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1306 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
1307 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1308 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001309 static constexpr char kFlateDecode[] = "FlateDecode";
1310 EXPECT_EQ(sizeof(kFlateDecode),
1311 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1312 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001313 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
1314
1315 // Verify all the filters for an image object with a list of filters.
1316 obj = FPDFPage_GetObject(page, 38);
1317 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1318 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
1319 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1320 buf.clear();
1321 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001322 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
1323 EXPECT_EQ(sizeof(kASCIIHexDecode),
1324 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1325 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001326
1327 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
1328 buf.clear();
1329 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001330 static constexpr char kDCTDecode[] = "DCTDecode";
1331 EXPECT_EQ(sizeof(kDCTDecode),
1332 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
1333 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001334
1335 UnloadPage(page);
1336}
Jane Liuca898292017-08-16 11:25:35 -04001337
1338TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
1339 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1340 FPDF_PAGE page = LoadPage(0);
1341 ASSERT_TRUE(page);
1342
1343 // Check that getting the metadata of a null object would fail.
1344 FPDF_IMAGEOBJ_METADATA metadata;
1345 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
1346
1347 // Check that receiving the metadata with a null metadata object would fail.
1348 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
1349 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
1350
1351 // Check that when retrieving an image object's metadata without passing in
1352 // |page|, all values are correct, with the last two being default values.
1353 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1354 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001355 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001356 EXPECT_EQ(92u, metadata.width);
1357 EXPECT_EQ(68u, metadata.height);
1358 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1359 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1360 EXPECT_EQ(0u, metadata.bits_per_pixel);
1361 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
1362
1363 // Verify the metadata of a bitmap image with indexed colorspace.
1364 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001365 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001366 EXPECT_EQ(92u, metadata.width);
1367 EXPECT_EQ(68u, metadata.height);
1368 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1369 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1370 EXPECT_EQ(1u, metadata.bits_per_pixel);
1371 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
1372
1373 // Verify the metadata of an image with RGB colorspace.
1374 obj = FPDFPage_GetObject(page, 37);
1375 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1376 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001377 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001378 EXPECT_EQ(126u, metadata.width);
1379 EXPECT_EQ(106u, metadata.height);
1380 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
1381 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
1382 EXPECT_EQ(24u, metadata.bits_per_pixel);
1383 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
1384
1385 UnloadPage(page);
1386}