blob: 44d5aaf4386cc95ad6b963730866f5d44c234b11 [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"));
Ryan Harrison275e2602017-09-18 14:23:18 -040067 ByteString present("FontFile");
68 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050069 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
Miklos Vajna12abfd02017-09-15 07:49:03 +0200259 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
260 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200261 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200262 // Verify actual coordinates.
263 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
264 float x;
265 float y;
266 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
267 EXPECT_EQ(100, x);
268 EXPECT_EQ(100, y);
269 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
270 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
271 segment = FPDFPath_GetPathSegment(green_rect, 1);
272 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
273 EXPECT_EQ(100, x);
274 EXPECT_EQ(140, y);
275 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
276 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
277 segment = FPDFPath_GetPathSegment(green_rect, 2);
278 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
279 EXPECT_EQ(140, x);
280 EXPECT_EQ(140, y);
281 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
282 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
283 segment = FPDFPath_GetPathSegment(green_rect, 3);
284 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
285 EXPECT_EQ(140, x);
286 EXPECT_EQ(100, y);
287 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
288 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
289 segment = FPDFPath_GetPathSegment(green_rect, 4);
290 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
291 EXPECT_EQ(100, x);
292 EXPECT_EQ(100, y);
293 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
294 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200295
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500296 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
297 FPDFPage_InsertObject(page, green_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500298 page_bitmap = RenderPage(page);
299 CompareBitmap(page_bitmap, 612, 792, "7b0b87604594e773add528fae567a558");
300 FPDFBitmap_Destroy(page_bitmap);
301
302 // Add a black triangle.
303 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
304 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
305 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
306 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
307 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
308 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200309
310 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
311 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200312 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200313 // Verify actual coordinates.
314 segment = FPDFPath_GetPathSegment(black_path, 0);
315 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
316 EXPECT_EQ(400, x);
317 EXPECT_EQ(100, y);
318 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
319 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
320 segment = FPDFPath_GetPathSegment(black_path, 1);
321 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
322 EXPECT_EQ(400, x);
323 EXPECT_EQ(200, y);
324 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
325 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
326 segment = FPDFPath_GetPathSegment(black_path, 2);
327 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
328 EXPECT_EQ(300, x);
329 EXPECT_EQ(100, y);
330 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
331 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
332 // Make sure out of bounds index access fails properly.
333 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200334
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500335 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500336 page_bitmap = RenderPage(page);
337 CompareBitmap(page_bitmap, 612, 792, "eadc8020a14dfcf091da2688733d8806");
338 FPDFBitmap_Destroy(page_bitmap);
339
340 // Now add a more complex blue path.
341 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
342 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
343 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
344 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
345 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
346 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
347 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
348 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
349 EXPECT_TRUE(FPDFPath_Close(blue_path));
350 FPDFPage_InsertObject(page, blue_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500351 page_bitmap = RenderPage(page);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400352 const char last_md5[] = "9823e1a21bd9b72b6a442ba4f12af946";
353 CompareBitmap(page_bitmap, 612, 792, last_md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500354 FPDFBitmap_Destroy(page_bitmap);
355
356 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400357 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500358 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500359 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500360
361 // Render the saved result
Nicolas Pena3ff54002017-07-05 11:55:35 -0400362 TestAndCloseSaved(612, 792, last_md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500363}
364
Miklos Vajna12abfd02017-09-15 07:49:03 +0200365TEST_F(FPDFEditEmbeddertest, PathsPoints) {
366 CreateNewDocument();
367 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
368 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +0200369 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200370
371 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +0200372 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200373
Miklos Vajna36eed872017-09-20 22:52:43 +0200374 // FPDFPath_GetPathSegment() with a non-path.
375 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
376 // FPDFPath_GetPathSegment() with a NULL path.
377 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
378 float x;
379 float y;
380 // FPDFPathSegment_GetPoint() with a NULL segment.
381 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
382
383 // FPDFPathSegment_GetType() with a NULL segment.
384 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
385
386 // FPDFPathSegment_GetClose() with a NULL segment.
387 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
388
Miklos Vajna12abfd02017-09-15 07:49:03 +0200389 FPDFPageObj_Destroy(img);
390}
391
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500392TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
393 // Load document with some text
394 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
395 FPDF_PAGE page = LoadPage(0);
396 EXPECT_NE(nullptr, page);
397
398 // Add an opaque rectangle on top of some of the text.
399 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
400 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
401 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
402 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500403
404 // Add a transparent triangle on top of other part of the text.
405 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
406 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
407 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
408 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
409 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
410 EXPECT_TRUE(FPDFPath_Close(black_path));
411 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500412
413 // Render and check the result. Text is slightly different on Mac.
414 FPDF_BITMAP bitmap = RenderPage(page);
415#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700416 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500417#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400418 const char md5[] = "bc6e6eb50dda4695ba0fb4d04ed82ada";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400419#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700420 CompareBitmap(bitmap, 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500421 FPDFBitmap_Destroy(bitmap);
422 UnloadPage(page);
423}
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500424
wileyryae858aa42017-05-31 14:49:05 -0500425TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
426 // Load document with existing content
427 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
428 FPDF_PAGE page = LoadPage(0);
429 EXPECT_NE(nullptr, page);
430
431 // Add a transparent rectangle on top of the existing content
432 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
433 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
434 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
435 FPDFPage_InsertObject(page, red_rect2);
436
437 // Add an opaque rectangle on top of the existing content
438 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
439 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
440 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
441 FPDFPage_InsertObject(page, red_rect);
442
443 FPDF_BITMAP bitmap = RenderPage(page);
444 CompareBitmap(bitmap, 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
445 FPDFBitmap_Destroy(bitmap);
446 EXPECT_TRUE(FPDFPage_GenerateContent(page));
447
448 // Now save the result, closing the page and document
449 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400450 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -0500451
Nicolas Pena3ff54002017-07-05 11:55:35 -0400452 // Render the saved result without closing the page and document
453 TestSaved(612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500454
455 ClearString();
456 // Add another opaque rectangle on top of the existing content
457 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
458 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
459 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400460 FPDFPage_InsertObject(m_SavedPage, green_rect);
wileyryae858aa42017-05-31 14:49:05 -0500461
462 // Add another transparent rectangle on top of existing content
463 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
464 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
465 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400466 FPDFPage_InsertObject(m_SavedPage, green_rect2);
467 FPDF_BITMAP new_bitmap = RenderPageWithFlags(m_SavedPage, m_SavedForm, 0);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400468 const char last_md5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
469 CompareBitmap(new_bitmap, 612, 792, last_md5);
wileyryae858aa42017-05-31 14:49:05 -0500470 FPDFBitmap_Destroy(new_bitmap);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400471 EXPECT_TRUE(FPDFPage_GenerateContent(m_SavedPage));
wileyryae858aa42017-05-31 14:49:05 -0500472
473 // Now save the result, closing the page and document
Nicolas Pena3ff54002017-07-05 11:55:35 -0400474 EXPECT_TRUE(FPDF_SaveAsCopy(m_SavedDocument, this, 0));
475 CloseSaved();
wileyryae858aa42017-05-31 14:49:05 -0500476
477 // Render the saved result
Nicolas Pena3ff54002017-07-05 11:55:35 -0400478 TestAndCloseSaved(612, 792, last_md5);
wileyryae858aa42017-05-31 14:49:05 -0500479}
480
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500481TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
482 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500483 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500484
485 // Add a large stroked rectangle (fill color should not affect it).
486 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
487 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
488 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
489 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
490 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
491 FPDFPage_InsertObject(page, rect);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500492 FPDF_BITMAP page_bitmap = RenderPage(page);
493 CompareBitmap(page_bitmap, 612, 792, "64bd31f862a89e0a9e505a5af6efd506");
494 FPDFBitmap_Destroy(page_bitmap);
495
496 // Add crossed-checkmark
497 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
498 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
499 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
500 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
501 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
502 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
503 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
504 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
505 FPDFPage_InsertObject(page, check);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500506 page_bitmap = RenderPage(page);
507 CompareBitmap(page_bitmap, 612, 792, "4b6f3b9d25c4e194821217d5016c3724");
508 FPDFBitmap_Destroy(page_bitmap);
509
510 // Add stroked and filled oval-ish path.
511 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
512 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
513 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
514 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
515 EXPECT_TRUE(FPDFPath_Close(path));
516 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
517 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
518 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
519 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
520 FPDFPage_InsertObject(page, path);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500521 page_bitmap = RenderPage(page);
522 CompareBitmap(page_bitmap, 612, 792, "ff3e6a22326754944cc6e56609acd73b");
523 FPDFBitmap_Destroy(page_bitmap);
524 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500525}
Nicolas Pena49058402017-02-14 18:26:20 -0500526
527TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
528 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500529 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -0500530
531 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400532 FPDF_PAGEOBJECT text_object1 =
533 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
534 EXPECT_TRUE(text_object1);
535 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
536 GetFPDFWideString(L"I'm at the bottom of the page");
537 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
538 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
539 FPDFPage_InsertObject(page, text_object1);
Nicolas Pena49058402017-02-14 18:26:20 -0500540 FPDF_BITMAP page_bitmap = RenderPage(page);
541#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700542 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -0500543#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400544 const char md5[] = "7a35771853a1cbba38f6775807878625";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400545#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700546 CompareBitmap(page_bitmap, 612, 792, md5);
Nicolas Pena49058402017-02-14 18:26:20 -0500547 FPDFBitmap_Destroy(page_bitmap);
548
549 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -0400550 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500551 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400552 EXPECT_TRUE(text_object2);
553 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
554 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
555 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
556 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
557 FPDFPage_InsertObject(page, text_object2);
Nicolas Pena49058402017-02-14 18:26:20 -0500558 page_bitmap = RenderPage(page);
559#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700560 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Lei Zhang3de50052017-03-29 21:02:13 -0700561#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400562 const char md5_2[] = "b231b329a4b566fb9b42bfc15fe59bb7";
Nicolas Pena49058402017-02-14 18:26:20 -0500563#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400564 const char md5_2[] = "f85fae151851436072b7b3c6703e506a";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400565#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700566 CompareBitmap(page_bitmap, 612, 792, md5_2);
Nicolas Pena49058402017-02-14 18:26:20 -0500567 FPDFBitmap_Destroy(page_bitmap);
568
569 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -0400570 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500571 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400572 EXPECT_TRUE(text_object3);
573 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
574 GetFPDFWideString(L"Can you read me? <:)>");
575 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
576 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
577 FPDFPage_InsertObject(page, text_object3);
Nicolas Pena49058402017-02-14 18:26:20 -0500578 page_bitmap = RenderPage(page);
579#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700580 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Lei Zhang3de50052017-03-29 21:02:13 -0700581#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400582 const char md5_3[] = "ba874b3b137f984510c4e287ed4ba7ae";
Nicolas Pena49058402017-02-14 18:26:20 -0500583#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400584 const char md5_3[] = "c5aed6a8ef05558c8c47d58c87cbcb46";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400585#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700586 CompareBitmap(page_bitmap, 612, 792, md5_3);
Nicolas Pena49058402017-02-14 18:26:20 -0500587 FPDFBitmap_Destroy(page_bitmap);
588
589 // TODO(npm): Why are there issues with text rotated by 90 degrees?
590 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
591 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -0500592}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500593
Nicolas Pena603a31d2017-06-14 11:41:18 -0400594TEST_F(FPDFEditEmbeddertest, GraphicsData) {
595 // New page
596 std::unique_ptr<void, FPDFPageDeleter> page(
597 FPDFPage_New(CreateNewDocument(), 0, 612, 792));
598
599 // Create a rect with nontrivial graphics
600 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
601 FPDFPageObj_SetBlendMode(rect1, "Color");
602 FPDFPage_InsertObject(page.get(), rect1);
603 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
604
605 // Check that the ExtGState was created
606 CPDF_Page* the_page = CPDFPageFromFPDFPage(page.get());
607 CPDF_Dictionary* graphics_dict =
608 the_page->m_pResources->GetDictFor("ExtGState");
609 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400610 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400611
612 // Add a text object causing no change to the graphics dictionary
613 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
614 // Only alpha, the last component, matters for the graphics dictionary. And
615 // the default value is 255.
616 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
617 FPDFPage_InsertObject(page.get(), text1);
618 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400619 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400620
621 // Add a text object increasing the size of the graphics dictionary
622 FPDF_PAGEOBJECT text2 =
623 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
624 FPDFPage_InsertObject(page.get(), text2);
625 FPDFPageObj_SetBlendMode(text2, "Darken");
626 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
627 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400628 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400629
630 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -0400631 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -0400632 FPDFPageObj_SetBlendMode(path, "Darken");
633 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
634 FPDFPage_InsertObject(page.get(), path);
635 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400636 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400637
638 // Add a rect increasing the size of the graphics dictionary
639 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
640 FPDFPageObj_SetBlendMode(rect2, "Darken");
641 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
642 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
643 FPDFPage_InsertObject(page.get(), rect2);
644 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400645 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400646}
647
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500648TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
649 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500650 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500651
652 // Add a red rectangle with some non-default alpha
653 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
654 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
655 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
656 FPDFPage_InsertObject(page, rect);
657 EXPECT_TRUE(FPDFPage_GenerateContent(page));
658
659 // Check the ExtGState
660 CPDF_Page* the_page = CPDFPageFromFPDFPage(page);
661 CPDF_Dictionary* graphics_dict =
662 the_page->m_pResources->GetDictFor("ExtGState");
663 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400664 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500665
666 // Check the bitmap
667 FPDF_BITMAP page_bitmap = RenderPage(page);
668 CompareBitmap(page_bitmap, 612, 792, "5384da3406d62360ffb5cac4476fff1c");
669 FPDFBitmap_Destroy(page_bitmap);
670
671 // Never mind, my new favorite color is blue, increase alpha
672 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
673 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400674 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500675
676 // Check that bitmap displays changed content
677 page_bitmap = RenderPage(page);
678 CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c");
679 FPDFBitmap_Destroy(page_bitmap);
680
681 // And now generate, without changes
682 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400683 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500684 page_bitmap = RenderPage(page);
685 CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c");
686 FPDFBitmap_Destroy(page_bitmap);
687
688 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400689 FPDF_PAGEOBJECT text_object =
690 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
691 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
692 GetFPDFWideString(L"Something something #text# something");
693 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
694 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
695 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500696 EXPECT_TRUE(FPDFPage_GenerateContent(page));
697 CPDF_Dictionary* font_dict = the_page->m_pResources->GetDictFor("Font");
698 ASSERT_TRUE(font_dict);
699 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
700
701 // Generate yet again, check dicts are reasonably sized
702 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400703 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500704 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
705 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500706}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500707
Nicolas Penad03ca422017-03-06 13:54:33 -0500708TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
709 CreateNewDocument();
710 // TODO(npm): use other fonts after disallowing loading any font as any type
711 const CPDF_Font* stock_font =
712 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700713 const uint8_t* data = stock_font->GetFont()->GetFontData();
714 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400715 std::unique_ptr<void, FPDFFontDeleter> font(
716 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
717 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400718 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500719 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500720
Nicolas Penad03ca422017-03-06 13:54:33 -0500721 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -0500722 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
723 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
724 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
725 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
726 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
727 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -0500728 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
729
Nicolas Penabe90aae2017-02-27 10:41:41 -0500730 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -0500731 ASSERT_TRUE(widths_array);
732 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500733 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -0500734 EXPECT_EQ(569, widths_array->GetNumberAt(11));
735 EXPECT_EQ(500, widths_array->GetNumberAt(223));
736 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
737}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500738
Nicolas Penad03ca422017-03-06 13:54:33 -0500739TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
740 CreateNewDocument();
741 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700742 const uint8_t* data = stock_font->GetFont()->GetFontData();
743 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400744 std::unique_ptr<void, FPDFFontDeleter> font(
745 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
746 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400747 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500748 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500749
Nicolas Penad03ca422017-03-06 13:54:33 -0500750 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
751 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
752 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
753 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
754 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
755 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
756 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
757 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -0500758
Nicolas Penad03ca422017-03-06 13:54:33 -0500759 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
760 ASSERT_TRUE(widths_array);
761 ASSERT_EQ(224U, widths_array->GetCount());
762 EXPECT_EQ(600, widths_array->GetNumberAt(33));
763 EXPECT_EQ(600, widths_array->GetNumberAt(74));
764 EXPECT_EQ(600, widths_array->GetNumberAt(223));
765 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
766}
767
768TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
769 CreateNewDocument();
770 const CPDF_Font* stock_font =
771 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700772 const uint8_t* data = stock_font->GetFont()->GetFontData();
773 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400774 std::unique_ptr<void, FPDFFontDeleter> font(
775 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
776 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400777 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500778 EXPECT_TRUE(typed_font->IsCIDFont());
779
780 // Check font dictionary entries
781 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
782 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
783 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
784 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
785 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
786 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
787 ASSERT_TRUE(descendant_array);
788 EXPECT_EQ(1U, descendant_array->GetCount());
789
790 // Check the CIDFontDict
791 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
792 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
793 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
794 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
795 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
796 ASSERT_TRUE(cidinfo_dict);
797 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
798 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
799 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
800 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
801
802 // Check widths
803 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
804 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -0400805 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -0500806 CheckCompositeFontWidths(widths_array, typed_font);
807}
808
809TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
810 CreateNewDocument();
811 const CPDF_Font* stock_font =
812 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700813 const uint8_t* data = stock_font->GetFont()->GetFontData();
814 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -0500815
Nicolas Penab3161852017-05-02 14:12:50 -0400816 std::unique_ptr<void, FPDFFontDeleter> font(
817 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
818 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400819 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500820 EXPECT_TRUE(typed_font->IsCIDFont());
821
822 // Check font dictionary entries
823 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
824 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
825 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
826 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
827 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
828 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
829 ASSERT_TRUE(descendant_array);
830 EXPECT_EQ(1U, descendant_array->GetCount());
831
832 // Check the CIDFontDict
833 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
834 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
835 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
836 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
837 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
838 ASSERT_TRUE(cidinfo_dict);
839 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
840 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
841 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
842 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
843 data);
844
845 // Check widths
846 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
847 ASSERT_TRUE(widths_array);
848 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -0500849}
rbpotterce8e51e2017-04-28 12:42:47 -0700850
851TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
852 // Load document with a -90 degree rotation
853 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
854 FPDF_PAGE page = LoadPage(0);
855 EXPECT_NE(nullptr, page);
856
857 EXPECT_EQ(3, FPDFPage_GetRotation(page));
858 UnloadPage(page);
859}
Nicolas Penab3161852017-05-02 14:12:50 -0400860
861TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
862 // Start with a blank page
863 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
864 {
865 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700866 const uint8_t* data = stock_font->GetFont()->GetFontData();
867 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400868 std::unique_ptr<void, FPDFFontDeleter> font(
869 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
870 ASSERT_TRUE(font.get());
871
872 // Add some text to the page
873 FPDF_PAGEOBJECT text_object =
874 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
875 EXPECT_TRUE(text_object);
876 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
877 GetFPDFWideString(L"I am testing my loaded font, WEE.");
878 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
879 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
880 FPDFPage_InsertObject(page, text_object);
Nicolas Penab3161852017-05-02 14:12:50 -0400881 FPDF_BITMAP page_bitmap = RenderPage(page);
882#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
883 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
884#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400885 const char md5[] = "1722c6a9deed953d730de9cd13dcbd55";
Nicolas Penab3161852017-05-02 14:12:50 -0400886#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
887 CompareBitmap(page_bitmap, 612, 792, md5);
888 FPDFBitmap_Destroy(page_bitmap);
889
890 // Add some more text, same font
891 FPDF_PAGEOBJECT text_object2 =
892 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
893 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
894 GetFPDFWideString(L"Bigger font size");
895 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
896 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
897 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -0400898 }
899 FPDF_BITMAP page_bitmap2 = RenderPage(page);
900#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
901 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
902#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400903 const char md5_2[] = "9d7885072058f6c3e68ecaf32e917f30";
Nicolas Penab3161852017-05-02 14:12:50 -0400904#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
905 CompareBitmap(page_bitmap2, 612, 792, md5_2);
906 FPDFBitmap_Destroy(page_bitmap2);
907
Nicolas Pena207b7272017-05-26 17:37:06 -0400908 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -0400909 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
910 FPDF_ClosePage(page);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400911 TestAndCloseSaved(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -0400912}
Nicolas Penaf45ade32017-05-03 10:23:49 -0400913
Jane Liueda65252017-06-07 11:31:27 -0400914TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
915 // Open a file with one annotation and load its first page.
916 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
917 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
918 ASSERT_TRUE(page);
919
920 // Add an underline annotation to the page without specifying its rectangle.
Jane Liud60e9ad2017-06-26 11:28:36 -0400921 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE);
922 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -0400923
924 // FPDFPage_TransformAnnots() should run without errors when modifying
925 // annotation rectangles.
926 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
927
Jane Liud60e9ad2017-06-26 11:28:36 -0400928 FPDFPage_CloseAnnot(annot);
Jane Liueda65252017-06-07 11:31:27 -0400929 UnloadPage(page);
930}
931
Nicolas Penaf45ade32017-05-03 10:23:49 -0400932// TODO(npm): Add tests using Japanese fonts in other OS.
933#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_
934TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
935 // Start with a blank page
936 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
937 CFX_Font CIDfont;
938 {
939 // First, get the data from the font
940 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
941 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
942 const uint8_t* data = CIDfont.GetFontData();
943 const uint32_t size = CIDfont.GetSize();
944
945 // Load the data into a FPDF_Font.
946 std::unique_ptr<void, FPDFFontDeleter> font(
947 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
948 ASSERT_TRUE(font.get());
949
950 // Add some text to the page
951 FPDF_PAGEOBJECT text_object =
952 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
953 ASSERT_TRUE(text_object);
954 std::wstring wstr = L"ABCDEFGhijklmnop.";
955 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
956 GetFPDFWideString(wstr);
957 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
958 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
959 FPDFPage_InsertObject(page, text_object);
960
961 // And add some Japanese characters
962 FPDF_PAGEOBJECT text_object2 =
963 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
964 ASSERT_TRUE(text_object2);
965 std::wstring wstr2 =
966 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
967 L"\u756A";
968 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
969 GetFPDFWideString(wstr2);
970 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
971 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
972 FPDFPage_InsertObject(page, text_object2);
973 }
974
Nicolas Pena207b7272017-05-26 17:37:06 -0400975 // Check that the text renders properly.
Nicolas Penaf45ade32017-05-03 10:23:49 -0400976 FPDF_BITMAP page_bitmap = RenderPage(page);
977 const char md5[] = "2bc6c1aaa2252e73246a75775ccf38c2";
978 CompareBitmap(page_bitmap, 612, 792, md5);
979 FPDFBitmap_Destroy(page_bitmap);
980
981 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -0400982 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -0400983 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
984 FPDF_ClosePage(page);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400985 TestAndCloseSaved(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -0400986}
987#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400988
989TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400990 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400991 {
992 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
993 FPDF_PAGE page = LoadPage(0);
994 ASSERT_NE(nullptr, page);
995
996 // Now add a more complex blue path.
997 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
998 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
999 // TODO(npm): stroking will cause the MD5s to differ.
1000 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
1001 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
1002 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
1003 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
1004 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
1005 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
1006 EXPECT_TRUE(FPDFPath_Close(green_path));
1007 FPDFPage_InsertObject(page, green_path);
1008 FPDF_BITMAP page_bitmap = RenderPage(page);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04001009 CompareBitmap(page_bitmap, 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001010 FPDFBitmap_Destroy(page_bitmap);
1011
1012 // Now save the result, closing the page and document
1013 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1014 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1015 UnloadPage(page);
1016 }
Nicolas Pena3ff54002017-07-05 11:55:35 -04001017 TestAndCloseSaved(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001018}
Jane Liu28fb7ba2017-08-02 21:45:57 -04001019
1020TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
1021 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1022 FPDF_PAGE page = LoadPage(0);
1023 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001024 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04001025
1026 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1027 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1028 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
1029
1030 obj = FPDFPage_GetObject(page, 33);
1031 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1032 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
1033 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1034 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
1035 FPDFBitmap_Destroy(bitmap);
1036
1037 obj = FPDFPage_GetObject(page, 34);
1038 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1039 bitmap = FPDFImageObj_GetBitmap(obj);
1040 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1041 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
1042 FPDFBitmap_Destroy(bitmap);
1043
1044 obj = FPDFPage_GetObject(page, 35);
1045 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1046 bitmap = FPDFImageObj_GetBitmap(obj);
1047 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
1048 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
1049 FPDFBitmap_Destroy(bitmap);
1050
1051 obj = FPDFPage_GetObject(page, 36);
1052 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1053 bitmap = FPDFImageObj_GetBitmap(obj);
1054 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1055 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
1056 FPDFBitmap_Destroy(bitmap);
1057
1058 obj = FPDFPage_GetObject(page, 37);
1059 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1060 bitmap = FPDFImageObj_GetBitmap(obj);
1061 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1062 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
1063 FPDFBitmap_Destroy(bitmap);
1064
1065 obj = FPDFPage_GetObject(page, 38);
1066 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1067 bitmap = FPDFImageObj_GetBitmap(obj);
1068 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1069 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
1070 FPDFBitmap_Destroy(bitmap);
1071 UnloadPage(page);
1072}
Jane Liu548334e2017-08-03 16:33:40 -04001073
1074TEST_F(FPDFEditEmbeddertest, GetImageData) {
1075 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1076 FPDF_PAGE page = LoadPage(0);
1077 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001078 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04001079
1080 // Retrieve an image object with flate-encoded data stream.
1081 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
1082 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1083
1084 // Check that the raw image data has the correct length and hash value.
1085 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1086 std::vector<char> buf(len);
1087 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1088 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
1089 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1090
1091 // Check that the decoded image data has the correct length and hash value.
1092 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1093 buf.clear();
1094 buf.resize(len);
1095 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1096 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
1097 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1098
1099 // Retrieve an image obejct with DCTDecode-encoded data stream.
1100 obj = FPDFPage_GetObject(page, 37);
1101 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1102
1103 // Check that the raw image data has the correct length and hash value.
1104 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1105 buf.clear();
1106 buf.resize(len);
1107 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1108 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1109 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1110
1111 // Check that the decoded image data has the correct length and hash value,
1112 // which should be the same as those of the raw data, since this image is
1113 // encoded by a single DCTDecode filter and decoding is a noop.
1114 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1115 buf.clear();
1116 buf.resize(len);
1117 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1118 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1119 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1120
1121 UnloadPage(page);
1122}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04001123
1124TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
1125 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1126 ASSERT_TRUE(rect);
1127
1128 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
1129 FPDFPageObj_Destroy(rect);
1130}
Jane Liube63ab92017-08-09 14:09:34 -04001131
1132TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
1133 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1134 FPDF_PAGE page = LoadPage(0);
1135 ASSERT_TRUE(page);
1136
1137 // Verify that retrieving the filter of a non-image object would fail.
1138 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1139 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1140 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
1141 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
1142
1143 // Verify the returned filter string for an image object with a single filter.
1144 obj = FPDFPage_GetObject(page, 33);
1145 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1146 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
1147 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1148 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001149 static constexpr char kFlateDecode[] = "FlateDecode";
1150 EXPECT_EQ(sizeof(kFlateDecode),
1151 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1152 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001153 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
1154
1155 // Verify all the filters for an image object with a list of filters.
1156 obj = FPDFPage_GetObject(page, 38);
1157 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1158 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
1159 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1160 buf.clear();
1161 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001162 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
1163 EXPECT_EQ(sizeof(kASCIIHexDecode),
1164 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1165 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001166
1167 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
1168 buf.clear();
1169 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001170 static constexpr char kDCTDecode[] = "DCTDecode";
1171 EXPECT_EQ(sizeof(kDCTDecode),
1172 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
1173 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001174
1175 UnloadPage(page);
1176}
Jane Liuca898292017-08-16 11:25:35 -04001177
1178TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
1179 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1180 FPDF_PAGE page = LoadPage(0);
1181 ASSERT_TRUE(page);
1182
1183 // Check that getting the metadata of a null object would fail.
1184 FPDF_IMAGEOBJ_METADATA metadata;
1185 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
1186
1187 // Check that receiving the metadata with a null metadata object would fail.
1188 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
1189 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
1190
1191 // Check that when retrieving an image object's metadata without passing in
1192 // |page|, all values are correct, with the last two being default values.
1193 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1194 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
1195 EXPECT_EQ(92u, metadata.width);
1196 EXPECT_EQ(68u, metadata.height);
1197 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1198 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1199 EXPECT_EQ(0u, metadata.bits_per_pixel);
1200 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
1201
1202 // Verify the metadata of a bitmap image with indexed colorspace.
1203 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
1204 EXPECT_EQ(92u, metadata.width);
1205 EXPECT_EQ(68u, metadata.height);
1206 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1207 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1208 EXPECT_EQ(1u, metadata.bits_per_pixel);
1209 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
1210
1211 // Verify the metadata of an image with RGB colorspace.
1212 obj = FPDFPage_GetObject(page, 37);
1213 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1214 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
1215 EXPECT_EQ(126u, metadata.width);
1216 EXPECT_EQ(106u, metadata.height);
1217 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
1218 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
1219 EXPECT_EQ(24u, metadata.bits_per_pixel);
1220 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
1221
1222 UnloadPage(page);
1223}