blob: 7ca21744400f2df2edbe0f77a08c03f28b447cd3 [file] [log] [blame]
Tom Sepezd483eb42016-01-06 10:03:59 -08001// Copyright 2016 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05005#include <memory>
6#include <string>
Nicolas Penad03ca422017-03-06 13:54:33 -05007#include <utility>
Jane Liu548334e2017-08-03 16:33:40 -04008#include <vector>
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009
Nicolas Penabe90aae2017-02-27 10:41:41 -050010#include "core/fpdfapi/font/cpdf_font.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050011#include "core/fpdfapi/page/cpdf_page.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050012#include "core/fpdfapi/parser/cpdf_array.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050013#include "core/fpdfapi/parser/cpdf_dictionary.h"
Nicolas Penad03ca422017-03-06 13:54:33 -050014#include "core/fpdfapi/parser/cpdf_number.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050015#include "core/fpdfapi/parser/cpdf_stream.h"
Nicolas Pena0fc185e2017-02-08 12:13:20 -050016#include "core/fxcrt/fx_system.h"
Dan Sinclair00d47a62018-03-28 18:39:04 +000017#include "fpdfsdk/cpdfsdk_helpers.h"
Tom Sepeze08d2b12018-04-25 18:49:32 +000018#include "public/cpp/fpdf_scopers.h"
Jane Liueda65252017-06-07 11:31:27 -040019#include "public/fpdf_annot.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080020#include "public/fpdf_edit.h"
21#include "public/fpdfview.h"
22#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080023#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080024#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080025#include "testing/test_support.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080026
Nicolas Pena3ff54002017-07-05 11:55:35 -040027class FPDFEditEmbeddertest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050028 protected:
29 FPDF_DOCUMENT CreateNewDocument() {
30 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070031 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050032 return document_;
33 }
34
Lei Zhang710fa992018-05-25 16:24:48 +000035 void CheckFontDescriptor(const CPDF_Dictionary* font_dict,
Nicolas Penad03ca422017-03-06 13:54:33 -050036 int font_type,
37 bool bold,
38 bool italic,
39 uint32_t size,
40 const uint8_t* data) {
Lei Zhangb1ec2802018-05-25 21:55:24 +000041 const CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
Nicolas Penad03ca422017-03-06 13:54:33 -050042 ASSERT_TRUE(font_desc);
43 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
44 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
45 font_desc->GetStringFor("FontName"));
46
47 // Check that the font descriptor has the required keys according to spec
48 // 1.7 Table 5.19
49 ASSERT_TRUE(font_desc->KeyExist("Flags"));
Dan Sinclair10e1f052017-09-28 15:59:42 -040050
Nicolas Penad03ca422017-03-06 13:54:33 -050051 int font_flags = font_desc->GetIntegerFor("Flags");
Dan Sinclair10e1f052017-09-28 15:59:42 -040052 EXPECT_EQ(bold, FontStyleIsBold(font_flags));
53 EXPECT_EQ(italic, FontStyleIsItalic(font_flags));
54 EXPECT_TRUE(FontStyleIsNonSymbolic(font_flags));
Nicolas Penad03ca422017-03-06 13:54:33 -050055 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
Nicolás Peña5f95f362017-09-28 13:00:45 +090056
Lei Zhangb1ec2802018-05-25 21:55:24 +000057 const CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox");
Nicolás Peña5f95f362017-09-28 13:00:45 +090058 ASSERT_TRUE(fontBBox);
59 EXPECT_EQ(4U, fontBBox->GetCount());
60 // Check that the coordinates are in the preferred order according to spec
61 // 1.7 Section 3.8.4
62 EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2));
63 EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3));
64
Nicolas Penad03ca422017-03-06 13:54:33 -050065 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
66 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
67 EXPECT_TRUE(font_desc->KeyExist("Descent"));
68 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
69 EXPECT_TRUE(font_desc->KeyExist("StemV"));
Ryan Harrison275e2602017-09-18 14:23:18 -040070 ByteString present("FontFile");
71 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050072 if (font_type == FPDF_FONT_TRUETYPE)
73 std::swap(present, absent);
74 EXPECT_TRUE(font_desc->KeyExist(present));
75 EXPECT_FALSE(font_desc->KeyExist(absent));
76
77 // Check that the font stream is the one that was provided
Lei Zhangb1ec2802018-05-25 21:55:24 +000078 const CPDF_Stream* font_stream = font_desc->GetStreamFor(present);
Nicolas Penad03ca422017-03-06 13:54:33 -050079 ASSERT_EQ(size, font_stream->GetRawSize());
Nicolás Peña79eab232017-09-28 13:29:05 +090080 if (font_type == FPDF_FONT_TRUETYPE) {
81 ASSERT_EQ(static_cast<int>(size),
82 font_stream->GetDict()->GetIntegerFor("Length1"));
83 }
Nicolas Penad03ca422017-03-06 13:54:33 -050084 uint8_t* stream_data = font_stream->GetRawData();
85 for (size_t j = 0; j < size; j++)
86 EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j;
87 }
88
Lei Zhangde579ab2018-05-25 21:49:49 +000089 void CheckCompositeFontWidths(const CPDF_Array* widths_array,
Nicolas Penad03ca422017-03-06 13:54:33 -050090 CPDF_Font* typed_font) {
91 // Check that W array is in a format that conforms to PDF spec 1.7 section
92 // "Glyph Metrics in CIDFonts" (these checks are not
93 // implementation-specific).
94 EXPECT_GT(widths_array->GetCount(), 1U);
95 int num_cids_checked = 0;
96 int cur_cid = 0;
97 for (size_t idx = 0; idx < widths_array->GetCount(); idx++) {
98 int cid = widths_array->GetNumberAt(idx);
99 EXPECT_GE(cid, cur_cid);
100 ASSERT_FALSE(++idx == widths_array->GetCount());
Lei Zhangde579ab2018-05-25 21:49:49 +0000101 const CPDF_Object* next = widths_array->GetObjectAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500102 if (next->IsArray()) {
103 // We are in the c [w1 w2 ...] case
Lei Zhangde579ab2018-05-25 21:49:49 +0000104 const CPDF_Array* arr = next->AsArray();
Nicolas Penad03ca422017-03-06 13:54:33 -0500105 int cnt = static_cast<int>(arr->GetCount());
106 size_t inner_idx = 0;
107 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
Nicolas Pena23346602018-01-30 21:42:41 +0000108 uint32_t width = arr->GetNumberAt(inner_idx++);
Dan Sinclair971a6742018-03-28 19:23:25 +0000109 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
110 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500111 }
112 num_cids_checked += cnt;
113 continue;
114 }
115 // Otherwise, are in the c_first c_last w case.
116 ASSERT_TRUE(next->IsNumber());
117 int last_cid = next->AsNumber()->GetInteger();
118 ASSERT_FALSE(++idx == widths_array->GetCount());
Nicolas Pena23346602018-01-30 21:42:41 +0000119 uint32_t width = widths_array->GetNumberAt(idx);
Nicolas Penad03ca422017-03-06 13:54:33 -0500120 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
Dan Sinclair971a6742018-03-28 19:23:25 +0000121 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid))
122 << " at cid " << cur_cid;
Nicolas Penad03ca422017-03-06 13:54:33 -0500123 }
124 num_cids_checked += last_cid - cid + 1;
125 }
126 // Make sure we have a good amount of cids described
127 EXPECT_GT(num_cids_checked, 900);
128 }
129 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
130
131 private:
132 CPDF_Document* cpdf_doc_;
133};
Tom Sepezd483eb42016-01-06 10:03:59 -0800134
etienneb7712c262016-04-26 08:13:45 -0700135namespace {
thestigdc7ec032016-11-21 15:32:52 -0800136
etienneb7712c262016-04-26 08:13:45 -0700137const char kExpectedPDF[] =
138 "%PDF-1.7\r\n"
139 "%\xA1\xB3\xC5\xD7\r\n"
140 "1 0 obj\r\n"
141 "<</Pages 2 0 R /Type/Catalog>>\r\n"
142 "endobj\r\n"
143 "2 0 obj\r\n"
144 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
145 "endobj\r\n"
146 "3 0 obj\r\n"
147 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
148 "endobj\r\n"
149 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400150 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
151 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400152 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700153 ">>\r\n"
154 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400155 "5 0 obj\r\n"
156 "<</BM/Normal/CA 1/ca 1>>\r\n"
157 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700158 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400159 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700160 "0000000000 65535 f\r\n"
161 "0000000017 00000 n\r\n"
162 "0000000066 00000 n\r\n"
163 "0000000122 00000 n\r\n"
164 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400165 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700166 "trailer\r\n"
167 "<<\r\n"
168 "/Root 1 0 R\r\n"
169 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400170 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700171 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400172 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700173 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800174
etienneb7712c262016-04-26 08:13:45 -0700175} // namespace
176
Tom Sepezd483eb42016-01-06 10:03:59 -0800177TEST_F(FPDFEditEmbeddertest, EmptyCreation) {
178 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700179 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800180 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400181 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800182 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800183 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700184
Nicolas Penad9d6c292017-06-06 16:12:10 -0400185 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
186 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700187 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800188}
thestigdc7ec032016-11-21 15:32:52 -0800189
190// Regression test for https://crbug.com/667012
191TEST_F(FPDFEditEmbeddertest, RasterizePDF) {
192 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
193
194 // Get the bitmap for the original document/
Tom Sepeze08d2b12018-04-25 18:49:32 +0000195 ScopedFPDFBitmap orig_bitmap;
thestigdc7ec032016-11-21 15:32:52 -0800196 {
197 EXPECT_TRUE(OpenDocument("black.pdf"));
198 FPDF_PAGE orig_page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000199 ASSERT_TRUE(orig_page);
200 orig_bitmap = RenderLoadedPage(orig_page);
201 CompareBitmap(orig_bitmap.get(), 612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800202 UnloadPage(orig_page);
203 }
204
205 // Create a new document from |orig_bitmap| and save it.
206 {
207 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
208 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
209
210 // Add the bitmap to an image object and add the image object to the output
211 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700212 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000213 EXPECT_TRUE(
214 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap.get()));
thestigdc7ec032016-11-21 15:32:52 -0800215 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
216 FPDFPage_InsertObject(temp_page, temp_img);
217 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
218 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
219 FPDF_ClosePage(temp_page);
220 FPDF_CloseDocument(temp_doc);
221 }
thestigdc7ec032016-11-21 15:32:52 -0800222
223 // Get the generated content. Make sure it is at least as big as the original
224 // PDF.
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400225 EXPECT_GT(GetString().size(), 923U);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400226 VerifySavedDocument(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800227}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500228
229TEST_F(FPDFEditEmbeddertest, AddPaths) {
230 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500231 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000232 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500233
234 // We will first add a red rectangle
235 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000236 ASSERT_TRUE(red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500237 // Expect false when trying to set colors out of range
238 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
239 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
240
241 // Fill rectangle with red and insert to the page
242 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
243 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
Miklos Vajna491112b2018-05-30 13:30:10 +0000244
245 int fillmode = FPDF_FILLMODE_NONE;
246 FPDF_BOOL stroke = true;
247 EXPECT_TRUE(FPDFPath_GetDrawMode(red_rect, &fillmode, &stroke));
248 EXPECT_EQ(FPDF_FILLMODE_ALTERNATE, fillmode);
249 EXPECT_FALSE(stroke);
250
Miklos Vajna97f4d672018-06-04 14:47:17 +0000251 double matrix_a = 1;
252 double matrix_b = 2;
253 double matrix_c = 3;
254 double matrix_d = 4;
255 double matrix_e = 5;
256 double matrix_f = 6;
257 EXPECT_FALSE(FPDFPath_SetMatrix(nullptr, matrix_a, matrix_b, matrix_c,
258 matrix_d, matrix_e, matrix_f));
259 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
260 matrix_d, matrix_e, matrix_f));
261 EXPECT_FALSE(FPDFPath_GetMatrix(nullptr, &matrix_a, &matrix_b, &matrix_c,
262 &matrix_d, &matrix_e, &matrix_f));
263 EXPECT_TRUE(FPDFPath_GetMatrix(red_rect, &matrix_a, &matrix_b, &matrix_c,
264 &matrix_d, &matrix_e, &matrix_f));
265 EXPECT_EQ(1, static_cast<int>(matrix_a));
266 EXPECT_EQ(2, static_cast<int>(matrix_b));
267 EXPECT_EQ(3, static_cast<int>(matrix_c));
268 EXPECT_EQ(4, static_cast<int>(matrix_d));
269 EXPECT_EQ(5, static_cast<int>(matrix_e));
270 EXPECT_EQ(6, static_cast<int>(matrix_f));
271 // Set back the default
272 matrix_a = 1;
273 matrix_b = 0;
274 matrix_c = 0;
275 matrix_d = 1;
276 matrix_e = 0;
277 matrix_f = 0;
278 EXPECT_TRUE(FPDFPath_SetMatrix(red_rect, matrix_a, matrix_b, matrix_c,
279 matrix_d, matrix_e, matrix_f));
280
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500281 FPDFPage_InsertObject(page, red_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000282 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000283 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000284 CompareBitmap(page_bitmap.get(), 612, 792,
285 "66d02eaa6181e2c069ce2ea99beda497");
286 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500287
288 // Now add to that a green rectangle with some medium alpha
289 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
290 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200291
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200292 // Make sure the type of the rectangle is a path.
293 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
294
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200295 // Make sure we get back the same color we set previously.
296 unsigned int R;
297 unsigned int G;
298 unsigned int B;
299 unsigned int A;
300 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
301 EXPECT_EQ(0U, R);
302 EXPECT_EQ(255U, G);
303 EXPECT_EQ(0U, B);
304 EXPECT_EQ(128U, A);
305
Miklos Vajna12abfd02017-09-15 07:49:03 +0200306 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
307 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200308 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200309 // Verify actual coordinates.
310 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
311 float x;
312 float y;
313 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
314 EXPECT_EQ(100, x);
315 EXPECT_EQ(100, y);
316 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
317 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
318 segment = FPDFPath_GetPathSegment(green_rect, 1);
319 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
320 EXPECT_EQ(100, x);
321 EXPECT_EQ(140, y);
322 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
323 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
324 segment = FPDFPath_GetPathSegment(green_rect, 2);
325 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
326 EXPECT_EQ(140, x);
327 EXPECT_EQ(140, y);
328 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
329 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
330 segment = FPDFPath_GetPathSegment(green_rect, 3);
331 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
332 EXPECT_EQ(140, x);
333 EXPECT_EQ(100, y);
334 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
335 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
336 segment = FPDFPath_GetPathSegment(green_rect, 4);
337 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
338 EXPECT_EQ(100, x);
339 EXPECT_EQ(100, y);
340 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
341 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200342
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500343 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
344 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000345 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000346 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000347 CompareBitmap(page_bitmap.get(), 612, 792,
348 "7b0b87604594e773add528fae567a558");
349 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500350
351 // Add a black triangle.
352 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
353 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
354 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
355 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
356 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
357 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200358
359 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
360 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200361 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200362 // Verify actual coordinates.
363 segment = FPDFPath_GetPathSegment(black_path, 0);
364 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
365 EXPECT_EQ(400, x);
366 EXPECT_EQ(100, y);
367 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
368 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
369 segment = FPDFPath_GetPathSegment(black_path, 1);
370 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
371 EXPECT_EQ(400, x);
372 EXPECT_EQ(200, y);
373 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
374 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
375 segment = FPDFPath_GetPathSegment(black_path, 2);
376 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
377 EXPECT_EQ(300, x);
378 EXPECT_EQ(100, y);
379 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
380 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
381 // Make sure out of bounds index access fails properly.
382 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200383
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500384 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000385 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000386 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000387 CompareBitmap(page_bitmap.get(), 612, 792,
388 "eadc8020a14dfcf091da2688733d8806");
389 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500390
391 // Now add a more complex blue path.
392 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
393 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
394 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
395 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
396 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
397 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
398 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
399 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
400 EXPECT_TRUE(FPDFPath_Close(blue_path));
401 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000402 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
403 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000404 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000405 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
406 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500407
408 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400409 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500410 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500411 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500412
413 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000414 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500415}
416
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000417TEST_F(FPDFEditEmbeddertest, RemovePageObject) {
418 // Load document with some text.
419 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
420 FPDF_PAGE page = LoadPage(0);
421 ASSERT_TRUE(page);
422
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000423 // Show what the original file looks like.
424 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000425#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000426 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000427#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000428 const char kOriginalMD5[] = "e5a6fa28298db07484cd922f3e210c88";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000429#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000430 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000431#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000432 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000433 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
434 }
435
436 // Get the "Hello, world!" text object and remove it.
437 ASSERT_EQ(2, FPDFPage_CountObjects(page));
438 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
439 ASSERT_TRUE(page_object);
440 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
441
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000442 // Verify the "Hello, world!" text is gone.
443 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000444#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000445 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000446#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000447 const char kRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000448#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000449 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000450#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000451 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000452 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
453 }
454 ASSERT_EQ(1, FPDFPage_CountObjects(page));
455
456 UnloadPage(page);
457 FPDFPageObj_Destroy(page_object);
458}
459
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000460TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) {
461 // Load document with some text.
462 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
463 FPDF_PAGE page = LoadPage(0);
464 ASSERT_TRUE(page);
465
466 // Show what the original file looks like.
467 {
468#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
469 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
470#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
471 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
472#else
473 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
474#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000475 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000476 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
477 }
478
479 // Iterate over all objects, counting the number of times each content mark
480 // name appears.
481 int object_count = FPDFPage_CountObjects(page);
482 ASSERT_EQ(19, object_count);
483
484 unsigned long prime_count = 0;
485 unsigned long square_count = 0;
486 unsigned long greater_than_ten_count = 0;
487 std::vector<FPDF_PAGEOBJECT> primes;
488 for (int i = 0; i < object_count; ++i) {
489 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
490
491 int mark_count = FPDFPageObj_CountMarks(page_object);
492 for (int j = 0; j < mark_count; ++j) {
493 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
494
495 char buffer[256];
496 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
497 std::wstring name =
498 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
499 if (name == L"Prime") {
500 prime_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000501 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000502 primes.push_back(page_object);
503 } else if (name == L"Square") {
504 square_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000505 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
Henrique Nakashima132c38e2018-04-23 16:35:56 +0000506 ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u);
507 std::wstring key =
508 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
509 EXPECT_EQ(L"Factor", key);
510 EXPECT_EQ(FPDF_OBJECT_NUMBER,
511 FPDFPageObjMark_GetParamValueType(mark, 0));
512 int square_root = FPDFPageObjMark_GetParamIntValue(mark, 0);
513 EXPECT_EQ(i + 1, square_root * square_root);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000514 } else if (name == L"GreaterThanTen") {
515 greater_than_ten_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000516 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimab557bdc2018-04-23 18:04:26 +0000517 } else if (name == L"Bounds") {
518 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
519 ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u);
520 std::wstring key =
521 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
522 EXPECT_EQ(L"Position", key);
523 EXPECT_EQ(FPDF_OBJECT_STRING,
524 FPDFPageObjMark_GetParamValueType(mark, 0));
525 ASSERT_GT(FPDFPageObjMark_GetParamStringValue(mark, 0, buffer, 256),
526 0u);
527 std::wstring value =
528 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
529 EXPECT_EQ(L"Last", value);
530 EXPECT_EQ(18, i);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000531 } else {
532 FAIL();
533 }
534 }
535 }
536
537 // Expect certain number of tagged objects. The test file contains strings
538 // from 1 to 19.
539 EXPECT_EQ(8u, prime_count);
540 EXPECT_EQ(4u, square_count);
541 EXPECT_EQ(9u, greater_than_ten_count);
542
543 // Remove all objects marked with "Prime".
544 for (FPDF_PAGEOBJECT page_object : primes) {
545 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
546 FPDFPageObj_Destroy(page_object);
547 }
548
549 EXPECT_EQ(11, FPDFPage_CountObjects(page));
550
551 {
552#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
553 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
554#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
555 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
556#else
557 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
558#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000559 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000560 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
561 }
562
563 UnloadPage(page);
564}
565
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000566// Fails due to pdfium:1051.
567TEST_F(FPDFEditEmbeddertest, DISABLED_RemoveExistingPageObject) {
568 // Load document with some text.
569 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
570 FPDF_PAGE page = LoadPage(0);
571 ASSERT_TRUE(page);
572
573 // Get the "Hello, world!" text object and remove it.
574 ASSERT_EQ(2, FPDFPage_CountObjects(page));
575 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
576 ASSERT_TRUE(page_object);
577 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
578
579 // Verify the "Hello, world!" text is gone.
580 ASSERT_EQ(1, FPDFPage_CountObjects(page));
581
582 // Save the file
583 EXPECT_TRUE(FPDFPage_GenerateContent(page));
584 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
585 UnloadPage(page);
586 FPDFPageObj_Destroy(page_object);
587
588 // Re-open the file and check the page object count is still 1.
589 OpenSavedDocument();
590 FPDF_PAGE saved_page = LoadSavedPage(0);
591 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
592 CloseSavedPage(saved_page);
593 CloseSavedDocument();
594}
595
596TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) {
597 // Load document with some text.
598 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
599 FPDF_PAGE page = LoadPage(0);
600 ASSERT_TRUE(page);
601
602 // Add a red rectangle.
603 ASSERT_EQ(2, FPDFPage_CountObjects(page));
604 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
605 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
606 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
607 FPDFPage_InsertObject(page, red_rect);
608
609 // Verify the red rectangle was added.
610 ASSERT_EQ(3, FPDFPage_CountObjects(page));
611
612 // Save the file
613 EXPECT_TRUE(FPDFPage_GenerateContent(page));
614 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
615 UnloadPage(page);
616
617 // Re-open the file and check the page object count is still 3.
618 OpenSavedDocument();
619 FPDF_PAGE saved_page = LoadSavedPage(0);
620 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
621 CloseSavedPage(saved_page);
622 CloseSavedDocument();
623}
624
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000625TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) {
626 // Start with a blank page.
627 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
628 ASSERT_TRUE(page);
629
630 // Render the blank page and verify it's a blank bitmap.
631 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
632 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000633 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000634 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
635 }
636 ASSERT_EQ(0, FPDFPage_CountObjects(page));
637
638 // Add a red rectangle.
639 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
640 ASSERT_TRUE(red_rect);
641 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
642 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
643 FPDFPage_InsertObject(page, red_rect);
644 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
645 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000646 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000647 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
648 }
649 EXPECT_EQ(1, FPDFPage_CountObjects(page));
650
651 // Remove rectangle and verify it does not render anymore and the bitmap is
652 // back to a blank one.
653 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
654 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000655 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000656 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
657 }
658 EXPECT_EQ(0, FPDFPage_CountObjects(page));
659
660 // Trying to remove an object not in the page should return false.
661 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
662
663 FPDF_ClosePage(page);
664 FPDFPageObj_Destroy(red_rect);
665}
666
Miklos Vajna12abfd02017-09-15 07:49:03 +0200667TEST_F(FPDFEditEmbeddertest, PathsPoints) {
668 CreateNewDocument();
669 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
670 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +0200671 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200672
673 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +0200674 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200675
Miklos Vajna36eed872017-09-20 22:52:43 +0200676 // FPDFPath_GetPathSegment() with a non-path.
677 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
678 // FPDFPath_GetPathSegment() with a NULL path.
679 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
680 float x;
681 float y;
682 // FPDFPathSegment_GetPoint() with a NULL segment.
683 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
684
685 // FPDFPathSegment_GetType() with a NULL segment.
686 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
687
688 // FPDFPathSegment_GetClose() with a NULL segment.
689 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
690
Miklos Vajna12abfd02017-09-15 07:49:03 +0200691 FPDFPageObj_Destroy(img);
692}
693
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500694TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
695 // Load document with some text
696 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
697 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000698 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500699
700 // Add an opaque rectangle on top of some of the text.
701 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
702 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
703 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
704 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500705
706 // Add a transparent triangle on top of other part of the text.
707 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
708 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
709 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
710 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
711 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
712 EXPECT_TRUE(FPDFPath_Close(black_path));
713 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500714
715 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000716 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400717#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700718 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500719#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000720 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400721#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000722 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500723 UnloadPage(page);
724}
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500725
wileyryae858aa42017-05-31 14:49:05 -0500726TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
727 // Load document with existing content
728 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
729 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000730 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -0500731
732 // Add a transparent rectangle on top of the existing content
733 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
734 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
735 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
736 FPDFPage_InsertObject(page, red_rect2);
737
738 // Add an opaque rectangle on top of the existing content
739 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
740 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
741 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
742 FPDFPage_InsertObject(page, red_rect);
743
Tom Sepeze08d2b12018-04-25 18:49:32 +0000744 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000745 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500746 EXPECT_TRUE(FPDFPage_GenerateContent(page));
747
748 // Now save the result, closing the page and document
749 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400750 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -0500751
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400752 OpenSavedDocument();
Lei Zhang107fa7b2018-02-09 21:48:15 +0000753 FPDF_PAGE saved_page = LoadSavedPage(0);
754 VerifySavedRendering(saved_page, 612, 792,
755 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500756
757 ClearString();
758 // Add another opaque rectangle on top of the existing content
759 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
760 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
761 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000762 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -0500763
764 // Add another transparent rectangle on top of existing content
765 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
766 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
767 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000768 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000769 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
770 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000771 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000772 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
773 }
Lei Zhang107fa7b2018-02-09 21:48:15 +0000774 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -0500775
776 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +0000777 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400778
Lei Zhang107fa7b2018-02-09 21:48:15 +0000779 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400780 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -0500781
782 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +0000783 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -0500784}
785
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500786TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
787 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500788 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500789
790 // Add a large stroked rectangle (fill color should not affect it).
791 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
792 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
793 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
794 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
Miklos Vajna366df7f2018-05-22 14:27:29 +0000795
796 float width = 0;
797 EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width));
798 EXPECT_EQ(15.0f, width);
799
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500800 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
801 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000802 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000803 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000804 CompareBitmap(page_bitmap.get(), 612, 792,
805 "64bd31f862a89e0a9e505a5af6efd506");
806 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500807
808 // Add crossed-checkmark
809 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
810 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
811 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
812 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
813 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
814 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
815 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
816 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
817 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000818 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000819 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000820 CompareBitmap(page_bitmap.get(), 612, 792,
821 "4b6f3b9d25c4e194821217d5016c3724");
822 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500823
824 // Add stroked and filled oval-ish path.
825 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
826 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
827 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
828 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
829 EXPECT_TRUE(FPDFPath_Close(path));
830 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
831 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
832 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
833 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
834 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000835 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000836 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000837 CompareBitmap(page_bitmap.get(), 612, 792,
838 "ff3e6a22326754944cc6e56609acd73b");
839 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500840 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500841}
Nicolas Pena49058402017-02-14 18:26:20 -0500842
843TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
844 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500845 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -0500846
847 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400848 FPDF_PAGEOBJECT text_object1 =
849 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
850 EXPECT_TRUE(text_object1);
851 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
852 GetFPDFWideString(L"I'm at the bottom of the page");
853 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
854 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
855 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000856 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000857 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400858#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000859 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -0500860#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000861 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400862#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000863 CompareBitmap(page_bitmap.get(), 612, 792, md5);
864 }
Nicolas Pena49058402017-02-14 18:26:20 -0500865
866 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -0400867 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500868 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400869 EXPECT_TRUE(text_object2);
870 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
871 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
872 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
873 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
874 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000875 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000876 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400877#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000878 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -0400879#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000880 const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d";
Nicolas Pena49058402017-02-14 18:26:20 -0500881#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000882 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400883#endif
Dan Sinclair971a6742018-03-28 19:23:25 +0000884 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000885 }
Nicolas Pena49058402017-02-14 18:26:20 -0500886
887 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -0400888 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500889 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400890 EXPECT_TRUE(text_object3);
891 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
892 GetFPDFWideString(L"Can you read me? <:)>");
893 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
894 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
895 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000896 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000897 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400898#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000899 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -0400900#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000901 const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2";
Nicolas Pena49058402017-02-14 18:26:20 -0500902#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000903 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400904#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000905 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
906 }
Nicolas Pena49058402017-02-14 18:26:20 -0500907
908 // TODO(npm): Why are there issues with text rotated by 90 degrees?
909 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
910 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -0500911}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500912
Nicolas Pena603a31d2017-06-14 11:41:18 -0400913TEST_F(FPDFEditEmbeddertest, GraphicsData) {
914 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +0000915 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400916
917 // Create a rect with nontrivial graphics
918 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
919 FPDFPageObj_SetBlendMode(rect1, "Color");
920 FPDFPage_InsertObject(page.get(), rect1);
921 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
922
923 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +0000924 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
925 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -0400926 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400927 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400928
929 // Add a text object causing no change to the graphics dictionary
930 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
931 // Only alpha, the last component, matters for the graphics dictionary. And
932 // the default value is 255.
933 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
934 FPDFPage_InsertObject(page.get(), text1);
935 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400936 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400937
938 // Add a text object increasing the size of the graphics dictionary
939 FPDF_PAGEOBJECT text2 =
940 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
941 FPDFPage_InsertObject(page.get(), text2);
942 FPDFPageObj_SetBlendMode(text2, "Darken");
943 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
944 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400945 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400946
947 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -0400948 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -0400949 FPDFPageObj_SetBlendMode(path, "Darken");
950 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
951 FPDFPage_InsertObject(page.get(), path);
952 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400953 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400954
955 // Add a rect increasing the size of the graphics dictionary
956 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
957 FPDFPageObj_SetBlendMode(rect2, "Darken");
958 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
959 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
960 FPDFPage_InsertObject(page.get(), rect2);
961 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400962 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400963}
964
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500965TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
966 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500967 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500968
969 // Add a red rectangle with some non-default alpha
970 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
971 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
972 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
973 FPDFPage_InsertObject(page, rect);
974 EXPECT_TRUE(FPDFPage_GenerateContent(page));
975
976 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +0000977 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
978 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500979 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400980 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500981
982 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +0000983 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000984 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000985 CompareBitmap(page_bitmap.get(), 612, 792,
986 "5384da3406d62360ffb5cac4476fff1c");
987 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500988
989 // Never mind, my new favorite color is blue, increase alpha
990 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
991 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400992 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500993
994 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +0000995 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000996 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000997 CompareBitmap(page_bitmap.get(), 612, 792,
998 "2e51656f5073b0bee611d9cd086aa09c");
999 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001000
1001 // And now generate, without changes
1002 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001003 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001004 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001005 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001006 CompareBitmap(page_bitmap.get(), 612, 792,
1007 "2e51656f5073b0bee611d9cd086aa09c");
1008 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001009
1010 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -04001011 FPDF_PAGEOBJECT text_object =
1012 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
1013 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1014 GetFPDFWideString(L"Something something #text# something");
1015 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1016 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
1017 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001018 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +00001019 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001020 ASSERT_TRUE(font_dict);
1021 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1022
1023 // Generate yet again, check dicts are reasonably sized
1024 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001025 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001026 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
1027 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -05001028}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001029
Nicolas Penad03ca422017-03-06 13:54:33 -05001030TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
1031 CreateNewDocument();
1032 // TODO(npm): use other fonts after disallowing loading any font as any type
1033 const CPDF_Font* stock_font =
1034 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001035 const uint8_t* data = stock_font->GetFont()->GetFontData();
1036 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001037 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001038 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
1039 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001040 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001041 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001042
Lei Zhang710fa992018-05-25 16:24:48 +00001043 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05001044 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1045 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
1046 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
1047 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1048 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1049 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05001050 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
1051
Lei Zhangde579ab2018-05-25 21:49:49 +00001052 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001053 ASSERT_TRUE(widths_array);
1054 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001055 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05001056 EXPECT_EQ(569, widths_array->GetNumberAt(11));
1057 EXPECT_EQ(500, widths_array->GetNumberAt(223));
1058 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
1059}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001060
Nicolas Penad03ca422017-03-06 13:54:33 -05001061TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
1062 CreateNewDocument();
1063 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001064 const uint8_t* data = stock_font->GetFont()->GetFontData();
1065 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001066 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001067 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
1068 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001069 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001070 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001071
Lei Zhang710fa992018-05-25 16:24:48 +00001072 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001073 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1074 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
1075 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
1076 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1077 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1078 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
1079 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05001080
Lei Zhangde579ab2018-05-25 21:49:49 +00001081 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001082 ASSERT_TRUE(widths_array);
1083 ASSERT_EQ(224U, widths_array->GetCount());
1084 EXPECT_EQ(600, widths_array->GetNumberAt(33));
1085 EXPECT_EQ(600, widths_array->GetNumberAt(74));
1086 EXPECT_EQ(600, widths_array->GetNumberAt(223));
1087 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
1088}
1089
1090TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
1091 CreateNewDocument();
1092 const CPDF_Font* stock_font =
1093 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001094 const uint8_t* data = stock_font->GetFont()->GetFontData();
1095 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001096 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001097 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
1098 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001099 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001100 EXPECT_TRUE(typed_font->IsCIDFont());
1101
1102 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001103 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001104 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1105 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1106 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
1107 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001108 const CPDF_Array* descendant_array =
1109 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001110 ASSERT_TRUE(descendant_array);
1111 EXPECT_EQ(1U, descendant_array->GetCount());
1112
1113 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001114 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001115 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1116 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
1117 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001118 const CPDF_Dictionary* cidinfo_dict =
1119 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001120 ASSERT_TRUE(cidinfo_dict);
1121 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1122 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1123 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1124 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
1125
1126 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001127 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001128 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001129 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -05001130 CheckCompositeFontWidths(widths_array, typed_font);
1131}
1132
1133TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
1134 CreateNewDocument();
1135 const CPDF_Font* stock_font =
1136 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001137 const uint8_t* data = stock_font->GetFont()->GetFontData();
1138 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -05001139
Tom Sepeze08d2b12018-04-25 18:49:32 +00001140 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001141 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1142 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001143 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001144 EXPECT_TRUE(typed_font->IsCIDFont());
1145
1146 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001147 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001148 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1149 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1150 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
1151 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001152 const CPDF_Array* descendant_array =
1153 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001154 ASSERT_TRUE(descendant_array);
1155 EXPECT_EQ(1U, descendant_array->GetCount());
1156
1157 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001158 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001159 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1160 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
1161 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001162 const CPDF_Dictionary* cidinfo_dict =
1163 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001164 ASSERT_TRUE(cidinfo_dict);
1165 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1166 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1167 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1168 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
1169 data);
1170
1171 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001172 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001173 ASSERT_TRUE(widths_array);
1174 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05001175}
rbpotterce8e51e2017-04-28 12:42:47 -07001176
1177TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
1178 // Load document with a -90 degree rotation
1179 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
1180 FPDF_PAGE page = LoadPage(0);
1181 EXPECT_NE(nullptr, page);
1182
1183 EXPECT_EQ(3, FPDFPage_GetRotation(page));
1184 UnloadPage(page);
1185}
Nicolas Penab3161852017-05-02 14:12:50 -04001186
1187TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
1188 // Start with a blank page
1189 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1190 {
1191 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001192 const uint8_t* data = stock_font->GetFont()->GetFontData();
1193 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001194 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001195 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
1196 ASSERT_TRUE(font.get());
1197
1198 // Add some text to the page
1199 FPDF_PAGEOBJECT text_object =
1200 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1201 EXPECT_TRUE(text_object);
1202 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1203 GetFPDFWideString(L"I am testing my loaded font, WEE.");
1204 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1205 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
1206 FPDFPage_InsertObject(page, text_object);
Tom Sepeze08d2b12018-04-25 18:49:32 +00001207 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001208#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001209 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
1210#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001211 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04001212#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001213 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04001214
1215 // Add some more text, same font
1216 FPDF_PAGEOBJECT text_object2 =
1217 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
1218 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1219 GetFPDFWideString(L"Bigger font size");
1220 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1221 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
1222 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04001223 }
Tom Sepeze08d2b12018-04-25 18:49:32 +00001224 ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001225#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001226 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
1227#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001228 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04001229#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001230 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001231
Nicolas Pena207b7272017-05-26 17:37:06 -04001232 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04001233 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1234 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001235
1236 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001237}
Nicolas Penaf45ade32017-05-03 10:23:49 -04001238
Jane Liueda65252017-06-07 11:31:27 -04001239TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
1240 // Open a file with one annotation and load its first page.
1241 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001242 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04001243 ASSERT_TRUE(page);
1244
Lei Zhanga21d5932018-02-05 18:28:38 +00001245 {
1246 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001247 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001248 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
1249 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04001250
Lei Zhanga21d5932018-02-05 18:28:38 +00001251 // FPDFPage_TransformAnnots() should run without errors when modifying
1252 // annotation rectangles.
1253 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
1254 }
Jane Liueda65252017-06-07 11:31:27 -04001255 UnloadPage(page);
1256}
1257
Nicolas Penaf45ade32017-05-03 10:23:49 -04001258// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04001259#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -04001260TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
1261 // Start with a blank page
1262 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1263 CFX_Font CIDfont;
1264 {
1265 // First, get the data from the font
1266 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
1267 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
1268 const uint8_t* data = CIDfont.GetFontData();
1269 const uint32_t size = CIDfont.GetSize();
1270
1271 // Load the data into a FPDF_Font.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001272 ScopedFPDFFont font(
Nicolas Penaf45ade32017-05-03 10:23:49 -04001273 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1274 ASSERT_TRUE(font.get());
1275
1276 // Add some text to the page
1277 FPDF_PAGEOBJECT text_object =
1278 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1279 ASSERT_TRUE(text_object);
1280 std::wstring wstr = L"ABCDEFGhijklmnop.";
1281 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1282 GetFPDFWideString(wstr);
1283 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1284 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
1285 FPDFPage_InsertObject(page, text_object);
1286
1287 // And add some Japanese characters
1288 FPDF_PAGEOBJECT text_object2 =
1289 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
1290 ASSERT_TRUE(text_object2);
1291 std::wstring wstr2 =
1292 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
1293 L"\u756A";
1294 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1295 GetFPDFWideString(wstr2);
1296 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1297 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
1298 FPDFPage_InsertObject(page, text_object2);
1299 }
1300
Nicolas Pena207b7272017-05-26 17:37:06 -04001301 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00001302 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00001303 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001304 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001305 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1306 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04001307
1308 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04001309 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04001310 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1311 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001312
1313 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001314}
Dan Sinclair698aed72017-09-26 16:24:49 -04001315#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001316
1317TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04001318 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001319 {
1320 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
1321 FPDF_PAGE page = LoadPage(0);
1322 ASSERT_NE(nullptr, page);
1323
1324 // Now add a more complex blue path.
1325 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
1326 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
1327 // TODO(npm): stroking will cause the MD5s to differ.
1328 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
1329 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
1330 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
1331 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
1332 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
1333 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
1334 EXPECT_TRUE(FPDFPath_Close(green_path));
1335 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00001336 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001337 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001338
1339 // Now save the result, closing the page and document
1340 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1341 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1342 UnloadPage(page);
1343 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001344
1345 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001346}
Jane Liu28fb7ba2017-08-02 21:45:57 -04001347
1348TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
1349 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1350 FPDF_PAGE page = LoadPage(0);
1351 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001352 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04001353
1354 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1355 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1356 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
1357
1358 obj = FPDFPage_GetObject(page, 33);
1359 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1360 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
1361 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1362 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
1363 FPDFBitmap_Destroy(bitmap);
1364
1365 obj = FPDFPage_GetObject(page, 34);
1366 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1367 bitmap = FPDFImageObj_GetBitmap(obj);
1368 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1369 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
1370 FPDFBitmap_Destroy(bitmap);
1371
1372 obj = FPDFPage_GetObject(page, 35);
1373 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1374 bitmap = FPDFImageObj_GetBitmap(obj);
1375 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
1376 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
1377 FPDFBitmap_Destroy(bitmap);
1378
1379 obj = FPDFPage_GetObject(page, 36);
1380 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1381 bitmap = FPDFImageObj_GetBitmap(obj);
1382 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1383 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
1384 FPDFBitmap_Destroy(bitmap);
1385
1386 obj = FPDFPage_GetObject(page, 37);
1387 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1388 bitmap = FPDFImageObj_GetBitmap(obj);
1389 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1390 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
1391 FPDFBitmap_Destroy(bitmap);
1392
1393 obj = FPDFPage_GetObject(page, 38);
1394 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1395 bitmap = FPDFImageObj_GetBitmap(obj);
1396 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1397 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
1398 FPDFBitmap_Destroy(bitmap);
1399 UnloadPage(page);
1400}
Jane Liu548334e2017-08-03 16:33:40 -04001401
Lei Zhang53341dd2018-03-01 15:42:47 +00001402TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) {
1403 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
1404 FPDF_PAGE page = LoadPage(0);
1405 ASSERT_TRUE(page);
1406 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1407
1408 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
1409 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1410 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001411 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00001412 ASSERT_TRUE(bitmap);
1413 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
1414 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00001415 }
1416
1417 UnloadPage(page);
1418}
1419
Jane Liu548334e2017-08-03 16:33:40 -04001420TEST_F(FPDFEditEmbeddertest, GetImageData) {
1421 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1422 FPDF_PAGE page = LoadPage(0);
1423 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001424 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04001425
1426 // Retrieve an image object with flate-encoded data stream.
1427 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
1428 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1429
1430 // Check that the raw image data has the correct length and hash value.
1431 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1432 std::vector<char> buf(len);
1433 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1434 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
1435 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1436
1437 // Check that the decoded image data has the correct length and hash value.
1438 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1439 buf.clear();
1440 buf.resize(len);
1441 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1442 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
1443 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1444
1445 // Retrieve an image obejct with DCTDecode-encoded data stream.
1446 obj = FPDFPage_GetObject(page, 37);
1447 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1448
1449 // Check that the raw image data has the correct length and hash value.
1450 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1451 buf.clear();
1452 buf.resize(len);
1453 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1454 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1455 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1456
1457 // Check that the decoded image data has the correct length and hash value,
1458 // which should be the same as those of the raw data, since this image is
1459 // encoded by a single DCTDecode filter and decoding is a noop.
1460 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1461 buf.clear();
1462 buf.resize(len);
1463 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1464 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1465 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1466
1467 UnloadPage(page);
1468}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04001469
1470TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
1471 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1472 ASSERT_TRUE(rect);
1473
1474 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
1475 FPDFPageObj_Destroy(rect);
1476}
Jane Liube63ab92017-08-09 14:09:34 -04001477
1478TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
1479 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1480 FPDF_PAGE page = LoadPage(0);
1481 ASSERT_TRUE(page);
1482
1483 // Verify that retrieving the filter of a non-image object would fail.
1484 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1485 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1486 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
1487 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
1488
1489 // Verify the returned filter string for an image object with a single filter.
1490 obj = FPDFPage_GetObject(page, 33);
1491 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1492 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
1493 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1494 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001495 static constexpr char kFlateDecode[] = "FlateDecode";
1496 EXPECT_EQ(sizeof(kFlateDecode),
1497 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1498 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001499 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
1500
1501 // Verify all the filters for an image object with a list of filters.
1502 obj = FPDFPage_GetObject(page, 38);
1503 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1504 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
1505 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1506 buf.clear();
1507 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001508 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
1509 EXPECT_EQ(sizeof(kASCIIHexDecode),
1510 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1511 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001512
1513 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
1514 buf.clear();
1515 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001516 static constexpr char kDCTDecode[] = "DCTDecode";
1517 EXPECT_EQ(sizeof(kDCTDecode),
1518 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
1519 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001520
1521 UnloadPage(page);
1522}
Jane Liuca898292017-08-16 11:25:35 -04001523
1524TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
1525 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1526 FPDF_PAGE page = LoadPage(0);
1527 ASSERT_TRUE(page);
1528
1529 // Check that getting the metadata of a null object would fail.
1530 FPDF_IMAGEOBJ_METADATA metadata;
1531 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
1532
1533 // Check that receiving the metadata with a null metadata object would fail.
1534 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
1535 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
1536
1537 // Check that when retrieving an image object's metadata without passing in
1538 // |page|, all values are correct, with the last two being default values.
1539 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1540 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001541 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001542 EXPECT_EQ(92u, metadata.width);
1543 EXPECT_EQ(68u, metadata.height);
1544 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1545 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1546 EXPECT_EQ(0u, metadata.bits_per_pixel);
1547 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
1548
1549 // Verify the metadata of a bitmap image with indexed colorspace.
1550 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001551 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001552 EXPECT_EQ(92u, metadata.width);
1553 EXPECT_EQ(68u, metadata.height);
1554 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1555 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1556 EXPECT_EQ(1u, metadata.bits_per_pixel);
1557 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
1558
1559 // Verify the metadata of an image with RGB colorspace.
1560 obj = FPDFPage_GetObject(page, 37);
1561 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1562 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001563 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001564 EXPECT_EQ(126u, metadata.width);
1565 EXPECT_EQ(106u, metadata.height);
1566 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
1567 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
1568 EXPECT_EQ(24u, metadata.bits_per_pixel);
1569 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
1570
1571 UnloadPage(page);
1572}