blob: f9b165c28d45b04cde53f7799a2a1616ba213853 [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
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500251 FPDFPage_InsertObject(page, red_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000252 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000253 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000254 CompareBitmap(page_bitmap.get(), 612, 792,
255 "66d02eaa6181e2c069ce2ea99beda497");
256 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500257
258 // Now add to that a green rectangle with some medium alpha
259 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
260 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200261
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200262 // Make sure the type of the rectangle is a path.
263 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
264
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200265 // Make sure we get back the same color we set previously.
266 unsigned int R;
267 unsigned int G;
268 unsigned int B;
269 unsigned int A;
270 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
271 EXPECT_EQ(0U, R);
272 EXPECT_EQ(255U, G);
273 EXPECT_EQ(0U, B);
274 EXPECT_EQ(128U, A);
275
Miklos Vajna12abfd02017-09-15 07:49:03 +0200276 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
277 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200278 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200279 // Verify actual coordinates.
280 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
281 float x;
282 float y;
283 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
284 EXPECT_EQ(100, x);
285 EXPECT_EQ(100, y);
286 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
287 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
288 segment = FPDFPath_GetPathSegment(green_rect, 1);
289 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
290 EXPECT_EQ(100, x);
291 EXPECT_EQ(140, y);
292 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
293 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
294 segment = FPDFPath_GetPathSegment(green_rect, 2);
295 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
296 EXPECT_EQ(140, x);
297 EXPECT_EQ(140, y);
298 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
299 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
300 segment = FPDFPath_GetPathSegment(green_rect, 3);
301 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
302 EXPECT_EQ(140, x);
303 EXPECT_EQ(100, y);
304 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
305 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
306 segment = FPDFPath_GetPathSegment(green_rect, 4);
307 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
308 EXPECT_EQ(100, x);
309 EXPECT_EQ(100, y);
310 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
311 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200312
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500313 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
314 FPDFPage_InsertObject(page, green_rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000315 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000316 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000317 CompareBitmap(page_bitmap.get(), 612, 792,
318 "7b0b87604594e773add528fae567a558");
319 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500320
321 // Add a black triangle.
322 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
323 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
324 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
325 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
326 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
327 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200328
329 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
330 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200331 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200332 // Verify actual coordinates.
333 segment = FPDFPath_GetPathSegment(black_path, 0);
334 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
335 EXPECT_EQ(400, x);
336 EXPECT_EQ(100, y);
337 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
338 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
339 segment = FPDFPath_GetPathSegment(black_path, 1);
340 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
341 EXPECT_EQ(400, x);
342 EXPECT_EQ(200, y);
343 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
344 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
345 segment = FPDFPath_GetPathSegment(black_path, 2);
346 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
347 EXPECT_EQ(300, x);
348 EXPECT_EQ(100, y);
349 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
350 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
351 // Make sure out of bounds index access fails properly.
352 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200353
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500354 FPDFPage_InsertObject(page, black_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000355 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000356 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000357 CompareBitmap(page_bitmap.get(), 612, 792,
358 "eadc8020a14dfcf091da2688733d8806");
359 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500360
361 // Now add a more complex blue path.
362 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
363 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
364 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
365 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
366 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
367 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
368 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
369 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
370 EXPECT_TRUE(FPDFPath_Close(blue_path));
371 FPDFPage_InsertObject(page, blue_path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000372 const char kLastMD5[] = "9823e1a21bd9b72b6a442ba4f12af946";
373 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000374 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000375 CompareBitmap(page_bitmap.get(), 612, 792, kLastMD5);
376 }
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500377
378 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400379 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500380 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500381 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500382
383 // Render the saved result
Lei Zhang107fa7b2018-02-09 21:48:15 +0000384 VerifySavedDocument(612, 792, kLastMD5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500385}
386
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000387TEST_F(FPDFEditEmbeddertest, RemovePageObject) {
388 // Load document with some text.
389 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
390 FPDF_PAGE page = LoadPage(0);
391 ASSERT_TRUE(page);
392
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000393 // Show what the original file looks like.
394 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000395#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000396 const char kOriginalMD5[] = "b90475ca64d1348c3bf5e2b77ad9187a";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000397#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000398 const char kOriginalMD5[] = "e5a6fa28298db07484cd922f3e210c88";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000399#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000400 const char kOriginalMD5[] = "2baa4c0e1758deba1b9c908e1fbd04ed";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000401#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000402 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000403 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
404 }
405
406 // Get the "Hello, world!" text object and remove it.
407 ASSERT_EQ(2, FPDFPage_CountObjects(page));
408 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
409 ASSERT_TRUE(page_object);
410 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
411
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000412 // Verify the "Hello, world!" text is gone.
413 {
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000414#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000415 const char kRemovedMD5[] = "af760c4702467cb1492a57fb8215efaa";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000416#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000417 const char kRemovedMD5[] = "72be917349bf7004a5c39661fe1fc433";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000418#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000419 const char kRemovedMD5[] = "b76df015fe88009c3c342395df96abf1";
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000420#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000421 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000422 CompareBitmap(page_bitmap.get(), 200, 200, kRemovedMD5);
423 }
424 ASSERT_EQ(1, FPDFPage_CountObjects(page));
425
426 UnloadPage(page);
427 FPDFPageObj_Destroy(page_object);
428}
429
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000430TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) {
431 // Load document with some text.
432 EXPECT_TRUE(OpenDocument("text_in_page_marked.pdf"));
433 FPDF_PAGE page = LoadPage(0);
434 ASSERT_TRUE(page);
435
436 // Show what the original file looks like.
437 {
438#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
439 const char kOriginalMD5[] = "5a5eb63cb21cc15084fea1f14284b8df";
440#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
441 const char kOriginalMD5[] = "587c507a40f613f9c530b2ce2d58d655";
442#else
443 const char kOriginalMD5[] = "2edc6e70d54889aa0c0b7bdf3e168f86";
444#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000445 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000446 CompareBitmap(page_bitmap.get(), 200, 200, kOriginalMD5);
447 }
448
449 // Iterate over all objects, counting the number of times each content mark
450 // name appears.
451 int object_count = FPDFPage_CountObjects(page);
452 ASSERT_EQ(19, object_count);
453
454 unsigned long prime_count = 0;
455 unsigned long square_count = 0;
456 unsigned long greater_than_ten_count = 0;
457 std::vector<FPDF_PAGEOBJECT> primes;
458 for (int i = 0; i < object_count; ++i) {
459 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, i);
460
461 int mark_count = FPDFPageObj_CountMarks(page_object);
462 for (int j = 0; j < mark_count; ++j) {
463 FPDF_PAGEOBJECTMARK mark = FPDFPageObj_GetMark(page_object, j);
464
465 char buffer[256];
466 ASSERT_GT(FPDFPageObjMark_GetName(mark, buffer, 256), 0u);
467 std::wstring name =
468 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
469 if (name == L"Prime") {
470 prime_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000471 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000472 primes.push_back(page_object);
473 } else if (name == L"Square") {
474 square_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000475 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
Henrique Nakashima132c38e2018-04-23 16:35:56 +0000476 ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u);
477 std::wstring key =
478 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
479 EXPECT_EQ(L"Factor", key);
480 EXPECT_EQ(FPDF_OBJECT_NUMBER,
481 FPDFPageObjMark_GetParamValueType(mark, 0));
482 int square_root = FPDFPageObjMark_GetParamIntValue(mark, 0);
483 EXPECT_EQ(i + 1, square_root * square_root);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000484 } else if (name == L"GreaterThanTen") {
485 greater_than_ten_count++;
Henrique Nakashimaaed62532018-04-17 20:34:38 +0000486 EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark));
Henrique Nakashimab557bdc2018-04-23 18:04:26 +0000487 } else if (name == L"Bounds") {
488 EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark));
489 ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u);
490 std::wstring key =
491 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
492 EXPECT_EQ(L"Position", key);
493 EXPECT_EQ(FPDF_OBJECT_STRING,
494 FPDFPageObjMark_GetParamValueType(mark, 0));
495 ASSERT_GT(FPDFPageObjMark_GetParamStringValue(mark, 0, buffer, 256),
496 0u);
497 std::wstring value =
498 GetPlatformWString(reinterpret_cast<unsigned short*>(buffer));
499 EXPECT_EQ(L"Last", value);
500 EXPECT_EQ(18, i);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000501 } else {
502 FAIL();
503 }
504 }
505 }
506
507 // Expect certain number of tagged objects. The test file contains strings
508 // from 1 to 19.
509 EXPECT_EQ(8u, prime_count);
510 EXPECT_EQ(4u, square_count);
511 EXPECT_EQ(9u, greater_than_ten_count);
512
513 // Remove all objects marked with "Prime".
514 for (FPDF_PAGEOBJECT page_object : primes) {
515 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
516 FPDFPageObj_Destroy(page_object);
517 }
518
519 EXPECT_EQ(11, FPDFPage_CountObjects(page));
520
521 {
522#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
523 const char kNonPrimesMD5[] = "57e76dc7375d896704f0fd6d6d1b9e65";
524#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
525 const char kNonPrimesMD5[] = "4d906b57fba36c70c600cf50d60f508c";
526#else
527 const char kNonPrimesMD5[] = "33d9c45bec41ead92a295e252f6b7922";
528#endif
Tom Sepeze08d2b12018-04-25 18:49:32 +0000529 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashimac90adc52018-03-27 16:26:44 +0000530 CompareBitmap(page_bitmap.get(), 200, 200, kNonPrimesMD5);
531 }
532
533 UnloadPage(page);
534}
535
Henrique Nakashimac49e62e2018-04-16 20:58:47 +0000536// Fails due to pdfium:1051.
537TEST_F(FPDFEditEmbeddertest, DISABLED_RemoveExistingPageObject) {
538 // Load document with some text.
539 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
540 FPDF_PAGE page = LoadPage(0);
541 ASSERT_TRUE(page);
542
543 // Get the "Hello, world!" text object and remove it.
544 ASSERT_EQ(2, FPDFPage_CountObjects(page));
545 FPDF_PAGEOBJECT page_object = FPDFPage_GetObject(page, 0);
546 ASSERT_TRUE(page_object);
547 EXPECT_TRUE(FPDFPage_RemoveObject(page, page_object));
548
549 // Verify the "Hello, world!" text is gone.
550 ASSERT_EQ(1, FPDFPage_CountObjects(page));
551
552 // Save the file
553 EXPECT_TRUE(FPDFPage_GenerateContent(page));
554 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
555 UnloadPage(page);
556 FPDFPageObj_Destroy(page_object);
557
558 // Re-open the file and check the page object count is still 1.
559 OpenSavedDocument();
560 FPDF_PAGE saved_page = LoadSavedPage(0);
561 EXPECT_EQ(1, FPDFPage_CountObjects(saved_page));
562 CloseSavedPage(saved_page);
563 CloseSavedDocument();
564}
565
566TEST_F(FPDFEditEmbeddertest, InsertPageObjectAndSave) {
567 // Load document with some text.
568 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
569 FPDF_PAGE page = LoadPage(0);
570 ASSERT_TRUE(page);
571
572 // Add a red rectangle.
573 ASSERT_EQ(2, FPDFPage_CountObjects(page));
574 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
575 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
576 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
577 FPDFPage_InsertObject(page, red_rect);
578
579 // Verify the red rectangle was added.
580 ASSERT_EQ(3, 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
587 // Re-open the file and check the page object count is still 3.
588 OpenSavedDocument();
589 FPDF_PAGE saved_page = LoadSavedPage(0);
590 EXPECT_EQ(3, FPDFPage_CountObjects(saved_page));
591 CloseSavedPage(saved_page);
592 CloseSavedDocument();
593}
594
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000595TEST_F(FPDFEditEmbeddertest, AddAndRemovePaths) {
596 // Start with a blank page.
597 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
598 ASSERT_TRUE(page);
599
600 // Render the blank page and verify it's a blank bitmap.
601 const char kBlankMD5[] = "1940568c9ba33bac5d0b1ee9558c76b3";
602 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000603 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000604 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
605 }
606 ASSERT_EQ(0, FPDFPage_CountObjects(page));
607
608 // Add a red rectangle.
609 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
610 ASSERT_TRUE(red_rect);
611 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
612 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
613 FPDFPage_InsertObject(page, red_rect);
614 const char kRedRectangleMD5[] = "66d02eaa6181e2c069ce2ea99beda497";
615 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000616 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000617 CompareBitmap(page_bitmap.get(), 612, 792, kRedRectangleMD5);
618 }
619 EXPECT_EQ(1, FPDFPage_CountObjects(page));
620
621 // Remove rectangle and verify it does not render anymore and the bitmap is
622 // back to a blank one.
623 EXPECT_TRUE(FPDFPage_RemoveObject(page, red_rect));
624 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000625 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Henrique Nakashima35841fa2018-03-15 15:25:16 +0000626 CompareBitmap(page_bitmap.get(), 612, 792, kBlankMD5);
627 }
628 EXPECT_EQ(0, FPDFPage_CountObjects(page));
629
630 // Trying to remove an object not in the page should return false.
631 EXPECT_FALSE(FPDFPage_RemoveObject(page, red_rect));
632
633 FPDF_ClosePage(page);
634 FPDFPageObj_Destroy(red_rect);
635}
636
Miklos Vajna12abfd02017-09-15 07:49:03 +0200637TEST_F(FPDFEditEmbeddertest, PathsPoints) {
638 CreateNewDocument();
639 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
640 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +0200641 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200642
643 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +0200644 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200645
Miklos Vajna36eed872017-09-20 22:52:43 +0200646 // FPDFPath_GetPathSegment() with a non-path.
647 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
648 // FPDFPath_GetPathSegment() with a NULL path.
649 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
650 float x;
651 float y;
652 // FPDFPathSegment_GetPoint() with a NULL segment.
653 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
654
655 // FPDFPathSegment_GetType() with a NULL segment.
656 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
657
658 // FPDFPathSegment_GetClose() with a NULL segment.
659 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
660
Miklos Vajna12abfd02017-09-15 07:49:03 +0200661 FPDFPageObj_Destroy(img);
662}
663
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500664TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
665 // Load document with some text
666 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
667 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000668 ASSERT_TRUE(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500669
670 // Add an opaque rectangle on top of some of the text.
671 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
672 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
673 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
674 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500675
676 // Add a transparent triangle on top of other part of the text.
677 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
678 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
679 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
680 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
681 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
682 EXPECT_TRUE(FPDFPath_Close(black_path));
683 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500684
685 // Render and check the result. Text is slightly different on Mac.
Tom Sepeze08d2b12018-04-25 18:49:32 +0000686 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400687#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700688 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500689#else
Henrique Nakashima09b41922017-10-27 20:39:29 +0000690 const char md5[] = "aa71b09b93b55f467f1290e5111babee";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400691#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000692 CompareBitmap(bitmap.get(), 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500693 UnloadPage(page);
694}
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500695
wileyryae858aa42017-05-31 14:49:05 -0500696TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
697 // Load document with existing content
698 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
699 FPDF_PAGE page = LoadPage(0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000700 ASSERT_TRUE(page);
wileyryae858aa42017-05-31 14:49:05 -0500701
702 // Add a transparent rectangle on top of the existing content
703 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
704 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
705 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
706 FPDFPage_InsertObject(page, red_rect2);
707
708 // Add an opaque rectangle on top of the existing content
709 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
710 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
711 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
712 FPDFPage_InsertObject(page, red_rect);
713
Tom Sepeze08d2b12018-04-25 18:49:32 +0000714 ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000715 CompareBitmap(bitmap.get(), 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500716 EXPECT_TRUE(FPDFPage_GenerateContent(page));
717
718 // Now save the result, closing the page and document
719 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400720 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -0500721
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400722 OpenSavedDocument();
Lei Zhang107fa7b2018-02-09 21:48:15 +0000723 FPDF_PAGE saved_page = LoadSavedPage(0);
724 VerifySavedRendering(saved_page, 612, 792,
725 "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500726
727 ClearString();
728 // Add another opaque rectangle on top of the existing content
729 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
730 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
731 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000732 FPDFPage_InsertObject(saved_page, green_rect);
wileyryae858aa42017-05-31 14:49:05 -0500733
734 // Add another transparent rectangle on top of existing content
735 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
736 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
737 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000738 FPDFPage_InsertObject(saved_page, green_rect2);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000739 const char kLastMD5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
740 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000741 ScopedFPDFBitmap new_bitmap = RenderSavedPage(saved_page);
Lei Zhangc113c7a2018-02-12 14:58:44 +0000742 CompareBitmap(new_bitmap.get(), 612, 792, kLastMD5);
743 }
Lei Zhang107fa7b2018-02-09 21:48:15 +0000744 EXPECT_TRUE(FPDFPage_GenerateContent(saved_page));
wileyryae858aa42017-05-31 14:49:05 -0500745
746 // Now save the result, closing the page and document
Lei Zhang0729be22018-02-05 21:13:51 +0000747 EXPECT_TRUE(FPDF_SaveAsCopy(saved_document_, this, 0));
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400748
Lei Zhang107fa7b2018-02-09 21:48:15 +0000749 CloseSavedPage(saved_page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -0400750 CloseSavedDocument();
wileyryae858aa42017-05-31 14:49:05 -0500751
752 // Render the saved result
Lei Zhangc113c7a2018-02-12 14:58:44 +0000753 VerifySavedDocument(612, 792, kLastMD5);
wileyryae858aa42017-05-31 14:49:05 -0500754}
755
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500756TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
757 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500758 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500759
760 // Add a large stroked rectangle (fill color should not affect it).
761 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
762 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
763 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
764 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
Miklos Vajna366df7f2018-05-22 14:27:29 +0000765
766 float width = 0;
767 EXPECT_TRUE(FPDFPageObj_GetStrokeWidth(rect, &width));
768 EXPECT_EQ(15.0f, width);
769
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500770 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
771 FPDFPage_InsertObject(page, rect);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000772 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000773 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000774 CompareBitmap(page_bitmap.get(), 612, 792,
775 "64bd31f862a89e0a9e505a5af6efd506");
776 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500777
778 // Add crossed-checkmark
779 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
780 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
781 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
782 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
783 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
784 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
785 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
786 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
787 FPDFPage_InsertObject(page, check);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000788 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000789 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000790 CompareBitmap(page_bitmap.get(), 612, 792,
791 "4b6f3b9d25c4e194821217d5016c3724");
792 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500793
794 // Add stroked and filled oval-ish path.
795 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
796 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
797 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
798 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
799 EXPECT_TRUE(FPDFPath_Close(path));
800 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
801 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
802 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
803 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
804 FPDFPage_InsertObject(page, path);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000805 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000806 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000807 CompareBitmap(page_bitmap.get(), 612, 792,
808 "ff3e6a22326754944cc6e56609acd73b");
809 }
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500810 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500811}
Nicolas Pena49058402017-02-14 18:26:20 -0500812
813TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
814 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500815 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -0500816
817 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400818 FPDF_PAGEOBJECT text_object1 =
819 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
820 EXPECT_TRUE(text_object1);
821 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
822 GetFPDFWideString(L"I'm at the bottom of the page");
823 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
824 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
825 FPDFPage_InsertObject(page, text_object1);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000826 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000827 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400828#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000829 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -0500830#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000831 const char md5[] = "eacaa24573b8ce997b3882595f096f00";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400832#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000833 CompareBitmap(page_bitmap.get(), 612, 792, md5);
834 }
Nicolas Pena49058402017-02-14 18:26:20 -0500835
836 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -0400837 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500838 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400839 EXPECT_TRUE(text_object2);
840 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
841 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
842 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
843 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
844 FPDFPage_InsertObject(page, text_object2);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000845 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000846 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400847#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Dan Sinclair971a6742018-03-28 19:23:25 +0000848 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -0400849#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Dan Sinclair971a6742018-03-28 19:23:25 +0000850 const char md5_2[] = "2587eac9a787e97a37636d54d11bd28d";
Nicolas Pena49058402017-02-14 18:26:20 -0500851#else
Dan Sinclair971a6742018-03-28 19:23:25 +0000852 const char md5_2[] = "76fcc7d08aa15445efd2e2ceb7c6cc3b";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400853#endif
Dan Sinclair971a6742018-03-28 19:23:25 +0000854 CompareBitmap(page_bitmap.get(), 612, 792, md5_2);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000855 }
Nicolas Pena49058402017-02-14 18:26:20 -0500856
857 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -0400858 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500859 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400860 EXPECT_TRUE(text_object3);
861 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
862 GetFPDFWideString(L"Can you read me? <:)>");
863 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
864 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
865 FPDFPage_InsertObject(page, text_object3);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000866 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000867 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -0400868#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000869 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -0400870#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Lei Zhang107fa7b2018-02-09 21:48:15 +0000871 const char md5_3[] = "7cb61ec112cf400b489360d443ffc9d2";
Nicolas Pena49058402017-02-14 18:26:20 -0500872#else
Lei Zhang107fa7b2018-02-09 21:48:15 +0000873 const char md5_3[] = "b8a21668f1dab625af7c072e07fcefc4";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400874#endif
Lei Zhang107fa7b2018-02-09 21:48:15 +0000875 CompareBitmap(page_bitmap.get(), 612, 792, md5_3);
876 }
Nicolas Pena49058402017-02-14 18:26:20 -0500877
878 // TODO(npm): Why are there issues with text rotated by 90 degrees?
879 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
880 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -0500881}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500882
Nicolas Pena603a31d2017-06-14 11:41:18 -0400883TEST_F(FPDFEditEmbeddertest, GraphicsData) {
884 // New page
Tom Sepeze08d2b12018-04-25 18:49:32 +0000885 ScopedFPDFPage page(FPDFPage_New(CreateNewDocument(), 0, 612, 792));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400886
887 // Create a rect with nontrivial graphics
888 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
889 FPDFPageObj_SetBlendMode(rect1, "Color");
890 FPDFPage_InsertObject(page.get(), rect1);
891 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
892
893 // Check that the ExtGState was created
Lei Zhang107fa7b2018-02-09 21:48:15 +0000894 CPDF_Page* cpage = CPDFPageFromFPDFPage(page.get());
895 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Pena603a31d2017-06-14 11:41:18 -0400896 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400897 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400898
899 // Add a text object causing no change to the graphics dictionary
900 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
901 // Only alpha, the last component, matters for the graphics dictionary. And
902 // the default value is 255.
903 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
904 FPDFPage_InsertObject(page.get(), text1);
905 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400906 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400907
908 // Add a text object increasing the size of the graphics dictionary
909 FPDF_PAGEOBJECT text2 =
910 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
911 FPDFPage_InsertObject(page.get(), text2);
912 FPDFPageObj_SetBlendMode(text2, "Darken");
913 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
914 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400915 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400916
917 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -0400918 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -0400919 FPDFPageObj_SetBlendMode(path, "Darken");
920 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
921 FPDFPage_InsertObject(page.get(), path);
922 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400923 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400924
925 // Add a rect increasing the size of the graphics dictionary
926 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
927 FPDFPageObj_SetBlendMode(rect2, "Darken");
928 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
929 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
930 FPDFPage_InsertObject(page.get(), rect2);
931 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400932 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400933}
934
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500935TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
936 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500937 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500938
939 // Add a red rectangle with some non-default alpha
940 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
941 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
942 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
943 FPDFPage_InsertObject(page, rect);
944 EXPECT_TRUE(FPDFPage_GenerateContent(page));
945
946 // Check the ExtGState
Lei Zhang107fa7b2018-02-09 21:48:15 +0000947 CPDF_Page* cpage = CPDFPageFromFPDFPage(page);
948 CPDF_Dictionary* graphics_dict = cpage->m_pResources->GetDictFor("ExtGState");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500949 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400950 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500951
952 // Check the bitmap
Lei Zhang107fa7b2018-02-09 21:48:15 +0000953 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000954 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000955 CompareBitmap(page_bitmap.get(), 612, 792,
956 "5384da3406d62360ffb5cac4476fff1c");
957 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500958
959 // Never mind, my new favorite color is blue, increase alpha
960 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
961 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400962 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500963
964 // Check that bitmap displays changed content
Lei Zhang107fa7b2018-02-09 21:48:15 +0000965 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000966 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000967 CompareBitmap(page_bitmap.get(), 612, 792,
968 "2e51656f5073b0bee611d9cd086aa09c");
969 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500970
971 // And now generate, without changes
972 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400973 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000974 {
Tom Sepeze08d2b12018-04-25 18:49:32 +0000975 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +0000976 CompareBitmap(page_bitmap.get(), 612, 792,
977 "2e51656f5073b0bee611d9cd086aa09c");
978 }
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500979
980 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400981 FPDF_PAGEOBJECT text_object =
982 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
983 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
984 GetFPDFWideString(L"Something something #text# something");
985 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
986 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
987 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500988 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Lei Zhang107fa7b2018-02-09 21:48:15 +0000989 CPDF_Dictionary* font_dict = cpage->m_pResources->GetDictFor("Font");
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500990 ASSERT_TRUE(font_dict);
991 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
992
993 // Generate yet again, check dicts are reasonably sized
994 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400995 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500996 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
997 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500998}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500999
Nicolas Penad03ca422017-03-06 13:54:33 -05001000TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
1001 CreateNewDocument();
1002 // TODO(npm): use other fonts after disallowing loading any font as any type
1003 const CPDF_Font* stock_font =
1004 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001005 const uint8_t* data = stock_font->GetFont()->GetFontData();
1006 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001007 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001008 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
1009 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001010 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001011 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001012
Lei Zhang710fa992018-05-25 16:24:48 +00001013 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -05001014 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1015 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
1016 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
1017 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1018 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1019 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -05001020 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
1021
Lei Zhangde579ab2018-05-25 21:49:49 +00001022 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001023 ASSERT_TRUE(widths_array);
1024 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001025 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -05001026 EXPECT_EQ(569, widths_array->GetNumberAt(11));
1027 EXPECT_EQ(500, widths_array->GetNumberAt(223));
1028 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
1029}
Nicolas Penabe90aae2017-02-27 10:41:41 -05001030
Nicolas Penad03ca422017-03-06 13:54:33 -05001031TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
1032 CreateNewDocument();
1033 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001034 const uint8_t* data = stock_font->GetFont()->GetFontData();
1035 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001036 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001037 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
1038 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001039 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001040 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -05001041
Lei Zhang710fa992018-05-25 16:24:48 +00001042 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001043 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1044 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
1045 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
1046 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
1047 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
1048 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
1049 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -05001050
Lei Zhangde579ab2018-05-25 21:49:49 +00001051 const CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -05001052 ASSERT_TRUE(widths_array);
1053 ASSERT_EQ(224U, widths_array->GetCount());
1054 EXPECT_EQ(600, widths_array->GetNumberAt(33));
1055 EXPECT_EQ(600, widths_array->GetNumberAt(74));
1056 EXPECT_EQ(600, widths_array->GetNumberAt(223));
1057 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
1058}
1059
1060TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
1061 CreateNewDocument();
1062 const CPDF_Font* stock_font =
1063 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
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_TYPE1, 1));
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->IsCIDFont());
1071
1072 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001073 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001074 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1075 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1076 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
1077 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001078 const CPDF_Array* descendant_array =
1079 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001080 ASSERT_TRUE(descendant_array);
1081 EXPECT_EQ(1U, descendant_array->GetCount());
1082
1083 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001084 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001085 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1086 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
1087 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001088 const CPDF_Dictionary* cidinfo_dict =
1089 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001090 ASSERT_TRUE(cidinfo_dict);
1091 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1092 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1093 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1094 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
1095
1096 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001097 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001098 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001099 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -05001100 CheckCompositeFontWidths(widths_array, typed_font);
1101}
1102
1103TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
1104 CreateNewDocument();
1105 const CPDF_Font* stock_font =
1106 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001107 const uint8_t* data = stock_font->GetFont()->GetFontData();
1108 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -05001109
Tom Sepeze08d2b12018-04-25 18:49:32 +00001110 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001111 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1112 ASSERT_TRUE(font.get());
Tom Sepez525147a2018-05-03 17:19:53 +00001113 CPDF_Font* typed_font = CPDFFontFromFPDFFont(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -05001114 EXPECT_TRUE(typed_font->IsCIDFont());
1115
1116 // Check font dictionary entries
Lei Zhang710fa992018-05-25 16:24:48 +00001117 const CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penad03ca422017-03-06 13:54:33 -05001118 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
1119 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
1120 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
1121 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
Lei Zhangde579ab2018-05-25 21:49:49 +00001122 const CPDF_Array* descendant_array =
1123 font_dict->GetArrayFor("DescendantFonts");
Nicolas Penad03ca422017-03-06 13:54:33 -05001124 ASSERT_TRUE(descendant_array);
1125 EXPECT_EQ(1U, descendant_array->GetCount());
1126
1127 // Check the CIDFontDict
Lei Zhangde579ab2018-05-25 21:49:49 +00001128 const CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
Nicolas Penad03ca422017-03-06 13:54:33 -05001129 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
1130 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
1131 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
Lei Zhangb1ec2802018-05-25 21:55:24 +00001132 const CPDF_Dictionary* cidinfo_dict =
1133 cidfont_dict->GetDictFor("CIDSystemInfo");
Nicolas Penad03ca422017-03-06 13:54:33 -05001134 ASSERT_TRUE(cidinfo_dict);
1135 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
1136 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
1137 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
1138 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
1139 data);
1140
1141 // Check widths
Lei Zhangde579ab2018-05-25 21:49:49 +00001142 const CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
Nicolas Penad03ca422017-03-06 13:54:33 -05001143 ASSERT_TRUE(widths_array);
1144 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -05001145}
rbpotterce8e51e2017-04-28 12:42:47 -07001146
1147TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
1148 // Load document with a -90 degree rotation
1149 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
1150 FPDF_PAGE page = LoadPage(0);
1151 EXPECT_NE(nullptr, page);
1152
1153 EXPECT_EQ(3, FPDFPage_GetRotation(page));
1154 UnloadPage(page);
1155}
Nicolas Penab3161852017-05-02 14:12:50 -04001156
1157TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
1158 // Start with a blank page
1159 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1160 {
1161 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -07001162 const uint8_t* data = stock_font->GetFont()->GetFontData();
1163 const uint32_t size = stock_font->GetFont()->GetSize();
Tom Sepeze08d2b12018-04-25 18:49:32 +00001164 ScopedFPDFFont font(
Nicolas Penab3161852017-05-02 14:12:50 -04001165 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
1166 ASSERT_TRUE(font.get());
1167
1168 // Add some text to the page
1169 FPDF_PAGEOBJECT text_object =
1170 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1171 EXPECT_TRUE(text_object);
1172 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1173 GetFPDFWideString(L"I am testing my loaded font, WEE.");
1174 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1175 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
1176 FPDFPage_InsertObject(page, text_object);
Tom Sepeze08d2b12018-04-25 18:49:32 +00001177 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001178#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001179 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
1180#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001181 const char md5[] = "70592859010ffbf532a2237b8118bcc4";
Dan Sinclair698aed72017-09-26 16:24:49 -04001182#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001183 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Penab3161852017-05-02 14:12:50 -04001184
1185 // Add some more text, same font
1186 FPDF_PAGEOBJECT text_object2 =
1187 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
1188 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1189 GetFPDFWideString(L"Bigger font size");
1190 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1191 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
1192 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -04001193 }
Tom Sepeze08d2b12018-04-25 18:49:32 +00001194 ScopedFPDFBitmap page_bitmap2 = RenderPageWithFlags(page, nullptr, 0);
Dan Sinclair698aed72017-09-26 16:24:49 -04001195#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -04001196 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
1197#else
Henrique Nakashima09b41922017-10-27 20:39:29 +00001198 const char md5_2[] = "c1d10cce1761c4a998a16b2562030568";
Dan Sinclair698aed72017-09-26 16:24:49 -04001199#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang107fa7b2018-02-09 21:48:15 +00001200 CompareBitmap(page_bitmap2.get(), 612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001201
Nicolas Pena207b7272017-05-26 17:37:06 -04001202 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -04001203 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1204 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001205
1206 VerifySavedDocument(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -04001207}
Nicolas Penaf45ade32017-05-03 10:23:49 -04001208
Jane Liueda65252017-06-07 11:31:27 -04001209TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
1210 // Open a file with one annotation and load its first page.
1211 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
Lei Zhang75c81712018-02-08 17:22:39 +00001212 FPDF_PAGE page = LoadPage(0);
Jane Liueda65252017-06-07 11:31:27 -04001213 ASSERT_TRUE(page);
1214
Lei Zhanga21d5932018-02-05 18:28:38 +00001215 {
1216 // Add an underline annotation to the page without specifying its rectangle.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001217 ScopedFPDFAnnotation annot(
Lei Zhanga21d5932018-02-05 18:28:38 +00001218 FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE));
1219 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -04001220
Lei Zhanga21d5932018-02-05 18:28:38 +00001221 // FPDFPage_TransformAnnots() should run without errors when modifying
1222 // annotation rectangles.
1223 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
1224 }
Jane Liueda65252017-06-07 11:31:27 -04001225 UnloadPage(page);
1226}
1227
Nicolas Penaf45ade32017-05-03 10:23:49 -04001228// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -04001229#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -04001230TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
1231 // Start with a blank page
1232 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
1233 CFX_Font CIDfont;
1234 {
1235 // First, get the data from the font
1236 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
1237 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
1238 const uint8_t* data = CIDfont.GetFontData();
1239 const uint32_t size = CIDfont.GetSize();
1240
1241 // Load the data into a FPDF_Font.
Tom Sepeze08d2b12018-04-25 18:49:32 +00001242 ScopedFPDFFont font(
Nicolas Penaf45ade32017-05-03 10:23:49 -04001243 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
1244 ASSERT_TRUE(font.get());
1245
1246 // Add some text to the page
1247 FPDF_PAGEOBJECT text_object =
1248 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
1249 ASSERT_TRUE(text_object);
1250 std::wstring wstr = L"ABCDEFGhijklmnop.";
1251 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
1252 GetFPDFWideString(wstr);
1253 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
1254 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
1255 FPDFPage_InsertObject(page, text_object);
1256
1257 // And add some Japanese characters
1258 FPDF_PAGEOBJECT text_object2 =
1259 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
1260 ASSERT_TRUE(text_object2);
1261 std::wstring wstr2 =
1262 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
1263 L"\u756A";
1264 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
1265 GetFPDFWideString(wstr2);
1266 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
1267 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
1268 FPDFPage_InsertObject(page, text_object2);
1269 }
1270
Nicolas Pena207b7272017-05-26 17:37:06 -04001271 // Check that the text renders properly.
Henrique Nakashima09b41922017-10-27 20:39:29 +00001272 const char md5[] = "c68cd79aa72bf83a7b25271370d46b21";
Lei Zhang107fa7b2018-02-09 21:48:15 +00001273 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001274 ScopedFPDFBitmap page_bitmap = RenderPageWithFlags(page, nullptr, 0);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001275 CompareBitmap(page_bitmap.get(), 612, 792, md5);
1276 }
Nicolas Penaf45ade32017-05-03 10:23:49 -04001277
1278 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -04001279 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -04001280 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1281 FPDF_ClosePage(page);
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001282
1283 VerifySavedDocument(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -04001284}
Dan Sinclair698aed72017-09-26 16:24:49 -04001285#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001286
1287TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04001288 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001289 {
1290 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
1291 FPDF_PAGE page = LoadPage(0);
1292 ASSERT_NE(nullptr, page);
1293
1294 // Now add a more complex blue path.
1295 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
1296 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
1297 // TODO(npm): stroking will cause the MD5s to differ.
1298 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
1299 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
1300 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
1301 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
1302 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
1303 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
1304 EXPECT_TRUE(FPDFPath_Close(green_path));
1305 FPDFPage_InsertObject(page, green_path);
Tom Sepeze08d2b12018-04-25 18:49:32 +00001306 ScopedFPDFBitmap page_bitmap = RenderLoadedPage(page);
Lei Zhang107fa7b2018-02-09 21:48:15 +00001307 CompareBitmap(page_bitmap.get(), 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001308
1309 // Now save the result, closing the page and document
1310 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1311 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1312 UnloadPage(page);
1313 }
Dan Sinclair04e4dc82017-10-18 12:17:14 -04001314
1315 VerifySavedDocument(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001316}
Jane Liu28fb7ba2017-08-02 21:45:57 -04001317
1318TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
1319 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1320 FPDF_PAGE page = LoadPage(0);
1321 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001322 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04001323
1324 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1325 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1326 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
1327
1328 obj = FPDFPage_GetObject(page, 33);
1329 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1330 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
1331 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1332 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
1333 FPDFBitmap_Destroy(bitmap);
1334
1335 obj = FPDFPage_GetObject(page, 34);
1336 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1337 bitmap = FPDFImageObj_GetBitmap(obj);
1338 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1339 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
1340 FPDFBitmap_Destroy(bitmap);
1341
1342 obj = FPDFPage_GetObject(page, 35);
1343 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1344 bitmap = FPDFImageObj_GetBitmap(obj);
1345 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
1346 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
1347 FPDFBitmap_Destroy(bitmap);
1348
1349 obj = FPDFPage_GetObject(page, 36);
1350 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1351 bitmap = FPDFImageObj_GetBitmap(obj);
1352 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1353 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
1354 FPDFBitmap_Destroy(bitmap);
1355
1356 obj = FPDFPage_GetObject(page, 37);
1357 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1358 bitmap = FPDFImageObj_GetBitmap(obj);
1359 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1360 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
1361 FPDFBitmap_Destroy(bitmap);
1362
1363 obj = FPDFPage_GetObject(page, 38);
1364 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1365 bitmap = FPDFImageObj_GetBitmap(obj);
1366 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1367 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
1368 FPDFBitmap_Destroy(bitmap);
1369 UnloadPage(page);
1370}
Jane Liu548334e2017-08-03 16:33:40 -04001371
Lei Zhang53341dd2018-03-01 15:42:47 +00001372TEST_F(FPDFEditEmbeddertest, ExtractJBigImageBitmap) {
1373 ASSERT_TRUE(OpenDocument("bug_631912.pdf"));
1374 FPDF_PAGE page = LoadPage(0);
1375 ASSERT_TRUE(page);
1376 ASSERT_EQ(1, FPDFPage_CountObjects(page));
1377
1378 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 0);
1379 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1380 {
Tom Sepeze08d2b12018-04-25 18:49:32 +00001381 ScopedFPDFBitmap bitmap(FPDFImageObj_GetBitmap(obj));
Lei Zhang1330ebb2018-03-05 15:16:37 +00001382 ASSERT_TRUE(bitmap);
1383 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap.get()));
1384 CompareBitmap(bitmap.get(), 1152, 720, "3f6a48e2b3e91b799bf34567f55cb4de");
Lei Zhang53341dd2018-03-01 15:42:47 +00001385 }
1386
1387 UnloadPage(page);
1388}
1389
Jane Liu548334e2017-08-03 16:33:40 -04001390TEST_F(FPDFEditEmbeddertest, GetImageData) {
1391 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1392 FPDF_PAGE page = LoadPage(0);
1393 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001394 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04001395
1396 // Retrieve an image object with flate-encoded data stream.
1397 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
1398 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1399
1400 // Check that the raw image data has the correct length and hash value.
1401 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1402 std::vector<char> buf(len);
1403 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1404 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
1405 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1406
1407 // Check that the decoded image data has the correct length and hash value.
1408 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1409 buf.clear();
1410 buf.resize(len);
1411 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1412 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
1413 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1414
1415 // Retrieve an image obejct with DCTDecode-encoded data stream.
1416 obj = FPDFPage_GetObject(page, 37);
1417 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1418
1419 // Check that the raw image data has the correct length and hash value.
1420 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1421 buf.clear();
1422 buf.resize(len);
1423 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1424 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1425 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1426
1427 // Check that the decoded image data has the correct length and hash value,
1428 // which should be the same as those of the raw data, since this image is
1429 // encoded by a single DCTDecode filter and decoding is a noop.
1430 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1431 buf.clear();
1432 buf.resize(len);
1433 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1434 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1435 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1436
1437 UnloadPage(page);
1438}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04001439
1440TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
1441 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1442 ASSERT_TRUE(rect);
1443
1444 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
1445 FPDFPageObj_Destroy(rect);
1446}
Jane Liube63ab92017-08-09 14:09:34 -04001447
1448TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
1449 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1450 FPDF_PAGE page = LoadPage(0);
1451 ASSERT_TRUE(page);
1452
1453 // Verify that retrieving the filter of a non-image object would fail.
1454 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1455 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1456 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
1457 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
1458
1459 // Verify the returned filter string for an image object with a single filter.
1460 obj = FPDFPage_GetObject(page, 33);
1461 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1462 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
1463 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1464 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001465 static constexpr char kFlateDecode[] = "FlateDecode";
1466 EXPECT_EQ(sizeof(kFlateDecode),
1467 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1468 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001469 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
1470
1471 // Verify all the filters for an image object with a list of filters.
1472 obj = FPDFPage_GetObject(page, 38);
1473 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1474 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
1475 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1476 buf.clear();
1477 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001478 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
1479 EXPECT_EQ(sizeof(kASCIIHexDecode),
1480 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1481 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001482
1483 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
1484 buf.clear();
1485 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001486 static constexpr char kDCTDecode[] = "DCTDecode";
1487 EXPECT_EQ(sizeof(kDCTDecode),
1488 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
1489 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001490
1491 UnloadPage(page);
1492}
Jane Liuca898292017-08-16 11:25:35 -04001493
1494TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
1495 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1496 FPDF_PAGE page = LoadPage(0);
1497 ASSERT_TRUE(page);
1498
1499 // Check that getting the metadata of a null object would fail.
1500 FPDF_IMAGEOBJ_METADATA metadata;
1501 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
1502
1503 // Check that receiving the metadata with a null metadata object would fail.
1504 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
1505 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
1506
1507 // Check that when retrieving an image object's metadata without passing in
1508 // |page|, all values are correct, with the last two being default values.
1509 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1510 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001511 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001512 EXPECT_EQ(92u, metadata.width);
1513 EXPECT_EQ(68u, metadata.height);
1514 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1515 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1516 EXPECT_EQ(0u, metadata.bits_per_pixel);
1517 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
1518
1519 // Verify the metadata of a bitmap image with indexed colorspace.
1520 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001521 EXPECT_EQ(7, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001522 EXPECT_EQ(92u, metadata.width);
1523 EXPECT_EQ(68u, metadata.height);
1524 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1525 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1526 EXPECT_EQ(1u, metadata.bits_per_pixel);
1527 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
1528
1529 // Verify the metadata of an image with RGB colorspace.
1530 obj = FPDFPage_GetObject(page, 37);
1531 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1532 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
Julian Lungerecd063e2017-12-27 10:18:50 -05001533 EXPECT_EQ(9, metadata.marked_content_id);
Jane Liuca898292017-08-16 11:25:35 -04001534 EXPECT_EQ(126u, metadata.width);
1535 EXPECT_EQ(106u, metadata.height);
1536 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
1537 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
1538 EXPECT_EQ(24u, metadata.bits_per_pixel);
1539 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
1540
1541 UnloadPage(page);
1542}