Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Unified Diff: pdf/pdfium/pdfium_engine.cc

Issue 1421493002: Printing: Move some printing code out of pdf/ and into printing/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: simplify a function, add some unit tests, rebase Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pdf/DEPS ('k') | printing/BUILD.gn » ('j') | printing/pdf_transform.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pdf/pdfium/pdfium_engine.cc
diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc
index 67be88d96af7b07691fb585c5c0288967d4579a9..eb502a151965970482d71a27dab3a82a18c8262f 100644
--- a/pdf/pdfium/pdfium_engine.cc
+++ b/pdf/pdfium/pdfium_engine.cc
@@ -36,6 +36,7 @@
#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/fpdf_edit.h"
#include "third_party/pdfium/public/fpdf_ext.h"
@@ -46,6 +47,7 @@
#include "third_party/pdfium/public/fpdf_sysfontinfo.h"
#include "third_party/pdfium/public/fpdf_transformpage.h"
#include "ui/events/keycodes/keyboard_codes.h"
+#include "ui/gfx/geometry/rect.h"
#include "v8/include/v8.h"
using printing::ConvertUnit;
@@ -115,13 +117,6 @@ std::vector<uint32_t> GetPageNumbersFromPrintPageNumberRange(
PP_Instance g_last_instance_id;
-struct PDFFontSubstitution {
- const char* pdf_name;
- const char* face;
- bool bold;
- bool italic;
-};
-
PP_BrowserFont_Trusted_Weight WeightToBrowserFontTrustedWeight(int weight) {
static_assert(PP_BROWSERFONT_TRUSTED_WEIGHT_100 == 0,
"PP_BrowserFont_Trusted_Weight min");
@@ -147,7 +142,33 @@ void EnumFonts(struct _FPDF_SYSFONTINFO* sysfontinfo, void* mapper) {
}
}
-const PDFFontSubstitution PDFFontSubstitutions[] = {
+void* MapFont(struct _FPDF_SYSFONTINFO*, int weight, int italic,
+ int charset, int pitch_family, const char* face, int* exact) {
+ // Do not attempt to map fonts if pepper is not initialized (for privet local
+ // printing).
+ // TODO(noamsml): Real font substitution (http://crbug.com/391978)
+ if (!pp::Module::Get())
+ return NULL;
+
+ pp::BrowserFontDescription description;
+
+ // Pretend the system does not have the Symbol font to force a fallback to
+ // the built in Symbol font in CFX_FontMapper::FindSubstFont().
+ if (strcmp(face, "Symbol") == 0)
+ return NULL;
+
+ if (pitch_family & FXFONT_FF_FIXEDPITCH) {
+ description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE);
+ } else if (pitch_family & FXFONT_FF_ROMAN) {
+ description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SERIF);
+ }
+
+ static const struct {
+ const char* pdf_name;
+ const char* face;
+ bool bold;
+ bool italic;
+ } kPdfFontSubstitutions[] = {
{"Courier", "Courier New", false, false},
{"Courier-Bold", "Courier New", true, false},
{"Courier-BoldOblique", "Courier New", true, true},
@@ -183,43 +204,22 @@ const PDFFontSubstitution PDFFontSubstitutions[] = {
// MS Mincho in Shift_JIS encoding.
{"\x82\x6C\x82\x72\x96\xBE\x92\xA9",
"MS Mincho", false, false},
-};
-
-void* MapFont(struct _FPDF_SYSFONTINFO*, int weight, int italic,
- int charset, int pitch_family, const char* face, int* exact) {
- // Do not attempt to map fonts if pepper is not initialized (for privet local
- // printing).
- // TODO(noamsml): Real font substitution (http://crbug.com/391978)
- if (!pp::Module::Get())
- return NULL;
-
- pp::BrowserFontDescription description;
-
- // Pretend the system does not have the Symbol font to force a fallback to
- // the built in Symbol font in CFX_FontMapper::FindSubstFont().
- if (strcmp(face, "Symbol") == 0)
- return NULL;
-
- if (pitch_family & FXFONT_FF_FIXEDPITCH) {
- description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_MONOSPACE);
- } else if (pitch_family & FXFONT_FF_ROMAN) {
- description.set_family(PP_BROWSERFONT_TRUSTED_FAMILY_SERIF);
- }
+ };
// Map from the standard PDF fonts to TrueType font names.
size_t i;
- for (i = 0; i < arraysize(PDFFontSubstitutions); ++i) {
- if (strcmp(face, PDFFontSubstitutions[i].pdf_name) == 0) {
- description.set_face(PDFFontSubstitutions[i].face);
- if (PDFFontSubstitutions[i].bold)
+ for (i = 0; i < arraysize(kPdfFontSubstitutions); ++i) {
+ if (strcmp(face, kPdfFontSubstitutions[i].pdf_name) == 0) {
+ description.set_face(kPdfFontSubstitutions[i].face);
+ if (kPdfFontSubstitutions[i].bold)
description.set_weight(PP_BROWSERFONT_TRUSTED_WEIGHT_BOLD);
- if (PDFFontSubstitutions[i].italic)
+ if (kPdfFontSubstitutions[i].italic)
description.set_italic(true);
break;
}
}
- if (i == arraysize(PDFFontSubstitutions)) {
+ if (i == arraysize(kPdfFontSubstitutions)) {
// Convert to UTF-8 before calling set_face().
std::string face_utf8;
if (base::IsStringUTF8(face)) {
@@ -297,7 +297,7 @@ void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
g_engine_for_unsupported->UnsupportedFeature(type);
}
-UNSUPPORT_INFO g_unsuppored_info = {
+UNSUPPORT_INFO g_unsupported_info = {
1,
Unsupported_Handler
};
@@ -325,154 +325,6 @@ void SetPageSizeAndContentRect(bool rotated,
}
}
-// Calculate the scale factor between |content_rect| and a page of size
-// |src_width| x |src_height|.
-//
-// |scale_to_fit| is true, if we need to calculate the scale factor.
-// |content_rect| specifies the printable area of the destination page, with
-// origin at left-bottom. Values are in points.
-// |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.
-double CalculateScaleFactor(bool scale_to_fit,
- const pp::Rect& content_rect,
- double src_width, double src_height, bool rotated) {
- if (!scale_to_fit || src_width == 0 || src_height == 0)
- return 1.0;
-
- 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;
- return std::min(ratio_x, ratio_y);
-}
-
-// A rect struct for use with FPDF bounding box functions.
-// Remember with PDFs, origin is bottom-left.
-struct ClipBox {
- float left;
- float right;
- float top;
- float bottom;
-};
-
-// 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.
-void SetDefaultClipBox(bool rotated, ClipBox* clip_box) {
- const int kDpi = 72;
- const float kPaperWidth = 8.5 * kDpi;
- const float kPaperHeight = 11 * kDpi;
- clip_box->left = 0;
- clip_box->bottom = 0;
- clip_box->right = rotated ? kPaperHeight : kPaperWidth;
- clip_box->top = rotated ? kPaperWidth : kPaperHeight;
-}
-
-// Compute source clip box boundaries based on the crop box / media box of
-// source page and scale factor.
-//
-// |page| Handle to the source page. Returned by FPDF_LoadPage function.
-// |scale_factor| specifies the scale factor that should be applied to source
-// clip box boundaries.
-// |rotated| True if source page is rotated 90 degree or 270 degree.
-// |clip_box| out param to hold the computed source clip box values.
-void CalculateClipBoxBoundary(FPDF_PAGE page, double scale_factor, bool rotated,
- ClipBox* clip_box) {
- ClipBox media_box;
- if (!FPDFPage_GetMediaBox(page, &media_box.left, &media_box.bottom,
- &media_box.right, &media_box.top)) {
- SetDefaultClipBox(rotated, &media_box);
- }
-
- ClipBox crop_box;
- if (!FPDFPage_GetCropBox(page, &crop_box.left, &crop_box.bottom,
- &crop_box.right, &crop_box.top)) {
- SetDefaultClipBox(rotated, &crop_box);
- }
-
- // Clip |media_box| to the size of |crop_box|, but ignore |crop_box| if it is
- // bigger than |media_box|.
- clip_box->left =
- (crop_box.left < media_box.left) ? media_box.left : crop_box.left;
- clip_box->right =
- (crop_box.right > media_box.right) ? media_box.right : crop_box.right;
- clip_box->top = (crop_box.top > media_box.top) ? media_box.top : crop_box.top;
- clip_box->bottom =
- (crop_box.bottom < media_box.bottom) ? media_box.bottom : crop_box.bottom;
-
- // Finally, scale |clip_box|.
- clip_box->left *= scale_factor;
- clip_box->right *= scale_factor;
- clip_box->bottom *= scale_factor;
- clip_box->top *= scale_factor;
-}
-
-// Calculate the clip box translation offset for a page that does need to be
-// scaled. All parameters are in points.
-//
-// |content_rect| specifies the printable area of the destination page, with
-// origin at left-bottom.
-// |source_clip_box| specifies the source clip box positions, relative to
-// 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.
-void CalculateScaledClipBoxOffset(const pp::Rect& content_rect,
- const ClipBox& source_clip_box,
- double* offset_x, double* offset_y) {
- const float clip_box_width = source_clip_box.right - source_clip_box.left;
- const float clip_box_height = source_clip_box.top - source_clip_box.bottom;
-
- // Center the intended clip region to real clip region.
- *offset_x = (content_rect.width() - clip_box_width) / 2 + content_rect.x() -
- source_clip_box.left;
- *offset_y = (content_rect.height() - clip_box_height) / 2 + content_rect.y() -
- source_clip_box.bottom;
-}
-
-// Calculate the clip box offset for a page that does not need to be scaled.
-// All parameters are in points.
-//
-// |content_rect| specifies the printable area of the destination page, with
-// origin at left-bottom.
-// |rotation| specifies the source page rotation values which are N / 90
-// degrees.
-// |page_width| specifies the screen destination page width.
-// |page_height| specifies the screen destination page height.
-// |source_clip_box| specifies the source clip box positions, relative to 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.
-void CalculateNonScaledClipBoxOffset(const pp::Rect& content_rect, int rotation,
- int page_width, int page_height,
- const ClipBox& source_clip_box,
- double* offset_x, double* offset_y) {
- // Align the intended clip region to left-top corner of real clip region.
- switch (rotation) {
- case 0:
- *offset_x = -1 * source_clip_box.left;
- *offset_y = page_height - source_clip_box.top;
- break;
- case 1:
- *offset_x = 0;
- *offset_y = -1 * source_clip_box.bottom;
- break;
- case 2:
- *offset_x = page_width - source_clip_box.right;
- *offset_y = 0;
- break;
- case 3:
- *offset_x = page_height - source_clip_box.right;
- *offset_y = page_width - source_clip_box.top;
- break;
- default:
- NOTREACHED();
- break;
- }
-}
-
// This formats a string with special 0xfffe end-of-line hyphens the same way
// as Adobe Reader. When a hyphen is encountered, the next non-CR/LF whitespace
// becomes CR+LF and the hyphen is erased. If there is no whitespace between
@@ -612,7 +464,7 @@ bool InitializeSDK() {
FPDF_SetSystemFontInfo(&g_font_info);
#endif
- FSDK_SetUnSpObjProcessHandler(&g_unsuppored_info);
+ FSDK_SetUnSpObjProcessHandler(&g_unsupported_info);
return true;
}
@@ -3412,22 +3264,37 @@ void PDFiumEngine::TransformPDFPageForPrinting(
const int actual_page_height =
rotated ? page_size.width() : page_size.height();
- const double scale_factor = CalculateScaleFactor(fit_to_page, content_rect,
- src_page_width,
- src_page_height, rotated);
+ 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;
// Calculate positions for the clip box.
- ClipBox source_clip_box;
- CalculateClipBoxBoundary(page, scale_factor, rotated, &source_clip_box);
+ printing::ClipBox media_box;
+ if (!FPDFPage_GetMediaBox(page, &media_box.left, &media_box.bottom,
+ &media_box.right, &media_box.top)) {
+ printing::SetDefaultClipBox(rotated, &media_box);
+ }
+
+ printing::ClipBox crop_box;
+ if (!FPDFPage_GetCropBox(page, &crop_box.left, &crop_box.bottom,
+ &crop_box.right, &crop_box.top)) {
+ printing::SetDefaultClipBox(rotated, &crop_box);
+ }
+ printing::ClipBox source_clip_box = printing::CalculateClipBoxBoundary(
+ media_box, crop_box, scale_factor, rotated);
// Calculate the translation offset values.
double offset_x = 0;
double offset_y = 0;
if (fit_to_page) {
- CalculateScaledClipBoxOffset(content_rect, source_clip_box, &offset_x,
+ CalculateScaledClipBoxOffset(gfx_content_rect, source_clip_box, &offset_x,
&offset_y);
} else {
- CalculateNonScaledClipBoxOffset(content_rect, src_page_rotation,
+ CalculateNonScaledClipBoxOffset(gfx_content_rect, src_page_rotation,
actual_page_width, actual_page_height,
source_clip_box, &offset_x, &offset_y);
}
« no previous file with comments | « pdf/DEPS ('k') | printing/BUILD.gn » ('j') | printing/pdf_transform.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698