1 // Copyright 2018 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 
5 #include "core/fxcrt/fx_system.h"
6 #include "public/fpdf_edit.h"
7 #include "testing/embedder_test.h"
8 
9 class FPDFEditPageEmbedderTest : public EmbedderTest {};
10 
11 // TODO(crbug.com/pdfium/11): Fix this test and enable.
12 #if defined(_SKIA_SUPPORT_) || defined(_SKIA_SUPPORT_PATHS_)
13 #define MAYBE_Rotation DISABLED_Rotation
14 #else
15 #define MAYBE_Rotation Rotation
16 #endif
TEST_F(FPDFEditPageEmbedderTest,MAYBE_Rotation)17 TEST_F(FPDFEditPageEmbedderTest, MAYBE_Rotation) {
18   const char kOriginalMD5[] = "0a90de37f52127619c3dfb642b5fa2fe";
19   const char kRotatedMD5[] = "d599429574ff0dcad3bc898ea8b874ca";
20 
21   {
22     ASSERT_TRUE(OpenDocument("rectangles.pdf"));
23     FPDF_PAGE page = LoadPage(0);
24     ASSERT_TRUE(page);
25 
26     {
27       // Render the page as is.
28       EXPECT_EQ(0, FPDFPage_GetRotation(page));
29       const int page_width = static_cast<int>(FPDF_GetPageWidth(page));
30       const int page_height = static_cast<int>(FPDF_GetPageHeight(page));
31       EXPECT_EQ(200, page_width);
32       EXPECT_EQ(300, page_height);
33       ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
34       CompareBitmap(bitmap.get(), page_width, page_height, kOriginalMD5);
35     }
36 
37     FPDFPage_SetRotation(page, 1);
38 
39     {
40       // Render the page after rotation.
41       // Note that the change affects the rendering, as expected.
42       // It behaves just like the case below, rather than the case above.
43       EXPECT_EQ(1, FPDFPage_GetRotation(page));
44       const int page_width = static_cast<int>(FPDF_GetPageWidth(page));
45       const int page_height = static_cast<int>(FPDF_GetPageHeight(page));
46       EXPECT_EQ(300, page_width);
47       EXPECT_EQ(200, page_height);
48       ScopedFPDFBitmap bitmap = RenderLoadedPage(page);
49       CompareBitmap(bitmap.get(), page_width, page_height, kRotatedMD5);
50     }
51 
52     UnloadPage(page);
53   }
54 
55   {
56     // Save a copy, open the copy, and render it.
57     // Note that it renders the rotation.
58     EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
59     ASSERT_TRUE(OpenSavedDocument());
60     FPDF_PAGE saved_page = LoadSavedPage(0);
61     ASSERT_TRUE(saved_page);
62 
63     EXPECT_EQ(1, FPDFPage_GetRotation(saved_page));
64     const int page_width = static_cast<int>(FPDF_GetPageWidth(saved_page));
65     const int page_height = static_cast<int>(FPDF_GetPageHeight(saved_page));
66     EXPECT_EQ(300, page_width);
67     EXPECT_EQ(200, page_height);
68     ScopedFPDFBitmap bitmap = RenderSavedPage(saved_page);
69     CompareBitmap(bitmap.get(), page_width, page_height, kRotatedMD5);
70 
71     CloseSavedPage(saved_page);
72     CloseSavedDocument();
73   }
74 }
75 
TEST_F(FPDFEditPageEmbedderTest,HasTransparencyImage)76 TEST_F(FPDFEditPageEmbedderTest, HasTransparencyImage) {
77   constexpr int kExpectedObjectCount = 39;
78   ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
79   FPDF_PAGE page = LoadPage(0);
80   ASSERT_TRUE(page);
81   ASSERT_EQ(kExpectedObjectCount, FPDFPage_CountObjects(page));
82 
83   for (int i = 0; i < kExpectedObjectCount; ++i) {
84     FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, i);
85     EXPECT_FALSE(FPDFPageObj_HasTransparency(obj));
86 
87     FPDFPageObj_SetFillColor(obj, 255, 0, 0, 127);
88     EXPECT_TRUE(FPDFPageObj_HasTransparency(obj));
89   }
90 
91   UnloadPage(page);
92 }
93 
TEST_F(FPDFEditPageEmbedderTest,HasTransparencyInvalid)94 TEST_F(FPDFEditPageEmbedderTest, HasTransparencyInvalid) {
95   EXPECT_FALSE(FPDFPageObj_HasTransparency(nullptr));
96 }
97 
TEST_F(FPDFEditPageEmbedderTest,HasTransparencyPath)98 TEST_F(FPDFEditPageEmbedderTest, HasTransparencyPath) {
99   constexpr int kExpectedObjectCount = 8;
100   EXPECT_TRUE(OpenDocument("rectangles.pdf"));
101   FPDF_PAGE page = LoadPage(0);
102   ASSERT_TRUE(page);
103   ASSERT_EQ(kExpectedObjectCount, FPDFPage_CountObjects(page));
104 
105   for (int i = 0; i < kExpectedObjectCount; ++i) {
106     FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, i);
107     EXPECT_FALSE(FPDFPageObj_HasTransparency(obj));
108 
109     FPDFPageObj_SetStrokeColor(obj, 63, 63, 0, 127);
110     EXPECT_TRUE(FPDFPageObj_HasTransparency(obj));
111   }
112 
113   UnloadPage(page);
114 }
115 
TEST_F(FPDFEditPageEmbedderTest,HasTransparencyText)116 TEST_F(FPDFEditPageEmbedderTest, HasTransparencyText) {
117   constexpr int kExpectedObjectCount = 2;
118   EXPECT_TRUE(OpenDocument("text_render_mode.pdf"));
119   FPDF_PAGE page = LoadPage(0);
120   ASSERT_TRUE(page);
121   ASSERT_EQ(kExpectedObjectCount, FPDFPage_CountObjects(page));
122 
123   for (int i = 0; i < kExpectedObjectCount; ++i) {
124     FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page, i);
125     EXPECT_FALSE(FPDFPageObj_HasTransparency(obj));
126 
127     FPDFPageObj_SetBlendMode(obj, "Lighten");
128     EXPECT_TRUE(FPDFPageObj_HasTransparency(obj));
129   }
130 
131   UnloadPage(page);
132 }
133 
TEST_F(FPDFEditPageEmbedderTest,GetFillAndStrokeForImage)134 TEST_F(FPDFEditPageEmbedderTest, GetFillAndStrokeForImage) {
135   constexpr int kExpectedObjectCount = 39;
136   constexpr int kImageObjectsStartIndex = 33;
137   ASSERT_TRUE(OpenDocument("embedded_images.pdf"));
138   FPDF_PAGE page = LoadPage(0);
139   ASSERT_TRUE(page);
140 
141   ASSERT_EQ(kExpectedObjectCount, FPDFPage_CountObjects(page));
142 
143   for (int i = kImageObjectsStartIndex; i < kExpectedObjectCount; ++i) {
144     FPDF_PAGEOBJECT image = FPDFPage_GetObject(page, i);
145     ASSERT_TRUE(image);
146     EXPECT_EQ(FPDF_PAGEOBJ_IMAGE, FPDFPageObj_GetType(image));
147 
148     unsigned int r;
149     unsigned int g;
150     unsigned int b;
151     unsigned int a;
152     EXPECT_FALSE(FPDFPageObj_GetFillColor(image, &r, &g, &b, &a));
153     EXPECT_FALSE(FPDFPageObj_GetStrokeColor(image, &r, &g, &b, &a));
154   }
155 
156   UnloadPage(page);
157 }
158