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 "ui/gfx/gdi_util.h" | 5 #include "ui/gfx/gdi_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 | 13 #include "skia/ext/skia_utils_win.h" |
14 namespace { | |
15 | |
16 void CreateBitmapHeaderWithColorDepth(LONG width, | |
17 LONG height, | |
18 WORD color_depth, | |
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 | |
34 } // namespace | |
35 | 14 |
36 namespace gfx { | 15 namespace gfx { |
37 | 16 |
38 void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) { | |
39 CreateBitmapHeaderWithColorDepth(width, height, 32, hdr); | |
40 } | |
41 | |
42 void CreateBitmapV4Header(int width, int height, BITMAPV4HEADER* hdr) { | 17 void CreateBitmapV4Header(int width, int height, BITMAPV4HEADER* hdr) { |
43 // Because bmp v4 header is just an extension, we just create a v3 header and | 18 // Because bmp v4 header is just an extension, we just create a v3 header and |
44 // copy the bits over to the v4 header. | 19 // copy the bits over to the v4 header. |
45 BITMAPINFOHEADER header_v3; | 20 BITMAPINFOHEADER header_v3; |
46 CreateBitmapHeader(width, height, &header_v3); | 21 skia::CreateBitmapHeader(width, height, &header_v3); |
47 memset(hdr, 0, sizeof(BITMAPV4HEADER)); | 22 memset(hdr, 0, sizeof(BITMAPV4HEADER)); |
48 memcpy(hdr, &header_v3, sizeof(BITMAPINFOHEADER)); | 23 memcpy(hdr, &header_v3, sizeof(BITMAPINFOHEADER)); |
49 | 24 |
50 // Correct the size of the header and fill in the mask values. | 25 // Correct the size of the header and fill in the mask values. |
51 hdr->bV4Size = sizeof(BITMAPV4HEADER); | 26 hdr->bV4Size = sizeof(BITMAPV4HEADER); |
52 hdr->bV4RedMask = 0x00ff0000; | 27 hdr->bV4RedMask = 0x00ff0000; |
53 hdr->bV4GreenMask = 0x0000ff00; | 28 hdr->bV4GreenMask = 0x0000ff00; |
54 hdr->bV4BlueMask = 0x000000ff; | 29 hdr->bV4BlueMask = 0x000000ff; |
55 hdr->bV4AlphaMask = 0xff000000; | 30 hdr->bV4AlphaMask = 0xff000000; |
56 } | 31 } |
57 | 32 |
58 // Creates a monochrome bitmap header. | |
59 void CreateMonochromeBitmapHeader(int width, | |
60 int height, | |
61 BITMAPINFOHEADER* hdr) { | |
62 CreateBitmapHeaderWithColorDepth(width, height, 1, hdr); | |
63 } | |
64 | |
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 | |
81 HRGN ConvertPathToHRGN(const gfx::Path& path) { | |
82 int point_count = path.getPoints(NULL, 0); | |
83 std::unique_ptr<SkPoint[]> points(new SkPoint[point_count]); | |
84 path.getPoints(points.get(), point_count); | |
85 std::unique_ptr<POINT[]> windows_points(new POINT[point_count]); | |
86 for (int i = 0; i < point_count; ++i) { | |
87 windows_points[i].x = SkScalarRoundToInt(points[i].fX); | |
88 windows_points[i].y = SkScalarRoundToInt(points[i].fY); | |
89 } | |
90 | |
91 return ::CreatePolygonRgn(windows_points.get(), point_count, ALTERNATE); | |
92 } | |
93 | |
94 | |
95 float CalculatePageScale(HDC dc, int page_width, int page_height) { | 33 float CalculatePageScale(HDC dc, int page_width, int page_height) { |
96 int dc_width = GetDeviceCaps(dc, HORZRES); | 34 int dc_width = GetDeviceCaps(dc, HORZRES); |
97 int dc_height = GetDeviceCaps(dc, VERTRES); | 35 int dc_height = GetDeviceCaps(dc, VERTRES); |
98 | 36 |
99 // If page fits DC - no scaling needed. | 37 // If page fits DC - no scaling needed. |
100 if (dc_width >= page_width && dc_height >= page_height) | 38 if (dc_width >= page_width && dc_height >= page_height) |
101 return 1.0; | 39 return 1.0; |
102 | 40 |
103 float x_factor = | 41 float x_factor = |
104 static_cast<float>(dc_width) / static_cast<float>(page_width); | 42 static_cast<float>(dc_width) / static_cast<float>(page_width); |
(...skipping 27 matching lines...) Expand all Loading... |
132 } else { | 70 } else { |
133 rv = ::StretchDIBits(hdc, | 71 rv = ::StretchDIBits(hdc, |
134 dest_x, dest_y, dest_w, dest_h, | 72 dest_x, dest_y, dest_w, dest_h, |
135 src_x, bottom_up_src_y, src_w, src_h, | 73 src_x, bottom_up_src_y, src_w, src_h, |
136 pixels, bitmap_info, DIB_RGB_COLORS, SRCCOPY); | 74 pixels, bitmap_info, DIB_RGB_COLORS, SRCCOPY); |
137 } | 75 } |
138 DCHECK(rv != static_cast<int>(GDI_ERROR)); | 76 DCHECK(rv != static_cast<int>(GDI_ERROR)); |
139 } | 77 } |
140 | 78 |
141 } // namespace gfx | 79 } // namespace gfx |
OLD | NEW |