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