0

Move PDF transform code from printing/ to pdf/

When the code was written, pdf_unittests did not exist, so the code
ended up in printing/ since printing_unittests existed, and the code is
used for printing PDFs.

Change-Id: I31e01524904da6ded6dffa22f80d7c68580c58a9
Reviewed-on: https://chromium-review.googlesource.com/938852
Reviewed-by: dsinclair <dsinclair@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#540926}
This commit is contained in:
Lei Zhang
2018-03-05 20:49:13 +00:00
committed by Commit Bot
parent c8fc0aecdd
commit cc9acc1e2a
8 changed files with 87 additions and 102 deletions

@@ -52,6 +52,8 @@ if (enable_pdf) {
"pdf_engine.h", "pdf_engine.h",
"pdf_ppapi.cc", "pdf_ppapi.cc",
"pdf_ppapi.h", "pdf_ppapi.h",
"pdf_transform.cc",
"pdf_transform.h",
"preview_mode_client.cc", "preview_mode_client.cc",
"preview_mode_client.h", "preview_mode_client.h",
"range_set.cc", "range_set.cc",
@@ -102,6 +104,7 @@ if (enable_pdf) {
sources = [ sources = [
"chunk_stream_unittest.cc", "chunk_stream_unittest.cc",
"document_loader_unittest.cc", "document_loader_unittest.cc",
"pdf_transform_unittest.cc",
"range_set_unittest.cc", "range_set_unittest.cc",
"run_all_unittests.cc", "run_all_unittests.cc",
] ]

@@ -2,9 +2,11 @@ include_rules = [
"+chrome/common/content_restriction.h", "+chrome/common/content_restriction.h",
"+net", "+net",
"+ppapi", "+ppapi",
"+printing/units.h",
"+ui/base/window_open_disposition.h", "+ui/base/window_open_disposition.h",
"+ui/events/keycodes/keyboard_codes.h", "+ui/events/keycodes/keyboard_codes.h",
"+ui/gfx/geometry/point_f.h", "+ui/gfx/geometry/point_f.h",
"+ui/gfx/geometry/rect.h",
"+ui/gfx/range/range.h", "+ui/gfx/range/range.h",
"+v8/include/v8.h" "+v8/include/v8.h"
] ]

@@ -2,14 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "printing/pdf_transform.h" #include "pdf/pdf_transform.h"
#include <algorithm> #include <algorithm>
#include <utility>
#include "base/logging.h" #include "base/logging.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
namespace printing { namespace chrome_pdf {
namespace { namespace {
@@ -33,10 +34,10 @@ double CalculateScaleFactor(const gfx::Rect& content_rect,
double actual_source_page_width = rotated ? src_height : src_width; double actual_source_page_width = rotated ? src_height : src_width;
double actual_source_page_height = rotated ? src_width : src_height; double actual_source_page_height = rotated ? src_width : src_height;
double ratio_x = static_cast<double>(content_rect.width()) / double ratio_x =
actual_source_page_width; static_cast<double>(content_rect.width()) / actual_source_page_width;
double ratio_y = static_cast<double>(content_rect.height()) / double ratio_y =
actual_source_page_height; static_cast<double>(content_rect.height()) / actual_source_page_height;
return std::min(ratio_x, ratio_y); return std::min(ratio_x, ratio_y);
} }
@@ -135,4 +136,4 @@ void CalculateNonScaledClipBoxOffset(const gfx::Rect& content_rect,
} }
} }
} // namespace printing } // namespace chrome_pdf

@@ -2,20 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef PRINTING_PDF_TRANSFORM_H_ #ifndef PDF_PDF_TRANSFORM_H_
#define PRINTING_PDF_TRANSFORM_H_ #define PDF_PDF_TRANSFORM_H_
#include "printing/printing_export.h"
namespace gfx { namespace gfx {
class Rect; class Rect;
} }
namespace printing { namespace chrome_pdf {
// A rect struct for use with FPDF bounding box functions. // A rect struct for use with FPDF bounding box functions.
// With PDFs, origin is bottom-left. // With PDFs, origin is bottom-left.
struct PRINTING_EXPORT PdfRectangle { struct PdfRectangle {
float left; float left;
float bottom; float bottom;
float right; float right;
@@ -30,25 +28,25 @@ struct PRINTING_EXPORT PdfRectangle {
// |src_width| specifies the source page width in points. // |src_width| specifies the source page width in points.
// |src_height| specifies the source page height in points. // |src_height| specifies the source page height in points.
// |rotated| True if source page is rotated 90 degree or 270 degree. // |rotated| True if source page is rotated 90 degree or 270 degree.
PRINTING_EXPORT double CalculateScaleFactor(const gfx::Rect& content_rect, double CalculateScaleFactor(const gfx::Rect& content_rect,
double src_width, double src_width,
double src_height, double src_height,
bool rotated); bool rotated);
// Make the default size to be letter size (8.5" X 11"). We are just following // Make the default size to be letter size (8.5" X 11"). We are just following
// the PDFium way of handling these corner cases. PDFium always consider // the PDFium way of handling these corner cases. PDFium always consider
// US-Letter as the default page size. // US-Letter as the default page size.
PRINTING_EXPORT void SetDefaultClipBox(bool rotated, PdfRectangle* clip_box); void SetDefaultClipBox(bool rotated, PdfRectangle* clip_box);
// Set the media box and/or crop box as needed. If both boxes are there, then // Set the media box and/or crop box as needed. If both boxes are there, then
// nothing needs to be done. If one box is missing, then fill it with the value // nothing needs to be done. If one box is missing, then fill it with the value
// from the other box. If both boxes are missing, then they both get the default // from the other box. If both boxes are missing, then they both get the default
// value from SetDefaultClipBox(), based on |rotated|. // value from SetDefaultClipBox(), based on |rotated|.
PRINTING_EXPORT void CalculateMediaBoxAndCropBox(bool rotated, void CalculateMediaBoxAndCropBox(bool rotated,
bool has_media_box, bool has_media_box,
bool has_crop_box, bool has_crop_box,
PdfRectangle* media_box, PdfRectangle* media_box,
PdfRectangle* crop_box); PdfRectangle* crop_box);
// Compute source clip box boundaries based on the crop box / media box of // Compute source clip box boundaries based on the crop box / media box of
// source page and scale factor. // source page and scale factor.
@@ -56,12 +54,11 @@ PRINTING_EXPORT void CalculateMediaBoxAndCropBox(bool rotated,
// //
// |media_box| The PDF's media box. // |media_box| The PDF's media box.
// |crop_box| The PDF's crop box. // |crop_box| The PDF's crop box.
PRINTING_EXPORT PdfRectangle CalculateClipBoxBoundary( PdfRectangle CalculateClipBoxBoundary(const PdfRectangle& media_box,
const PdfRectangle& media_box, const PdfRectangle& crop_box);
const PdfRectangle& crop_box);
// Scale |box| by |scale_factor|. // Scale |box| by |scale_factor|.
PRINTING_EXPORT void ScalePdfRectangle(double scale_factor, PdfRectangle* rect); void ScalePdfRectangle(double scale_factor, PdfRectangle* rect);
// Calculate the clip box translation offset for a page that does need to be // Calculate the clip box translation offset for a page that does need to be
// scaled. All parameters are in points. // scaled. All parameters are in points.
@@ -72,11 +69,10 @@ PRINTING_EXPORT void ScalePdfRectangle(double scale_factor, PdfRectangle* rect);
// origin at left-bottom. // origin at left-bottom.
// |offset_x| and |offset_y| will contain the final translation offsets for the // |offset_x| and |offset_y| will contain the final translation offsets for the
// source clip box, relative to origin at left-bottom. // source clip box, relative to origin at left-bottom.
PRINTING_EXPORT void CalculateScaledClipBoxOffset( void CalculateScaledClipBoxOffset(const gfx::Rect& content_rect,
const gfx::Rect& content_rect, const PdfRectangle& source_clip_box,
const PdfRectangle& source_clip_box, double* offset_x,
double* offset_x, double* offset_y);
double* offset_y);
// Calculate the clip box offset for a page that does not need to be scaled. // Calculate the clip box offset for a page that does not need to be scaled.
// All parameters are in points. // All parameters are in points.
@@ -91,15 +87,14 @@ PRINTING_EXPORT void CalculateScaledClipBoxOffset(
// at left-bottom. // at left-bottom.
// |offset_x| and |offset_y| will contain the final translation offsets for the // |offset_x| and |offset_y| will contain the final translation offsets for the
// source clip box, relative to origin at left-bottom. // source clip box, relative to origin at left-bottom.
PRINTING_EXPORT void CalculateNonScaledClipBoxOffset( void CalculateNonScaledClipBoxOffset(const gfx::Rect& content_rect,
const gfx::Rect& content_rect, int rotation,
int rotation, int page_width,
int page_width, int page_height,
int page_height, const PdfRectangle& source_clip_box,
const PdfRectangle& source_clip_box, double* offset_x,
double* offset_x, double* offset_y);
double* offset_y);
} // namespace printing } // namespace chrome_pdf
#endif // PRINTING_PDF_TRANSFORM_H_ #endif // PDF_PDF_TRANSFORM_H_

@@ -2,18 +2,18 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "printing/pdf_transform.h" #include "pdf/pdf_transform.h"
#include "printing/units.h" #include "printing/units.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
namespace printing { namespace chrome_pdf {
namespace { namespace {
const float kDefaultWidth = 8.5 * kPointsPerInch; const float kDefaultWidth = 8.5 * printing::kPointsPerInch;
const float kDefaultHeight = 11.0 * kPointsPerInch; const float kDefaultHeight = 11.0 * printing::kPointsPerInch;
const float kDefaultRatio = kDefaultWidth / kDefaultHeight; const float kDefaultRatio = kDefaultWidth / kDefaultHeight;
const double kTolerance = 0.0001; const double kTolerance = 0.0001;
@@ -217,40 +217,40 @@ TEST(PdfTransformTest, CalculateNonScaledClipBoxOffset) {
// |rect|, page size and |clip_box| are the same. // |rect|, page size and |clip_box| are the same.
InitializeBoxToDefaultPortraitValues(&clip_box); InitializeBoxToDefaultPortraitValues(&clip_box);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 0, page_width, page_height, clip_box,
rect, 0, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(0, offset_x); EXPECT_DOUBLE_EQ(0, offset_x);
EXPECT_DOUBLE_EQ(0, offset_y); EXPECT_DOUBLE_EQ(0, offset_y);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 1, page_width, page_height, clip_box,
rect, 1, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(0, offset_x); EXPECT_DOUBLE_EQ(0, offset_x);
EXPECT_DOUBLE_EQ(0, offset_y); EXPECT_DOUBLE_EQ(0, offset_y);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 2, page_width, page_height, clip_box,
rect, 2, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(0, offset_x); EXPECT_DOUBLE_EQ(0, offset_x);
EXPECT_DOUBLE_EQ(0, offset_y); EXPECT_DOUBLE_EQ(0, offset_y);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 3, page_width, page_height, clip_box,
rect, 3, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(180, offset_x); EXPECT_DOUBLE_EQ(180, offset_x);
EXPECT_DOUBLE_EQ(-180, offset_y); EXPECT_DOUBLE_EQ(-180, offset_y);
// Smaller |clip_box|. // Smaller |clip_box|.
clip_box.top /= 4; clip_box.top /= 4;
clip_box.right /= 2; clip_box.right /= 2;
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 0, page_width, page_height, clip_box,
rect, 0, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(0, offset_x); EXPECT_DOUBLE_EQ(0, offset_x);
EXPECT_DOUBLE_EQ(594, offset_y); EXPECT_DOUBLE_EQ(594, offset_y);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 1, page_width, page_height, clip_box,
rect, 1, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(0, offset_x); EXPECT_DOUBLE_EQ(0, offset_x);
EXPECT_DOUBLE_EQ(0, offset_y); EXPECT_DOUBLE_EQ(0, offset_y);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 2, page_width, page_height, clip_box,
rect, 2, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(306, offset_x); EXPECT_DOUBLE_EQ(306, offset_x);
EXPECT_DOUBLE_EQ(0, offset_y); EXPECT_DOUBLE_EQ(0, offset_y);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 3, page_width, page_height, clip_box,
rect, 3, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(486, offset_x); EXPECT_DOUBLE_EQ(486, offset_x);
EXPECT_DOUBLE_EQ(414, offset_y); EXPECT_DOUBLE_EQ(414, offset_y);
@@ -258,20 +258,20 @@ TEST(PdfTransformTest, CalculateNonScaledClipBoxOffset) {
InitializeBoxToDefaultPortraitValues(&clip_box); InitializeBoxToDefaultPortraitValues(&clip_box);
page_width += 10; page_width += 10;
page_height += 20; page_height += 20;
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 0, page_width, page_height, clip_box,
rect, 0, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(0, offset_x); EXPECT_DOUBLE_EQ(0, offset_x);
EXPECT_DOUBLE_EQ(20, offset_y); EXPECT_DOUBLE_EQ(20, offset_y);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 1, page_width, page_height, clip_box,
rect, 1, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(0, offset_x); EXPECT_DOUBLE_EQ(0, offset_x);
EXPECT_DOUBLE_EQ(0, offset_y); EXPECT_DOUBLE_EQ(0, offset_y);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 2, page_width, page_height, clip_box,
rect, 2, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(10, offset_x); EXPECT_DOUBLE_EQ(10, offset_x);
EXPECT_DOUBLE_EQ(0, offset_y); EXPECT_DOUBLE_EQ(0, offset_y);
CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(rect, 3, page_width, page_height, clip_box,
rect, 3, page_width, page_height, clip_box, &offset_x, &offset_y); &offset_x, &offset_y);
EXPECT_DOUBLE_EQ(200, offset_x); EXPECT_DOUBLE_EQ(200, offset_x);
EXPECT_DOUBLE_EQ(-170, offset_y); EXPECT_DOUBLE_EQ(-170, offset_y);
} }
@@ -332,4 +332,4 @@ TEST(PdfTransformTest, ReversedMediaBox) {
EXPECT_DOUBLE_EQ(0, offset_y); EXPECT_DOUBLE_EQ(0, offset_y);
} }
} // namespace printing } // namespace chrome_pdf

@@ -1,9 +1,6 @@
include_rules = [ include_rules = [
"+gin/array_buffer.h", "+gin/array_buffer.h",
"+gin/public", "+gin/public",
"+printing/pdf_transform.h",
"+printing/units.h",
"+third_party/pdfium/public", "+third_party/pdfium/public",
"+ui/gfx/codec/jpeg_codec.h", "+ui/gfx/codec/jpeg_codec.h",
"+ui/gfx/geometry/rect.h",
] ]

@@ -31,6 +31,7 @@
#include "gin/public/gin_embedders.h" #include "gin/public/gin_embedders.h"
#include "gin/public/isolate_holder.h" #include "gin/public/isolate_holder.h"
#include "pdf/draw_utils.h" #include "pdf/draw_utils.h"
#include "pdf/pdf_transform.h"
#include "pdf/pdfium/pdfium_api_string_buffer_adapter.h" #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
#include "pdf/pdfium/pdfium_mem_buffer_file_read.h" #include "pdf/pdfium/pdfium_mem_buffer_file_read.h"
#include "pdf/pdfium/pdfium_mem_buffer_file_write.h" #include "pdf/pdfium/pdfium_mem_buffer_file_write.h"
@@ -48,7 +49,6 @@
#include "ppapi/cpp/url_response_info.h" #include "ppapi/cpp/url_response_info.h"
#include "ppapi/cpp/var.h" #include "ppapi/cpp/var.h"
#include "ppapi/cpp/var_dictionary.h" #include "ppapi/cpp/var_dictionary.h"
#include "printing/pdf_transform.h"
#include "printing/units.h" #include "printing/units.h"
#include "third_party/pdfium/public/cpp/fpdf_deleters.h" #include "third_party/pdfium/public/cpp/fpdf_deleters.h"
#include "third_party/pdfium/public/fpdf_annot.h" #include "third_party/pdfium/public/fpdf_annot.h"
@@ -3792,35 +3792,33 @@ void PDFiumEngine::TransformPDFPageForPrinting(
const gfx::Rect gfx_content_rect(content_rect.x(), content_rect.y(), const gfx::Rect gfx_content_rect(content_rect.x(), content_rect.y(),
content_rect.width(), content_rect.height()); content_rect.width(), content_rect.height());
const double scale_factor = const double scale_factor =
fit_to_page fit_to_page ? CalculateScaleFactor(gfx_content_rect, src_page_width,
? printing::CalculateScaleFactor(gfx_content_rect, src_page_width, src_page_height, rotated)
src_page_height, rotated) : 1.0;
: 1.0;
// Calculate positions for the clip box. // Calculate positions for the clip box.
printing::PdfRectangle media_box; PdfRectangle media_box;
printing::PdfRectangle crop_box; PdfRectangle crop_box;
bool has_media_box = bool has_media_box =
!!FPDFPage_GetMediaBox(page, &media_box.left, &media_box.bottom, !!FPDFPage_GetMediaBox(page, &media_box.left, &media_box.bottom,
&media_box.right, &media_box.top); &media_box.right, &media_box.top);
bool has_crop_box = !!FPDFPage_GetCropBox( bool has_crop_box = !!FPDFPage_GetCropBox(
page, &crop_box.left, &crop_box.bottom, &crop_box.right, &crop_box.top); page, &crop_box.left, &crop_box.bottom, &crop_box.right, &crop_box.top);
printing::CalculateMediaBoxAndCropBox(rotated, has_media_box, has_crop_box, CalculateMediaBoxAndCropBox(rotated, has_media_box, has_crop_box, &media_box,
&media_box, &crop_box); &crop_box);
printing::PdfRectangle source_clip_box = PdfRectangle source_clip_box = CalculateClipBoxBoundary(media_box, crop_box);
printing::CalculateClipBoxBoundary(media_box, crop_box); ScalePdfRectangle(scale_factor, &source_clip_box);
printing::ScalePdfRectangle(scale_factor, &source_clip_box);
// Calculate the translation offset values. // Calculate the translation offset values.
double offset_x = 0; double offset_x = 0;
double offset_y = 0; double offset_y = 0;
if (fit_to_page) { if (fit_to_page) {
printing::CalculateScaledClipBoxOffset(gfx_content_rect, source_clip_box, CalculateScaledClipBoxOffset(gfx_content_rect, source_clip_box, &offset_x,
&offset_x, &offset_y); &offset_y);
} else { } else {
printing::CalculateNonScaledClipBoxOffset( CalculateNonScaledClipBoxOffset(gfx_content_rect, src_page_rotation,
gfx_content_rect, src_page_rotation, actual_page_width, actual_page_width, actual_page_height,
actual_page_height, source_clip_box, &offset_x, &offset_y); source_clip_box, &offset_x, &offset_y);
} }
// Reset the media box and crop box. When the page has crop box and media box, // Reset the media box and crop box. When the page has crop box and media box,

@@ -229,13 +229,6 @@ component("printing") {
"printing_context_linux.h", "printing_context_linux.h",
] ]
} }
if (!is_android) {
sources += [
"pdf_transform.cc",
"pdf_transform.h",
]
}
} }
static_library("test_support") { static_library("test_support") {
@@ -289,10 +282,6 @@ test("printing_unittests") {
"//ui/gfx/geometry", "//ui/gfx/geometry",
] ]
if (!is_android) {
sources += [ "pdf_transform_unittest.cc" ]
}
if (is_win || is_mac) { if (is_win || is_mac) {
sources += [ "printed_document_unittest.cc" ] sources += [ "printed_document_unittest.cc" ]
} }