Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fpdf_editimg_embeddertest.cpp
Go to the documentation of this file.
1// Copyright 2016 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "public/fpdf_edit.h"
6
7#include "public/cpp/fpdf_scopers.h"
8#include "testing/embedder_test.h"
9#include "testing/utils/file_util.h"
10
11class PDFEditImgTest : public EmbedderTest {};
12
14 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
15 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
16 EXPECT_EQ(0, FPDFPage_CountObjects(page.get()));
17
18 FPDFPage_InsertObject(nullptr, nullptr);
19 EXPECT_EQ(0, FPDFPage_CountObjects(page.get()));
20
21 FPDFPage_InsertObject(page.get(), nullptr);
22 EXPECT_EQ(0, FPDFPage_CountObjects(page.get()));
23
24 FPDFPage_InsertObject(nullptr, FPDFPageObj_NewImageObj(doc.get()));
25 EXPECT_EQ(0, FPDFPage_CountObjects(page.get()));
26}
27
29 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
30 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
31 EXPECT_EQ(0, FPDFPage_CountObjects(page.get()));
32
33 FPDFPage_InsertObject(page.get(), FPDFPageObj_NewImageObj(doc.get()));
34 EXPECT_EQ(1, FPDFPage_CountObjects(page.get()));
35 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
36}
37
39 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
40 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
41 EXPECT_EQ(0, FPDFPage_CountObjects(page.get()));
42
43 constexpr int kBitmapSize = 50;
44 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(kBitmapSize, kBitmapSize, 0));
45 FPDFBitmap_FillRect(bitmap.get(), 0, 0, kBitmapSize, kBitmapSize, 0x00000000);
46 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetWidth(bitmap.get()));
47 EXPECT_EQ(kBitmapSize, FPDFBitmap_GetHeight(bitmap.get()));
48
49 ScopedFPDFPageObject page_image(FPDFPageObj_NewImageObj(doc.get()));
50 FPDF_PAGE pages_array[] = {page.get()};
51 ASSERT_TRUE(
52 FPDFImageObj_SetBitmap(pages_array, 0, page_image.get(), bitmap.get()));
53 static constexpr FS_MATRIX kScaleBitmapMatrix{kBitmapSize, 0, 0,
54 kBitmapSize, 0, 0};
55 ASSERT_TRUE(FPDFPageObj_SetMatrix(page_image.get(), &kScaleBitmapMatrix));
56 FPDFPage_InsertObject(page.get(), page_image.release());
57 EXPECT_EQ(1, FPDFPage_CountObjects(page.get()));
58 EXPECT_TRUE(FPDFPage_GenerateContent(page.get()));
59}
60
62 CreateEmptyDocumentWithoutFormFillEnvironment();
63 constexpr int kPageWidth = 200;
64 constexpr int kPageHeight = 200;
65 ScopedFPDFPage page(FPDFPage_New(document(), 0, kPageWidth, kPageHeight));
66 ASSERT_TRUE(page);
67
68 ScopedFPDFPageObject image(FPDFPageObj_NewImageObj(document()));
69 ASSERT_TRUE(image);
70
71 FileAccessForTesting file_access("mona_lisa.jpg");
72 FPDF_PAGE temp_page = page.get();
73 EXPECT_TRUE(
74 FPDFImageObj_LoadJpegFile(&temp_page, 1, image.get(), &file_access));
75
76 constexpr int kImageWidth = 120;
77 constexpr int kImageHeight = 120;
78 const char kImageChecksum[] = "58589c36b3b27a0058f5ca1fbed4d5e5";
79 const char kPageChecksum[] = "52b3a852f39c5fa9143e59d805dcb343";
80 {
81 ScopedFPDFBitmap image_bitmap(FPDFImageObj_GetBitmap(image.get()));
82 CompareBitmap(image_bitmap.get(), kImageWidth, kImageHeight,
83 kImageChecksum);
84 }
85
86 FPDFImageObj_SetMatrix(image.get(), kImageWidth, 0, 0, kImageHeight, 0, 0);
87 FPDFPage_InsertObject(page.get(), image.release());
88 FPDFPage_GenerateContent(page.get());
89 {
90 ScopedFPDFBitmap page_bitmap = RenderPage(page.get());
91 CompareBitmap(page_bitmap.get(), kPageWidth, kPageHeight, kPageChecksum);
92 }
93
94 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
95 VerifySavedDocument(kPageWidth, kPageHeight, kPageChecksum);
96}
97
99 CreateEmptyDocumentWithoutFormFillEnvironment();
100 constexpr int kPageWidth = 200;
101 constexpr int kPageHeight = 200;
102 ScopedFPDFPage page(FPDFPage_New(document(), 0, kPageWidth, kPageHeight));
103 ASSERT_TRUE(page);
104
105 ScopedFPDFPageObject image(FPDFPageObj_NewImageObj(document()));
106 ASSERT_TRUE(image);
107
108 FileAccessForTesting file_access("mona_lisa.jpg");
109 FPDF_PAGE temp_page = page.get();
110 EXPECT_TRUE(FPDFImageObj_LoadJpegFileInline(&temp_page, 1, image.get(),
111 &file_access));
112
113 constexpr int kImageWidth = 120;
114 constexpr int kImageHeight = 120;
115 const char kImageChecksum[] = "58589c36b3b27a0058f5ca1fbed4d5e5";
116 const char kPageChecksum[] = "52b3a852f39c5fa9143e59d805dcb343";
117 {
118 ScopedFPDFBitmap image_bitmap(FPDFImageObj_GetBitmap(image.get()));
119 CompareBitmap(image_bitmap.get(), kImageWidth, kImageHeight,
120 kImageChecksum);
121 }
122
123 FPDFImageObj_SetMatrix(image.get(), kImageWidth, 0, 0, kImageHeight, 0, 0);
124 FPDFPage_InsertObject(page.get(), image.release());
125 FPDFPage_GenerateContent(page.get());
126 {
127 ScopedFPDFBitmap page_bitmap = RenderPage(page.get());
128 CompareBitmap(page_bitmap.get(), kPageWidth, kPageHeight, kPageChecksum);
129 }
130
131 ASSERT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
132 VerifySavedDocument(kPageWidth, kPageHeight, kPageChecksum);
133}
134
136 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
137 ScopedFPDFPage page(FPDFPage_New(doc.get(), 0, 100, 100));
138 ScopedFPDFPageObject image(FPDFPageObj_NewImageObj(doc.get()));
139 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(100, 100, 0));
140
141 FPDF_PAGE page_ptr = page.get();
142 FPDF_PAGE* pages = &page_ptr;
143 EXPECT_TRUE(FPDFImageObj_SetBitmap(nullptr, 1, image.get(), bitmap.get()));
144 EXPECT_TRUE(FPDFImageObj_SetBitmap(pages, 0, image.get(), bitmap.get()));
145 EXPECT_FALSE(FPDFImageObj_SetBitmap(pages, 1, nullptr, bitmap.get()));
146 EXPECT_FALSE(FPDFImageObj_SetBitmap(pages, 1, image.get(), nullptr));
147}
148
150 ScopedFPDFDocument doc(FPDF_CreateNewDocument());
151 ScopedFPDFPageObject image(FPDFPageObj_NewImageObj(doc.get()));
152
153 FS_MATRIX matrix;
154 EXPECT_FALSE(FPDFPageObj_GetMatrix(nullptr, nullptr));
155 EXPECT_FALSE(FPDFPageObj_GetMatrix(nullptr, &matrix));
156 EXPECT_FALSE(FPDFPageObj_GetMatrix(image.get(), nullptr));
157
158 EXPECT_TRUE(FPDFPageObj_GetMatrix(image.get(), &matrix));
159 EXPECT_FLOAT_EQ(1.0f, matrix.a);
160 EXPECT_FLOAT_EQ(0.0f, matrix.b);
161 EXPECT_FLOAT_EQ(0.0f, matrix.c);
162 EXPECT_FLOAT_EQ(1.0f, matrix.d);
163 EXPECT_FLOAT_EQ(0.0f, matrix.e);
164 EXPECT_FLOAT_EQ(0.0f, matrix.f);
165
166 static constexpr FS_MATRIX kMatrix{1, 2, 3, 4, 5, 6};
167 EXPECT_TRUE(FPDFPageObj_SetMatrix(image.get(), &kMatrix));
168 EXPECT_TRUE(FPDFPageObj_GetMatrix(image.get(), &matrix));
169 EXPECT_FLOAT_EQ(1.0f, matrix.a);
170 EXPECT_FLOAT_EQ(2.0f, matrix.b);
171 EXPECT_FLOAT_EQ(3.0f, matrix.c);
172 EXPECT_FLOAT_EQ(4.0f, matrix.d);
173 EXPECT_FLOAT_EQ(5.0f, matrix.e);
174 EXPECT_FLOAT_EQ(6.0f, matrix.f);
175}
FPDF_EXPORT void FPDF_CALLCONV FPDFPage_InsertObject(FPDF_PAGE page, FPDF_PAGEOBJECT page_object)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPageObj_GetMatrix(FPDF_PAGEOBJECT page_object, FS_MATRIX *matrix)
TEST_F(PDFEditImgTest, InsertObjectWithInvalidPage)