Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / printing / printed_document_win.cc
blobddba4ca48a6d362413fe5da96533f9317ded1875
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
3 // found in the LICENSE file.
5 #include "printing/printed_document.h"
7 #include "base/logging.h"
8 #include "printing/page_number.h"
9 #include "printing/printed_pages_source.h"
10 #include "printing/printed_page.h"
11 #include "printing/units.h"
12 #include "skia/ext/platform_device.h"
14 namespace {
16 void SimpleModifyWorldTransform(HDC context,
17 int offset_x,
18 int offset_y,
19 float shrink_factor) {
20 XFORM xform = { 0 };
21 xform.eDx = static_cast<float>(offset_x);
22 xform.eDy = static_cast<float>(offset_y);
23 xform.eM11 = xform.eM22 = 1.f / shrink_factor;
24 BOOL res = ModifyWorldTransform(context, &xform, MWT_LEFTMULTIPLY);
25 DCHECK_NE(res, 0);
28 } // namespace
30 namespace printing {
32 void PrintedDocument::RenderPrintedPage(
33 const PrintedPage& page, gfx::NativeDrawingContext context) const {
34 #ifndef NDEBUG
36 // Make sure the page is from our list.
37 base::AutoLock lock(lock_);
38 DCHECK(&page == mutable_.pages_.find(page.page_number() - 1)->second.get());
40 #endif
42 DCHECK(context);
44 const PageSetup& page_setup(immutable_.settings_.page_setup_device_units());
45 gfx::Rect content_area;
46 page.GetCenteredPageContentRect(page_setup.physical_size(), &content_area);
48 // Save the state to make sure the context this function call does not modify
49 // the device context.
50 int saved_state = SaveDC(context);
51 DCHECK_NE(saved_state, 0);
52 skia::InitializeDC(context);
54 // Save the state (again) to apply the necessary world transformation.
55 int saved_state = SaveDC(context);
56 DCHECK_NE(saved_state, 0);
58 // Setup the matrix to translate and scale to the right place. Take in
59 // account the actual shrinking factor.
60 // Note that the printing output is relative to printable area of the page.
61 // That is 0,0 is offset by PHYSICALOFFSETX/Y from the page.
62 SimpleModifyWorldTransform(
63 context,
64 content_area.x() - page_setup.printable_area().x(),
65 content_area.y() - page_setup.printable_area().y(),
66 page.shrink_factor());
68 ::StartPage(context);
69 if (!page.metafile()->SafePlayback(context)) {
70 NOTREACHED();
72 ::EndPage(context);
74 BOOL res = RestoreDC(context, saved_state);
75 DCHECK_NE(res, 0);
78 int res = RestoreDC(context, saved_state);
79 DCHECK_NE(res, 0);
82 } // namespace printing