blob: 9f43054b4c14c4ee2a76f0f18ae2ef97e418a127 [file] [log] [blame]
Tom Sepezd483eb42016-01-06 10:03:59 -08001// Copyright 2016 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05005#include <memory>
6#include <string>
Nicolas Penad03ca422017-03-06 13:54:33 -05007#include <utility>
Jane Liu548334e2017-08-03 16:33:40 -04008#include <vector>
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009
Nicolas Penabe90aae2017-02-27 10:41:41 -050010#include "core/fpdfapi/font/cpdf_font.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050011#include "core/fpdfapi/page/cpdf_page.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050012#include "core/fpdfapi/parser/cpdf_array.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050013#include "core/fpdfapi/parser/cpdf_dictionary.h"
Nicolas Penad03ca422017-03-06 13:54:33 -050014#include "core/fpdfapi/parser/cpdf_number.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050015#include "core/fpdfapi/parser/cpdf_stream.h"
Nicolas Pena0fc185e2017-02-08 12:13:20 -050016#include "core/fxcrt/fx_system.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050017#include "fpdfsdk/fsdk_define.h"
Nicolas Penab3161852017-05-02 14:12:50 -040018#include "public/cpp/fpdf_deleters.h"
Jane Liueda65252017-06-07 11:31:27 -040019#include "public/fpdf_annot.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080020#include "public/fpdf_edit.h"
21#include "public/fpdfview.h"
22#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080023#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080024#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080025#include "testing/test_support.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080026
Nicolas Pena3ff54002017-07-05 11:55:35 -040027class FPDFEditEmbeddertest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050028 protected:
29 FPDF_DOCUMENT CreateNewDocument() {
30 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070031 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050032 return document_;
33 }
34
35 void CheckFontDescriptor(CPDF_Dictionary* font_dict,
36 int font_type,
37 bool bold,
38 bool italic,
39 uint32_t size,
40 const uint8_t* data) {
41 CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
42 ASSERT_TRUE(font_desc);
43 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
44 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
45 font_desc->GetStringFor("FontName"));
46
47 // Check that the font descriptor has the required keys according to spec
48 // 1.7 Table 5.19
49 ASSERT_TRUE(font_desc->KeyExist("Flags"));
50 int font_flags = font_desc->GetIntegerFor("Flags");
51 if (bold)
52 EXPECT_TRUE(font_flags & FXFONT_BOLD);
53 else
54 EXPECT_FALSE(font_flags & FXFONT_BOLD);
55 if (italic)
56 EXPECT_TRUE(font_flags & FXFONT_ITALIC);
57 else
58 EXPECT_FALSE(font_flags & FXFONT_ITALIC);
59 EXPECT_TRUE(font_flags & FXFONT_NONSYMBOLIC);
60 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
Nicolás Peña5f95f362017-09-28 13:00:45 +090061
62 CPDF_Array* fontBBox = font_desc->GetArrayFor("FontBBox");
63 ASSERT_TRUE(fontBBox);
64 EXPECT_EQ(4U, fontBBox->GetCount());
65 // Check that the coordinates are in the preferred order according to spec
66 // 1.7 Section 3.8.4
67 EXPECT_TRUE(fontBBox->GetIntegerAt(0) < fontBBox->GetIntegerAt(2));
68 EXPECT_TRUE(fontBBox->GetIntegerAt(1) < fontBBox->GetIntegerAt(3));
69
Nicolas Penad03ca422017-03-06 13:54:33 -050070 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
71 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
72 EXPECT_TRUE(font_desc->KeyExist("Descent"));
73 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
74 EXPECT_TRUE(font_desc->KeyExist("StemV"));
Ryan Harrison275e2602017-09-18 14:23:18 -040075 ByteString present("FontFile");
76 ByteString absent("FontFile2");
Nicolas Penad03ca422017-03-06 13:54:33 -050077 if (font_type == FPDF_FONT_TRUETYPE)
78 std::swap(present, absent);
79 EXPECT_TRUE(font_desc->KeyExist(present));
80 EXPECT_FALSE(font_desc->KeyExist(absent));
81
82 // Check that the font stream is the one that was provided
83 CPDF_Stream* font_stream = font_desc->GetStreamFor(present);
84 ASSERT_EQ(size, font_stream->GetRawSize());
85 uint8_t* stream_data = font_stream->GetRawData();
86 for (size_t j = 0; j < size; j++)
87 EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j;
88 }
89
90 void CheckCompositeFontWidths(CPDF_Array* widths_array,
91 CPDF_Font* typed_font) {
92 // Check that W array is in a format that conforms to PDF spec 1.7 section
93 // "Glyph Metrics in CIDFonts" (these checks are not
94 // implementation-specific).
95 EXPECT_GT(widths_array->GetCount(), 1U);
96 int num_cids_checked = 0;
97 int cur_cid = 0;
98 for (size_t idx = 0; idx < widths_array->GetCount(); idx++) {
99 int cid = widths_array->GetNumberAt(idx);
100 EXPECT_GE(cid, cur_cid);
101 ASSERT_FALSE(++idx == widths_array->GetCount());
102 CPDF_Object* next = widths_array->GetObjectAt(idx);
103 if (next->IsArray()) {
104 // We are in the c [w1 w2 ...] case
105 CPDF_Array* arr = next->AsArray();
106 int cnt = static_cast<int>(arr->GetCount());
107 size_t inner_idx = 0;
108 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
109 int width = arr->GetNumberAt(inner_idx++);
110 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
111 << cur_cid;
112 }
113 num_cids_checked += cnt;
114 continue;
115 }
116 // Otherwise, are in the c_first c_last w case.
117 ASSERT_TRUE(next->IsNumber());
118 int last_cid = next->AsNumber()->GetInteger();
119 ASSERT_FALSE(++idx == widths_array->GetCount());
120 int width = widths_array->GetNumberAt(idx);
121 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
122 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
123 << cur_cid;
124 }
125 num_cids_checked += last_cid - cid + 1;
126 }
127 // Make sure we have a good amount of cids described
128 EXPECT_GT(num_cids_checked, 900);
129 }
130 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
131
132 private:
133 CPDF_Document* cpdf_doc_;
134};
Tom Sepezd483eb42016-01-06 10:03:59 -0800135
etienneb7712c262016-04-26 08:13:45 -0700136namespace {
thestigdc7ec032016-11-21 15:32:52 -0800137
etienneb7712c262016-04-26 08:13:45 -0700138const char kExpectedPDF[] =
139 "%PDF-1.7\r\n"
140 "%\xA1\xB3\xC5\xD7\r\n"
141 "1 0 obj\r\n"
142 "<</Pages 2 0 R /Type/Catalog>>\r\n"
143 "endobj\r\n"
144 "2 0 obj\r\n"
145 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
146 "endobj\r\n"
147 "3 0 obj\r\n"
148 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
149 "endobj\r\n"
150 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400151 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
152 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400153 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700154 ">>\r\n"
155 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400156 "5 0 obj\r\n"
157 "<</BM/Normal/CA 1/ca 1>>\r\n"
158 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700159 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400160 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700161 "0000000000 65535 f\r\n"
162 "0000000017 00000 n\r\n"
163 "0000000066 00000 n\r\n"
164 "0000000122 00000 n\r\n"
165 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400166 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700167 "trailer\r\n"
168 "<<\r\n"
169 "/Root 1 0 R\r\n"
170 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400171 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700172 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400173 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700174 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800175
etienneb7712c262016-04-26 08:13:45 -0700176} // namespace
177
Tom Sepezd483eb42016-01-06 10:03:59 -0800178TEST_F(FPDFEditEmbeddertest, EmptyCreation) {
179 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700180 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800181 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400182 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800183 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800184 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700185
Nicolas Penad9d6c292017-06-06 16:12:10 -0400186 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
187 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700188 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800189}
thestigdc7ec032016-11-21 15:32:52 -0800190
191// Regression test for https://crbug.com/667012
192TEST_F(FPDFEditEmbeddertest, RasterizePDF) {
193 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
194
195 // Get the bitmap for the original document/
196 FPDF_BITMAP orig_bitmap;
197 {
198 EXPECT_TRUE(OpenDocument("black.pdf"));
199 FPDF_PAGE orig_page = LoadPage(0);
200 EXPECT_NE(nullptr, orig_page);
201 orig_bitmap = RenderPage(orig_page);
202 CompareBitmap(orig_bitmap, 612, 792, kAllBlackMd5sum);
203 UnloadPage(orig_page);
204 }
205
206 // Create a new document from |orig_bitmap| and save it.
207 {
208 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
209 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
210
211 // Add the bitmap to an image object and add the image object to the output
212 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700213 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
thestigdc7ec032016-11-21 15:32:52 -0800214 EXPECT_TRUE(FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap));
215 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 }
222 FPDFBitmap_Destroy(orig_bitmap);
223
224 // Get the generated content. Make sure it is at least as big as the original
225 // PDF.
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400226 EXPECT_GT(GetString().size(), 923U);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400227 TestAndCloseSaved(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800228}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500229
230TEST_F(FPDFEditEmbeddertest, AddPaths) {
231 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500232 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
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);
236 ASSERT_NE(nullptr, red_rect);
237 // Expect false when trying to set colors out of range
238 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
239 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
240
241 // Fill rectangle with red and insert to the page
242 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
243 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
244 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500245 FPDF_BITMAP page_bitmap = RenderPage(page);
246 CompareBitmap(page_bitmap, 612, 792, "66d02eaa6181e2c069ce2ea99beda497");
247 FPDFBitmap_Destroy(page_bitmap);
248
249 // Now add to that a green rectangle with some medium alpha
250 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
251 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200252
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200253 // Make sure the type of the rectangle is a path.
254 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
255
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200256 // Make sure we get back the same color we set previously.
257 unsigned int R;
258 unsigned int G;
259 unsigned int B;
260 unsigned int A;
261 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
262 EXPECT_EQ(0U, R);
263 EXPECT_EQ(255U, G);
264 EXPECT_EQ(0U, B);
265 EXPECT_EQ(128U, A);
266
Miklos Vajna12abfd02017-09-15 07:49:03 +0200267 // Make sure the path has 5 points (1 FXPT_TYPE::MoveTo and 4
268 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200269 ASSERT_EQ(5, FPDFPath_CountSegments(green_rect));
Miklos Vajna36eed872017-09-20 22:52:43 +0200270 // Verify actual coordinates.
271 FPDF_PATHSEGMENT segment = FPDFPath_GetPathSegment(green_rect, 0);
272 float x;
273 float y;
274 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
275 EXPECT_EQ(100, x);
276 EXPECT_EQ(100, y);
277 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
278 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
279 segment = FPDFPath_GetPathSegment(green_rect, 1);
280 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
281 EXPECT_EQ(100, x);
282 EXPECT_EQ(140, y);
283 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
284 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
285 segment = FPDFPath_GetPathSegment(green_rect, 2);
286 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
287 EXPECT_EQ(140, x);
288 EXPECT_EQ(140, y);
289 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
290 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
291 segment = FPDFPath_GetPathSegment(green_rect, 3);
292 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
293 EXPECT_EQ(140, x);
294 EXPECT_EQ(100, y);
295 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
296 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
297 segment = FPDFPath_GetPathSegment(green_rect, 4);
298 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
299 EXPECT_EQ(100, x);
300 EXPECT_EQ(100, y);
301 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
302 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200303
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500304 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
305 FPDFPage_InsertObject(page, green_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500306 page_bitmap = RenderPage(page);
307 CompareBitmap(page_bitmap, 612, 792, "7b0b87604594e773add528fae567a558");
308 FPDFBitmap_Destroy(page_bitmap);
309
310 // Add a black triangle.
311 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
312 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
313 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
314 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
315 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
316 EXPECT_TRUE(FPDFPath_Close(black_path));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200317
318 // Make sure the path has 3 points (1 FXPT_TYPE::MoveTo and 2
319 // FXPT_TYPE::LineTo).
Miklos Vajna0150a542017-09-21 21:46:56 +0200320 ASSERT_EQ(3, FPDFPath_CountSegments(black_path));
Miklos Vajna36eed872017-09-20 22:52:43 +0200321 // Verify actual coordinates.
322 segment = FPDFPath_GetPathSegment(black_path, 0);
323 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
324 EXPECT_EQ(400, x);
325 EXPECT_EQ(100, y);
326 EXPECT_EQ(FPDF_SEGMENT_MOVETO, FPDFPathSegment_GetType(segment));
327 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
328 segment = FPDFPath_GetPathSegment(black_path, 1);
329 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
330 EXPECT_EQ(400, x);
331 EXPECT_EQ(200, y);
332 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
333 EXPECT_FALSE(FPDFPathSegment_GetClose(segment));
334 segment = FPDFPath_GetPathSegment(black_path, 2);
335 EXPECT_TRUE(FPDFPathSegment_GetPoint(segment, &x, &y));
336 EXPECT_EQ(300, x);
337 EXPECT_EQ(100, y);
338 EXPECT_EQ(FPDF_SEGMENT_LINETO, FPDFPathSegment_GetType(segment));
339 EXPECT_TRUE(FPDFPathSegment_GetClose(segment));
340 // Make sure out of bounds index access fails properly.
341 EXPECT_EQ(nullptr, FPDFPath_GetPathSegment(black_path, 3));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200342
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500343 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500344 page_bitmap = RenderPage(page);
345 CompareBitmap(page_bitmap, 612, 792, "eadc8020a14dfcf091da2688733d8806");
346 FPDFBitmap_Destroy(page_bitmap);
347
348 // Now add a more complex blue path.
349 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
350 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
351 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
352 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
353 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
354 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
355 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
356 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
357 EXPECT_TRUE(FPDFPath_Close(blue_path));
358 FPDFPage_InsertObject(page, blue_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500359 page_bitmap = RenderPage(page);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400360 const char last_md5[] = "9823e1a21bd9b72b6a442ba4f12af946";
361 CompareBitmap(page_bitmap, 612, 792, last_md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500362 FPDFBitmap_Destroy(page_bitmap);
363
364 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400365 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500366 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500367 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500368
369 // Render the saved result
Nicolas Pena3ff54002017-07-05 11:55:35 -0400370 TestAndCloseSaved(612, 792, last_md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500371}
372
Miklos Vajna12abfd02017-09-15 07:49:03 +0200373TEST_F(FPDFEditEmbeddertest, PathsPoints) {
374 CreateNewDocument();
375 FPDF_PAGEOBJECT img = FPDFPageObj_NewImageObj(document_);
376 // This should fail gracefully, even if img is not a path.
Miklos Vajna0150a542017-09-21 21:46:56 +0200377 ASSERT_EQ(-1, FPDFPath_CountSegments(img));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200378
379 // This should fail gracefully, even if path is NULL.
Miklos Vajna0150a542017-09-21 21:46:56 +0200380 ASSERT_EQ(-1, FPDFPath_CountSegments(nullptr));
Miklos Vajna12abfd02017-09-15 07:49:03 +0200381
Miklos Vajna36eed872017-09-20 22:52:43 +0200382 // FPDFPath_GetPathSegment() with a non-path.
383 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(img, 0));
384 // FPDFPath_GetPathSegment() with a NULL path.
385 ASSERT_EQ(nullptr, FPDFPath_GetPathSegment(nullptr, 0));
386 float x;
387 float y;
388 // FPDFPathSegment_GetPoint() with a NULL segment.
389 EXPECT_FALSE(FPDFPathSegment_GetPoint(nullptr, &x, &y));
390
391 // FPDFPathSegment_GetType() with a NULL segment.
392 ASSERT_EQ(FPDF_SEGMENT_UNKNOWN, FPDFPathSegment_GetType(nullptr));
393
394 // FPDFPathSegment_GetClose() with a NULL segment.
395 EXPECT_FALSE(FPDFPathSegment_GetClose(nullptr));
396
Miklos Vajna12abfd02017-09-15 07:49:03 +0200397 FPDFPageObj_Destroy(img);
398}
399
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500400TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
401 // Load document with some text
402 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
403 FPDF_PAGE page = LoadPage(0);
404 EXPECT_NE(nullptr, page);
405
406 // Add an opaque rectangle on top of some of the text.
407 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
408 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
409 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
410 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500411
412 // Add a transparent triangle on top of other part of the text.
413 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
414 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
415 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
416 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
417 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
418 EXPECT_TRUE(FPDFPath_Close(black_path));
419 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500420
421 // Render and check the result. Text is slightly different on Mac.
422 FPDF_BITMAP bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400423#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700424 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500425#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400426 const char md5[] = "bc6e6eb50dda4695ba0fb4d04ed82ada";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400427#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700428 CompareBitmap(bitmap, 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500429 FPDFBitmap_Destroy(bitmap);
430 UnloadPage(page);
431}
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500432
wileyryae858aa42017-05-31 14:49:05 -0500433TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
434 // Load document with existing content
435 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
436 FPDF_PAGE page = LoadPage(0);
437 EXPECT_NE(nullptr, page);
438
439 // Add a transparent rectangle on top of the existing content
440 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
441 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
442 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
443 FPDFPage_InsertObject(page, red_rect2);
444
445 // Add an opaque rectangle on top of the existing content
446 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
447 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
448 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
449 FPDFPage_InsertObject(page, red_rect);
450
451 FPDF_BITMAP bitmap = RenderPage(page);
452 CompareBitmap(bitmap, 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
453 FPDFBitmap_Destroy(bitmap);
454 EXPECT_TRUE(FPDFPage_GenerateContent(page));
455
456 // Now save the result, closing the page and document
457 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400458 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -0500459
Nicolas Pena3ff54002017-07-05 11:55:35 -0400460 // Render the saved result without closing the page and document
461 TestSaved(612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500462
463 ClearString();
464 // Add another opaque rectangle on top of the existing content
465 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
466 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
467 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400468 FPDFPage_InsertObject(m_SavedPage, green_rect);
wileyryae858aa42017-05-31 14:49:05 -0500469
470 // Add another transparent rectangle on top of existing content
471 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
472 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
473 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400474 FPDFPage_InsertObject(m_SavedPage, green_rect2);
475 FPDF_BITMAP new_bitmap = RenderPageWithFlags(m_SavedPage, m_SavedForm, 0);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400476 const char last_md5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
477 CompareBitmap(new_bitmap, 612, 792, last_md5);
wileyryae858aa42017-05-31 14:49:05 -0500478 FPDFBitmap_Destroy(new_bitmap);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400479 EXPECT_TRUE(FPDFPage_GenerateContent(m_SavedPage));
wileyryae858aa42017-05-31 14:49:05 -0500480
481 // Now save the result, closing the page and document
Nicolas Pena3ff54002017-07-05 11:55:35 -0400482 EXPECT_TRUE(FPDF_SaveAsCopy(m_SavedDocument, this, 0));
483 CloseSaved();
wileyryae858aa42017-05-31 14:49:05 -0500484
485 // Render the saved result
Nicolas Pena3ff54002017-07-05 11:55:35 -0400486 TestAndCloseSaved(612, 792, last_md5);
wileyryae858aa42017-05-31 14:49:05 -0500487}
488
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500489TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
490 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500491 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500492
493 // Add a large stroked rectangle (fill color should not affect it).
494 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
495 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
496 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
497 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
498 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
499 FPDFPage_InsertObject(page, rect);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500500 FPDF_BITMAP page_bitmap = RenderPage(page);
501 CompareBitmap(page_bitmap, 612, 792, "64bd31f862a89e0a9e505a5af6efd506");
502 FPDFBitmap_Destroy(page_bitmap);
503
504 // Add crossed-checkmark
505 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
506 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
507 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
508 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
509 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
510 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
511 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
512 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
513 FPDFPage_InsertObject(page, check);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500514 page_bitmap = RenderPage(page);
515 CompareBitmap(page_bitmap, 612, 792, "4b6f3b9d25c4e194821217d5016c3724");
516 FPDFBitmap_Destroy(page_bitmap);
517
518 // Add stroked and filled oval-ish path.
519 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
520 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
521 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
522 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
523 EXPECT_TRUE(FPDFPath_Close(path));
524 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
525 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
526 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
527 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
528 FPDFPage_InsertObject(page, path);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500529 page_bitmap = RenderPage(page);
530 CompareBitmap(page_bitmap, 612, 792, "ff3e6a22326754944cc6e56609acd73b");
531 FPDFBitmap_Destroy(page_bitmap);
532 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500533}
Nicolas Pena49058402017-02-14 18:26:20 -0500534
535TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
536 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500537 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -0500538
539 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400540 FPDF_PAGEOBJECT text_object1 =
541 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
542 EXPECT_TRUE(text_object1);
543 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
544 GetFPDFWideString(L"I'm at the bottom of the page");
545 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
546 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
547 FPDFPage_InsertObject(page, text_object1);
Nicolas Pena49058402017-02-14 18:26:20 -0500548 FPDF_BITMAP page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400549#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700550 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -0500551#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400552 const char md5[] = "7a35771853a1cbba38f6775807878625";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400553#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700554 CompareBitmap(page_bitmap, 612, 792, md5);
Nicolas Pena49058402017-02-14 18:26:20 -0500555 FPDFBitmap_Destroy(page_bitmap);
556
557 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -0400558 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500559 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400560 EXPECT_TRUE(text_object2);
561 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
562 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
563 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
564 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
565 FPDFPage_InsertObject(page, text_object2);
Nicolas Pena49058402017-02-14 18:26:20 -0500566 page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400567#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700568 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Dan Sinclair698aed72017-09-26 16:24:49 -0400569#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400570 const char md5_2[] = "b231b329a4b566fb9b42bfc15fe59bb7";
Nicolas Pena49058402017-02-14 18:26:20 -0500571#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400572 const char md5_2[] = "f85fae151851436072b7b3c6703e506a";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400573#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700574 CompareBitmap(page_bitmap, 612, 792, md5_2);
Nicolas Pena49058402017-02-14 18:26:20 -0500575 FPDFBitmap_Destroy(page_bitmap);
576
577 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -0400578 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500579 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400580 EXPECT_TRUE(text_object3);
581 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
582 GetFPDFWideString(L"Can you read me? <:)>");
583 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
584 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
585 FPDFPage_InsertObject(page, text_object3);
Nicolas Pena49058402017-02-14 18:26:20 -0500586 page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400587#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700588 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Dan Sinclair698aed72017-09-26 16:24:49 -0400589#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400590 const char md5_3[] = "ba874b3b137f984510c4e287ed4ba7ae";
Nicolas Pena49058402017-02-14 18:26:20 -0500591#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400592 const char md5_3[] = "c5aed6a8ef05558c8c47d58c87cbcb46";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400593#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700594 CompareBitmap(page_bitmap, 612, 792, md5_3);
Nicolas Pena49058402017-02-14 18:26:20 -0500595 FPDFBitmap_Destroy(page_bitmap);
596
597 // TODO(npm): Why are there issues with text rotated by 90 degrees?
598 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
599 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -0500600}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500601
Nicolas Pena603a31d2017-06-14 11:41:18 -0400602TEST_F(FPDFEditEmbeddertest, GraphicsData) {
603 // New page
604 std::unique_ptr<void, FPDFPageDeleter> page(
605 FPDFPage_New(CreateNewDocument(), 0, 612, 792));
606
607 // Create a rect with nontrivial graphics
608 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
609 FPDFPageObj_SetBlendMode(rect1, "Color");
610 FPDFPage_InsertObject(page.get(), rect1);
611 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
612
613 // Check that the ExtGState was created
614 CPDF_Page* the_page = CPDFPageFromFPDFPage(page.get());
615 CPDF_Dictionary* graphics_dict =
616 the_page->m_pResources->GetDictFor("ExtGState");
617 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400618 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400619
620 // Add a text object causing no change to the graphics dictionary
621 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
622 // Only alpha, the last component, matters for the graphics dictionary. And
623 // the default value is 255.
624 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
625 FPDFPage_InsertObject(page.get(), text1);
626 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400627 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400628
629 // Add a text object increasing the size of the graphics dictionary
630 FPDF_PAGEOBJECT text2 =
631 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
632 FPDFPage_InsertObject(page.get(), text2);
633 FPDFPageObj_SetBlendMode(text2, "Darken");
634 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
635 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400636 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400637
638 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -0400639 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -0400640 FPDFPageObj_SetBlendMode(path, "Darken");
641 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
642 FPDFPage_InsertObject(page.get(), path);
643 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400644 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400645
646 // Add a rect increasing the size of the graphics dictionary
647 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
648 FPDFPageObj_SetBlendMode(rect2, "Darken");
649 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
650 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
651 FPDFPage_InsertObject(page.get(), rect2);
652 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400653 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400654}
655
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500656TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
657 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500658 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500659
660 // Add a red rectangle with some non-default alpha
661 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
662 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
663 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
664 FPDFPage_InsertObject(page, rect);
665 EXPECT_TRUE(FPDFPage_GenerateContent(page));
666
667 // Check the ExtGState
668 CPDF_Page* the_page = CPDFPageFromFPDFPage(page);
669 CPDF_Dictionary* graphics_dict =
670 the_page->m_pResources->GetDictFor("ExtGState");
671 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400672 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500673
674 // Check the bitmap
675 FPDF_BITMAP page_bitmap = RenderPage(page);
676 CompareBitmap(page_bitmap, 612, 792, "5384da3406d62360ffb5cac4476fff1c");
677 FPDFBitmap_Destroy(page_bitmap);
678
679 // Never mind, my new favorite color is blue, increase alpha
680 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
681 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400682 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500683
684 // Check that bitmap displays changed content
685 page_bitmap = RenderPage(page);
686 CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c");
687 FPDFBitmap_Destroy(page_bitmap);
688
689 // And now generate, without changes
690 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400691 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500692 page_bitmap = RenderPage(page);
693 CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c");
694 FPDFBitmap_Destroy(page_bitmap);
695
696 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400697 FPDF_PAGEOBJECT text_object =
698 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
699 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
700 GetFPDFWideString(L"Something something #text# something");
701 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
702 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
703 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500704 EXPECT_TRUE(FPDFPage_GenerateContent(page));
705 CPDF_Dictionary* font_dict = the_page->m_pResources->GetDictFor("Font");
706 ASSERT_TRUE(font_dict);
707 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
708
709 // Generate yet again, check dicts are reasonably sized
710 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400711 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500712 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
713 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500714}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500715
Nicolas Penad03ca422017-03-06 13:54:33 -0500716TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
717 CreateNewDocument();
718 // TODO(npm): use other fonts after disallowing loading any font as any type
719 const CPDF_Font* stock_font =
720 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700721 const uint8_t* data = stock_font->GetFont()->GetFontData();
722 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400723 std::unique_ptr<void, FPDFFontDeleter> font(
724 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
725 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400726 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500727 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500728
Nicolas Penad03ca422017-03-06 13:54:33 -0500729 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -0500730 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
731 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
732 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
733 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
734 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
735 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -0500736 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
737
Nicolas Penabe90aae2017-02-27 10:41:41 -0500738 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -0500739 ASSERT_TRUE(widths_array);
740 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500741 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -0500742 EXPECT_EQ(569, widths_array->GetNumberAt(11));
743 EXPECT_EQ(500, widths_array->GetNumberAt(223));
744 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
745}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500746
Nicolas Penad03ca422017-03-06 13:54:33 -0500747TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
748 CreateNewDocument();
749 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700750 const uint8_t* data = stock_font->GetFont()->GetFontData();
751 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400752 std::unique_ptr<void, FPDFFontDeleter> font(
753 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
754 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400755 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500756 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500757
Nicolas Penad03ca422017-03-06 13:54:33 -0500758 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
759 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
760 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
761 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
762 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
763 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
764 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
765 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -0500766
Nicolas Penad03ca422017-03-06 13:54:33 -0500767 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
768 ASSERT_TRUE(widths_array);
769 ASSERT_EQ(224U, widths_array->GetCount());
770 EXPECT_EQ(600, widths_array->GetNumberAt(33));
771 EXPECT_EQ(600, widths_array->GetNumberAt(74));
772 EXPECT_EQ(600, widths_array->GetNumberAt(223));
773 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
774}
775
776TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
777 CreateNewDocument();
778 const CPDF_Font* stock_font =
779 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700780 const uint8_t* data = stock_font->GetFont()->GetFontData();
781 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400782 std::unique_ptr<void, FPDFFontDeleter> font(
783 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
784 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400785 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500786 EXPECT_TRUE(typed_font->IsCIDFont());
787
788 // Check font dictionary entries
789 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
790 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
791 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
792 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
793 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
794 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
795 ASSERT_TRUE(descendant_array);
796 EXPECT_EQ(1U, descendant_array->GetCount());
797
798 // Check the CIDFontDict
799 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
800 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
801 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
802 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
803 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
804 ASSERT_TRUE(cidinfo_dict);
805 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
806 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
807 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
808 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
809
810 // Check widths
811 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
812 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -0400813 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -0500814 CheckCompositeFontWidths(widths_array, typed_font);
815}
816
817TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
818 CreateNewDocument();
819 const CPDF_Font* stock_font =
820 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700821 const uint8_t* data = stock_font->GetFont()->GetFontData();
822 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -0500823
Nicolas Penab3161852017-05-02 14:12:50 -0400824 std::unique_ptr<void, FPDFFontDeleter> font(
825 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
826 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400827 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500828 EXPECT_TRUE(typed_font->IsCIDFont());
829
830 // Check font dictionary entries
831 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
832 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
833 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
834 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
835 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
836 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
837 ASSERT_TRUE(descendant_array);
838 EXPECT_EQ(1U, descendant_array->GetCount());
839
840 // Check the CIDFontDict
841 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
842 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
843 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
844 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
845 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
846 ASSERT_TRUE(cidinfo_dict);
847 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
848 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
849 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
850 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
851 data);
852
853 // Check widths
854 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
855 ASSERT_TRUE(widths_array);
856 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -0500857}
rbpotterce8e51e2017-04-28 12:42:47 -0700858
859TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
860 // Load document with a -90 degree rotation
861 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
862 FPDF_PAGE page = LoadPage(0);
863 EXPECT_NE(nullptr, page);
864
865 EXPECT_EQ(3, FPDFPage_GetRotation(page));
866 UnloadPage(page);
867}
Nicolas Penab3161852017-05-02 14:12:50 -0400868
869TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
870 // Start with a blank page
871 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
872 {
873 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700874 const uint8_t* data = stock_font->GetFont()->GetFontData();
875 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400876 std::unique_ptr<void, FPDFFontDeleter> font(
877 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
878 ASSERT_TRUE(font.get());
879
880 // Add some text to the page
881 FPDF_PAGEOBJECT text_object =
882 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
883 EXPECT_TRUE(text_object);
884 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
885 GetFPDFWideString(L"I am testing my loaded font, WEE.");
886 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
887 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
888 FPDFPage_InsertObject(page, text_object);
Nicolas Penab3161852017-05-02 14:12:50 -0400889 FPDF_BITMAP page_bitmap = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400890#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -0400891 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
892#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400893 const char md5[] = "1722c6a9deed953d730de9cd13dcbd55";
Dan Sinclair698aed72017-09-26 16:24:49 -0400894#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -0400895 CompareBitmap(page_bitmap, 612, 792, md5);
896 FPDFBitmap_Destroy(page_bitmap);
897
898 // Add some more text, same font
899 FPDF_PAGEOBJECT text_object2 =
900 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
901 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
902 GetFPDFWideString(L"Bigger font size");
903 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
904 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
905 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -0400906 }
907 FPDF_BITMAP page_bitmap2 = RenderPage(page);
Dan Sinclair698aed72017-09-26 16:24:49 -0400908#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -0400909 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
910#else
Nicolas Pena00c3cfd2017-07-10 17:29:54 -0400911 const char md5_2[] = "9d7885072058f6c3e68ecaf32e917f30";
Dan Sinclair698aed72017-09-26 16:24:49 -0400912#endif // _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
Nicolas Penab3161852017-05-02 14:12:50 -0400913 CompareBitmap(page_bitmap2, 612, 792, md5_2);
914 FPDFBitmap_Destroy(page_bitmap2);
915
Nicolas Pena207b7272017-05-26 17:37:06 -0400916 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -0400917 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
918 FPDF_ClosePage(page);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400919 TestAndCloseSaved(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -0400920}
Nicolas Penaf45ade32017-05-03 10:23:49 -0400921
Jane Liueda65252017-06-07 11:31:27 -0400922TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
923 // Open a file with one annotation and load its first page.
924 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
925 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
926 ASSERT_TRUE(page);
927
928 // Add an underline annotation to the page without specifying its rectangle.
Jane Liud60e9ad2017-06-26 11:28:36 -0400929 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE);
930 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -0400931
932 // FPDFPage_TransformAnnots() should run without errors when modifying
933 // annotation rectangles.
934 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
935
Jane Liud60e9ad2017-06-26 11:28:36 -0400936 FPDFPage_CloseAnnot(annot);
Jane Liueda65252017-06-07 11:31:27 -0400937 UnloadPage(page);
938}
939
Nicolas Penaf45ade32017-05-03 10:23:49 -0400940// TODO(npm): Add tests using Japanese fonts in other OS.
Dan Sinclair698aed72017-09-26 16:24:49 -0400941#if _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Penaf45ade32017-05-03 10:23:49 -0400942TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
943 // Start with a blank page
944 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
945 CFX_Font CIDfont;
946 {
947 // First, get the data from the font
948 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
949 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
950 const uint8_t* data = CIDfont.GetFontData();
951 const uint32_t size = CIDfont.GetSize();
952
953 // Load the data into a FPDF_Font.
954 std::unique_ptr<void, FPDFFontDeleter> font(
955 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
956 ASSERT_TRUE(font.get());
957
958 // Add some text to the page
959 FPDF_PAGEOBJECT text_object =
960 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
961 ASSERT_TRUE(text_object);
962 std::wstring wstr = L"ABCDEFGhijklmnop.";
963 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
964 GetFPDFWideString(wstr);
965 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
966 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
967 FPDFPage_InsertObject(page, text_object);
968
969 // And add some Japanese characters
970 FPDF_PAGEOBJECT text_object2 =
971 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
972 ASSERT_TRUE(text_object2);
973 std::wstring wstr2 =
974 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
975 L"\u756A";
976 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
977 GetFPDFWideString(wstr2);
978 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
979 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
980 FPDFPage_InsertObject(page, text_object2);
981 }
982
Nicolas Pena207b7272017-05-26 17:37:06 -0400983 // Check that the text renders properly.
Nicolas Penaf45ade32017-05-03 10:23:49 -0400984 FPDF_BITMAP page_bitmap = RenderPage(page);
985 const char md5[] = "2bc6c1aaa2252e73246a75775ccf38c2";
986 CompareBitmap(page_bitmap, 612, 792, md5);
987 FPDFBitmap_Destroy(page_bitmap);
988
989 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -0400990 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -0400991 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
992 FPDF_ClosePage(page);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400993 TestAndCloseSaved(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -0400994}
Dan Sinclair698aed72017-09-26 16:24:49 -0400995#endif // _FX_PLATFORM_ == _FX_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400996
997TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400998 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400999 {
1000 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
1001 FPDF_PAGE page = LoadPage(0);
1002 ASSERT_NE(nullptr, page);
1003
1004 // Now add a more complex blue path.
1005 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
1006 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
1007 // TODO(npm): stroking will cause the MD5s to differ.
1008 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
1009 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
1010 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
1011 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
1012 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
1013 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
1014 EXPECT_TRUE(FPDFPath_Close(green_path));
1015 FPDFPage_InsertObject(page, green_path);
1016 FPDF_BITMAP page_bitmap = RenderPage(page);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -04001017 CompareBitmap(page_bitmap, 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001018 FPDFBitmap_Destroy(page_bitmap);
1019
1020 // Now save the result, closing the page and document
1021 EXPECT_TRUE(FPDFPage_GenerateContent(page));
1022 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
1023 UnloadPage(page);
1024 }
Nicolas Pena3ff54002017-07-05 11:55:35 -04001025 TestAndCloseSaved(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -04001026}
Jane Liu28fb7ba2017-08-02 21:45:57 -04001027
1028TEST_F(FPDFEditEmbeddertest, ExtractImageBitmap) {
1029 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1030 FPDF_PAGE page = LoadPage(0);
1031 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001032 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu28fb7ba2017-08-02 21:45:57 -04001033
1034 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1035 EXPECT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1036 EXPECT_FALSE(FPDFImageObj_GetBitmap(obj));
1037
1038 obj = FPDFPage_GetObject(page, 33);
1039 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1040 FPDF_BITMAP bitmap = FPDFImageObj_GetBitmap(obj);
1041 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1042 CompareBitmap(bitmap, 109, 88, "d65e98d968d196abf13f78aec655ffae");
1043 FPDFBitmap_Destroy(bitmap);
1044
1045 obj = FPDFPage_GetObject(page, 34);
1046 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1047 bitmap = FPDFImageObj_GetBitmap(obj);
1048 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1049 CompareBitmap(bitmap, 103, 75, "1287711c84dbef767c435d11697661d6");
1050 FPDFBitmap_Destroy(bitmap);
1051
1052 obj = FPDFPage_GetObject(page, 35);
1053 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1054 bitmap = FPDFImageObj_GetBitmap(obj);
1055 EXPECT_EQ(FPDFBitmap_Gray, FPDFBitmap_GetFormat(bitmap));
1056 CompareBitmap(bitmap, 92, 68, "9c6d76cb1e37ef8514f9455d759391f3");
1057 FPDFBitmap_Destroy(bitmap);
1058
1059 obj = FPDFPage_GetObject(page, 36);
1060 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1061 bitmap = FPDFImageObj_GetBitmap(obj);
1062 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1063 CompareBitmap(bitmap, 79, 60, "15cb6a49a2e354ed0e9f45dd34e3da1a");
1064 FPDFBitmap_Destroy(bitmap);
1065
1066 obj = FPDFPage_GetObject(page, 37);
1067 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1068 bitmap = FPDFImageObj_GetBitmap(obj);
1069 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1070 CompareBitmap(bitmap, 126, 106, "be5a64ba7890d2657522af6524118534");
1071 FPDFBitmap_Destroy(bitmap);
1072
1073 obj = FPDFPage_GetObject(page, 38);
1074 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1075 bitmap = FPDFImageObj_GetBitmap(obj);
1076 EXPECT_EQ(FPDFBitmap_BGR, FPDFBitmap_GetFormat(bitmap));
1077 CompareBitmap(bitmap, 194, 119, "f9e24207ee1bc0db6c543d33a5f12ec5");
1078 FPDFBitmap_Destroy(bitmap);
1079 UnloadPage(page);
1080}
Jane Liu548334e2017-08-03 16:33:40 -04001081
1082TEST_F(FPDFEditEmbeddertest, GetImageData) {
1083 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1084 FPDF_PAGE page = LoadPage(0);
1085 ASSERT_TRUE(page);
Miklos Vajna92627612017-09-25 12:59:29 +02001086 ASSERT_EQ(39, FPDFPage_CountObjects(page));
Jane Liu548334e2017-08-03 16:33:40 -04001087
1088 // Retrieve an image object with flate-encoded data stream.
1089 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 33);
1090 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1091
1092 // Check that the raw image data has the correct length and hash value.
1093 unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1094 std::vector<char> buf(len);
1095 EXPECT_EQ(4091u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1096 EXPECT_EQ("f73802327d2e88e890f653961bcda81a",
1097 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1098
1099 // Check that the decoded image data has the correct length and hash value.
1100 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1101 buf.clear();
1102 buf.resize(len);
1103 EXPECT_EQ(28776u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1104 EXPECT_EQ("cb3637934bb3b95a6e4ae1ea9eb9e56e",
1105 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1106
1107 // Retrieve an image obejct with DCTDecode-encoded data stream.
1108 obj = FPDFPage_GetObject(page, 37);
1109 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1110
1111 // Check that the raw image data has the correct length and hash value.
1112 len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
1113 buf.clear();
1114 buf.resize(len);
1115 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataRaw(obj, buf.data(), len));
1116 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1117 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1118
1119 // Check that the decoded image data has the correct length and hash value,
1120 // which should be the same as those of the raw data, since this image is
1121 // encoded by a single DCTDecode filter and decoding is a noop.
1122 len = FPDFImageObj_GetImageDataDecoded(obj, nullptr, 0);
1123 buf.clear();
1124 buf.resize(len);
1125 EXPECT_EQ(4370u, FPDFImageObj_GetImageDataDecoded(obj, buf.data(), len));
1126 EXPECT_EQ("6aae1f3710335023a9e12191be66b64b",
1127 GenerateMD5Base16(reinterpret_cast<uint8_t*>(buf.data()), len));
1128
1129 UnloadPage(page);
1130}
Jane Liu2e5f0ae2017-08-08 15:23:27 -04001131
1132TEST_F(FPDFEditEmbeddertest, DestroyPageObject) {
1133 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
1134 ASSERT_TRUE(rect);
1135
1136 // There should be no memory leaks with a call to FPDFPageObj_Destroy().
1137 FPDFPageObj_Destroy(rect);
1138}
Jane Liube63ab92017-08-09 14:09:34 -04001139
1140TEST_F(FPDFEditEmbeddertest, GetImageFilters) {
1141 EXPECT_TRUE(OpenDocument("embedded_images.pdf"));
1142 FPDF_PAGE page = LoadPage(0);
1143 ASSERT_TRUE(page);
1144
1145 // Verify that retrieving the filter of a non-image object would fail.
1146 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 32);
1147 ASSERT_NE(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1148 ASSERT_EQ(0, FPDFImageObj_GetImageFilterCount(obj));
1149 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0));
1150
1151 // Verify the returned filter string for an image object with a single filter.
1152 obj = FPDFPage_GetObject(page, 33);
1153 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1154 ASSERT_EQ(1, FPDFImageObj_GetImageFilterCount(obj));
1155 unsigned long len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1156 std::vector<char> buf(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001157 static constexpr char kFlateDecode[] = "FlateDecode";
1158 EXPECT_EQ(sizeof(kFlateDecode),
1159 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1160 EXPECT_STREQ(kFlateDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001161 EXPECT_EQ(0u, FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0));
1162
1163 // Verify all the filters for an image object with a list of filters.
1164 obj = FPDFPage_GetObject(page, 38);
1165 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1166 ASSERT_EQ(2, FPDFImageObj_GetImageFilterCount(obj));
1167 len = FPDFImageObj_GetImageFilter(obj, 0, nullptr, 0);
1168 buf.clear();
1169 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001170 static constexpr char kASCIIHexDecode[] = "ASCIIHexDecode";
1171 EXPECT_EQ(sizeof(kASCIIHexDecode),
1172 FPDFImageObj_GetImageFilter(obj, 0, buf.data(), len));
1173 EXPECT_STREQ(kASCIIHexDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001174
1175 len = FPDFImageObj_GetImageFilter(obj, 1, nullptr, 0);
1176 buf.clear();
1177 buf.resize(len);
Lei Zhang0733a1b2017-08-31 12:36:31 -07001178 static constexpr char kDCTDecode[] = "DCTDecode";
1179 EXPECT_EQ(sizeof(kDCTDecode),
1180 FPDFImageObj_GetImageFilter(obj, 1, buf.data(), len));
1181 EXPECT_STREQ(kDCTDecode, buf.data());
Jane Liube63ab92017-08-09 14:09:34 -04001182
1183 UnloadPage(page);
1184}
Jane Liuca898292017-08-16 11:25:35 -04001185
1186TEST_F(FPDFEditEmbeddertest, GetImageMetadata) {
1187 ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
1188 FPDF_PAGE page = LoadPage(0);
1189 ASSERT_TRUE(page);
1190
1191 // Check that getting the metadata of a null object would fail.
1192 FPDF_IMAGEOBJ_METADATA metadata;
1193 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(nullptr, page, &metadata));
1194
1195 // Check that receiving the metadata with a null metadata object would fail.
1196 FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, 35);
1197 EXPECT_FALSE(FPDFImageObj_GetImageMetadata(obj, page, nullptr));
1198
1199 // Check that when retrieving an image object's metadata without passing in
1200 // |page|, all values are correct, with the last two being default values.
1201 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1202 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, nullptr, &metadata));
1203 EXPECT_EQ(92u, metadata.width);
1204 EXPECT_EQ(68u, metadata.height);
1205 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1206 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1207 EXPECT_EQ(0u, metadata.bits_per_pixel);
1208 EXPECT_EQ(FPDF_COLORSPACE_UNKNOWN, metadata.colorspace);
1209
1210 // Verify the metadata of a bitmap image with indexed colorspace.
1211 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
1212 EXPECT_EQ(92u, metadata.width);
1213 EXPECT_EQ(68u, metadata.height);
1214 EXPECT_NEAR(96.000000, metadata.horizontal_dpi, 0.001);
1215 EXPECT_NEAR(96.000000, metadata.vertical_dpi, 0.001);
1216 EXPECT_EQ(1u, metadata.bits_per_pixel);
1217 EXPECT_EQ(FPDF_COLORSPACE_INDEXED, metadata.colorspace);
1218
1219 // Verify the metadata of an image with RGB colorspace.
1220 obj = FPDFPage_GetObject(page, 37);
1221 ASSERT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(obj));
1222 ASSERT_TRUE(FPDFImageObj_GetImageMetadata(obj, page, &metadata));
1223 EXPECT_EQ(126u, metadata.width);
1224 EXPECT_EQ(106u, metadata.height);
1225 EXPECT_NEAR(162.173752, metadata.horizontal_dpi, 0.001);
1226 EXPECT_NEAR(162.555878, metadata.vertical_dpi, 0.001);
1227 EXPECT_EQ(24u, metadata.bits_per_pixel);
1228 EXPECT_EQ(FPDF_COLORSPACE_DEVICERGB, metadata.colorspace);
1229
1230 UnloadPage(page);
1231}