OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "printing/pdf_transform.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "ui/gfx/geometry/rect.h" |
| 11 |
| 12 namespace printing { |
| 13 |
| 14 double CalculateScaleFactor(const gfx::Rect& content_rect, |
| 15 double src_width, |
| 16 double src_height, |
| 17 bool rotated) { |
| 18 if (src_width == 0 || src_height == 0) |
| 19 return 1.0; |
| 20 |
| 21 double actual_source_page_width = rotated ? src_height : src_width; |
| 22 double actual_source_page_height = rotated ? src_width : src_height; |
| 23 double ratio_x = static_cast<double>(content_rect.width()) / |
| 24 actual_source_page_width; |
| 25 double ratio_y = static_cast<double>(content_rect.height()) / |
| 26 actual_source_page_height; |
| 27 return std::min(ratio_x, ratio_y); |
| 28 } |
| 29 |
| 30 void SetDefaultClipBox(bool rotated, ClipBox* clip_box) { |
| 31 const int kDpi = 72; |
| 32 const float kPaperWidth = 8.5 * kDpi; |
| 33 const float kPaperHeight = 11 * kDpi; |
| 34 clip_box->left = 0; |
| 35 clip_box->bottom = 0; |
| 36 clip_box->right = rotated ? kPaperHeight : kPaperWidth; |
| 37 clip_box->top = rotated ? kPaperWidth : kPaperHeight; |
| 38 } |
| 39 |
| 40 void CalculateMediaBoxAndCropBox(bool rotated, |
| 41 bool has_media_box, |
| 42 bool has_crop_box, |
| 43 printing::ClipBox* media_box, |
| 44 printing::ClipBox* crop_box) { |
| 45 if (!has_media_box && !has_crop_box) { |
| 46 SetDefaultClipBox(rotated, crop_box); |
| 47 SetDefaultClipBox(rotated, media_box); |
| 48 } else if (has_crop_box && !has_media_box) { |
| 49 *media_box = *crop_box; |
| 50 } else if (has_media_box && !has_crop_box) { |
| 51 *crop_box = *media_box; |
| 52 } |
| 53 } |
| 54 |
| 55 ClipBox CalculateClipBoxBoundary(const ClipBox& media_box, |
| 56 const ClipBox& crop_box) { |
| 57 ClipBox clip_box; |
| 58 |
| 59 // Clip |media_box| to the size of |crop_box|, but ignore |crop_box| if it is |
| 60 // bigger than |media_box|. |
| 61 clip_box.left = |
| 62 (crop_box.left < media_box.left) ? media_box.left : crop_box.left; |
| 63 clip_box.right = |
| 64 (crop_box.right > media_box.right) ? media_box.right : crop_box.right; |
| 65 clip_box.top = (crop_box.top > media_box.top) ? media_box.top : crop_box.top; |
| 66 clip_box.bottom = |
| 67 (crop_box.bottom < media_box.bottom) ? media_box.bottom : crop_box.bottom; |
| 68 return clip_box; |
| 69 } |
| 70 |
| 71 void ScaleClipBox(double scale_factor, ClipBox* box) { |
| 72 box->left *= scale_factor; |
| 73 box->right *= scale_factor; |
| 74 box->bottom *= scale_factor; |
| 75 box->top *= scale_factor; |
| 76 } |
| 77 |
| 78 void CalculateScaledClipBoxOffset(const gfx::Rect& content_rect, |
| 79 const ClipBox& source_clip_box, |
| 80 double* offset_x, |
| 81 double* offset_y) { |
| 82 const float clip_box_width = source_clip_box.right - source_clip_box.left; |
| 83 const float clip_box_height = source_clip_box.top - source_clip_box.bottom; |
| 84 |
| 85 // Center the intended clip region to real clip region. |
| 86 *offset_x = (content_rect.width() - clip_box_width) / 2 + content_rect.x() - |
| 87 source_clip_box.left; |
| 88 *offset_y = (content_rect.height() - clip_box_height) / 2 + content_rect.y() - |
| 89 source_clip_box.bottom; |
| 90 } |
| 91 |
| 92 void CalculateNonScaledClipBoxOffset(const gfx::Rect& content_rect, |
| 93 int rotation, |
| 94 int page_width, |
| 95 int page_height, |
| 96 const ClipBox& source_clip_box, |
| 97 double* offset_x, |
| 98 double* offset_y) { |
| 99 // Align the intended clip region to left-top corner of real clip region. |
| 100 switch (rotation) { |
| 101 case 0: |
| 102 *offset_x = -1 * source_clip_box.left; |
| 103 *offset_y = page_height - source_clip_box.top; |
| 104 break; |
| 105 case 1: |
| 106 *offset_x = 0; |
| 107 *offset_y = -1 * source_clip_box.bottom; |
| 108 break; |
| 109 case 2: |
| 110 *offset_x = page_width - source_clip_box.right; |
| 111 *offset_y = 0; |
| 112 break; |
| 113 case 3: |
| 114 *offset_x = page_height - source_clip_box.right; |
| 115 *offset_y = page_width - source_clip_box.top; |
| 116 break; |
| 117 default: |
| 118 NOTREACHED(); |
| 119 break; |
| 120 } |
| 121 } |
| 122 |
| 123 } // namespace printing |
OLD | NEW |