Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 1 | // 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 Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <string> |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 7 | #include <utility> |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 8 | |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 9 | #include "core/fpdfapi/font/cpdf_font.h" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 10 | #include "core/fpdfapi/page/cpdf_page.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 11 | #include "core/fpdfapi/parser/cpdf_array.h" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_number.h" |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 14 | #include "core/fpdfapi/parser/cpdf_stream.h" |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 15 | #include "core/fxcrt/fx_system.h" |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 16 | #include "fpdfsdk/fsdk_define.h" |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 17 | #include "public/cpp/fpdf_deleters.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 18 | #include "public/fpdf_edit.h" |
| 19 | #include "public/fpdfview.h" |
| 20 | #include "testing/embedder_test.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 21 | #include "testing/gmock/include/gmock/gmock-matchers.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 23 | #include "testing/test_support.h" |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 24 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 25 | class FPDFEditEmbeddertest : public EmbedderTest, public TestSaver { |
| 26 | protected: |
| 27 | FPDF_DOCUMENT CreateNewDocument() { |
| 28 | document_ = FPDF_CreateNewDocument(); |
Tom Sepez | 41066c1 | 2017-05-18 09:28:49 -0700 | [diff] [blame] | 29 | cpdf_doc_ = CPDFDocumentFromFPDFDocument(document_); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 30 | return document_; |
| 31 | } |
| 32 | |
| 33 | void CheckFontDescriptor(CPDF_Dictionary* font_dict, |
| 34 | int font_type, |
| 35 | bool bold, |
| 36 | bool italic, |
| 37 | uint32_t size, |
| 38 | const uint8_t* data) { |
| 39 | CPDF_Dictionary* font_desc = font_dict->GetDictFor("FontDescriptor"); |
| 40 | ASSERT_TRUE(font_desc); |
| 41 | EXPECT_EQ("FontDescriptor", font_desc->GetStringFor("Type")); |
| 42 | EXPECT_EQ(font_dict->GetStringFor("BaseFont"), |
| 43 | font_desc->GetStringFor("FontName")); |
| 44 | |
| 45 | // Check that the font descriptor has the required keys according to spec |
| 46 | // 1.7 Table 5.19 |
| 47 | ASSERT_TRUE(font_desc->KeyExist("Flags")); |
| 48 | int font_flags = font_desc->GetIntegerFor("Flags"); |
| 49 | if (bold) |
| 50 | EXPECT_TRUE(font_flags & FXFONT_BOLD); |
| 51 | else |
| 52 | EXPECT_FALSE(font_flags & FXFONT_BOLD); |
| 53 | if (italic) |
| 54 | EXPECT_TRUE(font_flags & FXFONT_ITALIC); |
| 55 | else |
| 56 | EXPECT_FALSE(font_flags & FXFONT_ITALIC); |
| 57 | EXPECT_TRUE(font_flags & FXFONT_NONSYMBOLIC); |
| 58 | ASSERT_TRUE(font_desc->KeyExist("FontBBox")); |
| 59 | EXPECT_EQ(4U, font_desc->GetArrayFor("FontBBox")->GetCount()); |
| 60 | EXPECT_TRUE(font_desc->KeyExist("ItalicAngle")); |
| 61 | EXPECT_TRUE(font_desc->KeyExist("Ascent")); |
| 62 | EXPECT_TRUE(font_desc->KeyExist("Descent")); |
| 63 | EXPECT_TRUE(font_desc->KeyExist("CapHeight")); |
| 64 | EXPECT_TRUE(font_desc->KeyExist("StemV")); |
| 65 | CFX_ByteString present("FontFile"); |
| 66 | CFX_ByteString absent("FontFile2"); |
| 67 | if (font_type == FPDF_FONT_TRUETYPE) |
| 68 | std::swap(present, absent); |
| 69 | EXPECT_TRUE(font_desc->KeyExist(present)); |
| 70 | EXPECT_FALSE(font_desc->KeyExist(absent)); |
| 71 | |
| 72 | // Check that the font stream is the one that was provided |
| 73 | CPDF_Stream* font_stream = font_desc->GetStreamFor(present); |
| 74 | ASSERT_EQ(size, font_stream->GetRawSize()); |
| 75 | uint8_t* stream_data = font_stream->GetRawData(); |
| 76 | for (size_t j = 0; j < size; j++) |
| 77 | EXPECT_EQ(data[j], stream_data[j]) << " at byte " << j; |
| 78 | } |
| 79 | |
| 80 | void CheckCompositeFontWidths(CPDF_Array* widths_array, |
| 81 | CPDF_Font* typed_font) { |
| 82 | // Check that W array is in a format that conforms to PDF spec 1.7 section |
| 83 | // "Glyph Metrics in CIDFonts" (these checks are not |
| 84 | // implementation-specific). |
| 85 | EXPECT_GT(widths_array->GetCount(), 1U); |
| 86 | int num_cids_checked = 0; |
| 87 | int cur_cid = 0; |
| 88 | for (size_t idx = 0; idx < widths_array->GetCount(); idx++) { |
| 89 | int cid = widths_array->GetNumberAt(idx); |
| 90 | EXPECT_GE(cid, cur_cid); |
| 91 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
| 92 | CPDF_Object* next = widths_array->GetObjectAt(idx); |
| 93 | if (next->IsArray()) { |
| 94 | // We are in the c [w1 w2 ...] case |
| 95 | CPDF_Array* arr = next->AsArray(); |
| 96 | int cnt = static_cast<int>(arr->GetCount()); |
| 97 | size_t inner_idx = 0; |
| 98 | for (cur_cid = cid; cur_cid < cid + cnt; cur_cid++) { |
| 99 | int width = arr->GetNumberAt(inner_idx++); |
| 100 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid " |
| 101 | << cur_cid; |
| 102 | } |
| 103 | num_cids_checked += cnt; |
| 104 | continue; |
| 105 | } |
| 106 | // Otherwise, are in the c_first c_last w case. |
| 107 | ASSERT_TRUE(next->IsNumber()); |
| 108 | int last_cid = next->AsNumber()->GetInteger(); |
| 109 | ASSERT_FALSE(++idx == widths_array->GetCount()); |
| 110 | int width = widths_array->GetNumberAt(idx); |
| 111 | for (cur_cid = cid; cur_cid <= last_cid; cur_cid++) { |
| 112 | EXPECT_EQ(width, typed_font->GetCharWidthF(cur_cid)) << " at cid " |
| 113 | << cur_cid; |
| 114 | } |
| 115 | num_cids_checked += last_cid - cid + 1; |
| 116 | } |
| 117 | // Make sure we have a good amount of cids described |
| 118 | EXPECT_GT(num_cids_checked, 900); |
| 119 | } |
| 120 | CPDF_Document* cpdf_doc() { return cpdf_doc_; } |
| 121 | |
| 122 | private: |
| 123 | CPDF_Document* cpdf_doc_; |
| 124 | }; |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 125 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 126 | namespace { |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 127 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 128 | const char kExpectedPDF[] = |
| 129 | "%PDF-1.7\r\n" |
| 130 | "%\xA1\xB3\xC5\xD7\r\n" |
| 131 | "1 0 obj\r\n" |
| 132 | "<</Pages 2 0 R /Type/Catalog>>\r\n" |
| 133 | "endobj\r\n" |
| 134 | "2 0 obj\r\n" |
| 135 | "<</Count 1/Kids\\[ 4 0 R \\]/Type/Pages>>\r\n" |
| 136 | "endobj\r\n" |
| 137 | "3 0 obj\r\n" |
| 138 | "<</CreationDate\\(D:.*\\)/Creator\\(PDFium\\)>>\r\n" |
| 139 | "endobj\r\n" |
| 140 | "4 0 obj\r\n" |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 141 | "<</MediaBox\\[ 0 0 640 480\\]/Parent 2 0 R /Resources<<>>" |
| 142 | "/Rotate 0/Type/Page" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 143 | ">>\r\n" |
| 144 | "endobj\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 145 | "xref\r\n" |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 146 | "0 5\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 147 | "0000000000 65535 f\r\n" |
| 148 | "0000000017 00000 n\r\n" |
| 149 | "0000000066 00000 n\r\n" |
| 150 | "0000000122 00000 n\r\n" |
| 151 | "0000000192 00000 n\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 152 | "trailer\r\n" |
| 153 | "<<\r\n" |
| 154 | "/Root 1 0 R\r\n" |
| 155 | "/Info 3 0 R\r\n" |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 156 | "/Size 5/ID\\[<.*><.*>\\]>>\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 157 | "startxref\r\n" |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 158 | "285\r\n" |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 159 | "%%EOF\r\n"; |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 160 | |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 161 | } // namespace |
| 162 | |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 163 | TEST_F(FPDFEditEmbeddertest, EmptyCreation) { |
| 164 | EXPECT_TRUE(CreateEmptyDocument()); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 165 | FPDF_PAGE page = FPDFPage_New(document(), 0, 640.0, 480.0); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 166 | EXPECT_NE(nullptr, page); |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 167 | // The FPDFPage_GenerateContent call should do nothing. |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 168 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Tom Sepez | 0aec19b | 2016-01-07 12:22:44 -0800 | [diff] [blame] | 169 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
etienneb | 7712c26 | 2016-04-26 08:13:45 -0700 | [diff] [blame] | 170 | |
Nicolas Pena | d9d6c29 | 2017-06-06 16:12:10 -0400 | [diff] [blame] | 171 | EXPECT_THAT(GetString(), testing::MatchesRegex(std::string( |
| 172 | kExpectedPDF, sizeof(kExpectedPDF)))); |
weili | 9b777de | 2016-08-19 16:19:46 -0700 | [diff] [blame] | 173 | FPDF_ClosePage(page); |
Tom Sepez | d483eb4 | 2016-01-06 10:03:59 -0800 | [diff] [blame] | 174 | } |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 175 | |
| 176 | // Regression test for https://crbug.com/667012 |
| 177 | TEST_F(FPDFEditEmbeddertest, RasterizePDF) { |
| 178 | const char kAllBlackMd5sum[] = "5708fc5c4a8bd0abde99c8e8f0390615"; |
| 179 | |
| 180 | // Get the bitmap for the original document/ |
| 181 | FPDF_BITMAP orig_bitmap; |
| 182 | { |
| 183 | EXPECT_TRUE(OpenDocument("black.pdf")); |
| 184 | FPDF_PAGE orig_page = LoadPage(0); |
| 185 | EXPECT_NE(nullptr, orig_page); |
| 186 | orig_bitmap = RenderPage(orig_page); |
| 187 | CompareBitmap(orig_bitmap, 612, 792, kAllBlackMd5sum); |
| 188 | UnloadPage(orig_page); |
| 189 | } |
| 190 | |
| 191 | // Create a new document from |orig_bitmap| and save it. |
| 192 | { |
| 193 | FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); |
| 194 | FPDF_PAGE temp_page = FPDFPage_New(temp_doc, 0, 612, 792); |
| 195 | |
| 196 | // Add the bitmap to an image object and add the image object to the output |
| 197 | // page. |
Lei Zhang | cbd8957 | 2017-03-15 17:35:47 -0700 | [diff] [blame] | 198 | FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImageObj(temp_doc); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 199 | EXPECT_TRUE(FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, orig_bitmap)); |
| 200 | EXPECT_TRUE(FPDFImageObj_SetMatrix(temp_img, 612, 0, 0, 792, 0, 0)); |
| 201 | FPDFPage_InsertObject(temp_page, temp_img); |
| 202 | EXPECT_TRUE(FPDFPage_GenerateContent(temp_page)); |
| 203 | EXPECT_TRUE(FPDF_SaveAsCopy(temp_doc, this, 0)); |
| 204 | FPDF_ClosePage(temp_page); |
| 205 | FPDF_CloseDocument(temp_doc); |
| 206 | } |
| 207 | FPDFBitmap_Destroy(orig_bitmap); |
| 208 | |
| 209 | // Get the generated content. Make sure it is at least as big as the original |
| 210 | // PDF. |
| 211 | std::string new_file = GetString(); |
| 212 | EXPECT_GT(new_file.size(), 923U); |
| 213 | |
| 214 | // Read |new_file| in, and verify its rendered bitmap. |
| 215 | { |
| 216 | FPDF_FILEACCESS file_access; |
| 217 | memset(&file_access, 0, sizeof(file_access)); |
| 218 | file_access.m_FileLen = new_file.size(); |
| 219 | file_access.m_GetBlock = GetBlockFromString; |
| 220 | file_access.m_Param = &new_file; |
| 221 | |
| 222 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 223 | EXPECT_EQ(1, FPDF_GetPageCount(document_)); |
| 224 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 225 | EXPECT_NE(nullptr, new_page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 226 | FPDF_BITMAP new_bitmap = RenderPage(new_page); |
thestig | dc7ec03 | 2016-11-21 15:32:52 -0800 | [diff] [blame] | 227 | CompareBitmap(new_bitmap, 612, 792, kAllBlackMd5sum); |
| 228 | FPDF_ClosePage(new_page); |
| 229 | FPDF_CloseDocument(new_doc); |
| 230 | FPDFBitmap_Destroy(new_bitmap); |
| 231 | } |
| 232 | } |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 233 | |
| 234 | TEST_F(FPDFEditEmbeddertest, AddPaths) { |
| 235 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 236 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 237 | |
| 238 | // We will first add a red rectangle |
| 239 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(10, 10, 20, 20); |
| 240 | ASSERT_NE(nullptr, red_rect); |
| 241 | // Expect false when trying to set colors out of range |
| 242 | EXPECT_FALSE(FPDFPath_SetStrokeColor(red_rect, 100, 100, 100, 300)); |
| 243 | EXPECT_FALSE(FPDFPath_SetFillColor(red_rect, 200, 256, 200, 0)); |
| 244 | |
| 245 | // Fill rectangle with red and insert to the page |
| 246 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 247 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 248 | FPDFPage_InsertObject(page, red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 249 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 250 | CompareBitmap(page_bitmap, 612, 792, "66d02eaa6181e2c069ce2ea99beda497"); |
| 251 | FPDFBitmap_Destroy(page_bitmap); |
| 252 | |
| 253 | // Now add to that a green rectangle with some medium alpha |
| 254 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(100, 100, 40, 40); |
| 255 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 128)); |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 256 | |
Miklos Vajna | 1ef04c9 | 2017-05-08 18:14:19 +0200 | [diff] [blame] | 257 | // Make sure the type of the rectangle is a path. |
| 258 | EXPECT_EQ(FPDF_PAGEOBJ_PATH, FPDFPageObj_GetType(green_rect)); |
| 259 | |
Miklos Vajna | ed4705b | 2017-04-05 09:24:50 +0200 | [diff] [blame] | 260 | // Make sure we get back the same color we set previously. |
| 261 | unsigned int R; |
| 262 | unsigned int G; |
| 263 | unsigned int B; |
| 264 | unsigned int A; |
| 265 | EXPECT_TRUE(FPDFPath_GetFillColor(green_rect, &R, &G, &B, &A)); |
| 266 | EXPECT_EQ(0U, R); |
| 267 | EXPECT_EQ(255U, G); |
| 268 | EXPECT_EQ(0U, B); |
| 269 | EXPECT_EQ(128U, A); |
| 270 | |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 271 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_WINDING, 0)); |
| 272 | FPDFPage_InsertObject(page, green_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 273 | page_bitmap = RenderPage(page); |
| 274 | CompareBitmap(page_bitmap, 612, 792, "7b0b87604594e773add528fae567a558"); |
| 275 | FPDFBitmap_Destroy(page_bitmap); |
| 276 | |
| 277 | // Add a black triangle. |
| 278 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(400, 100); |
| 279 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 200)); |
| 280 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 281 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 400, 200)); |
| 282 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 300, 100)); |
| 283 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
| 284 | FPDFPage_InsertObject(page, black_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 285 | page_bitmap = RenderPage(page); |
| 286 | CompareBitmap(page_bitmap, 612, 792, "eadc8020a14dfcf091da2688733d8806"); |
| 287 | FPDFBitmap_Destroy(page_bitmap); |
| 288 | |
| 289 | // Now add a more complex blue path. |
| 290 | FPDF_PAGEOBJECT blue_path = FPDFPageObj_CreateNewPath(200, 200); |
| 291 | EXPECT_TRUE(FPDFPath_SetFillColor(blue_path, 0, 0, 255, 255)); |
| 292 | EXPECT_TRUE(FPDFPath_SetDrawMode(blue_path, FPDF_FILLMODE_WINDING, 0)); |
| 293 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 230, 230)); |
| 294 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 250, 250, 280, 280, 300, 300)); |
| 295 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 325, 325)); |
| 296 | EXPECT_TRUE(FPDFPath_LineTo(blue_path, 350, 325)); |
| 297 | EXPECT_TRUE(FPDFPath_BezierTo(blue_path, 375, 330, 390, 360, 400, 400)); |
| 298 | EXPECT_TRUE(FPDFPath_Close(blue_path)); |
| 299 | FPDFPage_InsertObject(page, blue_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 300 | page_bitmap = RenderPage(page); |
| 301 | CompareBitmap(page_bitmap, 612, 792, "9823e1a21bd9b72b6a442ba4f12af946"); |
| 302 | FPDFBitmap_Destroy(page_bitmap); |
| 303 | |
| 304 | // Now save the result, closing the page and document |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 305 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 306 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 307 | FPDF_ClosePage(page); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 308 | std::string new_file = GetString(); |
| 309 | |
| 310 | // Render the saved result |
| 311 | FPDF_FILEACCESS file_access; |
| 312 | memset(&file_access, 0, sizeof(file_access)); |
| 313 | file_access.m_FileLen = new_file.size(); |
| 314 | file_access.m_GetBlock = GetBlockFromString; |
| 315 | file_access.m_Param = &new_file; |
| 316 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 317 | ASSERT_NE(nullptr, new_doc); |
| 318 | EXPECT_EQ(1, FPDF_GetPageCount(new_doc)); |
| 319 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 320 | ASSERT_NE(nullptr, new_page); |
| 321 | FPDF_BITMAP new_bitmap = RenderPage(new_page); |
| 322 | CompareBitmap(new_bitmap, 612, 792, "9823e1a21bd9b72b6a442ba4f12af946"); |
| 323 | FPDFBitmap_Destroy(new_bitmap); |
| 324 | FPDF_ClosePage(new_page); |
| 325 | FPDF_CloseDocument(new_doc); |
| 326 | } |
| 327 | |
| 328 | TEST_F(FPDFEditEmbeddertest, PathOnTopOfText) { |
| 329 | // Load document with some text |
| 330 | EXPECT_TRUE(OpenDocument("hello_world.pdf")); |
| 331 | FPDF_PAGE page = LoadPage(0); |
| 332 | EXPECT_NE(nullptr, page); |
| 333 | |
| 334 | // Add an opaque rectangle on top of some of the text. |
| 335 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(20, 100, 50, 50); |
| 336 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 337 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 338 | FPDFPage_InsertObject(page, red_rect); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 339 | |
| 340 | // Add a transparent triangle on top of other part of the text. |
| 341 | FPDF_PAGEOBJECT black_path = FPDFPageObj_CreateNewPath(20, 50); |
| 342 | EXPECT_TRUE(FPDFPath_SetFillColor(black_path, 0, 0, 0, 100)); |
| 343 | EXPECT_TRUE(FPDFPath_SetDrawMode(black_path, FPDF_FILLMODE_ALTERNATE, 0)); |
| 344 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 30, 80)); |
| 345 | EXPECT_TRUE(FPDFPath_LineTo(black_path, 40, 10)); |
| 346 | EXPECT_TRUE(FPDFPath_Close(black_path)); |
| 347 | FPDFPage_InsertObject(page, black_path); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 348 | |
| 349 | // Render and check the result. Text is slightly different on Mac. |
| 350 | FPDF_BITMAP bitmap = RenderPage(page); |
| 351 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 352 | const char md5[] = "f9e6fa74230f234286bfcada9f7606d8"; |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 353 | #else |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 354 | const char md5[] = "2fdfc5dda29374cfba4349362e38ebdb"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 355 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 356 | CompareBitmap(bitmap, 200, 200, md5); |
Nicolas Pena | 0fc185e | 2017-02-08 12:13:20 -0500 | [diff] [blame] | 357 | FPDFBitmap_Destroy(bitmap); |
| 358 | UnloadPage(page); |
| 359 | } |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 360 | |
wileyrya | e858aa4 | 2017-05-31 14:49:05 -0500 | [diff] [blame] | 361 | TEST_F(FPDFEditEmbeddertest, EditOverExistingContent) { |
| 362 | // Load document with existing content |
| 363 | EXPECT_TRUE(OpenDocument("bug_717.pdf")); |
| 364 | FPDF_PAGE page = LoadPage(0); |
| 365 | EXPECT_NE(nullptr, page); |
| 366 | |
| 367 | // Add a transparent rectangle on top of the existing content |
| 368 | FPDF_PAGEOBJECT red_rect2 = FPDFPageObj_CreateNewRect(90, 700, 25, 50); |
| 369 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect2, 255, 0, 0, 100)); |
| 370 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
| 371 | FPDFPage_InsertObject(page, red_rect2); |
| 372 | |
| 373 | // Add an opaque rectangle on top of the existing content |
| 374 | FPDF_PAGEOBJECT red_rect = FPDFPageObj_CreateNewRect(115, 700, 25, 50); |
| 375 | EXPECT_TRUE(FPDFPath_SetFillColor(red_rect, 255, 0, 0, 255)); |
| 376 | EXPECT_TRUE(FPDFPath_SetDrawMode(red_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 377 | FPDFPage_InsertObject(page, red_rect); |
| 378 | |
| 379 | FPDF_BITMAP bitmap = RenderPage(page); |
| 380 | CompareBitmap(bitmap, 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); |
| 381 | FPDFBitmap_Destroy(bitmap); |
| 382 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 383 | |
| 384 | // Now save the result, closing the page and document |
| 385 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 386 | FPDF_ClosePage(page); |
| 387 | |
| 388 | // Render the saved result |
| 389 | std::string new_file = GetString(); |
| 390 | FPDF_FILEACCESS file_access; |
| 391 | memset(&file_access, 0, sizeof(file_access)); |
| 392 | file_access.m_FileLen = new_file.size(); |
| 393 | file_access.m_GetBlock = GetBlockFromString; |
| 394 | file_access.m_Param = &new_file; |
| 395 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 396 | ASSERT_NE(nullptr, new_doc); |
| 397 | EXPECT_EQ(1, FPDF_GetPageCount(new_doc)); |
| 398 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 399 | ASSERT_NE(nullptr, new_page); |
| 400 | FPDF_BITMAP new_bitmap = RenderPage(new_page); |
| 401 | CompareBitmap(new_bitmap, 612, 792, "ad04e5bd0f471a9a564fb034bd0fb073"); |
| 402 | FPDFBitmap_Destroy(new_bitmap); |
| 403 | |
| 404 | ClearString(); |
| 405 | // Add another opaque rectangle on top of the existing content |
| 406 | FPDF_PAGEOBJECT green_rect = FPDFPageObj_CreateNewRect(150, 700, 25, 50); |
| 407 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect, 0, 255, 0, 255)); |
| 408 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect, FPDF_FILLMODE_ALTERNATE, 0)); |
| 409 | FPDFPage_InsertObject(new_page, green_rect); |
| 410 | |
| 411 | // Add another transparent rectangle on top of existing content |
| 412 | FPDF_PAGEOBJECT green_rect2 = FPDFPageObj_CreateNewRect(175, 700, 25, 50); |
| 413 | EXPECT_TRUE(FPDFPath_SetFillColor(green_rect2, 0, 255, 0, 100)); |
| 414 | EXPECT_TRUE(FPDFPath_SetDrawMode(green_rect2, FPDF_FILLMODE_ALTERNATE, 0)); |
| 415 | FPDFPage_InsertObject(new_page, green_rect2); |
| 416 | new_bitmap = RenderPage(new_page); |
| 417 | CompareBitmap(new_bitmap, 612, 792, "4b5b00f824620f8c9b8801ebb98e1cdd"); |
| 418 | FPDFBitmap_Destroy(new_bitmap); |
| 419 | EXPECT_TRUE(FPDFPage_GenerateContent(new_page)); |
| 420 | |
| 421 | // Now save the result, closing the page and document |
| 422 | EXPECT_TRUE(FPDF_SaveAsCopy(new_doc, this, 0)); |
| 423 | FPDF_ClosePage(new_page); |
| 424 | FPDF_CloseDocument(new_doc); |
| 425 | |
| 426 | // Render the saved result |
| 427 | new_file = GetString(); |
| 428 | memset(&file_access, 0, sizeof(file_access)); |
| 429 | file_access.m_FileLen = new_file.size(); |
| 430 | file_access.m_GetBlock = GetBlockFromString; |
| 431 | file_access.m_Param = &new_file; |
| 432 | new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 433 | ASSERT_NE(nullptr, new_doc); |
| 434 | EXPECT_EQ(1, FPDF_GetPageCount(new_doc)); |
| 435 | new_page = FPDF_LoadPage(new_doc, 0); |
| 436 | ASSERT_NE(nullptr, new_page); |
| 437 | new_bitmap = RenderPage(new_page); |
| 438 | CompareBitmap(new_bitmap, 612, 792, "4b5b00f824620f8c9b8801ebb98e1cdd"); |
| 439 | FPDFBitmap_Destroy(new_bitmap); |
| 440 | |
| 441 | FPDF_ClosePage(new_page); |
| 442 | FPDF_CloseDocument(new_doc); |
| 443 | } |
| 444 | |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 445 | TEST_F(FPDFEditEmbeddertest, AddStrokedPaths) { |
| 446 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 447 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 448 | |
| 449 | // Add a large stroked rectangle (fill color should not affect it). |
| 450 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(20, 20, 200, 400); |
| 451 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 255)); |
| 452 | EXPECT_TRUE(FPDFPath_SetStrokeColor(rect, 0, 255, 0, 255)); |
| 453 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(rect, 15.0f)); |
| 454 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, 0, 1)); |
| 455 | FPDFPage_InsertObject(page, rect); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 456 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 457 | CompareBitmap(page_bitmap, 612, 792, "64bd31f862a89e0a9e505a5af6efd506"); |
| 458 | FPDFBitmap_Destroy(page_bitmap); |
| 459 | |
| 460 | // Add crossed-checkmark |
| 461 | FPDF_PAGEOBJECT check = FPDFPageObj_CreateNewPath(300, 500); |
| 462 | EXPECT_TRUE(FPDFPath_LineTo(check, 400, 400)); |
| 463 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 600)); |
| 464 | EXPECT_TRUE(FPDFPath_MoveTo(check, 400, 600)); |
| 465 | EXPECT_TRUE(FPDFPath_LineTo(check, 600, 400)); |
| 466 | EXPECT_TRUE(FPDFPath_SetStrokeColor(check, 128, 128, 128, 180)); |
| 467 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(check, 8.35f)); |
| 468 | EXPECT_TRUE(FPDFPath_SetDrawMode(check, 0, 1)); |
| 469 | FPDFPage_InsertObject(page, check); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 470 | page_bitmap = RenderPage(page); |
| 471 | CompareBitmap(page_bitmap, 612, 792, "4b6f3b9d25c4e194821217d5016c3724"); |
| 472 | FPDFBitmap_Destroy(page_bitmap); |
| 473 | |
| 474 | // Add stroked and filled oval-ish path. |
| 475 | FPDF_PAGEOBJECT path = FPDFPageObj_CreateNewPath(250, 100); |
| 476 | EXPECT_TRUE(FPDFPath_BezierTo(path, 180, 166, 180, 233, 250, 300)); |
| 477 | EXPECT_TRUE(FPDFPath_LineTo(path, 255, 305)); |
| 478 | EXPECT_TRUE(FPDFPath_BezierTo(path, 325, 233, 325, 166, 255, 105)); |
| 479 | EXPECT_TRUE(FPDFPath_Close(path)); |
| 480 | EXPECT_TRUE(FPDFPath_SetFillColor(path, 200, 128, 128, 100)); |
| 481 | EXPECT_TRUE(FPDFPath_SetStrokeColor(path, 128, 200, 128, 150)); |
| 482 | EXPECT_TRUE(FPDFPath_SetStrokeWidth(path, 10.5f)); |
| 483 | EXPECT_TRUE(FPDFPath_SetDrawMode(path, FPDF_FILLMODE_ALTERNATE, 1)); |
| 484 | FPDFPage_InsertObject(page, path); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 485 | page_bitmap = RenderPage(page); |
| 486 | CompareBitmap(page_bitmap, 612, 792, "ff3e6a22326754944cc6e56609acd73b"); |
| 487 | FPDFBitmap_Destroy(page_bitmap); |
| 488 | FPDF_ClosePage(page); |
Nicolas Pena | 2eb1a70 | 2017-02-09 18:17:33 -0500 | [diff] [blame] | 489 | } |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 490 | |
| 491 | TEST_F(FPDFEditEmbeddertest, AddStandardFontText) { |
| 492 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 493 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 494 | |
| 495 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 496 | FPDF_PAGEOBJECT text_object1 = |
| 497 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 498 | EXPECT_TRUE(text_object1); |
| 499 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text1 = |
| 500 | GetFPDFWideString(L"I'm at the bottom of the page"); |
| 501 | EXPECT_TRUE(FPDFText_SetText(text_object1, text1.get())); |
| 502 | FPDFPageObj_Transform(text_object1, 1, 0, 0, 1, 20, 20); |
| 503 | FPDFPage_InsertObject(page, text_object1); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 504 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 505 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 506 | const char md5[] = "a4dddc1a3930fa694bbff9789dab4161"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 507 | #else |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 508 | const char md5[] = "6e8a9b0682f60fd3ff1bf087b093d30d"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 509 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 510 | CompareBitmap(page_bitmap, 612, 792, md5); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 511 | FPDFBitmap_Destroy(page_bitmap); |
| 512 | |
| 513 | // Try another font |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 514 | FPDF_PAGEOBJECT text_object2 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 515 | FPDFPageObj_NewTextObj(document(), "TimesNewRomanBold", 15.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 516 | EXPECT_TRUE(text_object2); |
| 517 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 518 | GetFPDFWideString(L"Hi, I'm Bold. Times New Roman Bold."); |
| 519 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 520 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 100, 600); |
| 521 | FPDFPage_InsertObject(page, text_object2); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 522 | page_bitmap = RenderPage(page); |
| 523 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 524 | const char md5_2[] = "a5c4ace4c6f27644094813fe1441a21c"; |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 525 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 526 | const char md5_2[] = "56863642d4d8b418cfd810fdb5a5d92f"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 527 | #else |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 528 | const char md5_2[] = "0c83875429688bda45a55a692d5aa781"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 529 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 530 | CompareBitmap(page_bitmap, 612, 792, md5_2); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 531 | FPDFBitmap_Destroy(page_bitmap); |
| 532 | |
| 533 | // And some randomly transformed text |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 534 | FPDF_PAGEOBJECT text_object3 = |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 535 | FPDFPageObj_NewTextObj(document(), "Courier-Bold", 20.0f); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 536 | EXPECT_TRUE(text_object3); |
| 537 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text3 = |
| 538 | GetFPDFWideString(L"Can you read me? <:)>"); |
| 539 | EXPECT_TRUE(FPDFText_SetText(text_object3, text3.get())); |
| 540 | FPDFPageObj_Transform(text_object3, 1, 1.5, 2, 0.5, 200, 200); |
| 541 | FPDFPage_InsertObject(page, text_object3); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 542 | page_bitmap = RenderPage(page); |
| 543 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 544 | const char md5_3[] = "40b3ef04f915ff4c4208948001763544"; |
Lei Zhang | 3de5005 | 2017-03-29 21:02:13 -0700 | [diff] [blame] | 545 | #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
Lei Zhang | b8e89e3 | 2017-05-02 11:53:08 -0700 | [diff] [blame] | 546 | const char md5_3[] = "d69d419def35a098d9c10f8317d6709b"; |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 547 | #else |
Lei Zhang | b8e89e3 | 2017-05-02 11:53:08 -0700 | [diff] [blame] | 548 | const char md5_3[] = "8b300d3c6dfc12fa9af97e12ed5bd80a"; |
Nicolas Pena | 5bcd9a3 | 2017-03-22 11:04:35 -0400 | [diff] [blame] | 549 | #endif |
Lei Zhang | 0d6d178 | 2017-03-24 15:52:00 -0700 | [diff] [blame] | 550 | CompareBitmap(page_bitmap, 612, 792, md5_3); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 551 | FPDFBitmap_Destroy(page_bitmap); |
| 552 | |
| 553 | // TODO(npm): Why are there issues with text rotated by 90 degrees? |
| 554 | // TODO(npm): FPDF_SaveAsCopy not giving the desired result after this. |
| 555 | FPDF_ClosePage(page); |
Nicolas Pena | 4905840 | 2017-02-14 18:26:20 -0500 | [diff] [blame] | 556 | } |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 557 | |
| 558 | TEST_F(FPDFEditEmbeddertest, DoubleGenerating) { |
| 559 | // Start with a blank page |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 560 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 561 | |
| 562 | // Add a red rectangle with some non-default alpha |
| 563 | FPDF_PAGEOBJECT rect = FPDFPageObj_CreateNewRect(10, 10, 100, 100); |
| 564 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 255, 0, 0, 128)); |
| 565 | EXPECT_TRUE(FPDFPath_SetDrawMode(rect, FPDF_FILLMODE_WINDING, 0)); |
| 566 | FPDFPage_InsertObject(page, rect); |
| 567 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 568 | |
| 569 | // Check the ExtGState |
| 570 | CPDF_Page* the_page = CPDFPageFromFPDFPage(page); |
| 571 | CPDF_Dictionary* graphics_dict = |
| 572 | the_page->m_pResources->GetDictFor("ExtGState"); |
| 573 | ASSERT_TRUE(graphics_dict); |
| 574 | EXPECT_EQ(1, static_cast<int>(graphics_dict->GetCount())); |
| 575 | |
| 576 | // Check the bitmap |
| 577 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 578 | CompareBitmap(page_bitmap, 612, 792, "5384da3406d62360ffb5cac4476fff1c"); |
| 579 | FPDFBitmap_Destroy(page_bitmap); |
| 580 | |
| 581 | // Never mind, my new favorite color is blue, increase alpha |
| 582 | EXPECT_TRUE(FPDFPath_SetFillColor(rect, 0, 0, 255, 180)); |
| 583 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 584 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
| 585 | |
| 586 | // Check that bitmap displays changed content |
| 587 | page_bitmap = RenderPage(page); |
| 588 | CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c"); |
| 589 | FPDFBitmap_Destroy(page_bitmap); |
| 590 | |
| 591 | // And now generate, without changes |
| 592 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 593 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
| 594 | page_bitmap = RenderPage(page); |
| 595 | CompareBitmap(page_bitmap, 612, 792, "2e51656f5073b0bee611d9cd086aa09c"); |
| 596 | FPDFBitmap_Destroy(page_bitmap); |
| 597 | |
| 598 | // Add some text to the page |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 599 | FPDF_PAGEOBJECT text_object = |
| 600 | FPDFPageObj_NewTextObj(document(), "Arial", 12.0f); |
| 601 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 602 | GetFPDFWideString(L"Something something #text# something"); |
| 603 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 604 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 300, 300); |
| 605 | FPDFPage_InsertObject(page, text_object); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 606 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 607 | CPDF_Dictionary* font_dict = the_page->m_pResources->GetDictFor("Font"); |
| 608 | ASSERT_TRUE(font_dict); |
| 609 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 610 | |
| 611 | // Generate yet again, check dicts are reasonably sized |
| 612 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
| 613 | EXPECT_EQ(2, static_cast<int>(graphics_dict->GetCount())); |
| 614 | EXPECT_EQ(1, static_cast<int>(font_dict->GetCount())); |
| 615 | FPDF_ClosePage(page); |
Nicolas Pena | a4ad01f | 2017-02-15 16:26:48 -0500 | [diff] [blame] | 616 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 617 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 618 | TEST_F(FPDFEditEmbeddertest, LoadSimpleType1Font) { |
| 619 | CreateNewDocument(); |
| 620 | // TODO(npm): use other fonts after disallowing loading any font as any type |
| 621 | const CPDF_Font* stock_font = |
| 622 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Bold"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 623 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 624 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 625 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 626 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, false)); |
| 627 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 628 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 629 | EXPECT_TRUE(typed_font->IsType1Font()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 630 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 631 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 632 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 633 | EXPECT_EQ("Type1", font_dict->GetStringFor("Subtype")); |
| 634 | EXPECT_EQ("Times New Roman Bold", font_dict->GetStringFor("BaseFont")); |
| 635 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 636 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 637 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 638 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
| 639 | |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 640 | CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 641 | ASSERT_TRUE(widths_array); |
| 642 | ASSERT_EQ(224U, widths_array->GetCount()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 643 | EXPECT_EQ(250, widths_array->GetNumberAt(0)); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 644 | EXPECT_EQ(569, widths_array->GetNumberAt(11)); |
| 645 | EXPECT_EQ(500, widths_array->GetNumberAt(223)); |
| 646 | CheckFontDescriptor(font_dict, FPDF_FONT_TYPE1, true, false, size, data); |
| 647 | } |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 648 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 649 | TEST_F(FPDFEditEmbeddertest, LoadSimpleTrueTypeFont) { |
| 650 | CreateNewDocument(); |
| 651 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Courier"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 652 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 653 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 654 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 655 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, false)); |
| 656 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 657 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 658 | EXPECT_TRUE(typed_font->IsTrueTypeFont()); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 659 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 660 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
| 661 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 662 | EXPECT_EQ("TrueType", font_dict->GetStringFor("Subtype")); |
| 663 | EXPECT_EQ("Courier New", font_dict->GetStringFor("BaseFont")); |
| 664 | ASSERT_TRUE(font_dict->KeyExist("FirstChar")); |
| 665 | ASSERT_TRUE(font_dict->KeyExist("LastChar")); |
| 666 | EXPECT_EQ(32, font_dict->GetIntegerFor("FirstChar")); |
| 667 | EXPECT_EQ(255, font_dict->GetIntegerFor("LastChar")); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 668 | |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 669 | CPDF_Array* widths_array = font_dict->GetArrayFor("Widths"); |
| 670 | ASSERT_TRUE(widths_array); |
| 671 | ASSERT_EQ(224U, widths_array->GetCount()); |
| 672 | EXPECT_EQ(600, widths_array->GetNumberAt(33)); |
| 673 | EXPECT_EQ(600, widths_array->GetNumberAt(74)); |
| 674 | EXPECT_EQ(600, widths_array->GetNumberAt(223)); |
| 675 | CheckFontDescriptor(font_dict, FPDF_FONT_TRUETYPE, false, false, size, data); |
| 676 | } |
| 677 | |
| 678 | TEST_F(FPDFEditEmbeddertest, LoadCIDType0Font) { |
| 679 | CreateNewDocument(); |
| 680 | const CPDF_Font* stock_font = |
| 681 | CPDF_Font::GetStockFont(cpdf_doc(), "Times-Roman"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 682 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 683 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 684 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 685 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TYPE1, 1)); |
| 686 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 687 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 688 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 689 | |
| 690 | // Check font dictionary entries |
| 691 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
| 692 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 693 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 694 | EXPECT_EQ("Times New Roman-Identity-H", font_dict->GetStringFor("BaseFont")); |
| 695 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
| 696 | CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts"); |
| 697 | ASSERT_TRUE(descendant_array); |
| 698 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 699 | |
| 700 | // Check the CIDFontDict |
| 701 | CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
| 702 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 703 | EXPECT_EQ("CIDFontType0", cidfont_dict->GetStringFor("Subtype")); |
| 704 | EXPECT_EQ("Times New Roman", cidfont_dict->GetStringFor("BaseFont")); |
| 705 | CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo"); |
| 706 | ASSERT_TRUE(cidinfo_dict); |
| 707 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 708 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 709 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 710 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TYPE1, false, false, size, data); |
| 711 | |
| 712 | // Check widths |
| 713 | CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
| 714 | ASSERT_TRUE(widths_array); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 715 | EXPECT_GT(widths_array->GetCount(), 1U); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 716 | CheckCompositeFontWidths(widths_array, typed_font); |
| 717 | } |
| 718 | |
| 719 | TEST_F(FPDFEditEmbeddertest, LoadCIDType2Font) { |
| 720 | CreateNewDocument(); |
| 721 | const CPDF_Font* stock_font = |
| 722 | CPDF_Font::GetStockFont(cpdf_doc(), "Helvetica-Oblique"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 723 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 724 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 725 | |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 726 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 727 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 1)); |
| 728 | ASSERT_TRUE(font.get()); |
Nicolas Pena | 46abb66 | 2017-05-17 17:23:22 -0400 | [diff] [blame] | 729 | CPDF_Font* typed_font = static_cast<CPDF_Font*>(font.get()); |
Nicolas Pena | d03ca42 | 2017-03-06 13:54:33 -0500 | [diff] [blame] | 730 | EXPECT_TRUE(typed_font->IsCIDFont()); |
| 731 | |
| 732 | // Check font dictionary entries |
| 733 | CPDF_Dictionary* font_dict = typed_font->GetFontDict(); |
| 734 | EXPECT_EQ("Font", font_dict->GetStringFor("Type")); |
| 735 | EXPECT_EQ("Type0", font_dict->GetStringFor("Subtype")); |
| 736 | EXPECT_EQ("Arial Italic", font_dict->GetStringFor("BaseFont")); |
| 737 | EXPECT_EQ("Identity-H", font_dict->GetStringFor("Encoding")); |
| 738 | CPDF_Array* descendant_array = font_dict->GetArrayFor("DescendantFonts"); |
| 739 | ASSERT_TRUE(descendant_array); |
| 740 | EXPECT_EQ(1U, descendant_array->GetCount()); |
| 741 | |
| 742 | // Check the CIDFontDict |
| 743 | CPDF_Dictionary* cidfont_dict = descendant_array->GetDictAt(0); |
| 744 | EXPECT_EQ("Font", cidfont_dict->GetStringFor("Type")); |
| 745 | EXPECT_EQ("CIDFontType2", cidfont_dict->GetStringFor("Subtype")); |
| 746 | EXPECT_EQ("Arial Italic", cidfont_dict->GetStringFor("BaseFont")); |
| 747 | CPDF_Dictionary* cidinfo_dict = cidfont_dict->GetDictFor("CIDSystemInfo"); |
| 748 | ASSERT_TRUE(cidinfo_dict); |
| 749 | EXPECT_EQ("Adobe", cidinfo_dict->GetStringFor("Registry")); |
| 750 | EXPECT_EQ("Identity", cidinfo_dict->GetStringFor("Ordering")); |
| 751 | EXPECT_EQ(0, cidinfo_dict->GetNumberFor("Supplement")); |
| 752 | CheckFontDescriptor(cidfont_dict, FPDF_FONT_TRUETYPE, false, true, size, |
| 753 | data); |
| 754 | |
| 755 | // Check widths |
| 756 | CPDF_Array* widths_array = cidfont_dict->GetArrayFor("W"); |
| 757 | ASSERT_TRUE(widths_array); |
| 758 | CheckCompositeFontWidths(widths_array, typed_font); |
Nicolas Pena | be90aae | 2017-02-27 10:41:41 -0500 | [diff] [blame] | 759 | } |
rbpotter | ce8e51e | 2017-04-28 12:42:47 -0700 | [diff] [blame] | 760 | |
| 761 | TEST_F(FPDFEditEmbeddertest, NormalizeNegativeRotation) { |
| 762 | // Load document with a -90 degree rotation |
| 763 | EXPECT_TRUE(OpenDocument("bug_713197.pdf")); |
| 764 | FPDF_PAGE page = LoadPage(0); |
| 765 | EXPECT_NE(nullptr, page); |
| 766 | |
| 767 | EXPECT_EQ(3, FPDFPage_GetRotation(page)); |
| 768 | UnloadPage(page); |
| 769 | } |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 770 | |
| 771 | TEST_F(FPDFEditEmbeddertest, AddTrueTypeFontText) { |
| 772 | // Start with a blank page |
| 773 | FPDF_PAGE page = FPDFPage_New(CreateNewDocument(), 0, 612, 792); |
| 774 | { |
| 775 | const CPDF_Font* stock_font = CPDF_Font::GetStockFont(cpdf_doc(), "Arial"); |
Lei Zhang | d74da7b | 2017-05-04 13:30:29 -0700 | [diff] [blame] | 776 | const uint8_t* data = stock_font->GetFont()->GetFontData(); |
| 777 | const uint32_t size = stock_font->GetFont()->GetSize(); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 778 | std::unique_ptr<void, FPDFFontDeleter> font( |
| 779 | FPDFText_LoadFont(document(), data, size, FPDF_FONT_TRUETYPE, 0)); |
| 780 | ASSERT_TRUE(font.get()); |
| 781 | |
| 782 | // Add some text to the page |
| 783 | FPDF_PAGEOBJECT text_object = |
| 784 | FPDFPageObj_CreateTextObj(document(), font.get(), 12.0f); |
| 785 | EXPECT_TRUE(text_object); |
| 786 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text = |
| 787 | GetFPDFWideString(L"I am testing my loaded font, WEE."); |
| 788 | EXPECT_TRUE(FPDFText_SetText(text_object, text.get())); |
| 789 | FPDFPageObj_Transform(text_object, 1, 0, 0, 1, 400, 400); |
| 790 | FPDFPage_InsertObject(page, text_object); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 791 | FPDF_BITMAP page_bitmap = RenderPage(page); |
| 792 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 793 | const char md5[] = "17d2b6cd574cf66170b09c8927529a94"; |
| 794 | #else |
| 795 | const char md5[] = "28e5b10743660dcdfd1618db47b39d32"; |
| 796 | #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 797 | CompareBitmap(page_bitmap, 612, 792, md5); |
| 798 | FPDFBitmap_Destroy(page_bitmap); |
| 799 | |
| 800 | // Add some more text, same font |
| 801 | FPDF_PAGEOBJECT text_object2 = |
| 802 | FPDFPageObj_CreateTextObj(document(), font.get(), 15.0f); |
| 803 | std::unique_ptr<unsigned short, pdfium::FreeDeleter> text2 = |
| 804 | GetFPDFWideString(L"Bigger font size"); |
| 805 | EXPECT_TRUE(FPDFText_SetText(text_object2, text2.get())); |
| 806 | FPDFPageObj_Transform(text_object2, 1, 0, 0, 1, 200, 200); |
| 807 | FPDFPage_InsertObject(page, text_object2); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 808 | } |
| 809 | FPDF_BITMAP page_bitmap2 = RenderPage(page); |
| 810 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 811 | const char md5_2[] = "8eded4193ff1f0f77b8b600a825e97ea"; |
| 812 | #else |
| 813 | const char md5_2[] = "a068eef4110d607f77c87ea8340fa2a5"; |
| 814 | #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 815 | CompareBitmap(page_bitmap2, 612, 792, md5_2); |
| 816 | FPDFBitmap_Destroy(page_bitmap2); |
| 817 | |
Nicolas Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 818 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | b316185 | 2017-05-02 14:12:50 -0400 | [diff] [blame] | 819 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 820 | FPDF_ClosePage(page); |
| 821 | std::string new_file = GetString(); |
| 822 | |
| 823 | // Render the saved result |
| 824 | FPDF_FILEACCESS file_access; |
| 825 | memset(&file_access, 0, sizeof(file_access)); |
| 826 | file_access.m_FileLen = new_file.size(); |
| 827 | file_access.m_GetBlock = GetBlockFromString; |
| 828 | file_access.m_Param = &new_file; |
| 829 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 830 | ASSERT_NE(nullptr, new_doc); |
| 831 | EXPECT_EQ(1, FPDF_GetPageCount(new_doc)); |
| 832 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 833 | ASSERT_NE(nullptr, new_page); |
| 834 | FPDF_BITMAP new_bitmap = RenderPage(new_page); |
| 835 | CompareBitmap(new_bitmap, 612, 792, md5_2); |
| 836 | FPDFBitmap_Destroy(new_bitmap); |
| 837 | FPDF_ClosePage(new_page); |
| 838 | FPDF_CloseDocument(new_doc); |
| 839 | } |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 840 | |
| 841 | // TODO(npm): Add tests using Japanese fonts in other OS. |
| 842 | #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ |
| 843 | TEST_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 Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 884 | // Check that the text renders properly. |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 885 | 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 Pena | 207b727 | 2017-05-26 17:37:06 -0400 | [diff] [blame] | 891 | EXPECT_TRUE(FPDFPage_GenerateContent(page)); |
Nicolas Pena | f45ade3 | 2017-05-03 10:23:49 -0400 | [diff] [blame] | 892 | EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0)); |
| 893 | FPDF_ClosePage(page); |
| 894 | std::string new_file = GetString(); |
| 895 | |
| 896 | // Render the saved result |
| 897 | FPDF_FILEACCESS file_access; |
| 898 | memset(&file_access, 0, sizeof(file_access)); |
| 899 | file_access.m_FileLen = new_file.size(); |
| 900 | file_access.m_GetBlock = GetBlockFromString; |
| 901 | file_access.m_Param = &new_file; |
| 902 | FPDF_DOCUMENT new_doc = FPDF_LoadCustomDocument(&file_access, nullptr); |
| 903 | ASSERT_NE(nullptr, new_doc); |
| 904 | EXPECT_EQ(1, FPDF_GetPageCount(new_doc)); |
| 905 | FPDF_PAGE new_page = FPDF_LoadPage(new_doc, 0); |
| 906 | ASSERT_NE(nullptr, new_page); |
| 907 | FPDF_BITMAP new_bitmap = RenderPage(new_page); |
| 908 | CompareBitmap(new_bitmap, 612, 792, md5); |
| 909 | FPDFBitmap_Destroy(new_bitmap); |
| 910 | FPDF_ClosePage(new_page); |
| 911 | FPDF_CloseDocument(new_doc); |
| 912 | } |
| 913 | #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ |