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