OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/printed_document.h" | 5 #include "printing/printed_document.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/i18n/file_util_icu.h" | 14 #include "base/i18n/file_util_icu.h" |
15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
20 #include "base/i18n/time_formatting.h" | 20 #include "base/i18n/time_formatting.h" |
21 #include "printing/page_number.h" | 21 #include "printing/page_number.h" |
22 #include "printing/page_overlays.h" | |
23 #include "printing/printed_pages_source.h" | 22 #include "printing/printed_pages_source.h" |
24 #include "printing/printed_page.h" | 23 #include "printing/printed_page.h" |
25 #include "printing/units.h" | 24 #include "printing/units.h" |
26 #include "skia/ext/platform_device.h" | 25 #include "skia/ext/platform_device.h" |
27 #include "ui/base/text/text_elider.h" | 26 #include "ui/base/text/text_elider.h" |
28 #include "ui/gfx/font.h" | 27 #include "ui/gfx/font.h" |
29 | 28 |
30 namespace { | 29 namespace { |
31 | 30 |
32 struct PrintDebugDumpPath { | 31 struct PrintDebugDumpPath { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 int PrintedDocument::page_count() const { | 172 int PrintedDocument::page_count() const { |
174 base::AutoLock lock(lock_); | 173 base::AutoLock lock(lock_); |
175 return mutable_.page_count_; | 174 return mutable_.page_count_; |
176 } | 175 } |
177 | 176 |
178 int PrintedDocument::expected_page_count() const { | 177 int PrintedDocument::expected_page_count() const { |
179 base::AutoLock lock(lock_); | 178 base::AutoLock lock(lock_); |
180 return mutable_.expected_page_count_; | 179 return mutable_.expected_page_count_; |
181 } | 180 } |
182 | 181 |
183 void PrintedDocument::PrintHeaderFooter(gfx::NativeDrawingContext context, | |
184 const PrintedPage& page, | |
185 PageOverlays::HorizontalPosition x, | |
186 PageOverlays::VerticalPosition y, | |
187 const gfx::Font& font) const { | |
188 const PrintSettings& settings = immutable_.settings_; | |
189 if (!settings.use_overlays || !page.has_visible_overlays()) { | |
190 return; | |
191 } | |
192 const std::wstring& line = settings.overlays.GetOverlay(x, y); | |
193 if (line.empty()) { | |
194 return; | |
195 } | |
196 std::wstring output(PageOverlays::ReplaceVariables(line, *this, page)); | |
197 if (output.empty()) { | |
198 // May happen if document name or url is empty. | |
199 return; | |
200 } | |
201 const gfx::Size string_size(font.GetStringWidth(WideToUTF16Hack(output)), | |
202 font.GetHeight()); | |
203 gfx::Rect bounding; | |
204 bounding.set_height(string_size.height()); | |
205 const gfx::Rect& overlay_area( | |
206 settings.page_setup_device_units().overlay_area()); | |
207 // Hard code .25 cm interstice between overlays. Make sure that some space is | |
208 // kept between each headers. | |
209 const int interstice = ConvertUnit(250, kHundrethsMMPerInch, | |
210 settings.device_units_per_inch()); | |
211 const int max_width = overlay_area.width() / 3 - interstice; | |
212 const int actual_width = std::min(string_size.width(), max_width); | |
213 switch (x) { | |
214 case PageOverlays::LEFT: | |
215 bounding.set_x(overlay_area.x()); | |
216 bounding.set_width(max_width); | |
217 break; | |
218 case PageOverlays::CENTER: | |
219 bounding.set_x(overlay_area.x() + | |
220 (overlay_area.width() - actual_width) / 2); | |
221 bounding.set_width(actual_width); | |
222 break; | |
223 case PageOverlays::RIGHT: | |
224 bounding.set_x(overlay_area.right() - actual_width); | |
225 bounding.set_width(actual_width); | |
226 break; | |
227 } | |
228 | |
229 DCHECK_LE(bounding.right(), overlay_area.right()); | |
230 | |
231 switch (y) { | |
232 case PageOverlays::BOTTOM: | |
233 bounding.set_y(overlay_area.bottom() - string_size.height()); | |
234 break; | |
235 case PageOverlays::TOP: | |
236 bounding.set_y(overlay_area.y()); | |
237 break; | |
238 } | |
239 | |
240 if (string_size.width() > bounding.width()) { | |
241 if (line == PageOverlays::kUrl) { | |
242 output = UTF16ToWideHack(ui::ElideUrl(url(), font, bounding.width(), | |
243 std::string())); | |
244 } else { | |
245 output = UTF16ToWideHack(ui::ElideText(WideToUTF16Hack(output), | |
246 font, bounding.width(), false)); | |
247 } | |
248 } | |
249 | |
250 DrawHeaderFooter(context, output, bounding); | |
251 } | |
252 | |
253 void PrintedDocument::DebugDump(const PrintedPage& page) { | 182 void PrintedDocument::DebugDump(const PrintedPage& page) { |
254 if (!g_debug_dump_info.Get().enabled) | 183 if (!g_debug_dump_info.Get().enabled) |
255 return; | 184 return; |
256 | 185 |
257 string16 filename; | 186 string16 filename; |
258 filename += date(); | |
259 filename += ASCIIToUTF16("_"); | |
260 filename += time(); | |
261 filename += ASCIIToUTF16("_"); | |
262 filename += name(); | 187 filename += name(); |
263 filename += ASCIIToUTF16("_"); | 188 filename += ASCIIToUTF16("_"); |
264 filename += ASCIIToUTF16(StringPrintf("%02d", page.page_number())); | 189 filename += ASCIIToUTF16(StringPrintf("%02d", page.page_number())); |
265 #if defined(OS_WIN) | 190 #if defined(OS_WIN) |
266 filename += ASCIIToUTF16("_.emf"); | 191 filename += ASCIIToUTF16("_.emf"); |
267 page.metafile()->SaveTo( | 192 page.metafile()->SaveTo( |
268 g_debug_dump_info.Get().debug_dump_path.Append(filename)); | 193 g_debug_dump_info.Get().debug_dump_path.Append(filename)); |
269 #else // OS_WIN | 194 #else // OS_WIN |
270 filename += ASCIIToUTF16("_.pdf"); | 195 filename += ASCIIToUTF16("_.pdf"); |
271 page.metafile()->SaveTo( | 196 page.metafile()->SaveTo( |
(...skipping 24 matching lines...) Expand all Loading... |
296 } | 221 } |
297 | 222 |
298 PrintedDocument::Immutable::Immutable(const PrintSettings& settings, | 223 PrintedDocument::Immutable::Immutable(const PrintSettings& settings, |
299 PrintedPagesSource* source, | 224 PrintedPagesSource* source, |
300 int cookie) | 225 int cookie) |
301 : settings_(settings), | 226 : settings_(settings), |
302 source_message_loop_(MessageLoop::current()), | 227 source_message_loop_(MessageLoop::current()), |
303 name_(source->RenderSourceName()), | 228 name_(source->RenderSourceName()), |
304 url_(source->RenderSourceUrl()), | 229 url_(source->RenderSourceUrl()), |
305 cookie_(cookie) { | 230 cookie_(cookie) { |
306 SetDocumentDate(); | |
307 } | 231 } |
308 | 232 |
309 PrintedDocument::Immutable::~Immutable() { | 233 PrintedDocument::Immutable::~Immutable() { |
310 } | 234 } |
311 | 235 |
312 } // namespace printing | 236 } // namespace printing |
OLD | NEW |