1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334

pdf / pdf_transform_unittest.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "pdf/pdf_transform.h"

#include "printing/units.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size_f.h"

namespace chrome_pdf {

namespace {

constexpr float kDefaultWidth = 8.5 * printing::kPointsPerInch;
constexpr float kDefaultHeight = 11.0 * printing::kPointsPerInch;
constexpr float kDefaultRatio = kDefaultWidth / kDefaultHeight;
constexpr float kTolerance = 0.0001f;

void ExpectDefaultPortraitBox(const PdfRectangle& box) {
  EXPECT_FLOAT_EQ(0, box.left);
  EXPECT_FLOAT_EQ(0, box.bottom);
  EXPECT_FLOAT_EQ(kDefaultWidth, box.right);
  EXPECT_FLOAT_EQ(kDefaultHeight, box.top);
}

void ExpectDefaultLandscapeBox(const PdfRectangle& box) {
  EXPECT_FLOAT_EQ(0, box.left);
  EXPECT_FLOAT_EQ(0, box.bottom);
  EXPECT_FLOAT_EQ(kDefaultHeight, box.right);
  EXPECT_FLOAT_EQ(kDefaultWidth, box.top);
}

void ExpectBoxesAreEqual(const PdfRectangle& expected,
                         const PdfRectangle& actual) {
  EXPECT_FLOAT_EQ(expected.left, actual.left);
  EXPECT_FLOAT_EQ(expected.bottom, actual.bottom);
  EXPECT_FLOAT_EQ(expected.right, actual.right);
  EXPECT_FLOAT_EQ(expected.top, actual.top);
}

void InitializeBoxToInvalidValues(PdfRectangle* box) {
  box->left = box->bottom = box->right = box->top = -1;
}

void InitializeBoxToDefaultPortraitValues(PdfRectangle* box) {
  box->left = 0;
  box->bottom = 0;
  box->right = kDefaultWidth;
  box->top = kDefaultHeight;
}

void InitializeBoxToDefaultLandscapeValue(PdfRectangle* box) {
  box->left = 0;
  box->bottom = 0;
  box->right = kDefaultHeight;
  box->top = kDefaultWidth;
}

}  // namespace

TEST(PdfTransformTest, CalculateScaleFactor) {
  static constexpr gfx::SizeF kSize(kDefaultWidth, kDefaultHeight);
  gfx::Rect rect(kDefaultWidth, kDefaultHeight);
  float scale;

  // 1:1
  scale = CalculateScaleFactor(rect, kSize, false);
  EXPECT_NEAR(1.0f, scale, kTolerance);
  scale = CalculateScaleFactor(rect, kSize, true);
  EXPECT_NEAR(kDefaultRatio, scale, kTolerance);

  // 1:2
  rect = gfx::Rect(kDefaultWidth / 2, kDefaultHeight / 2);
  scale = CalculateScaleFactor(rect, kSize, false);
  EXPECT_NEAR(0.5f, scale, kTolerance);
  scale = CalculateScaleFactor(rect, kSize, true);
  EXPECT_NEAR(kDefaultRatio / 2, scale, kTolerance);

  // 3:1
  rect = gfx::Rect(kDefaultWidth * 3, kDefaultHeight * 3);
  scale = CalculateScaleFactor(rect, kSize, false);
  EXPECT_NEAR(3.0f, scale, kTolerance);
  scale = CalculateScaleFactor(rect, kSize, true);
  EXPECT_NEAR(kDefaultRatio * 3, scale, kTolerance);

  // 3:1, rotated.
  rect = gfx::Rect(kDefaultHeight * 3, kDefaultWidth * 3);
  scale = CalculateScaleFactor(rect, kSize, false);
  EXPECT_NEAR(kDefaultRatio * 3, scale, kTolerance);
  scale = CalculateScaleFactor(rect, kSize, true);
  EXPECT_NEAR(3.0f, scale, kTolerance);

  // Odd size
  rect = gfx::Rect(10, 1000);
  scale = CalculateScaleFactor(rect, kSize, false);
  EXPECT_NEAR(0.01634f, scale, kTolerance);
  scale = CalculateScaleFactor(rect, kSize, true);
  EXPECT_NEAR(0.01263f, scale, kTolerance);
}

TEST(PdfTransformTest, SetDefaultClipBox) {
  PdfRectangle box;

  SetDefaultClipBox(false, &box);
  ExpectDefaultPortraitBox(box);

  SetDefaultClipBox(true, &box);
  ExpectDefaultLandscapeBox(box);
}

TEST(PdfTransformTest, CalculateMediaBoxAndCropBox) {
  PdfRectangle media_box;
  PdfRectangle crop_box;

  // Assume both boxes are there.
  InitializeBoxToDefaultPortraitValues(&media_box);
  InitializeBoxToDefaultLandscapeValue(&crop_box);
  CalculateMediaBoxAndCropBox(true, true, true, &media_box, &crop_box);
  ExpectDefaultPortraitBox(media_box);
  ExpectDefaultLandscapeBox(crop_box);

  // Assume both boxes are missing.
  InitializeBoxToInvalidValues(&media_box);
  InitializeBoxToInvalidValues(&crop_box);
  CalculateMediaBoxAndCropBox(false, false, false, &media_box, &crop_box);
  ExpectDefaultPortraitBox(media_box);
  ExpectDefaultPortraitBox(crop_box);
  CalculateMediaBoxAndCropBox(true, false, false, &media_box, &crop_box);
  ExpectDefaultLandscapeBox(media_box);
  ExpectDefaultLandscapeBox(crop_box);

  // Assume crop box is missing.
  constexpr PdfRectangle expected_box = {0, 0, 42, 420};
  media_box = expected_box;
  InitializeBoxToInvalidValues(&crop_box);
  CalculateMediaBoxAndCropBox(false, true, false, &media_box, &crop_box);
  ExpectBoxesAreEqual(expected_box, media_box);
  ExpectBoxesAreEqual(expected_box, crop_box);

  // Assume media box is missing.
  InitializeBoxToInvalidValues(&media_box);
  CalculateMediaBoxAndCropBox(false, false, true, &media_box, &crop_box);
  ExpectBoxesAreEqual(expected_box, media_box);
  ExpectBoxesAreEqual(expected_box, crop_box);
}

TEST(PdfTransformTest, CalculateClipBoxBoundary) {
  PdfRectangle media_box;
  PdfRectangle crop_box;
  PdfRectangle result;

  // media box and crop box are the same.
  InitializeBoxToDefaultPortraitValues(&media_box);
  InitializeBoxToDefaultPortraitValues(&crop_box);
  result = CalculateClipBoxBoundary(media_box, crop_box);
  ExpectDefaultPortraitBox(result);

  // media box is portrait and crop box is landscape.
  InitializeBoxToDefaultLandscapeValue(&crop_box);
  result = CalculateClipBoxBoundary(media_box, crop_box);
  EXPECT_FLOAT_EQ(0, result.left);
  EXPECT_FLOAT_EQ(0, result.bottom);
  EXPECT_FLOAT_EQ(kDefaultWidth, result.right);
  EXPECT_FLOAT_EQ(kDefaultWidth, result.top);

  // crop box is smaller than media box.
  crop_box.left = 0;
  crop_box.bottom = 0;
  crop_box.right = 100;
  crop_box.top = 200;
  result = CalculateClipBoxBoundary(media_box, crop_box);
  EXPECT_FLOAT_EQ(0, result.left);
  EXPECT_FLOAT_EQ(0, result.bottom);
  EXPECT_FLOAT_EQ(100, result.right);
  EXPECT_FLOAT_EQ(200, result.top);

  // crop box is smaller than the media box in one dimension and longer in the
  // other.
  crop_box.left = 0;
  crop_box.bottom = 0;
  crop_box.right = 100;
  crop_box.top = 2000;
  result = CalculateClipBoxBoundary(media_box, crop_box);
  EXPECT_FLOAT_EQ(0, result.left);
  EXPECT_FLOAT_EQ(0, result.bottom);
  EXPECT_FLOAT_EQ(100, result.right);
  EXPECT_FLOAT_EQ(kDefaultHeight, result.top);
}

TEST(PdfTransformTest, CalculateScaledClipBoxOffset) {
  constexpr gfx::Rect rect(kDefaultWidth, kDefaultHeight);
  PdfRectangle clip_box;
  gfx::PointF offset;

  // `rect` and `clip_box` are the same size.
  InitializeBoxToDefaultPortraitValues(&clip_box);
  offset = CalculateScaledClipBoxOffset(rect, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());

  // `rect` is larger than `clip_box`.
  clip_box.top /= 2;
  clip_box.right /= 4;
  offset = CalculateScaledClipBoxOffset(rect, clip_box);
  EXPECT_FLOAT_EQ(229.5f, offset.x());
  EXPECT_FLOAT_EQ(198, offset.y());
}

TEST(PdfTransformTest, CalculateNonScaledClipBoxOffset) {
  int page_width = kDefaultWidth;
  int page_height = kDefaultHeight;
  PdfRectangle clip_box;
  gfx::PointF offset;

  // `rect`, page size and `clip_box` are the same.
  InitializeBoxToDefaultPortraitValues(&clip_box);
  offset =
      CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());
  offset =
      CalculateNonScaledClipBoxOffset(1, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());
  offset =
      CalculateNonScaledClipBoxOffset(2, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());
  offset =
      CalculateNonScaledClipBoxOffset(3, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(180, offset.x());
  EXPECT_FLOAT_EQ(-180, offset.y());

  // Smaller `clip_box`.
  clip_box.top /= 4;
  clip_box.right /= 2;
  offset =
      CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(594, offset.y());
  offset =
      CalculateNonScaledClipBoxOffset(1, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());
  offset =
      CalculateNonScaledClipBoxOffset(2, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(306, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());
  offset =
      CalculateNonScaledClipBoxOffset(3, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(486, offset.x());
  EXPECT_FLOAT_EQ(414, offset.y());

  // Larger page size.
  InitializeBoxToDefaultPortraitValues(&clip_box);
  page_width += 10;
  page_height += 20;
  offset =
      CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(20, offset.y());
  offset =
      CalculateNonScaledClipBoxOffset(1, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());
  offset =
      CalculateNonScaledClipBoxOffset(2, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(10, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());
  offset =
      CalculateNonScaledClipBoxOffset(3, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(200, offset.x());
  EXPECT_FLOAT_EQ(-170, offset.y());
}

// https://crbug.com/491160 and https://crbug.com/588757
TEST(PdfTransformTest, ReversedMediaBox) {
  int page_width = kDefaultWidth;
  int page_height = kDefaultHeight;
  constexpr gfx::Rect rect(kDefaultWidth, kDefaultHeight);
  PdfRectangle clip_box;
  gfx::PointF offset;

  constexpr PdfRectangle expected_media_box_b491160 = {0, -792, 612, 0};
  PdfRectangle media_box_b491160 = {0, 0, 612, -792};
  CalculateMediaBoxAndCropBox(false, true, false, &media_box_b491160,
                              &clip_box);
  ExpectBoxesAreEqual(expected_media_box_b491160, media_box_b491160);
  ExpectBoxesAreEqual(expected_media_box_b491160, clip_box);

  offset = CalculateScaledClipBoxOffset(rect, media_box_b491160);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(792, offset.y());

  offset = CalculateNonScaledClipBoxOffset(0, page_width, page_height,
                                           media_box_b491160);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(792, offset.y());

  PdfRectangle media_box_b588757 = {0, 792, 612, 0};
  CalculateMediaBoxAndCropBox(false, true, false, &media_box_b588757,
                              &clip_box);
  ExpectDefaultPortraitBox(media_box_b588757);
  ExpectDefaultPortraitBox(clip_box);

  offset = CalculateScaledClipBoxOffset(rect, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());

  offset =
      CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());

  PdfRectangle media_box_left_right_flipped = {612, 792, 0, 0};
  CalculateMediaBoxAndCropBox(false, true, false, &media_box_left_right_flipped,
                              &clip_box);
  ExpectDefaultPortraitBox(media_box_left_right_flipped);
  ExpectDefaultPortraitBox(clip_box);

  offset = CalculateScaledClipBoxOffset(rect, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());

  offset =
      CalculateNonScaledClipBoxOffset(0, page_width, page_height, clip_box);
  EXPECT_FLOAT_EQ(0, offset.x());
  EXPECT_FLOAT_EQ(0, offset.y());
}

}  // namespace chrome_pdf