| 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 "chrome/renderer/mock_printer_driver_win.h" | |
| 6 | |
| 7 #include "base/gfx/gdi_util.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "printing/emf_win.h" | |
| 10 #include "chrome/renderer/mock_printer.h" | |
| 11 #include "skia/ext/platform_device.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // A simple class which temporarily overrides system settings. | |
| 16 // The bitmap image rendered via the PlayEnhMetaFile() function depends on | |
| 17 // some system settings. | |
| 18 // As a workaround for such dependency, this class saves the system settings | |
| 19 // and changes them. This class also restore the saved settings in its | |
| 20 // destructor. | |
| 21 class SystemSettingsOverride { | |
| 22 public: | |
| 23 SystemSettingsOverride() : font_smoothing_(0) { | |
| 24 } | |
| 25 | |
| 26 ~SystemSettingsOverride() { | |
| 27 SystemParametersInfo(SPI_SETFONTSMOOTHING, font_smoothing_, NULL, 0); | |
| 28 } | |
| 29 | |
| 30 BOOL Init(BOOL font_smoothing) { | |
| 31 if (!SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &font_smoothing_, 0)) | |
| 32 return FALSE; | |
| 33 return SystemParametersInfo(SPI_SETFONTSMOOTHING, font_smoothing, NULL, 0); | |
| 34 } | |
| 35 | |
| 36 private: | |
| 37 BOOL font_smoothing_; | |
| 38 }; | |
| 39 | |
| 40 // A class which renders an EMF data and returns a raw bitmap data. | |
| 41 // The bitmap data returned from Create() is deleted in the destructor of this | |
| 42 // class. So, we need to create a copy of this bitmap data if it is used after | |
| 43 // this object is deleted, | |
| 44 class EmfRenderer { | |
| 45 public: | |
| 46 EmfRenderer() : dc_(NULL), bitmap_(NULL) { | |
| 47 } | |
| 48 | |
| 49 ~EmfRenderer() { | |
| 50 if (bitmap_) { | |
| 51 DeleteObject(bitmap_); | |
| 52 bitmap_ = NULL; | |
| 53 } | |
| 54 if (dc_) { | |
| 55 DeleteDC(dc_); | |
| 56 dc_ = NULL; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 const void* Create(int width, int height, const printing::Emf* emf) { | |
| 61 CHECK(!dc_ && !bitmap_); | |
| 62 | |
| 63 BITMAPV4HEADER header; | |
| 64 gfx::CreateBitmapV4Header(width, height, &header); | |
| 65 | |
| 66 dc_ = CreateCompatibleDC(NULL); | |
| 67 if (!dc_) | |
| 68 return NULL; | |
| 69 | |
| 70 void* bits; | |
| 71 bitmap_ = CreateDIBSection(dc_, reinterpret_cast<BITMAPINFO*>(&header), 0, | |
| 72 &bits, NULL, 0); | |
| 73 if (!bitmap_ || !bits) | |
| 74 return NULL; | |
| 75 | |
| 76 SelectObject(dc_, bitmap_); | |
| 77 | |
| 78 skia::PlatformDevice::InitializeDC(dc_); | |
| 79 emf->Playback(dc_, NULL); | |
| 80 | |
| 81 return reinterpret_cast<uint8*>(bits); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 HDC dc_; | |
| 86 HBITMAP bitmap_; | |
| 87 }; | |
| 88 | |
| 89 } // namespace | |
| 90 | |
| 91 MockPrinterDriverWin::MockPrinterDriverWin() { | |
| 92 } | |
| 93 | |
| 94 MockPrinterDriverWin::~MockPrinterDriverWin() { | |
| 95 } | |
| 96 | |
| 97 MockPrinterPage* MockPrinterDriverWin::LoadSource(const void* source_data, | |
| 98 size_t source_size) { | |
| 99 // This code is mostly copied from the Image::LoadEMF() function in | |
| 100 // "src/chrome/browser/printing/printing_layout_uitest.cc". | |
| 101 printing::Emf emf; | |
| 102 emf.CreateFromData(source_data, source_size); | |
| 103 gfx::Rect rect(emf.GetBounds()); | |
| 104 | |
| 105 // Create a temporary DC and bitmap to retrieve the renderered data. | |
| 106 if (rect.width() <= 0 || rect.height() <= 0) | |
| 107 return NULL; | |
| 108 | |
| 109 // Disable the font-smoothing feature of Windows. | |
| 110 SystemSettingsOverride system_settings; | |
| 111 system_settings.Init(FALSE); | |
| 112 | |
| 113 // Render the EMF data and create a bitmap. | |
| 114 EmfRenderer renderer; | |
| 115 const void* bitmap_data = renderer.Create(rect.width(), rect.height(), &emf); | |
| 116 if (!bitmap_data) | |
| 117 return NULL; | |
| 118 | |
| 119 // Create a new MockPrinterPage instance and return it. | |
| 120 size_t row_byte_width = rect.width() * 4; | |
| 121 size_t bitmap_size = row_byte_width * rect.height(); | |
| 122 return new MockPrinterPage(rect.width(), rect.height(), | |
| 123 source_data, source_size, | |
| 124 bitmap_data, bitmap_size); | |
| 125 } | |
| OLD | NEW |