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:
@ -52,6 +52,8 @@ if (enable_pdf) {
|
||||
"pdf_engine.h",
|
||||
"pdf_ppapi.cc",
|
||||
"pdf_ppapi.h",
|
||||
"pdf_transform.cc",
|
||||
"pdf_transform.h",
|
||||
"preview_mode_client.cc",
|
||||
"preview_mode_client.h",
|
||||
"range_set.cc",
|
||||
@ -102,6 +104,7 @@ if (enable_pdf) {
|
||||
sources = [
|
||||
"chunk_stream_unittest.cc",
|
||||
"document_loader_unittest.cc",
|
||||
"pdf_transform_unittest.cc",
|
||||
"range_set_unittest.cc",
|
||||
"run_all_unittests.cc",
|
||||
]
|
||||
|
2
pdf/DEPS
2
pdf/DEPS
@ -2,9 +2,11 @@ include_rules = [
|
||||
"+chrome/common/content_restriction.h",
|
||||
"+net",
|
||||
"+ppapi",
|
||||
"+printing/units.h",
|
||||
"+ui/base/window_open_disposition.h",
|
||||
"+ui/events/keycodes/keyboard_codes.h",
|
||||
"+ui/gfx/geometry/point_f.h",
|
||||
"+ui/gfx/geometry/rect.h",
|
||||
"+ui/gfx/range/range.h",
|
||||
"+v8/include/v8.h"
|
||||
]
|
||||
|
@ -2,14 +2,15 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "printing/pdf_transform.h"
|
||||
#include "pdf/pdf_transform.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
|
||||
namespace printing {
|
||||
namespace chrome_pdf {
|
||||
|
||||
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_height = rotated ? src_width : src_height;
|
||||
double ratio_x = static_cast<double>(content_rect.width()) /
|
||||
actual_source_page_width;
|
||||
double ratio_y = static_cast<double>(content_rect.height()) /
|
||||
actual_source_page_height;
|
||||
double ratio_x =
|
||||
static_cast<double>(content_rect.width()) / actual_source_page_width;
|
||||
double ratio_y =
|
||||
static_cast<double>(content_rect.height()) / actual_source_page_height;
|
||||
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
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef PRINTING_PDF_TRANSFORM_H_
|
||||
#define PRINTING_PDF_TRANSFORM_H_
|
||||
|
||||
#include "printing/printing_export.h"
|
||||
#ifndef PDF_PDF_TRANSFORM_H_
|
||||
#define PDF_PDF_TRANSFORM_H_
|
||||
|
||||
namespace gfx {
|
||||
class Rect;
|
||||
}
|
||||
|
||||
namespace printing {
|
||||
namespace chrome_pdf {
|
||||
|
||||
// A rect struct for use with FPDF bounding box functions.
|
||||
// With PDFs, origin is bottom-left.
|
||||
struct PRINTING_EXPORT PdfRectangle {
|
||||
struct PdfRectangle {
|
||||
float left;
|
||||
float bottom;
|
||||
float right;
|
||||
@ -30,25 +28,25 @@ struct PRINTING_EXPORT PdfRectangle {
|
||||
// |src_width| specifies the source page width in points.
|
||||
// |src_height| specifies the source page height in points.
|
||||
// |rotated| True if source page is rotated 90 degree or 270 degree.
|
||||
PRINTING_EXPORT double CalculateScaleFactor(const gfx::Rect& content_rect,
|
||||
double src_width,
|
||||
double src_height,
|
||||
bool rotated);
|
||||
double CalculateScaleFactor(const gfx::Rect& content_rect,
|
||||
double src_width,
|
||||
double src_height,
|
||||
bool rotated);
|
||||
|
||||
// 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
|
||||
// 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
|
||||
// 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
|
||||
// value from SetDefaultClipBox(), based on |rotated|.
|
||||
PRINTING_EXPORT void CalculateMediaBoxAndCropBox(bool rotated,
|
||||
bool has_media_box,
|
||||
bool has_crop_box,
|
||||
PdfRectangle* media_box,
|
||||
PdfRectangle* crop_box);
|
||||
void CalculateMediaBoxAndCropBox(bool rotated,
|
||||
bool has_media_box,
|
||||
bool has_crop_box,
|
||||
PdfRectangle* media_box,
|
||||
PdfRectangle* crop_box);
|
||||
|
||||
// Compute source clip box boundaries based on the crop box / media box of
|
||||
// source page and scale factor.
|
||||
@ -56,12 +54,11 @@ PRINTING_EXPORT void CalculateMediaBoxAndCropBox(bool rotated,
|
||||
//
|
||||
// |media_box| The PDF's media box.
|
||||
// |crop_box| The PDF's crop box.
|
||||
PRINTING_EXPORT PdfRectangle CalculateClipBoxBoundary(
|
||||
const PdfRectangle& media_box,
|
||||
const PdfRectangle& crop_box);
|
||||
PdfRectangle CalculateClipBoxBoundary(const PdfRectangle& media_box,
|
||||
const PdfRectangle& crop_box);
|
||||
|
||||
// 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
|
||||
// scaled. All parameters are in points.
|
||||
@ -72,11 +69,10 @@ PRINTING_EXPORT void ScalePdfRectangle(double scale_factor, PdfRectangle* rect);
|
||||
// origin at left-bottom.
|
||||
// |offset_x| and |offset_y| will contain the final translation offsets for the
|
||||
// source clip box, relative to origin at left-bottom.
|
||||
PRINTING_EXPORT void CalculateScaledClipBoxOffset(
|
||||
const gfx::Rect& content_rect,
|
||||
const PdfRectangle& source_clip_box,
|
||||
double* offset_x,
|
||||
double* offset_y);
|
||||
void CalculateScaledClipBoxOffset(const gfx::Rect& content_rect,
|
||||
const PdfRectangle& source_clip_box,
|
||||
double* offset_x,
|
||||
double* offset_y);
|
||||
|
||||
// Calculate the clip box offset for a page that does not need to be scaled.
|
||||
// All parameters are in points.
|
||||
@ -91,15 +87,14 @@ PRINTING_EXPORT void CalculateScaledClipBoxOffset(
|
||||
// at left-bottom.
|
||||
// |offset_x| and |offset_y| will contain the final translation offsets for the
|
||||
// source clip box, relative to origin at left-bottom.
|
||||
PRINTING_EXPORT void CalculateNonScaledClipBoxOffset(
|
||||
const gfx::Rect& content_rect,
|
||||
int rotation,
|
||||
int page_width,
|
||||
int page_height,
|
||||
const PdfRectangle& source_clip_box,
|
||||
double* offset_x,
|
||||
double* offset_y);
|
||||
void CalculateNonScaledClipBoxOffset(const gfx::Rect& content_rect,
|
||||
int rotation,
|
||||
int page_width,
|
||||
int page_height,
|
||||
const PdfRectangle& source_clip_box,
|
||||
double* offset_x,
|
||||
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
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "printing/pdf_transform.h"
|
||||
#include "pdf/pdf_transform.h"
|
||||
|
||||
#include "printing/units.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
#include "ui/gfx/geometry/rect.h"
|
||||
|
||||
namespace printing {
|
||||
namespace chrome_pdf {
|
||||
|
||||
namespace {
|
||||
|
||||
const float kDefaultWidth = 8.5 * kPointsPerInch;
|
||||
const float kDefaultHeight = 11.0 * kPointsPerInch;
|
||||
const float kDefaultWidth = 8.5 * printing::kPointsPerInch;
|
||||
const float kDefaultHeight = 11.0 * printing::kPointsPerInch;
|
||||
const float kDefaultRatio = kDefaultWidth / kDefaultHeight;
|
||||
const double kTolerance = 0.0001;
|
||||
|
||||
@ -217,40 +217,40 @@ TEST(PdfTransformTest, CalculateNonScaledClipBoxOffset) {
|
||||
|
||||
// |rect|, page size and |clip_box| are the same.
|
||||
InitializeBoxToDefaultPortraitValues(&clip_box);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 0, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 0, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(0, offset_x);
|
||||
EXPECT_DOUBLE_EQ(0, offset_y);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 1, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 1, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(0, offset_x);
|
||||
EXPECT_DOUBLE_EQ(0, offset_y);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 2, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 2, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(0, offset_x);
|
||||
EXPECT_DOUBLE_EQ(0, offset_y);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 3, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 3, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(180, offset_x);
|
||||
EXPECT_DOUBLE_EQ(-180, offset_y);
|
||||
|
||||
// Smaller |clip_box|.
|
||||
clip_box.top /= 4;
|
||||
clip_box.right /= 2;
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 0, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 0, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(0, offset_x);
|
||||
EXPECT_DOUBLE_EQ(594, offset_y);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 1, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 1, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(0, offset_x);
|
||||
EXPECT_DOUBLE_EQ(0, offset_y);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 2, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 2, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(306, offset_x);
|
||||
EXPECT_DOUBLE_EQ(0, offset_y);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 3, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 3, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(486, offset_x);
|
||||
EXPECT_DOUBLE_EQ(414, offset_y);
|
||||
|
||||
@ -258,20 +258,20 @@ TEST(PdfTransformTest, CalculateNonScaledClipBoxOffset) {
|
||||
InitializeBoxToDefaultPortraitValues(&clip_box);
|
||||
page_width += 10;
|
||||
page_height += 20;
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 0, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 0, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(0, offset_x);
|
||||
EXPECT_DOUBLE_EQ(20, offset_y);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 1, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 1, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(0, offset_x);
|
||||
EXPECT_DOUBLE_EQ(0, offset_y);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 2, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 2, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(10, offset_x);
|
||||
EXPECT_DOUBLE_EQ(0, offset_y);
|
||||
CalculateNonScaledClipBoxOffset(
|
||||
rect, 3, page_width, page_height, clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(rect, 3, page_width, page_height, clip_box,
|
||||
&offset_x, &offset_y);
|
||||
EXPECT_DOUBLE_EQ(200, offset_x);
|
||||
EXPECT_DOUBLE_EQ(-170, offset_y);
|
||||
}
|
||||
@ -332,4 +332,4 @@ TEST(PdfTransformTest, ReversedMediaBox) {
|
||||
EXPECT_DOUBLE_EQ(0, offset_y);
|
||||
}
|
||||
|
||||
} // namespace printing
|
||||
} // namespace chrome_pdf
|
@ -1,9 +1,6 @@
|
||||
include_rules = [
|
||||
"+gin/array_buffer.h",
|
||||
"+gin/public",
|
||||
"+printing/pdf_transform.h",
|
||||
"+printing/units.h",
|
||||
"+third_party/pdfium/public",
|
||||
"+ui/gfx/codec/jpeg_codec.h",
|
||||
"+ui/gfx/geometry/rect.h",
|
||||
]
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "gin/public/gin_embedders.h"
|
||||
#include "gin/public/isolate_holder.h"
|
||||
#include "pdf/draw_utils.h"
|
||||
#include "pdf/pdf_transform.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_write.h"
|
||||
@ -48,7 +49,6 @@
|
||||
#include "ppapi/cpp/url_response_info.h"
|
||||
#include "ppapi/cpp/var.h"
|
||||
#include "ppapi/cpp/var_dictionary.h"
|
||||
#include "printing/pdf_transform.h"
|
||||
#include "printing/units.h"
|
||||
#include "third_party/pdfium/public/cpp/fpdf_deleters.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(),
|
||||
content_rect.width(), content_rect.height());
|
||||
const double scale_factor =
|
||||
fit_to_page
|
||||
? printing::CalculateScaleFactor(gfx_content_rect, src_page_width,
|
||||
src_page_height, rotated)
|
||||
: 1.0;
|
||||
fit_to_page ? CalculateScaleFactor(gfx_content_rect, src_page_width,
|
||||
src_page_height, rotated)
|
||||
: 1.0;
|
||||
|
||||
// Calculate positions for the clip box.
|
||||
printing::PdfRectangle media_box;
|
||||
printing::PdfRectangle crop_box;
|
||||
PdfRectangle media_box;
|
||||
PdfRectangle crop_box;
|
||||
bool has_media_box =
|
||||
!!FPDFPage_GetMediaBox(page, &media_box.left, &media_box.bottom,
|
||||
&media_box.right, &media_box.top);
|
||||
bool has_crop_box = !!FPDFPage_GetCropBox(
|
||||
page, &crop_box.left, &crop_box.bottom, &crop_box.right, &crop_box.top);
|
||||
printing::CalculateMediaBoxAndCropBox(rotated, has_media_box, has_crop_box,
|
||||
&media_box, &crop_box);
|
||||
printing::PdfRectangle source_clip_box =
|
||||
printing::CalculateClipBoxBoundary(media_box, crop_box);
|
||||
printing::ScalePdfRectangle(scale_factor, &source_clip_box);
|
||||
CalculateMediaBoxAndCropBox(rotated, has_media_box, has_crop_box, &media_box,
|
||||
&crop_box);
|
||||
PdfRectangle source_clip_box = CalculateClipBoxBoundary(media_box, crop_box);
|
||||
ScalePdfRectangle(scale_factor, &source_clip_box);
|
||||
|
||||
// Calculate the translation offset values.
|
||||
double offset_x = 0;
|
||||
double offset_y = 0;
|
||||
if (fit_to_page) {
|
||||
printing::CalculateScaledClipBoxOffset(gfx_content_rect, source_clip_box,
|
||||
&offset_x, &offset_y);
|
||||
CalculateScaledClipBoxOffset(gfx_content_rect, source_clip_box, &offset_x,
|
||||
&offset_y);
|
||||
} else {
|
||||
printing::CalculateNonScaledClipBoxOffset(
|
||||
gfx_content_rect, src_page_rotation, actual_page_width,
|
||||
actual_page_height, source_clip_box, &offset_x, &offset_y);
|
||||
CalculateNonScaledClipBoxOffset(gfx_content_rect, src_page_rotation,
|
||||
actual_page_width, actual_page_height,
|
||||
source_clip_box, &offset_x, &offset_y);
|
||||
}
|
||||
|
||||
// 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",
|
||||
]
|
||||
}
|
||||
|
||||
if (!is_android) {
|
||||
sources += [
|
||||
"pdf_transform.cc",
|
||||
"pdf_transform.h",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
static_library("test_support") {
|
||||
@ -289,10 +282,6 @@ test("printing_unittests") {
|
||||
"//ui/gfx/geometry",
|
||||
]
|
||||
|
||||
if (!is_android) {
|
||||
sources += [ "pdf_transform_unittest.cc" ]
|
||||
}
|
||||
|
||||
if (is_win || is_mac) {
|
||||
sources += [ "printed_document_unittest.cc" ]
|
||||
}
|
||||
|
Reference in New Issue
Block a user