blob: 24000aef13ea4acd5fed7a590bf8de37c60fe0ac [file] [log] [blame]
[email protected]1a30dd32012-01-28 00:56:431// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]08397d52011-02-05 01:53:385#include "ui/gfx/gdi_util.h"
initial.commitd7cae122008-07-26 21:49:386
avic89eb8d42015-12-23 08:08:187#include <stddef.h>
8
dcheng0917ec42015-11-19 07:00:209#include <algorithm>
danakj25c52c32016-04-12 21:51:0810#include <memory>
dcheng0917ec42015-11-19 07:00:2011
[email protected]b4f00162013-03-09 02:51:1412#include "base/logging.h"
[email protected]82d74e92012-07-18 17:11:5513
pkasting7bc277b2014-10-13 20:58:3914namespace {
initial.commitd7cae122008-07-26 21:49:3815
pkasting7bc277b2014-10-13 20:58:3916void CreateBitmapHeaderWithColorDepth(LONG width,
17 LONG height,
18 WORD color_depth,
initial.commitd7cae122008-07-26 21:49:3819 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
pkasting7bc277b2014-10-13 20:58:3934} // namespace
35
36namespace gfx {
37
38void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) {
39 CreateBitmapHeaderWithColorDepth(width, height, 32, hdr);
40}
41
initial.commitd7cae122008-07-26 21:49:3842void 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.
59void CreateMonochromeBitmapHeader(int width,
60 int height,
61 BITMAPINFOHEADER* hdr) {
pkasting7bc277b2014-10-13 20:58:3962 CreateBitmapHeaderWithColorDepth(width, height, 1, hdr);
initial.commitd7cae122008-07-26 21:49:3863}
64
[email protected]8a2820a2008-10-09 21:58:0565void 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]82d74e92012-07-18 17:11:5581HRGN ConvertPathToHRGN(const gfx::Path& path) {
[email protected]82d74e92012-07-18 17:11:5582 int point_count = path.getPoints(NULL, 0);
danakj25c52c32016-04-12 21:51:0883 std::unique_ptr<SkPoint[]> points(new SkPoint[point_count]);
[email protected]82d74e92012-07-18 17:11:5584 path.getPoints(points.get(), point_count);
danakj25c52c32016-04-12 21:51:0885 std::unique_ptr<POINT[]> windows_points(new POINT[point_count]);
[email protected]82d74e92012-07-18 17:11:5586 for (int i = 0; i < point_count; ++i) {
[email protected]7839ade92014-01-06 21:49:3887 windows_points[i].x = SkScalarRoundToInt(points[i].fX);
88 windows_points[i].y = SkScalarRoundToInt(points[i].fY);
[email protected]82d74e92012-07-18 17:11:5589 }
90
91 return ::CreatePolygonRgn(windows_points.get(), point_count, ALTERNATE);
[email protected]82d74e92012-07-18 17:11:5592}
93
94
pkasting46a62912014-10-09 21:30:0095float CalculatePageScale(HDC dc, int page_width, int page_height) {
[email protected]1a30dd32012-01-28 00:56:4396 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
pkasting46a62912014-10-09 21:30:00103 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]1a30dd32012-01-28 00:56:43107 return std::min(x_factor, y_factor);
108}
109
110// Apply scaling to the DC.
pkasting46a62912014-10-09 21:30:00111bool ScaleDC(HDC dc, float scale_factor) {
[email protected]1a30dd32012-01-28 00:56:43112 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]b4f00162013-03-09 02:51:14118void 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 }
thakis6ef917b2015-12-10 19:29:57138 DCHECK(rv != static_cast<int>(GDI_ERROR));
[email protected]b4f00162013-03-09 02:51:14139}
140
initial.commitd7cae122008-07-26 21:49:38141} // namespace gfx