blob: a461f3744033f6eebc76363e16e643da444f426b [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>
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05008
Nicolas Penabe90aae2017-02-27 10:41:41 -05009#include "core/fpdfapi/font/cpdf_font.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050010#include "core/fpdfapi/page/cpdf_page.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050011#include "core/fpdfapi/parser/cpdf_array.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050012#include "core/fpdfapi/parser/cpdf_dictionary.h"
Nicolas Penad03ca422017-03-06 13:54:33 -050013#include "core/fpdfapi/parser/cpdf_number.h"
Nicolas Penabe90aae2017-02-27 10:41:41 -050014#include "core/fpdfapi/parser/cpdf_stream.h"
Nicolas Pena0fc185e2017-02-08 12:13:20 -050015#include "core/fxcrt/fx_system.h"
Nicolas Penaa4ad01f2017-02-15 16:26:48 -050016#include "fpdfsdk/fsdk_define.h"
Nicolas Penab3161852017-05-02 14:12:50 -040017#include "public/cpp/fpdf_deleters.h"
Jane Liueda65252017-06-07 11:31:27 -040018#include "public/fpdf_annot.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080019#include "public/fpdf_edit.h"
20#include "public/fpdfview.h"
21#include "testing/embedder_test.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080022#include "testing/gmock/include/gmock/gmock-matchers.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080023#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez0aec19b2016-01-07 12:22:44 -080024#include "testing/test_support.h"
Tom Sepezd483eb42016-01-06 10:03:59 -080025
Nicolas Pena3ff54002017-07-05 11:55:35 -040026class FPDFEditEmbeddertest : public EmbedderTest {
Nicolas Penad03ca422017-03-06 13:54:33 -050027 protected:
28 FPDF_DOCUMENT CreateNewDocument() {
29 document_ = FPDF_CreateNewDocument();
Tom Sepez41066c12017-05-18 09:28:49 -070030 cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_);
Nicolas Penad03ca422017-03-06 13:54:33 -050031 return document_;
32 }
33
34 void CheckFontDescriptor(CPDF_Dictionary* font_dict,
35 int font_type,
36 bool bold,
37 bool italic,
38 uint32_t size,
39 const uint8_t* data) {
40 CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor");
41 ASSERT_TRUE(font_desc);
42 EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type"));
43 EXPECT_EQ(font_dict->GetStringFor("BaseFont"),
44 font_desc->GetStringFor("FontName"));
45
46 // Check that the font descriptor has the required keys according to spec
47 // 1.7 Table 5.19
48 ASSERT_TRUE(font_desc->KeyExist("Flags"));
49 int font_flags = font_desc->GetIntegerFor("Flags");
50 if (bold)
51 EXPECT_TRUE(font_flags & FXFONT_BOLD);
52 else
53 EXPECT_FALSE(font_flags & FXFONT_BOLD);
54 if (italic)
55 EXPECT_TRUE(font_flags & FXFONT_ITALIC);
56 else
57 EXPECT_FALSE(font_flags & FXFONT_ITALIC);
58 EXPECT_TRUE(font_flags & FXFONT_NONSYMBOLIC);
59 ASSERT_TRUE(font_desc->KeyExist("FontBBox"));
60 EXPECT_EQ(4U, font_desc->GetArrayFor("FontBBox")->GetCount());
61 EXPECT_TRUE(font_desc->KeyExist("ItalicAngle"));
62 EXPECT_TRUE(font_desc->KeyExist("Ascent"));
63 EXPECT_TRUE(font_desc->KeyExist("Descent"));
64 EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
65 EXPECT_TRUE(font_desc->KeyExist("StemV"));
66 CFX_ByteString present("FontFile");
67 CFX_ByteString absent("FontFile2");
68 if (font_type == FPDF_FONT_TRUETYPE)
69 std::swap(present, absent);
70 EXPECT_TRUE(font_desc->KeyExist(present));
71 EXPECT_FALSE(font_desc->KeyExist(absent));
72
73 // Check that the font stream is the one that was provided
74 CPDF_Stream* font_stream = font_desc->GetStreamFor(present);
75 ASSERT_EQ(size, font_stream->GetRawSize());
76 uint8_t* stream_data = font_stream->GetRawData();
77 for (size_t j = 0; j < size; j++)
78 EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j;
79 }
80
81 void CheckCompositeFontWidths(CPDF_Array* widths_array,
82 CPDF_Font* typed_font) {
83 // Check that W array is in a format that conforms to PDF spec 1.7 section
84 // "Glyph Metrics in CIDFonts" (these checks are not
85 // implementation-specific).
86 EXPECT_GT(widths_array->GetCount(), 1U);
87 int num_cids_checked = 0;
88 int cur_cid = 0;
89 for (size_t idx = 0; idx < widths_array->GetCount(); idx++) {
90 int cid = widths_array->GetNumberAt(idx);
91 EXPECT_GE(cid, cur_cid);
92 ASSERT_FALSE(++idx == widths_array->GetCount());
93 CPDF_Object* next = widths_array->GetObjectAt(idx);
94 if (next->IsArray()) {
95 // We are in the c [w1 w2 ...] case
96 CPDF_Array* arr = next->AsArray();
97 int cnt = static_cast<int>(arr->GetCount());
98 size_t inner_idx = 0;
99 for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) {
100 int width = arr->GetNumberAt(inner_idx++);
101 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
102 << cur_cid;
103 }
104 num_cids_checked += cnt;
105 continue;
106 }
107 // Otherwise, are in the c_first c_last w case.
108 ASSERT_TRUE(next->IsNumber());
109 int last_cid = next->AsNumber()->GetInteger();
110 ASSERT_FALSE(++idx == widths_array->GetCount());
111 int width = widths_array->GetNumberAt(idx);
112 for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) {
113 EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid "
114 << cur_cid;
115 }
116 num_cids_checked += last_cid - cid + 1;
117 }
118 // Make sure we have a good amount of cids described
119 EXPECT_GT(num_cids_checked, 900);
120 }
121 CPDF_Document* cpdf_doc() { return cpdf_doc_; }
122
123 private:
124 CPDF_Document* cpdf_doc_;
125};
Tom Sepezd483eb42016-01-06 10:03:59 -0800126
etienneb7712c262016-04-26 08:13:45 -0700127namespace {
thestigdc7ec032016-11-21 15:32:52 -0800128
etienneb7712c262016-04-26 08:13:45 -0700129const char kExpectedPDF[] =
130 "%PDF-1.7\r\n"
131 "%\xA1\xB3\xC5\xD7\r\n"
132 "1 0 obj\r\n"
133 "<</Pages 2 0 R /Type/Catalog>>\r\n"
134 "endobj\r\n"
135 "2 0 obj\r\n"
136 "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n"
137 "endobj\r\n"
138 "3 0 obj\r\n"
139 "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n"
140 "endobj\r\n"
141 "4 0 obj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400142 "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R "
143 "/Resources<</ExtGState<</FXE1 5 0 R >>>>"
Nicolas Penad9d6c292017-06-06 16:12:10 -0400144 "/Rotate 0/Type/Page"
etienneb7712c262016-04-26 08:13:45 -0700145 ">>\r\n"
146 "endobj\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400147 "5 0 obj\r\n"
148 "<</BM/Normal/CA 1/ca 1>>\r\n"
149 "endobj\r\n"
etienneb7712c262016-04-26 08:13:45 -0700150 "xref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400151 "0 6\r\n"
etienneb7712c262016-04-26 08:13:45 -0700152 "0000000000 65535 f\r\n"
153 "0000000017 00000 n\r\n"
154 "0000000066 00000 n\r\n"
155 "0000000122 00000 n\r\n"
156 "0000000192 00000 n\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400157 "0000000311 00000 n\r\n"
etienneb7712c262016-04-26 08:13:45 -0700158 "trailer\r\n"
159 "<<\r\n"
160 "/Root 1 0 R\r\n"
161 "/Info 3 0 R\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400162 "/Size 6/ID\\[<.*><.*>\\]>>\r\n"
etienneb7712c262016-04-26 08:13:45 -0700163 "startxref\r\n"
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400164 "354\r\n"
etienneb7712c262016-04-26 08:13:45 -0700165 "%%EOF\r\n";
thestigdc7ec032016-11-21 15:32:52 -0800166
etienneb7712c262016-04-26 08:13:45 -0700167} // namespace
168
Tom Sepezd483eb42016-01-06 10:03:59 -0800169TEST_F(FPDFEditEmbeddertest, EmptyCreation) {
170 EXPECT_TRUE(CreateEmptyDocument());
weili9b777de2016-08-19 16:19:46 -0700171 FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0);
Tom Sepezd483eb42016-01-06 10:03:59 -0800172 EXPECT_NE(nullptr, page);
Nicolas Penad9d6c292017-06-06 16:12:10 -0400173 // The FPDFPage_GenerateContent call should do nothing.
Tom Sepezd483eb42016-01-06 10:03:59 -0800174 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Tom Sepez0aec19b2016-01-07 12:22:44 -0800175 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
etienneb7712c262016-04-26 08:13:45 -0700176
Nicolas Penad9d6c292017-06-06 16:12:10 -0400177 EXPECT_THAT(GetString(), testing::MatchesRegex(std::string(
178 kExpectedPDF, sizeof(kExpectedPDF))));
weili9b777de2016-08-19 16:19:46 -0700179 FPDF_ClosePage(page);
Tom Sepezd483eb42016-01-06 10:03:59 -0800180}
thestigdc7ec032016-11-21 15:32:52 -0800181
182// Regression test for https://crbug.com/667012
183TEST_F(FPDFEditEmbeddertest, RasterizePDF) {
184 const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615";
185
186 // Get the bitmap for the original document/
187 FPDF_BITMAP orig_bitmap;
188 {
189 EXPECT_TRUE(OpenDocument("black.pdf"));
190 FPDF_PAGE orig_page = LoadPage(0);
191 EXPECT_NE(nullptr, orig_page);
192 orig_bitmap = RenderPage(orig_page);
193 CompareBitmap(orig_bitmap, 612, 792, kAllBlackMd5sum);
194 UnloadPage(orig_page);
195 }
196
197 // Create a new document from |orig_bitmap| and save it.
198 {
199 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
200 FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792);
201
202 // Add the bitmap to an image object and add the image object to the output
203 // page.
Lei Zhangcbd89572017-03-15 17:35:47 -0700204 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc);
thestigdc7ec032016-11-21 15:32:52 -0800205 EXPECT_TRUE(FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap));
206 EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0));
207 FPDFPage_InsertObject(temp_page, temp_img);
208 EXPECT_TRUE(FPDFPage_GenerateContent(temp_page));
209 EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0));
210 FPDF_ClosePage(temp_page);
211 FPDF_CloseDocument(temp_doc);
212 }
213 FPDFBitmap_Destroy(orig_bitmap);
214
215 // Get the generated content. Make sure it is at least as big as the original
216 // PDF.
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400217 EXPECT_GT(GetString().size(), 923U);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400218 TestAndCloseSaved(612, 792, kAllBlackMd5sum);
thestigdc7ec032016-11-21 15:32:52 -0800219}
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500220
221TEST_F(FPDFEditEmbeddertest, AddPaths) {
222 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500223 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500224
225 // We will first add a red rectangle
226 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20);
227 ASSERT_NE(nullptr, red_rect);
228 // Expect false when trying to set colors out of range
229 EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300));
230 EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0));
231
232 // Fill rectangle with red and insert to the page
233 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
234 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
235 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500236 FPDF_BITMAP page_bitmap = RenderPage(page);
237 CompareBitmap(page_bitmap, 612, 792, "66d02eaa6181e2c069ce2ea99beda497");
238 FPDFBitmap_Destroy(page_bitmap);
239
240 // Now add to that a green rectangle with some medium alpha
241 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40);
242 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128));
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200243
Miklos Vajna1ef04c92017-05-08 18:14:19 +0200244 // Make sure the type of the rectangle is a path.
245 EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect));
246
Miklos Vajnaed4705b2017-04-05 09:24:50 +0200247 // Make sure we get back the same color we set previously.
248 unsigned int R;
249 unsigned int G;
250 unsigned int B;
251 unsigned int A;
252 EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A));
253 EXPECT_EQ(0U, R);
254 EXPECT_EQ(255U, G);
255 EXPECT_EQ(0U, B);
256 EXPECT_EQ(128U, A);
257
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500258 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0));
259 FPDFPage_InsertObject(page, green_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500260 page_bitmap = RenderPage(page);
261 CompareBitmap(page_bitmap, 612, 792, "7b0b87604594e773add528fae567a558");
262 FPDFBitmap_Destroy(page_bitmap);
263
264 // Add a black triangle.
265 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100);
266 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200));
267 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
268 EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200));
269 EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100));
270 EXPECT_TRUE(FPDFPath_Close(black_path));
271 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500272 page_bitmap = RenderPage(page);
273 CompareBitmap(page_bitmap, 612, 792, "eadc8020a14dfcf091da2688733d8806");
274 FPDFBitmap_Destroy(page_bitmap);
275
276 // Now add a more complex blue path.
277 FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200);
278 EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255));
279 EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0));
280 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230));
281 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300));
282 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325));
283 EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325));
284 EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400));
285 EXPECT_TRUE(FPDFPath_Close(blue_path));
286 FPDFPage_InsertObject(page, blue_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500287 page_bitmap = RenderPage(page);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400288 const char last_md5[] = "9823e1a21bd9b72b6a442ba4f12af946";
289 CompareBitmap(page_bitmap, 612, 792, last_md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500290 FPDFBitmap_Destroy(page_bitmap);
291
292 // Now save the result, closing the page and document
Nicolas Pena207b7272017-05-26 17:37:06 -0400293 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penad03ca422017-03-06 13:54:33 -0500294 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500295 FPDF_ClosePage(page);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500296
297 // Render the saved result
Nicolas Pena3ff54002017-07-05 11:55:35 -0400298 TestAndCloseSaved(612, 792, last_md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500299}
300
301TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) {
302 // Load document with some text
303 EXPECT_TRUE(OpenDocument("hello_world.pdf"));
304 FPDF_PAGE page = LoadPage(0);
305 EXPECT_NE(nullptr, page);
306
307 // Add an opaque rectangle on top of some of the text.
308 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50);
309 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
310 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
311 FPDFPage_InsertObject(page, red_rect);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500312
313 // Add a transparent triangle on top of other part of the text.
314 FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50);
315 EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100));
316 EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0));
317 EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80));
318 EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10));
319 EXPECT_TRUE(FPDFPath_Close(black_path));
320 FPDFPage_InsertObject(page, black_path);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500321
322 // Render and check the result. Text is slightly different on Mac.
323 FPDF_BITMAP bitmap = RenderPage(page);
324#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700325 const char md5[] = "f9e6fa74230f234286bfcada9f7606d8";
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500326#else
Lei Zhang3de50052017-03-29 21:02:13 -0700327 const char md5[] = "2fdfc5dda29374cfba4349362e38ebdb";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400328#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700329 CompareBitmap(bitmap, 200, 200, md5);
Nicolas Pena0fc185e2017-02-08 12:13:20 -0500330 FPDFBitmap_Destroy(bitmap);
331 UnloadPage(page);
332}
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500333
wileyryae858aa42017-05-31 14:49:05 -0500334TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) {
335 // Load document with existing content
336 EXPECT_TRUE(OpenDocument("bug_717.pdf"));
337 FPDF_PAGE page = LoadPage(0);
338 EXPECT_NE(nullptr, page);
339
340 // Add a transparent rectangle on top of the existing content
341 FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50);
342 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100));
343 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0));
344 FPDFPage_InsertObject(page, red_rect2);
345
346 // Add an opaque rectangle on top of the existing content
347 FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50);
348 EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255));
349 EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0));
350 FPDFPage_InsertObject(page, red_rect);
351
352 FPDF_BITMAP bitmap = RenderPage(page);
353 CompareBitmap(bitmap, 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
354 FPDFBitmap_Destroy(bitmap);
355 EXPECT_TRUE(FPDFPage_GenerateContent(page));
356
357 // Now save the result, closing the page and document
358 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400359 UnloadPage(page);
wileyryae858aa42017-05-31 14:49:05 -0500360
Nicolas Pena3ff54002017-07-05 11:55:35 -0400361 // Render the saved result without closing the page and document
362 TestSaved(612, 792, "ad04e5bd0f471a9a564fb034bd0fb073");
wileyryae858aa42017-05-31 14:49:05 -0500363
364 ClearString();
365 // Add another opaque rectangle on top of the existing content
366 FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50);
367 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255));
368 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400369 FPDFPage_InsertObject(m_SavedPage, green_rect);
wileyryae858aa42017-05-31 14:49:05 -0500370
371 // Add another transparent rectangle on top of existing content
372 FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50);
373 EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100));
374 EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0));
Nicolas Pena3ff54002017-07-05 11:55:35 -0400375 FPDFPage_InsertObject(m_SavedPage, green_rect2);
376 FPDF_BITMAP new_bitmap = RenderPageWithFlags(m_SavedPage, m_SavedForm, 0);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400377 const char last_md5[] = "4b5b00f824620f8c9b8801ebb98e1cdd";
378 CompareBitmap(new_bitmap, 612, 792, last_md5);
wileyryae858aa42017-05-31 14:49:05 -0500379 FPDFBitmap_Destroy(new_bitmap);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400380 EXPECT_TRUE(FPDFPage_GenerateContent(m_SavedPage));
wileyryae858aa42017-05-31 14:49:05 -0500381
382 // Now save the result, closing the page and document
Nicolas Pena3ff54002017-07-05 11:55:35 -0400383 EXPECT_TRUE(FPDF_SaveAsCopy(m_SavedDocument, this, 0));
384 CloseSaved();
wileyryae858aa42017-05-31 14:49:05 -0500385
386 // Render the saved result
Nicolas Pena3ff54002017-07-05 11:55:35 -0400387 TestAndCloseSaved(612, 792, last_md5);
wileyryae858aa42017-05-31 14:49:05 -0500388}
389
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500390TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) {
391 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500392 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500393
394 // Add a large stroked rectangle (fill color should not affect it).
395 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400);
396 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255));
397 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255));
398 EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f));
399 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1));
400 FPDFPage_InsertObject(page, rect);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500401 FPDF_BITMAP page_bitmap = RenderPage(page);
402 CompareBitmap(page_bitmap, 612, 792, "64bd31f862a89e0a9e505a5af6efd506");
403 FPDFBitmap_Destroy(page_bitmap);
404
405 // Add crossed-checkmark
406 FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500);
407 EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400));
408 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600));
409 EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600));
410 EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400));
411 EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180));
412 EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f));
413 EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1));
414 FPDFPage_InsertObject(page, check);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500415 page_bitmap = RenderPage(page);
416 CompareBitmap(page_bitmap, 612, 792, "4b6f3b9d25c4e194821217d5016c3724");
417 FPDFBitmap_Destroy(page_bitmap);
418
419 // Add stroked and filled oval-ish path.
420 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100);
421 EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300));
422 EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305));
423 EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105));
424 EXPECT_TRUE(FPDFPath_Close(path));
425 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100));
426 EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150));
427 EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f));
428 EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1));
429 FPDFPage_InsertObject(page, path);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500430 page_bitmap = RenderPage(page);
431 CompareBitmap(page_bitmap, 612, 792, "ff3e6a22326754944cc6e56609acd73b");
432 FPDFBitmap_Destroy(page_bitmap);
433 FPDF_ClosePage(page);
Nicolas Pena2eb1a702017-02-09 18:17:33 -0500434}
Nicolas Pena49058402017-02-14 18:26:20 -0500435
436TEST_F(FPDFEditEmbeddertest, AddStandardFontText) {
437 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500438 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Pena49058402017-02-14 18:26:20 -0500439
440 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400441 FPDF_PAGEOBJECT text_object1 =
442 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
443 EXPECT_TRUE(text_object1);
444 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 =
445 GetFPDFWideString(L"I'm at the bottom of the page");
446 EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get()));
447 FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20);
448 FPDFPage_InsertObject(page, text_object1);
Nicolas Pena49058402017-02-14 18:26:20 -0500449 FPDF_BITMAP page_bitmap = RenderPage(page);
450#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700451 const char md5[] = "a4dddc1a3930fa694bbff9789dab4161";
Nicolas Pena49058402017-02-14 18:26:20 -0500452#else
Lei Zhang3de50052017-03-29 21:02:13 -0700453 const char md5[] = "6e8a9b0682f60fd3ff1bf087b093d30d";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400454#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700455 CompareBitmap(page_bitmap, 612, 792, md5);
Nicolas Pena49058402017-02-14 18:26:20 -0500456 FPDFBitmap_Destroy(page_bitmap);
457
458 // Try another font
Nicolas Penab3161852017-05-02 14:12:50 -0400459 FPDF_PAGEOBJECT text_object2 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500460 FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400461 EXPECT_TRUE(text_object2);
462 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
463 GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold.");
464 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
465 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600);
466 FPDFPage_InsertObject(page, text_object2);
Nicolas Pena49058402017-02-14 18:26:20 -0500467 page_bitmap = RenderPage(page);
468#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700469 const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c";
Lei Zhang3de50052017-03-29 21:02:13 -0700470#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
471 const char md5_2[] = "56863642d4d8b418cfd810fdb5a5d92f";
Nicolas Pena49058402017-02-14 18:26:20 -0500472#else
Lei Zhang3de50052017-03-29 21:02:13 -0700473 const char md5_2[] = "0c83875429688bda45a55a692d5aa781";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400474#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700475 CompareBitmap(page_bitmap, 612, 792, md5_2);
Nicolas Pena49058402017-02-14 18:26:20 -0500476 FPDFBitmap_Destroy(page_bitmap);
477
478 // And some randomly transformed text
Nicolas Penab3161852017-05-02 14:12:50 -0400479 FPDF_PAGEOBJECT text_object3 =
Nicolas Penad03ca422017-03-06 13:54:33 -0500480 FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f);
Nicolas Penab3161852017-05-02 14:12:50 -0400481 EXPECT_TRUE(text_object3);
482 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 =
483 GetFPDFWideString(L"Can you read me? <:)>");
484 EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get()));
485 FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200);
486 FPDFPage_InsertObject(page, text_object3);
Nicolas Pena49058402017-02-14 18:26:20 -0500487 page_bitmap = RenderPage(page);
488#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
Lei Zhang0d6d1782017-03-24 15:52:00 -0700489 const char md5_3[] = "40b3ef04f915ff4c4208948001763544";
Lei Zhang3de50052017-03-29 21:02:13 -0700490#elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
Lei Zhangb8e89e32017-05-02 11:53:08 -0700491 const char md5_3[] = "d69d419def35a098d9c10f8317d6709b";
Nicolas Pena49058402017-02-14 18:26:20 -0500492#else
Lei Zhangb8e89e32017-05-02 11:53:08 -0700493 const char md5_3[] = "8b300d3c6dfc12fa9af97e12ed5bd80a";
Nicolas Pena5bcd9a32017-03-22 11:04:35 -0400494#endif
Lei Zhang0d6d1782017-03-24 15:52:00 -0700495 CompareBitmap(page_bitmap, 612, 792, md5_3);
Nicolas Pena49058402017-02-14 18:26:20 -0500496 FPDFBitmap_Destroy(page_bitmap);
497
498 // TODO(npm): Why are there issues with text rotated by 90 degrees?
499 // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this.
500 FPDF_ClosePage(page);
Nicolas Pena49058402017-02-14 18:26:20 -0500501}
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500502
Nicolas Pena603a31d2017-06-14 11:41:18 -0400503TEST_F(FPDFEditEmbeddertest, GraphicsData) {
504 // New page
505 std::unique_ptr<void, FPDFPageDeleter> page(
506 FPDFPage_New(CreateNewDocument(), 0, 612, 792));
507
508 // Create a rect with nontrivial graphics
509 FPDF_PAGEOBJECT rect1 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
510 FPDFPageObj_SetBlendMode(rect1, "Color");
511 FPDFPage_InsertObject(page.get(), rect1);
512 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
513
514 // Check that the ExtGState was created
515 CPDF_Page* the_page = CPDFPageFromFPDFPage(page.get());
516 CPDF_Dictionary* graphics_dict =
517 the_page->m_pResources->GetDictFor("ExtGState");
518 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400519 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400520
521 // Add a text object causing no change to the graphics dictionary
522 FPDF_PAGEOBJECT text1 = FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
523 // Only alpha, the last component, matters for the graphics dictionary. And
524 // the default value is 255.
525 EXPECT_TRUE(FPDFText_SetFillColor(text1, 100, 100, 100, 255));
526 FPDFPage_InsertObject(page.get(), text1);
527 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400528 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400529
530 // Add a text object increasing the size of the graphics dictionary
531 FPDF_PAGEOBJECT text2 =
532 FPDFPageObj_NewTextObj(document(), "Times-Roman", 12.0f);
533 FPDFPage_InsertObject(page.get(), text2);
534 FPDFPageObj_SetBlendMode(text2, "Darken");
535 EXPECT_TRUE(FPDFText_SetFillColor(text2, 0, 0, 255, 150));
536 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400537 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400538
539 // Add a path that should reuse graphics
Nicolas Penace67be42017-06-14 14:52:49 -0400540 FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(400, 100);
Nicolas Pena603a31d2017-06-14 11:41:18 -0400541 FPDFPageObj_SetBlendMode(path, "Darken");
542 EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 200, 100, 150));
543 FPDFPage_InsertObject(page.get(), path);
544 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400545 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400546
547 // Add a rect increasing the size of the graphics dictionary
548 FPDF_PAGEOBJECT rect2 = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
549 FPDFPageObj_SetBlendMode(rect2, "Darken");
550 EXPECT_TRUE(FPDFPath_SetFillColor(rect2, 0, 0, 255, 150));
551 EXPECT_TRUE(FPDFPath_SetStrokeColor(rect2, 0, 0, 0, 200));
552 FPDFPage_InsertObject(page.get(), rect2);
553 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400554 EXPECT_EQ(4, static_cast<int>(graphics_dict->GetCount()));
Nicolas Pena603a31d2017-06-14 11:41:18 -0400555}
556
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500557TEST_F(FPDFEditEmbeddertest, DoubleGenerating) {
558 // Start with a blank page
Nicolas Penad03ca422017-03-06 13:54:33 -0500559 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500560
561 // Add a red rectangle with some non-default alpha
562 FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100);
563 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128));
564 EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0));
565 FPDFPage_InsertObject(page, rect);
566 EXPECT_TRUE(FPDFPage_GenerateContent(page));
567
568 // Check the ExtGState
569 CPDF_Page* the_page = CPDFPageFromFPDFPage(page);
570 CPDF_Dictionary* graphics_dict =
571 the_page->m_pResources->GetDictFor("ExtGState");
572 ASSERT_TRUE(graphics_dict);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400573 EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500574
575 // Check the bitmap
576 FPDF_BITMAP page_bitmap = RenderPage(page);
577 CompareBitmap(page_bitmap, 612, 792, "5384da3406d62360ffb5cac4476fff1c");
578 FPDFBitmap_Destroy(page_bitmap);
579
580 // Never mind, my new favorite color is blue, increase alpha
581 EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180));
582 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400583 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500584
585 // Check that bitmap displays changed content
586 page_bitmap = RenderPage(page);
587 CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c");
588 FPDFBitmap_Destroy(page_bitmap);
589
590 // And now generate, without changes
591 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400592 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500593 page_bitmap = RenderPage(page);
594 CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c");
595 FPDFBitmap_Destroy(page_bitmap);
596
597 // Add some text to the page
Nicolas Penab3161852017-05-02 14:12:50 -0400598 FPDF_PAGEOBJECT text_object =
599 FPDFPageObj_NewTextObj(document(), "Arial", 12.0f);
600 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
601 GetFPDFWideString(L"Something something #text# something");
602 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
603 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300);
604 FPDFPage_InsertObject(page, text_object);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500605 EXPECT_TRUE(FPDFPage_GenerateContent(page));
606 CPDF_Dictionary* font_dict = the_page->m_pResources->GetDictFor("Font");
607 ASSERT_TRUE(font_dict);
608 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
609
610 // Generate yet again, check dicts are reasonably sized
611 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400612 EXPECT_EQ(3, static_cast<int>(graphics_dict->GetCount()));
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500613 EXPECT_EQ(1, static_cast<int>(font_dict->GetCount()));
614 FPDF_ClosePage(page);
Nicolas Penaa4ad01f2017-02-15 16:26:48 -0500615}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500616
Nicolas Penad03ca422017-03-06 13:54:33 -0500617TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) {
618 CreateNewDocument();
619 // TODO(npm): use other fonts after disallowing loading any font as any type
620 const CPDF_Font* stock_font =
621 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700622 const uint8_t* data = stock_font->GetFont()->GetFontData();
623 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400624 std::unique_ptr<void, FPDFFontDeleter> font(
625 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false));
626 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400627 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500628 EXPECT_TRUE(typed_font->IsType1Font());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500629
Nicolas Penad03ca422017-03-06 13:54:33 -0500630 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
Nicolas Penabe90aae2017-02-27 10:41:41 -0500631 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
632 EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype"));
633 EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont"));
634 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
635 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
636 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
Nicolas Penad03ca422017-03-06 13:54:33 -0500637 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
638
Nicolas Penabe90aae2017-02-27 10:41:41 -0500639 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
Nicolas Penad03ca422017-03-06 13:54:33 -0500640 ASSERT_TRUE(widths_array);
641 ASSERT_EQ(224U, widths_array->GetCount());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500642 EXPECT_EQ(250, widths_array->GetNumberAt(0));
Nicolas Penad03ca422017-03-06 13:54:33 -0500643 EXPECT_EQ(569, widths_array->GetNumberAt(11));
644 EXPECT_EQ(500, widths_array->GetNumberAt(223));
645 CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data);
646}
Nicolas Penabe90aae2017-02-27 10:41:41 -0500647
Nicolas Penad03ca422017-03-06 13:54:33 -0500648TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) {
649 CreateNewDocument();
650 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700651 const uint8_t* data = stock_font->GetFont()->GetFontData();
652 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400653 std::unique_ptr<void, FPDFFontDeleter> font(
654 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false));
655 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400656 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500657 EXPECT_TRUE(typed_font->IsTrueTypeFont());
Nicolas Penabe90aae2017-02-27 10:41:41 -0500658
Nicolas Penad03ca422017-03-06 13:54:33 -0500659 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
660 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
661 EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype"));
662 EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont"));
663 ASSERT_TRUE(font_dict->KeyExist("FirstChar"));
664 ASSERT_TRUE(font_dict->KeyExist("LastChar"));
665 EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar"));
666 EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar"));
Nicolas Penabe90aae2017-02-27 10:41:41 -0500667
Nicolas Penad03ca422017-03-06 13:54:33 -0500668 CPDF_Array* widths_array = font_dict->GetArrayFor("Widths");
669 ASSERT_TRUE(widths_array);
670 ASSERT_EQ(224U, widths_array->GetCount());
671 EXPECT_EQ(600, widths_array->GetNumberAt(33));
672 EXPECT_EQ(600, widths_array->GetNumberAt(74));
673 EXPECT_EQ(600, widths_array->GetNumberAt(223));
674 CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data);
675}
676
677TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) {
678 CreateNewDocument();
679 const CPDF_Font* stock_font =
680 CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700681 const uint8_t* data = stock_font->GetFont()->GetFontData();
682 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400683 std::unique_ptr<void, FPDFFontDeleter> font(
684 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1));
685 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400686 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500687 EXPECT_TRUE(typed_font->IsCIDFont());
688
689 // Check font dictionary entries
690 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
691 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
692 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
693 EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont"));
694 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
695 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
696 ASSERT_TRUE(descendant_array);
697 EXPECT_EQ(1U, descendant_array->GetCount());
698
699 // Check the CIDFontDict
700 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
701 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
702 EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype"));
703 EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont"));
704 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
705 ASSERT_TRUE(cidinfo_dict);
706 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
707 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
708 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
709 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data);
710
711 // Check widths
712 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
713 ASSERT_TRUE(widths_array);
Nicolas Penaf45ade32017-05-03 10:23:49 -0400714 EXPECT_GT(widths_array->GetCount(), 1U);
Nicolas Penad03ca422017-03-06 13:54:33 -0500715 CheckCompositeFontWidths(widths_array, typed_font);
716}
717
718TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) {
719 CreateNewDocument();
720 const CPDF_Font* stock_font =
721 CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700722 const uint8_t* data = stock_font->GetFont()->GetFontData();
723 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penad03ca422017-03-06 13:54:33 -0500724
Nicolas Penab3161852017-05-02 14:12:50 -0400725 std::unique_ptr<void, FPDFFontDeleter> font(
726 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
727 ASSERT_TRUE(font.get());
Nicolas Pena46abb662017-05-17 17:23:22 -0400728 CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get());
Nicolas Penad03ca422017-03-06 13:54:33 -0500729 EXPECT_TRUE(typed_font->IsCIDFont());
730
731 // Check font dictionary entries
732 CPDF_Dictionary* font_dict = typed_font->GetFontDict();
733 EXPECT_EQ("Font", font_dict->GetStringFor("Type"));
734 EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype"));
735 EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont"));
736 EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding"));
737 CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts");
738 ASSERT_TRUE(descendant_array);
739 EXPECT_EQ(1U, descendant_array->GetCount());
740
741 // Check the CIDFontDict
742 CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0);
743 EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type"));
744 EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype"));
745 EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont"));
746 CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo");
747 ASSERT_TRUE(cidinfo_dict);
748 EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry"));
749 EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering"));
750 EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement"));
751 CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size,
752 data);
753
754 // Check widths
755 CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W");
756 ASSERT_TRUE(widths_array);
757 CheckCompositeFontWidths(widths_array, typed_font);
Nicolas Penabe90aae2017-02-27 10:41:41 -0500758}
rbpotterce8e51e2017-04-28 12:42:47 -0700759
760TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) {
761 // Load document with a -90 degree rotation
762 EXPECT_TRUE(OpenDocument("bug_713197.pdf"));
763 FPDF_PAGE page = LoadPage(0);
764 EXPECT_NE(nullptr, page);
765
766 EXPECT_EQ(3, FPDFPage_GetRotation(page));
767 UnloadPage(page);
768}
Nicolas Penab3161852017-05-02 14:12:50 -0400769
770TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) {
771 // Start with a blank page
772 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
773 {
774 const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial");
Lei Zhangd74da7b2017-05-04 13:30:29 -0700775 const uint8_t* data = stock_font->GetFont()->GetFontData();
776 const uint32_t size = stock_font->GetFont()->GetSize();
Nicolas Penab3161852017-05-02 14:12:50 -0400777 std::unique_ptr<void, FPDFFontDeleter> font(
778 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0));
779 ASSERT_TRUE(font.get());
780
781 // Add some text to the page
782 FPDF_PAGEOBJECT text_object =
783 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
784 EXPECT_TRUE(text_object);
785 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
786 GetFPDFWideString(L"I am testing my loaded font, WEE.");
787 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
788 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400);
789 FPDFPage_InsertObject(page, text_object);
Nicolas Penab3161852017-05-02 14:12:50 -0400790 FPDF_BITMAP page_bitmap = RenderPage(page);
791#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
792 const char md5[] = "17d2b6cd574cf66170b09c8927529a94";
793#else
794 const char md5[] = "28e5b10743660dcdfd1618db47b39d32";
795#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
796 CompareBitmap(page_bitmap, 612, 792, md5);
797 FPDFBitmap_Destroy(page_bitmap);
798
799 // Add some more text, same font
800 FPDF_PAGEOBJECT text_object2 =
801 FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f);
802 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
803 GetFPDFWideString(L"Bigger font size");
804 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
805 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200);
806 FPDFPage_InsertObject(page, text_object2);
Nicolas Penab3161852017-05-02 14:12:50 -0400807 }
808 FPDF_BITMAP page_bitmap2 = RenderPage(page);
809#if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
810 const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea";
811#else
812 const char md5_2[] = "a068eef4110d607f77c87ea8340fa2a5";
813#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
814 CompareBitmap(page_bitmap2, 612, 792, md5_2);
815 FPDFBitmap_Destroy(page_bitmap2);
816
Nicolas Pena207b7272017-05-26 17:37:06 -0400817 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penab3161852017-05-02 14:12:50 -0400818 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
819 FPDF_ClosePage(page);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400820 TestAndCloseSaved(612, 792, md5_2);
Nicolas Penab3161852017-05-02 14:12:50 -0400821}
Nicolas Penaf45ade32017-05-03 10:23:49 -0400822
Jane Liueda65252017-06-07 11:31:27 -0400823TEST_F(FPDFEditEmbeddertest, TransformAnnot) {
824 // Open a file with one annotation and load its first page.
825 ASSERT_TRUE(OpenDocument("annotation_highlight_long_content.pdf"));
826 FPDF_PAGE page = FPDF_LoadPage(document(), 0);
827 ASSERT_TRUE(page);
828
829 // Add an underline annotation to the page without specifying its rectangle.
Jane Liud60e9ad2017-06-26 11:28:36 -0400830 FPDF_ANNOTATION annot = FPDFPage_CreateAnnot(page, FPDF_ANNOT_UNDERLINE);
831 ASSERT_TRUE(annot);
Jane Liueda65252017-06-07 11:31:27 -0400832
833 // FPDFPage_TransformAnnots() should run without errors when modifying
834 // annotation rectangles.
835 FPDFPage_TransformAnnots(page, 1, 2, 3, 4, 5, 6);
836
Jane Liud60e9ad2017-06-26 11:28:36 -0400837 FPDFPage_CloseAnnot(annot);
Jane Liueda65252017-06-07 11:31:27 -0400838 UnloadPage(page);
839}
840
Nicolas Penaf45ade32017-05-03 10:23:49 -0400841// TODO(npm): Add tests using Japanese fonts in other OS.
842#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_
843TEST_F(FPDFEditEmbeddertest, AddCIDFontText) {
844 // Start with a blank page
845 FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792);
846 CFX_Font CIDfont;
847 {
848 // First, get the data from the font
849 CIDfont.LoadSubst("IPAGothic", 1, 0, 400, 0, 932, 0);
850 EXPECT_EQ("IPAGothic", CIDfont.GetFaceName());
851 const uint8_t* data = CIDfont.GetFontData();
852 const uint32_t size = CIDfont.GetSize();
853
854 // Load the data into a FPDF_Font.
855 std::unique_ptr<void, FPDFFontDeleter> font(
856 FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1));
857 ASSERT_TRUE(font.get());
858
859 // Add some text to the page
860 FPDF_PAGEOBJECT text_object =
861 FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f);
862 ASSERT_TRUE(text_object);
863 std::wstring wstr = L"ABCDEFGhijklmnop.";
864 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text =
865 GetFPDFWideString(wstr);
866 EXPECT_TRUE(FPDFText_SetText(text_object, text.get()));
867 FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 200, 200);
868 FPDFPage_InsertObject(page, text_object);
869
870 // And add some Japanese characters
871 FPDF_PAGEOBJECT text_object2 =
872 FPDFPageObj_CreateTextObj(document(), font.get(), 18.0f);
873 ASSERT_TRUE(text_object2);
874 std::wstring wstr2 =
875 L"\u3053\u3093\u306B\u3061\u306f\u4e16\u754C\u3002\u3053\u3053\u306B1"
876 L"\u756A";
877 std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 =
878 GetFPDFWideString(wstr2);
879 EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get()));
880 FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 500);
881 FPDFPage_InsertObject(page, text_object2);
882 }
883
Nicolas Pena207b7272017-05-26 17:37:06 -0400884 // Check that the text renders properly.
Nicolas Penaf45ade32017-05-03 10:23:49 -0400885 FPDF_BITMAP page_bitmap = RenderPage(page);
886 const char md5[] = "2bc6c1aaa2252e73246a75775ccf38c2";
887 CompareBitmap(page_bitmap, 612, 792, md5);
888 FPDFBitmap_Destroy(page_bitmap);
889
890 // Save the document, close the page.
Nicolas Pena207b7272017-05-26 17:37:06 -0400891 EXPECT_TRUE(FPDFPage_GenerateContent(page));
Nicolas Penaf45ade32017-05-03 10:23:49 -0400892 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
893 FPDF_ClosePage(page);
Nicolas Pena3ff54002017-07-05 11:55:35 -0400894 TestAndCloseSaved(612, 792, md5);
Nicolas Penaf45ade32017-05-03 10:23:49 -0400895}
896#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400897
898TEST_F(FPDFEditEmbeddertest, SaveAndRender) {
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400899 const char md5[] = "3c20472b0552c0c22b88ab1ed8c6202b";
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400900 {
901 EXPECT_TRUE(OpenDocument("bug_779.pdf"));
902 FPDF_PAGE page = LoadPage(0);
903 ASSERT_NE(nullptr, page);
904
905 // Now add a more complex blue path.
906 FPDF_PAGEOBJECT green_path = FPDFPageObj_CreateNewPath(20, 20);
907 EXPECT_TRUE(FPDFPath_SetFillColor(green_path, 0, 255, 0, 200));
908 // TODO(npm): stroking will cause the MD5s to differ.
909 EXPECT_TRUE(FPDFPath_SetDrawMode(green_path, FPDF_FILLMODE_WINDING, 0));
910 EXPECT_TRUE(FPDFPath_LineTo(green_path, 20, 63));
911 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 55, 55, 78, 78, 90, 90));
912 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 133));
913 EXPECT_TRUE(FPDFPath_LineTo(green_path, 133, 33));
914 EXPECT_TRUE(FPDFPath_BezierTo(green_path, 38, 33, 39, 36, 40, 40));
915 EXPECT_TRUE(FPDFPath_Close(green_path));
916 FPDFPage_InsertObject(page, green_path);
917 FPDF_BITMAP page_bitmap = RenderPage(page);
Nicolas Penaa0b48aa2017-06-29 11:01:46 -0400918 CompareBitmap(page_bitmap, 612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400919 FPDFBitmap_Destroy(page_bitmap);
920
921 // Now save the result, closing the page and document
922 EXPECT_TRUE(FPDFPage_GenerateContent(page));
923 EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
924 UnloadPage(page);
925 }
Nicolas Pena3ff54002017-07-05 11:55:35 -0400926 TestAndCloseSaved(612, 792, md5);
Nicolas Pena9ba8fbc2017-06-28 15:31:56 -0400927}