[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 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 9 | #include <algorithm> |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 10 | #include <memory> |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 11 | |
[email protected] | b4f0016 | 2013-03-09 02:51:14 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 82d74e9 | 2012-07-18 17:11:55 | [diff] [blame] | 13 | |
pkasting | 7bc277b | 2014-10-13 20:58:39 | [diff] [blame] | 14 | namespace { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | |
pkasting | 7bc277b | 2014-10-13 20:58:39 | [diff] [blame] | 16 | void CreateBitmapHeaderWithColorDepth(LONG width, |
| 17 | LONG height, |
| 18 | WORD color_depth, |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 19 | BITMAPINFOHEADER* hdr) { |
| 20 | // These values are shared with gfx::PlatformDevice |
| 21 | hdr->biSize = sizeof(BITMAPINFOHEADER); |
| 22 | hdr->biWidth = width; |
| 23 | hdr->biHeight = -height; // minus means top-down bitmap |
| 24 | hdr->biPlanes = 1; |
| 25 | hdr->biBitCount = color_depth; |
| 26 | hdr->biCompression = BI_RGB; // no compression |
| 27 | hdr->biSizeImage = 0; |
| 28 | hdr->biXPelsPerMeter = 1; |
| 29 | hdr->biYPelsPerMeter = 1; |
| 30 | hdr->biClrUsed = 0; |
| 31 | hdr->biClrImportant = 0; |
| 32 | } |
| 33 | |
pkasting | 7bc277b | 2014-10-13 20:58:39 | [diff] [blame] | 34 | } // namespace |
| 35 | |
| 36 | namespace gfx { |
| 37 | |
| 38 | void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { |
| 39 | CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); |
| 40 | } |
| 41 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 42 | void CreateBitmapV4Header(int width, int height, BITMAPV4HEADER* hdr) { |
| 43 | // Because bmp v4 header is just an extension, we just create a v3 header and |
| 44 | // copy the bits over to the v4 header. |
| 45 | BITMAPINFOHEADER header_v3; |
| 46 | CreateBitmapHeader(width, height, &header_v3); |
| 47 | memset(hdr, 0, sizeof(BITMAPV4HEADER)); |
| 48 | memcpy(hdr, &header_v3, sizeof(BITMAPINFOHEADER)); |
| 49 | |
| 50 | // Correct the size of the header and fill in the mask values. |
| 51 | hdr->bV4Size = sizeof(BITMAPV4HEADER); |
| 52 | hdr->bV4RedMask = 0x00ff0000; |
| 53 | hdr->bV4GreenMask = 0x0000ff00; |
| 54 | hdr->bV4BlueMask = 0x000000ff; |
| 55 | hdr->bV4AlphaMask = 0xff000000; |
| 56 | } |
| 57 | |
| 58 | // Creates a monochrome bitmap header. |
| 59 | void CreateMonochromeBitmapHeader(int width, |
| 60 | int height, |
| 61 | BITMAPINFOHEADER* hdr) { |
pkasting | 7bc277b | 2014-10-13 20:58:39 | [diff] [blame] | 62 | CreateBitmapHeaderWithColorDepth(width, height, 1, hdr); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 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) { |
[email protected] | 82d74e9 | 2012-07-18 17:11:55 | [diff] [blame] | 82 | int point_count = path.getPoints(NULL, 0); |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 83 | std::unique_ptr<SkPoint[]> points(new SkPoint[point_count]); |
[email protected] | 82d74e9 | 2012-07-18 17:11:55 | [diff] [blame] | 84 | path.getPoints(points.get(), point_count); |
danakj | 25c52c3 | 2016-04-12 21:51:08 | [diff] [blame] | 85 | std::unique_ptr<POINT[]> windows_points(new POINT[point_count]); |
[email protected] | 82d74e9 | 2012-07-18 17:11:55 | [diff] [blame] | 86 | for (int i = 0; i < point_count; ++i) { |
[email protected] | 7839ade9 | 2014-01-06 21:49:38 | [diff] [blame] | 87 | windows_points[i].x = SkScalarRoundToInt(points[i].fX); |
| 88 | windows_points[i].y = SkScalarRoundToInt(points[i].fY); |
[email protected] | 82d74e9 | 2012-07-18 17:11:55 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | return ::CreatePolygonRgn(windows_points.get(), point_count, ALTERNATE); |
[email protected] | 82d74e9 | 2012-07-18 17:11:55 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
pkasting | 46a6291 | 2014-10-09 21:30:00 | [diff] [blame] | 95 | float CalculatePageScale(HDC dc, int page_width, int page_height) { |
[email protected] | 1a30dd3 | 2012-01-28 00:56:43 | [diff] [blame] | 96 | int dc_width = GetDeviceCaps(dc, HORZRES); |
| 97 | int dc_height = GetDeviceCaps(dc, VERTRES); |
| 98 | |
| 99 | // If page fits DC - no scaling needed. |
| 100 | if (dc_width >= page_width && dc_height >= page_height) |
| 101 | return 1.0; |
| 102 | |
pkasting | 46a6291 | 2014-10-09 21:30:00 | [diff] [blame] | 103 | float x_factor = |
| 104 | static_cast<float>(dc_width) / static_cast<float>(page_width); |
| 105 | float y_factor = |
| 106 | static_cast<float>(dc_height) / static_cast<float>(page_height); |
[email protected] | 1a30dd3 | 2012-01-28 00:56:43 | [diff] [blame] | 107 | return std::min(x_factor, y_factor); |
| 108 | } |
| 109 | |
| 110 | // Apply scaling to the DC. |
pkasting | 46a6291 | 2014-10-09 21:30:00 | [diff] [blame] | 111 | bool ScaleDC(HDC dc, float scale_factor) { |
[email protected] | 1a30dd3 | 2012-01-28 00:56:43 | [diff] [blame] | 112 | SetGraphicsMode(dc, GM_ADVANCED); |
| 113 | XFORM xform = {0}; |
| 114 | xform.eM11 = xform.eM22 = scale_factor; |
| 115 | return !!ModifyWorldTransform(dc, &xform, MWT_LEFTMULTIPLY); |
| 116 | } |
| 117 | |
[email protected] | b4f0016 | 2013-03-09 02:51:14 | [diff] [blame] | 118 | void StretchDIBits(HDC hdc, int dest_x, int dest_y, int dest_w, int dest_h, |
| 119 | int src_x, int src_y, int src_w, int src_h, void* pixels, |
| 120 | const BITMAPINFO* bitmap_info) { |
| 121 | // When blitting a rectangle that touches the bottom, left corner of the |
| 122 | // bitmap, StretchDIBits looks at it top-down! For more details, see |
| 123 | // http://wiki.allegro.cc/index.php?title=StretchDIBits. |
| 124 | int rv; |
| 125 | int bitmap_h = -bitmap_info->bmiHeader.biHeight; |
| 126 | int bottom_up_src_y = bitmap_h - src_y - src_h; |
| 127 | if (bottom_up_src_y == 0 && src_x == 0 && src_h != bitmap_h) { |
| 128 | rv = ::StretchDIBits(hdc, |
| 129 | dest_x, dest_h + dest_y - 1, dest_w, -dest_h, |
| 130 | src_x, bitmap_h - src_y + 1, src_w, -src_h, |
| 131 | pixels, bitmap_info, DIB_RGB_COLORS, SRCCOPY); |
| 132 | } else { |
| 133 | rv = ::StretchDIBits(hdc, |
| 134 | dest_x, dest_y, dest_w, dest_h, |
| 135 | src_x, bottom_up_src_y, src_w, src_h, |
| 136 | pixels, bitmap_info, DIB_RGB_COLORS, SRCCOPY); |
| 137 | } |
thakis | 6ef917b | 2015-12-10 19:29:57 | [diff] [blame] | 138 | DCHECK(rv != static_cast<int>(GDI_ERROR)); |
[email protected] | b4f0016 | 2013-03-09 02:51:14 | [diff] [blame] | 139 | } |
| 140 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 141 | } // namespace gfx |