[email protected] | 1a30dd3 | 2012-01-28 00:56:43 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 5 | #include "ui/gfx/gdi_util.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | |
[email protected] | 82d74e9 | 2012-07-18 17:11:55 | [diff] [blame^] | 7 | #include "base/memory/scoped_ptr.h" |
| 8 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | namespace gfx { |
| 10 | |
| 11 | void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { |
| 12 | CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); |
| 13 | } |
| 14 | |
| 15 | void CreateBitmapHeaderWithColorDepth(int width, int height, int color_depth, |
| 16 | BITMAPINFOHEADER* hdr) { |
| 17 | // These values are shared with gfx::PlatformDevice |
| 18 | hdr->biSize = sizeof(BITMAPINFOHEADER); |
| 19 | hdr->biWidth = width; |
| 20 | hdr->biHeight = -height; // minus means top-down bitmap |
| 21 | hdr->biPlanes = 1; |
| 22 | hdr->biBitCount = color_depth; |
| 23 | hdr->biCompression = BI_RGB; // no compression |
| 24 | hdr->biSizeImage = 0; |
| 25 | hdr->biXPelsPerMeter = 1; |
| 26 | hdr->biYPelsPerMeter = 1; |
| 27 | hdr->biClrUsed = 0; |
| 28 | hdr->biClrImportant = 0; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | void CreateBitmapV4Header(int width, int height, BITMAPV4HEADER* hdr) { |
| 33 | // Because bmp v4 header is just an extension, we just create a v3 header and |
| 34 | // copy the bits over to the v4 header. |
| 35 | BITMAPINFOHEADER header_v3; |
| 36 | CreateBitmapHeader(width, height, &header_v3); |
| 37 | memset(hdr, 0, sizeof(BITMAPV4HEADER)); |
| 38 | memcpy(hdr, &header_v3, sizeof(BITMAPINFOHEADER)); |
| 39 | |
| 40 | // Correct the size of the header and fill in the mask values. |
| 41 | hdr->bV4Size = sizeof(BITMAPV4HEADER); |
| 42 | hdr->bV4RedMask = 0x00ff0000; |
| 43 | hdr->bV4GreenMask = 0x0000ff00; |
| 44 | hdr->bV4BlueMask = 0x000000ff; |
| 45 | hdr->bV4AlphaMask = 0xff000000; |
| 46 | } |
| 47 | |
| 48 | // Creates a monochrome bitmap header. |
| 49 | void CreateMonochromeBitmapHeader(int width, |
| 50 | int height, |
| 51 | BITMAPINFOHEADER* hdr) { |
| 52 | hdr->biSize = sizeof(BITMAPINFOHEADER); |
| 53 | hdr->biWidth = width; |
| 54 | hdr->biHeight = -height; |
| 55 | hdr->biPlanes = 1; |
| 56 | hdr->biBitCount = 1; |
| 57 | hdr->biCompression = BI_RGB; |
| 58 | hdr->biSizeImage = 0; |
| 59 | hdr->biXPelsPerMeter = 1; |
| 60 | hdr->biYPelsPerMeter = 1; |
| 61 | hdr->biClrUsed = 0; |
| 62 | hdr->biClrImportant = 0; |
| 63 | } |
| 64 | |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 65 | void SubtractRectanglesFromRegion(HRGN hrgn, |
| 66 | const std::vector<gfx::Rect>& cutouts) { |
| 67 | if (cutouts.size()) { |
| 68 | HRGN cutout = ::CreateRectRgn(0, 0, 0, 0); |
| 69 | for (size_t i = 0; i < cutouts.size(); i++) { |
| 70 | ::SetRectRgn(cutout, |
| 71 | cutouts[i].x(), |
| 72 | cutouts[i].y(), |
| 73 | cutouts[i].right(), |
| 74 | cutouts[i].bottom()); |
| 75 | ::CombineRgn(hrgn, hrgn, cutout, RGN_DIFF); |
| 76 | } |
| 77 | ::DeleteObject(cutout); |
| 78 | } |
| 79 | } |
| 80 | |
[email protected] | 82d74e9 | 2012-07-18 17:11:55 | [diff] [blame^] | 81 | HRGN ConvertPathToHRGN(const gfx::Path& path) { |
| 82 | #if defined(USE_AURA) |
| 83 | int point_count = path.getPoints(NULL, 0); |
| 84 | scoped_array<SkPoint> points(new SkPoint[point_count]); |
| 85 | path.getPoints(points.get(), point_count); |
| 86 | scoped_array<POINT> windows_points(new POINT[point_count]); |
| 87 | for (int i = 0; i < point_count; ++i) { |
| 88 | windows_points[i].x = SkScalarRound(points[i].fX); |
| 89 | windows_points[i].y = SkScalarRound(points[i].fY); |
| 90 | } |
| 91 | |
| 92 | return ::CreatePolygonRgn(windows_points.get(), point_count, ALTERNATE); |
| 93 | #elif defined(OS_WIN) |
| 94 | return path.CreateNativeRegion(); |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | |
[email protected] | 1a30dd3 | 2012-01-28 00:56:43 | [diff] [blame] | 99 | double CalculatePageScale(HDC dc, int page_width, int page_height) { |
| 100 | int dc_width = GetDeviceCaps(dc, HORZRES); |
| 101 | int dc_height = GetDeviceCaps(dc, VERTRES); |
| 102 | |
| 103 | // If page fits DC - no scaling needed. |
| 104 | if (dc_width >= page_width && dc_height >= page_height) |
| 105 | return 1.0; |
| 106 | |
| 107 | double x_factor = |
| 108 | static_cast<double>(dc_width) / static_cast<double>(page_width); |
| 109 | double y_factor = |
| 110 | static_cast<double>(dc_height) / static_cast<double>(page_height); |
| 111 | return std::min(x_factor, y_factor); |
| 112 | } |
| 113 | |
| 114 | // Apply scaling to the DC. |
| 115 | bool ScaleDC(HDC dc, double scale_factor) { |
| 116 | SetGraphicsMode(dc, GM_ADVANCED); |
| 117 | XFORM xform = {0}; |
| 118 | xform.eM11 = xform.eM22 = scale_factor; |
| 119 | return !!ModifyWorldTransform(dc, &xform, MWT_LEFTMULTIPLY); |
| 120 | } |
| 121 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 122 | } // namespace gfx |