| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "app/gfx/font.h" | 9 #include "app/gfx/font.h" |
| 10 #include "app/gfx/text_elider.h" | 10 #include "app/gfx/text_elider.h" |
| 11 #include "app/win_util.h" | |
| 12 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 13 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 14 #include "base/singleton.h" | 13 #include "base/singleton.h" |
| 15 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 16 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "base/time_format.h" |
| 17 #include "printing/page_number.h" | 17 #include "printing/page_number.h" |
| 18 #include "printing/page_overlays.h" | 18 #include "printing/page_overlays.h" |
| 19 #include "printing/printed_pages_source.h" | 19 #include "printing/printed_pages_source.h" |
| 20 #include "printing/printed_page.h" | 20 #include "printing/printed_page.h" |
| 21 #include "printing/units.h" | 21 #include "printing/units.h" |
| 22 #include "skia/ext/platform_device.h" | 22 #include "skia/ext/platform_device.h" |
| 23 | 23 |
| 24 #if defined(OS_WIN) |
| 25 #include "app/win_util.h" |
| 26 #endif |
| 27 |
| 24 using base::Time; | 28 using base::Time; |
| 25 | 29 |
| 26 namespace { | 30 namespace { |
| 27 | 31 |
| 28 struct PrintDebugDumpPath { | 32 struct PrintDebugDumpPath { |
| 29 PrintDebugDumpPath() | 33 PrintDebugDumpPath() |
| 30 : enabled(false) { | 34 : enabled(false) { |
| 31 } | 35 } |
| 32 | 36 |
| 33 bool enabled; | 37 bool enabled; |
| 34 std::wstring debug_dump_path; | 38 std::wstring debug_dump_path; |
| 35 }; | 39 }; |
| 36 | 40 |
| 37 Singleton<PrintDebugDumpPath> g_debug_dump_info; | 41 Singleton<PrintDebugDumpPath> g_debug_dump_info; |
| 38 | 42 |
| 43 #if defined(OS_WIN) |
| 39 void SimpleModifyWorldTransform(HDC context, | 44 void SimpleModifyWorldTransform(HDC context, |
| 40 int offset_x, | 45 int offset_x, |
| 41 int offset_y, | 46 int offset_y, |
| 42 double shrink_factor) { | 47 double shrink_factor) { |
| 43 XFORM xform = { 0 }; | 48 XFORM xform = { 0 }; |
| 44 xform.eDx = static_cast<float>(offset_x); | 49 xform.eDx = static_cast<float>(offset_x); |
| 45 xform.eDy = static_cast<float>(offset_y); | 50 xform.eDy = static_cast<float>(offset_y); |
| 46 xform.eM11 = xform.eM22 = static_cast<float>(1. / shrink_factor); | 51 xform.eM11 = xform.eM22 = static_cast<float>(1. / shrink_factor); |
| 47 BOOL res = ModifyWorldTransform(context, &xform, MWT_LEFTMULTIPLY); | 52 BOOL res = ModifyWorldTransform(context, &xform, MWT_LEFTMULTIPLY); |
| 48 DCHECK_NE(res, 0); | 53 DCHECK_NE(res, 0); |
| 49 } | 54 } |
| 50 | 55 |
| 51 void DrawRect(HDC context, gfx::Rect rect) { | 56 void DrawRect(HDC context, gfx::Rect rect) { |
| 52 Rectangle(context, rect.x(), rect.y(), rect.right(), rect.bottom()); | 57 Rectangle(context, rect.x(), rect.y(), rect.right(), rect.bottom()); |
| 53 } | 58 } |
| 59 #endif // OS_WIN |
| 54 | 60 |
| 55 } // namespace | 61 } // namespace |
| 56 | 62 |
| 57 namespace printing { | 63 namespace printing { |
| 58 | 64 |
| 59 PrintedDocument::PrintedDocument(const PrintSettings& settings, | 65 PrintedDocument::PrintedDocument(const PrintSettings& settings, |
| 60 PrintedPagesSource* source, | 66 PrintedPagesSource* source, |
| 61 int cookie) | 67 int cookie) |
| 62 : mutable_(source), | 68 : mutable_(source), |
| 63 immutable_(settings, source, cookie) { | 69 immutable_(settings, source, cookie) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 void PrintedDocument::RenderPrintedPage(const PrintedPage& page, | 117 void PrintedDocument::RenderPrintedPage(const PrintedPage& page, |
| 112 HDC context) const { | 118 HDC context) const { |
| 113 #ifndef NDEBUG | 119 #ifndef NDEBUG |
| 114 { | 120 { |
| 115 // Make sure the page is from our list. | 121 // Make sure the page is from our list. |
| 116 AutoLock lock(lock_); | 122 AutoLock lock(lock_); |
| 117 DCHECK(&page == mutable_.pages_.find(page.page_number() - 1)->second.get()); | 123 DCHECK(&page == mutable_.pages_.find(page.page_number() - 1)->second.get()); |
| 118 } | 124 } |
| 119 #endif | 125 #endif |
| 120 | 126 |
| 127 #if defined(OS_WIN) |
| 121 const printing::PageSetup& page_setup( | 128 const printing::PageSetup& page_setup( |
| 122 immutable_.settings_.page_setup_pixels()); | 129 immutable_.settings_.page_setup_pixels()); |
| 123 | 130 |
| 124 // Save the state to make sure the context this function call does not modify | 131 // Save the state to make sure the context this function call does not modify |
| 125 // the device context. | 132 // the device context. |
| 126 int saved_state = SaveDC(context); | 133 int saved_state = SaveDC(context); |
| 127 DCHECK_NE(saved_state, 0); | 134 DCHECK_NE(saved_state, 0); |
| 128 skia::PlatformDevice::InitializeDC(context); | 135 skia::PlatformDevice::InitializeDC(context); |
| 129 { | 136 { |
| 130 // Save the state (again) to apply the necessary world transformation. | 137 // Save the state (again) to apply the necessary world transformation. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 PrintHeaderFooter(context, page, PageOverlays::RIGHT, PageOverlays::TOP, | 206 PrintHeaderFooter(context, page, PageOverlays::RIGHT, PageOverlays::TOP, |
| 200 font); | 207 font); |
| 201 PrintHeaderFooter(context, page, PageOverlays::LEFT, PageOverlays::BOTTOM, | 208 PrintHeaderFooter(context, page, PageOverlays::LEFT, PageOverlays::BOTTOM, |
| 202 font); | 209 font); |
| 203 PrintHeaderFooter(context, page, PageOverlays::CENTER, PageOverlays::BOTTOM, | 210 PrintHeaderFooter(context, page, PageOverlays::CENTER, PageOverlays::BOTTOM, |
| 204 font); | 211 font); |
| 205 PrintHeaderFooter(context, page, PageOverlays::RIGHT, PageOverlays::BOTTOM, | 212 PrintHeaderFooter(context, page, PageOverlays::RIGHT, PageOverlays::BOTTOM, |
| 206 font); | 213 font); |
| 207 int res = RestoreDC(context, saved_state); | 214 int res = RestoreDC(context, saved_state); |
| 208 DCHECK_NE(res, 0); | 215 DCHECK_NE(res, 0); |
| 216 #else // OS_WIN |
| 217 NOTIMPLEMENTED(); |
| 218 #endif // OS_WIN |
| 209 } | 219 } |
| 210 | 220 |
| 211 bool PrintedDocument::RenderPrintedPageNumber(int page_number, HDC context) { | 221 bool PrintedDocument::RenderPrintedPageNumber(int page_number, HDC context) { |
| 212 scoped_refptr<PrintedPage> page; | 222 scoped_refptr<PrintedPage> page; |
| 213 if (!GetPage(page_number, &page)) | 223 if (!GetPage(page_number, &page)) |
| 214 return false; | 224 return false; |
| 215 RenderPrintedPage(*page.get(), context); | 225 RenderPrintedPage(*page.get(), context); |
| 216 return true; | 226 return true; |
| 217 } | 227 } |
| 218 | 228 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 231 } | 241 } |
| 232 return true; | 242 return true; |
| 233 } | 243 } |
| 234 | 244 |
| 235 void PrintedDocument::DisconnectSource() { | 245 void PrintedDocument::DisconnectSource() { |
| 236 AutoLock lock(lock_); | 246 AutoLock lock(lock_); |
| 237 mutable_.source_ = NULL; | 247 mutable_.source_ = NULL; |
| 238 } | 248 } |
| 239 | 249 |
| 240 size_t PrintedDocument::MemoryUsage() const { | 250 size_t PrintedDocument::MemoryUsage() const { |
| 241 std::vector<scoped_refptr<PrintedPage>> pages_copy; | 251 std::vector< scoped_refptr<PrintedPage> > pages_copy; |
| 242 { | 252 { |
| 243 AutoLock lock(lock_); | 253 AutoLock lock(lock_); |
| 244 pages_copy.reserve(mutable_.pages_.size()); | 254 pages_copy.reserve(mutable_.pages_.size()); |
| 245 PrintedPages::const_iterator end = mutable_.pages_.end(); | 255 PrintedPages::const_iterator end = mutable_.pages_.end(); |
| 246 for (PrintedPages::const_iterator itr = mutable_.pages_.begin(); | 256 for (PrintedPages::const_iterator itr = mutable_.pages_.begin(); |
| 247 itr != end; ++itr) { | 257 itr != end; ++itr) { |
| 248 if (itr->second.get()) { | 258 if (itr->second.get()) { |
| 249 pages_copy.push_back(itr->second); | 259 pages_copy.push_back(itr->second); |
| 250 } | 260 } |
| 251 } | 261 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 } | 342 } |
| 333 | 343 |
| 334 if (string_size.width() > bounding.width()) { | 344 if (string_size.width() > bounding.width()) { |
| 335 if (line == PageOverlays::kUrl) { | 345 if (line == PageOverlays::kUrl) { |
| 336 output = gfx::ElideUrl(url(), font, bounding.width(), std::wstring()); | 346 output = gfx::ElideUrl(url(), font, bounding.width(), std::wstring()); |
| 337 } else { | 347 } else { |
| 338 output = gfx::ElideText(output, font, bounding.width()); | 348 output = gfx::ElideText(output, font, bounding.width()); |
| 339 } | 349 } |
| 340 } | 350 } |
| 341 | 351 |
| 352 #if defined(OS_WIN) |
| 342 // Save the state (again) for the clipping region. | 353 // Save the state (again) for the clipping region. |
| 343 int saved_state = SaveDC(context); | 354 int saved_state = SaveDC(context); |
| 344 DCHECK_NE(saved_state, 0); | 355 DCHECK_NE(saved_state, 0); |
| 345 | 356 |
| 346 int result = IntersectClipRect(context, bounding.x(), bounding.y(), | 357 int result = IntersectClipRect(context, bounding.x(), bounding.y(), |
| 347 bounding.right() + 1, bounding.bottom() + 1); | 358 bounding.right() + 1, bounding.bottom() + 1); |
| 348 DCHECK(result == SIMPLEREGION || result == COMPLEXREGION); | 359 DCHECK(result == SIMPLEREGION || result == COMPLEXREGION); |
| 349 TextOut(context, | 360 TextOut(context, |
| 350 bounding.x(), bounding.y(), | 361 bounding.x(), bounding.y(), |
| 351 output.c_str(), | 362 output.c_str(), |
| 352 static_cast<int>(output.size())); | 363 static_cast<int>(output.size())); |
| 353 int res = RestoreDC(context, saved_state); | 364 int res = RestoreDC(context, saved_state); |
| 354 DCHECK_NE(res, 0); | 365 DCHECK_NE(res, 0); |
| 366 #else // OS_WIN |
| 367 NOTIMPLEMENTED(); |
| 368 #endif // OS_WIN |
| 355 } | 369 } |
| 356 | 370 |
| 357 void PrintedDocument::DebugDump(const PrintedPage& page) { | 371 void PrintedDocument::DebugDump(const PrintedPage& page) { |
| 358 if (!g_debug_dump_info->enabled) | 372 if (!g_debug_dump_info->enabled) |
| 359 return; | 373 return; |
| 360 | 374 |
| 361 std::wstring filename; | 375 std::wstring filename; |
| 362 filename += date(); | 376 filename += date(); |
| 363 filename += L"_"; | 377 filename += L"_"; |
| 364 filename += time(); | 378 filename += time(); |
| 365 filename += L"_"; | 379 filename += L"_"; |
| 366 filename += name(); | 380 filename += name(); |
| 367 filename += L"_"; | 381 filename += L"_"; |
| 368 filename += StringPrintf(L"%02d", page.page_number()); | 382 filename += StringPrintf(L"%02d", page.page_number()); |
| 369 filename += L"_.emf"; | 383 filename += L"_.emf"; |
| 370 file_util::ReplaceIllegalCharacters(&filename, '_'); | 384 file_util::ReplaceIllegalCharacters(&filename, '_'); |
| 371 std::wstring path(g_debug_dump_info->debug_dump_path); | 385 std::wstring path(g_debug_dump_info->debug_dump_path); |
| 372 file_util::AppendToPath(&path, filename); | 386 file_util::AppendToPath(&path, filename); |
| 387 #if defined(OS_WIN) |
| 373 page.native_metafile()->SaveTo(path); | 388 page.native_metafile()->SaveTo(path); |
| 389 #else // OS_WIN |
| 390 NOTIMPLEMENTED(); |
| 391 #endif // OS_WIN |
| 374 } | 392 } |
| 375 | 393 |
| 376 void PrintedDocument::set_debug_dump_path(const std::wstring& debug_dump_path) { | 394 void PrintedDocument::set_debug_dump_path(const std::wstring& debug_dump_path) { |
| 377 g_debug_dump_info->enabled = !debug_dump_path.empty(); | 395 g_debug_dump_info->enabled = !debug_dump_path.empty(); |
| 378 g_debug_dump_info->debug_dump_path = debug_dump_path; | 396 g_debug_dump_info->debug_dump_path = debug_dump_path; |
| 379 } | 397 } |
| 380 | 398 |
| 381 const std::wstring& PrintedDocument::debug_dump_path() { | 399 const std::wstring& PrintedDocument::debug_dump_path() { |
| 382 return g_debug_dump_info->debug_dump_path; | 400 return g_debug_dump_info->debug_dump_path; |
| 383 } | 401 } |
| 384 | 402 |
| 385 PrintedDocument::Mutable::Mutable(PrintedPagesSource* source) | 403 PrintedDocument::Mutable::Mutable(PrintedPagesSource* source) |
| 386 : source_(source), | 404 : source_(source), |
| 387 expected_page_count_(0), | 405 expected_page_count_(0), |
| 388 page_count_(0), | 406 page_count_(0), |
| 389 shrink_factor(0) { | 407 shrink_factor(0) { |
| 390 } | 408 } |
| 391 | 409 |
| 392 PrintedDocument::Immutable::Immutable(const PrintSettings& settings, | 410 PrintedDocument::Immutable::Immutable(const PrintSettings& settings, |
| 393 PrintedPagesSource* source, | 411 PrintedPagesSource* source, |
| 394 int cookie) | 412 int cookie) |
| 395 : settings_(settings), | 413 : settings_(settings), |
| 396 source_message_loop_(MessageLoop::current()), | 414 source_message_loop_(MessageLoop::current()), |
| 397 name_(source->RenderSourceName()), | 415 name_(source->RenderSourceName()), |
| 398 url_(source->RenderSourceUrl()), | 416 url_(source->RenderSourceUrl()), |
| 399 cookie_(cookie) { | 417 cookie_(cookie) { |
| 400 // Setup the document's date. | 418 // Setup the document's date. |
| 401 #ifdef WIN32 | 419 #if defined(OS_WIN) |
| 402 // On Windows, use the native time formatting for printing. | 420 // On Windows, use the native time formatting for printing. |
| 403 SYSTEMTIME systemtime; | 421 SYSTEMTIME systemtime; |
| 404 GetLocalTime(&systemtime); | 422 GetLocalTime(&systemtime); |
| 405 date_ = win_util::FormatSystemDate(systemtime, std::wstring()); | 423 date_ = win_util::FormatSystemDate(systemtime, std::wstring()); |
| 406 time_ = win_util::FormatSystemTime(systemtime, std::wstring()); | 424 time_ = win_util::FormatSystemTime(systemtime, std::wstring()); |
| 407 #else | 425 #else // OS_WIN |
| 408 Time now = Time::Now(); | 426 Time now = Time::Now(); |
| 409 date_ = TimeFormat::ShortDateNumeric(now); | 427 date_ = base::TimeFormatShortDateNumeric(now); |
| 410 time_ = TimeFormat::TimeOfDay(now); | 428 time_ = base::TimeFormatTimeOfDay(now); |
| 411 #endif // WIN32 | 429 #endif // OS_WIN |
| 412 } | 430 } |
| 413 | 431 |
| 414 } // namespace printing | 432 } // namespace printing |
| OLD | NEW |