OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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/printed_document.h" |
| 6 |
| 7 #include "app/win_util.h" |
| 8 #include "app/gfx/font.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" |
| 11 #include "printing/page_number.h" |
| 12 #include "printing/page_overlays.h" |
| 13 #include "printing/printed_pages_source.h" |
| 14 #include "printing/printed_page.h" |
| 15 #include "printing/units.h" |
| 16 #include "skia/ext/platform_device.h" |
| 17 |
| 18 #if defined(OS_WIN) |
| 19 namespace { |
| 20 |
| 21 void SimpleModifyWorldTransform(HDC context, |
| 22 int offset_x, |
| 23 int offset_y, |
| 24 double shrink_factor) { |
| 25 XFORM xform = { 0 }; |
| 26 xform.eDx = static_cast<float>(offset_x); |
| 27 xform.eDy = static_cast<float>(offset_y); |
| 28 xform.eM11 = xform.eM22 = static_cast<float>(1. / shrink_factor); |
| 29 BOOL res = ModifyWorldTransform(context, &xform, MWT_LEFTMULTIPLY); |
| 30 DCHECK_NE(res, 0); |
| 31 } |
| 32 |
| 33 void DrawRect(HDC context, gfx::Rect rect) { |
| 34 Rectangle(context, rect.x(), rect.y(), rect.right(), rect.bottom()); |
| 35 } |
| 36 |
| 37 } // namespace |
| 38 #endif // OS_WIN |
| 39 |
| 40 namespace printing { |
| 41 |
| 42 void PrintedDocument::RenderPrintedPage( |
| 43 const PrintedPage& page, gfx::NativeDrawingContext context) const { |
| 44 #ifndef NDEBUG |
| 45 { |
| 46 // Make sure the page is from our list. |
| 47 AutoLock lock(lock_); |
| 48 DCHECK(&page == mutable_.pages_.find(page.page_number() - 1)->second.get()); |
| 49 } |
| 50 #endif |
| 51 |
| 52 const printing::PageSetup& page_setup( |
| 53 immutable_.settings_.page_setup_pixels()); |
| 54 |
| 55 // Save the state to make sure the context this function call does not modify |
| 56 // the device context. |
| 57 int saved_state = SaveDC(context); |
| 58 DCHECK_NE(saved_state, 0); |
| 59 skia::PlatformDevice::InitializeDC(context); |
| 60 { |
| 61 // Save the state (again) to apply the necessary world transformation. |
| 62 int saved_state = SaveDC(context); |
| 63 DCHECK_NE(saved_state, 0); |
| 64 |
| 65 #if 0 |
| 66 // Debug code to visually verify margins (leaks GDI handles). |
| 67 XFORM debug_xform = { 0 }; |
| 68 ModifyWorldTransform(context, &debug_xform, MWT_IDENTITY); |
| 69 // Printable area: |
| 70 SelectObject(context, CreatePen(PS_SOLID, 1, RGB(0, 0, 0))); |
| 71 SelectObject(context, CreateSolidBrush(RGB(0x90, 0x90, 0x90))); |
| 72 Rectangle(context, |
| 73 0, |
| 74 0, |
| 75 page_setup.printable_area().width(), |
| 76 page_setup.printable_area().height()); |
| 77 // Overlay area: |
| 78 gfx::Rect debug_overlay_area(page_setup.overlay_area()); |
| 79 debug_overlay_area.Offset(-page_setup.printable_area().x(), |
| 80 -page_setup.printable_area().y()); |
| 81 SelectObject(context, CreateSolidBrush(RGB(0xb0, 0xb0, 0xb0))); |
| 82 DrawRect(context, debug_overlay_area); |
| 83 // Content area: |
| 84 gfx::Rect debug_content_area(page_setup.content_area()); |
| 85 debug_content_area.Offset(-page_setup.printable_area().x(), |
| 86 -page_setup.printable_area().y()); |
| 87 SelectObject(context, CreateSolidBrush(RGB(0xd0, 0xd0, 0xd0))); |
| 88 DrawRect(context, debug_content_area); |
| 89 #endif |
| 90 |
| 91 // Setup the matrix to translate and scale to the right place. Take in |
| 92 // account the actual shrinking factor. |
| 93 // Note that the printing output is relative to printable area of the page. |
| 94 // That is 0,0 is offset by PHYSICALOFFSETX/Y from the page. |
| 95 SimpleModifyWorldTransform( |
| 96 context, |
| 97 page_setup.content_area().x() - page_setup.printable_area().x(), |
| 98 page_setup.content_area().y() - page_setup.printable_area().y(), |
| 99 mutable_.shrink_factor); |
| 100 |
| 101 if (!page.native_metafile()->SafePlayback(context)) { |
| 102 NOTREACHED(); |
| 103 } |
| 104 |
| 105 BOOL res = RestoreDC(context, saved_state); |
| 106 DCHECK_NE(res, 0); |
| 107 } |
| 108 |
| 109 // Print the header and footer. Offset by printable area offset (see comment |
| 110 // above). |
| 111 SimpleModifyWorldTransform( |
| 112 context, |
| 113 -page_setup.printable_area().x(), |
| 114 -page_setup.printable_area().y(), |
| 115 1); |
| 116 int base_font_size = gfx::Font().height(); |
| 117 int new_font_size = ConvertUnit(10, |
| 118 immutable_.settings_.desired_dpi, |
| 119 immutable_.settings_.dpi()); |
| 120 DCHECK_GT(new_font_size, base_font_size); |
| 121 gfx::Font font(gfx::Font().DeriveFont(new_font_size - base_font_size)); |
| 122 HGDIOBJ old_font = SelectObject(context, font.hfont()); |
| 123 DCHECK(old_font != NULL); |
| 124 // We don't want a white square around the text ever if overflowing. |
| 125 SetBkMode(context, TRANSPARENT); |
| 126 PrintHeaderFooter(context, page, PageOverlays::LEFT, PageOverlays::TOP, |
| 127 font); |
| 128 PrintHeaderFooter(context, page, PageOverlays::CENTER, PageOverlays::TOP, |
| 129 font); |
| 130 PrintHeaderFooter(context, page, PageOverlays::RIGHT, PageOverlays::TOP, |
| 131 font); |
| 132 PrintHeaderFooter(context, page, PageOverlays::LEFT, PageOverlays::BOTTOM, |
| 133 font); |
| 134 PrintHeaderFooter(context, page, PageOverlays::CENTER, PageOverlays::BOTTOM, |
| 135 font); |
| 136 PrintHeaderFooter(context, page, PageOverlays::RIGHT, PageOverlays::BOTTOM, |
| 137 font); |
| 138 int res = RestoreDC(context, saved_state); |
| 139 DCHECK_NE(res, 0); |
| 140 } |
| 141 |
| 142 } // namespace printing |
OLD | NEW |