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