OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/emf_win.h" | 5 #include "printing/emf_win.h" |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 &header_.bmiHeader); | 106 &header_.bmiHeader); |
107 bitmap_.Set(::CreateDIBSection(context_.Get(), &header_, DIB_RGB_COLORS, | 107 bitmap_.Set(::CreateDIBSection(context_.Get(), &header_, DIB_RGB_COLORS, |
108 &bits, NULL, 0)); | 108 &bits, NULL, 0)); |
109 if (!bitmap_) | 109 if (!bitmap_) |
110 NOTREACHED() << "Raster bitmap creation for printing failed"; | 110 NOTREACHED() << "Raster bitmap creation for printing failed"; |
111 | 111 |
112 saved_object_ = ::SelectObject(context_.Get(), bitmap_); | 112 saved_object_ = ::SelectObject(context_.Get(), bitmap_); |
113 RECT rect = bitmap_rect.ToRECT(); | 113 RECT rect = bitmap_rect.ToRECT(); |
114 ::FillRect(context_.Get(), &rect, | 114 ::FillRect(context_.Get(), &rect, |
115 static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH))); | 115 static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH))); |
116 | |
117 } | 116 } |
118 | 117 |
119 ~RasterBitmap() { | 118 ~RasterBitmap() { |
120 ::SelectObject(context_.Get(), saved_object_); | 119 ::SelectObject(context_.Get(), saved_object_); |
121 } | 120 } |
122 | 121 |
123 HDC context() const { | 122 HDC context() const { |
124 return context_.Get(); | 123 return context_.Get(); |
125 } | 124 } |
126 | 125 |
127 base::win::ScopedCreateDC context_; | 126 base::win::ScopedCreateDC context_; |
128 BITMAPINFO header_; | 127 BITMAPINFO header_; |
129 base::win::ScopedBitmap bitmap_; | 128 base::win::ScopedBitmap bitmap_; |
130 HGDIOBJ saved_object_; | 129 HGDIOBJ saved_object_; |
131 | 130 |
132 private: | 131 private: |
133 DISALLOW_COPY_AND_ASSIGN(RasterBitmap); | 132 DISALLOW_COPY_AND_ASSIGN(RasterBitmap); |
134 }; | 133 }; |
135 | 134 |
136 | 135 |
137 | 136 |
138 } // namespace | 137 } // namespace |
139 | 138 |
140 namespace printing { | 139 namespace printing { |
141 | 140 |
142 bool DIBFormatNativelySupported(HDC dc, uint32 escape, const BYTE* bits, | 141 bool DIBFormatNativelySupported(HDC dc, uint32_t escape, const BYTE* bits, |
143 int size) { | 142 int size) { |
144 BOOL supported = FALSE; | 143 BOOL supported = FALSE; |
145 if (ExtEscape(dc, QUERYESCSUPPORT, sizeof(escape), | 144 if (ExtEscape(dc, QUERYESCSUPPORT, sizeof(escape), |
146 reinterpret_cast<LPCSTR>(&escape), 0, 0) > 0) { | 145 reinterpret_cast<LPCSTR>(&escape), 0, 0) > 0) { |
147 ExtEscape(dc, escape, size, reinterpret_cast<LPCSTR>(bits), | 146 ExtEscape(dc, escape, size, reinterpret_cast<LPCSTR>(bits), |
148 sizeof(supported), reinterpret_cast<LPSTR>(&supported)); | 147 sizeof(supported), reinterpret_cast<LPSTR>(&supported)); |
149 } | 148 } |
150 return !!supported; | 149 return !!supported; |
151 } | 150 } |
152 | 151 |
(...skipping 25 matching lines...) Expand all Loading... |
178 return emf_ != NULL; | 177 return emf_ != NULL; |
179 } | 178 } |
180 | 179 |
181 bool Emf::Init() { | 180 bool Emf::Init() { |
182 DCHECK(!emf_ && !hdc_); | 181 DCHECK(!emf_ && !hdc_); |
183 hdc_ = CreateEnhMetaFile(NULL, NULL, NULL, NULL); | 182 hdc_ = CreateEnhMetaFile(NULL, NULL, NULL, NULL); |
184 DCHECK(hdc_); | 183 DCHECK(hdc_); |
185 return hdc_ != NULL; | 184 return hdc_ != NULL; |
186 } | 185 } |
187 | 186 |
188 bool Emf::InitFromData(const void* src_buffer, uint32 src_buffer_size) { | 187 bool Emf::InitFromData(const void* src_buffer, uint32_t src_buffer_size) { |
189 DCHECK(!emf_ && !hdc_); | 188 DCHECK(!emf_ && !hdc_); |
190 emf_ = SetEnhMetaFileBits(src_buffer_size, | 189 emf_ = SetEnhMetaFileBits(src_buffer_size, |
191 reinterpret_cast<const BYTE*>(src_buffer)); | 190 reinterpret_cast<const BYTE*>(src_buffer)); |
192 return emf_ != NULL; | 191 return emf_ != NULL; |
193 } | 192 } |
194 | 193 |
195 bool Emf::FinishDocument() { | 194 bool Emf::FinishDocument() { |
196 DCHECK(!emf_ && hdc_); | 195 DCHECK(!emf_ && hdc_); |
197 emf_ = CloseEnhMetaFile(hdc_); | 196 emf_ = CloseEnhMetaFile(hdc_); |
198 DCHECK(emf_); | 197 DCHECK(emf_); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } | 246 } |
248 | 247 |
249 unsigned int Emf::GetPageCount() const { | 248 unsigned int Emf::GetPageCount() const { |
250 return 1; | 249 return 1; |
251 } | 250 } |
252 | 251 |
253 HDC Emf::context() const { | 252 HDC Emf::context() const { |
254 return hdc_; | 253 return hdc_; |
255 } | 254 } |
256 | 255 |
257 uint32 Emf::GetDataSize() const { | 256 uint32_t Emf::GetDataSize() const { |
258 DCHECK(emf_ && !hdc_); | 257 DCHECK(emf_ && !hdc_); |
259 return GetEnhMetaFileBits(emf_, 0, NULL); | 258 return GetEnhMetaFileBits(emf_, 0, NULL); |
260 } | 259 } |
261 | 260 |
262 bool Emf::GetData(void* buffer, uint32 size) const { | 261 bool Emf::GetData(void* buffer, uint32_t size) const { |
263 DCHECK(emf_ && !hdc_); | 262 DCHECK(emf_ && !hdc_); |
264 DCHECK(buffer && size); | 263 DCHECK(buffer && size); |
265 uint32 size2 = | 264 uint32_t size2 = |
266 GetEnhMetaFileBits(emf_, size, reinterpret_cast<BYTE*>(buffer)); | 265 GetEnhMetaFileBits(emf_, size, reinterpret_cast<BYTE*>(buffer)); |
267 DCHECK(size2 == size); | 266 DCHECK(size2 == size); |
268 return size2 == size && size2 != 0; | 267 return size2 == size && size2 != 0; |
269 } | 268 } |
270 | 269 |
271 int CALLBACK Emf::SafePlaybackProc(HDC hdc, | 270 int CALLBACK Emf::SafePlaybackProc(HDC hdc, |
272 HANDLETABLE* handle_table, | 271 HANDLETABLE* handle_table, |
273 const ENHMETARECORD* record, | 272 const ENHMETARECORD* record, |
274 int objects_count, | 273 int objects_count, |
275 LPARAM param) { | 274 LPARAM param) { |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 } | 522 } |
524 | 523 |
525 scoped_ptr<Emf> Emf::RasterizeMetafile(int raster_area_in_pixels) const { | 524 scoped_ptr<Emf> Emf::RasterizeMetafile(int raster_area_in_pixels) const { |
526 gfx::Rect page_bounds = GetPageBounds(1); | 525 gfx::Rect page_bounds = GetPageBounds(1); |
527 gfx::Size page_size(page_bounds.size()); | 526 gfx::Size page_size(page_bounds.size()); |
528 if (page_size.GetArea() <= 0) { | 527 if (page_size.GetArea() <= 0) { |
529 NOTREACHED() << "Metafile is empty"; | 528 NOTREACHED() << "Metafile is empty"; |
530 page_bounds = gfx::Rect(1, 1); | 529 page_bounds = gfx::Rect(1, 1); |
531 } | 530 } |
532 | 531 |
533 float scale = sqrt(float(raster_area_in_pixels) / page_size.GetArea()); | 532 float scale = sqrt( |
| 533 static_cast<float>(raster_area_in_pixels) / page_size.GetArea()); |
534 page_size.set_width(std::max<int>(1, page_size.width() * scale)); | 534 page_size.set_width(std::max<int>(1, page_size.width() * scale)); |
535 page_size.set_height(std::max<int>(1, page_size.height() * scale)); | 535 page_size.set_height(std::max<int>(1, page_size.height() * scale)); |
536 | 536 |
537 | 537 |
538 RasterBitmap bitmap(page_size); | 538 RasterBitmap bitmap(page_size); |
539 | 539 |
540 gfx::Rect bitmap_rect(page_size); | 540 gfx::Rect bitmap_rect(page_size); |
541 RECT rect = bitmap_rect.ToRECT(); | 541 RECT rect = bitmap_rect.ToRECT(); |
542 Playback(bitmap.context(), &rect); | 542 Playback(bitmap.context(), &rect); |
543 | 543 |
544 scoped_ptr<Emf> result(new Emf); | 544 scoped_ptr<Emf> result(new Emf); |
545 result->Init(); | 545 result->Init(); |
546 HDC hdc = result->context(); | 546 HDC hdc = result->context(); |
547 DCHECK(hdc); | 547 DCHECK(hdc); |
548 skia::InitializeDC(hdc); | 548 skia::InitializeDC(hdc); |
549 | 549 |
550 // Params are ignored. | 550 // Params are ignored. |
551 result->StartPage(page_bounds.size(), page_bounds, 1); | 551 result->StartPage(page_bounds.size(), page_bounds, 1); |
552 | 552 |
553 ::ModifyWorldTransform(hdc, NULL, MWT_IDENTITY); | 553 ::ModifyWorldTransform(hdc, NULL, MWT_IDENTITY); |
554 XFORM xform = { | 554 XFORM xform = { |
555 float(page_bounds.width()) / bitmap_rect.width(), 0, | 555 static_cast<float>(page_bounds.width()) / bitmap_rect.width(), |
556 0, float(page_bounds.height()) / bitmap_rect.height(), | 556 0, |
| 557 0, |
| 558 static_cast<float>(page_bounds.height()) / bitmap_rect.height(), |
557 static_cast<float>(page_bounds.x()), | 559 static_cast<float>(page_bounds.x()), |
558 static_cast<float>(page_bounds.y()), | 560 static_cast<float>(page_bounds.y()), |
559 }; | 561 }; |
560 ::SetWorldTransform(hdc, &xform); | 562 ::SetWorldTransform(hdc, &xform); |
561 ::BitBlt(hdc, 0, 0, bitmap_rect.width(), bitmap_rect.height(), | 563 ::BitBlt(hdc, 0, 0, bitmap_rect.width(), bitmap_rect.height(), |
562 bitmap.context(), bitmap_rect.x(), bitmap_rect.y(), SRCCOPY); | 564 bitmap.context(), bitmap_rect.x(), bitmap_rect.y(), SRCCOPY); |
563 | 565 |
564 result->FinishPage(); | 566 result->FinishPage(); |
565 result->FinishDocument(); | 567 result->FinishDocument(); |
566 | 568 |
(...skipping 28 matching lines...) Expand all Loading... |
595 RECT rect = page_bounds.ToRECT(); | 597 RECT rect = page_bounds.ToRECT(); |
596 ::EnumEnhMetaFile(hdc, emf(), &RasterizeAlphaBlendProc, &bitmap_dc, &rect); | 598 ::EnumEnhMetaFile(hdc, emf(), &RasterizeAlphaBlendProc, &bitmap_dc, &rect); |
597 | 599 |
598 result->FinishDocument(); | 600 result->FinishDocument(); |
599 | 601 |
600 return result.Pass(); | 602 return result.Pass(); |
601 } | 603 } |
602 | 604 |
603 | 605 |
604 } // namespace printing | 606 } // namespace printing |
OLD | NEW |