Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lei Zhang | cc9acc1 | 2018-03-05 20:49:13 | [diff] [blame] | 5 | #include "pdf/pdf_transform.h" |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 6 | |
| 7 | #include "printing/units.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 9 | #include "ui/gfx/geometry/point_f.h" |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 10 | #include "ui/gfx/geometry/rect.h" |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 11 | #include "ui/gfx/geometry/size_f.h" |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 12 | |
Lei Zhang | cc9acc1 | 2018-03-05 20:49:13 | [diff] [blame] | 13 | namespace chrome_pdf { |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 14 | |
| 15 | namespace { |
| 16 | |
Henrique Nakashima | 1c5da8e | 2018-10-05 21:56:43 | [diff] [blame] | 17 | constexpr float kDefaultWidth = 8.5 * printing::kPointsPerInch; |
| 18 | constexpr float kDefaultHeight = 11.0 * printing::kPointsPerInch; |
| 19 | constexpr float kDefaultRatio = kDefaultWidth / kDefaultHeight; |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 20 | constexpr float kTolerance = 0.0001f; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 21 | |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 22 | void ExpectDefaultPortraitBox(const PdfRectangle& box) { |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 23 | EXPECT_FLOAT_EQ(0, box.left); |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 24 | EXPECT_FLOAT_EQ(0, box.bottom); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 25 | EXPECT_FLOAT_EQ(kDefaultWidth, box.right); |
| 26 | EXPECT_FLOAT_EQ(kDefaultHeight, box.top); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 27 | } |
| 28 | |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 29 | void ExpectDefaultLandscapeBox(const PdfRectangle& box) { |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 30 | EXPECT_FLOAT_EQ(0, box.left); |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 31 | EXPECT_FLOAT_EQ(0, box.bottom); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 32 | EXPECT_FLOAT_EQ(kDefaultHeight, box.right); |
| 33 | EXPECT_FLOAT_EQ(kDefaultWidth, box.top); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 34 | } |
| 35 | |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 36 | void ExpectBoxesAreEqual(const PdfRectangle& expected, |
| 37 | const PdfRectangle& actual) { |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 38 | EXPECT_FLOAT_EQ(expected.left, actual.left); |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 39 | EXPECT_FLOAT_EQ(expected.bottom, actual.bottom); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 40 | EXPECT_FLOAT_EQ(expected.right, actual.right); |
| 41 | EXPECT_FLOAT_EQ(expected.top, actual.top); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 42 | } |
| 43 | |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 44 | void InitializeBoxToInvalidValues(PdfRectangle* box) { |
| 45 | box->left = box->bottom = box->right = box->top = -1; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 46 | } |
| 47 | |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 48 | void InitializeBoxToDefaultPortraitValues(PdfRectangle* box) { |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 49 | box->left = 0; |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 50 | box->bottom = 0; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 51 | box->right = kDefaultWidth; |
| 52 | box->top = kDefaultHeight; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 53 | } |
| 54 | |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 55 | void InitializeBoxToDefaultLandscapeValue(PdfRectangle* box) { |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 56 | box->left = 0; |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 57 | box->bottom = 0; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 58 | box->right = kDefaultHeight; |
| 59 | box->top = kDefaultWidth; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | } // namespace |
| 63 | |
| 64 | TEST(PdfTransformTest, CalculateScaleFactor) { |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 65 | static constexpr gfx::SizeF kSize(kDefaultWidth, kDefaultHeight); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 66 | gfx::Rect rect(kDefaultWidth, kDefaultHeight); |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 67 | float scale; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 68 | |
| 69 | // 1:1 |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 70 | scale = CalculateScaleFactor(rect, kSize, false); |
| 71 | EXPECT_NEAR(1.0f, scale, kTolerance); |
| 72 | scale = CalculateScaleFactor(rect, kSize, true); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 73 | EXPECT_NEAR(kDefaultRatio, scale, kTolerance); |
| 74 | |
| 75 | // 1:2 |
| 76 | rect = gfx::Rect(kDefaultWidth / 2, kDefaultHeight / 2); |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 77 | scale = CalculateScaleFactor(rect, kSize, false); |
| 78 | EXPECT_NEAR(0.5f, scale, kTolerance); |
| 79 | scale = CalculateScaleFactor(rect, kSize, true); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 80 | EXPECT_NEAR(kDefaultRatio / 2, scale, kTolerance); |
| 81 | |
| 82 | // 3:1 |
| 83 | rect = gfx::Rect(kDefaultWidth * 3, kDefaultHeight * 3); |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 84 | scale = CalculateScaleFactor(rect, kSize, false); |
| 85 | EXPECT_NEAR(3.0f, scale, kTolerance); |
| 86 | scale = CalculateScaleFactor(rect, kSize, true); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 87 | EXPECT_NEAR(kDefaultRatio * 3, scale, kTolerance); |
| 88 | |
| 89 | // 3:1, rotated. |
| 90 | rect = gfx::Rect(kDefaultHeight * 3, kDefaultWidth * 3); |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 91 | scale = CalculateScaleFactor(rect, kSize, false); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 92 | EXPECT_NEAR(kDefaultRatio * 3, scale, kTolerance); |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 93 | scale = CalculateScaleFactor(rect, kSize, true); |
| 94 | EXPECT_NEAR(3.0f, scale, kTolerance); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 95 | |
| 96 | // Odd size |
| 97 | rect = gfx::Rect(10, 1000); |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 98 | scale = CalculateScaleFactor(rect, kSize, false); |
| 99 | EXPECT_NEAR(0.01634f, scale, kTolerance); |
| 100 | scale = CalculateScaleFactor(rect, kSize, true); |
| 101 | EXPECT_NEAR(0.01263f, scale, kTolerance); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | TEST(PdfTransformTest, SetDefaultClipBox) { |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 105 | PdfRectangle box; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 106 | |
| 107 | SetDefaultClipBox(false, &box); |
| 108 | ExpectDefaultPortraitBox(box); |
| 109 | |
| 110 | SetDefaultClipBox(true, &box); |
| 111 | ExpectDefaultLandscapeBox(box); |
| 112 | } |
| 113 | |
| 114 | TEST(PdfTransformTest, CalculateMediaBoxAndCropBox) { |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 115 | PdfRectangle media_box; |
| 116 | PdfRectangle crop_box; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 117 | |
| 118 | // Assume both boxes are there. |
| 119 | InitializeBoxToDefaultPortraitValues(&media_box); |
| 120 | InitializeBoxToDefaultLandscapeValue(&crop_box); |
| 121 | CalculateMediaBoxAndCropBox(true, true, true, &media_box, &crop_box); |
| 122 | ExpectDefaultPortraitBox(media_box); |
| 123 | ExpectDefaultLandscapeBox(crop_box); |
| 124 | |
| 125 | // Assume both boxes are missing. |
| 126 | InitializeBoxToInvalidValues(&media_box); |
| 127 | InitializeBoxToInvalidValues(&crop_box); |
| 128 | CalculateMediaBoxAndCropBox(false, false, false, &media_box, &crop_box); |
| 129 | ExpectDefaultPortraitBox(media_box); |
| 130 | ExpectDefaultPortraitBox(crop_box); |
| 131 | CalculateMediaBoxAndCropBox(true, false, false, &media_box, &crop_box); |
| 132 | ExpectDefaultLandscapeBox(media_box); |
| 133 | ExpectDefaultLandscapeBox(crop_box); |
| 134 | |
| 135 | // Assume crop box is missing. |
Henrique Nakashima | 1c5da8e | 2018-10-05 21:56:43 | [diff] [blame] | 136 | constexpr PdfRectangle expected_box = {0, 0, 42, 420}; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 137 | media_box = expected_box; |
| 138 | InitializeBoxToInvalidValues(&crop_box); |
| 139 | CalculateMediaBoxAndCropBox(false, true, false, &media_box, &crop_box); |
| 140 | ExpectBoxesAreEqual(expected_box, media_box); |
| 141 | ExpectBoxesAreEqual(expected_box, crop_box); |
| 142 | |
| 143 | // Assume media box is missing. |
| 144 | InitializeBoxToInvalidValues(&media_box); |
| 145 | CalculateMediaBoxAndCropBox(false, false, true, &media_box, &crop_box); |
| 146 | ExpectBoxesAreEqual(expected_box, media_box); |
| 147 | ExpectBoxesAreEqual(expected_box, crop_box); |
| 148 | } |
| 149 | |
| 150 | TEST(PdfTransformTest, CalculateClipBoxBoundary) { |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 151 | PdfRectangle media_box; |
| 152 | PdfRectangle crop_box; |
| 153 | PdfRectangle result; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 154 | |
| 155 | // media box and crop box are the same. |
| 156 | InitializeBoxToDefaultPortraitValues(&media_box); |
| 157 | InitializeBoxToDefaultPortraitValues(&crop_box); |
| 158 | result = CalculateClipBoxBoundary(media_box, crop_box); |
| 159 | ExpectDefaultPortraitBox(result); |
| 160 | |
| 161 | // media box is portrait and crop box is landscape. |
| 162 | InitializeBoxToDefaultLandscapeValue(&crop_box); |
| 163 | result = CalculateClipBoxBoundary(media_box, crop_box); |
| 164 | EXPECT_FLOAT_EQ(0, result.left); |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 165 | EXPECT_FLOAT_EQ(0, result.bottom); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 166 | EXPECT_FLOAT_EQ(kDefaultWidth, result.right); |
| 167 | EXPECT_FLOAT_EQ(kDefaultWidth, result.top); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 168 | |
| 169 | // crop box is smaller than media box. |
| 170 | crop_box.left = 0; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 171 | crop_box.bottom = 0; |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 172 | crop_box.right = 100; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 173 | crop_box.top = 200; |
| 174 | result = CalculateClipBoxBoundary(media_box, crop_box); |
| 175 | EXPECT_FLOAT_EQ(0, result.left); |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 176 | EXPECT_FLOAT_EQ(0, result.bottom); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 177 | EXPECT_FLOAT_EQ(100, result.right); |
| 178 | EXPECT_FLOAT_EQ(200, result.top); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 179 | |
| 180 | // crop box is smaller than the media box in one dimension and longer in the |
| 181 | // other. |
| 182 | crop_box.left = 0; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 183 | crop_box.bottom = 0; |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 184 | crop_box.right = 100; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 185 | crop_box.top = 2000; |
| 186 | result = CalculateClipBoxBoundary(media_box, crop_box); |
| 187 | EXPECT_FLOAT_EQ(0, result.left); |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 188 | EXPECT_FLOAT_EQ(0, result.bottom); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 189 | EXPECT_FLOAT_EQ(100, result.right); |
| 190 | EXPECT_FLOAT_EQ(kDefaultHeight, result.top); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | TEST(PdfTransformTest, CalculateScaledClipBoxOffset) { |
Henrique Nakashima | 1c5da8e | 2018-10-05 21:56:43 | [diff] [blame] | 194 | constexpr gfx::Rect rect(kDefaultWidth, kDefaultHeight); |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 195 | PdfRectangle clip_box; |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 196 | gfx::PointF offset; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 197 | |
Daniel Hosseinian | e257d96 | 2021-04-23 21:18:35 | [diff] [blame] | 198 | // `rect` and `clip_box` are the same size. |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 199 | InitializeBoxToDefaultPortraitValues(&clip_box); |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 200 | offset = CalculateScaledClipBoxOffset(rect, clip_box); |
| 201 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 202 | EXPECT_FLOAT_EQ(0, offset.y()); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 203 | |
Daniel Hosseinian | e257d96 | 2021-04-23 21:18:35 | [diff] [blame] | 204 | // `rect` is larger than `clip_box`. |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 205 | clip_box.top /= 2; |
| 206 | clip_box.right /= 4; |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 207 | offset = CalculateScaledClipBoxOffset(rect, clip_box); |
| 208 | EXPECT_FLOAT_EQ(229.5f, offset.x()); |
| 209 | EXPECT_FLOAT_EQ(198, offset.y()); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | TEST(PdfTransformTest, CalculateNonScaledClipBoxOffset) { |
| 213 | int page_width = kDefaultWidth; |
| 214 | int page_height = kDefaultHeight; |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 215 | PdfRectangle clip_box; |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 216 | gfx::PointF offset; |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 217 | |
Daniel Hosseinian | e257d96 | 2021-04-23 21:18:35 | [diff] [blame] | 218 | // `rect`, page size and `clip_box` are the same. |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 219 | InitializeBoxToDefaultPortraitValues(&clip_box); |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 220 | offset = |
| 221 | CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box); |
| 222 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 223 | EXPECT_FLOAT_EQ(0, offset.y()); |
| 224 | offset = |
| 225 | CalculateNonScaledClipBoxOffset(1, page_width, page_height, clip_box); |
| 226 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 227 | EXPECT_FLOAT_EQ(0, offset.y()); |
| 228 | offset = |
| 229 | CalculateNonScaledClipBoxOffset(2, page_width, page_height, clip_box); |
| 230 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 231 | EXPECT_FLOAT_EQ(0, offset.y()); |
| 232 | offset = |
| 233 | CalculateNonScaledClipBoxOffset(3, page_width, page_height, clip_box); |
| 234 | EXPECT_FLOAT_EQ(180, offset.x()); |
| 235 | EXPECT_FLOAT_EQ(-180, offset.y()); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 236 | |
Daniel Hosseinian | e257d96 | 2021-04-23 21:18:35 | [diff] [blame] | 237 | // Smaller `clip_box`. |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 238 | clip_box.top /= 4; |
| 239 | clip_box.right /= 2; |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 240 | offset = |
| 241 | CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box); |
| 242 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 243 | EXPECT_FLOAT_EQ(594, offset.y()); |
| 244 | offset = |
| 245 | CalculateNonScaledClipBoxOffset(1, page_width, page_height, clip_box); |
| 246 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 247 | EXPECT_FLOAT_EQ(0, offset.y()); |
| 248 | offset = |
| 249 | CalculateNonScaledClipBoxOffset(2, page_width, page_height, clip_box); |
| 250 | EXPECT_FLOAT_EQ(306, offset.x()); |
| 251 | EXPECT_FLOAT_EQ(0, offset.y()); |
| 252 | offset = |
| 253 | CalculateNonScaledClipBoxOffset(3, page_width, page_height, clip_box); |
| 254 | EXPECT_FLOAT_EQ(486, offset.x()); |
| 255 | EXPECT_FLOAT_EQ(414, offset.y()); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 256 | |
| 257 | // Larger page size. |
| 258 | InitializeBoxToDefaultPortraitValues(&clip_box); |
| 259 | page_width += 10; |
| 260 | page_height += 20; |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 261 | offset = |
| 262 | CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box); |
| 263 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 264 | EXPECT_FLOAT_EQ(20, offset.y()); |
| 265 | offset = |
| 266 | CalculateNonScaledClipBoxOffset(1, page_width, page_height, clip_box); |
| 267 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 268 | EXPECT_FLOAT_EQ(0, offset.y()); |
| 269 | offset = |
| 270 | CalculateNonScaledClipBoxOffset(2, page_width, page_height, clip_box); |
| 271 | EXPECT_FLOAT_EQ(10, offset.x()); |
| 272 | EXPECT_FLOAT_EQ(0, offset.y()); |
| 273 | offset = |
| 274 | CalculateNonScaledClipBoxOffset(3, page_width, page_height, clip_box); |
| 275 | EXPECT_FLOAT_EQ(200, offset.x()); |
| 276 | EXPECT_FLOAT_EQ(-170, offset.y()); |
thestig | 64c8e26 | 2015-10-28 19:04:26 | [diff] [blame] | 277 | } |
| 278 | |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 279 | // https://crbug.com/491160 and https://crbug.com/588757 |
| 280 | TEST(PdfTransformTest, ReversedMediaBox) { |
| 281 | int page_width = kDefaultWidth; |
| 282 | int page_height = kDefaultHeight; |
Henrique Nakashima | 1c5da8e | 2018-10-05 21:56:43 | [diff] [blame] | 283 | constexpr gfx::Rect rect(kDefaultWidth, kDefaultHeight); |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 284 | PdfRectangle clip_box; |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 285 | gfx::PointF offset; |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 286 | |
Henrique Nakashima | 1c5da8e | 2018-10-05 21:56:43 | [diff] [blame] | 287 | constexpr PdfRectangle expected_media_box_b491160 = {0, -792, 612, 0}; |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 288 | PdfRectangle media_box_b491160 = {0, 0, 612, -792}; |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 289 | CalculateMediaBoxAndCropBox(false, true, false, &media_box_b491160, |
| 290 | &clip_box); |
| 291 | ExpectBoxesAreEqual(expected_media_box_b491160, media_box_b491160); |
| 292 | ExpectBoxesAreEqual(expected_media_box_b491160, clip_box); |
| 293 | |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 294 | offset = CalculateScaledClipBoxOffset(rect, media_box_b491160); |
| 295 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 296 | EXPECT_FLOAT_EQ(792, offset.y()); |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 297 | |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 298 | offset = CalculateNonScaledClipBoxOffset(0, page_width, page_height, |
| 299 | media_box_b491160); |
| 300 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 301 | EXPECT_FLOAT_EQ(792, offset.y()); |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 302 | |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 303 | PdfRectangle media_box_b588757 = {0, 792, 612, 0}; |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 304 | CalculateMediaBoxAndCropBox(false, true, false, &media_box_b588757, |
| 305 | &clip_box); |
| 306 | ExpectDefaultPortraitBox(media_box_b588757); |
| 307 | ExpectDefaultPortraitBox(clip_box); |
| 308 | |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 309 | offset = CalculateScaledClipBoxOffset(rect, clip_box); |
| 310 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 311 | EXPECT_FLOAT_EQ(0, offset.y()); |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 312 | |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 313 | offset = |
| 314 | CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box); |
| 315 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 316 | EXPECT_FLOAT_EQ(0, offset.y()); |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 317 | |
thestig | 09649fa | 2016-04-13 03:26:49 | [diff] [blame] | 318 | PdfRectangle media_box_left_right_flipped = {612, 792, 0, 0}; |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 319 | CalculateMediaBoxAndCropBox(false, true, false, &media_box_left_right_flipped, |
| 320 | &clip_box); |
| 321 | ExpectDefaultPortraitBox(media_box_left_right_flipped); |
| 322 | ExpectDefaultPortraitBox(clip_box); |
| 323 | |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 324 | offset = CalculateScaledClipBoxOffset(rect, clip_box); |
| 325 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 326 | EXPECT_FLOAT_EQ(0, offset.y()); |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 327 | |
Lei Zhang | 63443b20 | 2019-12-10 23:59:35 | [diff] [blame] | 328 | offset = |
| 329 | CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box); |
| 330 | EXPECT_FLOAT_EQ(0, offset.x()); |
| 331 | EXPECT_FLOAT_EQ(0, offset.y()); |
thestig | b923f18 | 2016-04-05 00:23:33 | [diff] [blame] | 332 | } |
| 333 | |
Lei Zhang | cc9acc1 | 2018-03-05 20:49:13 | [diff] [blame] | 334 | } // namespace chrome_pdf |