blob: e62ef2149b7323e3b932a6edf120f0dd20312212 [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"));
50 int font_flags = font_desc->GetIntegerFor("Flags");
51 if (bold)
52 EXPECT_TRUE(font_flags & FXFONT_BOLD);
53 else
54 EXPECT_FALSE(font_flags & FXFONT_BOLD);
55 if (italic)
56 EXPECT_TRUE(font_flags & FXFONT_ITALIC);
57 else
58 EXPECT_FALSE(font_flags & FXFONT_ITALIC);
59 EXPECT_TRUE(font_flags & FXFONT_NONSYMBOLIC);
60 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
61 EXPECT_EQ(4U, font_desc->GetArrayFor("FontBBox")->GetCount());
62 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
63 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
64 EXPECT_TRUE(font_desc->KeyExist("Descent"));
65 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
66 EXPECT_TRUE(font_desc->KeyExist("StemV"));
67 CFX_ByteString present("FontFile");
68 CFX_ByteString absent("FontFile2");
69 if (font_type == FPDF_FONT_TRUETYPE)
70 std::swap(present, absent);
71 EXPECT_TRUE(font_desc->KeyExist(present));
72 EXPECT_FALSE(font_desc->KeyExist(absent));
73
74 // Check that the font stream is the one that was provided
75 CPDF_Stream* font_stream = font_desc->GetStreamFor(present);
76 ASSERT_EQ(size, font_stream->GetRawSize());
77 uint8_t* stream_data = font_stream->GetRawData();
78 for (size_t j = 0; j < size; j++)
79 EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j;
80 }
81
82 void CheckCompositeFontWidths(CPDF_Array* widths_array,
83 CPDF_Font* typed_font) {
84 // Check that W array is in a format that conforms to PDF spec 1.7 section
85 // "Glyph Metrics in CIDFonts" (these checks are not
86 // implementation-specific).
87 EXPECT_GT(widths_array->GetCount(), 1U);
88 int num_cids_checked = 0;
89 int cur_cid = 0;
90 for (size_t idx = 0; idx < widths_array->GetCount(); idx++) {
91 int cid = widths_array->GetNumberAt(idx);
92 EXPECT_GE(cid, cur_cid);
93 ASSERT_FALSE(++idx == widths_array->GetCount());
94 CPDF_Object* next = widths_array->GetObjectAt(idx);
95 if (next->IsArray()) {
96 // We are in the c [w1 w2 ...] case
97 CPDF_Array* arr = next->AsArray();
98 int cnt = static_cast<int>(arr->GetCount());
99 size_t inner_idx = 0;
100 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
101 int width = arr->GetNumberAt(inner_idx++);
102 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
103 << cur_cid;
104 }
105 num_cids_checked += cnt;
106 continue;
107 }
108 // Otherwise, are in the c_first c_last w case.
109 ASSERT_TRUE(next->IsNumber());
110 int last_cid = next->AsNumber()->GetInteger();
111 ASSERT_FALSE(++idx == widths_array->GetCount());
112 int width = widths_array->GetNumberAt(idx);
113 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
114 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
115 << cur_cid;
116 }
117 num_cids_checked += last_cid - cid + 1;
118 }
119 // Make sure we have a good amount of cids described
120 EXPECT_GT(num_cids_checked, 900);
121 }
122 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
123
124 private:
125 CPDF_Document* cpdf_doc_;
126};
Tom Sepezd483eb42016-01-06 10:03:59 -0800127
etienneb7712c262016-04-26 08:13:45 -0700128namespace {
thestigdc7ec032016-11-21 15:32:52 -0800129
etienneb7712c262016-04-26 08:13:45 -0700130const char kExpectedPDF[] =
131 "%PDF-1.7\r\n"
132 "%\xA1\xB3\xC5\xD7\r\n"
133 "1 0 obj\r\n"
134 "<</Pages 2 0 R /Type/Catalog>>\r\n"
135 "endobj\r\n"
136 "2 0 obj\r\n"
137 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
138 "endobj\r\n"
139 "3 0 obj\r\n"
140 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
141 "endobj\r\n"
142 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400143 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
144 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400145 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700146 ">>\r\n"
147 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400148 "5 0 obj\r\n"
149 "<</BM/Normal/CA 1/ca 1>>\r\n"
150 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700151 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400152 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700153 "0000000000 65535 f\r\n"
154 "0000000017 00000 n\r\n"
155 "0000000066 00000 n\r\n"
156 "0000000122 00000 n\r\n"
157 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400158 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700159 "trailer\r\n"
160 "<<\r\n"
161 "/Root 1 0 R\r\n"
162 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400163 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700164 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400165 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700166 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800167
etienneb7712c262016-04-26 08:13:45 -0700168} // namespace
169
Tom Sepezd483eb42016-01-06 10:03:59 -0800170TEST_F(FPDFEditEmbeddertest, EmptyCreation) {
171 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700172 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800173 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400174 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800175 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800176 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700177
Nicolas Penad9d6c292017-06-06 16:12:10 -0400178 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
179 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700180 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800181}
thestigdc7ec032016-11-21 15:32:52 -0800182
183// Regression test for https://crbug.com/667012
184TEST_F(FPDFEditEmbeddertest, RasterizePDF) {
185 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
186
187 // Get the bitmap for the original document/
188 FPDF_BITMAP orig_bitmap;
189 {
190 EXPECT_TRUE(OpenDocument("black.pdf"));
191 FPDF_PAGE orig_page = LoadPage(0);
192 EXPECT_NE(nullptr, orig_page);
193 orig_bitmap = RenderPage(orig_page);
194 CompareBitmap(orig_bitmap, 612, 792, kAllBlackMd5sum);
195 UnloadPage(orig_page);
196 }
197
198 // Create a new document from |orig_bitmap| and save it.
199 {
200 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
201 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
202
203 // Add the bitmap to an image object and add the image object to the output
204 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700205 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
thestigdc7ec032016-11-21 15:32:52 -0800206 EXPECT_TRUE(FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap));
207 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
208 FPDFPage_InsertObject(temp_page, temp_img);
209 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
210 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
211 FPDF_ClosePage(temp_page);
212 FPDF_CloseDocument(temp_doc);
213 }
214 FPDFBitmap_Destroy(orig_bitmap);
215
216 // Get the generated content. Make sure it is at least as big as the original
217 // PDF.
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400218 EXPECT_GT(GetString().size(), 923U);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400219 TestAndCloseSaved(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800220}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500221
222TEST_F(FPDFEditEmbeddertest, AddPaths) {
223 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500224 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500225
226 // We will first add a red rectangle
227 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
228 ASSERT_NE(nullptr, red_rect);
229 // Expect false when trying to set colors out of range
230 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
231 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
232
233 // Fill rectangle with red and insert to the page
234 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
235 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
236 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500237 FPDF_BITMAP page_bitmap = RenderPage(page);
238 CompareBitmap(page_bitmap, 612, 792, "66d02eaa6181e2c069ce2ea99beda497");
239 FPDFBitmap_Destroy(page_bitmap);
240
241 // Now add to that a green rectangle with some medium alpha
242 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
243 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200244
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200245 // Make sure the type of the rectangle is a path.
246 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
247
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200248 // Make sure we get back the same color we set previously.
249 unsigned int R;
250 unsigned int G;
251 unsigned int B;
252 unsigned int A;
253 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
254 EXPECT_EQ(0U, R);
255 EXPECT_EQ(255U, G);
256 EXPECT_EQ(0U, B);
257 EXPECT_EQ(128U, A);
258
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500259 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
260 FPDFPage_InsertObject(page, green_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500261 page_bitmap = RenderPage(page);
262 CompareBitmap(page_bitmap, 612, 792, "7b0b87604594e773add528fae567a558");
263 FPDFBitmap_Destroy(page_bitmap);
264
265 // Add a black triangle.
266 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
267 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
268 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
269 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
270 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
271 EXPECT_TRUE(FPDFPath_Close(black_path));
272 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500273 page_bitmap = RenderPage(page);
274 CompareBitmap(page_bitmap, 612, 792, "eadc8020a14dfcf091da2688733d8806");
275 FPDFBitmap_Destroy(page_bitmap);
276
277 // Now add a more complex blue path.
278 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
279 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
280 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
281 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
282 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
283 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
284 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
285 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
286 EXPECT_TRUE(FPDFPath_Close(blue_path));
287 FPDFPage_InsertObject(page, blue_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500288 page_bitmap = RenderPage(page);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400289 const char last_md5[] = "9823e1a21bd9b72b6a442ba4f12af946";
290 CompareBitmap(page_bitmap, 612, 792, last_md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500291 FPDFBitmap_Destroy(page_bitmap);
292
293 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400294 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500295 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500296 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500297
298 // Render the saved result
Nicolas Pena3ff54002017-07-05 11:55:35 -0400299 TestAndCloseSaved(612, 792, last_md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500300}
301
302TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
303 // Load document with some text
304 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
305 FPDF_PAGE page = LoadPage(0);
306 EXPECT_NE(nullptr, page);
307
308 // Add an opaque rectangle on top of some of the text.
309 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
310 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
311 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
312 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500313
314 // Add a transparent triangle on top of other part of the text.
315 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
316 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
317 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
318 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
319 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
320 EXPECT_TRUE(FPDFPath_Close(black_path));
321 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500322
323 // Render and check the result. Text is slightly different on Mac.
324 FPDF_BITMAP bitmap = RenderPage(page);
325#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700326 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500327#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400328 const char md5[] = "bc6e6eb50dda4695ba0fb4d04ed82ada";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400329#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700330 CompareBitmap(bitmap, 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500331 FPDFBitmap_Destroy(bitmap);
332 UnloadPage(page);
333}
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500334
wileyryae858aa42017-05-31 14:49:05 -0500335TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
336 // Load document with existing content
337 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
338 FPDF_PAGE page = LoadPage(0);
339 EXPECT_NE(nullptr, page);
340
341 // Add a transparent rectangle on top of the existing content
342 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
343 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
344 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
345 FPDFPage_InsertObject(page, red_rect2);
346
347 // Add an opaque rectangle on top of the existing content
348 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
349 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
350 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
351 FPDFPage_InsertObject(page, red_rect);
352
353 FPDF_BITMAP bitmap = RenderPage(page);
354 CompareBitmap(bitmap, 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
355 FPDFBitmap_Destroy(bitmap);
356 EXPECT_TRUE(FPDFPage_GenerateContent(page));
357
358 // Now save the result, closing the page and document
359 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400360 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -0500361
Nicolas Pena3ff54002017-07-05 11:55:35 -0400362 // Render the saved result without closing the page and document
363 TestSaved(612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500364
365 ClearString();
366 // Add another opaque rectangle on top of the existing content
367 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
368 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
369 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400370 FPDFPage_InsertObject(m_SavedPage, green_rect);
wileyryae858aa42017-05-31 14:49:05 -0500371
372 // Add another transparent rectangle on top of existing content
373 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
374 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
375 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400376 FPDFPage_InsertObject(m_SavedPage, green_rect2);
377 FPDF_BITMAP new_bitmap = RenderPageWithFlags(m_SavedPage, m_SavedForm, 0);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400378 const char last_md5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
379 CompareBitmap(new_bitmap, 612, 792, last_md5);
wileyryae858aa42017-05-31 14:49:05 -0500380 FPDFBitmap_Destroy(new_bitmap);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400381 EXPECT_TRUE(FPDFPage_GenerateContent(m_SavedPage));
wileyryae858aa42017-05-31 14:49:05 -0500382
383 // Now save the result, closing the page and document
Nicolas Pena3ff54002017-07-05 11:55:35 -0400384 EXPECT_TRUE(FPDF_SaveAsCopy(m_SavedDocument, this, 0));
385 CloseSaved();
wileyryae858aa42017-05-31 14:49:05 -0500386
387 // Render the saved result
Nicolas Pena3ff54002017-07-05 11:55:35 -0400388 TestAndCloseSaved(612, 792, last_md5);
wileyryae858aa42017-05-31 14:49:05 -0500389}
390
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500391TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
392 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500393 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500394
395 // Add a large stroked rectangle (fill color should not affect it).
396 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
397 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
398 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
399 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
400 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
401 FPDFPage_InsertObject(page, rect);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500402 FPDF_BITMAP page_bitmap = RenderPage(page);
403 CompareBitmap(page_bitmap, 612, 792, "64bd31f862a89e0a9e505a5af6efd506");
404 FPDFBitmap_Destroy(page_bitmap);
405
406 // Add crossed-checkmark
407 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
408 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
409 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
410 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
411 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
412 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
413 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
414 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
415 FPDFPage_InsertObject(page, check);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500416 page_bitmap = RenderPage(page);
417 CompareBitmap(page_bitmap, 612, 792, "4b6f3b9d25c4e194821217d5016c3724");
418 FPDFBitmap_Destroy(page_bitmap);
419
420 // Add stroked and filled oval-ish path.
421 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
422 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
423 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
424 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
425 EXPECT_TRUE(FPDFPath_Close(path));
426 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
427 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
428 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
429 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
430 FPDFPage_InsertObject(page, path);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500431 page_bitmap = RenderPage(page);
432 CompareBitmap(page_bitmap, 612, 792, "ff3e6a22326754944cc6e56609acd73b");
433 FPDFBitmap_Destroy(page_bitmap);
434 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500435}
Nicolas Pena49058402017-02-14 18:26:20 -0500436
437TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
438 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500439 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -0500440
441 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400442 FPDF_PAGEOBJECT text_object1 =
443 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
444 EXPECT_TRUE(text_object1);
445 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
446 GetFPDFWideString(L"I'm at the bottom of the page");
447 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
448 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
449 FPDFPage_InsertObject(page, text_object1);
Nicolas Pena49058402017-02-14 18:26:20 -0500450 FPDF_BITMAP page_bitmap = RenderPage(page);
451#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700452 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -0500453#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400454 const char md5[] = "7a35771853a1cbba38f6775807878625";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400455#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700456 CompareBitmap(page_bitmap, 612, 792, md5);
Nicolas Pena49058402017-02-14 18:26:20 -0500457 FPDFBitmap_Destroy(page_bitmap);
458
459 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -0400460 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500461 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400462 EXPECT_TRUE(text_object2);
463 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
464 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
465 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
466 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
467 FPDFPage_InsertObject(page, text_object2);
Nicolas Pena49058402017-02-14 18:26:20 -0500468 page_bitmap = RenderPage(page);
469#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700470 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Lei Zhang3de50052017-03-29 21:02:13 -0700471#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400472 const char md5_2[] = "b231b329a4b566fb9b42bfc15fe59bb7";
Nicolas Pena49058402017-02-14 18:26:20 -0500473#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400474 const char md5_2[] = "f85fae151851436072b7b3c6703e506a";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400475#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700476 CompareBitmap(page_bitmap, 612, 792, md5_2);
Nicolas Pena49058402017-02-14 18:26:20 -0500477 FPDFBitmap_Destroy(page_bitmap);
478
479 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -0400480 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500481 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400482 EXPECT_TRUE(text_object3);
483 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
484 GetFPDFWideString(L"Can you read me? <:)>");
485 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
486 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
487 FPDFPage_InsertObject(page, text_object3);
Nicolas Pena49058402017-02-14 18:26:20 -0500488 page_bitmap = RenderPage(page);
489#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700490 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Lei Zhang3de50052017-03-29 21:02:13 -0700491#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400492 const char md5_3[] = "ba874b3b137f984510c4e287ed4ba7ae";
Nicolas Pena49058402017-02-14 18:26:20 -0500493#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400494 const char md5_3[] = "c5aed6a8ef05558c8c47d58c87cbcb46";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400495#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700496 CompareBitmap(page_bitmap, 612, 792, md5_3);
Nicolas Pena49058402017-02-14 18:26:20 -0500497 FPDFBitmap_Destroy(page_bitmap);
498
499 // TODO(npm): Why are there issues with text rotated by 90 degrees?
500 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
501 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -0500502}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500503
Nicolas Pena603a31d2017-06-14 11:41:18 -0400504TEST_F(FPDFEditEmbeddertest, GraphicsData) {
505 // New page
506 std::unique_ptr<void, FPDFPageDeleter> page(
507 FPDFPage_New(CreateNewDocument(), 0, 612, 792));
508
509 // Create a rect with nontrivial graphics
510 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
511 FPDFPageObj_SetBlendMode(rect1, "Color");
512 FPDFPage_InsertObject(page.get(), rect1);
513 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
514
515 // Check that the ExtGState was created
516 CPDF_Page* the_page = CPDFPageFromFPDFPage(page.get());
517 CPDF_Dictionary* graphics_dict =
518 the_page->m_pResources->GetDictFor("ExtGState");
519 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400520 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400521
522 // Add a text object causing no change to the graphics dictionary
523 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
524 // Only alpha, the last component, matters for the graphics dictionary. And
525 // the default value is 255.
526 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
527 FPDFPage_InsertObject(page.get(), text1);
528 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400529 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400530
531 // Add a text object increasing the size of the graphics dictionary
532 FPDF_PAGEOBJECT text2 =
533 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
534 FPDFPage_InsertObject(page.get(), text2);
535 FPDFPageObj_SetBlendMode(text2, "Darken");
536 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
537 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400538 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400539
540 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -0400541 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -0400542 FPDFPageObj_SetBlendMode(path, "Darken");
543 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
544 FPDFPage_InsertObject(page.get(), path);
545 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400546 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400547
548 // Add a rect increasing the size of the graphics dictionary
549 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
550 FPDFPageObj_SetBlendMode(rect2, "Darken");
551 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
552 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
553 FPDFPage_InsertObject(page.get(), rect2);
554 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400555 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400556}
557
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500558TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
559 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500560 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500561
562 // Add a red rectangle with some non-default alpha
563 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
564 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
565 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
566 FPDFPage_InsertObject(page, rect);
567 EXPECT_TRUE(FPDFPage_GenerateContent(page));
568
569 // Check the ExtGState
570 CPDF_Page* the_page = CPDFPageFromFPDFPage(page);
571 CPDF_Dictionary* graphics_dict =
572 the_page->m_pResources->GetDictFor("ExtGState");
573 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400574 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500575
576 // Check the bitmap
577 FPDF_BITMAP page_bitmap = RenderPage(page);
578 CompareBitmap(page_bitmap, 612, 792, "5384da3406d62360ffb5cac4476fff1c");
579 FPDFBitmap_Destroy(page_bitmap);
580
581 // Never mind, my new favorite color is blue, increase alpha
582 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
583 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400584 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500585
586 // Check that bitmap displays changed content
587 page_bitmap = RenderPage(page);
588 CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c");
589 FPDFBitmap_Destroy(page_bitmap);
590
591 // And now generate, without changes
592 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400593 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500594 page_bitmap = RenderPage(page);
595 CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c");
596 FPDFBitmap_Destroy(page_bitmap);
597
598 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400599 FPDF_PAGEOBJECT text_object =
600 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
601 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
602 GetFPDFWideString(L"Something something #text# something");
603 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
604 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
605 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500606 EXPECT_TRUE(FPDFPage_GenerateContent(page));
607 CPDF_Dictionary* font_dict = the_page->m_pResources->GetDictFor("Font");
608 ASSERT_TRUE(font_dict);
609 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
610
611 // Generate yet again, check dicts are reasonably sized
612 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400613 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500614 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
615 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500616}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500617
Nicolas Penad03ca422017-03-06 13:54:33 -0500618TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
619 CreateNewDocument();
620 // TODO(npm): use other fonts after disallowing loading any font as any type
621 const CPDF_Font* stock_font =
622 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700623 const uint8_t* data = stock_font->GetFont()->GetFontData();
624 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400625 std::unique_ptr<void, FPDFFontDeleter> font(
626 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
627 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400628 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500629 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500630
Nicolas Penad03ca422017-03-06 13:54:33 -0500631 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -0500632 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
633 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
634 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
635 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
636 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
637 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -0500638 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
639
Nicolas Penabe90aae2017-02-27 10:41:41 -0500640 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -0500641 ASSERT_TRUE(widths_array);
642 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500643 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -0500644 EXPECT_EQ(569, widths_array->GetNumberAt(11));
645 EXPECT_EQ(500, widths_array->GetNumberAt(223));
646 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
647}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500648
Nicolas Penad03ca422017-03-06 13:54:33 -0500649TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
650 CreateNewDocument();
651 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700652 const uint8_t* data = stock_font->GetFont()->GetFontData();
653 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400654 std::unique_ptr<void, FPDFFontDeleter> font(
655 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
656 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400657 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500658 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500659
Nicolas Penad03ca422017-03-06 13:54:33 -0500660 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
661 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
662 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
663 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
664 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
665 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
666 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
667 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -0500668
Nicolas Penad03ca422017-03-06 13:54:33 -0500669 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
670 ASSERT_TRUE(widths_array);
671 ASSERT_EQ(224U, widths_array->GetCount());
672 EXPECT_EQ(600, widths_array->GetNumberAt(33));
673 EXPECT_EQ(600, widths_array->GetNumberAt(74));
674 EXPECT_EQ(600, widths_array->GetNumberAt(223));
675 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
676}
677
678TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
679 CreateNewDocument();
680 const CPDF_Font* stock_font =
681 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700682 const uint8_t* data = stock_font->GetFont()->GetFontData();
683 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400684 std::unique_ptr<void, FPDFFontDeleter> font(
685 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
686 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400687 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500688 EXPECT_TRUE(typed_font->IsCIDFont());
689
690 // Check font dictionary entries
691 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
692 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
693 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
694 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
695 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
696 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
697 ASSERT_TRUE(descendant_array);
698 EXPECT_EQ(1U, descendant_array->GetCount());
699
700 // Check the CIDFontDict
701 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
702 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
703 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
704 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
705 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
706 ASSERT_TRUE(cidinfo_dict);
707 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
708 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
709 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
710 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
711
712 // Check widths
713 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
714 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -0400715 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -0500716 CheckCompositeFontWidths(widths_array, typed_font);
717}
718
719TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
720 CreateNewDocument();
721 const CPDF_Font* stock_font =
722 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700723 const uint8_t* data = stock_font->GetFont()->GetFontData();
724 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -0500725
Nicolas Penab3161852017-05-02 14:12:50 -0400726 std::unique_ptr<void, FPDFFontDeleter> font(
727 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
728 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400729 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500730 EXPECT_TRUE(typed_font->IsCIDFont());
731
732 // Check font dictionary entries
733 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
734 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
735 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
736 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
737 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
738 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
739 ASSERT_TRUE(descendant_array);
740 EXPECT_EQ(1U, descendant_array->GetCount());
741
742 // Check the CIDFontDict
743 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
744 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
745 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
746 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
747 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
748 ASSERT_TRUE(cidinfo_dict);
749 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
750 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
751 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
752 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
753 data);
754
755 // Check widths
756 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
757 ASSERT_TRUE(widths_array);
758 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -0500759}
rbpotterce8e51e2017-04-28 12:42:47 -0700760
761TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
762 // Load document with a -90 degree rotation
763 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
764 FPDF_PAGE page = LoadPage(0);
765 EXPECT_NE(nullptr, page);
766
767 EXPECT_EQ(3, FPDFPage_GetRotation(page));
768 UnloadPage(page);
769}
Nicolas Penab3161852017-05-02 14:12:50 -0400770
771TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
772 // Start with a blank page
773 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
774 {
775 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700776 const uint8_t* data = stock_font->GetFont()->GetFontData();
777 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400778 std::unique_ptr<void, FPDFFontDeleter> font(
779 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
780 ASSERT_TRUE(font.get());
781
782 // Add some text to the page
783 FPDF_PAGEOBJECT text_object =
784 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
785 EXPECT_TRUE(text_object);
786 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
787 GetFPDFWideString(L"I am testing my loaded font, WEE.");
788 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
789 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
790 FPDFPage_InsertObject(page, text_object);
Nicolas Penab3161852017-05-02 14:12:50 -0400791 FPDF_BITMAP page_bitmap = RenderPage(page);
792#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
793 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
794#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400795 const char md5[] = "1722c6a9deed953d730de9cd13dcbd55";
Nicolas Penab3161852017-05-02 14:12:50 -0400796#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
797 CompareBitmap(page_bitmap, 612, 792, md5);
798 FPDFBitmap_Destroy(page_bitmap);
799
800 // Add some more text, same font
801 FPDF_PAGEOBJECT text_object2 =
802 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
803 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
804 GetFPDFWideString(L"Bigger font size");
805 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
806 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
807 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -0400808 }
809 FPDF_BITMAP page_bitmap2 = RenderPage(page);
810#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
811 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
812#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400813 const char md5_2[] = "9d7885072058f6c3e68ecaf32e917f30";
Nicolas Penab3161852017-05-02 14:12:50 -0400814#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
815 CompareBitmap(page_bitmap2, 612, 792, md5_2);
816 FPDFBitmap_Destroy(page_bitmap2);
817
Nicolas Pena207b7272017-05-26 17:37:06 -0400818 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -0400819 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
820 FPDF_ClosePage(page);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400821 TestAndCloseSaved(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -0400822}
Nicolas Penaf45ade32017-05-03 10:23:49 -0400823
Jane Liueda65252017-06-07 11:31:27 -0400824TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
825 // Open a file with one annotation and load its first page.
826 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
827 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
828 ASSERT_TRUE(page);
829
830 // Add an underline annotation to the page without specifying its rectangle.
Jane Liud60e9ad2017-06-26 11:28:36 -0400831 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE);
832 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -0400833
834 // FPDFPage_TransformAnnots() should run without errors when modifying
835 // annotation rectangles.
836 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
837
Jane Liud60e9ad2017-06-26 11:28:36 -0400838 FPDFPage_CloseAnnot(annot);
Jane Liueda65252017-06-07 11:31:27 -0400839 UnloadPage(page);
840}
841
Nicolas Penaf45ade32017-05-03 10:23:49 -0400842// TODO(npm): Add tests using Japanese fonts in other OS.
843#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_
844TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
845 // Start with a blank page
846 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
847 CFX_Font CIDfont;
848 {
849 // First, get the data from the font
850 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
851 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
852 const uint8_t* data = CIDfont.GetFontData();
853 const uint32_t size = CIDfont.GetSize();
854
855 // Load the data into a FPDF_Font.
856 std::unique_ptr<void, FPDFFontDeleter> font(
857 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
858 ASSERT_TRUE(font.get());
859
860 // Add some text to the page
861 FPDF_PAGEOBJECT text_object =
862 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
863 ASSERT_TRUE(text_object);
864 std::wstring wstr = L"ABCDEFGhijklmnop.";
865 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
866 GetFPDFWideString(wstr);
867 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
868 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
869 FPDFPage_InsertObject(page, text_object);
870
871 // And add some Japanese characters
872 FPDF_PAGEOBJECT text_object2 =
873 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
874 ASSERT_TRUE(text_object2);
875 std::wstring wstr2 =
876 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
877 L"\u756A";
878 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
879 GetFPDFWideString(wstr2);
880 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
881 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
882 FPDFPage_InsertObject(page, text_object2);
883 }
884
Nicolas Pena207b7272017-05-26 17:37:06 -0400885 // Check that the text renders properly.
Nicolas Penaf45ade32017-05-03 10:23:49 -0400886 FPDF_BITMAP page_bitmap = RenderPage(page);
887 const char md5[] = "2bc6c1aaa2252e73246a75775ccf38c2";
888 CompareBitmap(page_bitmap, 612, 792, md5);
889 FPDFBitmap_Destroy(page_bitmap);
890
891 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -0400892 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -0400893 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
894 FPDF_ClosePage(page);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400895 TestAndCloseSaved(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -0400896}
897#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400898
899TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400900 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400901 {
902 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
903 FPDF_PAGE page = LoadPage(0);
904 ASSERT_NE(nullptr, page);
905
906 // Now add a more complex blue path.
907 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
908 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
909 // TODO(npm): stroking will cause the MD5s to differ.
910 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
911 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
912 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
913 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
914 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
915 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
916 EXPECT_TRUE(FPDFPath_Close(green_path));
917 FPDFPage_InsertObject(page, green_path);
918 FPDF_BITMAP page_bitmap = RenderPage(page);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400919 CompareBitmap(page_bitmap, 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400920 FPDFBitmap_Destroy(page_bitmap);
921
922 // Now save the result, closing the page and document
923 EXPECT_TRUE(FPDFPage_GenerateContent(page));
924 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
925 UnloadPage(page);
926 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400927 TestAndCloseSaved(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400928}
Jane Liu28fb7ba2017-08-02 21:45:57 -0400929
930TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
931 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
932 FPDF_PAGE page = LoadPage(0);
933 ASSERT_TRUE(page);
934 ASSERT_EQ(39, FPDFPage_CountObject(page));
935
936 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
937 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
938 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
939
940 obj = FPDFPage_GetObject(page, 33);
941 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
942 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
943 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
944 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
945 FPDFBitmap_Destroy(bitmap);
946
947 obj = FPDFPage_GetObject(page, 34);
948 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
949 bitmap = FPDFImageObj_GetBitmap(obj);
950 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
951 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
952 FPDFBitmap_Destroy(bitmap);
953
954 obj = FPDFPage_GetObject(page, 35);
955 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
956 bitmap = FPDFImageObj_GetBitmap(obj);
957 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
958 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
959 FPDFBitmap_Destroy(bitmap);
960
961 obj = FPDFPage_GetObject(page, 36);
962 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
963 bitmap = FPDFImageObj_GetBitmap(obj);
964 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
965 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
966 FPDFBitmap_Destroy(bitmap);
967
968 obj = FPDFPage_GetObject(page, 37);
969 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
970 bitmap = FPDFImageObj_GetBitmap(obj);
971 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
972 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
973 FPDFBitmap_Destroy(bitmap);
974
975 obj = FPDFPage_GetObject(page, 38);
976 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
977 bitmap = FPDFImageObj_GetBitmap(obj);
978 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
979 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
980 FPDFBitmap_Destroy(bitmap);
981 UnloadPage(page);
982}
Jane Liu548334e2017-08-03 16:33:40 -0400983
984TEST_F(FPDFEditEmbeddertest, GetImageData) {
985 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
986 FPDF_PAGE page = LoadPage(0);
987 ASSERT_TRUE(page);
988 ASSERT_EQ(39, FPDFPage_CountObject(page));
989
990 // Retrieve an image object with flate-encoded data stream.
991 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
992 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
993
994 // Check that the raw image data has the correct length and hash value.
995 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
996 std::vector<char> buf(len);
997 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
998 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
999 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1000
1001 // Check that the decoded image data has the correct length and hash value.
1002 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1003 buf.clear();
1004 buf.resize(len);
1005 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1006 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
1007 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1008
1009 // Retrieve an image obejct with DCTDecode-encoded data stream.
1010 obj = FPDFPage_GetObject(page, 37);
1011 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1012
1013 // Check that the raw image data has the correct length and hash value.
1014 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1015 buf.clear();
1016 buf.resize(len);
1017 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1018 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1019 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1020
1021 // Check that the decoded image data has the correct length and hash value,
1022 // which should be the same as those of the raw data, since this image is
1023 // encoded by a single DCTDecode filter and decoding is a noop.
1024 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1025 buf.clear();
1026 buf.resize(len);
1027 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1028 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1029 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1030
1031 UnloadPage(page);
1032}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04001033
1034TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
1035 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1036 ASSERT_TRUE(rect);
1037
1038 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
1039 FPDFPageObj_Destroy(rect);
1040}
Jane Liube63ab92017-08-09 14:09:34 -04001041
1042TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
1043 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1044 FPDF_PAGE page = LoadPage(0);
1045 ASSERT_TRUE(page);
1046
1047 // Verify that retrieving the filter of a non-image object would fail.
1048 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1049 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1050 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
1051 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
1052
1053 // Verify the returned filter string for an image object with a single filter.
1054 obj = FPDFPage_GetObject(page, 33);
1055 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1056 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
1057 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1058 std::vector<char> buf(len);
1059 EXPECT_EQ(24u, FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1060 EXPECT_STREQ(L"FlateDecode",
1061 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
1062 .c_str());
1063 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
1064
1065 // Verify all the filters for an image object with a list of filters.
1066 obj = FPDFPage_GetObject(page, 38);
1067 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1068 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
1069 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1070 buf.clear();
1071 buf.resize(len);
1072 EXPECT_EQ(30u, FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1073 EXPECT_STREQ(L"ASCIIHexDecode",
1074 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
1075 .c_str());
1076
1077 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
1078 buf.clear();
1079 buf.resize(len);
1080 EXPECT_EQ(20u, FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
1081 EXPECT_STREQ(L"DCTDecode",
1082 GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
1083 .c_str());
1084
1085 UnloadPage(page);
1086}